From 2bfd2c74d256483cab8775a94204839d25020577 Mon Sep 17 00:00:00 2001 From: Geoff Thorpe Date: Tue, 2 Dec 2003 20:01:30 +0000 Subject: Incremental cleanups to bn_lib.c. - Add missing bn_check_top() calls and relocate some others - Use BN_is_zero() where appropriate - Remove assert()s that bn_check_top() is already covering - Simplify the code in places (esp. bn_expand2()) - Only keep ambiguous zero handling if BN_STRICT isn't defined - Remove some white-space and make some other aesthetic tweaks --- crypto/bn/bn_lib.c | 86 ++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 48 deletions(-) (limited to 'crypto/bn/bn_lib.c') diff --git a/crypto/bn/bn_lib.c b/crypto/bn/bn_lib.c index 3bc67f9de2..d29a7cc8df 100644 --- a/crypto/bn/bn_lib.c +++ b/crypto/bn/bn_lib.c @@ -244,16 +244,11 @@ int BN_num_bits_word(BN_ULONG l) int BN_num_bits(const BIGNUM *a) { - BN_ULONG l; - int i; - + int i = a->top - 1; bn_check_top(a); - if (a->top == 0) return(0); - l=a->d[a->top-1]; - assert(l != 0); - i=(a->top-1)*BN_BITS2; - return(i+BN_num_bits_word(l)); + if (BN_is_zero(a)) return 0; + return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); } void BN_clear_free(BIGNUM *a) @@ -261,6 +256,7 @@ void BN_clear_free(BIGNUM *a) int i; if (a == NULL) return; + bn_check_top(a); if (a->d != NULL) { OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0])); @@ -276,6 +272,7 @@ void BN_clear_free(BIGNUM *a) void BN_free(BIGNUM *a) { if (a == NULL) return; + bn_check_top(a); if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) OPENSSL_free(a->d); if (a->flags & BN_FLG_MALLOCED) @@ -321,13 +318,13 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) const BN_ULONG *B; int i; + bn_check_top(b); + if (words > (INT_MAX/(4*BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG); return NULL; } - - bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); @@ -394,12 +391,14 @@ BIGNUM *bn_dup_expand(const BIGNUM *b, int words) { BIGNUM *r = NULL; + bn_check_top(b); + /* This function does not work if * words <= b->dmax && top < words * because BN_dup() does not preserve 'dmax'! * (But bn_dup_expand() is not used anywhere yet.) */ - + if (words > b->dmax) { BN_ULONG *a = bn_expand_internal(b, words); @@ -443,23 +442,19 @@ BIGNUM *bn_expand2(BIGNUM *b, int words) BN_ULONG *A; int i; + bn_check_top(b); + if (words > b->dmax) { BN_ULONG *a = bn_expand_internal(b, words); - - if (a) - { - if (b->d) - OPENSSL_free(b->d); - b->d=a; - b->dmax=words; - } - else - b = NULL; + if(!a) return NULL; + if(b->d) OPENSSL_free(b->d); + b->d=a; + b->dmax=words; } - + /* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */ - if ((b != NULL) && (b->top < b->dmax)) + if (b->top < b->dmax) { A = &(b->d[b->top]); for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8) @@ -471,26 +466,26 @@ BIGNUM *bn_expand2(BIGNUM *b, int words) A[0]=0; assert(A == &(b->d[b->dmax])); } - else if(b) bn_check_top(b); + bn_check_top(b); return b; } BIGNUM *BN_dup(const BIGNUM *a) { - BIGNUM *r, *t; + BIGNUM *t; if (a == NULL) return NULL; - bn_check_top(a); t = BN_new(); - if (t == NULL) return(NULL); - r = BN_copy(t, a); - /* now r == t || r == NULL */ - if (r == NULL) + if (t == NULL) return NULL; + if(!BN_copy(t, a)) + { BN_free(t); - bn_check_top(r); - return r; + return NULL; + } + bn_check_top(t); + return t; } BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) @@ -524,10 +519,11 @@ BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif -/* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/ a->top=b->top; +#ifndef BN_STRICT if ((a->top == 0) && (a->d != NULL)) a->d[0]=0; +#endif a->neg=b->neg; bn_check_top(a); return(a); @@ -540,18 +536,15 @@ BIGNUM *BN_ncopy(BIGNUM *a, const BIGNUM *b, size_t n) const BN_ULONG *B; bn_check_top(b); - if (a == b) return a; min = (b->top < (int)n)? b->top: (int)n; - if (!min) { BN_zero(a); return a; } - if (bn_wexpand(a, min) == NULL) return NULL; @@ -571,11 +564,8 @@ BIGNUM *BN_ncopy(BIGNUM *a, const BIGNUM *b, size_t n) case 0: ; } a->top = min; - a->neg = b->neg; bn_correct_top(a); - - bn_check_top(a); return(a); } @@ -612,7 +602,6 @@ void BN_swap(BIGNUM *a, BIGNUM *b) bn_check_top(b); } - void BN_clear(BIGNUM *a) { bn_check_top(a); @@ -743,7 +732,7 @@ int BN_ucmp(const BIGNUM *a, const BIGNUM *b) t1= ap[i]; t2= bp[i]; if (t1 != t2) - return(t1 > t2?1:-1); + return((t1 > t2) ? 1 : -1); } return(0); } @@ -815,8 +804,8 @@ int BN_clear_bit(BIGNUM *a, int n) { int i,j; - if (n < 0) - return 0; + bn_check_top(a); + if (n < 0) return 0; i=n/BN_BITS2; j=n%BN_BITS2; @@ -831,10 +820,11 @@ int BN_is_bit_set(const BIGNUM *a, int n) { int i,j; - if (n < 0) return(0); + bn_check_top(a); + if (n < 0) return 0; i=n/BN_BITS2; j=n%BN_BITS2; - if (a->top <= i) return(0); + if (a->top <= i) return 0; return((a->d[i]&(((BN_ULONG)1)<= a->top) return(0); + if (w >= a->top) return 0; if (b == 0) a->top=w; else -- cgit v1.2.3