2
* PSA crypto layer on top of Mbed TLS crypto
5
* Copyright The Mbed TLS Contributors
6
* SPDX-License-Identifier: Apache-2.0
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
12
* http://www.apache.org/licenses/LICENSE-2.0
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.
23
#if defined(MBEDTLS_PSA_CRYPTO_C)
25
#include "psa_crypto_service_integration.h"
26
#include "psa/crypto.h"
28
#include "psa_crypto_core.h"
29
#include "psa_crypto_slot_management.h"
30
#include "psa_crypto_storage.h"
31
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
32
#include "psa_crypto_se.h"
37
#if defined(MBEDTLS_PLATFORM_C)
38
#include "mbedtls/platform.h"
40
#define mbedtls_calloc calloc
41
#define mbedtls_free free
44
#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
48
psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
49
unsigned key_slots_initialized : 1;
52
static psa_global_data_t global_data;
54
psa_status_t psa_validate_key_id(
55
mbedtls_svc_key_id_t key, int vendor_ok )
57
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
59
if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
60
( key_id <= PSA_KEY_ID_USER_MAX ) )
61
return( PSA_SUCCESS );
64
( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
65
( key_id <= PSA_KEY_ID_VENDOR_MAX ) )
66
return( PSA_SUCCESS );
68
return( PSA_ERROR_INVALID_HANDLE );
71
/** Get the description in memory of a key given its identifier and lock it.
73
* 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.
77
* 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
82
* 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 ).
86
* 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.
89
* \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.
94
* \retval #PSA_SUCCESS
95
* The pointer to the key slot containing the description of the key
96
* identified by \p key was returned.
97
* \retval #PSA_ERROR_INVALID_HANDLE
98
* \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.
102
static psa_status_t psa_get_and_lock_key_slot_in_memory(
103
mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
105
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
106
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
108
psa_key_slot_t *slot = NULL;
110
if( psa_key_id_is_volatile( key_id ) )
112
slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
115
* Check if both the PSA key identifier key_id and the owner
116
* identifier of key match those of the key slot.
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.
122
status = mbedtls_svc_key_id_equal( key, slot->attr.id ) ?
123
PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
127
status = psa_validate_key_id( key, 1 );
128
if( status != PSA_SUCCESS )
131
for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
133
slot = &global_data.key_slots[ slot_idx ];
134
if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
137
status = ( slot_idx < PSA_KEY_SLOT_COUNT ) ?
138
PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
141
if( status == PSA_SUCCESS )
143
status = psa_lock_key_slot( slot );
144
if( status == PSA_SUCCESS )
151
psa_status_t psa_initialize_key_slots( void )
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 );
160
void psa_wipe_all_key_slots( void )
164
for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
166
psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
167
slot->lock_count = 1;
168
(void) psa_wipe_key_slot( slot );
170
global_data.key_slots_initialized = 0;
173
psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
174
psa_key_slot_t **p_slot )
176
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
178
psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
180
if( ! global_data.key_slots_initialized )
182
status = PSA_ERROR_BAD_STATE;
186
selected_slot = unlocked_persistent_key_slot = NULL;
187
for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
189
psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
190
if( ! psa_is_key_slot_occupied( slot ) )
192
selected_slot = slot;
196
if( ( unlocked_persistent_key_slot == NULL ) &&
197
( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
198
( ! psa_is_key_slot_locked( slot ) ) )
199
unlocked_persistent_key_slot = slot;
203
* If there is no unused key slot and there is at least one unlocked key
204
* 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
209
if( ( selected_slot == NULL ) &&
210
( unlocked_persistent_key_slot != NULL ) )
212
selected_slot = unlocked_persistent_key_slot;
213
selected_slot->lock_count = 1;
214
psa_wipe_key_slot( selected_slot );
217
if( selected_slot != NULL )
219
status = psa_lock_key_slot( selected_slot );
220
if( status != PSA_SUCCESS )
223
*volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
224
( (psa_key_id_t)( selected_slot - global_data.key_slots ) );
225
*p_slot = selected_slot;
227
return( PSA_SUCCESS );
229
status = PSA_ERROR_INSUFFICIENT_MEMORY;
233
*volatile_key_id = 0;
238
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
239
static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
241
psa_status_t status = PSA_SUCCESS;
242
uint8_t *key_data = NULL;
243
size_t key_data_length = 0;
245
status = psa_load_persistent_key( &slot->attr,
246
&key_data, &key_data_length );
247
if( status != PSA_SUCCESS )
250
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
251
if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
253
psa_se_key_data_storage_t *data;
254
if( key_data_length != sizeof( *data ) )
256
status = PSA_ERROR_STORAGE_FAILURE;
259
data = (psa_se_key_data_storage_t *) key_data;
260
memcpy( &slot->data.se.slot_number, &data->slot_number,
261
sizeof( slot->data.se.slot_number ) );
264
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
266
status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
267
if( status != PSA_SUCCESS )
272
psa_free_persistent_key_data( key_data, key_data_length );
275
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
277
psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
278
psa_key_slot_t **p_slot )
280
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
283
if( ! global_data.key_slots_initialized )
284
return( PSA_ERROR_BAD_STATE );
287
* On success, the pointer to the slot is passed directly to the caller
288
* thus no need to unlock the key slot here.
290
status = psa_get_and_lock_key_slot_in_memory( key, p_slot );
291
if( status != PSA_ERROR_DOES_NOT_EXIST )
294
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
295
psa_key_id_t volatile_key_id;
297
status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
298
if( status != PSA_SUCCESS )
301
(*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
302
(*p_slot)->attr.id = key;
304
status = psa_load_persistent_key_into_slot( *p_slot );
305
if( status != PSA_SUCCESS )
306
psa_wipe_key_slot( *p_slot );
310
return( PSA_ERROR_DOES_NOT_EXIST );
311
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
315
psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
318
return( PSA_SUCCESS );
320
if( slot->lock_count > 0 )
323
return( PSA_SUCCESS );
327
* As the return error code may not be handled in case of multiple errors,
328
* do our best to report if the lock counter is equal to zero: if
329
* available call MBEDTLS_PARAM_FAILED that may terminate execution (if
330
* called as part of the execution of a unit test suite this will stop the
331
* test suite execution).
333
#ifdef MBEDTLS_CHECK_PARAMS
334
MBEDTLS_PARAM_FAILED( slot->lock_count > 0 );
337
return( PSA_ERROR_CORRUPTION_DETECTED );
340
psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
341
psa_se_drv_table_entry_t **p_drv )
343
if ( psa_key_lifetime_is_external( lifetime ) )
345
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
346
psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
348
return( PSA_ERROR_INVALID_ARGUMENT );
353
return( PSA_SUCCESS );
357
return( PSA_ERROR_INVALID_ARGUMENT );
358
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
361
/* Local/internal keys are always valid */
362
return( PSA_SUCCESS );
365
psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
367
if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
369
/* Volatile keys are always supported */
370
return( PSA_SUCCESS );
374
/* Persistent keys require storage support */
375
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
376
return( PSA_SUCCESS );
377
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
378
return( PSA_ERROR_NOT_SUPPORTED );
379
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
383
psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
385
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
387
psa_key_slot_t *slot;
389
status = psa_get_and_lock_key_slot( key, &slot );
390
if( status != PSA_SUCCESS )
392
*handle = PSA_KEY_HANDLE_INIT;
398
return( psa_unlock_key_slot( slot ) );
400
#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
402
*handle = PSA_KEY_HANDLE_INIT;
403
return( PSA_ERROR_NOT_SUPPORTED );
404
#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
407
psa_status_t psa_close_key( psa_key_handle_t handle )
410
psa_key_slot_t *slot;
412
if( psa_key_handle_is_null( handle ) )
413
return( PSA_SUCCESS );
415
status = psa_get_and_lock_key_slot_in_memory( handle, &slot );
416
if( status != PSA_SUCCESS )
419
if( slot->lock_count <= 1 )
420
return( psa_wipe_key_slot( slot ) );
422
return( psa_unlock_key_slot( slot ) );
425
psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
428
psa_key_slot_t *slot;
430
status = psa_get_and_lock_key_slot_in_memory( key, &slot );
431
if( status != PSA_SUCCESS )
434
if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
435
( slot->lock_count <= 1 ) )
436
return( psa_wipe_key_slot( slot ) );
438
return( psa_unlock_key_slot( slot ) );
441
void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
445
memset( stats, 0, sizeof( *stats ) );
447
for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
449
const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
450
if( psa_is_key_slot_locked( slot ) )
452
++stats->locked_slots;
454
if( ! psa_is_key_slot_occupied( slot ) )
456
++stats->empty_slots;
459
if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
460
++stats->volatile_slots;
461
else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
463
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
464
++stats->persistent_slots;
465
if( id > stats->max_open_internal_key_id )
466
stats->max_open_internal_key_id = id;
470
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
471
++stats->external_slots;
472
if( id > stats->max_open_external_key_id )
473
stats->max_open_external_key_id = id;
478
#endif /* MBEDTLS_PSA_CRYPTO_C */