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 | |
| 21 | Generally speaking, the numbering above doesn't mean that each goal requires |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 22 | the preceding ones to be completed. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 23 | |
| 24 | Compile-time options |
| 25 | ==================== |
| 26 | |
| 27 | We currently have two compile-time options that are relevant to the migration: |
| 28 | |
| 29 | - `MBEDTLS_PSA_CRYPTO_C` - enabled by default, controls the presence of the PSA |
| 30 | Crypto APIs. |
| 31 | - `MBEDTLS_USE_PSA_CRYPTO` - disabled by default (enabled in "full" config), |
| 32 | controls usage of PSA Crypto APIs to perform operations in X.509 and TLS |
| 33 | (G1 above), as well as the availability of some new APIs (G2 above). |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 34 | - `PSA_CRYPTO_CONFIG` - disabled by default, supports builds with drivers and |
| 35 | without the corresponding software implementation (G5 above). |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | a6c601c | 2021-10-27 14:12:44 +0200 | [diff] [blame] | 37 | The reasons why `MBEDTLS_USE_PSA_CRYPTO` is optional and disabled by default |
| 38 | are: |
Manuel Pégourié-Gonnard | ce6c087 | 2022-02-01 10:34:20 +0100 | [diff] [blame] | 39 | - it's incompatible with `MBEDTLS_ECP_RESTARTABLE`; |
Manuel Pégourié-Gonnard | ec3fd75 | 2022-01-17 11:29:18 +0100 | [diff] [blame] | 40 | - 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] | 41 | `MBEDTLS_PSA_CRYPTO_C`, for backward compatibility reasons: |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 42 | - When `MBEDTLS_PSA_CRYPTO_C` is enabled and used, applications need to call |
| 43 | `psa_crypto_init()` before TLS/X.509 uses PSA functions. (This prevents us |
| 44 | from even enabling the option by default.) |
Manuel Pégourié-Gonnard | ce6c087 | 2022-02-01 10:34:20 +0100 | [diff] [blame] | 45 | - `MBEDTLS_PSA_CRYPTO_C` has a hard depend on `MBEDTLS_ENTROPY_C || |
| 46 | MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` but it's |
| 47 | currently possible to compilte TLS and X.509 without any of the options. |
Manuel Pégourié-Gonnard | ec3fd75 | 2022-01-17 11:29:18 +0100 | [diff] [blame] | 48 | 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] | 49 | out of the box on all platforms, and even less |
| 50 | `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` as it requires a user-provided RNG |
| 51 | function. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 53 | 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] | 54 | `MBDEDTLS_USE_PSA_CRYPTO` non-optional (always enabled), we have to maintain |
| 55 | two versions of some parts of the code: one using PSA, the other using the |
| 56 | 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] | 57 | cost. The rest of this section explains the reasons for the |
| 58 | incompatibilities mentioned above. |
| 59 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 60 | At the time of writing (early 2022) it is unclear what could be done about the |
| 61 | backward compatibility issues, and in particular if the cost of implementing |
| 62 | solutions to these problems would be higher or lower than the cost of |
| 63 | maintaining dual code paths until the next major version. (Note: these |
| 64 | 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] | 65 | |
Manuel Pégourié-Gonnard | a6c601c | 2021-10-27 14:12:44 +0200 | [diff] [blame] | 66 | ### `MBEDTLS_ECP_RESTARTABLE` |
| 67 | |
| 68 | Currently this option controls not only the presence of restartable APIs in |
| 69 | the crypto library, but also their use in the TLS and X.509 layers. Since PSA |
| 70 | Crypto does not support restartable operations, there's a clear conflict: the |
| 71 | TLS and X.509 layers can't both use only PSA APIs and get restartable |
| 72 | behaviour. |
| 73 | |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 74 | Supporting this in PSA is on our roadmap and currently planned for end of |
| 75 | 2022, see <https://github.com/orgs/Mbed-TLS/projects/1#column-18883250>. |
Manuel Pégourié-Gonnard | a6c601c | 2021-10-27 14:12:44 +0200 | [diff] [blame] | 76 | |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 77 | It will then require follow-up work to make use of the new PSA API in |
| 78 | 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] | 79 | |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 80 | ### 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] | 81 | |
| 82 | 1. Existing applications may not be calling `psa_crypto_init()` before using |
| 83 | TLS, X.509 or PK. We can try to work around that by calling (the relevant |
| 84 | part of) it ourselves under the hood as needed, but that would likely require |
| 85 | 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] | 86 | <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] | 87 | 2. It's currently not possible to enable `MBEDTLS_PSA_CRYPTO_C` in |
| 88 | configurations that don't have `MBEDTLS_ENTROPY_C`, and we can't just |
| 89 | auto-enable the latter, as it won't build or work out of the box on all |
| 90 | platforms. There are two kinds of things we'd need to do if we want to work |
| 91 | around that: |
| 92 | 1. Make it possible to enable the parts of PSA Crypto that don't require an |
| 93 | RNG (typically, public key operations, symmetric crypto, some key |
| 94 | management functions (destroy etc)) in configurations that don't have |
| 95 | `ENTROPY_C`. This requires going through the PSA code base to adjust |
| 96 | dependencies. Risk: there may be annoying dependencies, some of which may be |
| 97 | surprising. |
| 98 | 2. For operations that require an RNG, provide an alternative function |
| 99 | accepting an explicit `f_rng` parameter (see #5238), that would be |
| 100 | available in entropy-less builds. (Then code using those functions still needs |
| 101 | to have one version using it, for entropy-less builds, and one version using |
| 102 | the standard function, for driver support in build with entropy.) |
| 103 | |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 104 | See <https://github.com/Mbed-TLS/mbedtls/issues/5156>. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 105 | |
| 106 | Taking advantage of the existing abstractions layers - or not |
| 107 | ============================================================= |
| 108 | |
| 109 | The Crypto library in Mbed TLS currently has 3 abstraction layers that offer |
| 110 | algorithm-agnostic APIs for a class of algorithms: |
| 111 | |
| 112 | - MD for messages digests aka hashes (including HMAC) |
| 113 | - Cipher for symmetric ciphers (included AEAD) |
| 114 | - PK for asymmetric (aka public-key) cryptography (excluding key exchange) |
| 115 | |
| 116 | Note: key exchange (FFDH, ECDH) is not covered by an abstraction layer. |
| 117 | |
| 118 | These abstraction layers typically provide, in addition to the API for crypto |
| 119 | operations, types and numerical identifiers for algorithms (for |
| 120 | example `mbedtls_cipher_mode_t` and its values). The |
| 121 | current strategy is to keep using those identifiers in most of the code, in |
| 122 | particular in existing structures and public APIs, even when |
| 123 | `MBEDTLS_USE_PSA_CRYPTO` is enabled. (This is not an issue for G1, G2, G3 |
| 124 | above, and is only potentially relevant for G4.) |
| 125 | |
| 126 | The are multiple strategies that can be used regarding the place of those |
| 127 | layers in the migration to PSA. |
| 128 | |
| 129 | Silently call to PSA from the abstraction layer |
| 130 | ----------------------------------------------- |
| 131 | |
| 132 | - Provide a new definition (conditionally on `USE_PSA_CRYPTO`) of wrapper |
| 133 | functions in the abstraction layer, that calls PSA instead of the legacy |
| 134 | crypto API. |
| 135 | - Upside: changes contained to a single place, no need to change TLS or X.509 |
| 136 | code anywhere. |
| 137 | - Downside: tricky to implement if the PSA implementation is currently done on |
| 138 | top of that layer (dependency loop). |
| 139 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 140 | This strategy is currently (early 2022) used for all operations in the PK |
| 141 | layer. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 0950359 | 2021-10-27 14:21:23 +0200 | [diff] [blame] | 143 | This strategy is not very well suited to the Cipher layer, as the PSA |
| 144 | implementation is currently done on top of that layer. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | ec3fd75 | 2022-01-17 11:29:18 +0100 | [diff] [blame] | 146 | This strategy will probably be used for some time for the PK layer, while we |
| 147 | figure out what the future of that layer is: parts of it (parse/write, ECDSA |
| 148 | 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] | 149 | 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] | 150 | 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] | 151 | 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] | 152 | |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 153 | Replace calls for each operation |
| 154 | -------------------------------- |
| 155 | |
| 156 | - For every operation that's done through this layer in TLS or X.509, just |
| 157 | replace function call with calls to PSA (conditionally on `USE_PSA_CRYPTO`) |
| 158 | - Upside: conceptually simple, and if the PSA implementation is currently done |
| 159 | on top of that layer, avoids concerns about dependency loops. |
Manuel Pégourié-Gonnard | ec3fd75 | 2022-01-17 11:29:18 +0100 | [diff] [blame] | 160 | - Upside: opens the door to building TLS/X.509 without that layer, saving some |
| 161 | code size. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 162 | - Downside: TLS/X.509 code has to be done for each operation. |
| 163 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 164 | This strategy is currently (early 2022) used for the MD layer and the Cipher |
| 165 | layer. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 166 | |
| 167 | Opt-in use of PSA from the abstraction layer |
| 168 | -------------------------------------------- |
| 169 | |
| 170 | - Provide a new way to set up a context that causes operations on that context |
| 171 | to be done via PSA. |
| 172 | - Upside: changes mostly contained in one place, TLS/X.509 code only needs to |
| 173 | be changed when setting up the context, but not when using it. In |
| 174 | particular, no changes to/duplication of existing public APIs that expect a |
| 175 | key to be passed as a context of this layer (eg, `mbedtls_pk_context`). |
| 176 | - Upside: avoids dependency loop when PSA implemented on top of that layer. |
| 177 | - Downside: when the context is typically set up by the application, requires |
| 178 | changes in application code. |
| 179 | |
Manuel Pégourié-Gonnard | 0950359 | 2021-10-27 14:21:23 +0200 | [diff] [blame] | 180 | This strategy is not useful when no context is used, for example with the |
| 181 | one-shot function `mbedtls_md()`. |
| 182 | |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 183 | There are two variants of this strategy: one where using the new setup |
| 184 | function also allows for key isolation (the key is only held by PSA, |
| 185 | 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] | 186 | 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] | 187 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 188 | This strategy, with support for key isolation, is currently (early 2022) used for |
| 189 | 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] | 190 | 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] | 191 | 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] | 192 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 193 | This strategy, without key isolation, was also previously used (until 3.1 |
| 194 | included) in the Cipher layer - see `mbedtls_cipher_setup_psa()`. This allowed |
| 195 | use of PSA for cipher operations in TLS with no change to the application |
| 196 | code, and a contained change in TLS code. (It only supported a subset of |
| 197 | ciphers.) |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 198 | |
| 199 | Note: for private key operations in the PK layer, both the "silent" and the |
| 200 | "opt-in" strategy can apply, and can complement each other, as one provides |
| 201 | support for key isolation, but at the (unavoidable) code of change in |
| 202 | application code, while the other requires no application change to get |
| 203 | support for drivers, but fails to provide isolation support. |
| 204 | |
Manuel Pégourié-Gonnard | 0950359 | 2021-10-27 14:21:23 +0200 | [diff] [blame] | 205 | Summary |
| 206 | ------- |
| 207 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 208 | Strategies currently (early 2022) used with each abstraction layer: |
Manuel Pégourié-Gonnard | 0950359 | 2021-10-27 14:21:23 +0200 | [diff] [blame] | 209 | |
| 210 | - PK (for G1): silently call PSA |
| 211 | - PK (for G2): opt-in use of PSA (new key type) |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 212 | - Cipher (G1): replace calls at each call site |
Manuel Pégourié-Gonnard | 0950359 | 2021-10-27 14:21:23 +0200 | [diff] [blame] | 213 | - MD (G1): replace calls at each call site |
| 214 | |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 215 | |
| 216 | Supporting builds with drivers without the software implementation |
| 217 | ================================================================== |
| 218 | |
| 219 | This section presents a plan towards G5: save code size by compiling out our |
| 220 | software implementation when a driver is available. |
| 221 | |
Manuel Pégourié-Gonnard | 8c7b81c | 2022-07-18 11:17:06 +0200 | [diff] [blame] | 222 | Additionally, we want to save code size by compiling out the |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 223 | abstractions layers that we are not using when `MBEDTLS_USE_PSA_CRYPTO` is |
| 224 | enabled (see previous section): MD and Cipher. |
| 225 | |
| 226 | Let's expand a bit on the definition of the goal: in such a configuration |
| 227 | (driver used, software implementation and abstraction layer compiled out), |
| 228 | we want: |
| 229 | |
| 230 | a. the library to build in a reasonably-complete configuration, |
| 231 | b. with all tests passing, |
| 232 | c. and no more tests skipped than the same configuration with software |
| 233 | implementation. |
| 234 | |
| 235 | Criterion (c) ensures not only test coverage, but that driver-based builds are |
| 236 | at feature parity with software-based builds. |
| 237 | |
| 238 | We can roughly divide the work needed to get there in the following steps: |
| 239 | |
| 240 | 0. Have a working driver interface for the algorithms we want to replace. |
| 241 | 1. Have users of these algorithms call to PSA, not the legacy API, for all |
| 242 | operations. (This is G1, and for PK, X.509 and TLS this is controlled by |
| 243 | `MBEDTLS_USE_PSA_CRYPTO`.) This needs to be done in the library and tests. |
| 244 | 2. Have users of these algorithms not depend on the legacy API for information |
| 245 | management (getting a size for a given algorithm, etc.) |
| 246 | 3. Adapt compile-time guards used to query availability of a given algorithm; |
| 247 | this needs to be done in the library (for crypto operations and data) and |
| 248 | tests. |
| 249 | |
| 250 | Note: the first two steps enable use of drivers, but not by themselves removal |
| 251 | of the software implementation. |
| 252 | |
| 253 | Note: the fact that step 1 is not achieved for all of libmbedcrypto (see |
| 254 | below) is the reason why criterion (a) has "a reasonably-complete |
| 255 | configuration", to allow working around internal crypto dependencies when |
| 256 | working on other parts such as X.509 and TLS - for example, a configuration |
| 257 | without RSA PKCS#1 v2.1 still allows reasonable use of X.509 and TLS. |
| 258 | |
| 259 | **Status as of Mbed TLS 3.2:** |
| 260 | |
| 261 | - Step 0 is achieved for most algorithms, with only a few gaps remaining. |
| 262 | - Step 1 is achieved for most of PK, X.509, and TLS when |
| 263 | `MBEDTLS_USE_PSA_CRYPTO` is enabled with only a few gaps remaining (see |
| 264 | docs/use-psa-crypto.md). |
| 265 | - Step 1 is not achieved for a lot of the crypto library including the PSA |
| 266 | core. For example, `entropy.c` calls the legacy API |
| 267 | `mbedtls_sha256` (or `mbedtls_sha512` optionally); `hmac_drbg.c` calls the |
| 268 | legacy API `mbedtls_md` and `ctr_drbg.c` calls the legacy API `mbedtls_aes`; |
| 269 | the PSA core depends on the entropy module and at least one of the DRBG |
| 270 | modules (unless `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` is used). Further, several |
| 271 | crypto modules have similar issues, for example RSA PKCS#1 v2.1 calls |
| 272 | `mbedtls_md` directly. |
| 273 | - Step 2 is achieved for most of X.509 and TLS (same gaps as step 1) when |
| 274 | `MBEDTLS_USE_PSA_CRYPTO` is enabled - this was tasks like #5795, #5796, |
| 275 | #5797. It is being done in PK and RSA PKCS#1 v1.5 by PR #6065. |
| 276 | - Step 3 was mostly not started at all before 3.2; it is being done for PK by |
| 277 | PR #6065. |
| 278 | |
| 279 | **Strategy for step 1:** |
| 280 | |
| 281 | 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] | 282 | (The strategy was outlined in the previous section.) |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 283 | |
| 284 | Regarding libmbedcrypto, including the PSA Crypto core, this has not been |
| 285 | studied yet. For dependencies outside the PSA Crypto code (such as RSA |
| 286 | PKCS#1 v2.1 depending on MD), it should be checked whether this can be |
| 287 | achieved without backwards compatibility issues (currently applications can |
| 288 | call `mbedtls_rsa_xxx()` functions without calling `psa_crypto_init()` first), |
| 289 | otherwise a new compile-time option might be needed. For dependencies in the |
| 290 | PSA Crypto core, splitting `psa_crypto_init()` might be a topic (which might |
| 291 | also help for dependencies outside the core), with likely questions about |
Manuel Pégourié-Gonnard | 8c7b81c | 2022-07-18 11:17:06 +0200 | [diff] [blame] | 292 | ordering (can we initialize drivers before the RNG or do some drivers expect a |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 293 | working RNG?) and trying to avoid circular dependencies. |
| 294 | |
| 295 | **Strategy for step 2:** |
| 296 | |
| 297 | The most satisfying situation here is when we can just use the PSA Crypto API |
| 298 | 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] | 299 | 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] | 300 | (such as `mbedtls_md_type_t`) in their API and can't assume PSA to be |
| 301 | compiled in (such as `rsa.c`). |
| 302 | |
| 303 | It is suggested that, as a temporary solution until we clean this up |
| 304 | later when removing the legacy API including its identifiers (G4), we may |
| 305 | occasionally use ad-hoc internal functions, such as the one introduced by PR |
| 306 | 6065 in `library/md_internal.h`. |
| 307 | |
| 308 | An alternative would be to have two different code paths depending on whether |
| 309 | `MBEDTLS_PSA_CRYPTO_C` is defined or not. However this is not great for |
| 310 | readability or testability. |
| 311 | |
| 312 | **Strategy for step 3:** |
| 313 | |
Manuel Pégourié-Gonnard | f88b1b5 | 2022-07-15 11:05:05 +0200 | [diff] [blame] | 314 | 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] | 315 | particular algorithm is supported: using `MBEDTLS_xxx` macros, and using |
| 316 | `PSA_WANT_xxx` macros. For example, PSA-based code that want to use SHA-256 |
| 317 | will check for `PSA_WANT_ALG_SHA_256`, while legacy-based code that wants to |
| 318 | 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] | 319 | 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] | 320 | |
| 321 | It is suggested to introduce a new set of macros, `MBEDTLS_USE_PSA_WANT_xxx`, |
| 322 | for use in the parts of the code that use either API depending of whether |
| 323 | `MBEDTLS_USE_PSA_CRYPTO` is enabled (that is, PK, X.509 and TLS 1.2). This is |
| 324 | done for hash algorithms, as an example, by PR 6065. These macros can be used |
Manuel Pégourié-Gonnard | 8c7b81c | 2022-07-18 11:17:06 +0200 | [diff] [blame] | 325 | in library code (most useful when algorithm availability is checked far from the |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 326 | code that will be using it, such as in TLS negotiation) as well as test |
| 327 | dependencies. |
| 328 | |
| 329 | It should also be noted that there is a fourth case: utility functions / |
| 330 | information tables that are not tied to a particular crypto API, and may be |
| 331 | used by functions that are either purely PSA-based, purely legacy-based, or |
Manuel Pégourié-Gonnard | 8c7b81c | 2022-07-18 11:17:06 +0200 | [diff] [blame] | 332 | hybrid governed by `MBEDTLS_USE_PSA_CRYPTO` should use `MBEDTLS_xxx || |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 333 | PSA_WANT_xxx` - for example, `oid_md_alg` from `oid.c`, used by both X.509 and |
Manuel Pégourié-Gonnard | f88b1b5 | 2022-07-15 11:05:05 +0200 | [diff] [blame] | 334 | RSA. A new family of macros `MBEDTLS_OR_PSA_WANT_xxx` is defined for this. |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 335 | |
| 336 | To sum up, there are 4 categories: |
| 337 | |
| 338 | - legacy-based code depends on `MBEDTLS_xxx`; |
| 339 | - PSA-based code depends on `PSA_WANT_xxx`; |
| 340 | - hybrid code governed by `MBEDTLS_USE_PSA_CRYPTO` can use |
| 341 | `MBEDTLS_USE_PSA_WANT_xxx` to express dependencies in common parts; |
| 342 | - data and crypto-agnostic helpers that can be used by code from at least two |
Manuel Pégourié-Gonnard | f88b1b5 | 2022-07-15 11:05:05 +0200 | [diff] [blame] | 343 | of the above categories should depend on `MBEDTLS_OR_PSA_WANT_xxx`. |
Manuel Pégourié-Gonnard | af8cf5c | 2022-07-12 11:05:53 +0200 | [diff] [blame] | 344 | |
| 345 | |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 346 | Migrating away from the legacy API |
| 347 | ================================== |
| 348 | |
| 349 | This section briefly introduces questions and possible plans towards G4, |
| 350 | mainly as they relate to choices in previous stages. |
| 351 | |
| 352 | The role of the PK/Cipher/MD APIs in user migration |
| 353 | --------------------------------------------------- |
| 354 | |
Manuel Pégourié-Gonnard | 481846c | 2022-07-12 09:27:39 +0200 | [diff] [blame] | 355 | 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] | 356 | to reduce the number of places where library code needs to be changed. It's |
| 357 | only natural to consider using the same strategy (with the PK, MD and Cipher |
| 358 | layers) for facilitating migration of application code. |
| 359 | |
| 360 | Note: a necessary first step for that would be to make sure PSA is no longer |
| 361 | implemented of top of the concerned layers |
| 362 | |
| 363 | ### Zero-cost compatibility layer? |
| 364 | |
| 365 | The most favourable case is if we can have a zero-cost abstraction (no |
| 366 | 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] | 367 | `#define`s, essentially mapping `mbedtls_` APIs to their `psa_` equivalent. |
Manuel Pégourié-Gonnard | b89fd95 | 2021-09-30 11:52:04 +0200 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | 2a47d23 | 2022-04-20 15:01:13 +0200 | [diff] [blame] | 369 | 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] | 370 | same context type for hashes and HMACs, while the PSA API (rightfully) has |
| 371 | distinct operation types. Similarly, the Cipher layer uses the same context |
| 372 | type for unauthenticated and AEAD ciphers, which again the PSA API |
| 373 | distinguishes. |
| 374 | |
| 375 | It is unclear how much value, if any, a zero-cost compatibility layer that's |
| 376 | incomplete (for example, for MD covering only hashes, or for Cipher covering |
| 377 | only AEAD) or differs significantly from the existing API (for example, |
| 378 | introducing new context types) would provide to users. |
| 379 | |
| 380 | ### Low-cost compatibility layers? |
| 381 | |
| 382 | Another possibility is to keep most or all of the existing API for the PK, MD |
| 383 | and Cipher layers, implemented on top of PSA, aiming for the lowest possible |
| 384 | cost. For example, `mbedtls_md_context_t` would be defined as a (tagged) union |
| 385 | of `psa_hash_operation_t` and `psa_mac_operation_t`, then `mbedtls_md_setup()` |
| 386 | would initialize the correct part, and the rest of the functions be simple |
| 387 | wrappers around PSA functions. This would vastly reduce the complexity of the |
| 388 | layers compared to the existing (no need to dispatch through function |
| 389 | pointers, just call the corresponding PSA API). |
| 390 | |
| 391 | 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] | 392 | 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] | 393 | be a temporary solution: for example keep the compatibility layers in 4.0 (and |
| 394 | make them optional), but remove them in 5.0. |
| 395 | |
| 396 | 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] | 397 | 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] | 398 | reducing the cost, and judgment calls may need to be made. |
| 399 | |
| 400 | Note: when it comes to holding public keys in the PK layer, depending on how |
| 401 | the rest of the code is structured, it may be worth holding the key data in |
| 402 | memory controlled by the PK layer as opposed to a PSA key slot, moving it to a |
| 403 | slot only when needed (see current `ecdsa_verify_wrap` when |
| 404 | `MBEDTLS_USE_PSA_CRYPTO` is defined) For example, when parsing a large |
| 405 | number, N, of X.509 certificates (for example the list of trusted roots), it |
| 406 | might be undesirable to use N PSA key slots for their public keys as long as |
| 407 | the certs are loaded. OTOH, this could also be addressed by merging the "X.509 |
| 408 | parsing on-demand" (#2478), and then the public key data would be held as |
| 409 | bytes in the X.509 CRT structure, and only moved to a PK context / PSA slot |
| 410 | when it's actually used. |
| 411 | |
| 412 | Note: the PK layer actually consists of two relatively distinct parts: crypto |
| 413 | operations, which will be covered by PSA, and parsing/writing (exporting) |
| 414 | from/to various formats, which is currently not fully covered by the PSA |
| 415 | Crypto API. |
| 416 | |
| 417 | ### Algorithm identifiers and other identifiers |
| 418 | |
| 419 | It should be easy to provide the user with a bunch of `#define`s for algorithm |
| 420 | identifiers, for example `#define MBEDTLS_MD_SHA256 PSA_ALG_SHA_256`; most of |
| 421 | those would be in the MD, Cipher and PK compatibility layers mentioned above, |
| 422 | but there might be some in other modules that may be worth considering, for |
| 423 | example identifiers for elliptic curves. |
| 424 | |
| 425 | ### Lower layers |
| 426 | |
| 427 | Generally speaking, we would retire all of the low-level, non-generic modules, |
| 428 | such as AES, SHA-256, RSA, DHM, ECDH, ECP, bignum, etc, without providing |
| 429 | 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] | 430 | 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] | 431 | would mostly benefit people who already used those generic APis rather than |
| 432 | the low-level, alg-specific ones.) |
| 433 | |
| 434 | ### APIs in TLS and X.509 |
| 435 | |
| 436 | Public APIs in TLS and X.509 may be affected by the migration in at least two |
| 437 | ways: |
| 438 | |
| 439 | 1. APIs that rely on a legacy `mbedtls_` crypto type: for example |
| 440 | `mbedtls_ssl_conf_own_cert()` to configure a (certificate and the |
| 441 | associated) private key. Currently the private key is passed as a |
| 442 | `mbedtls_pk_context` object, which would probably change to a `psa_key_id_t`. |
| 443 | Since some users would probably still be using the compatibility PK layer, it |
| 444 | would need a way to easily extract the PSA key ID from the PK context. |
| 445 | |
| 446 | 2. APIs the accept list of identifiers: for example |
| 447 | `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] | 448 | 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] | 449 | 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] | 450 | the underlying crypto implementation and use TLS-specific identifiers instead |
| 451 | (based on IANA values or custom enums), as is currently done in the new |
| 452 | `mbedtls_ssl_conf_groups()` API, see #4859). |
| 453 | |
| 454 | Testing |
| 455 | ------- |
| 456 | |
| 457 | An question that needs careful consideration when we come around to removing |
| 458 | the low-level crypto APIs and making PK, MD and Cipher optional compatibility |
| 459 | layers is to be sure to preserve testing quality. A lot of the existing test |
| 460 | 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] | 461 | 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] | 462 | combination of both, perhaps evolving gradually over time. |