blob: 2c4da7833b73787b4f428dc470c8c850af35123a [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 Rodgman7ff79652023-11-03 12:04:52 +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"
Steven Cooremane3842522021-03-18 18:52:44 +010016#include "psa_crypto_driver_wrappers.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"
Gilles Peskine961849f2018-11-30 18:54:54 +010026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine961849f2018-11-30 18:54:54 +010028
Gilles Peskinef16263e2024-06-20 22:15:42 +020029
30
31/* Make sure we have distinct ranges of key identifiers for distinct
32 * purposes. */
33MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MIN < PSA_KEY_ID_USER_MAX,
34 "Empty user key ID range");
35MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN < PSA_KEY_ID_VENDOR_MAX,
36 "Empty vendor key ID range");
37MBEDTLS_STATIC_ASSERT(MBEDTLS_PSA_KEY_ID_BUILTIN_MIN < MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
38 "Empty builtin key ID range");
39MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MIN < PSA_KEY_ID_VOLATILE_MAX,
40 "Empty volatile key ID range");
41
42MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MAX < PSA_KEY_ID_VENDOR_MIN ||
43 PSA_KEY_ID_VENDOR_MAX < PSA_KEY_ID_USER_MIN,
44 "Overlap between user key IDs and vendor key IDs");
45
46MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN &&
47 MBEDTLS_PSA_KEY_ID_BUILTIN_MAX <= PSA_KEY_ID_VENDOR_MAX,
48 "Builtin key identifiers are not in the vendor range");
49
50MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= PSA_KEY_ID_VOLATILE_MIN &&
51 PSA_KEY_ID_VOLATILE_MAX <= PSA_KEY_ID_VENDOR_MAX,
52 "Volatile key identifiers are not in the vendor range");
53
54MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MAX < MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ||
55 MBEDTLS_PSA_KEY_ID_BUILTIN_MAX < PSA_KEY_ID_VOLATILE_MIN,
56 "Overlap between builtin key IDs and volatile key IDs");
57
58
59
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010061 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskine66fb1262018-12-10 16:29:04 +010062 unsigned key_slots_initialized : 1;
63} psa_global_data_t;
64
Gilles Peskine2e14bd32018-12-12 14:05:08 +010065static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010066
Gilles Peskine18f659b2024-07-16 20:02:37 +020067MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
68 PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
69 "The volatile key range is larger than the key slot array");
70
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020072{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
76 (key_id <= PSA_KEY_ID_USER_MAX)) {
77 return 1;
78 }
Ronald Crond2ed4812020-07-17 16:11:30 +020079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (vendor_ok &&
81 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
82 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
83 return 1;
84 }
Ronald Crond2ed4812020-07-17 16:11:30 +020085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020087}
88
Ronald Cron5c522922020-11-14 16:35:34 +010089/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020090 *
Ronald Cron5c522922020-11-14 16:35:34 +010091 * The descriptions of volatile keys and loaded persistent keys are
92 * stored in key slots. This function returns a pointer to the key slot
93 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020094 *
Ronald Cron5c522922020-11-14 16:35:34 +010095 * The function searches the key slots containing the description of the key
96 * with \p key identifier. The function does only read accesses to the key
97 * slots. The function does not load any persistent key thus does not access
98 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020099 *
Ronald Cron5c522922020-11-14 16:35:34 +0100100 * For volatile key identifiers, only one key slot is queried as a volatile
101 * key with identifier key_id can only be stored in slot of index
102 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +0200103 *
Ronald Cron5c522922020-11-14 16:35:34 +0100104 * On success, the function locks the key slot. It is the responsibility of
105 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200106 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200107 * \param key Key identifier to query.
108 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
109 * key slot containing the description of the key
110 * identified by \p key.
111 *
Ronald Cron96783552020-10-19 12:06:30 +0200112 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200113 * The pointer to the key slot containing the description of the key
114 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200115 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200116 * \p key is not a valid key identifier.
117 * \retval #PSA_ERROR_DOES_NOT_EXIST
118 * There is no key with key identifier \p key in the key slots.
119 */
Ronald Cron5c522922020-11-14 16:35:34 +0100120static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100122{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100123 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100125 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200126 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 if (psa_key_id_is_volatile(key_id)) {
129 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100130
131 /*
132 * Check if both the PSA key identifier key_id and the owner
133 * identifier of key match those of the key slot.
134 *
135 * Note that, if the key slot is not occupied, its PSA key identifier
136 * is equal to zero. This is an invalid value for a PSA key identifier
137 * and thus cannot be equal to the valid PSA key identifier key_id.
138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100140 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 } else {
142 if (!psa_is_valid_key_id(key, 1)) {
143 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200144 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145
146 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
147 slot = &global_data.key_slots[slot_idx];
148 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
149 break;
150 }
151 }
152 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100153 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (status == PSA_SUCCESS) {
157 status = psa_lock_key_slot(slot);
158 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100159 *p_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200161 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200164}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100167{
168 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
169 * guarantee that the key slots are initialized to all-zero, which
170 * means that all the key slots are in a valid, empty state. */
171 global_data.key_slots_initialized = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100173}
174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100176{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200177 size_t slot_idx;
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
180 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ronald Cron5c522922020-11-14 16:35:34 +0100181 slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100183 }
184 global_data.key_slots_initialized = 0;
185}
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
188 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100189{
Ronald Crona5b894f2020-10-21 09:04:34 +0200190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200191 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100192 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200195 status = PSA_ERROR_BAD_STATE;
196 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100197 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200198
Ronald Cron5c522922020-11-14 16:35:34 +0100199 selected_slot = unlocked_persistent_key_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
201 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
202 if (!psa_is_key_slot_occupied(slot)) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200203 selected_slot = slot;
204 break;
205 }
206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 if ((unlocked_persistent_key_slot == NULL) &&
208 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
209 (!psa_is_key_slot_locked(slot))) {
Ronald Cron5c522922020-11-14 16:35:34 +0100210 unlocked_persistent_key_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200212 }
213
214 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100215 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100216 * slot containing the description of a persistent key, recycle the first
217 * such key slot we encountered. If we later need to operate on the
218 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100219 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 if ((selected_slot == NULL) &&
222 (unlocked_persistent_key_slot != NULL)) {
Ronald Cron5c522922020-11-14 16:35:34 +0100223 selected_slot = unlocked_persistent_key_slot;
224 selected_slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 psa_wipe_key_slot(selected_slot);
Ronald Crona5b894f2020-10-21 09:04:34 +0200226 }
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 if (selected_slot != NULL) {
229 status = psa_lock_key_slot(selected_slot);
230 if (status != PSA_SUCCESS) {
231 goto error;
232 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100233
Ronald Crona5b894f2020-10-21 09:04:34 +0200234 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200236 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200239 }
240 status = PSA_ERROR_INSUFFICIENT_MEMORY;
241
242error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200243 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200244 *volatile_key_id = 0;
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100247}
248
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100249#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100251{
252 psa_status_t status = PSA_SUCCESS;
253 uint8_t *key_data = NULL;
254 size_t key_data_length = 0;
255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 status = psa_load_persistent_key(&slot->attr,
257 &key_data, &key_data_length);
258 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100259 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200261
Steven Cooreman98435dd2021-01-08 19:19:40 +0100262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100263 /* Special handling is required for loading keys associated with a
264 * dynamically registered SE interface. */
265 const psa_drv_se_t *drv;
266 psa_drv_se_context_t *drv_context;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100268 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100271 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100272 goto exit;
273 }
274 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100275 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 slot, data->slot_number, sizeof(data->slot_number));
Steven Cooremanac3434f2021-01-15 17:36:02 +0100277 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200278 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Gilles Peskine1df83d42019-07-23 16:13:14 +0200282
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100283exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 psa_free_persistent_key_data(key_data, key_data_length);
285 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100286}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200287#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
288
Steven Cooreman6801f082021-02-19 17:21:22 +0100289#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100292{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100295 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100296 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100297 size_t key_buffer_size = 0;
298 size_t key_buffer_length = 0;
299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (!psa_key_id_is_builtin(
301 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
302 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100303 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100304
305 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100306 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 slot->attr.id, &lifetime, &slot_number);
308 if (status != PSA_SUCCESS) {
309 return status;
310 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100311
Steven Cooreman966db262021-04-13 13:45:45 +0200312 /* Set required key attributes to ensure get_builtin_key can retrieve the
313 * full attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 psa_set_key_id(&attributes, slot->attr.id);
315 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100316
Steven Cooremance487022021-04-07 18:09:53 +0200317 /* Get the full key attributes from the driver in order to be able to
318 * calculate the required buffer size. */
319 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 slot_number, &attributes,
321 NULL, 0, NULL);
322 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200323 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200325 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 }
327 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200328 }
329
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200330 /* If the key should exist according to the platform, then ask the driver
331 * what its expected size is. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
333 &key_buffer_size);
334 if (status != PSA_SUCCESS) {
335 return status;
336 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100337
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200338 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200339 * into the (now properly sized) slot buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
341 if (status != PSA_SUCCESS) {
342 return status;
343 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100344
345 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 slot_number, &attributes,
347 slot->key.data, slot->key.bytes, &key_buffer_length);
348 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100349 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100351
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200352 /* Copy actual key length and core attributes into the slot on success */
353 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100354 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100355
356exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 if (status != PSA_SUCCESS) {
358 psa_remove_key_data_from_memory(slot);
359 }
360 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100361}
362#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
365 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200366{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200368
369 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 if (!global_data.key_slots_initialized) {
371 return PSA_ERROR_BAD_STATE;
372 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200373
Ronald Cronf95a2b12020-10-22 15:24:49 +0200374 /*
375 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100376 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200377 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
379 if (status != PSA_ERROR_DOES_NOT_EXIST) {
380 return status;
381 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200382
Steven Cooreman0bb65362021-04-06 15:09:57 +0200383 /* Loading keys from storage requires support for such a mechanism */
384#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
385 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200386 psa_key_id_t volatile_key_id;
387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
389 if (status != PSA_SUCCESS) {
390 return status;
391 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200392
Ronald Cronc4d1b512020-07-31 11:26:37 +0200393 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100394 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200395
Steven Cooreman6801f082021-02-19 17:21:22 +0100396 status = PSA_ERROR_DOES_NOT_EXIST;
397#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200398 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100400#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
401
402#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 if (status == PSA_ERROR_DOES_NOT_EXIST) {
404 status = psa_load_persistent_key_into_slot(*p_slot);
405 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100406#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 if (status != PSA_SUCCESS) {
409 psa_wipe_key_slot(*p_slot);
410 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000411 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 }
413 } else {
gabor-mezei-arm6c185412021-06-28 14:59:52 +0200414 /* Add implicit usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
416 }
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200419#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200421#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200422}
423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200425{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if (slot == NULL) {
427 return PSA_SUCCESS;
428 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (slot->lock_count > 0) {
Ronald Cron5c522922020-11-14 16:35:34 +0100431 slot->lock_count--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200433 }
434
435 /*
436 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100437 * do our best to report if the lock counter is equal to zero: if
Ronald Cronf95a2b12020-10-22 15:24:49 +0200438 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
439 * called as part of the execution of a unit test suite this will stop the
Ronald Cron4640c152020-11-13 10:11:01 +0100440 * test suite execution).
Ronald Cronf95a2b12020-10-22 15:24:49 +0200441 */
442#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 MBEDTLS_PARAM_FAILED(slot->lock_count > 0);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200444#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200447}
448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
450 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200451{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200453#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100454 /* Check whether a driver is registered against this lifetime */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
456 if (driver != NULL) {
457 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200458 *p_drv = driver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 }
460 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200461 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100462#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200463 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100464#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
465
Steven Cooreman98435dd2021-01-08 19:19:40 +0100466#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
467 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 return PSA_SUCCESS;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100469#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
470 /* No support for external lifetimes at all, or dynamic interface
471 * did not find driver for requested lifetime. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100473#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200475 /* Local/internal keys are always valid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return PSA_SUCCESS;
477 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200478}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200481{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200483 /* Volatile keys are always supported */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 return PSA_SUCCESS;
485 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200486 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200487#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
489 return PSA_ERROR_INVALID_ARGUMENT;
490 } else {
491 return PSA_SUCCESS;
492 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200493#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200495#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200496 }
Gilles Peskined167b942019-04-19 18:19:40 +0200497}
498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100500{
Archana6d342f32021-07-14 13:59:48 +0530501#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
502 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100503 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200504 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 status = psa_get_and_lock_key_slot(key, &slot);
507 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200508 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000510 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100514 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200515
516 *handle = key;
517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 return psa_unlock_key_slot(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200519
Archana6d342f32021-07-14 13:59:48 +0530520#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200521 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200522 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return PSA_ERROR_NOT_SUPPORTED;
Archana6d342f32021-07-14 13:59:48 +0530524#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100525}
526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100528{
Gilles Peskine267c6562019-05-27 19:01:54 +0200529 psa_status_t status;
530 psa_key_slot_t *slot;
531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 if (psa_key_handle_is_null(handle)) {
533 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000534 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535
536 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
537 if (status != PSA_SUCCESS) {
538 if (status == PSA_ERROR_DOES_NOT_EXIST) {
539 status = PSA_ERROR_INVALID_HANDLE;
540 }
541
542 return status;
543 }
544 if (slot->lock_count <= 1) {
545 return psa_wipe_key_slot(slot);
546 } else {
547 return psa_unlock_key_slot(slot);
548 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100549}
550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200552{
553 psa_status_t status;
554 psa_key_slot_t *slot;
555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
557 if (status != PSA_SUCCESS) {
558 return status;
559 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
562 (slot->lock_count <= 1)) {
563 return psa_wipe_key_slot(slot);
564 } else {
565 return psa_unlock_key_slot(slot);
566 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200567}
568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200570{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200571 size_t slot_idx;
572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
576 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
577 if (psa_is_key_slot_locked(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100578 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100579 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 if (!psa_is_key_slot_occupied(slot)) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200581 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200582 continue;
583 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200585 ++stats->volatile_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 } else {
587 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200588 ++stats->persistent_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100590 stats->max_open_internal_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200592 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
594 PSA_KEY_LOCATION_LOCAL_STORAGE) {
595 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200596 ++stats->external_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100598 stats->max_open_external_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200600 }
601 }
602}
603
Gilles Peskine961849f2018-11-30 18:54:54 +0100604#endif /* MBEDTLS_PSA_CRYPTO_C */