blob: 1581eecb3bb086ac21a0871b911bae0d6e305777 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine514a8fd2020-11-13 17:41:53 +01009 */
10
11#include <test/helpers.h>
12#include <test/macros.h>
Przemyslaw Stekiel53de2622021-11-03 09:35:35 +010013#include <psa_crypto_slot_management.h>
Gilles Peskined4008d52020-11-24 17:34:30 +010014#include <test/psa_crypto_helpers.h>
Gilles Peskine514a8fd2020-11-13 17:41:53 +010015
Gilles Peskine48048472024-06-20 21:47:31 +020016#if defined(MBEDTLS_CTR_DRBG_C)
17#include <mbedtls/ctr_drbg.h>
18#endif
19
Gilles Peskine514a8fd2020-11-13 17:41:53 +010020#if defined(MBEDTLS_PSA_CRYPTO_C)
21
22#include <psa/crypto.h>
23
Gilles Peskine313ffb82021-02-14 12:51:14 +010024#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
25
26#include <psa_crypto_storage.h>
27
28static mbedtls_svc_key_id_t key_ids_used_in_test[9];
29static size_t num_key_ids_used;
30
Gilles Peskine449bd832023-01-11 14:50:10 +010031int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)
Gilles Peskine313ffb82021-02-14 12:51:14 +010032{
33 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010034 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id) >
35 PSA_MAX_PERSISTENT_KEY_IDENTIFIER) {
Gilles Peskine313ffb82021-02-14 12:51:14 +010036 /* Don't touch key id values that designate non-key files. */
Gilles Peskine449bd832023-01-11 14:50:10 +010037 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010038 }
Gilles Peskine449bd832023-01-11 14:50:10 +010039 for (i = 0; i < num_key_ids_used; i++) {
40 if (mbedtls_svc_key_id_equal(key_id, key_ids_used_in_test[i])) {
41 return 1;
42 }
Gilles Peskine313ffb82021-02-14 12:51:14 +010043 }
Gilles Peskine449bd832023-01-11 14:50:10 +010044 if (num_key_ids_used == ARRAY_LENGTH(key_ids_used_in_test)) {
45 return 0;
46 }
Gilles Peskine313ffb82021-02-14 12:51:14 +010047 key_ids_used_in_test[num_key_ids_used] = key_id;
48 ++num_key_ids_used;
Gilles Peskine449bd832023-01-11 14:50:10 +010049 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052void mbedtls_test_psa_purge_key_storage(void)
Gilles Peskine313ffb82021-02-14 12:51:14 +010053{
54 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 for (i = 0; i < num_key_ids_used; i++) {
56 psa_destroy_persistent_key(key_ids_used_in_test[i]);
57 }
Gilles Peskine313ffb82021-02-14 12:51:14 +010058 num_key_ids_used = 0;
59}
Gilles Peskineaae718c2021-02-14 13:46:39 +010060
Gilles Peskine449bd832023-01-11 14:50:10 +010061void mbedtls_test_psa_purge_key_cache(void)
Gilles Peskineaae718c2021-02-14 13:46:39 +010062{
63 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010064 for (i = 0; i < num_key_ids_used; i++) {
65 psa_purge_key(key_ids_used_in_test[i]);
66 }
Gilles Peskineaae718c2021-02-14 13:46:39 +010067}
68
Gilles Peskine313ffb82021-02-14 12:51:14 +010069#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071const char *mbedtls_test_helper_is_psa_leaking(void)
Gilles Peskined4008d52020-11-24 17:34:30 +010072{
73 mbedtls_psa_stats_t stats;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_psa_get_stats(&stats);
Gilles Peskined4008d52020-11-24 17:34:30 +010076
Gilles Peskine48048472024-06-20 21:47:31 +020077#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
78 defined(MBEDTLS_CTR_DRBG_C) && \
79 defined(MBEDTLS_CTR_DRBG_USE_PSA_CRYPTO)
Valerio Setti74483672023-11-24 08:36:12 +010080 /* When AES_C is not defined and PSA does not have an external RNG,
81 * then CTR_DRBG uses PSA to perform AES-ECB. In this scenario 1 key
82 * slot is used internally from PSA to hold the AES key and it should
83 * not be taken into account when evaluating remaining open slots. */
84 if (stats.volatile_slots > 1) {
85 return "A volatile slot has not been closed properly.";
86 }
87#else
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (stats.volatile_slots != 0) {
89 return "A volatile slot has not been closed properly.";
90 }
Valerio Setti74483672023-11-24 08:36:12 +010091#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (stats.persistent_slots != 0) {
93 return "A persistent slot has not been closed properly.";
94 }
95 if (stats.external_slots != 0) {
96 return "An external slot has not been closed properly.";
97 }
98 if (stats.half_filled_slots != 0) {
99 return "A half-filled slot has not been cleared properly.";
100 }
101 if (stats.locked_slots != 0) {
102 return "Some slots are still marked as locked.";
103 }
Gilles Peskined4008d52020-11-24 17:34:30 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return NULL;
Gilles Peskined4008d52020-11-24 17:34:30 +0100106}
107
108#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
109/** Name of the file where return statuses are logged by #RECORD_STATUS. */
110#define STATUS_LOG_FILE_NAME "statuses.log"
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112psa_status_t mbedtls_test_record_status(psa_status_t status,
113 const char *func,
114 const char *file, int line,
115 const char *expr)
Gilles Peskined4008d52020-11-24 17:34:30 +0100116{
117 /* We open the log file on first use.
118 * We never close the log file, so the record_status feature is not
119 * compatible with resource leak detectors such as Asan.
120 */
121 static FILE *log;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (log == NULL) {
123 log = fopen(STATUS_LOG_FILE_NAME, "a");
124 }
125 fprintf(log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr);
126 return status;
Gilles Peskined4008d52020-11-24 17:34:30 +0100127}
128#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200131{
132 psa_key_usage_t updated_usage = usage_flags;
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200135 updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 }
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200139 updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 }
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return updated_usage;
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200143}
144
Yanray Wange64b4052022-10-28 18:12:01 +0800145int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename)
146{
147 const char *msg = mbedtls_test_helper_is_psa_leaking();
148 if (msg == NULL) {
149 return 0;
150 } else {
151 mbedtls_test_fail(msg, line_no, filename);
152 return 1;
153 }
154}
155
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +0530156uint64_t mbedtls_test_parse_binary_string(data_t *bin_string)
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530157{
158 uint64_t result = 0;
159 TEST_LE_U(bin_string->len, 8);
160 for (size_t i = 0; i < bin_string->len; i++) {
161 result = result << 8 | bin_string->x[i];
162 }
163exit:
164 return result; /* returns 0 if len > 8 */
165}
166
Gilles Peskinea08def92023-04-28 21:01:49 +0200167#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
168
169#include <mbedtls/entropy.h>
170#include <psa_crypto_its.h>
171
172int mbedtls_test_inject_entropy_seed_read(unsigned char *buf, size_t len)
173{
174 size_t actual_len = 0;
175 psa_status_t status = psa_its_get(PSA_CRYPTO_ITS_RANDOM_SEED_UID,
176 0, len, buf, &actual_len);
177 if (status != 0) {
178 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
179 }
180 if (actual_len != len) {
181 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
182 }
183 return 0;
184}
185
186int mbedtls_test_inject_entropy_seed_write(unsigned char *buf, size_t len)
187{
188 psa_status_t status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID,
189 len, buf, 0);
190 if (status != 0) {
191 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
192 }
193 return 0;
194}
195
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200196int mbedtls_test_inject_entropy_restore(void)
197{
198 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
199 for (size_t i = 0; i < sizeof(buf); i++) {
200 buf[i] = (unsigned char) i;
201 }
202 psa_status_t status = mbedtls_psa_inject_entropy(buf, sizeof(buf));
203 /* It's ok if the file was just created, or if it already exists. */
204 if (status != PSA_SUCCESS && status != PSA_ERROR_NOT_PERMITTED) {
205 return status;
206 }
207 return PSA_SUCCESS;
208}
209
Gilles Peskinea08def92023-04-28 21:01:49 +0200210#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
211
Gilles Peskine514a8fd2020-11-13 17:41:53 +0100212#endif /* MBEDTLS_PSA_CRYPTO_C */