blob: f30b75ada0a6806a76e1c4588829076785661a8d [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"
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{
48 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
49 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 Cronfc9c5562020-10-15 19:24:49 +020054psa_status_t psa_validate_key_id(
Ronald Croncbd7bea2020-11-11 14:57:44 +010055 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 ) )
61 return( PSA_SUCCESS );
62
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 Crond2ed4812020-07-17 16:11:30 +020066 return( PSA_SUCCESS );
67
Ronald Cronc4d1b512020-07-31 11:26:37 +020068 return( PSA_ERROR_INVALID_HANDLE );
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 Croncbd7bea2020-11-11 14:57:44 +0100127 status = psa_validate_key_id( key, 1 );
Ronald Cronf473d8b2020-11-12 10:07:21 +0100128 if( status != PSA_SUCCESS )
129 return( status );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200130
Ronald Cronf473d8b2020-11-12 10:07:21 +0100131 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200132 {
Ronald Cronf473d8b2020-11-12 10:07:21 +0100133 slot = &global_data.key_slots[ slot_idx ];
Ronald Cron97c8ad52020-10-15 11:17:11 +0200134 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
Ronald Cron97c8ad52020-10-15 11:17:11 +0200135 break;
Ronald Cron97c8ad52020-10-15 11:17:11 +0200136 }
Ronald Cronf473d8b2020-11-12 10:07:21 +0100137 status = ( slot_idx < PSA_KEY_SLOT_COUNT ) ?
138 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200139 }
140
Ronald Cron97c8ad52020-10-15 11:17:11 +0200141 if( status == PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200142 {
Ronald Cron5c522922020-11-14 16:35:34 +0100143 status = psa_lock_key_slot( slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100144 if( status == PSA_SUCCESS )
145 *p_slot = slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200146 }
Ronald Cron97c8ad52020-10-15 11:17:11 +0200147
148 return( status );
Ronald Cronc4d1b512020-07-31 11:26:37 +0200149}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200150
Gilles Peskine66fb1262018-12-10 16:29:04 +0100151psa_status_t psa_initialize_key_slots( void )
152{
153 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
154 * guarantee that the key slots are initialized to all-zero, which
155 * means that all the key slots are in a valid, empty state. */
156 global_data.key_slots_initialized = 1;
157 return( PSA_SUCCESS );
158}
159
160void psa_wipe_all_key_slots( void )
161{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200162 size_t slot_idx;
163
164 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100165 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200166 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron5c522922020-11-14 16:35:34 +0100167 slot->lock_count = 1;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100168 (void) psa_wipe_key_slot( slot );
169 }
170 global_data.key_slots_initialized = 0;
171}
172
Ronald Cronc4d1b512020-07-31 11:26:37 +0200173psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Ronald Cron2a993152020-07-17 14:13:26 +0200174 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100175{
Ronald Crona5b894f2020-10-21 09:04:34 +0200176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200177 size_t slot_idx;
Ronald Cron5c522922020-11-14 16:35:34 +0100178 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
Ronald Cron98a54dd2020-07-24 16:33:11 +0200179
Gilles Peskine267c6562019-05-27 19:01:54 +0200180 if( ! global_data.key_slots_initialized )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100181 {
Ronald Crona5b894f2020-10-21 09:04:34 +0200182 status = PSA_ERROR_BAD_STATE;
183 goto error;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100184 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200185
Ronald Cron5c522922020-11-14 16:35:34 +0100186 selected_slot = unlocked_persistent_key_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200187 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
188 {
189 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
190 if( ! psa_is_key_slot_occupied( slot ) )
191 {
192 selected_slot = slot;
193 break;
194 }
195
Ronald Cron5c522922020-11-14 16:35:34 +0100196 if( ( unlocked_persistent_key_slot == NULL ) &&
Ronald Crona5b894f2020-10-21 09:04:34 +0200197 ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100198 ( ! psa_is_key_slot_locked( slot ) ) )
199 unlocked_persistent_key_slot = slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200200 }
201
202 /*
Ronald Cron5c522922020-11-14 16:35:34 +0100203 * If there is no unused key slot and there is at least one unlocked key
Ronald Cron1d12d872020-11-18 17:21:22 +0100204 * slot containing the description of a persistent key, recycle the first
205 * such key slot we encountered. If we later need to operate on the
206 * persistent key we are evicting now, we will reload its description from
Ronald Cron19daca92020-11-10 18:08:03 +0100207 * storage.
Ronald Crona5b894f2020-10-21 09:04:34 +0200208 */
209 if( ( selected_slot == NULL ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100210 ( unlocked_persistent_key_slot != NULL ) )
Ronald Crona5b894f2020-10-21 09:04:34 +0200211 {
Ronald Cron5c522922020-11-14 16:35:34 +0100212 selected_slot = unlocked_persistent_key_slot;
213 selected_slot->lock_count = 1;
Ronald Crona5b894f2020-10-21 09:04:34 +0200214 psa_wipe_key_slot( selected_slot );
215 }
216
217 if( selected_slot != NULL )
218 {
Ronald Cron5c522922020-11-14 16:35:34 +0100219 status = psa_lock_key_slot( selected_slot );
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100220 if( status != PSA_SUCCESS )
221 goto error;
222
Ronald Crona5b894f2020-10-21 09:04:34 +0200223 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
224 ( (psa_key_id_t)( selected_slot - global_data.key_slots ) );
225 *p_slot = selected_slot;
Ronald Crona5b894f2020-10-21 09:04:34 +0200226
227 return( PSA_SUCCESS );
228 }
229 status = PSA_ERROR_INSUFFICIENT_MEMORY;
230
231error:
Gilles Peskine267c6562019-05-27 19:01:54 +0200232 *p_slot = NULL;
Ronald Crona5b894f2020-10-21 09:04:34 +0200233 *volatile_key_id = 0;
234
235 return( status );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100236}
237
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100238#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200239static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100240{
241 psa_status_t status = PSA_SUCCESS;
242 uint8_t *key_data = NULL;
243 size_t key_data_length = 0;
244
Gilles Peskine24318592019-07-30 20:30:51 +0200245 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200246 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100247 if( status != PSA_SUCCESS )
248 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200249
Gilles Peskine24318592019-07-30 20:30:51 +0200250 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200251 {
Steven Cooreman98435dd2021-01-08 19:19:40 +0100252#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
253 const psa_drv_se_t *drv;
254 psa_drv_se_context_t *drv_context;
255 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200256 {
Steven Cooreman98435dd2021-01-08 19:19:40 +0100257 psa_se_key_data_storage_t *data;
258 if( key_data_length != sizeof( *data ) )
259 {
260 status = PSA_ERROR_STORAGE_FAILURE;
261 goto exit;
262 }
263 data = (psa_se_key_data_storage_t *) key_data;
264 memcpy( &slot->data.se.slot_number, &data->slot_number,
265 sizeof( slot->data.se.slot_number ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200266 }
Steven Cooreman98435dd2021-01-08 19:19:40 +0100267 else
268#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
269 {
270 /* A key that is successfully loaded from storage with an
271 * external lifetime, but doesn't belong to an SE driver,
272 * must be a PSA driver-associated key which we can just
273 * load like an internal key. */
274 if ( key_data == NULL )
275 {
276 status = PSA_ERROR_STORAGE_FAILURE;
277 goto exit;
278 }
279
280 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
281 }
Gilles Peskine1df83d42019-07-23 16:13:14 +0200282 }
283 else
Gilles Peskine1df83d42019-07-23 16:13:14 +0200284 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200285 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200286 }
287
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100288exit:
289 psa_free_persistent_key_data( key_data, key_data_length );
290 return( status );
291}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200292#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
293
Ronald Cron5c522922020-11-14 16:35:34 +0100294psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
295 psa_key_slot_t **p_slot )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200296{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200298
299 *p_slot = NULL;
300 if( ! global_data.key_slots_initialized )
301 return( PSA_ERROR_BAD_STATE );
302
Ronald Cronf95a2b12020-10-22 15:24:49 +0200303 /*
304 * On success, the pointer to the slot is passed directly to the caller
Ronald Cron5c522922020-11-14 16:35:34 +0100305 * thus no need to unlock the key slot here.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200306 */
Ronald Cron5c522922020-11-14 16:35:34 +0100307 status = psa_get_and_lock_key_slot_in_memory( key, p_slot );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200308 if( status != PSA_ERROR_DOES_NOT_EXIST )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200309 return( status );
310
Ronald Cronc4d1b512020-07-31 11:26:37 +0200311#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
312 psa_key_id_t volatile_key_id;
313
Ronald Cronc4d1b512020-07-31 11:26:37 +0200314 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
315 if( status != PSA_SUCCESS )
316 return( status );
317
318 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
319 (*p_slot)->attr.id = key;
320
321 status = psa_load_persistent_key_into_slot( *p_slot );
322 if( status != PSA_SUCCESS )
323 psa_wipe_key_slot( *p_slot );
324
325 return( status );
326#else
327 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200328#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100329
Ronald Cronc4d1b512020-07-31 11:26:37 +0200330}
331
Ronald Cron5c522922020-11-14 16:35:34 +0100332psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200333{
334 if( slot == NULL )
335 return( PSA_SUCCESS );
336
Ronald Cron5c522922020-11-14 16:35:34 +0100337 if( slot->lock_count > 0 )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200338 {
Ronald Cron5c522922020-11-14 16:35:34 +0100339 slot->lock_count--;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200340 return( PSA_SUCCESS );
341 }
342
343 /*
344 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100345 * do our best to report if the lock counter is equal to zero: if
Ronald Cronf95a2b12020-10-22 15:24:49 +0200346 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
347 * called as part of the execution of a unit test suite this will stop the
Ronald Cron4640c152020-11-13 10:11:01 +0100348 * test suite execution).
Ronald Cronf95a2b12020-10-22 15:24:49 +0200349 */
350#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100351 MBEDTLS_PARAM_FAILED( slot->lock_count > 0 );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200352#endif
Ronald Cronf95a2b12020-10-22 15:24:49 +0200353
354 return( PSA_ERROR_CORRUPTION_DETECTED );
355}
356
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200357psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200358 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200359{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200360 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200361 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200362#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman00106a12020-06-08 18:54:23 +0200363 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
364 if( driver == NULL )
Steven Cooreman98435dd2021-01-08 19:19:40 +0100365 {
366#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
367 /* Key location for external keys gets checked by the wrapper */
368 return( PSA_SUCCESS );
369#else
Gilles Peskine011e4282019-06-26 18:34:38 +0200370 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman98435dd2021-01-08 19:19:40 +0100371#endif
372 }
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200373 else
374 {
375 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200376 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200377 return( PSA_SUCCESS );
378 }
379#else
380 (void) p_drv;
Steven Cooreman98435dd2021-01-08 19:19:40 +0100381#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
382 /* Key location for external keys gets checked by the wrapper */
383 return( PSA_SUCCESS );
384#else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200385 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman98435dd2021-01-08 19:19:40 +0100386#endif
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200387#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine011e4282019-06-26 18:34:38 +0200388 }
389 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200390 /* Local/internal keys are always valid */
391 return( PSA_SUCCESS );
392}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200393
Ronald Crond2ed4812020-07-17 16:11:30 +0200394psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200395{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200396 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
397 {
398 /* Volatile keys are always supported */
399 return( PSA_SUCCESS );
400 }
401 else
402 {
403 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200404#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond2ed4812020-07-17 16:11:30 +0200405 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200406#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200407 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200408#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200409 }
Gilles Peskined167b942019-04-19 18:19:40 +0200410}
411
Ronald Cron71016a92020-08-28 19:01:50 +0200412psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100413{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200414#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100415 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200416 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100417
Ronald Cron5c522922020-11-14 16:35:34 +0100418 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200419 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100420 {
Ronald Cron91e95152020-07-30 17:48:03 +0200421 *handle = PSA_KEY_HANDLE_INIT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200422 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100423 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200424
425 *handle = key;
426
Ronald Cron5c522922020-11-14 16:35:34 +0100427 return( psa_unlock_key_slot( slot ) );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200428
Gilles Peskine30afafd2019-04-25 13:47:40 +0200429#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Ronald Cron27238fc2020-07-23 12:30:41 +0200430 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200431 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200432 return( PSA_ERROR_NOT_SUPPORTED );
433#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100434}
435
Gilles Peskine961849f2018-11-30 18:54:54 +0100436psa_status_t psa_close_key( psa_key_handle_t handle )
437{
Gilles Peskine267c6562019-05-27 19:01:54 +0200438 psa_status_t status;
439 psa_key_slot_t *slot;
440
Ronald Cronc26f8d42020-09-01 10:51:51 +0200441 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200442 return( PSA_SUCCESS );
443
Ronald Cron5c522922020-11-14 16:35:34 +0100444 status = psa_get_and_lock_key_slot_in_memory( handle, &slot );
Gilles Peskine267c6562019-05-27 19:01:54 +0200445 if( status != PSA_SUCCESS )
446 return( status );
447
Ronald Cron5c522922020-11-14 16:35:34 +0100448 if( slot->lock_count <= 1 )
Ronald Cronf2911112020-10-29 17:51:10 +0100449 return( psa_wipe_key_slot( slot ) );
450 else
Ronald Cron5c522922020-11-14 16:35:34 +0100451 return( psa_unlock_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100452}
453
Ronald Cron277a85f2020-08-04 15:49:48 +0200454psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
455{
456 psa_status_t status;
457 psa_key_slot_t *slot;
458
Ronald Cron5c522922020-11-14 16:35:34 +0100459 status = psa_get_and_lock_key_slot_in_memory( key, &slot );
Ronald Cron277a85f2020-08-04 15:49:48 +0200460 if( status != PSA_SUCCESS )
461 return( status );
462
Ronald Cronf2911112020-10-29 17:51:10 +0100463 if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
Ronald Cron5c522922020-11-14 16:35:34 +0100464 ( slot->lock_count <= 1 ) )
Ronald Cronf2911112020-10-29 17:51:10 +0100465 return( psa_wipe_key_slot( slot ) );
466 else
Ronald Cron5c522922020-11-14 16:35:34 +0100467 return( psa_unlock_key_slot( slot ) );
Ronald Cron277a85f2020-08-04 15:49:48 +0200468}
469
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200470void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
471{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200472 size_t slot_idx;
473
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200474 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200475
476 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200477 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200478 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100479 if( psa_is_key_slot_locked( slot ) )
Ronald Cron0c3752a2020-10-30 11:54:03 +0100480 {
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100481 ++stats->locked_slots;
Ronald Cron0c3752a2020-10-30 11:54:03 +0100482 }
Gilles Peskine41e50d22019-07-31 15:01:55 +0200483 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200484 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200485 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200486 continue;
487 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200488 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200489 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200490 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200491 {
Ronald Cron71016a92020-08-28 19:01:50 +0200492 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200493 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100494 if( id > stats->max_open_internal_key_id )
495 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200496 }
497 else
498 {
Ronald Cron71016a92020-08-28 19:01:50 +0200499 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200500 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100501 if( id > stats->max_open_external_key_id )
502 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200503 }
504 }
505}
506
Gilles Peskine961849f2018-11-30 18:54:54 +0100507#endif /* MBEDTLS_PSA_CRYPTO_C */