• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13073795340

31 Jan 2025 01:32PM UTC coverage: 91.224% (-0.009%) from 91.233%
13073795340

push

github

web-flow
Merge pull request #4617 from randombit/jack/fix-build-configs

Fix build/test errors caught by test_all_configs.py

94150 of 103208 relevant lines covered (91.22%)

11427437.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.71
/src/lib/pubkey/ec_group/ec_inner_data.cpp
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

19
namespace Botan {
20

21
EC_Group_Data::~EC_Group_Data() = default;
13,939✔
22

23
// Note this constructor *does not* initialize m_curve, m_base_point or m_base_mult
24
EC_Group_Data::EC_Group_Data(const BigInt& p,
1,162✔
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) :
1,162✔
33
      m_p(p),
1,162✔
34
      m_a(a),
1,162✔
35
      m_b(b),
1,162✔
36
      m_g_x(g_x),
1,162✔
37
      m_g_y(g_y),
1,162✔
38
      m_order(order),
1,162✔
39
      m_cofactor(cofactor),
1,162✔
40
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
41
      m_mod_field(Modular_Reducer::for_public_modulus(p)),
1,162✔
42
      m_mod_order(Modular_Reducer::for_public_modulus(order)),
1,162✔
43
      m_monty(m_p, m_mod_field),
1,162✔
44
#endif
45
      m_oid(oid),
1,162✔
46
      m_p_words(p.sig_words()),
1,162✔
47
      m_p_bits(p.bits()),
1,162✔
48
      m_order_bits(order.bits()),
1,162✔
49
      m_order_bytes((m_order_bits + 7) / 8),
1,162✔
50
      m_a_is_minus_3(a == p - 3),
2,324✔
51
      m_a_is_zero(a.is_zero()),
1,162✔
52
      m_has_cofactor(m_cofactor != 1),
1,162✔
53
      m_order_is_less_than_p(m_order < p),
1,162✔
54
      m_source(source) {
3,486✔
55
   if(!m_oid.empty()) {
1,162✔
56
      DER_Encoder der(m_der_named_curve);
1,152✔
57
      der.encode(m_oid);
1,152✔
58

59
      if(const auto id = PCurve::PrimeOrderCurveId::from_oid(m_oid)) {
1,152✔
60
         m_pcurve = PCurve::PrimeOrderCurve::from_id(*id);
913✔
61
         // still possibly null, if the curve is supported in general but not
62
         // available in the build
63
      }
64
   }
1,152✔
65

66
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
67
   secure_vector<word> ws;
1,162✔
68
   m_a_r = m_monty.mul(a, m_monty.R2(), ws);
2,324✔
69
   m_b_r = m_monty.mul(b, m_monty.R2(), ws);
2,324✔
70
#else
71
   if(!m_pcurve) {
72
      if(m_oid.empty()) {
73
         throw Not_Implemented("EC_Group this group is not supported in this build configuration");
74
      } else {
75
         throw Not_Implemented(
76
            fmt("EC_Group the group {} is not supported in this build configuration", oid.to_string()));
77
      }
78
   }
79
#endif
80
}
1,162✔
81

82
std::shared_ptr<EC_Group_Data> EC_Group_Data::create(const BigInt& p,
1,162✔
83
                                                     const BigInt& a,
84
                                                     const BigInt& b,
85
                                                     const BigInt& g_x,
86
                                                     const BigInt& g_y,
87
                                                     const BigInt& order,
88
                                                     const BigInt& cofactor,
89
                                                     const OID& oid,
90
                                                     EC_Group_Source source) {
91
   auto group = std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, source);
1,162✔
92

93
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
94
   group->m_curve = CurveGFp(group.get());
1,162✔
95
   group->m_base_point = EC_Point(group->m_curve, g_x, g_y);
2,324✔
96
   if(!group->m_pcurve) {
1,162✔
97
      group->m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(group->m_base_point, group->m_mod_order);
249✔
98
   }
99
#endif
100

101
   return group;
1,162✔
102
}
×
103

104
bool EC_Group_Data::params_match(const BigInt& p,
853✔
105
                                 const BigInt& a,
106
                                 const BigInt& b,
107
                                 const BigInt& g_x,
108
                                 const BigInt& g_y,
109
                                 const BigInt& order,
110
                                 const BigInt& cofactor) const {
111
   return (this->p() == p && this->a() == a && this->b() == b && this->order() == order &&
945✔
112
           this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
936✔
113
}
114

115
bool EC_Group_Data::params_match(const EC_Group_Data& other) const {
19✔
116
   return params_match(other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
19✔
117
}
118

119
void EC_Group_Data::set_oid(const OID& oid) {
5✔
120
   BOTAN_ARG_CHECK(!oid.empty(), "OID should be set");
5✔
121
   BOTAN_STATE_CHECK(m_oid.empty() && m_der_named_curve.empty());
5✔
122
   m_oid = oid;
5✔
123

124
   DER_Encoder der(m_der_named_curve);
5✔
125
   der.encode(m_oid);
5✔
126
}
5✔
127

128
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_with_trunc(std::span<const uint8_t> bytes) const {
26,054✔
129
   const size_t bit_length = 8 * bytes.size();
26,054✔
130

131
   if(bit_length < order_bits()) {
26,054✔
132
      // No shifting required, but might still need to reduce by modulus
133
      return this->scalar_from_bytes_mod_order(bytes);
5,263✔
134
   } else {
135
      const size_t shift = bit_length - order_bits();
20,791✔
136

137
      const size_t new_length = bytes.size() - (shift / 8);
20,791✔
138
      const size_t bit_shift = shift % 8;
20,791✔
139

140
      if(bit_shift == 0) {
20,791✔
141
         // Easy case just read different bytes
142
         return this->scalar_from_bytes_mod_order(bytes.first(new_length));
18,151✔
143
      } else {
144
         std::vector<uint8_t> sbytes(new_length);
2,640✔
145

146
         uint8_t carry = 0;
2,640✔
147
         for(size_t i = 0; i != new_length; ++i) {
72,171✔
148
            const uint8_t w = bytes[i];
69,531✔
149
            sbytes[i] = (w >> bit_shift) | carry;
69,531✔
150
            carry = w << (8 - bit_shift);
69,531✔
151
         }
152

153
         return this->scalar_from_bytes_mod_order(sbytes);
2,640✔
154
      }
2,640✔
155
   }
156
}
157

158
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_mod_order(std::span<const uint8_t> bytes) const {
34,764✔
159
   if(bytes.size() > 2 * order_bytes()) {
34,764✔
160
      return {};
×
161
   }
162

163
   if(m_pcurve) {
34,764✔
164
      if(auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
23,533✔
165
         return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
23,533✔
166
      } else {
167
         return {};
×
168
      }
23,533✔
169
   } else {
170
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
171
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(BigInt(bytes)));
22,462✔
172
#else
173
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
174
#endif
175
   }
176
}
177

178
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_random(RandomNumberGenerator& rng) const {
44,250✔
179
   if(m_pcurve) {
44,250✔
180
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->random_scalar(rng));
19,444✔
181
   } else {
182
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
183
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
49,612✔
184
                                                 BigInt::random_integer(rng, BigInt::one(), m_order));
99,224✔
185
#else
186
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
187
#endif
188
   }
189
}
190

191
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_zero() const {
×
192
   if(m_pcurve) {
×
193
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_zero());
×
194
   } else {
195
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
196
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::zero());
×
197
#else
198
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
199
#endif
200
   }
201
}
202

203
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_one() const {
101✔
204
   if(m_pcurve) {
101✔
205
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_one());
61✔
206
   } else {
207
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
208
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::one());
40✔
209
#else
210
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
211
#endif
212
   }
213
}
214

215
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bigint(const BigInt& bn) const {
4,485✔
216
   if(bn <= 0 || bn >= m_order) {
4,485✔
217
      return {};
×
218
   }
219

220
   if(m_pcurve) {
4,485✔
221
      return this->scalar_deserialize(bn.serialize(m_order_bytes));
6,754✔
222
   } else {
223
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
224
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
1,108✔
225
#else
226
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
227
#endif
228
   }
229
}
230

231
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::gk_x_mod_order(const EC_Scalar_Data& scalar,
3,309✔
232
                                                              RandomNumberGenerator& rng,
233
                                                              std::vector<BigInt>& ws) const {
234
   if(m_pcurve) {
3,309✔
235
      const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
1,688✔
236
      auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
1,688✔
237
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), gk_x_mod_order);
1,688✔
238
   } else {
1,688✔
239
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
240
      const auto& k = EC_Scalar_Data_BN::checked_ref(scalar);
1,621✔
241
      BOTAN_STATE_CHECK(m_base_mult != nullptr);
1,621✔
242
      const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
1,621✔
243

244
      if(pt.is_zero()) {
3,242✔
245
         return scalar_zero();
×
246
      } else {
247
         return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(pt.get_affine_x()));
3,242✔
248
      }
249
#else
250
      BOTAN_UNUSED(ws);
251
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
252
#endif
253
   }
1,621✔
254
}
255

256
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_deserialize(std::span<const uint8_t> bytes) const {
68,626✔
257
   if(bytes.size() != m_order_bytes) {
68,626✔
258
      return nullptr;
5,856✔
259
   }
260

261
   if(m_pcurve) {
62,770✔
262
      if(auto s = m_pcurve->deserialize_scalar(bytes)) {
46,920✔
263
         return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
44,712✔
264
      } else {
265
         return nullptr;
2,208✔
266
      }
46,920✔
267
   } else {
268
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
269
      BigInt r(bytes);
15,850✔
270

271
      if(r.is_zero() || r >= m_order) {
31,700✔
272
         return nullptr;
684✔
273
      }
274

275
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
15,166✔
276
#else
277
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
278
#endif
279
   }
15,850✔
280
}
281

282
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_deserialize(std::span<const uint8_t> bytes) const {
44,413✔
283
   try {
44,413✔
284
      if(m_pcurve) {
44,413✔
285
         if(auto pt = m_pcurve->deserialize_point(bytes)) {
33,027✔
286
            return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
26,910✔
287
         } else {
288
            return nullptr;
6,117✔
289
         }
33,027✔
290
      } else {
291
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
292
         return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), bytes);
11,646✔
293
#else
294
         throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
295
#endif
296
      }
297
   } catch(...) {
260✔
298
      return nullptr;
260✔
299
   }
260✔
300
}
301

302
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_ro(std::string_view hash_fn,
25✔
303
                                                                           std::span<const uint8_t> input,
304
                                                                           std::span<const uint8_t> domain_sep) const {
305
   if(m_pcurve) {
25✔
306
      auto pt = m_pcurve->hash_to_curve_ro(hash_fn, input, domain_sep);
25✔
307
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
25✔
308
   } else {
25✔
309
      throw Not_Implemented("Hash to curve is not implemented for this curve");
×
310
   }
311
}
312

313
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_nu(std::string_view hash_fn,
34✔
314
                                                                           std::span<const uint8_t> input,
315
                                                                           std::span<const uint8_t> domain_sep) const {
316
   if(m_pcurve) {
34✔
317
      auto pt = m_pcurve->hash_to_curve_nu(hash_fn, input, domain_sep);
34✔
318
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
31✔
319
   } else {
31✔
320
      throw Not_Implemented("Hash to curve is not implemented for this curve");
×
321
   }
322
}
323

324
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_g_mul(const EC_Scalar_Data& scalar,
15,367✔
325
                                                                RandomNumberGenerator& rng,
326
                                                                std::vector<BigInt>& ws) const {
327
   if(m_pcurve) {
15,367✔
328
      const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
7,974✔
329
      auto pt = m_pcurve->mul_by_g(k.value(), rng).to_affine();
15,948✔
330
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
7,974✔
331
   } else {
7,974✔
332
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
333
      const auto& group = scalar.group();
7,393✔
334
      const auto& bn = EC_Scalar_Data_BN::checked_ref(scalar);
7,393✔
335

336
      BOTAN_STATE_CHECK(group->m_base_mult != nullptr);
7,393✔
337
      auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
7,393✔
338
      return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
7,393✔
339
#else
340
      BOTAN_UNUSED(ws);
341
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
342
#endif
343
   }
7,393✔
344
}
345

346
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::mul_px_qy(const EC_AffinePoint_Data& p,
3,472✔
347
                                                              const EC_Scalar_Data& x,
348
                                                              const EC_AffinePoint_Data& q,
349
                                                              const EC_Scalar_Data& y,
350
                                                              RandomNumberGenerator& rng) const {
351
   if(m_pcurve) {
3,472✔
352
      auto pt = m_pcurve->mul_px_qy(EC_AffinePoint_Data_PC::checked_ref(p).value(),
5,208✔
353
                                    EC_Scalar_Data_PC::checked_ref(x).value(),
2,604✔
354
                                    EC_AffinePoint_Data_PC::checked_ref(q).value(),
2,604✔
355
                                    EC_Scalar_Data_PC::checked_ref(y).value(),
2,604✔
356
                                    rng);
2,604✔
357

358
      if(pt) {
2,604✔
359
         return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt->to_affine());
2,520✔
360
      } else {
361
         return nullptr;
84✔
362
      }
363
   } else {
2,604✔
364
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
365
      std::vector<BigInt> ws;
868✔
366
      const auto& group = p.group();
868✔
367

368
      // TODO this could be better!
369
      EC_Point_Var_Point_Precompute p_mul(p.to_legacy_point(), rng, ws);
868✔
370
      EC_Point_Var_Point_Precompute q_mul(q.to_legacy_point(), rng, ws);
868✔
371

372
      const auto order = group->order() * group->cofactor();  // See #3800
868✔
373

374
      auto px = p_mul.mul(EC_Scalar_Data_BN::checked_ref(x).value(), rng, order, ws);
868✔
375
      auto qy = q_mul.mul(EC_Scalar_Data_BN::checked_ref(y).value(), rng, order, ws);
868✔
376

377
      auto px_qy = px + qy;
868✔
378

379
      if(!px_qy.is_zero()) {
1,624✔
380
         px_qy.force_affine();
756✔
381
         return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
756✔
382
      } else {
383
         return nullptr;
112✔
384
      }
385
#else
386
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
387
#endif
388
   }
3,472✔
389
}
390

391
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::affine_add(const EC_AffinePoint_Data& p,
7,754✔
392
                                                               const EC_AffinePoint_Data& q) const {
393
   if(m_pcurve) {
7,754✔
394
      auto pt = m_pcurve->point_add_mixed(
5,046✔
395
         PCurve::PrimeOrderCurve::ProjectivePoint::from_affine(EC_AffinePoint_Data_PC::checked_ref(p).value()),
5,047✔
396
         EC_AffinePoint_Data_PC::checked_ref(q).value());
10,092✔
397

398
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
5,045✔
399
   } else {
5,045✔
400
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
401
      auto pt = p.to_legacy_point() + q.to_legacy_point();
2,708✔
402
      return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
2,708✔
403
#else
404
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
405
#endif
406
   }
2,708✔
407
}
408

409
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::affine_neg(const EC_AffinePoint_Data& p) const {
9,512✔
410
   if(m_pcurve) {
9,512✔
411
      auto pt = m_pcurve->point_negate(EC_AffinePoint_Data_PC::checked_ref(p).value());
6,312✔
412
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt);
6,312✔
413
   } else {
6,312✔
414
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
415
      auto pt = p.to_legacy_point();
3,200✔
416
      pt.negate();  // negates in place
3,200✔
417
      return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
3,200✔
418
#else
419
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
420
#endif
421
   }
3,200✔
422
}
423

424
std::unique_ptr<EC_Mul2Table_Data> EC_Group_Data::make_mul2_table(const EC_AffinePoint_Data& h) const {
13,257✔
425
   if(m_pcurve) {
13,257✔
426
      return std::make_unique<EC_Mul2Table_Data_PC>(h);
11,176✔
427
   } else {
428
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
429
      EC_AffinePoint_Data_BN g(shared_from_this(), this->base_point());
4,162✔
430
      return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
2,081✔
431
#else
432
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
433
#endif
434
   }
2,081✔
435
}
436

437
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc