blob: 67777212686d25b134307158f23d3dcdc0e08b96 [file] [log] [blame]
Gilles Peskine9d6a63b2023-09-04 17:49:07 +02001/**
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 Wang9b811652023-09-07 16:18:00 +080037/*
38 * ECB, CBC, XTS, KW modes require both ENCRYPT and DECRYPT directions.
Yanray Wangb67b4742023-10-31 17:10:32 +080039 * MBEDTLS_BLOCK_CIPHER_NO_DECRYPT is only enabled when those modes
Yanray Wang9b811652023-09-07 16:18:00 +080040 * are not requested via the PSA API and are not enabled in the legacy API.
41 *
Yanray Wang3caaf0c2023-09-07 17:50:14 +080042 * DES only supports ECB and CBC modes in Mbed TLS. As it's a deprecated and
Yanray Wangb67b4742023-10-31 17:10:32 +080043 * insecure block cipher, MBEDTLS_BLOCK_CIPHER_NO_DECRYPT is enabled when DES
Yanray Wang3caaf0c2023-09-07 17:50:14 +080044 * is not requested via the PSA API and is not enabled in the legacy API.
45 *
Yanray Wang9b811652023-09-07 16:18:00 +080046 * 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 Wang3caaf0c2023-09-07 17:50:14 +080052 !defined(PSA_WANT_KEY_TYPE_DES) && \
Yanray Wang9b811652023-09-07 16:18:00 +080053 !defined(MBEDTLS_CIPHER_MODE_CBC) && \
54 !defined(MBEDTLS_CIPHER_MODE_XTS) && \
Yanray Wang3caaf0c2023-09-07 17:50:14 +080055 !defined(MBEDTLS_DES_C) && \
Yanray Wang9b811652023-09-07 16:18:00 +080056 !defined(MBEDTLS_NIST_KW_C)
Yanray Wangb67b4742023-10-31 17:10:32 +080057#define MBEDTLS_BLOCK_CIPHER_NO_DECRYPT 1
Yanray Wang9b811652023-09-07 16:18:00 +080058#endif
59#endif /* MBEDTLS_PSA_CRYPTO_CONFIG */
60
Gilles Peskine9d6a63b2023-09-04 17:49:07 +020061/* 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 Daubney540324c2023-10-06 17:07:24 +0100106/* MBEDTLS_PK_PARSE_EC_COMPRESSED is introduced in Mbed TLS version 3.5, while
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200107 * 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 Setti67d82e72023-08-16 17:11:53 +0200162/* 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 Settidb6b4db2023-09-01 09:20:51 +0200164#define MBEDTLS_ECP_HAVE_SECP521R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200165#endif
166#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200167#define MBEDTLS_ECP_HAVE_BP512R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200168#endif
169#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200170#define MBEDTLS_ECP_HAVE_CURVE448
Valerio Setti67d82e72023-08-16 17:11:53 +0200171#endif
172#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200173#define MBEDTLS_ECP_HAVE_BP384R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200174#endif
175#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200176#define MBEDTLS_ECP_HAVE_SECP384R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200177#endif
178#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200179#define MBEDTLS_ECP_HAVE_BP256R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200180#endif
181#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200182#define MBEDTLS_ECP_HAVE_SECP256K1
Valerio Setti67d82e72023-08-16 17:11:53 +0200183#endif
184#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200185#define MBEDTLS_ECP_HAVE_SECP256R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200186#endif
187#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200188#define MBEDTLS_ECP_HAVE_CURVE25519
Valerio Setti67d82e72023-08-16 17:11:53 +0200189#endif
190#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200191#define MBEDTLS_ECP_HAVE_SECP224K1
Valerio Setti67d82e72023-08-16 17:11:53 +0200192#endif
193#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200194#define MBEDTLS_ECP_HAVE_SECP224R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200195#endif
196#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200197#define MBEDTLS_ECP_HAVE_SECP192K1
Valerio Setti67d82e72023-08-16 17:11:53 +0200198#endif
199#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200200#define MBEDTLS_ECP_HAVE_SECP192R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200201#endif
202
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200203/* 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 Peskineca1e6052023-09-25 16:16:26 +0200211/* 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 Peskine9d6a63b2023-09-04 17:49:07 +0200219#endif /* MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H */