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