From bbc4b8d2be613ac6eb7c5ed4f629e6bf12ca2afd Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 11 Jun 2021 14:13:53 +0200 Subject: [PATCH] New test helper mbedtls_test_read_mpi This test helper reads an MPI from a string and guarantees control over the number of limbs of the MPI, allowing test cases to construct values with or without leading zeros, including 0 with 0 limbs. Signed-off-by: Gilles Peskine --- tests/suites/helpers.function | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index ae207fd042..5a8e21148a 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -46,6 +46,10 @@ typedef UINT32 uint32_t; #include #endif +#if defined(MBEDTLS_BIGNUM_C) +#include "mbedtls/bignum.h" +#endif + #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \ defined(MBEDTLS_TEST_HOOKS) #include "mbedtls/threading.h" @@ -847,6 +851,21 @@ int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_le return ret; } +#if defined(MBEDTLS_BIGNUM_C) +int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s ) +{ + /* mbedtls_mpi_read_string() currently retains leading zeros. + * It always allocates at least one limb for the value 0. */ + if( s[0] == 0 ) + { + mbedtls_mpi_free( X ); + return( 0 ); + } + else + return( mbedtls_mpi_read_string( X, radix, s ) ); +} +#endif /* MBEDTLS_BIGNUM_C */ + #if defined(MBEDTLS_TEST_MUTEX_USAGE) /** Mutex usage verification framework. *