aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey/blinding.cpp
blob: cd2b3d11813430fb3ca9d887c5a76e5e300d33bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Blinding for public key operations
* (C) 1999-2010 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#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 {

// TODO: use Montgomery

Blinder::Blinder(const BigInt& modulus,
                 std::function<BigInt (const BigInt&)> fwd_func,
                 std::function<BigInt (const BigInt&)> inv_func)
   {
   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);

   m_e = fwd_func(k);
   m_d = inv_func(k);
   }

BigInt Blinder::blind(const BigInt& i) const
   {
   if(!m_reducer.initialized())
      throw std::runtime_error("Blinder not initialized, cannot blind");

   m_e = m_reducer.square(m_e);
   m_d = m_reducer.square(m_d);
   return m_reducer.multiply(i, m_e);
   }

BigInt Blinder::unblind(const BigInt& i) const
   {
   if(!m_reducer.initialized())
      throw std::runtime_error("Blinder not initialized, cannot unblind");

   return m_reducer.multiply(i, m_d);
   }

}