From 02b750781f64f04c9c23f06245f39535ac4607bb Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sun, 1 Jul 2018 22:31:34 +0200 Subject: [PATCH] Factor duplicated code into exercise_key Also fail the test if the test code lacks a way to exercise the key. --- tests/suites/test_suite_psa_crypto.function | 54 +++++++++++++-------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 5f705e3e3..1017e88c8 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -345,6 +345,36 @@ static int exercise_asymmetric_encryption_key( psa_key_slot_t key, exit: return( 0 ); } + +static int exercise_key( psa_key_slot_t slot, + psa_key_usage_t usage, + psa_algorithm_t alg ) +{ + int ok; + if( alg == 0 ) + ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ + else if( PSA_ALG_IS_MAC( alg ) ) + ok = exercise_mac_key( slot, usage, alg ); + else if( PSA_ALG_IS_CIPHER( alg ) ) + ok = exercise_cipher_key( slot, usage, alg ); + else if( PSA_ALG_IS_AEAD( alg ) ) + ok = exercise_aead_key( slot, usage, alg ); + else if( PSA_ALG_IS_SIGN( alg ) ) + ok = exercise_signature_key( slot, usage, alg ); + else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) + ok = exercise_asymmetric_encryption_key( slot, usage, alg ); + else + { + char message[40]; + mbedtls_snprintf( message, sizeof( message ), + "No code to exercise alg=0x%08lx", + (unsigned long) alg ); + test_fail( message, __LINE__, __FILE__ ); + ok = 0; + } + return( ok ); +} + /* END_HEADER */ /* BEGIN_DEPENDENCIES @@ -640,16 +670,8 @@ void import_and_exercise_key( data_t *data, TEST_ASSERT( got_bits == bits ); /* Do something with the key according to its type and permitted usage. */ - if( PSA_ALG_IS_MAC( alg ) ) - exercise_mac_key( slot, usage, alg ); - else if( PSA_ALG_IS_CIPHER( alg ) ) - exercise_cipher_key( slot, usage, alg ); - else if( PSA_ALG_IS_AEAD( alg ) ) - exercise_aead_key( slot, usage, alg ); - else if( PSA_ALG_IS_SIGN( alg ) ) - exercise_signature_key( slot, usage, alg ); - else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) - exercise_asymmetric_encryption_key( slot, usage, alg ); + if( ! exercise_key( slot, usage, alg ) ) + goto exit; exit: psa_destroy_key( slot ); @@ -2260,16 +2282,8 @@ void generate_key( int type_arg, } /* Do something with the key according to its type and permitted usage. */ - if( PSA_ALG_IS_MAC( alg ) ) - exercise_mac_key( slot, usage, alg ); - else if( PSA_ALG_IS_CIPHER( alg ) ) - exercise_cipher_key( slot, usage, alg ); - else if( PSA_ALG_IS_AEAD( alg ) ) - exercise_aead_key( slot, usage, alg ); - else if( PSA_ALG_IS_SIGN( alg ) ) - exercise_signature_key( slot, usage, alg ); - else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) - exercise_asymmetric_encryption_key( slot, usage, alg ); + if( ! exercise_key( slot, usage, alg ) ) + goto exit; exit: psa_destroy_key( slot );