diff options
Diffstat (limited to 'src/lib/pubkey/rw/rw.cpp')
-rw-r--r-- | src/lib/pubkey/rw/rw.cpp | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/src/lib/pubkey/rw/rw.cpp b/src/lib/pubkey/rw/rw.cpp index aa92578af..bf6b647a1 100644 --- a/src/lib/pubkey/rw/rw.cpp +++ b/src/lib/pubkey/rw/rw.cpp @@ -28,19 +28,19 @@ RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng, if(exp < 2 || exp % 2 == 1) throw Invalid_Argument(algo_name() + ": Invalid encryption exponent"); - e = exp; + m_e = exp; do { - p = random_prime(rng, (bits + 1) / 2, e / 2, 3, 4); - q = random_prime(rng, bits - p.bits(), e / 2, ((p % 8 == 3) ? 7 : 3), 8); - n = p * q; - } while(n.bits() != bits); + m_p = random_prime(rng, (bits + 1) / 2, m_e / 2, 3, 4); + m_q = random_prime(rng, bits - m_p.bits(), m_e / 2, ((m_p % 8 == 3) ? 7 : 3), 8); + m_n = m_p * m_q; + } while(m_n.bits() != bits); - d = inverse_mod(e, lcm(p - 1, q - 1) >> 1); - d1 = d % (p - 1); - d2 = d % (q - 1); - c = inverse_mod(q, p); + m_d = inverse_mod(m_e, lcm(m_p - 1, m_q - 1) >> 1); + m_d1 = m_d % (m_p - 1); + m_d2 = m_d % (m_q - 1); + m_c = inverse_mod(m_q, m_p); gen_check(rng); } @@ -56,7 +56,7 @@ bool RW_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const if(!strong) return true; - if((e * d) % (lcm(p - 1, q - 1) / 2) != 1) + if((m_e * m_d) % (lcm(m_p - 1, m_q - 1) / 2) != 1) return false; return KeyPair::signature_consistency_check(rng, *this, "EMSA2(SHA-1)"); @@ -75,32 +75,32 @@ class RW_Signature_Operation : public PK_Ops::Signature_with_EMSA RW_Signature_Operation(const RW_PrivateKey& rw, const std::string& emsa) : PK_Ops::Signature_with_EMSA(emsa), - n(rw.get_n()), - e(rw.get_e()), - q(rw.get_q()), - c(rw.get_c()), - powermod_d1_p(rw.get_d1(), rw.get_p()), - powermod_d2_q(rw.get_d2(), rw.get_q()), - mod_p(rw.get_p()), - blinder(n, - [this](const BigInt& k) { return power_mod(k, e, n); }, - [this](const BigInt& k) { return inverse_mod(k, n); }) + m_n(rw.get_n()), + m_e(rw.get_e()), + m_q(rw.get_q()), + m_c(rw.get_c()), + m_powermod_d1_p(rw.get_d1(), rw.get_p()), + m_powermod_d2_q(rw.get_d2(), rw.get_q()), + m_mod_p(rw.get_p()), + m_blinder(m_n, + [this](const BigInt& k) { return power_mod(k, m_e, m_n); }, + [this](const BigInt& k) { return inverse_mod(k, m_n); }) { } - size_t max_input_bits() const override { return (n.bits() - 1); } + size_t max_input_bits() const override { return (m_n.bits() - 1); } secure_vector<byte> raw_sign(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) override; private: - const BigInt& n; - const BigInt& e; - const BigInt& q; - const BigInt& c; - - Fixed_Exponent_Power_Mod powermod_d1_p, powermod_d2_q; - Modular_Reducer mod_p; - Blinder blinder; + const BigInt& m_n; + const BigInt& m_e; + const BigInt& m_q; + const BigInt& m_c; + + Fixed_Exponent_Power_Mod m_powermod_d1_p, m_powermod_d2_q; + Modular_Reducer m_mod_p; + Blinder m_blinder; }; secure_vector<byte> @@ -109,23 +109,23 @@ RW_Signature_Operation::raw_sign(const byte msg[], size_t msg_len, { BigInt i(msg, msg_len); - if(i >= n || i % 16 != 12) + if(i >= m_n || i % 16 != 12) throw Invalid_Argument("Rabin-Williams: invalid input"); - if(jacobi(i, n) != 1) + if(jacobi(i, m_n) != 1) i >>= 1; - i = blinder.blind(i); + i = m_blinder.blind(i); - auto future_j1 = std::async(std::launch::async, powermod_d1_p, i); - const BigInt j2 = powermod_d2_q(i); + auto future_j1 = std::async(std::launch::async, m_powermod_d1_p, i); + const BigInt j2 = m_powermod_d2_q(i); BigInt j1 = future_j1.get(); - j1 = mod_p.reduce(sub_mul(j1, j2, c)); + j1 = m_mod_p.reduce(sub_mul(j1, j2, m_c)); - const BigInt r = blinder.unblind(mul_add(j1, q, j2)); + const BigInt r = m_blinder.unblind(mul_add(j1, m_q, j2)); - return BigInt::encode_1363(std::min(r, n - r), n.bytes()); + return BigInt::encode_1363(std::min(r, m_n - r), m_n.bytes()); } /** @@ -138,17 +138,17 @@ class RW_Verification_Operation : public PK_Ops::Verification_with_EMSA RW_Verification_Operation(const RW_PublicKey& rw, const std::string& emsa) : PK_Ops::Verification_with_EMSA(emsa), - n(rw.get_n()), powermod_e_n(rw.get_e(), rw.get_n()) + m_n(rw.get_n()), m_powermod_e_n(rw.get_e(), rw.get_n()) {} - size_t max_input_bits() const override { return (n.bits() - 1); } + size_t max_input_bits() const override { return (m_n.bits() - 1); } bool with_recovery() const override { return true; } secure_vector<byte> verify_mr(const byte msg[], size_t msg_len) override; private: - const BigInt& n; - Fixed_Exponent_Power_Mod powermod_e_n; + const BigInt& m_n; + Fixed_Exponent_Power_Mod m_powermod_e_n; }; secure_vector<byte> @@ -156,16 +156,16 @@ RW_Verification_Operation::verify_mr(const byte msg[], size_t msg_len) { BigInt m(msg, msg_len); - if((m > (n >> 1)) || m.is_negative()) + if((m > (m_n >> 1)) || m.is_negative()) throw Invalid_Argument("RW signature verification: m > n / 2 || m < 0"); - BigInt r = powermod_e_n(m); + BigInt r = m_powermod_e_n(m); if(r % 16 == 12) return BigInt::encode_locked(r); if(r % 8 == 6) return BigInt::encode_locked(2*r); - r = n - r; + r = m_n - r; if(r % 16 == 12) return BigInt::encode_locked(r); if(r % 8 == 6) |