summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Polyakov <appro@openssl.org>2012-11-09 13:58:40 +0000
committerAndy Polyakov <appro@openssl.org>2013-02-02 22:39:00 +0100
commitb2226c6c8371bf69bb0fcb59cb7bb8562a548fba (patch)
tree3e3dc98ea94da6671dcb137f5cea49c2f214f14c
parent024de2174b9a92c6d8a54d231da4d5f9ec3a4285 (diff)
downloadopenssl-new-b2226c6c8371bf69bb0fcb59cb7bb8562a548fba.tar.gz
bn_word.c: fix overflow bug in BN_add_word.
(cherry picked from commit 134c00659a1bc67ad35a1e4620e16bc4315e6e37)
-rw-r--r--crypto/bn/bn_word.c25
1 files changed, 8 insertions, 17 deletions
diff --git a/crypto/bn/bn_word.c b/crypto/bn/bn_word.c
index ee7b87c45c..de83a15b99 100644
--- a/crypto/bn/bn_word.c
+++ b/crypto/bn/bn_word.c
@@ -144,26 +144,17 @@ int BN_add_word(BIGNUM *a, BN_ULONG w)
a->neg=!(a->neg);
return(i);
}
- /* Only expand (and risk failing) if it's possibly necessary */
- if (((BN_ULONG)(a->d[a->top - 1] + 1) == 0) &&
- (bn_wexpand(a,a->top+1) == NULL))
- return(0);
- i=0;
- for (;;)
+ for (i=0;w!=0 && i<a->top;i++)
{
- if (i >= a->top)
- l=w;
- else
- l=(a->d[i]+w)&BN_MASK2;
- a->d[i]=l;
- if (w > l)
- w=1;
- else
- break;
- i++;
+ a->d[i] = l = (a->d[i]+w)&BN_MASK2;
+ w = (w>l)?1:0;
}
- if (i >= a->top)
+ if (w && i==a->top)
+ {
+ if (bn_wexpand(a,a->top+1) == NULL) return 0;
a->top++;
+ a->d[i]=w;
+ }
bn_check_top(a);
return(1);
}