blob: 26d5623448f36d0ef4b1351be81c3c7c3e80915b [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 */
4/* Copyright (C) 2019, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
Gilles Peskine1838e822019-06-20 12:40:56 +020022#ifndef PSA_CRYPTO_HELPERS_H
23#define PSA_CRYPTO_HELPERS_H
24
Gilles Peskine3cff7682019-06-20 12:54:43 +020025#include "psa_helpers.h"
Gilles Peskine952f4092019-05-23 20:25:48 +020026
Gilles Peskine3cff7682019-06-20 12:54:43 +020027#include <psa/crypto.h>
Gilles Peskine952f4092019-05-23 20:25:48 +020028
Gilles Peskinedd413d32019-05-28 15:06:43 +020029static int test_helper_is_psa_pristine( int line, const char *file )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020030{
31 mbedtls_psa_stats_t stats;
32 const char *msg = NULL;
33
34 mbedtls_psa_get_stats( &stats );
35
36 if( stats.volatile_slots != 0 )
37 msg = "A volatile slot has not been closed properly.";
38 else if( stats.persistent_slots != 0 )
39 msg = "A persistent slot has not been closed properly.";
40 else if( stats.external_slots != 0 )
41 msg = "An external slot has not been closed properly.";
42 else if( stats.half_filled_slots != 0 )
43 msg = "A half-filled slot has not been cleared properly.";
44
Gilles Peskinedd413d32019-05-28 15:06:43 +020045 /* If the test has already failed, don't overwrite the failure
46 * information. Do keep the stats lookup above, because it can be
47 * convenient to break on it when debugging a failure. */
Gilles Peskinea6d252a2019-05-23 20:34:30 +020048 if( msg != NULL && test_info.failed == 0 )
49 test_fail( msg, line, file );
50
Gilles Peskinedd413d32019-05-28 15:06:43 +020051 return( msg == NULL );
52}
53
Gilles Peskine3cff7682019-06-20 12:54:43 +020054/** Check that no PSA Crypto key slots are in use.
Gilles Peskinedd413d32019-05-28 15:06:43 +020055 */
56#define ASSERT_PSA_PRISTINE( ) \
57 do \
58 { \
59 if( ! test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \
60 goto exit; \
61 } \
62 while( 0 )
63
64static void test_helper_psa_done( int line, const char *file )
65{
66 (void) test_helper_is_psa_pristine( line, file );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020067 mbedtls_psa_crypto_free( );
68}
69
Gilles Peskine3cff7682019-06-20 12:54:43 +020070/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
Gilles Peskinea6d252a2019-05-23 20:34:30 +020071 * in use.
72 */
73#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
74
Gilles Peskine1838e822019-06-20 12:40:56 +020075#endif /* PSA_CRYPTO_HELPERS_H */