Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 1 | This document explains how to create builds of Mbed TLS where some |
| 2 | cryptographic mechanisms are provided only by PSA drivers (that is, no |
| 3 | built-in implementation of those algorithms), from a user's perspective. |
| 4 | |
| 5 | This is useful to save code size for people who are using either a hardware |
| 6 | accelerator, or an alternative software implementation that's more |
| 7 | aggressively optimized for code size than the default one in Mbed TLS. |
| 8 | |
| 9 | General considerations |
| 10 | ---------------------- |
| 11 | |
| 12 | This document assumes that you already have a working driver. |
| 13 | Otherwise, please see the [PSA driver example and |
| 14 | guide](psa-driver-example-and-guide.md) for information on writing a |
| 15 | driver. |
| 16 | |
| 17 | In order to have some mechanism provided only by a driver, you'll want |
| 18 | the following compile-time configuration options enabled: |
| 19 | - `MBEDTLS_PSA_CRYPTO_C` (enabled by default) - this enables PSA Crypto. |
| 20 | - `MBEDTLS_USE_PSA_CRYPTO` (disabled by default) - this makes PK, X.509 and |
| 21 | TLS use PSA Crypto. You need to enable this if you're using PK, X.509 or TLS |
| 22 | and want them to have access to the algorithms provided by your driver. (See |
| 23 | [the dedicated document](use-psa-crypto.md) for details.) |
| 24 | - `MBEDTLS_PSA_CRYPTO_CONFIG` (disabled by default) - this enables |
| 25 | configuration of cryptographic algorithms using `PSA_WANT` macros in |
| 26 | `include/psa/crypto_config.h`. See [Conditional inclusion of cryptographic |
| 27 | mechanism through the PSA API in Mbed |
| 28 | TLS](proposed/psa-conditional-inclusion-c.md) for details. |
| 29 | |
| 30 | In addition, for each mechanism you want provided only by your driver: |
| 31 | - Define the corresponding `PSA_WANT` macro in `psa/crypto_config.h` - this |
| 32 | means the algorithm will be available in the PSA Crypto API. |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 33 | - Define the corresponding `MBEDTLS_PSA_ACCEL` in your build. This could be |
| 34 | defined in `psa/crypto_config.h` or your compiler's command line. This |
| 35 | informs the PSA code that an accelerator is available for this mechanism. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 36 | - Undefine / comment out the corresponding `MBEDTLS_xxx_C` macro in |
| 37 | `mbedtls/mbedtls_config.h`. This ensures the built-in implementation is not |
| 38 | included in the build. |
| 39 | |
| 40 | For example, if you want SHA-256 to be provided only by a driver, you'll want |
| 41 | `PSA_WANT_ALG_SHA_256` and `MBEDTLS_PSA_ACCEL_SHA_256` defined, and |
| 42 | `MBEDTLS_SHA256_C` undefined. |
| 43 | |
| 44 | In addition to these compile-time considerations, at runtime you'll need to |
| 45 | make sure you call `psa_crypto_init()` before any function that uses the |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 46 | driver-only mechanisms. Note that this is already a requirement for any use of |
| 47 | the PSA Crypto API, as well as for use of the PK, X.509 and TLS modules when |
| 48 | `MBEDTLS_USE_PSA_CRYPTO` is enabled, so in most cases your application will |
| 49 | already be doing this. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 50 | |
| 51 | Mechanisms covered |
| 52 | ------------------ |
| 53 | |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 54 | For now, only the following (families of) mechanisms are supported: |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 55 | - hashes: SHA-3, SHA-2, SHA-1, MD5, etc. |
| 56 | - elliptic-curve cryptography (ECC): ECDH, ECDSA, EC J-PAKE, ECC key types. |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 57 | - finite-field Diffie-Hellman: FFDH algorithm, DH key types. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 58 | |
| 59 | Supported means that when those are provided only by drivers, everything |
| 60 | (including PK, X.509 and TLS if `MBEDTLS_USE_PSA_CRYPTO` is enabled) should |
| 61 | work in the same way as if the mechanisms where built-in, except as documented |
| 62 | in the "Limitations" sub-sections of the sections dedicated to each family |
| 63 | below. |
| 64 | |
| 65 | In the near future (end of 2023), we are planning to also add support for |
| 66 | ciphers (AES) and AEADs (GCM, CCM, ChachaPoly). |
| 67 | |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 68 | Currently (mid-2023) we don't have plans to extend this to RSA. If |
| 69 | you're interested in driver-only support for RSA, please let us know. |
Manuel Pégourié-Gonnard | 6d5f494 | 2023-07-07 12:00:49 +0200 | [diff] [blame] | 70 | |
| 71 | Hashes |
| 72 | ------ |
| 73 | |
| 74 | TODO |
| 75 | |
| 76 | Elliptic-curve cryptography (ECC) |
| 77 | --------------------------------- |
| 78 | |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 79 | It is possible to have most ECC operations provided only by a driver: |
| 80 | - the ECDH, ECDSA and EC J-PAKE algorithms; |
| 81 | - key import, export, and random generation. |
| 82 | |
| 83 | More precisely: |
| 84 | - you can enable `PSA_WANT_ALG_ECDH` without `MBEDTLS_ECDH_C` provided |
| 85 | `MBEDTLS_PSA_ACCEL_ALG_ECDH` is enabled; |
| 86 | - you can enable `PSA_WANT_ALG_ECDSA` without `MBEDTLS_ECDSA_C` provided |
| 87 | `MBEDTLS_PSA_ACCEL_ALG_ECDSA` is enabled; |
| 88 | - you can enable `PSA_WANT_ALG_JPAKE` without `MBEDTLS_ECJPAKE_C` provided |
| 89 | `MBEDTLS_PSA_ACCEL_ALG_JPAKE` is enabled. |
| 90 | |
| 91 | In addition, if none of `MBEDTLS_ECDH_C`, `MBEDTLS_ECDSA_C`, |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 92 | `MBEDTLS_ECJPAKE_C` are enabled, you can enable: |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 93 | - `PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY`; |
| 94 | - `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC`; |
| 95 | - `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT`; |
| 96 | - `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_EXPORT`; |
| 97 | - `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE`; |
| 98 | without `MBEDTLS_ECP_C` provided the corresponding |
| 99 | `MBEDTLS_PSA_ACCEL_KEY_TYPE_xxx` are enabled. |
| 100 | |
| 101 | [Coming soon] If `MBEDTLS_ECP_C` is disabled and `ecp.c` is fully removed (see |
| 102 | "Limitations regarding fully removing `ecp.c`" below), and you're not using |
| 103 | RSA or FFDH, then you can also disable `MBEDTLS_BIGNUM_C` for further code |
| 104 | size saving. |
| 105 | |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 106 | [Coming soon] As noted in the "Limitations regarding the selection of curves" |
| 107 | section below, there is an upcoming requirement for all the required curves to |
Valerio Setti | 7373a66 | 2023-09-04 13:59:03 +0200 | [diff] [blame] | 108 | also be accelerated in the PSA driver in order to exclude the builtin algs |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 109 | support. |
| 110 | |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 111 | ### Limitations regarding fully removing `ecp.c` |
| 112 | |
| 113 | A limited subset of `ecp.c` will still be automatically re-enabled if any of |
| 114 | the following is enabled: |
| 115 | - `MBEDTLS_PK_PARSE_EC_COMPRESSED` - support for parsing ECC keys where the |
| 116 | public part is in compressed format; |
| 117 | - `MBEDTLS_PK_PARSE_EC_EXTENDED` - support for parsing ECC keys where the |
| 118 | curve is identified not by name, but by explicit parameters; |
| 119 | - `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE` - support for deterministic |
| 120 | derivation of an ECC keypair with `psa_key_derivation_output_key()`. |
| 121 | |
Manuel Pégourié-Gonnard | 1937cf8 | 2023-07-11 11:14:15 +0200 | [diff] [blame] | 122 | Note: when any of the above options is enabled, a subset of `ecp.c` will |
| 123 | automatically be included in the build in order to support it. Therefore |
| 124 | you can still disable `MBEDTLS_ECP_C` in `mbedtls_config.h` and this will |
| 125 | result in some code size savings, but not as much as when none of the |
| 126 | above features are enabled. |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 127 | |
| 128 | We do have plans to support each of these with `ecp.c` fully removed in the |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 129 | future, however there is no established timeline. If you're interested, please |
| 130 | let us know, so we can take it into consideration in our planning. |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 131 | |
| 132 | ### Limitations regarding restartable / interruptible ECC operations |
| 133 | |
Manuel Pégourié-Gonnard | 1937cf8 | 2023-07-11 11:14:15 +0200 | [diff] [blame] | 134 | At the moment, there is not driver support for interruptible operations |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 135 | (see `psa_sign_hash_start()` + `psa_sign_hash_complete()` etc.) so as a |
| 136 | consequence these are not supported in builds without `MBEDTLS_ECDSA_C`. |
| 137 | |
| 138 | Similarly, there is no PSA support for interruptible ECDH operations so these |
| 139 | are not supported without `ECDH_C`. See also limitations regarding |
| 140 | restartable operations with `MBEDTLS_USE_PSA_CRYPTO` in [its |
| 141 | documentation](use-psa-crypto.md). |
| 142 | |
Manuel Pégourié-Gonnard | fb22c27 | 2023-07-18 10:40:56 +0200 | [diff] [blame] | 143 | Again, we have plans to support this in the future but not with an established |
Manuel Pégourié-Gonnard | 7a82e27 | 2023-07-07 16:43:56 +0200 | [diff] [blame] | 144 | timeline, please let us know if you're interested. |
| 145 | |
| 146 | ### Limitations regarding the selection of curves |
| 147 | |
Paul Elliott | 3d0bffb | 2023-09-13 15:15:37 +0100 | [diff] [blame^] | 148 | There is ongoing work which is trying to establish the links and constraints |
| 149 | between the list of supported curves and supported algorithms both in the |
| 150 | builtin and PSA sides. In particular: |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 151 | |
| 152 | - #8014 ensures that the curves supported on the PSA side (`PSA_WANT_ECC_xxx`) |
| 153 | are always a superset of the builtin ones (`MBEDTLS_ECP_DP_xxx`) |
| 154 | - #8016 forces builtin alg support as soon as there is at least one builtin |
| 155 | curve. In other words, in order to exclue all builtin algs, all the required |
| 156 | curves should be supported and accelerated by the PSA driver. |
Manuel Pégourié-Gonnard | c977751 | 2023-07-11 11:11:20 +0200 | [diff] [blame] | 157 | |
| 158 | Finite-field Diffie-Hellman |
| 159 | --------------------------- |
| 160 | |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 161 | Support is pretty similar to the "Elliptic-curve cryptography (ECC)" section |
| 162 | above. |
| 163 | Key management and usage can be enabled by means of the usual `PSA_WANT` + |
| 164 | `MBEDTLS_PSA_ACCEL` pairs: |
| 165 | |
| 166 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_PUBLIC_KEY`; |
| 167 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_BASIC`; |
| 168 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_IMPORT`; |
| 169 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_EXPORT`; |
| 170 | - `[PSA_WANT|MBEDTLS_PSA_ACCEL]_KEY_TYPE_DH_KEY_PAIR_GENERATE`; |
| 171 | |
| 172 | The same holds for the associated algorithm: |
Valerio Setti | 7373a66 | 2023-09-04 13:59:03 +0200 | [diff] [blame] | 173 | `[PSA_WANT|MBEDTLS_PSA_ACCEL]_ALG_FFDH` allow builds accelerating FFDH and |
Valerio Setti | d31b284 | 2023-08-15 10:59:58 +0200 | [diff] [blame] | 174 | removing builtin support (i.e. `MBEDTLS_DHM_C`). |
| 175 | |
| 176 | ### Limitations |
| 177 | Support for deterministic derivation of a DH keypair |
| 178 | (i.e. `PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE`) is not supported. |