blob: bdb45eed8e5bf254e950d0ce594e692736cbab35 [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
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010026#include "psa/crypto.h"
27
Gilles Peskine66fb1262018-12-10 16:29:04 +010028#include "psa_crypto_core.h"
Steven Cooremane3842522021-03-18 18:52:44 +010029#include "psa_crypto_driver_wrappers.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010030#include "psa_crypto_slot_management.h"
31#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
33#include "psa_crypto_se.h"
34#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035
36#include <stdlib.h>
37#include <string.h>
38#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#define mbedtls_calloc calloc
42#define mbedtls_free free
43#endif
44
45#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
46
Gilles Peskine66fb1262018-12-10 16:29:04 +010047typedef struct
48{
Steven Cooreman863470a2021-02-15 14:03:19 +010049 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskine66fb1262018-12-10 16:29:04 +010050 unsigned key_slots_initialized : 1;
51} psa_global_data_t;
52
Gilles Peskine2e14bd32018-12-12 14:05:08 +010053static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010054
Ronald Cron77e412c2021-03-31 17:36:31 +020055int psa_is_valid_key_id( mbedtls_svc_key_id_t key, int vendor_ok )
Ronald Crond2ed4812020-07-17 16:11:30 +020056{
57 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
58
59 if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
60 ( key_id <= PSA_KEY_ID_USER_MAX ) )
Ronald Cron77e412c2021-03-31 17:36:31 +020061 return( 1 );
Ronald Crond2ed4812020-07-17 16:11:30 +020062
63 if( vendor_ok &&
64 ( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
Ronald Croncbd7bea2020-11-11 14:57:44 +010065 ( key_id <= PSA_KEY_ID_VENDOR_MAX ) )
Ronald Cron77e412c2021-03-31 17:36:31 +020066 return( 1 );
Ronald Crond2ed4812020-07-17 16:11:30 +020067
Ronald Cron77e412c2021-03-31 17:36:31 +020068 return( 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +020069}
70
Ronald Cron5c522922020-11-14 16:35:34 +010071/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020072 *
Ronald Cron5c522922020-11-14 16:35:34 +010073 * The descriptions of volatile keys and loaded persistent keys are
74 * stored in key slots. This function returns a pointer to the key slot
75 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020076 *
Ronald Cron5c522922020-11-14 16:35:34 +010077 * The function searches the key slots containing the description of the key
78 * with \p key identifier. The function does only read accesses to the key
79 * slots. The function does not load any persistent key thus does not access
80 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020081 *
Ronald Cron5c522922020-11-14 16:35:34 +010082 * For volatile key identifiers, only one key slot is queried as a volatile
83 * key with identifier key_id can only be stored in slot of index
84 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020085 *
Ronald Cron5c522922020-11-14 16:35:34 +010086 * On success, the function locks the key slot. It is the responsibility of
87 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020088 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020089 * \param key Key identifier to query.
90 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
91 * key slot containing the description of the key
92 * identified by \p key.
93 *
Ronald Cron96783552020-10-19 12:06:30 +020094 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020095 * The pointer to the key slot containing the description of the key
96 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020097 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020098 * \p key is not a valid key identifier.
99 * \retval #PSA_ERROR_DOES_NOT_EXIST
100 * There is no key with key identifier \p key in the key slots.
101 */
Ronald Cron5c522922020-11-14 16:35:34 +0100102static psa_status_t psa_get_and_lock_key_slot_in_memory(
Ronald Cron97c8ad52020-10-15 11:17:11 +0200103 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100104{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100105 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200106 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
Ronald Cronf473d8b2020-11-12 10:07:21 +0100107 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200108 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100109
Ronald Cron97c8ad52020-10-15 11:17:11 +0200110 if( psa_key_id_is_volatile( key_id ) )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200111 {
Ronald Cron97c8ad52020-10-15 11:17:11 +0200112 slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
Ronald Cron1d12d872020-11-18 17:21:22 +0100113
114 /*
115 * Check if both the PSA key identifier key_id and the owner
116 * identifier of key match those of the key slot.
117 *
118 * Note that, if the key slot is not occupied, its PSA key identifier
119 * is equal to zero. This is an invalid value for a PSA key identifier
120 * and thus cannot be equal to the valid PSA key identifier key_id.
121 */
Ronald Cronf473d8b2020-11-12 10:07:21 +0100122 status = mbedtls_svc_key_id_equal( key, slot->attr.id ) ?
123 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200124 }
125 else
126 {
Ronald Cron77e412c2021-03-31 17:36:31 +0200127 if ( !psa_is_valid_key_id( key, 1 ) )
128 return( PSA_ERROR_INVALID_HANDLE );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200129
Steven Cooreman863470a2021-02-15 14:03:19 +0100130 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200131 {
Ronald Cronf473d8b2020-11-12 10:07:21 +0100132 slot = &global_data.key_slots[ slot_idx ];
Ronald Cron97c8ad52020-10-15 11:17:11 +0200133 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200134 break;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200135 }
Steven Cooreman863470a2021-02-15 14:03:19 +0100136 status = ( slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT ) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100137 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200138 }
139
Ronald Cron97c8ad52020-10-15 11:17:11 +0200140 if( status == PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200141 {
Ronald Cron5c522922020-11-14 16:35:34 +0100142 status = psa_lock_key_slot( slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100143 if( status == PSA_SUCCESS )
144 *p_slot = slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200145 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200146
147 return( status );
Ronald Cronc4d1b512020-07-31 11:26:37 +0200148}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200149
Gilles Peskine66fb1262018-12-10 16:29:04 +0100150psa_status_t psa_initialize_key_slots( void )
151{
152 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
153 * guarantee that the key slots are initialized to all-zero, which
154 * means that all the key slots are in a valid, empty state. */
155 global_data.key_slots_initialized = 1;
156 return( PSA_SUCCESS );
157}
158
159void psa_wipe_all_key_slots( void )
160{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200161 size_t slot_idx;
162
Steven Cooreman863470a2021-02-15 14:03:19 +0100163 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100164 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200165 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron5c522922020-11-14 16:35:34 +0100166 slot->lock_count = 1;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100167 (void) psa_wipe_key_slot( slot );
168 }
169 global_data.key_slots_initialized = 0;
170}
171
Ronald Cronc4d1b512020-07-31 11:26:37 +0200172psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Ronald Cron2a993152020-07-17 14:13:26 +0200173 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100174{
Ronald Crona5b894f2020-10-21 09:04:34 +0200175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200176 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100177 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200178
Gilles Peskine267c6562019-05-27 19:01:54 +0200179 if( ! global_data.key_slots_initialized )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100180 {
Ronald Crona5b894f2020-10-21 09:04:34 +0200181 status = PSA_ERROR_BAD_STATE;
182 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100183 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200184
Ronald Cron5c522922020-11-14 16:35:34 +0100185 selected_slot = unlocked_persistent_key_slot = NULL;
Steven Cooreman863470a2021-02-15 14:03:19 +0100186 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Crona5b894f2020-10-21 09:04:34 +0200187 {
188 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
189 if( ! psa_is_key_slot_occupied( slot ) )
190 {
191 selected_slot = slot;
192 break;
193 }
194
Ronald Cron5c522922020-11-14 16:35:34 +0100195 if( ( unlocked_persistent_key_slot == NULL ) &&
Ronald Crona5b894f2020-10-21 09:04:34 +0200196 ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100197 ( ! psa_is_key_slot_locked( slot ) ) )
198 unlocked_persistent_key_slot = slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200199 }
200
201 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100202 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100203 * slot containing the description of a persistent key, recycle the first
204 * such key slot we encountered. If we later need to operate on the
205 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100206 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200207 */
208 if( ( selected_slot == NULL ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100209 ( unlocked_persistent_key_slot != NULL ) )
Ronald Crona5b894f2020-10-21 09:04:34 +0200210 {
Ronald Cron5c522922020-11-14 16:35:34 +0100211 selected_slot = unlocked_persistent_key_slot;
212 selected_slot->lock_count = 1;
Ronald Crona5b894f2020-10-21 09:04:34 +0200213 psa_wipe_key_slot( selected_slot );
214 }
215
216 if( selected_slot != NULL )
217 {
Ronald Cron5c522922020-11-14 16:35:34 +0100218 status = psa_lock_key_slot( selected_slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100219 if( status != PSA_SUCCESS )
220 goto error;
221
Ronald Crona5b894f2020-10-21 09:04:34 +0200222 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
223 ( (psa_key_id_t)( selected_slot - global_data.key_slots ) );
224 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200225
226 return( PSA_SUCCESS );
227 }
228 status = PSA_ERROR_INSUFFICIENT_MEMORY;
229
230error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200231 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200232 *volatile_key_id = 0;
233
234 return( status );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100235}
236
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100237#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200238static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100239{
240 psa_status_t status = PSA_SUCCESS;
241 uint8_t *key_data = NULL;
242 size_t key_data_length = 0;
243
Gilles Peskine24318592019-07-30 20:30:51 +0200244 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200245 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100246 if( status != PSA_SUCCESS )
247 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200248
Steven Cooreman98435dd2021-01-08 19:19:40 +0100249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100250 /* Special handling is required for loading keys associated with a
251 * dynamically registered SE interface. */
252 const psa_drv_se_t *drv;
253 psa_drv_se_context_t *drv_context;
254 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200255 {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100256 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100257
Steven Cooremanac3434f2021-01-15 17:36:02 +0100258 if( key_data_length != sizeof( *data ) )
259 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100260 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100261 goto exit;
262 }
263 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100264 status = psa_copy_key_material_into_slot(
265 slot, data->slot_number, sizeof( data->slot_number ) );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100266 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200267 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100268#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
269
Steven Cooremanac3434f2021-01-15 17:36:02 +0100270 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200271
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100272exit:
273 psa_free_persistent_key_data( key_data, key_data_length );
274 return( status );
275}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200276#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
277
Steven Cooreman6801f082021-02-19 17:21:22 +0100278#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100279
280static psa_status_t psa_load_builtin_key_into_slot( psa_key_slot_t *slot )
281{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100284 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100285 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100286 size_t key_buffer_size = 0;
287 size_t key_buffer_length = 0;
288
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100289 if( ! psa_key_id_is_builtin(
290 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id ) ) )
Steven Cooreman6801f082021-02-19 17:21:22 +0100291 {
Steven Cooreman6801f082021-02-19 17:21:22 +0100292 return( PSA_ERROR_DOES_NOT_EXIST );
293 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100294
295 /* Check the platform function to see whether this key actually exists */
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100296 psa_set_key_id( &attributes, slot->attr.id );
Steven Cooremanc8b95342021-03-18 20:48:06 +0100297 status = mbedtls_psa_platform_get_builtin_key(
298 slot->attr.id, &lifetime, &slot_number );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100299 if( status != PSA_SUCCESS )
300 return( status );
301
Steven Cooremanc8b95342021-03-18 20:48:06 +0100302 /* Set mapped lifetime on the attributes */
303 psa_set_key_lifetime( &attributes, lifetime );
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. */
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100307 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
Steven Cooreman85d554a2021-03-18 17:19:30 +0100308 &key_buffer_size );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100309 if( status != PSA_SUCCESS )
310 return( status );
311
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200312 /* Allocate a buffer of the required size and load the builtin key directly
313 * into the slot buffer. */
314 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
315 if( status != PSA_SUCCESS )
316 return( status );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100317
318 status = psa_driver_wrapper_get_builtin_key(
319 slot_number, &attributes,
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200320 slot->key.data, slot->key.bytes, &key_buffer_length );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100321 if( status != PSA_SUCCESS )
322 goto exit;
323
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200324 /* Copy actual key length and core attributes into the slot on success */
325 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100326 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100327
328exit:
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200329 if( status != PSA_SUCCESS )
330 psa_wipe_key_slot( slot );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100331 return( status );
Steven Cooreman6801f082021-02-19 17:21:22 +0100332}
333#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
334
Ronald Cron5c522922020-11-14 16:35:34 +0100335psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
336 psa_key_slot_t **p_slot )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200337{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200339
340 *p_slot = NULL;
341 if( ! global_data.key_slots_initialized )
342 return( PSA_ERROR_BAD_STATE );
343
Ronald Cronf95a2b12020-10-22 15:24:49 +0200344 /*
345 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100346 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200347 */
Ronald Cron5c522922020-11-14 16:35:34 +0100348 status = psa_get_and_lock_key_slot_in_memory( key, p_slot );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200349 if( status != PSA_ERROR_DOES_NOT_EXIST )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200350 return( status );
351
Steven Cooreman0bb65362021-04-06 15:09:57 +0200352 /* Loading keys from storage requires support for such a mechanism */
353#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
354 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200355 psa_key_id_t volatile_key_id;
356
Ronald Cronc4d1b512020-07-31 11:26:37 +0200357 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
358 if( status != PSA_SUCCESS )
359 return( status );
360
Ronald Cronc4d1b512020-07-31 11:26:37 +0200361 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100362 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200363
Steven Cooreman6801f082021-02-19 17:21:22 +0100364 status = PSA_ERROR_DOES_NOT_EXIST;
365#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200366 /* Load keys in the 'builtin' range through their own interface */
Steven Cooreman6801f082021-02-19 17:21:22 +0100367 status = psa_load_builtin_key_into_slot( *p_slot );
Steven Cooreman6801f082021-02-19 17:21:22 +0100368#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
369
370#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremane5e308592021-02-22 14:40:04 +0100371 if( status == PSA_ERROR_DOES_NOT_EXIST )
372 status = psa_load_persistent_key_into_slot( *p_slot );
Steven Cooreman6801f082021-02-19 17:21:22 +0100373#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
374
Ronald Cronc4d1b512020-07-31 11:26:37 +0200375 if( status != PSA_SUCCESS )
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000376 {
Ronald Cronc4d1b512020-07-31 11:26:37 +0200377 psa_wipe_key_slot( *p_slot );
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000378 if( status == PSA_ERROR_DOES_NOT_EXIST )
379 status = PSA_ERROR_INVALID_HANDLE;
380 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200381 return( status );
Steven Cooreman0bb65362021-04-06 15:09:57 +0200382#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
383 return( PSA_ERROR_INVALID_HANDLE );
384#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200385}
386
Ronald Cron5c522922020-11-14 16:35:34 +0100387psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200388{
389 if( slot == NULL )
390 return( PSA_SUCCESS );
391
Ronald Cron5c522922020-11-14 16:35:34 +0100392 if( slot->lock_count > 0 )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200393 {
Ronald Cron5c522922020-11-14 16:35:34 +0100394 slot->lock_count--;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200395 return( PSA_SUCCESS );
396 }
397
398 /*
399 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100400 * do our best to report if the lock counter is equal to zero: if
Ronald Cronf95a2b12020-10-22 15:24:49 +0200401 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
402 * called as part of the execution of a unit test suite this will stop the
Ronald Cron4640c152020-11-13 10:11:01 +0100403 * test suite execution).
Ronald Cronf95a2b12020-10-22 15:24:49 +0200404 */
405#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100406 MBEDTLS_PARAM_FAILED( slot->lock_count > 0 );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200407#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200408
409 return( PSA_ERROR_CORRUPTION_DETECTED );
410}
411
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200412psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200413 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200414{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200415 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200416 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100418 /* Check whether a driver is registered against this lifetime */
Steven Cooreman00106a12020-06-08 18:54:23 +0200419 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100420 if( driver != NULL )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200421 {
422 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200423 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200424 return( PSA_SUCCESS );
425 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100426#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200427 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100428#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
429
Steven Cooreman98435dd2021-01-08 19:19:40 +0100430#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
431 /* Key location for external keys gets checked by the wrapper */
432 return( PSA_SUCCESS );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100433#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
434 /* No support for external lifetimes at all, or dynamic interface
435 * did not find driver for requested lifetime. */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200436 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100437#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine011e4282019-06-26 18:34:38 +0200438 }
439 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200440 /* Local/internal keys are always valid */
441 return( PSA_SUCCESS );
442}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200443
Ronald Crond2ed4812020-07-17 16:11:30 +0200444psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200445{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200446 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
447 {
448 /* Volatile keys are always supported */
449 return( PSA_SUCCESS );
450 }
451 else
452 {
453 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200454#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond2ed4812020-07-17 16:11:30 +0200455 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200456#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200457 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200458#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200459 }
Gilles Peskined167b942019-04-19 18:19:40 +0200460}
461
Ronald Cron71016a92020-08-28 19:01:50 +0200462psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100463{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200464#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100465 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200466 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100467
Ronald Cron5c522922020-11-14 16:35:34 +0100468 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200469 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100470 {
Ronald Cron91e95152020-07-30 17:48:03 +0200471 *handle = PSA_KEY_HANDLE_INIT;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000472 if( status == PSA_ERROR_INVALID_HANDLE )
473 status = PSA_ERROR_DOES_NOT_EXIST;
474
Ronald Cronc4d1b512020-07-31 11:26:37 +0200475 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100476 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200477
478 *handle = key;
479
Ronald Cron5c522922020-11-14 16:35:34 +0100480 return( psa_unlock_key_slot( slot ) );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200481
Gilles Peskine30afafd2019-04-25 13:47:40 +0200482#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Ronald Cron27238fc2020-07-23 12:30:41 +0200483 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200484 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200485 return( PSA_ERROR_NOT_SUPPORTED );
486#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100487}
488
Gilles Peskine961849f2018-11-30 18:54:54 +0100489psa_status_t psa_close_key( psa_key_handle_t handle )
490{
Gilles Peskine267c6562019-05-27 19:01:54 +0200491 psa_status_t status;
492 psa_key_slot_t *slot;
493
Ronald Cronc26f8d42020-09-01 10:51:51 +0200494 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200495 return( PSA_SUCCESS );
496
Ronald Cron5c522922020-11-14 16:35:34 +0100497 status = psa_get_and_lock_key_slot_in_memory( handle, &slot );
Gilles Peskine267c6562019-05-27 19:01:54 +0200498 if( status != PSA_SUCCESS )
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000499 {
500 if( status == PSA_ERROR_DOES_NOT_EXIST )
501 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine267c6562019-05-27 19:01:54 +0200502
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000503 return( status );
504 }
Ronald Cron5c522922020-11-14 16:35:34 +0100505 if( slot->lock_count <= 1 )
Ronald Cronf2911112020-10-29 17:51:10 +0100506 return( psa_wipe_key_slot( slot ) );
507 else
Ronald Cron5c522922020-11-14 16:35:34 +0100508 return( psa_unlock_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100509}
510
Ronald Cron277a85f2020-08-04 15:49:48 +0200511psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
512{
513 psa_status_t status;
514 psa_key_slot_t *slot;
515
Ronald Cron5c522922020-11-14 16:35:34 +0100516 status = psa_get_and_lock_key_slot_in_memory( key, &slot );
Ronald Cron277a85f2020-08-04 15:49:48 +0200517 if( status != PSA_SUCCESS )
518 return( status );
519
Ronald Cronf2911112020-10-29 17:51:10 +0100520 if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100521 ( slot->lock_count <= 1 ) )
Ronald Cronf2911112020-10-29 17:51:10 +0100522 return( psa_wipe_key_slot( slot ) );
523 else
Ronald Cron5c522922020-11-14 16:35:34 +0100524 return( psa_unlock_key_slot( slot ) );
Ronald Cron277a85f2020-08-04 15:49:48 +0200525}
526
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200527void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
528{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200529 size_t slot_idx;
530
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200531 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200532
Steven Cooreman863470a2021-02-15 14:03:19 +0100533 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200534 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200535 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100536 if( psa_is_key_slot_locked( slot ) )
Ronald Cron0c3752a2020-10-30 11:54:03 +0100537 {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100538 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100539 }
Gilles Peskine41e50d22019-07-31 15:01:55 +0200540 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200541 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200542 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200543 continue;
544 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200545 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200546 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200547 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200548 {
Ronald Cron71016a92020-08-28 19:01:50 +0200549 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200550 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100551 if( id > stats->max_open_internal_key_id )
552 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200553 }
554 else
555 {
Ronald Cron71016a92020-08-28 19:01:50 +0200556 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200557 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100558 if( id > stats->max_open_external_key_id )
559 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200560 }
561 }
562}
563
Gilles Peskine961849f2018-11-30 18:54:54 +0100564#endif /* MBEDTLS_PSA_CRYPTO_C */