Simplify code

Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
Gabor Mezei 2022-08-12 17:47:39 +02:00
parent 5a5c0c5f0a
commit c414ba3fc0
No known key found for this signature in database
GPG Key ID: F072ACA227ACD71D
2 changed files with 16 additions and 15 deletions

View File

@ -185,28 +185,27 @@ int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
const unsigned char *buf, const unsigned char *buf,
size_t buflen ) size_t buflen )
{ {
const size_t limbs = CHARS_TO_LIMBS( buflen ); size_t const limbs = CHARS_TO_LIMBS( buflen );
if( nx < limbs ) if( nx < limbs )
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
if( X != NULL ) /* If nx is 0, buflen must also be 0 (from previous test). Nothing to do. */
if( nx == 0 )
return( 0 );
memset( X, 0, nx * ciL );
/* memcpy() with (NULL, 0) is undefined behaviour */
if( buflen != 0 )
{ {
memset( X, 0, nx * ciL ); size_t overhead = ( nx * ciL ) - buflen;
unsigned char *Xp = (unsigned char *) X;
const size_t overhead = ( nx * ciL ) - buflen; memcpy( Xp + overhead, buf, buflen );
/* Avoid calling `memcpy` with NULL source or destination argument,
* even if buflen is 0. */
if( buf != NULL )
{
unsigned char *Xp = (unsigned char *) X;
memcpy( Xp + overhead, buf, buflen );
mbedtls_mpi_core_bigendian_to_host( X, nx );
}
} }
mbedtls_mpi_core_bigendian_to_host( X, nx );
return( 0 ); return( 0 );
} }

View File

@ -85,8 +85,10 @@ int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
* most significant zero bytes in the input). * most significant zero bytes in the input).
* *
* \param X The address of the MPI. * \param X The address of the MPI.
* May only be #NULL if \nx is 0 and \p buflen is 0.
* \param nx The number of limbs of \p X. * \param nx The number of limbs of \p X.
* \param buf The input buffer to import from. * \param buf The input buffer to import from.
* May only be #NULL if \p buflen is 0.
* \param buflen The length in bytes of \p buf. * \param buflen The length in bytes of \p buf.
* *
* \return \c 0 if successful. * \return \c 0 if successful.