blob: d740960dd5dde54c9c6490597d646f27255fef21 [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 Peskinee8199f52024-06-10 11:53:33 +020061#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
62
63/* Dynamic key store.
64 *
65 * The key store consists of multiple slices.
66 *
67 * The volatile keys are stored in variable-sized tables called slices.
68 * Slices are allocated on demand and deallocated when possible.
69 * The size of slices increases exponentially, so the average overhead
70 * (number of slots that are allocated but not used) is roughly
71 * proportional to the number of keys (with a factor that grows
72 * when the key store is fragmented).
73 *
74 * One slice is dedicated to the cache of persistent and built-in keys.
75 * For simplicity, they are separated from volatile keys. This cache
76 * slice has a fixed size and has the slice index KEY_SLOT_CACHE_SLICE_INDEX,
77 * located after the slices for volatile keys.
78 */
79
80/* Size of slice 0 containing the cache of persistent and built-in keys. */
81#define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
82
83/* Volatile keys are stored in slices 1 through KEY_SLICE_COUNT inclusive.
84 * Each slice is twice the size of the previous slice.
85 * Volatile key identifiers encode the slice number as follows:
86 * bits 30..31: 0b10 (mandated by the PSA Crypto specification).
87 * bits 25..29: slice index (0...KEY_SLOT_VOLATILE_SLICE_COUNT-1)
88 * bits 0..24: slot index in slice
89 */
90#define KEY_ID_SLOT_INDEX_WIDTH 25u
91#define KEY_ID_SLICE_INDEX_WIDTH 5u
92
93#define KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH 16u
94#define KEY_SLOT_VOLATILE_SLICE_COUNT 22u
95#define KEY_SLICE_COUNT (KEY_SLOT_VOLATILE_SLICE_COUNT + 1u)
96#define KEY_SLOT_CACHE_SLICE_INDEX KEY_SLOT_VOLATILE_SLICE_COUNT
97
98#if KEY_ID_SLICE_INDEX_WIDTH + KEY_ID_SLOT_INDEX_WIDTH > 30
99#error "Not enough room in volatile key IDs for slice index and slot index"
100#endif
101#if KEY_SLICE_COUNT >= (1 << KEY_ID_SLICE_INDEX_WIDTH) - 1
102#error "Too many slices to fit the slice index in a volatile key ID"
103#endif
104#define KEY_SLICE_LENGTH_MAX \
105 (KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << (KEY_SLOT_VOLATILE_SLICE_COUNT - 1))
106#if KEY_SLICE_LENGTH_MAX > 1 << KEY_ID_SLOT_INDEX_WIDTH
107#error "Not enough room in volatile key IDs for a slot index in the largest slice"
108#endif
109#if KEY_ID_SLICE_INDEX_WIDTH > 8
110#error "Slice index does not fit in uint8_t for psa_key_slot_t::slice_index"
111#endif
112
113
114/* Calculate the volatile key id to use for a given slot.
115 * This function assumes valid parameter values. */
116static psa_key_id_t volatile_key_id_of_index(size_t slice_idx,
117 size_t slot_idx)
118{
119 return 0x40000000u | (slice_idx << KEY_ID_SLOT_INDEX_WIDTH) | slot_idx;
120}
121
122/* Calculate the slice containing the given volatile key.
123 * This function assumes valid parameter values. */
124static size_t slice_index_of_volatile_key_id(psa_key_id_t key_id)
125{
126 size_t mask = (1LU << KEY_ID_SLICE_INDEX_WIDTH) - 1;
127 return (key_id >> KEY_ID_SLOT_INDEX_WIDTH) & mask;
128}
129
130/* Calculate the index of the slot containing the given volatile key.
131 * This function assumes valid parameter values. */
132static size_t slot_index_of_volatile_key_id(psa_key_id_t key_id)
133{
134 return key_id & ((1LU << KEY_ID_SLOT_INDEX_WIDTH) - 1);
135}
136
137/* In global_data.first_free_slot_index, use this special value to
138 * indicate that the slice is full. */
139#define FREE_SLOT_INDEX_NONE ((size_t) -1)
140
Gilles Peskine3bc9d2b2024-06-21 00:09:07 +0200141#if defined(MBEDTLS_TEST_HOOKS)
142size_t psa_key_slot_volatile_slice_count(void)
143{
144 return KEY_SLOT_VOLATILE_SLICE_COUNT;
145}
146#endif
147
Gilles Peskinee8199f52024-06-10 11:53:33 +0200148#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
149
150/* Static key store.
151 *
152 * All the keys (volatile or persistent) are in a single slice.
153 * We only use slices as a concept to allow some differences between
154 * static and dynamic key store management to be buried in auxiliary
155 * functions.
156 */
157
Gilles Peskine5064af62024-06-07 13:53:28 +0200158#define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
159#define KEY_SLICE_COUNT 1u
160#define KEY_SLOT_CACHE_SLICE_INDEX 0
161
Gilles Peskinee8199f52024-06-10 11:53:33 +0200162#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
163
Gilles Peskine5064af62024-06-07 13:53:28 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165typedef struct {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200166#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
167 psa_key_slot_t *key_slices[KEY_SLICE_COUNT];
168 size_t first_free_slot_index[KEY_SLOT_VOLATILE_SLICE_COUNT];
169#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Steven Cooreman863470a2021-02-15 14:03:19 +0100170 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskinee8199f52024-06-10 11:53:33 +0200171#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Dave Rodgman164614a2023-08-16 17:56:28 +0100172 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100173} psa_global_data_t;
174
Gilles Peskine2e14bd32018-12-12 14:05:08 +0100175static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100176
Paul Elliott838886d2024-03-12 15:26:04 +0000177static uint8_t psa_get_key_slots_initialized(void)
178{
Paul Elliott838886d2024-03-12 15:26:04 +0000179 uint8_t initialized;
180
181#if defined(MBEDTLS_THREADING_C)
182 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
183#endif /* defined(MBEDTLS_THREADING_C) */
184
185 initialized = global_data.key_slots_initialized;
186
187#if defined(MBEDTLS_THREADING_C)
188 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
189#endif /* defined(MBEDTLS_THREADING_C) */
190
191 return initialized;
192}
193
Gilles Peskine5064af62024-06-07 13:53:28 +0200194
195
196/** The length of the given slice in the key slot table.
197 *
198 * \param slice_idx The slice number. It must satisfy
199 * 0 <= slice_idx < KEY_SLICE_COUNT.
200 *
201 * \return The number of elements in the given slice.
202 */
203static inline size_t key_slice_length(size_t slice_idx);
204
205/** Get a pointer to the slot where the given volatile key is located.
206 *
207 * \param key_id The key identifier. It must be a valid volatile key
208 * identifier.
209 * \return A pointer to the only slot that the given key
210 * can be in. Note that the slot may be empty or
211 * contain a different key.
212 */
213static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id);
214
215/** Get a pointer to an entry in the persistent key cache.
216 *
217 * \param slot_idx The index in the table. It must satisfy
218 * 0 <= slot_idx < PERSISTENT_KEY_CACHE_COUNT.
219 * \return A pointer to the slot containing the given
220 * persistent key cache entry.
221 */
222static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx);
223
224/** Get a pointer to a slot given by slice and index.
225 *
226 * \param slice_idx The slice number. It must satisfy
227 * 0 <= slice_idx < KEY_SLICE_COUNT.
228 * \param slot_idx An index in the given slice. It must satisfy
229 * 0 <= slot_idx < key_slice_length(slice_idx).
230 *
231 * \return A pointer to the given slot.
232 */
233static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx);
234
Gilles Peskinee8199f52024-06-10 11:53:33 +0200235#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
236
Gilles Peskine3bc9d2b2024-06-21 00:09:07 +0200237#if defined(MBEDTLS_TEST_HOOKS)
238size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)(size_t slice_idx) = NULL;
239#endif
240
Gilles Peskinee8199f52024-06-10 11:53:33 +0200241static inline size_t key_slice_length(size_t slice_idx)
242{
243 if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) {
244 return PERSISTENT_KEY_CACHE_COUNT;
245 } else {
Gilles Peskine3bc9d2b2024-06-21 00:09:07 +0200246#if defined(MBEDTLS_TEST_HOOKS)
247 if (mbedtls_test_hook_psa_volatile_key_slice_length != NULL) {
248 return mbedtls_test_hook_psa_volatile_key_slice_length(slice_idx);
249 }
250#endif
Gilles Peskinee8199f52024-06-10 11:53:33 +0200251 return KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << slice_idx;
252 }
253}
254
255static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
256{
257 size_t slice_idx = slice_index_of_volatile_key_id(key_id);
258 if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) {
259 return NULL;
260 }
261 size_t slot_idx = slot_index_of_volatile_key_id(key_id);
262 if (slot_idx >= key_slice_length(slice_idx)) {
263 return NULL;
264 }
265 psa_key_slot_t *slice = global_data.key_slices[slice_idx];
266 if (slice == NULL) {
267 return NULL;
268 }
269 return &slice[slot_idx];
270}
271
272static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
273{
274 return &global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX][slot_idx];
275}
276
277static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
278{
279 return &global_data.key_slices[slice_idx][slot_idx];
280}
281
282#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
283
Gilles Peskine5064af62024-06-07 13:53:28 +0200284static inline size_t key_slice_length(size_t slice_idx)
285{
286 (void) slice_idx;
287 return ARRAY_LENGTH(global_data.key_slots);
288}
289
290static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
291{
292 MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
293 PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
294 "The key slot array is larger than the volatile key ID range");
295 return &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
296}
297
298static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
299{
300 return &global_data.key_slots[slot_idx];
301}
302
303static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
304{
305 (void) slice_idx;
306 return &global_data.key_slots[slot_idx];
307}
308
Gilles Peskinee8199f52024-06-10 11:53:33 +0200309#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
310
311
Gilles Peskine5064af62024-06-07 13:53:28 +0200312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +0200314{
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
318 (key_id <= PSA_KEY_ID_USER_MAX)) {
319 return 1;
320 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (vendor_ok &&
323 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
324 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
325 return 1;
326 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +0200329}
330
Ronald Cron5c522922020-11-14 16:35:34 +0100331/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200332 *
Ronald Cron5c522922020-11-14 16:35:34 +0100333 * The descriptions of volatile keys and loaded persistent keys are
334 * stored in key slots. This function returns a pointer to the key slot
335 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200336 *
Ronald Cron5c522922020-11-14 16:35:34 +0100337 * The function searches the key slots containing the description of the key
338 * with \p key identifier. The function does only read accesses to the key
339 * slots. The function does not load any persistent key thus does not access
340 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200341 *
Ronald Cron5c522922020-11-14 16:35:34 +0100342 * For volatile key identifiers, only one key slot is queried as a volatile
343 * key with identifier key_id can only be stored in slot of index
344 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +0200345 *
Ronald Cron5c522922020-11-14 16:35:34 +0100346 * On success, the function locks the key slot. It is the responsibility of
347 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200348 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000349 * If multi-threading is enabled, the caller must hold the
350 * global key slot mutex.
351 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200352 * \param key Key identifier to query.
353 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
354 * key slot containing the description of the key
355 * identified by \p key.
356 *
Ronald Cron96783552020-10-19 12:06:30 +0200357 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200358 * The pointer to the key slot containing the description of the key
359 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200360 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200361 * \p key is not a valid key identifier.
362 * \retval #PSA_ERROR_DOES_NOT_EXIST
363 * There is no key with key identifier \p key in the key slots.
364 */
Ronald Cron5c522922020-11-14 16:35:34 +0100365static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100367{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100370 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200371 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (psa_key_id_is_volatile(key_id)) {
Gilles Peskine5064af62024-06-07 13:53:28 +0200374 slot = get_volatile_key_slot(key_id);
Ronald Cron1d12d872020-11-18 17:21:22 +0100375
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000376 /* Check if both the PSA key identifier key_id and the owner
377 * identifier of key match those of the key slot. */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200378 if (slot != NULL &&
379 slot->state == PSA_SLOT_FULL &&
380 mbedtls_svc_key_id_equal(key, slot->attr.id)) {
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000381 status = PSA_SUCCESS;
382 } else {
383 status = PSA_ERROR_DOES_NOT_EXIST;
384 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 } else {
386 if (!psa_is_valid_key_id(key, 1)) {
387 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200388 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100389
Gilles Peskine5064af62024-06-07 13:53:28 +0200390 for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
391 slot = get_persistent_key_slot(slot_idx);
Ryan Everett098c6652024-01-03 13:03:36 +0000392 /* Only consider slots which are in a full state. */
393 if ((slot->state == PSA_SLOT_FULL) &&
394 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 break;
396 }
397 }
398 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100399 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200400 }
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000403 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100405 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200407 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200410}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100413{
Gilles Peskinee8199f52024-06-10 11:53:33 +0200414#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
415 global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] =
416 mbedtls_calloc(PERSISTENT_KEY_CACHE_COUNT,
417 sizeof(*global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX]));
418 if (global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] == NULL) {
419 return PSA_ERROR_INSUFFICIENT_MEMORY;
420 }
421#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Ryan Everett558da2f2024-01-19 12:59:28 +0000422 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100423 * guarantee that the key slots are initialized to all-zero, which
Paul Elliott838886d2024-03-12 15:26:04 +0000424 * means that all the key slots are in a valid, empty state. The global
425 * data mutex is already held when calling this function, so no need to
426 * lock it here, to set the flag. */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200427#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
428
Gilles Peskine66fb1262018-12-10 16:29:04 +0100429 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100431}
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100434{
Gilles Peskine5064af62024-06-07 13:53:28 +0200435 for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200436#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
437 if (global_data.key_slices[slice_idx] == NULL) {
438 continue;
439 }
440#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine5064af62024-06-07 13:53:28 +0200441 for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
442 psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
Gilles Peskinea81282c2024-06-10 15:41:38 +0200443#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
444 /* When MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled, calling
445 * psa_wipe_key_slot() on an unused slot is useless, but it
446 * happens to work (because we flip the state to PENDING_DELETION).
447 *
448 * When MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled,
449 * psa_wipe_key_slot() needs to have a valid slice_index
450 * field, but that value might not be correct in a
451 * free slot, so we must not call it.
452 *
453 * Bypass the call to psa_wipe_key_slot() if the slot is empty,
454 * but only if MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, to save
455 * a few bytes of code size otherwise.
456 */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200457 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200458 continue;
459 }
Gilles Peskinea81282c2024-06-10 15:41:38 +0200460#endif
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200461 slot->var.occupied.registered_readers = 1;
Gilles Peskine5064af62024-06-07 13:53:28 +0200462 slot->state = PSA_SLOT_PENDING_DELETION;
463 (void) psa_wipe_key_slot(slot);
464 }
Gilles Peskinee8199f52024-06-10 11:53:33 +0200465#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
466 mbedtls_free(global_data.key_slices[slice_idx]);
467 global_data.key_slices[slice_idx] = NULL;
468#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100469 }
Gilles Peskinee8199f52024-06-10 11:53:33 +0200470
471#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
472 for (size_t slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) {
473 global_data.first_free_slot_index[slice_idx] = 0;
474 }
475#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
476
Paul Elliott838886d2024-03-12 15:26:04 +0000477 /* The global data mutex is already held when calling this function. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100478 global_data.key_slots_initialized = 0;
479}
480
Gilles Peskinee8199f52024-06-10 11:53:33 +0200481#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
482
483static psa_status_t psa_allocate_volatile_key_slot(psa_key_id_t *key_id,
484 psa_key_slot_t **p_slot)
485{
486 size_t slice_idx;
487 for (slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) {
488 if (global_data.first_free_slot_index[slice_idx] != FREE_SLOT_INDEX_NONE) {
489 break;
490 }
491 }
492 if (slice_idx == KEY_SLOT_VOLATILE_SLICE_COUNT) {
493 return PSA_ERROR_INSUFFICIENT_MEMORY;
494 }
495
496 if (global_data.key_slices[slice_idx] == NULL) {
497 global_data.key_slices[slice_idx] =
498 mbedtls_calloc(key_slice_length(slice_idx),
499 sizeof(psa_key_slot_t));
500 if (global_data.key_slices[slice_idx] == NULL) {
501 return PSA_ERROR_INSUFFICIENT_MEMORY;
502 }
503 }
504 psa_key_slot_t *slice = global_data.key_slices[slice_idx];
505
506 size_t slot_idx = global_data.first_free_slot_index[slice_idx];
507 *key_id = volatile_key_id_of_index(slice_idx, slot_idx);
508
509 psa_key_slot_t *slot = &slice[slot_idx];
510 size_t next_free = slot_idx + 1 + slot->var.free.next_free_relative_to_next;
511 if (next_free >= key_slice_length(slice_idx)) {
512 next_free = FREE_SLOT_INDEX_NONE;
513 }
514 global_data.first_free_slot_index[slice_idx] = next_free;
515 /* The .next_free field is not meaningful when the slot is not free,
516 * so give it the same content as freshly initialized memory. */
517 slot->var.free.next_free_relative_to_next = 0;
518
519 psa_status_t status = psa_key_slot_state_transition(slot,
520 PSA_SLOT_EMPTY,
521 PSA_SLOT_FILLING);
522 if (status != PSA_SUCCESS) {
523 /* The only reason for failure is if the slot state was not empty.
524 * This indicates that something has gone horribly wrong.
525 * In this case, we leave the slot out of the free list, and stop
526 * modifying it. This minimizes any further corruption. The slot
527 * is a memory leak, but that's a lesser evil. */
528 return status;
529 }
530
531 *p_slot = slot;
532 slot->slice_index = slice_idx;
533 return PSA_SUCCESS;
534}
535
536psa_status_t psa_free_key_slot(size_t slice_idx,
537 psa_key_slot_t *slot)
538{
539
540 if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) {
541 /* This is a cache entry. We don't maintain a free list, so
542 * there's nothing to do. */
543 return PSA_SUCCESS;
544 }
545 if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) {
546 return PSA_ERROR_CORRUPTION_DETECTED;
547 }
548
549 psa_key_slot_t *slice = global_data.key_slices[slice_idx];
550 psa_key_slot_t *slice_end = slice + key_slice_length(slice_idx);
551 if (slot < slice || slot >= slice_end) {
552 /* The slot isn't actually in the slice! We can't detect that
553 * condition for sure, because the pointer comparison itself is
554 * undefined behavior in that case. That same condition makes the
555 * subtraction to calculate the slot index also UB.
556 * Give up now to avoid causing further corruption.
557 */
558 return PSA_ERROR_CORRUPTION_DETECTED;
559 }
560 size_t slot_idx = slot - slice;
561
562 size_t next_free = global_data.first_free_slot_index[slice_idx];
563 if (next_free >= key_slice_length(slice_idx)) {
564 /* The slot was full. The newly freed slot thus becomes the
565 * end of the free list. */
566 next_free = key_slice_length(slice_idx);
567 }
568 global_data.first_free_slot_index[slice_idx] = slot_idx;
569 slot->var.free.next_free_relative_to_next = next_free - slot_idx - 1;
570
571 return PSA_SUCCESS;
572}
573#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
574
Ryan Everett2afb5162023-12-22 15:59:45 +0000575psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
576 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100577{
Ronald Crona5b894f2020-10-21 09:04:34 +0200578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200579 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000580 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200581
Paul Elliott838886d2024-03-12 15:26:04 +0000582 if (!psa_get_key_slots_initialized()) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200583 status = PSA_ERROR_BAD_STATE;
584 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100585 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200586
Gilles Peskinee8199f52024-06-10 11:53:33 +0200587#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
Gilles Peskinea81282c2024-06-10 15:41:38 +0200588 if (volatile_key_id != NULL) {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200589 return psa_allocate_volatile_key_slot(volatile_key_id, p_slot);
Gilles Peskinee8199f52024-06-10 11:53:33 +0200590 }
Gilles Peskinea81282c2024-06-10 15:41:38 +0200591#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200592
593 /* With a dynamic key store, allocate an entry in the cache slice,
594 * applicable only to non-volatile keys that get cached in RAM.
595 * With a static key store, allocate an entry in the sole slice,
596 * applicable to all keys. */
Ryan Everett2afb5162023-12-22 15:59:45 +0000597 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine5064af62024-06-07 13:53:28 +0200598 for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
599 psa_key_slot_t *slot = get_key_slot(KEY_SLOT_CACHE_SLICE_INDEX, slot_idx);
Ryan Everett2afb5162023-12-22 15:59:45 +0000600 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200601 selected_slot = slot;
602 break;
603 }
604
Ryan Everett2afb5162023-12-22 15:59:45 +0000605 if ((unused_persistent_key_slot == NULL) &&
606 (slot->state == PSA_SLOT_FULL) &&
607 (!psa_key_slot_has_readers(slot)) &&
608 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
609 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200611 }
612
613 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100614 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100615 * slot containing the description of a persistent key, recycle the first
616 * such key slot we encountered. If we later need to operate on the
617 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100618 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200619 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000621 (unused_persistent_key_slot != NULL)) {
622 selected_slot = unused_persistent_key_slot;
623 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000624 status = psa_wipe_key_slot(selected_slot);
625 if (status != PSA_SUCCESS) {
626 goto error;
627 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200628 }
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000631 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
632 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000634 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100636
Gilles Peskinee8199f52024-06-10 11:53:33 +0200637#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
638 selected_slot->slice_index = KEY_SLOT_CACHE_SLICE_INDEX;
639#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
640
641#if !defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
642 if (volatile_key_id != NULL) {
643 /* Refresh slot_idx, for when the slot is not the original
644 * selected_slot but rather unused_persistent_key_slot. */
645 slot_idx = selected_slot - global_data.key_slots;
646 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN + slot_idx;
647 }
648#endif
Ronald Crona5b894f2020-10-21 09:04:34 +0200649 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200652 }
653 status = PSA_ERROR_INSUFFICIENT_MEMORY;
654
655error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200656 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100659}
660
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100661#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100662static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100663{
664 psa_status_t status = PSA_SUCCESS;
665 uint8_t *key_data = NULL;
666 size_t key_data_length = 0;
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 status = psa_load_persistent_key(&slot->attr,
669 &key_data, &key_data_length);
670 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100671 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200673
Steven Cooreman98435dd2021-01-08 19:19:40 +0100674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100675 /* Special handling is required for loading keys associated with a
676 * dynamically registered SE interface. */
677 const psa_drv_se_t *drv;
678 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100680 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100683 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100684 goto exit;
685 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000686 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000687 status = psa_copy_key_material_into_slot(
688 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000689 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200690 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100691#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000694 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000695 goto exit;
696 }
697
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100698exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 psa_free_persistent_key_data(key_data, key_data_length);
700 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200702#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
703
Steven Cooreman6801f082021-02-19 17:21:22 +0100704#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100707{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100708 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100710 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100711 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100712 size_t key_buffer_size = 0;
713 size_t key_buffer_length = 0;
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (!psa_key_id_is_builtin(
716 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
717 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100718 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100719
720 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100721 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 slot->attr.id, &lifetime, &slot_number);
723 if (status != PSA_SUCCESS) {
724 return status;
725 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100726
Steven Cooreman966db262021-04-13 13:45:45 +0200727 /* Set required key attributes to ensure get_builtin_key can retrieve the
728 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 psa_set_key_id(&attributes, slot->attr.id);
730 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100731
Steven Cooremance487022021-04-07 18:09:53 +0200732 /* Get the full key attributes from the driver in order to be able to
733 * calculate the required buffer size. */
734 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 slot_number, &attributes,
736 NULL, 0, NULL);
737 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200738 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200740 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 }
742 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200743 }
744
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200745 /* If the key should exist according to the platform, then ask the driver
746 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
748 &key_buffer_size);
749 if (status != PSA_SUCCESS) {
750 return status;
751 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100752
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200753 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200754 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
756 if (status != PSA_SUCCESS) {
757 return status;
758 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100759
760 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 slot_number, &attributes,
762 slot->key.data, slot->key.bytes, &key_buffer_length);
763 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100764 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100766
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200767 /* Copy actual key length and core attributes into the slot on success */
768 slot->key.bytes = key_buffer_length;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100769 slot->attr = attributes;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (status != PSA_SUCCESS) {
772 psa_remove_key_data_from_memory(slot);
773 }
774 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100775}
776#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
779 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200780{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200782
783 *p_slot = NULL;
Paul Elliott838886d2024-03-12 15:26:04 +0000784 if (!psa_get_key_slots_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return PSA_ERROR_BAD_STATE;
786 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200787
Ryan Everett2f1f1722024-01-31 13:31:00 +0000788#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000789 /* We need to set status as success, otherwise CORRUPTION_DETECTED
790 * would be returned if the lock fails. */
791 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000792 /* If the key is persistent and not loaded, we cannot unlock the mutex
793 * between checking if the key is loaded and setting the slot as FULL,
794 * as otherwise another thread may load and then destroy the key
795 * in the meantime. */
796 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
797 &mbedtls_threading_key_slot_mutex));
798#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200799 /*
800 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100801 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200802 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
804 if (status != PSA_ERROR_DOES_NOT_EXIST) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000805#if defined(MBEDTLS_THREADING_C)
806 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
807 &mbedtls_threading_key_slot_mutex));
808#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 return status;
810 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200811
Steven Cooreman0bb65362021-04-06 15:09:57 +0200812 /* Loading keys from storage requires support for such a mechanism */
813#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
814 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200815
Gilles Peskinee8199f52024-06-10 11:53:33 +0200816 status = psa_reserve_free_key_slot(NULL, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000818#if defined(MBEDTLS_THREADING_C)
819 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
820 &mbedtls_threading_key_slot_mutex));
821#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return status;
823 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200824
Ronald Cronc4d1b512020-07-31 11:26:37 +0200825 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100826 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200827
Steven Cooreman6801f082021-02-19 17:21:22 +0100828 status = PSA_ERROR_DOES_NOT_EXIST;
829#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200830 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100832#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
833
834#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (status == PSA_ERROR_DOES_NOT_EXIST) {
836 status = psa_load_persistent_key_into_slot(*p_slot);
837 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100838#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (status != PSA_SUCCESS) {
841 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000842
Ryan Everett1a3573e2024-04-29 18:29:48 +0100843 /* If the key does not exist, we need to return
844 * PSA_ERROR_INVALID_HANDLE. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000846 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 }
848 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200849 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000851
852 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
853 PSA_SLOT_FULL);
854 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200856
Steven Cooreman0bb65362021-04-06 15:09:57 +0200857#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000858 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200859#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000860
Ryan Everettd4ea40d2024-04-29 18:24:58 +0100861 if (status != PSA_SUCCESS) {
862 *p_slot = NULL;
863 }
Ryan Everett2f1f1722024-01-31 13:31:00 +0000864#if defined(MBEDTLS_THREADING_C)
865 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
866 &mbedtls_threading_key_slot_mutex));
867#endif
868 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200869}
870
Ryan Everett39cc9d72023-12-21 17:57:14 +0000871psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200872{
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (slot == NULL) {
874 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200875 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000876 if ((slot->state != PSA_SLOT_FULL) &&
877 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000878 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000879 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200880
Ryan Everett39cc9d72023-12-21 17:57:14 +0000881 /* If we are the last reader and the slot is marked for deletion,
882 * we must wipe the slot here. */
883 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200884 (slot->var.occupied.registered_readers == 1)) {
Ryan Everett39cc9d72023-12-21 17:57:14 +0000885 return psa_wipe_key_slot(slot);
886 }
887
888 if (psa_key_slot_has_readers(slot)) {
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200889 slot->var.occupied.registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 return PSA_SUCCESS;
891 }
892
893 /*
894 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000895 * do our best to report if there are no registered readers. Assert with
896 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
897 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 * the function is called as part of the execution of a test suite, the
899 * execution of the test suite is stopped in error if the assertion fails.
900 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000901 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903}
904
Ryan Everetteb1722a2024-01-31 13:36:39 +0000905psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
906{
907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
908#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000909 /* We need to set status as success, otherwise CORRUPTION_DETECTED
910 * would be returned if the lock fails. */
911 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000912 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
913 &mbedtls_threading_key_slot_mutex));
914#endif
915 status = psa_unregister_read(slot);
916#if defined(MBEDTLS_THREADING_C)
917 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
918 &mbedtls_threading_key_slot_mutex));
919#endif
920 return status;
921}
922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
924 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200925{
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200927#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100928 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
930 if (driver != NULL) {
931 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200932 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 }
934 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200935 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100936#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200937 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
939
Steven Cooreman98435dd2021-01-08 19:19:40 +0100940 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200943 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 return PSA_SUCCESS;
945 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200946}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200949{
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200951 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return PSA_SUCCESS;
953 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200954 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200955#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
957 return PSA_ERROR_INVALID_ARGUMENT;
958 } else {
959 return PSA_SUCCESS;
960 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200961#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200963#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200964 }
Gilles Peskined167b942019-04-19 18:19:40 +0200965}
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100968{
Archana0dc86b52021-07-14 13:59:48 +0530969#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
970 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100971 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200972 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 status = psa_get_and_lock_key_slot(key, &slot);
975 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200976 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000978 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100982 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200983
984 *handle = key;
985
Ryan Everette110a4c2024-02-22 10:43:03 +0000986 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200987
Archana0dc86b52021-07-14 13:59:48 +0530988#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200989 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200990 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530992#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100993}
994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100996{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000997 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +0200998 psa_key_slot_t *slot;
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (psa_key_handle_is_null(handle)) {
1001 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +00001002 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001003
Ryan Everett3af9bc12024-01-30 17:21:57 +00001004#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +00001005 /* We need to set status as success, otherwise CORRUPTION_DETECTED
1006 * would be returned if the lock fails. */
1007 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +00001008 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1009 &mbedtls_threading_key_slot_mutex));
1010#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
1012 if (status != PSA_SUCCESS) {
1013 if (status == PSA_ERROR_DOES_NOT_EXIST) {
1014 status = PSA_ERROR_INVALID_HANDLE;
1015 }
Ryan Everett3af9bc12024-01-30 17:21:57 +00001016#if defined(MBEDTLS_THREADING_C)
1017 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1018 &mbedtls_threading_key_slot_mutex));
1019#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 return status;
1021 }
Ryan Everettf23336e2024-01-24 11:39:21 +00001022
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001023 if (slot->var.occupied.registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +00001024 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +00001025 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +00001026 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 }
Ryan Everettf23336e2024-01-24 11:39:21 +00001028#if defined(MBEDTLS_THREADING_C)
1029 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1030 &mbedtls_threading_key_slot_mutex));
1031#endif
1032
1033 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +01001034}
1035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +02001037{
Ryan Everett3af9bc12024-01-30 17:21:57 +00001038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +02001039 psa_key_slot_t *slot;
1040
Ryan Everettb0821952024-01-24 11:42:32 +00001041#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +00001042 /* We need to set status as success, otherwise CORRUPTION_DETECTED
1043 * would be returned if the lock fails. */
1044 status = PSA_SUCCESS;
Ryan Everettb0821952024-01-24 11:42:32 +00001045 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1046 &mbedtls_threading_key_slot_mutex));
1047#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +00001048 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
1049 if (status != PSA_SUCCESS) {
1050#if defined(MBEDTLS_THREADING_C)
1051 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1052 &mbedtls_threading_key_slot_mutex));
1053#endif
1054 return status;
1055 }
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001058 (slot->var.occupied.registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +00001059 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +00001060 } else {
Ryan Everettb0821952024-01-24 11:42:32 +00001061 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Ryan Everettb0821952024-01-24 11:42:32 +00001063#if defined(MBEDTLS_THREADING_C)
1064 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1065 &mbedtls_threading_key_slot_mutex));
1066#endif
1067
1068 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +02001069}
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +02001072{
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +02001074
Gilles Peskine5064af62024-06-07 13:53:28 +02001075 for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
Gilles Peskinee8199f52024-06-10 11:53:33 +02001076#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1077 if (global_data.key_slices[slice_idx] == NULL) {
1078 continue;
1079 }
1080#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine5064af62024-06-07 13:53:28 +02001081 for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
1082 const psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
Gilles Peskine5064af62024-06-07 13:53:28 +02001083 if (slot->state == PSA_SLOT_EMPTY) {
1084 ++stats->empty_slots;
1085 continue;
1086 }
Gilles Peskinee8199f52024-06-10 11:53:33 +02001087 if (psa_key_slot_has_readers(slot)) {
1088 ++stats->locked_slots;
1089 }
Gilles Peskine5064af62024-06-07 13:53:28 +02001090 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1091 ++stats->volatile_slots;
1092 } else {
1093 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
1094 ++stats->persistent_slots;
1095 if (id > stats->max_open_internal_key_id) {
1096 stats->max_open_internal_key_id = id;
1097 }
1098 }
1099 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
1100 PSA_KEY_LOCATION_LOCAL_STORAGE) {
1101 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
1102 ++stats->external_slots;
1103 if (id > stats->max_open_external_key_id) {
1104 stats->max_open_external_key_id = id;
1105 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +02001107 }
1108 }
1109}
1110
Gilles Peskine961849f2018-11-30 18:54:54 +01001111#endif /* MBEDTLS_PSA_CRYPTO_C */