xref: /aosp_15_r20/external/mbedtls/docs/architecture/psa-migration/strategy.md (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf EbrahimiThis document explains the strategy that was used so far in starting the
2*62c56f98SSadaf Ebrahimimigration to PSA Crypto and mentions future perspectives and open questions.
3*62c56f98SSadaf Ebrahimi
4*62c56f98SSadaf EbrahimiGoals
5*62c56f98SSadaf Ebrahimi=====
6*62c56f98SSadaf Ebrahimi
7*62c56f98SSadaf EbrahimiSeveral benefits are expected from migrating to PSA Crypto:
8*62c56f98SSadaf Ebrahimi
9*62c56f98SSadaf EbrahimiG1. Use PSA Crypto drivers when available.
10*62c56f98SSadaf EbrahimiG2. Allow isolation of long-term secrets (for example, private keys).
11*62c56f98SSadaf EbrahimiG3. Allow isolation of short-term secrets (for example, TLS session keys).
12*62c56f98SSadaf EbrahimiG4. Have a clean, unified API for Crypto (retire the legacy API).
13*62c56f98SSadaf EbrahimiG5. Code size: compile out our implementation when a driver is available.
14*62c56f98SSadaf Ebrahimi
15*62c56f98SSadaf EbrahimiAs of Mbed TLS 3.2, most of (G1) and all of (G2) is implemented when
16*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is enabled. For (G2) to take effect, the application
17*62c56f98SSadaf Ebrahimineeds to be changed to use new APIs. For a more detailed account of what's
18*62c56f98SSadaf Ebrahimiimplemented, see `docs/use-psa-crypto.md`, where new APIs are about (G2), and
19*62c56f98SSadaf Ebrahimiinternal changes implement (G1).
20*62c56f98SSadaf Ebrahimi
21*62c56f98SSadaf EbrahimiAs of early 2023, work towards G5 is in progress: Mbed TLS 3.3 and 3.4 saw
22*62c56f98SSadaf Ebrahimisome improvements in this area, and more will be coming in future releases.
23*62c56f98SSadaf Ebrahimi
24*62c56f98SSadaf EbrahimiGenerally speaking, the numbering above doesn't mean that each goal requires
25*62c56f98SSadaf Ebrahimithe preceding ones to be completed.
26*62c56f98SSadaf Ebrahimi
27*62c56f98SSadaf Ebrahimi
28*62c56f98SSadaf EbrahimiCompile-time options
29*62c56f98SSadaf Ebrahimi====================
30*62c56f98SSadaf Ebrahimi
31*62c56f98SSadaf EbrahimiWe currently have a few compile-time options that are relevant to the migration:
32*62c56f98SSadaf Ebrahimi
33*62c56f98SSadaf Ebrahimi- `MBEDTLS_PSA_CRYPTO_C` - enabled by default, controls the presence of the PSA
34*62c56f98SSadaf Ebrahimi  Crypto APIs.
35*62c56f98SSadaf Ebrahimi- `MBEDTLS_USE_PSA_CRYPTO` - disabled by default (enabled in "full" config),
36*62c56f98SSadaf Ebrahimi  controls usage of PSA Crypto APIs to perform operations in X.509 and TLS
37*62c56f98SSadaf Ebrahimi(G1 above), as well as the availability of some new APIs (G2 above).
38*62c56f98SSadaf Ebrahimi- `PSA_CRYPTO_CONFIG` - disabled by default, supports builds with drivers and
39*62c56f98SSadaf Ebrahimi  without the corresponding software implementation (G5 above).
40*62c56f98SSadaf Ebrahimi
41*62c56f98SSadaf EbrahimiThe reasons why `MBEDTLS_USE_PSA_CRYPTO` is optional and disabled by default
42*62c56f98SSadaf Ebrahimiare:
43*62c56f98SSadaf Ebrahimi- it's not fully compatible with `MBEDTLS_ECP_RESTARTABLE`: you can enable
44*62c56f98SSadaf Ebrahimi  both, but then you won't get the full effect of RESTARTBLE (see the
45*62c56f98SSadaf Ebrahimidocumentation of this option in `mbedtls_config.h`);
46*62c56f98SSadaf Ebrahimi- to avoid a hard/default dependency of TLS, X.509 and PK on
47*62c56f98SSadaf Ebrahimi  `MBEDTLS_PSA_CRYPTO_C`, for backward compatibility reasons:
48*62c56f98SSadaf Ebrahimi  - When `MBEDTLS_PSA_CRYPTO_C` is enabled and used, applications need to call
49*62c56f98SSadaf Ebrahimi    `psa_crypto_init()` before TLS/X.509 uses PSA functions. (This prevents us
50*62c56f98SSadaf Ebrahimifrom even enabling the option by default.)
51*62c56f98SSadaf Ebrahimi  - `MBEDTLS_PSA_CRYPTO_C` has a hard dependency on `MBEDTLS_ENTROPY_C ||
52*62c56f98SSadaf Ebrahimi    MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` but it's
53*62c56f98SSadaf Ebrahimi    currently possible to compile TLS and X.509 without any of the options.
54*62c56f98SSadaf Ebrahimi    Also, we can't just auto-enable `MBEDTLS_ENTROPY_C` as it doesn't build
55*62c56f98SSadaf Ebrahimi    out of the box on all platforms, and even less
56*62c56f98SSadaf Ebrahimi    `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` as it requires a user-provided RNG
57*62c56f98SSadaf Ebrahimi    function.
58*62c56f98SSadaf Ebrahimi
59*62c56f98SSadaf EbrahimiThe downside of this approach is that until we are able to make
60*62c56f98SSadaf Ebrahimi`MBDEDTLS_USE_PSA_CRYPTO` non-optional (always enabled), we have to maintain
61*62c56f98SSadaf Ebrahimitwo versions of some parts of the code: one using PSA, the other using the
62*62c56f98SSadaf Ebrahimilegacy APIs. However, see next section for strategies that can lower that
63*62c56f98SSadaf Ebrahimicost. The rest of this section explains the reasons for the
64*62c56f98SSadaf Ebrahimiincompatibilities mentioned above.
65*62c56f98SSadaf Ebrahimi
66*62c56f98SSadaf EbrahimiAt the time of writing (early 2022) it is unclear what could be done about the
67*62c56f98SSadaf Ebrahimibackward compatibility issues, and in particular if the cost of implementing
68*62c56f98SSadaf Ebrahimisolutions to these problems would be higher or lower than the cost of
69*62c56f98SSadaf Ebrahimimaintaining dual code paths until the next major version. (Note: these
70*62c56f98SSadaf Ebrahimisolutions would probably also solve other problems at the same time.)
71*62c56f98SSadaf Ebrahimi
72*62c56f98SSadaf Ebrahimi### `MBEDTLS_ECP_RESTARTABLE`
73*62c56f98SSadaf Ebrahimi
74*62c56f98SSadaf EbrahimiCurrently this option controls not only the presence of restartable APIs in
75*62c56f98SSadaf Ebrahimithe crypto library, but also their use in the TLS and X.509 layers. Since PSA
76*62c56f98SSadaf EbrahimiCrypto does not support restartable operations, there's a clear conflict: the
77*62c56f98SSadaf EbrahimiTLS and X.509 layers can't both use only PSA APIs and get restartable
78*62c56f98SSadaf Ebrahimibehaviour.
79*62c56f98SSadaf Ebrahimi
80*62c56f98SSadaf EbrahimiSupport for restartable (aka interruptible) ECDSA sign/verify operation was
81*62c56f98SSadaf Ebrahimiadded to PSA in Mbed TLS 3.4, but support for ECDH is not present yet.
82*62c56f98SSadaf Ebrahimi
83*62c56f98SSadaf EbrahimiIt will then require follow-up work to make use of the new PSA APIs in
84*62c56f98SSadaf EbrahimiPK/X.509/TLS in all places where we currently allow restartable operations.
85*62c56f98SSadaf Ebrahimi
86*62c56f98SSadaf Ebrahimi### Backward compatibility issues with making `MBEDTLS_USE_PSA_CRYPTO` always on
87*62c56f98SSadaf Ebrahimi
88*62c56f98SSadaf Ebrahimi1. Existing applications may not be calling `psa_crypto_init()` before using
89*62c56f98SSadaf Ebrahimi   TLS, X.509 or PK. We can try to work around that by calling (the relevant
90*62c56f98SSadaf Ebrahimipart of) it ourselves under the hood as needed, but that would likely require
91*62c56f98SSadaf Ebrahimisplitting init between the parts that can fail and the parts that can't (see
92*62c56f98SSadaf Ebrahimi<https://github.com/ARM-software/psa-crypto-api/pull/536> for that).
93*62c56f98SSadaf Ebrahimi2. It's currently not possible to enable `MBEDTLS_PSA_CRYPTO_C` in
94*62c56f98SSadaf Ebrahimi   configurations that don't have `MBEDTLS_ENTROPY_C`, and we can't just
95*62c56f98SSadaf Ebrahimiauto-enable the latter, as it won't build or work out of the box on all
96*62c56f98SSadaf Ebrahimiplatforms. There are two kinds of things we'd need to do if we want to work
97*62c56f98SSadaf Ebrahimiaround that:
98*62c56f98SSadaf Ebrahimi   1. Make it possible to enable the parts of PSA Crypto that don't require an
99*62c56f98SSadaf Ebrahimi      RNG (typically, public key operations, symmetric crypto, some key
100*62c56f98SSadaf Ebrahimimanagement functions (destroy etc)) in configurations that don't have
101*62c56f98SSadaf Ebrahimi`ENTROPY_C`. This requires going through the PSA code base to adjust
102*62c56f98SSadaf Ebrahimidependencies. Risk: there may be annoying dependencies, some of which may be
103*62c56f98SSadaf Ebrahimisurprising.
104*62c56f98SSadaf Ebrahimi   2. For operations that require an RNG, provide an alternative function
105*62c56f98SSadaf Ebrahimi      accepting an explicit `f_rng` parameter (see #5238), that would be
106*62c56f98SSadaf Ebrahimiavailable in entropy-less builds. (Then code using those functions still needs
107*62c56f98SSadaf Ebrahimito have one version using it, for entropy-less builds, and one version using
108*62c56f98SSadaf Ebrahimithe standard function, for driver support in build with entropy.)
109*62c56f98SSadaf Ebrahimi
110*62c56f98SSadaf EbrahimiSee <https://github.com/Mbed-TLS/mbedtls/issues/5156>.
111*62c56f98SSadaf Ebrahimi
112*62c56f98SSadaf EbrahimiTaking advantage of the existing abstractions layers - or not
113*62c56f98SSadaf Ebrahimi=============================================================
114*62c56f98SSadaf Ebrahimi
115*62c56f98SSadaf EbrahimiThe Crypto library in Mbed TLS currently has 3 abstraction layers that offer
116*62c56f98SSadaf Ebrahimialgorithm-agnostic APIs for a class of algorithms:
117*62c56f98SSadaf Ebrahimi
118*62c56f98SSadaf Ebrahimi- MD for messages digests aka hashes (including HMAC)
119*62c56f98SSadaf Ebrahimi- Cipher for symmetric ciphers (included AEAD)
120*62c56f98SSadaf Ebrahimi- PK for asymmetric (aka public-key) cryptography (excluding key exchange)
121*62c56f98SSadaf Ebrahimi
122*62c56f98SSadaf EbrahimiNote: key exchange (FFDH, ECDH) is not covered by an abstraction layer.
123*62c56f98SSadaf Ebrahimi
124*62c56f98SSadaf EbrahimiThese abstraction layers typically provide, in addition to the API for crypto
125*62c56f98SSadaf Ebrahimioperations, types and numerical identifiers for algorithms (for
126*62c56f98SSadaf Ebrahimiexample `mbedtls_cipher_mode_t` and its values). The
127*62c56f98SSadaf Ebrahimicurrent strategy is to keep using those identifiers in most of the code, in
128*62c56f98SSadaf Ebrahimiparticular in existing structures and public APIs, even when
129*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is enabled. (This is not an issue for G1, G2, G3
130*62c56f98SSadaf Ebrahimiabove, and is only potentially relevant for G4.)
131*62c56f98SSadaf Ebrahimi
132*62c56f98SSadaf EbrahimiThe are multiple strategies that can be used regarding the place of those
133*62c56f98SSadaf Ebrahimilayers in the migration to PSA.
134*62c56f98SSadaf Ebrahimi
135*62c56f98SSadaf EbrahimiSilently call to PSA from the abstraction layer
136*62c56f98SSadaf Ebrahimi-----------------------------------------------
137*62c56f98SSadaf Ebrahimi
138*62c56f98SSadaf Ebrahimi- Provide a new definition (conditionally on `USE_PSA_CRYPTO`) of wrapper
139*62c56f98SSadaf Ebrahimi  functions in the abstraction layer, that calls PSA instead of the legacy
140*62c56f98SSadaf Ebrahimicrypto API.
141*62c56f98SSadaf Ebrahimi- Upside: changes contained to a single place, no need to change TLS or X.509
142*62c56f98SSadaf Ebrahimi  code anywhere.
143*62c56f98SSadaf Ebrahimi- Downside: tricky to implement if the PSA implementation is currently done on
144*62c56f98SSadaf Ebrahimi  top of that layer (dependency loop).
145*62c56f98SSadaf Ebrahimi
146*62c56f98SSadaf EbrahimiThis strategy is currently (early 2023) used for all operations in the PK
147*62c56f98SSadaf Ebrahimilayer; the MD layer uses a variant where it dispatches to PSA if a driver is
148*62c56f98SSadaf Ebrahimiavailable and the driver subsystem has been initialized, regardless of whether
149*62c56f98SSadaf Ebrahimi`USE_PSA_CRYPTO` is enabled; see `md-cipher-dispatch.md` in the same directory
150*62c56f98SSadaf Ebrahimifor details.
151*62c56f98SSadaf Ebrahimi
152*62c56f98SSadaf EbrahimiThis strategy is not very well suited to the Cipher layer, as the PSA
153*62c56f98SSadaf Ebrahimiimplementation is currently done on top of that layer.
154*62c56f98SSadaf Ebrahimi
155*62c56f98SSadaf EbrahimiThis strategy will probably be used for some time for the PK layer, while we
156*62c56f98SSadaf Ebrahimifigure out what the future of that layer is: parts of it (parse/write, ECDSA
157*62c56f98SSadaf Ebrahimisignatures in the format that X.509 & TLS want) are not covered by PSA, so
158*62c56f98SSadaf Ebrahimithey will need to keep existing in some way. (Also, the PK layer is a good
159*62c56f98SSadaf Ebrahimiplace for dispatching to either PSA or `mbedtls_xxx_restartable` while that
160*62c56f98SSadaf Ebrahimipart is not covered by PSA yet, if we decide to do that.)
161*62c56f98SSadaf Ebrahimi
162*62c56f98SSadaf EbrahimiReplace calls for each operation
163*62c56f98SSadaf Ebrahimi--------------------------------
164*62c56f98SSadaf Ebrahimi
165*62c56f98SSadaf Ebrahimi- For every operation that's done through this layer in TLS or X.509, just
166*62c56f98SSadaf Ebrahimi  replace function call with calls to PSA (conditionally on `USE_PSA_CRYPTO`)
167*62c56f98SSadaf Ebrahimi- Upside: conceptually simple, and if the PSA implementation is currently done
168*62c56f98SSadaf Ebrahimi  on top of that layer, avoids concerns about dependency loops.
169*62c56f98SSadaf Ebrahimi- Upside: opens the door to building TLS/X.509 without that layer, saving some
170*62c56f98SSadaf Ebrahimi  code size.
171*62c56f98SSadaf Ebrahimi- Downside: TLS/X.509 code has to be done for each operation.
172*62c56f98SSadaf Ebrahimi
173*62c56f98SSadaf EbrahimiThis strategy is currently (early 2023) used for the MD layer and the Cipher
174*62c56f98SSadaf Ebrahimilayer in X.509 and TLS. Crypto modules however always call to MD which may
175*62c56f98SSadaf Ebrahimithen dispatch to PSA, see `md-cipher-dispatch.md`.
176*62c56f98SSadaf Ebrahimi
177*62c56f98SSadaf EbrahimiOpt-in use of PSA from the abstraction layer
178*62c56f98SSadaf Ebrahimi--------------------------------------------
179*62c56f98SSadaf Ebrahimi
180*62c56f98SSadaf Ebrahimi- Provide a new way to set up a context that causes operations on that context
181*62c56f98SSadaf Ebrahimi  to be done via PSA.
182*62c56f98SSadaf Ebrahimi- Upside: changes mostly contained in one place, TLS/X.509 code only needs to
183*62c56f98SSadaf Ebrahimi  be changed when setting up the context, but not when using it. In
184*62c56f98SSadaf Ebrahimi  particular, no changes to/duplication of existing public APIs that expect a
185*62c56f98SSadaf Ebrahimi  key to be passed as a context of this layer (eg, `mbedtls_pk_context`).
186*62c56f98SSadaf Ebrahimi- Upside: avoids dependency loop when PSA implemented on top of that layer.
187*62c56f98SSadaf Ebrahimi- Downside: when the context is typically set up by the application, requires
188*62c56f98SSadaf Ebrahimi  changes in application code.
189*62c56f98SSadaf Ebrahimi
190*62c56f98SSadaf EbrahimiThis strategy is not useful when no context is used, for example with the
191*62c56f98SSadaf Ebrahimione-shot function `mbedtls_md()`.
192*62c56f98SSadaf Ebrahimi
193*62c56f98SSadaf EbrahimiThere are two variants of this strategy: one where using the new setup
194*62c56f98SSadaf Ebrahimifunction also allows for key isolation (the key is only held by PSA,
195*62c56f98SSadaf Ebrahimisupporting both G1 and G2 in that area), and one without isolation (the key is
196*62c56f98SSadaf Ebrahimistill stored outside of PSA most of the time, supporting only G1).
197*62c56f98SSadaf Ebrahimi
198*62c56f98SSadaf EbrahimiThis strategy, with support for key isolation, is currently (early 2022) used for
199*62c56f98SSadaf Ebrahimiprivate-key operations in the PK layer - see `mbedtls_pk_setup_opaque()`. This
200*62c56f98SSadaf Ebrahimiallows use of PSA-held private ECDSA keys in TLS and X.509 with no change to
201*62c56f98SSadaf Ebrahimithe TLS/X.509 code, but a contained change in the application.
202*62c56f98SSadaf Ebrahimi
203*62c56f98SSadaf EbrahimiThis strategy, without key isolation, was also previously used (until 3.1
204*62c56f98SSadaf Ebrahimiincluded) in the Cipher layer - see `mbedtls_cipher_setup_psa()`. This allowed
205*62c56f98SSadaf Ebrahimiuse of PSA for cipher operations in TLS with no change to the application
206*62c56f98SSadaf Ebrahimicode, and a contained change in TLS code. (It only supported a subset of
207*62c56f98SSadaf Ebrahimiciphers.)
208*62c56f98SSadaf Ebrahimi
209*62c56f98SSadaf EbrahimiNote: for private key operations in the PK layer, both the "silent" and the
210*62c56f98SSadaf Ebrahimi"opt-in" strategy can apply, and can complement each other, as one provides
211*62c56f98SSadaf Ebrahimisupport for key isolation, but at the (unavoidable) code of change in
212*62c56f98SSadaf Ebrahimiapplication code, while the other requires no application change to get
213*62c56f98SSadaf Ebrahimisupport for drivers, but fails to provide isolation support.
214*62c56f98SSadaf Ebrahimi
215*62c56f98SSadaf EbrahimiSummary
216*62c56f98SSadaf Ebrahimi-------
217*62c56f98SSadaf Ebrahimi
218*62c56f98SSadaf EbrahimiStrategies currently (early 2022) used with each abstraction layer:
219*62c56f98SSadaf Ebrahimi
220*62c56f98SSadaf Ebrahimi- PK (for G1): silently call PSA
221*62c56f98SSadaf Ebrahimi- PK (for G2): opt-in use of PSA (new key type)
222*62c56f98SSadaf Ebrahimi- Cipher (G1): replace calls at each call site
223*62c56f98SSadaf Ebrahimi- MD (G1, X.509 and TLS): replace calls at each call site (depending on
224*62c56f98SSadaf Ebrahimi  `USE_PSA_CRYPTO`)
225*62c56f98SSadaf Ebrahimi- MD (G5): silently call PSA when a driver is available, see
226*62c56f98SSadaf Ebrahimi  `md-cipher-dispatch.md`.
227*62c56f98SSadaf Ebrahimi
228*62c56f98SSadaf Ebrahimi
229*62c56f98SSadaf EbrahimiSupporting builds with drivers without the software implementation
230*62c56f98SSadaf Ebrahimi==================================================================
231*62c56f98SSadaf Ebrahimi
232*62c56f98SSadaf EbrahimiThis section presents a plan towards G5: save code size by compiling out our
233*62c56f98SSadaf Ebrahimisoftware implementation when a driver is available.
234*62c56f98SSadaf Ebrahimi
235*62c56f98SSadaf EbrahimiLet's expand a bit on the definition of the goal: in such a configuration
236*62c56f98SSadaf Ebrahimi(driver used, software implementation and abstraction layer compiled out),
237*62c56f98SSadaf Ebrahimiwe want:
238*62c56f98SSadaf Ebrahimi
239*62c56f98SSadaf Ebrahimia. the library to build in a reasonably-complete configuration,
240*62c56f98SSadaf Ebrahimib. with all tests passing,
241*62c56f98SSadaf Ebrahimic. and no more tests skipped than the same configuration with software
242*62c56f98SSadaf Ebrahimi   implementation.
243*62c56f98SSadaf Ebrahimi
244*62c56f98SSadaf EbrahimiCriterion (c) ensures not only test coverage, but that driver-based builds are
245*62c56f98SSadaf Ebrahimiat feature parity with software-based builds.
246*62c56f98SSadaf Ebrahimi
247*62c56f98SSadaf EbrahimiWe can roughly divide the work needed to get there in the following steps:
248*62c56f98SSadaf Ebrahimi
249*62c56f98SSadaf Ebrahimi0. Have a working driver interface for the algorithms we want to replace.
250*62c56f98SSadaf Ebrahimi1. Have users of these algorithms call to PSA or an abstraction layer than can
251*62c56f98SSadaf Ebrahimi   dispatch to PSA, but not the low-level legacy API, for all operations.
252*62c56f98SSadaf Ebrahimi(This is G1, and for PK, X.509 and TLS this is controlled by
253*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO`.) This needs to be done in the library and tests.
254*62c56f98SSadaf Ebrahimi2. Have users of these algorithms not depend on the legacy API for information
255*62c56f98SSadaf Ebrahimi   management (getting a size for a given algorithm, etc.)
256*62c56f98SSadaf Ebrahimi3. Adapt compile-time guards used to query availability of a given algorithm;
257*62c56f98SSadaf Ebrahimi   this needs to be done in the library (for crypto operations and data) and
258*62c56f98SSadaf Ebrahimitests.
259*62c56f98SSadaf Ebrahimi
260*62c56f98SSadaf EbrahimiNote: the first two steps enable use of drivers, but not by themselves removal
261*62c56f98SSadaf Ebrahimiof the software implementation.
262*62c56f98SSadaf Ebrahimi
263*62c56f98SSadaf EbrahimiNote: the fact that step 1 is not achieved for all of libmbedcrypto (see
264*62c56f98SSadaf Ebrahimibelow) is the reason why criterion (a) has "a reasonably-complete
265*62c56f98SSadaf Ebrahimiconfiguration", to allow working around internal crypto dependencies when
266*62c56f98SSadaf Ebrahimiworking on other parts such as X.509 and TLS - for example, a configuration
267*62c56f98SSadaf Ebrahimiwithout RSA PKCS#1 v2.1 still allows reasonable use of X.509 and TLS.
268*62c56f98SSadaf Ebrahimi
269*62c56f98SSadaf EbrahimiNote: this is a conceptual division that will sometimes translate to how the
270*62c56f98SSadaf Ebrahimiwork is divided into PRs, sometimes not. For example, in situations where it's
271*62c56f98SSadaf Ebrahiminot possible to achieve good test coverage at the end of step 1 or step 2, it
272*62c56f98SSadaf Ebrahimiis preferable to group with the next step(s) in the same PR until good test
273*62c56f98SSadaf Ebrahimicoverage can be reached.
274*62c56f98SSadaf Ebrahimi
275*62c56f98SSadaf Ebrahimi**Status as of end of March 2023 (shortly after 3.4):**
276*62c56f98SSadaf Ebrahimi
277*62c56f98SSadaf Ebrahimi- Step 0 is achieved for most algorithms, with only a few gaps remaining.
278*62c56f98SSadaf Ebrahimi- Step 1 is achieved for most of PK, X.509, and TLS when
279*62c56f98SSadaf Ebrahimi  `MBEDTLS_USE_PSA_CRYPTO` is enabled with only a few gaps remaining (see
280*62c56f98SSadaf Ebrahimi  docs/use-psa-crypto.md).
281*62c56f98SSadaf Ebrahimi- Step 1 is achieved for the crypto library regarding hashes: everything uses
282*62c56f98SSadaf Ebrahimi  MD (not low-level hash APIs), which then dispatches to PSA if applicable.
283*62c56f98SSadaf Ebrahimi- Step 1 is not achieved for all of the crypto library when it come to
284*62c56f98SSadaf Ebrahimi  ciphers. For example,`ctr_drbg.c` calls the legacy API `mbedtls_aes`.
285*62c56f98SSadaf Ebrahimi- Step 2 is achieved for most of X.509 and TLS (same gaps as step 1) when
286*62c56f98SSadaf Ebrahimi  `MBEDTLS_USE_PSA_CRYPTO` is enabled.
287*62c56f98SSadaf Ebrahimi- Step 3 is done for hashes and top-level ECC modules (ECDSA, ECDH, ECJPAKE).
288*62c56f98SSadaf Ebrahimi
289*62c56f98SSadaf Ebrahimi**Strategy for step 1:**
290*62c56f98SSadaf Ebrahimi
291*62c56f98SSadaf EbrahimiRegarding PK, X.509, and TLS, this is mostly achieved with only a few gaps.
292*62c56f98SSadaf Ebrahimi(The strategy was outlined in the previous section.)
293*62c56f98SSadaf Ebrahimi
294*62c56f98SSadaf EbrahimiRegarding libmbedcrypto:
295*62c56f98SSadaf Ebrahimi- for hashes and ciphers, see `md-cipher-dispatch.md` in the same directory;
296*62c56f98SSadaf Ebrahimi- for ECC, we have no internal uses of the top-level algorithms (ECDSA, ECDH,
297*62c56f98SSadaf Ebrahimi  ECJPAKE), however they all depend on `ECP_C` which in turn depends on
298*62c56f98SSadaf Ebrahimi`BIGNUM_C`. So, direct calls from TLS, X.509 and PK to ECP and Bignum will
299*62c56f98SSadaf Ebrahimineed to be replaced; see <https://github.com/Mbed-TLS/mbedtls/issues/6839> and
300*62c56f98SSadaf Ebrahimilinked issues for a summary of intermediate steps and open points.
301*62c56f98SSadaf Ebrahimi
302*62c56f98SSadaf Ebrahimi**Strategy for step 2:**
303*62c56f98SSadaf Ebrahimi
304*62c56f98SSadaf EbrahimiThe most satisfying situation here is when we can just use the PSA Crypto API
305*62c56f98SSadaf Ebrahimifor information management as well. However sometimes it may not be
306*62c56f98SSadaf Ebrahimiconvenient, for example in parts of the code that accept old-style identifiers
307*62c56f98SSadaf Ebrahimi(such as `mbedtls_md_type_t`) in their API and can't assume PSA to be
308*62c56f98SSadaf Ebrahimicompiled in (such as `rsa.c`).
309*62c56f98SSadaf Ebrahimi
310*62c56f98SSadaf EbrahimiWhen using an existing abstraction layer such as MD, it can provide
311*62c56f98SSadaf Ebrahimiinformation management functions. In other cases, information that was in a
312*62c56f98SSadaf Ebrahimilow-level module but logically belongs in a higher-level module can be moved
313*62c56f98SSadaf Ebrahimito that module (for example, TLS identifiers of curves and there conversion
314*62c56f98SSadaf Ebrahimito/from PSA or legacy identifiers belongs in TLS, not `ecp.c`).
315*62c56f98SSadaf Ebrahimi
316*62c56f98SSadaf Ebrahimi**Strategy for step 3:**
317*62c56f98SSadaf Ebrahimi
318*62c56f98SSadaf EbrahimiThere are currently two (complementary) ways for crypto-using code to check if a
319*62c56f98SSadaf Ebrahimiparticular algorithm is supported: using `MBEDTLS_xxx` macros, and using
320*62c56f98SSadaf Ebrahimi`PSA_WANT_xxx` macros. For example, PSA-based code that want to use SHA-256
321*62c56f98SSadaf Ebrahimiwill check for `PSA_WANT_ALG_SHA_256`, while legacy-based code that wants to
322*62c56f98SSadaf Ebrahimiuse SHA-256 will check for `MBEDTLS_SHA256_C` if using the `mbedtls_sha256`
323*62c56f98SSadaf EbrahimiAPI, or for `MBEDTLS_MD_C && MBEDTLS_SHA256_C` if using the `mbedtls_md` API.
324*62c56f98SSadaf Ebrahimi
325*62c56f98SSadaf EbrahimiCode that obeys `MBEDTLS_USE_PSA_CRYPTO` will want to use one of the two
326*62c56f98SSadaf Ebrahimidependencies above depending on whether `MBEDTLS_USE_PSA_CRYPTO` is defined:
327*62c56f98SSadaf Ebrahimiif it is, the code want the algorithm available in PSA, otherwise, it wants it
328*62c56f98SSadaf Ebrahimiavailable via the legacy API(s) is it using (MD and/or low-level).
329*62c56f98SSadaf Ebrahimi
330*62c56f98SSadaf EbrahimiAs much as possible, we're trying to create for each algorithm a single new
331*62c56f98SSadaf Ebrahimimacro that can be used to express dependencies everywhere (except pure PSA
332*62c56f98SSadaf Ebrahimicode that should always use `PSA_WANT`). For example, for hashes this is the
333*62c56f98SSadaf Ebrahimi`MBEDTLS_MD_CAN_xxx` family. For ECC algorithms, we have similar
334*62c56f98SSadaf Ebrahimi`MBEDTLS_PK_CAN_xxx` macros.
335*62c56f98SSadaf Ebrahimi
336*62c56f98SSadaf EbrahimiNote that in order to achieve that goal, even for code that obeys
337*62c56f98SSadaf Ebrahimi`USE_PSA_CRYPTO`, it is useful to impose that all algorithms that are
338*62c56f98SSadaf Ebrahimiavailable via the legacy APIs are also available via PSA.
339*62c56f98SSadaf Ebrahimi
340*62c56f98SSadaf EbrahimiExecuting step 3 will mostly consist of using the right dependency macros in
341*62c56f98SSadaf Ebrahimithe right places (once the previous steps are done).
342*62c56f98SSadaf Ebrahimi
343*62c56f98SSadaf Ebrahimi**Note on testing**
344*62c56f98SSadaf Ebrahimi
345*62c56f98SSadaf EbrahimiSince supporting driver-only builds is not about adding features, but about
346*62c56f98SSadaf Ebrahimisupporting existing features in new types of builds, testing will not involve
347*62c56f98SSadaf Ebrahimiadding cases to the test suites, but instead adding new components in `all.sh`
348*62c56f98SSadaf Ebrahimithat build and run tests in newly-supported configurations. For example, if
349*62c56f98SSadaf Ebrahimiwe're making some part of the library work with hashes provided only by
350*62c56f98SSadaf Ebrahimidrivers when `MBEDTLS_USE_PSA_CRYPTO` is defined, there should be a place in
351*62c56f98SSadaf Ebrahimi`all.sh` that builds and run tests in such a configuration.
352*62c56f98SSadaf Ebrahimi
353*62c56f98SSadaf EbrahimiThere is however a risk, especially in step 3 where we change how dependencies
354*62c56f98SSadaf Ebrahimiare expressed (sometimes in bulk), to get things wrong in a way that would
355*62c56f98SSadaf Ebrahimiresult in more tests being skipped, which is easy to miss. Care must be
356*62c56f98SSadaf Ebrahimitaken to ensure this does not happen. The following criteria can be used:
357*62c56f98SSadaf Ebrahimi
358*62c56f98SSadaf Ebrahimi1. The sets of tests skipped in the default config and the full config must be
359*62c56f98SSadaf Ebrahimi  the same before and after the PR that implements step 3. This is tested
360*62c56f98SSadaf Ebrahimimanually for each PR that changes dependency declarations by using the script
361*62c56f98SSadaf Ebrahimi`outcome-analysis.sh` in the present directory.
362*62c56f98SSadaf Ebrahimi2. The set of tests skipped in the driver-only build is the same as in an
363*62c56f98SSadaf Ebrahimi  equivalent software-based configuration. This is tested automatically by the
364*62c56f98SSadaf EbrahimiCI in the "Results analysis" stage, by running
365*62c56f98SSadaf Ebrahimi`tests/scripts/analyze_outcomes.py`. See the
366*62c56f98SSadaf Ebrahimi`analyze_driver_vs_reference_xxx` actions in the script and the comments above
367*62c56f98SSadaf Ebrahimitheir declaration for how to do that locally.
368*62c56f98SSadaf Ebrahimi
369*62c56f98SSadaf Ebrahimi
370*62c56f98SSadaf EbrahimiMigrating away from the legacy API
371*62c56f98SSadaf Ebrahimi==================================
372*62c56f98SSadaf Ebrahimi
373*62c56f98SSadaf EbrahimiThis section briefly introduces questions and possible plans towards G4,
374*62c56f98SSadaf Ebrahimimainly as they relate to choices in previous stages.
375*62c56f98SSadaf Ebrahimi
376*62c56f98SSadaf EbrahimiThe role of the PK/Cipher/MD APIs in user migration
377*62c56f98SSadaf Ebrahimi---------------------------------------------------
378*62c56f98SSadaf Ebrahimi
379*62c56f98SSadaf EbrahimiWe're currently taking advantage of the existing PK layer in order
380*62c56f98SSadaf Ebrahimito reduce the number of places where library code needs to be changed. It's
381*62c56f98SSadaf Ebrahimionly natural to consider using the same strategy (with the PK, MD and Cipher
382*62c56f98SSadaf Ebrahimilayers) for facilitating migration of application code.
383*62c56f98SSadaf Ebrahimi
384*62c56f98SSadaf EbrahimiNote: a necessary first step for that would be to make sure PSA is no longer
385*62c56f98SSadaf Ebrahimiimplemented of top of the concerned layers
386*62c56f98SSadaf Ebrahimi
387*62c56f98SSadaf Ebrahimi### Zero-cost compatibility layer?
388*62c56f98SSadaf Ebrahimi
389*62c56f98SSadaf EbrahimiThe most favourable case is if we can have a zero-cost abstraction (no
390*62c56f98SSadaf Ebrahimiruntime, RAM usage or code size penalty), for example just a bunch of
391*62c56f98SSadaf Ebrahimi`#define`s, essentially mapping `mbedtls_` APIs to their `psa_` equivalent.
392*62c56f98SSadaf Ebrahimi
393*62c56f98SSadaf EbrahimiUnfortunately that's unlikely to fully work. For example, the MD layer uses the
394*62c56f98SSadaf Ebrahimisame context type for hashes and HMACs, while the PSA API (rightfully) has
395*62c56f98SSadaf Ebrahimidistinct operation types. Similarly, the Cipher layer uses the same context
396*62c56f98SSadaf Ebrahimitype for unauthenticated and AEAD ciphers, which again the PSA API
397*62c56f98SSadaf Ebrahimidistinguishes.
398*62c56f98SSadaf Ebrahimi
399*62c56f98SSadaf EbrahimiIt is unclear how much value, if any, a zero-cost compatibility layer that's
400*62c56f98SSadaf Ebrahimiincomplete (for example, for MD covering only hashes, or for Cipher covering
401*62c56f98SSadaf Ebrahimionly AEAD) or differs significantly from the existing API (for example,
402*62c56f98SSadaf Ebrahimiintroducing new context types) would provide to users.
403*62c56f98SSadaf Ebrahimi
404*62c56f98SSadaf Ebrahimi### Low-cost compatibility layers?
405*62c56f98SSadaf Ebrahimi
406*62c56f98SSadaf EbrahimiAnother possibility is to keep most or all of the existing API for the PK, MD
407*62c56f98SSadaf Ebrahimiand Cipher layers, implemented on top of PSA, aiming for the lowest possible
408*62c56f98SSadaf Ebrahimicost. For example, `mbedtls_md_context_t` would be defined as a (tagged) union
409*62c56f98SSadaf Ebrahimiof `psa_hash_operation_t` and `psa_mac_operation_t`, then `mbedtls_md_setup()`
410*62c56f98SSadaf Ebrahimiwould initialize the correct part, and the rest of the functions be simple
411*62c56f98SSadaf Ebrahimiwrappers around PSA functions. This would vastly reduce the complexity of the
412*62c56f98SSadaf Ebrahimilayers compared to the existing (no need to dispatch through function
413*62c56f98SSadaf Ebrahimipointers, just call the corresponding PSA API).
414*62c56f98SSadaf Ebrahimi
415*62c56f98SSadaf EbrahimiSince this would still represent a non-zero cost, not only in terms of code
416*62c56f98SSadaf Ebrahimisize, but also in terms of maintenance (testing, etc.) this would probably
417*62c56f98SSadaf Ebrahimibe a temporary solution: for example keep the compatibility layers in 4.0 (and
418*62c56f98SSadaf Ebrahimimake them optional), but remove them in 5.0.
419*62c56f98SSadaf Ebrahimi
420*62c56f98SSadaf EbrahimiAgain, this provides the most value to users if we can manage to keep the
421*62c56f98SSadaf Ebrahimiexisting API unchanged. Their might be conflicts between this goal and that of
422*62c56f98SSadaf Ebrahimireducing the cost, and judgment calls may need to be made.
423*62c56f98SSadaf Ebrahimi
424*62c56f98SSadaf EbrahimiNote: when it comes to holding public keys in the PK layer, depending on how
425*62c56f98SSadaf Ebrahimithe rest of the code is structured, it may be worth holding the key data in
426*62c56f98SSadaf Ebrahimimemory controlled by the PK layer as opposed to a PSA key slot, moving it to a
427*62c56f98SSadaf Ebrahimislot only when needed (see current `ecdsa_verify_wrap` when
428*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is defined)  For example, when parsing a large
429*62c56f98SSadaf Ebrahiminumber, N, of X.509 certificates (for example the list of trusted roots), it
430*62c56f98SSadaf Ebrahimimight be undesirable to use N PSA key slots for their public keys as long as
431*62c56f98SSadaf Ebrahimithe certs are loaded. OTOH, this could also be addressed by merging the "X.509
432*62c56f98SSadaf Ebrahimiparsing on-demand" (#2478), and then the public key data would be held as
433*62c56f98SSadaf Ebrahimibytes in the X.509 CRT structure, and only moved to a PK context / PSA slot
434*62c56f98SSadaf Ebrahimiwhen it's actually used.
435*62c56f98SSadaf Ebrahimi
436*62c56f98SSadaf EbrahimiNote: the PK layer actually consists of two relatively distinct parts: crypto
437*62c56f98SSadaf Ebrahimioperations, which will be covered by PSA, and parsing/writing (exporting)
438*62c56f98SSadaf Ebrahimifrom/to various formats, which is currently not fully covered by the PSA
439*62c56f98SSadaf EbrahimiCrypto API.
440*62c56f98SSadaf Ebrahimi
441*62c56f98SSadaf Ebrahimi### Algorithm identifiers and other identifiers
442*62c56f98SSadaf Ebrahimi
443*62c56f98SSadaf EbrahimiIt should be easy to provide the user with a bunch of `#define`s for algorithm
444*62c56f98SSadaf Ebrahimiidentifiers, for example `#define MBEDTLS_MD_SHA256 PSA_ALG_SHA_256`; most of
445*62c56f98SSadaf Ebrahimithose would be in the MD, Cipher and PK compatibility layers mentioned above,
446*62c56f98SSadaf Ebrahimibut there might be some in other modules that may be worth considering, for
447*62c56f98SSadaf Ebrahimiexample identifiers for elliptic curves.
448*62c56f98SSadaf Ebrahimi
449*62c56f98SSadaf Ebrahimi### Lower layers
450*62c56f98SSadaf Ebrahimi
451*62c56f98SSadaf EbrahimiGenerally speaking, we would retire all of the low-level, non-generic modules,
452*62c56f98SSadaf Ebrahimisuch as AES, SHA-256, RSA, DHM, ECDH, ECP, bignum, etc, without providing
453*62c56f98SSadaf Ebrahimicompatibility APIs for them. People would be encouraged to switch to the PSA
454*62c56f98SSadaf EbrahimiAPI. (The compatibility implementation of the existing PK, MD, Cipher APIs
455*62c56f98SSadaf Ebrahimiwould mostly benefit people who already used those generic APis rather than
456*62c56f98SSadaf Ebrahimithe low-level, alg-specific ones.)
457*62c56f98SSadaf Ebrahimi
458*62c56f98SSadaf Ebrahimi### APIs in TLS and X.509
459*62c56f98SSadaf Ebrahimi
460*62c56f98SSadaf EbrahimiPublic APIs in TLS and X.509 may be affected by the migration in at least two
461*62c56f98SSadaf Ebrahimiways:
462*62c56f98SSadaf Ebrahimi
463*62c56f98SSadaf Ebrahimi1. APIs that rely on a legacy `mbedtls_` crypto type: for example
464*62c56f98SSadaf Ebrahimi   `mbedtls_ssl_conf_own_cert()` to configure a (certificate and the
465*62c56f98SSadaf Ebrahimiassociated) private key. Currently the private key is passed as a
466*62c56f98SSadaf Ebrahimi`mbedtls_pk_context` object, which would probably change to a `psa_key_id_t`.
467*62c56f98SSadaf EbrahimiSince some users would probably still be using the compatibility PK layer, it
468*62c56f98SSadaf Ebrahimiwould need a way to easily extract the PSA key ID from the PK context.
469*62c56f98SSadaf Ebrahimi
470*62c56f98SSadaf Ebrahimi2. APIs the accept list of identifiers: for example
471*62c56f98SSadaf Ebrahimi   `mbedtls_ssl_conf_curves()` taking a list of `mbedtls_ecp_group_id`s. This
472*62c56f98SSadaf Ebrahimicould be changed to accept a list of pairs (`psa_ecc_family_t`, size) but we
473*62c56f98SSadaf Ebrahimishould probably take this opportunity to move to a identifier independent from
474*62c56f98SSadaf Ebrahimithe underlying crypto implementation and use TLS-specific identifiers instead
475*62c56f98SSadaf Ebrahimi(based on IANA values or custom enums), as is currently done in the new
476*62c56f98SSadaf Ebrahimi`mbedtls_ssl_conf_groups()` API, see #4859).
477*62c56f98SSadaf Ebrahimi
478*62c56f98SSadaf EbrahimiTesting
479*62c56f98SSadaf Ebrahimi-------
480*62c56f98SSadaf Ebrahimi
481*62c56f98SSadaf EbrahimiAn question that needs careful consideration when we come around to removing
482*62c56f98SSadaf Ebrahimithe low-level crypto APIs and making PK, MD and Cipher optional compatibility
483*62c56f98SSadaf Ebrahimilayers is to be sure to preserve testing quality. A lot of the existing test
484*62c56f98SSadaf Ebrahimicases use the low level crypto APIs; we would need to either keep using that
485*62c56f98SSadaf EbrahimiAPI for tests, or manually migrate tests to the PSA Crypto API. Perhaps a
486*62c56f98SSadaf Ebrahimicombination of both, perhaps evolving gradually over time.
487