blob: 2d2790208579b1e89aad5d61e619fe6115614829 [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 Peskine1b6c09a2023-01-11 14:52:35 +010039#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine961849f2018-11-30 18:54:54 +010040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041typedef struct {
Steven Cooreman863470a2021-02-15 14:03:19 +010042 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskine66fb1262018-12-10 16:29:04 +010043 unsigned key_slots_initialized : 1;
44} psa_global_data_t;
45
Gilles Peskine2e14bd32018-12-12 14:05:08 +010046static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Ronald Crond2ed4812020-07-17 16:11:30 +020049{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Crond2ed4812020-07-17 16:11:30 +020051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
53 (key_id <= PSA_KEY_ID_USER_MAX)) {
54 return 1;
55 }
Ronald Crond2ed4812020-07-17 16:11:30 +020056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 if (vendor_ok &&
58 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
59 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
60 return 1;
61 }
Ronald Crond2ed4812020-07-17 16:11:30 +020062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 return 0;
Ronald Crond2ed4812020-07-17 16:11:30 +020064}
65
Ronald Cron5c522922020-11-14 16:35:34 +010066/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020067 *
Ronald Cron5c522922020-11-14 16:35:34 +010068 * The descriptions of volatile keys and loaded persistent keys are
69 * stored in key slots. This function returns a pointer to the key slot
70 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020071 *
Ronald Cron5c522922020-11-14 16:35:34 +010072 * The function searches the key slots containing the description of the key
73 * with \p key identifier. The function does only read accesses to the key
74 * slots. The function does not load any persistent key thus does not access
75 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020076 *
Ronald Cron5c522922020-11-14 16:35:34 +010077 * For volatile key identifiers, only one key slot is queried as a volatile
78 * key with identifier key_id can only be stored in slot of index
79 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020080 *
Ronald Cron5c522922020-11-14 16:35:34 +010081 * On success, the function locks the key slot. It is the responsibility of
82 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020083 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020084 * \param key Key identifier to query.
85 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
86 * key slot containing the description of the key
87 * identified by \p key.
88 *
Ronald Cron96783552020-10-19 12:06:30 +020089 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020090 * The pointer to the key slot containing the description of the key
91 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020092 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020093 * \p key is not a valid key identifier.
94 * \retval #PSA_ERROR_DOES_NOT_EXIST
95 * There is no key with key identifier \p key in the key slots.
96 */
Ronald Cron5c522922020-11-14 16:35:34 +010097static psa_status_t psa_get_and_lock_key_slot_in_memory(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +010099{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Ronald Cronf473d8b2020-11-12 10:07:21 +0100102 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200103 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if (psa_key_id_is_volatile(key_id)) {
106 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
Ronald Cron1d12d872020-11-18 17:21:22 +0100107
108 /*
109 * Check if both the PSA key identifier key_id and the owner
110 * identifier of key match those of the key slot.
111 *
112 * Note that, if the key slot is not occupied, its PSA key identifier
113 * is equal to zero. This is an invalid value for a PSA key identifier
114 * and thus cannot be equal to the valid PSA key identifier key_id.
115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100117 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 } else {
119 if (!psa_is_valid_key_id(key, 1)) {
120 return PSA_ERROR_INVALID_HANDLE;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200121 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122
123 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
124 slot = &global_data.key_slots[slot_idx];
125 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
126 break;
127 }
128 }
129 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100130 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200131 }
132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if (status == PSA_SUCCESS) {
134 status = psa_lock_key_slot(slot);
135 if (status == PSA_SUCCESS) {
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100136 *p_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200138 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return status;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200141}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143psa_status_t psa_initialize_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100144{
145 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
146 * guarantee that the key slots are initialized to all-zero, which
147 * means that all the key slots are in a valid, empty state. */
148 global_data.key_slots_initialized = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 return PSA_SUCCESS;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100150}
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152void psa_wipe_all_key_slots(void)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100153{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200154 size_t slot_idx;
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
157 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
Ronald Cron5c522922020-11-14 16:35:34 +0100158 slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 (void) psa_wipe_key_slot(slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100160 }
161 global_data.key_slots_initialized = 0;
162}
163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
165 psa_key_slot_t **p_slot)
Gilles Peskine66fb1262018-12-10 16:29:04 +0100166{
Ronald Crona5b894f2020-10-21 09:04:34 +0200167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200168 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100169 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 if (!global_data.key_slots_initialized) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200172 status = PSA_ERROR_BAD_STATE;
173 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100174 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200175
Ronald Cron5c522922020-11-14 16:35:34 +0100176 selected_slot = unlocked_persistent_key_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
178 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
179 if (!psa_is_key_slot_occupied(slot)) {
Ronald Crona5b894f2020-10-21 09:04:34 +0200180 selected_slot = slot;
181 break;
182 }
183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if ((unlocked_persistent_key_slot == NULL) &&
185 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
186 (!psa_is_key_slot_locked(slot))) {
Ronald Cron5c522922020-11-14 16:35:34 +0100187 unlocked_persistent_key_slot = slot;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 }
Ronald Crona5b894f2020-10-21 09:04:34 +0200189 }
190
191 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100192 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100193 * slot containing the description of a persistent key, recycle the first
194 * such key slot we encountered. If we later need to operate on the
195 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100196 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200197 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 if ((selected_slot == NULL) &&
199 (unlocked_persistent_key_slot != NULL)) {
Ronald Cron5c522922020-11-14 16:35:34 +0100200 selected_slot = unlocked_persistent_key_slot;
201 selected_slot->lock_count = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 psa_wipe_key_slot(selected_slot);
Ronald Crona5b894f2020-10-21 09:04:34 +0200203 }
204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (selected_slot != NULL) {
206 status = psa_lock_key_slot(selected_slot);
207 if (status != PSA_SUCCESS) {
208 goto error;
209 }
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100210
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 ((psa_key_id_t) (selected_slot - global_data.key_slots));
Ronald Crona5b894f2020-10-21 09:04:34 +0200213 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 return PSA_SUCCESS;
Ronald Crona5b894f2020-10-21 09:04:34 +0200216 }
217 status = PSA_ERROR_INSUFFICIENT_MEMORY;
218
219error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200220 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200221 *volatile_key_id = 0;
222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return status;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100224}
225
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100226#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100228{
229 psa_status_t status = PSA_SUCCESS;
230 uint8_t *key_data = NULL;
231 size_t key_data_length = 0;
232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 status = psa_load_persistent_key(&slot->attr,
234 &key_data, &key_data_length);
235 if (status != PSA_SUCCESS) {
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100236 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200238
Steven Cooreman98435dd2021-01-08 19:19:40 +0100239#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100240 /* Special handling is required for loading keys associated with a
241 * dynamically registered SE interface. */
242 const psa_drv_se_t *drv;
243 psa_drv_se_context_t *drv_context;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100245 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if (key_data_length != sizeof(*data)) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100248 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100249 goto exit;
250 }
251 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100252 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 slot, data->slot_number, sizeof(data->slot_number));
Steven Cooremanac3434f2021-01-15 17:36:02 +0100254 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200255 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100256#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
Gilles Peskine1df83d42019-07-23 16:13:14 +0200259
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100260exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 psa_free_persistent_key_data(key_data, key_data_length);
262 return status;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100263}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200264#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
265
Steven Cooreman6801f082021-02-19 17:21:22 +0100266#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
Steven Cooreman6801f082021-02-19 17:21:22 +0100269{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100272 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100273 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100274 size_t key_buffer_size = 0;
275 size_t key_buffer_length = 0;
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (!psa_key_id_is_builtin(
278 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
279 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100280 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100281
282 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100283 status = mbedtls_psa_platform_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 slot->attr.id, &lifetime, &slot_number);
285 if (status != PSA_SUCCESS) {
286 return status;
287 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100288
Steven Cooreman966db262021-04-13 13:45:45 +0200289 /* Set required key attributes to ensure get_builtin_key can retrieve the
290 * full attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 psa_set_key_id(&attributes, slot->attr.id);
292 psa_set_key_lifetime(&attributes, lifetime);
Steven Cooremanc8b95342021-03-18 20:48:06 +0100293
Steven Cooremance487022021-04-07 18:09:53 +0200294 /* Get the full key attributes from the driver in order to be able to
295 * calculate the required buffer size. */
296 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 slot_number, &attributes,
298 NULL, 0, NULL);
299 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
Steven Cooremance487022021-04-07 18:09:53 +0200300 /* Builtin keys cannot be defined by the attributes alone */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 if (status == PSA_SUCCESS) {
Steven Cooremance487022021-04-07 18:09:53 +0200302 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 }
304 return status;
Steven Cooremance487022021-04-07 18:09:53 +0200305 }
306
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200307 /* If the key should exist according to the platform, then ask the driver
308 * what its expected size is. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
310 &key_buffer_size);
311 if (status != PSA_SUCCESS) {
312 return status;
313 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100314
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200315 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200316 * into the (now properly sized) slot buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
318 if (status != PSA_SUCCESS) {
319 return status;
320 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100321
322 status = psa_driver_wrapper_get_builtin_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 slot_number, &attributes,
324 slot->key.data, slot->key.bytes, &key_buffer_length);
325 if (status != PSA_SUCCESS) {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100326 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100328
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200329 /* Copy actual key length and core attributes into the slot on success */
330 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100331 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100332
333exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 if (status != PSA_SUCCESS) {
335 psa_remove_key_data_from_memory(slot);
336 }
337 return status;
Steven Cooreman6801f082021-02-19 17:21:22 +0100338}
339#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
342 psa_key_slot_t **p_slot)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200343{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200345
346 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 if (!global_data.key_slots_initialized) {
348 return PSA_ERROR_BAD_STATE;
349 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200350
Ronald Cronf95a2b12020-10-22 15:24:49 +0200351 /*
352 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100353 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200354 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
356 if (status != PSA_ERROR_DOES_NOT_EXIST) {
357 return status;
358 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200359
Steven Cooreman0bb65362021-04-06 15:09:57 +0200360 /* Loading keys from storage requires support for such a mechanism */
361#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
362 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200363 psa_key_id_t volatile_key_id;
364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
366 if (status != PSA_SUCCESS) {
367 return status;
368 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200369
Ronald Cronc4d1b512020-07-31 11:26:37 +0200370 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100371 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200372
Steven Cooreman6801f082021-02-19 17:21:22 +0100373 status = PSA_ERROR_DOES_NOT_EXIST;
374#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200375 /* Load keys in the 'builtin' range through their own interface */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 status = psa_load_builtin_key_into_slot(*p_slot);
Steven Cooreman6801f082021-02-19 17:21:22 +0100377#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
378
379#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (status == PSA_ERROR_DOES_NOT_EXIST) {
381 status = psa_load_persistent_key_into_slot(*p_slot);
382 }
Steven Cooreman6801f082021-02-19 17:21:22 +0100383#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 if (status != PSA_SUCCESS) {
386 psa_wipe_key_slot(*p_slot);
387 if (status == PSA_ERROR_DOES_NOT_EXIST) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000388 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 }
390 } else {
gabor-mezei-arm6c185412021-06-28 14:59:52 +0200391 /* Add implicit usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
393 }
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return status;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200396#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_ERROR_INVALID_HANDLE;
Steven Cooreman0bb65362021-04-06 15:09:57 +0200398#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200399}
400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200402{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 if (slot == NULL) {
404 return PSA_SUCCESS;
405 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 if (slot->lock_count > 0) {
Ronald Cron5c522922020-11-14 16:35:34 +0100408 slot->lock_count--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200410 }
411
412 /*
413 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100414 * do our best to report if the lock counter is equal to zero: if
Ronald Cronf95a2b12020-10-22 15:24:49 +0200415 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
416 * called as part of the execution of a unit test suite this will stop the
Ronald Cron4640c152020-11-13 10:11:01 +0100417 * test suite execution).
Ronald Cronf95a2b12020-10-22 15:24:49 +0200418 */
419#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 MBEDTLS_PARAM_FAILED(slot->lock_count > 0);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200421#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200424}
425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
427 psa_se_drv_table_entry_t **p_drv)
Gilles Peskined167b942019-04-19 18:19:40 +0200428{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 if (psa_key_lifetime_is_external(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200430#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100431 /* Check whether a driver is registered against this lifetime */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
433 if (driver != NULL) {
434 if (p_drv != NULL) {
Steven Cooreman00106a12020-06-08 18:54:23 +0200435 *p_drv = driver;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 }
437 return PSA_SUCCESS;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200438 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100439#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200440 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
442
Steven Cooreman98435dd2021-01-08 19:19:40 +0100443#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
444 /* Key location for external keys gets checked by the wrapper */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 return PSA_SUCCESS;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100446#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
447 /* No support for external lifetimes at all, or dynamic interface
448 * did not find driver for requested lifetime. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100450#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200452 /* Local/internal keys are always valid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 return PSA_SUCCESS;
454 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200455}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200458{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200460 /* Volatile keys are always supported */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return PSA_SUCCESS;
462 } else {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200463 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200464#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
466 return PSA_ERROR_INVALID_ARGUMENT;
467 } else {
468 return PSA_SUCCESS;
469 }
Gilles Peskine30afafd2019-04-25 13:47:40 +0200470#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200472#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200473 }
Gilles Peskined167b942019-04-19 18:19:40 +0200474}
475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100477{
Archana6d342f32021-07-14 13:59:48 +0530478#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
479 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100480 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200481 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 status = psa_get_and_lock_key_slot(key, &slot);
484 if (status != PSA_SUCCESS) {
Ronald Cron91e95152020-07-30 17:48:03 +0200485 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 if (status == PSA_ERROR_INVALID_HANDLE) {
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000487 status = PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 }
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 return status;
Gilles Peskine961849f2018-11-30 18:54:54 +0100491 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200492
493 *handle = key;
494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 return psa_unlock_key_slot(slot);
Gilles Peskine70e085a2019-05-27 19:04:07 +0200496
Archana6d342f32021-07-14 13:59:48 +0530497#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200498 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200499 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 return PSA_ERROR_NOT_SUPPORTED;
Archana6d342f32021-07-14 13:59:48 +0530501#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100502}
503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504psa_status_t psa_close_key(psa_key_handle_t handle)
Gilles Peskine961849f2018-11-30 18:54:54 +0100505{
Gilles Peskine267c6562019-05-27 19:01:54 +0200506 psa_status_t status;
507 psa_key_slot_t *slot;
508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 if (psa_key_handle_is_null(handle)) {
510 return PSA_SUCCESS;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000511 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512
513 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
514 if (status != PSA_SUCCESS) {
515 if (status == PSA_ERROR_DOES_NOT_EXIST) {
516 status = PSA_ERROR_INVALID_HANDLE;
517 }
518
519 return status;
520 }
521 if (slot->lock_count <= 1) {
522 return psa_wipe_key_slot(slot);
523 } else {
524 return psa_unlock_key_slot(slot);
525 }
Gilles Peskine961849f2018-11-30 18:54:54 +0100526}
527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
Ronald Cron277a85f2020-08-04 15:49:48 +0200529{
530 psa_status_t status;
531 psa_key_slot_t *slot;
532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
534 if (status != PSA_SUCCESS) {
535 return status;
536 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
539 (slot->lock_count <= 1)) {
540 return psa_wipe_key_slot(slot);
541 } else {
542 return psa_unlock_key_slot(slot);
543 }
Ronald Cron277a85f2020-08-04 15:49:48 +0200544}
545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200547{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200548 size_t slot_idx;
549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 memset(stats, 0, sizeof(*stats));
Ronald Cron98a54dd2020-07-24 16:33:11 +0200551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
553 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
554 if (psa_is_key_slot_locked(slot)) {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100555 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100556 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 if (!psa_is_key_slot_occupied(slot)) {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200558 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200559 continue;
560 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200562 ++stats->volatile_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 } else {
564 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200565 ++stats->persistent_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 if (id > stats->max_open_internal_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100567 stats->max_open_internal_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200569 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
571 PSA_KEY_LOCATION_LOCAL_STORAGE) {
572 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200573 ++stats->external_slots;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 if (id > stats->max_open_external_key_id) {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100575 stats->max_open_external_key_id = id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 }
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200577 }
578 }
579}
580
Gilles Peskine961849f2018-11-30 18:54:54 +0100581#endif /* MBEDTLS_PSA_CRYPTO_C */