summaryrefslogtreecommitdiffstats
path: root/crypto/bn/bn_rand.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/bn/bn_rand.c')
-rw-r--r--crypto/bn/bn_rand.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/crypto/bn/bn_rand.c b/crypto/bn/bn_rand.c
index bab4510345..f2c79b5e31 100644
--- a/crypto/bn/bn_rand.c
+++ b/crypto/bn/bn_rand.c
@@ -168,3 +168,14 @@ int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
return bnrand(2, rnd, bits, top, bottom);
}
#endif
+
+/* random number r: min <= r < max */
+int BN_rand_range(BIGNUM *r, BIGNUM *min, BIGNUM *max)
+ {
+ int n = BN_num_bits(max);
+ do
+ {
+ if (!BN_rand(r, n, 0, 0)) return 0;
+ } while ((min && BN_cmp(r, min) < 0) || BN_cmp(r, max) >= 0);
+ return 1;
+ }