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 | 7ff7965 | 2023-11-03 12:04:52 +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 | |
| 14 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 15 | |
Ronald Cron | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 16 | #include "test/psa_helpers.h" |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 17 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 18 | #include <psa/crypto.h> |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 19 | |
Gilles Peskine | 50580a5 | 2024-01-04 16:44:16 +0100 | [diff] [blame] | 20 | #include "test/psa_test_wrappers.h" |
| 21 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 22 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 23 | #include "mbedtls/psa_util.h" |
| 24 | #endif |
| 25 | |
David Horstmann | 5d05b47 | 2023-12-20 11:26:40 +0000 | [diff] [blame] | 26 | #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \ |
| 27 | && defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
David Horstmann | 9c97fda | 2023-12-14 14:27:50 +0000 | [diff] [blame] | 28 | #include "test/psa_memory_poisoning_wrappers.h" |
| 29 | #endif |
| 30 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 31 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 32 | |
| 33 | /* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 34 | 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] | 35 | |
| 36 | /** Destroy persistent keys recorded with #TEST_USES_KEY_ID. |
| 37 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 38 | void mbedtls_test_psa_purge_key_storage(void); |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 39 | |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 40 | /** Purge the in-memory cache of persistent keys recorded with |
| 41 | * #TEST_USES_KEY_ID. |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 42 | * |
| 43 | * Call this function before calling PSA_DONE() if it's ok for |
| 44 | * persistent keys to still exist at this point. |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 45 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 46 | void mbedtls_test_psa_purge_key_cache(void); |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 48 | /** \def TEST_USES_KEY_ID |
| 49 | * |
| 50 | * Call this macro in a test function before potentially creating a |
| 51 | * persistent key. Test functions that use this mechanism must call |
| 52 | * mbedtls_test_psa_purge_key_storage() in their cleanup code. |
| 53 | * |
| 54 | * This macro records a persistent key identifier as potentially used in the |
| 55 | * current test case. Recorded key identifiers will be cleaned up at the end |
| 56 | * of the test case, even on failure. |
| 57 | * |
| 58 | * This macro has no effect on volatile keys. Therefore, it is safe to call |
| 59 | * this macro in a test function that creates either volatile or persistent |
| 60 | * keys depending on the test data. |
| 61 | * |
| 62 | * This macro currently has no effect on special identifiers |
| 63 | * used to store implementation-specific files. |
| 64 | * |
| 65 | * Calling this macro multiple times on the same key identifier in the same |
| 66 | * test case has no effect. |
| 67 | * |
| 68 | * This macro can fail the test case if there isn't enough memory to |
| 69 | * record the key id. |
| 70 | * |
| 71 | * \param key_id The PSA key identifier to record. |
| 72 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | #define TEST_USES_KEY_ID(key_id) \ |
| 74 | TEST_ASSERT(mbedtls_test_uses_key_id(key_id)) |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 75 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 76 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 77 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | #define TEST_USES_KEY_ID(key_id) ((void) (key_id)) |
| 79 | #define mbedtls_test_psa_purge_key_storage() ((void) 0) |
| 80 | #define mbedtls_test_psa_purge_key_cache() ((void) 0) |
Gilles Peskine | e09ef87 | 2021-02-14 13:10:38 +0100 | [diff] [blame] | 81 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 82 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | #define PSA_INIT() PSA_ASSERT(psa_crypto_init()) |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 85 | |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 86 | /** Check for things that have not been cleaned up properly in the |
| 87 | * PSA subsystem. |
| 88 | * |
| 89 | * \return NULL if nothing has leaked. |
| 90 | * \return A string literal explaining what has not been cleaned up |
| 91 | * if applicable. |
| 92 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | const char *mbedtls_test_helper_is_psa_leaking(void); |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 94 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 95 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 96 | * |
| 97 | * If any slots are in use, mark the current test as failed and jump to |
| 98 | * the exit label. This is equivalent to |
| 99 | * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )` |
| 100 | * but with a more informative message. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 101 | */ |
Yanray Wang | 89b4d12 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 102 | #define ASSERT_PSA_PRISTINE() \ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 103 | do \ |
| 104 | { \ |
Yanray Wang | 89b4d12 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 105 | if (mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__)) \ |
| 106 | goto exit; \ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 107 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | while (0) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 109 | |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 110 | /** Shut down the PSA Crypto subsystem and destroy persistent keys. |
| 111 | * Expect a clean shutdown, with no slots in use. |
| 112 | * |
| 113 | * If some key slots are still in use, record the test case as failed, |
| 114 | * but continue executing. This macro is suitable (and primarily intended) |
| 115 | * for use in the cleanup section of test functions. |
| 116 | * |
| 117 | * \note Persistent keys must be recorded with #TEST_USES_KEY_ID before |
| 118 | * creating them. |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 119 | */ |
Yanray Wang | 89b4d12 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 120 | #define PSA_DONE() \ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 121 | do \ |
| 122 | { \ |
Yanray Wang | 89b4d12 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 123 | mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__); \ |
| 124 | mbedtls_test_psa_purge_key_storage(); \ |
| 125 | mbedtls_psa_crypto_free(); \ |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 126 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | while (0) |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 128 | |
| 129 | /** Shut down the PSA Crypto subsystem, allowing persistent keys to survive. |
| 130 | * Expect a clean shutdown, with no slots in use. |
| 131 | * |
| 132 | * If some key slots are still in use, record the test case as failed and |
| 133 | * jump to the `exit` label. |
| 134 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | #define PSA_SESSION_DONE() \ |
Gilles Peskine | 65048ad | 2021-02-14 14:08:22 +0100 | [diff] [blame] | 136 | do \ |
| 137 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | mbedtls_test_psa_purge_key_cache(); \ |
| 139 | ASSERT_PSA_PRISTINE(); \ |
| 140 | mbedtls_psa_crypto_free(); \ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 141 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | while (0) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 144 | |
| 145 | |
| 146 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 147 | psa_status_t mbedtls_test_record_status(psa_status_t status, |
| 148 | const char *func, |
| 149 | const char *file, int line, |
| 150 | const char *expr); |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 151 | |
| 152 | /** Return value logging wrapper macro. |
| 153 | * |
| 154 | * Evaluate \p expr. Write a line recording its value to the log file |
| 155 | * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated |
| 156 | * list of fields: |
| 157 | * ``` |
| 158 | * value of expr:string:__FILE__:__LINE__:expr |
| 159 | * ``` |
| 160 | * |
| 161 | * The test code does not call this macro explicitly because that would |
| 162 | * be very invasive. Instead, we instrument the source code by defining |
| 163 | * a bunch of wrapper macros like |
| 164 | * ``` |
| 165 | * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init()) |
| 166 | * ``` |
| 167 | * These macro definitions must be present in `instrument_record_status.h` |
| 168 | * when building the test suites. |
| 169 | * |
| 170 | * \param string A string, normally a function name. |
| 171 | * \param expr An expression to evaluate, normally a call of the function |
| 172 | * whose name is in \p string. This expression must return |
| 173 | * a value of type #psa_status_t. |
| 174 | * \return The value of \p expr. |
| 175 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | #define RECORD_STATUS(string, expr) \ |
| 177 | mbedtls_test_record_status((expr), string, __FILE__, __LINE__, #expr) |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 178 | |
| 179 | #include "instrument_record_status.h" |
| 180 | |
| 181 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 182 | |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 183 | /** Return extended key usage policies. |
| 184 | * |
| 185 | * Do a key policy permission extension on key usage policies always involves |
| 186 | * permissions of other usage policies |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 187 | * (like PSA_KEY_USAGE_SIGN_HASH involves PSA_KEY_USAGE_SIGN_MESSAGE). |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 188 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 190 | |
Yanray Wang | 89b4d12 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 191 | /** Check that no PSA Crypto key slots are in use. |
| 192 | * |
| 193 | * If any slots are in use, mark the current test as failed. |
| 194 | * |
| 195 | * \return 0 if the key store is empty, 1 otherwise. |
| 196 | */ |
| 197 | int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename); |
| 198 | |
Gilles Peskine | 73521b0 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 199 | |
| 200 | |
| 201 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 202 | /* The #MBEDTLS_PSA_INJECT_ENTROPY feature requires two extra platform |
| 203 | * functions, which must be configured as #MBEDTLS_PLATFORM_NV_SEED_READ_MACRO |
| 204 | * and #MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO. The job of these functions |
| 205 | * is to read and write from the entropy seed file, which is located |
| 206 | * in the PSA ITS file whose uid is #PSA_CRYPTO_ITS_RANDOM_SEED_UID. |
| 207 | * (These could have been provided as library functions, but for historical |
| 208 | * reasons, they weren't, and so each integrator has to provide a copy |
| 209 | * of these functions.) |
| 210 | * |
| 211 | * Provide implementations of these functions for testing. */ |
| 212 | int mbedtls_test_inject_entropy_seed_read(unsigned char *buf, size_t len); |
| 213 | int mbedtls_test_inject_entropy_seed_write(unsigned char *buf, size_t len); |
Gilles Peskine | 4f8bf3c | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 214 | |
| 215 | |
| 216 | /** Make sure that the injected entropy is present. |
| 217 | * |
| 218 | * When MBEDTLS_PSA_INJECT_ENTROPY is enabled, psa_crypto_init() |
| 219 | * will fail if the PSA entropy seed is not present. |
| 220 | * This function must be called at least once in a test suite or other |
| 221 | * program before any call to psa_crypto_init(). |
| 222 | * It does not need to be called in each test case. |
| 223 | * |
| 224 | * The test framework calls this function before running any test case. |
| 225 | * |
| 226 | * The few tests that might remove the entropy file must call this function |
| 227 | * in their cleanup. |
| 228 | */ |
| 229 | int mbedtls_test_inject_entropy_restore(void); |
Gilles Peskine | 73521b0 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 230 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
| 231 | |
| 232 | |
| 233 | |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 234 | /** Skip a test case if the given key is a 192 bits AES key and the AES |
| 235 | * implementation is at least partially provided by an accelerator or |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 236 | * alternative implementation. |
| 237 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 238 | * Call this macro in a test case when a cryptographic operation that may |
| 239 | * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code. |
| 240 | * The macro call will skip and not fail the test case in case the operation |
| 241 | * involves a 192 bits AES key and the AES implementation is at least |
| 242 | * partially provided by an accelerator or alternative implementation. |
| 243 | * |
| 244 | * Hardware AES implementations not supporting 192 bits keys commonly exist. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 245 | * 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] | 246 | * a 192 bits key performed by an alternative AES implementation returns |
| 247 | * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro |
| 248 | * is to facilitate this and make the test case code more readable. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 249 | * |
| 250 | * \param key_type Key type |
| 251 | * \param key_bits Key length in number of bits. |
| 252 | */ |
| 253 | #if defined(MBEDTLS_AES_ALT) || \ |
| 254 | defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \ |
| 255 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES) |
| 256 | #define MBEDTLS_TEST_HAVE_ALT_AES 1 |
| 257 | #else |
| 258 | #define MBEDTLS_TEST_HAVE_ALT_AES 0 |
| 259 | #endif |
| 260 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 261 | #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] | 262 | do \ |
| 263 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | if ((MBEDTLS_TEST_HAVE_ALT_AES) && \ |
| 265 | ((key_type) == PSA_KEY_TYPE_AES) && \ |
| 266 | (key_bits == 192)) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 267 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | mbedtls_test_skip("AES-192 not supported", __LINE__, __FILE__); \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 269 | goto exit; \ |
| 270 | } \ |
| 271 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | while (0) |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 273 | |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 274 | /** Skip a test case if a GCM operation with a nonce length different from |
| 275 | * 12 bytes fails and was performed by an accelerator or alternative |
| 276 | * implementation. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 277 | * |
| 278 | * 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] | 279 | * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error |
| 280 | * code. The macro call will skip and not fail the test case in case the |
| 281 | * operation involves the GCM mode, a nonce with a length different from |
| 282 | * 12 bytes and the GCM mode implementation is an alternative one. |
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 | * Hardware GCM implementations not supporting nonce lengths different from |
| 285 | * 12 bytes commonly exist, as supporting a non-12-byte nonce requires |
| 286 | * additional computations involving the GHASH function. |
| 287 | * Consequently, PSA test cases aim at not failing when an AEAD operation in |
| 288 | * GCM mode with a nonce length different from 12 bytes is performed by an |
| 289 | * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED |
| 290 | * error code. The purpose of this macro is to facilitate this check and make |
| 291 | * the test case code more readable. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 292 | * |
Steven Cooreman | 1e9c042 | 2021-02-10 17:02:05 +0100 | [diff] [blame] | 293 | * \param alg The AEAD algorithm. |
| 294 | * \param nonce_length The nonce length in number of bytes. |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 295 | */ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 296 | #if defined(MBEDTLS_GCM_ALT) || \ |
| 297 | defined(MBEDTLS_PSA_ACCEL_ALG_GCM) |
| 298 | #define MBEDTLS_TEST_HAVE_ALT_GCM 1 |
| 299 | #else |
| 300 | #define MBEDTLS_TEST_HAVE_ALT_GCM 0 |
| 301 | #endif |
| 302 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \ |
| 304 | nonce_length) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 305 | do \ |
| 306 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 307 | if ((MBEDTLS_TEST_HAVE_ALT_GCM) && \ |
| 308 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \ |
| 309 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \ |
| 310 | ((nonce_length) != 12)) \ |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 311 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | 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] | 313 | goto exit; \ |
| 314 | } \ |
| 315 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | while (0) |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 317 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 318 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 319 | |
| 320 | /** \def USE_PSA_INIT |
| 321 | * |
| 322 | * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO |
| 323 | * is enabled and do nothing otherwise. If the initialization fails, mark |
| 324 | * the test case as failed and jump to the \p exit label. |
| 325 | */ |
| 326 | /** \def USE_PSA_DONE |
| 327 | * |
| 328 | * Call this macro at the end of a test case if you called #USE_PSA_INIT. |
| 329 | * This is like #PSA_DONE, except that it does nothing if |
| 330 | * #MBEDTLS_USE_PSA_CRYPTO is disabled. |
| 331 | */ |
| 332 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | #define USE_PSA_INIT() PSA_INIT() |
| 334 | #define USE_PSA_DONE() PSA_DONE() |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 335 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 336 | /* Define empty macros so that we can use them in the preamble and teardown |
| 337 | * of every test function that uses PSA conditionally based on |
| 338 | * MBEDTLS_USE_PSA_CRYPTO. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | #define USE_PSA_INIT() ((void) 0) |
| 340 | #define USE_PSA_DONE() ((void) 0) |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 341 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
| 342 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 343 | #endif /* PSA_CRYPTO_HELPERS_H */ |