aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-03-16 18:42:49 -0400
committerJack Lloyd <[email protected]>2016-03-16 18:42:49 -0400
commitb5d8783fccbd4b6686708fd4f2f84eaada3e8fed (patch)
tree75e6615a0c972da54f29593dd19f468b3a1fe0c3 /src/lib
parentf209329d885310fb510742317a20d1f51099b29e (diff)
Use rejection sampling in BigInt::random_integer
Avoids the test vector contortions in RSA-KEM
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/math/bigint/big_rand.cpp18
-rw-r--r--src/lib/rng/rng.h2
2 files changed, 10 insertions, 10 deletions
diff --git a/src/lib/math/bigint/big_rand.cpp b/src/lib/math/bigint/big_rand.cpp
index cfc1facee..73f3cf070 100644
--- a/src/lib/math/bigint/big_rand.cpp
+++ b/src/lib/math/bigint/big_rand.cpp
@@ -45,19 +45,17 @@ void BigInt::randomize(RandomNumberGenerator& rng,
BigInt BigInt::random_integer(RandomNumberGenerator& rng,
const BigInt& min, const BigInt& max)
{
- BigInt delta_upper_bound = max - min - 1;
+ BigInt r;
- if(delta_upper_bound < 0)
- throw Invalid_Argument("random_integer: invalid min/max values");
+ const size_t bits = max.bits();
- // Choose x in [0, delta_upper_bound]
- BigInt x;
- do {
- auto bitsize = delta_upper_bound.bits();
- x.randomize(rng, bitsize, false);
- } while(x > delta_upper_bound);
+ do
+ {
+ r.randomize(rng, bits, false);
+ }
+ while(r < min || r >= max);
- return min + x;
+ return r;
}
}
diff --git a/src/lib/rng/rng.h b/src/lib/rng/rng.h
index 2e29a713c..3fd3dcec8 100644
--- a/src/lib/rng/rng.h
+++ b/src/lib/rng/rng.h
@@ -132,6 +132,8 @@ class BOTAN_DLL RandomNumberGenerator
virtual ~RandomNumberGenerator() {}
};
+typedef RandomNumberGenerator RNG;
+
/**
* Null/stub RNG - fails if you try to use it for anything
*/