From 9a4a5ac4defa98424d7de14d16f49f1b126af4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 4 Dec 2013 18:05:29 +0100 Subject: [PATCH] Fix bug in mpi_set_bit --- ChangeLog | 1 + library/bignum.c | 3 ++- tests/suites/test_suite_mpi.data | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) 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: