blob: 8f708259ce5b3351a39e9c9d6cf9a771e7aae38d [file] [log] [blame] [view]
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +02001This document explains how to create builds of Mbed TLS where some
2cryptographic mechanisms are provided only by PSA drivers (that is, no
3built-in implementation of those algorithms), from a user's perspective.
4
5This is useful to save code size for people who are using either a hardware
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +02006accelerator, or an alternative software implementation that is more
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +02007aggressively optimized for code size than the default one in Mbed TLS.
8
9General considerations
10----------------------
11
12This document assumes that you already have a working driver.
13Otherwise, please see the [PSA driver example and
14guide](psa-driver-example-and-guide.md) for information on writing a
15driver.
16
17In order to have some mechanism provided only by a driver, you'll want
18the 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
22and 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
27mechanism through the PSA API in Mbed
28TLS](proposed/psa-conditional-inclusion-c.md) for details.
29
30In 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é-Gonnardfb22c272023-07-18 10:40:56 +020033- 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
35informs the PSA code that an accelerator is available for this mechanism.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020036- Undefine / comment out the corresponding `MBEDTLS_xxx_C` macro in
37 `mbedtls/mbedtls_config.h`. This ensures the built-in implementation is not
38included in the build.
39
40For 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
44In addition to these compile-time considerations, at runtime you'll need to
45make sure you call `psa_crypto_init()` before any function that uses the
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +020046driver-only mechanisms. Note that this is already a requirement for any use of
47the 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
49already be doing this.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020050
51Mechanisms covered
52------------------
53
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +020054For now, only the following (families of) mechanisms are supported:
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020055- 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é-Gonnardc9777512023-07-11 11:11:20 +020057- finite-field Diffie-Hellman: FFDH algorithm, DH key types.
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +010058- RSA: PKCS#1 v1.5 and v2.1 signature and encryption algorithms, RSA key types
59 (for now, only crypto, no X.509 or TLS support).
Valerio Setti7e11dd62023-12-18 15:52:44 +010060- AEADs:
61 - GCM and CCM with AES, ARIA and Camellia key types
62 - ChachaPoly with ChaCha20 Key type
Valerio Setti92e5c692023-12-28 13:28:03 +010063- Unauthenticated ciphers:
Valerio Setti7e11dd62023-12-18 15:52:44 +010064 - key types: AES, ARIA, Camellia, DES
65 - modes: ECB, CBC, CTR, CFB, OFB, XTS
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020066
Valerio Setti92e5c692023-12-28 13:28:03 +010067For each family listed above, all the mentioned alorithms/key types are also
68all the mechanisms that exist in PSA API.
69
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020070Supported means that when those are provided only by drivers, everything
71(including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should
72work in the same way as if the mechanisms where built-in, except as documented
73in the "Limitations" sub-sections of the sections dedicated to each family
74below.
75
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020076Hashes
77------
78
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020079It is possible to have all hash operations provided only by a driver.
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020080
81More precisely:
82- you can enable `PSA_WANT_ALG_SHA_256` without `MBEDTLS_SHA256_C`, provided
83 you have `MBEDTLS_PSA_ACCEL_ALG_SHA_256` enabled;
84- and similarly for all supported hash algorithms: `MD5`, `RIPEMD160`,
85 `SHA_1`, `SHA_224`, `SHA_256`, `SHA_384`, `SHA_512`, `SHA3_224`, `SHA3_256`,
86`SHA3_384`, `SHA3_512`.
87
88In such a build, all crypto operations (via the PSA Crypto API, or non-PSA
89APIs), as well as X.509 and TLS, will work as usual, except that direct calls
90to low-level hash APIs (`mbedtls_sha256()` etc.) are not possible for the
91modules that are disabled.
92
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020093You need to call `psa_crypto_init()` before any crypto operation that uses
94a hash algorithm that is provided only by a driver, as mentioned in [General
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020095considerations](#general-considerations) above.
96
97If you want to check at compile-time whether a certain hash algorithm is
98available in the present build of Mbed TLS, regardless of whether it's
99provided by a driver or built-in, you should use the following macros:
100- for code that uses only the PSA Crypto API: `PSA_WANT_ALG_xxx` from
101 `psa/crypto.h`;
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +0200102- for code that uses non-PSA crypto APIs: `MBEDTLS_MD_CAN_xxx` from
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +0200103 `mbedtls/md.h`.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +0200104
105Elliptic-curve cryptography (ECC)
106---------------------------------
107
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200108It is possible to have most ECC operations provided only by a driver:
109- the ECDH, ECDSA and EC J-PAKE algorithms;
110- key import, export, and random generation.
111
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200112More precisely, if:
113- you have driver support for ECC public and using private keys (that is,
114`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY` and
115`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_BASIC` are enabled), and
116- you have driver support for all ECC curves that are enabled (that is, for
117 each `PSA_WANT_ECC_xxx` macro enabled, the corresponding
118`MBEDTLS_PSA_ACCEL_ECC_xxx` macros is enabled as well);
Manuel Pégourié-Gonnard8c40f3d2023-09-28 11:06:09 +0200119
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200120then you can:
121- enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C`, provided
122 `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled
123- enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C`, provided
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200124 `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled;
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200125- enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C`, provided
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200126 `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled.
127
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200128In addition, if:
129- none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, `MBEDTLS_ECJPAKE_C` are enabled
130 (see conditions above), and
131- you have driver support for all enabled ECC key pair operations - that is,
132 for each `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx` macro enabled, the
133corresponding `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_xxx` macros is also
134enabled,
Manuel Pégourié-Gonnard8c40f3d2023-09-28 11:06:09 +0200135
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200136then you can also disable `MBEDTLS_ECP_C`. However, a small subset of it might
137still be included in the build, see limitations sub-section below.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200138
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200139In addition, if:
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +0100140- `MBEDTLS_ECP_C` is fully removed (see limitation sub-section below),
141- and support for RSA key types and algorithms is either fully disabled or
142 fully provided by a driver,
143- and support for DH key types and the FFDH algorithm is either disabled or
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200144 fully provided by a driver,
Manuel Pégourié-Gonnard8c40f3d2023-09-28 11:06:09 +0200145
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200146then you can also disable `MBEDTLS_BIGNUM_C`.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200147
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200148In such builds, all crypto operations via the PSA Crypto API will work as
149usual, as well as the PK, X.509 and TLS modules if `MBEDTLS_USE_PSA_CRYPTO` is
150enabled, 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
155If you want to check at compile-time whether a certain curve is available in
156the present build of Mbed TLS, regardless of whether ECC is provided by a
157driver 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 Settid31b2842023-08-15 10:59:58 +0200163
Manuel Pégourié-Gonnard140c08e2023-09-28 11:02:37 +0200164Note that for externally-provided drivers, the integrator is responsible for
165ensuring the appropriate `MBEDTLS_PSA_ACCEL_xxx` macros are defined. However,
166for the p256-m driver that's provided with the library, those macros are
167automatically defined when enabling `MBEDTLS_PSA_P256M_DRIVER_ENABLED`.
168
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200169### Limitations regarding fully removing `ecp.c`
170
171A limited subset of `ecp.c` will still be automatically re-enabled if any of
172the 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é-Gonnard1937cf82023-07-11 11:14:15 +0200180Note: when any of the above options is enabled, a subset of `ecp.c` will
181automatically be included in the build in order to support it. Therefore
182you can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will
183result in some code size savings, but not as much as when none of the
184above features are enabled.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200185
186We do have plans to support each of these with `ecp.c` fully removed in the
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +0200187future, however there is no established timeline. If you're interested, please
188let us know, so we can take it into consideration in our planning.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200189
190### Limitations regarding restartable / interruptible ECC operations
191
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200192At the moment, there is no driver support for interruptible operations
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200193(see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a
194consequence these are not supported in builds without `MBEDTLS_ECDSA_C`.
195
196Similarly, there is no PSA support for interruptible ECDH operations so these
197are not supported without `ECDH_C`. See also limitations regarding
198restartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its
199documentation](use-psa-crypto.md).
200
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +0200201Again, we have plans to support this in the future but not with an established
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200202timeline, please let us know if you're interested.
203
Manuel Pégourié-Gonnardf7dc6cf2023-09-27 10:34:52 +0200204### Limitations regarding "mixed" builds (driver and built-in)
205
206In order for a build to be driver-only (no built-in implementation), all the
207requested algorithms, key types (key operations) and curves must be
208accelerated (plus a few other restrictions, see "Limitations regarding fully
209removing `ecp.c`" above). However, what if you have an accelerator that only
210supports some algorithms, some key types (key operations), or some curves, but
211want to have more enabled in you build?
212
213It is possible to have acceleration for only a subset of the requested
214algorithms. In this case, the built-in implementation of the accelerated
215algorithms will be disabled, provided all the requested curves and key types
216that can be used with this algorithm are also declared as accelerated.
217
218There is very limited support for having acceleration for only a subset of the
219requested key type operations. The only configuration that's tested is that of
220a driver accelerating `PUBLIC_KEY`, `KEY_PAIR_BASIC`, `KEY_PAIR_IMPORT`,
221`KEY_PAIR_EXPORT` but not `KEY_PAIR_GENERATE`. (Note: currently the driver
222interface does not support `KEY_PAIR_DERIVE`.)
223
224There is limited support for having acceleration for only a subset of the
225requested curves. In such builds, only the PSA API is currently tested and
226working; there are known issues in PK, and X.509 and TLS are untested.
227
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +0200228Finite-field Diffie-Hellman
229---------------------------
230
Valerio Settid31b2842023-08-15 10:59:58 +0200231Support is pretty similar to the "Elliptic-curve cryptography (ECC)" section
232above.
233Key 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
242The same holds for the associated algorithm:
Valerio Setti7373a662023-09-04 13:59:03 +0200243`[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and
Valerio Settid31b2842023-08-15 10:59:58 +0200244removing builtin support (i.e. `MBEDTLS_DHM_C`).
245
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +0100246RSA
247---
248
249It is possible for all RSA operations to be provided only by a driver.
250
251More precisely, if:
252- all the RSA algorithms that are enabled (`PSA_WANT_ALG_RSA_*`) are also
253 accelerated (`MBEDTLS_PSA_ACCEL_ALG_RSA_*`),
254- and all the RSA key types that are enabled (`PSA_WANT_KEY_TYPE_RSA_*`) are
255 also accelerated (`MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_*`),
256
257then you can disable `MBEDTLS_RSA_C`, `MBEDTLS_PKCS1_V15` and
258`MBEDTLS_PKCS1_V21`, and RSA will still work in PSA Crypto.
259
260### Limitations on RSA acceleration
261
262Unlike other mechanisms, for now in configurations with driver-only RSA, only
263PSA Crypto works. In particular, PK, X.509 and TLS will _not_ work with
264driver-only RSA even if `MBEDTLS_USE_PSA_CRYPTO` is enabled.
265
266Currently (early 2024) we don't have plans to extend this support. If you're
267interested in wider driver-only support for RSA, please let us know.
268
Valerio Setti045d6802023-12-29 15:42:22 +0100269Ciphers (unauthenticated and AEAD)
270----------------------------------
Valerio Setti20e93a22023-12-04 11:29:36 +0100271
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100272It is possible to have all ciphers and AEAD operations provided only by a
Valerio Setti7e11dd62023-12-18 15:52:44 +0100273driver. More precisely, for each desired combination of key type and
274algorithm/mode you can:
Valerio Setti92e5c692023-12-28 13:28:03 +0100275- Enable desired PSA key type(s):
Valerio Setti7e11dd62023-12-18 15:52:44 +0100276 - `PSA_WANT_KEY_TYPE_AES`,
277 - `PSA_WANT_KEY_TYPE_ARIA`,
278 - `PSA_WANT_KEY_TYPE_CAMELLIA`,
279 - `PSA_WANT_KEY_TYPE_CHACHA20`,
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100280 - `PSA_WANT_KEY_TYPE_DES`.
Valerio Setti92e5c692023-12-28 13:28:03 +0100281- Enable desired PSA algorithm(s):
282 - Unauthenticated ciphers modes:
Valerio Setti7e11dd62023-12-18 15:52:44 +0100283 - `PSA_WANT_ALG_CBC_NO_PADDING`,
284 - `PSA_WANT_ALG_CBC_PKCS7`,
285 - `PSA_WANT_ALG_CCM_STAR_NO_TAG`,
286 - `PSA_WANT_ALG_CFB`,
287 - `PSA_WANT_ALG_CTR`,
288 - `PSA_WANT_ALG_ECB_NO_PADDING`,
289 - `PSA_WANT_ALG_OFB`,
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100290 - `PSA_WANT_ALG_STREAM_CIPHER`.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100291 - AEADs:
292 - `PSA_WANT_ALG_CCM`,
293 - `PSA_WANT_ALG_GCM`,
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100294 - `PSA_WANT_ALG_CHACHA20_POLY1305`.
Valerio Setti92e5c692023-12-28 13:28:03 +0100295- Enable `MBEDTLS_PSA_ACCEL_[KEY_TYPE_xxx|ALG_yyy]` symbol(s) which correspond
Valerio Settif333b3f2023-12-29 14:49:03 +0100296 to the `PSA_WANT_KEY_TYPE_xxx` and `PSA_WANT_ALG_yyy` of the previous steps.
Valerio Setti92e5c692023-12-28 13:28:03 +0100297- Disable builtin support of key types:
Valerio Setti7e11dd62023-12-18 15:52:44 +0100298 - `MBEDTLS_AES_C`,
299 - `MBEDTLS_ARIA_C`,
300 - `MBEDTLS_CAMELLIA_C`,
301 - `MBEDTLS_DES_C`,
Valerio Settif333b3f2023-12-29 14:49:03 +0100302 - `MBEDTLS_CHACHA20_C`.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100303 and algorithms/modes:
Valerio Settif333b3f2023-12-29 14:49:03 +0100304 - `MBEDTLS_CBC_C`,
305 - `MBEDTLS_CFB_C`,
306 - `MBEDTLS_CTR_C`,
307 - `MBEDTLS_OFB_C`,
308 - `MBEDTLS_XTS_C`,
309 - `MBEDTLS_CCM_C`,
310 - `MBEDTLS_GCM_C`,
311 - `MBEDTLS_CHACHAPOLY_C`,
312 - `MBEDTLS_NULL_CIPHER`.
Valerio Settiacd7baf2023-12-06 15:17:12 +0100313
Valerio Setti49067d72023-12-21 17:07:10 +0100314Once a key type and related algorithm are accelerated, all the PSA Crypto APIs
Valerio Settif333b3f2023-12-29 14:49:03 +0100315will work, as well as X.509 and TLS (with `MBEDTLS_USE_PSA_CRYPTO` enabled) but
Valerio Setti49067d72023-12-21 17:07:10 +0100316some non-PSA APIs will be absent or have reduced functionality, see
Valerio Setti045d6802023-12-29 15:42:22 +0100317[Restrictions](#restrictions) for details.
Valerio Setti20e93a22023-12-04 11:29:36 +0100318
Valerio Setti66134662023-12-20 17:06:13 +0100319### Restrictions
320
Valerio Settif333b3f2023-12-29 14:49:03 +0100321- If an algorithm other than CCM and GCM (see
Valerio Setti66134662023-12-20 17:06:13 +0100322 ["Partial acceleration for CCM/GCM"](#partial-acceleration-for-ccmgcm) below)
Valerio Settif333b3f2023-12-29 14:49:03 +0100323 is enabled but not accelerated, then all key types that can be used with it
Valerio Setti92e5c692023-12-28 13:28:03 +0100324 will need to be built-in.
Valerio Setti7406b742024-01-03 14:47:36 +0100325- If a key type is enabled but not accelerated, then all algorithms that can be
Valerio Setti66134662023-12-20 17:06:13 +0100326 used with it will need to be built-in.
327
Valerio Setti045d6802023-12-29 15:42:22 +0100328Some legacy modules can't take advantage of PSA drivers yet, and will either
329need to be disabled, or have reduced features when the built-in implementations
330of some ciphers are removed:
331- `MBEDTLS_NIST_KW_C` needs built-in AES: it must be disabled when
332 `MBEDTLS_AES_C` is disabled.
333- `MBEDTLS_CMAC_C` needs built-in AES/DES: it must be disabled when
334 `MBEDTLS_AES_C` and `MBEDTLS_DES_C` are both disabled. When only one of them
335 is enabled, then only the corresponding cipher will be available at runtime
336 for use with `mbedtls_cipher_cmac_xxx`. (Note: if there is driver support for
337 CMAC and all compatible key types, then `PSA_WANT_ALG_CMAC` can be enabled
338 without `MBEDTLS_CMAC_C` and CMAC will be usable with `psa_max_xxx` APIs.)
339- `MBEDTLS_CIPHER_C`: the `mbedtls_cipher_xxx()` APIs will only work with
340 ciphers that are built-in - that is, both the underlying cipher
341 (eg `MBEDTLS_AES_C`) and the mode (eg `MBEDTLS_CIPHER_MODE_CBC` or
342 `MBEDTLS_GCM_C`).
343- `MBEDTLS_PKCS5_C`: encryption/decryption (PBES2, PBE) will only work with
344 ciphers that are built-in.
345- PEM decryption will only work with ciphers that are built-in.
346- PK parse will only be able to parse encrypted keys using built-in ciphers.
347
348Note that if you also disable `MBEDTLS_CIPHER_C`, there will be additional
349restrictions, see [Disabling `MBEDTLS_CIPHER_C`](#disabling-mbedtls_cipher_c).
350
Valerio Setti7e11dd62023-12-18 15:52:44 +0100351### Legacy <-> PSA matching
Valerio Setti20e93a22023-12-04 11:29:36 +0100352
Valerio Settif333b3f2023-12-29 14:49:03 +0100353Note that the relationship between legacy (i.e. `MBEDTLS_xxx_C`) and PSA
Valerio Setti92e5c692023-12-28 13:28:03 +0100354(i.e. `PSA_WANT_xxx`) symbols is not always 1:1. For example:
Valerio Settif333b3f2023-12-29 14:49:03 +0100355- ECB mode is always enabled in the legacy configuration for each key type that
Valerio Setti7e11dd62023-12-18 15:52:44 +0100356 allows it (AES, ARIA, Camellia, DES), whereas it must be explicitly enabled
Valerio Setti92e5c692023-12-28 13:28:03 +0100357 in PSA with `PSA_WANT_ALG_ECB_NO_PADDING`.
Valerio Settif333b3f2023-12-29 14:49:03 +0100358- In the legacy API, `MBEDTLS_CHACHA20_C` enables the ChaCha20 stream cipher, and
359 enabling `MBEDTLS_CHACHAPOLY_C` also enables the ChaCha20-Poly1305 AEAD. In the
360 PSA API, you need to enable `PSA_KEY_TYPE_CHACHA20` for both, plus
361 `PSA_ALG_STREAM_CIPHER` or `PSA_ALG_CHACHA20_POLY1305` as desired.
Valerio Setti92e5c692023-12-28 13:28:03 +0100362- The legacy symbol `MBEDTLS_CCM_C` adds support for both cipher and AEAD,
363 whereas in PSA there are 2 different symbols: `PSA_WANT_ALG_CCM_STAR_NO_TAG`
364 and `PSA_WANT_ALG_CCM`, respectively.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100365
366### Partial acceleration for CCM/GCM
367
Valerio Settif333b3f2023-12-29 14:49:03 +0100368[This section depends on #8598 so it might be updated while that PR progresses.]
Valerio Setti7e11dd62023-12-18 15:52:44 +0100369
Valerio Settif333b3f2023-12-29 14:49:03 +0100370In case legacy CCM/GCM algorithms are enabled, it is still possible to benefit
Valerio Setti92e5c692023-12-28 13:28:03 +0100371from PSA acceleration of the underlying block cipher by enabling support for
Valerio Setti045d6802023-12-29 15:42:22 +0100372ECB mode (`PSA_WANT_ALG_ECB_NO_PADDING` + `MBEDTLS_PSA_ACCEL_ALG_ECB_NO_PADDING`)
373together with desired key type(s) (`PSA_WANT_KEY_TYPE_[AES|ARIA|CAMELLIA]` +
374`MBEDTLS_PSA_ACCEL_KEY_TYPE_[AES|ARIA|CAMELLIA]`).
Manuel Pégourié-Gonnarddc4103e2024-01-08 10:54:47 +0100375
Valerio Setti045d6802023-12-29 15:42:22 +0100376In such configurations it is possible to:
377- Use CCM and GCM via the PSA Crypto APIs.
Manuel Pégourié-Gonnarddc4103e2024-01-08 10:54:47 +0100378- Use CCM and GCM via legacy functions `mbedtls_[ccm|gcm]_xxx()` (but not the
379 legacy functions `mbedtls_cipher_xxx()`).
Valerio Setti92e5c692023-12-28 13:28:03 +0100380- Disable legacy key types (`MBEDTLS_[AES|ARIA|CAMELLIA]_C`) if there is no
Valerio Settif333b3f2023-12-29 14:49:03 +0100381 other dependency requiring them.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100382
Valerio Settif333b3f2023-12-29 14:49:03 +0100383ChaChaPoly has no such feature, so it requires full acceleration (key type +
Valerio Setti7e11dd62023-12-18 15:52:44 +0100384algorithm) in order to work with a driver.
385
386### CTR-DRBG
387
Valerio Setti92e5c692023-12-28 13:28:03 +0100388The legacy CTR-DRBG module (enabled by `MBEDTLS_CTR_DRBG_C`) can also benefit
389from PSA acceleration if both of the following conditions are met:
390- The legacy AES module (`MBEDTLS_AES_C`) is not enabled and
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100391- AES is supported on the PSA side together with ECB mode, i.e.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100392 `PSA_WANT_KEY_TYPE_AES` + `PSA_WANT_ALG_ECB_NO_PADDING`.
393
Valerio Setti045d6802023-12-29 15:42:22 +0100394### Disabling `MBEDTLS_CIPHER_C`
Valerio Setti7e11dd62023-12-18 15:52:44 +0100395
Valerio Setti92e5c692023-12-28 13:28:03 +0100396It is possible to save code size by disabling MBEDTLS_CIPHER_C when all of the
397following conditions are met:
398- The application is not using the `mbedtls_cipher_` API.
399- In PSA, all unauthenticated (that is, non-AEAD) ciphers are either disabled or
400 fully accelerated (that is, all compatible key types are accelerated too).
401- Either TLS is disabled, or `MBEDTLS_USE_PSA_CRYPTO` is enabled.
402- `MBEDTLS_NIST_KW` is disabled.
Valerio Setti045d6802023-12-29 15:42:22 +0100403- `MBEDTLS_CMAC_C` is disabled. (Note: support for CMAC in PSA can be provided by
404 a driver.)
Valerio Setti7e11dd62023-12-18 15:52:44 +0100405
Valerio Setti92e5c692023-12-28 13:28:03 +0100406In such a build, everything will work as usual except for the following:
407- Encryption/decryption functions from the PKCS5 and PKCS12 module will not be
408 available (only key derivation functions).
409- Parsing of PKCS5- or PKCS12-encrypted keys in PK parse will fail.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100410
Valerio Setti92e5c692023-12-28 13:28:03 +0100411Note: AEAD ciphers (CCM, GCM, ChachaPoly) do not have a dependency on
412MBEDTLS_CIPHER_C even when using the built-in implementations.
413
Valerio Setti045d6802023-12-29 15:42:22 +0100414If you also have some ciphers fully accelerated and the built-ins removed, see
415[Restrictions](#restrictions) for restrictions related to removing the built-ins.
416
Valerio Setti7e11dd62023-12-18 15:52:44 +0100417
Valerio Setti20e93a22023-12-04 11:29:36 +0100418