1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* (C) 2015,2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/internal/point_mul.h>
#include <botan/rng.h>
#include <botan/reducer.h>
#include <botan/internal/rounding.h>
#include <botan/internal/ct_utils.h>
namespace Botan {
PointGFp multi_exponentiate(const PointGFp& x, const BigInt& z1,
const PointGFp& y, const BigInt& z2)
{
PointGFp_Multi_Point_Precompute xy_mul(x, y);
return xy_mul.multi_exp(z1, z2);
}
Blinded_Point_Multiply::Blinded_Point_Multiply(const PointGFp& base,
const BigInt& order,
size_t h) :
m_ws(PointGFp::WORKSPACE_SIZE),
m_order(order)
{
BOTAN_UNUSED(h);
m_point_mul.reset(new PointGFp_Var_Point_Precompute(base));
}
Blinded_Point_Multiply::~Blinded_Point_Multiply()
{
/* for ~unique_ptr */
}
PointGFp Blinded_Point_Multiply::blinded_multiply(const BigInt& scalar,
RandomNumberGenerator& rng)
{
return m_point_mul->mul(scalar, rng, m_order, m_ws);
}
PointGFp_Base_Point_Precompute::PointGFp_Base_Point_Precompute(const PointGFp& base,
const Modular_Reducer& mod_order) :
m_base_point(base),
m_mod_order(mod_order),
m_p_words(base.get_curve().get_p().sig_words()),
m_T_size(base.get_curve().get_p().bits() + PointGFp_SCALAR_BLINDING_BITS + 1)
{
std::vector<BigInt> ws(PointGFp::WORKSPACE_SIZE);
const size_t p_bits = base.get_curve().get_p().bits();
/*
* Some of the curves (eg secp160k1) have an order slightly larger than
* the size of the prime modulus. In all cases they are at most 1 bit
* longer. The +1 compensates for this.
*/
const size_t T_bits = round_up(p_bits + PointGFp_SCALAR_BLINDING_BITS + 1, 2) / 2;
std::vector<PointGFp> T(3*T_bits);
T.resize(3*T_bits);
T[0] = base;
T[1] = T[0];
T[1].mult2(ws);
T[2] = T[1];
T[2].add(T[0], ws);
for(size_t i = 1; i != T_bits; ++i)
{
T[3*i+0] = T[3*i - 2];
T[3*i+0].mult2(ws);
T[3*i+1] = T[3*i+0];
T[3*i+1].mult2(ws);
T[3*i+2] = T[3*i+1];
T[3*i+2].add(T[3*i+0], ws);
}
PointGFp::force_all_affine(T, ws[0].get_word_vector());
m_W.resize(T.size() * 2 * m_p_words);
word* p = &m_W[0];
for(size_t i = 0; i != T.size(); ++i)
{
T[i].get_x().encode_words(p, m_p_words);
p += m_p_words;
T[i].get_y().encode_words(p, m_p_words);
p += m_p_words;
}
}
PointGFp PointGFp_Base_Point_Precompute::mul(const BigInt& k,
RandomNumberGenerator& rng,
const BigInt& group_order,
std::vector<BigInt>& ws) const
{
if(k.is_negative())
throw Invalid_Argument("PointGFp_Base_Point_Precompute scalar must be positive");
// Choose a small mask m and use k' = k + m*order (Coron's 1st countermeasure)
const BigInt mask(rng, PointGFp_SCALAR_BLINDING_BITS);
// Instead of reducing k mod group order should we alter the mask size??
const BigInt scalar = m_mod_order.reduce(k) + group_order * mask;
const size_t windows = round_up(scalar.bits(), 2) / 2;
const size_t elem_size = 2*m_p_words;
BOTAN_ASSERT(windows <= m_W.size() / (3*elem_size),
"Precomputed sufficient values for scalar mult");
PointGFp R = m_base_point.zero();
if(ws.size() < PointGFp::WORKSPACE_SIZE)
ws.resize(PointGFp::WORKSPACE_SIZE);
// the precomputed multiples are not secret so use std::vector
std::vector<word> Wt(elem_size);
for(size_t i = 0; i != windows; ++i)
{
const size_t window = windows - i - 1;
const size_t base_addr = (3*window)*elem_size;
const word w = scalar.get_substring(2*window, 2);
const word w_is_1 = CT::is_equal<word>(w, 1);
const word w_is_2 = CT::is_equal<word>(w, 2);
const word w_is_3 = CT::is_equal<word>(w, 3);
for(size_t j = 0; j != elem_size; ++j)
{
const word w1 = m_W[base_addr + 0*elem_size + j];
const word w2 = m_W[base_addr + 1*elem_size + j];
const word w3 = m_W[base_addr + 2*elem_size + j];
Wt[j] = CT::select3<word>(w_is_1, w1, w_is_2, w2, w_is_3, w3, 0);
}
R.add_affine(&Wt[0], m_p_words, &Wt[m_p_words], m_p_words, ws);
if(i == 0)
{
/*
* Since we start with the top bit of the exponent we know the
* first window must have a non-zero element, and thus R is
* now a point other than the point at infinity.
*/
BOTAN_DEBUG_ASSERT(w != 0);
R.randomize_repr(rng, ws[0].get_word_vector());
}
}
BOTAN_DEBUG_ASSERT(R.on_the_curve());
return R;
}
PointGFp_Var_Point_Precompute::PointGFp_Var_Point_Precompute(const PointGFp& point)
{
m_window_bits = 4;
std::vector<BigInt> ws(PointGFp::WORKSPACE_SIZE);
m_U.resize(1U << m_window_bits);
m_U[0] = point.zero();
m_U[1] = point;
for(size_t i = 2; i < m_U.size(); i += 2)
{
m_U[i] = m_U[i/2].double_of(ws);
m_U[i+1] = m_U[i].plus(point, ws);
}
}
void PointGFp_Var_Point_Precompute::randomize_repr(RandomNumberGenerator& rng,
std::vector<BigInt>& ws_bn)
{
if(BOTAN_POINTGFP_RANDOMIZE_BLINDING_BITS <= 1)
return;
if(ws_bn.size() < 7)
ws_bn.resize(7);
BigInt& mask = ws_bn[0];
BigInt& mask2 = ws_bn[1];
BigInt& mask3 = ws_bn[2];
BigInt& new_x = ws_bn[3];
BigInt& new_y = ws_bn[4];
BigInt& new_z = ws_bn[5];
secure_vector<word>& ws = ws_bn[6].get_word_vector();
const CurveGFp& curve = m_U[0].get_curve();
// Skipping zero point since it can't be randomized
for(size_t i = 1; i != m_U.size(); ++i)
{
mask.randomize(rng, BOTAN_POINTGFP_RANDOMIZE_BLINDING_BITS, false);
// Easy way of ensuring mask != 0
mask.set_bit(0);
curve.sqr(mask2, mask, ws);
curve.mul(mask3, mask, mask2, ws);
curve.mul(new_x, m_U[i].get_x(), mask2, ws);
curve.mul(new_y, m_U[i].get_y(), mask3, ws);
curve.mul(new_z, m_U[i].get_z(), mask, ws);
m_U[i].swap_coords(new_x, new_y, new_z);
}
}
PointGFp PointGFp_Var_Point_Precompute::mul(const BigInt& k,
RandomNumberGenerator& rng,
const BigInt& group_order,
std::vector<BigInt>& ws) const
{
if(k.is_negative())
throw Invalid_Argument("PointGFp_Base_Point_Precompute scalar must be positive");
if(ws.size() < PointGFp::WORKSPACE_SIZE)
ws.resize(PointGFp::WORKSPACE_SIZE);
// Choose a small mask m and use k' = k + m*order (Coron's 1st countermeasure)
const BigInt mask(rng, PointGFp_SCALAR_BLINDING_BITS, false);
const BigInt scalar = k + group_order * mask;
const size_t scalar_bits = scalar.bits();
size_t windows = round_up(scalar_bits, m_window_bits) / m_window_bits;
PointGFp R = m_U[0];
if(windows > 0)
{
windows--;
const uint32_t nibble = scalar.get_substring(windows*m_window_bits, m_window_bits);
// cache side channel here, we are relying on blinding...
R.add(m_U[nibble], ws);
/*
Randomize after adding the first nibble as before the addition R
is zero, and we cannot effectively randomize the point
representation of the zero point.
*/
R.randomize_repr(rng);
while(windows)
{
R.mult2i(m_window_bits, ws);
const uint32_t inner_nibble = scalar.get_substring((windows-1)*m_window_bits, m_window_bits);
// cache side channel here, we are relying on blinding...
R.add(m_U[inner_nibble], ws);
windows--;
}
}
BOTAN_DEBUG_ASSERT(R.on_the_curve());
return R;
}
PointGFp_Multi_Point_Precompute::PointGFp_Multi_Point_Precompute(const PointGFp& x,
const PointGFp& y)
{
std::vector<BigInt> ws(PointGFp::WORKSPACE_SIZE);
PointGFp x2 = x;
x2.mult2(ws);
const PointGFp x3(x2.plus(x, ws));
PointGFp y2 = y;
y2.mult2(ws);
const PointGFp y3(y2.plus(y, ws));
m_M.reserve(15);
m_M.push_back(x);
m_M.push_back(x2);
m_M.push_back(x3);
m_M.push_back(y);
m_M.push_back(y.plus(x, ws));
m_M.push_back(y.plus(x2, ws));
m_M.push_back(y.plus(x3, ws));
m_M.push_back(y2);
m_M.push_back(y2.plus(x, ws));
m_M.push_back(y2.plus(x2, ws));
m_M.push_back(y2.plus(x3, ws));
m_M.push_back(y3);
m_M.push_back(y3.plus(x, ws));
m_M.push_back(y3.plus(x2, ws));
m_M.push_back(y3.plus(x3, ws));
PointGFp::force_all_affine(m_M, ws[0].get_word_vector());
}
PointGFp PointGFp_Multi_Point_Precompute::multi_exp(const BigInt& z1,
const BigInt& z2) const
{
std::vector<BigInt> ws(PointGFp::WORKSPACE_SIZE);
const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
PointGFp H = m_M[0].zero();
for(size_t i = 0; i != z_bits; i += 2)
{
if(i > 0)
{
H.mult2i(2, ws);
}
const uint8_t z1_b = z1.get_substring(z_bits - i - 2, 2);
const uint8_t z2_b = z2.get_substring(z_bits - i - 2, 2);
const uint8_t z12 = (4*z2_b) + z1_b;
// This function is not intended to be const time
if(z12)
{
H.add_affine(m_M[z12-1], ws);
}
}
if(z1.is_negative() != z2.is_negative())
H.negate();
return H;
}
}
|