~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/lib/pubkey/blinding.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
   {
 
25
   const BigInt k = blinding_nonce();
 
26
   m_e = m_fwd_fn(k);
 
27
   m_d = m_inv_fn(k);
 
28
   }
 
29
 
 
30
BigInt Blinder::blinding_nonce() const
 
31
   {
 
32
   return BigInt(m_rng, m_modulus_bits - 1);
 
33
   }
 
34
 
 
35
BigInt Blinder::blind(const BigInt& i) const
 
36
   {
 
37
   if(!m_reducer.initialized())
 
38
      throw Exception("Blinder not initialized, cannot blind");
 
39
 
 
40
   ++m_counter;
 
41
 
 
42
   if((BOTAN_BLINDING_REINIT_INTERVAL > 0) && (m_counter > BOTAN_BLINDING_REINIT_INTERVAL))
 
43
      {
 
44
      const BigInt k = blinding_nonce();
 
45
      m_e = m_fwd_fn(k);
 
46
      m_d = m_inv_fn(k);
 
47
      m_counter = 0;
 
48
      }
 
49
   else
 
50
      {
 
51
      m_e = m_reducer.square(m_e);
 
52
      m_d = m_reducer.square(m_d);
 
53
      }
 
54
 
 
55
   return m_reducer.multiply(i, m_e);
 
56
   }
 
57
 
 
58
BigInt Blinder::unblind(const BigInt& i) const
 
59
   {
 
60
   if(!m_reducer.initialized())
 
61
      throw Exception("Blinder not initialized, cannot unblind");
 
62
 
 
63
   return m_reducer.multiply(i, m_d);
 
64
   }
 
65
 
 
66
}