Botan 3.6.1
Crypto and TLS for C&
monty_exp.cpp
Go to the documentation of this file.
1/*
2* Montgomery Exponentiation
3* (C) 1999-2010,2012,2018 Jack Lloyd
4* 2016 Matthias Gierlings
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/monty_exp.h>
10
11#include <botan/numthry.h>
12#include <botan/reducer.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/monty.h>
15#include <botan/internal/rounding.h>
16
17namespace Botan {
18
19class Montgomery_Exponentation_State final {
20 public:
21 Montgomery_Exponentation_State(const std::shared_ptr<const Montgomery_Params>& params,
22 const BigInt& g,
23 size_t window_bits,
24 bool const_time);
25
26 BigInt exponentiation(const BigInt& k, size_t max_k_bits) const;
27
28 BigInt exponentiation_vartime(const BigInt& k) const;
29
30 private:
31 std::shared_ptr<const Montgomery_Params> m_params;
32 std::vector<Montgomery_Int> m_g;
33 size_t m_window_bits;
34};
35
36Montgomery_Exponentation_State::Montgomery_Exponentation_State(const std::shared_ptr<const Montgomery_Params>& params,
37 const BigInt& g,
38 size_t window_bits,
39 bool const_time) :
40 m_params(params), m_window_bits(window_bits == 0 ? 4 : window_bits) {
41 BOTAN_ARG_CHECK(g < m_params->p(), "Montgomery base too big");
42
43 if(m_window_bits < 1 || m_window_bits > 12) { // really even 8 is too large ...
44 throw Invalid_Argument("Invalid window bits for Montgomery exponentiation");
45 }
46
47 const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
48
49 m_g.reserve(window_size);
50
51 m_g.push_back(Montgomery_Int(m_params, m_params->R1(), false));
52
53 m_g.push_back(Montgomery_Int(m_params, g));
54
55 for(size_t i = 2; i != window_size; ++i) {
56 m_g.push_back(m_g[1] * m_g[i - 1]);
57 }
58
59 // Resize each element to exactly p words
60 for(size_t i = 0; i != window_size; ++i) {
61 m_g[i].fix_size();
62 }
63
64 if(const_time) {
65 CT::poison_range(m_g);
66 }
67}
68
69namespace {
70
71void const_time_lookup(secure_vector<word>& output, const std::vector<Montgomery_Int>& g, size_t nibble) {
72 BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
73
74 const size_t words = output.size();
75
76 clear_mem(output.data(), output.size());
77
78 for(size_t i = 0; i != g.size(); i += 2) {
79 const secure_vector<word>& vec_0 = g[i].repr().get_word_vector();
80 const secure_vector<word>& vec_1 = g[i + 1].repr().get_word_vector();
81
82 BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
83
84 const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
85 const auto mask_1 = CT::Mask<word>::is_equal(nibble, i + 1);
86
87 for(size_t w = 0; w != words; ++w) {
88 output[w] |= mask_0.if_set_return(vec_0[w]);
89 output[w] |= mask_1.if_set_return(vec_1[w]);
90 }
91 }
92}
93
94} // namespace
95
96BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const {
97 BOTAN_DEBUG_ASSERT(scalar.bits() <= max_k_bits);
98 // TODO add a const-time implementation of above assert and use it in release builds
99
100 const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
101
102 if(exp_nibbles == 0) {
103 return BigInt::one();
104 }
105
106 secure_vector<word> e_bits(m_params->p_words());
107 secure_vector<word> ws;
108
109 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits));
110 Montgomery_Int x(m_params, e_bits.data(), e_bits.size(), false);
111
112 for(size_t i = exp_nibbles - 1; i > 0; --i) {
113 x.square_this_n_times(ws, m_window_bits);
114 const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits * (i - 1), m_window_bits));
115 x.mul_by(e_bits, ws);
116 }
117
118 CT::unpoison(x);
119 return x.value();
120}
121
122BigInt Montgomery_Exponentation_State::exponentiation_vartime(const BigInt& scalar) const {
123 const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
124
125 secure_vector<word> ws;
126
127 if(exp_nibbles == 0) {
128 return BigInt::one();
129 }
130
131 Montgomery_Int x = m_g[scalar.get_substring(m_window_bits * (exp_nibbles - 1), m_window_bits)];
132
133 for(size_t i = exp_nibbles - 1; i > 0; --i) {
134 x.square_this_n_times(ws, m_window_bits);
135
136 const uint32_t nibble = scalar.get_substring(m_window_bits * (i - 1), m_window_bits);
137 if(nibble > 0) {
138 x.mul_by(m_g[nibble], ws);
139 }
140 }
141
142 CT::unpoison(x);
143 return x.value();
144}
145
146std::shared_ptr<const Montgomery_Exponentation_State> monty_precompute(
147 const std::shared_ptr<const Montgomery_Params>& params, const BigInt& g, size_t window_bits, bool const_time) {
148 return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
149}
150
151BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state, const BigInt& k, size_t max_k_bits) {
152 return precomputed_state.exponentiation(k, max_k_bits);
153}
154
155BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state, const BigInt& k) {
156 return precomputed_state.exponentiation_vartime(k);
157}
158
159BigInt monty_multi_exp(const std::shared_ptr<const Montgomery_Params>& params_p,
160 const BigInt& x_bn,
161 const BigInt& z1,
162 const BigInt& y_bn,
163 const BigInt& z2) {
164 if(z1.is_negative() || z2.is_negative()) {
165 throw Invalid_Argument("multi_exponentiate exponents must be positive");
166 }
167
168 const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
169
171
172 const Montgomery_Int one(params_p, params_p->R1(), false);
173 //const Montgomery_Int one(params_p, 1);
174
175 const Montgomery_Int x1(params_p, x_bn);
176 const Montgomery_Int x2 = x1.square(ws);
177 const Montgomery_Int x3 = x2.mul(x1, ws);
178
179 const Montgomery_Int y1(params_p, y_bn);
180 const Montgomery_Int y2 = y1.square(ws);
181 const Montgomery_Int y3 = y2.mul(y1, ws);
182
183 const Montgomery_Int y1x1 = y1.mul(x1, ws);
184 const Montgomery_Int y1x2 = y1.mul(x2, ws);
185 const Montgomery_Int y1x3 = y1.mul(x3, ws);
186
187 const Montgomery_Int y2x1 = y2.mul(x1, ws);
188 const Montgomery_Int y2x2 = y2.mul(x2, ws);
189 const Montgomery_Int y2x3 = y2.mul(x3, ws);
190
191 const Montgomery_Int y3x1 = y3.mul(x1, ws);
192 const Montgomery_Int y3x2 = y3.mul(x2, ws);
193 const Montgomery_Int y3x3 = y3.mul(x3, ws);
194
195 const Montgomery_Int* M[16] = {&one,
196 &x1, // 0001
197 &x2, // 0010
198 &x3, // 0011
199 &y1, // 0100
200 &y1x1,
201 &y1x2,
202 &y1x3,
203 &y2, // 1000
204 &y2x1,
205 &y2x2,
206 &y2x3,
207 &y3, // 1100
208 &y3x1,
209 &y3x2,
210 &y3x3};
211
212 Montgomery_Int H = one;
213
214 for(size_t i = 0; i != z_bits; i += 2) {
215 if(i > 0) {
216 H.square_this(ws);
217 H.square_this(ws);
218 }
219
220 const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
221 const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
222
223 const uint32_t z12 = (4 * z2_b) + z1_b;
224
225 H.mul_by(*M[z12], ws);
226 }
227
228 return H.value();
229}
230
231} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
size_t bits() const
Definition bigint.cpp:295
bool is_negative() const
Definition bigint.h:560
uint32_t get_substring(size_t offset, size_t length) const
Definition bigint.cpp:227
Montgomery_Int square(secure_vector< word > &ws) const
Definition monty.cpp:374
BigInt value() const
Definition monty.cpp:294
Montgomery_Int & square_this(secure_vector< word > &ws)
Definition monty.cpp:369
Montgomery_Int mul(const Montgomery_Int &other, secure_vector< word > &ws) const
Definition monty.cpp:338
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:342
int(* final)(unsigned char *, CTX *)
constexpr size_t round_up(size_t n, size_t align_to)
Definition rounding.h:25
BigInt monty_multi_exp(const std::shared_ptr< const Montgomery_Params > &params_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
BigInt monty_execute_vartime(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k)
BigInt monty_execute(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:120
std::shared_ptr< const Montgomery_Exponentation_State > monty_precompute(const std::shared_ptr< const Montgomery_Params > &params, const BigInt &g, size_t window_bits, bool const_time)