blob: 47ace359d786a1b8a297cc6370c0e5b5cef75f4a [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine961849f2018-11-30 18:54:54 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include "psa/crypto.h"
14
Gilles Peskine66fb1262018-12-10 16:29:04 +010015#include "psa_crypto_core.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000016#include "psa_crypto_driver_wrappers_no_static.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010017#include "psa_crypto_slot_management.h"
18#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
20#include "psa_crypto_se.h"
21#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010022
23#include <stdlib.h>
24#include <string.h>
Gilles Peskine961849f2018-11-30 18:54:54 +010025#include "mbedtls/platform.h"
Ryan Everett491f7e52024-01-08 11:04:21 +000026#if defined(MBEDTLS_THREADING_C)
27#include "mbedtls/threading.h"
28#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010029
Gilles Peskine449bd832023-01-11 14:50:10 +010030typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010031 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Dave Rodgman164614a2023-08-16 17:56:28 +010032 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +010033} psa_global_data_t;
34
Gilles Peskine2e14bd32018-12-12 14:05:08 +010035static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010036
Gilles Peskine449bd832023-01-11 14:50:10 +010037int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020038{
Gilles Peskine449bd832023-01-11 14:50:10 +010039 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020040
Gilles Peskine449bd832023-01-11 14:50:10 +010041 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
42 (key_id <= PSA_KEY_ID_USER_MAX)) {
43 return 1;
44 }
Ronald Crond2ed4812020-07-17 16:11:30 +020045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 if (vendor_ok &&
47 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
48 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
49 return 1;
50 }
Ronald Crond2ed4812020-07-17 16:11:30 +020051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020053}
54
Ronald Cron5c522922020-11-14 16:35:34 +010055/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020056 *
Ronald Cron5c522922020-11-14 16:35:34 +010057 * The descriptions of volatile keys and loaded persistent keys are
58 * stored in key slots. This function returns a pointer to the key slot
59 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020060 *
Ronald Cron5c522922020-11-14 16:35:34 +010061 * The function searches the key slots containing the description of the key
62 * with \p key identifier. The function does only read accesses to the key
63 * slots. The function does not load any persistent key thus does not access
64 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020065 *
Ronald Cron5c522922020-11-14 16:35:34 +010066 * For volatile key identifiers, only one key slot is queried as a volatile
67 * key with identifier key_id can only be stored in slot of index
68 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020069 *
Ronald Cron5c522922020-11-14 16:35:34 +010070 * On success, the function locks the key slot. It is the responsibility of
71 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020072 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020073 * \param key Key identifier to query.
74 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
75 * key slot containing the description of the key
76 * identified by \p key.
77 *
Ronald Cron96783552020-10-19 12:06:30 +020078 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020079 * The pointer to the key slot containing the description of the key
80 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020081 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020082 * \p key is not a valid key identifier.
83 * \retval #PSA_ERROR_DOES_NOT_EXIST
84 * There is no key with key identifier \p key in the key slots.
85 */
Ronald Cron5c522922020-11-14 16:35:34 +010086static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +010088{
Ronald Cronf473d8b2020-11-12 10:07:21 +010089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +010091 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +020092 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +010093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (psa_key_id_is_volatile(key_id)) {
95 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +010096
97 /*
98 * Check if both the PSA key identifier key_id and the owner
99 * identifier of key match those of the key slot.
100 *
101 * Note that, if the key slot is not occupied, its PSA key identifier
102 * is equal to zero. This is an invalid value for a PSA key identifier
103 * and thus cannot be equal to the valid PSA key identifier key_id.
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100106 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 } else {
108 if (!psa_is_valid_key_id(key, 1)) {
109 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200110 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100111
112 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
113 slot = &global_data.key_slots[slot_idx];
Ryan Everett098c6652024-01-03 13:03:36 +0000114 /* Only consider slots which are in a full state. */
115 if ((slot->state == PSA_SLOT_FULL) &&
116 (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 break;
118 }
119 }
120 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100121 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (status == PSA_SUCCESS) {
Ryan Everett098c6652024-01-03 13:03:36 +0000125 status = psa_register_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100127 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200129 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200132}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100135{
Ryan Everett558da2f2024-01-19 12:59:28 +0000136 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
Gilles Peskine66fb1262018-12-10 16:29:04 +0100137 * guarantee that the key slots are initialized to all-zero, which
138 * means that all the key slots are in a valid, empty state. */
139 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100141}
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100144{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200145 size_t slot_idx;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
148 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6cd2b8d2024-01-04 12:10:18 +0000149 slot->registered_readers = 1;
150 slot->state = PSA_SLOT_PENDING_DELETION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100152 }
153 global_data.key_slots_initialized = 0;
154}
155
Ryan Everett2afb5162023-12-22 15:59:45 +0000156psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
157 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100158{
Ronald Crona5b894f2020-10-21 09:04:34 +0200159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200160 size_t slot_idx;
Ryan Everett2afb5162023-12-22 15:59:45 +0000161 psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200164 status = PSA_ERROR_BAD_STATE;
165 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100166 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200167
Ryan Everett2afb5162023-12-22 15:59:45 +0000168 selected_slot = unused_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
170 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett2afb5162023-12-22 15:59:45 +0000171 if (slot->state == PSA_SLOT_EMPTY) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200172 selected_slot = slot;
173 break;
174 }
175
Ryan Everett2afb5162023-12-22 15:59:45 +0000176 if ((unused_persistent_key_slot == NULL) &&
177 (slot->state == PSA_SLOT_FULL) &&
178 (!psa_key_slot_has_readers(slot)) &&
179 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
180 unused_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200182 }
183
184 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100185 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100186 * slot containing the description of a persistent key, recycle the first
187 * such key slot we encountered. If we later need to operate on the
188 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100189 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if ((selected_slot == NULL) &&
Ryan Everett2afb5162023-12-22 15:59:45 +0000192 (unused_persistent_key_slot != NULL)) {
193 selected_slot = unused_persistent_key_slot;
194 psa_register_read(selected_slot);
Ryan Everett2afb5162023-12-22 15:59:45 +0000195 status = psa_wipe_key_slot(selected_slot);
196 if (status != PSA_SUCCESS) {
197 goto error;
198 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200199 }
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (selected_slot != NULL) {
Ryan Everett2afb5162023-12-22 15:59:45 +0000202 status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
203 PSA_SLOT_FILLING);
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (status != PSA_SUCCESS) {
Ryan Everett709120a2024-01-15 11:19:03 +0000205 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100207
Ronald Crona5b894f2020-10-21 09:04:34 +0200208 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200210 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200213 }
214 status = PSA_ERROR_INSUFFICIENT_MEMORY;
215
216error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200217 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200218 *volatile_key_id = 0;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100221}
222
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100223#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100225{
226 psa_status_t status = PSA_SUCCESS;
227 uint8_t *key_data = NULL;
228 size_t key_data_length = 0;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 status = psa_load_persistent_key(&slot->attr,
231 &key_data, &key_data_length);
232 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200235
Steven Cooreman98435dd2021-01-08 19:19:40 +0100236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100237 /* Special handling is required for loading keys associated with a
238 * dynamically registered SE interface. */
239 const psa_drv_se_t *drv;
240 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100242 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100245 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100246 goto exit;
247 }
Ryan Everettd69f4012023-11-23 16:20:45 +0000248 data = (psa_se_key_data_storage_t *) key_data;
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000249 status = psa_copy_key_material_into_slot(
250 slot, data->slot_number, sizeof(data->slot_number));
251
252 if (status == PSA_SUCCESS) {
Ryan Everetteb27dc02024-01-03 16:19:12 +0000253 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
254 PSA_SLOT_FULL);
Ryan Everett2a0d4e22023-11-23 16:33:12 +0000255 }
256 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200257 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100258#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Ryan Everett9f176a22023-11-21 11:49:57 +0000261 if (status != PSA_SUCCESS) {
Ryan Everett975d4112023-11-16 13:37:51 +0000262 goto exit;
263 }
264
Ryan Everetteb27dc02024-01-03 16:19:12 +0000265 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
266 PSA_SLOT_FULL);
Gilles Peskine1df83d42019-07-23 16:13:14 +0200267
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 psa_free_persistent_key_data(key_data, key_data_length);
270 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100271}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200272#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
273
Steven Cooreman6801f082021-02-19 17:21:22 +0100274#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100277{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100280 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100281 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100282 size_t key_buffer_size = 0;
283 size_t key_buffer_length = 0;
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (!psa_key_id_is_builtin(
286 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
287 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100288 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100289
290 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100291 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 slot->attr.id, &lifetime, &slot_number);
293 if (status != PSA_SUCCESS) {
294 return status;
295 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100296
Steven Cooreman966db262021-04-13 13:45:45 +0200297 /* Set required key attributes to ensure get_builtin_key can retrieve the
298 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 psa_set_key_id(&attributes, slot->attr.id);
300 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100301
Steven Cooremance487022021-04-07 18:09:53 +0200302 /* Get the full key attributes from the driver in order to be able to
303 * calculate the required buffer size. */
304 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 slot_number, &attributes,
306 NULL, 0, NULL);
307 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200308 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200310 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 }
312 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200313 }
314
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200315 /* If the key should exist according to the platform, then ask the driver
316 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
318 &key_buffer_size);
319 if (status != PSA_SUCCESS) {
320 return status;
321 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100322
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200323 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200324 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
326 if (status != PSA_SUCCESS) {
327 return status;
328 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100329
330 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 slot_number, &attributes,
332 slot->key.data, slot->key.bytes, &key_buffer_length);
333 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100334 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100336
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200337 /* Copy actual key length and core attributes into the slot on success */
338 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100339 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100340
Ryan Everetteb27dc02024-01-03 16:19:12 +0000341 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
342 PSA_SLOT_FULL);
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (status != PSA_SUCCESS) {
345 psa_remove_key_data_from_memory(slot);
346 }
347 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100348}
349#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
352 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200353{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200355
356 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (!global_data.key_slots_initialized) {
358 return PSA_ERROR_BAD_STATE;
359 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200360
Ronald Cronf95a2b12020-10-22 15:24:49 +0200361 /*
362 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100363 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
366 if (status != PSA_ERROR_DOES_NOT_EXIST) {
367 return status;
368 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200369
Steven Cooreman0bb65362021-04-06 15:09:57 +0200370 /* Loading keys from storage requires support for such a mechanism */
371#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
372 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200373 psa_key_id_t volatile_key_id;
374
Ryan Everett098c6652024-01-03 13:03:36 +0000375 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (status != PSA_SUCCESS) {
377 return status;
378 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200379
Ronald Cronc4d1b512020-07-31 11:26:37 +0200380 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100381 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200382
Steven Cooreman6801f082021-02-19 17:21:22 +0100383 status = PSA_ERROR_DOES_NOT_EXIST;
384#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200385 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100387#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
388
389#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (status == PSA_ERROR_DOES_NOT_EXIST) {
391 status = psa_load_persistent_key_into_slot(*p_slot);
392 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100393#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (status != PSA_SUCCESS) {
396 psa_wipe_key_slot(*p_slot);
Ryan Everett098c6652024-01-03 13:03:36 +0000397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000399 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 }
401 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200402 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
Ryan Everett098c6652024-01-03 13:03:36 +0000404
405 psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
406 PSA_SLOT_FULL);
407 status = psa_register_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200411#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200413#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200414}
415
Ryan Everett39cc9d72023-12-21 17:57:14 +0000416psa_status_t psa_unregister_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200417{
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (slot == NULL) {
419 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200420 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000421 if ((slot->state != PSA_SLOT_FULL) &&
422 (slot->state != PSA_SLOT_PENDING_DELETION)) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000423 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett39cc9d72023-12-21 17:57:14 +0000424 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200425
Ryan Everett39cc9d72023-12-21 17:57:14 +0000426 /* If we are the last reader and the slot is marked for deletion,
427 * we must wipe the slot here. */
428 if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
429 (slot->registered_readers == 1)) {
430 return psa_wipe_key_slot(slot);
431 }
432
433 if (psa_key_slot_has_readers(slot)) {
434 slot->registered_readers--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return PSA_SUCCESS;
436 }
437
438 /*
439 * As the return error code may not be handled in case of multiple errors,
Ryan Everett39cc9d72023-12-21 17:57:14 +0000440 * do our best to report if there are no registered readers. Assert with
441 * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
442 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 * the function is called as part of the execution of a test suite, the
444 * execution of the test suite is stopped in error if the assertion fails.
445 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000446 MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200448}
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
451 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200452{
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200454#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100455 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
457 if (driver != NULL) {
458 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200459 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 }
461 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200462 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100463#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200464 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100465#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
466
Steven Cooreman98435dd2021-01-08 19:19:40 +0100467 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200470 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return PSA_SUCCESS;
472 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200473}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200476{
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200478 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return PSA_SUCCESS;
480 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200481 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200482#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
484 return PSA_ERROR_INVALID_ARGUMENT;
485 } else {
486 return PSA_SUCCESS;
487 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200488#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200490#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200491 }
Gilles Peskined167b942019-04-19 18:19:40 +0200492}
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100495{
Archana0dc86b52021-07-14 13:59:48 +0530496#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
497 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100498 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200499 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 status = psa_get_and_lock_key_slot(key, &slot);
502 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200503 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000505 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100509 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200510
511 *handle = key;
512
Ryan Everett1b70a072024-01-04 10:32:49 +0000513 return psa_unregister_read(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200514
Archana0dc86b52021-07-14 13:59:48 +0530515#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200516 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200517 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530519#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100520}
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100523{
Gilles Peskine267c6562019-05-27 19:01:54 +0200524 psa_status_t status;
525 psa_key_slot_t *slot;
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (psa_key_handle_is_null(handle)) {
528 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000529 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100530
531 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
532 if (status != PSA_SUCCESS) {
533 if (status == PSA_ERROR_DOES_NOT_EXIST) {
534 status = PSA_ERROR_INVALID_HANDLE;
535 }
536
537 return status;
538 }
Ryan Everettc70ce572024-01-03 16:04:33 +0000539 if (slot->registered_readers == 1) {
Ryan Everett4755e6b2024-01-12 16:35:59 +0000540 return psa_wipe_key_slot(slot);
541 } else {
542 return psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100544}
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200547{
548 psa_status_t status;
549 psa_key_slot_t *slot;
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
552 if (status != PSA_SUCCESS) {
553 return status;
554 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
Ryan Everettc70ce572024-01-03 16:04:33 +0000557 (slot->registered_readers == 1)) {
Ryan Everett4755e6b2024-01-12 16:35:59 +0000558 return psa_wipe_key_slot(slot);
559 } else {
560 return psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200562}
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200565{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200566 size_t slot_idx;
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
571 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000572 if (psa_key_slot_has_readers(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100573 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100574 }
Ryan Everett6a9c14b2024-01-04 12:13:45 +0000575 if (slot->state == PSA_SLOT_EMPTY) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200576 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200577 continue;
578 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200580 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 } else {
582 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200583 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100585 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200587 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
589 PSA_KEY_LOCATION_LOCAL_STORAGE) {
590 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200591 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100593 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200595 }
596 }
597}
598
Gilles Peskine961849f2018-11-30 18:54:54 +0100599#endif /* MBEDTLS_PSA_CRYPTO_C */