Use e_os2.h rather than opensslconf.h, since some needed macros are
[oweals/openssl.git] / crypto / bn / bn_rand.c
index c5c14130a37b1809da7fa880e43da8a7f0250240..fb583fb358fcb990f5311ca2a6b7e345f89b14fb 100644 (file)
@@ -76,7 +76,7 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
 
        bytes=(bits+7)/8;
        bit=(bits-1)%8;
-       mask=0xff<<bit;
+       mask=0xff<<(bit+1);
 
        buf=(unsigned char *)OPENSSL_malloc(bytes);
        if (buf == NULL)
@@ -121,25 +121,27 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
                }
 #endif
 
-       if (top)
+       if (top != -1)
                {
-               if (bit == 0)
+               if (top)
                        {
-                       buf[0]=1;
-                       buf[1]|=0x80;
+                       if (bit == 0)
+                               {
+                               buf[0]=1;
+                               buf[1]|=0x80;
+                               }
+                       else
+                               {
+                               buf[0]|=(3<<(bit-1));
+                               }
                        }
                else
                        {
-                       buf[0]|=(3<<(bit-1));
-                       buf[0]&= ~(mask<<1);
+                       buf[0]|=(1<<bit);
                        }
                }
-       else
-               {
-               buf[0]|=(1<<bit);
-               buf[0]&= ~(mask<<1);
-               }
-       if (bottom) /* set bottom bits to whatever odd is */
+       buf[0] &= ~mask;
+       if (bottom) /* set bottom bit if requested */
                buf[bytes-1]|=1;
        if (!BN_bin2bn(buf,bytes,rnd)) goto err;
        ret=1;
@@ -169,8 +171,9 @@ int     BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
        }
 #endif
 
-/* random number r: min <= r < min+range */
-int    BN_rand_range(BIGNUM *r, BIGNUM *min, BIGNUM *range)
+
+/* random number r:  0 <= r < range */
+int    BN_rand_range(BIGNUM *r, BIGNUM *range)
        {
        int n;
 
@@ -191,7 +194,7 @@ int BN_rand_range(BIGNUM *r, BIGNUM *min, BIGNUM *range)
                do
                        {
                        /* range = 11..._2, so each iteration succeeds with probability >= .75 */
-                       if (!BN_rand(r, n, 0, 0)) return 0;
+                       if (!BN_rand(r, n, -1, 0)) return 0;
                        }
                while (BN_cmp(r, range) >= 0);
                }
@@ -201,7 +204,7 @@ int BN_rand_range(BIGNUM *r, BIGNUM *min, BIGNUM *range)
                 * so  3*range (= 11..._2)  is exactly one bit longer than  range */
                do
                        {
-                       if (!BN_rand(r, n + 1, 0, 0)) return 0;
+                       if (!BN_rand(r, n + 1, -1, 0)) return 0;
                        /* If  r < 3*range,  use  r := r MOD range
                         * (which is either  r, r - range,  or  r - 2*range).
                         * Otherwise, iterate once more.
@@ -217,10 +220,5 @@ int        BN_rand_range(BIGNUM *r, BIGNUM *min, BIGNUM *range)
                while (BN_cmp(r, range) >= 0);
                }
 
-       if (min != NULL)
-               {
-               if (!BN_add(r, r, min)) return 0;
-               }
-       
        return 1;
        }