1 /** \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 OR GPL-2.0-or-later
9 */
10
11 #include <test/helpers.h>
12 #include <test/macros.h>
13 #include <psa_crypto_slot_management.h>
14 #include <test/psa_crypto_helpers.h>
15
16 #if defined(MBEDTLS_PSA_CRYPTO_C)
17
18 #include <psa/crypto.h>
19
20 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
21
22 #include <psa_crypto_storage.h>
23
24 static mbedtls_svc_key_id_t key_ids_used_in_test[9];
25 static size_t num_key_ids_used;
26
mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)27 int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)
28 {
29 size_t i;
30 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id) >
31 PSA_MAX_PERSISTENT_KEY_IDENTIFIER) {
32 /* Don't touch key id values that designate non-key files. */
33 return 1;
34 }
35 for (i = 0; i < num_key_ids_used; i++) {
36 if (mbedtls_svc_key_id_equal(key_id, key_ids_used_in_test[i])) {
37 return 1;
38 }
39 }
40 if (num_key_ids_used == ARRAY_LENGTH(key_ids_used_in_test)) {
41 return 0;
42 }
43 key_ids_used_in_test[num_key_ids_used] = key_id;
44 ++num_key_ids_used;
45 return 1;
46 }
47
mbedtls_test_psa_purge_key_storage(void)48 void mbedtls_test_psa_purge_key_storage(void)
49 {
50 size_t i;
51 for (i = 0; i < num_key_ids_used; i++) {
52 psa_destroy_persistent_key(key_ids_used_in_test[i]);
53 }
54 num_key_ids_used = 0;
55 }
56
mbedtls_test_psa_purge_key_cache(void)57 void mbedtls_test_psa_purge_key_cache(void)
58 {
59 size_t i;
60 for (i = 0; i < num_key_ids_used; i++) {
61 psa_purge_key(key_ids_used_in_test[i]);
62 }
63 }
64
65 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
66
mbedtls_test_helper_is_psa_leaking(void)67 const char *mbedtls_test_helper_is_psa_leaking(void)
68 {
69 mbedtls_psa_stats_t stats;
70
71 mbedtls_psa_get_stats(&stats);
72
73 if (stats.volatile_slots != 0) {
74 return "A volatile slot has not been closed properly.";
75 }
76 if (stats.persistent_slots != 0) {
77 return "A persistent slot has not been closed properly.";
78 }
79 if (stats.external_slots != 0) {
80 return "An external slot has not been closed properly.";
81 }
82 if (stats.half_filled_slots != 0) {
83 return "A half-filled slot has not been cleared properly.";
84 }
85 if (stats.locked_slots != 0) {
86 return "Some slots are still marked as locked.";
87 }
88
89 return NULL;
90 }
91
92 #if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
93 /** Name of the file where return statuses are logged by #RECORD_STATUS. */
94 #define STATUS_LOG_FILE_NAME "statuses.log"
95
mbedtls_test_record_status(psa_status_t status,const char * func,const char * file,int line,const char * expr)96 psa_status_t mbedtls_test_record_status(psa_status_t status,
97 const char *func,
98 const char *file, int line,
99 const char *expr)
100 {
101 /* We open the log file on first use.
102 * We never close the log file, so the record_status feature is not
103 * compatible with resource leak detectors such as Asan.
104 */
105 static FILE *log;
106 if (log == NULL) {
107 log = fopen(STATUS_LOG_FILE_NAME, "a");
108 }
109 fprintf(log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr);
110 return status;
111 }
112 #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
113
mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)114 psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)
115 {
116 psa_key_usage_t updated_usage = usage_flags;
117
118 if (usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
119 updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
120 }
121
122 if (usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
123 updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
124 }
125
126 return updated_usage;
127 }
128
mbedtls_test_fail_if_psa_leaking(int line_no,const char * filename)129 int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename)
130 {
131 const char *msg = mbedtls_test_helper_is_psa_leaking();
132 if (msg == NULL) {
133 return 0;
134 } else {
135 mbedtls_test_fail(msg, line_no, filename);
136 return 1;
137 }
138 }
139
140 #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
141
142 #include <mbedtls/entropy.h>
143 #include <psa_crypto_its.h>
144
mbedtls_test_inject_entropy_seed_read(unsigned char * buf,size_t len)145 int mbedtls_test_inject_entropy_seed_read(unsigned char *buf, size_t len)
146 {
147 size_t actual_len = 0;
148 psa_status_t status = psa_its_get(PSA_CRYPTO_ITS_RANDOM_SEED_UID,
149 0, len, buf, &actual_len);
150 if (status != 0) {
151 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
152 }
153 if (actual_len != len) {
154 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
155 }
156 return 0;
157 }
158
mbedtls_test_inject_entropy_seed_write(unsigned char * buf,size_t len)159 int mbedtls_test_inject_entropy_seed_write(unsigned char *buf, size_t len)
160 {
161 psa_status_t status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID,
162 len, buf, 0);
163 if (status != 0) {
164 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
165 }
166 return 0;
167 }
168
mbedtls_test_inject_entropy_restore(void)169 int mbedtls_test_inject_entropy_restore(void)
170 {
171 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
172 for (size_t i = 0; i < sizeof(buf); i++) {
173 buf[i] = (unsigned char) i;
174 }
175 psa_status_t status = mbedtls_psa_inject_entropy(buf, sizeof(buf));
176 /* It's ok if the file was just created, or if it already exists. */
177 if (status != PSA_SUCCESS && status != PSA_ERROR_NOT_PERMITTED) {
178 return status;
179 }
180 return PSA_SUCCESS;
181 }
182
183 #endif /* MBEDTLS_PSA_INJECT_ENTROPY */
184
185 #endif /* MBEDTLS_PSA_CRYPTO_C */
186