1a8e1175bSopenharmony_ciThis document explains how to create builds of Mbed TLS where some 2a8e1175bSopenharmony_cicryptographic mechanisms are provided only by PSA drivers (that is, no 3a8e1175bSopenharmony_cibuilt-in implementation of those algorithms), from a user's perspective. 4a8e1175bSopenharmony_ci 5a8e1175bSopenharmony_ciThis is useful to save code size for people who are using either a hardware 6a8e1175bSopenharmony_ciaccelerator, or an alternative software implementation that is more 7a8e1175bSopenharmony_ciaggressively optimized for code size than the default one in Mbed TLS. 8a8e1175bSopenharmony_ci 9a8e1175bSopenharmony_ciGeneral considerations 10a8e1175bSopenharmony_ci---------------------- 11a8e1175bSopenharmony_ci 12a8e1175bSopenharmony_ciThis document assumes that you already have a working driver. 13a8e1175bSopenharmony_ciOtherwise, please see the [PSA driver example and 14a8e1175bSopenharmony_ciguide](psa-driver-example-and-guide.md) for information on writing a 15a8e1175bSopenharmony_cidriver. 16a8e1175bSopenharmony_ci 17a8e1175bSopenharmony_ciIn order to have some mechanism provided only by a driver, you'll want 18a8e1175bSopenharmony_cithe following compile-time configuration options enabled: 19a8e1175bSopenharmony_ci 20a8e1175bSopenharmony_ci- `MBEDTLS_PSA_CRYPTO_C` (enabled by default) - this enables PSA Crypto. 21a8e1175bSopenharmony_ci- `MBEDTLS_USE_PSA_CRYPTO` (disabled by default) - this makes PK, X.509 and 22a8e1175bSopenharmony_ci TLS use PSA Crypto. You need to enable this if you're using PK, X.509 or TLS 23a8e1175bSopenharmony_ciand want them to have access to the algorithms provided by your driver. (See 24a8e1175bSopenharmony_ci[the dedicated document](use-psa-crypto.md) for details.) 25a8e1175bSopenharmony_ci- `MBEDTLS_PSA_CRYPTO_CONFIG` (disabled by default) - this enables 26a8e1175bSopenharmony_ci configuration of cryptographic algorithms using `PSA_WANT` macros in 27a8e1175bSopenharmony_ci`include/psa/crypto_config.h`. See [Conditional inclusion of cryptographic 28a8e1175bSopenharmony_cimechanism through the PSA API in Mbed 29a8e1175bSopenharmony_ciTLS](proposed/psa-conditional-inclusion-c.md) for details. 30a8e1175bSopenharmony_ci 31a8e1175bSopenharmony_ciIn addition, for each mechanism you want provided only by your driver: 32a8e1175bSopenharmony_ci 33a8e1175bSopenharmony_ci- Define the corresponding `PSA_WANT` macro in `psa/crypto_config.h` - this 34a8e1175bSopenharmony_ci means the algorithm will be available in the PSA Crypto API. 35a8e1175bSopenharmony_ci- Define the corresponding `MBEDTLS_PSA_ACCEL` in your build. This could be 36a8e1175bSopenharmony_ci defined in `psa/crypto_config.h` or your compiler's command line. This 37a8e1175bSopenharmony_ciinforms the PSA code that an accelerator is available for this mechanism. 38a8e1175bSopenharmony_ci- Undefine / comment out the corresponding `MBEDTLS_xxx_C` macro in 39a8e1175bSopenharmony_ci `mbedtls/mbedtls_config.h`. This ensures the built-in implementation is not 40a8e1175bSopenharmony_ciincluded in the build. 41a8e1175bSopenharmony_ci 42a8e1175bSopenharmony_ciFor example, if you want SHA-256 to be provided only by a driver, you'll want 43a8e1175bSopenharmony_ci`PSA_WANT_ALG_SHA_256` and `MBEDTLS_PSA_ACCEL_SHA_256` defined, and 44a8e1175bSopenharmony_ci`MBEDTLS_SHA256_C` undefined. 45a8e1175bSopenharmony_ci 46a8e1175bSopenharmony_ciIn addition to these compile-time considerations, at runtime you'll need to 47a8e1175bSopenharmony_cimake sure you call `psa_crypto_init()` before any function that uses the 48a8e1175bSopenharmony_cidriver-only mechanisms. Note that this is already a requirement for any use of 49a8e1175bSopenharmony_cithe PSA Crypto API, as well as for use of the PK, X.509 and TLS modules when 50a8e1175bSopenharmony_ci`MBEDTLS_USE_PSA_CRYPTO` is enabled, so in most cases your application will 51a8e1175bSopenharmony_cialready be doing this. 52a8e1175bSopenharmony_ci 53a8e1175bSopenharmony_ciMechanisms covered 54a8e1175bSopenharmony_ci------------------ 55a8e1175bSopenharmony_ci 56a8e1175bSopenharmony_ciFor now, only the following (families of) mechanisms are supported: 57a8e1175bSopenharmony_ci 58a8e1175bSopenharmony_ci- hashes: SHA-3, SHA-2, SHA-1, MD5, etc. 59a8e1175bSopenharmony_ci- elliptic-curve cryptography (ECC): ECDH, ECDSA, EC J-PAKE, ECC key types. 60a8e1175bSopenharmony_ci- finite-field Diffie-Hellman: FFDH algorithm, DH key types. 61a8e1175bSopenharmony_ci- RSA: PKCS#1 v1.5 and v2.1 signature and encryption algorithms, RSA key types 62a8e1175bSopenharmony_ci (for now, only crypto, no X.509 or TLS support). 63a8e1175bSopenharmony_ci- AEADs: 64a8e1175bSopenharmony_ci - GCM and CCM with AES, ARIA and Camellia key types 65a8e1175bSopenharmony_ci - ChachaPoly with ChaCha20 Key type 66a8e1175bSopenharmony_ci- Unauthenticated ciphers: 67a8e1175bSopenharmony_ci - key types: AES, ARIA, Camellia, DES 68a8e1175bSopenharmony_ci - modes: ECB, CBC, CTR, CFB, OFB, XTS 69a8e1175bSopenharmony_ci 70a8e1175bSopenharmony_ciFor each family listed above, all the mentioned alorithms/key types are also 71a8e1175bSopenharmony_ciall the mechanisms that exist in PSA API. 72a8e1175bSopenharmony_ci 73a8e1175bSopenharmony_ciSupported means that when those are provided only by drivers, everything 74a8e1175bSopenharmony_ci(including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should 75a8e1175bSopenharmony_ciwork in the same way as if the mechanisms where built-in, except as documented 76a8e1175bSopenharmony_ciin the "Limitations" sub-sections of the sections dedicated to each family 77a8e1175bSopenharmony_cibelow. 78a8e1175bSopenharmony_ci 79a8e1175bSopenharmony_ciHashes 80a8e1175bSopenharmony_ci------ 81a8e1175bSopenharmony_ci 82a8e1175bSopenharmony_ciIt is possible to have all hash operations provided only by a driver. 83a8e1175bSopenharmony_ci 84a8e1175bSopenharmony_ciMore precisely: 85a8e1175bSopenharmony_ci 86a8e1175bSopenharmony_ci- you can enable `PSA_WANT_ALG_SHA_256` without `MBEDTLS_SHA256_C`, provided 87a8e1175bSopenharmony_ci you have `MBEDTLS_PSA_ACCEL_ALG_SHA_256` enabled; 88a8e1175bSopenharmony_ci- and similarly for all supported hash algorithms: `MD5`, `RIPEMD160`, 89a8e1175bSopenharmony_ci `SHA_1`, `SHA_224`, `SHA_256`, `SHA_384`, `SHA_512`, `SHA3_224`, `SHA3_256`, 90a8e1175bSopenharmony_ci`SHA3_384`, `SHA3_512`. 91a8e1175bSopenharmony_ci 92a8e1175bSopenharmony_ciIn such a build, all crypto operations (via the PSA Crypto API, or non-PSA 93a8e1175bSopenharmony_ciAPIs), as well as X.509 and TLS, will work as usual, except that direct calls 94a8e1175bSopenharmony_cito low-level hash APIs (`mbedtls_sha256()` etc.) are not possible for the 95a8e1175bSopenharmony_cimodules that are disabled. 96a8e1175bSopenharmony_ci 97a8e1175bSopenharmony_ciYou need to call `psa_crypto_init()` before any crypto operation that uses 98a8e1175bSopenharmony_cia hash algorithm that is provided only by a driver, as mentioned in [General 99a8e1175bSopenharmony_ciconsiderations](#general-considerations) above. 100a8e1175bSopenharmony_ci 101a8e1175bSopenharmony_ciIf you want to check at compile-time whether a certain hash algorithm is 102a8e1175bSopenharmony_ciavailable in the present build of Mbed TLS, regardless of whether it's 103a8e1175bSopenharmony_ciprovided by a driver or built-in, you should use the following macros: 104a8e1175bSopenharmony_ci 105a8e1175bSopenharmony_ci- for code that uses only the PSA Crypto API: `PSA_WANT_ALG_xxx` from 106a8e1175bSopenharmony_ci `psa/crypto.h`; 107a8e1175bSopenharmony_ci- for code that uses non-PSA crypto APIs: `MBEDTLS_MD_CAN_xxx` from 108a8e1175bSopenharmony_ci `mbedtls/config_adjust_legacy_crypto.h`. 109a8e1175bSopenharmony_ci 110a8e1175bSopenharmony_ci### HMAC 111a8e1175bSopenharmony_ci 112a8e1175bSopenharmony_ciIn addition to accelerated hash operations, it is also possible to accelerate 113a8e1175bSopenharmony_ciHMAC by enabling and accelerating: 114a8e1175bSopenharmony_ci- HMAC algorithm and key type, i.e. `[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_HMAC` and 115a8e1175bSopenharmony_ci `[PSA_WANT|MBEDTLS_PSA_ACCEL]KEY_TYPE_HMAC`. 116a8e1175bSopenharmony_ci- Required hash algorithm(s) as explained in [Hashes](#hashes) section. 117a8e1175bSopenharmony_ci 118a8e1175bSopenharmony_ciIn such a build it is possible to disable legacy HMAC support by disabling 119a8e1175bSopenharmony_ci`MBEDTLS_MD_C` and still getting crypto operations, X.509 and TLS to work as 120a8e1175bSopenharmony_ciusual. Exceptions are: 121a8e1175bSopenharmony_ci- As mentioned in [Hashes](#hashes) direct calls to legacy lo-level hash APIs 122a8e1175bSopenharmony_ci (`mbedtls_sha256()` etc.) will not be possible for the legacy modules that 123a8e1175bSopenharmony_ci are disabled. 124a8e1175bSopenharmony_ci- Legacy HMAC support (`mbedtls_md_hmac_xxx()`) won't be possible. 125a8e1175bSopenharmony_ci- `MBEDTLS_PKCS[5|7]_C`, `MBEDTLS_HMAC_DRBG_C` and `MBEDTLS_HKDF_C` since they 126a8e1175bSopenharmony_ci depend on the legacy implementation of HMAC. 127a8e1175bSopenharmony_ci - disabling HMAC_DRBG_C cause deterministic ECDSA (i.e. 128a8e1175bSopenharmony_ci `MBEDTLS_DETERMINISTIC_ECDSA` on the legacy side and 129a8e1175bSopenharmony_ci `PSA_WANT_ALG_DETERMINISTIC_ECDSA` on the PSA one) to be not available. 130a8e1175bSopenharmony_ci 131a8e1175bSopenharmony_ciElliptic-curve cryptography (ECC) 132a8e1175bSopenharmony_ci--------------------------------- 133a8e1175bSopenharmony_ci 134a8e1175bSopenharmony_ciIt is possible to have most ECC operations provided only by a driver: 135a8e1175bSopenharmony_ci 136a8e1175bSopenharmony_ci- the ECDH, ECDSA and EC J-PAKE algorithms; 137a8e1175bSopenharmony_ci- key import, export, and random generation. 138a8e1175bSopenharmony_ci 139a8e1175bSopenharmony_ciMore precisely, if: 140a8e1175bSopenharmony_ci 141a8e1175bSopenharmony_ci- you have driver support for ECC public and using private keys (that is, 142a8e1175bSopenharmony_ci`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY` and 143a8e1175bSopenharmony_ci`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_BASIC` are enabled), and 144a8e1175bSopenharmony_ci- you have driver support for all ECC curves that are enabled (that is, for 145a8e1175bSopenharmony_ci each `PSA_WANT_ECC_xxx` macro enabled, the corresponding 146a8e1175bSopenharmony_ci`MBEDTLS_PSA_ACCEL_ECC_xxx` macros is enabled as well); 147a8e1175bSopenharmony_ci 148a8e1175bSopenharmony_cithen you can: 149a8e1175bSopenharmony_ci 150a8e1175bSopenharmony_ci- enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C`, provided 151a8e1175bSopenharmony_ci `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled 152a8e1175bSopenharmony_ci- enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C`, provided 153a8e1175bSopenharmony_ci `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled; 154a8e1175bSopenharmony_ci- enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C`, provided 155a8e1175bSopenharmony_ci `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled. 156a8e1175bSopenharmony_ci 157a8e1175bSopenharmony_ciIn addition, if: 158a8e1175bSopenharmony_ci 159a8e1175bSopenharmony_ci- none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, `MBEDTLS_ECJPAKE_C` are enabled 160a8e1175bSopenharmony_ci (see conditions above), and 161a8e1175bSopenharmony_ci- you have driver support for all enabled ECC key pair operations - that is, 162a8e1175bSopenharmony_ci for each `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx` macro enabled, the 163a8e1175bSopenharmony_cicorresponding `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_xxx` macros is also 164a8e1175bSopenharmony_cienabled, 165a8e1175bSopenharmony_ci 166a8e1175bSopenharmony_cithen you can also disable `MBEDTLS_ECP_C`. However, a small subset of it might 167a8e1175bSopenharmony_cistill be included in the build, see limitations sub-section below. 168a8e1175bSopenharmony_ci 169a8e1175bSopenharmony_ciIn addition, if: 170a8e1175bSopenharmony_ci 171a8e1175bSopenharmony_ci- `MBEDTLS_ECP_C` is fully removed (see limitation sub-section below), 172a8e1175bSopenharmony_ci- and support for RSA key types and algorithms is either fully disabled or 173a8e1175bSopenharmony_ci fully provided by a driver, 174a8e1175bSopenharmony_ci- and support for DH key types and the FFDH algorithm is either disabled or 175a8e1175bSopenharmony_ci fully provided by a driver, 176a8e1175bSopenharmony_ci 177a8e1175bSopenharmony_cithen you can also disable `MBEDTLS_BIGNUM_C`. 178a8e1175bSopenharmony_ci 179a8e1175bSopenharmony_ciIn such builds, all crypto operations via the PSA Crypto API will work as 180a8e1175bSopenharmony_ciusual, as well as the PK, X.509 and TLS modules if `MBEDTLS_USE_PSA_CRYPTO` is 181a8e1175bSopenharmony_cienabled, with the following exceptions: 182a8e1175bSopenharmony_ci 183a8e1175bSopenharmony_ci- direct calls to APIs from the disabled modules are not possible; 184a8e1175bSopenharmony_ci- PK, X.509 and TLS will not support restartable ECC operations (see 185a8e1175bSopenharmony_ci limitation sub-section below). 186a8e1175bSopenharmony_ci 187a8e1175bSopenharmony_ciIf you want to check at compile-time whether a certain curve is available in 188a8e1175bSopenharmony_cithe present build of Mbed TLS, regardless of whether ECC is provided by a 189a8e1175bSopenharmony_cidriver or built-in, you should use the following macros: 190a8e1175bSopenharmony_ci 191a8e1175bSopenharmony_ci- for code that uses only the PSA Crypto API: `PSA_WANT_ECC_xxx` from 192a8e1175bSopenharmony_ci `psa/crypto.h`; 193a8e1175bSopenharmony_ci- for code that may also use non-PSA crypto APIs: `MBEDTLS_ECP_HAVE_xxx` from 194a8e1175bSopenharmony_ci `mbedtls/build_info.h` where xxx can take the same values as for 195a8e1175bSopenharmony_ci`MBEDTLS_ECP_DP_xxx` macros. 196a8e1175bSopenharmony_ci 197a8e1175bSopenharmony_ciNote that for externally-provided drivers, the integrator is responsible for 198a8e1175bSopenharmony_ciensuring the appropriate `MBEDTLS_PSA_ACCEL_xxx` macros are defined. However, 199a8e1175bSopenharmony_cifor the p256-m driver that's provided with the library, those macros are 200a8e1175bSopenharmony_ciautomatically defined when enabling `MBEDTLS_PSA_P256M_DRIVER_ENABLED`. 201a8e1175bSopenharmony_ci 202a8e1175bSopenharmony_ci### Limitations regarding fully removing `ecp.c` 203a8e1175bSopenharmony_ci 204a8e1175bSopenharmony_ciA limited subset of `ecp.c` will still be automatically re-enabled if any of 205a8e1175bSopenharmony_cithe following is enabled: 206a8e1175bSopenharmony_ci 207a8e1175bSopenharmony_ci- `MBEDTLS_PK_PARSE_EC_COMPRESSED` - support for parsing ECC keys where the 208a8e1175bSopenharmony_ci public part is in compressed format; 209a8e1175bSopenharmony_ci- `MBEDTLS_PK_PARSE_EC_EXTENDED` - support for parsing ECC keys where the 210a8e1175bSopenharmony_ci curve is identified not by name, but by explicit parameters; 211a8e1175bSopenharmony_ci- `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE` - support for deterministic 212a8e1175bSopenharmony_ci derivation of an ECC keypair with `psa_key_derivation_output_key()`. 213a8e1175bSopenharmony_ci 214a8e1175bSopenharmony_ciNote: when any of the above options is enabled, a subset of `ecp.c` will 215a8e1175bSopenharmony_ciautomatically be included in the build in order to support it. Therefore 216a8e1175bSopenharmony_ciyou can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will 217a8e1175bSopenharmony_ciresult in some code size savings, but not as much as when none of the 218a8e1175bSopenharmony_ciabove features are enabled. 219a8e1175bSopenharmony_ci 220a8e1175bSopenharmony_ciWe do have plans to support each of these with `ecp.c` fully removed in the 221a8e1175bSopenharmony_cifuture, however there is no established timeline. If you're interested, please 222a8e1175bSopenharmony_cilet us know, so we can take it into consideration in our planning. 223a8e1175bSopenharmony_ci 224a8e1175bSopenharmony_ci### Limitations regarding restartable / interruptible ECC operations 225a8e1175bSopenharmony_ci 226a8e1175bSopenharmony_ciAt the moment, there is no driver support for interruptible operations 227a8e1175bSopenharmony_ci(see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a 228a8e1175bSopenharmony_ciconsequence these are not supported in builds without `MBEDTLS_ECDSA_C`. 229a8e1175bSopenharmony_ci 230a8e1175bSopenharmony_ciSimilarly, there is no PSA support for interruptible ECDH operations so these 231a8e1175bSopenharmony_ciare not supported without `ECDH_C`. See also limitations regarding 232a8e1175bSopenharmony_cirestartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its 233a8e1175bSopenharmony_cidocumentation](use-psa-crypto.md). 234a8e1175bSopenharmony_ci 235a8e1175bSopenharmony_ciAgain, we have plans to support this in the future but not with an established 236a8e1175bSopenharmony_citimeline, please let us know if you're interested. 237a8e1175bSopenharmony_ci 238a8e1175bSopenharmony_ci### Limitations regarding "mixed" builds (driver and built-in) 239a8e1175bSopenharmony_ci 240a8e1175bSopenharmony_ciIn order for a build to be driver-only (no built-in implementation), all the 241a8e1175bSopenharmony_cirequested algorithms, key types (key operations) and curves must be 242a8e1175bSopenharmony_ciaccelerated (plus a few other restrictions, see "Limitations regarding fully 243a8e1175bSopenharmony_ciremoving `ecp.c`" above). However, what if you have an accelerator that only 244a8e1175bSopenharmony_cisupports some algorithms, some key types (key operations), or some curves, but 245a8e1175bSopenharmony_ciwant to have more enabled in you build? 246a8e1175bSopenharmony_ci 247a8e1175bSopenharmony_ciIt is possible to have acceleration for only a subset of the requested 248a8e1175bSopenharmony_cialgorithms. In this case, the built-in implementation of the accelerated 249a8e1175bSopenharmony_cialgorithms will be disabled, provided all the requested curves and key types 250a8e1175bSopenharmony_cithat can be used with this algorithm are also declared as accelerated. 251a8e1175bSopenharmony_ci 252a8e1175bSopenharmony_ciThere is very limited support for having acceleration for only a subset of the 253a8e1175bSopenharmony_cirequested key type operations. The only configuration that's tested is that of 254a8e1175bSopenharmony_cia driver accelerating `PUBLIC_KEY`, `KEY_PAIR_BASIC`, `KEY_PAIR_IMPORT`, 255a8e1175bSopenharmony_ci`KEY_PAIR_EXPORT` but not `KEY_PAIR_GENERATE`. (Note: currently the driver 256a8e1175bSopenharmony_ciinterface does not support `KEY_PAIR_DERIVE`.) 257a8e1175bSopenharmony_ci 258a8e1175bSopenharmony_ciThere is limited support for having acceleration for only a subset of the 259a8e1175bSopenharmony_cirequested curves. In such builds, only the PSA API is currently tested and 260a8e1175bSopenharmony_ciworking; there are known issues in PK, and X.509 and TLS are untested. 261a8e1175bSopenharmony_ci 262a8e1175bSopenharmony_ciFinite-field Diffie-Hellman 263a8e1175bSopenharmony_ci--------------------------- 264a8e1175bSopenharmony_ci 265a8e1175bSopenharmony_ciSupport is pretty similar to the "Elliptic-curve cryptography (ECC)" section 266a8e1175bSopenharmony_ciabove. 267a8e1175bSopenharmony_ciKey management and usage can be enabled by means of the usual `PSA_WANT` + 268a8e1175bSopenharmony_ci`MBEDTLS_PSA_ACCEL` pairs: 269a8e1175bSopenharmony_ci 270a8e1175bSopenharmony_ci- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_PUBLIC_KEY`; 271a8e1175bSopenharmony_ci- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_BASIC`; 272a8e1175bSopenharmony_ci- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_IMPORT`; 273a8e1175bSopenharmony_ci- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_EXPORT`; 274a8e1175bSopenharmony_ci- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_GENERATE`; 275a8e1175bSopenharmony_ci 276a8e1175bSopenharmony_ciThe same holds for the associated algorithm: 277a8e1175bSopenharmony_ci`[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and 278a8e1175bSopenharmony_ciremoving builtin support (i.e. `MBEDTLS_DHM_C`). 279a8e1175bSopenharmony_ci 280a8e1175bSopenharmony_ciRSA 281a8e1175bSopenharmony_ci--- 282a8e1175bSopenharmony_ci 283a8e1175bSopenharmony_ciIt is possible for all RSA operations to be provided only by a driver. 284a8e1175bSopenharmony_ci 285a8e1175bSopenharmony_ciMore precisely, if: 286a8e1175bSopenharmony_ci 287a8e1175bSopenharmony_ci- all the RSA algorithms that are enabled (`PSA_WANT_ALG_RSA_*`) are also 288a8e1175bSopenharmony_ci accelerated (`MBEDTLS_PSA_ACCEL_ALG_RSA_*`), 289a8e1175bSopenharmony_ci- and all the RSA key types that are enabled (`PSA_WANT_KEY_TYPE_RSA_*`) are 290a8e1175bSopenharmony_ci also accelerated (`MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_*`), 291a8e1175bSopenharmony_ci 292a8e1175bSopenharmony_cithen you can disable `MBEDTLS_RSA_C`, `MBEDTLS_PKCS1_V15` and 293a8e1175bSopenharmony_ci`MBEDTLS_PKCS1_V21`, and RSA will still work in PSA Crypto. 294a8e1175bSopenharmony_ci 295a8e1175bSopenharmony_ci### Limitations on RSA acceleration 296a8e1175bSopenharmony_ci 297a8e1175bSopenharmony_ciUnlike other mechanisms, for now in configurations with driver-only RSA, only 298a8e1175bSopenharmony_ciPSA Crypto works. In particular, PK, X.509 and TLS will _not_ work with 299a8e1175bSopenharmony_cidriver-only RSA even if `MBEDTLS_USE_PSA_CRYPTO` is enabled. 300a8e1175bSopenharmony_ci 301a8e1175bSopenharmony_ciCurrently (early 2024) we don't have plans to extend this support. If you're 302a8e1175bSopenharmony_ciinterested in wider driver-only support for RSA, please let us know. 303a8e1175bSopenharmony_ci 304a8e1175bSopenharmony_ciCiphers (unauthenticated and AEAD) 305a8e1175bSopenharmony_ci---------------------------------- 306a8e1175bSopenharmony_ci 307a8e1175bSopenharmony_ciIt is possible to have all ciphers and AEAD operations provided only by a 308a8e1175bSopenharmony_cidriver. More precisely, for each desired combination of key type and 309a8e1175bSopenharmony_cialgorithm/mode you can: 310a8e1175bSopenharmony_ci 311a8e1175bSopenharmony_ci- Enable desired PSA key type(s): 312a8e1175bSopenharmony_ci - `PSA_WANT_KEY_TYPE_AES`, 313a8e1175bSopenharmony_ci - `PSA_WANT_KEY_TYPE_ARIA`, 314a8e1175bSopenharmony_ci - `PSA_WANT_KEY_TYPE_CAMELLIA`, 315a8e1175bSopenharmony_ci - `PSA_WANT_KEY_TYPE_CHACHA20`, 316a8e1175bSopenharmony_ci - `PSA_WANT_KEY_TYPE_DES`. 317a8e1175bSopenharmony_ci- Enable desired PSA algorithm(s): 318a8e1175bSopenharmony_ci - Unauthenticated ciphers modes: 319a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CBC_NO_PADDING`, 320a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CBC_PKCS7`, 321a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CCM_STAR_NO_TAG`, 322a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CFB`, 323a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CTR`, 324a8e1175bSopenharmony_ci - `PSA_WANT_ALG_ECB_NO_PADDING`, 325a8e1175bSopenharmony_ci - `PSA_WANT_ALG_OFB`, 326a8e1175bSopenharmony_ci - `PSA_WANT_ALG_STREAM_CIPHER`. 327a8e1175bSopenharmony_ci - AEADs: 328a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CCM`, 329a8e1175bSopenharmony_ci - `PSA_WANT_ALG_GCM`, 330a8e1175bSopenharmony_ci - `PSA_WANT_ALG_CHACHA20_POLY1305`. 331a8e1175bSopenharmony_ci- Enable `MBEDTLS_PSA_ACCEL_[KEY_TYPE_xxx|ALG_yyy]` symbol(s) which correspond 332a8e1175bSopenharmony_ci to the `PSA_WANT_KEY_TYPE_xxx` and `PSA_WANT_ALG_yyy` of the previous steps. 333a8e1175bSopenharmony_ci- Disable builtin support of key types: 334a8e1175bSopenharmony_ci - `MBEDTLS_AES_C`, 335a8e1175bSopenharmony_ci - `MBEDTLS_ARIA_C`, 336a8e1175bSopenharmony_ci - `MBEDTLS_CAMELLIA_C`, 337a8e1175bSopenharmony_ci - `MBEDTLS_DES_C`, 338a8e1175bSopenharmony_ci - `MBEDTLS_CHACHA20_C`. 339a8e1175bSopenharmony_ci and algorithms/modes: 340a8e1175bSopenharmony_ci - `MBEDTLS_CBC_C`, 341a8e1175bSopenharmony_ci - `MBEDTLS_CFB_C`, 342a8e1175bSopenharmony_ci - `MBEDTLS_CTR_C`, 343a8e1175bSopenharmony_ci - `MBEDTLS_OFB_C`, 344a8e1175bSopenharmony_ci - `MBEDTLS_XTS_C`, 345a8e1175bSopenharmony_ci - `MBEDTLS_CCM_C`, 346a8e1175bSopenharmony_ci - `MBEDTLS_GCM_C`, 347a8e1175bSopenharmony_ci - `MBEDTLS_CHACHAPOLY_C`, 348a8e1175bSopenharmony_ci - `MBEDTLS_NULL_CIPHER`. 349a8e1175bSopenharmony_ci 350a8e1175bSopenharmony_ciOnce a key type and related algorithm are accelerated, all the PSA Crypto APIs 351a8e1175bSopenharmony_ciwill work, as well as X.509 and TLS (with `MBEDTLS_USE_PSA_CRYPTO` enabled) but 352a8e1175bSopenharmony_cisome non-PSA APIs will be absent or have reduced functionality, see 353a8e1175bSopenharmony_ci[Restrictions](#restrictions) for details. 354a8e1175bSopenharmony_ci 355a8e1175bSopenharmony_ci### Restrictions 356a8e1175bSopenharmony_ci 357a8e1175bSopenharmony_ci- If an algorithm other than CCM and GCM (see 358a8e1175bSopenharmony_ci ["Partial acceleration for CCM/GCM"](#partial-acceleration-for-ccmgcm) below) 359a8e1175bSopenharmony_ci is enabled but not accelerated, then all key types that can be used with it 360a8e1175bSopenharmony_ci will need to be built-in. 361a8e1175bSopenharmony_ci- If a key type is enabled but not accelerated, then all algorithms that can be 362a8e1175bSopenharmony_ci used with it will need to be built-in. 363a8e1175bSopenharmony_ci 364a8e1175bSopenharmony_ciSome legacy modules can't take advantage of PSA drivers yet, and will either 365a8e1175bSopenharmony_cineed to be disabled, or have reduced features when the built-in implementations 366a8e1175bSopenharmony_ciof some ciphers are removed: 367a8e1175bSopenharmony_ci 368a8e1175bSopenharmony_ci- `MBEDTLS_NIST_KW_C` needs built-in AES: it must be disabled when 369a8e1175bSopenharmony_ci `MBEDTLS_AES_C` is disabled. 370a8e1175bSopenharmony_ci- `MBEDTLS_CMAC_C` needs built-in AES/DES: it must be disabled when 371a8e1175bSopenharmony_ci `MBEDTLS_AES_C` and `MBEDTLS_DES_C` are both disabled. When only one of them 372a8e1175bSopenharmony_ci is enabled, then only the corresponding cipher will be available at runtime 373a8e1175bSopenharmony_ci for use with `mbedtls_cipher_cmac_xxx`. (Note: if there is driver support for 374a8e1175bSopenharmony_ci CMAC and all compatible key types, then `PSA_WANT_ALG_CMAC` can be enabled 375a8e1175bSopenharmony_ci without `MBEDTLS_CMAC_C` and CMAC will be usable with `psa_max_xxx` APIs.) 376a8e1175bSopenharmony_ci- `MBEDTLS_CIPHER_C`: the `mbedtls_cipher_xxx()` APIs will only work with 377a8e1175bSopenharmony_ci ciphers that are built-in - that is, both the underlying cipher 378a8e1175bSopenharmony_ci (eg `MBEDTLS_AES_C`) and the mode (eg `MBEDTLS_CIPHER_MODE_CBC` or 379a8e1175bSopenharmony_ci `MBEDTLS_GCM_C`). 380a8e1175bSopenharmony_ci- `MBEDTLS_PKCS5_C`: encryption/decryption (PBES2, PBE) will only work with 381a8e1175bSopenharmony_ci ciphers that are built-in. 382a8e1175bSopenharmony_ci- PEM decryption will only work with ciphers that are built-in. 383a8e1175bSopenharmony_ci- PK parse will only be able to parse encrypted keys using built-in ciphers. 384a8e1175bSopenharmony_ci 385a8e1175bSopenharmony_ciNote that if you also disable `MBEDTLS_CIPHER_C`, there will be additional 386a8e1175bSopenharmony_cirestrictions, see [Disabling `MBEDTLS_CIPHER_C`](#disabling-mbedtls_cipher_c). 387a8e1175bSopenharmony_ci 388a8e1175bSopenharmony_ci### Legacy <-> PSA matching 389a8e1175bSopenharmony_ci 390a8e1175bSopenharmony_ciNote that the relationship between legacy (i.e. `MBEDTLS_xxx_C`) and PSA 391a8e1175bSopenharmony_ci(i.e. `PSA_WANT_xxx`) symbols is not always 1:1. For example: 392a8e1175bSopenharmony_ci 393a8e1175bSopenharmony_ci- ECB mode is always enabled in the legacy configuration for each key type that 394a8e1175bSopenharmony_ci allows it (AES, ARIA, Camellia, DES), whereas it must be explicitly enabled 395a8e1175bSopenharmony_ci in PSA with `PSA_WANT_ALG_ECB_NO_PADDING`. 396a8e1175bSopenharmony_ci- In the legacy API, `MBEDTLS_CHACHA20_C` enables the ChaCha20 stream cipher, and 397a8e1175bSopenharmony_ci enabling `MBEDTLS_CHACHAPOLY_C` also enables the ChaCha20-Poly1305 AEAD. In the 398a8e1175bSopenharmony_ci PSA API, you need to enable `PSA_KEY_TYPE_CHACHA20` for both, plus 399a8e1175bSopenharmony_ci `PSA_ALG_STREAM_CIPHER` or `PSA_ALG_CHACHA20_POLY1305` as desired. 400a8e1175bSopenharmony_ci- The legacy symbol `MBEDTLS_CCM_C` adds support for both cipher and AEAD, 401a8e1175bSopenharmony_ci whereas in PSA there are 2 different symbols: `PSA_WANT_ALG_CCM_STAR_NO_TAG` 402a8e1175bSopenharmony_ci and `PSA_WANT_ALG_CCM`, respectively. 403a8e1175bSopenharmony_ci 404a8e1175bSopenharmony_ci### Partial acceleration for CCM/GCM 405a8e1175bSopenharmony_ci 406a8e1175bSopenharmony_ci[This section depends on #8598 so it might be updated while that PR progresses.] 407a8e1175bSopenharmony_ci 408a8e1175bSopenharmony_ciIn case legacy CCM/GCM algorithms are enabled, it is still possible to benefit 409a8e1175bSopenharmony_cifrom PSA acceleration of the underlying block cipher by enabling support for 410a8e1175bSopenharmony_ciECB mode (`PSA_WANT_ALG_ECB_NO_PADDING` + `MBEDTLS_PSA_ACCEL_ALG_ECB_NO_PADDING`) 411a8e1175bSopenharmony_citogether with desired key type(s) (`PSA_WANT_KEY_TYPE_[AES|ARIA|CAMELLIA]` + 412a8e1175bSopenharmony_ci`MBEDTLS_PSA_ACCEL_KEY_TYPE_[AES|ARIA|CAMELLIA]`). 413a8e1175bSopenharmony_ci 414a8e1175bSopenharmony_ciIn such configurations it is possible to: 415a8e1175bSopenharmony_ci 416a8e1175bSopenharmony_ci- Use CCM and GCM via the PSA Crypto APIs. 417a8e1175bSopenharmony_ci- Use CCM and GCM via legacy functions `mbedtls_[ccm|gcm]_xxx()` (but not the 418a8e1175bSopenharmony_ci legacy functions `mbedtls_cipher_xxx()`). 419a8e1175bSopenharmony_ci- Disable legacy key types (`MBEDTLS_[AES|ARIA|CAMELLIA]_C`) if there is no 420a8e1175bSopenharmony_ci other dependency requiring them. 421a8e1175bSopenharmony_ci 422a8e1175bSopenharmony_ciChaChaPoly has no such feature, so it requires full acceleration (key type + 423a8e1175bSopenharmony_cialgorithm) in order to work with a driver. 424a8e1175bSopenharmony_ci 425a8e1175bSopenharmony_ci### CTR-DRBG 426a8e1175bSopenharmony_ci 427a8e1175bSopenharmony_ciThe legacy CTR-DRBG module (enabled by `MBEDTLS_CTR_DRBG_C`) can also benefit 428a8e1175bSopenharmony_cifrom PSA acceleration if both of the following conditions are met: 429a8e1175bSopenharmony_ci 430a8e1175bSopenharmony_ci- The legacy AES module (`MBEDTLS_AES_C`) is not enabled and 431a8e1175bSopenharmony_ci- AES is supported on the PSA side together with ECB mode, i.e. 432a8e1175bSopenharmony_ci `PSA_WANT_KEY_TYPE_AES` + `PSA_WANT_ALG_ECB_NO_PADDING`. 433a8e1175bSopenharmony_ci 434a8e1175bSopenharmony_ci### Disabling `MBEDTLS_CIPHER_C` 435a8e1175bSopenharmony_ci 436a8e1175bSopenharmony_ciIt is possible to save code size by disabling MBEDTLS_CIPHER_C when all of the 437a8e1175bSopenharmony_cifollowing conditions are met: 438a8e1175bSopenharmony_ci 439a8e1175bSopenharmony_ci- The application is not using the `mbedtls_cipher_` API. 440a8e1175bSopenharmony_ci- In PSA, all unauthenticated (that is, non-AEAD) ciphers are either disabled or 441a8e1175bSopenharmony_ci fully accelerated (that is, all compatible key types are accelerated too). 442a8e1175bSopenharmony_ci- Either TLS is disabled, or `MBEDTLS_USE_PSA_CRYPTO` is enabled. 443a8e1175bSopenharmony_ci- `MBEDTLS_NIST_KW` is disabled. 444a8e1175bSopenharmony_ci- `MBEDTLS_CMAC_C` is disabled. (Note: support for CMAC in PSA can be provided by 445a8e1175bSopenharmony_ci a driver.) 446a8e1175bSopenharmony_ci 447a8e1175bSopenharmony_ciIn such a build, everything will work as usual except for the following: 448a8e1175bSopenharmony_ci 449a8e1175bSopenharmony_ci- Encryption/decryption functions from the PKCS5 and PKCS12 module will not be 450a8e1175bSopenharmony_ci available (only key derivation functions). 451a8e1175bSopenharmony_ci- Parsing of PKCS5- or PKCS12-encrypted keys in PK parse will fail. 452a8e1175bSopenharmony_ci 453a8e1175bSopenharmony_ciNote: AEAD ciphers (CCM, GCM, ChachaPoly) do not have a dependency on 454a8e1175bSopenharmony_ciMBEDTLS_CIPHER_C even when using the built-in implementations. 455a8e1175bSopenharmony_ci 456a8e1175bSopenharmony_ciIf you also have some ciphers fully accelerated and the built-ins removed, see 457a8e1175bSopenharmony_ci[Restrictions](#restrictions) for restrictions related to removing the built-ins. 458a8e1175bSopenharmony_ci 459a8e1175bSopenharmony_ci 460a8e1175bSopenharmony_ci 461