Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 1 | /* |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 2 | * Helper functions for tests that use the PSA Crypto API. |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 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. |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 21 | #ifndef PSA_CRYPTO_HELPERS_H |
| 22 | #define PSA_CRYPTO_HELPERS_H |
| 23 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 24 | #include "test/helpers.h" |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 27 | |
Ronald Cron | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 28 | #include "test/psa_helpers.h" |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 29 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 30 | #include <psa/crypto.h> |
Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 31 | #include <psa_crypto_slot_management.h> |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 32 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 33 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 34 | #include "mbedtls/psa_util.h" |
| 35 | #endif |
| 36 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame^] | 37 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 38 | /* All test functions that create persistent keys must call |
| 39 | * `TEST_USES_KEY_ID( key_id )` before creating a persistent key with this |
| 40 | * identifier, and must call psa_purge_key_storage() in their cleanup |
| 41 | * code. */ |
| 42 | int mbedtls_test_uses_key_id( mbedtls_svc_key_id_t key_id ); |
| 43 | void mbedtls_test_psa_purge_key_storage( void ); |
| 44 | #define TEST_USES_KEY_ID( key_id ) \ |
| 45 | TEST_ASSERT( mbedtls_test_uses_key_id( key_id ) ) |
| 46 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 47 | #define TEST_USES_KEY_ID( key_id ) ( (void) ( key_id ) ) |
| 48 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 49 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 50 | #define PSA_INIT( ) PSA_ASSERT( psa_crypto_init( ) ) |
| 51 | |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 52 | /** Check for things that have not been cleaned up properly in the |
| 53 | * PSA subsystem. |
| 54 | * |
| 55 | * \return NULL if nothing has leaked. |
| 56 | * \return A string literal explaining what has not been cleaned up |
| 57 | * if applicable. |
| 58 | */ |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 59 | const char *mbedtls_test_helper_is_psa_leaking( void ); |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 60 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 61 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 62 | * |
| 63 | * If any slots are in use, mark the current test as failed and jump to |
| 64 | * the exit label. This is equivalent to |
| 65 | * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )` |
| 66 | * but with a more informative message. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 67 | */ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 68 | #define ASSERT_PSA_PRISTINE( ) \ |
| 69 | do \ |
| 70 | { \ |
| 71 | if( test_fail_if_psa_leaking( __LINE__, __FILE__ ) ) \ |
| 72 | goto exit; \ |
| 73 | } \ |
| 74 | while( 0 ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 76 | /** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 77 | * in use. |
| 78 | */ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 79 | #define PSA_DONE( ) \ |
| 80 | do \ |
| 81 | { \ |
| 82 | test_fail_if_psa_leaking( __LINE__, __FILE__ ); \ |
| 83 | mbedtls_psa_crypto_free( ); \ |
| 84 | } \ |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 85 | while( 0 ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 86 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 87 | |
| 88 | |
| 89 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 90 | psa_status_t mbedtls_test_record_status( psa_status_t status, |
| 91 | const char *func, |
| 92 | const char *file, int line, |
| 93 | const char *expr ); |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 94 | |
| 95 | /** Return value logging wrapper macro. |
| 96 | * |
| 97 | * Evaluate \p expr. Write a line recording its value to the log file |
| 98 | * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated |
| 99 | * list of fields: |
| 100 | * ``` |
| 101 | * value of expr:string:__FILE__:__LINE__:expr |
| 102 | * ``` |
| 103 | * |
| 104 | * The test code does not call this macro explicitly because that would |
| 105 | * be very invasive. Instead, we instrument the source code by defining |
| 106 | * a bunch of wrapper macros like |
| 107 | * ``` |
| 108 | * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init()) |
| 109 | * ``` |
| 110 | * These macro definitions must be present in `instrument_record_status.h` |
| 111 | * when building the test suites. |
| 112 | * |
| 113 | * \param string A string, normally a function name. |
| 114 | * \param expr An expression to evaluate, normally a call of the function |
| 115 | * whose name is in \p string. This expression must return |
| 116 | * a value of type #psa_status_t. |
| 117 | * \return The value of \p expr. |
| 118 | */ |
| 119 | #define RECORD_STATUS( string, expr ) \ |
Gilles Peskine | ddfd080 | 2020-11-24 17:07:05 +0100 | [diff] [blame] | 120 | mbedtls_test_record_status( ( expr ), string, __FILE__, __LINE__, #expr ) |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 121 | |
| 122 | #include "instrument_record_status.h" |
| 123 | |
| 124 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 125 | |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 126 | /** Skip a test case if the given key is a 192 bits AES key and the AES |
| 127 | * implementation is at least partially provided by an accelerator or |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 128 | * alternative implementation. |
| 129 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 130 | * Call this macro in a test case when a cryptographic operation that may |
| 131 | * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code. |
| 132 | * The macro call will skip and not fail the test case in case the operation |
| 133 | * involves a 192 bits AES key and the AES implementation is at least |
| 134 | * partially provided by an accelerator or alternative implementation. |
| 135 | * |
| 136 | * Hardware AES implementations not supporting 192 bits keys commonly exist. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 137 | * Consequently, PSA test cases aim at not failing when an AES operation with |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 138 | * a 192 bits key performed by an alternative AES implementation returns |
| 139 | * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro |
| 140 | * is to facilitate this and make the test case code more readable. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 141 | * |
| 142 | * \param key_type Key type |
| 143 | * \param key_bits Key length in number of bits. |
| 144 | */ |
| 145 | #if defined(MBEDTLS_AES_ALT) || \ |
| 146 | defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \ |
| 147 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES) |
| 148 | #define MBEDTLS_TEST_HAVE_ALT_AES 1 |
| 149 | #else |
| 150 | #define MBEDTLS_TEST_HAVE_ALT_AES 0 |
| 151 | #endif |
| 152 | |
| 153 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_bits ) \ |
| 154 | do \ |
| 155 | { \ |
| 156 | if( ( MBEDTLS_TEST_HAVE_ALT_AES ) && \ |
| 157 | ( ( key_type ) == PSA_KEY_TYPE_AES ) && \ |
| 158 | ( key_bits == 192 ) ) \ |
| 159 | { \ |
| 160 | mbedtls_test_skip( "AES-192 not supported", __LINE__, __FILE__ ); \ |
| 161 | goto exit; \ |
| 162 | } \ |
| 163 | } \ |
| 164 | while( 0 ) |
| 165 | |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 166 | /** Skip a test case if a GCM operation with a nonce length different from |
| 167 | * 12 bytes fails and was performed by an accelerator or alternative |
| 168 | * implementation. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 169 | * |
| 170 | * Call this macro in a test case when an AEAD cryptography operation that |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 171 | * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error |
| 172 | * code. The macro call will skip and not fail the test case in case the |
| 173 | * operation involves the GCM mode, a nonce with a length different from |
| 174 | * 12 bytes and the GCM mode implementation is an alternative one. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 175 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 176 | * Hardware GCM implementations not supporting nonce lengths different from |
| 177 | * 12 bytes commonly exist, as supporting a non-12-byte nonce requires |
| 178 | * additional computations involving the GHASH function. |
| 179 | * Consequently, PSA test cases aim at not failing when an AEAD operation in |
| 180 | * GCM mode with a nonce length different from 12 bytes is performed by an |
| 181 | * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED |
| 182 | * error code. The purpose of this macro is to facilitate this check and make |
| 183 | * the test case code more readable. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 184 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 185 | * \param alg The AEAD algorithm. |
| 186 | * \param nonce_length The nonce length in number of bytes. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 187 | */ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_GCM_ALT) || \ |
| 189 | defined(MBEDTLS_PSA_ACCEL_ALG_GCM) |
| 190 | #define MBEDTLS_TEST_HAVE_ALT_GCM 1 |
| 191 | #else |
| 192 | #define MBEDTLS_TEST_HAVE_ALT_GCM 0 |
| 193 | #endif |
| 194 | |
| 195 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, \ |
| 196 | nonce_length ) \ |
| 197 | do \ |
| 198 | { \ |
| 199 | if( ( MBEDTLS_TEST_HAVE_ALT_GCM ) && \ |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 200 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( ( alg ) , 0 ) == \ |
| 201 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) ) && \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 202 | ( ( nonce_length ) != 12 ) ) \ |
| 203 | { \ |
| 204 | mbedtls_test_skip( "GCM with non-12-byte IV is not supported", __LINE__, __FILE__ ); \ |
| 205 | goto exit; \ |
| 206 | } \ |
| 207 | } \ |
| 208 | while( 0 ) |
| 209 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 210 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 211 | |
| 212 | /** \def USE_PSA_INIT |
| 213 | * |
| 214 | * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO |
| 215 | * is enabled and do nothing otherwise. If the initialization fails, mark |
| 216 | * the test case as failed and jump to the \p exit label. |
| 217 | */ |
| 218 | /** \def USE_PSA_DONE |
| 219 | * |
| 220 | * Call this macro at the end of a test case if you called #USE_PSA_INIT. |
| 221 | * This is like #PSA_DONE, except that it does nothing if |
| 222 | * #MBEDTLS_USE_PSA_CRYPTO is disabled. |
| 223 | */ |
| 224 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 225 | #define USE_PSA_INIT( ) PSA_INIT( ) |
| 226 | #define USE_PSA_DONE( ) PSA_DONE( ) |
| 227 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 228 | /* Define empty macros so that we can use them in the preamble and teardown |
| 229 | * of every test function that uses PSA conditionally based on |
| 230 | * MBEDTLS_USE_PSA_CRYPTO. */ |
| 231 | #define USE_PSA_INIT( ) ( (void) 0 ) |
| 232 | #define USE_PSA_DONE( ) ( (void) 0 ) |
| 233 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
| 234 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 235 | #endif /* PSA_CRYPTO_HELPERS_H */ |