blob: a7ec8b0f7da387e15f44940d28f092e3b19eca9c [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é-Gonnard6d5f4942023-07-07 12:00:49 +020058
59Supported means that when those are provided only by drivers, everything
60(including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should
61work in the same way as if the mechanisms where built-in, except as documented
62in the "Limitations" sub-sections of the sections dedicated to each family
63below.
64
65In the near future (end of 2023), we are planning to also add support for
66ciphers (AES) and AEADs (GCM, CCM, ChachaPoly).
67
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +020068Currently (mid-2023) we don't have plans to extend this to RSA. If
69you're interested in driver-only support for RSA, please let us know.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020070
71Hashes
72------
73
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020074It is possible to have all hash operations provided only by a driver.
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020075
76More precisely:
77- you can enable `PSA_WANT_ALG_SHA_256` without `MBEDTLS_SHA256_C`, provided
78 you have `MBEDTLS_PSA_ACCEL_ALG_SHA_256` enabled;
79- and similarly for all supported hash algorithms: `MD5`, `RIPEMD160`,
80 `SHA_1`, `SHA_224`, `SHA_256`, `SHA_384`, `SHA_512`, `SHA3_224`, `SHA3_256`,
81`SHA3_384`, `SHA3_512`.
82
83In such a build, all crypto operations (via the PSA Crypto API, or non-PSA
84APIs), as well as X.509 and TLS, will work as usual, except that direct calls
85to low-level hash APIs (`mbedtls_sha256()` etc.) are not possible for the
86modules that are disabled.
87
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020088You need to call `psa_crypto_init()` before any crypto operation that uses
89a hash algorithm that is provided only by a driver, as mentioned in [General
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020090considerations](#general-considerations) above.
91
92If you want to check at compile-time whether a certain hash algorithm is
93available in the present build of Mbed TLS, regardless of whether it's
94provided by a driver or built-in, you should use the following macros:
95- for code that uses only the PSA Crypto API: `PSA_WANT_ALG_xxx` from
96 `psa/crypto.h`;
Manuel Pégourié-Gonnard030f11b2023-09-23 09:02:42 +020097- for code that uses non-PSA crypto APIs: `MBEDTLS_MD_CAN_xxx` from
Manuel Pégourié-Gonnard1f61b7b2023-09-22 10:15:22 +020098 `mbedtls/md.h`.
Manuel Pégourié-Gonnard6d5f4942023-07-07 12:00:49 +020099
100Elliptic-curve cryptography (ECC)
101---------------------------------
102
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200103It is possible to have most ECC operations provided only by a driver:
104- the ECDH, ECDSA and EC J-PAKE algorithms;
105- key import, export, and random generation.
106
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200107More precisely, if:
108- you have driver support for ECC public and using private keys (that is,
109`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY` and
110`MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_BASIC` are enabled), and
111- you have driver support for all ECC curves that are enabled (that is, for
112 each `PSA_WANT_ECC_xxx` macro enabled, the corresponding
113`MBEDTLS_PSA_ACCEL_ECC_xxx` macros is enabled as well);
114then you can:
115- enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C`, provided
116 `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled
117- enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C`, provided
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200118 `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled;
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200119- enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C`, provided
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200120 `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled.
121
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200122In addition, if:
123- none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, `MBEDTLS_ECJPAKE_C` are enabled
124 (see conditions above), and
125- you have driver support for all enabled ECC key pair operations - that is,
126 for each `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx` macro enabled, the
127corresponding `MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR_xxx` macros is also
128enabled,
129then you can also disable `MBEDTLS_ECP_C`. However, a small subset of it might
130still be included in the build, see limitations sub-section below.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200131
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200132In addition, if:
133- `MBEDTLS_ECP_C` is fully removed (see limitation sub-section below), and
134- support for RSA key types and algorithms is fully disabled, and
135- support for DH key types and the FFDH algorithm is either disabled, or
136 fully provided by a driver,
137then you can also disable `MBEDTLS_BIGNUM_C`.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200138
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200139In such builds, all crypto operations via the PSA Crypto API will work as
140usual, as well as the PK, X.509 and TLS modules if `MBEDTLS_USE_PSA_CRYPTO` is
141enabled, with the following exceptions:
142- direct calls to APIs from the disabled modules are not possible;
143- PK, X.509 and TLS will not support restartable ECC operations (see
144 limitation sub-section below).
145
146If you want to check at compile-time whether a certain curve is available in
147the present build of Mbed TLS, regardless of whether ECC is provided by a
148driver or built-in, you should use the following macros:
149- for code that uses only the PSA Crypto API: `PSA_WANT_ECC_xxx` from
150 `psa/crypto.h`;
151- for code that may also use non-PSA crypto APIs: `MBEDTLS_ECP_HAVE_xxx` from
152 `mbedtls/build_info.h` where xxx can take the same values as for
153`MBEDTLS_ECP_DP_xxx` macros.
Valerio Settid31b2842023-08-15 10:59:58 +0200154
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200155### Limitations regarding fully removing `ecp.c`
156
157A limited subset of `ecp.c` will still be automatically re-enabled if any of
158the following is enabled:
159- `MBEDTLS_PK_PARSE_EC_COMPRESSED` - support for parsing ECC keys where the
160 public part is in compressed format;
161- `MBEDTLS_PK_PARSE_EC_EXTENDED` - support for parsing ECC keys where the
162 curve is identified not by name, but by explicit parameters;
163- `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE` - support for deterministic
164 derivation of an ECC keypair with `psa_key_derivation_output_key()`.
165
Manuel Pégourié-Gonnard1937cf82023-07-11 11:14:15 +0200166Note: when any of the above options is enabled, a subset of `ecp.c` will
167automatically be included in the build in order to support it. Therefore
168you can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will
169result in some code size savings, but not as much as when none of the
170above features are enabled.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200171
172We do have plans to support each of these with `ecp.c` fully removed in the
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +0200173future, however there is no established timeline. If you're interested, please
174let us know, so we can take it into consideration in our planning.
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200175
176### Limitations regarding restartable / interruptible ECC operations
177
Manuel Pégourié-Gonnard89ae2662023-09-22 13:05:25 +0200178At the moment, there is no driver support for interruptible operations
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200179(see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a
180consequence these are not supported in builds without `MBEDTLS_ECDSA_C`.
181
182Similarly, there is no PSA support for interruptible ECDH operations so these
183are not supported without `ECDH_C`. See also limitations regarding
184restartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its
185documentation](use-psa-crypto.md).
186
Manuel Pégourié-Gonnardfb22c272023-07-18 10:40:56 +0200187Again, we have plans to support this in the future but not with an established
Manuel Pégourié-Gonnard7a82e272023-07-07 16:43:56 +0200188timeline, please let us know if you're interested.
189
Manuel Pégourié-Gonnardf7dc6cf2023-09-27 10:34:52 +0200190### Limitations regarding "mixed" builds (driver and built-in)
191
192In order for a build to be driver-only (no built-in implementation), all the
193requested algorithms, key types (key operations) and curves must be
194accelerated (plus a few other restrictions, see "Limitations regarding fully
195removing `ecp.c`" above). However, what if you have an accelerator that only
196supports some algorithms, some key types (key operations), or some curves, but
197want to have more enabled in you build?
198
199It is possible to have acceleration for only a subset of the requested
200algorithms. In this case, the built-in implementation of the accelerated
201algorithms will be disabled, provided all the requested curves and key types
202that can be used with this algorithm are also declared as accelerated.
203
204There is very limited support for having acceleration for only a subset of the
205requested key type operations. The only configuration that's tested is that of
206a driver accelerating `PUBLIC_KEY`, `KEY_PAIR_BASIC`, `KEY_PAIR_IMPORT`,
207`KEY_PAIR_EXPORT` but not `KEY_PAIR_GENERATE`. (Note: currently the driver
208interface does not support `KEY_PAIR_DERIVE`.)
209
210There is limited support for having acceleration for only a subset of the
211requested curves. In such builds, only the PSA API is currently tested and
212working; there are known issues in PK, and X.509 and TLS are untested.
213
Manuel Pégourié-Gonnardc9777512023-07-11 11:11:20 +0200214Finite-field Diffie-Hellman
215---------------------------
216
Valerio Settid31b2842023-08-15 10:59:58 +0200217Support is pretty similar to the "Elliptic-curve cryptography (ECC)" section
218above.
219Key management and usage can be enabled by means of the usual `PSA_WANT` +
220`MBEDTLS_PSA_ACCEL` pairs:
221
222- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_PUBLIC_KEY`;
223- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_BASIC`;
224- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_IMPORT`;
225- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_EXPORT`;
226- `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_GENERATE`;
227
228The same holds for the associated algorithm:
Valerio Setti7373a662023-09-04 13:59:03 +0200229`[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and
Valerio Settid31b2842023-08-15 10:59:58 +0200230removing builtin support (i.e. `MBEDTLS_DHM_C`).
231
232### Limitations
233Support for deterministic derivation of a DH keypair
234(i.e. `PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE`) is not supported.