From 70fa89c1f97fe2472c7a324ff5fed00aa648cdcb Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 14 Aug 2024 05:12:45 +0200 Subject: [PATCH] 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 --- library/psa_crypto.c | 26 +++----------------------- library/psa_crypto_core.h | 1 - 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 696e830b8e..8c2c543d09 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -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( diff --git a/library/psa_crypto_core.h b/library/psa_crypto_core.h index a3c0fd6f19..7832001d8c 100644 --- a/library/psa_crypto_core.h +++ b/library/psa_crypto_core.h @@ -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;