Skip to content

Commit

Permalink
Fix mpi_bigendian_to_host() on bigendian systems
Browse files Browse the repository at this point in the history
The previous implementation of mpi_bigendian_to_host() did
a byte-swapping regardless of the endianness of the system.

Fixes #2622.
  • Loading branch information
Hanno Becker committed May 1, 2019
1 parent 1cd7bea commit 031d633
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions library/bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,15 @@ int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE
static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
{
uint8_t i;
unsigned char *x_ptr;
mbedtls_mpi_uint tmp = 0;
/* This works regardless of the endianness. */
for( i = 0; i < ciL; i++, x >>= 8 )
tmp |= ( x & 0xFF ) << ( ( ciL - 1 - i ) << 3 );

for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
{
tmp <<= CHAR_BIT;
tmp |= (mbedtls_mpi_uint) *x_ptr;
}

return( tmp );
}

Expand Down

0 comments on commit 031d633

Please sign in to comment.