blob: 2c08c69063f77a5f94c1a61401259dbcee5d5a70 [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:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +010019
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020020- `MBEDTLS_PSA_CRYPTO_C` (enabled by default) - this enables PSA Crypto.
21- `MBEDTLS_USE_PSA_CRYPTO` (disabled by default) - this makes PK, X.509 and
22 TLS use PSA Crypto. You need to enable this if you're using PK, X.509 or TLS
23and want them to have access to the algorithms provided by your driver. (See
24[the dedicated document](use-psa-crypto.md) for details.)
25- `MBEDTLS_PSA_CRYPTO_CONFIG` (disabled by default) - this enables
26 configuration of cryptographic algorithms using `PSA_WANT` macros in
27`include/psa/crypto_config.h`. See [Conditional inclusion of cryptographic
28mechanism through the PSA API in Mbed
29TLS](proposed/psa-conditional-inclusion-c.md) for details.
30
31In addition, for each mechanism you want provided only by your driver:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +010032
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020033- Define the corresponding `PSA_WANT` macro in `psa/crypto_config.h` - this
34 means the algorithm will be available in the PSA Crypto API.
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +020035- Define the corresponding `MBEDTLS_PSA_ACCEL` in your build. This could be
36 defined in `psa/crypto_config.h` or your compiler's command line. This
37informs the PSA code that an accelerator is available for this mechanism.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020038- Undefine / comment out the corresponding `MBEDTLS_xxx_C` macro in
39 `mbedtls/mbedtls_config.h`. This ensures the built-in implementation is not
40included in the build.
41
42For example, if you want SHA-256 to be provided only by a driver, you'll want
43`PSA_WANT_ALG_SHA_256` and `MBEDTLS_PSA_ACCEL_SHA_256` defined, and
44`MBEDTLS_SHA256_C` undefined.
45
46In addition to these compile-time considerations, at runtime you'll need to
47make sure you call `psa_crypto_init()` before any function that uses the
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +020048driver-only mechanisms. Note that this is already a requirement for any use of
49the PSA Crypto API, as well as for use of the PK, X.509 and TLS modules when
50`MBEDTLS_USE_PSA_CRYPTO` is enabled, so in most cases your application will
51already be doing this.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020052
53Mechanisms covered
54------------------
55
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +020056For now, only the following (families of) mechanisms are supported:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +010057
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020058- hashes: SHA-3, SHA-2, SHA-1, MD5, etc.
59- elliptic-curve cryptography (ECC): ECDH, ECDSA, EC J-PAKE, ECC key types.
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +020060- finite-field Diffie-Hellman: FFDH algorithm, DH key types.
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +010061- RSA: PKCS#1 v1.5 and v2.1 signature and encryption algorithms, RSA key types
62 (for now, only crypto, no X.509 or TLS support).
Valerio Setti7e11dd62023-12-18 15:52:44 +010063- AEADs:
64 - GCM and CCM with AES, ARIA and Camellia key types
65 - ChachaPoly with ChaCha20 Key type
Valerio Setti92e5c692023-12-28 13:28:03 +010066- Unauthenticated ciphers:
Valerio Setti7e11dd62023-12-18 15:52:44 +010067 - key types: AES, ARIA, Camellia, DES
68 - modes: ECB, CBC, CTR, CFB, OFB, XTS
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020069
Valerio Setti92e5c692023-12-28 13:28:03 +010070For each family listed above, all the mentioned alorithms/key types are also
71all the mechanisms that exist in PSA API.
72
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020073Supported means that when those are provided only by drivers, everything
74(including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should
75work in the same way as if the mechanisms where built-in, except as documented
76in the "Limitations" sub-sections of the sections dedicated to each family
77below.
78
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020079Hashes
80------
81
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020082It is possible to have all hash operations provided only by a driver.
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020083
84More precisely:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +010085
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020086- you can enable `PSA_WANT_ALG_SHA_256` without `MBEDTLS_SHA256_C`, provided
87 you have `MBEDTLS_PSA_ACCEL_ALG_SHA_256` enabled;
88- and similarly for all supported hash algorithms: `MD5`, `RIPEMD160`,
89 `SHA_1`, `SHA_224`, `SHA_256`, `SHA_384`, `SHA_512`, `SHA3_224`, `SHA3_256`,
90`SHA3_384`, `SHA3_512`.
91
92In such a build, all crypto operations (via the PSA Crypto API, or non-PSA
93APIs), as well as X.509 and TLS, will work as usual, except that direct calls
94to low-level hash APIs (`mbedtls_sha256()` etc.) are not possible for the
95modules that are disabled.
96
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020097You need to call `psa_crypto_init()` before any crypto operation that uses
98a hash algorithm that is provided only by a driver, as mentioned in [General
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020099considerations](#general-considerations) above.
100
101If you want to check at compile-time whether a certain hash algorithm is
102available in the present build of Mbed TLS, regardless of whether it's
Elena Uziunaite9b0bdd02024-09-06 16:10:20 +0100103provided by a driver or built-in, you should use `PSA_WANT_ALG_xxx` from
104`psa/crypto.h`.
Valerio Setti9f521052024-01-24 15:44:24 +0100105
106### HMAC
107
108In addition to accelerated hash operations, it is also possible to accelerate
109HMAC by enabling and accelerating:
110- HMAC algorithm and key type, i.e. `[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_HMAC` and
111 `[PSA_WANT|MBEDTLS_PSA_ACCEL]KEY_TYPE_HMAC`.
112- Required hash algorithm(s) as explained in [Hashes](#hashes) section.
113
114In such a build it is possible to disable legacy HMAC support by disabling
115`MBEDTLS_MD_C` and still getting crypto operations, X.509 and TLS to work as
116usual. Exceptions are:
Valerio Setti18be2fb2024-01-26 15:07:02 +0100117- As mentioned in [Hashes](#hashes) direct calls to legacy lo-level hash APIs
118 (`mbedtls_sha256()` etc.) will not be possible for the legacy modules that
119 are disabled.
Valerio Setti9f521052024-01-24 15:44:24 +0100120- Legacy HMAC support (`mbedtls_md_hmac_xxx()`) won't be possible.
121- `MBEDTLS_PKCS[5|7]_C`, `MBEDTLS_HMAC_DRBG_C` and `MBEDTLS_HKDF_C` since they
122 depend on the legacy implementation of HMAC.
123 - disabling HMAC_DRBG_C cause deterministic ECDSA (i.e.
124 `MBEDTLS_DETERMINISTIC_ECDSA` on the legacy side and
125 `PSA_WANT_ALG_DETERMINISTIC_ECDSA` on the PSA one) to be not available.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +0200126
127Elliptic-curve cryptography (ECC)
128---------------------------------
129
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200130It is possible to have most ECC operations provided only by a driver:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100131
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200132- the ECDH, ECDSA and EC J-PAKE algorithms;
133- key import, export, and random generation.
134
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200135More precisely, if:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100136
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200137- you have driver support for ECC public and using private keys (that is,
138`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY` and
139`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_BASIC` are enabled), and
140- you have driver support for all ECC curves that are enabled (that is, for
141 each `PSA_WANT_ECC_xxx` macro enabled, the corresponding
142`MBEDTLS_PSA_ACCEL_ECC_xxx` macros is enabled as well);
Manuel Pégourié-Gonnard8c40f3d2023-09-28 11:06:09 +0200143
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200144then you can:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100145
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200146- enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C`, provided
147 `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled
148- enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C`, provided
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200149 `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled;
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200150- enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C`, provided
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200151 `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled.
152
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200153In addition, if:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100154
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200155- none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, `MBEDTLS_ECJPAKE_C` are enabled
156 (see conditions above), and
157- you have driver support for all enabled ECC key pair operations - that is,
158 for each `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx` macro enabled, the
159corresponding `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_xxx` macros is also
160enabled,
Manuel Pégourié-Gonnard8c40f3d2023-09-28 11:06:09 +0200161
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200162then you can also disable `MBEDTLS_ECP_C`. However, a small subset of it might
163still be included in the build, see limitations sub-section below.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200164
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200165In addition, if:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100166
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +0100167- `MBEDTLS_ECP_C` is fully removed (see limitation sub-section below),
168- and support for RSA key types and algorithms is either fully disabled or
169 fully provided by a driver,
170- and support for DH key types and the FFDH algorithm is either disabled or
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200171 fully provided by a driver,
Manuel Pégourié-Gonnard8c40f3d2023-09-28 11:06:09 +0200172
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200173then you can also disable `MBEDTLS_BIGNUM_C`.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200174
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200175In such builds, all crypto operations via the PSA Crypto API will work as
176usual, as well as the PK, X.509 and TLS modules if `MBEDTLS_USE_PSA_CRYPTO` is
177enabled, with the following exceptions:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100178
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200179- direct calls to APIs from the disabled modules are not possible;
180- PK, X.509 and TLS will not support restartable ECC operations (see
181 limitation sub-section below).
182
183If you want to check at compile-time whether a certain curve is available in
184the present build of Mbed TLS, regardless of whether ECC is provided by a
Elena Uziunaite9b0bdd02024-09-06 16:10:20 +0100185driver or built-in, you should use `PSA_WANT_ECC_xxx` from
186 `psa/crypto.h`.
Valerio Settid31b2842023-08-15 10:59:58 +0200187
Manuel Pégourié-Gonnard140c08e2023-09-28 11:02:37 +0200188Note that for externally-provided drivers, the integrator is responsible for
189ensuring the appropriate `MBEDTLS_PSA_ACCEL_xxx` macros are defined. However,
190for the p256-m driver that's provided with the library, those macros are
191automatically defined when enabling `MBEDTLS_PSA_P256M_DRIVER_ENABLED`.
192
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200193### Limitations regarding fully removing `ecp.c`
194
195A limited subset of `ecp.c` will still be automatically re-enabled if any of
196the following is enabled:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100197
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200198- `MBEDTLS_PK_PARSE_EC_COMPRESSED` - support for parsing ECC keys where the
199 public part is in compressed format;
200- `MBEDTLS_PK_PARSE_EC_EXTENDED` - support for parsing ECC keys where the
201 curve is identified not by name, but by explicit parameters;
202- `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE` - support for deterministic
203 derivation of an ECC keypair with `psa_key_derivation_output_key()`.
204
Manuel Pégourié-Gonnard1937cf82023-07-11 11:14:15 +0200205Note: when any of the above options is enabled, a subset of `ecp.c` will
206automatically be included in the build in order to support it. Therefore
207you can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will
208result in some code size savings, but not as much as when none of the
209above features are enabled.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200210
211We do have plans to support each of these with `ecp.c` fully removed in the
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +0200212future, however there is no established timeline. If you're interested, please
213let us know, so we can take it into consideration in our planning.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200214
215### Limitations regarding restartable / interruptible ECC operations
216
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200217At the moment, there is no driver support for interruptible operations
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200218(see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a
219consequence these are not supported in builds without `MBEDTLS_ECDSA_C`.
220
221Similarly, there is no PSA support for interruptible ECDH operations so these
222are not supported without `ECDH_C`. See also limitations regarding
223restartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its
224documentation](use-psa-crypto.md).
225
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +0200226Again, we have plans to support this in the future but not with an established
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200227timeline, please let us know if you're interested.
228
Manuel Pégourié-Gonnardf7dc6cf2023-09-27 10:34:52 +0200229### Limitations regarding "mixed" builds (driver and built-in)
230
231In order for a build to be driver-only (no built-in implementation), all the
232requested algorithms, key types (key operations) and curves must be
233accelerated (plus a few other restrictions, see "Limitations regarding fully
234removing `ecp.c`" above). However, what if you have an accelerator that only
235supports some algorithms, some key types (key operations), or some curves, but
236want to have more enabled in you build?
237
238It is possible to have acceleration for only a subset of the requested
239algorithms. In this case, the built-in implementation of the accelerated
240algorithms will be disabled, provided all the requested curves and key types
241that can be used with this algorithm are also declared as accelerated.
242
243There is very limited support for having acceleration for only a subset of the
244requested key type operations. The only configuration that's tested is that of
245a driver accelerating `PUBLIC_KEY`, `KEY_PAIR_BASIC`, `KEY_PAIR_IMPORT`,
246`KEY_PAIR_EXPORT` but not `KEY_PAIR_GENERATE`. (Note: currently the driver
247interface does not support `KEY_PAIR_DERIVE`.)
248
249There is limited support for having acceleration for only a subset of the
250requested curves. In such builds, only the PSA API is currently tested and
251working; there are known issues in PK, and X.509 and TLS are untested.
252
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +0200253Finite-field Diffie-Hellman
254---------------------------
255
Valerio Settid31b2842023-08-15 10:59:58 +0200256Support is pretty similar to the "Elliptic-curve cryptography (ECC)" section
257above.
258Key management and usage can be enabled by means of the usual `PSA_WANT` +
259`MBEDTLS_PSA_ACCEL` pairs:
260
261- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_PUBLIC_KEY`;
262- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_BASIC`;
263- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_IMPORT`;
264- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_EXPORT`;
265- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_GENERATE`;
266
267The same holds for the associated algorithm:
Valerio Setti7373a662023-09-04 13:59:03 +0200268`[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and
Valerio Settid31b2842023-08-15 10:59:58 +0200269removing builtin support (i.e. `MBEDTLS_DHM_C`).
270
Gilles Peskineff3b8212024-04-30 14:25:30 +0200271Note that the PSA API only supports FFDH with RFC 7919 groups, whereas the
272Mbed TLS legacy API supports custom groups. As a consequence, the TLS layer
273of Mbed TLS only supports DHE cipher suites if built-in FFDH
274(`MBEDTLS_DHM_C`) is present, even when `MBEDTLS_USE_PSA_CRYPTO` is enabled.
275
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +0100276RSA
277---
278
279It is possible for all RSA operations to be provided only by a driver.
280
281More precisely, if:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100282
Manuel Pégourié-Gonnardc1cea632024-01-08 11:02:26 +0100283- all the RSA algorithms that are enabled (`PSA_WANT_ALG_RSA_*`) are also
284 accelerated (`MBEDTLS_PSA_ACCEL_ALG_RSA_*`),
285- and all the RSA key types that are enabled (`PSA_WANT_KEY_TYPE_RSA_*`) are
286 also accelerated (`MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_*`),
287
288then you can disable `MBEDTLS_RSA_C`, `MBEDTLS_PKCS1_V15` and
289`MBEDTLS_PKCS1_V21`, and RSA will still work in PSA Crypto.
290
291### Limitations on RSA acceleration
292
293Unlike other mechanisms, for now in configurations with driver-only RSA, only
294PSA Crypto works. In particular, PK, X.509 and TLS will _not_ work with
295driver-only RSA even if `MBEDTLS_USE_PSA_CRYPTO` is enabled.
296
297Currently (early 2024) we don't have plans to extend this support. If you're
298interested in wider driver-only support for RSA, please let us know.
299
Valerio Setti045d6802023-12-29 15:42:22 +0100300Ciphers (unauthenticated and AEAD)
301----------------------------------
Valerio Setti20e93a22023-12-04 11:29:36 +0100302
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100303It is possible to have all ciphers and AEAD operations provided only by a
Valerio Setti7e11dd62023-12-18 15:52:44 +0100304driver. More precisely, for each desired combination of key type and
305algorithm/mode you can:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100306
Valerio Setti92e5c692023-12-28 13:28:03 +0100307- Enable desired PSA key type(s):
Valerio Setti7e11dd62023-12-18 15:52:44 +0100308 - `PSA_WANT_KEY_TYPE_AES`,
309 - `PSA_WANT_KEY_TYPE_ARIA`,
310 - `PSA_WANT_KEY_TYPE_CAMELLIA`,
311 - `PSA_WANT_KEY_TYPE_CHACHA20`,
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100312 - `PSA_WANT_KEY_TYPE_DES`.
Valerio Setti92e5c692023-12-28 13:28:03 +0100313- Enable desired PSA algorithm(s):
314 - Unauthenticated ciphers modes:
Valerio Setti7e11dd62023-12-18 15:52:44 +0100315 - `PSA_WANT_ALG_CBC_NO_PADDING`,
316 - `PSA_WANT_ALG_CBC_PKCS7`,
317 - `PSA_WANT_ALG_CCM_STAR_NO_TAG`,
318 - `PSA_WANT_ALG_CFB`,
319 - `PSA_WANT_ALG_CTR`,
320 - `PSA_WANT_ALG_ECB_NO_PADDING`,
321 - `PSA_WANT_ALG_OFB`,
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100322 - `PSA_WANT_ALG_STREAM_CIPHER`.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100323 - AEADs:
324 - `PSA_WANT_ALG_CCM`,
325 - `PSA_WANT_ALG_GCM`,
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100326 - `PSA_WANT_ALG_CHACHA20_POLY1305`.
Valerio Setti92e5c692023-12-28 13:28:03 +0100327- Enable `MBEDTLS_PSA_ACCEL_[KEY_TYPE_xxx|ALG_yyy]` symbol(s) which correspond
Valerio Settif333b3f2023-12-29 14:49:03 +0100328 to the `PSA_WANT_KEY_TYPE_xxx` and `PSA_WANT_ALG_yyy` of the previous steps.
Valerio Setti92e5c692023-12-28 13:28:03 +0100329- Disable builtin support of key types:
Valerio Setti7e11dd62023-12-18 15:52:44 +0100330 - `MBEDTLS_AES_C`,
331 - `MBEDTLS_ARIA_C`,
332 - `MBEDTLS_CAMELLIA_C`,
333 - `MBEDTLS_DES_C`,
Valerio Settif333b3f2023-12-29 14:49:03 +0100334 - `MBEDTLS_CHACHA20_C`.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100335 and algorithms/modes:
Valerio Settif333b3f2023-12-29 14:49:03 +0100336 - `MBEDTLS_CBC_C`,
337 - `MBEDTLS_CFB_C`,
338 - `MBEDTLS_CTR_C`,
339 - `MBEDTLS_OFB_C`,
340 - `MBEDTLS_XTS_C`,
341 - `MBEDTLS_CCM_C`,
342 - `MBEDTLS_GCM_C`,
343 - `MBEDTLS_CHACHAPOLY_C`,
344 - `MBEDTLS_NULL_CIPHER`.
Valerio Settiacd7baf2023-12-06 15:17:12 +0100345
Valerio Setti49067d72023-12-21 17:07:10 +0100346Once a key type and related algorithm are accelerated, all the PSA Crypto APIs
Valerio Settif333b3f2023-12-29 14:49:03 +0100347will work, as well as X.509 and TLS (with `MBEDTLS_USE_PSA_CRYPTO` enabled) but
Valerio Setti49067d72023-12-21 17:07:10 +0100348some non-PSA APIs will be absent or have reduced functionality, see
Valerio Setti045d6802023-12-29 15:42:22 +0100349[Restrictions](#restrictions) for details.
Valerio Setti20e93a22023-12-04 11:29:36 +0100350
Valerio Setti66134662023-12-20 17:06:13 +0100351### Restrictions
352
Valerio Settif333b3f2023-12-29 14:49:03 +0100353- If an algorithm other than CCM and GCM (see
Valerio Setti66134662023-12-20 17:06:13 +0100354 ["Partial acceleration for CCM/GCM"](#partial-acceleration-for-ccmgcm) below)
Valerio Settif333b3f2023-12-29 14:49:03 +0100355 is enabled but not accelerated, then all key types that can be used with it
Valerio Setti92e5c692023-12-28 13:28:03 +0100356 will need to be built-in.
Valerio Setti7406b742024-01-03 14:47:36 +0100357- If a key type is enabled but not accelerated, then all algorithms that can be
Valerio Setti66134662023-12-20 17:06:13 +0100358 used with it will need to be built-in.
359
Valerio Setti045d6802023-12-29 15:42:22 +0100360Some legacy modules can't take advantage of PSA drivers yet, and will either
361need to be disabled, or have reduced features when the built-in implementations
362of some ciphers are removed:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100363
Valerio Setti045d6802023-12-29 15:42:22 +0100364- `MBEDTLS_NIST_KW_C` needs built-in AES: it must be disabled when
365 `MBEDTLS_AES_C` is disabled.
366- `MBEDTLS_CMAC_C` needs built-in AES/DES: it must be disabled when
367 `MBEDTLS_AES_C` and `MBEDTLS_DES_C` are both disabled. When only one of them
368 is enabled, then only the corresponding cipher will be available at runtime
369 for use with `mbedtls_cipher_cmac_xxx`. (Note: if there is driver support for
370 CMAC and all compatible key types, then `PSA_WANT_ALG_CMAC` can be enabled
371 without `MBEDTLS_CMAC_C` and CMAC will be usable with `psa_max_xxx` APIs.)
372- `MBEDTLS_CIPHER_C`: the `mbedtls_cipher_xxx()` APIs will only work with
373 ciphers that are built-in - that is, both the underlying cipher
374 (eg `MBEDTLS_AES_C`) and the mode (eg `MBEDTLS_CIPHER_MODE_CBC` or
375 `MBEDTLS_GCM_C`).
376- `MBEDTLS_PKCS5_C`: encryption/decryption (PBES2, PBE) will only work with
377 ciphers that are built-in.
378- PEM decryption will only work with ciphers that are built-in.
379- PK parse will only be able to parse encrypted keys using built-in ciphers.
380
381Note that if you also disable `MBEDTLS_CIPHER_C`, there will be additional
382restrictions, see [Disabling `MBEDTLS_CIPHER_C`](#disabling-mbedtls_cipher_c).
383
Valerio Setti7e11dd62023-12-18 15:52:44 +0100384### Legacy <-> PSA matching
Valerio Setti20e93a22023-12-04 11:29:36 +0100385
Valerio Settif333b3f2023-12-29 14:49:03 +0100386Note that the relationship between legacy (i.e. `MBEDTLS_xxx_C`) and PSA
Valerio Setti92e5c692023-12-28 13:28:03 +0100387(i.e. `PSA_WANT_xxx`) symbols is not always 1:1. For example:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100388
Valerio Settif333b3f2023-12-29 14:49:03 +0100389- ECB mode is always enabled in the legacy configuration for each key type that
Valerio Setti7e11dd62023-12-18 15:52:44 +0100390 allows it (AES, ARIA, Camellia, DES), whereas it must be explicitly enabled
Valerio Setti92e5c692023-12-28 13:28:03 +0100391 in PSA with `PSA_WANT_ALG_ECB_NO_PADDING`.
Valerio Settif333b3f2023-12-29 14:49:03 +0100392- In the legacy API, `MBEDTLS_CHACHA20_C` enables the ChaCha20 stream cipher, and
393 enabling `MBEDTLS_CHACHAPOLY_C` also enables the ChaCha20-Poly1305 AEAD. In the
394 PSA API, you need to enable `PSA_KEY_TYPE_CHACHA20` for both, plus
395 `PSA_ALG_STREAM_CIPHER` or `PSA_ALG_CHACHA20_POLY1305` as desired.
Valerio Setti92e5c692023-12-28 13:28:03 +0100396- The legacy symbol `MBEDTLS_CCM_C` adds support for both cipher and AEAD,
397 whereas in PSA there are 2 different symbols: `PSA_WANT_ALG_CCM_STAR_NO_TAG`
398 and `PSA_WANT_ALG_CCM`, respectively.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100399
400### Partial acceleration for CCM/GCM
401
Valerio Settif333b3f2023-12-29 14:49:03 +0100402[This section depends on #8598 so it might be updated while that PR progresses.]
Valerio Setti7e11dd62023-12-18 15:52:44 +0100403
Valerio Settif333b3f2023-12-29 14:49:03 +0100404In case legacy CCM/GCM algorithms are enabled, it is still possible to benefit
Valerio Setti92e5c692023-12-28 13:28:03 +0100405from PSA acceleration of the underlying block cipher by enabling support for
Valerio Setti045d6802023-12-29 15:42:22 +0100406ECB mode (`PSA_WANT_ALG_ECB_NO_PADDING` + `MBEDTLS_PSA_ACCEL_ALG_ECB_NO_PADDING`)
407together with desired key type(s) (`PSA_WANT_KEY_TYPE_[AES|ARIA|CAMELLIA]` +
408`MBEDTLS_PSA_ACCEL_KEY_TYPE_[AES|ARIA|CAMELLIA]`).
Manuel Pégourié-Gonnarddc4103e2024-01-08 10:54:47 +0100409
Valerio Setti045d6802023-12-29 15:42:22 +0100410In such configurations it is possible to:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100411
Valerio Setti045d6802023-12-29 15:42:22 +0100412- Use CCM and GCM via the PSA Crypto APIs.
Manuel Pégourié-Gonnarddc4103e2024-01-08 10:54:47 +0100413- Use CCM and GCM via legacy functions `mbedtls_[ccm|gcm]_xxx()` (but not the
414 legacy functions `mbedtls_cipher_xxx()`).
Valerio Setti92e5c692023-12-28 13:28:03 +0100415- Disable legacy key types (`MBEDTLS_[AES|ARIA|CAMELLIA]_C`) if there is no
Valerio Settif333b3f2023-12-29 14:49:03 +0100416 other dependency requiring them.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100417
Valerio Settif333b3f2023-12-29 14:49:03 +0100418ChaChaPoly has no such feature, so it requires full acceleration (key type +
Valerio Setti7e11dd62023-12-18 15:52:44 +0100419algorithm) in order to work with a driver.
420
421### CTR-DRBG
422
Valerio Setti92e5c692023-12-28 13:28:03 +0100423The legacy CTR-DRBG module (enabled by `MBEDTLS_CTR_DRBG_C`) can also benefit
424from PSA acceleration if both of the following conditions are met:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100425
Valerio Setti92e5c692023-12-28 13:28:03 +0100426- The legacy AES module (`MBEDTLS_AES_C`) is not enabled and
Valerio Setti8c1e6bb2023-12-21 15:02:48 +0100427- AES is supported on the PSA side together with ECB mode, i.e.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100428 `PSA_WANT_KEY_TYPE_AES` + `PSA_WANT_ALG_ECB_NO_PADDING`.
429
Valerio Setti045d6802023-12-29 15:42:22 +0100430### Disabling `MBEDTLS_CIPHER_C`
Valerio Setti7e11dd62023-12-18 15:52:44 +0100431
Valerio Setti92e5c692023-12-28 13:28:03 +0100432It is possible to save code size by disabling MBEDTLS_CIPHER_C when all of the
433following conditions are met:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100434
Valerio Setti92e5c692023-12-28 13:28:03 +0100435- The application is not using the `mbedtls_cipher_` API.
436- In PSA, all unauthenticated (that is, non-AEAD) ciphers are either disabled or
437 fully accelerated (that is, all compatible key types are accelerated too).
438- Either TLS is disabled, or `MBEDTLS_USE_PSA_CRYPTO` is enabled.
439- `MBEDTLS_NIST_KW` is disabled.
Valerio Setti045d6802023-12-29 15:42:22 +0100440- `MBEDTLS_CMAC_C` is disabled. (Note: support for CMAC in PSA can be provided by
441 a driver.)
Valerio Setti7e11dd62023-12-18 15:52:44 +0100442
Valerio Setti92e5c692023-12-28 13:28:03 +0100443In such a build, everything will work as usual except for the following:
Manuel Pégourié-Gonnarde3344862024-01-10 10:24:31 +0100444
Valerio Setti92e5c692023-12-28 13:28:03 +0100445- Encryption/decryption functions from the PKCS5 and PKCS12 module will not be
446 available (only key derivation functions).
447- Parsing of PKCS5- or PKCS12-encrypted keys in PK parse will fail.
Valerio Setti7e11dd62023-12-18 15:52:44 +0100448
Valerio Setti92e5c692023-12-28 13:28:03 +0100449Note: AEAD ciphers (CCM, GCM, ChachaPoly) do not have a dependency on
450MBEDTLS_CIPHER_C even when using the built-in implementations.
451
Valerio Setti045d6802023-12-29 15:42:22 +0100452If you also have some ciphers fully accelerated and the built-ins removed, see
453[Restrictions](#restrictions) for restrictions related to removing the built-ins.
454
Valerio Setti7e11dd62023-12-18 15:52:44 +0100455
Valerio Setti20e93a22023-12-04 11:29:36 +0100456