1*62c56f98SSadaf EbrahimiThis document explains how to create builds of Mbed TLS where some 2*62c56f98SSadaf Ebrahimicryptographic mechanisms are provided only by PSA drivers (that is, no 3*62c56f98SSadaf Ebrahimibuilt-in implementation of those algorithms), from a user's perspective. 4*62c56f98SSadaf Ebrahimi 5*62c56f98SSadaf EbrahimiThis is useful to save code size for people who are using either a hardware 6*62c56f98SSadaf Ebrahimiaccelerator, or an alternative software implementation that is more 7*62c56f98SSadaf Ebrahimiaggressively optimized for code size than the default one in Mbed TLS. 8*62c56f98SSadaf Ebrahimi 9*62c56f98SSadaf EbrahimiGeneral considerations 10*62c56f98SSadaf Ebrahimi---------------------- 11*62c56f98SSadaf Ebrahimi 12*62c56f98SSadaf EbrahimiThis document assumes that you already have a working driver. 13*62c56f98SSadaf EbrahimiOtherwise, please see the [PSA driver example and 14*62c56f98SSadaf Ebrahimiguide](psa-driver-example-and-guide.md) for information on writing a 15*62c56f98SSadaf Ebrahimidriver. 16*62c56f98SSadaf Ebrahimi 17*62c56f98SSadaf EbrahimiIn order to have some mechanism provided only by a driver, you'll want 18*62c56f98SSadaf Ebrahimithe following compile-time configuration options enabled: 19*62c56f98SSadaf Ebrahimi- `MBEDTLS_PSA_CRYPTO_C` (enabled by default) - this enables PSA Crypto. 20*62c56f98SSadaf Ebrahimi- `MBEDTLS_USE_PSA_CRYPTO` (disabled by default) - this makes PK, X.509 and 21*62c56f98SSadaf Ebrahimi TLS use PSA Crypto. You need to enable this if you're using PK, X.509 or TLS 22*62c56f98SSadaf Ebrahimiand want them to have access to the algorithms provided by your driver. (See 23*62c56f98SSadaf Ebrahimi[the dedicated document](use-psa-crypto.md) for details.) 24*62c56f98SSadaf Ebrahimi- `MBEDTLS_PSA_CRYPTO_CONFIG` (disabled by default) - this enables 25*62c56f98SSadaf Ebrahimi configuration of cryptographic algorithms using `PSA_WANT` macros in 26*62c56f98SSadaf Ebrahimi`include/psa/crypto_config.h`. See [Conditional inclusion of cryptographic 27*62c56f98SSadaf Ebrahimimechanism through the PSA API in Mbed 28*62c56f98SSadaf EbrahimiTLS](proposed/psa-conditional-inclusion-c.md) for details. 29*62c56f98SSadaf Ebrahimi 30*62c56f98SSadaf EbrahimiIn addition, for each mechanism you want provided only by your driver: 31*62c56f98SSadaf Ebrahimi- Define the corresponding `PSA_WANT` macro in `psa/crypto_config.h` - this 32*62c56f98SSadaf Ebrahimi means the algorithm will be available in the PSA Crypto API. 33*62c56f98SSadaf Ebrahimi- Define the corresponding `MBEDTLS_PSA_ACCEL` in your build. This could be 34*62c56f98SSadaf Ebrahimi defined in `psa/crypto_config.h` or your compiler's command line. This 35*62c56f98SSadaf Ebrahimiinforms the PSA code that an accelerator is available for this mechanism. 36*62c56f98SSadaf Ebrahimi- Undefine / comment out the corresponding `MBEDTLS_xxx_C` macro in 37*62c56f98SSadaf Ebrahimi `mbedtls/mbedtls_config.h`. This ensures the built-in implementation is not 38*62c56f98SSadaf Ebrahimiincluded in the build. 39*62c56f98SSadaf Ebrahimi 40*62c56f98SSadaf EbrahimiFor example, if you want SHA-256 to be provided only by a driver, you'll want 41*62c56f98SSadaf Ebrahimi`PSA_WANT_ALG_SHA_256` and `MBEDTLS_PSA_ACCEL_SHA_256` defined, and 42*62c56f98SSadaf Ebrahimi`MBEDTLS_SHA256_C` undefined. 43*62c56f98SSadaf Ebrahimi 44*62c56f98SSadaf EbrahimiIn addition to these compile-time considerations, at runtime you'll need to 45*62c56f98SSadaf Ebrahimimake sure you call `psa_crypto_init()` before any function that uses the 46*62c56f98SSadaf Ebrahimidriver-only mechanisms. Note that this is already a requirement for any use of 47*62c56f98SSadaf Ebrahimithe PSA Crypto API, as well as for use of the PK, X.509 and TLS modules when 48*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is enabled, so in most cases your application will 49*62c56f98SSadaf Ebrahimialready be doing this. 50*62c56f98SSadaf Ebrahimi 51*62c56f98SSadaf EbrahimiMechanisms covered 52*62c56f98SSadaf Ebrahimi------------------ 53*62c56f98SSadaf Ebrahimi 54*62c56f98SSadaf EbrahimiFor now, only the following (families of) mechanisms are supported: 55*62c56f98SSadaf Ebrahimi- hashes: SHA-3, SHA-2, SHA-1, MD5, etc. 56*62c56f98SSadaf Ebrahimi- elliptic-curve cryptography (ECC): ECDH, ECDSA, EC J-PAKE, ECC key types. 57*62c56f98SSadaf Ebrahimi- finite-field Diffie-Hellman: FFDH algorithm, DH key types. 58*62c56f98SSadaf Ebrahimi 59*62c56f98SSadaf EbrahimiSupported means that when those are provided only by drivers, everything 60*62c56f98SSadaf Ebrahimi(including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should 61*62c56f98SSadaf Ebrahimiwork in the same way as if the mechanisms where built-in, except as documented 62*62c56f98SSadaf Ebrahimiin the "Limitations" sub-sections of the sections dedicated to each family 63*62c56f98SSadaf Ebrahimibelow. 64*62c56f98SSadaf Ebrahimi 65*62c56f98SSadaf EbrahimiIn the near future (end of 2023), we are planning to also add support for 66*62c56f98SSadaf Ebrahimiciphers (AES) and AEADs (GCM, CCM, ChachaPoly). 67*62c56f98SSadaf Ebrahimi 68*62c56f98SSadaf EbrahimiCurrently (mid-2023) we don't have plans to extend this to RSA. If 69*62c56f98SSadaf Ebrahimiyou're interested in driver-only support for RSA, please let us know. 70*62c56f98SSadaf Ebrahimi 71*62c56f98SSadaf EbrahimiHashes 72*62c56f98SSadaf Ebrahimi------ 73*62c56f98SSadaf Ebrahimi 74*62c56f98SSadaf EbrahimiIt is possible to have all hash operations provided only by a driver. 75*62c56f98SSadaf Ebrahimi 76*62c56f98SSadaf EbrahimiMore precisely: 77*62c56f98SSadaf Ebrahimi- you can enable `PSA_WANT_ALG_SHA_256` without `MBEDTLS_SHA256_C`, provided 78*62c56f98SSadaf Ebrahimi you have `MBEDTLS_PSA_ACCEL_ALG_SHA_256` enabled; 79*62c56f98SSadaf Ebrahimi- and similarly for all supported hash algorithms: `MD5`, `RIPEMD160`, 80*62c56f98SSadaf Ebrahimi `SHA_1`, `SHA_224`, `SHA_256`, `SHA_384`, `SHA_512`, `SHA3_224`, `SHA3_256`, 81*62c56f98SSadaf Ebrahimi`SHA3_384`, `SHA3_512`. 82*62c56f98SSadaf Ebrahimi 83*62c56f98SSadaf EbrahimiIn such a build, all crypto operations (via the PSA Crypto API, or non-PSA 84*62c56f98SSadaf EbrahimiAPIs), as well as X.509 and TLS, will work as usual, except that direct calls 85*62c56f98SSadaf Ebrahimito low-level hash APIs (`mbedtls_sha256()` etc.) are not possible for the 86*62c56f98SSadaf Ebrahimimodules that are disabled. 87*62c56f98SSadaf Ebrahimi 88*62c56f98SSadaf EbrahimiYou need to call `psa_crypto_init()` before any crypto operation that uses 89*62c56f98SSadaf Ebrahimia hash algorithm that is provided only by a driver, as mentioned in [General 90*62c56f98SSadaf Ebrahimiconsiderations](#general-considerations) above. 91*62c56f98SSadaf Ebrahimi 92*62c56f98SSadaf EbrahimiIf you want to check at compile-time whether a certain hash algorithm is 93*62c56f98SSadaf Ebrahimiavailable in the present build of Mbed TLS, regardless of whether it's 94*62c56f98SSadaf Ebrahimiprovided by a driver or built-in, you should use the following macros: 95*62c56f98SSadaf Ebrahimi- for code that uses only the PSA Crypto API: `PSA_WANT_ALG_xxx` from 96*62c56f98SSadaf Ebrahimi `psa/crypto.h`; 97*62c56f98SSadaf Ebrahimi- for code that uses non-PSA crypto APIs: `MBEDTLS_MD_CAN_xxx` from 98*62c56f98SSadaf Ebrahimi `mbedtls/md.h`. 99*62c56f98SSadaf Ebrahimi 100*62c56f98SSadaf EbrahimiElliptic-curve cryptography (ECC) 101*62c56f98SSadaf Ebrahimi--------------------------------- 102*62c56f98SSadaf Ebrahimi 103*62c56f98SSadaf EbrahimiIt is possible to have most ECC operations provided only by a driver: 104*62c56f98SSadaf Ebrahimi- the ECDH, ECDSA and EC J-PAKE algorithms; 105*62c56f98SSadaf Ebrahimi- key import, export, and random generation. 106*62c56f98SSadaf Ebrahimi 107*62c56f98SSadaf EbrahimiMore precisely, if: 108*62c56f98SSadaf Ebrahimi- you have driver support for ECC public and using private keys (that is, 109*62c56f98SSadaf Ebrahimi`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY` and 110*62c56f98SSadaf Ebrahimi`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_BASIC` are enabled), and 111*62c56f98SSadaf Ebrahimi- you have driver support for all ECC curves that are enabled (that is, for 112*62c56f98SSadaf Ebrahimi each `PSA_WANT_ECC_xxx` macro enabled, the corresponding 113*62c56f98SSadaf Ebrahimi`MBEDTLS_PSA_ACCEL_ECC_xxx` macros is enabled as well); 114*62c56f98SSadaf Ebrahimi 115*62c56f98SSadaf Ebrahimithen you can: 116*62c56f98SSadaf Ebrahimi- enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C`, provided 117*62c56f98SSadaf Ebrahimi `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled 118*62c56f98SSadaf Ebrahimi- enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C`, provided 119*62c56f98SSadaf Ebrahimi `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled; 120*62c56f98SSadaf Ebrahimi- enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C`, provided 121*62c56f98SSadaf Ebrahimi `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled. 122*62c56f98SSadaf Ebrahimi 123*62c56f98SSadaf EbrahimiIn addition, if: 124*62c56f98SSadaf Ebrahimi- none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, `MBEDTLS_ECJPAKE_C` are enabled 125*62c56f98SSadaf Ebrahimi (see conditions above), and 126*62c56f98SSadaf Ebrahimi- you have driver support for all enabled ECC key pair operations - that is, 127*62c56f98SSadaf Ebrahimi for each `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx` macro enabled, the 128*62c56f98SSadaf Ebrahimicorresponding `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_xxx` macros is also 129*62c56f98SSadaf Ebrahimienabled, 130*62c56f98SSadaf Ebrahimi 131*62c56f98SSadaf Ebrahimithen you can also disable `MBEDTLS_ECP_C`. However, a small subset of it might 132*62c56f98SSadaf Ebrahimistill be included in the build, see limitations sub-section below. 133*62c56f98SSadaf Ebrahimi 134*62c56f98SSadaf EbrahimiIn addition, if: 135*62c56f98SSadaf Ebrahimi- `MBEDTLS_ECP_C` is fully removed (see limitation sub-section below), and 136*62c56f98SSadaf Ebrahimi- support for RSA key types and algorithms is fully disabled, and 137*62c56f98SSadaf Ebrahimi- support for DH key types and the FFDH algorithm is either disabled, or 138*62c56f98SSadaf Ebrahimi fully provided by a driver, 139*62c56f98SSadaf Ebrahimi 140*62c56f98SSadaf Ebrahimithen you can also disable `MBEDTLS_BIGNUM_C`. 141*62c56f98SSadaf Ebrahimi 142*62c56f98SSadaf EbrahimiIn such builds, all crypto operations via the PSA Crypto API will work as 143*62c56f98SSadaf Ebrahimiusual, as well as the PK, X.509 and TLS modules if `MBEDTLS_USE_PSA_CRYPTO` is 144*62c56f98SSadaf Ebrahimienabled, with the following exceptions: 145*62c56f98SSadaf Ebrahimi- direct calls to APIs from the disabled modules are not possible; 146*62c56f98SSadaf Ebrahimi- PK, X.509 and TLS will not support restartable ECC operations (see 147*62c56f98SSadaf Ebrahimi limitation sub-section below). 148*62c56f98SSadaf Ebrahimi 149*62c56f98SSadaf EbrahimiIf you want to check at compile-time whether a certain curve is available in 150*62c56f98SSadaf Ebrahimithe present build of Mbed TLS, regardless of whether ECC is provided by a 151*62c56f98SSadaf Ebrahimidriver or built-in, you should use the following macros: 152*62c56f98SSadaf Ebrahimi- for code that uses only the PSA Crypto API: `PSA_WANT_ECC_xxx` from 153*62c56f98SSadaf Ebrahimi `psa/crypto.h`; 154*62c56f98SSadaf Ebrahimi- for code that may also use non-PSA crypto APIs: `MBEDTLS_ECP_HAVE_xxx` from 155*62c56f98SSadaf Ebrahimi `mbedtls/build_info.h` where xxx can take the same values as for 156*62c56f98SSadaf Ebrahimi`MBEDTLS_ECP_DP_xxx` macros. 157*62c56f98SSadaf Ebrahimi 158*62c56f98SSadaf EbrahimiNote that for externally-provided drivers, the integrator is responsible for 159*62c56f98SSadaf Ebrahimiensuring the appropriate `MBEDTLS_PSA_ACCEL_xxx` macros are defined. However, 160*62c56f98SSadaf Ebrahimifor the p256-m driver that's provided with the library, those macros are 161*62c56f98SSadaf Ebrahimiautomatically defined when enabling `MBEDTLS_PSA_P256M_DRIVER_ENABLED`. 162*62c56f98SSadaf Ebrahimi 163*62c56f98SSadaf Ebrahimi### Limitations regarding fully removing `ecp.c` 164*62c56f98SSadaf Ebrahimi 165*62c56f98SSadaf EbrahimiA limited subset of `ecp.c` will still be automatically re-enabled if any of 166*62c56f98SSadaf Ebrahimithe following is enabled: 167*62c56f98SSadaf Ebrahimi- `MBEDTLS_PK_PARSE_EC_COMPRESSED` - support for parsing ECC keys where the 168*62c56f98SSadaf Ebrahimi public part is in compressed format; 169*62c56f98SSadaf Ebrahimi- `MBEDTLS_PK_PARSE_EC_EXTENDED` - support for parsing ECC keys where the 170*62c56f98SSadaf Ebrahimi curve is identified not by name, but by explicit parameters; 171*62c56f98SSadaf Ebrahimi- `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE` - support for deterministic 172*62c56f98SSadaf Ebrahimi derivation of an ECC keypair with `psa_key_derivation_output_key()`. 173*62c56f98SSadaf Ebrahimi 174*62c56f98SSadaf EbrahimiNote: when any of the above options is enabled, a subset of `ecp.c` will 175*62c56f98SSadaf Ebrahimiautomatically be included in the build in order to support it. Therefore 176*62c56f98SSadaf Ebrahimiyou can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will 177*62c56f98SSadaf Ebrahimiresult in some code size savings, but not as much as when none of the 178*62c56f98SSadaf Ebrahimiabove features are enabled. 179*62c56f98SSadaf Ebrahimi 180*62c56f98SSadaf EbrahimiWe do have plans to support each of these with `ecp.c` fully removed in the 181*62c56f98SSadaf Ebrahimifuture, however there is no established timeline. If you're interested, please 182*62c56f98SSadaf Ebrahimilet us know, so we can take it into consideration in our planning. 183*62c56f98SSadaf Ebrahimi 184*62c56f98SSadaf Ebrahimi### Limitations regarding restartable / interruptible ECC operations 185*62c56f98SSadaf Ebrahimi 186*62c56f98SSadaf EbrahimiAt the moment, there is no driver support for interruptible operations 187*62c56f98SSadaf Ebrahimi(see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a 188*62c56f98SSadaf Ebrahimiconsequence these are not supported in builds without `MBEDTLS_ECDSA_C`. 189*62c56f98SSadaf Ebrahimi 190*62c56f98SSadaf EbrahimiSimilarly, there is no PSA support for interruptible ECDH operations so these 191*62c56f98SSadaf Ebrahimiare not supported without `ECDH_C`. See also limitations regarding 192*62c56f98SSadaf Ebrahimirestartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its 193*62c56f98SSadaf Ebrahimidocumentation](use-psa-crypto.md). 194*62c56f98SSadaf Ebrahimi 195*62c56f98SSadaf EbrahimiAgain, we have plans to support this in the future but not with an established 196*62c56f98SSadaf Ebrahimitimeline, please let us know if you're interested. 197*62c56f98SSadaf Ebrahimi 198*62c56f98SSadaf Ebrahimi### Limitations regarding "mixed" builds (driver and built-in) 199*62c56f98SSadaf Ebrahimi 200*62c56f98SSadaf EbrahimiIn order for a build to be driver-only (no built-in implementation), all the 201*62c56f98SSadaf Ebrahimirequested algorithms, key types (key operations) and curves must be 202*62c56f98SSadaf Ebrahimiaccelerated (plus a few other restrictions, see "Limitations regarding fully 203*62c56f98SSadaf Ebrahimiremoving `ecp.c`" above). However, what if you have an accelerator that only 204*62c56f98SSadaf Ebrahimisupports some algorithms, some key types (key operations), or some curves, but 205*62c56f98SSadaf Ebrahimiwant to have more enabled in you build? 206*62c56f98SSadaf Ebrahimi 207*62c56f98SSadaf EbrahimiIt is possible to have acceleration for only a subset of the requested 208*62c56f98SSadaf Ebrahimialgorithms. In this case, the built-in implementation of the accelerated 209*62c56f98SSadaf Ebrahimialgorithms will be disabled, provided all the requested curves and key types 210*62c56f98SSadaf Ebrahimithat can be used with this algorithm are also declared as accelerated. 211*62c56f98SSadaf Ebrahimi 212*62c56f98SSadaf EbrahimiThere is very limited support for having acceleration for only a subset of the 213*62c56f98SSadaf Ebrahimirequested key type operations. The only configuration that's tested is that of 214*62c56f98SSadaf Ebrahimia driver accelerating `PUBLIC_KEY`, `KEY_PAIR_BASIC`, `KEY_PAIR_IMPORT`, 215*62c56f98SSadaf Ebrahimi`KEY_PAIR_EXPORT` but not `KEY_PAIR_GENERATE`. (Note: currently the driver 216*62c56f98SSadaf Ebrahimiinterface does not support `KEY_PAIR_DERIVE`.) 217*62c56f98SSadaf Ebrahimi 218*62c56f98SSadaf EbrahimiThere is limited support for having acceleration for only a subset of the 219*62c56f98SSadaf Ebrahimirequested curves. In such builds, only the PSA API is currently tested and 220*62c56f98SSadaf Ebrahimiworking; there are known issues in PK, and X.509 and TLS are untested. 221*62c56f98SSadaf Ebrahimi 222*62c56f98SSadaf EbrahimiFinite-field Diffie-Hellman 223*62c56f98SSadaf Ebrahimi--------------------------- 224*62c56f98SSadaf Ebrahimi 225*62c56f98SSadaf EbrahimiSupport is pretty similar to the "Elliptic-curve cryptography (ECC)" section 226*62c56f98SSadaf Ebrahimiabove. 227*62c56f98SSadaf EbrahimiKey management and usage can be enabled by means of the usual `PSA_WANT` + 228*62c56f98SSadaf Ebrahimi`MBEDTLS_PSA_ACCEL` pairs: 229*62c56f98SSadaf Ebrahimi 230*62c56f98SSadaf Ebrahimi- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_PUBLIC_KEY`; 231*62c56f98SSadaf Ebrahimi- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_BASIC`; 232*62c56f98SSadaf Ebrahimi- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_IMPORT`; 233*62c56f98SSadaf Ebrahimi- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_EXPORT`; 234*62c56f98SSadaf Ebrahimi- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_GENERATE`; 235*62c56f98SSadaf Ebrahimi 236*62c56f98SSadaf EbrahimiThe same holds for the associated algorithm: 237*62c56f98SSadaf Ebrahimi`[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and 238*62c56f98SSadaf Ebrahimiremoving builtin support (i.e. `MBEDTLS_DHM_C`). 239*62c56f98SSadaf Ebrahimi 240*62c56f98SSadaf Ebrahimi### Limitations 241*62c56f98SSadaf EbrahimiSupport for deterministic derivation of a DH keypair 242*62c56f98SSadaf Ebrahimi(i.e. `PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE`) is not supported. 243