blob: 69bb8a1d838c737389eeb940027b3da74ddebc33 [file] [log] [blame]
Gilles Peskine514a8fd2020-11-13 17:41:53 +01001/** \file psa_crypto_helpers.c
2 *
3 * \brief Helper functions to test PSA crypto functionality.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include <test/helpers.h>
24#include <test/macros.h>
Gilles Peskined4008d52020-11-24 17:34:30 +010025#include <test/psa_crypto_helpers.h>
Gilles Peskine514a8fd2020-11-13 17:41:53 +010026
27#if defined(MBEDTLS_PSA_CRYPTO_C)
28
29#include <psa/crypto.h>
30
Gilles Peskine313ffb82021-02-14 12:51:14 +010031#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
32
33#include <psa_crypto_storage.h>
34
35static mbedtls_svc_key_id_t key_ids_used_in_test[9];
36static size_t num_key_ids_used;
37
38/* Record a key id as potentially used in a test case. */
39int mbedtls_test_uses_key_id( mbedtls_svc_key_id_t key_id )
40{
41 size_t i;
42 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key_id ) >
43 PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
44 {
45 /* Don't touch key id values that designate non-key files. */
46 return( 1 );
47 }
48 for( i = 0; i < num_key_ids_used ; i++ )
49 {
50 if( mbedtls_svc_key_id_equal( key_id, key_ids_used_in_test[i] ) )
51 return( 1 );
52 }
53 if( num_key_ids_used == ARRAY_LENGTH( key_ids_used_in_test ) )
54 return( 0 );
55 key_ids_used_in_test[num_key_ids_used] = key_id;
56 ++num_key_ids_used;
57 return( 1 );
58}
59
60/* Destroy all key ids that may have been created by the current test case. */
61void mbedtls_test_psa_purge_key_storage( void )
62{
63 size_t i;
64 for( i = 0; i < num_key_ids_used; i++ )
65 psa_destroy_persistent_key( key_ids_used_in_test[i] );
66 num_key_ids_used = 0;
67}
68#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
69
Gilles Peskined4008d52020-11-24 17:34:30 +010070const char *mbedtls_test_helper_is_psa_leaking( void )
71{
72 mbedtls_psa_stats_t stats;
73
74 mbedtls_psa_get_stats( &stats );
75
76 if( stats.volatile_slots != 0 )
77 return( "A volatile slot has not been closed properly." );
78 if( stats.persistent_slots != 0 )
79 return( "A persistent slot has not been closed properly." );
80 if( stats.external_slots != 0 )
81 return( "An external slot has not been closed properly." );
82 if( stats.half_filled_slots != 0 )
83 return( "A half-filled slot has not been cleared properly." );
84 if( stats.locked_slots != 0 )
85 return( "Some slots are still marked as locked." );
86
87 return( NULL );
88}
89
90#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
91/** Name of the file where return statuses are logged by #RECORD_STATUS. */
92#define STATUS_LOG_FILE_NAME "statuses.log"
93
94psa_status_t mbedtls_test_record_status( psa_status_t status,
95 const char *func,
96 const char *file, int line,
97 const char *expr )
98{
99 /* We open the log file on first use.
100 * We never close the log file, so the record_status feature is not
101 * compatible with resource leak detectors such as Asan.
102 */
103 static FILE *log;
104 if( log == NULL )
105 log = fopen( STATUS_LOG_FILE_NAME, "a" );
106 fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
107 return( status );
108}
109#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
110
Gilles Peskine514a8fd2020-11-13 17:41:53 +0100111#endif /* MBEDTLS_PSA_CRYPTO_C */