diff options
Diffstat (limited to 'src/lib/pubkey/blinding.cpp')
-rw-r--r-- | src/lib/pubkey/blinding.cpp | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/lib/pubkey/blinding.cpp b/src/lib/pubkey/blinding.cpp index 61da26a04..cd2b3d118 100644 --- a/src/lib/pubkey/blinding.cpp +++ b/src/lib/pubkey/blinding.cpp @@ -8,42 +8,50 @@ #include <botan/blinding.h> #include <botan/numthry.h> +#if defined(BOTAN_HAS_SYSTEM_RNG) + #include <botan/system_rng.h> +#else + #include <botan/auto_rng.h> +#endif + namespace Botan { -/* -* Blinder Constructor -*/ -Blinder::Blinder(const BigInt& e, const BigInt& d, const BigInt& n) +// TODO: use Montgomery + +Blinder::Blinder(const BigInt& modulus, + std::function<BigInt (const BigInt&)> fwd_func, + std::function<BigInt (const BigInt&)> inv_func) { - if(e < 1 || d < 1 || n < 1) - throw Invalid_Argument("Blinder: Arguments too small"); + m_reducer = Modular_Reducer(modulus); + +#if defined(BOTAN_HAS_SYSTEM_RNG) + auto& rng = system_rng(); +#else + AutoSeeded_RNG rng; +#endif + + const BigInt k(rng, modulus.bits() - 1); - reducer = Modular_Reducer(n); - this->e = e; - this->d = d; + m_e = fwd_func(k); + m_d = inv_func(k); } -/* -* Blind a number -*/ BigInt Blinder::blind(const BigInt& i) const { - if(!reducer.initialized()) - return i; + if(!m_reducer.initialized()) + throw std::runtime_error("Blinder not initialized, cannot blind"); - e = reducer.square(e); - d = reducer.square(d); - return reducer.multiply(i, e); + m_e = m_reducer.square(m_e); + m_d = m_reducer.square(m_d); + return m_reducer.multiply(i, m_e); } -/* -* Unblind a number -*/ BigInt Blinder::unblind(const BigInt& i) const { - if(!reducer.initialized()) - return i; - return reducer.multiply(i, d); + if(!m_reducer.initialized()) + throw std::runtime_error("Blinder not initialized, cannot unblind"); + + return m_reducer.multiply(i, m_d); } } |