diff --git a/library/psa_crypto.c b/library/psa_crypto.c index bbd6b24ed..49dd91546 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1849,6 +1849,8 @@ static psa_status_t psa_start_key_creation( } #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ + slot->status = PSA_SLOT_OCCUPIED; + return PSA_SUCCESS; } diff --git a/library/psa_crypto_core.h b/library/psa_crypto_core.h index d406ce459..46c57755e 100644 --- a/library/psa_crypto_core.h +++ b/library/psa_crypto_core.h @@ -33,12 +33,19 @@ */ int psa_can_do_hash(psa_algorithm_t hash_alg); +typedef enum { + PSA_SLOT_EMPTY = 0, + PSA_SLOT_OCCUPIED, +} psa_key_slot_status_t; + /** The data structure representing a key slot, containing key material * and metadata for one key. */ typedef struct { psa_core_key_attributes_t attr; + psa_key_slot_status_t status; + /* * Number of locks on the key slot held by the library. * @@ -88,7 +95,7 @@ typedef struct { */ static inline int psa_is_key_slot_occupied(const psa_key_slot_t *slot) { - return slot->attr.type != 0; + return slot->status == PSA_SLOT_OCCUPIED; } /** Test whether a key slot is locked. diff --git a/library/psa_crypto_slot_management.c b/library/psa_crypto_slot_management.c index 3b8a319cb..5ecc3a76c 100644 --- a/library/psa_crypto_slot_management.c +++ b/library/psa_crypto_slot_management.c @@ -237,11 +237,20 @@ static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot) data = (psa_se_key_data_storage_t *) key_data; status = psa_copy_key_material_into_slot( slot, data->slot_number, sizeof(data->slot_number)); + + if (status == PSA_SUCCESS) { + slot->status = PSA_SLOT_OCCUPIED; + } goto exit; } #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ status = psa_copy_key_material_into_slot(slot, key_data, key_data_length); + if (status != PSA_SUCCESS) { + goto exit; + } + + slot->status = PSA_SLOT_OCCUPIED; exit: psa_free_persistent_key_data(key_data, key_data_length); @@ -315,6 +324,7 @@ static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot) /* Copy actual key length and core attributes into the slot on success */ slot->key.bytes = key_buffer_length; slot->attr = attributes.core; + slot->status = PSA_SLOT_OCCUPIED; exit: if (status != PSA_SUCCESS) {