psa-core: remove unnecessary element in psa_key_slot_t

Instead of checking for "in_use" to be true/false or "key.data"
to be not NULL, simply check that "key.bytes" is 0/not-0.
psa_allocate_buffer_to_slot() will update this value whenever
a new slot is allocated (for the fully static case "allocated"
actually mean "taken").

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit is contained in:
Valerio Setti 2024-08-14 05:12:45 +02:00
parent faed169e57
commit 70fa89c1f9
2 changed files with 3 additions and 24 deletions

View File

@ -706,15 +706,9 @@ psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
size_t buffer_length)
{
#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
if (slot->key.in_use) {
return PSA_ERROR_ALREADY_EXISTS;
}
if (buffer_length > ((size_t) MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)) {
return PSA_ERROR_NOT_SUPPORTED;
}
slot->key.in_use = 1;
#else
if (slot->key.data != NULL) {
return PSA_ERROR_ALREADY_EXISTS;
@ -1189,9 +1183,7 @@ static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
{
#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
slot->key.in_use = 0;
#else /* MBEDTLS_PSA_STATIC_KEY_SLOTS */
#if !defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
if (slot->key.data != NULL) {
mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
}
@ -2113,13 +2105,7 @@ psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
* storage ( thus not in the case of importing a key in a secure element
* with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
* buffer to hold the imported key material. */
#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
int is_slot_unused = (slot->key.in_use == 0);
#else
int is_slot_unused = (slot->key.data == NULL);
#endif
if (is_slot_unused) {
if (slot->key.bytes == 0) {
if (psa_key_lifetime_is_external(attributes->lifetime)) {
status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
attributes, data, data_length, &storage_size);
@ -8036,13 +8022,7 @@ psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
* storage ( thus not in the case of generating a key in a secure element
* with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
* buffer to hold the generated key material. */
#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
int is_slot_unused = (slot->key.in_use == 0);
#else
int is_slot_unused = (slot->key.data == NULL);
#endif
if (is_slot_unused) {
if (slot->key.bytes == 0) {
if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
PSA_KEY_LOCATION_LOCAL_STORAGE) {
status = psa_validate_key_type_and_size_for_key_generation(

View File

@ -165,7 +165,6 @@ typedef struct {
* Format as specified in psa_export_key(). */
struct key_data {
#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
int in_use;
uint8_t data[MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE];
#else /* MBEDTLS_PSA_STATIC_KEY_SLOTS */
uint8_t *data;