Add tests for big endian core I/O

The test case where there were extra limbs in the MPI failed and this
commit contains the corresponding fix as well. (We used to use the
minimum required limbs instead of the actual limbs present.)

Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2022-07-21 09:29:32 +01:00
parent ba5c139e4c
commit f1d617deb8
3 changed files with 123 additions and 2 deletions

View file

@ -347,8 +347,8 @@ int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
size_t buflen )
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t const limbs = CHARS_TO_LIMBS( buflen );
size_t const overhead = ( limbs * ciL ) - buflen;
size_t const limbs = CHARS_TO_LIMBS( buflen );
size_t overhead;
unsigned char *Xp;
MPI_VALIDATE_RET( X != NULL );
@ -357,6 +357,8 @@ int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
/* Ensure that target MPI has at least the necessary number of limbs */
MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
overhead = ( nx * ciL ) - buflen;
/* Avoid calling `memcpy` with NULL source or destination argument,
* even if buflen is 0. */
if( buflen != 0 )

View file

@ -82,6 +82,63 @@ mpi_read_write_string:16:"":2:"0":4:0:0
Test mpi_write_string #10 (Negative hex with odd number of digits)
mpi_read_write_string:16:"-1":16:"":3:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
Test mbedtls_mpi_core_io_be #1 (Buffer and limbs just fit, input limb-aligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:12:0:0
Test mbedtls_mpi_core_io_be #2 (Buffer and limbs just fit, input unaligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:12:0:0
Test mbedtls_mpi_core_io_be #3 (Buffer just fits, extra limbs, input limb-aligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:14:0:0
Test mbedtls_mpi_core_io_be #4 (Buffer just fits, extra limbs, input unaligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:14:0:0
Test mbedtls_mpi_core_io_be #5 (Extra limbs, buffer aligned to extra limbs, input limb-aligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":112:14:0:0
Test mbedtls_mpi_core_io_be #6 (Extra limbs, buffer aligned to extra limbs, input unaligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":112:14:0:0
Test mbedtls_mpi_core_io_be #7 (Buffer and limbs just fit, input limb-aligned with leading zeroes)
mbedtls_mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":88:12:0:0
Test mbedtls_mpi_core_io_be #8 (Buffer and limbs just fit, input unaligned with leading zeroes)
mbedtls_mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":86:12:0:0
Test mbedtls_mpi_core_io_be #9 (Buffer just fits, extra limbs, input limb-aligned with leading zeroes)
mbedtls_mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":88:14:0:0
Test mbedtls_mpi_core_io_be #10 (Buffer just fits, extra limbs, input unaligned with leading zeroes)
mbedtls_mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":86:14:0:0
Test mbedtls_mpi_core_io_be #11 (Zero)
mbedtls_mpi_core_io_be:"00":1:1:0:0
Test mbedtls_mpi_core_io_be #12 (Zero, empty output)
mbedtls_mpi_core_io_be:"00":0:1:0:0
Test mbedtls_mpi_core_io_be #13 (Zero, empty input)
mbedtls_mpi_core_io_be:"":1:1:0:0
Test mbedtls_mpi_core_io_be #14 (One)
mbedtls_mpi_core_io_be:"01":1:1:0:0
Test mbedtls_mpi_core_io_be #14 (One limb)
mbedtls_mpi_core_io_be:"ff00000000000000":8:1:0:0
Test mbedtls_mpi_core_io_be #15 (not enough limbs, input limb-aligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:11:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:0
Test mbedtls_mpi_core_io_be #16 (not enough limbs, input unaligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:11:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:0
Test mbedtls_mpi_core_io_be #17 (buffer too small, input limb-aligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":95:12:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
Test mbedtls_mpi_core_io_be #18 (buffer too small, input unaligned)
mbedtls_mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":93:12:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
Base test mbedtls_mpi_read_binary #1
mbedtls_mpi_read_binary:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":"0941379D00FED1491FE15DF284DFDE4A142F68AA8D412023195CEE66883E6290FFE703F4EA5963BF212713CEE46B107C09182B5EDCD955ADAC418BF4918E2889AF48E1099D513830CEC85C26AC1E158B52620E33BA8692F893EFBB2F958B4424"

View file

@ -1,6 +1,7 @@
/* BEGIN_HEADER */
#include "mbedtls/bignum.h"
#include "mbedtls/entropy.h"
#include "bignum_core.h"
#if MBEDTLS_MPI_MAX_BITS > 792
#define MPI_MAX_BITS_LARGER_THAN_792
@ -196,6 +197,67 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_mpi_core_io_be( data_t *input, int nb_int, int nx_64_int, int iret,
int oret )
{
#define BMAX 1024
unsigned char buf[BMAX];
#define XMAX BMAX / sizeof( mbedtls_mpi_uint )
mbedtls_mpi_uint X[XMAX];
size_t nx, nb;
int ret;
if( iret != 0 )
TEST_ASSERT( oret == 0 );
TEST_ASSERT( 0 <= nb_int );
nb = nb_int;
TEST_ASSERT( nb <= BMAX );
TEST_ASSERT( 0 <= nx_64_int );
nx = nx_64_int;
/* nx_64_int is the number of 64 bit limbs, if we have 32 bit limbs we need
* to double the number of limbs to have the same size. */
if( sizeof( mbedtls_mpi_uint ) == 4 )
nx *= 2;
TEST_ASSERT( nx <= XMAX );
ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
TEST_ASSERT( ret == iret );
if( iret == 0 )
{
ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
TEST_ASSERT( ret == oret );
}
if( ( iret == 0 ) && ( oret == 0 ) )
{
if( nb > input->len )
{
size_t leading_zeroes = nb - input->len;
TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
for( size_t i = 0; i < leading_zeroes; i++ )
TEST_ASSERT( buf[i] == 0 );
}
else
{
size_t leading_zeroes = input->len - nb;
TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
for( size_t i = 0; i < leading_zeroes; i++ )
TEST_ASSERT( input->x[i] == 0 );
}
}
exit:
;
#undef BMAX
#undef XMAX
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_mpi_read_binary_le( data_t * buf, char * input_A )
{