From 788e524d35d01d90c56c825dbf63a96c3c42a32c Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 4 Mar 2010 17:38:59 +0000 Subject: New IF constructors, simplifies RSA/RW --- src/pubkey/if_algo/if_algo.cpp | 32 ++++++++++++++++++++++++++++++++ src/pubkey/if_algo/if_algo.h | 11 ++++++++++- src/pubkey/rsa/rsa.cpp | 30 ------------------------------ src/pubkey/rsa/rsa.h | 23 +++++++++++++++-------- src/pubkey/rw/rw.cpp | 30 ------------------------------ src/pubkey/rw/rw.h | 13 ++++++++++--- 6 files changed, 67 insertions(+), 72 deletions(-) diff --git a/src/pubkey/if_algo/if_algo.cpp b/src/pubkey/if_algo/if_algo.cpp index e0042fc1a..62f83ff00 100644 --- a/src/pubkey/if_algo/if_algo.cpp +++ b/src/pubkey/if_algo/if_algo.cpp @@ -78,6 +78,38 @@ IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(const AlgorithmIdentifier&, throw Decoding_Error("Unknown PKCS #1 key format version"); } +IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng, + const BigInt& prime1, + const BigInt& prime2, + const BigInt& exp, + const BigInt& d_exp, + const BigInt& mod) + { + p = prime1; + q = prime2; + e = exp; + d = d_exp; + n = mod; + + if(d == 0) + { + BigInt inv_for_d = lcm(p - 1, q - 1); + if(e.is_even()) + inv_for_d >>= 1; + + d = inverse_mod(e, inv_for_d); + } + + if(n == 0) n = p * q; + if(d1 == 0) d1 = d % (p - 1); + if(d2 == 0) d2 = d % (q - 1); + if(c == 0) c = inverse_mod(q, p); + + core = IF_Core(rng, e, n, d, p, q, d1, d2, c); + + load_check(rng); + } + /* * Algorithm Specific PKCS #8 Initialization Code */ diff --git a/src/pubkey/if_algo/if_algo.h b/src/pubkey/if_algo/if_algo.h index d5e3ce5b3..01e370854 100644 --- a/src/pubkey/if_algo/if_algo.h +++ b/src/pubkey/if_algo/if_algo.h @@ -24,6 +24,9 @@ class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key IF_Scheme_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion& key_bits); + IF_Scheme_PublicKey(const BigInt& n, const BigInt& e) : + n(n), e(e) {} + bool check_key(RandomNumberGenerator& rng, bool) const; AlgorithmIdentifier algorithm_identifier() const; @@ -59,6 +62,12 @@ class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey, public virtual Private_Key { public: + + IF_Scheme_PrivateKey(RandomNumberGenerator& rng, + const BigInt& prime1, const BigInt& prime2, + const BigInt& exp, const BigInt& d_exp, + const BigInt& mod); + IF_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion& key_bits); @@ -87,7 +96,7 @@ class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey, protected: IF_Scheme_PrivateKey() {} - virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + void PKCS8_load_hook(RandomNumberGenerator&, bool = false); BigInt d, p, q, d1, d2, c; }; diff --git a/src/pubkey/rsa/rsa.cpp b/src/pubkey/rsa/rsa.cpp index 33999f1cd..c606e5c53 100644 --- a/src/pubkey/rsa/rsa.cpp +++ b/src/pubkey/rsa/rsa.cpp @@ -13,16 +13,6 @@ namespace Botan { -/* -* RSA_PublicKey Constructor -*/ -RSA_PublicKey::RSA_PublicKey(const BigInt& mod, const BigInt& exp) - { - n = mod; - e = exp; - core = IF_Core(e, n); - } - /* * RSA Public Operation */ @@ -75,26 +65,6 @@ RSA_PrivateKey::RSA_PrivateKey(RandomNumberGenerator& rng, throw Self_Test_Failure(algo_name() + " private key generation failed"); } -/* -* RSA_PrivateKey Constructor -*/ -RSA_PrivateKey::RSA_PrivateKey(RandomNumberGenerator& rng, - const BigInt& prime1, const BigInt& prime2, - const BigInt& exp, const BigInt& d_exp, - const BigInt& mod) - { - p = prime1; - q = prime2; - e = exp; - d = d_exp; - n = mod; - - if(d == 0) - d = inverse_mod(e, lcm(p - 1, q - 1)); - - PKCS8_load_hook(rng); - } - /* * RSA Private Operation */ diff --git a/src/pubkey/rsa/rsa.h b/src/pubkey/rsa/rsa.h index c1210d22a..ce79e2440 100644 --- a/src/pubkey/rsa/rsa.h +++ b/src/pubkey/rsa/rsa.h @@ -39,7 +39,12 @@ class BOTAN_DLL RSA_PublicKey : public PK_Encrypting_Key, * @arg n the modulus * @arg e the exponent */ - RSA_PublicKey(const BigInt& n, const BigInt& e); + RSA_PublicKey(const BigInt& n, const BigInt& e) : + IF_Scheme_PublicKey(n, e) + { + core = IF_Core(e, n); + } + protected: RSA_PublicKey() {} BigInt public_op(const BigInt&) const; @@ -71,19 +76,21 @@ class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey, /** * Construct a private key from the specified parameters. - * @param rng the random number generator to use - * @param prime1 the first prime - * @param prime2 the second prime - * @param exp the exponent - * @param d_exp if specified, this has to be d with + * @param rng a random number generator + * @param p the first prime + * @param q the second prime + * @param e the exponent + * @param d if specified, this has to be d with * exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to * the constructor to calculate it. * @param n if specified, this must be n = p * q. Leave it as 0 * if you wish to the constructor to calculate it. */ RSA_PrivateKey(RandomNumberGenerator& rng, - const BigInt& p, const BigInt& q, const BigInt& e, - const BigInt& d = 0, const BigInt& n = 0); + const BigInt& p, const BigInt& q, + const BigInt& e, const BigInt& d = 0, + const BigInt& n = 0) : + IF_Scheme_PrivateKey(rng, p, q, e, d, n) {} /** * Create a new private key with the specified bit length diff --git a/src/pubkey/rw/rw.cpp b/src/pubkey/rw/rw.cpp index f6d67bbde..259e53a26 100644 --- a/src/pubkey/rw/rw.cpp +++ b/src/pubkey/rw/rw.cpp @@ -14,16 +14,6 @@ namespace Botan { -/* -* RW_PublicKey Constructor -*/ -RW_PublicKey::RW_PublicKey(const BigInt& mod, const BigInt& exp) - { - n = mod; - e = exp; - core = IF_Core(e, n); - } - /* * Rabin-Williams Public Operation */ @@ -75,26 +65,6 @@ RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng, throw Self_Test_Failure(algo_name() + " private key generation failed"); } -/* -* RW_PrivateKey Constructor -*/ -RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng, - const BigInt& prime1, const BigInt& prime2, - const BigInt& exp, const BigInt& d_exp, - const BigInt& mod) - { - p = prime1; - q = prime2; - e = exp; - d = d_exp; - n = mod; - - if(d == 0) - d = inverse_mod(e, lcm(p - 1, q - 1) >> 1); - - PKCS8_load_hook(rng); - } - /* * Rabin-Williams Signature Operation */ diff --git a/src/pubkey/rw/rw.h b/src/pubkey/rw/rw.h index bc8f053b6..d2411d630 100644 --- a/src/pubkey/rw/rw.h +++ b/src/pubkey/rw/rw.h @@ -30,7 +30,12 @@ class BOTAN_DLL RW_PublicKey : public PK_Verifying_with_MR_Key, core = IF_Core(e, n); } - RW_PublicKey(const BigInt& mod, const BigInt& exponent); + RW_PublicKey(const BigInt& mod, const BigInt& exponent) : + IF_Scheme_PublicKey(mod, exponent) + { + core = IF_Core(e, n); + } + protected: RW_PublicKey() {} BigInt public_op(const BigInt&) const; @@ -58,8 +63,10 @@ class BOTAN_DLL RW_PrivateKey : public RW_PublicKey, } RW_PrivateKey(RandomNumberGenerator& rng, - const BigInt&, const BigInt&, const BigInt&, - const BigInt& = 0, const BigInt& = 0); + const BigInt& p, const BigInt& q, + const BigInt& e, const BigInt& d = 0, + const BigInt& n = 0) : + IF_Scheme_PrivateKey(rng, p, q, e, d, n) {} RW_PrivateKey(RandomNumberGenerator& rng, u32bit bits, u32bit = 2); }; -- cgit v1.2.3