From d02abde904ab168c5b422cd9b4cbbb45a29a234b Mon Sep 17 00:00:00 2001 From: cameronrich Date: Wed, 5 Mar 2008 08:47:05 +0000 Subject: [PATCH] fixed a bigint issue git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@149 9a5d90b5-6617-0410-8a86-bb477d3ed2e3 --- crypto/bigint.c | 10 +++++++--- crypto/bigint_impl.h | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/crypto/bigint.c b/crypto/bigint.c index 303c20a1c..53a583929 100644 --- a/crypto/bigint.c +++ b/crypto/bigint.c @@ -517,7 +517,7 @@ static bigint *bi_int_divide(BI_CTX *ctx, bigint *biR, comp denom) r = (r<comps[i]; biR->comps[i] = (comp)(r / denom); r %= denom; - } while (--i != 0); + } while (--i >= 0); return trim(biR); } @@ -947,6 +947,7 @@ static bigint *regular_square(BI_CTX *ctx, bigint *bi) for (j = i+1; j < t; j++) { long_comp xx = (long_comp)x[i]*x[j]; + long_comp xx2 = 2*xx; long_comp blob = (long_comp)w[i+j]+carry; if (u) /* previous overflow */ @@ -954,13 +955,16 @@ static bigint *regular_square(BI_CTX *ctx, bigint *bi) blob += COMP_RADIX; } + u = 0; - if (xx & COMP_BIG_MSB) /* check for overflow */ + tmp = xx2 + blob; + + /* check for overflow */ + if ((COMP_MAX-xx) < xx || (COMP_MAX-xx2) < blob) { u = 1; } - tmp = 2*xx + blob; w[i+j] = (comp)tmp; carry = (comp)(tmp >> COMP_BIT_SIZE); } diff --git a/crypto/bigint_impl.h b/crypto/bigint_impl.h index c23572733..e2d456532 100644 --- a/crypto/bigint_impl.h +++ b/crypto/bigint_impl.h @@ -44,10 +44,10 @@ /* Architecture specific functions for big ints */ #ifdef WIN32 #define COMP_RADIX 4294967296i64 -#define COMP_BIG_MSB 0x8000000000000000i64 +#define COMP_MAX 0xFFFFFFFFFFFFFFFFi64 #else #define COMP_RADIX 4294967296ULL /**< Max component + 1 */ -#define COMP_BIG_MSB 0x8000000000000000ULL /**< (Max dbl comp + 1)/ 2 */ +#define COMP_MAX 0xFFFFFFFFFFFFFFFFULL/**< (Max dbl comp -1) */ #endif #define COMP_BIT_SIZE 32 /**< Number of bits in a component. */ #define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */