Gilles Peskine | 9d6a63b | 2023-09-04 17:49:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file mbedtls/config_adjust_legacy_crypto.h |
| 3 | * \brief Adjust legacy configuration configuration |
| 4 | * |
| 5 | * Automatically enable certain dependencies. Generally, MBEDLTS_xxx |
| 6 | * configurations need to be explicitly enabled by the user: enabling |
| 7 | * MBEDTLS_xxx_A but not MBEDTLS_xxx_B when A requires B results in a |
| 8 | * compilation error. However, we do automatically enable certain options |
| 9 | * in some circumstances. One case is if MBEDTLS_xxx_B is an internal option |
| 10 | * used to identify parts of a module that are used by other module, and we |
| 11 | * don't want to make the symbol MBEDTLS_xxx_B part of the public API. |
| 12 | * Another case is if A didn't depend on B in earlier versions, and we |
| 13 | * want to use B in A but we need to preserve backward compatibility with |
| 14 | * configurations that explicitly activate MBEDTLS_xxx_A but not |
| 15 | * MBEDTLS_xxx_B. |
| 16 | */ |
| 17 | /* |
| 18 | * Copyright The Mbed TLS Contributors |
| 19 | * SPDX-License-Identifier: Apache-2.0 |
| 20 | * |
| 21 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 22 | * not use this file except in compliance with the License. |
| 23 | * You may obtain a copy of the License at |
| 24 | * |
| 25 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | * |
| 27 | * Unless required by applicable law or agreed to in writing, software |
| 28 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 29 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 30 | * See the License for the specific language governing permissions and |
| 31 | * limitations under the License. |
| 32 | */ |
| 33 | |
| 34 | #ifndef MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H |
| 35 | #define MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H |
| 36 | |
Yanray Wang | 9b81165 | 2023-09-07 16:18:00 +0800 | [diff] [blame] | 37 | /* |
| 38 | * ECB, CBC, XTS, KW modes require both ENCRYPT and DECRYPT directions. |
Yanray Wang | b67b474 | 2023-10-31 17:10:32 +0800 | [diff] [blame^] | 39 | * MBEDTLS_BLOCK_CIPHER_NO_DECRYPT is only enabled when those modes |
Yanray Wang | 9b81165 | 2023-09-07 16:18:00 +0800 | [diff] [blame] | 40 | * are not requested via the PSA API and are not enabled in the legacy API. |
| 41 | * |
Yanray Wang | 3caaf0c | 2023-09-07 17:50:14 +0800 | [diff] [blame] | 42 | * DES only supports ECB and CBC modes in Mbed TLS. As it's a deprecated and |
Yanray Wang | b67b474 | 2023-10-31 17:10:32 +0800 | [diff] [blame^] | 43 | * insecure block cipher, MBEDTLS_BLOCK_CIPHER_NO_DECRYPT is enabled when DES |
Yanray Wang | 3caaf0c | 2023-09-07 17:50:14 +0800 | [diff] [blame] | 44 | * is not requested via the PSA API and is not enabled in the legacy API. |
| 45 | * |
Yanray Wang | 9b81165 | 2023-09-07 16:18:00 +0800 | [diff] [blame] | 46 | * Note: XTS, KW are not yet supported via the PSA API in Mbed TLS. |
| 47 | */ |
| 48 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 49 | #if !defined(PSA_WANT_ALG_ECB_NO_PADDING) && \ |
| 50 | !defined(PSA_WANT_ALG_CBC_NO_PADDING) && \ |
| 51 | !defined(PSA_WANT_ALG_CBC_PKCS7) && \ |
Yanray Wang | 3caaf0c | 2023-09-07 17:50:14 +0800 | [diff] [blame] | 52 | !defined(PSA_WANT_KEY_TYPE_DES) && \ |
Yanray Wang | 9b81165 | 2023-09-07 16:18:00 +0800 | [diff] [blame] | 53 | !defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 54 | !defined(MBEDTLS_CIPHER_MODE_XTS) && \ |
Yanray Wang | 3caaf0c | 2023-09-07 17:50:14 +0800 | [diff] [blame] | 55 | !defined(MBEDTLS_DES_C) && \ |
Yanray Wang | 9b81165 | 2023-09-07 16:18:00 +0800 | [diff] [blame] | 56 | !defined(MBEDTLS_NIST_KW_C) |
Yanray Wang | b67b474 | 2023-10-31 17:10:32 +0800 | [diff] [blame^] | 57 | #define MBEDTLS_BLOCK_CIPHER_NO_DECRYPT 1 |
Yanray Wang | 9b81165 | 2023-09-07 16:18:00 +0800 | [diff] [blame] | 58 | #endif |
| 59 | #endif /* MBEDTLS_PSA_CRYPTO_CONFIG */ |
| 60 | |
Gilles Peskine | 9d6a63b | 2023-09-04 17:49:07 +0200 | [diff] [blame] | 61 | /* Auto-enable MBEDTLS_MD_LIGHT based on MBEDTLS_MD_C. |
| 62 | * This allows checking for MD_LIGHT rather than MD_LIGHT || MD_C. |
| 63 | */ |
| 64 | #if defined(MBEDTLS_MD_C) |
| 65 | #define MBEDTLS_MD_LIGHT |
| 66 | #endif |
| 67 | |
| 68 | /* Auto-enable MBEDTLS_MD_LIGHT if needed by a module that didn't require it |
| 69 | * in a previous release, to ensure backwards compatibility. |
| 70 | */ |
| 71 | #if defined(MBEDTLS_ECJPAKE_C) || \ |
| 72 | defined(MBEDTLS_PEM_PARSE_C) || \ |
| 73 | defined(MBEDTLS_ENTROPY_C) || \ |
| 74 | defined(MBEDTLS_PK_C) || \ |
| 75 | defined(MBEDTLS_PKCS12_C) || \ |
| 76 | defined(MBEDTLS_RSA_C) || \ |
| 77 | defined(MBEDTLS_SSL_TLS_C) || \ |
| 78 | defined(MBEDTLS_X509_USE_C) || \ |
| 79 | defined(MBEDTLS_X509_CREATE_C) |
| 80 | #define MBEDTLS_MD_LIGHT |
| 81 | #endif |
| 82 | |
| 83 | /* MBEDTLS_ECP_LIGHT is auto-enabled by the following symbols: |
| 84 | * - MBEDTLS_ECP_C because now it consists of MBEDTLS_ECP_LIGHT plus functions |
| 85 | * for curve arithmetic. As a consequence if MBEDTLS_ECP_C is required for |
| 86 | * some reason, then MBEDTLS_ECP_LIGHT should be enabled as well. |
| 87 | * - MBEDTLS_PK_PARSE_EC_EXTENDED and MBEDTLS_PK_PARSE_EC_COMPRESSED because |
| 88 | * these features are not supported in PSA so the only way to have them is |
| 89 | * to enable the built-in solution. |
| 90 | * Both of them are temporary dependencies: |
| 91 | * - PK_PARSE_EC_EXTENDED will be removed after #7779 and #7789 |
| 92 | * - support for compressed points should also be added to PSA, but in this |
| 93 | * case there is no associated issue to track it yet. |
| 94 | * - PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE because Weierstrass key derivation |
| 95 | * still depends on ECP_LIGHT. |
| 96 | * - PK_C + USE_PSA + PSA_WANT_ALG_ECDSA is a temporary dependency which will |
| 97 | * be fixed by #7453. |
| 98 | */ |
| 99 | #if defined(MBEDTLS_ECP_C) || \ |
| 100 | defined(MBEDTLS_PK_PARSE_EC_EXTENDED) || \ |
| 101 | defined(MBEDTLS_PK_PARSE_EC_COMPRESSED) || \ |
| 102 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
| 103 | #define MBEDTLS_ECP_LIGHT |
| 104 | #endif |
| 105 | |
Thomas Daubney | 540324c | 2023-10-06 17:07:24 +0100 | [diff] [blame] | 106 | /* MBEDTLS_PK_PARSE_EC_COMPRESSED is introduced in Mbed TLS version 3.5, while |
Gilles Peskine | 9d6a63b | 2023-09-04 17:49:07 +0200 | [diff] [blame] | 107 | * in previous version compressed points were automatically supported as long |
| 108 | * as PK_PARSE_C and ECP_C were enabled. As a consequence, for backward |
| 109 | * compatibility, we auto-enable PK_PARSE_EC_COMPRESSED when these conditions |
| 110 | * are met. */ |
| 111 | #if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_ECP_C) |
| 112 | #define MBEDTLS_PK_PARSE_EC_COMPRESSED |
| 113 | #endif |
| 114 | |
| 115 | /* Helper symbol to state that there is support for ECDH, either through |
| 116 | * library implementation (ECDH_C) or through PSA. */ |
| 117 | #if (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_ECDH)) || \ |
| 118 | (!defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)) |
| 119 | #define MBEDTLS_CAN_ECDH |
| 120 | #endif |
| 121 | |
| 122 | /* PK module can achieve ECDSA functionalities by means of either software |
| 123 | * implementations (ECDSA_C) or through a PSA driver. The following defines |
| 124 | * are meant to list these capabilities in a general way which abstracts how |
| 125 | * they are implemented under the hood. */ |
| 126 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
| 127 | #if defined(MBEDTLS_ECDSA_C) |
| 128 | #define MBEDTLS_PK_CAN_ECDSA_SIGN |
| 129 | #define MBEDTLS_PK_CAN_ECDSA_VERIFY |
| 130 | #endif /* MBEDTLS_ECDSA_C */ |
| 131 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 132 | #if defined(PSA_WANT_ALG_ECDSA) |
| 133 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) |
| 134 | #define MBEDTLS_PK_CAN_ECDSA_SIGN |
| 135 | #endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC */ |
| 136 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
| 137 | #define MBEDTLS_PK_CAN_ECDSA_VERIFY |
| 138 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 139 | #endif /* PSA_WANT_ALG_ECDSA */ |
| 140 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 141 | |
| 142 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) || defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
| 143 | #define MBEDTLS_PK_CAN_ECDSA_SOME |
| 144 | #endif |
| 145 | |
| 146 | /* If MBEDTLS_PSA_CRYPTO_C is defined, make sure MBEDTLS_PSA_CRYPTO_CLIENT |
| 147 | * is defined as well to include all PSA code. |
| 148 | */ |
| 149 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 150 | #define MBEDTLS_PSA_CRYPTO_CLIENT |
| 151 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 152 | |
| 153 | /* The PK wrappers need pk_write functions to format RSA key objects |
| 154 | * when they are dispatching to the PSA API. This happens under USE_PSA_CRYPTO, |
| 155 | * and also even without USE_PSA_CRYPTO for mbedtls_pk_sign_ext(). */ |
| 156 | #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_RSA_C) |
| 157 | #define MBEDTLS_PK_C |
| 158 | #define MBEDTLS_PK_WRITE_C |
| 159 | #define MBEDTLS_PK_PARSE_C |
| 160 | #endif |
| 161 | |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 162 | /* Helpers to state that each key is supported either on the builtin or PSA side. */ |
| 163 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 164 | #define MBEDTLS_ECP_HAVE_SECP521R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 165 | #endif |
| 166 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 167 | #define MBEDTLS_ECP_HAVE_BP512R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 168 | #endif |
| 169 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 170 | #define MBEDTLS_ECP_HAVE_CURVE448 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 171 | #endif |
| 172 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 173 | #define MBEDTLS_ECP_HAVE_BP384R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 174 | #endif |
| 175 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 176 | #define MBEDTLS_ECP_HAVE_SECP384R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 177 | #endif |
| 178 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 179 | #define MBEDTLS_ECP_HAVE_BP256R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 180 | #endif |
| 181 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 182 | #define MBEDTLS_ECP_HAVE_SECP256K1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 183 | #endif |
| 184 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 185 | #define MBEDTLS_ECP_HAVE_SECP256R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 186 | #endif |
| 187 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 188 | #define MBEDTLS_ECP_HAVE_CURVE25519 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 189 | #endif |
| 190 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 191 | #define MBEDTLS_ECP_HAVE_SECP224K1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 192 | #endif |
| 193 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 194 | #define MBEDTLS_ECP_HAVE_SECP224R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 195 | #endif |
| 196 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 197 | #define MBEDTLS_ECP_HAVE_SECP192K1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 198 | #endif |
| 199 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192) |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 200 | #define MBEDTLS_ECP_HAVE_SECP192R1 |
Valerio Setti | 67d82e7 | 2023-08-16 17:11:53 +0200 | [diff] [blame] | 201 | #endif |
| 202 | |
Gilles Peskine | 9d6a63b | 2023-09-04 17:49:07 +0200 | [diff] [blame] | 203 | /* Helper symbol to state that the PK module has support for EC keys. This |
| 204 | * can either be provided through the legacy ECP solution or through the |
| 205 | * PSA friendly MBEDTLS_PK_USE_PSA_EC_DATA (see pk.h for its description). */ |
| 206 | #if defined(MBEDTLS_ECP_C) || \ |
| 207 | (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)) |
| 208 | #define MBEDTLS_PK_HAVE_ECC_KEYS |
| 209 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_ECP_C */ |
| 210 | |
Gilles Peskine | ca1e605 | 2023-09-25 16:16:26 +0200 | [diff] [blame] | 211 | /* Historically pkparse did not check the CBC padding when decrypting |
| 212 | * a key. This was a bug, which is now fixed. As a consequence, pkparse |
| 213 | * now needs PKCS7 padding support, but existing configurations might not |
| 214 | * enable it, so we enable it here. */ |
| 215 | #if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 216 | #define MBEDTLS_CIPHER_PADDING_PKCS7 |
| 217 | #endif |
| 218 | |
Gilles Peskine | 9d6a63b | 2023-09-04 17:49:07 +0200 | [diff] [blame] | 219 | #endif /* MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H */ |