mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-14 01:26:49 +00:00
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 <Gilles.Peskine@arm.com>
This commit is contained in:
parent
5fb6f700ba
commit
ebc49e5bff
@ -59,6 +59,10 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C)
|
||||
#include "mbedtls/bignum.h"
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MBEDTLS_TEST_RESULT_SUCCESS = 0,
|
||||
@ -210,4 +214,25 @@ void mbedtls_test_err_add_check( int high, int low,
|
||||
const char *file, int line);
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C)
|
||||
/** Read an MPI from a string.
|
||||
*
|
||||
* Like mbedtls_mpi_read_string(), but size the resulting bignum based
|
||||
* on the number of digits in the string. In particular, construct a
|
||||
* bignum with 0 limbs for an empty string, and a bignum with leading 0
|
||||
* limbs if the string has sufficiently many leading 0 digits.
|
||||
*
|
||||
* This is important so that the "0 (null)" and "0 (1 limb)" and
|
||||
* "leading zeros" test cases do what they claim.
|
||||
*
|
||||
* \param[out] X The MPI object to populate. It must be initialized.
|
||||
* \param radix The radix (2 to 16).
|
||||
* \param[in] s The null-terminated string to read from.
|
||||
*
|
||||
* \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
|
||||
*/
|
||||
/* Since the library has exactly the desired behavior, this is trivial. */
|
||||
int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s );
|
||||
#endif /* MBEDTLS_BIGNUM_C */
|
||||
|
||||
#endif /* TEST_HELPERS_H */
|
||||
|
@ -259,3 +259,18 @@ void mbedtls_test_err_add_check( int high, int low,
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_TEST_HOOKS */
|
||||
|
||||
#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
|
||||
|
Loading…
x
Reference in New Issue
Block a user