diff options
-rw-r--r-- | include/bigint.h | 1 | ||||
-rw-r--r-- | include/numthry.h | 1 | ||||
-rw-r--r-- | src/big_base.cpp | 9 | ||||
-rw-r--r-- | src/big_rand.cpp | 13 | ||||
-rw-r--r-- | src/dh.cpp | 2 | ||||
-rw-r--r-- | src/dl_group.cpp | 2 | ||||
-rw-r--r-- | src/elgamal.cpp | 8 | ||||
-rw-r--r-- | src/make_prm.cpp | 2 | ||||
-rw-r--r-- | src/numthry.cpp | 2 | ||||
-rw-r--r-- | src/pk_core.cpp | 14 | ||||
-rw-r--r-- | src/x509_ca.cpp | 3 |
11 files changed, 28 insertions, 29 deletions
diff --git a/include/bigint.h b/include/bigint.h index 5c4a9c997..2487fa91c 100644 --- a/include/bigint.h +++ b/include/bigint.h @@ -102,6 +102,7 @@ class BOTAN_DLL BigInt BigInt(const BigInt&); BigInt(const std::string&); BigInt(const byte[], u32bit, Base = Binary); + BigInt(RandomNumberGenerator& rng, u32bit bits); BigInt(Sign, u32bit); BigInt(NumberType, u32bit); private: diff --git a/include/numthry.h b/include/numthry.h index 6ca06be10..c6313bdb7 100644 --- a/include/numthry.h +++ b/include/numthry.h @@ -58,7 +58,6 @@ bool BOTAN_DLL run_primality_tests(RandomNumberGenerator&, /************************************************* * Random Number Generation * *************************************************/ -BigInt BOTAN_DLL random_integer(RandomNumberGenerator&, u32bit); BigInt BOTAN_DLL random_integer(RandomNumberGenerator&, const BigInt&, const BigInt&); diff --git a/src/big_base.cpp b/src/big_base.cpp index 3b5562265..95896495d 100644 --- a/src/big_base.cpp +++ b/src/big_base.cpp @@ -90,6 +90,15 @@ BigInt::BigInt(const byte input[], u32bit length, Base base) } /************************************************* +* Construct a BigInt from an encoded BigInt * +*************************************************/ +BigInt::BigInt(RandomNumberGenerator& rng, u32bit bits) + { + set_sign(Positive); + randomize(rng, bits); + } + +/************************************************* * Swap this BigInt with another * *************************************************/ void BigInt::swap(BigInt& other) diff --git a/src/big_rand.cpp b/src/big_rand.cpp index 5e6ec594b..b8cad3a4c 100644 --- a/src/big_rand.cpp +++ b/src/big_rand.cpp @@ -44,17 +44,6 @@ void BigInt::randomize(RandomNumberGenerator& rng, } /************************************************* -* Generate a random integer * -*************************************************/ -BigInt random_integer(RandomNumberGenerator& rng, - u32bit bits) - { - BigInt x; - x.randomize(rng, bits); - return x; - } - -/************************************************* * Generate a random integer within given range * *************************************************/ BigInt random_integer(RandomNumberGenerator& rng, @@ -65,7 +54,7 @@ BigInt random_integer(RandomNumberGenerator& rng, if(range <= 0) throw Invalid_Argument("random_integer: invalid min/max values"); - return (min + (random_integer(rng, range.bits() + 2) % range)); + return (min + (BigInt(rng, range.bits() + 2) % range)); } /************************************************* diff --git a/src/dh.cpp b/src/dh.cpp index 08377f06d..cf83590d0 100644 --- a/src/dh.cpp +++ b/src/dh.cpp @@ -53,7 +53,7 @@ DH_PrivateKey::DH_PrivateKey(const DL_Group& grp, group = grp; const BigInt& p = group_p(); - x = random_integer(rng, 2 * dl_work_factor(p.bits())); + x.randomize(rng, 2 * dl_work_factor(p.bits())); PKCS8_load_hook(true); } diff --git a/src/dl_group.cpp b/src/dl_group.cpp index 7ec591b15..0e37c197e 100644 --- a/src/dl_group.cpp +++ b/src/dl_group.cpp @@ -65,7 +65,7 @@ DL_Group::DL_Group(RandomNumberGenerator& rng, BigInt X; while(p.bits() != pbits || !is_prime(p, rng)) { - X = random_integer(rng, pbits); + X.randomize(rng, pbits); p = X - (X % (2*q) - 1); } } diff --git a/src/elgamal.cpp b/src/elgamal.cpp index b815dd396..99cb7ff83 100644 --- a/src/elgamal.cpp +++ b/src/elgamal.cpp @@ -36,9 +36,8 @@ void ElGamal_PublicKey::X509_load_hook() SecureVector<byte> ElGamal_PublicKey::encrypt(const byte in[], u32bit length) const { - BigInt k = random_integer( - global_state().prng_reference(), - 2 * dl_work_factor(group_p().bits())); + BigInt k(global_state().prng_reference(), + 2 * dl_work_factor(group_p().bits())); return core.encrypt(in, length, k); } @@ -58,8 +57,7 @@ ElGamal_PrivateKey::ElGamal_PrivateKey(const DL_Group& grp, RandomNumberGenerator& rng) { group = grp; - - x = random_integer(rng, 2 * dl_work_factor(group_p().bits())); + x.randomize(rng, 2 * dl_work_factor(group_p().bits())); PKCS8_load_hook(true); } diff --git a/src/make_prm.cpp b/src/make_prm.cpp index 7d399b825..35d1dde38 100644 --- a/src/make_prm.cpp +++ b/src/make_prm.cpp @@ -29,7 +29,7 @@ BigInt random_prime(RandomNumberGenerator& rng, while(true) { - BigInt p = random_integer(rng, bits); + BigInt p(rng, bits); p.set_bit(bits - 2); p.set_bit(0); diff --git a/src/numthry.cpp b/src/numthry.cpp index 49d078f51..4f74fdc59 100644 --- a/src/numthry.cpp +++ b/src/numthry.cpp @@ -268,7 +268,7 @@ bool passes_mr_tests(RandomNumberGenerator& rng, BigInt nonce; for(u32bit j = 0; j != tests; ++j) { - if(verify) nonce = random_integer(rng, NONCE_BITS); + if(verify) nonce.randomize(rng, NONCE_BITS); else nonce = PRIMES[j]; if(!mr.passes_test(nonce)) diff --git a/src/pk_core.cpp b/src/pk_core.cpp index 6d02c1cd3..daee59273 100644 --- a/src/pk_core.cpp +++ b/src/pk_core.cpp @@ -31,7 +31,8 @@ IF_Core::IF_Core(RandomNumberGenerator& rng, if(d != 0) { - BigInt k = random_integer(rng, std::min(n.bits()-1, BLINDING_BITS)); + BigInt k(rng, std::min(n.bits()-1, BLINDING_BITS)); + if(k != 0) blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n); } @@ -182,8 +183,9 @@ ELG_Core::ELG_Core(const DL_Group& group, const BigInt& y, const BigInt& x) const BigInt& p = group.get_p(); p_bytes = p.bytes(); - BigInt k = random_integer(global_state().prng_reference(), - std::min(p.bits()-1, BLINDING_BITS)); + BigInt k(global_state().prng_reference(), + std::min(p.bits()-1, BLINDING_BITS)); + if(k != 0) blinder = Blinder(k, power_mod(k, x, p), p); } @@ -245,8 +247,10 @@ DH_Core::DH_Core(const DL_Group& group, const BigInt& x) op = Engine_Core::dh_op(group, x); const BigInt& p = group.get_p(); - BigInt k = random_integer(global_state().prng_reference(), - std::min(p.bits()-1, BLINDING_BITS)); + + BigInt k(global_state().prng_reference(), + std::min(p.bits()-1, BLINDING_BITS)); + if(k != 0) blinder = Blinder(k, power_mod(inverse_mod(k, p), x, p), p); } diff --git a/src/x509_ca.cpp b/src/x509_ca.cpp index 602649930..d3737108b 100644 --- a/src/x509_ca.cpp +++ b/src/x509_ca.cpp @@ -91,8 +91,7 @@ X509_Certificate X509_CA::make_cert(PK_Signer* signer, const u32bit X509_CERT_VERSION = 3; const u32bit SERIAL_BITS = 128; - BigInt serial_no = random_integer(global_state().prng_reference(), - SERIAL_BITS); + BigInt serial_no(global_state().prng_reference(), SERIAL_BITS); DataSource_Memory source(X509_Object::make_signed(signer, sig_algo, DER_Encoder().start_cons(SEQUENCE) |