Botan  2.1.0
Crypto and TLS for C++11
blinding.cpp
Go to the documentation of this file.
1 /*
2 * Blinding for public key operations
3 * (C) 1999-2010,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/blinding.h>
9 #include <botan/numthry.h>
10 
11 namespace Botan {
12 
13 Blinder::Blinder(const BigInt& modulus,
15  std::function<BigInt (const BigInt&)> fwd,
16  std::function<BigInt (const BigInt&)> inv) :
17  m_reducer(modulus),
18  m_rng(rng),
19  m_fwd_fn(fwd),
20  m_inv_fn(inv),
21  m_modulus_bits(modulus.bits()),
22  m_e{},
23  m_d{},
24  m_counter{}
25  {
26  const BigInt k = blinding_nonce();
27  m_e = m_fwd_fn(k);
28  m_d = m_inv_fn(k);
29  }
30 
31 BigInt Blinder::blinding_nonce() const
32  {
33  return BigInt(m_rng, m_modulus_bits - 1);
34  }
35 
36 BigInt Blinder::blind(const BigInt& i) const
37  {
38  if(!m_reducer.initialized())
39  throw Exception("Blinder not initialized, cannot blind");
40 
41  ++m_counter;
42 
43  if((BOTAN_BLINDING_REINIT_INTERVAL > 0) && (m_counter > BOTAN_BLINDING_REINIT_INTERVAL))
44  {
45  const BigInt k = blinding_nonce();
46  m_e = m_fwd_fn(k);
47  m_d = m_inv_fn(k);
48  m_counter = 0;
49  }
50  else
51  {
52  m_e = m_reducer.square(m_e);
53  m_d = m_reducer.square(m_d);
54  }
55 
56  return m_reducer.multiply(i, m_e);
57  }
58 
60  {
61  if(!m_reducer.initialized())
62  throw Exception("Blinder not initialized, cannot unblind");
63 
64  return m_reducer.multiply(i, m_d);
65  }
66 
67 }
Blinder(const BigInt &modulus, RandomNumberGenerator &rng, std::function< BigInt(const BigInt &)> fwd_func, std::function< BigInt(const BigInt &)> inv_func)
Definition: blinding.cpp:13
bool initialized() const
Definition: reducer.h:50
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31
BigInt unblind(const BigInt &x) const
Definition: blinding.cpp:59
Definition: alg_id.cpp:13
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:52
BigInt blind(const BigInt &x) const
Definition: blinding.cpp:36
BigInt square(const BigInt &x) const
Definition: reducer.h:39