diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h index fe3b787fd..6ec967e18 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -298,12 +298,13 @@ void mbedtls_test_err_add_check( int high, int low, * of limbs is 0. * \param[out] plimbs The address where the number of limbs will be stored. * \param[in] input The test argument to read. - * It is interpreted as a big-endian integer in base 256. + * It is interpreted as a hexadecimal representation + * of a non-negative integer. * * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise. */ int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs, - const data_t *input ); + const char *input ); /** Read an MPI from a hexadecimal string. * diff --git a/tests/src/helpers.c b/tests/src/helpers.c index 557c13c33..b7c83646c 100644 --- a/tests/src/helpers.c +++ b/tests/src/helpers.c @@ -348,19 +348,46 @@ void mbedtls_test_err_add_check( int high, int low, #include "bignum_core.h" int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs, - const data_t *input ) + const char *input ) { /* Sanity check */ if( *pX != NULL ) return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); - *plimbs = CHARS_TO_LIMBS( input->len ); + size_t hex_len = strlen( input ); + size_t byte_len = ( hex_len + 1 ) / 2; + *plimbs = CHARS_TO_LIMBS( byte_len ); if( *plimbs == 0 ) return( 0 ); + *pX = mbedtls_calloc( *plimbs, sizeof( **pX ) ); if( *pX == NULL ) return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); - return( mbedtls_mpi_core_read_be( *pX, *plimbs, input->x, input->len ) ); + + unsigned char *byte_start = ( unsigned char * ) *pX; + if( byte_len % sizeof( mbedtls_mpi_uint ) != 0 ) + { + byte_start += sizeof( mbedtls_mpi_uint ) - byte_len % sizeof( mbedtls_mpi_uint ); + } + if( ( hex_len & 1 ) != 0 ) + { + /* mbedtls_test_unhexify wants an even number of hex digits */ + TEST_ASSERT( ascii2uc( *input, byte_start ) == 0 ); + ++byte_start; + ++input; + --byte_len; + } + TEST_ASSERT( mbedtls_test_unhexify( byte_start, + byte_len, + input, + &byte_len ) == 0 ); + + mbedtls_mpi_core_bigendian_to_host( *pX, *plimbs ); + return( 0 ); + +exit: + mbedtls_free( *pX ); + return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); } int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s ) diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index be4c056d7..2a1e48f2e 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -595,13 +595,13 @@ Test mbedtls_mpi_cmp_mpi: large negative < 0 (1 limb) mpi_cmp_mpi:"-1230000000000000000":"0":-1 mbedtls_mpi_core_lt_ct: x=y (1 limb) -mpi_core_lt_ct:"02B5":"02B5":0 +mpi_core_lt_ct:"2B5":"2B5":0 mbedtls_mpi_core_lt_ct: x>y (1 limb) -mpi_core_lt_ct:"02B5":"02B4":0 +mpi_core_lt_ct:"2B5":"2B4":0 mbedtls_mpi_core_lt_ct: xy, equal MS limbs mpi_core_lt_ct:"EEFFFFFFFFFFFFFFFF":"EEFFFFFFFFFFFFFFF1":0 diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index d4501972d..2528b9cba 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -728,7 +728,7 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void mpi_core_lt_ct( data_t * input_X, data_t * input_Y, int exp_ret ) +void mpi_core_lt_ct( char *input_X, char *input_Y, int exp_ret ) { mbedtls_mpi_uint *X = NULL; size_t X_limbs;