Merge pull request #6398 from gilles-peskine-arm/bignum-mbedtls_test_read_mpi_core-char

mbedtls_test_read_mpi_core: support an odd number of hex digits
This commit is contained in:
Janos Follath 2022-10-11 15:08:06 +01:00 committed by GitHub
commit 301e866f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 18 deletions

View File

@ -298,12 +298,13 @@ void mbedtls_test_err_add_check( int high, int low,
* of limbs is 0. * of limbs is 0.
* \param[out] plimbs The address where the number of limbs will be stored. * \param[out] plimbs The address where the number of limbs will be stored.
* \param[in] input The test argument to read. * \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. * \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, 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. /** Read an MPI from a hexadecimal string.
* *

View File

@ -348,19 +348,46 @@ void mbedtls_test_err_add_check( int high, int low,
#include "bignum_core.h" #include "bignum_core.h"
int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs, int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
const data_t *input ) const char *input )
{ {
/* Sanity check */ /* Sanity check */
if( *pX != NULL ) if( *pX != NULL )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); 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 ) if( *plimbs == 0 )
return( 0 ); return( 0 );
*pX = mbedtls_calloc( *plimbs, sizeof( **pX ) ); *pX = mbedtls_calloc( *plimbs, sizeof( **pX ) );
if( *pX == NULL ) if( *pX == NULL )
return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); 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 ) int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )

View File

@ -441,30 +441,66 @@ mpi_lsb:"24":2
Base test mbedtls_mpi_lsb #4 Base test mbedtls_mpi_lsb #4
mpi_lsb:"2000":13 mpi_lsb:"2000":13
Base test mbedtls_mpi_bitlen #1 Test mbedtls_mpi_core_bitlen 764-bit
mpi_core_bitlen:"941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":764
Test mbedtls_mpi_core_bitlen 0x18
mpi_core_bitlen:"18":5
Test mbedtls_mpi_core_bitlen 0x18 with leading 0 limb(s)
mpi_core_bitlen:"00000000000000018":5
Test mbedtls_mpi_core_bitlen 0x18 << 64
mpi_core_bitlen:"180000000000000000":69
Test mbedtls_mpi_core_bitlen 0x01
mpi_core_bitlen:"1":1
Test mbedtls_mpi_core_bitlen 0x0f
mpi_core_bitlen:"f":4
Test mbedtls_mpi_core_bitlen 0x10
mpi_core_bitlen:"10":5
Test mbedtls_mpi_core_bitlen 0x0a
mpi_core_bitlen:"a":4
Test mbedtls_mpi_core_bitlen: 0 (1 limb)
mpi_core_bitlen:"0":0
Test mbedtls_mpi_bitlen 764-bit
mpi_bitlen:"941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":764 mpi_bitlen:"941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":764
Base test mbedtls_mpi_bitlen #2 Test mbedtls_mpi_bitlen 0x18
mpi_bitlen:"18":5 mpi_bitlen:"18":5
Base test mbedtls_mpi_bitlen #3 Test mbedtls_mpi_bitlen 0x18 with leading 0 limb(s)
mpi_bitlen:"00000000000000018":5
Test mbedtls_mpi_bitlen 0x18 << 64
mpi_bitlen:"180000000000000000":69
Test mbedtls_mpi_bitlen 0x01
mpi_bitlen:"1":1 mpi_bitlen:"1":1
Base test mbedtls_mpi_bitlen #4 Test mbedtls_mpi_bitlen 0x0f
mpi_bitlen:"f":4 mpi_bitlen:"f":4
Base test mbedtls_mpi_bitlen #5 Test mbedtls_mpi_bitlen 0x10
mpi_bitlen:"10":5 mpi_bitlen:"10":5
Base test mbedtls_mpi_bitlen #6 Test mbedtls_mpi_bitlen 0x0a
mpi_bitlen:"a":4 mpi_bitlen:"a":4
Base test mbedtls_mpi_bitlen: 0 (null) Test mbedtls_mpi_bitlen: 0 (null)
mpi_bitlen:"":0 mpi_bitlen:"":0
Base test mbedtls_mpi_bitlen: 0 (1 limb) Test mbedtls_mpi_bitlen: 0 (1 limb)
mpi_bitlen:"0":0 mpi_bitlen:"0":0
Test mbedtls_mpi_bitlen: -0x18
mpi_bitlen:"-18":5
Base test mbedtls_mpi_cmp_int #1 Base test mbedtls_mpi_cmp_int #1
mpi_cmp_int:693:693:0 mpi_cmp_int:693:693:0
@ -595,13 +631,13 @@ Test mbedtls_mpi_cmp_mpi: large negative < 0 (1 limb)
mpi_cmp_mpi:"-1230000000000000000":"0":-1 mpi_cmp_mpi:"-1230000000000000000":"0":-1
mbedtls_mpi_core_lt_ct: x=y (1 limb) 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) 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: x<y (1 limb) mbedtls_mpi_core_lt_ct: x<y (1 limb)
mpi_core_lt_ct:"02B5":"02B6":1 mpi_core_lt_ct:"2B5":"2B6":1
mbedtls_mpi_core_lt_ct: x=y (0 limbs) mbedtls_mpi_core_lt_ct: x=y (0 limbs)
mpi_core_lt_ct:"":"":0 mpi_core_lt_ct:"":"":0
@ -667,7 +703,7 @@ mbedtls_mpi_core_lt_ct: x<y (32 bit y, first bytes equal)
mpi_core_lt_ct:"000000FF":"FFFFFFFF":1 mpi_core_lt_ct:"000000FF":"FFFFFFFF":1
mbedtls_mpi_core_lt_ct: x<y, zero vs non-zero MS limb mbedtls_mpi_core_lt_ct: x<y, zero vs non-zero MS limb
mpi_core_lt_ct:"00FFFFFFFFFFFFFFFF":"01FFFFFFFFFFFFFFFF":1 mpi_core_lt_ct:"0FFFFFFFFFFFFFFFF":"1FFFFFFFFFFFFFFFF":1
mbedtls_mpi_core_lt_ct: x>y, equal MS limbs mbedtls_mpi_core_lt_ct: x>y, equal MS limbs
mpi_core_lt_ct:"EEFFFFFFFFFFFFFFFF":"EEFFFFFFFFFFFFFFF1":0 mpi_core_lt_ct:"EEFFFFFFFFFFFFFFFF":"EEFFFFFFFFFFFFFFF1":0

View File

@ -664,6 +664,20 @@ exit:
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */
void mpi_core_bitlen( char *input_X, int nr_bits )
{
mbedtls_mpi_uint *X = NULL;
size_t limbs;
TEST_EQUAL( mbedtls_test_read_mpi_core( &X, &limbs, input_X ), 0 );
TEST_EQUAL( mbedtls_mpi_core_bitlen( X, limbs ), nr_bits );
exit:
mbedtls_free( X );
}
/* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mpi_bitlen( char * input_X, int nr_bits ) void mpi_bitlen( char * input_X, int nr_bits )
{ {
@ -728,7 +742,7 @@ exit:
/* END_CASE */ /* END_CASE */
/* BEGIN_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; mbedtls_mpi_uint *X = NULL;
size_t X_limbs; size_t X_limbs;