X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=gosthash2012.c;h=ee534c2f9f838f2ef52797c23e88e63f98b9b5ef;hb=419263293a3075c630d028b94378107714ab6fa6;hp=6a89237e1d9ba7ba35f0924780cd0c147385df22;hpb=cba16944bff9d8c5dcf37be641822cd3de6d2ec1;p=openssl-gost%2Fengine.git diff --git a/gosthash2012.c b/gosthash2012.c index 6a89237..ee534c2 100644 --- a/gosthash2012.c +++ b/gosthash2012.c @@ -32,8 +32,6 @@ void init_gost2012_hash_ctx(gost2012_hash_ctx * CTX, const unsigned int digest_size) { - unsigned int i; - memset(CTX, 0, sizeof(gost2012_hash_ctx)); CTX->digest_size = digest_size; @@ -62,18 +60,33 @@ static INLINE void add512(const union uint512_u *x, { #ifndef __GOST3411_BIG_ENDIAN__ unsigned int CF, OF; + unsigned long long tmp; unsigned int i; CF = 0; - for (i = 0; i < 8; i++) { - r->QWORD[i] = x->QWORD[i] + y->QWORD[i]; - if (r->QWORD[i] < y->QWORD[i] || r->QWORD[i] < x->QWORD[i]) + for (i = 0; i < 8; i++) + { + /* Detecting integer overflow condition for three numbers + * in a portable way is tricky a little. */ + + /* Step 1: numbers cause overflow */ + tmp = x->QWORD[i] + y->QWORD[i]; + + /* Compare with any of two summands, no need to check both */ + if (tmp < x->QWORD[i]) OF = 1; else OF = 0; - r->QWORD[i] += CF; + /* Step 2: carry bit causes overflow */ + tmp += CF; + + if (CF > 0 && tmp == 0) + OF = 1; + CF = OF; + + r->QWORD[i] = tmp; } #else const unsigned char *xp, *yp;