From 0c3752a46beda8be9ddb6134ae4d12cb2f4b6b15 Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Fri, 30 Oct 2020 11:54:03 +0100 Subject: [PATCH] psa: slot mgmt: Add unaccessed slots counter in stats Add a counter of unaccessed slots and use it in tests to check that at the end of PSA tests all key slot are unaccessed. Signed-off-by: Ronald Cron --- include/psa/crypto_extra.h | 2 ++ library/psa_crypto_slot_management.c | 4 ++++ tests/include/test/psa_crypto_helpers.h | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/include/psa/crypto_extra.h b/include/psa/crypto_extra.h index 71adb9355..7986eb23b 100644 --- a/include/psa/crypto_extra.h +++ b/include/psa/crypto_extra.h @@ -231,6 +231,8 @@ typedef struct mbedtls_psa_stats_s size_t cache_slots; /** Number of slots that are not used for anything. */ size_t empty_slots; + /** Number of slots that are not accessed. */ + size_t unaccessed_slots; /** Largest key id value among open keys in internal persistent storage. */ psa_key_id_t max_open_internal_key_id; /** Largest key id value among open keys in secure elements. */ diff --git a/library/psa_crypto_slot_management.c b/library/psa_crypto_slot_management.c index e2074774d..2fa0a0d54 100644 --- a/library/psa_crypto_slot_management.c +++ b/library/psa_crypto_slot_management.c @@ -408,6 +408,10 @@ void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ) for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ ) { const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ]; + if( ! psa_is_key_slot_accessed( slot ) ) + { + ++stats->unaccessed_slots; + } if( ! psa_is_key_slot_occupied( slot ) ) { ++stats->empty_slots; diff --git a/tests/include/test/psa_crypto_helpers.h b/tests/include/test/psa_crypto_helpers.h index c8013a1a8..214ee87f3 100644 --- a/tests/include/test/psa_crypto_helpers.h +++ b/tests/include/test/psa_crypto_helpers.h @@ -24,6 +24,7 @@ #include "test/psa_helpers.h" #include +#include static int test_helper_is_psa_pristine( int line, const char *file ) { @@ -40,6 +41,10 @@ static int test_helper_is_psa_pristine( int line, const char *file ) msg = "An external slot has not been closed properly."; else if( stats.half_filled_slots != 0 ) msg = "A half-filled slot has not been cleared properly."; + else if( stats.unaccessed_slots != PSA_KEY_SLOT_COUNT ) + { + msg = "Some slots are still marked as accessed."; + } /* If the test has already failed, don't overwrite the failure * information. Do keep the stats lookup above, because it can be