blob: e2074774da5450b24ded1676a0f0f478330694b9 [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(
55 mbedtls_svc_key_id_t key, int vendor_ok, int volatile_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 Cronfc9c5562020-10-15 19:24:49 +020065 ( key_id < PSA_KEY_ID_VOLATILE_MIN ) )
66 return( PSA_SUCCESS );
67
68 if( volatile_ok &&
69 ( PSA_KEY_ID_VOLATILE_MIN <= key_id ) &&
70 ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) )
Ronald Crond2ed4812020-07-17 16:11:30 +020071 return( PSA_SUCCESS );
72
Ronald Cronc4d1b512020-07-31 11:26:37 +020073 return( PSA_ERROR_INVALID_HANDLE );
Ronald Crond2ed4812020-07-17 16:11:30 +020074}
75
Ronald Cron97c8ad52020-10-15 11:17:11 +020076/** Search for the description of a key given its identifier.
77 *
78 * The descriptions of volatile keys and loaded persistent keys are
79 * stored in key slots. This function returns a pointer to the key slot
80 * containing the description of a key given its identifier.
81 *
82 * The function searches the key slots containing the description of the key
83 * with \p key identifier. The function does only read accesses to the key
84 * slots. The function does not load any persistent key thus does not access
85 * any storage.
86 *
87 * For volatile key identifiers, only one key slot is queried as a volatile
88 * key with identifier key_id can only be stored in slot of index
Ronald Cron96783552020-10-19 12:06:30 +020089 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
Ronald Cron97c8ad52020-10-15 11:17:11 +020090 *
Ronald Cronf95a2b12020-10-22 15:24:49 +020091 * On success, the access counter of the returned key slot is incremented by
92 * one. It is the responsibility of the caller to call
93 * psa_decrement_key_slot_access_count() when it does not access the key slot
94 * anymore.
95 *
Ronald Cron97c8ad52020-10-15 11:17:11 +020096 * \param key Key identifier to query.
97 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
98 * key slot containing the description of the key
99 * identified by \p key.
100 *
Ronald Cron96783552020-10-19 12:06:30 +0200101 * \retval #PSA_SUCCESS
Ronald Cron97c8ad52020-10-15 11:17:11 +0200102 * The pointer to the key slot containing the description of the key
103 * identified by \p key was returned.
Ronald Cron96783552020-10-19 12:06:30 +0200104 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Cron97c8ad52020-10-15 11:17:11 +0200105 * \p key is not a valid key identifier.
106 * \retval #PSA_ERROR_DOES_NOT_EXIST
107 * There is no key with key identifier \p key in the key slots.
108 */
109static psa_status_t psa_search_key_in_slots(
110 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100111{
Ronald Cronc4d1b512020-07-31 11:26:37 +0200112 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
Ronald Cron97c8ad52020-10-15 11:17:11 +0200113 psa_key_slot_t *slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100114
Ronald Cron97c8ad52020-10-15 11:17:11 +0200115 psa_status_t status = psa_validate_key_id( key, 1, 1 );
116 if( status != PSA_SUCCESS )
117 return( status );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100118
Ronald Cron97c8ad52020-10-15 11:17:11 +0200119 if( psa_key_id_is_volatile( key_id ) )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200120 {
Ronald Cron97c8ad52020-10-15 11:17:11 +0200121 slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
122
123 if( ! mbedtls_svc_key_id_equal( key, slot->attr.id ) )
124 status = PSA_ERROR_DOES_NOT_EXIST;
125 }
126 else
127 {
128 status = PSA_ERROR_DOES_NOT_EXIST;
129 slot = &global_data.key_slots[ PSA_KEY_SLOT_COUNT ];
130
131 while( slot > &global_data.key_slots[ 0 ] )
132 {
133 slot--;
134 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
135 {
136 status = PSA_SUCCESS;
137 break;
138 }
139 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200140 }
141
Ronald Cron97c8ad52020-10-15 11:17:11 +0200142 if( status == PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200143 {
Ronald Cron97c8ad52020-10-15 11:17:11 +0200144 *p_slot = slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200145 psa_increment_key_slot_access_count( slot );
146 }
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 ];
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 Cron98a54dd2020-07-24 16:33:11 +0200175 size_t slot_idx;
176
Gilles Peskine267c6562019-05-27 19:01:54 +0200177 if( ! global_data.key_slots_initialized )
178 return( PSA_ERROR_BAD_STATE );
179
Ronald Cron98a54dd2020-07-24 16:33:11 +0200180 for( slot_idx = PSA_KEY_SLOT_COUNT; slot_idx > 0; slot_idx-- )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100181 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200182 *p_slot = &global_data.key_slots[ slot_idx - 1 ];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200183 if( ! psa_is_key_slot_occupied( *p_slot ) )
Ronald Cron2a993152020-07-17 14:13:26 +0200184 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200185 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
186 ( (psa_key_id_t)slot_idx ) - 1;
Ronald Cron2a993152020-07-17 14:13:26 +0200187
Ronald Cronf95a2b12020-10-22 15:24:49 +0200188 psa_increment_key_slot_access_count( *p_slot );
189
Gilles Peskine66fb1262018-12-10 16:29:04 +0100190 return( PSA_SUCCESS );
Ronald Cron2a993152020-07-17 14:13:26 +0200191 }
Gilles Peskine66fb1262018-12-10 16:29:04 +0100192 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200193
Gilles Peskine267c6562019-05-27 19:01:54 +0200194 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100195 return( PSA_ERROR_INSUFFICIENT_MEMORY );
196}
197
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100198#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200199static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100200{
201 psa_status_t status = PSA_SUCCESS;
202 uint8_t *key_data = NULL;
203 size_t key_data_length = 0;
204
Gilles Peskine24318592019-07-30 20:30:51 +0200205 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200206 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100207 if( status != PSA_SUCCESS )
208 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200209
210#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200211 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200212 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200213 psa_se_key_data_storage_t *data;
214 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200215 {
216 status = PSA_ERROR_STORAGE_FAILURE;
217 goto exit;
218 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200219 data = (psa_se_key_data_storage_t *) key_data;
220 memcpy( &slot->data.se.slot_number, &data->slot_number,
221 sizeof( slot->data.se.slot_number ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200222 }
223 else
224#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
225 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200226 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
227 if( status != PSA_SUCCESS )
228 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200229 }
230
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100231exit:
232 psa_free_persistent_key_data( key_data, key_data_length );
233 return( status );
234}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200235#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
236
237psa_status_t psa_get_key_slot( mbedtls_svc_key_id_t key,
238 psa_key_slot_t **p_slot )
239{
Ronald Cron97c8ad52020-10-15 11:17:11 +0200240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200241
242 *p_slot = NULL;
243 if( ! global_data.key_slots_initialized )
244 return( PSA_ERROR_BAD_STATE );
245
Ronald Cronf95a2b12020-10-22 15:24:49 +0200246 /*
247 * On success, the pointer to the slot is passed directly to the caller
248 * thus no need to decrement the key slot access counter here.
249 */
Ronald Cron97c8ad52020-10-15 11:17:11 +0200250 status = psa_search_key_in_slots( key, p_slot );
251 if( status != PSA_ERROR_DOES_NOT_EXIST )
Ronald Cronc4d1b512020-07-31 11:26:37 +0200252 return( status );
253
Ronald Cronc4d1b512020-07-31 11:26:37 +0200254#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
255 psa_key_id_t volatile_key_id;
256
Ronald Cronc4d1b512020-07-31 11:26:37 +0200257 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
258 if( status != PSA_SUCCESS )
259 return( status );
260
261 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
262 (*p_slot)->attr.id = key;
263
264 status = psa_load_persistent_key_into_slot( *p_slot );
265 if( status != PSA_SUCCESS )
266 psa_wipe_key_slot( *p_slot );
267
268 return( status );
269#else
270 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200271#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100272
Ronald Cronc4d1b512020-07-31 11:26:37 +0200273}
274
Ronald Cronf95a2b12020-10-22 15:24:49 +0200275psa_status_t psa_decrement_key_slot_access_count( psa_key_slot_t *slot )
276{
277 if( slot == NULL )
278 return( PSA_SUCCESS );
279
280 if( slot->access_count > 0 )
281 {
282 slot->access_count--;
283 return( PSA_SUCCESS );
284 }
285
286 /*
287 * As the return error code may not be handled in case of multiple errors,
288 * do our best to report if the access counter is equal to zero: if
289 * available call MBEDTLS_PARAM_FAILED that may terminate execution (if
290 * called as part of the execution of a unit test suite this will stop the
291 * test suite execution) and if MBEDTLS_PARAM_FAILED does not terminate
292 * execution ouput an error message on standard error output.
293 */
294#ifdef MBEDTLS_CHECK_PARAMS
295 MBEDTLS_PARAM_FAILED( slot->access_count > 0 );
296#endif
297#ifdef MBEDTLS_PLATFORM_C
298 mbedtls_fprintf( stderr,
299 "\nFATAL psa_decrement_key_slot_access_count Decrementing a zero access counter.\n" );
300#endif
301
302 return( PSA_ERROR_CORRUPTION_DETECTED );
303}
304
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200305psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200306 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200307{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200308 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200309 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman00106a12020-06-08 18:54:23 +0200311 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
312 if( driver == NULL )
Gilles Peskine011e4282019-06-26 18:34:38 +0200313 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200314 else
315 {
316 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200317 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200318 return( PSA_SUCCESS );
319 }
320#else
321 (void) p_drv;
322 return( PSA_ERROR_INVALID_ARGUMENT );
323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine011e4282019-06-26 18:34:38 +0200324 }
325 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200326 /* Local/internal keys are always valid */
327 return( PSA_SUCCESS );
328}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200329
Ronald Crond2ed4812020-07-17 16:11:30 +0200330psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200331{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200332 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
333 {
334 /* Volatile keys are always supported */
335 return( PSA_SUCCESS );
336 }
337 else
338 {
339 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200340#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond2ed4812020-07-17 16:11:30 +0200341 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200342#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200343 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200344#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200345 }
Gilles Peskined167b942019-04-19 18:19:40 +0200346}
347
Ronald Cron71016a92020-08-28 19:01:50 +0200348psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100349{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200350#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100351 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200352 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100353
Ronald Cronc4d1b512020-07-31 11:26:37 +0200354 status = psa_get_key_slot( key, &slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200355 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100356 {
Ronald Cron91e95152020-07-30 17:48:03 +0200357 *handle = PSA_KEY_HANDLE_INIT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200358 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100359 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200360
361 *handle = key;
362
Ronald Cronf95a2b12020-10-22 15:24:49 +0200363 return( psa_decrement_key_slot_access_count( slot ) );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200364
Gilles Peskine30afafd2019-04-25 13:47:40 +0200365#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Ronald Cron27238fc2020-07-23 12:30:41 +0200366 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200367 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200368 return( PSA_ERROR_NOT_SUPPORTED );
369#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100370}
371
Gilles Peskine961849f2018-11-30 18:54:54 +0100372psa_status_t psa_close_key( psa_key_handle_t handle )
373{
Gilles Peskine267c6562019-05-27 19:01:54 +0200374 psa_status_t status;
375 psa_key_slot_t *slot;
376
Ronald Cronc26f8d42020-09-01 10:51:51 +0200377 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200378 return( PSA_SUCCESS );
379
Ronald Cron51345192020-10-16 16:07:03 +0200380 status = psa_search_key_in_slots( handle, &slot );
Gilles Peskine267c6562019-05-27 19:01:54 +0200381 if( status != PSA_SUCCESS )
382 return( status );
383
384 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100385}
386
Ronald Cron277a85f2020-08-04 15:49:48 +0200387psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
388{
389 psa_status_t status;
390 psa_key_slot_t *slot;
391
Ronald Cron51345192020-10-16 16:07:03 +0200392 status = psa_search_key_in_slots( key, &slot );
Ronald Cron277a85f2020-08-04 15:49:48 +0200393 if( status != PSA_SUCCESS )
394 return( status );
395
Ronald Crond98059d2020-10-23 18:00:55 +0200396 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200397 return( psa_decrement_key_slot_access_count( slot ) );
Ronald Cron277a85f2020-08-04 15:49:48 +0200398
399 return( psa_wipe_key_slot( slot ) );
400}
401
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200402void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
403{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200404 size_t slot_idx;
405
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200406 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200407
408 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200409 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200410 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200411 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200412 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200413 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200414 continue;
415 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200416 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200417 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200418 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200419 {
Ronald Cron71016a92020-08-28 19:01:50 +0200420 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200421 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100422 if( id > stats->max_open_internal_key_id )
423 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200424 }
425 else
426 {
Ronald Cron71016a92020-08-28 19:01:50 +0200427 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200428 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100429 if( id > stats->max_open_external_key_id )
430 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200431 }
432 }
433}
434
Gilles Peskine961849f2018-11-30 18:54:54 +0100435#endif /* MBEDTLS_PSA_CRYPTO_C */