blob: 860e071c0db3946177dd7582d2b6e6e3e5ae56a0 [file] [log] [blame]
Gilles Peskine952f4092019-05-23 20:25:48 +02001/*
Gilles Peskine3cff7682019-06-20 12:54:43 +02002 * Helper functions for tests that use the PSA Crypto API.
Gilles Peskine952f4092019-05-23 20:25:48 +02003 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskine952f4092019-05-23 20:25:48 +02006 * 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.
Gilles Peskine952f4092019-05-23 20:25:48 +020019 */
20
Gilles Peskine1838e822019-06-20 12:40:56 +020021#ifndef PSA_CRYPTO_HELPERS_H
22#define PSA_CRYPTO_HELPERS_H
23
Ronald Cron28a45ed2021-02-09 20:35:42 +010024#include "test/helpers.h"
Gilles Peskine9de97e22021-02-02 21:00:11 +010025
26#if defined(MBEDTLS_PSA_CRYPTO_C)
Ronald Cron02c78b72020-05-27 09:22:32 +020027#include "test/psa_helpers.h"
Gilles Peskine3cff7682019-06-20 12:54:43 +020028#include <psa/crypto.h>
Manuel Pégourié-Gonnard9f132b72023-03-14 10:26:46 +010029#endif
Gilles Peskine952f4092019-05-23 20:25:48 +020030
Gilles Peskine9de97e22021-02-02 21:00:11 +010031#if defined(MBEDTLS_USE_PSA_CRYPTO)
32#include "mbedtls/psa_util.h"
33#endif
34
Manuel Pégourié-Gonnardffcda562023-03-14 23:37:18 +010035#if defined(MBEDTLS_MD_LIGHT)
36#include "mbedtls/md.h"
37#endif
38
39#if defined(MBEDTLS_MD_SOME_PSA)
40#define MD_PSA_INIT() PSA_INIT()
41#define MD_PSA_DONE() PSA_DONE()
42#else /* MBEDTLS_MD_SOME_PSA */
43#define MD_PSA_INIT() ((void) 0)
44#define MD_PSA_DONE() ((void) 0)
45#endif /* MBEDTLS_MD_SOME_PSA */
46
Manuel Pégourié-Gonnard9f132b72023-03-14 10:26:46 +010047#if defined(MBEDTLS_PSA_CRYPTO_C)
48/** Initialize the PSA Crypto subsystem. */
49#define PSA_INIT() PSA_ASSERT(psa_crypto_init())
50
51/** Shut down the PSA Crypto subsystem and destroy persistent keys.
52 * Expect a clean shutdown, with no slots in use.
53 *
54 * If some key slots are still in use, record the test case as failed,
55 * but continue executing. This macro is suitable (and primarily intended)
56 * for use in the cleanup section of test functions.
57 *
58 * \note Persistent keys must be recorded with #TEST_USES_KEY_ID before
59 * creating them.
60 */
61#define PSA_DONE() \
62 do \
63 { \
64 mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__); \
65 mbedtls_test_psa_purge_key_storage(); \
66 mbedtls_psa_crypto_free(); \
67 } \
68 while (0)
69#else /*MBEDTLS_PSA_CRYPTO_C */
70#define PSA_INIT() ((void) 0)
71#define PSA_DONE() ((void) 0)
72#endif /* MBEDTLS_PSA_CRYPTO_C */
73
74#if defined(MBEDTLS_PSA_CRYPTO_C)
75
Gilles Peskine313ffb82021-02-14 12:51:14 +010076#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinee09ef872021-02-14 13:10:38 +010077
78/* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +010079int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id);
Gilles Peskinee09ef872021-02-14 13:10:38 +010080
81/** Destroy persistent keys recorded with #TEST_USES_KEY_ID.
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void mbedtls_test_psa_purge_key_storage(void);
Gilles Peskinee09ef872021-02-14 13:10:38 +010084
Gilles Peskineaae718c2021-02-14 13:46:39 +010085/** Purge the in-memory cache of persistent keys recorded with
86 * #TEST_USES_KEY_ID.
Gilles Peskine65048ad2021-02-14 14:08:22 +010087 *
88 * Call this function before calling PSA_DONE() if it's ok for
89 * persistent keys to still exist at this point.
Gilles Peskineaae718c2021-02-14 13:46:39 +010090 */
Gilles Peskine449bd832023-01-11 14:50:10 +010091void mbedtls_test_psa_purge_key_cache(void);
Gilles Peskineaae718c2021-02-14 13:46:39 +010092
Gilles Peskinee09ef872021-02-14 13:10:38 +010093/** \def TEST_USES_KEY_ID
94 *
95 * Call this macro in a test function before potentially creating a
96 * persistent key. Test functions that use this mechanism must call
97 * mbedtls_test_psa_purge_key_storage() in their cleanup code.
98 *
99 * This macro records a persistent key identifier as potentially used in the
100 * current test case. Recorded key identifiers will be cleaned up at the end
101 * of the test case, even on failure.
102 *
103 * This macro has no effect on volatile keys. Therefore, it is safe to call
104 * this macro in a test function that creates either volatile or persistent
105 * keys depending on the test data.
106 *
107 * This macro currently has no effect on special identifiers
108 * used to store implementation-specific files.
109 *
110 * Calling this macro multiple times on the same key identifier in the same
111 * test case has no effect.
112 *
113 * This macro can fail the test case if there isn't enough memory to
114 * record the key id.
115 *
116 * \param key_id The PSA key identifier to record.
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118#define TEST_USES_KEY_ID(key_id) \
119 TEST_ASSERT(mbedtls_test_uses_key_id(key_id))
Gilles Peskinee09ef872021-02-14 13:10:38 +0100120
Gilles Peskine313ffb82021-02-14 12:51:14 +0100121#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskinee09ef872021-02-14 13:10:38 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123#define TEST_USES_KEY_ID(key_id) ((void) (key_id))
124#define mbedtls_test_psa_purge_key_storage() ((void) 0)
125#define mbedtls_test_psa_purge_key_cache() ((void) 0)
Gilles Peskinee09ef872021-02-14 13:10:38 +0100126
Gilles Peskine313ffb82021-02-14 12:51:14 +0100127#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
128
Gilles Peskine1e005652020-11-24 17:41:07 +0100129/** Check for things that have not been cleaned up properly in the
130 * PSA subsystem.
131 *
132 * \return NULL if nothing has leaked.
133 * \return A string literal explaining what has not been cleaned up
134 * if applicable.
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136const char *mbedtls_test_helper_is_psa_leaking(void);
Gilles Peskinedd413d32019-05-28 15:06:43 +0200137
Gilles Peskine3cff7682019-06-20 12:54:43 +0200138/** Check that no PSA Crypto key slots are in use.
Gilles Peskinec85c2012021-01-06 20:47:16 +0100139 *
140 * If any slots are in use, mark the current test as failed and jump to
141 * the exit label. This is equivalent to
142 * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )`
143 * but with a more informative message.
Gilles Peskinedd413d32019-05-28 15:06:43 +0200144 */
Yanray Wange64b4052022-10-28 18:12:01 +0800145#define ASSERT_PSA_PRISTINE() \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100146 do \
147 { \
Yanray Wange64b4052022-10-28 18:12:01 +0800148 if (mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__)) \
149 goto exit; \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100150 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 while (0)
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200152
Gilles Peskine65048ad2021-02-14 14:08:22 +0100153/** Shut down the PSA Crypto subsystem, allowing persistent keys to survive.
154 * Expect a clean shutdown, with no slots in use.
155 *
156 * If some key slots are still in use, record the test case as failed and
157 * jump to the `exit` label.
158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159#define PSA_SESSION_DONE() \
Gilles Peskine65048ad2021-02-14 14:08:22 +0100160 do \
161 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_test_psa_purge_key_cache(); \
163 ASSERT_PSA_PRISTINE(); \
164 mbedtls_psa_crypto_free(); \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100165 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 while (0)
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200167
Gilles Peskine51681552019-05-20 19:35:37 +0200168
169
170#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100171psa_status_t mbedtls_test_record_status(psa_status_t status,
172 const char *func,
173 const char *file, int line,
174 const char *expr);
Gilles Peskine51681552019-05-20 19:35:37 +0200175
176/** Return value logging wrapper macro.
177 *
178 * Evaluate \p expr. Write a line recording its value to the log file
179 * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
180 * list of fields:
181 * ```
182 * value of expr:string:__FILE__:__LINE__:expr
183 * ```
184 *
185 * The test code does not call this macro explicitly because that would
186 * be very invasive. Instead, we instrument the source code by defining
187 * a bunch of wrapper macros like
188 * ```
189 * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
190 * ```
191 * These macro definitions must be present in `instrument_record_status.h`
192 * when building the test suites.
193 *
194 * \param string A string, normally a function name.
195 * \param expr An expression to evaluate, normally a call of the function
196 * whose name is in \p string. This expression must return
197 * a value of type #psa_status_t.
198 * \return The value of \p expr.
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200#define RECORD_STATUS(string, expr) \
201 mbedtls_test_record_status((expr), string, __FILE__, __LINE__, #expr)
Gilles Peskine51681552019-05-20 19:35:37 +0200202
203#include "instrument_record_status.h"
204
205#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
206
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200207/** Return extended key usage policies.
208 *
209 * Do a key policy permission extension on key usage policies always involves
210 * permissions of other usage policies
Tom Cosgrove1797b052022-12-04 17:19:59 +0000211 * (like PSA_KEY_USAGE_SIGN_HASH involves PSA_KEY_USAGE_SIGN_MESSAGE).
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200212 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags);
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200214
Yanray Wange64b4052022-10-28 18:12:01 +0800215/** Check that no PSA Crypto key slots are in use.
216 *
217 * If any slots are in use, mark the current test as failed.
218 *
219 * \return 0 if the key store is empty, 1 otherwise.
220 */
221int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename);
222
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100223/** Skip a test case if the given key is a 192 bits AES key and the AES
224 * implementation is at least partially provided by an accelerator or
Ronald Cron28a45ed2021-02-09 20:35:42 +0100225 * alternative implementation.
226 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100227 * Call this macro in a test case when a cryptographic operation that may
228 * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code.
229 * The macro call will skip and not fail the test case in case the operation
230 * involves a 192 bits AES key and the AES implementation is at least
231 * partially provided by an accelerator or alternative implementation.
232 *
233 * Hardware AES implementations not supporting 192 bits keys commonly exist.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100234 * Consequently, PSA test cases aim at not failing when an AES operation with
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100235 * a 192 bits key performed by an alternative AES implementation returns
236 * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro
237 * is to facilitate this and make the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100238 *
239 * \param key_type Key type
240 * \param key_bits Key length in number of bits.
241 */
242#if defined(MBEDTLS_AES_ALT) || \
243 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
244 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
245#define MBEDTLS_TEST_HAVE_ALT_AES 1
246#else
247#define MBEDTLS_TEST_HAVE_ALT_AES 0
248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_bits) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100251 do \
252 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if ((MBEDTLS_TEST_HAVE_ALT_AES) && \
254 ((key_type) == PSA_KEY_TYPE_AES) && \
255 (key_bits == 192)) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100256 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_test_skip("AES-192 not supported", __LINE__, __FILE__); \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100258 goto exit; \
259 } \
260 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 while (0)
Ronald Cron28a45ed2021-02-09 20:35:42 +0100262
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100263/** Skip a test case if a GCM operation with a nonce length different from
264 * 12 bytes fails and was performed by an accelerator or alternative
265 * implementation.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100266 *
267 * Call this macro in a test case when an AEAD cryptography operation that
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100268 * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error
269 * code. The macro call will skip and not fail the test case in case the
270 * operation involves the GCM mode, a nonce with a length different from
271 * 12 bytes and the GCM mode implementation is an alternative one.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100272 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100273 * Hardware GCM implementations not supporting nonce lengths different from
274 * 12 bytes commonly exist, as supporting a non-12-byte nonce requires
275 * additional computations involving the GHASH function.
276 * Consequently, PSA test cases aim at not failing when an AEAD operation in
277 * GCM mode with a nonce length different from 12 bytes is performed by an
278 * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED
279 * error code. The purpose of this macro is to facilitate this check and make
280 * the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100281 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100282 * \param alg The AEAD algorithm.
283 * \param nonce_length The nonce length in number of bytes.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100284 */
Ronald Cron28a45ed2021-02-09 20:35:42 +0100285#if defined(MBEDTLS_GCM_ALT) || \
286 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
287#define MBEDTLS_TEST_HAVE_ALT_GCM 1
288#else
289#define MBEDTLS_TEST_HAVE_ALT_GCM 0
290#endif
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \
293 nonce_length) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100294 do \
295 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if ((MBEDTLS_TEST_HAVE_ALT_GCM) && \
297 (PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \
298 PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \
299 ((nonce_length) != 12)) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100300 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_test_skip("GCM with non-12-byte IV is not supported", __LINE__, __FILE__); \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100302 goto exit; \
303 } \
304 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 while (0)
Ronald Cron28a45ed2021-02-09 20:35:42 +0100306
Andrzej Kurek7a320722022-09-01 09:23:09 -0400307#if !defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308#define PSA_INIT_IF_NO_MD() PSA_INIT()
309#define PSA_DONE_IF_NO_MD() PSA_DONE()
Andrzej Kurek7a320722022-09-01 09:23:09 -0400310#endif
Gilles Peskine9de97e22021-02-02 21:00:11 +0100311#endif /* MBEDTLS_PSA_CRYPTO_C */
312
Andrzej Kurek7a320722022-09-01 09:23:09 -0400313#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314#define PSA_INIT_IF_NO_MD() ((void) 0)
315#define PSA_DONE_IF_NO_MD() ((void) 0)
Andrzej Kurek7a320722022-09-01 09:23:09 -0400316#endif
Manuel Pégourié-Gonnard7dc8b952023-03-10 10:07:51 +0100317
Gilles Peskine9de97e22021-02-02 21:00:11 +0100318/** \def USE_PSA_INIT
319 *
320 * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
Ronald Cron3cec8e82022-03-27 14:34:09 +0200321 * or #MBEDTLS_SSL_PROTO_TLS1_3 (In contrast to TLS 1.2 implementation, the
322 * TLS 1.3 one uses PSA independently of the definition of
323 * #MBEDTLS_USE_PSA_CRYPTO) is enabled and do nothing otherwise. If the
324 * initialization fails, mark the test case as failed and jump to the \p exit
325 * label.
Gilles Peskine9de97e22021-02-02 21:00:11 +0100326 */
327/** \def USE_PSA_DONE
328 *
329 * Call this macro at the end of a test case if you called #USE_PSA_INIT.
330 * This is like #PSA_DONE, except that it does nothing if
331 * #MBEDTLS_USE_PSA_CRYPTO is disabled.
332 */
Ronald Cron3cec8e82022-03-27 14:34:09 +0200333#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100334#define USE_PSA_INIT() PSA_INIT()
335#define USE_PSA_DONE() PSA_DONE()
Ronald Cron3cec8e82022-03-27 14:34:09 +0200336#else /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine9de97e22021-02-02 21:00:11 +0100337/* Define empty macros so that we can use them in the preamble and teardown
338 * of every test function that uses PSA conditionally based on
339 * MBEDTLS_USE_PSA_CRYPTO. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340#define USE_PSA_INIT() ((void) 0)
341#define USE_PSA_DONE() ((void) 0)
Ronald Cron3cec8e82022-03-27 14:34:09 +0200342#endif /* !MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine9de97e22021-02-02 21:00:11 +0100343
Gilles Peskine1838e822019-06-20 12:40:56 +0200344#endif /* PSA_CRYPTO_HELPERS_H */