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" |
Ronald Cron | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 25 | #include "test/psa_helpers.h" |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 26 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 27 | #include <psa/crypto.h> |
Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 28 | #include <psa_crypto_slot_management.h> |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 29 | |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 30 | /** Check for things that have not been cleaned up properly in the |
| 31 | * PSA subsystem. |
| 32 | * |
| 33 | * \return NULL if nothing has leaked. |
| 34 | * \return A string literal explaining what has not been cleaned up |
| 35 | * if applicable. |
| 36 | */ |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 37 | const char *mbedtls_test_helper_is_psa_leaking( void ); |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 38 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 39 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 40 | * |
| 41 | * If any slots are in use, mark the current test as failed and jump to |
| 42 | * the exit label. This is equivalent to |
| 43 | * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )` |
| 44 | * but with a more informative message. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 45 | */ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 46 | #define ASSERT_PSA_PRISTINE( ) \ |
| 47 | do \ |
| 48 | { \ |
| 49 | if( test_fail_if_psa_leaking( __LINE__, __FILE__ ) ) \ |
| 50 | goto exit; \ |
| 51 | } \ |
| 52 | while( 0 ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 53 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 54 | /** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 55 | * in use. |
| 56 | */ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame] | 57 | #define PSA_DONE( ) \ |
| 58 | do \ |
| 59 | { \ |
| 60 | test_fail_if_psa_leaking( __LINE__, __FILE__ ); \ |
| 61 | mbedtls_psa_crypto_free( ); \ |
| 62 | } \ |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 63 | while( 0 ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 64 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 65 | |
| 66 | |
| 67 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 68 | psa_status_t mbedtls_test_record_status( psa_status_t status, |
| 69 | const char *func, |
| 70 | const char *file, int line, |
| 71 | const char *expr ); |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 72 | |
| 73 | /** Return value logging wrapper macro. |
| 74 | * |
| 75 | * Evaluate \p expr. Write a line recording its value to the log file |
| 76 | * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated |
| 77 | * list of fields: |
| 78 | * ``` |
| 79 | * value of expr:string:__FILE__:__LINE__:expr |
| 80 | * ``` |
| 81 | * |
| 82 | * The test code does not call this macro explicitly because that would |
| 83 | * be very invasive. Instead, we instrument the source code by defining |
| 84 | * a bunch of wrapper macros like |
| 85 | * ``` |
| 86 | * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init()) |
| 87 | * ``` |
| 88 | * These macro definitions must be present in `instrument_record_status.h` |
| 89 | * when building the test suites. |
| 90 | * |
| 91 | * \param string A string, normally a function name. |
| 92 | * \param expr An expression to evaluate, normally a call of the function |
| 93 | * whose name is in \p string. This expression must return |
| 94 | * a value of type #psa_status_t. |
| 95 | * \return The value of \p expr. |
| 96 | */ |
| 97 | #define RECORD_STATUS( string, expr ) \ |
Gilles Peskine | ddfd080 | 2020-11-24 17:07:05 +0100 | [diff] [blame] | 98 | mbedtls_test_record_status( ( expr ), string, __FILE__, __LINE__, #expr ) |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 99 | |
| 100 | #include "instrument_record_status.h" |
| 101 | |
| 102 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 103 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame^] | 104 | /** Skip a test case if the given key is an 192 bits AES key and the AES |
| 105 | * implementation is at least partially an alternative implementation. |
| 106 | * |
| 107 | * Call this macro in a test case when a cryptography operation that may |
| 108 | * involve an AES operation returns with the PSA_ERROR_NOT_SUPPORTED error |
| 109 | * code to skip and not fail the test case in case the operation involves an |
| 110 | * 192 bits AES key and the AES implementation is at least partially an |
| 111 | * alternative implementation. |
| 112 | * |
| 113 | * Hardware AES implementations are likely to not support 192 bits keys. |
| 114 | * Consequently, PSA test cases aim at not failing when an AES operation with |
| 115 | * an 192 bits key performed by an alternative AES implementation returns |
| 116 | * with the PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro |
| 117 | * is to facilitate this and make the related code more readable. |
| 118 | * |
| 119 | * \param key_type Key type |
| 120 | * \param key_bits Key length in number of bits. |
| 121 | */ |
| 122 | #if defined(MBEDTLS_AES_ALT) || \ |
| 123 | defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \ |
| 124 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES) |
| 125 | #define MBEDTLS_TEST_HAVE_ALT_AES 1 |
| 126 | #else |
| 127 | #define MBEDTLS_TEST_HAVE_ALT_AES 0 |
| 128 | #endif |
| 129 | |
| 130 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_bits ) \ |
| 131 | do \ |
| 132 | { \ |
| 133 | if( ( MBEDTLS_TEST_HAVE_ALT_AES ) && \ |
| 134 | ( ( key_type ) == PSA_KEY_TYPE_AES ) && \ |
| 135 | ( key_bits == 192 ) ) \ |
| 136 | { \ |
| 137 | mbedtls_test_skip( "AES-192 not supported", __LINE__, __FILE__ ); \ |
| 138 | goto exit; \ |
| 139 | } \ |
| 140 | } \ |
| 141 | while( 0 ) |
| 142 | |
| 143 | /** Skip a test case in case of a GCM operation with a nonce length different |
| 144 | * from 12 bytes. |
| 145 | * |
| 146 | * Call this macro in a test case when an AEAD cryptography operation that |
| 147 | * may involve the GCM mode returns with the PSA_ERROR_NOT_SUPPORTED error |
| 148 | * code to skip and not fail the test case in case the operation involves the |
| 149 | * GCM mode, a nonce with a length different from 12 bytes and the GCM mode |
| 150 | * implementation is an alternative one. |
| 151 | * |
| 152 | * Hardware GCM implementations are likely to not support nonce lengths |
| 153 | * different from 12 are those imply additional computations involving the |
| 154 | * GHASH function. Consequently, PSA test cases aim at not failing when an |
| 155 | * AEAD operation in GCM mode with a nonce length different from 12 bytes |
| 156 | * performed by an alternative GCM implementation returns with the |
| 157 | * PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro is to |
| 158 | * facilitate this and make the related code more readable. |
| 159 | * |
| 160 | * \param alg The AEAD algorithm. |
| 161 | * \param nonce_length The nonce length in number of bytes. |
| 162 | */ |
| 163 | |
| 164 | #if defined(MBEDTLS_GCM_ALT) || \ |
| 165 | defined(MBEDTLS_PSA_ACCEL_ALG_GCM) |
| 166 | #define MBEDTLS_TEST_HAVE_ALT_GCM 1 |
| 167 | #else |
| 168 | #define MBEDTLS_TEST_HAVE_ALT_GCM 0 |
| 169 | #endif |
| 170 | |
| 171 | #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, \ |
| 172 | nonce_length ) \ |
| 173 | do \ |
| 174 | { \ |
| 175 | if( ( MBEDTLS_TEST_HAVE_ALT_GCM ) && \ |
| 176 | ( PSA_ALG_AEAD_WITH_TAG_LENGTH( ( alg ) , 0 ) == \ |
| 177 | PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ) ) && \ |
| 178 | ( ( nonce_length ) != 12 ) ) \ |
| 179 | { \ |
| 180 | mbedtls_test_skip( "GCM with non-12-byte IV is not supported", __LINE__, __FILE__ ); \ |
| 181 | goto exit; \ |
| 182 | } \ |
| 183 | } \ |
| 184 | while( 0 ) |
| 185 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 186 | #endif /* PSA_CRYPTO_HELPERS_H */ |