blob: 216e0c27cf10015bdf3b125905ea403d8b7840f2 [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
David Horstmanna8e13d72024-08-21 14:41:06 +010080/* Size of the last slice containing the cache of persistent and built-in keys. */
Gilles Peskinee8199f52024-06-10 11:53:33 +020081#define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
82
David Horstmanna8e13d72024-08-21 14:41:06 +010083/* Volatile keys are stored in slices 0 through
84 * (KEY_SLOT_VOLATILE_SLICE_COUNT - 1) inclusive.
Gilles Peskinee8199f52024-06-10 11:53:33 +020085 * Each slice is twice the size of the previous slice.
86 * Volatile key identifiers encode the slice number as follows:
87 * bits 30..31: 0b10 (mandated by the PSA Crypto specification).
88 * bits 25..29: slice index (0...KEY_SLOT_VOLATILE_SLICE_COUNT-1)
89 * bits 0..24: slot index in slice
90 */
91#define KEY_ID_SLOT_INDEX_WIDTH 25u
92#define KEY_ID_SLICE_INDEX_WIDTH 5u
93
94#define KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH 16u
95#define KEY_SLOT_VOLATILE_SLICE_COUNT 22u
96#define KEY_SLICE_COUNT (KEY_SLOT_VOLATILE_SLICE_COUNT + 1u)
97#define KEY_SLOT_CACHE_SLICE_INDEX KEY_SLOT_VOLATILE_SLICE_COUNT
98
99#if KEY_ID_SLICE_INDEX_WIDTH + KEY_ID_SLOT_INDEX_WIDTH > 30
100#error "Not enough room in volatile key IDs for slice index and slot index"
101#endif
David Horstmann43124912024-08-21 14:48:32 +0100102#if KEY_SLOT_VOLATILE_SLICE_COUNT > (1 << KEY_ID_SLICE_INDEX_WIDTH)
Gilles Peskinee8199f52024-06-10 11:53:33 +0200103#error "Too many slices to fit the slice index in a volatile key ID"
104#endif
105#define KEY_SLICE_LENGTH_MAX \
106 (KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << (KEY_SLOT_VOLATILE_SLICE_COUNT - 1))
107#if KEY_SLICE_LENGTH_MAX > 1 << KEY_ID_SLOT_INDEX_WIDTH
108#error "Not enough room in volatile key IDs for a slot index in the largest slice"
109#endif
110#if KEY_ID_SLICE_INDEX_WIDTH > 8
111#error "Slice index does not fit in uint8_t for psa_key_slot_t::slice_index"
112#endif
113
David Horstmann9183ba12024-08-21 15:38:44 +0100114MBEDTLS_STATIC_ASSERT((KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH
115 & (SIZE_MAX >> (KEY_SLOT_VOLATILE_SLICE_COUNT - 1)))
116 == KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH,
117 "Maximum slice length overflows size_t");
118
Gilles Peskinee8199f52024-06-10 11:53:33 +0200119
120/* Calculate the volatile key id to use for a given slot.
121 * This function assumes valid parameter values. */
122static psa_key_id_t volatile_key_id_of_index(size_t slice_idx,
123 size_t slot_idx)
124{
Gilles Peskineac43de02024-06-13 20:36:50 +0200125 /* We assert above that the slice and slot indexes fit in separate
126 * bit-fields inside psa_key_id_t, which is a 32-bit type per the
127 * PSA Cryptography specification. */
128 return (psa_key_id_t) (0x40000000u |
129 (slice_idx << KEY_ID_SLOT_INDEX_WIDTH) |
130 slot_idx);
Gilles Peskinee8199f52024-06-10 11:53:33 +0200131}
132
133/* Calculate the slice containing the given volatile key.
134 * This function assumes valid parameter values. */
135static size_t slice_index_of_volatile_key_id(psa_key_id_t key_id)
136{
137 size_t mask = (1LU << KEY_ID_SLICE_INDEX_WIDTH) - 1;
138 return (key_id >> KEY_ID_SLOT_INDEX_WIDTH) & mask;
139}
140
141/* Calculate the index of the slot containing the given volatile key.
142 * This function assumes valid parameter values. */
143static size_t slot_index_of_volatile_key_id(psa_key_id_t key_id)
144{
145 return key_id & ((1LU << KEY_ID_SLOT_INDEX_WIDTH) - 1);
146}
147
148/* In global_data.first_free_slot_index, use this special value to
149 * indicate that the slice is full. */
150#define FREE_SLOT_INDEX_NONE ((size_t) -1)
151
Gilles Peskine3bc9d2b2024-06-21 00:09:07 +0200152#if defined(MBEDTLS_TEST_HOOKS)
153size_t psa_key_slot_volatile_slice_count(void)
154{
155 return KEY_SLOT_VOLATILE_SLICE_COUNT;
156}
157#endif
158
Gilles Peskinee8199f52024-06-10 11:53:33 +0200159#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
160
161/* Static key store.
162 *
163 * All the keys (volatile or persistent) are in a single slice.
164 * We only use slices as a concept to allow some differences between
165 * static and dynamic key store management to be buried in auxiliary
166 * functions.
167 */
168
Gilles Peskine5064af62024-06-07 13:53:28 +0200169#define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
170#define KEY_SLICE_COUNT 1u
171#define KEY_SLOT_CACHE_SLICE_INDEX 0
172
Gilles Peskinee8199f52024-06-10 11:53:33 +0200173#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
174
Gilles Peskine5064af62024-06-07 13:53:28 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176typedef struct {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200177#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
178 psa_key_slot_t *key_slices[KEY_SLICE_COUNT];
179 size_t first_free_slot_index[KEY_SLOT_VOLATILE_SLICE_COUNT];
180#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Steven Cooreman863470a2021-02-15 14:03:19 +0100181 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskinee8199f52024-06-10 11:53:33 +0200182#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Dave Rodgman164614a2023-08-16 17:56:28 +0100183 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100184} psa_global_data_t;
185
Gilles Peskine2e14bd32018-12-12 14:05:08 +0100186static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100187
Paul Elliott838886d2024-03-12 15:26:04 +0000188static uint8_t psa_get_key_slots_initialized(void)
189{
Paul Elliott838886d2024-03-12 15:26:04 +0000190 uint8_t initialized;
191
192#if defined(MBEDTLS_THREADING_C)
193 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
194#endif /* defined(MBEDTLS_THREADING_C) */
195
196 initialized = global_data.key_slots_initialized;
197
198#if defined(MBEDTLS_THREADING_C)
199 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
200#endif /* defined(MBEDTLS_THREADING_C) */
201
202 return initialized;
203}
204
Gilles Peskine5064af62024-06-07 13:53:28 +0200205
206
207/** The length of the given slice in the key slot table.
208 *
209 * \param slice_idx The slice number. It must satisfy
210 * 0 <= slice_idx < KEY_SLICE_COUNT.
211 *
212 * \return The number of elements in the given slice.
213 */
214static inline size_t key_slice_length(size_t slice_idx);
215
216/** Get a pointer to the slot where the given volatile key is located.
217 *
218 * \param key_id The key identifier. It must be a valid volatile key
219 * identifier.
220 * \return A pointer to the only slot that the given key
221 * can be in. Note that the slot may be empty or
222 * contain a different key.
223 */
224static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id);
225
226/** Get a pointer to an entry in the persistent key cache.
227 *
228 * \param slot_idx The index in the table. It must satisfy
229 * 0 <= slot_idx < PERSISTENT_KEY_CACHE_COUNT.
230 * \return A pointer to the slot containing the given
231 * persistent key cache entry.
232 */
233static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx);
234
235/** Get a pointer to a slot given by slice and index.
236 *
237 * \param slice_idx The slice number. It must satisfy
238 * 0 <= slice_idx < KEY_SLICE_COUNT.
239 * \param slot_idx An index in the given slice. It must satisfy
240 * 0 <= slot_idx < key_slice_length(slice_idx).
241 *
242 * \return A pointer to the given slot.
243 */
244static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx);
245
Gilles Peskinee8199f52024-06-10 11:53:33 +0200246#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
247
Gilles Peskine3bc9d2b2024-06-21 00:09:07 +0200248#if defined(MBEDTLS_TEST_HOOKS)
249size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)(size_t slice_idx) = NULL;
250#endif
251
Gilles Peskinee8199f52024-06-10 11:53:33 +0200252static inline size_t key_slice_length(size_t slice_idx)
253{
254 if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) {
255 return PERSISTENT_KEY_CACHE_COUNT;
256 } else {
Gilles Peskine3bc9d2b2024-06-21 00:09:07 +0200257#if defined(MBEDTLS_TEST_HOOKS)
258 if (mbedtls_test_hook_psa_volatile_key_slice_length != NULL) {
259 return mbedtls_test_hook_psa_volatile_key_slice_length(slice_idx);
260 }
261#endif
Gilles Peskinee8199f52024-06-10 11:53:33 +0200262 return KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << slice_idx;
263 }
264}
265
266static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
267{
268 size_t slice_idx = slice_index_of_volatile_key_id(key_id);
269 if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) {
270 return NULL;
271 }
272 size_t slot_idx = slot_index_of_volatile_key_id(key_id);
273 if (slot_idx >= key_slice_length(slice_idx)) {
274 return NULL;
275 }
276 psa_key_slot_t *slice = global_data.key_slices[slice_idx];
277 if (slice == NULL) {
278 return NULL;
279 }
280 return &slice[slot_idx];
281}
282
283static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
284{
285 return &global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX][slot_idx];
286}
287
288static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
289{
290 return &global_data.key_slices[slice_idx][slot_idx];
291}
292
293#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
294
Gilles Peskine5064af62024-06-07 13:53:28 +0200295static inline size_t key_slice_length(size_t slice_idx)
296{
297 (void) slice_idx;
298 return ARRAY_LENGTH(global_data.key_slots);
299}
300
301static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
302{
303 MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
304 PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
305 "The key slot array is larger than the volatile key ID range");
306 return &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
307}
308
309static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
310{
311 return &global_data.key_slots[slot_idx];
312}
313
314static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
315{
316 (void) slice_idx;
317 return &global_data.key_slots[slot_idx];
318}
319
Gilles Peskinee8199f52024-06-10 11:53:33 +0200320#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
321
322
Gilles Peskine5064af62024-06-07 13:53:28 +0200323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +0200325{
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
329 (key_id <= PSA_KEY_ID_USER_MAX)) {
330 return 1;
331 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (vendor_ok &&
334 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
335 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
336 return 1;
337 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +0200340}
341
Ronald Cron5c522922020-11-14 16:35:34 +0100342/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200343 *
Ronald Cron5c522922020-11-14 16:35:34 +0100344 * The descriptions of volatile keys and loaded persistent keys are
345 * stored in key slots. This function returns a pointer to the key slot
346 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200347 *
Ronald Cron5c522922020-11-14 16:35:34 +0100348 * The function searches the key slots containing the description of the key
349 * with \p key identifier. The function does only read accesses to the key
350 * slots. The function does not load any persistent key thus does not access
351 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200352 *
Ronald Cron5c522922020-11-14 16:35:34 +0100353 * For volatile key identifiers, only one key slot is queried as a volatile
354 * key with identifier key_id can only be stored in slot of index
355 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +0200356 *
Ronald Cron5c522922020-11-14 16:35:34 +0100357 * On success, the function locks the key slot. It is the responsibility of
358 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200359 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000360 * If multi-threading is enabled, the caller must hold the
361 * global key slot mutex.
362 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200363 * \param key Key identifier to query.
364 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
365 * key slot containing the description of the key
366 * identified by \p key.
367 *
Ronald Cron96783552020-10-19 12:06:30 +0200368 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200369 * The pointer to the key slot containing the description of the key
370 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200371 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200372 * \p key is not a valid key identifier.
373 * \retval #PSA_ERROR_DOES_NOT_EXIST
374 * There is no key with key identifier \p key in the key slots.
375 */
Ronald Cron5c522922020-11-14 16:35:34 +0100376static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100378{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100381 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200382 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (psa_key_id_is_volatile(key_id)) {
Gilles Peskine5064af62024-06-07 13:53:28 +0200385 slot = get_volatile_key_slot(key_id);
Ronald Cron1d12d872020-11-18 17:21:22 +0100386
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000387 /* Check if both the PSA key identifier key_id and the owner
388 * identifier of key match those of the key slot. */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200389 if (slot != NULL &&
390 slot->state == PSA_SLOT_FULL &&
391 mbedtls_svc_key_id_equal(key, slot->attr.id)) {
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000392 status = PSA_SUCCESS;
393 } else {
394 status = PSA_ERROR_DOES_NOT_EXIST;
395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 } else {
397 if (!psa_is_valid_key_id(key, 1)) {
398 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200399 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100400
Gilles Peskine5064af62024-06-07 13:53:28 +0200401 for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
402 slot = get_persistent_key_slot(slot_idx);
Ryan Everett098c6652024-01-03 13:03:36 +0000403 /* Only consider slots which are in a full state. */
404 if ((slot->state == PSA_SLOT_FULL) &&
405 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 break;
407 }
408 }
409 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100410 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200411 }
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000414 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100416 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200418 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200421}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100424{
Gilles Peskinee8199f52024-06-10 11:53:33 +0200425#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
426 global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] =
427 mbedtls_calloc(PERSISTENT_KEY_CACHE_COUNT,
428 sizeof(*global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX]));
429 if (global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] == NULL) {
430 return PSA_ERROR_INSUFFICIENT_MEMORY;
431 }
432#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Ryan Everett558da2f2024-01-19 12:59:28 +0000433 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100434 * guarantee that the key slots are initialized to all-zero, which
Paul Elliott838886d2024-03-12 15:26:04 +0000435 * means that all the key slots are in a valid, empty state. The global
436 * data mutex is already held when calling this function, so no need to
437 * lock it here, to set the flag. */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200438#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
439
Gilles Peskine66fb1262018-12-10 16:29:04 +0100440 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100442}
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100445{
Gilles Peskine5064af62024-06-07 13:53:28 +0200446 for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200447#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
448 if (global_data.key_slices[slice_idx] == NULL) {
449 continue;
450 }
451#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine5064af62024-06-07 13:53:28 +0200452 for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
453 psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
Gilles Peskinea81282c2024-06-10 15:41:38 +0200454#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
455 /* When MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled, calling
456 * psa_wipe_key_slot() on an unused slot is useless, but it
457 * happens to work (because we flip the state to PENDING_DELETION).
458 *
459 * When MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled,
460 * psa_wipe_key_slot() needs to have a valid slice_index
461 * field, but that value might not be correct in a
462 * free slot, so we must not call it.
463 *
464 * Bypass the call to psa_wipe_key_slot() if the slot is empty,
465 * but only if MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, to save
466 * a few bytes of code size otherwise.
467 */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200468 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200469 continue;
470 }
Gilles Peskinea81282c2024-06-10 15:41:38 +0200471#endif
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200472 slot->var.occupied.registered_readers = 1;
Gilles Peskine5064af62024-06-07 13:53:28 +0200473 slot->state = PSA_SLOT_PENDING_DELETION;
474 (void) psa_wipe_key_slot(slot);
475 }
Gilles Peskinee8199f52024-06-10 11:53:33 +0200476#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
477 mbedtls_free(global_data.key_slices[slice_idx]);
478 global_data.key_slices[slice_idx] = NULL;
479#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100480 }
Gilles Peskinee8199f52024-06-10 11:53:33 +0200481
482#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
483 for (size_t slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) {
484 global_data.first_free_slot_index[slice_idx] = 0;
485 }
486#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
487
Paul Elliott838886d2024-03-12 15:26:04 +0000488 /* The global data mutex is already held when calling this function. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100489 global_data.key_slots_initialized = 0;
490}
491
Gilles Peskinee8199f52024-06-10 11:53:33 +0200492#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
493
494static psa_status_t psa_allocate_volatile_key_slot(psa_key_id_t *key_id,
495 psa_key_slot_t **p_slot)
496{
497 size_t slice_idx;
498 for (slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) {
499 if (global_data.first_free_slot_index[slice_idx] != FREE_SLOT_INDEX_NONE) {
500 break;
501 }
502 }
503 if (slice_idx == KEY_SLOT_VOLATILE_SLICE_COUNT) {
504 return PSA_ERROR_INSUFFICIENT_MEMORY;
505 }
506
507 if (global_data.key_slices[slice_idx] == NULL) {
508 global_data.key_slices[slice_idx] =
509 mbedtls_calloc(key_slice_length(slice_idx),
510 sizeof(psa_key_slot_t));
511 if (global_data.key_slices[slice_idx] == NULL) {
512 return PSA_ERROR_INSUFFICIENT_MEMORY;
513 }
514 }
515 psa_key_slot_t *slice = global_data.key_slices[slice_idx];
516
517 size_t slot_idx = global_data.first_free_slot_index[slice_idx];
518 *key_id = volatile_key_id_of_index(slice_idx, slot_idx);
519
520 psa_key_slot_t *slot = &slice[slot_idx];
521 size_t next_free = slot_idx + 1 + slot->var.free.next_free_relative_to_next;
522 if (next_free >= key_slice_length(slice_idx)) {
523 next_free = FREE_SLOT_INDEX_NONE;
524 }
525 global_data.first_free_slot_index[slice_idx] = next_free;
526 /* The .next_free field is not meaningful when the slot is not free,
527 * so give it the same content as freshly initialized memory. */
528 slot->var.free.next_free_relative_to_next = 0;
529
530 psa_status_t status = psa_key_slot_state_transition(slot,
531 PSA_SLOT_EMPTY,
532 PSA_SLOT_FILLING);
533 if (status != PSA_SUCCESS) {
534 /* The only reason for failure is if the slot state was not empty.
535 * This indicates that something has gone horribly wrong.
536 * In this case, we leave the slot out of the free list, and stop
537 * modifying it. This minimizes any further corruption. The slot
538 * is a memory leak, but that's a lesser evil. */
539 return status;
540 }
541
542 *p_slot = slot;
Gilles Peskineac43de02024-06-13 20:36:50 +0200543 /* We assert at compile time that the slice index fits in uint8_t. */
544 slot->slice_index = (uint8_t) slice_idx;
Gilles Peskinee8199f52024-06-10 11:53:33 +0200545 return PSA_SUCCESS;
546}
547
548psa_status_t psa_free_key_slot(size_t slice_idx,
549 psa_key_slot_t *slot)
550{
551
552 if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) {
553 /* This is a cache entry. We don't maintain a free list, so
554 * there's nothing to do. */
555 return PSA_SUCCESS;
556 }
557 if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) {
558 return PSA_ERROR_CORRUPTION_DETECTED;
559 }
560
561 psa_key_slot_t *slice = global_data.key_slices[slice_idx];
562 psa_key_slot_t *slice_end = slice + key_slice_length(slice_idx);
563 if (slot < slice || slot >= slice_end) {
564 /* The slot isn't actually in the slice! We can't detect that
565 * condition for sure, because the pointer comparison itself is
566 * undefined behavior in that case. That same condition makes the
567 * subtraction to calculate the slot index also UB.
568 * Give up now to avoid causing further corruption.
569 */
570 return PSA_ERROR_CORRUPTION_DETECTED;
571 }
572 size_t slot_idx = slot - slice;
573
574 size_t next_free = global_data.first_free_slot_index[slice_idx];
575 if (next_free >= key_slice_length(slice_idx)) {
576 /* The slot was full. The newly freed slot thus becomes the
577 * end of the free list. */
578 next_free = key_slice_length(slice_idx);
579 }
580 global_data.first_free_slot_index[slice_idx] = slot_idx;
Gilles Peskineac43de02024-06-13 20:36:50 +0200581 slot->var.free.next_free_relative_to_next =
582 (int32_t) next_free - (int32_t) slot_idx - 1;
Gilles Peskinee8199f52024-06-10 11:53:33 +0200583
584 return PSA_SUCCESS;
585}
586#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
587
Ryan Everett2afb5162023-12-22 15:59:45 +0000588psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
589 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100590{
Ronald Crona5b894f2020-10-21 09:04:34 +0200591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200592 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000593 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200594
Paul Elliott838886d2024-03-12 15:26:04 +0000595 if (!psa_get_key_slots_initialized()) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200596 status = PSA_ERROR_BAD_STATE;
597 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100598 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200599
Gilles Peskinee8199f52024-06-10 11:53:33 +0200600#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
Gilles Peskinea81282c2024-06-10 15:41:38 +0200601 if (volatile_key_id != NULL) {
Gilles Peskinee8199f52024-06-10 11:53:33 +0200602 return psa_allocate_volatile_key_slot(volatile_key_id, p_slot);
Gilles Peskinee8199f52024-06-10 11:53:33 +0200603 }
Gilles Peskinea81282c2024-06-10 15:41:38 +0200604#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskinee8199f52024-06-10 11:53:33 +0200605
606 /* With a dynamic key store, allocate an entry in the cache slice,
607 * applicable only to non-volatile keys that get cached in RAM.
608 * With a static key store, allocate an entry in the sole slice,
609 * applicable to all keys. */
Ryan Everett2afb5162023-12-22 15:59:45 +0000610 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine5064af62024-06-07 13:53:28 +0200611 for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
612 psa_key_slot_t *slot = get_key_slot(KEY_SLOT_CACHE_SLICE_INDEX, slot_idx);
Ryan Everett2afb5162023-12-22 15:59:45 +0000613 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200614 selected_slot = slot;
615 break;
616 }
617
Ryan Everett2afb5162023-12-22 15:59:45 +0000618 if ((unused_persistent_key_slot == NULL) &&
619 (slot->state == PSA_SLOT_FULL) &&
620 (!psa_key_slot_has_readers(slot)) &&
621 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
622 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200624 }
625
626 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100627 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100628 * slot containing the description of a persistent key, recycle the first
629 * such key slot we encountered. If we later need to operate on the
630 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100631 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200632 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000634 (unused_persistent_key_slot != NULL)) {
635 selected_slot = unused_persistent_key_slot;
636 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000637 status = psa_wipe_key_slot(selected_slot);
638 if (status != PSA_SUCCESS) {
639 goto error;
640 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200641 }
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000644 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
645 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000647 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100649
Gilles Peskinee8199f52024-06-10 11:53:33 +0200650#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
651 selected_slot->slice_index = KEY_SLOT_CACHE_SLICE_INDEX;
652#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
653
654#if !defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
655 if (volatile_key_id != NULL) {
656 /* Refresh slot_idx, for when the slot is not the original
657 * selected_slot but rather unused_persistent_key_slot. */
658 slot_idx = selected_slot - global_data.key_slots;
659 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN + slot_idx;
660 }
661#endif
Ronald Crona5b894f2020-10-21 09:04:34 +0200662 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200665 }
666 status = PSA_ERROR_INSUFFICIENT_MEMORY;
667
668error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200669 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100672}
673
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100674#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100675static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100676{
677 psa_status_t status = PSA_SUCCESS;
678 uint8_t *key_data = NULL;
679 size_t key_data_length = 0;
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 status = psa_load_persistent_key(&slot->attr,
682 &key_data, &key_data_length);
683 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100684 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200686
Steven Cooreman98435dd2021-01-08 19:19:40 +0100687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100688 /* Special handling is required for loading keys associated with a
689 * dynamically registered SE interface. */
690 const psa_drv_se_t *drv;
691 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100693 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100696 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100697 goto exit;
698 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000699 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000700 status = psa_copy_key_material_into_slot(
701 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000702 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200703 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100704#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000707 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000708 goto exit;
709 }
710
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100711exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 psa_free_persistent_key_data(key_data, key_data_length);
713 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100714}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200715#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
716
Steven Cooreman6801f082021-02-19 17:21:22 +0100717#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100720{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100721 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100723 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100724 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100725 size_t key_buffer_size = 0;
726 size_t key_buffer_length = 0;
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (!psa_key_id_is_builtin(
729 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
730 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100731 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100732
733 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100734 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 slot->attr.id, &lifetime, &slot_number);
736 if (status != PSA_SUCCESS) {
737 return status;
738 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100739
Steven Cooreman966db262021-04-13 13:45:45 +0200740 /* Set required key attributes to ensure get_builtin_key can retrieve the
741 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 psa_set_key_id(&attributes, slot->attr.id);
743 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100744
Steven Cooremance487022021-04-07 18:09:53 +0200745 /* Get the full key attributes from the driver in order to be able to
746 * calculate the required buffer size. */
747 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 slot_number, &attributes,
749 NULL, 0, NULL);
750 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200751 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200753 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 }
755 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200756 }
757
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200758 /* If the key should exist according to the platform, then ask the driver
759 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
761 &key_buffer_size);
762 if (status != PSA_SUCCESS) {
763 return status;
764 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100765
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200766 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200767 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
769 if (status != PSA_SUCCESS) {
770 return status;
771 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100772
773 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 slot_number, &attributes,
775 slot->key.data, slot->key.bytes, &key_buffer_length);
776 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100777 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100779
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200780 /* Copy actual key length and core attributes into the slot on success */
781 slot->key.bytes = key_buffer_length;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100782 slot->attr = attributes;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100783exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (status != PSA_SUCCESS) {
785 psa_remove_key_data_from_memory(slot);
786 }
787 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100788}
789#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
792 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200793{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200795
796 *p_slot = NULL;
Paul Elliott838886d2024-03-12 15:26:04 +0000797 if (!psa_get_key_slots_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 return PSA_ERROR_BAD_STATE;
799 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200800
Ryan Everett2f1f1722024-01-31 13:31:00 +0000801#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000802 /* We need to set status as success, otherwise CORRUPTION_DETECTED
803 * would be returned if the lock fails. */
804 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000805 /* If the key is persistent and not loaded, we cannot unlock the mutex
806 * between checking if the key is loaded and setting the slot as FULL,
807 * as otherwise another thread may load and then destroy the key
808 * in the meantime. */
809 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
810 &mbedtls_threading_key_slot_mutex));
811#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200812 /*
813 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100814 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200815 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
817 if (status != PSA_ERROR_DOES_NOT_EXIST) {
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
Steven Cooreman0bb65362021-04-06 15:09:57 +0200825 /* Loading keys from storage requires support for such a mechanism */
826#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
827 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200828
Gilles Peskinee8199f52024-06-10 11:53:33 +0200829 status = psa_reserve_free_key_slot(NULL, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000831#if defined(MBEDTLS_THREADING_C)
832 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
833 &mbedtls_threading_key_slot_mutex));
834#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 return status;
836 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200837
Ronald Cronc4d1b512020-07-31 11:26:37 +0200838 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100839 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200840
Steven Cooreman6801f082021-02-19 17:21:22 +0100841 status = PSA_ERROR_DOES_NOT_EXIST;
842#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200843 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100845#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
846
847#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (status == PSA_ERROR_DOES_NOT_EXIST) {
849 status = psa_load_persistent_key_into_slot(*p_slot);
850 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100851#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (status != PSA_SUCCESS) {
854 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000855
Ryan Everett1a3573e2024-04-29 18:29:48 +0100856 /* If the key does not exist, we need to return
857 * PSA_ERROR_INVALID_HANDLE. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000859 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 }
861 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200862 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000864
865 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
866 PSA_SLOT_FULL);
867 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200869
Steven Cooreman0bb65362021-04-06 15:09:57 +0200870#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000871 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200872#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000873
Ryan Everettd4ea40d2024-04-29 18:24:58 +0100874 if (status != PSA_SUCCESS) {
875 *p_slot = NULL;
876 }
Ryan Everett2f1f1722024-01-31 13:31:00 +0000877#if defined(MBEDTLS_THREADING_C)
878 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
879 &mbedtls_threading_key_slot_mutex));
880#endif
881 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200882}
883
Ryan Everett39cc9d72023-12-21 17:57:14 +0000884psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200885{
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (slot == NULL) {
887 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200888 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000889 if ((slot->state != PSA_SLOT_FULL) &&
890 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000891 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000892 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200893
Ryan Everett39cc9d72023-12-21 17:57:14 +0000894 /* If we are the last reader and the slot is marked for deletion,
895 * we must wipe the slot here. */
896 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200897 (slot->var.occupied.registered_readers == 1)) {
Ryan Everett39cc9d72023-12-21 17:57:14 +0000898 return psa_wipe_key_slot(slot);
899 }
900
901 if (psa_key_slot_has_readers(slot)) {
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200902 slot->var.occupied.registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 return PSA_SUCCESS;
904 }
905
906 /*
907 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000908 * do our best to report if there are no registered readers. Assert with
909 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
910 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 * the function is called as part of the execution of a test suite, the
912 * execution of the test suite is stopped in error if the assertion fails.
913 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000914 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200916}
917
Ryan Everetteb1722a2024-01-31 13:36:39 +0000918psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
919{
920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
921#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000922 /* We need to set status as success, otherwise CORRUPTION_DETECTED
923 * would be returned if the lock fails. */
924 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000925 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
926 &mbedtls_threading_key_slot_mutex));
927#endif
928 status = psa_unregister_read(slot);
929#if defined(MBEDTLS_THREADING_C)
930 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
931 &mbedtls_threading_key_slot_mutex));
932#endif
933 return status;
934}
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
937 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200938{
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200940#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100941 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
943 if (driver != NULL) {
944 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200945 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 }
947 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200948 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100949#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200950 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Steven Cooreman98435dd2021-01-08 19:19:40 +0100953 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200956 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return PSA_SUCCESS;
958 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200959}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200962{
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200964 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return PSA_SUCCESS;
966 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200967 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200968#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
970 return PSA_ERROR_INVALID_ARGUMENT;
971 } else {
972 return PSA_SUCCESS;
973 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200974#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200976#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200977 }
Gilles Peskined167b942019-04-19 18:19:40 +0200978}
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100981{
Archana0dc86b52021-07-14 13:59:48 +0530982#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
983 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100984 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200985 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 status = psa_get_and_lock_key_slot(key, &slot);
988 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200989 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000991 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100995 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200996
997 *handle = key;
998
Ryan Everette110a4c2024-02-22 10:43:03 +0000999 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +02001000
Archana0dc86b52021-07-14 13:59:48 +05301001#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +02001002 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +02001003 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +05301005#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +01001006}
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +01001009{
Ryan Everett3af9bc12024-01-30 17:21:57 +00001010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +02001011 psa_key_slot_t *slot;
1012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (psa_key_handle_is_null(handle)) {
1014 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +00001015 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001016
Ryan Everett3af9bc12024-01-30 17:21:57 +00001017#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +00001018 /* We need to set status as success, otherwise CORRUPTION_DETECTED
1019 * would be returned if the lock fails. */
1020 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +00001021 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1022 &mbedtls_threading_key_slot_mutex));
1023#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
1025 if (status != PSA_SUCCESS) {
1026 if (status == PSA_ERROR_DOES_NOT_EXIST) {
1027 status = PSA_ERROR_INVALID_HANDLE;
1028 }
Ryan Everett3af9bc12024-01-30 17:21:57 +00001029#if defined(MBEDTLS_THREADING_C)
1030 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1031 &mbedtls_threading_key_slot_mutex));
1032#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return status;
1034 }
Ryan Everettf23336e2024-01-24 11:39:21 +00001035
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001036 if (slot->var.occupied.registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +00001037 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +00001038 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +00001039 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 }
Ryan Everettf23336e2024-01-24 11:39:21 +00001041#if defined(MBEDTLS_THREADING_C)
1042 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1043 &mbedtls_threading_key_slot_mutex));
1044#endif
1045
1046 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +01001047}
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +02001050{
Ryan Everett3af9bc12024-01-30 17:21:57 +00001051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +02001052 psa_key_slot_t *slot;
1053
Ryan Everettb0821952024-01-24 11:42:32 +00001054#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +00001055 /* We need to set status as success, otherwise CORRUPTION_DETECTED
1056 * would be returned if the lock fails. */
1057 status = PSA_SUCCESS;
Ryan Everettb0821952024-01-24 11:42:32 +00001058 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1059 &mbedtls_threading_key_slot_mutex));
1060#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +00001061 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
1062 if (status != PSA_SUCCESS) {
1063#if defined(MBEDTLS_THREADING_C)
1064 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1065 &mbedtls_threading_key_slot_mutex));
1066#endif
1067 return status;
1068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001071 (slot->var.occupied.registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +00001072 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +00001073 } else {
Ryan Everettb0821952024-01-24 11:42:32 +00001074 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Ryan Everettb0821952024-01-24 11:42:32 +00001076#if defined(MBEDTLS_THREADING_C)
1077 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1078 &mbedtls_threading_key_slot_mutex));
1079#endif
1080
1081 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +02001082}
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +02001085{
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +02001087
Gilles Peskine5064af62024-06-07 13:53:28 +02001088 for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
Gilles Peskinee8199f52024-06-10 11:53:33 +02001089#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1090 if (global_data.key_slices[slice_idx] == NULL) {
1091 continue;
1092 }
1093#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine5064af62024-06-07 13:53:28 +02001094 for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
1095 const psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
Gilles Peskine5064af62024-06-07 13:53:28 +02001096 if (slot->state == PSA_SLOT_EMPTY) {
1097 ++stats->empty_slots;
1098 continue;
1099 }
Gilles Peskinee8199f52024-06-10 11:53:33 +02001100 if (psa_key_slot_has_readers(slot)) {
1101 ++stats->locked_slots;
1102 }
Gilles Peskine5064af62024-06-07 13:53:28 +02001103 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1104 ++stats->volatile_slots;
1105 } else {
1106 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
1107 ++stats->persistent_slots;
1108 if (id > stats->max_open_internal_key_id) {
1109 stats->max_open_internal_key_id = id;
1110 }
1111 }
1112 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
1113 PSA_KEY_LOCATION_LOCAL_STORAGE) {
1114 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
1115 ++stats->external_slots;
1116 if (id > stats->max_open_external_key_id) {
1117 stats->max_open_external_key_id = id;
1118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +02001120 }
1121 }
1122}
1123
Gilles Peskine961849f2018-11-30 18:54:54 +01001124#endif /* MBEDTLS_PSA_CRYPTO_C */