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
|
/*
* Elliptic curves over GF(p) Montgomery Representation
* (C) 2014,2015 Jack Lloyd
* 2016 Matthias Gierlings
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/curve_gfp.h>
#include <botan/curve_nistp.h>
#include <botan/numthry.h>
#include <botan/reducer.h>
#include <botan/internal/mp_core.h>
#include <botan/internal/mp_asmi.h>
namespace Botan {
namespace {
class CurveGFp_Montgomery final : public CurveGFp_Repr
{
public:
CurveGFp_Montgomery(const BigInt& p, const BigInt& a, const BigInt& b) :
m_p(p), m_a(a), m_b(b),
m_p_words(m_p.sig_words()),
m_p_dash(monty_inverse(m_p.word_at(0)))
{
Modular_Reducer mod_p(m_p);
m_r.set_bit(m_p_words * BOTAN_MP_WORD_BITS);
m_r = mod_p.reduce(m_r);
m_r2 = mod_p.square(m_r);
m_r3 = mod_p.multiply(m_r, m_r2);
m_a_r = mod_p.multiply(m_r, m_a);
m_b_r = mod_p.multiply(m_r, m_b);
}
const BigInt& get_a() const override { return m_a; }
const BigInt& get_b() const override { return m_b; }
const BigInt& get_p() const override { return m_p; }
const BigInt& get_a_rep() const override { return m_a_r; }
const BigInt& get_b_rep() const override { return m_b_r; }
bool is_one(const BigInt& x) const override { return x == m_r; }
size_t get_p_words() const override { return m_p_words; }
size_t get_ws_size() const override { return 2*m_p_words + 4; }
BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override;
void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override;
void curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
secure_vector<word>& ws) const override;
void curve_sqr(BigInt& z, const BigInt& x,
secure_vector<word>& ws) const override;
private:
BigInt m_p;
BigInt m_a, m_b;
BigInt m_a_r, m_b_r;
size_t m_p_words; // cache of m_p.sig_words()
// Montgomery parameters
BigInt m_r, m_r2, m_r3;
word m_p_dash;
};
BigInt CurveGFp_Montgomery::invert_element(const BigInt& x, secure_vector<word>& ws) const
{
// Should we use Montgomery inverse instead?
const BigInt inv = inverse_mod(x, m_p);
BigInt res;
curve_mul(res, inv, m_r3, ws);
return res;
}
void CurveGFp_Montgomery::to_curve_rep(BigInt& x, secure_vector<word>& ws) const
{
const BigInt tx = x;
curve_mul(x, tx, m_r2, ws);
}
void CurveGFp_Montgomery::from_curve_rep(BigInt& z, secure_vector<word>& ws) const
{
if(ws.size() < get_ws_size())
ws.resize(get_ws_size());
const size_t output_size = 2*m_p_words + 2;
if(z.size() < output_size)
z.grow_to(output_size);
bigint_monty_redc(z.mutable_data(),
m_p.data(), m_p_words, m_p_dash,
ws.data(), ws.size());
}
void CurveGFp_Montgomery::curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
secure_vector<word>& ws) const
{
if(ws.size() < get_ws_size())
ws.resize(get_ws_size());
const size_t output_size = 2*m_p_words + 2;
if(z.size() < output_size)
z.grow_to(output_size);
BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
BOTAN_DEBUG_ASSERT(y.sig_words() <= m_p_words);
const size_t x_words = (x.size() >= m_p_words) ? m_p_words : x.sig_words();
const size_t y_words = (y.size() >= m_p_words) ? m_p_words : y.sig_words();
bigint_mul(z.mutable_data(), z.size(),
x.data(), x.size(), x_words,
y.data(), y.size(), y_words,
ws.data(), ws.size());
bigint_monty_redc(z.mutable_data(),
m_p.data(), m_p_words, m_p_dash,
ws.data(), ws.size());
}
void CurveGFp_Montgomery::curve_sqr(BigInt& z, const BigInt& x,
secure_vector<word>& ws) const
{
if(ws.size() < get_ws_size())
ws.resize(get_ws_size());
const size_t output_size = 2*m_p_words + 2;
if(z.size() < output_size)
z.grow_to(output_size);
const size_t x_words = (x.size() >= m_p_words) ? m_p_words : x.sig_words();
bigint_sqr(z.mutable_data(), z.size(),
x.data(), x.size(), x_words,
ws.data(), ws.size());
bigint_monty_redc(z.mutable_data(),
m_p.data(), m_p_words, m_p_dash,
ws.data(), ws.size());
}
class CurveGFp_NIST : public CurveGFp_Repr
{
public:
CurveGFp_NIST(size_t p_bits, const BigInt& a, const BigInt& b) :
m_a(a), m_b(b), m_p_words((p_bits + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS)
{
}
const BigInt& get_a() const override { return m_a; }
const BigInt& get_b() const override { return m_b; }
size_t get_p_words() const override { return m_p_words; }
size_t get_ws_size() const override { return 2*m_p_words + 4; }
const BigInt& get_a_rep() const override { return m_a; }
const BigInt& get_b_rep() const override { return m_b; }
bool is_one(const BigInt& x) const override { return x == 1; }
void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override
{ redc(x, ws); }
void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override
{ redc(x, ws); }
BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
void curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
secure_vector<word>& ws) const override;
void curve_sqr(BigInt& z, const BigInt& x,
secure_vector<word>& ws) const override;
private:
virtual void redc(BigInt& x, secure_vector<word>& ws) const = 0;
// Curve parameters
BigInt m_a, m_b;
size_t m_p_words; // cache of m_p.sig_words()
};
BigInt CurveGFp_NIST::invert_element(const BigInt& x, secure_vector<word>& ws) const
{
BOTAN_UNUSED(ws);
return inverse_mod(x, get_p());
}
void CurveGFp_NIST::curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
secure_vector<word>& ws) const
{
if(ws.size() < get_ws_size())
ws.resize(get_ws_size());
const size_t output_size = 2*m_p_words + 2;
if(z.size() < output_size)
z.grow_to(output_size);
const size_t x_words = (x.size() >= m_p_words) ? m_p_words : x.sig_words();
const size_t y_words = (y.size() >= m_p_words) ? m_p_words : y.sig_words();
bigint_mul(z.mutable_data(), z.size(),
x.data(), x.size(), x_words,
y.data(), y.size(), y_words,
ws.data(), ws.size());
this->redc(z, ws);
}
void CurveGFp_NIST::curve_sqr(BigInt& z, const BigInt& x,
secure_vector<word>& ws) const
{
if(ws.size() < get_ws_size())
ws.resize(get_ws_size());
const size_t output_size = 2*m_p_words + 2;
if(z.size() < output_size)
z.grow_to(output_size);
const size_t x_words = (x.size() >= m_p_words) ? m_p_words : x.sig_words();
bigint_sqr(z.mutable_data(), output_size,
x.data(), x.size(), x_words,
ws.data(), ws.size());
this->redc(z, ws);
}
#if defined(BOTAN_HAS_NIST_PRIME_REDUCERS_W32)
/**
* The NIST P-192 curve
*/
class CurveGFp_P192 final : public CurveGFp_NIST
{
public:
CurveGFp_P192(const BigInt& a, const BigInt& b) : CurveGFp_NIST(192, a, b) {}
const BigInt& get_p() const override { return prime_p192(); }
private:
void redc(BigInt& x, secure_vector<word>& ws) const override { redc_p192(x, ws); }
};
/**
* The NIST P-224 curve
*/
class CurveGFp_P224 final : public CurveGFp_NIST
{
public:
CurveGFp_P224(const BigInt& a, const BigInt& b) : CurveGFp_NIST(224, a, b) {}
const BigInt& get_p() const override { return prime_p224(); }
private:
void redc(BigInt& x, secure_vector<word>& ws) const override { redc_p224(x, ws); }
};
/**
* The NIST P-256 curve
*/
class CurveGFp_P256 final : public CurveGFp_NIST
{
public:
CurveGFp_P256(const BigInt& a, const BigInt& b) : CurveGFp_NIST(256, a, b) {}
const BigInt& get_p() const override { return prime_p256(); }
private:
void redc(BigInt& x, secure_vector<word>& ws) const override { redc_p256(x, ws); }
};
/**
* The NIST P-384 curve
*/
class CurveGFp_P384 final : public CurveGFp_NIST
{
public:
CurveGFp_P384(const BigInt& a, const BigInt& b) : CurveGFp_NIST(384, a, b) {}
const BigInt& get_p() const override { return prime_p384(); }
private:
void redc(BigInt& x, secure_vector<word>& ws) const override { redc_p384(x, ws); }
};
#endif
/**
* The NIST P-521 curve
*/
class CurveGFp_P521 final : public CurveGFp_NIST
{
public:
CurveGFp_P521(const BigInt& a, const BigInt& b) : CurveGFp_NIST(521, a, b) {}
const BigInt& get_p() const override { return prime_p521(); }
private:
void redc(BigInt& x, secure_vector<word>& ws) const override { redc_p521(x, ws); }
};
}
std::shared_ptr<CurveGFp_Repr>
CurveGFp::choose_repr(const BigInt& p, const BigInt& a, const BigInt& b)
{
#if defined(BOTAN_HAS_NIST_PRIME_REDUCERS_W32)
if(p == prime_p192())
return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P192(a, b));
if(p == prime_p224())
return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P224(a, b));
if(p == prime_p256())
return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P256(a, b));
if(p == prime_p384())
return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P384(a, b));
#endif
if(p == prime_p521())
return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P521(a, b));
return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_Montgomery(p, a, b));
}
}
|