blob: 9dceaac6d4595d523e655e3197b709255c96aeff [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
39#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
40
Gilles Peskine66fb1262018-12-10 16:29:04 +010041typedef struct
42{
Steven Cooreman863470a2021-02-15 14:03:19 +010043 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskine66fb1262018-12-10 16:29:04 +010044 unsigned key_slots_initialized : 1;
45} psa_global_data_t;
46
Gilles Peskine2e14bd32018-12-12 14:05:08 +010047static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010048
Ronald Cron77e412c2021-03-31 17:36:31 +020049int psa_is_valid_key_id( mbedtls_svc_key_id_t key, int vendor_ok )
Ronald Crond2ed4812020-07-17 16:11:30 +020050{
51 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
52
53 if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
54 ( key_id <= PSA_KEY_ID_USER_MAX ) )
Ronald Cron77e412c2021-03-31 17:36:31 +020055 return( 1 );
Ronald Crond2ed4812020-07-17 16:11:30 +020056
57 if( vendor_ok &&
58 ( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
Ronald Croncbd7bea2020-11-11 14:57:44 +010059 ( key_id <= PSA_KEY_ID_VENDOR_MAX ) )
Ronald Cron77e412c2021-03-31 17:36:31 +020060 return( 1 );
Ronald Crond2ed4812020-07-17 16:11:30 +020061
Ronald Cron77e412c2021-03-31 17:36:31 +020062 return( 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +020063}
64
Ronald Cron5c522922020-11-14 16:35:34 +010065/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020066 *
Ronald Cron5c522922020-11-14 16:35:34 +010067 * The descriptions of volatile keys and loaded persistent keys are
68 * stored in key slots. This function returns a pointer to the key slot
69 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020070 *
Ronald Cron5c522922020-11-14 16:35:34 +010071 * The function searches the key slots containing the description of the key
72 * with \p key identifier. The function does only read accesses to the key
73 * slots. The function does not load any persistent key thus does not access
74 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020075 *
Ronald Cron5c522922020-11-14 16:35:34 +010076 * For volatile key identifiers, only one key slot is queried as a volatile
77 * key with identifier key_id can only be stored in slot of index
78 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020079 *
Ronald Cron5c522922020-11-14 16:35:34 +010080 * On success, the function locks the key slot. It is the responsibility of
81 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020082 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020083 * \param key Key identifier to query.
84 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
85 * key slot containing the description of the key
86 * identified by \p key.
87 *
Ronald Cron96783552020-10-19 12:06:30 +020088 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020089 * The pointer to the key slot containing the description of the key
90 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020091 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020092 * \p key is not a valid key identifier.
93 * \retval #PSA_ERROR_DOES_NOT_EXIST
94 * There is no key with key identifier \p key in the key slots.
95 */
Ronald Cron5c522922020-11-14 16:35:34 +010096static psa_status_t psa_get_and_lock_key_slot_in_memory(
Ronald Cron97c8ad52020-10-15 11:17:11 +020097 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +010098{
Ronald Cronf473d8b2020-11-12 10:07:21 +010099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200100 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
Ronald Cronf473d8b2020-11-12 10:07:21 +0100101 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200102 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100103
Ronald Cron97c8ad52020-10-15 11:17:11 +0200104 if( psa_key_id_is_volatile( key_id ) )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200105 {
Ronald Cron97c8ad52020-10-15 11:17:11 +0200106 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 */
Ronald Cronf473d8b2020-11-12 10:07:21 +0100116 status = mbedtls_svc_key_id_equal( key, slot->attr.id ) ?
117 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200118 }
119 else
120 {
Ronald Cron77e412c2021-03-31 17:36:31 +0200121 if ( !psa_is_valid_key_id( key, 1 ) )
122 return( PSA_ERROR_INVALID_HANDLE );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200123
Steven Cooreman863470a2021-02-15 14:03:19 +0100124 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200125 {
Ronald Cronf473d8b2020-11-12 10:07:21 +0100126 slot = &global_data.key_slots[ slot_idx ];
Ronald Cron97c8ad52020-10-15 11:17:11 +0200127 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200128 break;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200129 }
Steven Cooreman863470a2021-02-15 14:03:19 +0100130 status = ( slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT ) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100131 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200132 }
133
Ronald Cron97c8ad52020-10-15 11:17:11 +0200134 if( status == PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200135 {
Ronald Cron5c522922020-11-14 16:35:34 +0100136 status = psa_lock_key_slot( slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100137 if( status == PSA_SUCCESS )
138 *p_slot = slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200139 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200140
141 return( status );
Ronald Cronc4d1b512020-07-31 11:26:37 +0200142}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200143
Gilles Peskine66fb1262018-12-10 16:29:04 +0100144psa_status_t psa_initialize_key_slots( void )
145{
146 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
147 * guarantee that the key slots are initialized to all-zero, which
148 * means that all the key slots are in a valid, empty state. */
149 global_data.key_slots_initialized = 1;
150 return( PSA_SUCCESS );
151}
152
153void psa_wipe_all_key_slots( void )
154{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200155 size_t slot_idx;
156
Steven Cooreman863470a2021-02-15 14:03:19 +0100157 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100158 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200159 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron5c522922020-11-14 16:35:34 +0100160 slot->lock_count = 1;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100161 (void) psa_wipe_key_slot( slot );
162 }
163 global_data.key_slots_initialized = 0;
164}
165
Ronald Cronc4d1b512020-07-31 11:26:37 +0200166psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Ronald Cron2a993152020-07-17 14:13:26 +0200167 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100168{
Ronald Crona5b894f2020-10-21 09:04:34 +0200169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200170 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100171 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200172
Gilles Peskine267c6562019-05-27 19:01:54 +0200173 if( ! global_data.key_slots_initialized )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100174 {
Ronald Crona5b894f2020-10-21 09:04:34 +0200175 status = PSA_ERROR_BAD_STATE;
176 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100177 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200178
Ronald Cron5c522922020-11-14 16:35:34 +0100179 selected_slot = unlocked_persistent_key_slot = NULL;
Steven Cooreman863470a2021-02-15 14:03:19 +0100180 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Crona5b894f2020-10-21 09:04:34 +0200181 {
182 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
183 if( ! psa_is_key_slot_occupied( slot ) )
184 {
185 selected_slot = slot;
186 break;
187 }
188
Ronald Cron5c522922020-11-14 16:35:34 +0100189 if( ( unlocked_persistent_key_slot == NULL ) &&
Ronald Crona5b894f2020-10-21 09:04:34 +0200190 ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100191 ( ! psa_is_key_slot_locked( slot ) ) )
192 unlocked_persistent_key_slot = slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200193 }
194
195 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100196 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100197 * slot containing the description of a persistent key, recycle the first
198 * such key slot we encountered. If we later need to operate on the
199 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100200 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200201 */
202 if( ( selected_slot == NULL ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100203 ( unlocked_persistent_key_slot != NULL ) )
Ronald Crona5b894f2020-10-21 09:04:34 +0200204 {
Ronald Cron5c522922020-11-14 16:35:34 +0100205 selected_slot = unlocked_persistent_key_slot;
206 selected_slot->lock_count = 1;
Ronald Crona5b894f2020-10-21 09:04:34 +0200207 psa_wipe_key_slot( selected_slot );
208 }
209
210 if( selected_slot != NULL )
211 {
Ronald Cron5c522922020-11-14 16:35:34 +0100212 status = psa_lock_key_slot( selected_slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100213 if( status != PSA_SUCCESS )
214 goto error;
215
Ronald Crona5b894f2020-10-21 09:04:34 +0200216 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
217 ( (psa_key_id_t)( selected_slot - global_data.key_slots ) );
218 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200219
220 return( PSA_SUCCESS );
221 }
222 status = PSA_ERROR_INSUFFICIENT_MEMORY;
223
224error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200225 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200226 *volatile_key_id = 0;
227
228 return( status );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100229}
230
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100231#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200232static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100233{
234 psa_status_t status = PSA_SUCCESS;
235 uint8_t *key_data = NULL;
236 size_t key_data_length = 0;
237
Gilles Peskine24318592019-07-30 20:30:51 +0200238 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200239 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100240 if( status != PSA_SUCCESS )
241 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200242
Steven Cooreman98435dd2021-01-08 19:19:40 +0100243#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100244 /* Special handling is required for loading keys associated with a
245 * dynamically registered SE interface. */
246 const psa_drv_se_t *drv;
247 psa_drv_se_context_t *drv_context;
248 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200249 {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100250 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100251
Steven Cooremanac3434f2021-01-15 17:36:02 +0100252 if( key_data_length != sizeof( *data ) )
253 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100254 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100255 goto exit;
256 }
257 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100258 status = psa_copy_key_material_into_slot(
259 slot, data->slot_number, sizeof( data->slot_number ) );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100260 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200261 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
263
Steven Cooremanac3434f2021-01-15 17:36:02 +0100264 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200265
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100266exit:
267 psa_free_persistent_key_data( key_data, key_data_length );
268 return( status );
269}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200270#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
271
Steven Cooreman6801f082021-02-19 17:21:22 +0100272#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100273
274static psa_status_t psa_load_builtin_key_into_slot( psa_key_slot_t *slot )
275{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100278 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100279 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100280 size_t key_buffer_size = 0;
281 size_t key_buffer_length = 0;
282
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100283 if( ! psa_key_id_is_builtin(
284 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id ) ) )
Steven Cooreman6801f082021-02-19 17:21:22 +0100285 {
Steven Cooreman6801f082021-02-19 17:21:22 +0100286 return( PSA_ERROR_DOES_NOT_EXIST );
287 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100288
289 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100290 status = mbedtls_psa_platform_get_builtin_key(
291 slot->attr.id, &lifetime, &slot_number );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100292 if( status != PSA_SUCCESS )
293 return( status );
294
Steven Cooreman966db262021-04-13 13:45:45 +0200295 /* Set required key attributes to ensure get_builtin_key can retrieve the
296 * full attributes. */
297 psa_set_key_id( &attributes, slot->attr.id );
Steven Cooremanc8b95342021-03-18 20:48:06 +0100298 psa_set_key_lifetime( &attributes, lifetime );
299
Steven Cooremance487022021-04-07 18:09:53 +0200300 /* Get the full key attributes from the driver in order to be able to
301 * calculate the required buffer size. */
302 status = psa_driver_wrapper_get_builtin_key(
303 slot_number, &attributes,
304 NULL, 0, NULL );
305 if( status != PSA_ERROR_BUFFER_TOO_SMALL )
306 {
307 /* Builtin keys cannot be defined by the attributes alone */
308 if( status == PSA_SUCCESS )
309 status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman966db262021-04-13 13:45:45 +0200310 return( status );
Steven Cooremance487022021-04-07 18:09:53 +0200311 }
312
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200313 /* If the key should exist according to the platform, then ask the driver
314 * what its expected size is. */
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100315 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
Steven Cooreman85d554a2021-03-18 17:19:30 +0100316 &key_buffer_size );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100317 if( status != PSA_SUCCESS )
318 return( status );
319
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200320 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200321 * into the (now properly sized) slot buffer. */
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200322 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
323 if( status != PSA_SUCCESS )
324 return( status );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100325
326 status = psa_driver_wrapper_get_builtin_key(
327 slot_number, &attributes,
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200328 slot->key.data, slot->key.bytes, &key_buffer_length );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100329 if( status != PSA_SUCCESS )
330 goto exit;
331
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200332 /* Copy actual key length and core attributes into the slot on success */
333 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100334 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100335
336exit:
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200337 if( status != PSA_SUCCESS )
Steven Cooreman7ddee7f2021-04-07 18:08:30 +0200338 psa_remove_key_data_from_memory( slot );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100339 return( status );
Steven Cooreman6801f082021-02-19 17:21:22 +0100340}
341#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
342
Ronald Cron5c522922020-11-14 16:35:34 +0100343psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
344 psa_key_slot_t **p_slot )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200345{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200347
348 *p_slot = NULL;
349 if( ! global_data.key_slots_initialized )
350 return( PSA_ERROR_BAD_STATE );
351
Ronald Cronf95a2b12020-10-22 15:24:49 +0200352 /*
353 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100354 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200355 */
Ronald Cron5c522922020-11-14 16:35:34 +0100356 status = psa_get_and_lock_key_slot_in_memory( key, p_slot );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200357 if( status != PSA_ERROR_DOES_NOT_EXIST )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200358 return( status );
359
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
Ronald Cronc4d1b512020-07-31 11:26:37 +0200365 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 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100370 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200371
Steven Cooreman6801f082021-02-19 17:21:22 +0100372 status = PSA_ERROR_DOES_NOT_EXIST;
373#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200374 /* Load keys in the 'builtin' range through their own interface */
Steven Cooreman6801f082021-02-19 17:21:22 +0100375 status = psa_load_builtin_key_into_slot( *p_slot );
Steven Cooreman6801f082021-02-19 17:21:22 +0100376#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
377
378#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremane5e308592021-02-22 14:40:04 +0100379 if( status == PSA_ERROR_DOES_NOT_EXIST )
380 status = psa_load_persistent_key_into_slot( *p_slot );
Steven Cooreman6801f082021-02-19 17:21:22 +0100381#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
382
Ronald Cronc4d1b512020-07-31 11:26:37 +0200383 if( status != PSA_SUCCESS )
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000384 {
Ronald Cronc4d1b512020-07-31 11:26:37 +0200385 psa_wipe_key_slot( *p_slot );
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000386 if( status == PSA_ERROR_DOES_NOT_EXIST )
387 status = PSA_ERROR_INVALID_HANDLE;
388 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200389 else
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200390 /* Add implicit usage flags. */
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200391 psa_extend_key_usage_flags( &(*p_slot)->attr.policy.usage );
392
Ronald Cronc4d1b512020-07-31 11:26:37 +0200393 return( status );
Steven Cooreman0bb65362021-04-06 15:09:57 +0200394#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
395 return( PSA_ERROR_INVALID_HANDLE );
396#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200397}
398
Ronald Cron5c522922020-11-14 16:35:34 +0100399psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200400{
401 if( slot == NULL )
402 return( PSA_SUCCESS );
403
Ronald Cron5c522922020-11-14 16:35:34 +0100404 if( slot->lock_count > 0 )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200405 {
Ronald Cron5c522922020-11-14 16:35:34 +0100406 slot->lock_count--;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200407 return( PSA_SUCCESS );
408 }
409
TRodziewicz18cddc02021-07-13 12:19:15 +0200410 /*
411 * As the return error code may not be handled in case of multiple errors,
TRodziewiczc9890e92021-07-14 10:16:26 +0200412 * 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.
TRodziewicz18cddc02021-07-13 12:19:15 +0200417 */
TRodziewicz7871c2e2021-07-07 17:29:43 +0200418 MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count > 0 );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200419 return( PSA_ERROR_CORRUPTION_DETECTED );
420}
421
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200422psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200423 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200424{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200425 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200426 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200427#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100428 /* Check whether a driver is registered against this lifetime */
Steven Cooreman00106a12020-06-08 18:54:23 +0200429 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100430 if( driver != NULL )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200431 {
432 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200433 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200434 return( PSA_SUCCESS );
435 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100436#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200437 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100438#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
439
Steven Cooreman98435dd2021-01-08 19:19:40 +0100440#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
441 /* Key location for external keys gets checked by the wrapper */
442 return( PSA_SUCCESS );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100443#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
444 /* No support for external lifetimes at all, or dynamic interface
445 * did not find driver for requested lifetime. */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200446 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100447#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine011e4282019-06-26 18:34:38 +0200448 }
449 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200450 /* Local/internal keys are always valid */
451 return( PSA_SUCCESS );
452}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200453
Ronald Crond2ed4812020-07-17 16:11:30 +0200454psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200455{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200456 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
457 {
458 /* Volatile keys are always supported */
459 return( PSA_SUCCESS );
460 }
461 else
462 {
463 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200464#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine87bc91c2021-04-21 21:23:40 +0200465 if( PSA_KEY_LIFETIME_IS_READ_ONLY( lifetime ) )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 else
468 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200469#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200470 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200471#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200472 }
Gilles Peskined167b942019-04-19 18:19:40 +0200473}
474
Ronald Cron71016a92020-08-28 19:01:50 +0200475psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100476{
Archana0dc86b52021-07-14 13:59:48 +0530477#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
478 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100479 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200480 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100481
Ronald Cron5c522922020-11-14 16:35:34 +0100482 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200483 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100484 {
Ronald Cron91e95152020-07-30 17:48:03 +0200485 *handle = PSA_KEY_HANDLE_INIT;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000486 if( status == PSA_ERROR_INVALID_HANDLE )
487 status = PSA_ERROR_DOES_NOT_EXIST;
488
Ronald Cronc4d1b512020-07-31 11:26:37 +0200489 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100490 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200491
492 *handle = key;
493
Ronald Cron5c522922020-11-14 16:35:34 +0100494 return( psa_unlock_key_slot( slot ) );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200495
Archana0dc86b52021-07-14 13:59:48 +0530496#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200497 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200498 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200499 return( PSA_ERROR_NOT_SUPPORTED );
Archana0dc86b52021-07-14 13:59:48 +0530500#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100501}
502
Gilles Peskine961849f2018-11-30 18:54:54 +0100503psa_status_t psa_close_key( psa_key_handle_t handle )
504{
Gilles Peskine267c6562019-05-27 19:01:54 +0200505 psa_status_t status;
506 psa_key_slot_t *slot;
507
Ronald Cronc26f8d42020-09-01 10:51:51 +0200508 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200509 return( PSA_SUCCESS );
510
Ronald Cron5c522922020-11-14 16:35:34 +0100511 status = psa_get_and_lock_key_slot_in_memory( handle, &slot );
Gilles Peskine267c6562019-05-27 19:01:54 +0200512 if( status != PSA_SUCCESS )
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000513 {
514 if( status == PSA_ERROR_DOES_NOT_EXIST )
515 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine267c6562019-05-27 19:01:54 +0200516
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000517 return( status );
518 }
Ronald Cron5c522922020-11-14 16:35:34 +0100519 if( slot->lock_count <= 1 )
Ronald Cronf2911112020-10-29 17:51:10 +0100520 return( psa_wipe_key_slot( slot ) );
521 else
Ronald Cron5c522922020-11-14 16:35:34 +0100522 return( psa_unlock_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100523}
524
Ronald Cron277a85f2020-08-04 15:49:48 +0200525psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
526{
527 psa_status_t status;
528 psa_key_slot_t *slot;
529
Ronald Cron5c522922020-11-14 16:35:34 +0100530 status = psa_get_and_lock_key_slot_in_memory( key, &slot );
Ronald Cron277a85f2020-08-04 15:49:48 +0200531 if( status != PSA_SUCCESS )
532 return( status );
533
Ronald Cronf2911112020-10-29 17:51:10 +0100534 if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100535 ( slot->lock_count <= 1 ) )
Ronald Cronf2911112020-10-29 17:51:10 +0100536 return( psa_wipe_key_slot( slot ) );
537 else
Ronald Cron5c522922020-11-14 16:35:34 +0100538 return( psa_unlock_key_slot( slot ) );
Ronald Cron277a85f2020-08-04 15:49:48 +0200539}
540
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200541void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
542{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200543 size_t slot_idx;
544
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200545 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200546
Steven Cooreman863470a2021-02-15 14:03:19 +0100547 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200548 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200549 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100550 if( psa_is_key_slot_locked( slot ) )
Ronald Cron0c3752a2020-10-30 11:54:03 +0100551 {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100552 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100553 }
Gilles Peskine41e50d22019-07-31 15:01:55 +0200554 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200555 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200556 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200557 continue;
558 }
Gilles Peskinef5f07c82021-04-21 20:06:51 +0200559 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200560 ++stats->volatile_slots;
Gilles Peskinef5f07c82021-04-21 20:06:51 +0200561 else
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200562 {
Ronald Cron71016a92020-08-28 19:01:50 +0200563 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200564 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100565 if( id > stats->max_open_internal_key_id )
566 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200567 }
Gilles Peskinef5f07c82021-04-21 20:06:51 +0200568 if( PSA_KEY_LIFETIME_GET_LOCATION( slot->attr.lifetime ) !=
569 PSA_KEY_LOCATION_LOCAL_STORAGE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200570 {
Ronald Cron71016a92020-08-28 19:01:50 +0200571 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200572 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100573 if( id > stats->max_open_external_key_id )
574 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200575 }
576 }
577}
578
Gilles Peskine961849f2018-11-30 18:54:54 +0100579#endif /* MBEDTLS_PSA_CRYPTO_C */