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