/src/botan/src/lib/pubkey/blinding/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/internal/blinding.h> |
9 | | |
10 | | namespace Botan { |
11 | | |
12 | | Blinder::Blinder(const Modular_Reducer& reducer, |
13 | | RandomNumberGenerator& rng, |
14 | | std::function<BigInt(const BigInt&)> fwd, |
15 | | std::function<BigInt(const BigInt&)> inv) : |
16 | 0 | m_reducer(reducer), |
17 | 0 | m_rng(rng), |
18 | 0 | m_fwd_fn(std::move(fwd)), |
19 | 0 | m_inv_fn(std::move(inv)), |
20 | 0 | m_modulus_bits(reducer.get_modulus().bits()), |
21 | 0 | m_e{}, |
22 | 0 | m_d{}, |
23 | 0 | m_counter{} { |
24 | 0 | const BigInt k = blinding_nonce(); |
25 | 0 | m_e = m_fwd_fn(k); |
26 | 0 | m_d = m_inv_fn(k); |
27 | 0 | } |
28 | | |
29 | 0 | BigInt Blinder::blinding_nonce() const { |
30 | 0 | return BigInt(m_rng, m_modulus_bits - 1); |
31 | 0 | } |
32 | | |
33 | 0 | BigInt Blinder::blind(const BigInt& i) const { |
34 | 0 | ++m_counter; |
35 | |
|
36 | 0 | if((ReinitInterval > 0) && (m_counter > ReinitInterval)) { |
37 | 0 | const BigInt k = blinding_nonce(); |
38 | 0 | m_e = m_fwd_fn(k); |
39 | 0 | m_d = m_inv_fn(k); |
40 | 0 | m_counter = 0; |
41 | 0 | } else { |
42 | 0 | m_e = m_reducer.square(m_e); |
43 | 0 | m_d = m_reducer.square(m_d); |
44 | 0 | } |
45 | |
|
46 | 0 | return m_reducer.multiply(i, m_e); |
47 | 0 | } |
48 | | |
49 | 0 | BigInt Blinder::unblind(const BigInt& i) const { |
50 | 0 | return m_reducer.multiply(i, m_d); |
51 | 0 | } |
52 | | |
53 | | } // namespace Botan |