blob: aafd42e7a684226a861f30c1f852feed99f2ab17 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000019 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine9d6a63b2023-09-04 17:49:07 +020020 */
21
22#ifndef MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H
23#define MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H
24
25/* Auto-enable MBEDTLS_MD_LIGHT based on MBEDTLS_MD_C.
26 * This allows checking for MD_LIGHT rather than MD_LIGHT || MD_C.
27 */
28#if defined(MBEDTLS_MD_C)
29#define MBEDTLS_MD_LIGHT
30#endif
31
32/* Auto-enable MBEDTLS_MD_LIGHT if needed by a module that didn't require it
33 * in a previous release, to ensure backwards compatibility.
34 */
35#if defined(MBEDTLS_ECJPAKE_C) || \
36 defined(MBEDTLS_PEM_PARSE_C) || \
37 defined(MBEDTLS_ENTROPY_C) || \
38 defined(MBEDTLS_PK_C) || \
39 defined(MBEDTLS_PKCS12_C) || \
40 defined(MBEDTLS_RSA_C) || \
41 defined(MBEDTLS_SSL_TLS_C) || \
42 defined(MBEDTLS_X509_USE_C) || \
43 defined(MBEDTLS_X509_CREATE_C)
44#define MBEDTLS_MD_LIGHT
45#endif
46
Valerio Setti85d2a982023-10-06 16:04:49 +020047#if defined(MBEDTLS_MD_LIGHT)
48/*
49 * - MBEDTLS_MD_CAN_xxx is defined if the md module can perform xxx.
50 * - MBEDTLS_MD_xxx_VIA_PSA is defined if the md module may perform xxx via PSA
51 * (see below).
52 * - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm may be performed
53 * via PSA (see below).
54 * - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm may be performed
55 * via a direct legacy call (see below).
56 *
57 * The md module performs an algorithm via PSA if there is a PSA hash
58 * accelerator and the PSA driver subsytem is initialized at the time the
59 * operation is started, and makes a direct legacy call otherwise.
60 */
61
62/* PSA accelerated implementations */
63#if defined(MBEDTLS_PSA_CRYPTO_C)
64
65#if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
66#define MBEDTLS_MD_CAN_MD5
67#define MBEDTLS_MD_MD5_VIA_PSA
68#define MBEDTLS_MD_SOME_PSA
69#endif
70#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
71#define MBEDTLS_MD_CAN_SHA1
72#define MBEDTLS_MD_SHA1_VIA_PSA
73#define MBEDTLS_MD_SOME_PSA
74#endif
75#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
76#define MBEDTLS_MD_CAN_SHA224
77#define MBEDTLS_MD_SHA224_VIA_PSA
78#define MBEDTLS_MD_SOME_PSA
79#endif
80#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
81#define MBEDTLS_MD_CAN_SHA256
82#define MBEDTLS_MD_SHA256_VIA_PSA
83#define MBEDTLS_MD_SOME_PSA
84#endif
85#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
86#define MBEDTLS_MD_CAN_SHA384
87#define MBEDTLS_MD_SHA384_VIA_PSA
88#define MBEDTLS_MD_SOME_PSA
89#endif
90#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
91#define MBEDTLS_MD_CAN_SHA512
92#define MBEDTLS_MD_SHA512_VIA_PSA
93#define MBEDTLS_MD_SOME_PSA
94#endif
95#if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
96#define MBEDTLS_MD_CAN_RIPEMD160
97#define MBEDTLS_MD_RIPEMD160_VIA_PSA
98#define MBEDTLS_MD_SOME_PSA
99#endif
100#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_224)
101#define MBEDTLS_MD_CAN_SHA3_224
102#define MBEDTLS_MD_SHA3_224_VIA_PSA
103#define MBEDTLS_MD_SOME_PSA
104#endif
105#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_256)
106#define MBEDTLS_MD_CAN_SHA3_256
107#define MBEDTLS_MD_SHA3_256_VIA_PSA
108#define MBEDTLS_MD_SOME_PSA
109#endif
110#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_384)
111#define MBEDTLS_MD_CAN_SHA3_384
112#define MBEDTLS_MD_SHA3_384_VIA_PSA
113#define MBEDTLS_MD_SOME_PSA
114#endif
115#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_512)
116#define MBEDTLS_MD_CAN_SHA3_512
117#define MBEDTLS_MD_SHA3_512_VIA_PSA
118#define MBEDTLS_MD_SOME_PSA
119#endif
120#endif /* MBEDTLS_PSA_CRYPTO_C */
121
122/* Built-in implementations */
123#if defined(MBEDTLS_MD5_C)
124#define MBEDTLS_MD_CAN_MD5
125#define MBEDTLS_MD_SOME_LEGACY
126#endif
127#if defined(MBEDTLS_SHA1_C)
128#define MBEDTLS_MD_CAN_SHA1
129#define MBEDTLS_MD_SOME_LEGACY
130#endif
131#if defined(MBEDTLS_SHA224_C)
132#define MBEDTLS_MD_CAN_SHA224
133#define MBEDTLS_MD_SOME_LEGACY
134#endif
135#if defined(MBEDTLS_SHA256_C)
136#define MBEDTLS_MD_CAN_SHA256
137#define MBEDTLS_MD_SOME_LEGACY
138#endif
139#if defined(MBEDTLS_SHA384_C)
140#define MBEDTLS_MD_CAN_SHA384
141#define MBEDTLS_MD_SOME_LEGACY
142#endif
143#if defined(MBEDTLS_SHA512_C)
144#define MBEDTLS_MD_CAN_SHA512
145#define MBEDTLS_MD_SOME_LEGACY
146#endif
147#if defined(MBEDTLS_SHA3_C)
148#define MBEDTLS_MD_CAN_SHA3_224
149#define MBEDTLS_MD_CAN_SHA3_256
150#define MBEDTLS_MD_CAN_SHA3_384
151#define MBEDTLS_MD_CAN_SHA3_512
152#define MBEDTLS_MD_SOME_LEGACY
153#endif
154#if defined(MBEDTLS_RIPEMD160_C)
155#define MBEDTLS_MD_CAN_RIPEMD160
156#define MBEDTLS_MD_SOME_LEGACY
157#endif
158
159#endif /* MBEDTLS_MD_LIGHT */
160
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200161/* MBEDTLS_ECP_LIGHT is auto-enabled by the following symbols:
162 * - MBEDTLS_ECP_C because now it consists of MBEDTLS_ECP_LIGHT plus functions
163 * for curve arithmetic. As a consequence if MBEDTLS_ECP_C is required for
164 * some reason, then MBEDTLS_ECP_LIGHT should be enabled as well.
165 * - MBEDTLS_PK_PARSE_EC_EXTENDED and MBEDTLS_PK_PARSE_EC_COMPRESSED because
166 * these features are not supported in PSA so the only way to have them is
167 * to enable the built-in solution.
168 * Both of them are temporary dependencies:
169 * - PK_PARSE_EC_EXTENDED will be removed after #7779 and #7789
170 * - support for compressed points should also be added to PSA, but in this
171 * case there is no associated issue to track it yet.
172 * - PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE because Weierstrass key derivation
173 * still depends on ECP_LIGHT.
174 * - PK_C + USE_PSA + PSA_WANT_ALG_ECDSA is a temporary dependency which will
175 * be fixed by #7453.
176 */
177#if defined(MBEDTLS_ECP_C) || \
178 defined(MBEDTLS_PK_PARSE_EC_EXTENDED) || \
179 defined(MBEDTLS_PK_PARSE_EC_COMPRESSED) || \
180 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
181#define MBEDTLS_ECP_LIGHT
182#endif
183
Thomas Daubney540324c2023-10-06 17:07:24 +0100184/* MBEDTLS_PK_PARSE_EC_COMPRESSED is introduced in Mbed TLS version 3.5, while
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200185 * in previous version compressed points were automatically supported as long
186 * as PK_PARSE_C and ECP_C were enabled. As a consequence, for backward
187 * compatibility, we auto-enable PK_PARSE_EC_COMPRESSED when these conditions
188 * are met. */
189#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_ECP_C)
190#define MBEDTLS_PK_PARSE_EC_COMPRESSED
191#endif
192
193/* Helper symbol to state that there is support for ECDH, either through
194 * library implementation (ECDH_C) or through PSA. */
195#if (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_ECDH)) || \
196 (!defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C))
197#define MBEDTLS_CAN_ECDH
198#endif
199
200/* PK module can achieve ECDSA functionalities by means of either software
201 * implementations (ECDSA_C) or through a PSA driver. The following defines
202 * are meant to list these capabilities in a general way which abstracts how
203 * they are implemented under the hood. */
204#if !defined(MBEDTLS_USE_PSA_CRYPTO)
205#if defined(MBEDTLS_ECDSA_C)
206#define MBEDTLS_PK_CAN_ECDSA_SIGN
207#define MBEDTLS_PK_CAN_ECDSA_VERIFY
208#endif /* MBEDTLS_ECDSA_C */
209#else /* MBEDTLS_USE_PSA_CRYPTO */
210#if defined(PSA_WANT_ALG_ECDSA)
211#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC)
212#define MBEDTLS_PK_CAN_ECDSA_SIGN
213#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC */
214#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
215#define MBEDTLS_PK_CAN_ECDSA_VERIFY
216#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
217#endif /* PSA_WANT_ALG_ECDSA */
218#endif /* MBEDTLS_USE_PSA_CRYPTO */
219
220#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) || defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
221#define MBEDTLS_PK_CAN_ECDSA_SOME
222#endif
223
224/* If MBEDTLS_PSA_CRYPTO_C is defined, make sure MBEDTLS_PSA_CRYPTO_CLIENT
225 * is defined as well to include all PSA code.
226 */
227#if defined(MBEDTLS_PSA_CRYPTO_C)
228#define MBEDTLS_PSA_CRYPTO_CLIENT
229#endif /* MBEDTLS_PSA_CRYPTO_C */
230
231/* The PK wrappers need pk_write functions to format RSA key objects
232 * when they are dispatching to the PSA API. This happens under USE_PSA_CRYPTO,
233 * and also even without USE_PSA_CRYPTO for mbedtls_pk_sign_ext(). */
234#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_RSA_C)
235#define MBEDTLS_PK_C
236#define MBEDTLS_PK_WRITE_C
237#define MBEDTLS_PK_PARSE_C
238#endif
239
Valerio Setti67d82e72023-08-16 17:11:53 +0200240/* Helpers to state that each key is supported either on the builtin or PSA side. */
241#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200242#define MBEDTLS_ECP_HAVE_SECP521R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200243#endif
244#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200245#define MBEDTLS_ECP_HAVE_BP512R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200246#endif
247#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200248#define MBEDTLS_ECP_HAVE_CURVE448
Valerio Setti67d82e72023-08-16 17:11:53 +0200249#endif
250#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200251#define MBEDTLS_ECP_HAVE_BP384R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200252#endif
253#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200254#define MBEDTLS_ECP_HAVE_SECP384R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200255#endif
256#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200257#define MBEDTLS_ECP_HAVE_BP256R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200258#endif
259#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200260#define MBEDTLS_ECP_HAVE_SECP256K1
Valerio Setti67d82e72023-08-16 17:11:53 +0200261#endif
262#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200263#define MBEDTLS_ECP_HAVE_SECP256R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200264#endif
265#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200266#define MBEDTLS_ECP_HAVE_CURVE25519
Valerio Setti67d82e72023-08-16 17:11:53 +0200267#endif
268#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200269#define MBEDTLS_ECP_HAVE_SECP224K1
Valerio Setti67d82e72023-08-16 17:11:53 +0200270#endif
271#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200272#define MBEDTLS_ECP_HAVE_SECP224R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200273#endif
274#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200275#define MBEDTLS_ECP_HAVE_SECP192K1
Valerio Setti67d82e72023-08-16 17:11:53 +0200276#endif
277#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settidb6b4db2023-09-01 09:20:51 +0200278#define MBEDTLS_ECP_HAVE_SECP192R1
Valerio Setti67d82e72023-08-16 17:11:53 +0200279#endif
280
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200281/* Helper symbol to state that the PK module has support for EC keys. This
282 * can either be provided through the legacy ECP solution or through the
283 * PSA friendly MBEDTLS_PK_USE_PSA_EC_DATA (see pk.h for its description). */
284#if defined(MBEDTLS_ECP_C) || \
285 (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY))
286#define MBEDTLS_PK_HAVE_ECC_KEYS
287#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_ECP_C */
288
Gilles Peskineca1e6052023-09-25 16:16:26 +0200289/* Historically pkparse did not check the CBC padding when decrypting
290 * a key. This was a bug, which is now fixed. As a consequence, pkparse
291 * now needs PKCS7 padding support, but existing configurations might not
292 * enable it, so we enable it here. */
293#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
294#define MBEDTLS_CIPHER_PADDING_PKCS7
295#endif
296
Dave Rodgman94a634d2023-10-10 12:59:29 +0100297/* Backwards compatibility for some macros which were renamed to reflect that
298 * they are related to Armv8, not aarch64. */
Dave Rodgmanc5861d52023-10-10 14:01:54 +0100299#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) && \
Dave Rodgman5b89c552023-10-10 14:59:02 +0100300 !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
301#define MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
Dave Rodgman94a634d2023-10-10 12:59:29 +0100302#endif
Dave Rodgman5b89c552023-10-10 14:59:02 +0100303#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY) && !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
304#define MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
Dave Rodgman94a634d2023-10-10 12:59:29 +0100305#endif
Dave Rodgman94a634d2023-10-10 12:59:29 +0100306
Valerio Settie5707042023-10-11 11:54:42 +0200307#if (!defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_GCM_C)) || \
308 (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_GCM))
309#define MBEDTLS_SSL_HAVE_GCM
310#endif
311
312#if (!defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_CCM_C)) || \
313 (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CCM))
314#define MBEDTLS_SSL_HAVE_CCM
315#endif
316
317#if (!defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_CHACHAPOLY_C)) || \
318 (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CHACHA20_POLY1305))
319#define MBEDTLS_SSL_HAVE_CHACHAPOLY
320#endif
321
322#if defined(MBEDTLS_SSL_HAVE_GCM) || defined(MBEDTLS_SSL_HAVE_CCM) || \
323 defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
324#define MBEDTLS_SSL_HAVE_AEAD
325#endif
326
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200327#endif /* MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H */