fix BN_rand_range
authorBodo Möller <bodo@openssl.org>
Fri, 14 Dec 2001 10:09:29 +0000 (10:09 +0000)
committerBodo Möller <bodo@openssl.org>
Fri, 14 Dec 2001 10:09:29 +0000 (10:09 +0000)
CHANGES
crypto/bn/bn_rand.c

diff --git a/CHANGES b/CHANGES
index 1c9c7fd35c04544351d4b976111b101a1e645fbb..92cded83ef95d76373313ec1ca5d5e7439434f5f 100644 (file)
--- a/CHANGES
+++ b/CHANGES
          *) applies to 0.9.6a/0.9.6b/0.9.6c and 0.9.7
          +) applies to 0.9.7 only
 
+  *) Fix BN_rand_range bug pointed out by Dominikus Scherkl
+     <Dominikus.Scherkl@biodata.com>.  (The previous implementation
+     worked incorrectly for those cases where  range = 10..._2  and
+     3*range  is two bits longer than  range.)
+     [Bodo Moeller]
+
   *) Only add signing time to PKCS7 structures if it is not already present.
      [Steve Henson]
 
index b9ce9e5d3fb84fa819b40d4a35519ddd5a829d7b..9e08ccd22e78fca5cb4795cc35d3a6ab39429c19 100644 (file)
@@ -239,22 +239,15 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
 
        n = BN_num_bits(range); /* n > 0 */
 
+       /* BN_is_bit_set(range, n - 1) always holds */
+
        if (n == 1)
                {
                if (!BN_zero(r)) return 0;
                }
-       else if (BN_is_bit_set(range, n - 2))
-               {
-               do
-                       {
-                       /* range = 11..._2, so each iteration succeeds with probability >= .75 */
-                       if (!bn_rand(r, n, -1, 0)) return 0;
-                       }
-               while (BN_cmp(r, range) >= 0);
-               }
-       else
+       else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3))
                {
-               /* range = 10..._2,
+               /* range = 100..._2,
                 * so  3*range (= 11..._2)  is exactly one bit longer than  range */
                do
                        {
@@ -273,6 +266,15 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
                        }
                while (BN_cmp(r, range) >= 0);
                }
+       else
+               {
+               do
+                       {
+                       /* range = 11..._2  or  range = 101..._2 */
+                       if (!bn_rand(r, n, -1, 0)) return 0;
+                       }
+               while (BN_cmp(r, range) >= 0);
+               }
 
        return 1;
        }