2018-11-30 18:54:54 +01:00
|
|
|
/*
|
|
|
|
* PSA crypto layer on top of Mbed TLS crypto
|
|
|
|
*/
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2018-11-30 18:54:54 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2018-11-30 18:54:54 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
2019-02-14 09:28:02 +01:00
|
|
|
#include "psa_crypto_service_integration.h"
|
2018-11-30 18:54:54 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2018-12-10 16:29:04 +01:00
|
|
|
#include "psa_crypto_core.h"
|
2018-11-30 18:54:54 +01:00
|
|
|
#include "psa_crypto_slot_management.h"
|
|
|
|
#include "psa_crypto_storage.h"
|
2019-07-30 21:32:04 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
#include "psa_crypto_se.h"
|
|
|
|
#endif
|
2018-11-30 18:54:54 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
|
|
|
|
|
2018-12-10 16:29:04 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
|
|
|
|
unsigned key_slots_initialized : 1;
|
|
|
|
} psa_global_data_t;
|
|
|
|
|
2018-12-12 14:05:08 +01:00
|
|
|
static psa_global_data_t global_data;
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2020-10-15 19:24:49 +02:00
|
|
|
psa_status_t psa_validate_key_id(
|
|
|
|
mbedtls_svc_key_id_t key, int vendor_ok, int volatile_ok )
|
2020-07-17 16:11:30 +02:00
|
|
|
{
|
|
|
|
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
|
|
|
|
|
|
|
|
if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
|
|
|
|
( key_id <= PSA_KEY_ID_USER_MAX ) )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
|
|
|
if( vendor_ok &&
|
|
|
|
( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
|
2020-10-15 19:24:49 +02:00
|
|
|
( key_id < PSA_KEY_ID_VOLATILE_MIN ) )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
|
|
|
if( volatile_ok &&
|
|
|
|
( PSA_KEY_ID_VOLATILE_MIN <= key_id ) &&
|
|
|
|
( key_id <= PSA_KEY_ID_VOLATILE_MAX ) )
|
2020-07-17 16:11:30 +02:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
2020-07-31 11:26:37 +02:00
|
|
|
return( PSA_ERROR_INVALID_HANDLE );
|
2020-07-17 16:11:30 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 11:17:11 +02:00
|
|
|
/** Search for the description of a key given its identifier.
|
|
|
|
*
|
|
|
|
* The descriptions of volatile keys and loaded persistent keys are
|
|
|
|
* stored in key slots. This function returns a pointer to the key slot
|
|
|
|
* containing the description of a key given its identifier.
|
|
|
|
*
|
|
|
|
* The function searches the key slots containing the description of the key
|
|
|
|
* with \p key identifier. The function does only read accesses to the key
|
|
|
|
* slots. The function does not load any persistent key thus does not access
|
|
|
|
* any storage.
|
|
|
|
*
|
|
|
|
* For volatile key identifiers, only one key slot is queried as a volatile
|
|
|
|
* key with identifier key_id can only be stored in slot of index
|
2020-10-19 12:06:30 +02:00
|
|
|
* ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
|
2020-10-15 11:17:11 +02:00
|
|
|
*
|
2020-10-22 15:24:49 +02:00
|
|
|
* On success, the access counter of the returned key slot is incremented by
|
|
|
|
* one. It is the responsibility of the caller to call
|
|
|
|
* psa_decrement_key_slot_access_count() when it does not access the key slot
|
|
|
|
* anymore.
|
|
|
|
*
|
2020-10-15 11:17:11 +02:00
|
|
|
* \param key Key identifier to query.
|
|
|
|
* \param[out] p_slot On success, `*p_slot` contains a pointer to the
|
|
|
|
* key slot containing the description of the key
|
|
|
|
* identified by \p key.
|
|
|
|
*
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
2020-10-15 11:17:11 +02:00
|
|
|
* The pointer to the key slot containing the description of the key
|
|
|
|
* identified by \p key was returned.
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_ERROR_INVALID_HANDLE
|
2020-10-15 11:17:11 +02:00
|
|
|
* \p key is not a valid key identifier.
|
|
|
|
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
|
|
|
* There is no key with key identifier \p key in the key slots.
|
|
|
|
*/
|
|
|
|
static psa_status_t psa_search_key_in_slots(
|
|
|
|
mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-07-31 11:26:37 +02:00
|
|
|
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
|
2020-10-15 11:17:11 +02:00
|
|
|
psa_key_slot_t *slot = NULL;
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2020-10-15 11:17:11 +02:00
|
|
|
psa_status_t status = psa_validate_key_id( key, 1, 1 );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2020-10-15 11:17:11 +02:00
|
|
|
if( psa_key_id_is_volatile( key_id ) )
|
|
|
|
{
|
|
|
|
slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
|
2020-07-31 11:26:37 +02:00
|
|
|
|
2020-10-15 11:17:11 +02:00
|
|
|
if( ! mbedtls_svc_key_id_equal( key, slot->attr.id ) )
|
|
|
|
status = PSA_ERROR_DOES_NOT_EXIST;
|
|
|
|
}
|
|
|
|
else
|
2020-07-31 11:26:37 +02:00
|
|
|
{
|
2020-10-15 11:17:11 +02:00
|
|
|
status = PSA_ERROR_DOES_NOT_EXIST;
|
|
|
|
slot = &global_data.key_slots[ PSA_KEY_SLOT_COUNT ];
|
|
|
|
|
|
|
|
while( slot > &global_data.key_slots[ 0 ] )
|
|
|
|
{
|
|
|
|
slot--;
|
|
|
|
if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
|
|
|
|
{
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 11:17:11 +02:00
|
|
|
if( status == PSA_SUCCESS )
|
2020-10-22 15:24:49 +02:00
|
|
|
{
|
2020-10-15 11:17:11 +02:00
|
|
|
*p_slot = slot;
|
2020-10-22 15:24:49 +02:00
|
|
|
psa_increment_key_slot_access_count( slot );
|
|
|
|
}
|
2020-10-15 11:17:11 +02:00
|
|
|
|
|
|
|
return( status );
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_initialize_key_slots( void )
|
|
|
|
{
|
|
|
|
/* Nothing to do: program startup and psa_wipe_all_key_slots() both
|
|
|
|
* guarantee that the key slots are initialized to all-zero, which
|
|
|
|
* means that all the key slots are in a valid, empty state. */
|
|
|
|
global_data.key_slots_initialized = 1;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
void psa_wipe_all_key_slots( void )
|
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
size_t slot_idx;
|
|
|
|
|
|
|
|
for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
|
2020-10-30 14:07:07 +01:00
|
|
|
slot->access_count = 1;
|
2018-12-10 16:29:04 +01:00
|
|
|
(void) psa_wipe_key_slot( slot );
|
|
|
|
}
|
|
|
|
global_data.key_slots_initialized = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-31 11:26:37 +02:00
|
|
|
psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
|
2020-07-17 14:13:26 +02:00
|
|
|
psa_key_slot_t **p_slot )
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
size_t slot_idx;
|
|
|
|
|
2019-05-27 19:01:54 +02:00
|
|
|
if( ! global_data.key_slots_initialized )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2020-07-24 16:33:11 +02:00
|
|
|
for( slot_idx = PSA_KEY_SLOT_COUNT; slot_idx > 0; slot_idx-- )
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
*p_slot = &global_data.key_slots[ slot_idx - 1 ];
|
2019-07-31 15:01:55 +02:00
|
|
|
if( ! psa_is_key_slot_occupied( *p_slot ) )
|
2020-07-17 14:13:26 +02:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
*volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
|
|
|
|
( (psa_key_id_t)slot_idx ) - 1;
|
2020-07-17 14:13:26 +02:00
|
|
|
|
2020-10-22 15:24:49 +02:00
|
|
|
psa_increment_key_slot_access_count( *p_slot );
|
|
|
|
|
2018-12-10 16:29:04 +01:00
|
|
|
return( PSA_SUCCESS );
|
2020-07-17 14:13:26 +02:00
|
|
|
}
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
2019-05-27 19:01:54 +02:00
|
|
|
*p_slot = NULL;
|
2018-12-10 16:29:04 +01:00
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:48:53 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2019-07-30 20:30:51 +02:00
|
|
|
static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
|
2018-12-10 16:48:53 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
|
|
|
uint8_t *key_data = NULL;
|
|
|
|
size_t key_data_length = 0;
|
|
|
|
|
2019-07-30 20:30:51 +02:00
|
|
|
status = psa_load_persistent_key( &slot->attr,
|
2019-07-23 11:58:03 +02:00
|
|
|
&key_data, &key_data_length );
|
2018-12-10 16:48:53 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-07-23 16:13:14 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-07-30 20:30:51 +02:00
|
|
|
if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
|
2019-07-23 16:13:14 +02:00
|
|
|
{
|
2019-07-30 21:32:04 +02:00
|
|
|
psa_se_key_data_storage_t *data;
|
|
|
|
if( key_data_length != sizeof( *data ) )
|
2019-07-23 16:13:14 +02:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-07-30 21:32:04 +02:00
|
|
|
data = (psa_se_key_data_storage_t *) key_data;
|
|
|
|
memcpy( &slot->data.se.slot_number, &data->slot_number,
|
|
|
|
sizeof( slot->data.se.slot_number ) );
|
2019-07-23 16:13:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
{
|
2020-10-23 11:37:05 +02:00
|
|
|
status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-07-23 16:13:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:48:53 +01:00
|
|
|
exit:
|
|
|
|
psa_free_persistent_key_data( key_data, key_data_length );
|
|
|
|
return( status );
|
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
|
|
|
|
|
|
|
psa_status_t psa_get_key_slot( mbedtls_svc_key_id_t key,
|
|
|
|
psa_key_slot_t **p_slot )
|
|
|
|
{
|
2020-10-15 11:17:11 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-07-31 11:26:37 +02:00
|
|
|
|
|
|
|
*p_slot = NULL;
|
|
|
|
if( ! global_data.key_slots_initialized )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2020-10-22 15:24:49 +02:00
|
|
|
/*
|
|
|
|
* On success, the pointer to the slot is passed directly to the caller
|
|
|
|
* thus no need to decrement the key slot access counter here.
|
|
|
|
*/
|
2020-10-15 11:17:11 +02:00
|
|
|
status = psa_search_key_in_slots( key, p_slot );
|
|
|
|
if( status != PSA_ERROR_DOES_NOT_EXIST )
|
2020-07-31 11:26:37 +02:00
|
|
|
return( status );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
|
|
|
psa_key_id_t volatile_key_id;
|
|
|
|
|
|
|
|
status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
(*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
|
|
|
|
(*p_slot)->attr.id = key;
|
|
|
|
|
|
|
|
status = psa_load_persistent_key_into_slot( *p_slot );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
psa_wipe_key_slot( *p_slot );
|
|
|
|
|
|
|
|
return( status );
|
|
|
|
#else
|
|
|
|
return( PSA_ERROR_DOES_NOT_EXIST );
|
2019-04-25 13:47:40 +02:00
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2018-12-10 16:48:53 +01:00
|
|
|
|
2020-07-31 11:26:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:24:49 +02:00
|
|
|
psa_status_t psa_decrement_key_slot_access_count( psa_key_slot_t *slot )
|
|
|
|
{
|
|
|
|
if( slot == NULL )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
|
|
|
if( slot->access_count > 0 )
|
|
|
|
{
|
|
|
|
slot->access_count--;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As the return error code may not be handled in case of multiple errors,
|
|
|
|
* do our best to report if the access counter is equal to zero: if
|
|
|
|
* available call MBEDTLS_PARAM_FAILED that may terminate execution (if
|
|
|
|
* called as part of the execution of a unit test suite this will stop the
|
|
|
|
* test suite execution) and if MBEDTLS_PARAM_FAILED does not terminate
|
|
|
|
* execution ouput an error message on standard error output.
|
|
|
|
*/
|
|
|
|
#ifdef MBEDTLS_CHECK_PARAMS
|
|
|
|
MBEDTLS_PARAM_FAILED( slot->access_count > 0 );
|
|
|
|
#endif
|
|
|
|
#ifdef MBEDTLS_PLATFORM_C
|
|
|
|
mbedtls_fprintf( stderr,
|
|
|
|
"\nFATAL psa_decrement_key_slot_access_count Decrementing a zero access counter.\n" );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( PSA_ERROR_CORRUPTION_DETECTED );
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:52:05 +02:00
|
|
|
psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
|
2020-06-08 18:37:19 +02:00
|
|
|
psa_se_drv_table_entry_t **p_drv )
|
2019-04-19 18:19:40 +02:00
|
|
|
{
|
2020-06-08 18:37:19 +02:00
|
|
|
if ( psa_key_lifetime_is_external( lifetime ) )
|
2019-06-26 18:34:38 +02:00
|
|
|
{
|
2020-06-08 18:37:19 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2020-06-08 18:54:23 +02:00
|
|
|
psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
|
|
|
|
if( driver == NULL )
|
2019-06-26 18:34:38 +02:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2020-06-08 18:37:19 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (p_drv != NULL)
|
2020-06-08 18:54:23 +02:00
|
|
|
*p_drv = driver;
|
2020-06-08 18:37:19 +02:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) p_drv;
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-06-26 18:34:38 +02:00
|
|
|
}
|
|
|
|
else
|
2020-06-08 18:37:19 +02:00
|
|
|
/* Local/internal keys are always valid */
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2019-04-25 13:47:40 +02:00
|
|
|
|
2020-07-17 16:11:30 +02:00
|
|
|
psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
|
2020-06-08 18:37:19 +02:00
|
|
|
{
|
|
|
|
if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
|
|
|
|
{
|
|
|
|
/* Volatile keys are always supported */
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Persistent keys require storage support */
|
2019-04-25 13:47:40 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2020-07-17 16:11:30 +02:00
|
|
|
return( PSA_SUCCESS );
|
2019-04-25 13:47:40 +02:00
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-06-08 18:37:19 +02:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2019-04-25 13:47:40 +02:00
|
|
|
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-06-08 18:37:19 +02:00
|
|
|
}
|
2019-04-19 18:19:40 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 19:01:50 +02:00
|
|
|
psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
|
2018-11-30 18:54:54 +01:00
|
|
|
{
|
2019-05-27 19:04:07 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2018-11-30 18:54:54 +01:00
|
|
|
psa_status_t status;
|
2019-05-27 19:01:54 +02:00
|
|
|
psa_key_slot_t *slot;
|
2018-11-30 18:54:54 +01:00
|
|
|
|
2020-07-31 11:26:37 +02:00
|
|
|
status = psa_get_key_slot( key, &slot );
|
2019-05-27 19:04:07 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-11-30 18:54:54 +01:00
|
|
|
{
|
2020-07-30 17:48:03 +02:00
|
|
|
*handle = PSA_KEY_HANDLE_INIT;
|
2020-07-31 11:26:37 +02:00
|
|
|
return( status );
|
2018-11-30 18:54:54 +01:00
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
|
|
|
|
*handle = key;
|
|
|
|
|
2020-10-22 15:24:49 +02:00
|
|
|
return( psa_decrement_key_slot_access_count( slot ) );
|
2019-05-27 19:04:07 +02:00
|
|
|
|
2019-04-25 13:47:40 +02:00
|
|
|
#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2020-07-23 12:30:41 +02:00
|
|
|
(void) key;
|
2020-07-30 17:48:03 +02:00
|
|
|
*handle = PSA_KEY_HANDLE_INIT;
|
2019-04-25 13:47:40 +02:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2018-11-30 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_close_key( psa_key_handle_t handle )
|
|
|
|
{
|
2019-05-27 19:01:54 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2020-09-01 10:51:51 +02:00
|
|
|
if( psa_key_handle_is_null( handle ) )
|
2019-10-08 15:48:25 +02:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
2020-10-16 16:07:03 +02:00
|
|
|
status = psa_search_key_in_slots( handle, &slot );
|
2019-05-27 19:01:54 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-10-29 17:51:10 +01:00
|
|
|
if( slot->access_count <= 1 )
|
|
|
|
return( psa_wipe_key_slot( slot ) );
|
|
|
|
else
|
|
|
|
return( psa_decrement_key_slot_access_count( slot ) );
|
2018-11-30 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 15:49:48 +02:00
|
|
|
psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2020-10-16 16:07:03 +02:00
|
|
|
status = psa_search_key_in_slots( key, &slot );
|
2020-08-04 15:49:48 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2020-10-29 17:51:10 +01:00
|
|
|
if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
|
|
|
|
( slot->access_count <= 1 ) )
|
|
|
|
return( psa_wipe_key_slot( slot ) );
|
|
|
|
else
|
2020-10-22 15:24:49 +02:00
|
|
|
return( psa_decrement_key_slot_access_count( slot ) );
|
2020-08-04 15:49:48 +02:00
|
|
|
}
|
|
|
|
|
2019-05-23 20:32:30 +02:00
|
|
|
void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
|
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
size_t slot_idx;
|
|
|
|
|
2019-05-23 20:32:30 +02:00
|
|
|
memset( stats, 0, sizeof( *stats ) );
|
2020-07-24 16:33:11 +02:00
|
|
|
|
|
|
|
for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
|
2019-05-23 20:32:30 +02:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
|
2020-10-30 11:54:03 +01:00
|
|
|
if( ! psa_is_key_slot_accessed( slot ) )
|
|
|
|
{
|
|
|
|
++stats->unaccessed_slots;
|
|
|
|
}
|
2019-07-31 15:01:55 +02:00
|
|
|
if( ! psa_is_key_slot_occupied( slot ) )
|
2019-05-23 20:32:30 +02:00
|
|
|
{
|
2019-07-31 15:01:55 +02:00
|
|
|
++stats->empty_slots;
|
2019-05-23 20:32:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-30 20:06:31 +02:00
|
|
|
if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
|
2019-05-23 20:32:30 +02:00
|
|
|
++stats->volatile_slots;
|
2019-07-30 20:06:31 +02:00
|
|
|
else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
|
2019-05-23 20:32:30 +02:00
|
|
|
{
|
2020-08-28 19:01:50 +02:00
|
|
|
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
|
2019-05-23 20:32:30 +02:00
|
|
|
++stats->persistent_slots;
|
2019-08-20 18:43:48 +02:00
|
|
|
if( id > stats->max_open_internal_key_id )
|
|
|
|
stats->max_open_internal_key_id = id;
|
2019-05-23 20:32:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-28 19:01:50 +02:00
|
|
|
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
|
2019-05-23 20:32:30 +02:00
|
|
|
++stats->external_slots;
|
2019-08-20 18:43:48 +02:00
|
|
|
if( id > stats->max_open_external_key_id )
|
|
|
|
stats->max_open_external_key_id = id;
|
2019-05-23 20:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:54:54 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|