Some provisional bignum debugging has begun to detect inconsistent BIGNUM
[openssl.git] / crypto / bn / bn_lib.c
index f0dc7d52dc257ef84abebb5619402a76d9bd7adf..783881d3a6977964c8993a25a8673d4128c462d1 100644 (file)
@@ -62,6 +62,7 @@
 #endif
 
 #include <assert.h>
+#include <limits.h>
 #include <stdio.h>
 #include "cryptlib.h"
 #include "bn_lcl.h"
@@ -90,28 +91,28 @@ void BN_set_params(int mult, int high, int low, int mont)
        {
        if (mult >= 0)
                {
-               if (mult > (sizeof(int)*8)-1)
+               if (mult > (int)(sizeof(int)*8)-1)
                        mult=sizeof(int)*8-1;
                bn_limit_bits=mult;
                bn_limit_num=1<<mult;
                }
        if (high >= 0)
                {
-               if (high > (sizeof(int)*8)-1)
+               if (high > (int)(sizeof(int)*8)-1)
                        high=sizeof(int)*8-1;
                bn_limit_bits_high=high;
                bn_limit_num_high=1<<high;
                }
        if (low >= 0)
                {
-               if (low > (sizeof(int)*8)-1)
+               if (low > (int)(sizeof(int)*8)-1)
                        low=sizeof(int)*8-1;
                bn_limit_bits_low=low;
                bn_limit_num_low=1<<low;
                }
        if (mont >= 0)
                {
-               if (mont > (sizeof(int)*8)-1)
+               if (mont > (int)(sizeof(int)*8)-1)
                        mont=sizeof(int)*8-1;
                bn_limit_bits_mont=mont;
                bn_limit_num_mont=1<<mont;
@@ -127,7 +128,7 @@ int BN_get_params(int which)
        else return(0);
        }
 
-BIGNUM *BN_value_one(void)
+const BIGNUM *BN_value_one(void)
        {
        static BN_ULONG data_one=1L;
        static BIGNUM const_one={&data_one,1,1,0};
@@ -262,12 +263,12 @@ void BN_clear_free(BIGNUM *a)
        if (a == NULL) return;
        if (a->d != NULL)
                {
-               memset(a->d,0,a->dmax*sizeof(a->d[0]));
+               OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0]));
                if (!(BN_get_flags(a,BN_FLG_STATIC_DATA)))
                        OPENSSL_free(a->d);
                }
        i=BN_get_flags(a,BN_FLG_MALLOCED);
-       memset(a,0,sizeof(BIGNUM));
+       OPENSSL_cleanse(a,sizeof(BIGNUM));
        if (i)
                OPENSSL_free(a);
        }
@@ -312,6 +313,12 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
        const BN_ULONG *B;
        int i;
 
+       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))
                {
@@ -356,16 +363,6 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
                        }
                }
 
-       /* Now need to zero any data between b->top and b->max */
-
-       A= &(a[b->top]);
-       for (i=(words - b->top)>>3; i>0; i--,A+=8)
-               {
-               A[0]=0; A[1]=0; A[2]=0; A[3]=0;
-               A[4]=0; A[5]=0; A[6]=0; A[7]=0;
-               }
-       for (i=(words - b->top)&7; i>0; i--,A++)
-               A[0]=0;
 #else
        memset(A,0,sizeof(BN_ULONG)*(words+1));
        memcpy(A,b->d,sizeof(b->d[0])*b->top);
@@ -389,6 +386,12 @@ BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
        {
        BIGNUM *r = NULL;
 
+       /* 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);
@@ -421,12 +424,16 @@ BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
        }
 
 /* This is an internal function that should not be used in applications.
- * It ensures that 'b' has enough room for a 'words' word number number.
+ * It ensures that 'b' has enough room for a 'words' word number
+ * and initialises any unused part of b->d with leading zeros.
  * It is mostly used by the various BIGNUM routines. If there is an error,
  * NULL is returned. If not, 'b' is returned. */
 
 BIGNUM *bn_expand2(BIGNUM *b, int words)
        {
+       BN_ULONG *A;
+       int i;
+
        if (words > b->dmax)
                {
                BN_ULONG *a = bn_expand_internal(b, words);
@@ -441,6 +448,20 @@ BIGNUM *bn_expand2(BIGNUM *b, int words)
                else
                        b = NULL;
                }
+       
+       /* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */
+       if ((b != NULL) && (b->top < b->dmax))
+               {
+               A = &(b->d[b->top]);
+               for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8)
+                       {
+                       A[0]=0; A[1]=0; A[2]=0; A[3]=0;
+                       A[4]=0; A[5]=0; A[6]=0; A[7]=0;
+                       }
+               for (i=(b->dmax - b->top)&7; i>0; i--,A++)
+                       A[0]=0;
+               assert(A == &(b->d[b->dmax]));
+               }
        return b;
        }
 
@@ -500,6 +521,51 @@ BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
        return(a);
        }
 
+BIGNUM *BN_ncopy(BIGNUM *a, const BIGNUM *b, size_t n)
+       {
+       int i, min;
+       BN_ULONG *A;
+       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;
+
+       A=a->d;
+       B=b->d;
+       for (i=min>>2; i>0; i--, A+=4, B+=4)
+               {
+               BN_ULONG a0,a1,a2,a3;
+               a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
+               A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
+               }
+       switch (min&3)
+               {
+               case 3: A[2]=B[2];
+               case 2: A[1]=B[1];
+               case 1: A[0]=B[0];
+               case 0: ;
+               }
+       a->top = min;
+
+       a->neg = b->neg;
+       bn_fix_top(a);
+
+       return(a);
+       }
+
 void BN_swap(BIGNUM *a, BIGNUM *b)
        {
        int flags_old_a, flags_old_b;
@@ -543,7 +609,7 @@ BN_ULONG BN_get_word(const BIGNUM *a)
        BN_ULONG ret=0;
 
        n=BN_num_bytes(a);
-       if (n > sizeof(BN_ULONG))
+       if (n > (int)sizeof(BN_ULONG))
                return(BN_MASK2);
        for (i=a->top-1; i>=0; i--)
                {
@@ -561,7 +627,7 @@ BN_ULONG BN_get_word(const BIGNUM *a)
 int BN_set_word(BIGNUM *a, BN_ULONG w)
        {
        int i,n;
-       if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);
+       if (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0);
 
        n=sizeof(BN_ULONG)/BN_BYTES;
        a->neg=0;
@@ -585,7 +651,6 @@ int BN_set_word(BIGNUM *a, BN_ULONG w)
        return(1);
        }
 
-/* ignore negative */
 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
        {
        unsigned int i,m;
@@ -605,7 +670,8 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
                return(NULL);
        i=((n-1)/BN_BYTES)+1;
        m=((n-1)%(BN_BYTES));
-       ret->top=i-1;
+       ret->top=i;
+       ret->neg=0;
        while (n-- > 0)
                {
                l=(l<<8L)| *(s++);
@@ -777,3 +843,34 @@ int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
        return(0);
        }
 
+/* Here follows a specialised variants of bn_cmp_words().  It has the
+   property of performing the operation on arrays of different sizes.
+   The sizes of those arrays is expressed through cl, which is the
+   common length ( basicall, min(len(a),len(b)) ), and dl, which is the
+   delta between the two lengths, calculated as len(a)-len(b).
+   All lengths are the number of BN_ULONGs...  */
+
+int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
+       int cl, int dl)
+       {
+       int n,i;
+       n = cl-1;
+
+       if (dl < 0)
+               {
+               for (i=dl; i<0; i++)
+                       {
+                       if (b[n-i] != 0)
+                               return -1; /* a < b */
+                       }
+               }
+       if (dl > 0)
+               {
+               for (i=dl; i>0; i--)
+                       {
+                       if (a[n+i] != 0)
+                               return 1; /* a > b */
+                       }
+               }
+       return bn_cmp_words(a,b,cl);
+       }