blob: c8013a1a8fa393c35d3ab76659e7f622d5cfa893 [file] [log] [blame]
Gilles Peskine952f4092019-05-23 20:25:48 +02001/*
Gilles Peskine3cff7682019-06-20 12:54:43 +02002 * Helper functions for tests that use the PSA Crypto API.
Gilles Peskine952f4092019-05-23 20:25:48 +02003 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskine952f4092019-05-23 20:25:48 +02006 * 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 Peskine952f4092019-05-23 20:25:48 +020019 */
20
Gilles Peskine1838e822019-06-20 12:40:56 +020021#ifndef PSA_CRYPTO_HELPERS_H
22#define PSA_CRYPTO_HELPERS_H
23
Ronald Cron02c78b72020-05-27 09:22:32 +020024#include "test/psa_helpers.h"
Gilles Peskine952f4092019-05-23 20:25:48 +020025
Gilles Peskine3cff7682019-06-20 12:54:43 +020026#include <psa/crypto.h>
Gilles Peskine952f4092019-05-23 20:25:48 +020027
Gilles Peskinedd413d32019-05-28 15:06:43 +020028static int test_helper_is_psa_pristine( int line, const char *file )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020029{
30 mbedtls_psa_stats_t stats;
31 const char *msg = NULL;
32
33 mbedtls_psa_get_stats( &stats );
34
35 if( stats.volatile_slots != 0 )
36 msg = "A volatile slot has not been closed properly.";
37 else if( stats.persistent_slots != 0 )
38 msg = "A persistent slot has not been closed properly.";
39 else if( stats.external_slots != 0 )
40 msg = "An external slot has not been closed properly.";
41 else if( stats.half_filled_slots != 0 )
42 msg = "A half-filled slot has not been cleared properly.";
43
Gilles Peskinedd413d32019-05-28 15:06:43 +020044 /* If the test has already failed, don't overwrite the failure
45 * information. Do keep the stats lookup above, because it can be
46 * convenient to break on it when debugging a failure. */
Janos Follath849b05a2019-08-09 10:22:32 +010047 if( msg != NULL && test_info.result == TEST_RESULT_SUCCESS )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020048 test_fail( msg, line, file );
49
Gilles Peskinedd413d32019-05-28 15:06:43 +020050 return( msg == NULL );
51}
52
Gilles Peskine3cff7682019-06-20 12:54:43 +020053/** Check that no PSA Crypto key slots are in use.
Gilles Peskinedd413d32019-05-28 15:06:43 +020054 */
55#define ASSERT_PSA_PRISTINE( ) \
56 do \
57 { \
58 if( ! test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \
59 goto exit; \
60 } \
61 while( 0 )
62
63static void test_helper_psa_done( int line, const char *file )
64{
65 (void) test_helper_is_psa_pristine( line, file );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020066 mbedtls_psa_crypto_free( );
67}
68
Gilles Peskine3cff7682019-06-20 12:54:43 +020069/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +020070 * in use.
71 */
72#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
73
Gilles Peskine51681552019-05-20 19:35:37 +020074
75
76#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
77#include <psa/crypto.h>
78
79/** Name of the file where return statuses are logged by #RECORD_STATUS. */
80#define STATUS_LOG_FILE_NAME "statuses.log"
81
82static psa_status_t record_status( psa_status_t status,
83 const char *func,
84 const char *file, int line,
85 const char *expr )
86{
87 /* We open the log file on first use.
88 * We never close the log file, so the record_status feature is not
89 * compatible with resource leak detectors such as Asan.
90 */
91 static FILE *log;
92 if( log == NULL )
93 log = fopen( STATUS_LOG_FILE_NAME, "a" );
94 fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
95 return( status );
96}
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 ) \
123 record_status( ( expr ), string, __FILE__, __LINE__, #expr )
124
125#include "instrument_record_status.h"
126
127#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
128
Gilles Peskine1838e822019-06-20 12:40:56 +0200129#endif /* PSA_CRYPTO_HELPERS_H */