blob: fe87399e7734fa0e37360dc1fed3e363299712f2 [file] [log] [blame]
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +02001/**
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +02002 * Macros to express dependencies for code and tests that may use either the
3 * legacy API or PSA in various builds; mostly for internal use.
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +02004 *
Gilles Peskine49540ac2022-10-26 18:02:56 +02005 */
6/*
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +02007 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23/*
Manuel Pégourié-Gonnard138387f2022-09-16 09:45:43 +020024 * Note: applications that are targeting a specific configuration do not need
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +020025 * to use these macros; instead they should directly use the functions they
26 * know are available in their configuration.
27 *
28 * Note: code that is purely based on PSA Crypto (psa_xxx() functions)
29 * does not need to use these macros; instead it should use the relevant
30 * PSA_WANT_xxx macros.
31 *
32 * Note: code that is purely based on the legacy crypto APIs (mbedtls_xxx())
33 * does not need to use these macros; instead it should use the relevant
Manuel Pégourié-Gonnard138387f2022-09-16 09:45:43 +020034 * MBEDTLS_xxx macros.
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +020035 *
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020036 * These macros are for code that wants to use <crypto feature> and will do so
37 * using <legacy API> or PSA depending on <condition>, where:
38 * - <crypto feature> will generally be an algorithm (SHA-256, ECDH) but may
39 * also be a key type (AES, RSA, EC) or domain parameters (elliptic curve);
40 * - <legacy API> will be either:
41 * - low-level module API (aes.h, sha256.h), or
42 * - an abstraction layer (md.h, cipher.h);
43 * - <condition> will be either:
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +020044 * - depending on what's available in the build:
45 * legacy API used if available, PSA otherwise
46 * (this is done to ensure backwards compatibility); or
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020047 * - depending on whether MBEDTLS_USE_PSA_CRYPTO is defined.
48 *
49 * Examples:
50 * - TLS 1.2 will compute hashes using either mbedtls_md_xxx() (and
51 * mbedtls_sha256_xxx()) or psa_aead_xxx() depending on whether
52 * MBEDTLS_USE_PSA_CRYPTO is defined;
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +020053 * - RSA PKCS#1 v2.1 will compute hashes (for padding) using either
54 * `mbedtls_md()` if it's available, or `psa_hash_compute()` otherwise;
55 * - PEM decoding of PEM-encrypted keys will compute MD5 hashes using either
56 * `mbedtls_md5_xxx()` if it's available, or `psa_hash_xxx()` otherwise.
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020057 *
58 * Note: the macros are essential to express test dependencies. Inside code,
59 * we could instead just use the equivalent pre-processor condition, but
60 * that's not possible in test dependencies where we need a single macro.
61 * Hopefully, using these macros in code will also help with consistency.
62 *
63 * The naming scheme for these macros is:
64 * MBEDTLS_HAS_feature_VIA_legacy_OR_PSA(_condition)
65 * where:
66 * - feature is expressed the same way as in PSA_WANT macros, for example:
67 * KEY_TYPE_AES, ALG_SHA_256, ECC_SECP_R1_256;
68 * - legacy is either LOWLEVEL or the name of the layer: MD, CIPHER;
Manuel Pégourié-Gonnard68429fc2022-07-27 20:37:12 +020069 * - condition is omitted if it's based on availability, else it's
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020070 * BASED_ON_USE_PSA.
71 *
72 * Coming back to the examples above:
73 * - TLS 1.2 will determine if it can use SHA-256 using
74 * MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA
75 * for the purposes of negotiation, and in test dependencies;
76 * - RSA PKCS#1 v2.1 tests that used SHA-256 will depend on
77 * MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA
78 * - PEM decoding code and its associated tests will depend on
79 * MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA
80 *
81 * Note: every time it's possible to use, say SHA-256, via the MD API, then
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +020082 * it's also possible to use it via the low-level API. So, code that wants to
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020083 * use SHA-256 via both APIs only needs to depend on the MD macro. Also, it
Manuel Pégourié-Gonnardc42c7e62022-09-15 11:11:00 +020084 * just so happens that all the code choosing which API to use based on
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020085 * MBEDTLS_USE_PSA_CRYPTO (X.509, TLS 1.2/shared), always uses the abstraction
86 * layer (sometimes in addition to the low-level API), so we don't need the
87 * MBEDTLS_HAS_feature_VIA_LOWLEVEL_OR_PSA_BASED_ON_USE_PSA macros.
88 * (PK, while obeying MBEDTLS_USE_PSA_CRYPTO, doesn't compute hashes itself,
89 * even less makes use of ciphers.)
90 *
91 * Note: the macros MBEDTLS_HAS_feature_VIA_LOWLEVEL_OR_PSA are the minimal
92 * condition for being able to use <feature> at all. As such, they should be
93 * used for guarding data about <feature>, such as OIDs or size. For example,
94 * OID values related to SHA-256 are only useful when SHA-256 can be used at
95 * least in some way.
96 */
97
98#ifndef MBEDTLS_OR_PSA_HELPERS_H
99#define MBEDTLS_OR_PSA_HELPERS_H
100
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +0200101#include "mbedtls/build_info.h"
Przemek Stekielc410ccc2022-08-18 10:51:31 +0200102#if defined(MBEDTLS_PSA_CRYPTO_C)
103#include "psa/crypto.h"
104#endif /* MBEDTLS_PSA_CRYPTO_C */
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200105
106/*
107 * Hashes
108 */
109
110/* Hashes using low-level or PSA based on availability */
111#if defined(MBEDTLS_MD5_C) || \
112 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_MD5) )
113#define MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA
114#endif
115#if defined(MBEDTLS_RIPEMD160_C) || \
116 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_RIPEMD160) )
117#define MBEDTLS_HAS_ALG_RIPEMD160_VIA_LOWLEVEL_OR_PSA
118#endif
119#if defined(MBEDTLS_SHA1_C) || \
120 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_1) )
121#define MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA
122#endif
123#if defined(MBEDTLS_SHA224_C) || \
124 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_224) )
125#define MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA
126#endif
127#if defined(MBEDTLS_SHA256_C) || \
128 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_256) )
129#define MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA
130#endif
131#if defined(MBEDTLS_SHA384_C) || \
132 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_384) )
133#define MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA
134#endif
135#if defined(MBEDTLS_SHA512_C) || \
136 ( defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_512) )
137#define MBEDTLS_HAS_ALG_SHA_512_VIA_LOWLEVEL_OR_PSA
138#endif
139
140/* Hashes using MD or PSA based on availability */
141#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_MD5_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200142 ( !defined(MBEDTLS_MD_C) && \
143 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_MD5) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200144#define MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA
145#endif
146#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_RIPEMD160_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200147 ( !defined(MBEDTLS_MD_C) && \
148 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_RIPEMD160) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200149#define MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA
150#endif
151#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA1_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200152 ( !defined(MBEDTLS_MD_C) && \
153 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_1) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200154#define MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA
155#endif
156#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA224_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200157 ( !defined(MBEDTLS_MD_C) && \
158 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_224) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200159#define MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA
160#endif
161#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA256_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200162 ( !defined(MBEDTLS_MD_C) && \
163 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_256) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200164#define MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA
165#endif
166#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA384_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200167 ( !defined(MBEDTLS_MD_C) && \
168 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_384) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200169#define MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA
170#endif
171#if ( defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA512_C) ) || \
Manuel Pégourié-Gonnard79b99f42022-07-27 23:04:21 +0200172 ( !defined(MBEDTLS_MD_C) && \
173 defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_512) )
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +0200174#define MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA
175#endif
176
177/* Hashes using MD or PSA based on MBEDTLS_USE_PSA_CRYPTO */
178#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
179 defined(MBEDTLS_MD_C) && defined(MBEDTLS_MD5_C) ) || \
180 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_MD5) )
181#define MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA
182#endif
183#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
184 defined(MBEDTLS_MD_C) && defined(MBEDTLS_RIPEMD160_C) ) || \
185 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_RIPEMD160) )
186#define MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA_BASED_ON_USE_PSA
187#endif
188#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
189 defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA1_C) ) || \
190 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_SHA_1) )
191#define MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA
192#endif
193#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
194 defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA224_C) ) || \
195 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_SHA_224) )
196#define MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA
197#endif
198#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
199 defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA256_C) ) || \
200 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_SHA_256) )
201#define MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA
202#endif
203#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
204 defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA384_C) ) || \
205 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_SHA_384) )
206#define MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA
207#endif
208#if ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
209 defined(MBEDTLS_MD_C) && defined(MBEDTLS_SHA512_C) ) || \
210 ( defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_SHA_512) )
211#define MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA
212#endif
213
214#endif /* MBEDTLS_OR_PSA_HELPERS_H */