blob: 9b297c9c08a09ab3f00bfecd11786864e78d7abb [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine961849f2018-11-30 18:54:54 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include "psa/crypto.h"
14
Gilles Peskine66fb1262018-12-10 16:29:04 +010015#include "psa_crypto_core.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000016#include "psa_crypto_driver_wrappers_no_static.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010017#include "psa_crypto_slot_management.h"
18#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
20#include "psa_crypto_se.h"
21#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010022
23#include <stdlib.h>
24#include <string.h>
Gilles Peskine961849f2018-11-30 18:54:54 +010025#include "mbedtls/platform.h"
Ryan Everett491f7e52024-01-08 11:04:21 +000026#if defined(MBEDTLS_THREADING_C)
27#include "mbedtls/threading.h"
28#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010029
Gilles Peskineb6bf3702024-06-20 22:15:42 +020030
31
32/* Make sure we have distinct ranges of key identifiers for distinct
33 * purposes. */
34MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MIN < PSA_KEY_ID_USER_MAX,
35 "Empty user key ID range");
36MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN < PSA_KEY_ID_VENDOR_MAX,
37 "Empty vendor key ID range");
38MBEDTLS_STATIC_ASSERT(MBEDTLS_PSA_KEY_ID_BUILTIN_MIN < MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
39 "Empty builtin key ID range");
40MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MIN < PSA_KEY_ID_VOLATILE_MAX,
41 "Empty volatile key ID range");
42
43MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MAX < PSA_KEY_ID_VENDOR_MIN ||
44 PSA_KEY_ID_VENDOR_MAX < PSA_KEY_ID_USER_MIN,
45 "Overlap between user key IDs and vendor key IDs");
46
47MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN &&
48 MBEDTLS_PSA_KEY_ID_BUILTIN_MAX <= PSA_KEY_ID_VENDOR_MAX,
49 "Builtin key identifiers are not in the vendor range");
50
51MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= PSA_KEY_ID_VOLATILE_MIN &&
52 PSA_KEY_ID_VOLATILE_MAX <= PSA_KEY_ID_VENDOR_MAX,
53 "Volatile key identifiers are not in the vendor range");
54
55MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MAX < MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ||
56 MBEDTLS_PSA_KEY_ID_BUILTIN_MAX < PSA_KEY_ID_VOLATILE_MIN,
57 "Overlap between builtin key IDs and volatile key IDs");
58
59
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010062 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Dave Rodgman164614a2023-08-16 17:56:28 +010063 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +010064} psa_global_data_t;
65
Gilles Peskine2e14bd32018-12-12 14:05:08 +010066static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010067
Gilles Peskine708ec092024-07-16 20:02:37 +020068MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
69 PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
Gilles Peskine5eca4022024-08-07 20:08:23 +020070 "The key slot array is larger than the volatile key ID range");
Gilles Peskine708ec092024-07-16 20:02:37 +020071
Paul Elliott838886d2024-03-12 15:26:04 +000072static uint8_t psa_get_key_slots_initialized(void)
73{
Paul Elliott838886d2024-03-12 15:26:04 +000074 uint8_t initialized;
75
76#if defined(MBEDTLS_THREADING_C)
77 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
78#endif /* defined(MBEDTLS_THREADING_C) */
79
80 initialized = global_data.key_slots_initialized;
81
82#if defined(MBEDTLS_THREADING_C)
83 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
84#endif /* defined(MBEDTLS_THREADING_C) */
85
86 return initialized;
87}
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020090{
Gilles Peskine449bd832023-01-11 14:50:10 +010091 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
94 (key_id <= PSA_KEY_ID_USER_MAX)) {
95 return 1;
96 }
Ronald Crond2ed4812020-07-17 16:11:30 +020097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (vendor_ok &&
99 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
100 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
101 return 1;
102 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +0200105}
106
Ronald Cron5c522922020-11-14 16:35:34 +0100107/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200108 *
Ronald Cron5c522922020-11-14 16:35:34 +0100109 * The descriptions of volatile keys and loaded persistent keys are
110 * stored in key slots. This function returns a pointer to the key slot
111 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200112 *
Ronald Cron5c522922020-11-14 16:35:34 +0100113 * The function searches the key slots containing the description of the key
114 * with \p key identifier. The function does only read accesses to the key
115 * slots. The function does not load any persistent key thus does not access
116 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200117 *
Ronald Cron5c522922020-11-14 16:35:34 +0100118 * For volatile key identifiers, only one key slot is queried as a volatile
119 * key with identifier key_id can only be stored in slot of index
120 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +0200121 *
Ronald Cron5c522922020-11-14 16:35:34 +0100122 * On success, the function locks the key slot. It is the responsibility of
123 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200124 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000125 * If multi-threading is enabled, the caller must hold the
126 * global key slot mutex.
127 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200128 * \param key Key identifier to query.
129 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
130 * key slot containing the description of the key
131 * identified by \p key.
132 *
Ronald Cron96783552020-10-19 12:06:30 +0200133 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200134 * The pointer to the key slot containing the description of the key
135 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200136 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200137 * \p key is not a valid key identifier.
138 * \retval #PSA_ERROR_DOES_NOT_EXIST
139 * There is no key with key identifier \p key in the key slots.
140 */
Ronald Cron5c522922020-11-14 16:35:34 +0100141static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100143{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100146 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200147 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (psa_key_id_is_volatile(key_id)) {
150 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100151
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000152 /* Check if both the PSA key identifier key_id and the owner
153 * identifier of key match those of the key slot. */
154 if ((slot->state == PSA_SLOT_FULL) &&
155 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
156 status = PSA_SUCCESS;
157 } else {
158 status = PSA_ERROR_DOES_NOT_EXIST;
159 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 } else {
161 if (!psa_is_valid_key_id(key, 1)) {
162 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200163 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100164
165 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
166 slot = &global_data.key_slots[slot_idx];
Ryan Everett098c6652024-01-03 13:03:36 +0000167 /* Only consider slots which are in a full state. */
168 if ((slot->state == PSA_SLOT_FULL) &&
169 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 break;
171 }
172 }
173 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100174 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000178 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100180 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200182 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200185}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100188{
Ryan Everett558da2f2024-01-19 12:59:28 +0000189 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100190 * guarantee that the key slots are initialized to all-zero, which
Paul Elliott838886d2024-03-12 15:26:04 +0000191 * means that all the key slots are in a valid, empty state. The global
192 * data mutex is already held when calling this function, so no need to
193 * lock it here, to set the flag. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100194 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100196}
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100199{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200200 size_t slot_idx;
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
203 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6cd2b8d2024-01-04 12:10:18 +0000204 slot->registered_readers = 1;
205 slot->state = PSA_SLOT_PENDING_DELETION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100207 }
Paul Elliott838886d2024-03-12 15:26:04 +0000208 /* The global data mutex is already held when calling this function. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100209 global_data.key_slots_initialized = 0;
210}
211
Ryan Everett2afb5162023-12-22 15:59:45 +0000212psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
213 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100214{
Ronald Crona5b894f2020-10-21 09:04:34 +0200215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200216 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000217 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200218
Paul Elliott838886d2024-03-12 15:26:04 +0000219 if (!psa_get_key_slots_initialized()) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200220 status = PSA_ERROR_BAD_STATE;
221 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100222 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200223
Ryan Everett2afb5162023-12-22 15:59:45 +0000224 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
226 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett2afb5162023-12-22 15:59:45 +0000227 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200228 selected_slot = slot;
229 break;
230 }
231
Ryan Everett2afb5162023-12-22 15:59:45 +0000232 if ((unused_persistent_key_slot == NULL) &&
233 (slot->state == PSA_SLOT_FULL) &&
234 (!psa_key_slot_has_readers(slot)) &&
235 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
236 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200238 }
239
240 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100241 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100242 * slot containing the description of a persistent key, recycle the first
243 * such key slot we encountered. If we later need to operate on the
244 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100245 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000248 (unused_persistent_key_slot != NULL)) {
249 selected_slot = unused_persistent_key_slot;
250 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000251 status = psa_wipe_key_slot(selected_slot);
252 if (status != PSA_SUCCESS) {
253 goto error;
254 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200255 }
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000258 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
259 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000261 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100263
Ronald Crona5b894f2020-10-21 09:04:34 +0200264 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200266 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200269 }
270 status = PSA_ERROR_INSUFFICIENT_MEMORY;
271
272error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200273 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200274 *volatile_key_id = 0;
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100277}
278
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100279#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100281{
282 psa_status_t status = PSA_SUCCESS;
283 uint8_t *key_data = NULL;
284 size_t key_data_length = 0;
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 status = psa_load_persistent_key(&slot->attr,
287 &key_data, &key_data_length);
288 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200291
Steven Cooreman98435dd2021-01-08 19:19:40 +0100292#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100293 /* Special handling is required for loading keys associated with a
294 * dynamically registered SE interface. */
295 const psa_drv_se_t *drv;
296 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100298 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100301 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100302 goto exit;
303 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000304 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000305 status = psa_copy_key_material_into_slot(
306 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000307 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200308 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000312 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000313 goto exit;
314 }
315
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 psa_free_persistent_key_data(key_data, key_data_length);
318 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100319}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200320#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
321
Steven Cooreman6801f082021-02-19 17:21:22 +0100322#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100325{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100328 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100329 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100330 size_t key_buffer_size = 0;
331 size_t key_buffer_length = 0;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (!psa_key_id_is_builtin(
334 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
335 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100336 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100337
338 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100339 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 slot->attr.id, &lifetime, &slot_number);
341 if (status != PSA_SUCCESS) {
342 return status;
343 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100344
Steven Cooreman966db262021-04-13 13:45:45 +0200345 /* Set required key attributes to ensure get_builtin_key can retrieve the
346 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 psa_set_key_id(&attributes, slot->attr.id);
348 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100349
Steven Cooremance487022021-04-07 18:09:53 +0200350 /* Get the full key attributes from the driver in order to be able to
351 * calculate the required buffer size. */
352 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 slot_number, &attributes,
354 NULL, 0, NULL);
355 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200356 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200358 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 }
360 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200361 }
362
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200363 /* If the key should exist according to the platform, then ask the driver
364 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
366 &key_buffer_size);
367 if (status != PSA_SUCCESS) {
368 return status;
369 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100370
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200371 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200372 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
374 if (status != PSA_SUCCESS) {
375 return status;
376 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100377
378 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 slot_number, &attributes,
380 slot->key.data, slot->key.bytes, &key_buffer_length);
381 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100382 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100384
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200385 /* Copy actual key length and core attributes into the slot on success */
386 slot->key.bytes = key_buffer_length;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100387 slot->attr = attributes;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100388exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (status != PSA_SUCCESS) {
390 psa_remove_key_data_from_memory(slot);
391 }
392 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100393}
394#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
397 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200398{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200400
401 *p_slot = NULL;
Paul Elliott838886d2024-03-12 15:26:04 +0000402 if (!psa_get_key_slots_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return PSA_ERROR_BAD_STATE;
404 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200405
Ryan Everett2f1f1722024-01-31 13:31:00 +0000406#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000407 /* We need to set status as success, otherwise CORRUPTION_DETECTED
408 * would be returned if the lock fails. */
409 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000410 /* If the key is persistent and not loaded, we cannot unlock the mutex
411 * between checking if the key is loaded and setting the slot as FULL,
412 * as otherwise another thread may load and then destroy the key
413 * in the meantime. */
414 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
415 &mbedtls_threading_key_slot_mutex));
416#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200417 /*
418 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100419 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200420 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
422 if (status != PSA_ERROR_DOES_NOT_EXIST) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000423#if defined(MBEDTLS_THREADING_C)
424 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
425 &mbedtls_threading_key_slot_mutex));
426#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 return status;
428 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200429
Steven Cooreman0bb65362021-04-06 15:09:57 +0200430 /* Loading keys from storage requires support for such a mechanism */
431#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
432 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200433 psa_key_id_t volatile_key_id;
434
Ryan Everett098c6652024-01-03 13:03:36 +0000435 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000437#if defined(MBEDTLS_THREADING_C)
438 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
439 &mbedtls_threading_key_slot_mutex));
440#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return status;
442 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200443
Ronald Cronc4d1b512020-07-31 11:26:37 +0200444 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100445 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200446
Steven Cooreman6801f082021-02-19 17:21:22 +0100447 status = PSA_ERROR_DOES_NOT_EXIST;
448#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200449 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100451#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
452
453#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (status == PSA_ERROR_DOES_NOT_EXIST) {
455 status = psa_load_persistent_key_into_slot(*p_slot);
456 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100457#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (status != PSA_SUCCESS) {
460 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000461
Ryan Everett1a3573e2024-04-29 18:29:48 +0100462 /* If the key does not exist, we need to return
463 * PSA_ERROR_INVALID_HANDLE. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000465 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 }
467 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200468 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000470
471 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
472 PSA_SLOT_FULL);
473 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200475
Steven Cooreman0bb65362021-04-06 15:09:57 +0200476#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000477 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200478#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000479
Ryan Everettd4ea40d2024-04-29 18:24:58 +0100480 if (status != PSA_SUCCESS) {
481 *p_slot = NULL;
482 }
Ryan Everett2f1f1722024-01-31 13:31:00 +0000483#if defined(MBEDTLS_THREADING_C)
484 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
485 &mbedtls_threading_key_slot_mutex));
486#endif
487 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200488}
489
Ryan Everett39cc9d72023-12-21 17:57:14 +0000490psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200491{
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (slot == NULL) {
493 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200494 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000495 if ((slot->state != PSA_SLOT_FULL) &&
496 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000497 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000498 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200499
Ryan Everett39cc9d72023-12-21 17:57:14 +0000500 /* If we are the last reader and the slot is marked for deletion,
501 * we must wipe the slot here. */
502 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
503 (slot->registered_readers == 1)) {
504 return psa_wipe_key_slot(slot);
505 }
506
507 if (psa_key_slot_has_readers(slot)) {
508 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return PSA_SUCCESS;
510 }
511
512 /*
513 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000514 * do our best to report if there are no registered readers. Assert with
515 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
516 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 * the function is called as part of the execution of a test suite, the
518 * execution of the test suite is stopped in error if the assertion fails.
519 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000520 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200522}
523
Ryan Everetteb1722a2024-01-31 13:36:39 +0000524psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
525{
526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
527#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000528 /* We need to set status as success, otherwise CORRUPTION_DETECTED
529 * would be returned if the lock fails. */
530 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000531 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
532 &mbedtls_threading_key_slot_mutex));
533#endif
534 status = psa_unregister_read(slot);
535#if defined(MBEDTLS_THREADING_C)
536 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
537 &mbedtls_threading_key_slot_mutex));
538#endif
539 return status;
540}
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
543 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200544{
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200546#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100547 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
549 if (driver != NULL) {
550 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200551 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 }
553 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200554 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100555#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200556 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100557#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
558
Steven Cooreman98435dd2021-01-08 19:19:40 +0100559 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200562 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return PSA_SUCCESS;
564 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200565}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200568{
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200570 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return PSA_SUCCESS;
572 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200573 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200574#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
576 return PSA_ERROR_INVALID_ARGUMENT;
577 } else {
578 return PSA_SUCCESS;
579 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200580#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200582#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200583 }
Gilles Peskined167b942019-04-19 18:19:40 +0200584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100587{
Archana0dc86b52021-07-14 13:59:48 +0530588#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
589 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100590 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200591 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 status = psa_get_and_lock_key_slot(key, &slot);
594 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200595 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000597 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100601 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200602
603 *handle = key;
604
Ryan Everette110a4c2024-02-22 10:43:03 +0000605 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200606
Archana0dc86b52021-07-14 13:59:48 +0530607#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200608 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200609 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530611#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100612}
613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100615{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +0200617 psa_key_slot_t *slot;
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (psa_key_handle_is_null(handle)) {
620 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000621 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100622
Ryan Everett3af9bc12024-01-30 17:21:57 +0000623#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000624 /* We need to set status as success, otherwise CORRUPTION_DETECTED
625 * would be returned if the lock fails. */
626 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +0000627 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
628 &mbedtls_threading_key_slot_mutex));
629#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
631 if (status != PSA_SUCCESS) {
632 if (status == PSA_ERROR_DOES_NOT_EXIST) {
633 status = PSA_ERROR_INVALID_HANDLE;
634 }
Ryan Everett3af9bc12024-01-30 17:21:57 +0000635#if defined(MBEDTLS_THREADING_C)
636 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
637 &mbedtls_threading_key_slot_mutex));
638#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return status;
640 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000641
Ryan Everettc70ce572024-01-03 16:04:33 +0000642 if (slot->registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +0000643 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000644 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +0000645 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000647#if defined(MBEDTLS_THREADING_C)
648 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
649 &mbedtls_threading_key_slot_mutex));
650#endif
651
652 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100653}
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200656{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +0200658 psa_key_slot_t *slot;
659
Ryan Everettb0821952024-01-24 11:42:32 +0000660#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000661 /* We need to set status as success, otherwise CORRUPTION_DETECTED
662 * would be returned if the lock fails. */
663 status = PSA_SUCCESS;
Ryan Everettb0821952024-01-24 11:42:32 +0000664 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
665 &mbedtls_threading_key_slot_mutex));
666#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +0000667 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
668 if (status != PSA_SUCCESS) {
669#if defined(MBEDTLS_THREADING_C)
670 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
671 &mbedtls_threading_key_slot_mutex));
672#endif
673 return status;
674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Ryan Everettc70ce572024-01-03 16:04:33 +0000677 (slot->registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +0000678 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000679 } else {
Ryan Everettb0821952024-01-24 11:42:32 +0000680 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 }
Ryan Everettb0821952024-01-24 11:42:32 +0000682#if defined(MBEDTLS_THREADING_C)
683 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
684 &mbedtls_threading_key_slot_mutex));
685#endif
686
687 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +0200688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200691{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200692 size_t slot_idx;
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
697 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000698 if (psa_key_slot_has_readers(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100699 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100700 }
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000701 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200702 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200703 continue;
704 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200706 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 } else {
708 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200709 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100711 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200713 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
715 PSA_KEY_LOCATION_LOCAL_STORAGE) {
716 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200717 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100719 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200721 }
722 }
723}
724
Gilles Peskine961849f2018-11-30 18:54:54 +0100725#endif /* MBEDTLS_PSA_CRYPTO_C */