blob: b2a3c7e5a0d7f83c725cd9ead8a8d5d596572cac [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
Gilles Peskine449bd832023-01-11 14:50:10 +010037int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020038{
Gilles Peskine449bd832023-01-11 14:50:10 +010039 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020040
Gilles Peskine449bd832023-01-11 14:50:10 +010041 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
42 (key_id <= PSA_KEY_ID_USER_MAX)) {
43 return 1;
44 }
Ronald Crond2ed4812020-07-17 16:11:30 +020045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 if (vendor_ok &&
47 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
48 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
49 return 1;
50 }
Ronald Crond2ed4812020-07-17 16:11:30 +020051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020053}
54
Ronald Cron5c522922020-11-14 16:35:34 +010055/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020056 *
Ronald Cron5c522922020-11-14 16:35:34 +010057 * The descriptions of volatile keys and loaded persistent keys are
58 * stored in key slots. This function returns a pointer to the key slot
59 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020060 *
Ronald Cron5c522922020-11-14 16:35:34 +010061 * The function searches the key slots containing the description of the key
62 * with \p key identifier. The function does only read accesses to the key
63 * slots. The function does not load any persistent key thus does not access
64 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020065 *
Ronald Cron5c522922020-11-14 16:35:34 +010066 * For volatile key identifiers, only one key slot is queried as a volatile
67 * key with identifier key_id can only be stored in slot of index
68 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020069 *
Ronald Cron5c522922020-11-14 16:35:34 +010070 * On success, the function locks the key slot. It is the responsibility of
71 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020072 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +000073 * If multi-threading is enabled, the caller must hold the
74 * global key slot mutex.
75 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020076 * \param key Key identifier to query.
77 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
78 * key slot containing the description of the key
79 * identified by \p key.
80 *
Ronald Cron96783552020-10-19 12:06:30 +020081 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020082 * The pointer to the key slot containing the description of the key
83 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020084 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020085 * \p key is not a valid key identifier.
86 * \retval #PSA_ERROR_DOES_NOT_EXIST
87 * There is no key with key identifier \p key in the key slots.
88 */
Ronald Cron5c522922020-11-14 16:35:34 +010089static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +010091{
Ronald Cronf473d8b2020-11-12 10:07:21 +010092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +010094 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +020095 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +010096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (psa_key_id_is_volatile(key_id)) {
98 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +010099
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000100 /* Check if both the PSA key identifier key_id and the owner
101 * identifier of key match those of the key slot. */
102 if ((slot->state == PSA_SLOT_FULL) &&
103 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
104 status = PSA_SUCCESS;
105 } else {
106 status = PSA_ERROR_DOES_NOT_EXIST;
107 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 } else {
109 if (!psa_is_valid_key_id(key, 1)) {
110 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200111 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100112
113 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
114 slot = &global_data.key_slots[slot_idx];
Ryan Everett098c6652024-01-03 13:03:36 +0000115 /* Only consider slots which are in a full state. */
116 if ((slot->state == PSA_SLOT_FULL) &&
117 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 break;
119 }
120 }
121 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100122 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000126 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100128 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200130 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200133}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100136{
Ryan Everett558da2f2024-01-19 12:59:28 +0000137 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100138 * guarantee that the key slots are initialized to all-zero, which
139 * means that all the key slots are in a valid, empty state. */
140 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100142}
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100145{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200146 size_t slot_idx;
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
149 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6cd2b8d2024-01-04 12:10:18 +0000150 slot->registered_readers = 1;
151 slot->state = PSA_SLOT_PENDING_DELETION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100153 }
154 global_data.key_slots_initialized = 0;
155}
156
Ryan Everett2afb5162023-12-22 15:59:45 +0000157psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
158 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100159{
Ronald Crona5b894f2020-10-21 09:04:34 +0200160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200161 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000162 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200165 status = PSA_ERROR_BAD_STATE;
166 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100167 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200168
Ryan Everett2afb5162023-12-22 15:59:45 +0000169 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
171 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett2afb5162023-12-22 15:59:45 +0000172 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200173 selected_slot = slot;
174 break;
175 }
176
Ryan Everett2afb5162023-12-22 15:59:45 +0000177 if ((unused_persistent_key_slot == NULL) &&
178 (slot->state == PSA_SLOT_FULL) &&
179 (!psa_key_slot_has_readers(slot)) &&
180 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
181 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200183 }
184
185 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100186 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100187 * slot containing the description of a persistent key, recycle the first
188 * such key slot we encountered. If we later need to operate on the
189 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100190 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000193 (unused_persistent_key_slot != NULL)) {
194 selected_slot = unused_persistent_key_slot;
195 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000196 status = psa_wipe_key_slot(selected_slot);
197 if (status != PSA_SUCCESS) {
198 goto error;
199 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200200 }
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000203 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
204 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000206 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100208
Ronald Crona5b894f2020-10-21 09:04:34 +0200209 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200214 }
215 status = PSA_ERROR_INSUFFICIENT_MEMORY;
216
217error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200218 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200219 *volatile_key_id = 0;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100222}
223
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100224#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100226{
227 psa_status_t status = PSA_SUCCESS;
228 uint8_t *key_data = NULL;
229 size_t key_data_length = 0;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 status = psa_load_persistent_key(&slot->attr,
232 &key_data, &key_data_length);
233 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200236
Steven Cooreman98435dd2021-01-08 19:19:40 +0100237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100238 /* Special handling is required for loading keys associated with a
239 * dynamically registered SE interface. */
240 const psa_drv_se_t *drv;
241 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100243 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100246 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100247 goto exit;
248 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000249 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000250 status = psa_copy_key_material_into_slot(
251 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000252 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200253 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100254#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000257 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000258 goto exit;
259 }
260
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 psa_free_persistent_key_data(key_data, key_data_length);
263 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100264}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200265#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
266
Steven Cooreman6801f082021-02-19 17:21:22 +0100267#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100270{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100273 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100274 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100275 size_t key_buffer_size = 0;
276 size_t key_buffer_length = 0;
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (!psa_key_id_is_builtin(
279 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
280 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100281 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100282
283 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100284 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 slot->attr.id, &lifetime, &slot_number);
286 if (status != PSA_SUCCESS) {
287 return status;
288 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100289
Steven Cooreman966db262021-04-13 13:45:45 +0200290 /* Set required key attributes to ensure get_builtin_key can retrieve the
291 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 psa_set_key_id(&attributes, slot->attr.id);
293 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100294
Steven Cooremance487022021-04-07 18:09:53 +0200295 /* Get the full key attributes from the driver in order to be able to
296 * calculate the required buffer size. */
297 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 slot_number, &attributes,
299 NULL, 0, NULL);
300 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200301 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200303 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 }
305 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200306 }
307
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200308 /* If the key should exist according to the platform, then ask the driver
309 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
311 &key_buffer_size);
312 if (status != PSA_SUCCESS) {
313 return status;
314 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100315
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200316 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200317 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
319 if (status != PSA_SUCCESS) {
320 return status;
321 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100322
323 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 slot_number, &attributes,
325 slot->key.data, slot->key.bytes, &key_buffer_length);
326 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100327 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100329
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200330 /* Copy actual key length and core attributes into the slot on success */
331 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100332 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100333exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (status != PSA_SUCCESS) {
335 psa_remove_key_data_from_memory(slot);
336 }
337 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100338}
339#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
342 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200343{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200345
346 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (!global_data.key_slots_initialized) {
348 return PSA_ERROR_BAD_STATE;
349 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200350
Ryan Everett2f1f1722024-01-31 13:31:00 +0000351#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000352 /* We need to set status as success, otherwise CORRUPTION_DETECTED
353 * would be returned if the lock fails. */
354 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000355 /* If the key is persistent and not loaded, we cannot unlock the mutex
356 * between checking if the key is loaded and setting the slot as FULL,
357 * as otherwise another thread may load and then destroy the key
358 * in the meantime. */
359 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
360 &mbedtls_threading_key_slot_mutex));
361#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200362 /*
363 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100364 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
367 if (status != PSA_ERROR_DOES_NOT_EXIST) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000368#if defined(MBEDTLS_THREADING_C)
369 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
370 &mbedtls_threading_key_slot_mutex));
371#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return status;
373 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200374
Steven Cooreman0bb65362021-04-06 15:09:57 +0200375 /* Loading keys from storage requires support for such a mechanism */
376#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
377 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200378 psa_key_id_t volatile_key_id;
379
Ryan Everett098c6652024-01-03 13:03:36 +0000380 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000382#if defined(MBEDTLS_THREADING_C)
383 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
384 &mbedtls_threading_key_slot_mutex));
385#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return status;
387 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200388
Ronald Cronc4d1b512020-07-31 11:26:37 +0200389 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100390 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200391
Steven Cooreman6801f082021-02-19 17:21:22 +0100392 status = PSA_ERROR_DOES_NOT_EXIST;
393#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200394 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100396#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
397
398#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (status == PSA_ERROR_DOES_NOT_EXIST) {
400 status = psa_load_persistent_key_into_slot(*p_slot);
401 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100402#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (status != PSA_SUCCESS) {
405 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000408 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 }
410 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200411 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000413
414 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
415 PSA_SLOT_FULL);
416 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200418
Steven Cooreman0bb65362021-04-06 15:09:57 +0200419#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000420 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200421#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000422
423#if defined(MBEDTLS_THREADING_C)
424 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
425 &mbedtls_threading_key_slot_mutex));
426#endif
427 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200428}
429
Ryan Everett39cc9d72023-12-21 17:57:14 +0000430psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200431{
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (slot == NULL) {
433 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200434 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000435 if ((slot->state != PSA_SLOT_FULL) &&
436 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000437 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000438 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200439
Ryan Everett39cc9d72023-12-21 17:57:14 +0000440 /* If we are the last reader and the slot is marked for deletion,
441 * we must wipe the slot here. */
442 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
443 (slot->registered_readers == 1)) {
444 return psa_wipe_key_slot(slot);
445 }
446
447 if (psa_key_slot_has_readers(slot)) {
448 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return PSA_SUCCESS;
450 }
451
452 /*
453 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000454 * do our best to report if there are no registered readers. Assert with
455 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
456 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 * the function is called as part of the execution of a test suite, the
458 * execution of the test suite is stopped in error if the assertion fails.
459 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000460 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200462}
463
Ryan Everetteb1722a2024-01-31 13:36:39 +0000464psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
465{
466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
467#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000468 /* We need to set status as success, otherwise CORRUPTION_DETECTED
469 * would be returned if the lock fails. */
470 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000471 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
472 &mbedtls_threading_key_slot_mutex));
473#endif
474 status = psa_unregister_read(slot);
475#if defined(MBEDTLS_THREADING_C)
476 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
477 &mbedtls_threading_key_slot_mutex));
478#endif
479 return status;
480}
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
483 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200484{
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200486#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100487 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
489 if (driver != NULL) {
490 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200491 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 }
493 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200494 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100495#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200496 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
498
Steven Cooreman98435dd2021-01-08 19:19:40 +0100499 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200502 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return PSA_SUCCESS;
504 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200505}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200508{
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200510 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return PSA_SUCCESS;
512 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200513 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200514#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
516 return PSA_ERROR_INVALID_ARGUMENT;
517 } else {
518 return PSA_SUCCESS;
519 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200520#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200522#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200523 }
Gilles Peskined167b942019-04-19 18:19:40 +0200524}
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100527{
Archana0dc86b52021-07-14 13:59:48 +0530528#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
529 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100530 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200531 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 status = psa_get_and_lock_key_slot(key, &slot);
534 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200535 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000537 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100541 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200542
543 *handle = key;
544
Ryan Everette110a4c2024-02-22 10:43:03 +0000545 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200546
Archana0dc86b52021-07-14 13:59:48 +0530547#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200548 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200549 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530551#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100552}
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100555{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +0200557 psa_key_slot_t *slot;
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (psa_key_handle_is_null(handle)) {
560 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000561 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100562
Ryan Everett3af9bc12024-01-30 17:21:57 +0000563#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000564 /* We need to set status as success, otherwise CORRUPTION_DETECTED
565 * would be returned if the lock fails. */
566 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +0000567 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
568 &mbedtls_threading_key_slot_mutex));
569#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
571 if (status != PSA_SUCCESS) {
572 if (status == PSA_ERROR_DOES_NOT_EXIST) {
573 status = PSA_ERROR_INVALID_HANDLE;
574 }
Ryan Everett3af9bc12024-01-30 17:21:57 +0000575#if defined(MBEDTLS_THREADING_C)
576 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
577 &mbedtls_threading_key_slot_mutex));
578#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return status;
580 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000581
Ryan Everettc70ce572024-01-03 16:04:33 +0000582 if (slot->registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +0000583 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000584 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +0000585 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000587#if defined(MBEDTLS_THREADING_C)
588 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
589 &mbedtls_threading_key_slot_mutex));
590#endif
591
592 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200596{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +0200598 psa_key_slot_t *slot;
599
Ryan Everettb0821952024-01-24 11:42:32 +0000600#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000601 /* We need to set status as success, otherwise CORRUPTION_DETECTED
602 * would be returned if the lock fails. */
603 status = PSA_SUCCESS;
Ryan Everettb0821952024-01-24 11:42:32 +0000604 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
605 &mbedtls_threading_key_slot_mutex));
606#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +0000607 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
608 if (status != PSA_SUCCESS) {
609#if defined(MBEDTLS_THREADING_C)
610 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
611 &mbedtls_threading_key_slot_mutex));
612#endif
613 return status;
614 }
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Ryan Everettc70ce572024-01-03 16:04:33 +0000617 (slot->registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +0000618 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000619 } else {
Ryan Everettb0821952024-01-24 11:42:32 +0000620 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 }
Ryan Everettb0821952024-01-24 11:42:32 +0000622#if defined(MBEDTLS_THREADING_C)
623 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
624 &mbedtls_threading_key_slot_mutex));
625#endif
626
627 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +0200628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200631{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200632 size_t slot_idx;
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
637 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000638 if (psa_key_slot_has_readers(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100639 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100640 }
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000641 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200642 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200643 continue;
644 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200646 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 } else {
648 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200649 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100651 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200653 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
655 PSA_KEY_LOCATION_LOCAL_STORAGE) {
656 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200657 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100659 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200661 }
662 }
663}
664
Gilles Peskine961849f2018-11-30 18:54:54 +0100665#endif /* MBEDTLS_PSA_CRYPTO_C */