blob: fbcb26ebc8ad2c29558010f3745818a7b840df64 [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 Peskine449bd832023-01-11 14:50:10 +010030typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010031 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Dave Rodgman164614a2023-08-16 17:56:28 +010032 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +010033} psa_global_data_t;
34
Gilles Peskine2e14bd32018-12-12 14:05:08 +010035static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010036
Paul Elliott838886d2024-03-12 15:26:04 +000037static uint8_t psa_get_key_slots_initialized(void)
38{
Paul Elliott838886d2024-03-12 15:26:04 +000039 uint8_t initialized;
40
41#if defined(MBEDTLS_THREADING_C)
42 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
43#endif /* defined(MBEDTLS_THREADING_C) */
44
45 initialized = global_data.key_slots_initialized;
46
47#if defined(MBEDTLS_THREADING_C)
48 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
49#endif /* defined(MBEDTLS_THREADING_C) */
50
51 return initialized;
52}
53
Gilles Peskine449bd832023-01-11 14:50:10 +010054int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020055{
Gilles Peskine449bd832023-01-11 14:50:10 +010056 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
59 (key_id <= PSA_KEY_ID_USER_MAX)) {
60 return 1;
61 }
Ronald Crond2ed4812020-07-17 16:11:30 +020062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (vendor_ok &&
64 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
65 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
66 return 1;
67 }
Ronald Crond2ed4812020-07-17 16:11:30 +020068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020070}
71
Ronald Cron5c522922020-11-14 16:35:34 +010072/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020073 *
Ronald Cron5c522922020-11-14 16:35:34 +010074 * The descriptions of volatile keys and loaded persistent keys are
75 * stored in key slots. This function returns a pointer to the key slot
76 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020077 *
Ronald Cron5c522922020-11-14 16:35:34 +010078 * The function searches the key slots containing the description of the key
79 * with \p key identifier. The function does only read accesses to the key
80 * slots. The function does not load any persistent key thus does not access
81 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020082 *
Ronald Cron5c522922020-11-14 16:35:34 +010083 * For volatile key identifiers, only one key slot is queried as a volatile
84 * key with identifier key_id can only be stored in slot of index
85 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020086 *
Ronald Cron5c522922020-11-14 16:35:34 +010087 * On success, the function locks the key slot. It is the responsibility of
88 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020089 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +000090 * If multi-threading is enabled, the caller must hold the
91 * global key slot mutex.
92 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020093 * \param key Key identifier to query.
94 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
95 * key slot containing the description of the key
96 * identified by \p key.
97 *
Ronald Cron96783552020-10-19 12:06:30 +020098 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020099 * The pointer to the key slot containing the description of the key
100 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200101 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200102 * \p key is not a valid key identifier.
103 * \retval #PSA_ERROR_DOES_NOT_EXIST
104 * There is no key with key identifier \p key in the key slots.
105 */
Ronald Cron5c522922020-11-14 16:35:34 +0100106static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100108{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100111 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200112 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (psa_key_id_is_volatile(key_id)) {
115 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100116
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000117 /* Check if both the PSA key identifier key_id and the owner
118 * identifier of key match those of the key slot. */
119 if ((slot->state == PSA_SLOT_FULL) &&
120 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
121 status = PSA_SUCCESS;
122 } else {
123 status = PSA_ERROR_DOES_NOT_EXIST;
124 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 } else {
126 if (!psa_is_valid_key_id(key, 1)) {
127 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200128 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100129
130 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
131 slot = &global_data.key_slots[slot_idx];
Ryan Everett098c6652024-01-03 13:03:36 +0000132 /* Only consider slots which are in a full state. */
133 if ((slot->state == PSA_SLOT_FULL) &&
134 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 break;
136 }
137 }
138 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100139 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200140 }
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000143 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100145 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200147 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200150}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100153{
Ryan Everett558da2f2024-01-19 12:59:28 +0000154 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100155 * guarantee that the key slots are initialized to all-zero, which
Paul Elliott838886d2024-03-12 15:26:04 +0000156 * means that all the key slots are in a valid, empty state. The global
157 * data mutex is already held when calling this function, so no need to
158 * lock it here, to set the flag. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100159 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100161}
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100164{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200165 size_t slot_idx;
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
168 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6cd2b8d2024-01-04 12:10:18 +0000169 slot->registered_readers = 1;
170 slot->state = PSA_SLOT_PENDING_DELETION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100172 }
Paul Elliott838886d2024-03-12 15:26:04 +0000173 /* The global data mutex is already held when calling this function. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100174 global_data.key_slots_initialized = 0;
175}
176
Ryan Everett2afb5162023-12-22 15:59:45 +0000177psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
178 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100179{
Ronald Crona5b894f2020-10-21 09:04:34 +0200180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200181 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000182 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200183
Paul Elliott838886d2024-03-12 15:26:04 +0000184 if (!psa_get_key_slots_initialized()) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200185 status = PSA_ERROR_BAD_STATE;
186 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100187 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200188
Ryan Everett2afb5162023-12-22 15:59:45 +0000189 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
191 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett2afb5162023-12-22 15:59:45 +0000192 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200193 selected_slot = slot;
194 break;
195 }
196
Ryan Everett2afb5162023-12-22 15:59:45 +0000197 if ((unused_persistent_key_slot == NULL) &&
198 (slot->state == PSA_SLOT_FULL) &&
199 (!psa_key_slot_has_readers(slot)) &&
200 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
201 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200203 }
204
205 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100206 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100207 * slot containing the description of a persistent key, recycle the first
208 * such key slot we encountered. If we later need to operate on the
209 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100210 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000213 (unused_persistent_key_slot != NULL)) {
214 selected_slot = unused_persistent_key_slot;
215 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000216 status = psa_wipe_key_slot(selected_slot);
217 if (status != PSA_SUCCESS) {
218 goto error;
219 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200220 }
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000223 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
224 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000226 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100228
Ronald Crona5b894f2020-10-21 09:04:34 +0200229 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200231 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200234 }
235 status = PSA_ERROR_INSUFFICIENT_MEMORY;
236
237error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200238 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200239 *volatile_key_id = 0;
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100242}
243
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100244#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100246{
247 psa_status_t status = PSA_SUCCESS;
248 uint8_t *key_data = NULL;
249 size_t key_data_length = 0;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 status = psa_load_persistent_key(&slot->attr,
252 &key_data, &key_data_length);
253 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100254 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200256
Steven Cooreman98435dd2021-01-08 19:19:40 +0100257#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100258 /* Special handling is required for loading keys associated with a
259 * dynamically registered SE interface. */
260 const psa_drv_se_t *drv;
261 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100263 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100266 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100267 goto exit;
268 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000269 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000270 status = psa_copy_key_material_into_slot(
271 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000272 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200273 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100274#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000277 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000278 goto exit;
279 }
280
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 psa_free_persistent_key_data(key_data, key_data_length);
283 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100284}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200285#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
286
Steven Cooreman6801f082021-02-19 17:21:22 +0100287#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100290{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100293 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100294 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100295 size_t key_buffer_size = 0;
296 size_t key_buffer_length = 0;
297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (!psa_key_id_is_builtin(
299 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
300 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100301 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100302
303 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100304 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 slot->attr.id, &lifetime, &slot_number);
306 if (status != PSA_SUCCESS) {
307 return status;
308 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100309
Steven Cooreman966db262021-04-13 13:45:45 +0200310 /* Set required key attributes to ensure get_builtin_key can retrieve the
311 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 psa_set_key_id(&attributes, slot->attr.id);
313 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100314
Steven Cooremance487022021-04-07 18:09:53 +0200315 /* Get the full key attributes from the driver in order to be able to
316 * calculate the required buffer size. */
317 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 slot_number, &attributes,
319 NULL, 0, NULL);
320 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200321 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200323 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 }
325 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200326 }
327
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200328 /* If the key should exist according to the platform, then ask the driver
329 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
331 &key_buffer_size);
332 if (status != PSA_SUCCESS) {
333 return status;
334 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100335
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200336 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200337 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
339 if (status != PSA_SUCCESS) {
340 return status;
341 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100342
343 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 slot_number, &attributes,
345 slot->key.data, slot->key.bytes, &key_buffer_length);
346 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100349
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200350 /* Copy actual key length and core attributes into the slot on success */
351 slot->key.bytes = key_buffer_length;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100352 slot->attr = attributes;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100353exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (status != PSA_SUCCESS) {
355 psa_remove_key_data_from_memory(slot);
356 }
357 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100358}
359#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
362 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200363{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200365
366 *p_slot = NULL;
Paul Elliott838886d2024-03-12 15:26:04 +0000367 if (!psa_get_key_slots_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_BAD_STATE;
369 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200370
Ryan Everett2f1f1722024-01-31 13:31:00 +0000371#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000372 /* We need to set status as success, otherwise CORRUPTION_DETECTED
373 * would be returned if the lock fails. */
374 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000375 /* If the key is persistent and not loaded, we cannot unlock the mutex
376 * between checking if the key is loaded and setting the slot as FULL,
377 * as otherwise another thread may load and then destroy the key
378 * in the meantime. */
379 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
380 &mbedtls_threading_key_slot_mutex));
381#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200382 /*
383 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100384 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
387 if (status != PSA_ERROR_DOES_NOT_EXIST) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000388#if defined(MBEDTLS_THREADING_C)
389 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
390 &mbedtls_threading_key_slot_mutex));
391#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return status;
393 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200394
Steven Cooreman0bb65362021-04-06 15:09:57 +0200395 /* Loading keys from storage requires support for such a mechanism */
396#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
397 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200398 psa_key_id_t volatile_key_id;
399
Ryan Everett098c6652024-01-03 13:03:36 +0000400 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000402#if defined(MBEDTLS_THREADING_C)
403 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
404 &mbedtls_threading_key_slot_mutex));
405#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return status;
407 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200408
Ronald Cronc4d1b512020-07-31 11:26:37 +0200409 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100410 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200411
Steven Cooreman6801f082021-02-19 17:21:22 +0100412 status = PSA_ERROR_DOES_NOT_EXIST;
413#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200414 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100416#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
417
418#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (status == PSA_ERROR_DOES_NOT_EXIST) {
420 status = psa_load_persistent_key_into_slot(*p_slot);
421 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100422#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (status != PSA_SUCCESS) {
425 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000428 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 }
430 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200431 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000433
434 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
435 PSA_SLOT_FULL);
436 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200438
Steven Cooreman0bb65362021-04-06 15:09:57 +0200439#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000440 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200441#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000442
Ryan Everettd4ea40d2024-04-29 18:24:58 +0100443 if (status != PSA_SUCCESS) {
444 *p_slot = NULL;
445 }
Ryan Everett2f1f1722024-01-31 13:31:00 +0000446#if defined(MBEDTLS_THREADING_C)
447 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
448 &mbedtls_threading_key_slot_mutex));
449#endif
450 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200451}
452
Ryan Everett39cc9d72023-12-21 17:57:14 +0000453psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200454{
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (slot == NULL) {
456 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200457 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000458 if ((slot->state != PSA_SLOT_FULL) &&
459 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000460 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000461 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200462
Ryan Everett39cc9d72023-12-21 17:57:14 +0000463 /* If we are the last reader and the slot is marked for deletion,
464 * we must wipe the slot here. */
465 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
466 (slot->registered_readers == 1)) {
467 return psa_wipe_key_slot(slot);
468 }
469
470 if (psa_key_slot_has_readers(slot)) {
471 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return PSA_SUCCESS;
473 }
474
475 /*
476 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000477 * do our best to report if there are no registered readers. Assert with
478 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
479 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 * the function is called as part of the execution of a test suite, the
481 * execution of the test suite is stopped in error if the assertion fails.
482 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000483 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200485}
486
Ryan Everetteb1722a2024-01-31 13:36:39 +0000487psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
488{
489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
490#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000491 /* We need to set status as success, otherwise CORRUPTION_DETECTED
492 * would be returned if the lock fails. */
493 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000494 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
495 &mbedtls_threading_key_slot_mutex));
496#endif
497 status = psa_unregister_read(slot);
498#if defined(MBEDTLS_THREADING_C)
499 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
500 &mbedtls_threading_key_slot_mutex));
501#endif
502 return status;
503}
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
506 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200509#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100510 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
512 if (driver != NULL) {
513 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200514 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 }
516 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200517 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100518#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200519 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100520#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
521
Steven Cooreman98435dd2021-01-08 19:19:40 +0100522 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200525 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return PSA_SUCCESS;
527 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200528}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200531{
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200533 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return PSA_SUCCESS;
535 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200536 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200537#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
539 return PSA_ERROR_INVALID_ARGUMENT;
540 } else {
541 return PSA_SUCCESS;
542 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200543#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200545#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200546 }
Gilles Peskined167b942019-04-19 18:19:40 +0200547}
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100550{
Archana0dc86b52021-07-14 13:59:48 +0530551#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
552 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100553 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200554 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 status = psa_get_and_lock_key_slot(key, &slot);
557 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200558 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000560 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100564 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200565
566 *handle = key;
567
Ryan Everette110a4c2024-02-22 10:43:03 +0000568 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200569
Archana0dc86b52021-07-14 13:59:48 +0530570#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200571 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200572 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530574#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100575}
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100578{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +0200580 psa_key_slot_t *slot;
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (psa_key_handle_is_null(handle)) {
583 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000584 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100585
Ryan Everett3af9bc12024-01-30 17:21:57 +0000586#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000587 /* We need to set status as success, otherwise CORRUPTION_DETECTED
588 * would be returned if the lock fails. */
589 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +0000590 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
591 &mbedtls_threading_key_slot_mutex));
592#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
594 if (status != PSA_SUCCESS) {
595 if (status == PSA_ERROR_DOES_NOT_EXIST) {
596 status = PSA_ERROR_INVALID_HANDLE;
597 }
Ryan Everett3af9bc12024-01-30 17:21:57 +0000598#if defined(MBEDTLS_THREADING_C)
599 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
600 &mbedtls_threading_key_slot_mutex));
601#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return status;
603 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000604
Ryan Everettc70ce572024-01-03 16:04:33 +0000605 if (slot->registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +0000606 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000607 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +0000608 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000610#if defined(MBEDTLS_THREADING_C)
611 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
612 &mbedtls_threading_key_slot_mutex));
613#endif
614
615 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100616}
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200619{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +0200621 psa_key_slot_t *slot;
622
Ryan Everettb0821952024-01-24 11:42:32 +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 Everettb0821952024-01-24 11:42:32 +0000627 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
628 &mbedtls_threading_key_slot_mutex));
629#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +0000630 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
631 if (status != PSA_SUCCESS) {
632#if defined(MBEDTLS_THREADING_C)
633 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
634 &mbedtls_threading_key_slot_mutex));
635#endif
636 return status;
637 }
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Ryan Everettc70ce572024-01-03 16:04:33 +0000640 (slot->registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +0000641 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000642 } else {
Ryan Everettb0821952024-01-24 11:42:32 +0000643 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 }
Ryan Everettb0821952024-01-24 11:42:32 +0000645#if defined(MBEDTLS_THREADING_C)
646 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
647 &mbedtls_threading_key_slot_mutex));
648#endif
649
650 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +0200651}
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200654{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200655 size_t slot_idx;
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
660 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000661 if (psa_key_slot_has_readers(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100662 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100663 }
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000664 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200665 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200666 continue;
667 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200669 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 } else {
671 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200672 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100674 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200676 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
678 PSA_KEY_LOCATION_LOCAL_STORAGE) {
679 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200680 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100682 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200684 }
685 }
686}
687
Gilles Peskine961849f2018-11-30 18:54:54 +0100688#endif /* MBEDTLS_PSA_CRYPTO_C */