Coverage Report

Created: 2023-12-08 07:00

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