blob: f0cb4ee9f6ee0e93989dacd4eb96a1e8b8540d8d [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 Peskine1b6c09a2023-01-11 14:52:35 +010067int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020068{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
72 (key_id <= PSA_KEY_ID_USER_MAX)) {
73 return 1;
74 }
Ronald Crond2ed4812020-07-17 16:11:30 +020075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 if (vendor_ok &&
77 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
78 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
79 return 1;
80 }
Ronald Crond2ed4812020-07-17 16:11:30 +020081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020083}
84
Ronald Cron5c522922020-11-14 16:35:34 +010085/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020086 *
Ronald Cron5c522922020-11-14 16:35:34 +010087 * The descriptions of volatile keys and loaded persistent keys are
88 * stored in key slots. This function returns a pointer to the key slot
89 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020090 *
Ronald Cron5c522922020-11-14 16:35:34 +010091 * The function searches the key slots containing the description of the key
92 * with \p key identifier. The function does only read accesses to the key
93 * slots. The function does not load any persistent key thus does not access
94 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020095 *
Ronald Cron5c522922020-11-14 16:35:34 +010096 * For volatile key identifiers, only one key slot is queried as a volatile
97 * key with identifier key_id can only be stored in slot of index
98 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020099 *
Ronald Cron5c522922020-11-14 16:35:34 +0100100 * On success, the function locks the key slot. It is the responsibility of
101 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200102 *
Ronald Cron97c8ad52020-10-15 11:17:11 +0200103 * \param key Key identifier to query.
104 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
105 * key slot containing the description of the key
106 * identified by \p key.
107 *
Ronald Cron96783552020-10-19 12:06:30 +0200108 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200109 * The pointer to the key slot containing the description of the key
110 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200111 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200112 * \p key is not a valid key identifier.
113 * \retval #PSA_ERROR_DOES_NOT_EXIST
114 * There is no key with key identifier \p key in the key slots.
115 */
Ronald Cron5c522922020-11-14 16:35:34 +0100116static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100118{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100119 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100121 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200122 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if (psa_key_id_is_volatile(key_id)) {
125 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100126
127 /*
128 * Check if both the PSA key identifier key_id and the owner
129 * identifier of key match those of the key slot.
130 *
131 * Note that, if the key slot is not occupied, its PSA key identifier
132 * is equal to zero. This is an invalid value for a PSA key identifier
133 * and thus cannot be equal to the valid PSA key identifier key_id.
134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100136 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 } else {
138 if (!psa_is_valid_key_id(key, 1)) {
139 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200140 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141
142 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
143 slot = &global_data.key_slots[slot_idx];
144 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
145 break;
146 }
147 }
148 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100149 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200150 }
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 if (status == PSA_SUCCESS) {
153 status = psa_lock_key_slot(slot);
154 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100155 *p_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200157 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200160}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100163{
164 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
165 * guarantee that the key slots are initialized to all-zero, which
166 * means that all the key slots are in a valid, empty state. */
167 global_data.key_slots_initialized = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100169}
170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100172{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200173 size_t slot_idx;
174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
176 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ronald Cron5c522922020-11-14 16:35:34 +0100177 slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100179 }
180 global_data.key_slots_initialized = 0;
181}
182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
184 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100185{
Ronald Crona5b894f2020-10-21 09:04:34 +0200186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200187 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100188 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200191 status = PSA_ERROR_BAD_STATE;
192 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100193 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200194
Ronald Cron5c522922020-11-14 16:35:34 +0100195 selected_slot = unlocked_persistent_key_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
197 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
198 if (!psa_is_key_slot_occupied(slot)) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200199 selected_slot = slot;
200 break;
201 }
202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if ((unlocked_persistent_key_slot == NULL) &&
204 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
205 (!psa_is_key_slot_locked(slot))) {
Ronald Cron5c522922020-11-14 16:35:34 +0100206 unlocked_persistent_key_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200208 }
209
210 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100211 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100212 * slot containing the description of a persistent key, recycle the first
213 * such key slot we encountered. If we later need to operate on the
214 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100215 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 if ((selected_slot == NULL) &&
218 (unlocked_persistent_key_slot != NULL)) {
Ronald Cron5c522922020-11-14 16:35:34 +0100219 selected_slot = unlocked_persistent_key_slot;
220 selected_slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 psa_wipe_key_slot(selected_slot);
Ronald Crona5b894f2020-10-21 09:04:34 +0200222 }
223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 if (selected_slot != NULL) {
225 status = psa_lock_key_slot(selected_slot);
226 if (status != PSA_SUCCESS) {
227 goto error;
228 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100229
Ronald Crona5b894f2020-10-21 09:04:34 +0200230 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200232 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200235 }
236 status = PSA_ERROR_INSUFFICIENT_MEMORY;
237
238error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200239 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200240 *volatile_key_id = 0;
241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100243}
244
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100245#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100247{
248 psa_status_t status = PSA_SUCCESS;
249 uint8_t *key_data = NULL;
250 size_t key_data_length = 0;
251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 status = psa_load_persistent_key(&slot->attr,
253 &key_data, &key_data_length);
254 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100255 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200257
Steven Cooreman98435dd2021-01-08 19:19:40 +0100258#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100259 /* Special handling is required for loading keys associated with a
260 * dynamically registered SE interface. */
261 const psa_drv_se_t *drv;
262 psa_drv_se_context_t *drv_context;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100264 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100267 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100268 goto exit;
269 }
270 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100271 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 slot, data->slot_number, sizeof(data->slot_number));
Steven Cooremanac3434f2021-01-15 17:36:02 +0100273 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200274 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Gilles Peskine1df83d42019-07-23 16:13:14 +0200278
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100279exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 psa_free_persistent_key_data(key_data, key_data_length);
281 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100282}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200283#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
284
Steven Cooreman6801f082021-02-19 17:21:22 +0100285#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100288{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100291 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100292 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100293 size_t key_buffer_size = 0;
294 size_t key_buffer_length = 0;
295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 if (!psa_key_id_is_builtin(
297 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
298 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100299 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100300
301 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100302 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 slot->attr.id, &lifetime, &slot_number);
304 if (status != PSA_SUCCESS) {
305 return status;
306 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100307
Steven Cooreman966db262021-04-13 13:45:45 +0200308 /* Set required key attributes to ensure get_builtin_key can retrieve the
309 * full attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 psa_set_key_id(&attributes, slot->attr.id);
311 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100312
Steven Cooremance487022021-04-07 18:09:53 +0200313 /* Get the full key attributes from the driver in order to be able to
314 * calculate the required buffer size. */
315 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 slot_number, &attributes,
317 NULL, 0, NULL);
318 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200319 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200321 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 }
323 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200324 }
325
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200326 /* If the key should exist according to the platform, then ask the driver
327 * what its expected size is. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
329 &key_buffer_size);
330 if (status != PSA_SUCCESS) {
331 return status;
332 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100333
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200334 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200335 * into the (now properly sized) slot buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
337 if (status != PSA_SUCCESS) {
338 return status;
339 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100340
341 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 slot_number, &attributes,
343 slot->key.data, slot->key.bytes, &key_buffer_length);
344 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100345 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100347
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200348 /* Copy actual key length and core attributes into the slot on success */
349 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100350 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100351
352exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if (status != PSA_SUCCESS) {
354 psa_remove_key_data_from_memory(slot);
355 }
356 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100357}
358#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
361 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200362{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200363 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200364
365 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (!global_data.key_slots_initialized) {
367 return PSA_ERROR_BAD_STATE;
368 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200369
Ronald Cronf95a2b12020-10-22 15:24:49 +0200370 /*
371 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100372 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200373 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
375 if (status != PSA_ERROR_DOES_NOT_EXIST) {
376 return status;
377 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200378
Steven Cooreman0bb65362021-04-06 15:09:57 +0200379 /* Loading keys from storage requires support for such a mechanism */
380#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
381 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200382 psa_key_id_t volatile_key_id;
383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
385 if (status != PSA_SUCCESS) {
386 return status;
387 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200388
Ronald Cronc4d1b512020-07-31 11:26:37 +0200389 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100390 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200391
Steven Cooreman6801f082021-02-19 17:21:22 +0100392 status = PSA_ERROR_DOES_NOT_EXIST;
393#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200394 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100396#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
397
398#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 if (status == PSA_ERROR_DOES_NOT_EXIST) {
400 status = psa_load_persistent_key_into_slot(*p_slot);
401 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100402#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 if (status != PSA_SUCCESS) {
405 psa_wipe_key_slot(*p_slot);
406 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000407 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 }
409 } else {
gabor-mezei-arm6c185412021-06-28 14:59:52 +0200410 /* Add implicit usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
412 }
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200415#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200417#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200418}
419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200421{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 if (slot == NULL) {
423 return PSA_SUCCESS;
424 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if (slot->lock_count > 0) {
Ronald Cron5c522922020-11-14 16:35:34 +0100427 slot->lock_count--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200429 }
430
431 /*
432 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100433 * do our best to report if the lock counter is equal to zero: if
Ronald Cronf95a2b12020-10-22 15:24:49 +0200434 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
435 * called as part of the execution of a unit test suite this will stop the
Ronald Cron4640c152020-11-13 10:11:01 +0100436 * test suite execution).
Ronald Cronf95a2b12020-10-22 15:24:49 +0200437 */
438#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 MBEDTLS_PARAM_FAILED(slot->lock_count > 0);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200440#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200443}
444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
446 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200447{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200449#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100450 /* Check whether a driver is registered against this lifetime */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
452 if (driver != NULL) {
453 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200454 *p_drv = driver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 }
456 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200457 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100458#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200459 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100460#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
461
Steven Cooreman98435dd2021-01-08 19:19:40 +0100462#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
463 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 return PSA_SUCCESS;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100465#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
466 /* No support for external lifetimes at all, or dynamic interface
467 * did not find driver for requested lifetime. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100469#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200471 /* Local/internal keys are always valid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 return PSA_SUCCESS;
473 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200474}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200477{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200479 /* Volatile keys are always supported */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 return PSA_SUCCESS;
481 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200482 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200483#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
485 return PSA_ERROR_INVALID_ARGUMENT;
486 } else {
487 return PSA_SUCCESS;
488 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200489#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200491#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200492 }
Gilles Peskined167b942019-04-19 18:19:40 +0200493}
494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100496{
Archana6d342f32021-07-14 13:59:48 +0530497#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
498 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100499 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200500 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 status = psa_get_and_lock_key_slot(key, &slot);
503 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200504 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000506 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100510 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200511
512 *handle = key;
513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 return psa_unlock_key_slot(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200515
Archana6d342f32021-07-14 13:59:48 +0530516#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200517 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200518 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 return PSA_ERROR_NOT_SUPPORTED;
Archana6d342f32021-07-14 13:59:48 +0530520#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100521}
522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100524{
Gilles Peskine267c6562019-05-27 19:01:54 +0200525 psa_status_t status;
526 psa_key_slot_t *slot;
527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 if (psa_key_handle_is_null(handle)) {
529 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000530 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531
532 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
533 if (status != PSA_SUCCESS) {
534 if (status == PSA_ERROR_DOES_NOT_EXIST) {
535 status = PSA_ERROR_INVALID_HANDLE;
536 }
537
538 return status;
539 }
540 if (slot->lock_count <= 1) {
541 return psa_wipe_key_slot(slot);
542 } else {
543 return psa_unlock_key_slot(slot);
544 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100545}
546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200548{
549 psa_status_t status;
550 psa_key_slot_t *slot;
551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
553 if (status != PSA_SUCCESS) {
554 return status;
555 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
558 (slot->lock_count <= 1)) {
559 return psa_wipe_key_slot(slot);
560 } else {
561 return psa_unlock_key_slot(slot);
562 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200563}
564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200566{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200567 size_t slot_idx;
568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
572 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
573 if (psa_is_key_slot_locked(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100574 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100575 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 if (!psa_is_key_slot_occupied(slot)) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200577 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200578 continue;
579 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200581 ++stats->volatile_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 } else {
583 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200584 ++stats->persistent_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100586 stats->max_open_internal_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200588 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
590 PSA_KEY_LOCATION_LOCAL_STORAGE) {
591 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200592 ++stats->external_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100594 stats->max_open_external_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200596 }
597 }
598}
599
Gilles Peskine961849f2018-11-30 18:54:54 +0100600#endif /* MBEDTLS_PSA_CRYPTO_C */