test_suite_pk.function: Do not use MD_MAX_SIZE

In order to for tests to pass from the previous commit (which it mandatory for all pk verify/sign
functions to be given a hash_len that is exactly equal to the message digest length of md_alg) the
hash_len that is supplied to the fucntion cannot be MBEDTLS_MD_MAX_SIZE. This would result in all tests failing. Since the md alg for all of these funtions are SHA256, we can use mbedtls functions to get
the required length of a SHA256 digest (32 bytes). Then that number can be used for allocating the
hash buffer.

Signed-off-by: Nick Child <nick.child@ibm.com>
This commit is contained in:
Nick Child 2021-06-17 11:59:29 -04:00
parent b6d5693be2
commit 8930e14f3a

View File

@ -883,8 +883,9 @@ exit:
void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret ) void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret )
{ {
mbedtls_pk_context pk; mbedtls_pk_context pk;
size_t sig_len; size_t sig_len, hash_len;
unsigned char hash[MBEDTLS_MD_MAX_SIZE]; mbedtls_md_type_t md = MBEDTLS_MD_SHA256;
unsigned char *hash = NULL;
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
void *rs_ctx = NULL; void *rs_ctx = NULL;
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
@ -898,40 +899,43 @@ void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret )
mbedtls_ecp_set_max_ops( 42000 ); mbedtls_ecp_set_max_ops( 42000 );
#endif #endif
hash_len = mbedtls_md_get_size( mbedtls_md_info_from_type( md ) );
ASSERT_ALLOC( hash, hash_len );
mbedtls_pk_init( &pk ); mbedtls_pk_init( &pk );
USE_PSA_INIT( ); USE_PSA_INIT( );
memset( hash, 0x2a, sizeof hash ); memset( hash, 0x2a, hash_len );
memset( sig, 0, sizeof sig ); memset( sig, 0, sizeof sig );
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 ); TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 );
TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, md,
hash, sizeof hash, sig, &sig_len, hash, hash_len, sig, &sig_len,
mbedtls_test_rnd_std_rand, NULL, rs_ctx ) == sign_ret ); mbedtls_test_rnd_std_rand, NULL, rs_ctx ) == sign_ret );
if( sign_ret == 0 ) if( sign_ret == 0 )
TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE ); TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE );
else else
sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE; sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_verify( &pk, md,
hash, sizeof hash, sig, sig_len ) == verify_ret ); hash, hash_len, sig, sig_len ) == verify_ret );
if( verify_ret == 0 ) if( verify_ret == 0 )
{ {
hash[0]++; hash[0]++;
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_verify( &pk, md,
hash, sizeof hash, sig, sig_len ) != 0 ); hash, hash_len, sig, sig_len ) != 0 );
hash[0]--; hash[0]--;
sig[0]++; sig[0]++;
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_verify( &pk, md,
hash, sizeof hash, sig, sig_len ) != 0 ); hash, hash_len, sig, sig_len ) != 0 );
sig[0]--; sig[0]--;
} }
TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, sizeof hash, TEST_ASSERT( mbedtls_pk_sign( &pk, md, hash, hash_len,
sig, &sig_len, sig, &sig_len,
mbedtls_test_rnd_std_rand, mbedtls_test_rnd_std_rand,
NULL ) == sign_ret ); NULL ) == sign_ret );
@ -940,19 +944,19 @@ void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret )
else else
sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE; sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, md,
hash, sizeof hash, sig, sig_len, rs_ctx ) == verify_ret ); hash, hash_len, sig, sig_len, rs_ctx ) == verify_ret );
if( verify_ret == 0 ) if( verify_ret == 0 )
{ {
hash[0]++; hash[0]++;
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, md,
hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 ); hash, hash_len, sig, sig_len, rs_ctx ) != 0 );
hash[0]--; hash[0]--;
sig[0]++; sig[0]++;
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, md,
hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 ); hash, hash_len, sig, sig_len, rs_ctx ) != 0 );
sig[0]--; sig[0]--;
} }
@ -961,6 +965,7 @@ exit:
mbedtls_pk_restart_free( rs_ctx ); mbedtls_pk_restart_free( rs_ctx );
#endif #endif
mbedtls_pk_free( &pk ); mbedtls_pk_free( &pk );
mbedtls_free( hash );
USE_PSA_DONE( ); USE_PSA_DONE( );
} }
/* END_CASE */ /* END_CASE */