blob: 305ad6ec468aeb0ad1aadfae77793e5b38d1095f [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
Paul Elliott838886d2024-03-12 15:26:04 +000068static uint8_t psa_get_key_slots_initialized(void)
69{
Paul Elliott838886d2024-03-12 15:26:04 +000070 uint8_t initialized;
71
72#if defined(MBEDTLS_THREADING_C)
73 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
74#endif /* defined(MBEDTLS_THREADING_C) */
75
76 initialized = global_data.key_slots_initialized;
77
78#if defined(MBEDTLS_THREADING_C)
79 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
80#endif /* defined(MBEDTLS_THREADING_C) */
81
82 return initialized;
83}
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
90 (key_id <= PSA_KEY_ID_USER_MAX)) {
91 return 1;
92 }
Ronald Crond2ed4812020-07-17 16:11:30 +020093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (vendor_ok &&
95 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
96 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
97 return 1;
98 }
Ronald Crond2ed4812020-07-17 16:11:30 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +0200101}
102
Ronald Cron5c522922020-11-14 16:35:34 +0100103/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200104 *
Ronald Cron5c522922020-11-14 16:35:34 +0100105 * The descriptions of volatile keys and loaded persistent keys are
106 * stored in key slots. This function returns a pointer to the key slot
107 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200108 *
Ronald Cron5c522922020-11-14 16:35:34 +0100109 * The function searches the key slots containing the description of the key
110 * with \p key identifier. The function does only read accesses to the key
111 * slots. The function does not load any persistent key thus does not access
112 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200113 *
Ronald Cron5c522922020-11-14 16:35:34 +0100114 * For volatile key identifiers, only one key slot is queried as a volatile
115 * key with identifier key_id can only be stored in slot of index
116 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +0200117 *
Ronald Cron5c522922020-11-14 16:35:34 +0100118 * On success, the function locks the key slot. It is the responsibility of
119 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200120 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000121 * If multi-threading is enabled, the caller must hold the
122 * global key slot mutex.
123 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200124 * \param key Key identifier to query.
125 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
126 * key slot containing the description of the key
127 * identified by \p key.
128 *
Ronald Cron96783552020-10-19 12:06:30 +0200129 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200130 * The pointer to the key slot containing the description of the key
131 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200132 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200133 * \p key is not a valid key identifier.
134 * \retval #PSA_ERROR_DOES_NOT_EXIST
135 * There is no key with key identifier \p key in the key slots.
136 */
Ronald Cron5c522922020-11-14 16:35:34 +0100137static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100139{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100140 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100142 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200143 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (psa_key_id_is_volatile(key_id)) {
146 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100147
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000148 /* Check if both the PSA key identifier key_id and the owner
149 * identifier of key match those of the key slot. */
150 if ((slot->state == PSA_SLOT_FULL) &&
151 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
152 status = PSA_SUCCESS;
153 } else {
154 status = PSA_ERROR_DOES_NOT_EXIST;
155 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 } else {
157 if (!psa_is_valid_key_id(key, 1)) {
158 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200159 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100160
161 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
162 slot = &global_data.key_slots[slot_idx];
Ryan Everett098c6652024-01-03 13:03:36 +0000163 /* Only consider slots which are in a full state. */
164 if ((slot->state == PSA_SLOT_FULL) &&
165 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 break;
167 }
168 }
169 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100170 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200171 }
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000174 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100176 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200178 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200181}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100184{
Ryan Everett558da2f2024-01-19 12:59:28 +0000185 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100186 * guarantee that the key slots are initialized to all-zero, which
Paul Elliott838886d2024-03-12 15:26:04 +0000187 * means that all the key slots are in a valid, empty state. The global
188 * data mutex is already held when calling this function, so no need to
189 * lock it here, to set the flag. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100190 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100192}
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100195{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200196 size_t slot_idx;
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
199 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6cd2b8d2024-01-04 12:10:18 +0000200 slot->registered_readers = 1;
201 slot->state = PSA_SLOT_PENDING_DELETION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100203 }
Paul Elliott838886d2024-03-12 15:26:04 +0000204 /* The global data mutex is already held when calling this function. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100205 global_data.key_slots_initialized = 0;
206}
207
Ryan Everett2afb5162023-12-22 15:59:45 +0000208psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
209 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100210{
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200212 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000213 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200214
Paul Elliott838886d2024-03-12 15:26:04 +0000215 if (!psa_get_key_slots_initialized()) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200216 status = PSA_ERROR_BAD_STATE;
217 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100218 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200219
Ryan Everett2afb5162023-12-22 15:59:45 +0000220 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
222 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett2afb5162023-12-22 15:59:45 +0000223 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200224 selected_slot = slot;
225 break;
226 }
227
Ryan Everett2afb5162023-12-22 15:59:45 +0000228 if ((unused_persistent_key_slot == NULL) &&
229 (slot->state == PSA_SLOT_FULL) &&
230 (!psa_key_slot_has_readers(slot)) &&
231 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
232 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200234 }
235
236 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100237 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100238 * slot containing the description of a persistent key, recycle the first
239 * such key slot we encountered. If we later need to operate on the
240 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100241 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000244 (unused_persistent_key_slot != NULL)) {
245 selected_slot = unused_persistent_key_slot;
246 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000247 status = psa_wipe_key_slot(selected_slot);
248 if (status != PSA_SUCCESS) {
249 goto error;
250 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200251 }
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000254 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
255 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000257 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100259
Ronald Crona5b894f2020-10-21 09:04:34 +0200260 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200262 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200265 }
266 status = PSA_ERROR_INSUFFICIENT_MEMORY;
267
268error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200269 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200270 *volatile_key_id = 0;
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100273}
274
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100275#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100276static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100277{
278 psa_status_t status = PSA_SUCCESS;
279 uint8_t *key_data = NULL;
280 size_t key_data_length = 0;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 status = psa_load_persistent_key(&slot->attr,
283 &key_data, &key_data_length);
284 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200287
Steven Cooreman98435dd2021-01-08 19:19:40 +0100288#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100289 /* Special handling is required for loading keys associated with a
290 * dynamically registered SE interface. */
291 const psa_drv_se_t *drv;
292 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100294 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100297 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100298 goto exit;
299 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000300 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000301 status = psa_copy_key_material_into_slot(
302 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000303 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200304 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000308 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000309 goto exit;
310 }
311
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 psa_free_persistent_key_data(key_data, key_data_length);
314 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100315}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200316#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
317
Steven Cooreman6801f082021-02-19 17:21:22 +0100318#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100321{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100324 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100325 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100326 size_t key_buffer_size = 0;
327 size_t key_buffer_length = 0;
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (!psa_key_id_is_builtin(
330 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
331 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100332 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100333
334 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100335 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 slot->attr.id, &lifetime, &slot_number);
337 if (status != PSA_SUCCESS) {
338 return status;
339 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100340
Steven Cooreman966db262021-04-13 13:45:45 +0200341 /* Set required key attributes to ensure get_builtin_key can retrieve the
342 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 psa_set_key_id(&attributes, slot->attr.id);
344 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100345
Steven Cooremance487022021-04-07 18:09:53 +0200346 /* Get the full key attributes from the driver in order to be able to
347 * calculate the required buffer size. */
348 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 slot_number, &attributes,
350 NULL, 0, NULL);
351 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200352 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200354 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 }
356 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200357 }
358
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200359 /* If the key should exist according to the platform, then ask the driver
360 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
362 &key_buffer_size);
363 if (status != PSA_SUCCESS) {
364 return status;
365 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100366
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200367 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200368 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
370 if (status != PSA_SUCCESS) {
371 return status;
372 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100373
374 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 slot_number, &attributes,
376 slot->key.data, slot->key.bytes, &key_buffer_length);
377 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100378 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100380
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200381 /* Copy actual key length and core attributes into the slot on success */
382 slot->key.bytes = key_buffer_length;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100383 slot->attr = attributes;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (status != PSA_SUCCESS) {
386 psa_remove_key_data_from_memory(slot);
387 }
388 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100389}
390#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
393 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200394{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200396
397 *p_slot = NULL;
Paul Elliott838886d2024-03-12 15:26:04 +0000398 if (!psa_get_key_slots_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return PSA_ERROR_BAD_STATE;
400 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200401
Ryan Everett2f1f1722024-01-31 13:31:00 +0000402#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000403 /* We need to set status as success, otherwise CORRUPTION_DETECTED
404 * would be returned if the lock fails. */
405 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000406 /* If the key is persistent and not loaded, we cannot unlock the mutex
407 * between checking if the key is loaded and setting the slot as FULL,
408 * as otherwise another thread may load and then destroy the key
409 * in the meantime. */
410 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
411 &mbedtls_threading_key_slot_mutex));
412#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200413 /*
414 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100415 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
418 if (status != PSA_ERROR_DOES_NOT_EXIST) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000419#if defined(MBEDTLS_THREADING_C)
420 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
421 &mbedtls_threading_key_slot_mutex));
422#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return status;
424 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200425
Steven Cooreman0bb65362021-04-06 15:09:57 +0200426 /* Loading keys from storage requires support for such a mechanism */
427#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
428 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200429 psa_key_id_t volatile_key_id;
430
Ryan Everett098c6652024-01-03 13:03:36 +0000431 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000433#if defined(MBEDTLS_THREADING_C)
434 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
435 &mbedtls_threading_key_slot_mutex));
436#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return status;
438 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200439
Ronald Cronc4d1b512020-07-31 11:26:37 +0200440 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100441 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200442
Steven Cooreman6801f082021-02-19 17:21:22 +0100443 status = PSA_ERROR_DOES_NOT_EXIST;
444#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200445 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100447#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
448
449#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (status == PSA_ERROR_DOES_NOT_EXIST) {
451 status = psa_load_persistent_key_into_slot(*p_slot);
452 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100453#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (status != PSA_SUCCESS) {
456 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000457
Ryan Everett1a3573e2024-04-29 18:29:48 +0100458 /* If the key does not exist, we need to return
459 * PSA_ERROR_INVALID_HANDLE. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000461 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 }
463 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200464 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000466
467 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
468 PSA_SLOT_FULL);
469 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200471
Steven Cooreman0bb65362021-04-06 15:09:57 +0200472#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000473 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200474#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000475
Ryan Everettd4ea40d2024-04-29 18:24:58 +0100476 if (status != PSA_SUCCESS) {
477 *p_slot = NULL;
478 }
Ryan Everett2f1f1722024-01-31 13:31:00 +0000479#if defined(MBEDTLS_THREADING_C)
480 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
481 &mbedtls_threading_key_slot_mutex));
482#endif
483 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200484}
485
Ryan Everett39cc9d72023-12-21 17:57:14 +0000486psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200487{
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (slot == NULL) {
489 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200490 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000491 if ((slot->state != PSA_SLOT_FULL) &&
492 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000493 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000494 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200495
Ryan Everett39cc9d72023-12-21 17:57:14 +0000496 /* If we are the last reader and the slot is marked for deletion,
497 * we must wipe the slot here. */
498 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
499 (slot->registered_readers == 1)) {
500 return psa_wipe_key_slot(slot);
501 }
502
503 if (psa_key_slot_has_readers(slot)) {
504 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return PSA_SUCCESS;
506 }
507
508 /*
509 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000510 * do our best to report if there are no registered readers. Assert with
511 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
512 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 * the function is called as part of the execution of a test suite, the
514 * execution of the test suite is stopped in error if the assertion fails.
515 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000516 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200518}
519
Ryan Everetteb1722a2024-01-31 13:36:39 +0000520psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
521{
522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
523#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000524 /* We need to set status as success, otherwise CORRUPTION_DETECTED
525 * would be returned if the lock fails. */
526 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000527 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
528 &mbedtls_threading_key_slot_mutex));
529#endif
530 status = psa_unregister_read(slot);
531#if defined(MBEDTLS_THREADING_C)
532 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
533 &mbedtls_threading_key_slot_mutex));
534#endif
535 return status;
536}
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
539 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200540{
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200542#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100543 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
545 if (driver != NULL) {
546 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200547 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 }
549 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200550 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100551#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200552 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100553#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
554
Steven Cooreman98435dd2021-01-08 19:19:40 +0100555 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200558 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return PSA_SUCCESS;
560 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200561}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200564{
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200566 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return PSA_SUCCESS;
568 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200569 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200570#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
572 return PSA_ERROR_INVALID_ARGUMENT;
573 } else {
574 return PSA_SUCCESS;
575 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200576#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200578#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200579 }
Gilles Peskined167b942019-04-19 18:19:40 +0200580}
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100583{
Archana0dc86b52021-07-14 13:59:48 +0530584#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
585 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100586 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200587 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 status = psa_get_and_lock_key_slot(key, &slot);
590 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200591 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000593 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100597 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200598
599 *handle = key;
600
Ryan Everette110a4c2024-02-22 10:43:03 +0000601 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200602
Archana0dc86b52021-07-14 13:59:48 +0530603#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200604 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200605 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530607#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100611{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +0200613 psa_key_slot_t *slot;
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (psa_key_handle_is_null(handle)) {
616 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000617 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100618
Ryan Everett3af9bc12024-01-30 17:21:57 +0000619#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000620 /* We need to set status as success, otherwise CORRUPTION_DETECTED
621 * would be returned if the lock fails. */
622 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +0000623 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
624 &mbedtls_threading_key_slot_mutex));
625#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
627 if (status != PSA_SUCCESS) {
628 if (status == PSA_ERROR_DOES_NOT_EXIST) {
629 status = PSA_ERROR_INVALID_HANDLE;
630 }
Ryan Everett3af9bc12024-01-30 17:21:57 +0000631#if defined(MBEDTLS_THREADING_C)
632 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
633 &mbedtls_threading_key_slot_mutex));
634#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return status;
636 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000637
Ryan Everettc70ce572024-01-03 16:04:33 +0000638 if (slot->registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +0000639 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000640 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +0000641 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000643#if defined(MBEDTLS_THREADING_C)
644 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
645 &mbedtls_threading_key_slot_mutex));
646#endif
647
648 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100649}
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200652{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +0200654 psa_key_slot_t *slot;
655
Ryan Everettb0821952024-01-24 11:42:32 +0000656#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000657 /* We need to set status as success, otherwise CORRUPTION_DETECTED
658 * would be returned if the lock fails. */
659 status = PSA_SUCCESS;
Ryan Everettb0821952024-01-24 11:42:32 +0000660 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
661 &mbedtls_threading_key_slot_mutex));
662#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +0000663 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
664 if (status != PSA_SUCCESS) {
665#if defined(MBEDTLS_THREADING_C)
666 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
667 &mbedtls_threading_key_slot_mutex));
668#endif
669 return status;
670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Ryan Everettc70ce572024-01-03 16:04:33 +0000673 (slot->registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +0000674 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000675 } else {
Ryan Everettb0821952024-01-24 11:42:32 +0000676 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 }
Ryan Everettb0821952024-01-24 11:42:32 +0000678#if defined(MBEDTLS_THREADING_C)
679 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
680 &mbedtls_threading_key_slot_mutex));
681#endif
682
683 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +0200684}
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200687{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200688 size_t slot_idx;
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
693 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000694 if (psa_key_slot_has_readers(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100695 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100696 }
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000697 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200698 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200699 continue;
700 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200702 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 } else {
704 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200705 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100707 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200709 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
711 PSA_KEY_LOCATION_LOCAL_STORAGE) {
712 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200713 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100715 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200717 }
718 }
719}
720
Gilles Peskine961849f2018-11-30 18:54:54 +0100721#endif /* MBEDTLS_PSA_CRYPTO_C */