blob: 7e0a1c12c1c0ab2498683cb8dd3e5f3c6d69eb58 [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 Peskine5064af62024-06-07 13:53:28 +020061#define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
62#define KEY_SLICE_COUNT 1u
63#define KEY_SLOT_CACHE_SLICE_INDEX 0
64
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010067 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Dave Rodgman164614a2023-08-16 17:56:28 +010068 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +010069} psa_global_data_t;
70
Gilles Peskine2e14bd32018-12-12 14:05:08 +010071static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010072
Paul Elliott838886d2024-03-12 15:26:04 +000073static uint8_t psa_get_key_slots_initialized(void)
74{
Paul Elliott838886d2024-03-12 15:26:04 +000075 uint8_t initialized;
76
77#if defined(MBEDTLS_THREADING_C)
78 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
79#endif /* defined(MBEDTLS_THREADING_C) */
80
81 initialized = global_data.key_slots_initialized;
82
83#if defined(MBEDTLS_THREADING_C)
84 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
85#endif /* defined(MBEDTLS_THREADING_C) */
86
87 return initialized;
88}
89
Gilles Peskine5064af62024-06-07 13:53:28 +020090
91
92/** The length of the given slice in the key slot table.
93 *
94 * \param slice_idx The slice number. It must satisfy
95 * 0 <= slice_idx < KEY_SLICE_COUNT.
96 *
97 * \return The number of elements in the given slice.
98 */
99static inline size_t key_slice_length(size_t slice_idx);
100
101/** Get a pointer to the slot where the given volatile key is located.
102 *
103 * \param key_id The key identifier. It must be a valid volatile key
104 * identifier.
105 * \return A pointer to the only slot that the given key
106 * can be in. Note that the slot may be empty or
107 * contain a different key.
108 */
109static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id);
110
111/** Get a pointer to an entry in the persistent key cache.
112 *
113 * \param slot_idx The index in the table. It must satisfy
114 * 0 <= slot_idx < PERSISTENT_KEY_CACHE_COUNT.
115 * \return A pointer to the slot containing the given
116 * persistent key cache entry.
117 */
118static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx);
119
120/** Get a pointer to a slot given by slice and index.
121 *
122 * \param slice_idx The slice number. It must satisfy
123 * 0 <= slice_idx < KEY_SLICE_COUNT.
124 * \param slot_idx An index in the given slice. It must satisfy
125 * 0 <= slot_idx < key_slice_length(slice_idx).
126 *
127 * \return A pointer to the given slot.
128 */
129static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx);
130
131static inline size_t key_slice_length(size_t slice_idx)
132{
133 (void) slice_idx;
134 return ARRAY_LENGTH(global_data.key_slots);
135}
136
137static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
138{
139 MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
140 PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
141 "The key slot array is larger than the volatile key ID range");
142 return &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
143}
144
145static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
146{
147 return &global_data.key_slots[slot_idx];
148}
149
150static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
151{
152 (void) slice_idx;
153 return &global_data.key_slots[slot_idx];
154}
155
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +0200158{
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
162 (key_id <= PSA_KEY_ID_USER_MAX)) {
163 return 1;
164 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (vendor_ok &&
167 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
168 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
169 return 1;
170 }
Ronald Crond2ed4812020-07-17 16:11:30 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +0200173}
174
Ronald Cron5c522922020-11-14 16:35:34 +0100175/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200176 *
Ronald Cron5c522922020-11-14 16:35:34 +0100177 * The descriptions of volatile keys and loaded persistent keys are
178 * stored in key slots. This function returns a pointer to the key slot
179 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200180 *
Ronald Cron5c522922020-11-14 16:35:34 +0100181 * The function searches the key slots containing the description of the key
182 * with \p key identifier. The function does only read accesses to the key
183 * slots. The function does not load any persistent key thus does not access
184 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +0200185 *
Ronald Cron5c522922020-11-14 16:35:34 +0100186 * For volatile key identifiers, only one key slot is queried as a volatile
187 * key with identifier key_id can only be stored in slot of index
188 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +0200189 *
Ronald Cron5c522922020-11-14 16:35:34 +0100190 * On success, the function locks the key slot. It is the responsibility of
191 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200192 *
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000193 * If multi-threading is enabled, the caller must hold the
194 * global key slot mutex.
195 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200196 * \param key Key identifier to query.
197 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
198 * key slot containing the description of the key
199 * identified by \p key.
200 *
Ronald Cron96783552020-10-19 12:06:30 +0200201 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200202 * The pointer to the key slot containing the description of the key
203 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200204 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200205 * \p key is not a valid key identifier.
206 * \retval #PSA_ERROR_DOES_NOT_EXIST
207 * There is no key with key identifier \p key in the key slots.
208 */
Ronald Cron5c522922020-11-14 16:35:34 +0100209static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100211{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100214 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200215 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (psa_key_id_is_volatile(key_id)) {
Gilles Peskine5064af62024-06-07 13:53:28 +0200218 slot = get_volatile_key_slot(key_id);
Ronald Cron1d12d872020-11-18 17:21:22 +0100219
Ryan Everett6ad1fd12024-01-31 13:21:33 +0000220 /* Check if both the PSA key identifier key_id and the owner
221 * identifier of key match those of the key slot. */
222 if ((slot->state == PSA_SLOT_FULL) &&
223 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
224 status = PSA_SUCCESS;
225 } else {
226 status = PSA_ERROR_DOES_NOT_EXIST;
227 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 } else {
229 if (!psa_is_valid_key_id(key, 1)) {
230 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200231 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100232
Gilles Peskine5064af62024-06-07 13:53:28 +0200233 for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
234 slot = get_persistent_key_slot(slot_idx);
Ryan Everett098c6652024-01-03 13:03:36 +0000235 /* Only consider slots which are in a full state. */
236 if ((slot->state == PSA_SLOT_FULL) &&
237 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 break;
239 }
240 }
241 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100242 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200243 }
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000246 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100248 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200250 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200253}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100256{
Ryan Everett558da2f2024-01-19 12:59:28 +0000257 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100258 * guarantee that the key slots are initialized to all-zero, which
Paul Elliott838886d2024-03-12 15:26:04 +0000259 * means that all the key slots are in a valid, empty state. The global
260 * data mutex is already held when calling this function, so no need to
261 * lock it here, to set the flag. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100262 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100264}
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100267{
Gilles Peskine5064af62024-06-07 13:53:28 +0200268 for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
269 for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
270 psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
271 slot->registered_readers = 1;
272 slot->state = PSA_SLOT_PENDING_DELETION;
273 (void) psa_wipe_key_slot(slot);
274 }
Gilles Peskine66fb1262018-12-10 16:29:04 +0100275 }
Paul Elliott838886d2024-03-12 15:26:04 +0000276 /* The global data mutex is already held when calling this function. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100277 global_data.key_slots_initialized = 0;
278}
279
Ryan Everett2afb5162023-12-22 15:59:45 +0000280psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
281 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100282{
Ronald Crona5b894f2020-10-21 09:04:34 +0200283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200284 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000285 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200286
Paul Elliott838886d2024-03-12 15:26:04 +0000287 if (!psa_get_key_slots_initialized()) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200288 status = PSA_ERROR_BAD_STATE;
289 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100290 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200291
Ryan Everett2afb5162023-12-22 15:59:45 +0000292 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine5064af62024-06-07 13:53:28 +0200293 for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
294 psa_key_slot_t *slot = get_key_slot(KEY_SLOT_CACHE_SLICE_INDEX, slot_idx);
Ryan Everett2afb5162023-12-22 15:59:45 +0000295 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200296 selected_slot = slot;
297 break;
298 }
299
Ryan Everett2afb5162023-12-22 15:59:45 +0000300 if ((unused_persistent_key_slot == NULL) &&
301 (slot->state == PSA_SLOT_FULL) &&
302 (!psa_key_slot_has_readers(slot)) &&
303 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
304 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200306 }
307
308 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100309 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100310 * slot containing the description of a persistent key, recycle the first
311 * such key slot we encountered. If we later need to operate on the
312 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100313 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200314 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000316 (unused_persistent_key_slot != NULL)) {
317 selected_slot = unused_persistent_key_slot;
318 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000319 status = psa_wipe_key_slot(selected_slot);
320 if (status != PSA_SUCCESS) {
321 goto error;
322 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200323 }
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000326 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
327 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000329 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100331
Ronald Crona5b894f2020-10-21 09:04:34 +0200332 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200334 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200337 }
338 status = PSA_ERROR_INSUFFICIENT_MEMORY;
339
340error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200341 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200342 *volatile_key_id = 0;
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100345}
346
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100347#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100348static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100349{
350 psa_status_t status = PSA_SUCCESS;
351 uint8_t *key_data = NULL;
352 size_t key_data_length = 0;
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 status = psa_load_persistent_key(&slot->attr,
355 &key_data, &key_data_length);
356 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100357 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200359
Steven Cooreman98435dd2021-01-08 19:19:40 +0100360#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100361 /* Special handling is required for loading keys associated with a
362 * dynamically registered SE interface. */
363 const psa_drv_se_t *drv;
364 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100366 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100369 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100370 goto exit;
371 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000372 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000373 status = psa_copy_key_material_into_slot(
374 slot, data->slot_number, sizeof(data->slot_number));
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000375 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200376 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000380 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000381 goto exit;
382 }
383
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 psa_free_persistent_key_data(key_data, key_data_length);
386 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100387}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200388#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
389
Steven Cooreman6801f082021-02-19 17:21:22 +0100390#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100393{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100396 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100397 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100398 size_t key_buffer_size = 0;
399 size_t key_buffer_length = 0;
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (!psa_key_id_is_builtin(
402 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
403 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100404 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100405
406 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100407 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 slot->attr.id, &lifetime, &slot_number);
409 if (status != PSA_SUCCESS) {
410 return status;
411 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100412
Steven Cooreman966db262021-04-13 13:45:45 +0200413 /* Set required key attributes to ensure get_builtin_key can retrieve the
414 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 psa_set_key_id(&attributes, slot->attr.id);
416 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100417
Steven Cooremance487022021-04-07 18:09:53 +0200418 /* Get the full key attributes from the driver in order to be able to
419 * calculate the required buffer size. */
420 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 slot_number, &attributes,
422 NULL, 0, NULL);
423 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200424 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200426 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
428 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200429 }
430
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200431 /* If the key should exist according to the platform, then ask the driver
432 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
434 &key_buffer_size);
435 if (status != PSA_SUCCESS) {
436 return status;
437 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100438
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200439 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200440 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
442 if (status != PSA_SUCCESS) {
443 return status;
444 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100445
446 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 slot_number, &attributes,
448 slot->key.data, slot->key.bytes, &key_buffer_length);
449 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100452
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200453 /* Copy actual key length and core attributes into the slot on success */
454 slot->key.bytes = key_buffer_length;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100455 slot->attr = attributes;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (status != PSA_SUCCESS) {
458 psa_remove_key_data_from_memory(slot);
459 }
460 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100461}
462#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
465 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200466{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200468
469 *p_slot = NULL;
Paul Elliott838886d2024-03-12 15:26:04 +0000470 if (!psa_get_key_slots_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return PSA_ERROR_BAD_STATE;
472 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200473
Ryan Everett2f1f1722024-01-31 13:31:00 +0000474#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000475 /* We need to set status as success, otherwise CORRUPTION_DETECTED
476 * would be returned if the lock fails. */
477 status = PSA_SUCCESS;
Ryan Everett2f1f1722024-01-31 13:31:00 +0000478 /* If the key is persistent and not loaded, we cannot unlock the mutex
479 * between checking if the key is loaded and setting the slot as FULL,
480 * as otherwise another thread may load and then destroy the key
481 * in the meantime. */
482 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
483 &mbedtls_threading_key_slot_mutex));
484#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200485 /*
486 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100487 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200488 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
490 if (status != PSA_ERROR_DOES_NOT_EXIST) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000491#if defined(MBEDTLS_THREADING_C)
492 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
493 &mbedtls_threading_key_slot_mutex));
494#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return status;
496 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200497
Steven Cooreman0bb65362021-04-06 15:09:57 +0200498 /* Loading keys from storage requires support for such a mechanism */
499#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
500 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200501 psa_key_id_t volatile_key_id;
502
Ryan Everett098c6652024-01-03 13:03:36 +0000503 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (status != PSA_SUCCESS) {
Ryan Everett2f1f1722024-01-31 13:31:00 +0000505#if defined(MBEDTLS_THREADING_C)
506 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
507 &mbedtls_threading_key_slot_mutex));
508#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return status;
510 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200511
Ronald Cronc4d1b512020-07-31 11:26:37 +0200512 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100513 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200514
Steven Cooreman6801f082021-02-19 17:21:22 +0100515 status = PSA_ERROR_DOES_NOT_EXIST;
516#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200517 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100519#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
520
521#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (status == PSA_ERROR_DOES_NOT_EXIST) {
523 status = psa_load_persistent_key_into_slot(*p_slot);
524 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100525#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (status != PSA_SUCCESS) {
528 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000529
Ryan Everett1a3573e2024-04-29 18:29:48 +0100530 /* If the key does not exist, we need to return
531 * PSA_ERROR_INVALID_HANDLE. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000533 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 }
535 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200536 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000538
539 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
540 PSA_SLOT_FULL);
541 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200543
Steven Cooreman0bb65362021-04-06 15:09:57 +0200544#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000545 status = PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200546#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ryan Everett2f1f1722024-01-31 13:31:00 +0000547
Ryan Everettd4ea40d2024-04-29 18:24:58 +0100548 if (status != PSA_SUCCESS) {
549 *p_slot = NULL;
550 }
Ryan Everett2f1f1722024-01-31 13:31:00 +0000551#if defined(MBEDTLS_THREADING_C)
552 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
553 &mbedtls_threading_key_slot_mutex));
554#endif
555 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200556}
557
Ryan Everett39cc9d72023-12-21 17:57:14 +0000558psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200559{
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (slot == NULL) {
561 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200562 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000563 if ((slot->state != PSA_SLOT_FULL) &&
564 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000565 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000566 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200567
Ryan Everett39cc9d72023-12-21 17:57:14 +0000568 /* If we are the last reader and the slot is marked for deletion,
569 * we must wipe the slot here. */
570 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
571 (slot->registered_readers == 1)) {
572 return psa_wipe_key_slot(slot);
573 }
574
575 if (psa_key_slot_has_readers(slot)) {
576 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return PSA_SUCCESS;
578 }
579
580 /*
581 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000582 * do our best to report if there are no registered readers. Assert with
583 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
584 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 * the function is called as part of the execution of a test suite, the
586 * execution of the test suite is stopped in error if the assertion fails.
587 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000588 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200590}
591
Ryan Everetteb1722a2024-01-31 13:36:39 +0000592psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
593{
594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
595#if defined(MBEDTLS_THREADING_C)
Ryan Everett91ce7922024-02-12 12:17:28 +0000596 /* We need to set status as success, otherwise CORRUPTION_DETECTED
597 * would be returned if the lock fails. */
598 status = PSA_SUCCESS;
Ryan Everetteb1722a2024-01-31 13:36:39 +0000599 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
600 &mbedtls_threading_key_slot_mutex));
601#endif
602 status = psa_unregister_read(slot);
603#if defined(MBEDTLS_THREADING_C)
604 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
605 &mbedtls_threading_key_slot_mutex));
606#endif
607 return status;
608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
611 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200612{
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200614#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100615 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
617 if (driver != NULL) {
618 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200619 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 }
621 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200622 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100623#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200624 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100625#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
626
Steven Cooreman98435dd2021-01-08 19:19:40 +0100627 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200630 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 return PSA_SUCCESS;
632 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200633}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200636{
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200638 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return PSA_SUCCESS;
640 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200641 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200642#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
644 return PSA_ERROR_INVALID_ARGUMENT;
645 } else {
646 return PSA_SUCCESS;
647 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200648#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200650#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200651 }
Gilles Peskined167b942019-04-19 18:19:40 +0200652}
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100655{
Archana0dc86b52021-07-14 13:59:48 +0530656#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
657 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100658 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200659 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 status = psa_get_and_lock_key_slot(key, &slot);
662 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200663 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000665 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100669 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200670
671 *handle = key;
672
Ryan Everette110a4c2024-02-22 10:43:03 +0000673 return psa_unregister_read_under_mutex(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200674
Archana0dc86b52021-07-14 13:59:48 +0530675#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200676 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200677 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530679#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100683{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000684 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine267c6562019-05-27 19:01:54 +0200685 psa_key_slot_t *slot;
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (psa_key_handle_is_null(handle)) {
688 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000689 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100690
Ryan Everett3af9bc12024-01-30 17:21:57 +0000691#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000692 /* We need to set status as success, otherwise CORRUPTION_DETECTED
693 * would be returned if the lock fails. */
694 status = PSA_SUCCESS;
Ryan Everett3af9bc12024-01-30 17:21:57 +0000695 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
696 &mbedtls_threading_key_slot_mutex));
697#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
699 if (status != PSA_SUCCESS) {
700 if (status == PSA_ERROR_DOES_NOT_EXIST) {
701 status = PSA_ERROR_INVALID_HANDLE;
702 }
Ryan Everett3af9bc12024-01-30 17:21:57 +0000703#if defined(MBEDTLS_THREADING_C)
704 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
705 &mbedtls_threading_key_slot_mutex));
706#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return status;
708 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000709
Ryan Everettc70ce572024-01-03 16:04:33 +0000710 if (slot->registered_readers == 1) {
Ryan Everettf23336e2024-01-24 11:39:21 +0000711 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000712 } else {
Ryan Everettf23336e2024-01-24 11:39:21 +0000713 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 }
Ryan Everettf23336e2024-01-24 11:39:21 +0000715#if defined(MBEDTLS_THREADING_C)
716 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
717 &mbedtls_threading_key_slot_mutex));
718#endif
719
720 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200724{
Ryan Everett3af9bc12024-01-30 17:21:57 +0000725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron277a85f2020-08-04 15:49:48 +0200726 psa_key_slot_t *slot;
727
Ryan Everettb0821952024-01-24 11:42:32 +0000728#if defined(MBEDTLS_THREADING_C)
Ryan Everett9dc076b2024-02-09 14:20:09 +0000729 /* We need to set status as success, otherwise CORRUPTION_DETECTED
730 * would be returned if the lock fails. */
731 status = PSA_SUCCESS;
Ryan Everettb0821952024-01-24 11:42:32 +0000732 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
733 &mbedtls_threading_key_slot_mutex));
734#endif
Ryan Everett3af9bc12024-01-30 17:21:57 +0000735 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
736 if (status != PSA_SUCCESS) {
737#if defined(MBEDTLS_THREADING_C)
738 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
739 &mbedtls_threading_key_slot_mutex));
740#endif
741 return status;
742 }
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Ryan Everettc70ce572024-01-03 16:04:33 +0000745 (slot->registered_readers == 1)) {
Ryan Everettb0821952024-01-24 11:42:32 +0000746 status = psa_wipe_key_slot(slot);
Ryan Everett4755e6b2024-01-12 16:35:59 +0000747 } else {
Ryan Everettb0821952024-01-24 11:42:32 +0000748 status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 }
Ryan Everettb0821952024-01-24 11:42:32 +0000750#if defined(MBEDTLS_THREADING_C)
751 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
752 &mbedtls_threading_key_slot_mutex));
753#endif
754
755 return status;
Ronald Cron277a85f2020-08-04 15:49:48 +0200756}
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200759{
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200761
Gilles Peskine5064af62024-06-07 13:53:28 +0200762 for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
763 for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
764 const psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
765 if (psa_key_slot_has_readers(slot)) {
766 ++stats->locked_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 }
Gilles Peskine5064af62024-06-07 13:53:28 +0200768 if (slot->state == PSA_SLOT_EMPTY) {
769 ++stats->empty_slots;
770 continue;
771 }
772 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
773 ++stats->volatile_slots;
774 } else {
775 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
776 ++stats->persistent_slots;
777 if (id > stats->max_open_internal_key_id) {
778 stats->max_open_internal_key_id = id;
779 }
780 }
781 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
782 PSA_KEY_LOCATION_LOCAL_STORAGE) {
783 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
784 ++stats->external_slots;
785 if (id > stats->max_open_external_key_id) {
786 stats->max_open_external_key_id = id;
787 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200789 }
790 }
791}
792
Gilles Peskine961849f2018-11-30 18:54:54 +0100793#endif /* MBEDTLS_PSA_CRYPTO_C */