blob: b79c713abbab117811e54309ab9a54fee04b9944 [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 Peskine1b6c09a2023-01-11 14:52:35 +010029typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010030 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskine66fb1262018-12-10 16:29:04 +010031 unsigned key_slots_initialized : 1;
32} psa_global_data_t;
33
Gilles Peskine2e14bd32018-12-12 14:05:08 +010034static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020037{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
41 (key_id <= PSA_KEY_ID_USER_MAX)) {
42 return 1;
43 }
Ronald Crond2ed4812020-07-17 16:11:30 +020044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 if (vendor_ok &&
46 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
47 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
48 return 1;
49 }
Ronald Crond2ed4812020-07-17 16:11:30 +020050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020052}
53
Ronald Cron5c522922020-11-14 16:35:34 +010054/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020055 *
Ronald Cron5c522922020-11-14 16:35:34 +010056 * The descriptions of volatile keys and loaded persistent keys are
57 * stored in key slots. This function returns a pointer to the key slot
58 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020059 *
Ronald Cron5c522922020-11-14 16:35:34 +010060 * The function searches the key slots containing the description of the key
61 * with \p key identifier. The function does only read accesses to the key
62 * slots. The function does not load any persistent key thus does not access
63 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020064 *
Ronald Cron5c522922020-11-14 16:35:34 +010065 * For volatile key identifiers, only one key slot is queried as a volatile
66 * key with identifier key_id can only be stored in slot of index
67 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020068 *
Ronald Cron5c522922020-11-14 16:35:34 +010069 * On success, the function locks the key slot. It is the responsibility of
70 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020071 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020072 * \param key Key identifier to query.
73 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
74 * key slot containing the description of the key
75 * identified by \p key.
76 *
Ronald Cron96783552020-10-19 12:06:30 +020077 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020078 * The pointer to the key slot containing the description of the key
79 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020080 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020081 * \p key is not a valid key identifier.
82 * \retval #PSA_ERROR_DOES_NOT_EXIST
83 * There is no key with key identifier \p key in the key slots.
84 */
Ronald Cron5c522922020-11-14 16:35:34 +010085static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +010087{
Ronald Cronf473d8b2020-11-12 10:07:21 +010088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +010090 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +020091 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +010092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if (psa_key_id_is_volatile(key_id)) {
94 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +010095
96 /*
97 * Check if both the PSA key identifier key_id and the owner
98 * identifier of key match those of the key slot.
99 *
100 * Note that, if the key slot is not occupied, its PSA key identifier
101 * is equal to zero. This is an invalid value for a PSA key identifier
102 * and thus cannot be equal to the valid PSA key identifier key_id.
103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100105 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 } else {
107 if (!psa_is_valid_key_id(key, 1)) {
108 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200109 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110
111 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
112 slot = &global_data.key_slots[slot_idx];
113 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
114 break;
115 }
116 }
117 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100118 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200119 }
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 if (status == PSA_SUCCESS) {
122 status = psa_lock_key_slot(slot);
123 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100124 *p_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200126 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200129}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100132{
133 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
134 * guarantee that the key slots are initialized to all-zero, which
135 * means that all the key slots are in a valid, empty state. */
136 global_data.key_slots_initialized = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100138}
139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100141{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200142 size_t slot_idx;
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
145 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ronald Cron5c522922020-11-14 16:35:34 +0100146 slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100148 }
149 global_data.key_slots_initialized = 0;
150}
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
153 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100154{
Ronald Crona5b894f2020-10-21 09:04:34 +0200155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200156 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100157 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200160 status = PSA_ERROR_BAD_STATE;
161 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100162 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200163
Ronald Cron5c522922020-11-14 16:35:34 +0100164 selected_slot = unlocked_persistent_key_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
166 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
167 if (!psa_is_key_slot_occupied(slot)) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200168 selected_slot = slot;
169 break;
170 }
171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 if ((unlocked_persistent_key_slot == NULL) &&
173 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
174 (!psa_is_key_slot_locked(slot))) {
Ronald Cron5c522922020-11-14 16:35:34 +0100175 unlocked_persistent_key_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200177 }
178
179 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100180 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100181 * slot containing the description of a persistent key, recycle the first
182 * such key slot we encountered. If we later need to operate on the
183 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100184 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if ((selected_slot == NULL) &&
187 (unlocked_persistent_key_slot != NULL)) {
Ronald Cron5c522922020-11-14 16:35:34 +0100188 selected_slot = unlocked_persistent_key_slot;
189 selected_slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 psa_wipe_key_slot(selected_slot);
Ronald Crona5b894f2020-10-21 09:04:34 +0200191 }
192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if (selected_slot != NULL) {
194 status = psa_lock_key_slot(selected_slot);
195 if (status != PSA_SUCCESS) {
196 goto error;
197 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100198
Ronald Crona5b894f2020-10-21 09:04:34 +0200199 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200201 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200204 }
205 status = PSA_ERROR_INSUFFICIENT_MEMORY;
206
207error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200208 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200209 *volatile_key_id = 0;
210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100212}
213
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100214#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100216{
217 psa_status_t status = PSA_SUCCESS;
218 uint8_t *key_data = NULL;
219 size_t key_data_length = 0;
220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 status = psa_load_persistent_key(&slot->attr,
222 &key_data, &key_data_length);
223 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100224 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200226
Steven Cooreman98435dd2021-01-08 19:19:40 +0100227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100228 /* Special handling is required for loading keys associated with a
229 * dynamically registered SE interface. */
230 const psa_drv_se_t *drv;
231 psa_drv_se_context_t *drv_context;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100233 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100236 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100237 goto exit;
238 }
239 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100240 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 slot, data->slot_number, sizeof(data->slot_number));
Steven Cooremanac3434f2021-01-15 17:36:02 +0100242 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200243 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100244#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Gilles Peskine1df83d42019-07-23 16:13:14 +0200247
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100248exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 psa_free_persistent_key_data(key_data, key_data_length);
250 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100251}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200252#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
253
Steven Cooreman6801f082021-02-19 17:21:22 +0100254#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100257{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100260 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100261 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100262 size_t key_buffer_size = 0;
263 size_t key_buffer_length = 0;
264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 if (!psa_key_id_is_builtin(
266 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
267 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100268 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100269
270 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100271 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 slot->attr.id, &lifetime, &slot_number);
273 if (status != PSA_SUCCESS) {
274 return status;
275 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100276
Steven Cooreman966db262021-04-13 13:45:45 +0200277 /* Set required key attributes to ensure get_builtin_key can retrieve the
278 * full attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 psa_set_key_id(&attributes, slot->attr.id);
280 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100281
Steven Cooremance487022021-04-07 18:09:53 +0200282 /* Get the full key attributes from the driver in order to be able to
283 * calculate the required buffer size. */
284 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 slot_number, &attributes,
286 NULL, 0, NULL);
287 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200288 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200290 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 }
292 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200293 }
294
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200295 /* If the key should exist according to the platform, then ask the driver
296 * what its expected size is. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
298 &key_buffer_size);
299 if (status != PSA_SUCCESS) {
300 return status;
301 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100302
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200303 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200304 * into the (now properly sized) slot buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
306 if (status != PSA_SUCCESS) {
307 return status;
308 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100309
310 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 slot_number, &attributes,
312 slot->key.data, slot->key.bytes, &key_buffer_length);
313 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100314 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100316
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200317 /* Copy actual key length and core attributes into the slot on success */
318 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100319 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100320
321exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 if (status != PSA_SUCCESS) {
323 psa_remove_key_data_from_memory(slot);
324 }
325 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100326}
327#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
330 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200331{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200333
334 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 if (!global_data.key_slots_initialized) {
336 return PSA_ERROR_BAD_STATE;
337 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200338
Ronald Cronf95a2b12020-10-22 15:24:49 +0200339 /*
340 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100341 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
344 if (status != PSA_ERROR_DOES_NOT_EXIST) {
345 return status;
346 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200347
Steven Cooreman0bb65362021-04-06 15:09:57 +0200348 /* Loading keys from storage requires support for such a mechanism */
349#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
350 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200351 psa_key_id_t volatile_key_id;
352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
354 if (status != PSA_SUCCESS) {
355 return status;
356 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200357
Ronald Cronc4d1b512020-07-31 11:26:37 +0200358 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100359 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200360
Steven Cooreman6801f082021-02-19 17:21:22 +0100361 status = PSA_ERROR_DOES_NOT_EXIST;
362#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200363 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100365#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
366
367#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 if (status == PSA_ERROR_DOES_NOT_EXIST) {
369 status = psa_load_persistent_key_into_slot(*p_slot);
370 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100371#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 if (status != PSA_SUCCESS) {
374 psa_wipe_key_slot(*p_slot);
375 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000376 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 }
378 } else {
gabor-mezei-arm6c185412021-06-28 14:59:52 +0200379 /* Add implicit usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
381 }
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200384#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200386#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200387}
388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200390{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 if (slot == NULL) {
392 return PSA_SUCCESS;
393 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if (slot->lock_count > 0) {
Ronald Cron5c522922020-11-14 16:35:34 +0100396 slot->lock_count--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200398 }
399
400 /*
401 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100402 * do our best to report if the lock counter is equal to zero: if
Ronald Cronf95a2b12020-10-22 15:24:49 +0200403 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
404 * called as part of the execution of a unit test suite this will stop the
Ronald Cron4640c152020-11-13 10:11:01 +0100405 * test suite execution).
Ronald Cronf95a2b12020-10-22 15:24:49 +0200406 */
407#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 MBEDTLS_PARAM_FAILED(slot->lock_count > 0);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200409#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200412}
413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
415 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200416{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200418#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100419 /* Check whether a driver is registered against this lifetime */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
421 if (driver != NULL) {
422 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200423 *p_drv = driver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 }
425 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200426 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100427#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200428 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
430
Steven Cooreman98435dd2021-01-08 19:19:40 +0100431#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
432 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 return PSA_SUCCESS;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100434#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
435 /* No support for external lifetimes at all, or dynamic interface
436 * did not find driver for requested lifetime. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100438#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200440 /* Local/internal keys are always valid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return PSA_SUCCESS;
442 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200443}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200446{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200448 /* Volatile keys are always supported */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return PSA_SUCCESS;
450 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200451 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200452#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
454 return PSA_ERROR_INVALID_ARGUMENT;
455 } else {
456 return PSA_SUCCESS;
457 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200458#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200460#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200461 }
Gilles Peskined167b942019-04-19 18:19:40 +0200462}
463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100465{
Archana6d342f32021-07-14 13:59:48 +0530466#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
467 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100468 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200469 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 status = psa_get_and_lock_key_slot(key, &slot);
472 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200473 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000475 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100479 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200480
481 *handle = key;
482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 return psa_unlock_key_slot(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200484
Archana6d342f32021-07-14 13:59:48 +0530485#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200486 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200487 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 return PSA_ERROR_NOT_SUPPORTED;
Archana6d342f32021-07-14 13:59:48 +0530489#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100490}
491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100493{
Gilles Peskine267c6562019-05-27 19:01:54 +0200494 psa_status_t status;
495 psa_key_slot_t *slot;
496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 if (psa_key_handle_is_null(handle)) {
498 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000499 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500
501 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
502 if (status != PSA_SUCCESS) {
503 if (status == PSA_ERROR_DOES_NOT_EXIST) {
504 status = PSA_ERROR_INVALID_HANDLE;
505 }
506
507 return status;
508 }
509 if (slot->lock_count <= 1) {
510 return psa_wipe_key_slot(slot);
511 } else {
512 return psa_unlock_key_slot(slot);
513 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100514}
515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200517{
518 psa_status_t status;
519 psa_key_slot_t *slot;
520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
522 if (status != PSA_SUCCESS) {
523 return status;
524 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
527 (slot->lock_count <= 1)) {
528 return psa_wipe_key_slot(slot);
529 } else {
530 return psa_unlock_key_slot(slot);
531 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200532}
533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200535{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200536 size_t slot_idx;
537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
541 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
542 if (psa_is_key_slot_locked(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100543 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100544 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 if (!psa_is_key_slot_occupied(slot)) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200546 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200547 continue;
548 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200550 ++stats->volatile_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 } else {
552 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200553 ++stats->persistent_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100555 stats->max_open_internal_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200557 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
559 PSA_KEY_LOCATION_LOCAL_STORAGE) {
560 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200561 ++stats->external_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100563 stats->max_open_external_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100564 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200565 }
566 }
567}
568
Gilles Peskine961849f2018-11-30 18:54:54 +0100569#endif /* MBEDTLS_PSA_CRYPTO_C */