New function mbedtls_test_read_mpi_core

Allocate and read an MPI from a binary test argument.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-09-20 21:38:33 +02:00
parent 571576fc5c
commit 3aae4e815e
2 changed files with 40 additions and 0 deletions

View file

@ -283,6 +283,28 @@ void mbedtls_test_err_add_check( int high, int low,
#endif
#if defined(MBEDTLS_BIGNUM_C)
/** Allocate and populate a core MPI from a test case argument.
*
* This function allocates exactly as many limbs as necessary to fit
* the length of the input. In other words, it preserves leading zeros.
*
* The limb array is allocated with mbedtls_calloc() and must later be
* freed with mbedtls_free().
*
* \param[in,out] pX The address where a pointer to the allocated limb
* array will be stored.
* \c *pX must be null on entry.
* On exit, \c *pX is null on error or if the number
* 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.
*
* \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 );
/** Read an MPI from a hexadecimal string.
*
* Like mbedtls_mpi_read_string(), but size the resulting bignum based

View file

@ -345,6 +345,24 @@ void mbedtls_test_err_add_check( int high, int low,
#endif /* MBEDTLS_TEST_HOOKS */
#if defined(MBEDTLS_BIGNUM_C)
#include "bignum_core.h"
int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
const data_t *input )
{
/* Sanity check */
if( *pX != NULL )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
*plimbs = ( input->len + sizeof( **pX ) - 1 ) / sizeof( **pX );
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 ) );
}
int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
{
/* mbedtls_mpi_read_string() currently retains leading zeros.