Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 1 | This document explains how to create builds of Mbed TLS where some |
| 2 | cryptographic mechanisms are provided only by PSA drivers (that is, no |
| 3 | built-in implementation of those algorithms), from a user's perspective. |
| 4 | |
| 5 | This is useful to save code size for people who are using either a hardware |
Manuel Pégourié-Gonnard | 030f11b | 2023-09-23 09:02:42 +0200 | [diff] [blame] | 6 | accelerator, or an alternative software implementation that is more |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 7 | aggressively optimized for code size than the default one in Mbed TLS. |
| 8 | |
| 9 | General considerations |
| 10 | ---------------------- |
| 11 | |
| 12 | This document assumes that you already have a working driver. |
| 13 | Otherwise, please see the [PSA driver example and |
| 14 | guide](psa-driver-example-and-guide.md) for information on writing a |
| 15 | driver. |
| 16 | |
| 17 | In order to have some mechanism provided only by a driver, you'll want |
| 18 | the following compile-time configuration options enabled: |
| 19 | - `MBEDTLS_PSA_CRYPTO_C` (enabled by default) - this enables PSA Crypto. |
| 20 | - `MBEDTLS_USE_PSA_CRYPTO` (disabled by default) - this makes PK, X.509 and |
| 21 | TLS use PSA Crypto. You need to enable this if you're using PK, X.509 or TLS |
| 22 | and want them to have access to the algorithms provided by your driver. (See |
| 23 | [the dedicated document](use-psa-crypto.md) for details.) |
| 24 | - `MBEDTLS_PSA_CRYPTO_CONFIG` (disabled by default) - this enables |
| 25 | configuration of cryptographic algorithms using `PSA_WANT` macros in |
| 26 | `include/psa/crypto_config.h`. See [Conditional inclusion of cryptographic |
| 27 | mechanism through the PSA API in Mbed |
| 28 | TLS](proposed/psa-conditional-inclusion-c.md) for details. |
| 29 | |
| 30 | In addition, for each mechanism you want provided only by your driver: |
| 31 | - Define the corresponding `PSA_WANT` macro in `psa/crypto_config.h` - this |
| 32 | means the algorithm will be available in the PSA Crypto API. |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 33 | - Define the corresponding `MBEDTLS_PSA_ACCEL` in your build. This could be |
| 34 | defined in `psa/crypto_config.h` or your compiler's command line. This |
| 35 | informs the PSA code that an accelerator is available for this mechanism. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 36 | - Undefine / comment out the corresponding `MBEDTLS_xxx_C` macro in |
| 37 | `mbedtls/mbedtls_config.h`. This ensures the built-in implementation is not |
| 38 | included in the build. |
| 39 | |
| 40 | For example, if you want SHA-256 to be provided only by a driver, you'll want |
| 41 | `PSA_WANT_ALG_SHA_256` and `MBEDTLS_PSA_ACCEL_SHA_256` defined, and |
| 42 | `MBEDTLS_SHA256_C` undefined. |
| 43 | |
| 44 | In addition to these compile-time considerations, at runtime you'll need to |
| 45 | make sure you call `psa_crypto_init()` before any function that uses the |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 46 | driver-only mechanisms. Note that this is already a requirement for any use of |
| 47 | the PSA Crypto API, as well as for use of the PK, X.509 and TLS modules when |
| 48 | `MBEDTLS_USE_PSA_CRYPTO` is enabled, so in most cases your application will |
| 49 | already be doing this. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 50 | |
| 51 | Mechanisms covered |
| 52 | ------------------ |
| 53 | |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 54 | For now, only the following (families of) mechanisms are supported: |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 55 | - hashes: SHA-3, SHA-2, SHA-1, MD5, etc. |
| 56 | - elliptic-curve cryptography (ECC): ECDH, ECDSA, EC J-PAKE, ECC key types. |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 57 | - finite-field Diffie-Hellman: FFDH algorithm, DH key types. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 58 | - AEADs: |
| 59 | - GCM and CCM with AES, ARIA and Camellia key types |
| 60 | - ChachaPoly with ChaCha20 Key type |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 61 | - Unauthenticated ciphers: |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 62 | - key types: AES, ARIA, Camellia, DES |
| 63 | - modes: ECB, CBC, CTR, CFB, OFB, XTS |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 64 | |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 65 | For each family listed above, all the mentioned alorithms/key types are also |
| 66 | all the mechanisms that exist in PSA API. |
| 67 | |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 68 | Supported means that when those are provided only by drivers, everything |
| 69 | (including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should |
| 70 | work in the same way as if the mechanisms where built-in, except as documented |
| 71 | in the "Limitations" sub-sections of the sections dedicated to each family |
| 72 | below. |
| 73 | |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 74 | Currently (mid-2023) we don't have plans to extend this to RSA. If |
| 75 | you're interested in driver-only support for RSA, please let us know. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 76 | |
| 77 | Hashes |
| 78 | ------ |
| 79 | |
Manuel Pégourié-Gonnard | 030f11b | 2023-09-23 09:02:42 +0200 | [diff] [blame] | 80 | It is possible to have all hash operations provided only by a driver. |
Manuel Pégourié-Gonnard | 1f61b7b | 2023-09-22 10:15:22 +0200 | [diff] [blame] | 81 | |
| 82 | More precisely: |
| 83 | - you can enable `PSA_WANT_ALG_SHA_256` without `MBEDTLS_SHA256_C`, provided |
| 84 | you have `MBEDTLS_PSA_ACCEL_ALG_SHA_256` enabled; |
| 85 | - and similarly for all supported hash algorithms: `MD5`, `RIPEMD160`, |
| 86 | `SHA_1`, `SHA_224`, `SHA_256`, `SHA_384`, `SHA_512`, `SHA3_224`, `SHA3_256`, |
| 87 | `SHA3_384`, `SHA3_512`. |
| 88 | |
| 89 | In such a build, all crypto operations (via the PSA Crypto API, or non-PSA |
| 90 | APIs), as well as X.509 and TLS, will work as usual, except that direct calls |
| 91 | to low-level hash APIs (`mbedtls_sha256()` etc.) are not possible for the |
| 92 | modules that are disabled. |
| 93 | |
Manuel Pégourié-Gonnard | 030f11b | 2023-09-23 09:02:42 +0200 | [diff] [blame] | 94 | You need to call `psa_crypto_init()` before any crypto operation that uses |
| 95 | a hash algorithm that is provided only by a driver, as mentioned in [General |
Manuel Pégourié-Gonnard | 1f61b7b | 2023-09-22 10:15:22 +0200 | [diff] [blame] | 96 | considerations](#general-considerations) above. |
| 97 | |
| 98 | If you want to check at compile-time whether a certain hash algorithm is |
| 99 | available in the present build of Mbed TLS, regardless of whether it's |
| 100 | provided by a driver or built-in, you should use the following macros: |
| 101 | - for code that uses only the PSA Crypto API: `PSA_WANT_ALG_xxx` from |
| 102 | `psa/crypto.h`; |
Manuel Pégourié-Gonnard | 030f11b | 2023-09-23 09:02:42 +0200 | [diff] [blame] | 103 | - for code that uses non-PSA crypto APIs: `MBEDTLS_MD_CAN_xxx` from |
Manuel Pégourié-Gonnard | 1f61b7b | 2023-09-22 10:15:22 +0200 | [diff] [blame] | 104 | `mbedtls/md.h`. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 105 | |
| 106 | Elliptic-curve cryptography (ECC) |
| 107 | --------------------------------- |
| 108 | |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 109 | It is possible to have most ECC operations provided only by a driver: |
| 110 | - the ECDH, ECDSA and EC J-PAKE algorithms; |
| 111 | - key import, export, and random generation. |
| 112 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 113 | More precisely, if: |
| 114 | - you have driver support for ECC public and using private keys (that is, |
| 115 | `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY` and |
| 116 | `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_BASIC` are enabled), and |
| 117 | - you have driver support for all ECC curves that are enabled (that is, for |
| 118 | each `PSA_WANT_ECC_xxx` macro enabled, the corresponding |
| 119 | `MBEDTLS_PSA_ACCEL_ECC_xxx` macros is enabled as well); |
Manuel Pégourié-Gonnard | 8c40f3d | 2023-09-28 11:06:09 +0200 | [diff] [blame] | 120 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 121 | then you can: |
| 122 | - enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C`, provided |
| 123 | `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled |
| 124 | - enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C`, provided |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 125 | `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled; |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 126 | - enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C`, provided |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 127 | `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled. |
| 128 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 129 | In addition, if: |
| 130 | - none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, `MBEDTLS_ECJPAKE_C` are enabled |
| 131 | (see conditions above), and |
| 132 | - you have driver support for all enabled ECC key pair operations - that is, |
| 133 | for each `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx` macro enabled, the |
| 134 | corresponding `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_xxx` macros is also |
| 135 | enabled, |
Manuel Pégourié-Gonnard | 8c40f3d | 2023-09-28 11:06:09 +0200 | [diff] [blame] | 136 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 137 | then you can also disable `MBEDTLS_ECP_C`. However, a small subset of it might |
| 138 | still be included in the build, see limitations sub-section below. |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 140 | In addition, if: |
| 141 | - `MBEDTLS_ECP_C` is fully removed (see limitation sub-section below), and |
| 142 | - support for RSA key types and algorithms is fully disabled, and |
| 143 | - support for DH key types and the FFDH algorithm is either disabled, or |
| 144 | fully provided by a driver, |
Manuel Pégourié-Gonnard | 8c40f3d | 2023-09-28 11:06:09 +0200 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 146 | then you can also disable `MBEDTLS_BIGNUM_C`. |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 148 | In such builds, all crypto operations via the PSA Crypto API will work as |
| 149 | usual, as well as the PK, X.509 and TLS modules if `MBEDTLS_USE_PSA_CRYPTO` is |
| 150 | enabled, with the following exceptions: |
| 151 | - direct calls to APIs from the disabled modules are not possible; |
| 152 | - PK, X.509 and TLS will not support restartable ECC operations (see |
| 153 | limitation sub-section below). |
| 154 | |
| 155 | If you want to check at compile-time whether a certain curve is available in |
| 156 | the present build of Mbed TLS, regardless of whether ECC is provided by a |
| 157 | driver or built-in, you should use the following macros: |
| 158 | - for code that uses only the PSA Crypto API: `PSA_WANT_ECC_xxx` from |
| 159 | `psa/crypto.h`; |
| 160 | - for code that may also use non-PSA crypto APIs: `MBEDTLS_ECP_HAVE_xxx` from |
| 161 | `mbedtls/build_info.h` where xxx can take the same values as for |
| 162 | `MBEDTLS_ECP_DP_xxx` macros. |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | 140c08e | 2023-09-28 11:02:37 +0200 | [diff] [blame] | 164 | Note that for externally-provided drivers, the integrator is responsible for |
| 165 | ensuring the appropriate `MBEDTLS_PSA_ACCEL_xxx` macros are defined. However, |
| 166 | for the p256-m driver that's provided with the library, those macros are |
| 167 | automatically defined when enabling `MBEDTLS_PSA_P256M_DRIVER_ENABLED`. |
| 168 | |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 169 | ### Limitations regarding fully removing `ecp.c` |
| 170 | |
| 171 | A limited subset of `ecp.c` will still be automatically re-enabled if any of |
| 172 | the following is enabled: |
| 173 | - `MBEDTLS_PK_PARSE_EC_COMPRESSED` - support for parsing ECC keys where the |
| 174 | public part is in compressed format; |
| 175 | - `MBEDTLS_PK_PARSE_EC_EXTENDED` - support for parsing ECC keys where the |
| 176 | curve is identified not by name, but by explicit parameters; |
| 177 | - `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE` - support for deterministic |
| 178 | derivation of an ECC keypair with `psa_key_derivation_output_key()`. |
| 179 | |
Manuel Pégourié-Gonnard | 1937cf8 | 2023-07-11 11:14:15 +0200 | [diff] [blame] | 180 | Note: when any of the above options is enabled, a subset of `ecp.c` will |
| 181 | automatically be included in the build in order to support it. Therefore |
| 182 | you can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will |
| 183 | result in some code size savings, but not as much as when none of the |
| 184 | above features are enabled. |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 185 | |
| 186 | We do have plans to support each of these with `ecp.c` fully removed in the |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 187 | future, however there is no established timeline. If you're interested, please |
| 188 | let us know, so we can take it into consideration in our planning. |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 189 | |
| 190 | ### Limitations regarding restartable / interruptible ECC operations |
| 191 | |
Manuel Pégourié-Gonnard | 89ae266 | 2023-09-22 13:05:25 +0200 | [diff] [blame] | 192 | At the moment, there is no driver support for interruptible operations |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 193 | (see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a |
| 194 | consequence these are not supported in builds without `MBEDTLS_ECDSA_C`. |
| 195 | |
| 196 | Similarly, there is no PSA support for interruptible ECDH operations so these |
| 197 | are not supported without `ECDH_C`. See also limitations regarding |
| 198 | restartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its |
| 199 | documentation](use-psa-crypto.md). |
| 200 | |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 201 | Again, we have plans to support this in the future but not with an established |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 202 | timeline, please let us know if you're interested. |
| 203 | |
Manuel Pégourié-Gonnard | f7dc6cf | 2023-09-27 10:34:52 +0200 | [diff] [blame] | 204 | ### Limitations regarding "mixed" builds (driver and built-in) |
| 205 | |
| 206 | In order for a build to be driver-only (no built-in implementation), all the |
| 207 | requested algorithms, key types (key operations) and curves must be |
| 208 | accelerated (plus a few other restrictions, see "Limitations regarding fully |
| 209 | removing `ecp.c`" above). However, what if you have an accelerator that only |
| 210 | supports some algorithms, some key types (key operations), or some curves, but |
| 211 | want to have more enabled in you build? |
| 212 | |
| 213 | It is possible to have acceleration for only a subset of the requested |
| 214 | algorithms. In this case, the built-in implementation of the accelerated |
| 215 | algorithms will be disabled, provided all the requested curves and key types |
| 216 | that can be used with this algorithm are also declared as accelerated. |
| 217 | |
| 218 | There is very limited support for having acceleration for only a subset of the |
| 219 | requested key type operations. The only configuration that's tested is that of |
| 220 | a driver accelerating `PUBLIC_KEY`, `KEY_PAIR_BASIC`, `KEY_PAIR_IMPORT`, |
| 221 | `KEY_PAIR_EXPORT` but not `KEY_PAIR_GENERATE`. (Note: currently the driver |
| 222 | interface does not support `KEY_PAIR_DERIVE`.) |
| 223 | |
| 224 | There is limited support for having acceleration for only a subset of the |
| 225 | requested curves. In such builds, only the PSA API is currently tested and |
| 226 | working; there are known issues in PK, and X.509 and TLS are untested. |
| 227 | |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 228 | Finite-field Diffie-Hellman |
| 229 | --------------------------- |
| 230 | |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 231 | Support is pretty similar to the "Elliptic-curve cryptography (ECC)" section |
| 232 | above. |
| 233 | Key management and usage can be enabled by means of the usual `PSA_WANT` + |
| 234 | `MBEDTLS_PSA_ACCEL` pairs: |
| 235 | |
| 236 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_PUBLIC_KEY`; |
| 237 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_BASIC`; |
| 238 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_IMPORT`; |
| 239 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_EXPORT`; |
| 240 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_GENERATE`; |
| 241 | |
| 242 | The same holds for the associated algorithm: |
Valerio Setti | 7373a66 | 2023-09-04 13:59:03 +0200 | [diff] [blame] | 243 | `[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 244 | removing builtin support (i.e. `MBEDTLS_DHM_C`). |
| 245 | |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 246 | Ciphers (unauthenticated and AEAD) |
| 247 | ---------------------------------- |
Valerio Setti | 20e93a2 | 2023-12-04 11:29:36 +0100 | [diff] [blame] | 248 | |
Valerio Setti | 8c1e6bb | 2023-12-21 15:02:48 +0100 | [diff] [blame] | 249 | It is possible to have all ciphers and AEAD operations provided only by a |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 250 | driver. More precisely, for each desired combination of key type and |
| 251 | algorithm/mode you can: |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 252 | - Enable desired PSA key type(s): |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 253 | - `PSA_WANT_KEY_TYPE_AES`, |
| 254 | - `PSA_WANT_KEY_TYPE_ARIA`, |
| 255 | - `PSA_WANT_KEY_TYPE_CAMELLIA`, |
| 256 | - `PSA_WANT_KEY_TYPE_CHACHA20`, |
Valerio Setti | 8c1e6bb | 2023-12-21 15:02:48 +0100 | [diff] [blame] | 257 | - `PSA_WANT_KEY_TYPE_DES`. |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 258 | - Enable desired PSA algorithm(s): |
| 259 | - Unauthenticated ciphers modes: |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 260 | - `PSA_WANT_ALG_CBC_NO_PADDING`, |
| 261 | - `PSA_WANT_ALG_CBC_PKCS7`, |
| 262 | - `PSA_WANT_ALG_CCM_STAR_NO_TAG`, |
| 263 | - `PSA_WANT_ALG_CFB`, |
| 264 | - `PSA_WANT_ALG_CTR`, |
| 265 | - `PSA_WANT_ALG_ECB_NO_PADDING`, |
| 266 | - `PSA_WANT_ALG_OFB`, |
Valerio Setti | 8c1e6bb | 2023-12-21 15:02:48 +0100 | [diff] [blame] | 267 | - `PSA_WANT_ALG_STREAM_CIPHER`. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 268 | - AEADs: |
| 269 | - `PSA_WANT_ALG_CCM`, |
| 270 | - `PSA_WANT_ALG_GCM`, |
Valerio Setti | 8c1e6bb | 2023-12-21 15:02:48 +0100 | [diff] [blame] | 271 | - `PSA_WANT_ALG_CHACHA20_POLY1305`. |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 272 | - Enable `MBEDTLS_PSA_ACCEL_[KEY_TYPE_xxx|ALG_yyy]` symbol(s) which correspond |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 273 | to the `PSA_WANT_KEY_TYPE_xxx` and `PSA_WANT_ALG_yyy` of the previous steps. |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 274 | - Disable builtin support of key types: |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 275 | - `MBEDTLS_AES_C`, |
| 276 | - `MBEDTLS_ARIA_C`, |
| 277 | - `MBEDTLS_CAMELLIA_C`, |
| 278 | - `MBEDTLS_DES_C`, |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 279 | - `MBEDTLS_CHACHA20_C`. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 280 | and algorithms/modes: |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 281 | - `MBEDTLS_CBC_C`, |
| 282 | - `MBEDTLS_CFB_C`, |
| 283 | - `MBEDTLS_CTR_C`, |
| 284 | - `MBEDTLS_OFB_C`, |
| 285 | - `MBEDTLS_XTS_C`, |
| 286 | - `MBEDTLS_CCM_C`, |
| 287 | - `MBEDTLS_GCM_C`, |
| 288 | - `MBEDTLS_CHACHAPOLY_C`, |
| 289 | - `MBEDTLS_NULL_CIPHER`. |
Valerio Setti | acd7baf | 2023-12-06 15:17:12 +0100 | [diff] [blame] | 290 | |
Valerio Setti | 49067d7 | 2023-12-21 17:07:10 +0100 | [diff] [blame] | 291 | Once a key type and related algorithm are accelerated, all the PSA Crypto APIs |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 292 | will work, as well as X.509 and TLS (with `MBEDTLS_USE_PSA_CRYPTO` enabled) but |
Valerio Setti | 49067d7 | 2023-12-21 17:07:10 +0100 | [diff] [blame] | 293 | some non-PSA APIs will be absent or have reduced functionality, see |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 294 | [Restrictions](#restrictions) for details. |
Valerio Setti | 20e93a2 | 2023-12-04 11:29:36 +0100 | [diff] [blame] | 295 | |
Valerio Setti | 6613466 | 2023-12-20 17:06:13 +0100 | [diff] [blame] | 296 | ### Restrictions |
| 297 | |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 298 | - If an algorithm other than CCM and GCM (see |
Valerio Setti | 6613466 | 2023-12-20 17:06:13 +0100 | [diff] [blame] | 299 | ["Partial acceleration for CCM/GCM"](#partial-acceleration-for-ccmgcm) below) |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 300 | is enabled but not accelerated, then all key types that can be used with it |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 301 | will need to be built-in. |
Valerio Setti | 7406b74 | 2024-01-03 14:47:36 +0100 | [diff] [blame] | 302 | - If a key type is enabled but not accelerated, then all algorithms that can be |
Valerio Setti | 6613466 | 2023-12-20 17:06:13 +0100 | [diff] [blame] | 303 | used with it will need to be built-in. |
| 304 | |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 305 | Some legacy modules can't take advantage of PSA drivers yet, and will either |
| 306 | need to be disabled, or have reduced features when the built-in implementations |
| 307 | of some ciphers are removed: |
| 308 | - `MBEDTLS_NIST_KW_C` needs built-in AES: it must be disabled when |
| 309 | `MBEDTLS_AES_C` is disabled. |
| 310 | - `MBEDTLS_CMAC_C` needs built-in AES/DES: it must be disabled when |
| 311 | `MBEDTLS_AES_C` and `MBEDTLS_DES_C` are both disabled. When only one of them |
| 312 | is enabled, then only the corresponding cipher will be available at runtime |
| 313 | for use with `mbedtls_cipher_cmac_xxx`. (Note: if there is driver support for |
| 314 | CMAC and all compatible key types, then `PSA_WANT_ALG_CMAC` can be enabled |
| 315 | without `MBEDTLS_CMAC_C` and CMAC will be usable with `psa_max_xxx` APIs.) |
| 316 | - `MBEDTLS_CIPHER_C`: the `mbedtls_cipher_xxx()` APIs will only work with |
| 317 | ciphers that are built-in - that is, both the underlying cipher |
| 318 | (eg `MBEDTLS_AES_C`) and the mode (eg `MBEDTLS_CIPHER_MODE_CBC` or |
| 319 | `MBEDTLS_GCM_C`). |
| 320 | - `MBEDTLS_PKCS5_C`: encryption/decryption (PBES2, PBE) will only work with |
| 321 | ciphers that are built-in. |
| 322 | - PEM decryption will only work with ciphers that are built-in. |
| 323 | - PK parse will only be able to parse encrypted keys using built-in ciphers. |
| 324 | |
| 325 | Note that if you also disable `MBEDTLS_CIPHER_C`, there will be additional |
| 326 | restrictions, see [Disabling `MBEDTLS_CIPHER_C`](#disabling-mbedtls_cipher_c). |
| 327 | |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 328 | ### Legacy <-> PSA matching |
Valerio Setti | 20e93a2 | 2023-12-04 11:29:36 +0100 | [diff] [blame] | 329 | |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 330 | Note that the relationship between legacy (i.e. `MBEDTLS_xxx_C`) and PSA |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 331 | (i.e. `PSA_WANT_xxx`) symbols is not always 1:1. For example: |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 332 | - ECB mode is always enabled in the legacy configuration for each key type that |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 333 | allows it (AES, ARIA, Camellia, DES), whereas it must be explicitly enabled |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 334 | in PSA with `PSA_WANT_ALG_ECB_NO_PADDING`. |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 335 | - In the legacy API, `MBEDTLS_CHACHA20_C` enables the ChaCha20 stream cipher, and |
| 336 | enabling `MBEDTLS_CHACHAPOLY_C` also enables the ChaCha20-Poly1305 AEAD. In the |
| 337 | PSA API, you need to enable `PSA_KEY_TYPE_CHACHA20` for both, plus |
| 338 | `PSA_ALG_STREAM_CIPHER` or `PSA_ALG_CHACHA20_POLY1305` as desired. |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 339 | - The legacy symbol `MBEDTLS_CCM_C` adds support for both cipher and AEAD, |
| 340 | whereas in PSA there are 2 different symbols: `PSA_WANT_ALG_CCM_STAR_NO_TAG` |
| 341 | and `PSA_WANT_ALG_CCM`, respectively. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 342 | |
| 343 | ### Partial acceleration for CCM/GCM |
| 344 | |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 345 | [This section depends on #8598 so it might be updated while that PR progresses.] |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 346 | |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 347 | In case legacy CCM/GCM algorithms are enabled, it is still possible to benefit |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 348 | from PSA acceleration of the underlying block cipher by enabling support for |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 349 | ECB mode (`PSA_WANT_ALG_ECB_NO_PADDING` + `MBEDTLS_PSA_ACCEL_ALG_ECB_NO_PADDING`) |
| 350 | together with desired key type(s) (`PSA_WANT_KEY_TYPE_[AES|ARIA|CAMELLIA]` + |
| 351 | `MBEDTLS_PSA_ACCEL_KEY_TYPE_[AES|ARIA|CAMELLIA]`). |
Manuel Pégourié-Gonnard | dc4103e | 2024-01-08 10:54:47 +0100 | [diff] [blame^] | 352 | |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 353 | In such configurations it is possible to: |
| 354 | - Use CCM and GCM via the PSA Crypto APIs. |
Manuel Pégourié-Gonnard | dc4103e | 2024-01-08 10:54:47 +0100 | [diff] [blame^] | 355 | - Use CCM and GCM via legacy functions `mbedtls_[ccm|gcm]_xxx()` (but not the |
| 356 | legacy functions `mbedtls_cipher_xxx()`). |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 357 | - Disable legacy key types (`MBEDTLS_[AES|ARIA|CAMELLIA]_C`) if there is no |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 358 | other dependency requiring them. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 359 | |
Valerio Setti | f333b3f | 2023-12-29 14:49:03 +0100 | [diff] [blame] | 360 | ChaChaPoly has no such feature, so it requires full acceleration (key type + |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 361 | algorithm) in order to work with a driver. |
| 362 | |
| 363 | ### CTR-DRBG |
| 364 | |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 365 | The legacy CTR-DRBG module (enabled by `MBEDTLS_CTR_DRBG_C`) can also benefit |
| 366 | from PSA acceleration if both of the following conditions are met: |
| 367 | - The legacy AES module (`MBEDTLS_AES_C`) is not enabled and |
Valerio Setti | 8c1e6bb | 2023-12-21 15:02:48 +0100 | [diff] [blame] | 368 | - AES is supported on the PSA side together with ECB mode, i.e. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 369 | `PSA_WANT_KEY_TYPE_AES` + `PSA_WANT_ALG_ECB_NO_PADDING`. |
| 370 | |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 371 | ### Disabling `MBEDTLS_CIPHER_C` |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 372 | |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 373 | It is possible to save code size by disabling MBEDTLS_CIPHER_C when all of the |
| 374 | following conditions are met: |
| 375 | - The application is not using the `mbedtls_cipher_` API. |
| 376 | - In PSA, all unauthenticated (that is, non-AEAD) ciphers are either disabled or |
| 377 | fully accelerated (that is, all compatible key types are accelerated too). |
| 378 | - Either TLS is disabled, or `MBEDTLS_USE_PSA_CRYPTO` is enabled. |
| 379 | - `MBEDTLS_NIST_KW` is disabled. |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 380 | - `MBEDTLS_CMAC_C` is disabled. (Note: support for CMAC in PSA can be provided by |
| 381 | a driver.) |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 382 | |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 383 | In such a build, everything will work as usual except for the following: |
| 384 | - Encryption/decryption functions from the PKCS5 and PKCS12 module will not be |
| 385 | available (only key derivation functions). |
| 386 | - Parsing of PKCS5- or PKCS12-encrypted keys in PK parse will fail. |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 387 | |
Valerio Setti | 92e5c69 | 2023-12-28 13:28:03 +0100 | [diff] [blame] | 388 | Note: AEAD ciphers (CCM, GCM, ChachaPoly) do not have a dependency on |
| 389 | MBEDTLS_CIPHER_C even when using the built-in implementations. |
| 390 | |
Valerio Setti | 045d680 | 2023-12-29 15:42:22 +0100 | [diff] [blame] | 391 | If you also have some ciphers fully accelerated and the built-ins removed, see |
| 392 | [Restrictions](#restrictions) for restrictions related to removing the built-ins. |
| 393 | |
Valerio Setti | 7e11dd6 | 2023-12-18 15:52:44 +0100 | [diff] [blame] | 394 | |
Valerio Setti | 20e93a2 | 2023-12-04 11:29:36 +0100 | [diff] [blame] | 395 | |