blob: 499f4b3280f2b39ac7fb425577c470bb3d76786c [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 Peskined4008d52020-11-24 17:34:30 +010031const char *mbedtls_test_helper_is_psa_leaking( void )
32{
33 mbedtls_psa_stats_t stats;
34
35 mbedtls_psa_get_stats( &stats );
36
37 if( stats.volatile_slots != 0 )
38 return( "A volatile slot has not been closed properly." );
39 if( stats.persistent_slots != 0 )
40 return( "A persistent slot has not been closed properly." );
41 if( stats.external_slots != 0 )
42 return( "An external slot has not been closed properly." );
43 if( stats.half_filled_slots != 0 )
44 return( "A half-filled slot has not been cleared properly." );
45 if( stats.locked_slots != 0 )
46 return( "Some slots are still marked as locked." );
47
48 return( NULL );
49}
50
51#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
52/** Name of the file where return statuses are logged by #RECORD_STATUS. */
53#define STATUS_LOG_FILE_NAME "statuses.log"
54
55psa_status_t mbedtls_test_record_status( psa_status_t status,
56 const char *func,
57 const char *file, int line,
58 const char *expr )
59{
60 /* We open the log file on first use.
61 * We never close the log file, so the record_status feature is not
62 * compatible with resource leak detectors such as Asan.
63 */
64 static FILE *log;
65 if( log == NULL )
66 log = fopen( STATUS_LOG_FILE_NAME, "a" );
67 fprintf( log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr );
68 return( status );
69}
70#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
71
Gilles Peskine4fc21fd2020-11-13 18:47:18 +010072#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
73#include <test/random.h>
74
75psa_status_t mbedtls_psa_external_get_random(
76 mbedtls_psa_external_random_context_t *context,
77 uint8_t *output, size_t output_size, size_t *output_length )
78{
79 /* This implementation is for test purposes only!
80 * Use the libc non-cryptographic random generator. */
81 (void) context;
82 mbedtls_test_rnd_std_rand( NULL, output, output_size );
83 *output_length = output_size;
84 return( PSA_SUCCESS );
85}
86#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
87
Gilles Peskine514a8fd2020-11-13 17:41:53 +010088#endif /* MBEDTLS_PSA_CRYPTO_C */