blob: ec3bb8b31974b7f48f14935912bdcfa321f753f0 [file] [log] [blame] [view]
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +02001This document explains the strategy that was used so far in starting the
2migration to PSA Crypto and mentions future perspectives and open questions.
3
4Goals
5=====
6
7Several benefits are expected from migrating to PSA Crypto:
8
Manuel Pégourié-Gonnard74979912021-10-27 14:00:08 +02009G1. Use PSA Crypto drivers when available.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020010G2. Allow isolation of long-term secrets (for example, private keys).
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +010011G3. Allow isolation of short-term secrets (for example, TLS session keys).
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020012G4. Have a clean, unified API for Crypto (retire the legacy API).
Manuel Pégourié-Gonnard74979912021-10-27 14:00:08 +020013G5. Code size: compile out our implementation when a driver is available.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020014
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020015As of Mbed TLS 3.2, most of (G1) and all of (G2) is implemented when
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020016`MBEDTLS_USE_PSA_CRYPTO` is enabled. For (G2) to take effect, the application
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020017needs to be changed to use new APIs. For a more detailled account of what's
18implemented, see `docs/use-psa-crypto.md`, where new APIs are about (G2), and
19internal changes implement (G1).
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020020
21Generally speaking, the numbering above doesn't mean that each goal requires
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020022the preceding ones to be completed.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020023
24Compile-time options
25====================
26
27We currently have two compile-time options that are relevant to the migration:
28
29- `MBEDTLS_PSA_CRYPTO_C` - enabled by default, controls the presence of the PSA
30 Crypto APIs.
31- `MBEDTLS_USE_PSA_CRYPTO` - disabled by default (enabled in "full" config),
32 controls usage of PSA Crypto APIs to perform operations in X.509 and TLS
33(G1 above), as well as the availability of some new APIs (G2 above).
34
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020035The reasons why `MBEDTLS_USE_PSA_CRYPTO` is optional and disabled by default
36are:
Manuel Pégourié-Gonnardce6c0872022-02-01 10:34:20 +010037- it's incompatible with `MBEDTLS_ECP_RESTARTABLE`;
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010038- it does not work well with `MBEDTLS_PSA_CRYPTO_CONFIG` (could compile with
39 both of them, but then `MBEDTLS_PSA_CRYPTO_CONFIG` won't have the desired
40effect)
41- to avoid a hard/default dependency of TLS, X.509 and PK on
Manuel Pégourié-Gonnard80759c42022-02-08 10:33:11 +010042 `MBEDTLS_PSA_CRYPTO_C`, for backward compatibility reasons:
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +020043 - When `MBEDTLS_PSA_CRYPTO_C` is enabled and used, applications need to call
44 `psa_crypto_init()` before TLS/X.509 uses PSA functions. (This prevents us
45from even enabling the option by default.)
Manuel Pégourié-Gonnardce6c0872022-02-01 10:34:20 +010046 - `MBEDTLS_PSA_CRYPTO_C` has a hard depend on `MBEDTLS_ENTROPY_C ||
47 MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` but it's
48 currently possible to compilte TLS and X.509 without any of the options.
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010049 Also, we can't just auto-enable `MBEDTLS_ENTROPY_C` as it doesn't build
Manuel Pégourié-Gonnardce6c0872022-02-01 10:34:20 +010050 out of the box on all platforms, and even less
51 `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` as it requires a user-provided RNG
52 function.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020053
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020054The downside of this approach is that until we are able to make
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020055`MBDEDTLS_USE_PSA_CRYPTO` non-optional (always enabled), we have to maintain
56two versions of some parts of the code: one using PSA, the other using the
57legacy APIs. However, see next section for strategies that can lower that
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020058cost. The rest of this section explains the reasons for the
59incompatibilities mentioned above.
60
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +020061At the time of writing (early 2022) it is unclear what could be done about the
62backward compatibility issues, and in particular if the cost of implementing
63solutions to these problems would be higher or lower than the cost of
64maintaining dual code paths until the next major version. (Note: these
65solutions would probably also solve other problems at the same time.)
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010066
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020067### `MBEDTLS_ECP_RESTARTABLE`
68
69Currently this option controls not only the presence of restartable APIs in
70the crypto library, but also their use in the TLS and X.509 layers. Since PSA
71Crypto does not support restartable operations, there's a clear conflict: the
72TLS and X.509 layers can't both use only PSA APIs and get restartable
73behaviour.
74
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020075Supporting this in PSA is on our roadmap and currently planned for end of
762022, see <https://github.com/orgs/Mbed-TLS/projects/1#column-18883250>.
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020077
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020078It will then require follow-up work to make use of the new PSA API in
79PK/X.509/TLS in all places where we currently allow restartable operations.
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020080
81### `MBEDTLS_PSA_CRYPTO_CONFIG`
82
Manuel Pégourié-Gonnardce6c0872022-02-01 10:34:20 +010083(This section taken from a comment by Gilles.)
84
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020085X509 and TLS code use `MBEDTLS_xxx` macros to decide whether an algorithm is
86supported. This doesn't make `MBEDTLS_USE_PSA_CRYPTO` incompatible with
87`MBEDTLS_PSA_CRYPTO_CONFIG` per se, but it makes it incompatible with most
88useful uses of `MBEDTLS_PSA_CRYPTO_CONFIG`. The point of
89`MBEDTLS_PSA_CRYPTO_CONFIG` is to be able to build a library with support for
90an algorithm through a PSA driver only, without building the software
91implementation of that algorithm. But then the TLS code would consider the
92algorithm unavailable.
93
Dave Rodgman017a1992022-03-31 14:07:01 +010094This is tracked in https://github.com/Mbed-TLS/mbedtls/issues/3674 and
95https://github.com/Mbed-TLS/mbedtls/issues/3677. But now that I look at it with
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020096fresh eyes, I don't think the approach we were planning to use would actually
97works. This needs more design effort.
98
99This is something we need to support eventually, and several partners want it.
100I don't know what the priority is for `MBEDTLS_USE_PSA_CRYPTO` between
101improving driver support and covering more of the protocol. It seems to me
102that it'll be less work overall to first implement a good architecture for
103`MBEDTLS_USE_PSA_CRYPTO + MBEDTLS_PSA_CRYPTO_CONFIG` and then extend to more
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100104protocol features, because implementing that architecture will require changes
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +0200105to the existing code and the less code there is at this point the better,
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100106whereas extending to more protocol features will require the same amount of
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +0200107work either way.
108
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +0200109### Backward compatibility issues with making `MBEDTLS_USE_PSA_CRYPTO` always on
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100110
1111. Existing applications may not be calling `psa_crypto_init()` before using
112 TLS, X.509 or PK. We can try to work around that by calling (the relevant
113part of) it ourselves under the hood as needed, but that would likely require
114splitting init between the parts that can fail and the parts that can't (see
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +0200115<https://github.com/ARM-software/psa-crypto-api/pull/536> for that).
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +01001162. It's currently not possible to enable `MBEDTLS_PSA_CRYPTO_C` in
117 configurations that don't have `MBEDTLS_ENTROPY_C`, and we can't just
118auto-enable the latter, as it won't build or work out of the box on all
119platforms. There are two kinds of things we'd need to do if we want to work
120around that:
121 1. Make it possible to enable the parts of PSA Crypto that don't require an
122 RNG (typically, public key operations, symmetric crypto, some key
123management functions (destroy etc)) in configurations that don't have
124`ENTROPY_C`. This requires going through the PSA code base to adjust
125dependencies. Risk: there may be annoying dependencies, some of which may be
126surprising.
127 2. For operations that require an RNG, provide an alternative function
128 accepting an explicit `f_rng` parameter (see #5238), that would be
129available in entropy-less builds. (Then code using those functions still needs
130to have one version using it, for entropy-less builds, and one version using
131the standard function, for driver support in build with entropy.)
132
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +0200133See <https://github.com/Mbed-TLS/mbedtls/issues/5156>.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200134
135Taking advantage of the existing abstractions layers - or not
136=============================================================
137
138The Crypto library in Mbed TLS currently has 3 abstraction layers that offer
139algorithm-agnostic APIs for a class of algorithms:
140
141- MD for messages digests aka hashes (including HMAC)
142- Cipher for symmetric ciphers (included AEAD)
143- PK for asymmetric (aka public-key) cryptography (excluding key exchange)
144
145Note: key exchange (FFDH, ECDH) is not covered by an abstraction layer.
146
147These abstraction layers typically provide, in addition to the API for crypto
148operations, types and numerical identifiers for algorithms (for
149example `mbedtls_cipher_mode_t` and its values). The
150current strategy is to keep using those identifiers in most of the code, in
151particular in existing structures and public APIs, even when
152`MBEDTLS_USE_PSA_CRYPTO` is enabled. (This is not an issue for G1, G2, G3
153above, and is only potentially relevant for G4.)
154
155The are multiple strategies that can be used regarding the place of those
156layers in the migration to PSA.
157
158Silently call to PSA from the abstraction layer
159-----------------------------------------------
160
161- Provide a new definition (conditionally on `USE_PSA_CRYPTO`) of wrapper
162 functions in the abstraction layer, that calls PSA instead of the legacy
163crypto API.
164- Upside: changes contained to a single place, no need to change TLS or X.509
165 code anywhere.
166- Downside: tricky to implement if the PSA implementation is currently done on
167 top of that layer (dependency loop).
168
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200169This strategy is currently (early 2022) used for all operations in the PK
170layer.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200171
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200172This strategy is not very well suited to the Cipher layer, as the PSA
173implementation is currently done on top of that layer.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200174
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100175This strategy will probably be used for some time for the PK layer, while we
176figure out what the future of that layer is: parts of it (parse/write, ECDSA
177signatures in the format that X.509 & TLS want) are not covered by PSA, so
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200178they will need to keep existing in some way. (Also, the PK layer is a good
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100179place for dispatching to either PSA or `mbedtls_xxx_restartable` while that
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200180part is not covered by PSA yet, if we decide to do that.)
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100181
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200182Replace calls for each operation
183--------------------------------
184
185- For every operation that's done through this layer in TLS or X.509, just
186 replace function call with calls to PSA (conditionally on `USE_PSA_CRYPTO`)
187- Upside: conceptually simple, and if the PSA implementation is currently done
188 on top of that layer, avoids concerns about dependency loops.
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100189- Upside: opens the door to building TLS/X.509 without that layer, saving some
190 code size.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200191- Downside: TLS/X.509 code has to be done for each operation.
192
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200193This strategy is currently (early 2022) used for the MD layer and the Cipher
194layer.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200195
196Opt-in use of PSA from the abstraction layer
197--------------------------------------------
198
199- Provide a new way to set up a context that causes operations on that context
200 to be done via PSA.
201- Upside: changes mostly contained in one place, TLS/X.509 code only needs to
202 be changed when setting up the context, but not when using it. In
203 particular, no changes to/duplication of existing public APIs that expect a
204 key to be passed as a context of this layer (eg, `mbedtls_pk_context`).
205- Upside: avoids dependency loop when PSA implemented on top of that layer.
206- Downside: when the context is typically set up by the application, requires
207 changes in application code.
208
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200209This strategy is not useful when no context is used, for example with the
210one-shot function `mbedtls_md()`.
211
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200212There are two variants of this strategy: one where using the new setup
213function also allows for key isolation (the key is only held by PSA,
214supporting both G1 and G2 in that area), and one without isolation (the key is
Manuel Pégourié-Gonnard80759c42022-02-08 10:33:11 +0100215still stored outside of PSA most of the time, supporting only G1).
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200216
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200217This strategy, with support for key isolation, is currently (early 2022) used for
218private-key operations in the PK layer - see `mbedtls_pk_setup_opaque()`. This
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200219allows use of PSA-held private ECDSA keys in TLS and X.509 with no change to
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200220the TLS/X.509 code, but a contained change in the application.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200221
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200222This strategy, without key isolation, was also previously used (until 3.1
223included) in the Cipher layer - see `mbedtls_cipher_setup_psa()`. This allowed
224use of PSA for cipher operations in TLS with no change to the application
225code, and a contained change in TLS code. (It only supported a subset of
226ciphers.)
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200227
228Note: for private key operations in the PK layer, both the "silent" and the
229"opt-in" strategy can apply, and can complement each other, as one provides
230support for key isolation, but at the (unavoidable) code of change in
231application code, while the other requires no application change to get
232support for drivers, but fails to provide isolation support.
233
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200234Summary
235-------
236
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200237Strategies currently (early 2022) used with each abstraction layer:
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200238
239- PK (for G1): silently call PSA
240- PK (for G2): opt-in use of PSA (new key type)
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200241- Cipher (G1): replace calls at each call site
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200242- MD (G1): replace calls at each call site
243
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200244Migrating away from the legacy API
245==================================
246
247This section briefly introduces questions and possible plans towards G4,
248mainly as they relate to choices in previous stages.
249
250The role of the PK/Cipher/MD APIs in user migration
251---------------------------------------------------
252
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +0200253We're currently taking advantage of the existing PK layer in order
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200254to reduce the number of places where library code needs to be changed. It's
255only natural to consider using the same strategy (with the PK, MD and Cipher
256layers) for facilitating migration of application code.
257
258Note: a necessary first step for that would be to make sure PSA is no longer
259implemented of top of the concerned layers
260
261### Zero-cost compatibility layer?
262
263The most favourable case is if we can have a zero-cost abstraction (no
264runtime, RAM usage or code size penalty), for example just a bunch of
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100265`#define`s, essentially mapping `mbedtls_` APIs to their `psa_` equivalent.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200266
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200267Unfortunately that's unlikely to fully work. For example, the MD layer uses the
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200268same context type for hashes and HMACs, while the PSA API (rightfully) has
269distinct operation types. Similarly, the Cipher layer uses the same context
270type for unauthenticated and AEAD ciphers, which again the PSA API
271distinguishes.
272
273It is unclear how much value, if any, a zero-cost compatibility layer that's
274incomplete (for example, for MD covering only hashes, or for Cipher covering
275only AEAD) or differs significantly from the existing API (for example,
276introducing new context types) would provide to users.
277
278### Low-cost compatibility layers?
279
280Another possibility is to keep most or all of the existing API for the PK, MD
281and Cipher layers, implemented on top of PSA, aiming for the lowest possible
282cost. For example, `mbedtls_md_context_t` would be defined as a (tagged) union
283of `psa_hash_operation_t` and `psa_mac_operation_t`, then `mbedtls_md_setup()`
284would initialize the correct part, and the rest of the functions be simple
285wrappers around PSA functions. This would vastly reduce the complexity of the
286layers compared to the existing (no need to dispatch through function
287pointers, just call the corresponding PSA API).
288
289Since this would still represent a non-zero cost, not only in terms of code
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100290size, but also in terms of maintenance (testing, etc.) this would probably
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200291be a temporary solution: for example keep the compatibility layers in 4.0 (and
292make them optional), but remove them in 5.0.
293
294Again, this provides the most value to users if we can manage to keep the
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100295existing API unchanged. Their might be conflicts between this goal and that of
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200296reducing the cost, and judgment calls may need to be made.
297
298Note: when it comes to holding public keys in the PK layer, depending on how
299the rest of the code is structured, it may be worth holding the key data in
300memory controlled by the PK layer as opposed to a PSA key slot, moving it to a
301slot only when needed (see current `ecdsa_verify_wrap` when
302`MBEDTLS_USE_PSA_CRYPTO` is defined) For example, when parsing a large
303number, N, of X.509 certificates (for example the list of trusted roots), it
304might be undesirable to use N PSA key slots for their public keys as long as
305the certs are loaded. OTOH, this could also be addressed by merging the "X.509
306parsing on-demand" (#2478), and then the public key data would be held as
307bytes in the X.509 CRT structure, and only moved to a PK context / PSA slot
308when it's actually used.
309
310Note: the PK layer actually consists of two relatively distinct parts: crypto
311operations, which will be covered by PSA, and parsing/writing (exporting)
312from/to various formats, which is currently not fully covered by the PSA
313Crypto API.
314
315### Algorithm identifiers and other identifiers
316
317It should be easy to provide the user with a bunch of `#define`s for algorithm
318identifiers, for example `#define MBEDTLS_MD_SHA256 PSA_ALG_SHA_256`; most of
319those would be in the MD, Cipher and PK compatibility layers mentioned above,
320but there might be some in other modules that may be worth considering, for
321example identifiers for elliptic curves.
322
323### Lower layers
324
325Generally speaking, we would retire all of the low-level, non-generic modules,
326such as AES, SHA-256, RSA, DHM, ECDH, ECP, bignum, etc, without providing
327compatibility APIs for them. People would be encouraged to switch to the PSA
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100328API. (The compatibility implementation of the existing PK, MD, Cipher APIs
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200329would mostly benefit people who already used those generic APis rather than
330the low-level, alg-specific ones.)
331
332### APIs in TLS and X.509
333
334Public APIs in TLS and X.509 may be affected by the migration in at least two
335ways:
336
3371. APIs that rely on a legacy `mbedtls_` crypto type: for example
338 `mbedtls_ssl_conf_own_cert()` to configure a (certificate and the
339associated) private key. Currently the private key is passed as a
340`mbedtls_pk_context` object, which would probably change to a `psa_key_id_t`.
341Since some users would probably still be using the compatibility PK layer, it
342would need a way to easily extract the PSA key ID from the PK context.
343
3442. APIs the accept list of identifiers: for example
345 `mbedtls_ssl_conf_curves()` taking a list of `mbedtls_ecp_group_id`s. This
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400346could be changed to accept a list of pairs (`psa_ecc_family_t`, size) but we
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100347should probably take this opportunity to move to a identifier independent from
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200348the underlying crypto implementation and use TLS-specific identifiers instead
349(based on IANA values or custom enums), as is currently done in the new
350`mbedtls_ssl_conf_groups()` API, see #4859).
351
352Testing
353-------
354
355An question that needs careful consideration when we come around to removing
356the low-level crypto APIs and making PK, MD and Cipher optional compatibility
357layers is to be sure to preserve testing quality. A lot of the existing test
358cases use the low level crypto APIs; we would need to either keep using that
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200359API for tests, or manually migrate tests to the PSA Crypto API. Perhaps a
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200360combination of both, perhaps evolving gradually over time.