Close or destroy keys explicitly in tests
diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function
index acc2f8c..22eec33 100644
--- a/tests/suites/test_suite_psa_crypto.function
+++ b/tests/suites/test_suite_psa_crypto.function
@@ -1291,6 +1291,7 @@
     psa_set_key_type( &attributes, type );
     status = psa_import_key( &attributes, p, length, &handle );
     TEST_EQUAL( status, expected_status );
+
     if( status == PSA_SUCCESS )
         PSA_ASSERT( psa_destroy_key( handle ) );
 
@@ -2100,6 +2101,9 @@
     TEST_EQUAL( psa_copy_key( source_handle,
                               &target_attributes, &target_handle ),
                 expected_status_arg );
+
+    PSA_ASSERT( psa_destroy_key( source_handle ) );
+
 exit:
     psa_reset_key_attributes( &source_attributes );
     psa_reset_key_attributes( &target_attributes );
@@ -2587,6 +2591,8 @@
                 PSA_ERROR_BAD_STATE );
     PSA_ASSERT( psa_mac_abort( &operation ) );
 
+    PSA_ASSERT( psa_destroy_key( handle ) );
+
 exit:
     PSA_DONE( );
 }
@@ -2908,6 +2914,8 @@
                 PSA_ERROR_BAD_STATE );
     PSA_ASSERT( psa_cipher_abort( &operation ) );
 
+    PSA_ASSERT( psa_destroy_key( handle ) );
+
 exit:
     PSA_DONE( );
 }
@@ -5016,6 +5024,7 @@
     }
 
     /* Shutdown and restart */
+    PSA_ASSERT( psa_close_key( handle ) );
     PSA_DONE();
     PSA_ASSERT( psa_crypto_init() );
 
diff --git a/tests/suites/test_suite_psa_crypto_persistent_key.function b/tests/suites/test_suite_psa_crypto_persistent_key.function
index e4ab163..53f6cb8 100644
--- a/tests/suites/test_suite_psa_crypto_persistent_key.function
+++ b/tests/suites/test_suite_psa_crypto_persistent_key.function
@@ -108,6 +108,9 @@
     TEST_EQUAL( psa_import_key( &attributes, data, data_length, &handle ),
                 expected_status );
 
+    if( expected_status == PSA_SUCCESS )
+        PSA_ASSERT( psa_destroy_key( handle ) );
+
 exit:
     mbedtls_free( data );
     PSA_DONE();
@@ -161,6 +164,8 @@
     PSA_ASSERT( psa_import_key( &attributes, second_data->x, second_data->len,
                                 &handle ) );
 
+    PSA_ASSERT( psa_destroy_key( handle ) );
+
 exit:
     PSA_DONE();
     psa_destroy_persistent_key( key_id );
@@ -206,6 +211,8 @@
     TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
     TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
 
+    PSA_ASSERT( psa_destroy_key( handle ) );
+
 exit:
     psa_reset_key_attributes( &attributes );
     psa_destroy_persistent_key( key_id );
diff --git a/tests/suites/test_suite_psa_crypto_slot_management.function b/tests/suites/test_suite_psa_crypto_slot_management.function
index fde3b4d..589d1ec 100644
--- a/tests/suites/test_suite_psa_crypto_slot_management.function
+++ b/tests/suites/test_suite_psa_crypto_slot_management.function
@@ -228,6 +228,7 @@
                                             &reexported_length ),
                             PSA_ERROR_NOT_PERMITTED );
             }
+            PSA_ASSERT( psa_close_key( handle ) );
             break;
         case CLOSE_BY_DESTROY:
             TEST_EQUAL( psa_open_key( id, &handle ),
@@ -302,6 +303,8 @@
     ASSERT_COMPARE( material1, sizeof( material1 ),
                     reexported, reexported_length );
 
+    PSA_ASSERT( psa_close_key( handle1 ) );
+
 exit:
     PSA_DONE( );
     psa_purge_key_storage( );
@@ -428,7 +431,7 @@
      * sure that the material is still alive. */
     if( target_lifetime != PSA_KEY_LIFETIME_VOLATILE )
     {
-        PSA_DONE( );
+        mbedtls_psa_crypto_free( );
         PSA_ASSERT( psa_crypto_init( ) );
         PSA_ASSERT( psa_open_key( target_id, &target_handle ) );
     }
@@ -463,6 +466,8 @@
                     PSA_ERROR_NOT_PERMITTED );
     }
 
+    PSA_ASSERT( psa_destroy_key( target_handle ) );
+
 exit:
     PSA_DONE( );
     mbedtls_free( export_buffer );
@@ -566,6 +571,10 @@
                         export_buffer, length );
     }
 
+    PSA_ASSERT( psa_destroy_key( source_handle ) );
+    if( target_handle != source_handle )
+        PSA_ASSERT( psa_destroy_key( target_handle ) );
+
 exit:
     PSA_DONE( );
     mbedtls_free( export_buffer );