1 /** \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>
25 #include <test/psa_crypto_helpers.h>
26 
27 #if defined(MBEDTLS_PSA_CRYPTO_C)
28 
29 #include <psa/crypto.h>
30 
31 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
32 
33 #include <psa_crypto_storage.h>
34 
35 static mbedtls_svc_key_id_t key_ids_used_in_test[9];
36 static size_t num_key_ids_used;
37 
mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)38 int mbedtls_test_uses_key_id( mbedtls_svc_key_id_t key_id )
39 {
40     size_t i;
41     if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key_id ) >
42         PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
43     {
44         /* Don't touch key id values that designate non-key files. */
45         return( 1 );
46     }
47     for( i = 0; i < num_key_ids_used ; i++ )
48     {
49         if( mbedtls_svc_key_id_equal( key_id, key_ids_used_in_test[i] ) )
50             return( 1 );
51     }
52     if( num_key_ids_used == ARRAY_LENGTH( key_ids_used_in_test ) )
53         return( 0 );
54     key_ids_used_in_test[num_key_ids_used] = key_id;
55     ++num_key_ids_used;
56     return( 1 );
57 }
58 
mbedtls_test_psa_purge_key_storage(void)59 void mbedtls_test_psa_purge_key_storage( void )
60 {
61     size_t i;
62     for( i = 0; i < num_key_ids_used; i++ )
63         psa_destroy_persistent_key( key_ids_used_in_test[i] );
64     num_key_ids_used = 0;
65 }
66 
mbedtls_test_psa_purge_key_cache(void)67 void mbedtls_test_psa_purge_key_cache( void )
68 {
69     size_t i;
70     for( i = 0; i < num_key_ids_used; i++ )
71         psa_purge_key( key_ids_used_in_test[i] );
72 }
73 
74 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
75 
mbedtls_test_helper_is_psa_leaking(void)76 const char *mbedtls_test_helper_is_psa_leaking( void )
77 {
78     mbedtls_psa_stats_t stats;
79 
80     mbedtls_psa_get_stats( &stats );
81 
82     if( stats.volatile_slots != 0 )
83         return( "A volatile slot has not been closed properly." );
84     if( stats.persistent_slots != 0 )
85         return( "A persistent slot has not been closed properly." );
86     if( stats.external_slots != 0 )
87         return( "An external slot has not been closed properly." );
88      if( stats.half_filled_slots != 0 )
89         return( "A half-filled slot has not been cleared properly." );
90     if( stats.locked_slots != 0 )
91         return( "Some slots are still marked as locked." );
92 
93     return( NULL );
94 }
95 
96 #if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
97 /** Name of the file where return statuses are logged by #RECORD_STATUS. */
98 #define STATUS_LOG_FILE_NAME "statuses.log"
99 
mbedtls_test_record_status(psa_status_t status,const char * func,const char * file,int line,const char * expr)100 psa_status_t mbedtls_test_record_status( psa_status_t status,
101                                          const char *func,
102                                          const char *file, int line,
103                                          const char *expr )
104 {
105     /* We open the log file on first use.
106      * We never close the log file, so the record_status feature is not
107      * compatible with resource leak detectors such as Asan.
108      */
109     static FILE *log;
110     if( log == NULL )
111         log = fopen( STATUS_LOG_FILE_NAME, "a" );
112     fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
113     return( status );
114 }
115 #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
116 
mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)117 psa_key_usage_t mbedtls_test_update_key_usage_flags( psa_key_usage_t usage_flags )
118 {
119     psa_key_usage_t updated_usage = usage_flags;
120 
121     if( usage_flags & PSA_KEY_USAGE_SIGN_HASH )
122         updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
123 
124     if( usage_flags & PSA_KEY_USAGE_VERIFY_HASH )
125         updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
126 
127     return( updated_usage );
128 }
129 
130 #endif /* MBEDTLS_PSA_CRYPTO_C */
131