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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 9 | #ifndef PSA_CRYPTO_HELPERS_H |
| 10 | #define PSA_CRYPTO_HELPERS_H |
| 11 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 12 | #include "test/helpers.h" |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 13 | |
David Horstmann | 34d79ce | 2024-09-27 15:13:32 +0100 | [diff] [blame] | 14 | #if ((MBEDTLS_VERSION_MAJOR < 4) \ |
| 15 | && defined(MBEDTLS_PSA_CRYPTO_C)) \ |
| 16 | || (MBEDTLS_VERSION_MAJOR >= 4 \ |
| 17 | && defined(MBEDTLS_PSA_CRYPTO_CLIENT)) |
Ronald Cron | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 18 | #include "test/psa_helpers.h" |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 19 | #include <psa/crypto.h> |
Manuel Pégourié-Gonnard | 9f132b7 | 2023-03-14 10:26:46 +0100 | [diff] [blame] | 20 | #endif |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 21 | |
Gilles Peskine | bdd16d4 | 2024-06-20 21:47:31 +0200 | [diff] [blame] | 22 | #include <mbedtls/ctr_drbg.h> |
| 23 | |
Manuel Pégourié-Gonnard | 9f132b7 | 2023-03-14 10:26:46 +0100 | [diff] [blame] | 24 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 25 | /** Initialize the PSA Crypto subsystem. */ |
| 26 | #define PSA_INIT() PSA_ASSERT(psa_crypto_init()) |
| 27 | |
| 28 | /** Shut down the PSA Crypto subsystem and destroy persistent keys. |
| 29 | * Expect a clean shutdown, with no slots in use. |
| 30 | * |
| 31 | * If some key slots are still in use, record the test case as failed, |
| 32 | * but continue executing. This macro is suitable (and primarily intended) |
| 33 | * for use in the cleanup section of test functions. |
| 34 | * |
| 35 | * \note Persistent keys must be recorded with #TEST_USES_KEY_ID before |
| 36 | * creating them. |
| 37 | */ |
| 38 | #define PSA_DONE() \ |
| 39 | do \ |
| 40 | { \ |
| 41 | mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__); \ |
| 42 | mbedtls_test_psa_purge_key_storage(); \ |
| 43 | mbedtls_psa_crypto_free(); \ |
| 44 | } \ |
| 45 | while (0) |
David Horstmann | 34d79ce | 2024-09-27 15:13:32 +0100 | [diff] [blame] | 46 | #elif MBEDTLS_VERSION_MAJOR >= 4 && \ |
| 47 | defined(MBEDTLS_PSA_CRYPTO_CLIENT) /* MBEDTLS_PSA_CRYPTO_CLIENT && !MBEDTLS_PSA_CRYPTO_C */ |
Valerio Setti | 8473390 | 2024-06-27 08:05:09 +0200 | [diff] [blame] | 48 | #define PSA_INIT() PSA_ASSERT(psa_crypto_init()) |
| 49 | #define PSA_DONE() mbedtls_psa_crypto_free(); |
| 50 | #else /* MBEDTLS_PSA_CRYPTO_CLIENT && !MBEDTLS_PSA_CRYPTO_C */ |
Manuel Pégourié-Gonnard | 9f132b7 | 2023-03-14 10:26:46 +0100 | [diff] [blame] | 51 | #define PSA_INIT() ((void) 0) |
| 52 | #define PSA_DONE() ((void) 0) |
| 53 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 54 | |
David Horstmann | 34d79ce | 2024-09-27 15:13:32 +0100 | [diff] [blame] | 55 | #if ((MBEDTLS_VERSION_MAJOR < 4) \ |
| 56 | && defined(MBEDTLS_PSA_CRYPTO_C)) \ |
| 57 | || (MBEDTLS_VERSION_MAJOR >= 4 \ |
| 58 | && defined(MBEDTLS_PSA_CRYPTO_CLIENT)) |
Manuel Pégourié-Gonnard | 9f132b7 | 2023-03-14 10:26:46 +0100 | [diff] [blame] | 59 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 60 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 61 | |
| 62 | /* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id); |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 64 | |
| 65 | /** Destroy persistent keys recorded with #TEST_USES_KEY_ID. |
| 66 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | void mbedtls_test_psa_purge_key_storage(void); |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 68 | |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 69 | /** Purge the in-memory cache of persistent keys recorded with |
| 70 | * #TEST_USES_KEY_ID. |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 71 | * |
| 72 | * Call this function before calling PSA_DONE() if it's ok for |
| 73 | * persistent keys to still exist at this point. |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 74 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | void mbedtls_test_psa_purge_key_cache(void); |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 77 | /** \def TEST_USES_KEY_ID |
| 78 | * |
| 79 | * Call this macro in a test function before potentially creating a |
| 80 | * persistent key. Test functions that use this mechanism must call |
| 81 | * mbedtls_test_psa_purge_key_storage() in their cleanup code. |
| 82 | * |
| 83 | * This macro records a persistent key identifier as potentially used in the |
| 84 | * current test case. Recorded key identifiers will be cleaned up at the end |
| 85 | * of the test case, even on failure. |
| 86 | * |
| 87 | * This macro has no effect on volatile keys. Therefore, it is safe to call |
| 88 | * this macro in a test function that creates either volatile or persistent |
| 89 | * keys depending on the test data. |
| 90 | * |
| 91 | * This macro currently has no effect on special identifiers |
| 92 | * used to store implementation-specific files. |
| 93 | * |
| 94 | * Calling this macro multiple times on the same key identifier in the same |
| 95 | * test case has no effect. |
| 96 | * |
| 97 | * This macro can fail the test case if there isn't enough memory to |
| 98 | * record the key id. |
| 99 | * |
| 100 | * \param key_id The PSA key identifier to record. |
| 101 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | #define TEST_USES_KEY_ID(key_id) \ |
| 103 | TEST_ASSERT(mbedtls_test_uses_key_id(key_id)) |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 104 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 105 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | #define TEST_USES_KEY_ID(key_id) ((void) (key_id)) |
| 108 | #define mbedtls_test_psa_purge_key_storage() ((void) 0) |
| 109 | #define mbedtls_test_psa_purge_key_cache() ((void) 0) |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 110 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 111 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 112 | |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 113 | /** Check for things that have not been cleaned up properly in the |
| 114 | * PSA subsystem. |
| 115 | * |
| 116 | * \return NULL if nothing has leaked. |
| 117 | * \return A string literal explaining what has not been cleaned up |
| 118 | * if applicable. |
| 119 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | const char *mbedtls_test_helper_is_psa_leaking(void); |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 121 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 122 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 123 | * |
| 124 | * If any slots are in use, mark the current test as failed and jump to |
| 125 | * the exit label. This is equivalent to |
| 126 | * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )` |
| 127 | * but with a more informative message. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 128 | */ |
Yanray Wang | e64b405 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 129 | #define ASSERT_PSA_PRISTINE() \ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 130 | do \ |
| 131 | { \ |
Yanray Wang | e64b405 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 132 | if (mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__)) \ |
| 133 | goto exit; \ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 134 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | while (0) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 136 | |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 137 | /** Shut down the PSA Crypto subsystem, allowing persistent keys to survive. |
| 138 | * Expect a clean shutdown, with no slots in use. |
| 139 | * |
| 140 | * If some key slots are still in use, record the test case as failed and |
| 141 | * jump to the `exit` label. |
| 142 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | #define PSA_SESSION_DONE() \ |
Valerio Setti | 7448367 | 2023-11-24 08:36:12 +0100 | [diff] [blame] | 144 | do \ |
| 145 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | mbedtls_test_psa_purge_key_cache(); \ |
| 147 | ASSERT_PSA_PRISTINE(); \ |
| 148 | mbedtls_psa_crypto_free(); \ |
Valerio Setti | 7448367 | 2023-11-24 08:36:12 +0100 | [diff] [blame] | 149 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | while (0) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 151 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 152 | |
| 153 | |
| 154 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | psa_status_t mbedtls_test_record_status(psa_status_t status, |
| 156 | const char *func, |
| 157 | const char *file, int line, |
| 158 | const char *expr); |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 159 | |
| 160 | /** Return value logging wrapper macro. |
| 161 | * |
| 162 | * Evaluate \p expr. Write a line recording its value to the log file |
| 163 | * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated |
| 164 | * list of fields: |
| 165 | * ``` |
| 166 | * value of expr:string:__FILE__:__LINE__:expr |
| 167 | * ``` |
| 168 | * |
| 169 | * The test code does not call this macro explicitly because that would |
| 170 | * be very invasive. Instead, we instrument the source code by defining |
| 171 | * a bunch of wrapper macros like |
| 172 | * ``` |
| 173 | * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init()) |
| 174 | * ``` |
| 175 | * These macro definitions must be present in `instrument_record_status.h` |
| 176 | * when building the test suites. |
| 177 | * |
| 178 | * \param string A string, normally a function name. |
| 179 | * \param expr An expression to evaluate, normally a call of the function |
| 180 | * whose name is in \p string. This expression must return |
| 181 | * a value of type #psa_status_t. |
| 182 | * \return The value of \p expr. |
| 183 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | #define RECORD_STATUS(string, expr) \ |
| 185 | mbedtls_test_record_status((expr), string, __FILE__, __LINE__, #expr) |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 186 | |
| 187 | #include "instrument_record_status.h" |
| 188 | |
| 189 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 190 | |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 191 | /** Return extended key usage policies. |
| 192 | * |
| 193 | * Do a key policy permission extension on key usage policies always involves |
| 194 | * permissions of other usage policies |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 195 | * (like PSA_KEY_USAGE_SIGN_HASH involves PSA_KEY_USAGE_SIGN_MESSAGE). |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 196 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 198 | |
Yanray Wang | e64b405 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 199 | /** Check that no PSA Crypto key slots are in use. |
| 200 | * |
| 201 | * If any slots are in use, mark the current test as failed. |
| 202 | * |
| 203 | * \return 0 if the key store is empty, 1 otherwise. |
| 204 | */ |
| 205 | int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename); |
| 206 | |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 207 | |
| 208 | |
| 209 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 210 | /* The #MBEDTLS_PSA_INJECT_ENTROPY feature requires two extra platform |
| 211 | * functions, which must be configured as #MBEDTLS_PLATFORM_NV_SEED_READ_MACRO |
| 212 | * and #MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO. The job of these functions |
| 213 | * is to read and write from the entropy seed file, which is located |
| 214 | * in the PSA ITS file whose uid is #PSA_CRYPTO_ITS_RANDOM_SEED_UID. |
| 215 | * (These could have been provided as library functions, but for historical |
| 216 | * reasons, they weren't, and so each integrator has to provide a copy |
| 217 | * of these functions.) |
| 218 | * |
| 219 | * Provide implementations of these functions for testing. */ |
| 220 | int mbedtls_test_inject_entropy_seed_read(unsigned char *buf, size_t len); |
| 221 | int mbedtls_test_inject_entropy_seed_write(unsigned char *buf, size_t len); |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 222 | |
| 223 | |
| 224 | /** Make sure that the injected entropy is present. |
| 225 | * |
| 226 | * When MBEDTLS_PSA_INJECT_ENTROPY is enabled, psa_crypto_init() |
| 227 | * will fail if the PSA entropy seed is not present. |
| 228 | * This function must be called at least once in a test suite or other |
| 229 | * program before any call to psa_crypto_init(). |
| 230 | * It does not need to be called in each test case. |
| 231 | * |
| 232 | * The test framework calls this function before running any test case. |
| 233 | * |
| 234 | * The few tests that might remove the entropy file must call this function |
| 235 | * in their cleanup. |
| 236 | */ |
| 237 | int mbedtls_test_inject_entropy_restore(void); |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 238 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
| 239 | |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 240 | /** Parse binary string and convert it to a long integer |
| 241 | */ |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 242 | uint64_t mbedtls_test_parse_binary_string(data_t *bin_string); |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 243 | |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 244 | /** Skip a test case if the given key is a 192 bits AES key and the AES |
| 245 | * implementation is at least partially provided by an accelerator or |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 246 | * alternative implementation. |
| 247 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 248 | * Call this macro in a test case when a cryptographic operation that may |
| 249 | * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code. |
| 250 | * The macro call will skip and not fail the test case in case the operation |
| 251 | * involves a 192 bits AES key and the AES implementation is at least |
| 252 | * partially provided by an accelerator or alternative implementation. |
| 253 | * |
| 254 | * Hardware AES implementations not supporting 192 bits keys commonly exist. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 255 | * 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] | 256 | * a 192 bits key performed by an alternative AES implementation returns |
| 257 | * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro |
| 258 | * is to facilitate this and make the test case code more readable. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 259 | * |
| 260 | * \param key_type Key type |
| 261 | * \param key_bits Key length in number of bits. |
| 262 | */ |
David Horstmann | 0d8287c | 2024-07-26 18:01:04 +0100 | [diff] [blame] | 263 | #if defined(MBEDTLS_AES_ALT) || \ |
| 264 | defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \ |
| 265 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES) |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 266 | #define MBEDTLS_TEST_HAVE_ALT_AES 1 |
| 267 | #else |
| 268 | #define MBEDTLS_TEST_HAVE_ALT_AES 0 |
| 269 | #endif |
| 270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_bits) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 272 | do \ |
| 273 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | if ((MBEDTLS_TEST_HAVE_ALT_AES) && \ |
| 275 | ((key_type) == PSA_KEY_TYPE_AES) && \ |
| 276 | (key_bits == 192)) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 277 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | mbedtls_test_skip("AES-192 not supported", __LINE__, __FILE__); \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 279 | goto exit; \ |
| 280 | } \ |
| 281 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | while (0) |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 283 | |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 284 | /** Skip a test case if a GCM operation with a nonce length different from |
| 285 | * 12 bytes fails and was performed by an accelerator or alternative |
| 286 | * implementation. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 287 | * |
| 288 | * 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] | 289 | * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error |
| 290 | * code. The macro call will skip and not fail the test case in case the |
| 291 | * operation involves the GCM mode, a nonce with a length different from |
| 292 | * 12 bytes and the GCM mode implementation is an alternative one. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 293 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 294 | * Hardware GCM implementations not supporting nonce lengths different from |
| 295 | * 12 bytes commonly exist, as supporting a non-12-byte nonce requires |
| 296 | * additional computations involving the GHASH function. |
| 297 | * Consequently, PSA test cases aim at not failing when an AEAD operation in |
| 298 | * GCM mode with a nonce length different from 12 bytes is performed by an |
| 299 | * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED |
| 300 | * error code. The purpose of this macro is to facilitate this check and make |
| 301 | * the test case code more readable. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 302 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 303 | * \param alg The AEAD algorithm. |
| 304 | * \param nonce_length The nonce length in number of bytes. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 305 | */ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 306 | |
David Horstmann | 0d8287c | 2024-07-26 18:01:04 +0100 | [diff] [blame] | 307 | #if defined(MBEDTLS_GCM_ALT) || \ |
| 308 | defined(MBEDTLS_PSA_ACCEL_ALG_GCM) |
Thomas Daubney | 356309f | 2024-06-18 13:32:26 +0100 | [diff] [blame] | 309 | #define MBEDTLS_TEST_HAVE_ACCEL_GCM 1 |
| 310 | #else |
| 311 | #define MBEDTLS_TEST_HAVE_ACCEL_GCM 0 |
| 312 | #endif |
| 313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \ |
| 315 | nonce_length) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 316 | do \ |
| 317 | { \ |
Thomas Daubney | 356309f | 2024-06-18 13:32:26 +0100 | [diff] [blame] | 318 | if ((MBEDTLS_TEST_HAVE_ACCEL_GCM) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \ |
| 320 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \ |
| 321 | ((nonce_length) != 12)) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 322 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | mbedtls_test_skip("GCM with non-12-byte IV is not supported", __LINE__, __FILE__); \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 324 | goto exit; \ |
| 325 | } \ |
| 326 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | while (0) |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 328 | |
David Horstmann | 34d79ce | 2024-09-27 15:13:32 +0100 | [diff] [blame] | 329 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT || MBEDTLS_PSA_CRYPTO_C */ |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 330 | |
| 331 | /** \def USE_PSA_INIT |
| 332 | * |
| 333 | * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO |
Ronald Cron | 3cec8e8 | 2022-03-27 14:34:09 +0200 | [diff] [blame] | 334 | * or #MBEDTLS_SSL_PROTO_TLS1_3 (In contrast to TLS 1.2 implementation, the |
| 335 | * TLS 1.3 one uses PSA independently of the definition of |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 336 | * #MBEDTLS_USE_PSA_CRYPTO) is enabled and do nothing otherwise. |
| 337 | * |
| 338 | * If the initialization fails, mark the test case as failed and jump to the |
| 339 | * \p exit label. |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 340 | */ |
| 341 | /** \def USE_PSA_DONE |
| 342 | * |
| 343 | * Call this macro at the end of a test case if you called #USE_PSA_INIT. |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 344 | * |
| 345 | * This is like #PSA_DONE except it does nothing under the same conditions as |
| 346 | * #USE_PSA_INIT. |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 347 | */ |
David Horstmann | 19bc246 | 2024-10-18 20:00:27 +0100 | [diff] [blame^] | 348 | #if defined(MBEDTLS_USE_PSA_CRYPTO) \ |
| 349 | || (MBEDTLS_VERSION_MAJOR >= 4 && defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | #define USE_PSA_INIT() PSA_INIT() |
| 351 | #define USE_PSA_DONE() PSA_DONE() |
David Horstmann | 19bc246 | 2024-10-18 20:00:27 +0100 | [diff] [blame^] | 352 | #elif (MBEDTLS_VERSION_MAJOR < 4 && defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 353 | /* TLS 1.3 must work without having called psa_crypto_init(), for backward |
| 354 | * compatibility with Mbed TLS <= 3.5 when connecting with a peer that |
| 355 | * supports both TLS 1.2 and TLS 1.3. See mbedtls_ssl_tls13_crypto_init() |
| 356 | * and https://github.com/Mbed-TLS/mbedtls/issues/9072 . */ |
| 357 | #define USE_PSA_INIT() ((void) 0) |
| 358 | /* TLS 1.3 may have initialized the PSA subsystem. Shut it down cleanly, |
| 359 | * otherwise Asan and Valgrind would notice a resource leak. */ |
| 360 | #define USE_PSA_DONE() PSA_DONE() |
Ronald Cron | 3cec8e8 | 2022-03-27 14:34:09 +0200 | [diff] [blame] | 361 | #else /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 362 | /* Define empty macros so that we can use them in the preamble and teardown |
| 363 | * of every test function that uses PSA conditionally based on |
| 364 | * MBEDTLS_USE_PSA_CRYPTO. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | #define USE_PSA_INIT() ((void) 0) |
| 366 | #define USE_PSA_DONE() ((void) 0) |
Ronald Cron | 3cec8e8 | 2022-03-27 14:34:09 +0200 | [diff] [blame] | 367 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 369 | /** \def MD_PSA_INIT |
| 370 | * |
| 371 | * Call this macro to initialize the PSA subsystem if MD uses a driver, |
| 372 | * and do nothing otherwise. |
| 373 | * |
| 374 | * If the initialization fails, mark the test case as failed and jump to the |
| 375 | * \p exit label. |
| 376 | */ |
| 377 | /** \def MD_PSA_DONE |
| 378 | * |
| 379 | * Call this macro at the end of a test case if you called #MD_PSA_INIT. |
| 380 | * |
| 381 | * This is like #PSA_DONE except it does nothing under the same conditions as |
| 382 | * #MD_PSA_INIT. |
| 383 | */ |
| 384 | #if defined(MBEDTLS_MD_SOME_PSA) |
| 385 | #define MD_PSA_INIT() PSA_INIT() |
| 386 | #define MD_PSA_DONE() PSA_DONE() |
| 387 | #else /* MBEDTLS_MD_SOME_PSA */ |
| 388 | #define MD_PSA_INIT() ((void) 0) |
| 389 | #define MD_PSA_DONE() ((void) 0) |
| 390 | #endif /* MBEDTLS_MD_SOME_PSA */ |
| 391 | |
Valerio Setti | 10e9aa2 | 2023-12-12 11:54:20 +0100 | [diff] [blame] | 392 | /** \def BLOCK_CIPHER_PSA_INIT |
| 393 | * |
| 394 | * Call this macro to initialize the PSA subsystem if BLOCK_CIPHER uses a driver, |
| 395 | * and do nothing otherwise. |
| 396 | * |
| 397 | * If the initialization fails, mark the test case as failed and jump to the |
| 398 | * \p exit label. |
| 399 | */ |
| 400 | /** \def BLOCK_CIPHER_PSA_DONE |
| 401 | * |
| 402 | * Call this macro at the end of a test case if you called #BLOCK_CIPHER_PSA_INIT. |
| 403 | * |
| 404 | * This is like #PSA_DONE except it does nothing under the same conditions as |
| 405 | * #BLOCK_CIPHER_PSA_INIT. |
| 406 | */ |
| 407 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
| 408 | #define BLOCK_CIPHER_PSA_INIT() PSA_INIT() |
| 409 | #define BLOCK_CIPHER_PSA_DONE() PSA_DONE() |
| 410 | #else /* MBEDTLS_MD_SOME_PSA */ |
| 411 | #define BLOCK_CIPHER_PSA_INIT() ((void) 0) |
| 412 | #define BLOCK_CIPHER_PSA_DONE() ((void) 0) |
| 413 | #endif /* MBEDTLS_MD_SOME_PSA */ |
| 414 | |
| 415 | |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 416 | /** \def MD_OR_USE_PSA_INIT |
| 417 | * |
| 418 | * Call this macro to initialize the PSA subsystem if MD uses a driver, |
Manuel Pégourié-Gonnard | 161dca6 | 2023-03-21 16:22:59 +0100 | [diff] [blame] | 419 | * or if #MBEDTLS_USE_PSA_CRYPTO or #MBEDTLS_SSL_PROTO_TLS1_3 is enabled, |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 420 | * and do nothing otherwise. |
| 421 | * |
| 422 | * If the initialization fails, mark the test case as failed and jump to the |
| 423 | * \p exit label. |
| 424 | */ |
| 425 | /** \def MD_OR_USE_PSA_DONE |
| 426 | * |
| 427 | * Call this macro at the end of a test case if you called #MD_OR_USE_PSA_INIT. |
| 428 | * |
| 429 | * This is like #PSA_DONE except it does nothing under the same conditions as |
| 430 | * #MD_OR_USE_PSA_INIT. |
| 431 | */ |
| 432 | #if defined(MBEDTLS_MD_SOME_PSA) || \ |
| 433 | defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 434 | #define MD_OR_USE_PSA_INIT() PSA_INIT() |
| 435 | #define MD_OR_USE_PSA_DONE() PSA_DONE() |
| 436 | #else |
| 437 | #define MD_OR_USE_PSA_INIT() ((void) 0) |
| 438 | #define MD_OR_USE_PSA_DONE() ((void) 0) |
| 439 | #endif |
| 440 | |
Valerio Setti | dc32ac2 | 2023-11-13 10:27:56 +0100 | [diff] [blame] | 441 | /** \def AES_PSA_INIT |
| 442 | * |
| 443 | * Call this macro to initialize the PSA subsystem if AES_C is not defined, |
| 444 | * so that CTR_DRBG uses PSA implementation to get AES-ECB. |
| 445 | * |
| 446 | * If the initialization fails, mark the test case as failed and jump to the |
| 447 | * \p exit label. |
| 448 | */ |
| 449 | /** \def AES_PSA_DONE |
| 450 | * |
| 451 | * Call this macro at the end of a test case if you called #AES_PSA_INIT. |
| 452 | * |
| 453 | * This is like #PSA_DONE except it does nothing under the same conditions as |
| 454 | * #AES_PSA_INIT. |
| 455 | */ |
Gilles Peskine | 0e3704f | 2024-06-20 22:08:44 +0200 | [diff] [blame] | 456 | #if defined(MBEDTLS_CTR_DRBG_USE_PSA_CRYPTO) |
Valerio Setti | dc32ac2 | 2023-11-13 10:27:56 +0100 | [diff] [blame] | 457 | #define AES_PSA_INIT() PSA_INIT() |
| 458 | #define AES_PSA_DONE() PSA_DONE() |
Gilles Peskine | 0e3704f | 2024-06-20 22:08:44 +0200 | [diff] [blame] | 459 | #else /* MBEDTLS_CTR_DRBG_USE_PSA_CRYPTO */ |
| 460 | #define AES_PSA_INIT() ((void) 0) |
| 461 | #define AES_PSA_DONE() ((void) 0) |
Gilles Peskine | bdd16d4 | 2024-06-20 21:47:31 +0200 | [diff] [blame] | 462 | #endif /* MBEDTLS_CTR_DRBG_USE_PSA_CRYPTO */ |
Valerio Setti | dc32ac2 | 2023-11-13 10:27:56 +0100 | [diff] [blame] | 463 | |
Gilles Peskine | 35f0573 | 2024-07-17 14:00:31 +0200 | [diff] [blame] | 464 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 465 | defined(MBEDTLS_CTR_DRBG_C) && \ |
| 466 | defined(MBEDTLS_CTR_DRBG_USE_PSA_CRYPTO) |
| 467 | /* When AES_C is not defined and PSA does not have an external RNG, |
| 468 | * then CTR_DRBG uses PSA to perform AES-ECB. In this scenario 1 key |
| 469 | * slot is used internally from PSA to hold the AES key and it should |
| 470 | * not be taken into account when evaluating remaining open slots. */ |
| 471 | #define MBEDTLS_TEST_PSA_INTERNAL_KEYS_FOR_DRBG 1 |
| 472 | #else |
| 473 | #define MBEDTLS_TEST_PSA_INTERNAL_KEYS_FOR_DRBG 0 |
| 474 | #endif |
| 475 | |
| 476 | /** The number of volatile keys that PSA crypto uses internally. |
| 477 | * |
| 478 | * We expect that many volatile keys to be in use after a successful |
| 479 | * psa_crypto_init(). |
| 480 | */ |
| 481 | #define MBEDTLS_TEST_PSA_INTERNAL_KEYS \ |
| 482 | MBEDTLS_TEST_PSA_INTERNAL_KEYS_FOR_DRBG |
| 483 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 484 | #endif /* PSA_CRYPTO_HELPERS_H */ |