blob: 40ef08304512632ae749b465ee9cc791530a6d67 [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 *
Gilles Peskine975e74c2024-04-26 14:18:10 +02005 * This is an internal header. Do not include it directly.
6 *
Wenxing Houb4d03cc2024-06-19 11:04:13 +08007 * Automatically enable certain dependencies. Generally, MBEDTLS_xxx
Gilles Peskine9d6a63b2023-09-04 17:49:07 +02008 * configurations need to be explicitly enabled by the user: enabling
9 * MBEDTLS_xxx_A but not MBEDTLS_xxx_B when A requires B results in a
10 * compilation error. However, we do automatically enable certain options
11 * in some circumstances. One case is if MBEDTLS_xxx_B is an internal option
12 * used to identify parts of a module that are used by other module, and we
13 * don't want to make the symbol MBEDTLS_xxx_B part of the public API.
14 * Another case is if A didn't depend on B in earlier versions, and we
15 * want to use B in A but we need to preserve backward compatibility with
16 * configurations that explicitly activate MBEDTLS_xxx_A but not
17 * MBEDTLS_xxx_B.
18 */
19/*
20 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000021 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine9d6a63b2023-09-04 17:49:07 +020022 */
23
24#ifndef MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H
25#define MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H
26
Gilles Peskine0b8ece62024-05-16 14:46:09 +020027#if !defined(MBEDTLS_CONFIG_FILES_READ)
28#error "Do not include mbedtls/config_adjust_*.h manually! This can lead to problems, " \
29 "up to and including runtime errors such as buffer overflows. " \
30 "If you're trying to fix a complaint from check_config.h, just remove " \
31 "it from your configuration file: since Mbed TLS 3.0, it is included " \
Gilles Peskine9df78062024-05-29 09:33:04 +020032 "automatically at the right point."
Gilles Peskine0b8ece62024-05-16 14:46:09 +020033#endif /* */
34
Manuel Pégourié-Gonnard1463e492024-02-08 12:28:30 +010035/* Ideally, we'd set those as defaults in mbedtls_config.h, but
36 * putting an #ifdef _WIN32 in mbedtls_config.h would confuse config.py.
37 *
38 * So, adjust it here.
39 * Not related to crypto, but this is the bottom of the stack. */
40#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
41#if !defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && \
42 !defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
43#define MBEDTLS_PLATFORM_SNPRINTF_ALT
44#endif
45#if !defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT) && \
46 !defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
47#define MBEDTLS_PLATFORM_VSNPRINTF_ALT
48#endif
49#endif /* _MINGW32__ || (_MSC_VER && (_MSC_VER <= 1900)) */
50
Gilles Peskine9863b042024-10-24 20:56:37 +020051
52/**
53 * \def MBEDTLS_USE_PSA_CRYPTO
54 *
55 * Make the X.509 and TLS libraries use PSA for cryptographic operations as
56 * much as possible, and enable new APIs for using keys handled by PSA Crypto.
57 *
58 * \note This is a legacy symbol which still exists for backward compatibility.
59 * Up to Mbed TLS 3.x, it was not enabled by default. Now it is always
60 * enabled, and it will eventually disappear from the code base. This
61 * is not part of the public API of TF-PSA-Crypto or of Mbed TLS >=4.0.
62 */
63#define MBEDTLS_USE_PSA_CRYPTO
64
Valerio Setti97726422023-12-28 09:55:09 +010065/* Auto-enable CIPHER_C when any of the unauthenticated ciphers is builtin
66 * in PSA. */
67#if defined(MBEDTLS_PSA_CRYPTO_C) && \
68 (defined(MBEDTLS_PSA_BUILTIN_ALG_STREAM_CIPHER) || \
69 defined(MBEDTLS_PSA_BUILTIN_ALG_CTR) || \
70 defined(MBEDTLS_PSA_BUILTIN_ALG_CFB) || \
71 defined(MBEDTLS_PSA_BUILTIN_ALG_OFB) || \
72 defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING) || \
73 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
74 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) || \
Valerio Settia37ea262024-05-24 14:37:05 +020075 defined(MBEDTLS_PSA_BUILTIN_ALG_CCM_STAR_NO_TAG) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC))
Valerio Setti97726422023-12-28 09:55:09 +010077#define MBEDTLS_CIPHER_C
78#endif
79
Gilles Peskine9d6a63b2023-09-04 17:49:07 +020080/* Auto-enable MBEDTLS_MD_LIGHT based on MBEDTLS_MD_C.
81 * This allows checking for MD_LIGHT rather than MD_LIGHT || MD_C.
82 */
83#if defined(MBEDTLS_MD_C)
84#define MBEDTLS_MD_LIGHT
85#endif
86
87/* Auto-enable MBEDTLS_MD_LIGHT if needed by a module that didn't require it
88 * in a previous release, to ensure backwards compatibility.
89 */
90#if defined(MBEDTLS_ECJPAKE_C) || \
91 defined(MBEDTLS_PEM_PARSE_C) || \
92 defined(MBEDTLS_ENTROPY_C) || \
93 defined(MBEDTLS_PK_C) || \
94 defined(MBEDTLS_PKCS12_C) || \
95 defined(MBEDTLS_RSA_C) || \
96 defined(MBEDTLS_SSL_TLS_C) || \
97 defined(MBEDTLS_X509_USE_C) || \
98 defined(MBEDTLS_X509_CREATE_C)
99#define MBEDTLS_MD_LIGHT
100#endif
101
Valerio Setti85d2a982023-10-06 16:04:49 +0200102#if defined(MBEDTLS_MD_LIGHT)
103/*
Valerio Setti85d2a982023-10-06 16:04:49 +0200104 * - MBEDTLS_MD_xxx_VIA_PSA is defined if the md module may perform xxx via PSA
105 * (see below).
106 * - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm may be performed
107 * via PSA (see below).
108 * - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm may be performed
109 * via a direct legacy call (see below).
110 *
111 * The md module performs an algorithm via PSA if there is a PSA hash
112 * accelerator and the PSA driver subsytem is initialized at the time the
113 * operation is started, and makes a direct legacy call otherwise.
114 */
115
116/* PSA accelerated implementations */
117#if defined(MBEDTLS_PSA_CRYPTO_C)
118
119#if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
Valerio Setti85d2a982023-10-06 16:04:49 +0200120#define MBEDTLS_MD_MD5_VIA_PSA
121#define MBEDTLS_MD_SOME_PSA
122#endif
123#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
Valerio Setti85d2a982023-10-06 16:04:49 +0200124#define MBEDTLS_MD_SHA1_VIA_PSA
125#define MBEDTLS_MD_SOME_PSA
126#endif
127#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
Valerio Setti85d2a982023-10-06 16:04:49 +0200128#define MBEDTLS_MD_SHA224_VIA_PSA
129#define MBEDTLS_MD_SOME_PSA
130#endif
131#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
Valerio Setti85d2a982023-10-06 16:04:49 +0200132#define MBEDTLS_MD_SHA256_VIA_PSA
133#define MBEDTLS_MD_SOME_PSA
134#endif
135#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
Valerio Setti85d2a982023-10-06 16:04:49 +0200136#define MBEDTLS_MD_SHA384_VIA_PSA
137#define MBEDTLS_MD_SOME_PSA
138#endif
139#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
Valerio Setti85d2a982023-10-06 16:04:49 +0200140#define MBEDTLS_MD_SHA512_VIA_PSA
141#define MBEDTLS_MD_SOME_PSA
142#endif
143#if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
Valerio Setti85d2a982023-10-06 16:04:49 +0200144#define MBEDTLS_MD_RIPEMD160_VIA_PSA
145#define MBEDTLS_MD_SOME_PSA
146#endif
147#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_224)
Valerio Setti85d2a982023-10-06 16:04:49 +0200148#define MBEDTLS_MD_SHA3_224_VIA_PSA
149#define MBEDTLS_MD_SOME_PSA
150#endif
151#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_256)
Valerio Setti85d2a982023-10-06 16:04:49 +0200152#define MBEDTLS_MD_SHA3_256_VIA_PSA
153#define MBEDTLS_MD_SOME_PSA
154#endif
155#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_384)
Valerio Setti85d2a982023-10-06 16:04:49 +0200156#define MBEDTLS_MD_SHA3_384_VIA_PSA
157#define MBEDTLS_MD_SOME_PSA
158#endif
159#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_512)
Valerio Setti85d2a982023-10-06 16:04:49 +0200160#define MBEDTLS_MD_SHA3_512_VIA_PSA
161#define MBEDTLS_MD_SOME_PSA
162#endif
163#endif /* MBEDTLS_PSA_CRYPTO_C */
164
165/* Built-in implementations */
Elena Uziunaite7f85f1f2024-09-05 20:15:57 +0100166#if defined(MBEDTLS_MD5_C) || \
167 defined(MBEDTLS_SHA1_C) || \
168 defined(MBEDTLS_SHA224_C) || \
169 defined(MBEDTLS_SHA256_C) || \
170 defined(MBEDTLS_SHA384_C) || \
171 defined(MBEDTLS_SHA512_C) || \
172 defined(MBEDTLS_SHA3_C) || \
173 defined(MBEDTLS_RIPEMD160_C)
Valerio Setti85d2a982023-10-06 16:04:49 +0200174#define MBEDTLS_MD_SOME_LEGACY
175#endif
176
177#endif /* MBEDTLS_MD_LIGHT */
178
Valerio Setti8bba0872023-12-12 11:49:02 +0100179/* BLOCK_CIPHER module can dispatch to PSA when:
180 * - PSA is enabled and drivers have been initialized
181 * - desired key type is supported on the PSA side
182 * If the above conditions are not met, but the legacy support is enabled, then
Valerio Setti2684e3f2023-12-13 16:53:02 +0100183 * BLOCK_CIPHER will dynamically fallback to it.
Valerio Setti4a5d57d2023-12-14 09:34:15 +0100184 *
185 * In case BLOCK_CIPHER is defined (see below) the following symbols/helpers
186 * can be used to define its capabilities:
187 * - MBEDTLS_BLOCK_CIPHER_SOME_PSA: there is at least 1 key type between AES,
188 * ARIA and Camellia which is supported through a driver;
189 * - MBEDTLS_BLOCK_CIPHER_xxx_VIA_PSA: xxx key type is supported through a
190 * driver;
191 * - MBEDTLS_BLOCK_CIPHER_xxx_VIA_LEGACY: xxx key type is supported through
192 * a legacy module (i.e. MBEDTLS_xxx_C)
Valerio Setti8bba0872023-12-12 11:49:02 +0100193 */
Valerio Setti8bba0872023-12-12 11:49:02 +0100194#if defined(MBEDTLS_PSA_CRYPTO_C)
195#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
196#define MBEDTLS_BLOCK_CIPHER_AES_VIA_PSA
197#define MBEDTLS_BLOCK_CIPHER_SOME_PSA
198#endif
199#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ARIA)
200#define MBEDTLS_BLOCK_CIPHER_ARIA_VIA_PSA
201#define MBEDTLS_BLOCK_CIPHER_SOME_PSA
202#endif
203#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_CAMELLIA)
204#define MBEDTLS_BLOCK_CIPHER_CAMELLIA_VIA_PSA
205#define MBEDTLS_BLOCK_CIPHER_SOME_PSA
206#endif
207#endif /* MBEDTLS_PSA_CRYPTO_C */
208
209#if defined(MBEDTLS_AES_C)
210#define MBEDTLS_BLOCK_CIPHER_AES_VIA_LEGACY
211#endif
212#if defined(MBEDTLS_ARIA_C)
213#define MBEDTLS_BLOCK_CIPHER_ARIA_VIA_LEGACY
214#endif
215#if defined(MBEDTLS_CAMELLIA_C)
216#define MBEDTLS_BLOCK_CIPHER_CAMELLIA_VIA_LEGACY
217#endif
218
Valerio Setti4a5d57d2023-12-14 09:34:15 +0100219/* Helpers to state that BLOCK_CIPHER module supports AES, ARIA and/or Camellia
220 * block ciphers via either PSA or legacy. */
Valerio Setti8bba0872023-12-12 11:49:02 +0100221#if defined(MBEDTLS_BLOCK_CIPHER_AES_VIA_PSA) || \
222 defined(MBEDTLS_BLOCK_CIPHER_AES_VIA_LEGACY)
223#define MBEDTLS_BLOCK_CIPHER_CAN_AES
224#endif
225#if defined(MBEDTLS_BLOCK_CIPHER_ARIA_VIA_PSA) || \
226 defined(MBEDTLS_BLOCK_CIPHER_ARIA_VIA_LEGACY)
227#define MBEDTLS_BLOCK_CIPHER_CAN_ARIA
228#endif
229#if defined(MBEDTLS_BLOCK_CIPHER_CAMELLIA_VIA_PSA) || \
230 defined(MBEDTLS_BLOCK_CIPHER_CAMELLIA_VIA_LEGACY)
231#define MBEDTLS_BLOCK_CIPHER_CAN_CAMELLIA
232#endif
233
Valerio Setti4a5d57d2023-12-14 09:34:15 +0100234/* GCM_C and CCM_C can either depend on (in order of preference) BLOCK_CIPHER_C
235 * or CIPHER_C. The former is auto-enabled when:
236 * - CIPHER_C is not defined, which is also the legacy solution;
237 * - BLOCK_CIPHER_SOME_PSA because in this case BLOCK_CIPHER can take advantage
238 * of the driver's acceleration.
239 */
240#if (defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)) && \
241 (!defined(MBEDTLS_CIPHER_C) || defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA))
242#define MBEDTLS_BLOCK_CIPHER_C
243#endif
244
Valerio Settibfa675f2023-12-20 09:52:08 +0100245/* Helpers for GCM/CCM capabilities */
246#if (defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_AES_C)) || \
247 (defined(MBEDTLS_BLOCK_CIPHER_C) && defined(MBEDTLS_BLOCK_CIPHER_CAN_AES))
248#define MBEDTLS_CCM_GCM_CAN_AES
249#endif
250
251#if (defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_ARIA_C)) || \
252 (defined(MBEDTLS_BLOCK_CIPHER_C) && defined(MBEDTLS_BLOCK_CIPHER_CAN_ARIA))
253#define MBEDTLS_CCM_GCM_CAN_ARIA
254#endif
255
256#if (defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_CAMELLIA_C)) || \
257 (defined(MBEDTLS_BLOCK_CIPHER_C) && defined(MBEDTLS_BLOCK_CIPHER_CAN_CAMELLIA))
258#define MBEDTLS_CCM_GCM_CAN_CAMELLIA
259#endif
260
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200261/* MBEDTLS_ECP_LIGHT is auto-enabled by the following symbols:
262 * - MBEDTLS_ECP_C because now it consists of MBEDTLS_ECP_LIGHT plus functions
263 * for curve arithmetic. As a consequence if MBEDTLS_ECP_C is required for
264 * some reason, then MBEDTLS_ECP_LIGHT should be enabled as well.
265 * - MBEDTLS_PK_PARSE_EC_EXTENDED and MBEDTLS_PK_PARSE_EC_COMPRESSED because
266 * these features are not supported in PSA so the only way to have them is
267 * to enable the built-in solution.
268 * Both of them are temporary dependencies:
269 * - PK_PARSE_EC_EXTENDED will be removed after #7779 and #7789
270 * - support for compressed points should also be added to PSA, but in this
271 * case there is no associated issue to track it yet.
272 * - PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE because Weierstrass key derivation
273 * still depends on ECP_LIGHT.
274 * - PK_C + USE_PSA + PSA_WANT_ALG_ECDSA is a temporary dependency which will
275 * be fixed by #7453.
276 */
277#if defined(MBEDTLS_ECP_C) || \
278 defined(MBEDTLS_PK_PARSE_EC_EXTENDED) || \
279 defined(MBEDTLS_PK_PARSE_EC_COMPRESSED) || \
280 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
281#define MBEDTLS_ECP_LIGHT
282#endif
283
Valerio Setti89f5af82024-04-18 06:54:32 +0200284/* Backward compatibility: after #8740 the RSA module offers functions to parse
285 * and write RSA private/public keys without relying on the PK one. Of course
286 * this needs ASN1 support to do so, so we enable it here. */
287#if defined(MBEDTLS_RSA_C)
288#define MBEDTLS_ASN1_PARSE_C
289#define MBEDTLS_ASN1_WRITE_C
290#endif
291
Thomas Daubney540324c2023-10-06 17:07:24 +0100292/* MBEDTLS_PK_PARSE_EC_COMPRESSED is introduced in Mbed TLS version 3.5, while
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200293 * in previous version compressed points were automatically supported as long
294 * as PK_PARSE_C and ECP_C were enabled. As a consequence, for backward
295 * compatibility, we auto-enable PK_PARSE_EC_COMPRESSED when these conditions
296 * are met. */
297#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_ECP_C)
298#define MBEDTLS_PK_PARSE_EC_COMPRESSED
299#endif
300
301/* Helper symbol to state that there is support for ECDH, either through
302 * library implementation (ECDH_C) or through PSA. */
303#if (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_ECDH)) || \
304 (!defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C))
305#define MBEDTLS_CAN_ECDH
306#endif
307
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200308/* If MBEDTLS_PSA_CRYPTO_C is defined, make sure MBEDTLS_PSA_CRYPTO_CLIENT
309 * is defined as well to include all PSA code.
310 */
311#if defined(MBEDTLS_PSA_CRYPTO_C)
312#define MBEDTLS_PSA_CRYPTO_CLIENT
313#endif /* MBEDTLS_PSA_CRYPTO_C */
314
Gilles Peskineca1e6052023-09-25 16:16:26 +0200315/* Historically pkparse did not check the CBC padding when decrypting
316 * a key. This was a bug, which is now fixed. As a consequence, pkparse
317 * now needs PKCS7 padding support, but existing configurations might not
318 * enable it, so we enable it here. */
319#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
320#define MBEDTLS_CIPHER_PADDING_PKCS7
321#endif
322
Dave Rodgman94a634d2023-10-10 12:59:29 +0100323/* Backwards compatibility for some macros which were renamed to reflect that
324 * they are related to Armv8, not aarch64. */
Dave Rodgmanc5861d52023-10-10 14:01:54 +0100325#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) && \
Dave Rodgman5b89c552023-10-10 14:59:02 +0100326 !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
327#define MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
Dave Rodgman94a634d2023-10-10 12:59:29 +0100328#endif
Dave Rodgman5b89c552023-10-10 14:59:02 +0100329#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY) && !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
330#define MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
Dave Rodgman94a634d2023-10-10 12:59:29 +0100331#endif
Dave Rodgman94a634d2023-10-10 12:59:29 +0100332
Wenxing Houb4d03cc2024-06-19 11:04:13 +0800333/* Some internal helpers to determine which operation modes are available. */
Yanray Wang4ed86912023-11-16 15:20:56 +0800334
Elena Uziunaitec0d69432024-08-20 14:53:19 +0100335#if defined(PSA_WANT_ALG_GCM) || defined(PSA_WANT_ALG_CCM) || \
336 defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Valerio Settie5707042023-10-11 11:54:42 +0200337#define MBEDTLS_SSL_HAVE_AEAD
338#endif
339
Gilles Peskine9d6a63b2023-09-04 17:49:07 +0200340#endif /* MBEDTLS_CONFIG_ADJUST_LEGACY_CRYPTO_H */