blob: 0f480fb098a4ad654c96f0b03f217ed60fc0c6e9 [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"
Gilles Peskine961849f2018-11-30 18:54:54 +010026
Gilles Peskine449bd832023-01-11 14:50:10 +010027typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010028 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Dave Rodgman164614a2023-08-16 17:56:28 +010029 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +010030} psa_global_data_t;
31
Gilles Peskine2e14bd32018-12-12 14:05:08 +010032static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010033
Gilles Peskine449bd832023-01-11 14:50:10 +010034int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020035{
Gilles Peskine449bd832023-01-11 14:50:10 +010036 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
39 (key_id <= PSA_KEY_ID_USER_MAX)) {
40 return 1;
41 }
Ronald Crond2ed4812020-07-17 16:11:30 +020042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 if (vendor_ok &&
44 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
45 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
46 return 1;
47 }
Ronald Crond2ed4812020-07-17 16:11:30 +020048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020050}
51
Ronald Cron5c522922020-11-14 16:35:34 +010052/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020053 *
Ronald Cron5c522922020-11-14 16:35:34 +010054 * The descriptions of volatile keys and loaded persistent keys are
55 * stored in key slots. This function returns a pointer to the key slot
56 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020057 *
Ronald Cron5c522922020-11-14 16:35:34 +010058 * The function searches the key slots containing the description of the key
59 * with \p key identifier. The function does only read accesses to the key
60 * slots. The function does not load any persistent key thus does not access
61 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020062 *
Ronald Cron5c522922020-11-14 16:35:34 +010063 * For volatile key identifiers, only one key slot is queried as a volatile
64 * key with identifier key_id can only be stored in slot of index
65 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020066 *
Ronald Cron5c522922020-11-14 16:35:34 +010067 * On success, the function locks the key slot. It is the responsibility of
68 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020069 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020070 * \param key Key identifier to query.
71 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
72 * key slot containing the description of the key
73 * identified by \p key.
74 *
Ronald Cron96783552020-10-19 12:06:30 +020075 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020076 * The pointer to the key slot containing the description of the key
77 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020078 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020079 * \p key is not a valid key identifier.
80 * \retval #PSA_ERROR_DOES_NOT_EXIST
81 * There is no key with key identifier \p key in the key slots.
82 */
Ronald Cron5c522922020-11-14 16:35:34 +010083static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +010085{
Ronald Cronf473d8b2020-11-12 10:07:21 +010086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +010088 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +020089 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (psa_key_id_is_volatile(key_id)) {
92 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +010093
94 /*
95 * Check if both the PSA key identifier key_id and the owner
96 * identifier of key match those of the key slot.
97 *
98 * Note that, if the key slot is not occupied, its PSA key identifier
99 * is equal to zero. This is an invalid value for a PSA key identifier
100 * and thus cannot be equal to the valid PSA key identifier key_id.
101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100103 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 } else {
105 if (!psa_is_valid_key_id(key, 1)) {
106 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200107 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100108
109 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
110 slot = &global_data.key_slots[slot_idx];
111 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
112 break;
113 }
114 }
115 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100116 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200117 }
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (status == PSA_SUCCESS) {
120 status = psa_lock_key_slot(slot);
121 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100122 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200124 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200127}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100130{
131 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
132 * guarantee that the key slots are initialized to all-zero, which
133 * means that all the key slots are in a valid, empty state. */
134 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100136}
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100139{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200140 size_t slot_idx;
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
143 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ronald Cron5c522922020-11-14 16:35:34 +0100144 slot->lock_count = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100146 }
147 global_data.key_slots_initialized = 0;
148}
149
Ryan Everett2afb5162023-12-22 15:59:45 +0000150psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
151 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100152{
Ronald Crona5b894f2020-10-21 09:04:34 +0200153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200154 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000155 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200158 status = PSA_ERROR_BAD_STATE;
159 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100160 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200161
Ryan Everett2afb5162023-12-22 15:59:45 +0000162 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
164 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett2afb5162023-12-22 15:59:45 +0000165 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200166 selected_slot = slot;
167 break;
168 }
169
Ryan Everett2afb5162023-12-22 15:59:45 +0000170 if ((unused_persistent_key_slot == NULL) &&
171 (slot->state == PSA_SLOT_FULL) &&
172 (!psa_key_slot_has_readers(slot)) &&
173 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
174 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200176 }
177
178 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100179 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100180 * slot containing the description of a persistent key, recycle the first
181 * such key slot we encountered. If we later need to operate on the
182 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100183 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000186 (unused_persistent_key_slot != NULL)) {
187 selected_slot = unused_persistent_key_slot;
188 psa_register_read(selected_slot);
189 /* If the state is not changed then psa_wipe_key_slot
190 * will report an error. */
191 psa_key_slot_state_transition(selected_slot, PSA_SLOT_FULL,
192 PSA_SLOT_PENDING_DELETION);
193 status = psa_wipe_key_slot(selected_slot);
194 if (status != PSA_SUCCESS) {
195 goto error;
196 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000200 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
201 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (status != PSA_SUCCESS) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000203 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100205
Ronald Crona5b894f2020-10-21 09:04:34 +0200206 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200208 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 }
212 status = PSA_ERROR_INSUFFICIENT_MEMORY;
213
214error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200215 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200216 *volatile_key_id = 0;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100219}
220
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100221#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100223{
224 psa_status_t status = PSA_SUCCESS;
225 uint8_t *key_data = NULL;
226 size_t key_data_length = 0;
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 status = psa_load_persistent_key(&slot->attr,
229 &key_data, &key_data_length);
230 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100231 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200233
Steven Cooreman98435dd2021-01-08 19:19:40 +0100234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100235 /* Special handling is required for loading keys associated with a
236 * dynamically registered SE interface. */
237 const psa_drv_se_t *drv;
238 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100240 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100243 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100244 goto exit;
245 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000246 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000247 status = psa_copy_key_material_into_slot(
248 slot, data->slot_number, sizeof(data->slot_number));
249
250 if (status == PSA_SUCCESS) {
251 slot->status = PSA_SLOT_OCCUPIED;
252 }
253 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200254 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000258 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000259 goto exit;
260 }
261
262 slot->status = PSA_SLOT_OCCUPIED;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200263
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 psa_free_persistent_key_data(key_data, key_data_length);
266 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100267}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200268#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
269
Steven Cooreman6801f082021-02-19 17:21:22 +0100270#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100273{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100276 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100277 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100278 size_t key_buffer_size = 0;
279 size_t key_buffer_length = 0;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (!psa_key_id_is_builtin(
282 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
283 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100284 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100285
286 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100287 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 slot->attr.id, &lifetime, &slot_number);
289 if (status != PSA_SUCCESS) {
290 return status;
291 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100292
Steven Cooreman966db262021-04-13 13:45:45 +0200293 /* Set required key attributes to ensure get_builtin_key can retrieve the
294 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 psa_set_key_id(&attributes, slot->attr.id);
296 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100297
Steven Cooremance487022021-04-07 18:09:53 +0200298 /* Get the full key attributes from the driver in order to be able to
299 * calculate the required buffer size. */
300 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 slot_number, &attributes,
302 NULL, 0, NULL);
303 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200304 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200306 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 }
308 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200309 }
310
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200311 /* If the key should exist according to the platform, then ask the driver
312 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
314 &key_buffer_size);
315 if (status != PSA_SUCCESS) {
316 return status;
317 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100318
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200319 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200320 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
322 if (status != PSA_SUCCESS) {
323 return status;
324 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100325
326 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 slot_number, &attributes,
328 slot->key.data, slot->key.bytes, &key_buffer_length);
329 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100332
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200333 /* Copy actual key length and core attributes into the slot on success */
334 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100335 slot->attr = attributes.core;
Ryan Everett5567e3a2023-11-08 13:28:20 +0000336 slot->status = PSA_SLOT_OCCUPIED;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100337
338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (status != PSA_SUCCESS) {
340 psa_remove_key_data_from_memory(slot);
341 }
342 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100343}
344#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
347 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200348{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200349 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200350
351 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (!global_data.key_slots_initialized) {
353 return PSA_ERROR_BAD_STATE;
354 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200355
Ronald Cronf95a2b12020-10-22 15:24:49 +0200356 /*
357 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100358 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200359 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
361 if (status != PSA_ERROR_DOES_NOT_EXIST) {
362 return status;
363 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200364
Steven Cooreman0bb65362021-04-06 15:09:57 +0200365 /* Loading keys from storage requires support for such a mechanism */
366#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
367 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200368 psa_key_id_t volatile_key_id;
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
371 if (status != PSA_SUCCESS) {
372 return status;
373 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200374
Ronald Cronc4d1b512020-07-31 11:26:37 +0200375 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100376 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200377
Steven Cooreman6801f082021-02-19 17:21:22 +0100378 status = PSA_ERROR_DOES_NOT_EXIST;
379#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200380 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100382#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
383
384#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (status == PSA_ERROR_DOES_NOT_EXIST) {
386 status = psa_load_persistent_key_into_slot(*p_slot);
387 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100388#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (status != PSA_SUCCESS) {
391 psa_wipe_key_slot(*p_slot);
392 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000393 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 }
395 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200396 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
398 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200401#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200403#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200404}
405
Ryan Everett39cc9d72023-12-21 17:57:14 +0000406psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200407{
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (slot == NULL) {
409 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200410 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000411 if ((slot->state != PSA_SLOT_FULL) &&
412 (slot->state != PSA_SLOT_PENDING_DELETION)) {
413 return PSA_ERROR_BAD_STATE;
414 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200415
Ryan Everett39cc9d72023-12-21 17:57:14 +0000416 /* If we are the last reader and the slot is marked for deletion,
417 * we must wipe the slot here. */
418 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
419 (slot->registered_readers == 1)) {
420 return psa_wipe_key_slot(slot);
421 }
422
423 if (psa_key_slot_has_readers(slot)) {
424 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return PSA_SUCCESS;
426 }
427
428 /*
429 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000430 * do our best to report if there are no registered readers. Assert with
431 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
432 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 * the function is called as part of the execution of a test suite, the
434 * execution of the test suite is stopped in error if the assertion fails.
435 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000436 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200438}
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
441 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200442{
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200444#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100445 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
447 if (driver != NULL) {
448 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200449 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 }
451 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200452 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100453#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200454 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100455#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
456
Steven Cooreman98435dd2021-01-08 19:19:40 +0100457 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200460 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return PSA_SUCCESS;
462 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200463}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200466{
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200468 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return PSA_SUCCESS;
470 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200471 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200472#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
474 return PSA_ERROR_INVALID_ARGUMENT;
475 } else {
476 return PSA_SUCCESS;
477 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200478#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200480#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200481 }
Gilles Peskined167b942019-04-19 18:19:40 +0200482}
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100485{
Archana0dc86b52021-07-14 13:59:48 +0530486#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
487 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100488 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200489 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 status = psa_get_and_lock_key_slot(key, &slot);
492 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200493 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000495 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100499 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200500
501 *handle = key;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return psa_unlock_key_slot(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200504
Archana0dc86b52021-07-14 13:59:48 +0530505#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200506 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200507 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530509#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100510}
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100513{
Gilles Peskine267c6562019-05-27 19:01:54 +0200514 psa_status_t status;
515 psa_key_slot_t *slot;
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (psa_key_handle_is_null(handle)) {
518 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000519 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100520
521 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
522 if (status != PSA_SUCCESS) {
523 if (status == PSA_ERROR_DOES_NOT_EXIST) {
524 status = PSA_ERROR_INVALID_HANDLE;
525 }
526
527 return status;
528 }
529 if (slot->lock_count <= 1) {
530 return psa_wipe_key_slot(slot);
531 } else {
532 return psa_unlock_key_slot(slot);
533 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100534}
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200537{
538 psa_status_t status;
539 psa_key_slot_t *slot;
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
542 if (status != PSA_SUCCESS) {
543 return status;
544 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
547 (slot->lock_count <= 1)) {
548 return psa_wipe_key_slot(slot);
549 } else {
550 return psa_unlock_key_slot(slot);
551 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200552}
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200555{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200556 size_t slot_idx;
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
561 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
562 if (psa_is_key_slot_locked(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100563 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100564 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (!psa_is_key_slot_occupied(slot)) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200566 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200567 continue;
568 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200570 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 } else {
572 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200573 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100575 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200577 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
579 PSA_KEY_LOCATION_LOCAL_STORAGE) {
580 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200581 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100583 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200585 }
586 }
587}
588
Gilles Peskine961849f2018-11-30 18:54:54 +0100589#endif /* MBEDTLS_PSA_CRYPTO_C */