diff options
author | Bodo Möller <bodo@openssl.org> | 2001-12-14 10:09:29 +0000 |
---|---|---|
committer | Bodo Möller <bodo@openssl.org> | 2001-12-14 10:09:29 +0000 |
commit | 66df02fd98959497ad12dc50b7629e0ca87b8fe6 (patch) | |
tree | a67c9166cee631900780286cb2b0bb43836b1865 /crypto | |
parent | 206eb6a11d35f91b0165ac8fb597f71972e84489 (diff) |
fix BN_rand_range
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/bn/bn_rand.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/crypto/bn/bn_rand.c b/crypto/bn/bn_rand.c index b9ce9e5d3f..9e08ccd22e 100644 --- a/crypto/bn/bn_rand.c +++ b/crypto/bn/bn_rand.c @@ -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; } |