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