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

randombit / botan / 12939720817

23 Jan 2025 11:04PM UTC coverage: 91.208% (+0.006%) from 91.202%
12939720817

Pull #4588

github

web-flow
Merge d6c027109 into 525a35d62
Pull Request #4588: Reduce overhead from computing modular reduction parameters

93607 of 102630 relevant lines covered (91.21%)

11442383.67 hits per line

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

87.98
/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/numthry.h>
17
#include <botan/pem.h>
18
#include <botan/reducer.h>
19
#include <botan/rng.h>
20
#include <botan/internal/ec_inner_data.h>
21
#include <botan/internal/fmt.h>
22
#include <botan/internal/primality.h>
23
#include <vector>
24

25
namespace Botan {
26

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

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

38
      std::shared_ptr<EC_Group_Data> lookup(const OID& oid) {
21,746✔
39
         lock_guard_type<mutex_type> lock(m_mutex);
21,746✔
40

41
         for(auto i : m_registered_curves) {
159,480✔
42
            if(i->oid() == oid) {
158,289✔
43
               return i;
20,555✔
44
            }
45
         }
158,289✔
46

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

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

58
            m_registered_curves.push_back(data);
1,138✔
59
            return data;
1,138✔
60
         }
61

62
         // Nope, unknown curve
63
         return std::shared_ptr<EC_Group_Data>();
53✔
64
      }
22,937✔
65

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

176
   static Allocator_Initializer g_init_allocator;
23,612✔
177
   static EC_Group_Data_Map g_ec_data;
23,612✔
178
   return g_ec_data;
23,612✔
179
}
180

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

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

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

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

210
   auto next_obj_type = ber.peek_next_object().type_tag();
6,388✔
211

212
   if(next_obj_type == ASN1_Type::ObjectId) {
6,380✔
213
      OID oid;
6,048✔
214
      ber.decode(oid);
6,048✔
215

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

221
      return std::make_pair(data, false);
5,953✔
222
   } else if(next_obj_type == ASN1_Type::Sequence) {
6,433✔
223
      BigInt p, a, b, order, cofactor;
329✔
224
      std::vector<uint8_t> base_pt;
329✔
225
      std::vector<uint8_t> seed;
329✔
226

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

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

248
      auto mod_p = Modular_Reducer::for_public_modulus(p);
157✔
249
      if(!is_bailie_psw_probable_prime(p, mod_p)) {
157✔
250
         throw Decoding_Error("ECC p parameter is not a prime");
32✔
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.is_negative() || order.is_zero() || order >= 2 * p) {
241✔
262
         throw Decoding_Error("Invalid ECC group order");
2✔
263
      }
264

265
      auto mod_order = Modular_Reducer::for_public_modulus(order);
59✔
266
      if(!is_bailie_psw_probable_prime(order, mod_order)) {
59✔
267
         throw Decoding_Error("Invalid ECC order parameter");
×
268
      }
269

270
      if(cofactor <= 0 || cofactor >= 16) {
116✔
271
         throw Decoding_Error("Invalid ECC cofactor parameter");
3✔
272
      }
273

274
      const size_t p_bytes = p.bytes();
56✔
275
      if(base_pt.size() != 1 + p_bytes && base_pt.size() != 1 + 2 * p_bytes) {
56✔
276
         throw Decoding_Error("Invalid ECC base point encoding");
×
277
      }
278

279
      auto [g_x, g_y] = [&]() {
55✔
280
         const uint8_t hdr = base_pt[0];
56✔
281

282
         if(hdr == 0x04 && base_pt.size() == 1 + 2 * p_bytes) {
56✔
283
            BigInt x = BigInt::decode(&base_pt[1], p_bytes);
55✔
284
            BigInt y = BigInt::decode(&base_pt[p_bytes + 1], p_bytes);
55✔
285

286
            if(x < p && y < p) {
55✔
287
               return std::make_pair(x, y);
55✔
288
            }
289
         } else if((hdr == 0x02 || hdr == 0x03) && base_pt.size() == 1 + p_bytes) {
111✔
290
            BigInt x = BigInt::decode(&base_pt[1], p_bytes);
×
291
            BigInt y = sqrt_modulo_prime(((x * x + a) * x + b) % p, p);
×
292

293
            if(x < p && y >= 0) {
×
294
               const bool y_mod_2 = (hdr & 0x01) == 1;
×
295
               if(y.get_bit(0) != y_mod_2) {
×
296
                  y = p - y;
×
297
               }
298

299
               return std::make_pair(x, y);
×
300
            }
301
         }
×
302

303
         throw Decoding_Error("Invalid ECC base point encoding");
1✔
304
      }();
56✔
305

306
      auto y2 = mod_p.square(g_y);
55✔
307
      auto x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) + b);
220✔
308
      if(y2 != x3_ax_b) {
55✔
309
         throw Decoding_Error("Invalid ECC base point");
×
310
      }
311

312
      auto data = ec_group_data().lookup_or_create(p, a, b, g_x, g_y, order, cofactor, OID(), source);
55✔
313
      return std::make_pair(data, true);
55✔
314
   } else if(next_obj_type == ASN1_Type::Null) {
2,522✔
315
      throw Decoding_Error("Decoding ImplicitCA ECC parameters is not supported");
1✔
316
   } else {
317
      throw Decoding_Error(
2✔
318
         fmt("Unexpected tag {} while decoding ECC domain params", asn1_tag_to_string(next_obj_type)));
4✔
319
   }
320
}
6,388✔
321

322
EC_Group::EC_Group() = default;
15✔
323

324
EC_Group::~EC_Group() = default;
133,087✔
325

326
EC_Group::EC_Group(const EC_Group&) = default;
48,359✔
327

328
EC_Group& EC_Group::operator=(const EC_Group&) = default;
×
329

330
// Internal constructor
331
EC_Group::EC_Group(std::shared_ptr<EC_Group_Data>&& data) : m_data(std::move(data)) {}
15,684✔
332

333
//static
334
EC_Group EC_Group::from_OID(const OID& oid) {
71✔
335
   auto data = ec_group_data().lookup(oid);
71✔
336

337
   if(!data) {
71✔
338
      throw Invalid_Argument(fmt("No EC_Group associated with OID '{}'", oid.to_string()));
×
339
   }
340

341
   return EC_Group(std::move(data));
71✔
342
}
71✔
343

344
//static
345
EC_Group EC_Group::from_name(std::string_view name) {
15,613✔
346
   std::shared_ptr<EC_Group_Data> data;
15,613✔
347

348
   if(auto oid = OID::from_name(name)) {
15,613✔
349
      data = ec_group_data().lookup(oid.value());
31,226✔
350
   }
×
351

352
   if(!data) {
15,613✔
353
      throw Invalid_Argument(fmt("Unknown EC_Group '{}'", name));
×
354
   }
355

356
   return EC_Group(std::move(data));
15,613✔
357
}
15,613✔
358

359
EC_Group::EC_Group(std::string_view str) {
61✔
360
   if(str.empty()) {
61✔
361
      return;  // no initialization / uninitialized
362
   }
363

364
   try {
61✔
365
      const OID oid = OID::from_string(str);
61✔
366
      if(oid.has_value()) {
56✔
367
         m_data = ec_group_data().lookup(oid);
56✔
368
      }
369
   } catch(...) {}
61✔
370

371
   if(m_data == nullptr) {
61✔
372
      if(str.size() > 30 && str.substr(0, 29) == "-----BEGIN EC PARAMETERS-----") {
5✔
373
         // OK try it as PEM ...
374
         const auto ber = PEM_Code::decode_check_label(str, "EC PARAMETERS");
5✔
375

376
         auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
5✔
377
         this->m_data = data.first;
5✔
378
         this->m_explicit_encoding = data.second;
5✔
379
      }
10✔
380
   }
381

382
   if(m_data == nullptr) {
61✔
383
      throw Invalid_Argument(fmt("Unknown ECC group '{}'", str));
×
384
   }
385
}
×
386

387
//static
388
EC_Group EC_Group::from_PEM(std::string_view pem) {
1✔
389
   const auto ber = PEM_Code::decode_check_label(pem, "EC PARAMETERS");
1✔
390
   return EC_Group(ber);
1✔
391
}
1✔
392

393
EC_Group::EC_Group(const BigInt& p,
3✔
394
                   const BigInt& a,
395
                   const BigInt& b,
396
                   const BigInt& base_x,
397
                   const BigInt& base_y,
398
                   const BigInt& order,
399
                   const BigInt& cofactor,
400
                   const OID& oid) {
3✔
401
   m_data =
3✔
402
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
3✔
403
}
3✔
404

405
EC_Group::EC_Group(const OID& oid,
47✔
406
                   const BigInt& p,
407
                   const BigInt& a,
408
                   const BigInt& b,
409
                   const BigInt& base_x,
410
                   const BigInt& base_y,
411
                   const BigInt& order) {
47✔
412
   BOTAN_ARG_CHECK(oid.has_value(), "An OID is required for creating an EC_Group");
47✔
413

414
   // TODO(Botan4) remove this and require 192 bits minimum
415
#if defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
416
   constexpr size_t p_bits_lower_bound = 192;
417
#else
418
   constexpr size_t p_bits_lower_bound = 128;
47✔
419
#endif
420

421
   BOTAN_ARG_CHECK(p.bits() >= p_bits_lower_bound, "EC_Group p too small");
47✔
422
   BOTAN_ARG_CHECK(p.bits() <= 521, "EC_Group p too large");
47✔
423

424
   if(p.bits() == 521) {
47✔
425
      const auto p521 = BigInt::power_of_2(521) - 1;
2✔
426
      BOTAN_ARG_CHECK(p == p521, "EC_Group with p of 521 bits must be 2**521-1");
1✔
427
   } else if(p.bits() == 239) {
47✔
428
      const auto x962_p239 = []() {
18✔
429
         BigInt p239;
3✔
430
         for(size_t i = 0; i != 239; ++i) {
720✔
431
            if(i < 47 || ((i >= 94) && (i != 143))) {
717✔
432
               p239.set_bit(i);
717✔
433
            }
434
         }
435
         return p239;
3✔
436
      }();
3✔
437

438
      BOTAN_ARG_CHECK(p == x962_p239, "EC_Group with p of 239 bits must be the X9.62 prime");
3✔
439
   } else {
3✔
440
      BOTAN_ARG_CHECK(p.bits() % 32 == 0, "EC_Group p must be a multiple of 32 bits");
43✔
441
   }
442

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

445
   BOTAN_ARG_CHECK(a >= 0 && a < p, "EC_Group a is invalid");
84✔
446
   BOTAN_ARG_CHECK(b > 0 && b < p, "EC_Group b is invalid");
84✔
447
   BOTAN_ARG_CHECK(base_x >= 0 && base_x < p, "EC_Group base_x is invalid");
84✔
448
   BOTAN_ARG_CHECK(base_y >= 0 && base_y < p, "EC_Group base_y is invalid");
84✔
449
   BOTAN_ARG_CHECK(p.bits() == order.bits(), "EC_Group p and order must have the same number of bits");
42✔
450

451
   auto mod_p = Modular_Reducer::for_public_modulus(p);
39✔
452
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(p, mod_p), "EC_Group p is not prime");
39✔
453

454
   auto mod_order = Modular_Reducer::for_public_modulus(order);
39✔
455
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(order, mod_order), "EC_Group order is not prime");
39✔
456

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

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

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

470
   BigInt cofactor(1);
38✔
471

472
   m_data =
38✔
473
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
38✔
474
}
165✔
475

476
EC_Group::EC_Group(std::span<const uint8_t> ber) {
6,383✔
477
   auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
6,383✔
478
   m_data = data.first;
6,003✔
479
   m_explicit_encoding = data.second;
6,003✔
480
}
6,383✔
481

482
const EC_Group_Data& EC_Group::data() const {
119,799✔
483
   if(m_data == nullptr) {
119,799✔
484
      throw Invalid_State("EC_Group uninitialized");
×
485
   }
486
   return *m_data;
119,799✔
487
}
488

489
size_t EC_Group::get_p_bits() const {
1,284✔
490
   return data().p_bits();
1,284✔
491
}
492

493
size_t EC_Group::get_p_bytes() const {
13,609✔
494
   return data().p_bytes();
13,609✔
495
}
496

497
size_t EC_Group::get_order_bits() const {
1,147✔
498
   return data().order_bits();
1,147✔
499
}
500

501
size_t EC_Group::get_order_bytes() const {
27,836✔
502
   return data().order_bytes();
27,836✔
503
}
504

505
const BigInt& EC_Group::get_p() const {
26,567✔
506
   return data().p();
26,567✔
507
}
508

509
const BigInt& EC_Group::get_a() const {
433✔
510
   return data().a();
433✔
511
}
512

513
const BigInt& EC_Group::get_b() const {
321✔
514
   return data().b();
321✔
515
}
516

517
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
518
const EC_Point& EC_Group::get_base_point() const {
677✔
519
   return data().base_point();
677✔
520
}
521

522
const EC_Point& EC_Group::generator() const {
×
523
   return data().base_point();
×
524
}
525

526
bool EC_Group::verify_public_element(const EC_Point& point) const {
×
527
   //check that public point is not at infinity
528
   if(point.is_zero()) {
×
529
      return false;
530
   }
531

532
   //check that public point is on the curve
533
   if(point.on_the_curve() == false) {
×
534
      return false;
535
   }
536

537
   //check that public point has order q
538
   if((point * get_order()).is_zero() == false) {
×
539
      return false;
540
   }
541

542
   if(has_cofactor()) {
×
543
      if((point * get_cofactor()).is_zero()) {
×
544
         return false;
545
      }
546
   }
547

548
   return true;
549
}
550

551
#endif
552

553
const BigInt& EC_Group::get_order() const {
7,903✔
554
   return data().order();
7,903✔
555
}
556

557
const BigInt& EC_Group::get_g_x() const {
979✔
558
   return data().g_x();
979✔
559
}
560

561
const BigInt& EC_Group::get_g_y() const {
979✔
562
   return data().g_y();
979✔
563
}
564

565
const BigInt& EC_Group::get_cofactor() const {
208✔
566
   return data().cofactor();
208✔
567
}
568

569
bool EC_Group::has_cofactor() const {
11,808✔
570
   return data().has_cofactor();
11,808✔
571
}
572

573
const OID& EC_Group::get_curve_oid() const {
24,593✔
574
   return data().oid();
24,593✔
575
}
576

577
EC_Group_Source EC_Group::source() const {
134✔
578
   return data().source();
134✔
579
}
580

581
std::vector<uint8_t> EC_Group::DER_encode() const {
1,321✔
582
   const auto& der_named_curve = data().der_named_curve();
1,321✔
583
   // TODO(Botan4) this can be removed because an OID will always be defined
584
   if(der_named_curve.empty()) {
1,321✔
585
      throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
586
   }
587

588
   return der_named_curve;
1,321✔
589
}
590

591
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
1,263✔
592
   if(form == EC_Group_Encoding::Explicit) {
1,263✔
593
      std::vector<uint8_t> output;
31✔
594
      DER_Encoder der(output);
31✔
595
      const size_t ecpVers1 = 1;
31✔
596
      const OID curve_type("1.2.840.10045.1.1");  // prime field
31✔
597

598
      const size_t p_bytes = get_p_bytes();
31✔
599

600
      const auto generator = EC_AffinePoint::generator(*this).serialize_uncompressed();
31✔
601

602
      der.start_sequence()
31✔
603
         .encode(ecpVers1)
31✔
604
         .start_sequence()
31✔
605
         .encode(curve_type)
31✔
606
         .encode(get_p())
31✔
607
         .end_cons()
31✔
608
         .start_sequence()
31✔
609
         .encode(get_a().serialize(p_bytes), ASN1_Type::OctetString)
31✔
610
         .encode(get_b().serialize(p_bytes), ASN1_Type::OctetString)
62✔
611
         .end_cons()
31✔
612
         .encode(generator, ASN1_Type::OctetString)
31✔
613
         .encode(get_order())
31✔
614
         .encode(get_cofactor())
31✔
615
         .end_cons();
31✔
616
      return output;
31✔
617
   } else if(form == EC_Group_Encoding::NamedCurve) {
1,263✔
618
      return this->DER_encode();
1,232✔
619
   } else if(form == EC_Group_Encoding::ImplicitCA) {
×
620
      return {0x00, 0x05};
×
621
   } else {
622
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
623
   }
624
}
625

626
std::string EC_Group::PEM_encode() const {
1✔
627
   const std::vector<uint8_t> der = DER_encode(EC_Group_Encoding::Explicit);
1✔
628
   return PEM_Code::encode(der, "EC PARAMETERS");
1✔
629
}
1✔
630

631
bool EC_Group::operator==(const EC_Group& other) const {
48✔
632
   if(m_data == other.m_data) {
48✔
633
      return true;  // same shared rep
634
   }
635

636
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
×
637
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
×
638
           get_cofactor() == other.get_cofactor());
×
639
}
640

641
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
134✔
642
   const bool is_builtin = source() == EC_Group_Source::Builtin;
134✔
643

644
   if(is_builtin && !strong) {
134✔
645
      return true;
646
   }
647

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

652
   const BigInt& p = get_p();
28✔
653
   const BigInt& a = get_a();
28✔
654
   const BigInt& b = get_b();
28✔
655
   const BigInt& order = get_order();
28✔
656

657
   if(p <= 3 || order <= 0) {
56✔
658
      return false;
×
659
   }
660
   if(a < 0 || a >= p) {
56✔
661
      return false;
×
662
   }
663
   if(b <= 0 || b >= p) {
56✔
664
      return false;
×
665
   }
666

667
   const size_t test_prob = 128;
28✔
668
   const bool is_randomly_generated = is_builtin;
28✔
669

670
   //check if field modulus is prime
671
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
28✔
672
      return false;
673
   }
674

675
   //check if order is prime
676
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
28✔
677
      return false;
678
   }
679

680
   //compute the discriminant: 4*a^3 + 27*b^2 which must be nonzero
681
   auto mod_p = Modular_Reducer::for_public_modulus(p);
28✔
682

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

685
   if(discriminant == 0) {
28✔
686
      return false;
687
   }
688

689
   //check for valid cofactor
690
   if(get_cofactor() < 1) {
28✔
691
      return false;
692
   }
693

694
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
695
   const EC_Point& base_point = get_base_point();
28✔
696
   //check if the base point is on the curve
697
   if(!base_point.on_the_curve()) {
28✔
698
      return false;
699
   }
700
   if((base_point * get_cofactor()).is_zero()) {
56✔
701
      return false;
702
   }
703
   //check if order of the base point is correct
704
   if(!(base_point * order).is_zero()) {
28✔
705
      return false;
706
   }
707
#endif
708

709
   // check the Hasse bound (roughly)
710
   if((p - get_cofactor() * order).abs().bits() > (p.bits() / 2) + 1) {
112✔
711
      return false;
712
   }
713

714
   return true;
715
}
28✔
716

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

719
EC_Group::Mul2Table::~Mul2Table() = default;
13,257✔
720

721
std::optional<EC_AffinePoint> EC_Group::Mul2Table::mul2_vartime(const EC_Scalar& x, const EC_Scalar& y) const {
2,122✔
722
   auto pt = m_tbl->mul2_vartime(x._inner(), y._inner());
2,122✔
723
   if(pt) {
2,122✔
724
      return EC_AffinePoint::_from_inner(std::move(pt));
4,244✔
725
   } else {
726
      return {};
×
727
   }
728
}
2,122✔
729

730
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
23,005✔
731
                                                      const EC_Scalar& x,
732
                                                      const EC_Scalar& y) const {
733
   return m_tbl->mul2_vartime_x_mod_order_eq(v._inner(), x._inner(), y._inner());
23,005✔
734
}
735

736
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
22,836✔
737
                                                      const EC_Scalar& c,
738
                                                      const EC_Scalar& x,
739
                                                      const EC_Scalar& y) const {
740
   return this->mul2_vartime_x_mod_order_eq(v, c * x, c * y);
22,836✔
741
}
742

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