diff --git a/ChangeLog b/ChangeLog index 27a6eab78..f0a015707 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,7 @@ Changes * Split off curves from ecp.c into ecp_curves.c Bugfix + * Fixed bug in mpi_set_bit() on platforms where t_uint is wider than int * Fixed X.509 hostname comparison (with non-regular characters) * SSL now gracefully handles missing RNG * Missing defines / cases for RSA_PSK key exchange diff --git a/library/bignum.c b/library/bignum.c index 945da17b8..98d534a5a 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -280,7 +280,8 @@ int mpi_set_bit( mpi *X, size_t pos, unsigned char val ) MPI_CHK( mpi_grow( X, off + 1 ) ); } - X->p[off] = ( X->p[off] & ~( 0x01 << idx ) ) | ( val << idx ); + X->p[off] &= ~( (t_uint) 0x01 << idx ); + X->p[off] |= (t_uint) val << idx; cleanup: diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index b9e00f1d7..36ae8d6a8 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -665,6 +665,12 @@ mpi_set_bit:10:"49979687":80:0:10:"49979687" Test bit set (Add above existing limbs with a 1) mpi_set_bit:10:"49979687":80:1:10:"1208925819614629224685863" +Test bit set (Bit index larger than 31 with a 0) +mpi_set_bit:16:"FFFFFFFFFFFFFFFF":32:0:16:"FFFFFFFEFFFFFFFF" + +Test bit set (Bit index larger than 31 with a 1) +mpi_set_bit:16:"00":32:1:16:"0100000000" + MPI Selftest depends_on:POLARSSL_SELF_TEST mpi_selftest: