blob: ef285acb1b8ad14eb9f1466cd283ce7a01158b35 [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
Gilles Peskine961849f2018-11-30 18:54:54 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskine961849f2018-11-30 18:54:54 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include "psa/crypto.h"
26
Gilles Peskine66fb1262018-12-10 16:29:04 +010027#include "psa_crypto_core.h"
Steven Cooremane3842522021-03-18 18:52:44 +010028#include "psa_crypto_driver_wrappers.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010029#include "psa_crypto_slot_management.h"
30#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
32#include "psa_crypto_se.h"
33#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034
35#include <stdlib.h>
36#include <string.h>
Gilles Peskine961849f2018-11-30 18:54:54 +010037#include "mbedtls/platform.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010038
Gilles Peskine449bd832023-01-11 14:50:10 +010039typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010040 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Dave Rodgman164614a2023-08-16 17:56:28 +010041 uint8_t key_slots_initialized;
Gilles Peskine66fb1262018-12-10 16:29:04 +010042} psa_global_data_t;
43
Gilles Peskine2e14bd32018-12-12 14:05:08 +010044static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020047{
Gilles Peskine449bd832023-01-11 14:50:10 +010048 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
51 (key_id <= PSA_KEY_ID_USER_MAX)) {
52 return 1;
53 }
Ronald Crond2ed4812020-07-17 16:11:30 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if (vendor_ok &&
56 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
57 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
58 return 1;
59 }
Ronald Crond2ed4812020-07-17 16:11:30 +020060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020062}
63
Ronald Cron5c522922020-11-14 16:35:34 +010064/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020065 *
Ronald Cron5c522922020-11-14 16:35:34 +010066 * The descriptions of volatile keys and loaded persistent keys are
67 * stored in key slots. This function returns a pointer to the key slot
68 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020069 *
Ronald Cron5c522922020-11-14 16:35:34 +010070 * The function searches the key slots containing the description of the key
71 * with \p key identifier. The function does only read accesses to the key
72 * slots. The function does not load any persistent key thus does not access
73 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020074 *
Ronald Cron5c522922020-11-14 16:35:34 +010075 * For volatile key identifiers, only one key slot is queried as a volatile
76 * key with identifier key_id can only be stored in slot of index
77 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020078 *
Ronald Cron5c522922020-11-14 16:35:34 +010079 * On success, the function locks the key slot. It is the responsibility of
80 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020081 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020082 * \param key Key identifier to query.
83 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
84 * key slot containing the description of the key
85 * identified by \p key.
86 *
Ronald Cron96783552020-10-19 12:06:30 +020087 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020088 * The pointer to the key slot containing the description of the key
89 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020090 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020091 * \p key is not a valid key identifier.
92 * \retval #PSA_ERROR_DOES_NOT_EXIST
93 * There is no key with key identifier \p key in the key slots.
94 */
Ronald Cron5c522922020-11-14 16:35:34 +010095static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine449bd832023-01-11 14:50:10 +010096 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +010097{
Ronald Cronf473d8b2020-11-12 10:07:21 +010098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100100 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200101 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if (psa_key_id_is_volatile(key_id)) {
104 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100105
106 /*
107 * Check if both the PSA key identifier key_id and the owner
108 * identifier of key match those of the key slot.
109 *
110 * Note that, if the key slot is not occupied, its PSA key identifier
111 * is equal to zero. This is an invalid value for a PSA key identifier
112 * and thus cannot be equal to the valid PSA key identifier key_id.
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100115 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 } else {
117 if (!psa_is_valid_key_id(key, 1)) {
118 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200119 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100120
121 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
122 slot = &global_data.key_slots[slot_idx];
123 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
124 break;
125 }
126 }
127 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100128 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (status == PSA_SUCCESS) {
132 status = psa_lock_key_slot(slot);
133 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100134 *p_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200136 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200139}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100142{
143 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
144 * guarantee that the key slots are initialized to all-zero, which
145 * means that all the key slots are in a valid, empty state. */
146 global_data.key_slots_initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100148}
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100151{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200152 size_t slot_idx;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
155 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ronald Cron5c522922020-11-14 16:35:34 +0100156 slot->lock_count = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100158 }
159 global_data.key_slots_initialized = 0;
160}
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
163 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100164{
Ronald Crona5b894f2020-10-21 09:04:34 +0200165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200166 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100167 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200170 status = PSA_ERROR_BAD_STATE;
171 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100172 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200173
Ronald Cron5c522922020-11-14 16:35:34 +0100174 selected_slot = unlocked_persistent_key_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
176 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
177 if (!psa_is_key_slot_occupied(slot)) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200178 selected_slot = slot;
179 break;
180 }
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if ((unlocked_persistent_key_slot == NULL) &&
183 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
184 (!psa_is_key_slot_locked(slot))) {
Ronald Cron5c522922020-11-14 16:35:34 +0100185 unlocked_persistent_key_slot = slot;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200187 }
188
189 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100190 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100191 * slot containing the description of a persistent key, recycle the first
192 * such key slot we encountered. If we later need to operate on the
193 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100194 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((selected_slot == NULL) &&
197 (unlocked_persistent_key_slot != NULL)) {
Ronald Cron5c522922020-11-14 16:35:34 +0100198 selected_slot = unlocked_persistent_key_slot;
199 selected_slot->lock_count = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 psa_wipe_key_slot(selected_slot);
Ronald Crona5b894f2020-10-21 09:04:34 +0200201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (selected_slot != NULL) {
204 status = psa_lock_key_slot(selected_slot);
205 if (status != PSA_SUCCESS) {
206 goto error;
207 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100208
Ronald Crona5b894f2020-10-21 09:04:34 +0200209 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200214 }
215 status = PSA_ERROR_INSUFFICIENT_MEMORY;
216
217error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200218 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200219 *volatile_key_id = 0;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100222}
223
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100224#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100226{
227 psa_status_t status = PSA_SUCCESS;
228 uint8_t *key_data = NULL;
229 size_t key_data_length = 0;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 status = psa_load_persistent_key(&slot->attr,
232 &key_data, &key_data_length);
233 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200236
Steven Cooreman98435dd2021-01-08 19:19:40 +0100237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100238 /* Special handling is required for loading keys associated with a
239 * dynamically registered SE interface. */
240 const psa_drv_se_t *drv;
241 psa_drv_se_context_t *drv_context;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100243 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100246 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100247 goto exit;
248 }
249 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100250 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 slot, data->slot_number, sizeof(data->slot_number));
Steven Cooremanac3434f2021-01-15 17:36:02 +0100252 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200253 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100254#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Gilles Peskine1df83d42019-07-23 16:13:14 +0200257
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 psa_free_persistent_key_data(key_data, key_data_length);
260 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100261}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200262#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
263
Steven Cooreman6801f082021-02-19 17:21:22 +0100264#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100267{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100268 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100270 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100271 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100272 size_t key_buffer_size = 0;
273 size_t key_buffer_length = 0;
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (!psa_key_id_is_builtin(
276 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
277 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100278 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100279
280 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100281 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 slot->attr.id, &lifetime, &slot_number);
283 if (status != PSA_SUCCESS) {
284 return status;
285 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100286
Steven Cooreman966db262021-04-13 13:45:45 +0200287 /* Set required key attributes to ensure get_builtin_key can retrieve the
288 * full attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 psa_set_key_id(&attributes, slot->attr.id);
290 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100291
Steven Cooremance487022021-04-07 18:09:53 +0200292 /* Get the full key attributes from the driver in order to be able to
293 * calculate the required buffer size. */
294 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 slot_number, &attributes,
296 NULL, 0, NULL);
297 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200298 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200300 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 }
302 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200303 }
304
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200305 /* If the key should exist according to the platform, then ask the driver
306 * what its expected size is. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
308 &key_buffer_size);
309 if (status != PSA_SUCCESS) {
310 return status;
311 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100312
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200313 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200314 * into the (now properly sized) slot buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
316 if (status != PSA_SUCCESS) {
317 return status;
318 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100319
320 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 slot_number, &attributes,
322 slot->key.data, slot->key.bytes, &key_buffer_length);
323 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100324 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100326
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200327 /* Copy actual key length and core attributes into the slot on success */
328 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100329 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100330
331exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (status != PSA_SUCCESS) {
333 psa_remove_key_data_from_memory(slot);
334 }
335 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100336}
337#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
340 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200341{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200343
344 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (!global_data.key_slots_initialized) {
346 return PSA_ERROR_BAD_STATE;
347 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200348
Ronald Cronf95a2b12020-10-22 15:24:49 +0200349 /*
350 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100351 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
354 if (status != PSA_ERROR_DOES_NOT_EXIST) {
355 return status;
356 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200357
Steven Cooreman0bb65362021-04-06 15:09:57 +0200358 /* Loading keys from storage requires support for such a mechanism */
359#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
360 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200361 psa_key_id_t volatile_key_id;
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
364 if (status != PSA_SUCCESS) {
365 return status;
366 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200367
Ronald Cronc4d1b512020-07-31 11:26:37 +0200368 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100369 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200370
Steven Cooreman6801f082021-02-19 17:21:22 +0100371 status = PSA_ERROR_DOES_NOT_EXIST;
372#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200373 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100375#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
376
377#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (status == PSA_ERROR_DOES_NOT_EXIST) {
379 status = psa_load_persistent_key_into_slot(*p_slot);
380 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100381#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (status != PSA_SUCCESS) {
384 psa_wipe_key_slot(*p_slot);
385 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000386 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
388 } else {
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200389 /* Add implicit usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
391 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200394#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200396#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200397}
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200400{
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (slot == NULL) {
402 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200403 }
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (slot->lock_count > 0) {
406 slot->lock_count--;
407 return PSA_SUCCESS;
408 }
409
410 /*
411 * As the return error code may not be handled in case of multiple errors,
412 * do our best to report if the lock counter is equal to zero. Assert with
413 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is strictly greater
414 * than zero: if the MBEDTLS_TEST_HOOKS configuration option is enabled and
415 * the function is called as part of the execution of a test suite, the
416 * execution of the test suite is stopped in error if the assertion fails.
417 */
418 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count > 0);
419 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200420}
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
423 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200424{
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100427 /* Check whether a driver is registered against this lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
429 if (driver != NULL) {
430 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200431 *p_drv = driver;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 }
433 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200434 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100435#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200436 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100437#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
438
Steven Cooreman98435dd2021-01-08 19:19:40 +0100439 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200442 /* Local/internal keys are always valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return PSA_SUCCESS;
444 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200445}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200448{
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200450 /* Volatile keys are always supported */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return PSA_SUCCESS;
452 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200453 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200454#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
456 return PSA_ERROR_INVALID_ARGUMENT;
457 } else {
458 return PSA_SUCCESS;
459 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200460#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200462#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200463 }
Gilles Peskined167b942019-04-19 18:19:40 +0200464}
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100467{
Archana0dc86b52021-07-14 13:59:48 +0530468#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
469 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100470 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200471 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 status = psa_get_and_lock_key_slot(key, &slot);
474 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200475 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000477 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100481 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200482
483 *handle = key;
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return psa_unlock_key_slot(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200486
Archana0dc86b52021-07-14 13:59:48 +0530487#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200488 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200489 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return PSA_ERROR_NOT_SUPPORTED;
Archana0dc86b52021-07-14 13:59:48 +0530491#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100492}
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100495{
Gilles Peskine267c6562019-05-27 19:01:54 +0200496 psa_status_t status;
497 psa_key_slot_t *slot;
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (psa_key_handle_is_null(handle)) {
500 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000501 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100502
503 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
504 if (status != PSA_SUCCESS) {
505 if (status == PSA_ERROR_DOES_NOT_EXIST) {
506 status = PSA_ERROR_INVALID_HANDLE;
507 }
508
509 return status;
510 }
511 if (slot->lock_count <= 1) {
512 return psa_wipe_key_slot(slot);
513 } else {
514 return psa_unlock_key_slot(slot);
515 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100516}
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200519{
520 psa_status_t status;
521 psa_key_slot_t *slot;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
524 if (status != PSA_SUCCESS) {
525 return status;
526 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
529 (slot->lock_count <= 1)) {
530 return psa_wipe_key_slot(slot);
531 } else {
532 return psa_unlock_key_slot(slot);
533 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200534}
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200537{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200538 size_t slot_idx;
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
543 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
544 if (psa_is_key_slot_locked(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100545 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100546 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (!psa_is_key_slot_occupied(slot)) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200548 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200549 continue;
550 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200552 ++stats->volatile_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 } else {
554 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200555 ++stats->persistent_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100557 stats->max_open_internal_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200559 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
561 PSA_KEY_LOCATION_LOCAL_STORAGE) {
562 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200563 ++stats->external_slots;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100565 stats->max_open_external_key_id = id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200567 }
568 }
569}
570
Gilles Peskine961849f2018-11-30 18:54:54 +0100571#endif /* MBEDTLS_PSA_CRYPTO_C */