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 | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 29 | static int test_helper_is_psa_pristine( int line, const char *file ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 30 | { |
| 31 | mbedtls_psa_stats_t stats; |
| 32 | const char *msg = NULL; |
| 33 | |
| 34 | mbedtls_psa_get_stats( &stats ); |
| 35 | |
| 36 | if( stats.volatile_slots != 0 ) |
| 37 | msg = "A volatile slot has not been closed properly."; |
| 38 | else if( stats.persistent_slots != 0 ) |
| 39 | msg = "A persistent slot has not been closed properly."; |
| 40 | else if( stats.external_slots != 0 ) |
| 41 | msg = "An external slot has not been closed properly."; |
| 42 | else if( stats.half_filled_slots != 0 ) |
| 43 | msg = "A half-filled slot has not been cleared properly."; |
Ronald Cron | 1ad1eee | 2020-11-15 14:21:04 +0100 | [diff] [blame^] | 44 | else if( stats.locked_slots != 0 ) |
Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 45 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 46 | msg = "Some slots are still marked as locked."; |
Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 47 | } |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 48 | |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 49 | /* If the test has already failed, don't overwrite the failure |
| 50 | * information. Do keep the stats lookup above, because it can be |
| 51 | * convenient to break on it when debugging a failure. */ |
Janos Follath | 849b05a | 2019-08-09 10:22:32 +0100 | [diff] [blame] | 52 | if( msg != NULL && test_info.result == TEST_RESULT_SUCCESS ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 53 | test_fail( msg, line, file ); |
| 54 | |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 55 | return( msg == NULL ); |
| 56 | } |
| 57 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 58 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 59 | */ |
| 60 | #define ASSERT_PSA_PRISTINE( ) \ |
| 61 | do \ |
| 62 | { \ |
| 63 | if( ! test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \ |
| 64 | goto exit; \ |
| 65 | } \ |
| 66 | while( 0 ) |
| 67 | |
| 68 | static void test_helper_psa_done( int line, const char *file ) |
| 69 | { |
| 70 | (void) test_helper_is_psa_pristine( line, file ); |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 71 | mbedtls_psa_crypto_free( ); |
| 72 | } |
| 73 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 74 | /** 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] | 75 | * in use. |
| 76 | */ |
| 77 | #define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ ) |
| 78 | |
Gilles Peskine | 5168155 | 2019-05-20 19:35:37 +0200 | [diff] [blame] | 79 | |
| 80 | |
| 81 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
| 82 | #include <psa/crypto.h> |
| 83 | |
| 84 | /** Name of the file where return statuses are logged by #RECORD_STATUS. */ |
| 85 | #define STATUS_LOG_FILE_NAME "statuses.log" |
| 86 | |
| 87 | static psa_status_t record_status( psa_status_t status, |
| 88 | const char *func, |
| 89 | const char *file, int line, |
| 90 | const char *expr ) |
| 91 | { |
| 92 | /* We open the log file on first use. |
| 93 | * We never close the log file, so the record_status feature is not |
| 94 | * compatible with resource leak detectors such as Asan. |
| 95 | */ |
| 96 | static FILE *log; |
| 97 | if( log == NULL ) |
| 98 | log = fopen( STATUS_LOG_FILE_NAME, "a" ); |
| 99 | fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr ); |
| 100 | return( status ); |
| 101 | } |
| 102 | |
| 103 | /** Return value logging wrapper macro. |
| 104 | * |
| 105 | * Evaluate \p expr. Write a line recording its value to the log file |
| 106 | * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated |
| 107 | * list of fields: |
| 108 | * ``` |
| 109 | * value of expr:string:__FILE__:__LINE__:expr |
| 110 | * ``` |
| 111 | * |
| 112 | * The test code does not call this macro explicitly because that would |
| 113 | * be very invasive. Instead, we instrument the source code by defining |
| 114 | * a bunch of wrapper macros like |
| 115 | * ``` |
| 116 | * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init()) |
| 117 | * ``` |
| 118 | * These macro definitions must be present in `instrument_record_status.h` |
| 119 | * when building the test suites. |
| 120 | * |
| 121 | * \param string A string, normally a function name. |
| 122 | * \param expr An expression to evaluate, normally a call of the function |
| 123 | * whose name is in \p string. This expression must return |
| 124 | * a value of type #psa_status_t. |
| 125 | * \return The value of \p expr. |
| 126 | */ |
| 127 | #define RECORD_STATUS( string, expr ) \ |
| 128 | record_status( ( expr ), string, __FILE__, __LINE__, #expr ) |
| 129 | |
| 130 | #include "instrument_record_status.h" |
| 131 | |
| 132 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 133 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 134 | #endif /* PSA_CRYPTO_HELPERS_H */ |