Botan 3.9.0
Crypto and TLS for C&
blinding.h
Go to the documentation of this file.
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/internal/barrett.h>
13#include <functional>
14
15namespace Botan {
16
18
19/**
20* Blinding Function Object.
21*/
22class Blinder final {
23 public:
24 /**
25 * Normally blinding is performed by choosing a random starting point (plus
26 * its inverse, of a form appropriate to the algorithm being blinded), and
27 * then choosing new blinding operands by successive squaring of both
28 * values. This is much faster than computing a new starting point but
29 * introduces some possible corelation
30 *
31 * To avoid possible leakage problems in long-running processes, the blinder
32 * periodically reinitializes the sequence. This value specifies how often
33 * a new sequence should be started.
34 *
35 * If set to zero, reinitialization is disabled
36 */
37 static constexpr size_t ReinitInterval = 64;
38
39 /**
40 * Blind a value.
41 *
42 * The blinding nonce k is freshly generated after ReinitInterval
43 * calls to blind().
44 *
45 * ReinitInterval = 0 means a fresh nonce is only generated once.
46 * On every other call, the next nonce is derived via modular squaring.
47 *
48 * @param x value to blind
49 * @return blinded value
50 */
51 BigInt blind(const BigInt& x) const;
52
53 /**
54 * Unblind a value.
55 * @param x value to unblind
56 * @return unblinded value
57 */
58 BigInt unblind(const BigInt& x) const;
59
60 /**
61 * @param reducer precomputed Barrett reduction for the modulus
62 * @param rng the RNG to use for generating the nonce
63 * @param fwd_func a function that calculates the modular
64 * exponentiation of the public exponent and the given value (the nonce)
65 * @param inv_func a function that calculates the modular inverse
66 * of the given value (the nonce)
67 *
68 * @note Lifetime: The rng and reducer arguments are captured by
69 * reference and must live as long as the Blinder does
70 */
71 Blinder(const Barrett_Reduction& reducer,
73 std::function<BigInt(const BigInt&)> fwd_func,
74 std::function<BigInt(const BigInt&)> inv_func);
75
76 Blinder(const Blinder&) = delete;
77 Blinder(Blinder&&) = default;
78 Blinder& operator=(const Blinder&) = delete;
80 ~Blinder() = default;
81
82 RandomNumberGenerator& rng() const { return m_rng; }
83
84 private:
85 BigInt blinding_nonce() const;
86
87 const Barrett_Reduction& m_reducer;
89 std::function<BigInt(const BigInt&)> m_fwd_fn;
90 std::function<BigInt(const BigInt&)> m_inv_fn;
91 size_t m_modulus_bits = 0;
92
93 mutable BigInt m_e, m_d;
94 mutable size_t m_counter = 0;
95};
96
97} // namespace Botan
98
99#endif
Blinder & operator=(const Blinder &)=delete
BigInt blind(const BigInt &x) const
Definition blinding.cpp:31
Blinder(Blinder &&)=default
Blinder(const Blinder &)=delete
~Blinder()=default
static constexpr size_t ReinitInterval
Definition blinding.h:37
Blinder & operator=(Blinder &&)=delete
RandomNumberGenerator & rng() const
Definition blinding.h:82
BigInt unblind(const BigInt &x) const
Definition blinding.cpp:47
Blinder(const Barrett_Reduction &reducer, RandomNumberGenerator &rng, std::function< BigInt(const BigInt &)> fwd_func, std::function< BigInt(const BigInt &)> inv_func)
Definition blinding.cpp:12