blob: 32a6bb259e8645bdfb91be46985c4119cd8e03e7 [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>
37#if defined(MBEDTLS_PLATFORM_C)
38#include "mbedtls/platform.h"
39#else
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#endif
43
44#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
45
Gilles Peskine66fb1262018-12-10 16:29:04 +010046typedef struct
47{
Steven Cooreman863470a2021-02-15 14:03:19 +010048 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
Gilles Peskine66fb1262018-12-10 16:29:04 +010049 unsigned key_slots_initialized : 1;
50} psa_global_data_t;
51
Gilles Peskine2e14bd32018-12-12 14:05:08 +010052static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010053
Ronald Cron77e412c2021-03-31 17:36:31 +020054int psa_is_valid_key_id( mbedtls_svc_key_id_t key, int vendor_ok )
Ronald Crond2ed4812020-07-17 16:11:30 +020055{
56 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
57
58 if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
59 ( key_id <= PSA_KEY_ID_USER_MAX ) )
Ronald Cron77e412c2021-03-31 17:36:31 +020060 return( 1 );
Ronald Crond2ed4812020-07-17 16:11:30 +020061
62 if( vendor_ok &&
63 ( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
Ronald Croncbd7bea2020-11-11 14:57:44 +010064 ( key_id <= PSA_KEY_ID_VENDOR_MAX ) )
Ronald Cron77e412c2021-03-31 17:36:31 +020065 return( 1 );
Ronald Crond2ed4812020-07-17 16:11:30 +020066
Ronald Cron77e412c2021-03-31 17:36:31 +020067 return( 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +020068}
69
Ronald Cron5c522922020-11-14 16:35:34 +010070/** Get the description in memory of a key given its identifier and lock it.
Ronald Cron97c8ad52020-10-15 11:17:11 +020071 *
Ronald Cron5c522922020-11-14 16:35:34 +010072 * The descriptions of volatile keys and loaded persistent keys are
73 * stored in key slots. This function returns a pointer to the key slot
74 * containing the description of a key given its identifier.
Ronald Cron97c8ad52020-10-15 11:17:11 +020075 *
Ronald Cron5c522922020-11-14 16:35:34 +010076 * The function searches the key slots containing the description of the key
77 * with \p key identifier. The function does only read accesses to the key
78 * slots. The function does not load any persistent key thus does not access
79 * any storage.
Ronald Cron97c8ad52020-10-15 11:17:11 +020080 *
Ronald Cron5c522922020-11-14 16:35:34 +010081 * For volatile key identifiers, only one key slot is queried as a volatile
82 * key with identifier key_id can only be stored in slot of index
83 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020084 *
Ronald Cron5c522922020-11-14 16:35:34 +010085 * On success, the function locks the key slot. It is the responsibility of
86 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020087 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020088 * \param key Key identifier to query.
89 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
90 * key slot containing the description of the key
91 * identified by \p key.
92 *
Ronald Cron96783552020-10-19 12:06:30 +020093 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +020094 * The pointer to the key slot containing the description of the key
95 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +020096 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +020097 * \p key is not a valid key identifier.
98 * \retval #PSA_ERROR_DOES_NOT_EXIST
99 * There is no key with key identifier \p key in the key slots.
100 */
Ronald Cron5c522922020-11-14 16:35:34 +0100101static psa_status_t psa_get_and_lock_key_slot_in_memory(
Ronald Cron97c8ad52020-10-15 11:17:11 +0200102 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100103{
Ronald Cronf473d8b2020-11-12 10:07:21 +0100104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200105 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
Ronald Cronf473d8b2020-11-12 10:07:21 +0100106 size_t slot_idx;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200107 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100108
Ronald Cron97c8ad52020-10-15 11:17:11 +0200109 if( psa_key_id_is_volatile( key_id ) )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200110 {
Ronald Cron97c8ad52020-10-15 11:17:11 +0200111 slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
Ronald Cron1d12d872020-11-18 17:21:22 +0100112
113 /*
114 * Check if both the PSA key identifier key_id and the owner
115 * identifier of key match those of the key slot.
116 *
117 * Note that, if the key slot is not occupied, its PSA key identifier
118 * is equal to zero. This is an invalid value for a PSA key identifier
119 * and thus cannot be equal to the valid PSA key identifier key_id.
120 */
Ronald Cronf473d8b2020-11-12 10:07:21 +0100121 status = mbedtls_svc_key_id_equal( key, slot->attr.id ) ?
122 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200123 }
124 else
125 {
Ronald Cron77e412c2021-03-31 17:36:31 +0200126 if ( !psa_is_valid_key_id( key, 1 ) )
127 return( PSA_ERROR_INVALID_HANDLE );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200128
Steven Cooreman863470a2021-02-15 14:03:19 +0100129 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200130 {
Ronald Cronf473d8b2020-11-12 10:07:21 +0100131 slot = &global_data.key_slots[ slot_idx ];
Ronald Cron97c8ad52020-10-15 11:17:11 +0200132 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200133 break;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200134 }
Steven Cooreman863470a2021-02-15 14:03:19 +0100135 status = ( slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT ) ?
Ronald Cronf473d8b2020-11-12 10:07:21 +0100136 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200137 }
138
Ronald Cron97c8ad52020-10-15 11:17:11 +0200139 if( status == PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200140 {
Ronald Cron5c522922020-11-14 16:35:34 +0100141 status = psa_lock_key_slot( slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100142 if( status == PSA_SUCCESS )
143 *p_slot = slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200144 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200145
146 return( status );
Ronald Cronc4d1b512020-07-31 11:26:37 +0200147}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200148
Gilles Peskine66fb1262018-12-10 16:29:04 +0100149psa_status_t psa_initialize_key_slots( void )
150{
151 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
152 * guarantee that the key slots are initialized to all-zero, which
153 * means that all the key slots are in a valid, empty state. */
154 global_data.key_slots_initialized = 1;
155 return( PSA_SUCCESS );
156}
157
158void psa_wipe_all_key_slots( void )
159{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200160 size_t slot_idx;
161
Steven Cooreman863470a2021-02-15 14:03:19 +0100162 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100163 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200164 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron5c522922020-11-14 16:35:34 +0100165 slot->lock_count = 1;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100166 (void) psa_wipe_key_slot( slot );
167 }
168 global_data.key_slots_initialized = 0;
169}
170
Ronald Cronc4d1b512020-07-31 11:26:37 +0200171psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Ronald Cron2a993152020-07-17 14:13:26 +0200172 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100173{
Ronald Crona5b894f2020-10-21 09:04:34 +0200174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200175 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100176 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200177
Gilles Peskine267c6562019-05-27 19:01:54 +0200178 if( ! global_data.key_slots_initialized )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100179 {
Ronald Crona5b894f2020-10-21 09:04:34 +0200180 status = PSA_ERROR_BAD_STATE;
181 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100182 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200183
Ronald Cron5c522922020-11-14 16:35:34 +0100184 selected_slot = unlocked_persistent_key_slot = NULL;
Steven Cooreman863470a2021-02-15 14:03:19 +0100185 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Crona5b894f2020-10-21 09:04:34 +0200186 {
187 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
188 if( ! psa_is_key_slot_occupied( slot ) )
189 {
190 selected_slot = slot;
191 break;
192 }
193
Ronald Cron5c522922020-11-14 16:35:34 +0100194 if( ( unlocked_persistent_key_slot == NULL ) &&
Ronald Crona5b894f2020-10-21 09:04:34 +0200195 ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100196 ( ! psa_is_key_slot_locked( slot ) ) )
197 unlocked_persistent_key_slot = slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200198 }
199
200 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100201 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100202 * slot containing the description of a persistent key, recycle the first
203 * such key slot we encountered. If we later need to operate on the
204 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100205 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200206 */
207 if( ( selected_slot == NULL ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100208 ( unlocked_persistent_key_slot != NULL ) )
Ronald Crona5b894f2020-10-21 09:04:34 +0200209 {
Ronald Cron5c522922020-11-14 16:35:34 +0100210 selected_slot = unlocked_persistent_key_slot;
211 selected_slot->lock_count = 1;
Ronald Crona5b894f2020-10-21 09:04:34 +0200212 psa_wipe_key_slot( selected_slot );
213 }
214
215 if( selected_slot != NULL )
216 {
Ronald Cron5c522922020-11-14 16:35:34 +0100217 status = psa_lock_key_slot( selected_slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100218 if( status != PSA_SUCCESS )
219 goto error;
220
Ronald Crona5b894f2020-10-21 09:04:34 +0200221 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
222 ( (psa_key_id_t)( selected_slot - global_data.key_slots ) );
223 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200224
225 return( PSA_SUCCESS );
226 }
227 status = PSA_ERROR_INSUFFICIENT_MEMORY;
228
229error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200230 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200231 *volatile_key_id = 0;
232
233 return( status );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100234}
235
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100236#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200237static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100238{
239 psa_status_t status = PSA_SUCCESS;
240 uint8_t *key_data = NULL;
241 size_t key_data_length = 0;
242
Gilles Peskine24318592019-07-30 20:30:51 +0200243 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200244 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100245 if( status != PSA_SUCCESS )
246 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200247
Steven Cooreman98435dd2021-01-08 19:19:40 +0100248#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100249 /* Special handling is required for loading keys associated with a
250 * dynamically registered SE interface. */
251 const psa_drv_se_t *drv;
252 psa_drv_se_context_t *drv_context;
253 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200254 {
Steven Cooremanac3434f2021-01-15 17:36:02 +0100255 psa_se_key_data_storage_t *data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100256
Steven Cooremanac3434f2021-01-15 17:36:02 +0100257 if( key_data_length != sizeof( *data ) )
258 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100259 status = PSA_ERROR_DATA_INVALID;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100260 goto exit;
261 }
262 data = (psa_se_key_data_storage_t *) key_data;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100263 status = psa_copy_key_material_into_slot(
264 slot, data->slot_number, sizeof( data->slot_number ) );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100265 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200266 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Steven Cooremanac3434f2021-01-15 17:36:02 +0100269 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200270
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100271exit:
272 psa_free_persistent_key_data( key_data, key_data_length );
273 return( status );
274}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200275#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
276
Steven Cooreman6801f082021-02-19 17:21:22 +0100277#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooreman6801f082021-02-19 17:21:22 +0100278
279static psa_status_t psa_load_builtin_key_into_slot( psa_key_slot_t *slot )
280{
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremanc8b95342021-03-18 20:48:06 +0100283 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100284 psa_drv_slot_number_t slot_number = 0;
Steven Cooremanffc7fc92021-03-18 17:33:46 +0100285 size_t key_buffer_size = 0;
286 size_t key_buffer_length = 0;
287
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100288 if( ! psa_key_id_is_builtin(
289 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id ) ) )
Steven Cooreman6801f082021-02-19 17:21:22 +0100290 {
Steven Cooreman6801f082021-02-19 17:21:22 +0100291 return( PSA_ERROR_DOES_NOT_EXIST );
292 }
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100293
294 /* Check the platform function to see whether this key actually exists */
Steven Cooremanc8b95342021-03-18 20:48:06 +0100295 status = mbedtls_psa_platform_get_builtin_key(
296 slot->attr.id, &lifetime, &slot_number );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100297 if( status != PSA_SUCCESS )
298 return( status );
299
Steven Cooreman966db262021-04-13 13:45:45 +0200300 /* Set required key attributes to ensure get_builtin_key can retrieve the
301 * full attributes. */
302 psa_set_key_id( &attributes, slot->attr.id );
Steven Cooremanc8b95342021-03-18 20:48:06 +0100303 psa_set_key_lifetime( &attributes, lifetime );
304
Steven Cooremance487022021-04-07 18:09:53 +0200305 /* Get the full key attributes from the driver in order to be able to
306 * calculate the required buffer size. */
307 status = psa_driver_wrapper_get_builtin_key(
308 slot_number, &attributes,
309 NULL, 0, NULL );
310 if( status != PSA_ERROR_BUFFER_TOO_SMALL )
311 {
312 /* Builtin keys cannot be defined by the attributes alone */
313 if( status == PSA_SUCCESS )
314 status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman966db262021-04-13 13:45:45 +0200315 return( status );
Steven Cooremance487022021-04-07 18:09:53 +0200316 }
317
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200318 /* If the key should exist according to the platform, then ask the driver
319 * what its expected size is. */
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100320 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
Steven Cooreman85d554a2021-03-18 17:19:30 +0100321 &key_buffer_size );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100322 if( status != PSA_SUCCESS )
323 return( status );
324
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200325 /* Allocate a buffer of the required size and load the builtin key directly
Steven Cooremance487022021-04-07 18:09:53 +0200326 * into the (now properly sized) slot buffer. */
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200327 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
328 if( status != PSA_SUCCESS )
329 return( status );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100330
331 status = psa_driver_wrapper_get_builtin_key(
332 slot_number, &attributes,
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200333 slot->key.data, slot->key.bytes, &key_buffer_length );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100334 if( status != PSA_SUCCESS )
335 goto exit;
336
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200337 /* Copy actual key length and core attributes into the slot on success */
338 slot->key.bytes = key_buffer_length;
Steven Cooreman649a8f42021-03-18 17:34:55 +0100339 slot->attr = attributes.core;
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100340
341exit:
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200342 if( status != PSA_SUCCESS )
Steven Cooreman7ddee7f2021-04-07 18:08:30 +0200343 psa_remove_key_data_from_memory( slot );
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100344 return( status );
Steven Cooreman6801f082021-02-19 17:21:22 +0100345}
346#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
347
Ronald Cron5c522922020-11-14 16:35:34 +0100348psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
349 psa_key_slot_t **p_slot )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200350{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200352
353 *p_slot = NULL;
354 if( ! global_data.key_slots_initialized )
355 return( PSA_ERROR_BAD_STATE );
356
Ronald Cronf95a2b12020-10-22 15:24:49 +0200357 /*
358 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100359 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200360 */
Ronald Cron5c522922020-11-14 16:35:34 +0100361 status = psa_get_and_lock_key_slot_in_memory( key, p_slot );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200362 if( status != PSA_ERROR_DOES_NOT_EXIST )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200363 return( status );
364
Steven Cooreman0bb65362021-04-06 15:09:57 +0200365 /* Loading keys from storage requires support for such a mechanism */
366#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
367 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Ronald Cronc4d1b512020-07-31 11:26:37 +0200368 psa_key_id_t volatile_key_id;
369
Ronald Cronc4d1b512020-07-31 11:26:37 +0200370 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
371 if( status != PSA_SUCCESS )
372 return( status );
373
Ronald Cronc4d1b512020-07-31 11:26:37 +0200374 (*p_slot)->attr.id = key;
Steven Cooreman6801f082021-02-19 17:21:22 +0100375 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200376
Steven Cooreman6801f082021-02-19 17:21:22 +0100377 status = PSA_ERROR_DOES_NOT_EXIST;
378#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200379 /* Load keys in the 'builtin' range through their own interface */
Steven Cooreman6801f082021-02-19 17:21:22 +0100380 status = psa_load_builtin_key_into_slot( *p_slot );
Steven Cooreman6801f082021-02-19 17:21:22 +0100381#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
382
383#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremane5e308592021-02-22 14:40:04 +0100384 if( status == PSA_ERROR_DOES_NOT_EXIST )
385 status = psa_load_persistent_key_into_slot( *p_slot );
Steven Cooreman6801f082021-02-19 17:21:22 +0100386#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
387
Ronald Cronc4d1b512020-07-31 11:26:37 +0200388 if( status != PSA_SUCCESS )
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000389 {
Ronald Cronc4d1b512020-07-31 11:26:37 +0200390 psa_wipe_key_slot( *p_slot );
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000391 if( status == PSA_ERROR_DOES_NOT_EXIST )
392 status = PSA_ERROR_INVALID_HANDLE;
393 }
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200394 else
gabor-mezei-arm95180fe2021-06-28 14:59:52 +0200395 /* Add implicit usage flags. */
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200396 psa_extend_key_usage_flags( &(*p_slot)->attr.policy.usage );
397
Ronald Cronc4d1b512020-07-31 11:26:37 +0200398 return( status );
Steven Cooreman0bb65362021-04-06 15:09:57 +0200399#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
400 return( PSA_ERROR_INVALID_HANDLE );
401#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200402}
403
Ronald Cron5c522922020-11-14 16:35:34 +0100404psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200405{
406 if( slot == NULL )
407 return( PSA_SUCCESS );
408
Ronald Cron5c522922020-11-14 16:35:34 +0100409 if( slot->lock_count > 0 )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200410 {
Ronald Cron5c522922020-11-14 16:35:34 +0100411 slot->lock_count--;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200412 return( PSA_SUCCESS );
413 }
414
Ronald Cronf95a2b12020-10-22 15:24:49 +0200415 return( PSA_ERROR_CORRUPTION_DETECTED );
416}
417
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200418psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200419 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200420{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200421 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200422 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200423#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanac3434f2021-01-15 17:36:02 +0100424 /* Check whether a driver is registered against this lifetime */
Steven Cooreman00106a12020-06-08 18:54:23 +0200425 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100426 if( driver != NULL )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200427 {
428 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200429 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200430 return( PSA_SUCCESS );
431 }
Steven Cooremanac3434f2021-01-15 17:36:02 +0100432#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200433 (void) p_drv;
Steven Cooremanac3434f2021-01-15 17:36:02 +0100434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
435
Steven Cooreman98435dd2021-01-08 19:19:40 +0100436#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
437 /* Key location for external keys gets checked by the wrapper */
438 return( PSA_SUCCESS );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100439#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
440 /* No support for external lifetimes at all, or dynamic interface
441 * did not find driver for requested lifetime. */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200442 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanac3434f2021-01-15 17:36:02 +0100443#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Gilles Peskine011e4282019-06-26 18:34:38 +0200444 }
445 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200446 /* Local/internal keys are always valid */
447 return( PSA_SUCCESS );
448}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200449
Ronald Crond2ed4812020-07-17 16:11:30 +0200450psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200451{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200452 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
453 {
454 /* Volatile keys are always supported */
455 return( PSA_SUCCESS );
456 }
457 else
458 {
459 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200460#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine87bc91c2021-04-21 21:23:40 +0200461 if( PSA_KEY_LIFETIME_IS_READ_ONLY( lifetime ) )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 else
464 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200465#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200466 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200467#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200468 }
Gilles Peskined167b942019-04-19 18:19:40 +0200469}
470
Ronald Cron71016a92020-08-28 19:01:50 +0200471psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100472{
Archana0dc86b52021-07-14 13:59:48 +0530473#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
474 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
Gilles Peskine961849f2018-11-30 18:54:54 +0100475 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200476 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100477
Ronald Cron5c522922020-11-14 16:35:34 +0100478 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200479 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100480 {
Ronald Cron91e95152020-07-30 17:48:03 +0200481 *handle = PSA_KEY_HANDLE_INIT;
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000482 if( status == PSA_ERROR_INVALID_HANDLE )
483 status = PSA_ERROR_DOES_NOT_EXIST;
484
Ronald Cronc4d1b512020-07-31 11:26:37 +0200485 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100486 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200487
488 *handle = key;
489
Ronald Cron5c522922020-11-14 16:35:34 +0100490 return( psa_unlock_key_slot( slot ) );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200491
Archana0dc86b52021-07-14 13:59:48 +0530492#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron27238fc2020-07-23 12:30:41 +0200493 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200494 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200495 return( PSA_ERROR_NOT_SUPPORTED );
Archana0dc86b52021-07-14 13:59:48 +0530496#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine961849f2018-11-30 18:54:54 +0100497}
498
Gilles Peskine961849f2018-11-30 18:54:54 +0100499psa_status_t psa_close_key( psa_key_handle_t handle )
500{
Gilles Peskine267c6562019-05-27 19:01:54 +0200501 psa_status_t status;
502 psa_key_slot_t *slot;
503
Ronald Cronc26f8d42020-09-01 10:51:51 +0200504 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200505 return( PSA_SUCCESS );
506
Ronald Cron5c522922020-11-14 16:35:34 +0100507 status = psa_get_and_lock_key_slot_in_memory( handle, &slot );
Gilles Peskine267c6562019-05-27 19:01:54 +0200508 if( status != PSA_SUCCESS )
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000509 {
510 if( status == PSA_ERROR_DOES_NOT_EXIST )
511 status = PSA_ERROR_INVALID_HANDLE;
Gilles Peskine267c6562019-05-27 19:01:54 +0200512
Maulik Patelc1bfcdd2021-03-15 14:48:14 +0000513 return( status );
514 }
Ronald Cron5c522922020-11-14 16:35:34 +0100515 if( slot->lock_count <= 1 )
Ronald Cronf2911112020-10-29 17:51:10 +0100516 return( psa_wipe_key_slot( slot ) );
517 else
Ronald Cron5c522922020-11-14 16:35:34 +0100518 return( psa_unlock_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100519}
520
Ronald Cron277a85f2020-08-04 15:49:48 +0200521psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
522{
523 psa_status_t status;
524 psa_key_slot_t *slot;
525
Ronald Cron5c522922020-11-14 16:35:34 +0100526 status = psa_get_and_lock_key_slot_in_memory( key, &slot );
Ronald Cron277a85f2020-08-04 15:49:48 +0200527 if( status != PSA_SUCCESS )
528 return( status );
529
Ronald Cronf2911112020-10-29 17:51:10 +0100530 if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100531 ( slot->lock_count <= 1 ) )
Ronald Cronf2911112020-10-29 17:51:10 +0100532 return( psa_wipe_key_slot( slot ) );
533 else
Ronald Cron5c522922020-11-14 16:35:34 +0100534 return( psa_unlock_key_slot( slot ) );
Ronald Cron277a85f2020-08-04 15:49:48 +0200535}
536
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200537void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
538{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200539 size_t slot_idx;
540
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200541 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200542
Steven Cooreman863470a2021-02-15 14:03:19 +0100543 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200544 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200545 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100546 if( psa_is_key_slot_locked( slot ) )
Ronald Cron0c3752a2020-10-30 11:54:03 +0100547 {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100548 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100549 }
Gilles Peskine41e50d22019-07-31 15:01:55 +0200550 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200551 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200552 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200553 continue;
554 }
Gilles Peskinef5f07c82021-04-21 20:06:51 +0200555 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200556 ++stats->volatile_slots;
Gilles Peskinef5f07c82021-04-21 20:06:51 +0200557 else
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200558 {
Ronald Cron71016a92020-08-28 19:01:50 +0200559 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200560 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100561 if( id > stats->max_open_internal_key_id )
562 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200563 }
Gilles Peskinef5f07c82021-04-21 20:06:51 +0200564 if( PSA_KEY_LIFETIME_GET_LOCATION( slot->attr.lifetime ) !=
565 PSA_KEY_LOCATION_LOCAL_STORAGE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200566 {
Ronald Cron71016a92020-08-28 19:01:50 +0200567 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200568 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100569 if( id > stats->max_open_external_key_id )
570 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200571 }
572 }
573}
574
Gilles Peskine961849f2018-11-30 18:54:54 +0100575#endif /* MBEDTLS_PSA_CRYPTO_C */