Skip to content

Commit

Permalink
Have Blinder capture the Modular_Reducer by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Jan 28, 2025
1 parent 3c2fd2c commit e67f697
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/lib/prov/pkcs11/p11_rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ class PKCS11_RSA_Decryption_Operation final : public PK_Ops::Decryption {
RandomNumberGenerator& rng) :
m_key(key),
m_mechanism(MechanismWrapper::create_rsa_crypt_mechanism(padding)),
m_barrett_mod_n(Modular_Reducer::for_public_modulus(m_key.get_n())),
m_blinder(
m_key.get_n(),
Modular_Reducer::for_public_modulus(m_key.get_n()),
m_barrett_mod_n,
rng,
[this](const BigInt& k) { return power_mod(k, m_key.get_e(), m_key.get_n()); },
[this](const BigInt& k) { return inverse_mod_rsa_public_modulus(k, m_key.get_n()); }) {
Expand Down Expand Up @@ -161,6 +161,7 @@ class PKCS11_RSA_Decryption_Operation final : public PK_Ops::Decryption {
private:
const PKCS11_RSA_PrivateKey& m_key;
MechanismWrapper m_mechanism;
Modular_Reducer m_barrett_mod_n;
size_t m_bits = 0;
Blinder m_blinder;
};
Expand Down
5 changes: 2 additions & 3 deletions src/lib/pubkey/blinding/blinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@

namespace Botan {

Blinder::Blinder(const BigInt& modulus,
const Modular_Reducer& reducer,
Blinder::Blinder(const Modular_Reducer& reducer,
RandomNumberGenerator& rng,
std::function<BigInt(const BigInt&)> fwd,
std::function<BigInt(const BigInt&)> inv) :
m_reducer(reducer),
m_rng(rng),
m_fwd_fn(std::move(fwd)),
m_inv_fn(std::move(inv)),
m_modulus_bits(modulus.bits()),
m_modulus_bits(reducer.get_modulus().bits()),
m_e{},
m_d{},
m_counter{} {
Expand Down
10 changes: 6 additions & 4 deletions src/lib/pubkey/blinding/blinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ class Blinder final {
BigInt unblind(const BigInt& x) const;

/**
* @param modulus the modulus
* @param reducer precomputed Barrett reduction for the modulus
* @param rng the RNG to use for generating the nonce
* @param fwd_func a function that calculates the modular
* exponentiation of the public exponent and the given value (the nonce)
* @param inv_func a function that calculates the modular inverse
* of the given value (the nonce)
*
* @note Lifetime: The rng and reducer arguments are captured by
* reference and must live as long as the Blinder does
*/
Blinder(const BigInt& modulus,
const Modular_Reducer& reducer,
Blinder(const Modular_Reducer& reducer,
RandomNumberGenerator& rng,
std::function<BigInt(const BigInt&)> fwd_func,
std::function<BigInt(const BigInt&)> inv_func);
Expand All @@ -63,7 +65,7 @@ class Blinder final {
private:
BigInt blinding_nonce() const;

Modular_Reducer m_reducer;
const Modular_Reducer& m_reducer;
RandomNumberGenerator& m_rng;
std::function<BigInt(const BigInt&)> m_fwd_fn;
std::function<BigInt(const BigInt&)> m_inv_fn;
Expand Down
1 change: 0 additions & 1 deletion src/lib/pubkey/dh/dh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class DH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
m_key(key),
m_key_bits(m_key->private_key().bits()),
m_blinder(
m_key->group().get_p(),
m_key->group()._reducer_mod_p(),
rng,
[](const BigInt& k) { return k; },
Expand Down
1 change: 0 additions & 1 deletion src/lib/pubkey/elgamal/elgamal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class ElGamal_Decryption_Operation final : public PK_Ops::Decryption_with_EME {
PK_Ops::Decryption_with_EME(eme),
m_key(key),
m_blinder(
m_key->group().get_p(),
m_key->group()._reducer_mod_p(),
rng,
[](const BigInt& k) { return k; },
Expand Down
1 change: 0 additions & 1 deletion src/lib/pubkey/rsa/rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ class RSA_Private_Operation {
m_public(rsa.public_data()),
m_private(rsa.private_data()),
m_blinder(
m_public->get_n(),
m_public->reducer_mod_n(),
rng,
[this](const BigInt& k) { return m_public->public_op(k); },
Expand Down

0 comments on commit e67f697

Please sign in to comment.