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