Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 1 | /* |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 2 | * Helper functions for tests that use the PSA Crypto API. |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 3 | */ |
| 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 Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 22 | #ifndef PSA_CRYPTO_HELPERS_H |
| 23 | #define PSA_CRYPTO_HELPERS_H |
| 24 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 25 | #include "psa_helpers.h" |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 26 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 27 | #include <psa/crypto.h> |
Gilles Peskine | 952f409 | 2019-05-23 20:25:48 +0200 | [diff] [blame] | 28 | |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 29 | static int test_helper_is_psa_pristine( int line, const char *file ) |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 30 | { |
| 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 Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 45 | /* 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 Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 48 | if( msg != NULL && test_info.failed == 0 ) |
| 49 | test_fail( msg, line, file ); |
| 50 | |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 51 | return( msg == NULL ); |
| 52 | } |
| 53 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 54 | /** Check that no PSA Crypto key slots are in use. |
Gilles Peskine | dd413d3 | 2019-05-28 15:06:43 +0200 | [diff] [blame] | 55 | */ |
| 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 | |
| 64 | static void test_helper_psa_done( int line, const char *file ) |
| 65 | { |
| 66 | (void) test_helper_is_psa_pristine( line, file ); |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 67 | mbedtls_psa_crypto_free( ); |
| 68 | } |
| 69 | |
Gilles Peskine | 3cff768 | 2019-06-20 12:54:43 +0200 | [diff] [blame] | 70 | /** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots |
Gilles Peskine | a6d252a | 2019-05-23 20:34:30 +0200 | [diff] [blame] | 71 | * in use. |
| 72 | */ |
| 73 | #define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ ) |
| 74 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 75 | #endif /* PSA_CRYPTO_HELPERS_H */ |