Inline mpi_core_clear()

This used to resize MPIs in the legacy interface, which is not
needed/possible as the new interface has fixed size MPIs.

Inlining this function makes the code easier to read and maintain, while
there is no obvious drawback to it.

Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2022-08-11 16:13:53 +01:00
parent 56a10f97ba
commit 2ab2d3e3e9

View file

@ -75,20 +75,6 @@ size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
return( ( i * biL ) + j ); return( ( i * biL ) + j );
} }
/* Check X to have at least n limbs and set it to 0. */
static int mpi_core_clear( mbedtls_mpi_uint *X,
size_t nx,
size_t limbs )
{
if( nx < limbs )
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
if( X != NULL )
memset( X, 0, nx * ciL );
return( 0 );
}
/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
* into the storage form used by mbedtls_mpi. */ * into the storage form used by mbedtls_mpi. */
static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x ) static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
@ -190,18 +176,19 @@ int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
const unsigned char *buf, const unsigned char *buf,
size_t buflen ) size_t buflen )
{ {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i; size_t i;
size_t const limbs = CHARS_TO_LIMBS( buflen ); size_t const limbs = CHARS_TO_LIMBS( buflen );
/* Ensure that target MPI has at least the necessary number of limbs */ if( nx < limbs )
MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) ); return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
if( X != NULL )
memset( X, 0, nx * ciL );
for( i = 0; i < buflen; i++ ) for( i = 0; i < buflen; i++ )
X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3); X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
cleanup: return( 0 );
return( ret );
} }
/* /*
@ -215,13 +202,15 @@ int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
const unsigned char *buf, const unsigned char *buf,
size_t buflen ) size_t buflen )
{ {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t const limbs = CHARS_TO_LIMBS( buflen ); size_t const limbs = CHARS_TO_LIMBS( buflen );
size_t overhead; size_t overhead;
unsigned char *Xp; unsigned char *Xp;
/* Ensure that target MPI has at least the necessary number of limbs */ if( nx < limbs )
MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) ); return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
if( X != NULL )
memset( X, 0, nx * ciL );
overhead = ( nx * ciL ) - buflen; overhead = ( nx * ciL ) - buflen;
@ -235,8 +224,7 @@ int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
mbedtls_mpi_core_bigendian_to_host( X, nx ); mbedtls_mpi_core_bigendian_to_host( X, nx );
} }
cleanup: return( 0 );
return( ret );
} }
/* /*