diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index 146224a2b..aa51556a5 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -122,6 +122,7 @@ #define MBEDTLS_HAVE_INT64 typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; + /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); #define MBEDTLS_HAVE_UDBL #else diff --git a/library/bignum.c b/library/bignum.c index 21069d846..0e587f15e 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1221,10 +1221,11 @@ static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1, #if defined(MBEDTLS_HAVE_UDBL) mbedtls_t_udbl dividend, quotient; #else - const mbedtls_mpi_uint radix = 1 << biH; + const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH; + const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1; mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient; mbedtls_mpi_uint u0_msw, u0_lsw; - int s; + size_t s; #endif /* @@ -1245,7 +1246,7 @@ static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1, quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1; if( r != NULL ) - *r = dividend - (quotient * d); + *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) ); return (mbedtls_mpi_uint) quotient; #else @@ -1262,14 +1263,14 @@ static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1, d = d << s; u1 = u1 << s; - u1 |= ( u0 >> ( 32 - s ) ) & ( -s >> 31 ); + u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) ); u0 = u0 << s; d1 = d >> biH; - d0 = d & 0xffff; + d0 = d & uint_halfword_mask; u0_msw = u0 >> biH; - u0_lsw = u0 & 0xffff; + u0_lsw = u0 & uint_halfword_mask; /* * Find the first quotient and remainder