blob: 3e356f9a8d64802d4259dfec77fe14273af68253 [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)
27
Ronald Cron02c78b72020-05-27 09:22:32 +020028#include "test/psa_helpers.h"
Gilles Peskine952f4092019-05-23 20:25:48 +020029
Gilles Peskine3cff7682019-06-20 12:54:43 +020030#include <psa/crypto.h>
Ronald Cron0c3752a2020-10-30 11:54:03 +010031#include <psa_crypto_slot_management.h>
Gilles Peskine952f4092019-05-23 20:25:48 +020032
Gilles Peskine9de97e22021-02-02 21:00:11 +010033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#endif
36
Gilles Peskine313ffb82021-02-14 12:51:14 +010037#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinee09ef872021-02-14 13:10:38 +010038
39/* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */
Gilles Peskine313ffb82021-02-14 12:51:14 +010040int mbedtls_test_uses_key_id( mbedtls_svc_key_id_t key_id );
Gilles Peskinee09ef872021-02-14 13:10:38 +010041
42/** Destroy persistent keys recorded with #TEST_USES_KEY_ID.
43 */
Gilles Peskine313ffb82021-02-14 12:51:14 +010044void mbedtls_test_psa_purge_key_storage( void );
Gilles Peskinee09ef872021-02-14 13:10:38 +010045
Gilles Peskineaae718c2021-02-14 13:46:39 +010046/** Purge the in-memory cache of persistent keys recorded with
47 * #TEST_USES_KEY_ID.
48 */
49void mbedtls_test_psa_purge_key_cache( void );
50
Gilles Peskinee09ef872021-02-14 13:10:38 +010051/** \def TEST_USES_KEY_ID
52 *
53 * Call this macro in a test function before potentially creating a
54 * persistent key. Test functions that use this mechanism must call
55 * mbedtls_test_psa_purge_key_storage() in their cleanup code.
56 *
57 * This macro records a persistent key identifier as potentially used in the
58 * current test case. Recorded key identifiers will be cleaned up at the end
59 * of the test case, even on failure.
60 *
61 * This macro has no effect on volatile keys. Therefore, it is safe to call
62 * this macro in a test function that creates either volatile or persistent
63 * keys depending on the test data.
64 *
65 * This macro currently has no effect on special identifiers
66 * used to store implementation-specific files.
67 *
68 * Calling this macro multiple times on the same key identifier in the same
69 * test case has no effect.
70 *
71 * This macro can fail the test case if there isn't enough memory to
72 * record the key id.
73 *
74 * \param key_id The PSA key identifier to record.
75 */
76#define TEST_USES_KEY_ID( key_id ) \
Gilles Peskine313ffb82021-02-14 12:51:14 +010077 TEST_ASSERT( mbedtls_test_uses_key_id( key_id ) )
Gilles Peskinee09ef872021-02-14 13:10:38 +010078
Gilles Peskine313ffb82021-02-14 12:51:14 +010079#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskinee09ef872021-02-14 13:10:38 +010080
Gilles Peskine313ffb82021-02-14 12:51:14 +010081#define TEST_USES_KEY_ID( key_id ) ( (void) ( key_id ) )
Gilles Peskinee09ef872021-02-14 13:10:38 +010082
Gilles Peskine313ffb82021-02-14 12:51:14 +010083#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
84
Gilles Peskine9de97e22021-02-02 21:00:11 +010085#define PSA_INIT( ) PSA_ASSERT( psa_crypto_init( ) )
86
Gilles Peskine1e005652020-11-24 17:41:07 +010087/** Check for things that have not been cleaned up properly in the
88 * PSA subsystem.
89 *
90 * \return NULL if nothing has leaked.
91 * \return A string literal explaining what has not been cleaned up
92 * if applicable.
93 */
Gilles Peskined4008d52020-11-24 17:34:30 +010094const char *mbedtls_test_helper_is_psa_leaking( void );
Gilles Peskinedd413d32019-05-28 15:06:43 +020095
Gilles Peskine3cff7682019-06-20 12:54:43 +020096/** Check that no PSA Crypto key slots are in use.
Gilles Peskinec85c2012021-01-06 20:47:16 +010097 *
98 * If any slots are in use, mark the current test as failed and jump to
99 * the exit label. This is equivalent to
100 * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )`
101 * but with a more informative message.
Gilles Peskinedd413d32019-05-28 15:06:43 +0200102 */
Gilles Peskinec85c2012021-01-06 20:47:16 +0100103#define ASSERT_PSA_PRISTINE( ) \
104 do \
105 { \
106 if( test_fail_if_psa_leaking( __LINE__, __FILE__ ) ) \
107 goto exit; \
108 } \
109 while( 0 )
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200110
Gilles Peskine3cff7682019-06-20 12:54:43 +0200111/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200112 * in use.
113 */
Gilles Peskinec85c2012021-01-06 20:47:16 +0100114#define PSA_DONE( ) \
115 do \
116 { \
117 test_fail_if_psa_leaking( __LINE__, __FILE__ ); \
118 mbedtls_psa_crypto_free( ); \
119 } \
Gilles Peskine1e005652020-11-24 17:41:07 +0100120 while( 0 )
Gilles Peskinea6d252a2019-05-23 20:34:30 +0200121
Gilles Peskine51681552019-05-20 19:35:37 +0200122
123
124#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
Gilles Peskined4008d52020-11-24 17:34:30 +0100125psa_status_t mbedtls_test_record_status( psa_status_t status,
126 const char *func,
127 const char *file, int line,
128 const char *expr );
Gilles Peskine51681552019-05-20 19:35:37 +0200129
130/** Return value logging wrapper macro.
131 *
132 * Evaluate \p expr. Write a line recording its value to the log file
133 * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
134 * list of fields:
135 * ```
136 * value of expr:string:__FILE__:__LINE__:expr
137 * ```
138 *
139 * The test code does not call this macro explicitly because that would
140 * be very invasive. Instead, we instrument the source code by defining
141 * a bunch of wrapper macros like
142 * ```
143 * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
144 * ```
145 * These macro definitions must be present in `instrument_record_status.h`
146 * when building the test suites.
147 *
148 * \param string A string, normally a function name.
149 * \param expr An expression to evaluate, normally a call of the function
150 * whose name is in \p string. This expression must return
151 * a value of type #psa_status_t.
152 * \return The value of \p expr.
153 */
154#define RECORD_STATUS( string, expr ) \
Gilles Peskineddfd0802020-11-24 17:07:05 +0100155 mbedtls_test_record_status( ( expr ), string, __FILE__, __LINE__, #expr )
Gilles Peskine51681552019-05-20 19:35:37 +0200156
157#include "instrument_record_status.h"
158
159#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
160
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100161/** Skip a test case if the given key is a 192 bits AES key and the AES
162 * implementation is at least partially provided by an accelerator or
Ronald Cron28a45ed2021-02-09 20:35:42 +0100163 * alternative implementation.
164 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100165 * Call this macro in a test case when a cryptographic operation that may
166 * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code.
167 * The macro call will skip and not fail the test case in case the operation
168 * involves a 192 bits AES key and the AES implementation is at least
169 * partially provided by an accelerator or alternative implementation.
170 *
171 * Hardware AES implementations not supporting 192 bits keys commonly exist.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100172 * Consequently, PSA test cases aim at not failing when an AES operation with
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100173 * a 192 bits key performed by an alternative AES implementation returns
174 * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro
175 * is to facilitate this and make the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100176 *
177 * \param key_type Key type
178 * \param key_bits Key length in number of bits.
179 */
180#if defined(MBEDTLS_AES_ALT) || \
181 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
182 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
183#define MBEDTLS_TEST_HAVE_ALT_AES 1
184#else
185#define MBEDTLS_TEST_HAVE_ALT_AES 0
186#endif
187
188#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_bits ) \
189 do \
190 { \
191 if( ( MBEDTLS_TEST_HAVE_ALT_AES ) && \
192 ( ( key_type ) == PSA_KEY_TYPE_AES ) && \
193 ( key_bits == 192 ) ) \
194 { \
195 mbedtls_test_skip( "AES-192 not supported", __LINE__, __FILE__ ); \
196 goto exit; \
197 } \
198 } \
199 while( 0 )
200
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100201/** Skip a test case if a GCM operation with a nonce length different from
202 * 12 bytes fails and was performed by an accelerator or alternative
203 * implementation.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100204 *
205 * Call this macro in a test case when an AEAD cryptography operation that
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100206 * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error
207 * code. The macro call will skip and not fail the test case in case the
208 * operation involves the GCM mode, a nonce with a length different from
209 * 12 bytes and the GCM mode implementation is an alternative one.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100210 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100211 * Hardware GCM implementations not supporting nonce lengths different from
212 * 12 bytes commonly exist, as supporting a non-12-byte nonce requires
213 * additional computations involving the GHASH function.
214 * Consequently, PSA test cases aim at not failing when an AEAD operation in
215 * GCM mode with a nonce length different from 12 bytes is performed by an
216 * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED
217 * error code. The purpose of this macro is to facilitate this check and make
218 * the test case code more readable.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100219 *
Steven Cooreman1e9c0422021-02-10 17:02:05 +0100220 * \param alg The AEAD algorithm.
221 * \param nonce_length The nonce length in number of bytes.
Ronald Cron28a45ed2021-02-09 20:35:42 +0100222 */
Ronald Cron28a45ed2021-02-09 20:35:42 +0100223#if defined(MBEDTLS_GCM_ALT) || \
224 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
225#define MBEDTLS_TEST_HAVE_ALT_GCM 1
226#else
227#define MBEDTLS_TEST_HAVE_ALT_GCM 0
228#endif
229
230#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, \
231 nonce_length ) \
232 do \
233 { \
234 if( ( MBEDTLS_TEST_HAVE_ALT_GCM ) && \
Bence Szépkútia63b20d2020-12-16 11:36:46 +0100235 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( ( alg ) , 0 ) == \
236 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) ) && \
Ronald Cron28a45ed2021-02-09 20:35:42 +0100237 ( ( nonce_length ) != 12 ) ) \
238 { \
239 mbedtls_test_skip( "GCM with non-12-byte IV is not supported", __LINE__, __FILE__ ); \
240 goto exit; \
241 } \
242 } \
243 while( 0 )
244
Gilles Peskine9de97e22021-02-02 21:00:11 +0100245#endif /* MBEDTLS_PSA_CRYPTO_C */
246
247/** \def USE_PSA_INIT
248 *
249 * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
250 * is enabled and do nothing otherwise. If the initialization fails, mark
251 * the test case as failed and jump to the \p exit label.
252 */
253/** \def USE_PSA_DONE
254 *
255 * Call this macro at the end of a test case if you called #USE_PSA_INIT.
256 * This is like #PSA_DONE, except that it does nothing if
257 * #MBEDTLS_USE_PSA_CRYPTO is disabled.
258 */
259#if defined(MBEDTLS_USE_PSA_CRYPTO)
260#define USE_PSA_INIT( ) PSA_INIT( )
261#define USE_PSA_DONE( ) PSA_DONE( )
262#else /* MBEDTLS_USE_PSA_CRYPTO */
263/* Define empty macros so that we can use them in the preamble and teardown
264 * of every test function that uses PSA conditionally based on
265 * MBEDTLS_USE_PSA_CRYPTO. */
266#define USE_PSA_INIT( ) ( (void) 0 )
267#define USE_PSA_DONE( ) ( (void) 0 )
268#endif /* !MBEDTLS_USE_PSA_CRYPTO */
269
Gilles Peskine1838e822019-06-20 12:40:56 +0200270#endif /* PSA_CRYPTO_HELPERS_H */