From d7fe6fbd764c7c244ee24bc322772c07e01cda4c Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Sun, 3 Jan 2016 22:39:18 +0000 Subject: [PATCH] Fix for MPI divide on MSVC Resolves multiple platform issues when building bignum.c with Microsoft Visual Studio. --- include/polarssl/bignum.h | 1 + library/bignum.c | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/polarssl/bignum.h b/include/polarssl/bignum.h index 8e1687b97..2db29d5ae 100644 --- a/include/polarssl/bignum.h +++ b/include/polarssl/bignum.h @@ -152,6 +152,7 @@ typedef uint32_t t_udbl; #define POLARSSL_HAVE_INT64 typedef int64_t t_sint; typedef uint64_t t_uint; + /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef unsigned int t_udbl __attribute__((mode(TI))); #define POLARSSL_HAVE_UDBL #else diff --git a/library/bignum.c b/library/bignum.c index b606238c3..89fbe12c4 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1226,10 +1226,11 @@ static t_uint int_div_int( t_uint u1, t_uint u0, t_uint d, t_uint *r ) #if defined(POLARSSL_HAVE_UDBL) t_udbl dividend, quotient; #else - const t_uint radix = 1 << biH; + const t_uint radix = (t_uint) 1 << biH; + const t_uint uint_halfword_mask = ( (t_uint) 1 << biH ) - 1; t_uint d0, d1, q0, q1, rAX, r0, quotient; t_uint u0_msw, u0_lsw; - int s; + size_t s; #endif /* @@ -1250,7 +1251,7 @@ static t_uint int_div_int( t_uint u1, t_uint u0, t_uint d, t_uint *r ) quotient = ( (t_udbl) 1 << biL ) - 1; if( r != NULL ) - *r = dividend - (quotient * d); + *r = (t_uint)( dividend - (quotient * d ) ); return (t_uint) quotient; #else @@ -1267,14 +1268,14 @@ static t_uint int_div_int( t_uint u1, t_uint u0, t_uint d, t_uint *r ) d = d << s; u1 = u1 << s; - u1 |= ( u0 >> ( 32 - s ) ) & ( -s >> 31 ); + u1 |= ( u0 >> ( biL - s ) ) & ( -(t_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