blob: 09171ae767ddddfcdb6663d3648569b95fdcd4d5 [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>
Ronald Cron0c3752a2020-10-30 11:54:03 +010027#include <psa_crypto_slot_management.h>
Gilles Peskine952f4092019-05-23 20:25:48 +020028
Gilles Peskinedd413d32019-05-28 15:06:43 +020029static int test_helper_is_psa_pristine( int line, const char *file )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020030{
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 Cron5c522922020-11-14 16:35:34 +010044 else if( stats.unlocked_slots != PSA_KEY_SLOT_COUNT )
Ronald Cron0c3752a2020-10-30 11:54:03 +010045 {
Ronald Cron5c522922020-11-14 16:35:34 +010046 msg = "Some slots are still marked as locked.";
Ronald Cron0c3752a2020-10-30 11:54:03 +010047 }
Gilles Peskinea6d252a2019-05-23 20:34:30 +020048
Gilles Peskinedd413d32019-05-28 15:06:43 +020049 /* 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 Follath849b05a2019-08-09 10:22:32 +010052 if( msg != NULL && test_info.result == TEST_RESULT_SUCCESS )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020053 test_fail( msg, line, file );
54
Gilles Peskinedd413d32019-05-28 15:06:43 +020055 return( msg == NULL );
56}
57
Gilles Peskine3cff7682019-06-20 12:54:43 +020058/** Check that no PSA Crypto key slots are in use.
Gilles Peskinedd413d32019-05-28 15:06:43 +020059 */
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
68static void test_helper_psa_done( int line, const char *file )
69{
70 (void) test_helper_is_psa_pristine( line, file );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020071 mbedtls_psa_crypto_free( );
72}
73
Gilles Peskine3cff7682019-06-20 12:54:43 +020074/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +020075 * in use.
76 */
77#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
78
Gilles Peskine51681552019-05-20 19:35:37 +020079
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
87static 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 Peskine1838e822019-06-20 12:40:56 +0200134#endif /* PSA_CRYPTO_HELPERS_H */