Coverage Report

Created: 2020-09-16 07:52

/src/botan/src/lib/pubkey/blinding.cpp
Line
Count
Source (jump to first uncovered line)
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
10
namespace Botan {
11
12
Blinder::Blinder(const BigInt& modulus,
13
                 RandomNumberGenerator& rng,
14
                 std::function<BigInt (const BigInt&)> fwd,
15
                 std::function<BigInt (const BigInt&)> inv) :
16
      m_reducer(modulus),
17
      m_rng(rng),
18
      m_fwd_fn(fwd),
19
      m_inv_fn(inv),
20
      m_modulus_bits(modulus.bits()),
21
      m_e{},
22
      m_d{},
23
      m_counter{}
24
2.25k
   {
25
2.25k
   const BigInt k = blinding_nonce();
26
2.25k
   m_e = m_fwd_fn(k);
27
2.25k
   m_d = m_inv_fn(k);
28
2.25k
   }
29
30
BigInt Blinder::blinding_nonce() const
31
2.25k
   {
32
2.25k
   return BigInt(m_rng, m_modulus_bits - 1);
33
2.25k
   }
34
35
BigInt Blinder::blind(const BigInt& i) const
36
2.22k
   {
37
2.22k
   if(!m_reducer.initialized())
38
0
      throw Invalid_State("Blinder not initialized, cannot blind");
39
2.22k
40
2.22k
   ++m_counter;
41
2.22k
42
2.22k
   if((BOTAN_BLINDING_REINIT_INTERVAL > 0) && (m_counter > BOTAN_BLINDING_REINIT_INTERVAL))
43
0
      {
44
0
      const BigInt k = blinding_nonce();
45
0
      m_e = m_fwd_fn(k);
46
0
      m_d = m_inv_fn(k);
47
0
      m_counter = 0;
48
0
      }
49
2.22k
   else
50
2.22k
      {
51
2.22k
      m_e = m_reducer.square(m_e);
52
2.22k
      m_d = m_reducer.square(m_d);
53
2.22k
      }
54
2.22k
55
2.22k
   return m_reducer.multiply(i, m_e);
56
2.22k
   }
57
58
BigInt Blinder::unblind(const BigInt& i) const
59
2.22k
   {
60
2.22k
   if(!m_reducer.initialized())
61
0
      throw Invalid_State("Blinder not initialized, cannot unblind");
62
2.22k
63
2.22k
   return m_reducer.multiply(i, m_d);
64
2.22k
   }
65
66
}