blob: 18d46d8b2bed814f9951ad2d4d714326cacd3433 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine952f4092019-05-23 20:25:48 +02007 */
8
Gilles Peskine1838e822019-06-20 12:40:56 +02009#ifndef PSA_CRYPTO_HELPERS_H
10#define PSA_CRYPTO_HELPERS_H
11
Ronald Cron28a45ed2021-02-09 20:35:42 +010012#include "test/helpers.h"
Gilles Peskine9de97e22021-02-02 21:00:11 +010013
14#if defined(MBEDTLS_PSA_CRYPTO_C)
15
Ronald Cron02c78b72020-05-27 09:22:32 +020016#include "test/psa_helpers.h"
Gilles Peskine952f4092019-05-23 20:25:48 +020017
Gilles Peskine3cff7682019-06-20 12:54:43 +020018#include <psa/crypto.h>
Gilles Peskine952f4092019-05-23 20:25:48 +020019
Gilles Peskine9de97e22021-02-02 21:00:11 +010020#if defined(MBEDTLS_USE_PSA_CRYPTO)
21#include "mbedtls/psa_util.h"
22#endif
23
David Horstmann5d05b472023-12-20 11:26:40 +000024#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
25 && defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann9c97fda2023-12-14 14:27:50 +000026#include "test/psa_memory_poisoning_wrappers.h"
27#endif
28
Gilles Peskine313ffb82021-02-14 12:51:14 +010029#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinee09ef872021-02-14 13:10:38 +010030
31/* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id);
Gilles Peskinee09ef872021-02-14 13:10:38 +010033
34/** Destroy persistent keys recorded with #TEST_USES_KEY_ID.
35 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036void mbedtls_test_psa_purge_key_storage(void);
Gilles Peskinee09ef872021-02-14 13:10:38 +010037
Gilles Peskineaae718c2021-02-14 13:46:39 +010038/** Purge the in-memory cache of persistent keys recorded with
39 * #TEST_USES_KEY_ID.
Gilles Peskine65048ad2021-02-14 14:08:22 +010040 *
41 * Call this function before calling PSA_DONE() if it's ok for
42 * persistent keys to still exist at this point.
Gilles Peskineaae718c2021-02-14 13:46:39 +010043 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044void mbedtls_test_psa_purge_key_cache(void);
Gilles Peskineaae718c2021-02-14 13:46:39 +010045
Gilles Peskinee09ef872021-02-14 13:10:38 +010046/** \def TEST_USES_KEY_ID
47 *
48 * Call this macro in a test function before potentially creating a
49 * persistent key. Test functions that use this mechanism must call
50 * mbedtls_test_psa_purge_key_storage() in their cleanup code.
51 *
52 * This macro records a persistent key identifier as potentially used in the
53 * current test case. Recorded key identifiers will be cleaned up at the end
54 * of the test case, even on failure.
55 *
56 * This macro has no effect on volatile keys. Therefore, it is safe to call
57 * this macro in a test function that creates either volatile or persistent
58 * keys depending on the test data.
59 *
60 * This macro currently has no effect on special identifiers
61 * used to store implementation-specific files.
62 *
63 * Calling this macro multiple times on the same key identifier in the same
64 * test case has no effect.
65 *
66 * This macro can fail the test case if there isn't enough memory to
67 * record the key id.
68 *
69 * \param key_id The PSA key identifier to record.
70 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071#define TEST_USES_KEY_ID(key_id) \
72 TEST_ASSERT(mbedtls_test_uses_key_id(key_id))
Gilles Peskinee09ef872021-02-14 13:10:38 +010073
Gilles Peskine313ffb82021-02-14 12:51:14 +010074#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskinee09ef872021-02-14 13:10:38 +010075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define TEST_USES_KEY_ID(key_id) ((void) (key_id))
77#define mbedtls_test_psa_purge_key_storage() ((void) 0)
78#define mbedtls_test_psa_purge_key_cache() ((void) 0)
Gilles Peskinee09ef872021-02-14 13:10:38 +010079
Gilles Peskine313ffb82021-02-14 12:51:14 +010080#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082#define PSA_INIT() PSA_ASSERT(psa_crypto_init())
Gilles Peskine9de97e22021-02-02 21:00:11 +010083
Gilles Peskine1e005652020-11-24 17:41:07 +010084/** Check for things that have not been cleaned up properly in the
85 * PSA subsystem.
86 *
87 * \return NULL if nothing has leaked.
88 * \return A string literal explaining what has not been cleaned up
89 * if applicable.
90 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091const char *mbedtls_test_helper_is_psa_leaking(void);
Gilles Peskinedd413d32019-05-28 15:06:43 +020092
Gilles Peskine3cff7682019-06-20 12:54:43 +020093/** Check that no PSA Crypto key slots are in use.
Gilles Peskinec85c2012021-01-06 20:47:16 +010094 *
95 * If any slots are in use, mark the current test as failed and jump to
96 * the exit label. This is equivalent to
97 * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )`
98 * but with a more informative message.
Gilles Peskinedd413d32019-05-28 15:06:43 +020099 */
Yanray Wang89b4d122022-10-28 18:12:01 +0800100#define ASSERT_PSA_PRISTINE() \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100101 do \
102 { \
Yanray Wang89b4d122022-10-28 18:12:01 +0800103 if (mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__)) \
104 goto exit; \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100105 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 while (0)
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200107
Gilles Peskine65048ad2021-02-14 14:08:22 +0100108/** Shut down the PSA Crypto subsystem and destroy persistent keys.
109 * Expect a clean shutdown, with no slots in use.
110 *
111 * If some key slots are still in use, record the test case as failed,
112 * but continue executing. This macro is suitable (and primarily intended)
113 * for use in the cleanup section of test functions.
114 *
115 * \note Persistent keys must be recorded with #TEST_USES_KEY_ID before
116 * creating them.
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200117 */
Yanray Wang89b4d122022-10-28 18:12:01 +0800118#define PSA_DONE() \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100119 do \
120 { \
Yanray Wang89b4d122022-10-28 18:12:01 +0800121 mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__); \
122 mbedtls_test_psa_purge_key_storage(); \
123 mbedtls_psa_crypto_free(); \
Gilles Peskine65048ad2021-02-14 14:08:22 +0100124 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 while (0)
Gilles Peskine65048ad2021-02-14 14:08:22 +0100126
127/** Shut down the PSA Crypto subsystem, allowing persistent keys to survive.
128 * Expect a clean shutdown, with no slots in use.
129 *
130 * If some key slots are still in use, record the test case as failed and
131 * jump to the `exit` label.
132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133#define PSA_SESSION_DONE() \
Gilles Peskine65048ad2021-02-14 14:08:22 +0100134 do \
135 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_test_psa_purge_key_cache(); \
137 ASSERT_PSA_PRISTINE(); \
138 mbedtls_psa_crypto_free(); \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100139 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 while (0)
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200141
Gilles Peskine51681552019-05-20 19:35:37 +0200142
143
144#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145psa_status_t mbedtls_test_record_status(psa_status_t status,
146 const char *func,
147 const char *file, int line,
148 const char *expr);
Gilles Peskine51681552019-05-20 19:35:37 +0200149
150/** Return value logging wrapper macro.
151 *
152 * Evaluate \p expr. Write a line recording its value to the log file
153 * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
154 * list of fields:
155 * ```
156 * value of expr:string:__FILE__:__LINE__:expr
157 * ```
158 *
159 * The test code does not call this macro explicitly because that would
160 * be very invasive. Instead, we instrument the source code by defining
161 * a bunch of wrapper macros like
162 * ```
163 * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
164 * ```
165 * These macro definitions must be present in `instrument_record_status.h`
166 * when building the test suites.
167 *
168 * \param string A string, normally a function name.
169 * \param expr An expression to evaluate, normally a call of the function
170 * whose name is in \p string. This expression must return
171 * a value of type #psa_status_t.
172 * \return The value of \p expr.
173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174#define RECORD_STATUS(string, expr) \
175 mbedtls_test_record_status((expr), string, __FILE__, __LINE__, #expr)
Gilles Peskine51681552019-05-20 19:35:37 +0200176
177#include "instrument_record_status.h"
178
179#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
180
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200181/** Return extended key usage policies.
182 *
183 * Do a key policy permission extension on key usage policies always involves
184 * permissions of other usage policies
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000185 * (like PSA_KEY_USAGE_SIGN_HASH involves PSA_KEY_USAGE_SIGN_MESSAGE).
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200186 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags);
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200188
Yanray Wang89b4d122022-10-28 18:12:01 +0800189/** Check that no PSA Crypto key slots are in use.
190 *
191 * If any slots are in use, mark the current test as failed.
192 *
193 * \return 0 if the key store is empty, 1 otherwise.
194 */
195int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename);
196
Gilles Peskine73521b02023-04-28 21:01:49 +0200197
198
199#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
200/* The #MBEDTLS_PSA_INJECT_ENTROPY feature requires two extra platform
201 * functions, which must be configured as #MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
202 * and #MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO. The job of these functions
203 * is to read and write from the entropy seed file, which is located
204 * in the PSA ITS file whose uid is #PSA_CRYPTO_ITS_RANDOM_SEED_UID.
205 * (These could have been provided as library functions, but for historical
206 * reasons, they weren't, and so each integrator has to provide a copy
207 * of these functions.)
208 *
209 * Provide implementations of these functions for testing. */
210int mbedtls_test_inject_entropy_seed_read(unsigned char *buf, size_t len);
211int mbedtls_test_inject_entropy_seed_write(unsigned char *buf, size_t len);
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +0200212
213
214/** Make sure that the injected entropy is present.
215 *
216 * When MBEDTLS_PSA_INJECT_ENTROPY is enabled, psa_crypto_init()
217 * will fail if the PSA entropy seed is not present.
218 * This function must be called at least once in a test suite or other
219 * program before any call to psa_crypto_init().
220 * It does not need to be called in each test case.
221 *
222 * The test framework calls this function before running any test case.
223 *
224 * The few tests that might remove the entropy file must call this function
225 * in their cleanup.
226 */
227int mbedtls_test_inject_entropy_restore(void);
Gilles Peskine73521b02023-04-28 21:01:49 +0200228#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
229
230
231
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100232/** Skip a test case if the given key is a 192 bits AES key and the AES
233 * implementation is at least partially provided by an accelerator or
Ronald Cron28a45ed2021-02-09 20:35:42 +0100234 * alternative implementation.
235 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100236 * Call this macro in a test case when a cryptographic operation that may
237 * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code.
238 * The macro call will skip and not fail the test case in case the operation
239 * involves a 192 bits AES key and the AES implementation is at least
240 * partially provided by an accelerator or alternative implementation.
241 *
242 * Hardware AES implementations not supporting 192 bits keys commonly exist.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100243 * Consequently, PSA test cases aim at not failing when an AES operation with
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100244 * a 192 bits key performed by an alternative AES implementation returns
245 * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro
246 * is to facilitate this and make the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100247 *
248 * \param key_type Key type
249 * \param key_bits Key length in number of bits.
250 */
251#if defined(MBEDTLS_AES_ALT) || \
252 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
253 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
254#define MBEDTLS_TEST_HAVE_ALT_AES 1
255#else
256#define MBEDTLS_TEST_HAVE_ALT_AES 0
257#endif
258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_bits) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100260 do \
261 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if ((MBEDTLS_TEST_HAVE_ALT_AES) && \
263 ((key_type) == PSA_KEY_TYPE_AES) && \
264 (key_bits == 192)) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100265 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 mbedtls_test_skip("AES-192 not supported", __LINE__, __FILE__); \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100267 goto exit; \
268 } \
269 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 while (0)
Ronald Cron28a45ed2021-02-09 20:35:42 +0100271
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100272/** Skip a test case if a GCM operation with a nonce length different from
273 * 12 bytes fails and was performed by an accelerator or alternative
274 * implementation.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100275 *
276 * Call this macro in a test case when an AEAD cryptography operation that
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100277 * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error
278 * code. The macro call will skip and not fail the test case in case the
279 * operation involves the GCM mode, a nonce with a length different from
280 * 12 bytes and the GCM mode implementation is an alternative one.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100281 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100282 * Hardware GCM implementations not supporting nonce lengths different from
283 * 12 bytes commonly exist, as supporting a non-12-byte nonce requires
284 * additional computations involving the GHASH function.
285 * Consequently, PSA test cases aim at not failing when an AEAD operation in
286 * GCM mode with a nonce length different from 12 bytes is performed by an
287 * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED
288 * error code. The purpose of this macro is to facilitate this check and make
289 * the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100290 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100291 * \param alg The AEAD algorithm.
292 * \param nonce_length The nonce length in number of bytes.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100293 */
Ronald Cron28a45ed2021-02-09 20:35:42 +0100294#if defined(MBEDTLS_GCM_ALT) || \
295 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
296#define MBEDTLS_TEST_HAVE_ALT_GCM 1
297#else
298#define MBEDTLS_TEST_HAVE_ALT_GCM 0
299#endif
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \
302 nonce_length) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100303 do \
304 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 if ((MBEDTLS_TEST_HAVE_ALT_GCM) && \
306 (PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \
307 PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \
308 ((nonce_length) != 12)) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100309 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 mbedtls_test_skip("GCM with non-12-byte IV is not supported", __LINE__, __FILE__); \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100311 goto exit; \
312 } \
313 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 while (0)
Ronald Cron28a45ed2021-02-09 20:35:42 +0100315
Gilles Peskine9de97e22021-02-02 21:00:11 +0100316#endif /* MBEDTLS_PSA_CRYPTO_C */
317
318/** \def USE_PSA_INIT
319 *
320 * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
321 * is enabled and do nothing otherwise. If the initialization fails, mark
322 * the test case as failed and jump to the \p exit label.
323 */
324/** \def USE_PSA_DONE
325 *
326 * Call this macro at the end of a test case if you called #USE_PSA_INIT.
327 * This is like #PSA_DONE, except that it does nothing if
328 * #MBEDTLS_USE_PSA_CRYPTO is disabled.
329 */
330#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331#define USE_PSA_INIT() PSA_INIT()
332#define USE_PSA_DONE() PSA_DONE()
Gilles Peskine9de97e22021-02-02 21:00:11 +0100333#else /* MBEDTLS_USE_PSA_CRYPTO */
334/* Define empty macros so that we can use them in the preamble and teardown
335 * of every test function that uses PSA conditionally based on
336 * MBEDTLS_USE_PSA_CRYPTO. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337#define USE_PSA_INIT() ((void) 0)
338#define USE_PSA_DONE() ((void) 0)
Gilles Peskine9de97e22021-02-02 21:00:11 +0100339#endif /* !MBEDTLS_USE_PSA_CRYPTO */
340
Gilles Peskine1838e822019-06-20 12:40:56 +0200341#endif /* PSA_CRYPTO_HELPERS_H */