diff options
author | lloyd <[email protected]> | 2008-06-20 11:39:33 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-06-20 11:39:33 +0000 |
commit | 770a9850f1a7c6c65b0316503b99798a6ff910dd (patch) | |
tree | 8e10281d15a1095b2151a1d1482898d75251589f | |
parent | 8f32a90b0aa6ab873c1d5337947e3642c640e3a4 (diff) |
Consolidate the two DH_PrivateKey constructors into a single one taking
a RNG reference, a group, and an (optional) private key. The public key
is now always rederived from the private.
-rw-r--r-- | checks/pk.cpp | 30 | ||||
-rw-r--r-- | checks/pk_bench.cpp | 4 | ||||
-rw-r--r-- | include/dh.h | 4 | ||||
-rw-r--r-- | src/dh.cpp | 33 |
4 files changed, 35 insertions, 36 deletions
diff --git a/checks/pk.cpp b/checks/pk.cpp index 8498c8291..93007c8d8 100644 --- a/checks/pk.cpp +++ b/checks/pk.cpp @@ -427,9 +427,11 @@ u32bit validate_dh(const std::string& algo, if(str.size() != 5 && str.size() != 6) throw Exception("Invalid input from pk_valid.dat"); + RandomNumberGenerator& rng = global_state().prng_reference(); + DL_Group domain(to_bigint(str[0]), to_bigint(str[1])); - DH_PrivateKey mykey(domain, to_bigint(str[2]), 0); + DH_PrivateKey mykey(rng, domain, to_bigint(str[2])); DH_PublicKey otherkey(domain, to_bigint(str[3])); std::string kdf = algo.substr(3, std::string::npos); @@ -452,10 +454,12 @@ u32bit validate_dlies(const std::string& algo, if(str.size() != 6) throw Exception("Invalid input from pk_valid.dat"); + RandomNumberGenerator& rng = global_state().prng_reference(); + DL_Group domain(to_bigint(str[0]), to_bigint(str[1])); - DH_PrivateKey from(domain, to_bigint(str[2]), 0); - DH_PrivateKey to(domain, to_bigint(str[3]), 0); + DH_PrivateKey from(rng, domain, to_bigint(str[2])); + DH_PrivateKey to(rng, domain, to_bigint(str[3])); const std::string opt_str = algo.substr(6, std::string::npos); @@ -485,32 +489,34 @@ void do_pk_keygen_tests() /* Putting each key in a block reduces memory pressure, speeds it up */ #define IF_SIG_KEY(TYPE, BITS) \ { \ - TYPE key(BITS, global_state().prng_reference()); \ - key.check_key(global_state().prng_reference(), true); \ + TYPE key(BITS, rng); \ + key.check_key(rng, true); \ std::cout << '.' << std::flush; \ } #define DL_SIG_KEY(TYPE, GROUP) \ { \ - TYPE key(DL_Group(GROUP), global_state().prng_reference()); \ - key.check_key(global_state().prng_reference(), true); \ + TYPE key(DL_Group(GROUP), rng); \ + key.check_key(rng, true); \ std::cout << '.' << std::flush; \ } #define DL_ENC_KEY(TYPE, GROUP) \ { \ - TYPE key(DL_Group(GROUP), global_state().prng_reference()); \ - key.check_key(global_state().prng_reference(), true); \ - std::cout << '.' << std::flush; \ + TYPE key(DL_Group(GROUP), rng); \ + key.check_key(rng, true); \ + std::cout << '.' << std::flush; \ } #define DL_KEY(TYPE, GROUP) \ { \ - TYPE key(DL_Group(GROUP), global_state().prng_reference()); \ - key.check_key(global_state().prng_reference(), true); \ + TYPE key(rng, DL_Group(GROUP)); \ + key.check_key(rng, true); \ std::cout << '.' << std::flush; \ } + RandomNumberGenerator& rng = global_state().prng_reference(); + IF_SIG_KEY(RSA_PrivateKey, 1024); IF_SIG_KEY(RW_PrivateKey, 1024); diff --git a/checks/pk_bench.cpp b/checks/pk_bench.cpp index d00130ebd..8daa1fe91 100644 --- a/checks/pk_bench.cpp +++ b/checks/pk_bench.cpp @@ -108,8 +108,8 @@ void bench_pk(const std::string& algo, bool html, double seconds) { const std::string len_str = to_string(keylen[j]); - DH_PrivateKey key("modp/ietf/" + len_str, - global_state().prng_reference()); + DH_PrivateKey key(global_state().prng_reference(), + "modp/ietf/" + len_str); bench_kas(get_pk_kas(key, "Raw"), "DH-" + len_str, seconds, html); } diff --git a/include/dh.h b/include/dh.h index d7c2a3381..0e28bf73e 100644 --- a/include/dh.h +++ b/include/dh.h @@ -45,8 +45,8 @@ class BOTAN_DLL DH_PrivateKey : public DH_PublicKey, MemoryVector<byte> public_value() const; DH_PrivateKey() {} - DH_PrivateKey(const DL_Group&, RandomNumberGenerator&); - DH_PrivateKey(const DL_Group&, const BigInt&, const BigInt& = 0); + DH_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); private: void PKCS8_load_hook(RandomNumberGenerator&, bool = false); DH_Core core; diff --git a/src/dh.cpp b/src/dh.cpp index 8367a3bce..159eb0629 100644 --- a/src/dh.cpp +++ b/src/dh.cpp @@ -47,28 +47,21 @@ MemoryVector<byte> DH_PublicKey::public_value() const /************************************************* * Create a DH private key * *************************************************/ -DH_PrivateKey::DH_PrivateKey(const DL_Group& grp, - RandomNumberGenerator& rng) +DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, + const DL_Group& grp, + const BigInt& x_arg) { group = grp; - - const BigInt& p = group_p(); - x.randomize(rng, 2 * dl_work_factor(p.bits())); - - PKCS8_load_hook(rng, true); - } - -/************************************************* -* DH_PrivateKey Constructor * -*************************************************/ -DH_PrivateKey::DH_PrivateKey(const DL_Group& grp, const BigInt& x1, - const BigInt& y1) - { - group = grp; - y = y1; - x = x1; - - PKCS8_load_hook(global_state().prng_reference()); + x = x_arg; + + if(x == 0) + { + const BigInt& p = group_p(); + x.randomize(rng, 2 * dl_work_factor(p.bits())); + PKCS8_load_hook(rng, true); + } + else + PKCS8_load_hook(rng, false); } /************************************************* |