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