blob: 8cd361fb61f41c50b324508edd4b3d9dbd4afc25 [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/*
5 * Copyright (C) 2019, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
Gilles Peskine1838e822019-06-20 12:40:56 +020023#ifndef PSA_CRYPTO_HELPERS_H
24#define PSA_CRYPTO_HELPERS_H
25
Ronald Cron02c78b72020-05-27 09:22:32 +020026#include "test/psa_helpers.h"
Gilles Peskine952f4092019-05-23 20:25:48 +020027
Gilles Peskine3cff7682019-06-20 12:54:43 +020028#include <psa/crypto.h>
Gilles Peskine952f4092019-05-23 20:25:48 +020029
Gilles Peskinedd413d32019-05-28 15:06:43 +020030static int test_helper_is_psa_pristine( int line, const char *file )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020031{
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 Peskinedd413d32019-05-28 15:06:43 +020046 /* 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 Follath849b05a2019-08-09 10:22:32 +010049 if( msg != NULL && test_info.result == TEST_RESULT_SUCCESS )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020050 test_fail( msg, line, file );
51
Gilles Peskinedd413d32019-05-28 15:06:43 +020052 return( msg == NULL );
53}
54
Gilles Peskine3cff7682019-06-20 12:54:43 +020055/** Check that no PSA Crypto key slots are in use.
Gilles Peskinedd413d32019-05-28 15:06:43 +020056 */
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
65static void test_helper_psa_done( int line, const char *file )
66{
67 (void) test_helper_is_psa_pristine( line, file );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020068 mbedtls_psa_crypto_free( );
69}
70
Gilles Peskine3cff7682019-06-20 12:54:43 +020071/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +020072 * in use.
73 */
74#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
75
Gilles Peskine51681552019-05-20 19:35:37 +020076
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
84static 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 Peskine1838e822019-06-20 12:40:56 +0200131#endif /* PSA_CRYPTO_HELPERS_H */