blob: 361fbbb48a5a6978b39404cf83a77e52350f1162 [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 Peskine1e005652020-11-24 17:41:07 +010029/** Check for things that have not been cleaned up properly in the
30 * PSA subsystem.
31 *
32 * \return NULL if nothing has leaked.
33 * \return A string literal explaining what has not been cleaned up
34 * if applicable.
35 */
36static const char *mbedtls_test_helper_is_psa_leaking( void )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020037{
38 mbedtls_psa_stats_t stats;
Gilles Peskinea6d252a2019-05-23 20:34:30 +020039
40 mbedtls_psa_get_stats( &stats );
41
42 if( stats.volatile_slots != 0 )
Gilles Peskine1e005652020-11-24 17:41:07 +010043 return( "A volatile slot has not been closed properly." );
44 if( stats.persistent_slots != 0 )
45 return( "A persistent slot has not been closed properly." );
46 if( stats.external_slots != 0 )
47 return( "An external slot has not been closed properly." );
48 if( stats.half_filled_slots != 0 )
49 return( "A half-filled slot has not been cleared properly." );
50 if( stats.locked_slots != 0 )
51 return( "Some slots are still marked as locked." );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020052
Gilles Peskine1e005652020-11-24 17:41:07 +010053 return( NULL );
Gilles Peskinedd413d32019-05-28 15:06:43 +020054}
55
Gilles Peskine3cff7682019-06-20 12:54:43 +020056/** Check that no PSA Crypto key slots are in use.
Gilles Peskinedd413d32019-05-28 15:06:43 +020057 */
Gilles Peskine1e005652020-11-24 17:41:07 +010058#define ASSERT_PSA_PRISTINE( ) \
59 TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020060
Gilles Peskine3cff7682019-06-20 12:54:43 +020061/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +020062 * in use.
63 */
Gilles Peskine1e005652020-11-24 17:41:07 +010064#define PSA_DONE( ) \
65 do \
66 { \
67 ASSERT_PSA_PRISTINE( ); \
68 mbedtls_psa_crypto_free( ); \
69 } \
70 while( 0 )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020071
Gilles Peskine51681552019-05-20 19:35:37 +020072
73
74#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
75#include <psa/crypto.h>
76
77/** Name of the file where return statuses are logged by #RECORD_STATUS. */
78#define STATUS_LOG_FILE_NAME "statuses.log"
79
Gilles Peskineddfd0802020-11-24 17:07:05 +010080static psa_status_t mbedtls_test_record_status( psa_status_t status,
81 const char *func,
82 const char *file, int line,
83 const char *expr )
Gilles Peskine51681552019-05-20 19:35:37 +020084{
85 /* We open the log file on first use.
86 * We never close the log file, so the record_status feature is not
87 * compatible with resource leak detectors such as Asan.
88 */
89 static FILE *log;
90 if( log == NULL )
91 log = fopen( STATUS_LOG_FILE_NAME, "a" );
92 fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
93 return( status );
94}
95
96/** Return value logging wrapper macro.
97 *
98 * Evaluate \p expr. Write a line recording its value to the log file
99 * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
100 * list of fields:
101 * ```
102 * value of expr:string:__FILE__:__LINE__:expr
103 * ```
104 *
105 * The test code does not call this macro explicitly because that would
106 * be very invasive. Instead, we instrument the source code by defining
107 * a bunch of wrapper macros like
108 * ```
109 * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
110 * ```
111 * These macro definitions must be present in `instrument_record_status.h`
112 * when building the test suites.
113 *
114 * \param string A string, normally a function name.
115 * \param expr An expression to evaluate, normally a call of the function
116 * whose name is in \p string. This expression must return
117 * a value of type #psa_status_t.
118 * \return The value of \p expr.
119 */
120#define RECORD_STATUS( string, expr ) \
Gilles Peskineddfd0802020-11-24 17:07:05 +0100121 mbedtls_test_record_status( ( expr ), string, __FILE__, __LINE__, #expr )
Gilles Peskine51681552019-05-20 19:35:37 +0200122
123#include "instrument_record_status.h"
124
125#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
126
Gilles Peskine1838e822019-06-20 12:40:56 +0200127#endif /* PSA_CRYPTO_HELPERS_H */