Add little endian import to Bignum

The function `mbedtls_mpi_read_binary()` expects big endian byte order,
but we need to be able to read from little endian in some caseses. (For
example when handling keys corresponding to Montgomery curves.)

Used `echo xx | tac -rs .. | tr [a-z] [A-Z]` to transform the test data
to little endian and `echo "ibase=16;xx" | bc` to convert to decimal.
This commit is contained in:
Janos Follath 2019-02-13 10:28:28 +00:00
parent 3081629de4
commit a778a94b7d
4 changed files with 68 additions and 2 deletions
library

View file

@ -813,6 +813,34 @@ static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
}
}
/*
* Import X from unsigned binary data, little endian
*/
int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
const unsigned char *buf, size_t buflen )
{
int ret;
size_t i;
size_t const limbs = CHARS_TO_LIMBS( buflen );
/* Ensure that target MPI has exactly the necessary number of limbs */
if( X->n != limbs )
{
mbedtls_mpi_free( X );
mbedtls_mpi_init( X );
MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
}
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
for( i = 0; i < buflen; i++ )
X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
cleanup:
return( ret );
}
/*
* Import X from unsigned binary data, big endian
*/