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