blob: ed64ed0edd34021b0e166c0cf28865486e761009 [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
Manuel Pégourié-Gonnard9f132b72023-03-14 10:26:46 +010039#if defined(MBEDTLS_PSA_CRYPTO_C)
40/** Initialize the PSA Crypto subsystem. */
41#define PSA_INIT() PSA_ASSERT(psa_crypto_init())
42
43/** Shut down the PSA Crypto subsystem and destroy persistent keys.
44 * Expect a clean shutdown, with no slots in use.
45 *
46 * If some key slots are still in use, record the test case as failed,
47 * but continue executing. This macro is suitable (and primarily intended)
48 * for use in the cleanup section of test functions.
49 *
50 * \note Persistent keys must be recorded with #TEST_USES_KEY_ID before
51 * creating them.
52 */
53#define PSA_DONE() \
54 do \
55 { \
56 mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__); \
57 mbedtls_test_psa_purge_key_storage(); \
58 mbedtls_psa_crypto_free(); \
59 } \
60 while (0)
61#else /*MBEDTLS_PSA_CRYPTO_C */
62#define PSA_INIT() ((void) 0)
63#define PSA_DONE() ((void) 0)
64#endif /* MBEDTLS_PSA_CRYPTO_C */
65
66#if defined(MBEDTLS_PSA_CRYPTO_C)
67
Gilles Peskine313ffb82021-02-14 12:51:14 +010068#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinee09ef872021-02-14 13:10:38 +010069
70/* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id);
Gilles Peskinee09ef872021-02-14 13:10:38 +010072
73/** Destroy persistent keys recorded with #TEST_USES_KEY_ID.
74 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_test_psa_purge_key_storage(void);
Gilles Peskinee09ef872021-02-14 13:10:38 +010076
Gilles Peskineaae718c2021-02-14 13:46:39 +010077/** Purge the in-memory cache of persistent keys recorded with
78 * #TEST_USES_KEY_ID.
Gilles Peskine65048ad2021-02-14 14:08:22 +010079 *
80 * Call this function before calling PSA_DONE() if it's ok for
81 * persistent keys to still exist at this point.
Gilles Peskineaae718c2021-02-14 13:46:39 +010082 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void mbedtls_test_psa_purge_key_cache(void);
Gilles Peskineaae718c2021-02-14 13:46:39 +010084
Gilles Peskinee09ef872021-02-14 13:10:38 +010085/** \def TEST_USES_KEY_ID
86 *
87 * Call this macro in a test function before potentially creating a
88 * persistent key. Test functions that use this mechanism must call
89 * mbedtls_test_psa_purge_key_storage() in their cleanup code.
90 *
91 * This macro records a persistent key identifier as potentially used in the
92 * current test case. Recorded key identifiers will be cleaned up at the end
93 * of the test case, even on failure.
94 *
95 * This macro has no effect on volatile keys. Therefore, it is safe to call
96 * this macro in a test function that creates either volatile or persistent
97 * keys depending on the test data.
98 *
99 * This macro currently has no effect on special identifiers
100 * used to store implementation-specific files.
101 *
102 * Calling this macro multiple times on the same key identifier in the same
103 * test case has no effect.
104 *
105 * This macro can fail the test case if there isn't enough memory to
106 * record the key id.
107 *
108 * \param key_id The PSA key identifier to record.
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110#define TEST_USES_KEY_ID(key_id) \
111 TEST_ASSERT(mbedtls_test_uses_key_id(key_id))
Gilles Peskinee09ef872021-02-14 13:10:38 +0100112
Gilles Peskine313ffb82021-02-14 12:51:14 +0100113#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskinee09ef872021-02-14 13:10:38 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115#define TEST_USES_KEY_ID(key_id) ((void) (key_id))
116#define mbedtls_test_psa_purge_key_storage() ((void) 0)
117#define mbedtls_test_psa_purge_key_cache() ((void) 0)
Gilles Peskinee09ef872021-02-14 13:10:38 +0100118
Gilles Peskine313ffb82021-02-14 12:51:14 +0100119#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
120
Gilles Peskine1e005652020-11-24 17:41:07 +0100121/** Check for things that have not been cleaned up properly in the
122 * PSA subsystem.
123 *
124 * \return NULL if nothing has leaked.
125 * \return A string literal explaining what has not been cleaned up
126 * if applicable.
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128const char *mbedtls_test_helper_is_psa_leaking(void);
Gilles Peskinedd413d32019-05-28 15:06:43 +0200129
Gilles Peskine3cff7682019-06-20 12:54:43 +0200130/** Check that no PSA Crypto key slots are in use.
Gilles Peskinec85c2012021-01-06 20:47:16 +0100131 *
132 * If any slots are in use, mark the current test as failed and jump to
133 * the exit label. This is equivalent to
134 * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )`
135 * but with a more informative message.
Gilles Peskinedd413d32019-05-28 15:06:43 +0200136 */
Yanray Wange64b4052022-10-28 18:12:01 +0800137#define ASSERT_PSA_PRISTINE() \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100138 do \
139 { \
Yanray Wange64b4052022-10-28 18:12:01 +0800140 if (mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__)) \
141 goto exit; \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100142 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 while (0)
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200144
Gilles Peskine65048ad2021-02-14 14:08:22 +0100145/** Shut down the PSA Crypto subsystem, allowing persistent keys to survive.
146 * Expect a clean shutdown, with no slots in use.
147 *
148 * If some key slots are still in use, record the test case as failed and
149 * jump to the `exit` label.
150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151#define PSA_SESSION_DONE() \
Gilles Peskine65048ad2021-02-14 14:08:22 +0100152 do \
153 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_test_psa_purge_key_cache(); \
155 ASSERT_PSA_PRISTINE(); \
156 mbedtls_psa_crypto_free(); \
Gilles Peskinec85c2012021-01-06 20:47:16 +0100157 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 while (0)
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200159
Gilles Peskine51681552019-05-20 19:35:37 +0200160
161
162#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100163psa_status_t mbedtls_test_record_status(psa_status_t status,
164 const char *func,
165 const char *file, int line,
166 const char *expr);
Gilles Peskine51681552019-05-20 19:35:37 +0200167
168/** Return value logging wrapper macro.
169 *
170 * Evaluate \p expr. Write a line recording its value to the log file
171 * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
172 * list of fields:
173 * ```
174 * value of expr:string:__FILE__:__LINE__:expr
175 * ```
176 *
177 * The test code does not call this macro explicitly because that would
178 * be very invasive. Instead, we instrument the source code by defining
179 * a bunch of wrapper macros like
180 * ```
181 * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
182 * ```
183 * These macro definitions must be present in `instrument_record_status.h`
184 * when building the test suites.
185 *
186 * \param string A string, normally a function name.
187 * \param expr An expression to evaluate, normally a call of the function
188 * whose name is in \p string. This expression must return
189 * a value of type #psa_status_t.
190 * \return The value of \p expr.
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192#define RECORD_STATUS(string, expr) \
193 mbedtls_test_record_status((expr), string, __FILE__, __LINE__, #expr)
Gilles Peskine51681552019-05-20 19:35:37 +0200194
195#include "instrument_record_status.h"
196
197#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
198
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200199/** Return extended key usage policies.
200 *
201 * Do a key policy permission extension on key usage policies always involves
202 * permissions of other usage policies
Tom Cosgrove1797b052022-12-04 17:19:59 +0000203 * (like PSA_KEY_USAGE_SIGN_HASH involves PSA_KEY_USAGE_SIGN_MESSAGE).
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200204 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags);
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200206
Yanray Wange64b4052022-10-28 18:12:01 +0800207/** Check that no PSA Crypto key slots are in use.
208 *
209 * If any slots are in use, mark the current test as failed.
210 *
211 * \return 0 if the key store is empty, 1 otherwise.
212 */
213int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename);
214
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100215/** Skip a test case if the given key is a 192 bits AES key and the AES
216 * implementation is at least partially provided by an accelerator or
Ronald Cron28a45ed2021-02-09 20:35:42 +0100217 * alternative implementation.
218 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100219 * Call this macro in a test case when a cryptographic operation that may
220 * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code.
221 * The macro call will skip and not fail the test case in case the operation
222 * involves a 192 bits AES key and the AES implementation is at least
223 * partially provided by an accelerator or alternative implementation.
224 *
225 * Hardware AES implementations not supporting 192 bits keys commonly exist.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100226 * Consequently, PSA test cases aim at not failing when an AES operation with
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100227 * a 192 bits key performed by an alternative AES implementation returns
228 * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro
229 * is to facilitate this and make the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100230 *
231 * \param key_type Key type
232 * \param key_bits Key length in number of bits.
233 */
234#if defined(MBEDTLS_AES_ALT) || \
235 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
236 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
237#define MBEDTLS_TEST_HAVE_ALT_AES 1
238#else
239#define MBEDTLS_TEST_HAVE_ALT_AES 0
240#endif
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_bits) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100243 do \
244 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if ((MBEDTLS_TEST_HAVE_ALT_AES) && \
246 ((key_type) == PSA_KEY_TYPE_AES) && \
247 (key_bits == 192)) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100248 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_test_skip("AES-192 not supported", __LINE__, __FILE__); \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100250 goto exit; \
251 } \
252 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 while (0)
Ronald Cron28a45ed2021-02-09 20:35:42 +0100254
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100255/** Skip a test case if a GCM operation with a nonce length different from
256 * 12 bytes fails and was performed by an accelerator or alternative
257 * implementation.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100258 *
259 * Call this macro in a test case when an AEAD cryptography operation that
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100260 * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error
261 * code. The macro call will skip and not fail the test case in case the
262 * operation involves the GCM mode, a nonce with a length different from
263 * 12 bytes and the GCM mode implementation is an alternative one.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100264 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100265 * Hardware GCM implementations not supporting nonce lengths different from
266 * 12 bytes commonly exist, as supporting a non-12-byte nonce requires
267 * additional computations involving the GHASH function.
268 * Consequently, PSA test cases aim at not failing when an AEAD operation in
269 * GCM mode with a nonce length different from 12 bytes is performed by an
270 * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED
271 * error code. The purpose of this macro is to facilitate this check and make
272 * the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100273 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100274 * \param alg The AEAD algorithm.
275 * \param nonce_length The nonce length in number of bytes.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100276 */
Ronald Cron28a45ed2021-02-09 20:35:42 +0100277#if defined(MBEDTLS_GCM_ALT) || \
278 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
279#define MBEDTLS_TEST_HAVE_ALT_GCM 1
280#else
281#define MBEDTLS_TEST_HAVE_ALT_GCM 0
282#endif
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \
285 nonce_length) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100286 do \
287 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if ((MBEDTLS_TEST_HAVE_ALT_GCM) && \
289 (PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \
290 PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \
291 ((nonce_length) != 12)) \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100292 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_test_skip("GCM with non-12-byte IV is not supported", __LINE__, __FILE__); \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100294 goto exit; \
295 } \
296 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 while (0)
Ronald Cron28a45ed2021-02-09 20:35:42 +0100298
Gilles Peskine9de97e22021-02-02 21:00:11 +0100299#endif /* MBEDTLS_PSA_CRYPTO_C */
300
301/** \def USE_PSA_INIT
302 *
303 * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
Ronald Cron3cec8e82022-03-27 14:34:09 +0200304 * or #MBEDTLS_SSL_PROTO_TLS1_3 (In contrast to TLS 1.2 implementation, the
305 * TLS 1.3 one uses PSA independently of the definition of
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100306 * #MBEDTLS_USE_PSA_CRYPTO) is enabled and do nothing otherwise.
307 *
308 * If the initialization fails, mark the test case as failed and jump to the
309 * \p exit label.
Gilles Peskine9de97e22021-02-02 21:00:11 +0100310 */
311/** \def USE_PSA_DONE
312 *
313 * Call this macro at the end of a test case if you called #USE_PSA_INIT.
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100314 *
315 * This is like #PSA_DONE except it does nothing under the same conditions as
316 * #USE_PSA_INIT.
Gilles Peskine9de97e22021-02-02 21:00:11 +0100317 */
Ronald Cron3cec8e82022-03-27 14:34:09 +0200318#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100319#define USE_PSA_INIT() PSA_INIT()
320#define USE_PSA_DONE() PSA_DONE()
Ronald Cron3cec8e82022-03-27 14:34:09 +0200321#else /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine9de97e22021-02-02 21:00:11 +0100322/* Define empty macros so that we can use them in the preamble and teardown
323 * of every test function that uses PSA conditionally based on
324 * MBEDTLS_USE_PSA_CRYPTO. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325#define USE_PSA_INIT() ((void) 0)
326#define USE_PSA_DONE() ((void) 0)
Ronald Cron3cec8e82022-03-27 14:34:09 +0200327#endif /* !MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine9de97e22021-02-02 21:00:11 +0100328
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100329/** \def MD_PSA_INIT
330 *
331 * Call this macro to initialize the PSA subsystem if MD uses a driver,
332 * and do nothing otherwise.
333 *
334 * If the initialization fails, mark the test case as failed and jump to the
335 * \p exit label.
336 */
337/** \def MD_PSA_DONE
338 *
339 * Call this macro at the end of a test case if you called #MD_PSA_INIT.
340 *
341 * This is like #PSA_DONE except it does nothing under the same conditions as
342 * #MD_PSA_INIT.
343 */
344#if defined(MBEDTLS_MD_SOME_PSA)
345#define MD_PSA_INIT() PSA_INIT()
346#define MD_PSA_DONE() PSA_DONE()
347#else /* MBEDTLS_MD_SOME_PSA */
348#define MD_PSA_INIT() ((void) 0)
349#define MD_PSA_DONE() ((void) 0)
350#endif /* MBEDTLS_MD_SOME_PSA */
351
352/** \def MD_OR_USE_PSA_INIT
353 *
354 * Call this macro to initialize the PSA subsystem if MD uses a driver,
355 * of if #MBEDTLS_USE_PSA_CRYPTO or #MBEDTLS_SSL_PROTO_TLS1_3 is enabled,
356 * and do nothing otherwise.
357 *
358 * If the initialization fails, mark the test case as failed and jump to the
359 * \p exit label.
360 */
361/** \def MD_OR_USE_PSA_DONE
362 *
363 * Call this macro at the end of a test case if you called #MD_OR_USE_PSA_INIT.
364 *
365 * This is like #PSA_DONE except it does nothing under the same conditions as
366 * #MD_OR_USE_PSA_INIT.
367 */
368#if defined(MBEDTLS_MD_SOME_PSA) || \
369 defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
370#define MD_OR_USE_PSA_INIT() PSA_INIT()
371#define MD_OR_USE_PSA_DONE() PSA_DONE()
372#else
373#define MD_OR_USE_PSA_INIT() ((void) 0)
374#define MD_OR_USE_PSA_DONE() ((void) 0)
375#endif
376
Gilles Peskine1838e822019-06-20 12:40:56 +0200377#endif /* PSA_CRYPTO_HELPERS_H */