Botan 3.7.1
Crypto and TLS for C&
ec_inner_data.cpp
Go to the documentation of this file.
1/*
2* (C) 2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/ec_inner_data.h>
8
9#include <botan/der_enc.h>
10#include <botan/internal/ec_inner_pc.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/pcurves.h>
13
14#if defined(BOTAN_HAS_LEGACY_EC_POINT)
15 #include <botan/internal/ec_inner_bn.h>
16 #include <botan/internal/point_mul.h>
17#endif
18
19namespace Botan {
20
22
23// Note this constructor *does not* initialize m_curve, m_base_point or m_base_mult
25 const BigInt& a,
26 const BigInt& b,
27 const BigInt& g_x,
28 const BigInt& g_y,
29 const BigInt& order,
30 const BigInt& cofactor,
31 const OID& oid,
32 EC_Group_Source source) :
33 m_p(p),
34 m_a(a),
35 m_b(b),
36 m_g_x(g_x),
37 m_g_y(g_y),
38 m_order(order),
39 m_cofactor(cofactor),
41 m_mod_field(Modular_Reducer::for_public_modulus(p)),
42 m_mod_order(Modular_Reducer::for_public_modulus(order)),
43 m_monty(m_p, m_mod_field),
44#endif
45 m_oid(oid),
46 m_p_words(p.sig_words()),
47 m_p_bits(p.bits()),
48 m_order_bits(order.bits()),
49 m_order_bytes((m_order_bits + 7) / 8),
50 m_a_is_minus_3(a == p - 3),
51 m_a_is_zero(a.is_zero()),
52 m_has_cofactor(m_cofactor != 1),
53 m_order_is_less_than_p(m_order < p),
54 m_source(source) {
55 if(!m_oid.empty()) {
56 DER_Encoder der(m_der_named_curve);
57 der.encode(m_oid);
58
59 if(const auto id = PCurve::PrimeOrderCurveId::from_oid(m_oid)) {
60 m_pcurve = PCurve::PrimeOrderCurve::from_id(*id);
61 if(m_pcurve) {
62 m_engine = EC_Group_Engine::Optimized;
63 }
64 // still possibly null, if the curve is supported in general but not
65 // available in the build
66 }
67 }
68
69#if defined(BOTAN_HAS_LEGACY_EC_POINT)
71 m_a_r = m_monty.mul(a, m_monty.R2(), ws);
72 m_b_r = m_monty.mul(b, m_monty.R2(), ws);
73 if(!m_pcurve) {
74 m_engine = EC_Group_Engine::Legacy;
75 }
76#else
77 if(!m_pcurve) {
78 if(m_oid.empty()) {
79 throw Not_Implemented("EC_Group this group is not supported in this build configuration");
80 } else {
81 throw Not_Implemented(
82 fmt("EC_Group the group {} is not supported in this build configuration", oid.to_string()));
83 }
84 }
85#endif
86}
87
88std::shared_ptr<EC_Group_Data> EC_Group_Data::create(const BigInt& p,
89 const BigInt& a,
90 const BigInt& b,
91 const BigInt& g_x,
92 const BigInt& g_y,
93 const BigInt& order,
94 const BigInt& cofactor,
95 const OID& oid,
96 EC_Group_Source source) {
97 auto group = std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, source);
98
99#if defined(BOTAN_HAS_LEGACY_EC_POINT)
100 group->m_curve = CurveGFp(group.get());
101 group->m_base_point = EC_Point(group->m_curve, g_x, g_y);
102 if(!group->m_pcurve) {
103 group->m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(group->m_base_point, group->m_mod_order);
104 }
105#endif
106
107 return group;
108}
109
110bool EC_Group_Data::params_match(const BigInt& p,
111 const BigInt& a,
112 const BigInt& b,
113 const BigInt& g_x,
114 const BigInt& g_y,
115 const BigInt& order,
116 const BigInt& cofactor) const {
117 return (this->p() == p && this->a() == a && this->b() == b && this->order() == order &&
118 this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
119}
120
121bool EC_Group_Data::params_match(const EC_Group_Data& other) const {
122 return params_match(other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
123}
124
125void EC_Group_Data::set_oid(const OID& oid) {
126 BOTAN_ARG_CHECK(!oid.empty(), "OID should be set");
127 BOTAN_STATE_CHECK(m_oid.empty() && m_der_named_curve.empty());
128 m_oid = oid;
129
130 DER_Encoder der(m_der_named_curve);
131 der.encode(m_oid);
132}
133
134std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_with_trunc(std::span<const uint8_t> bytes) const {
135 const size_t bit_length = 8 * bytes.size();
136
137 if(bit_length < order_bits()) {
138 // No shifting required, but might still need to reduce by modulus
139 return this->scalar_from_bytes_mod_order(bytes);
140 } else {
141 const size_t shift = bit_length - order_bits();
142
143 const size_t new_length = bytes.size() - (shift / 8);
144 const size_t bit_shift = shift % 8;
145
146 if(bit_shift == 0) {
147 // Easy case just read different bytes
148 return this->scalar_from_bytes_mod_order(bytes.first(new_length));
149 } else {
150 std::vector<uint8_t> sbytes(new_length);
151
152 uint8_t carry = 0;
153 for(size_t i = 0; i != new_length; ++i) {
154 const uint8_t w = bytes[i];
155 sbytes[i] = (w >> bit_shift) | carry;
156 carry = w << (8 - bit_shift);
157 }
158
159 return this->scalar_from_bytes_mod_order(sbytes);
160 }
161 }
162}
163
164std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_mod_order(std::span<const uint8_t> bytes) const {
165 if(bytes.size() > 2 * order_bytes()) {
166 return {};
167 }
168
169 if(m_pcurve) {
170 if(auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
171 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
172 } else {
173 return {};
174 }
175 } else {
176#if defined(BOTAN_HAS_LEGACY_EC_POINT)
177 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(BigInt(bytes)));
178#else
179 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
180#endif
181 }
182}
183
184std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_random(RandomNumberGenerator& rng) const {
185 if(m_pcurve) {
186 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->random_scalar(rng));
187 } else {
188#if defined(BOTAN_HAS_LEGACY_EC_POINT)
189 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
190 BigInt::random_integer(rng, BigInt::one(), m_order));
191#else
192 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
193#endif
194 }
195}
196
197std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_zero() const {
198 if(m_pcurve) {
199 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_zero());
200 } else {
201#if defined(BOTAN_HAS_LEGACY_EC_POINT)
202 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::zero());
203#else
204 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
205#endif
206 }
207}
208
209std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_one() const {
210 if(m_pcurve) {
211 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_one());
212 } else {
213#if defined(BOTAN_HAS_LEGACY_EC_POINT)
214 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::one());
215#else
216 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
217#endif
218 }
219}
220
221std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bigint(const BigInt& bn) const {
222 if(bn <= 0 || bn >= m_order) {
223 return {};
224 }
225
226 if(m_pcurve) {
227 return this->scalar_deserialize(bn.serialize(m_order_bytes));
228 } else {
229#if defined(BOTAN_HAS_LEGACY_EC_POINT)
230 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
231#else
232 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
233#endif
234 }
235}
236
237std::unique_ptr<EC_Scalar_Data> EC_Group_Data::gk_x_mod_order(const EC_Scalar_Data& scalar,
239 std::vector<BigInt>& ws) const {
240 if(m_pcurve) {
241 const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
242 auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
243 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), gk_x_mod_order);
244 } else {
245#if defined(BOTAN_HAS_LEGACY_EC_POINT)
246 const auto& k = EC_Scalar_Data_BN::checked_ref(scalar);
247 BOTAN_STATE_CHECK(m_base_mult != nullptr);
248 const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
249
250 if(pt.is_zero()) {
251 return scalar_zero();
252 } else {
253 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(pt.get_affine_x()));
254 }
255#else
256 BOTAN_UNUSED(ws);
257 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
258#endif
259 }
260}
261
262std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_deserialize(std::span<const uint8_t> bytes) const {
263 if(bytes.size() != m_order_bytes) {
264 return nullptr;
265 }
266
267 if(m_pcurve) {
268 if(auto s = m_pcurve->deserialize_scalar(bytes)) {
269 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
270 } else {
271 return nullptr;
272 }
273 } else {
274#if defined(BOTAN_HAS_LEGACY_EC_POINT)
275 BigInt r(bytes);
276
277 if(r.is_zero() || r >= m_order) {
278 return nullptr;
279 }
280
281 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
282#else
283 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
284#endif
285 }
286}
287
288std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_deserialize(std::span<const uint8_t> bytes) const {
289 try {
290 if(m_pcurve) {
291 if(auto pt = m_pcurve->deserialize_point(bytes)) {
292 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
293 } else {
294 return nullptr;
295 }
296 } else {
297#if defined(BOTAN_HAS_LEGACY_EC_POINT)
298 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), bytes);
299#else
300 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
301#endif
302 }
303 } catch(...) {
304 return nullptr;
305 }
306}
307
308std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_ro(std::string_view hash_fn,
309 std::span<const uint8_t> input,
310 std::span<const uint8_t> domain_sep) const {
311 if(m_pcurve) {
312 auto pt = m_pcurve->hash_to_curve_ro(hash_fn, input, domain_sep);
313 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
314 } else {
315 throw Not_Implemented("Hash to curve is not implemented for this curve");
316 }
317}
318
319std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_nu(std::string_view hash_fn,
320 std::span<const uint8_t> input,
321 std::span<const uint8_t> domain_sep) const {
322 if(m_pcurve) {
323 auto pt = m_pcurve->hash_to_curve_nu(hash_fn, input, domain_sep);
324 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
325 } else {
326 throw Not_Implemented("Hash to curve is not implemented for this curve");
327 }
328}
329
330std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_g_mul(const EC_Scalar_Data& scalar,
332 std::vector<BigInt>& ws) const {
333 if(m_pcurve) {
334 const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
335 auto pt = m_pcurve->mul_by_g(k.value(), rng).to_affine();
336 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
337 } else {
338#if defined(BOTAN_HAS_LEGACY_EC_POINT)
339 const auto& group = scalar.group();
340 const auto& bn = EC_Scalar_Data_BN::checked_ref(scalar);
341
342 BOTAN_STATE_CHECK(group->m_base_mult != nullptr);
343 auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
344 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
345#else
346 BOTAN_UNUSED(ws);
347 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
348#endif
349 }
350}
351
352std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::mul_px_qy(const EC_AffinePoint_Data& p,
353 const EC_Scalar_Data& x,
354 const EC_AffinePoint_Data& q,
355 const EC_Scalar_Data& y,
356 RandomNumberGenerator& rng) const {
357 if(m_pcurve) {
358 auto pt = m_pcurve->mul_px_qy(EC_AffinePoint_Data_PC::checked_ref(p).value(),
359 EC_Scalar_Data_PC::checked_ref(x).value(),
360 EC_AffinePoint_Data_PC::checked_ref(q).value(),
361 EC_Scalar_Data_PC::checked_ref(y).value(),
362 rng);
363
364 if(pt) {
365 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt->to_affine());
366 } else {
367 return nullptr;
368 }
369 } else {
370#if defined(BOTAN_HAS_LEGACY_EC_POINT)
371 std::vector<BigInt> ws;
372 const auto& group = p.group();
373
374 // TODO this could be better!
375 EC_Point_Var_Point_Precompute p_mul(p.to_legacy_point(), rng, ws);
376 EC_Point_Var_Point_Precompute q_mul(q.to_legacy_point(), rng, ws);
377
378 const auto order = group->order() * group->cofactor(); // See #3800
379
380 auto px = p_mul.mul(EC_Scalar_Data_BN::checked_ref(x).value(), rng, order, ws);
381 auto qy = q_mul.mul(EC_Scalar_Data_BN::checked_ref(y).value(), rng, order, ws);
382
383 auto px_qy = px + qy;
384
385 if(!px_qy.is_zero()) {
386 px_qy.force_affine();
387 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
388 } else {
389 return nullptr;
390 }
391#else
392 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
393#endif
394 }
395}
396
397std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::affine_add(const EC_AffinePoint_Data& p,
398 const EC_AffinePoint_Data& q) const {
399 if(m_pcurve) {
400 auto pt = m_pcurve->point_add_mixed(
401 PCurve::PrimeOrderCurve::ProjectivePoint::from_affine(EC_AffinePoint_Data_PC::checked_ref(p).value()),
402 EC_AffinePoint_Data_PC::checked_ref(q).value());
403
404 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
405 } else {
406#if defined(BOTAN_HAS_LEGACY_EC_POINT)
407 auto pt = p.to_legacy_point() + q.to_legacy_point();
408 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
409#else
410 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
411#endif
412 }
413}
414
415std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::affine_neg(const EC_AffinePoint_Data& p) const {
416 if(m_pcurve) {
417 auto pt = m_pcurve->point_negate(EC_AffinePoint_Data_PC::checked_ref(p).value());
418 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt);
419 } else {
420#if defined(BOTAN_HAS_LEGACY_EC_POINT)
421 auto pt = p.to_legacy_point();
422 pt.negate(); // negates in place
423 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
424#else
425 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
426#endif
427 }
428}
429
430std::unique_ptr<EC_Mul2Table_Data> EC_Group_Data::make_mul2_table(const EC_AffinePoint_Data& h) const {
431 if(m_pcurve) {
432 return std::make_unique<EC_Mul2Table_Data_PC>(h);
433 } else {
434#if defined(BOTAN_HAS_LEGACY_EC_POINT)
435 EC_AffinePoint_Data_BN g(shared_from_this(), this->base_point());
436 return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
437#else
438 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
439#endif
440 }
441}
442
443} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
bool is_zero() const
Definition bigint.h:458
T serialize(size_t len) const
Definition bigint.h:712
DER_Encoder & encode(bool b)
Definition der_enc.cpp:250
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
const BigInt & p() const
const BigInt & g_x() const
const BigInt & a() const
EC_Group_Data(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor, const OID &oid, EC_Group_Source source)
const BigInt & cofactor() const
const BigInt & g_y() const
const BigInt & b() const
const BigInt & order() const
EC_Point mul(const BigInt &k, RandomNumberGenerator &rng, const BigInt &group_order, std::vector< BigInt > &ws) const
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
bool empty() const
Definition asn1_obj.h:266
#define BOTAN_HAS_LEGACY_EC_POINT
Definition build.h:292
EC_Group_Source
Definition ec_group.h:56
void carry(int64_t &h0, int64_t &h1)
const SIMD_8x32 & b
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61