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 | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 24 | #include "test/psa_helpers.h" |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 25 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 26 | #include <psa/crypto.h> |
Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 27 | #include <psa_crypto_slot_management.h> |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 28 | |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 29 | /** Check for things that have not been cleaned up properly in the |
| 30 | * PSA subsystem. |
| 31 | * |
| 32 | * \return NULL if nothing has leaked. |
| 33 | * \return A string literal explaining what has not been cleaned up |
| 34 | * if applicable. |
| 35 | */ |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 36 | const char *mbedtls_test_helper_is_psa_leaking( void ); |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 37 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 38 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame^] | 39 | * |
| 40 | * If any slots are in use, mark the current test as failed and jump to |
| 41 | * the exit label. This is equivalent to |
| 42 | * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )` |
| 43 | * but with a more informative message. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 44 | */ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame^] | 45 | #define ASSERT_PSA_PRISTINE( ) \ |
| 46 | do \ |
| 47 | { \ |
| 48 | if( test_fail_if_psa_leaking( __LINE__, __FILE__ ) ) \ |
| 49 | goto exit; \ |
| 50 | } \ |
| 51 | while( 0 ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 52 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 53 | /** 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] | 54 | * in use. |
| 55 | */ |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame^] | 56 | #define PSA_DONE( ) \ |
| 57 | do \ |
| 58 | { \ |
| 59 | test_fail_if_psa_leaking( __LINE__, __FILE__ ); \ |
| 60 | mbedtls_psa_crypto_free( ); \ |
| 61 | } \ |
Gilles Peskine | 1e00565 | 2020-11-24 17:41:07 +0100 | [diff] [blame] | 62 | while( 0 ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 63 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 64 | |
| 65 | |
Gilles Peskine | 76175ba | 2020-11-24 18:39:12 +0100 | [diff] [blame] | 66 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 67 | /** Enable the insecure implementation of mbedtls_psa_external_get_random(). |
| 68 | * |
| 69 | * The insecure implementation of mbedtls_psa_external_get_random() is |
| 70 | * disabled by default. |
| 71 | * |
| 72 | * When MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled and the test |
Gilles Peskine | c85c201 | 2021-01-06 20:47:16 +0100 | [diff] [blame^] | 73 | * helpers are linked into a program, you must enable this before running any |
| 74 | * code that uses the PSA subsystem to generate random data (including internal |
Gilles Peskine | 76175ba | 2020-11-24 18:39:12 +0100 | [diff] [blame] | 75 | * random generation for purposes such as blinding when the random generation |
| 76 | * is routed through PSA). |
| 77 | * |
| 78 | * You can enable and disable it at any time, regardless of the state |
| 79 | * of the PSA subsystem. You may disable it temporarily to simulate a |
| 80 | * depleted entropy source. |
| 81 | */ |
| 82 | void mbedtls_test_enable_insecure_external_rng( void ); |
| 83 | |
| 84 | /** Disable the insecure implementation of mbedtls_psa_external_get_random(). |
| 85 | * |
| 86 | * See mbedtls_test_enable_insecure_external_rng(). |
| 87 | */ |
| 88 | void mbedtls_test_disable_insecure_external_rng( void ); |
| 89 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 90 | |
| 91 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 92 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 93 | psa_status_t mbedtls_test_record_status( psa_status_t status, |
| 94 | const char *func, |
| 95 | const char *file, int line, |
| 96 | const char *expr ); |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 97 | |
| 98 | /** Return value logging wrapper macro. |
| 99 | * |
| 100 | * Evaluate \p expr. Write a line recording its value to the log file |
| 101 | * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated |
| 102 | * list of fields: |
| 103 | * ``` |
| 104 | * value of expr:string:__FILE__:__LINE__:expr |
| 105 | * ``` |
| 106 | * |
| 107 | * The test code does not call this macro explicitly because that would |
| 108 | * be very invasive. Instead, we instrument the source code by defining |
| 109 | * a bunch of wrapper macros like |
| 110 | * ``` |
| 111 | * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init()) |
| 112 | * ``` |
| 113 | * These macro definitions must be present in `instrument_record_status.h` |
| 114 | * when building the test suites. |
| 115 | * |
| 116 | * \param string A string, normally a function name. |
| 117 | * \param expr An expression to evaluate, normally a call of the function |
| 118 | * whose name is in \p string. This expression must return |
| 119 | * a value of type #psa_status_t. |
| 120 | * \return The value of \p expr. |
| 121 | */ |
| 122 | #define RECORD_STATUS( string, expr ) \ |
Gilles Peskine | ddfd080 | 2020-11-24 17:07:05 +0100 | [diff] [blame] | 123 | mbedtls_test_record_status( ( expr ), string, __FILE__, __LINE__, #expr ) |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 124 | |
| 125 | #include "instrument_record_status.h" |
| 126 | |
| 127 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 128 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 129 | #endif /* PSA_CRYPTO_HELPERS_H */ |