From ec93d30b45dbf7a5a36fe907f1d4e37ddcd0c248 Mon Sep 17 00:00:00 2001 From: itayzafrir Date: Thu, 18 Oct 2018 18:01:10 +0300 Subject: [PATCH] Add hash bad paths test Increase code coverage --- tests/suites/test_suite_psa_crypto.data | 4 ++ tests/suites/test_suite_psa_crypto.function | 56 +++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 43d243e1b5..0eb06e4367 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -428,6 +428,10 @@ PSA hash verify: RIPEMD160 depends_on:MBEDTLS_RIPEMD160_C hash_verify:PSA_ALG_RIPEMD160:"bd":"5089265ee5d9af75d12dbf7ea2f27dbdee435b37" +PSA hash: bad paths +depends_on:MBEDTLS_SHA256_C +hash_bad_paths: + PSA MAC setup: good, HMAC-SHA-256 depends_on:MBEDTLS_MD_C:MBEDTLS_SHA256_C mac_setup:PSA_KEY_TYPE_HMAC:"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 63d837fdca..4a05adf8c9 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -1628,6 +1628,62 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void hash_bad_paths( ) +{ + psa_algorithm_t alg = PSA_ALG_SHA_256; + unsigned char hash[PSA_HASH_MAX_SIZE] = { 0 }; + size_t expected_size = PSA_HASH_SIZE( alg ); + unsigned char input[] = "input"; + psa_hash_operation_t operation; + size_t hash_len; + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + /* psa_hash_update without calling psa_hash_setup beforehand */ + memset( &operation, 0, sizeof( operation ) ); + TEST_ASSERT( psa_hash_update( &operation, + input, sizeof( input ) ) == + PSA_ERROR_INVALID_ARGUMENT ); + + /* psa_hash_finish without calling psa_hash_setup beforehand */ + memset( &operation, 0, sizeof( operation ) ); + TEST_ASSERT( psa_hash_finish( &operation, + hash, expected_size, + &hash_len ) == PSA_ERROR_INVALID_ARGUMENT ); + + /* psa_hash_verify without calling psa_hash_setup beforehand */ + memset( &operation, 0, sizeof( operation ) ); + TEST_ASSERT( psa_hash_verify( &operation, + hash, expected_size ) == + PSA_ERROR_INVALID_ARGUMENT ); + + /* psa_hash_finish with a smaller hash buffer than expected */ + TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); + TEST_ASSERT( psa_hash_finish( &operation, + hash, expected_size - 1, + &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL ); + + + /* psa_hash_verify with a smaller hash buffer than expected */ + TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); + TEST_ASSERT( psa_hash_verify( &operation, + hash, expected_size - 1 ) == + PSA_ERROR_INVALID_SIGNATURE ); + + /* psa_hash_verify with a non-matching hash buffer */ + TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); + TEST_ASSERT( psa_hash_update( &operation, + input, sizeof( input ) ) == PSA_SUCCESS ); + TEST_ASSERT( psa_hash_verify( &operation, + hash, expected_size ) == + PSA_ERROR_INVALID_SIGNATURE ); + +exit: + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + /* BEGIN_CASE */ void mac_setup( int key_type_arg, data_t *key,