blob: 1dd6084337e14749e7dbd4b68816282aac9982a3 [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 */
4/* Copyright (C) 2019, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
Gilles Peskine1838e822019-06-20 12:40:56 +020022#ifndef PSA_CRYPTO_HELPERS_H
23#define PSA_CRYPTO_HELPERS_H
24
Ronald Cron02c78b72020-05-27 09:22:32 +020025#include "test/psa_helpers.h"
Gilles Peskine952f4092019-05-23 20:25:48 +020026
Gilles Peskine3cff7682019-06-20 12:54:43 +020027#include <psa/crypto.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.";
44
Gilles Peskinedd413d32019-05-28 15:06:43 +020045 /* If the test has already failed, don't overwrite the failure
46 * information. Do keep the stats lookup above, because it can be
47 * convenient to break on it when debugging a failure. */
Janos Follath849b05a2019-08-09 10:22:32 +010048 if( msg != NULL && test_info.result == TEST_RESULT_SUCCESS )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020049 test_fail( msg, line, file );
50
Gilles Peskinedd413d32019-05-28 15:06:43 +020051 return( msg == NULL );
52}
53
Gilles Peskine3cff7682019-06-20 12:54:43 +020054/** Check that no PSA Crypto key slots are in use.
Gilles Peskinedd413d32019-05-28 15:06:43 +020055 */
56#define ASSERT_PSA_PRISTINE( ) \
57 do \
58 { \
59 if( ! test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \
60 goto exit; \
61 } \
62 while( 0 )
63
64static void test_helper_psa_done( int line, const char *file )
65{
66 (void) test_helper_is_psa_pristine( line, file );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020067 mbedtls_psa_crypto_free( );
68}
69
Gilles Peskine3cff7682019-06-20 12:54:43 +020070/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +020071 * in use.
72 */
73#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
74
Gilles Peskine51681552019-05-20 19:35:37 +020075
76
77#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
78#include <psa/crypto.h>
79
80/** Name of the file where return statuses are logged by #RECORD_STATUS. */
81#define STATUS_LOG_FILE_NAME "statuses.log"
82
83static psa_status_t record_status( psa_status_t status,
84 const char *func,
85 const char *file, int line,
86 const char *expr )
87{
88 /* We open the log file on first use.
89 * We never close the log file, so the record_status feature is not
90 * compatible with resource leak detectors such as Asan.
91 */
92 static FILE *log;
93 if( log == NULL )
94 log = fopen( STATUS_LOG_FILE_NAME, "a" );
95 fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
96 return( status );
97}
98
99/** Return value logging wrapper macro.
100 *
101 * Evaluate \p expr. Write a line recording its value to the log file
102 * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
103 * list of fields:
104 * ```
105 * value of expr:string:__FILE__:__LINE__:expr
106 * ```
107 *
108 * The test code does not call this macro explicitly because that would
109 * be very invasive. Instead, we instrument the source code by defining
110 * a bunch of wrapper macros like
111 * ```
112 * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
113 * ```
114 * These macro definitions must be present in `instrument_record_status.h`
115 * when building the test suites.
116 *
117 * \param string A string, normally a function name.
118 * \param expr An expression to evaluate, normally a call of the function
119 * whose name is in \p string. This expression must return
120 * a value of type #psa_status_t.
121 * \return The value of \p expr.
122 */
123#define RECORD_STATUS( string, expr ) \
124 record_status( ( expr ), string, __FILE__, __LINE__, #expr )
125
126#include "instrument_record_status.h"
127
128#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
129
Gilles Peskine1838e822019-06-20 12:40:56 +0200130#endif /* PSA_CRYPTO_HELPERS_H */