Fix indent comment corruption issue
[openssl.git] / crypto / bn / bn_lib.c
index f22d358ce5267acd4aa09a9338bd42eb31dacdbf..7a8f8c1450177fcf2e2386f3d99a3f154f9ac760 100644 (file)
@@ -71,7 +71,8 @@ const char BN_version[]="Big Number" OPENSSL_VERSION_PTEXT;
 
 /* This stuff appears to be completely unused, so is deprecated */
 #ifndef OPENSSL_NO_DEPRECATED
-/* For a 32 bit machine
+/*-
+ * For a 32 bit machine
  * 2 -   4 ==  128
  * 3 -   8 ==  256
  * 4 -  16 ==  512
@@ -139,25 +140,6 @@ const BIGNUM *BN_value_one(void)
        return(&const_one);
        }
 
-char *BN_options(void)
-       {
-       static int init=0;
-       static char data[16];
-
-       if (!init)
-               {
-               init++;
-#ifdef BN_LLONG
-               BIO_snprintf(data,sizeof data,"bn(%d,%d)",
-                            (int)sizeof(BN_ULLONG)*8,(int)sizeof(BN_ULONG)*8);
-#else
-               BIO_snprintf(data,sizeof data,"bn(%d,%d)",
-                            (int)sizeof(BN_ULONG)*8,(int)sizeof(BN_ULONG)*8);
-#endif
-               }
-       return(data);
-       }
-
 int BN_num_bits_word(BN_ULONG l)
        {
        static const unsigned char bits[256]={
@@ -315,7 +297,7 @@ BIGNUM *BN_new(void)
 
 /* This is used both by bn_expand2() and bn_dup_expand() */
 /* The caller MUST check that words > b->dmax before calling this */
-static BN_ULONG *bn_expand_internal(const BIGNUM *b, size_t words)
+static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
        {
        BN_ULONG *A,*a = NULL;
        const BN_ULONG *B;
@@ -339,6 +321,15 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, size_t words)
                BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
+#ifdef PURIFY
+       /* Valgrind complains in BN_consttime_swap because we process the whole
+        * array even if it's not initialised yet. This doesn't matter in that
+        * function - what's important is constant time operation (we're not
+        * actually going to use the data)
+       */
+       memset(a, 0, sizeof(BN_ULONG)*words);
+#endif
+
 #if 1
        B=b->d;
        /* Check if the previous number needs to be copied */
@@ -364,10 +355,13 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, size_t words)
                case 3: A[2]=B[2];
                case 2: A[1]=B[1];
                case 1: A[0]=B[0];
-               case 0: /* workaround for ultrix cc: without 'case 0', the optimizer does
-                        * the switch table by doing a=top&3; a--; goto jump_table[a];
-                        * which fails for top== 0 */
+               case 0:
                        ;
+                       /*
+                        * workaround for ultrix cc: without 'case 0', the optimizer does
+                        * the switch table by doing a=top&3; a--; goto jump_table[a];
+                        * which fails for top== 0
+                        */
                        }
                }
 
@@ -391,7 +385,7 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, size_t words)
  */
 
 #ifndef OPENSSL_NO_DEPRECATED
-BIGNUM *bn_dup_expand(const BIGNUM *b, size_t words)
+BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
        {
        BIGNUM *r = NULL;
 
@@ -442,7 +436,7 @@ BIGNUM *bn_dup_expand(const BIGNUM *b, size_t words)
  * 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, size_t words)
+BIGNUM *bn_expand2(BIGNUM *b, int words)
        {
        bn_check_top(b);
 
@@ -594,7 +588,7 @@ int BN_set_word(BIGNUM *a, BN_ULONG w)
        return(1);
        }
 
-BIGNUM *BN_bin2bn(const unsigned char *s, size_t len, BIGNUM *ret)
+BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
        {
        unsigned int i,m;
        unsigned int n;
@@ -614,7 +608,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, size_t len, BIGNUM *ret)
                }
        i=((n-1)/BN_BYTES)+1;
        m=((n-1)%(BN_BYTES));
-       if (bn_wexpand(ret, i) == NULL)
+       if (bn_wexpand(ret, (int)i) == NULL)
                {
                if (bn) BN_free(bn);
                return NULL;
@@ -718,7 +712,7 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b)
 
 int BN_set_bit(BIGNUM *a, int n)
        {
-       size_t i,j,k;
+       int i,j,k;
 
        if (n < 0)
                return 0;
@@ -763,7 +757,7 @@ int BN_is_bit_set(const BIGNUM *a, int n)
        i=n/BN_BITS2;
        j=n%BN_BITS2;
        if (a->top <= i) return 0;
-       return(((a->d[i])>>j)&((BN_ULONG)1));
+       return (int)(((a->d[i])>>j)&((BN_ULONG)1));
        }
 
 int BN_mask_bits(BIGNUM *a, int n)
@@ -843,3 +837,55 @@ int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
                }
        return bn_cmp_words(a,b,cl);
        }
+
+/* 
+ * Constant-time conditional swap of a and b.  
+ * a and b are swapped if condition is not 0.  The code assumes that at most one bit of condition is set.
+ * nwords is the number of words to swap.  The code assumes that at least nwords are allocated in both a and b,
+ * and that no more than nwords are used by either a or b.
+ * a and b cannot be the same number
+ */
+void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
+       {
+       BN_ULONG t;
+       int i;
+
+       bn_wcheck_size(a, nwords);
+       bn_wcheck_size(b, nwords);
+
+       assert(a != b);
+       assert((condition & (condition - 1)) == 0);
+       assert(sizeof(BN_ULONG) >= sizeof(int));
+
+       condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
+
+       t = (a->top^b->top) & condition;
+       a->top ^= t;
+       b->top ^= t;
+
+#define BN_CONSTTIME_SWAP(ind) \
+       do { \
+               t = (a->d[ind] ^ b->d[ind]) & condition; \
+               a->d[ind] ^= t; \
+               b->d[ind] ^= t; \
+       } while (0)
+
+
+       switch (nwords) {
+       default:
+               for (i = 10; i < nwords; i++) 
+                       BN_CONSTTIME_SWAP(i);
+               /* Fallthrough */
+       case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */
+       case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */
+       case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */
+       case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */
+       case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */
+       case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */
+       case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */
+       case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */
+       case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */
+       case 1: BN_CONSTTIME_SWAP(0);
+       }
+#undef BN_CONSTTIME_SWAP
+}