aboutsummaryrefslogtreecommitdiff
path: root/tests/src
diff options
context:
space:
mode:
authorGilles Peskine <Gilles.Peskine@arm.com>2022-10-09 21:14:09 +0200
committerGilles Peskine <Gilles.Peskine@arm.com>2022-10-10 23:03:47 +0200
commitc5772a194e9ec52c3060979de0f966b24083da22 (patch)
treea49ea7e3a878bccca3275042683801b4ae1a518f /tests/src
parent1b5c85c75b208e52c316ea0946bc61c74d8aaebd (diff)
downloadmbedtls-c5772a194e9ec52c3060979de0f966b24083da22.zip
mbedtls-c5772a194e9ec52c3060979de0f966b24083da22.tar.gz
mbedtls-c5772a194e9ec52c3060979de0f966b24083da22.tar.bz2
mbedtls_test_read_mpi_core: allow odd number of hex digits
Test functions must now take a char* argument rather than data_t*. This does not affect existing test data. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/helpers.c33
1 files changed, 30 insertions, 3 deletions
diff --git a/tests/src/helpers.c b/tests/src/helpers.c
index 557c13c..b7c8364 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 )