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