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

randombit / botan / 12853409305

19 Jan 2025 12:14PM UTC coverage: 91.205% (-0.005%) from 91.21%
12853409305

push

github

web-flow
Merge pull request #4571 from randombit/jack/cleanup-ec-parse

Clean up EC_Group DER deserialization a bit

93413 of 102421 relevant lines covered (91.2%)

11551633.91 hits per line

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

90.21
/src/lib/pubkey/ec_group/ec_group.cpp
1
/*
2
* ECC Domain Parameters
3
*
4
* (C) 2007 Falko Strenzke, FlexSecure GmbH
5
* (C) 2008,2018,2024 Jack Lloyd
6
* (C) 2018 Tobias Niemann
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include <botan/ec_group.h>
12

13
#include <botan/ber_dec.h>
14
#include <botan/der_enc.h>
15
#include <botan/mutex.h>
16
#include <botan/pem.h>
17
#include <botan/reducer.h>
18
#include <botan/rng.h>
19
#include <botan/internal/ec_inner_data.h>
20
#include <botan/internal/fmt.h>
21
#include <botan/internal/primality.h>
22
#include <vector>
23

24
namespace Botan {
25

26
class EC_Group_Data_Map final {
27
   public:
28
      EC_Group_Data_Map() = default;
29

30
      size_t clear() {
1,770✔
31
         lock_guard_type<mutex_type> lock(m_mutex);
1,770✔
32
         size_t count = m_registered_curves.size();
1,770✔
33
         m_registered_curves.clear();
1,770✔
34
         return count;
1,770✔
35
      }
1,770✔
36

37
      std::shared_ptr<EC_Group_Data> lookup(const OID& oid) {
20,621✔
38
         lock_guard_type<mutex_type> lock(m_mutex);
20,621✔
39

40
         for(auto i : m_registered_curves) {
159,316✔
41
            if(i->oid() == oid) {
158,126✔
42
               return i;
19,431✔
43
            }
44
         }
158,126✔
45

46
         // Not found, check hardcoded data
47
         std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid);
1,190✔
48

49
         if(data) {
1,190✔
50
            for(auto curve : m_registered_curves) {
2,269✔
51
               if(curve->oid().empty() == true && curve->params_match(*data)) {
1,132✔
52
                  curve->set_oid(oid);
×
53
                  return curve;
×
54
               }
55
            }
1,132✔
56

57
            m_registered_curves.push_back(data);
1,137✔
58
            return data;
1,137✔
59
         }
60

61
         // Nope, unknown curve
62
         return std::shared_ptr<EC_Group_Data>();
53✔
63
      }
21,811✔
64

65
      std::shared_ptr<EC_Group_Data> lookup_or_create(const BigInt& p,
96✔
66
                                                      const BigInt& a,
67
                                                      const BigInt& b,
68
                                                      const BigInt& g_x,
69
                                                      const BigInt& g_y,
70
                                                      const BigInt& order,
71
                                                      const BigInt& cofactor,
72
                                                      const OID& oid,
73
                                                      EC_Group_Source source) {
74
         lock_guard_type<mutex_type> lock(m_mutex);
96✔
75

76
         for(auto i : m_registered_curves) {
1,596✔
77
            /*
78
            * The params may be the same but you are trying to register under a
79
            * different OID than the one we are using, so using a different
80
            * group, since EC_Group's model assumes a single OID per group.
81
            */
82
            if(!oid.empty() && !i->oid().empty() && i->oid() != oid) {
1,578✔
83
               continue;
694✔
84
            }
85

86
            const bool same_oid = !oid.empty() && i->oid() == oid;
884✔
87
            const bool same_params = i->params_match(p, a, b, g_x, g_y, order, cofactor);
884✔
88

89
            /*
90
            * If the params and OID are the same then we are done, just return
91
            * the already registered curve obj.
92
            */
93
            if(same_params && same_oid) {
884✔
94
               return i;
33✔
95
            }
96

97
            /*
98
            * If same params and the new OID is empty, then that's ok too
99
            */
100
            if(same_params && oid.empty()) {
851✔
101
               return i;
45✔
102
            }
103

104
            /*
105
            * Check for someone trying to reuse an already in-use OID
106
            */
107
            if(same_oid && !same_params) {
806✔
108
               throw Invalid_Argument("Attempting to register a curve using OID " + oid.to_string() +
×
109
                                      " but a distinct curve is already registered using that OID");
×
110
            }
111

112
            /*
113
            * If the same curve was previously created without an OID but is now
114
            * being registered again using an OID, save that OID.
115
            */
116
            if(same_params && i->oid().empty() && !oid.empty()) {
806✔
117
               i->set_oid(oid);
×
118
               return i;
×
119
            }
120
         }
1,578✔
121

122
         /*
123
         Not found in current list, so we need to create a new entry
124

125
         If an OID is set, try to look up relative our static tables to detect a duplicate
126
         registration under an OID
127
         */
128

129
         auto new_group = EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, oid, source);
18✔
130

131
         if(oid.has_value()) {
18✔
132
            std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid);
7✔
133
            if(data != nullptr && !new_group->params_match(*data)) {
7✔
134
               throw Invalid_Argument("Attempting to register an EC group under OID of hardcoded group");
×
135
            }
136
         } else {
7✔
137
            // Here try to use the order as a hint to look up the group id, to identify common groups
138
            const OID oid_from_store = EC_Group::EC_group_identity_from_order(order);
11✔
139
            if(oid_from_store.has_value()) {
11✔
140
               std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid_from_store);
8✔
141

142
               /*
143
               If EC_group_identity_from_order returned an OID then looking up that OID
144
               must always return a result.
145
               */
146
               BOTAN_ASSERT_NOMSG(data != nullptr);
8✔
147

148
               /*
149
               It is possible (if unlikely) that someone is registering another group
150
               that happens to have an order equal to that of a well known group -
151
               so verify all values before assigning the OID.
152
               */
153
               if(new_group->params_match(*data)) {
8✔
154
                  new_group->set_oid(oid_from_store);
6✔
155
               }
156
            }
8✔
157
         }
11✔
158

159
         m_registered_curves.push_back(new_group);
18✔
160
         return new_group;
18✔
161
      }
114✔
162

163
   private:
164
      mutex_type m_mutex;
165
      std::vector<std::shared_ptr<EC_Group_Data>> m_registered_curves;
166
};
167

168
//static
169
EC_Group_Data_Map& EC_Group::ec_group_data() {
22,487✔
170
   /*
171
   * This exists purely to ensure the allocator is constructed before g_ec_data,
172
   * which ensures that its destructor runs after ~g_ec_data is complete.
173
   */
174

175
   static Allocator_Initializer g_init_allocator;
22,487✔
176
   static EC_Group_Data_Map g_ec_data;
22,487✔
177
   return g_ec_data;
22,487✔
178
}
179

180
//static
181
size_t EC_Group::clear_registered_curve_data() {
1,770✔
182
   return ec_group_data().clear();
1,770✔
183
}
184

185
//static
186
std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(const char* p_str,
1,145✔
187
                                                            const char* a_str,
188
                                                            const char* b_str,
189
                                                            const char* g_x_str,
190
                                                            const char* g_y_str,
191
                                                            const char* order_str,
192
                                                            const OID& oid) {
193
   const BigInt p(p_str);
1,145✔
194
   const BigInt a(a_str);
1,145✔
195
   const BigInt b(b_str);
1,145✔
196
   const BigInt g_x(g_x_str);
1,145✔
197
   const BigInt g_y(g_y_str);
1,145✔
198
   const BigInt order(order_str);
1,145✔
199
   const BigInt cofactor(1);  // implicit
1,145✔
200

201
   return EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, oid, EC_Group_Source::Builtin);
1,145✔
202
}
8,015✔
203

204
//static
205
std::pair<std::shared_ptr<EC_Group_Data>, bool> EC_Group::BER_decode_EC_group(std::span<const uint8_t> bits,
5,260✔
206
                                                                              EC_Group_Source source) {
207
   BER_Decoder ber(bits);
5,260✔
208

209
   auto next_obj_type = ber.peek_next_object().type_tag();
5,260✔
210

211
   if(next_obj_type == ASN1_Type::ObjectId) {
5,252✔
212
      OID oid;
4,920✔
213
      ber.decode(oid);
4,920✔
214

215
      auto data = ec_group_data().lookup(oid);
4,878✔
216
      if(!data) {
4,878✔
217
         throw Decoding_Error(fmt("Unknown namedCurve OID '{}'", oid.to_string()));
106✔
218
      }
219

220
      return std::make_pair(data, false);
4,825✔
221
   } else if(next_obj_type == ASN1_Type::Sequence) {
5,305✔
222
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
223

224
      BigInt p, a, b, order, cofactor;
329✔
225
      std::vector<uint8_t> base_pt;
329✔
226
      std::vector<uint8_t> seed;
329✔
227

228
      ber.start_sequence()
329✔
229
         .decode_and_check<size_t>(1, "Unknown ECC param version code")
635✔
230
         .start_sequence()
307✔
231
         .decode_and_check(OID("1.2.840.10045.1.1"), "Only prime ECC fields supported")
614✔
232
         .decode(p)
306✔
233
         .end_cons()
306✔
234
         .start_sequence()
306✔
235
         .decode_octet_string_bigint(a)
306✔
236
         .decode_octet_string_bigint(b)
305✔
237
         .decode_optional_string(seed, ASN1_Type::BitString, ASN1_Type::BitString)
303✔
238
         .end_cons()
303✔
239
         .decode(base_pt, ASN1_Type::OctetString)
300✔
240
         .decode(order)
293✔
241
         .decode(cofactor)
292✔
242
         .end_cons()
292✔
243
         .verify_end();
291✔
244

245
      if(p.bits() < 112 || p.bits() > 521) {
291✔
246
         throw Decoding_Error("ECC p parameter is invalid size");
132✔
247
      }
248

249
      if(p.is_negative() || !is_bailie_psw_probable_prime(p)) {
159✔
250
         throw Decoding_Error("ECC p parameter is not a prime");
34✔
251
      }
252

253
      if(a.is_negative() || a >= p) {
250✔
254
         throw Decoding_Error("Invalid ECC a parameter");
9✔
255
      }
256

257
      if(b <= 0 || b >= p) {
232✔
258
         throw Decoding_Error("Invalid ECC b parameter");
55✔
259
      }
260

261
      if(order <= 0 || order >= 2 * p || !is_bailie_psw_probable_prime(order)) {
453✔
262
         throw Decoding_Error("Invalid ECC order parameter");
2✔
263
      }
264

265
      if(cofactor <= 0 || cofactor >= 16) {
116✔
266
         throw Decoding_Error("Invalid ECC cofactor parameter");
3✔
267
      }
268

269
      const auto [g_x, g_y] = Botan::OS2ECP(base_pt.data(), base_pt.size(), p, a, b);
56✔
270

271
      auto data = ec_group_data().lookup_or_create(p, a, b, g_x, g_y, order, cofactor, OID(), source);
55✔
272
      return std::make_pair(data, true);
55✔
273
#else
274
      BOTAN_UNUSED(source);
275
      throw Decoding_Error("Decoding explicit ECC parameters is not supported");
276
#endif
277
   } else if(next_obj_type == ASN1_Type::Null) {
2,306✔
278
      throw Decoding_Error("Decoding ImplicitCA ECC parameters is not supported");
1✔
279
   } else {
280
      throw Decoding_Error(
2✔
281
         fmt("Unexpected tag {} while decoding ECC domain params", asn1_tag_to_string(next_obj_type)));
4✔
282
   }
283
}
5,260✔
284

285
EC_Group::EC_Group() = default;
15✔
286

287
EC_Group::~EC_Group() = default;
127,468✔
288

289
EC_Group::EC_Group(const EC_Group&) = default;
46,109✔
290

291
EC_Group& EC_Group::operator=(const EC_Group&) = default;
×
292

293
// Internal constructor
294
EC_Group::EC_Group(std::shared_ptr<EC_Group_Data>&& data) : m_data(std::move(data)) {}
15,687✔
295

296
//static
297
EC_Group EC_Group::from_OID(const OID& oid) {
71✔
298
   auto data = ec_group_data().lookup(oid);
71✔
299

300
   if(!data) {
71✔
301
      throw Invalid_Argument(fmt("No EC_Group associated with OID '{}'", oid.to_string()));
×
302
   }
303

304
   return EC_Group(std::move(data));
71✔
305
}
71✔
306

307
//static
308
EC_Group EC_Group::from_name(std::string_view name) {
15,616✔
309
   std::shared_ptr<EC_Group_Data> data;
15,616✔
310

311
   if(auto oid = OID::from_name(name)) {
15,616✔
312
      data = ec_group_data().lookup(oid.value());
31,232✔
313
   }
×
314

315
   if(!data) {
15,616✔
316
      throw Invalid_Argument(fmt("Unknown EC_Group '{}'", name));
×
317
   }
318

319
   return EC_Group(std::move(data));
15,616✔
320
}
15,616✔
321

322
EC_Group::EC_Group(std::string_view str) {
61✔
323
   if(str.empty()) {
61✔
324
      return;  // no initialization / uninitialized
325
   }
326

327
   try {
61✔
328
      const OID oid = OID::from_string(str);
61✔
329
      if(oid.has_value()) {
56✔
330
         m_data = ec_group_data().lookup(oid);
56✔
331
      }
332
   } catch(...) {}
61✔
333

334
   if(m_data == nullptr) {
61✔
335
      if(str.size() > 30 && str.substr(0, 29) == "-----BEGIN EC PARAMETERS-----") {
5✔
336
         // OK try it as PEM ...
337
         const auto ber = PEM_Code::decode_check_label(str, "EC PARAMETERS");
5✔
338

339
         auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
5✔
340
         this->m_data = data.first;
5✔
341
         this->m_explicit_encoding = data.second;
5✔
342
      }
10✔
343
   }
344

345
   if(m_data == nullptr) {
61✔
346
      throw Invalid_Argument(fmt("Unknown ECC group '{}'", str));
×
347
   }
348
}
×
349

350
//static
351
EC_Group EC_Group::from_PEM(std::string_view pem) {
1✔
352
   const auto ber = PEM_Code::decode_check_label(pem, "EC PARAMETERS");
1✔
353
   return EC_Group(ber);
1✔
354
}
1✔
355

356
EC_Group::EC_Group(const BigInt& p,
3✔
357
                   const BigInt& a,
358
                   const BigInt& b,
359
                   const BigInt& base_x,
360
                   const BigInt& base_y,
361
                   const BigInt& order,
362
                   const BigInt& cofactor,
363
                   const OID& oid) {
3✔
364
   m_data =
3✔
365
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
3✔
366
}
3✔
367

368
EC_Group::EC_Group(const OID& oid,
47✔
369
                   const BigInt& p,
370
                   const BigInt& a,
371
                   const BigInt& b,
372
                   const BigInt& base_x,
373
                   const BigInt& base_y,
374
                   const BigInt& order) {
47✔
375
   BOTAN_ARG_CHECK(oid.has_value(), "An OID is required for creating an EC_Group");
47✔
376

377
   // TODO(Botan4) remove this and require 192 bits minimum
378
#if defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
379
   constexpr size_t p_bits_lower_bound = 192;
380
#else
381
   constexpr size_t p_bits_lower_bound = 128;
47✔
382
#endif
383

384
   BOTAN_ARG_CHECK(p.bits() >= p_bits_lower_bound, "EC_Group p too small");
47✔
385
   BOTAN_ARG_CHECK(p.bits() <= 521, "EC_Group p too large");
47✔
386

387
   if(p.bits() == 521) {
47✔
388
      const auto p521 = BigInt::power_of_2(521) - 1;
2✔
389
      BOTAN_ARG_CHECK(p == p521, "EC_Group with p of 521 bits must be 2**521-1");
1✔
390
   } else if(p.bits() == 239) {
47✔
391
      const auto x962_p239 = []() {
18✔
392
         BigInt p239;
3✔
393
         for(size_t i = 0; i != 239; ++i) {
720✔
394
            if(i < 47 || ((i >= 94) && (i != 143))) {
717✔
395
               p239.set_bit(i);
717✔
396
            }
397
         }
398
         return p239;
3✔
399
      }();
3✔
400

401
      BOTAN_ARG_CHECK(p == x962_p239, "EC_Group with p of 239 bits must be the X9.62 prime");
3✔
402
   } else {
3✔
403
      BOTAN_ARG_CHECK(p.bits() % 32 == 0, "EC_Group p must be a multiple of 32 bits");
43✔
404
   }
405

406
   BOTAN_ARG_CHECK(p % 4 == 3, "EC_Group p must be congruent to 3 modulo 4");
44✔
407

408
   BOTAN_ARG_CHECK(a >= 0 && a < p, "EC_Group a is invalid");
84✔
409
   BOTAN_ARG_CHECK(b > 0 && b < p, "EC_Group b is invalid");
84✔
410
   BOTAN_ARG_CHECK(base_x >= 0 && base_x < p, "EC_Group base_x is invalid");
84✔
411
   BOTAN_ARG_CHECK(base_y >= 0 && base_y < p, "EC_Group base_y is invalid");
84✔
412
   BOTAN_ARG_CHECK(p.bits() == order.bits(), "EC_Group p and order must have the same number of bits");
42✔
413

414
   Modular_Reducer mod_p(p);
39✔
415
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(p, mod_p), "EC_Group p is not prime");
39✔
416
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(order), "EC_Group order is not prime");
39✔
417

418
   // This catches someone "ignoring" a cofactor and just trying to
419
   // provide the subgroup order
420
   BOTAN_ARG_CHECK((p - order).abs().bits() <= (p.bits() / 2) + 1, "Hasse bound invalid");
117✔
421

422
   // Check that 4*a^3 + 27*b^2 != 0
423
   const auto discriminant = mod_p.reduce(mod_p.multiply(4, mod_p.cube(a)) + mod_p.multiply(27, mod_p.square(b)));
273✔
424
   BOTAN_ARG_CHECK(discriminant != 0, "EC_Group discriminant is invalid");
39✔
425

426
   // Check that the generator (base_x,base_y) is on the curve; y^2 = x^3 + a*x + b
427
   auto y2 = mod_p.square(base_y);
39✔
428
   auto x3_ax_b = mod_p.reduce(mod_p.cube(base_x) + mod_p.multiply(a, base_x) + b);
157✔
429
   BOTAN_ARG_CHECK(y2 == x3_ax_b, "EC_Group generator is not on the curve");
39✔
430

431
   BigInt cofactor(1);
38✔
432

433
   m_data =
38✔
434
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
38✔
435
}
164✔
436

437
EC_Group::EC_Group(std::span<const uint8_t> ber) {
5,255✔
438
   auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
5,255✔
439
   m_data = data.first;
4,875✔
440
   m_explicit_encoding = data.second;
4,875✔
441
}
5,255✔
442

443
const EC_Group_Data& EC_Group::data() const {
117,659✔
444
   if(m_data == nullptr) {
117,659✔
445
      throw Invalid_State("EC_Group uninitialized");
×
446
   }
447
   return *m_data;
117,659✔
448
}
449

450
size_t EC_Group::get_p_bits() const {
1,284✔
451
   return data().p_bits();
1,284✔
452
}
453

454
size_t EC_Group::get_p_bytes() const {
13,613✔
455
   return data().p_bytes();
13,613✔
456
}
457

458
size_t EC_Group::get_order_bits() const {
1,147✔
459
   return data().order_bits();
1,147✔
460
}
461

462
size_t EC_Group::get_order_bytes() const {
26,709✔
463
   return data().order_bytes();
26,709✔
464
}
465

466
const BigInt& EC_Group::get_p() const {
26,575✔
467
   return data().p();
26,575✔
468
}
469

470
const BigInt& EC_Group::get_a() const {
483✔
471
   return data().a();
483✔
472
}
473

474
const BigInt& EC_Group::get_b() const {
371✔
475
   return data().b();
371✔
476
}
477

478
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
479
const EC_Point& EC_Group::get_base_point() const {
677✔
480
   return data().base_point();
677✔
481
}
482

483
const EC_Point& EC_Group::generator() const {
×
484
   return data().base_point();
×
485
}
486

487
bool EC_Group::verify_public_element(const EC_Point& point) const {
×
488
   //check that public point is not at infinity
489
   if(point.is_zero()) {
×
490
      return false;
491
   }
492

493
   //check that public point is on the curve
494
   if(point.on_the_curve() == false) {
×
495
      return false;
496
   }
497

498
   //check that public point has order q
499
   if((point * get_order()).is_zero() == false) {
×
500
      return false;
501
   }
502

503
   if(has_cofactor()) {
×
504
      if((point * get_cofactor()).is_zero()) {
×
505
         return false;
506
      }
507
   }
508

509
   return true;
510
}
511

512
#endif
513

514
const BigInt& EC_Group::get_order() const {
7,903✔
515
   return data().order();
7,903✔
516
}
517

518
const BigInt& EC_Group::get_g_x() const {
979✔
519
   return data().g_x();
979✔
520
}
521

522
const BigInt& EC_Group::get_g_y() const {
979✔
523
   return data().g_y();
979✔
524
}
525

526
const BigInt& EC_Group::get_cofactor() const {
208✔
527
   return data().cofactor();
208✔
528
}
529

530
bool EC_Group::has_cofactor() const {
11,808✔
531
   return data().has_cofactor();
11,808✔
532
}
533

534
const OID& EC_Group::get_curve_oid() const {
23,468✔
535
   return data().oid();
23,468✔
536
}
537

538
EC_Group_Source EC_Group::source() const {
134✔
539
   return data().source();
134✔
540
}
541

542
std::vector<uint8_t> EC_Group::DER_encode() const {
1,321✔
543
   const auto& der_named_curve = data().der_named_curve();
1,321✔
544
   // TODO(Botan4) this can be removed because an OID will always be defined
545
   if(der_named_curve.empty()) {
1,321✔
546
      throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
547
   }
548

549
   return der_named_curve;
1,321✔
550
}
551

552
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
1,263✔
553
   if(form == EC_Group_Encoding::Explicit) {
1,263✔
554
      std::vector<uint8_t> output;
31✔
555
      DER_Encoder der(output);
31✔
556
      const size_t ecpVers1 = 1;
31✔
557
      const OID curve_type("1.2.840.10045.1.1");  // prime field
31✔
558

559
      const size_t p_bytes = get_p_bytes();
31✔
560

561
      const auto generator = EC_AffinePoint::generator(*this).serialize_uncompressed();
31✔
562

563
      der.start_sequence()
31✔
564
         .encode(ecpVers1)
31✔
565
         .start_sequence()
31✔
566
         .encode(curve_type)
31✔
567
         .encode(get_p())
31✔
568
         .end_cons()
31✔
569
         .start_sequence()
31✔
570
         .encode(get_a().serialize(p_bytes), ASN1_Type::OctetString)
31✔
571
         .encode(get_b().serialize(p_bytes), ASN1_Type::OctetString)
62✔
572
         .end_cons()
31✔
573
         .encode(generator, ASN1_Type::OctetString)
31✔
574
         .encode(get_order())
31✔
575
         .encode(get_cofactor())
31✔
576
         .end_cons();
31✔
577
      return output;
31✔
578
   } else if(form == EC_Group_Encoding::NamedCurve) {
1,263✔
579
      return this->DER_encode();
1,232✔
580
   } else if(form == EC_Group_Encoding::ImplicitCA) {
×
581
      return {0x00, 0x05};
×
582
   } else {
583
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
584
   }
585
}
586

587
std::string EC_Group::PEM_encode() const {
1✔
588
   const std::vector<uint8_t> der = DER_encode(EC_Group_Encoding::Explicit);
1✔
589
   return PEM_Code::encode(der, "EC PARAMETERS");
1✔
590
}
1✔
591

592
bool EC_Group::operator==(const EC_Group& other) const {
48✔
593
   if(m_data == other.m_data) {
48✔
594
      return true;  // same shared rep
595
   }
596

597
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
×
598
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
×
599
           get_cofactor() == other.get_cofactor());
×
600
}
601

602
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
134✔
603
   const bool is_builtin = source() == EC_Group_Source::Builtin;
134✔
604

605
   if(is_builtin && !strong) {
134✔
606
      return true;
607
   }
608

609
   // TODO(Botan4) this can probably all be removed once the deprecated EC_Group
610
   // constructor is removed, since at that point it no longer becomes possible
611
   // to create an EC_Group which fails to satisfy these conditions
612

613
   const BigInt& p = get_p();
28✔
614
   const BigInt& a = get_a();
28✔
615
   const BigInt& b = get_b();
28✔
616
   const BigInt& order = get_order();
28✔
617

618
   if(p <= 3 || order <= 0) {
56✔
619
      return false;
×
620
   }
621
   if(a < 0 || a >= p) {
56✔
622
      return false;
×
623
   }
624
   if(b <= 0 || b >= p) {
56✔
625
      return false;
×
626
   }
627

628
   const size_t test_prob = 128;
28✔
629
   const bool is_randomly_generated = is_builtin;
28✔
630

631
   //check if field modulus is prime
632
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
28✔
633
      return false;
634
   }
635

636
   //check if order is prime
637
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
28✔
638
      return false;
639
   }
640

641
   //compute the discriminant: 4*a^3 + 27*b^2 which must be nonzero
642
   const Modular_Reducer mod_p(p);
28✔
643

644
   const BigInt discriminant = mod_p.reduce(mod_p.multiply(4, mod_p.cube(a)) + mod_p.multiply(27, mod_p.square(b)));
196✔
645

646
   if(discriminant == 0) {
28✔
647
      return false;
648
   }
649

650
   //check for valid cofactor
651
   if(get_cofactor() < 1) {
28✔
652
      return false;
653
   }
654

655
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
656
   const EC_Point& base_point = get_base_point();
28✔
657
   //check if the base point is on the curve
658
   if(!base_point.on_the_curve()) {
28✔
659
      return false;
660
   }
661
   if((base_point * get_cofactor()).is_zero()) {
56✔
662
      return false;
663
   }
664
   //check if order of the base point is correct
665
   if(!(base_point * order).is_zero()) {
28✔
666
      return false;
667
   }
668
#endif
669

670
   // check the Hasse bound (roughly)
671
   if((p - get_cofactor() * order).abs().bits() > (p.bits() / 2) + 1) {
112✔
672
      return false;
673
   }
674

675
   return true;
676
}
28✔
677

678
EC_Group::Mul2Table::Mul2Table(const EC_AffinePoint& h) : m_tbl(h._group()->make_mul2_table(h._inner())) {}
13,257✔
679

680
EC_Group::Mul2Table::~Mul2Table() = default;
13,257✔
681

682
std::optional<EC_AffinePoint> EC_Group::Mul2Table::mul2_vartime(const EC_Scalar& x, const EC_Scalar& y) const {
2,123✔
683
   auto pt = m_tbl->mul2_vartime(x._inner(), y._inner());
2,123✔
684
   if(pt) {
2,123✔
685
      return EC_AffinePoint::_from_inner(std::move(pt));
4,246✔
686
   } else {
687
      return {};
×
688
   }
689
}
2,123✔
690

691
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
23,017✔
692
                                                      const EC_Scalar& x,
693
                                                      const EC_Scalar& y) const {
694
   return m_tbl->mul2_vartime_x_mod_order_eq(v._inner(), x._inner(), y._inner());
23,017✔
695
}
696

697
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
22,847✔
698
                                                      const EC_Scalar& c,
699
                                                      const EC_Scalar& x,
700
                                                      const EC_Scalar& y) const {
701
   return this->mul2_vartime_x_mod_order_eq(v, c * x, c * y);
22,847✔
702
}
703

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