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

randombit / botan / 13192903634

07 Feb 2025 01:53AM UTC coverage: 91.278% (+0.02%) from 91.255%
13192903634

push

github

web-flow
Merge pull request #4634 from randombit/jack/min-pcurves-surface

Remove unused pcurves interfaces

94433 of 103457 relevant lines covered (91.28%)

11248924.26 hits per line

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

96.77
/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
         if(m_pcurve) {
913✔
62
            m_engine = EC_Group_Engine::Optimized;
913✔
63
         }
64
         // still possibly null, if the curve is supported in general but not
65
         // available in the build
66
      }
67
   }
1,152✔
68

69
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
70
   secure_vector<word> ws;
1,162✔
71
   m_a_r = m_monty.mul(a, m_monty.R2(), ws);
2,324✔
72
   m_b_r = m_monty.mul(b, m_monty.R2(), ws);
2,324✔
73
   if(!m_pcurve) {
1,162✔
74
      m_engine = EC_Group_Engine::Legacy;
249✔
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
}
1,162✔
87

88
std::shared_ptr<EC_Group_Data> EC_Group_Data::create(const BigInt& p,
1,162✔
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);
1,162✔
98

99
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
100
   group->m_curve = CurveGFp(group.get());
1,162✔
101
   group->m_base_point = EC_Point(group->m_curve, g_x, g_y);
2,324✔
102
   if(!group->m_pcurve) {
1,162✔
103
      group->m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(group->m_base_point, group->m_mod_order);
249✔
104
   }
105
#endif
106

107
   return group;
1,162✔
108
}
×
109

110
bool EC_Group_Data::params_match(const BigInt& p,
854✔
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 &&
946✔
118
           this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
937✔
119
}
120

121
bool EC_Group_Data::params_match(const EC_Group_Data& other) const {
19✔
122
   return params_match(other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
19✔
123
}
124

125
void EC_Group_Data::set_oid(const OID& oid) {
5✔
126
   BOTAN_ARG_CHECK(!oid.empty(), "OID should be set");
5✔
127
   BOTAN_STATE_CHECK(m_oid.empty() && m_der_named_curve.empty());
5✔
128
   m_oid = oid;
5✔
129

130
   DER_Encoder der(m_der_named_curve);
5✔
131
   der.encode(m_oid);
5✔
132
}
5✔
133

134
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_with_trunc(std::span<const uint8_t> bytes) const {
26,055✔
135
   const size_t bit_length = 8 * bytes.size();
26,055✔
136

137
   if(bit_length < order_bits()) {
26,055✔
138
      // No shifting required, but might still need to reduce by modulus
139
      return this->scalar_from_bytes_mod_order(bytes);
5,261✔
140
   } else {
141
      const size_t shift = bit_length - order_bits();
20,794✔
142

143
      const size_t new_length = bytes.size() - (shift / 8);
20,794✔
144
      const size_t bit_shift = shift % 8;
20,794✔
145

146
      if(bit_shift == 0) {
20,794✔
147
         // Easy case just read different bytes
148
         return this->scalar_from_bytes_mod_order(bytes.first(new_length));
18,160✔
149
      } else {
150
         std::vector<uint8_t> sbytes(new_length);
2,634✔
151

152
         uint8_t carry = 0;
2,634✔
153
         for(size_t i = 0; i != new_length; ++i) {
72,010✔
154
            const uint8_t w = bytes[i];
69,376✔
155
            sbytes[i] = (w >> bit_shift) | carry;
69,376✔
156
            carry = w << (8 - bit_shift);
69,376✔
157
         }
158

159
         return this->scalar_from_bytes_mod_order(sbytes);
2,634✔
160
      }
2,634✔
161
   }
162
}
163

164
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_mod_order(std::span<const uint8_t> bytes) const {
34,767✔
165
   if(bytes.size() > 2 * order_bytes()) {
34,767✔
166
      return {};
×
167
   }
168

169
   if(m_pcurve) {
34,767✔
170
      if(auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
23,535✔
171
         return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
23,535✔
172
      } else {
173
         return {};
×
174
      }
23,535✔
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)));
22,464✔
178
#else
179
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
180
#endif
181
   }
182
}
183

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

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

209
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bigint(const BigInt& bn) const {
4,481✔
210
   if(bn <= 0 || bn >= m_order) {
4,481✔
211
      return {};
×
212
   }
213

214
   if(m_pcurve) {
4,481✔
215
      return this->scalar_deserialize(bn.serialize(m_order_bytes));
6,746✔
216
   } else {
217
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
218
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
1,108✔
219
#else
220
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
221
#endif
222
   }
223
}
224

225
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::gk_x_mod_order(const EC_Scalar_Data& scalar,
3,310✔
226
                                                              RandomNumberGenerator& rng,
227
                                                              std::vector<BigInt>& ws) const {
228
   if(m_pcurve) {
3,310✔
229
      const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
1,689✔
230
      auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
1,689✔
231
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), gk_x_mod_order);
1,689✔
232
   } else {
1,689✔
233
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
234
      const auto& k = EC_Scalar_Data_BN::checked_ref(scalar);
1,621✔
235
      BOTAN_STATE_CHECK(m_base_mult != nullptr);
1,621✔
236
      const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
1,621✔
237

238
      if(pt.is_zero()) {
3,242✔
239
         return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::zero());
×
240
      } else {
241
         return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(pt.get_affine_x()));
3,242✔
242
      }
243
#else
244
      BOTAN_UNUSED(ws);
245
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
246
#endif
247
   }
1,621✔
248
}
249

250
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_deserialize(std::span<const uint8_t> bytes) const {
68,659✔
251
   if(bytes.size() != m_order_bytes) {
68,659✔
252
      return nullptr;
5,820✔
253
   }
254

255
   if(m_pcurve) {
62,839✔
256
      if(auto s = m_pcurve->deserialize_scalar(bytes)) {
46,949✔
257
         return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
44,719✔
258
      } else {
259
         return nullptr;
2,230✔
260
      }
46,949✔
261
   } else {
262
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
263
      BigInt r(bytes);
15,890✔
264

265
      if(r.is_zero() || r >= m_order) {
31,780✔
266
         return nullptr;
714✔
267
      }
268

269
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
15,176✔
270
#else
271
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
272
#endif
273
   }
15,890✔
274
}
275

276
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_deserialize(std::span<const uint8_t> bytes) const {
44,555✔
277
   // The deprecated "hybrid" point format
278
   // TODO(Botan4) remove this
279
   if(bytes.size() >= 1 + 2 * 4 && (bytes[0] == 0x06 || bytes[0] == 0x07)) {
44,555✔
280
      bool hdr_y_is_even = bytes[0] == 0x06;
207✔
281
      bool y_is_even = (bytes.back() & 0x01) == 0;
207✔
282

283
      if(hdr_y_is_even == y_is_even) {
207✔
284
         std::vector<uint8_t> sec1(bytes.begin(), bytes.end());
147✔
285
         sec1[0] = 0x04;
147✔
286
         return this->point_deserialize(sec1);
147✔
287
      }
147✔
288
   }
289

290
   try {
44,408✔
291
      if(m_pcurve) {
44,408✔
292
         if(auto pt = m_pcurve->deserialize_point(bytes)) {
33,025✔
293
            return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
26,908✔
294
         } else {
295
            return {};
6,117✔
296
         }
33,025✔
297
      } else {
298
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
299
         auto pt = Botan::OS2ECP(bytes, m_curve);
11,383✔
300
         return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
11,122✔
301
#else
302
         throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
303
#endif
304
      }
11,122✔
305
   } catch(...) {
261✔
306
      return {};
261✔
307
   }
261✔
308
}
309

310
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_ro(std::string_view hash_fn,
25✔
311
                                                                           std::span<const uint8_t> input,
312
                                                                           std::span<const uint8_t> domain_sep) const {
313
   if(m_pcurve) {
25✔
314
      auto pt = m_pcurve->hash_to_curve_ro(hash_fn, input, domain_sep);
25✔
315
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_to_affine(pt));
25✔
316
   } else {
25✔
317
      throw Not_Implemented("Hash to curve is not implemented for this curve");
×
318
   }
319
}
320

321
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_nu(std::string_view hash_fn,
34✔
322
                                                                           std::span<const uint8_t> input,
323
                                                                           std::span<const uint8_t> domain_sep) const {
324
   if(m_pcurve) {
34✔
325
      auto pt = m_pcurve->hash_to_curve_nu(hash_fn, input, domain_sep);
34✔
326
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
31✔
327
   } else {
31✔
328
      throw Not_Implemented("Hash to curve is not implemented for this curve");
×
329
   }
330
}
331

332
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_g_mul(const EC_Scalar_Data& scalar,
15,373✔
333
                                                                RandomNumberGenerator& rng,
334
                                                                std::vector<BigInt>& ws) const {
335
   if(m_pcurve) {
15,373✔
336
      const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
7,980✔
337
      auto pt = m_pcurve->point_to_affine(m_pcurve->mul_by_g(k.value(), rng));
7,980✔
338
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
7,980✔
339
   } else {
7,980✔
340
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
341
      const auto& group = scalar.group();
7,393✔
342
      const auto& bn = EC_Scalar_Data_BN::checked_ref(scalar);
7,393✔
343

344
      BOTAN_STATE_CHECK(group->m_base_mult != nullptr);
7,393✔
345
      auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
7,393✔
346
      return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
7,393✔
347
#else
348
      BOTAN_UNUSED(ws);
349
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
350
#endif
351
   }
7,393✔
352
}
353

354
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::mul_px_qy(const EC_AffinePoint_Data& p,
3,472✔
355
                                                              const EC_Scalar_Data& x,
356
                                                              const EC_AffinePoint_Data& q,
357
                                                              const EC_Scalar_Data& y,
358
                                                              RandomNumberGenerator& rng) const {
359
   if(m_pcurve) {
3,472✔
360
      auto pt = m_pcurve->mul_px_qy(EC_AffinePoint_Data_PC::checked_ref(p).value(),
5,208✔
361
                                    EC_Scalar_Data_PC::checked_ref(x).value(),
2,604✔
362
                                    EC_AffinePoint_Data_PC::checked_ref(q).value(),
2,604✔
363
                                    EC_Scalar_Data_PC::checked_ref(y).value(),
2,604✔
364
                                    rng);
2,604✔
365

366
      if(pt) {
2,604✔
367
         return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_to_affine(*pt));
2,520✔
368
      } else {
369
         return nullptr;
84✔
370
      }
371
   } else {
2,604✔
372
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
373
      std::vector<BigInt> ws;
868✔
374
      const auto& group = p.group();
868✔
375

376
      // TODO this could be better!
377
      EC_Point_Var_Point_Precompute p_mul(p.to_legacy_point(), rng, ws);
868✔
378
      EC_Point_Var_Point_Precompute q_mul(q.to_legacy_point(), rng, ws);
868✔
379

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

382
      auto px = p_mul.mul(EC_Scalar_Data_BN::checked_ref(x).value(), rng, order, ws);
868✔
383
      auto qy = q_mul.mul(EC_Scalar_Data_BN::checked_ref(y).value(), rng, order, ws);
868✔
384

385
      auto px_qy = px + qy;
868✔
386

387
      if(!px_qy.is_zero()) {
1,624✔
388
         px_qy.force_affine();
756✔
389
         return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
756✔
390
      } else {
391
         return nullptr;
112✔
392
      }
393
#else
394
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
395
#endif
396
   }
3,472✔
397
}
398

399
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::affine_add(const EC_AffinePoint_Data& p,
7,754✔
400
                                                               const EC_AffinePoint_Data& q) const {
401
   if(m_pcurve) {
7,754✔
402
      auto pt = m_pcurve->point_add(EC_AffinePoint_Data_PC::checked_ref(p).value(),
5,046✔
403
                                    EC_AffinePoint_Data_PC::checked_ref(q).value());
5,046✔
404

405
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_to_affine(pt));
5,045✔
406
   } else {
5,045✔
407
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
408
      auto pt = p.to_legacy_point() + q.to_legacy_point();
2,708✔
409
      return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
2,708✔
410
#else
411
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
412
#endif
413
   }
2,708✔
414
}
415

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

431
std::unique_ptr<EC_Mul2Table_Data> EC_Group_Data::make_mul2_table(const EC_AffinePoint_Data& h) const {
13,254✔
432
   if(m_pcurve) {
13,254✔
433
      return std::make_unique<EC_Mul2Table_Data_PC>(h);
11,173✔
434
   } else {
435
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
436
      EC_AffinePoint_Data_BN g(shared_from_this(), this->base_point());
4,162✔
437
      return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
2,081✔
438
#else
439
      throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
440
#endif
441
   }
2,081✔
442
}
443

444
}  // 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