psa: Do not reset a key slot under access
When psa_close/destroy/purge_key is called, do not
reset a key slot containing the description
of a persistent key if it is currently accessed.
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index 04a6514..1f69b55 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -1344,10 +1344,30 @@
if( mbedtls_svc_key_id_is_null( key ) )
return( PSA_SUCCESS );
+ /*
+ * Get the description of the key in a key slot. In case of a permanent
+ * key, this will load the key description from persistent memory if not
+ * done yet. We cannot avoid this loading as without it we don't know if
+ * the key is operated by an SE or not and this information is needed by
+ * the current implementation.
+ */
status = psa_get_key_slot( key, &slot );
if( status != PSA_SUCCESS )
return( status );
+ /*
+ * If the key slot containing the key description is under access by the
+ * library (apart from the present access), the key cannot be destroyed
+ * yet. For the time being, just return in error. Eventually (to be
+ * implemented), the key should be destroyed when all accesses have
+ * stopped.
+ */
+ if( slot->access_count > 1 )
+ {
+ psa_decrement_key_slot_access_count( slot );
+ return( PSA_ERROR_GENERIC_ERROR );
+ }
+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
driver = psa_get_se_driver_entry( slot->attr.lifetime );
if( driver != NULL )