/src/botan/build/include/botan/internal/blinding.h
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 | | #ifndef BOTAN_BLINDER_H_ |
9 | | #define BOTAN_BLINDER_H_ |
10 | | |
11 | | #include <botan/bigint.h> |
12 | | #include <botan/reducer.h> |
13 | | #include <functional> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | class RandomNumberGenerator; |
18 | | |
19 | | /** |
20 | | * Blinding Function Object. |
21 | | */ |
22 | | class Blinder final |
23 | | { |
24 | | public: |
25 | | /** |
26 | | * Blind a value. |
27 | | * The blinding nonce k is freshly generated after |
28 | | * BOTAN_BLINDING_REINIT_INTERVAL calls to blind(). |
29 | | * BOTAN_BLINDING_REINIT_INTERVAL = 0 means a fresh |
30 | | * nonce is only generated once. On every other call, |
31 | | * an updated nonce is used for blinding: k' = k*k mod n. |
32 | | * @param x value to blind |
33 | | * @return blinded value |
34 | | */ |
35 | | BigInt blind(const BigInt& x) const; |
36 | | |
37 | | /** |
38 | | * Unblind a value. |
39 | | * @param x value to unblind |
40 | | * @return unblinded value |
41 | | */ |
42 | | BigInt unblind(const BigInt& x) const; |
43 | | |
44 | | /** |
45 | | * @param modulus the modulus |
46 | | * @param rng the RNG to use for generating the nonce |
47 | | * @param fwd_func a function that calculates the modular |
48 | | * exponentiation of the public exponent and the given value (the nonce) |
49 | | * @param inv_func a function that calculates the modular inverse |
50 | | * of the given value (the nonce) |
51 | | */ |
52 | | Blinder(const BigInt& modulus, |
53 | | RandomNumberGenerator& rng, |
54 | | std::function<BigInt (const BigInt&)> fwd_func, |
55 | | std::function<BigInt (const BigInt&)> inv_func); |
56 | | |
57 | | Blinder(const Blinder&) = delete; |
58 | | |
59 | | Blinder& operator=(const Blinder&) = delete; |
60 | | |
61 | 0 | RandomNumberGenerator& rng() const { return m_rng; } |
62 | | |
63 | | private: |
64 | | BigInt blinding_nonce() const; |
65 | | |
66 | | Modular_Reducer m_reducer; |
67 | | RandomNumberGenerator& m_rng; |
68 | | std::function<BigInt (const BigInt&)> m_fwd_fn; |
69 | | std::function<BigInt (const BigInt&)> m_inv_fn; |
70 | | size_t m_modulus_bits = 0; |
71 | | |
72 | | mutable BigInt m_e, m_d; |
73 | | mutable size_t m_counter = 0; |
74 | | }; |
75 | | |
76 | | } |
77 | | |
78 | | #endif |