blob: 147929e5a3cf5a2e69c95f76b96da0ad987cc4cf [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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020029# include <psa/crypto.h>
Gilles Peskine514a8fd2020-11-13 17:41:53 +010030
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine313ffb82021-02-14 12:51:14 +010032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033# include <psa_crypto_storage.h>
Gilles Peskine313ffb82021-02-14 12:51:14 +010034
35static mbedtls_svc_key_id_t key_ids_used_in_test[9];
36static size_t num_key_ids_used;
37
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)
Gilles Peskine313ffb82021-02-14 12:51:14 +010039{
40 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id) >
42 PSA_MAX_PERSISTENT_KEY_IDENTIFIER) {
Gilles Peskine313ffb82021-02-14 12:51:14 +010043 /* Don't touch key id values that designate non-key files. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010045 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046 for (i = 0; i < num_key_ids_used; i++) {
47 if (mbedtls_svc_key_id_equal(key_id, key_ids_used_in_test[i]))
48 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010049 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050 if (num_key_ids_used == ARRAY_LENGTH(key_ids_used_in_test))
51 return 0;
Gilles Peskine313ffb82021-02-14 12:51:14 +010052 key_ids_used_in_test[num_key_ids_used] = key_id;
53 ++num_key_ids_used;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010055}
56
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057void mbedtls_test_psa_purge_key_storage(void)
Gilles Peskine313ffb82021-02-14 12:51:14 +010058{
59 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060 for (i = 0; i < num_key_ids_used; i++)
61 psa_destroy_persistent_key(key_ids_used_in_test[i]);
Gilles Peskine313ffb82021-02-14 12:51:14 +010062 num_key_ids_used = 0;
63}
Gilles Peskineaae718c2021-02-14 13:46:39 +010064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065void mbedtls_test_psa_purge_key_cache(void)
Gilles Peskineaae718c2021-02-14 13:46:39 +010066{
67 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068 for (i = 0; i < num_key_ids_used; i++)
69 psa_purge_key(key_ids_used_in_test[i]);
Gilles Peskineaae718c2021-02-14 13:46:39 +010070}
71
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072# endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine313ffb82021-02-14 12:51:14 +010073
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020074const char *mbedtls_test_helper_is_psa_leaking(void)
Gilles Peskined4008d52020-11-24 17:34:30 +010075{
76 mbedtls_psa_stats_t stats;
77
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078 mbedtls_psa_get_stats(&stats);
Gilles Peskined4008d52020-11-24 17:34:30 +010079
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080 if (stats.volatile_slots != 0)
81 return "A volatile slot has not been closed properly.";
82 if (stats.persistent_slots != 0)
83 return "A persistent slot has not been closed properly.";
84 if (stats.external_slots != 0)
85 return "An external slot has not been closed properly.";
86 if (stats.half_filled_slots != 0)
87 return "A half-filled slot has not been cleared properly.";
88 if (stats.locked_slots != 0)
89 return "Some slots are still marked as locked.";
Gilles Peskined4008d52020-11-24 17:34:30 +010090
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091 return NULL;
Gilles Peskined4008d52020-11-24 17:34:30 +010092}
93
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094# if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
Gilles Peskined4008d52020-11-24 17:34:30 +010095/** Name of the file where return statuses are logged by #RECORD_STATUS. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096# define STATUS_LOG_FILE_NAME "statuses.log"
Gilles Peskined4008d52020-11-24 17:34:30 +010097
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098psa_status_t mbedtls_test_record_status(psa_status_t status,
99 const char *func,
100 const char *file,
101 int line,
102 const char *expr)
Gilles Peskined4008d52020-11-24 17:34:30 +0100103{
104 /* We open the log file on first use.
105 * We never close the log file, so the record_status feature is not
106 * compatible with resource leak detectors such as Asan.
107 */
108 static FILE *log;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 if (log == NULL)
110 log = fopen(STATUS_LOG_FILE_NAME, "a");
111 fprintf(log, "%d:%s:%s:%d:%s\n", (int)status, func, file, line, expr);
112 return status;
Gilles Peskined4008d52020-11-24 17:34:30 +0100113}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114# endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
Gilles Peskined4008d52020-11-24 17:34:30 +0100115
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200117{
118 psa_key_usage_t updated_usage = usage_flags;
119
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120 if (usage_flags & PSA_KEY_USAGE_SIGN_HASH)
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200121 updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
122
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200123 if (usage_flags & PSA_KEY_USAGE_VERIFY_HASH)
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200124 updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
125
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126 return updated_usage;
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200127}
128
Gilles Peskine514a8fd2020-11-13 17:41:53 +0100129#endif /* MBEDTLS_PSA_CRYPTO_C */