blob: a89fe67ad43b0c0de245ab0480b5ac6ff27994f2 [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é-Gonnard8c7b81c2022-07-18 11:17:06 +020017needs to be changed to use new APIs. For a more detailed account of what's
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020018implemented, 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
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +020021As of Mbed TLS 3.6 (early 2024), work towards G5 is well advanced: it is now
Manuel Pégourié-Gonnard72375632024-05-21 11:43:20 +020022possible to have hashes/HMAC, ciphers/AEAD, and ECC provided only by drivers,
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +020023with some limitations. See
24[`docs/driver-only-builds.md`](../../driver-only-builds.html) for details.
Manuel Pégourié-Gonnard72375632024-05-21 11:43:20 +020025The main gap is RSA in PK, X.509 and TLS; it should be resolved by 4.0 work.
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +010026
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020027Generally speaking, the numbering above doesn't mean that each goal requires
Manuel Pégourié-Gonnard72375632024-05-21 11:43:20 +020028the preceding ones to be completed. (As an example, much progress towards G5
29was made in 3.x, while G4 will be mostly 4.0 and probably not fully complete
30until 5.0.)
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020031
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +010032
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020033Compile-time options
34====================
35
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +010036We currently have a few compile-time options that are relevant to the migration:
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020037
38- `MBEDTLS_PSA_CRYPTO_C` - enabled by default, controls the presence of the PSA
39 Crypto APIs.
40- `MBEDTLS_USE_PSA_CRYPTO` - disabled by default (enabled in "full" config),
41 controls usage of PSA Crypto APIs to perform operations in X.509 and TLS
42(G1 above), as well as the availability of some new APIs (G2 above).
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +020043- `PSA_CRYPTO_CONFIG` - disabled by default, supports builds with drivers and
44 without the corresponding software implementation (G5 above).
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020045
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020046The reasons why `MBEDTLS_USE_PSA_CRYPTO` is optional and disabled by default
47are:
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +010048- it's not fully compatible with `MBEDTLS_ECP_RESTARTABLE`: you can enable
49 both, but then you won't get the full effect of RESTARTBLE (see the
50documentation of this option in `mbedtls_config.h`);
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010051- to avoid a hard/default dependency of TLS, X.509 and PK on
Manuel Pégourié-Gonnard80759c42022-02-08 10:33:11 +010052 `MBEDTLS_PSA_CRYPTO_C`, for backward compatibility reasons:
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +020053 - When `MBEDTLS_PSA_CRYPTO_C` is enabled and used, applications need to call
54 `psa_crypto_init()` before TLS/X.509 uses PSA functions. (This prevents us
55from even enabling the option by default.)
Tom Cosgrove0b86ac12022-07-29 13:44:01 +010056 - `MBEDTLS_PSA_CRYPTO_C` has a hard dependency on `MBEDTLS_ENTROPY_C ||
Manuel Pégourié-Gonnardce6c0872022-02-01 10:34:20 +010057 MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` but it's
Tom Cosgrove0b86ac12022-07-29 13:44:01 +010058 currently possible to compile TLS and X.509 without any of the options.
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010059 Also, we can't just auto-enable `MBEDTLS_ENTROPY_C` as it doesn't build
Manuel Pégourié-Gonnardce6c0872022-02-01 10:34:20 +010060 out of the box on all platforms, and even less
61 `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` as it requires a user-provided RNG
62 function.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020063
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020064The downside of this approach is that until we are able to make
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +020065`MBDEDTLS_USE_PSA_CRYPTO` non-optional (always enabled), we have to maintain
66two versions of some parts of the code: one using PSA, the other using the
67legacy APIs. However, see next section for strategies that can lower that
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020068cost. The rest of this section explains the reasons for the
69incompatibilities mentioned above.
70
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +020071At the time of writing (early 2022) it is unclear what could be done about the
72backward compatibility issues, and in particular if the cost of implementing
73solutions to these problems would be higher or lower than the cost of
74maintaining dual code paths until the next major version. (Note: these
75solutions would probably also solve other problems at the same time.)
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010076
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020077### `MBEDTLS_ECP_RESTARTABLE`
78
79Currently this option controls not only the presence of restartable APIs in
80the crypto library, but also their use in the TLS and X.509 layers. Since PSA
81Crypto does not support restartable operations, there's a clear conflict: the
82TLS and X.509 layers can't both use only PSA APIs and get restartable
83behaviour.
84
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +010085Support for restartable (aka interruptible) ECDSA sign/verify operation was
86added to PSA in Mbed TLS 3.4, but support for ECDH is not present yet.
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020087
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +010088It will then require follow-up work to make use of the new PSA APIs in
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020089PK/X.509/TLS in all places where we currently allow restartable operations.
Manuel Pégourié-Gonnarda6c601c2021-10-27 14:12:44 +020090
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020091### Backward compatibility issues with making `MBEDTLS_USE_PSA_CRYPTO` always on
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +010092
931. Existing applications may not be calling `psa_crypto_init()` before using
94 TLS, X.509 or PK. We can try to work around that by calling (the relevant
95part of) it ourselves under the hood as needed, but that would likely require
96splitting init between the parts that can fail and the parts that can't (see
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +020097<https://github.com/ARM-software/psa-crypto-api/pull/536> for that).
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100982. It's currently not possible to enable `MBEDTLS_PSA_CRYPTO_C` in
99 configurations that don't have `MBEDTLS_ENTROPY_C`, and we can't just
100auto-enable the latter, as it won't build or work out of the box on all
101platforms. There are two kinds of things we'd need to do if we want to work
102around that:
103 1. Make it possible to enable the parts of PSA Crypto that don't require an
104 RNG (typically, public key operations, symmetric crypto, some key
105management functions (destroy etc)) in configurations that don't have
106`ENTROPY_C`. This requires going through the PSA code base to adjust
107dependencies. Risk: there may be annoying dependencies, some of which may be
108surprising.
109 2. For operations that require an RNG, provide an alternative function
110 accepting an explicit `f_rng` parameter (see #5238), that would be
111available in entropy-less builds. (Then code using those functions still needs
112to have one version using it, for entropy-less builds, and one version using
113the standard function, for driver support in build with entropy.)
114
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +0200115See <https://github.com/Mbed-TLS/mbedtls/issues/5156>.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200116
117Taking advantage of the existing abstractions layers - or not
118=============================================================
119
120The Crypto library in Mbed TLS currently has 3 abstraction layers that offer
121algorithm-agnostic APIs for a class of algorithms:
122
123- MD for messages digests aka hashes (including HMAC)
124- Cipher for symmetric ciphers (included AEAD)
125- PK for asymmetric (aka public-key) cryptography (excluding key exchange)
126
127Note: key exchange (FFDH, ECDH) is not covered by an abstraction layer.
128
129These abstraction layers typically provide, in addition to the API for crypto
130operations, types and numerical identifiers for algorithms (for
131example `mbedtls_cipher_mode_t` and its values). The
132current strategy is to keep using those identifiers in most of the code, in
133particular in existing structures and public APIs, even when
134`MBEDTLS_USE_PSA_CRYPTO` is enabled. (This is not an issue for G1, G2, G3
135above, and is only potentially relevant for G4.)
136
137The are multiple strategies that can be used regarding the place of those
138layers in the migration to PSA.
139
140Silently call to PSA from the abstraction layer
141-----------------------------------------------
142
143- Provide a new definition (conditionally on `USE_PSA_CRYPTO`) of wrapper
144 functions in the abstraction layer, that calls PSA instead of the legacy
145crypto API.
146- Upside: changes contained to a single place, no need to change TLS or X.509
147 code anywhere.
148- Downside: tricky to implement if the PSA implementation is currently done on
149 top of that layer (dependency loop).
150
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100151This strategy is currently (early 2023) used for all operations in the PK
152layer; the MD layer uses a variant where it dispatches to PSA if a driver is
153available and the driver subsystem has been initialized, regardless of whether
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +0200154`USE_PSA_CRYPTO` is enabled; see [`md-cipher-dispatch.md`](md-cipher-dispatch.html)
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100155for details.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200156
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200157This strategy is not very well suited to the Cipher layer, as the PSA
158implementation is currently done on top of that layer.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200159
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100160This strategy will probably be used for some time for the PK layer, while we
161figure out what the future of that layer is: parts of it (parse/write, ECDSA
162signatures in the format that X.509 & TLS want) are not covered by PSA, so
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200163they will need to keep existing in some way. (Also, the PK layer is a good
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100164place for dispatching to either PSA or `mbedtls_xxx_restartable` while that
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200165part is not covered by PSA yet, if we decide to do that.)
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100166
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200167Replace calls for each operation
168--------------------------------
169
170- For every operation that's done through this layer in TLS or X.509, just
171 replace function call with calls to PSA (conditionally on `USE_PSA_CRYPTO`)
172- Upside: conceptually simple, and if the PSA implementation is currently done
173 on top of that layer, avoids concerns about dependency loops.
Manuel Pégourié-Gonnardec3fd752022-01-17 11:29:18 +0100174- Upside: opens the door to building TLS/X.509 without that layer, saving some
175 code size.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200176- Downside: TLS/X.509 code has to be done for each operation.
177
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100178This strategy is currently (early 2023) used for the MD layer and the Cipher
179layer in X.509 and TLS. Crypto modules however always call to MD which may
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +0200180then dispatch to PSA, see [`md-cipher-dispatch.md`](md-cipher-dispatch.html).
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200181
182Opt-in use of PSA from the abstraction layer
183--------------------------------------------
184
185- Provide a new way to set up a context that causes operations on that context
186 to be done via PSA.
187- Upside: changes mostly contained in one place, TLS/X.509 code only needs to
188 be changed when setting up the context, but not when using it. In
189 particular, no changes to/duplication of existing public APIs that expect a
190 key to be passed as a context of this layer (eg, `mbedtls_pk_context`).
191- Upside: avoids dependency loop when PSA implemented on top of that layer.
192- Downside: when the context is typically set up by the application, requires
193 changes in application code.
194
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200195This strategy is not useful when no context is used, for example with the
196one-shot function `mbedtls_md()`.
197
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200198There are two variants of this strategy: one where using the new setup
199function also allows for key isolation (the key is only held by PSA,
200supporting both G1 and G2 in that area), and one without isolation (the key is
Manuel Pégourié-Gonnard80759c42022-02-08 10:33:11 +0100201still stored outside of PSA most of the time, supporting only G1).
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200202
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200203This strategy, with support for key isolation, is currently (early 2022) used for
204private-key operations in the PK layer - see `mbedtls_pk_setup_opaque()`. This
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200205allows 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 +0200206the TLS/X.509 code, but a contained change in the application.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200207
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200208This strategy, without key isolation, was also previously used (until 3.1
209included) in the Cipher layer - see `mbedtls_cipher_setup_psa()`. This allowed
210use of PSA for cipher operations in TLS with no change to the application
211code, and a contained change in TLS code. (It only supported a subset of
212ciphers.)
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200213
214Note: for private key operations in the PK layer, both the "silent" and the
215"opt-in" strategy can apply, and can complement each other, as one provides
216support for key isolation, but at the (unavoidable) code of change in
217application code, while the other requires no application change to get
218support for drivers, but fails to provide isolation support.
219
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200220Summary
221-------
222
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200223Strategies currently (early 2022) used with each abstraction layer:
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200224
225- PK (for G1): silently call PSA
226- PK (for G2): opt-in use of PSA (new key type)
Manuel Pégourié-Gonnard72375632024-05-21 11:43:20 +0200227- PK (for G5): store keys in PSA-friendly format when `ECP_C` is disabled and
228 `USE_PSA` is enabled
229- Cipher (G1, TLS): replace calls at each call site
230- Cipher (G5): create a new internal abstraction layer for (non-DES) block
231 ciphers that silently calls PSA when a driver is available, see
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +0200232 [`md-cipher-dispatch.md`](md-cipher-dispatch.html).
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100233- MD (G1, X.509 and TLS): replace calls at each call site (depending on
234 `USE_PSA_CRYPTO`)
235- MD (G5): silently call PSA when a driver is available, see
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +0200236 [`md-cipher-dispatch.md`](md-cipher-dispatch.html).
Manuel Pégourié-Gonnard09503592021-10-27 14:21:23 +0200237
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200238
239Supporting builds with drivers without the software implementation
240==================================================================
241
242This section presents a plan towards G5: save code size by compiling out our
243software implementation when a driver is available.
244
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200245Let's expand a bit on the definition of the goal: in such a configuration
246(driver used, software implementation and abstraction layer compiled out),
247we want:
248
249a. the library to build in a reasonably-complete configuration,
250b. with all tests passing,
251c. and no more tests skipped than the same configuration with software
252 implementation.
253
254Criterion (c) ensures not only test coverage, but that driver-based builds are
255at feature parity with software-based builds.
256
257We can roughly divide the work needed to get there in the following steps:
258
2590. Have a working driver interface for the algorithms we want to replace.
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +01002601. Have users of these algorithms call to PSA or an abstraction layer than can
261 dispatch to PSA, but not the low-level legacy API, for all operations.
262(This is G1, and for PK, X.509 and TLS this is controlled by
263`MBEDTLS_USE_PSA_CRYPTO`.) This needs to be done in the library and tests.
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +02002642. Have users of these algorithms not depend on the legacy API for information
265 management (getting a size for a given algorithm, etc.)
2663. Adapt compile-time guards used to query availability of a given algorithm;
267 this needs to be done in the library (for crypto operations and data) and
268tests.
269
270Note: the first two steps enable use of drivers, but not by themselves removal
271of the software implementation.
272
273Note: the fact that step 1 is not achieved for all of libmbedcrypto (see
274below) is the reason why criterion (a) has "a reasonably-complete
275configuration", to allow working around internal crypto dependencies when
276working on other parts such as X.509 and TLS - for example, a configuration
277without RSA PKCS#1 v2.1 still allows reasonable use of X.509 and TLS.
278
Manuel Pégourié-Gonnardfb2ed582022-07-21 11:04:52 +0200279Note: this is a conceptual division that will sometimes translate to how the
280work is divided into PRs, sometimes not. For example, in situations where it's
281not possible to achieve good test coverage at the end of step 1 or step 2, it
282is preferable to group with the next step(s) in the same PR until good test
283coverage can be reached.
284
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100285**Status as of end of March 2023 (shortly after 3.4):**
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200286
287- Step 0 is achieved for most algorithms, with only a few gaps remaining.
288- Step 1 is achieved for most of PK, X.509, and TLS when
289 `MBEDTLS_USE_PSA_CRYPTO` is enabled with only a few gaps remaining (see
290 docs/use-psa-crypto.md).
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100291- Step 1 is achieved for the crypto library regarding hashes: everything uses
292 MD (not low-level hash APIs), which then dispatches to PSA if applicable.
293- Step 1 is not achieved for all of the crypto library when it come to
294 ciphers. For example,`ctr_drbg.c` calls the legacy API `mbedtls_aes`.
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200295- Step 2 is achieved for most of X.509 and TLS (same gaps as step 1) when
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100296 `MBEDTLS_USE_PSA_CRYPTO` is enabled.
297- Step 3 is done for hashes and top-level ECC modules (ECDSA, ECDH, ECJPAKE).
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200298
299**Strategy for step 1:**
300
301Regarding PK, X.509, and TLS, this is mostly achieved with only a few gaps.
Manuel Pégourié-Gonnard8c7b81c2022-07-18 11:17:06 +0200302(The strategy was outlined in the previous section.)
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200303
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100304Regarding libmbedcrypto:
Manuel Pégourié-Gonnard4837f722024-05-23 09:26:31 +0200305- for hashes and ciphers, see [`md-cipher-dispatch.md`](md-cipher-dispatch.html);
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100306- for ECC, we have no internal uses of the top-level algorithms (ECDSA, ECDH,
307 ECJPAKE), however they all depend on `ECP_C` which in turn depends on
308`BIGNUM_C`. So, direct calls from TLS, X.509 and PK to ECP and Bignum will
309need to be replaced; see <https://github.com/Mbed-TLS/mbedtls/issues/6839> and
Manuel Pégourié-Gonnardb38c9c82023-03-24 10:43:28 +0100310linked issues for a summary of intermediate steps and open points.
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200311
312**Strategy for step 2:**
313
314The most satisfying situation here is when we can just use the PSA Crypto API
315for information management as well. However sometimes it may not be
Manuel Pégourié-Gonnard8c7b81c2022-07-18 11:17:06 +0200316convenient, for example in parts of the code that accept old-style identifiers
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200317(such as `mbedtls_md_type_t`) in their API and can't assume PSA to be
318compiled in (such as `rsa.c`).
319
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100320When using an existing abstraction layer such as MD, it can provide
321information management functions. In other cases, information that was in a
322low-level module but logically belongs in a higher-level module can be moved
323to that module (for example, TLS identifiers of curves and there conversion
324to/from PSA or legacy identifiers belongs in TLS, not `ecp.c`).
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200325
326**Strategy for step 3:**
327
Manuel Pégourié-Gonnardf88b1b52022-07-15 11:05:05 +0200328There are currently two (complementary) ways for crypto-using code to check if a
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200329particular algorithm is supported: using `MBEDTLS_xxx` macros, and using
330`PSA_WANT_xxx` macros. For example, PSA-based code that want to use SHA-256
331will check for `PSA_WANT_ALG_SHA_256`, while legacy-based code that wants to
332use SHA-256 will check for `MBEDTLS_SHA256_C` if using the `mbedtls_sha256`
Manuel Pégourié-Gonnard8c7b81c2022-07-18 11:17:06 +0200333API, or for `MBEDTLS_MD_C && MBEDTLS_SHA256_C` if using the `mbedtls_md` API.
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200334
Manuel Pégourié-Gonnardc8c352c2022-07-21 11:01:50 +0200335Code that obeys `MBEDTLS_USE_PSA_CRYPTO` will want to use one of the two
336dependencies above depending on whether `MBEDTLS_USE_PSA_CRYPTO` is defined:
337if it is, the code want the algorithm available in PSA, otherwise, it wants it
338available via the legacy API(s) is it using (MD and/or low-level).
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200339
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100340As much as possible, we're trying to create for each algorithm a single new
341macro that can be used to express dependencies everywhere (except pure PSA
342code that should always use `PSA_WANT`). For example, for hashes this is the
343`MBEDTLS_MD_CAN_xxx` family. For ECC algorithms, we have similar
344`MBEDTLS_PK_CAN_xxx` macros.
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200345
Manuel Pégourié-Gonnard52f7edb2023-03-22 15:56:01 +0100346Note that in order to achieve that goal, even for code that obeys
347`USE_PSA_CRYPTO`, it is useful to impose that all algorithms that are
348available via the legacy APIs are also available via PSA.
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +0200349
Manuel Pégourié-Gonnardc8c352c2022-07-21 11:01:50 +0200350Executing step 3 will mostly consist of using the right dependency macros in
351the right places (once the previous steps are done).
Manuel Pégourié-Gonnardaf8cf5c2022-07-12 11:05:53 +0200352
Manuel Pégourié-Gonnardfb2ed582022-07-21 11:04:52 +0200353**Note on testing**
354
Manuel Pégourié-Gonnardfca4dc62022-07-26 10:10:07 +0200355Since supporting driver-only builds is not about adding features, but about
356supporting existing features in new types of builds, testing will not involve
357adding cases to the test suites, but instead adding new components in `all.sh`
358that build and run tests in newly-supported configurations. For example, if
359we're making some part of the library work with hashes provided only by
360drivers when `MBEDTLS_USE_PSA_CRYPTO` is defined, there should be a place in
361`all.sh` that builds and run tests in such a configuration.
Manuel Pégourié-Gonnardfb2ed582022-07-21 11:04:52 +0200362
363There is however a risk, especially in step 3 where we change how dependencies
364are expressed (sometimes in bulk), to get things wrong in a way that would
365result in more tests being skipped, which is easy to miss. Care must be
366taken to ensure this does not happen. The following criteria can be used:
367
Manuel Pégourié-Gonnard222bc852022-12-29 13:47:25 +01003681. The sets of tests skipped in the default config and the full config must be
369 the same before and after the PR that implements step 3. This is tested
370manually for each PR that changes dependency declarations by using the script
371`outcome-analysis.sh` in the present directory.
3722. The set of tests skipped in the driver-only build is the same as in an
373 equivalent software-based configuration. This is tested automatically by the
374CI in the "Results analysis" stage, by running
Manuel Pégourié-Gonnard5a2e0262023-01-23 12:51:52 +0100375`tests/scripts/analyze_outcomes.py`. See the
Manuel Pégourié-Gonnard222bc852022-12-29 13:47:25 +0100376`analyze_driver_vs_reference_xxx` actions in the script and the comments above
377their declaration for how to do that locally.
Manuel Pégourié-Gonnardfb2ed582022-07-21 11:04:52 +0200378
379
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200380Migrating away from the legacy API
381==================================
382
383This section briefly introduces questions and possible plans towards G4,
384mainly as they relate to choices in previous stages.
385
386The role of the PK/Cipher/MD APIs in user migration
387---------------------------------------------------
388
Manuel Pégourié-Gonnard481846c2022-07-12 09:27:39 +0200389We're currently taking advantage of the existing PK layer in order
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200390to reduce the number of places where library code needs to be changed. It's
391only natural to consider using the same strategy (with the PK, MD and Cipher
392layers) for facilitating migration of application code.
393
394Note: a necessary first step for that would be to make sure PSA is no longer
395implemented of top of the concerned layers
396
397### Zero-cost compatibility layer?
398
399The most favourable case is if we can have a zero-cost abstraction (no
400runtime, RAM usage or code size penalty), for example just a bunch of
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100401`#define`s, essentially mapping `mbedtls_` APIs to their `psa_` equivalent.
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200402
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200403Unfortunately that's unlikely to fully work. For example, the MD layer uses the
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200404same context type for hashes and HMACs, while the PSA API (rightfully) has
405distinct operation types. Similarly, the Cipher layer uses the same context
406type for unauthenticated and AEAD ciphers, which again the PSA API
407distinguishes.
408
409It is unclear how much value, if any, a zero-cost compatibility layer that's
410incomplete (for example, for MD covering only hashes, or for Cipher covering
411only AEAD) or differs significantly from the existing API (for example,
412introducing new context types) would provide to users.
413
414### Low-cost compatibility layers?
415
416Another possibility is to keep most or all of the existing API for the PK, MD
417and Cipher layers, implemented on top of PSA, aiming for the lowest possible
418cost. For example, `mbedtls_md_context_t` would be defined as a (tagged) union
419of `psa_hash_operation_t` and `psa_mac_operation_t`, then `mbedtls_md_setup()`
420would initialize the correct part, and the rest of the functions be simple
421wrappers around PSA functions. This would vastly reduce the complexity of the
422layers compared to the existing (no need to dispatch through function
423pointers, just call the corresponding PSA API).
424
425Since this would still represent a non-zero cost, not only in terms of code
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100426size, but also in terms of maintenance (testing, etc.) this would probably
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200427be a temporary solution: for example keep the compatibility layers in 4.0 (and
428make them optional), but remove them in 5.0.
429
430Again, this provides the most value to users if we can manage to keep the
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100431existing API unchanged. Their might be conflicts between this goal and that of
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200432reducing the cost, and judgment calls may need to be made.
433
434Note: when it comes to holding public keys in the PK layer, depending on how
435the rest of the code is structured, it may be worth holding the key data in
436memory controlled by the PK layer as opposed to a PSA key slot, moving it to a
437slot only when needed (see current `ecdsa_verify_wrap` when
438`MBEDTLS_USE_PSA_CRYPTO` is defined) For example, when parsing a large
439number, N, of X.509 certificates (for example the list of trusted roots), it
440might be undesirable to use N PSA key slots for their public keys as long as
441the certs are loaded. OTOH, this could also be addressed by merging the "X.509
442parsing on-demand" (#2478), and then the public key data would be held as
443bytes in the X.509 CRT structure, and only moved to a PK context / PSA slot
444when it's actually used.
445
446Note: the PK layer actually consists of two relatively distinct parts: crypto
447operations, which will be covered by PSA, and parsing/writing (exporting)
448from/to various formats, which is currently not fully covered by the PSA
449Crypto API.
450
451### Algorithm identifiers and other identifiers
452
453It should be easy to provide the user with a bunch of `#define`s for algorithm
454identifiers, for example `#define MBEDTLS_MD_SHA256 PSA_ALG_SHA_256`; most of
455those would be in the MD, Cipher and PK compatibility layers mentioned above,
456but there might be some in other modules that may be worth considering, for
457example identifiers for elliptic curves.
458
459### Lower layers
460
461Generally speaking, we would retire all of the low-level, non-generic modules,
462such as AES, SHA-256, RSA, DHM, ECDH, ECP, bignum, etc, without providing
463compatibility APIs for them. People would be encouraged to switch to the PSA
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100464API. (The compatibility implementation of the existing PK, MD, Cipher APIs
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200465would mostly benefit people who already used those generic APis rather than
466the low-level, alg-specific ones.)
467
468### APIs in TLS and X.509
469
470Public APIs in TLS and X.509 may be affected by the migration in at least two
471ways:
472
4731. APIs that rely on a legacy `mbedtls_` crypto type: for example
474 `mbedtls_ssl_conf_own_cert()` to configure a (certificate and the
475associated) private key. Currently the private key is passed as a
476`mbedtls_pk_context` object, which would probably change to a `psa_key_id_t`.
477Since some users would probably still be using the compatibility PK layer, it
478would need a way to easily extract the PSA key ID from the PK context.
479
4802. APIs the accept list of identifiers: for example
481 `mbedtls_ssl_conf_curves()` taking a list of `mbedtls_ecp_group_id`s. This
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400482could be changed to accept a list of pairs (`psa_ecc_family_t`, size) but we
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100483should probably take this opportunity to move to a identifier independent from
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200484the underlying crypto implementation and use TLS-specific identifiers instead
485(based on IANA values or custom enums), as is currently done in the new
486`mbedtls_ssl_conf_groups()` API, see #4859).
487
488Testing
489-------
490
491An question that needs careful consideration when we come around to removing
492the low-level crypto APIs and making PK, MD and Cipher optional compatibility
493layers is to be sure to preserve testing quality. A lot of the existing test
494cases use the low level crypto APIs; we would need to either keep using that
Manuel Pégourié-Gonnard2a47d232022-04-20 15:01:13 +0200495API for tests, or manually migrate tests to the PSA Crypto API. Perhaps a
Manuel Pégourié-Gonnardb89fd952021-09-30 11:52:04 +0200496combination of both, perhaps evolving gradually over time.