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

randombit / botan / 25619874939

09 May 2026 05:34PM UTC coverage: 89.328% (-0.002%) from 89.33%
25619874939

push

github

web-flow
Merge pull request #5591 from randombit/jack/gha-cache-cleanup

In GH Actions clean up branch caches after the PRs are merged

107641 of 120501 relevant lines covered (89.33%)

11279629.67 hits per line

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

87.7
/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/rng.h>
19
#include <botan/internal/barrett.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,773✔
32
         const lock_guard_type<mutex_type> lock(m_mutex);
1,773✔
33
         const size_t count = m_registered_curves.size();
1,773✔
34
         m_registered_curves.clear();
1,773✔
35
         return count;
1,773✔
36
      }
1,773✔
37

38
      bool unregister(const OID& oid) {
14✔
39
         // TODO(Botan4)
40
         if(oid.empty()) {
14✔
41
            throw Invalid_Argument("OID must not be empty");
1✔
42
         }
43

44
         const lock_guard_type<mutex_type> lock(m_mutex);
13✔
45
         for(size_t i = 0; i < m_registered_curves.size(); i++) {
111✔
46
            if(m_registered_curves[i]->oid() == oid) {
105✔
47
               m_registered_curves.erase(m_registered_curves.begin() + i);
7✔
48
               return true;
7✔
49
            }
50
         }
51
         return false;
52
      }
13✔
53

54
      std::shared_ptr<EC_Group_Data> lookup(const OID& oid) {
22,870✔
55
         const lock_guard_type<mutex_type> lock(m_mutex);
22,870✔
56

57
         for(auto i : m_registered_curves) {
235,145✔
58
            if(i->oid() == oid) {
234,518✔
59
               return i;
22,243✔
60
            }
61
         }
234,518✔
62

63
         // Not found, check hardcoded data
64
         std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid);
627✔
65

66
         if(data) {
627✔
67
            m_registered_curves.push_back(data);
614✔
68
            return data;
614✔
69
         }
70

71
         // Nope, unknown curve
72
         return std::shared_ptr<EC_Group_Data>();
13✔
73
      }
23,497✔
74

75
      std::shared_ptr<EC_Group_Data> lookup_or_create(const BigInt& p,
44✔
76
                                                      const BigInt& a,
77
                                                      const BigInt& b,
78
                                                      const BigInt& g_x,
79
                                                      const BigInt& g_y,
80
                                                      const BigInt& order,
81
                                                      const BigInt& cofactor,
82
                                                      const OID& oid,
83
                                                      EC_Group_Source source) {
84
         BOTAN_ASSERT_NOMSG(oid.has_value());
44✔
85

86
         const lock_guard_type<mutex_type> lock(m_mutex);
44✔
87

88
         for(auto i : m_registered_curves) {
835✔
89
            if(i->oid() == oid) {
824✔
90
               /*
91
               * If both OID and params are the same then we are done, just return
92
               * the already registered curve obj.
93
               *
94
               * First verify that the params match, to catch an application
95
               * that is attempting to register a EC_Group under the same OID as
96
               * another group currently in use
97
               */
98
               if(!i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
33✔
99
                  throw Invalid_Argument("Attempting to register a curve using OID " + oid.to_string() +
×
100
                                         " but a distinct curve is already registered using that OID");
×
101
               }
102

103
               return i;
33✔
104
            }
105

106
            /*
107
            * If the same curve was previously created without an OID but is now
108
            * being registered again using an OID, save that OID.
109
            *
110
            * TODO(Botan4) remove this block; this situation won't be possible since
111
            * we will require all groups to have an OID
112
            */
113
            if(i->oid().empty() && i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
791✔
114
               i->set_oid(oid);
×
115
               return i;
×
116
            }
117
         }
824✔
118

119
         /*
120
         * Not found in current list, so we need to create a new entry
121
         */
122
         auto new_group = [&] {
×
123
            if(auto g = EC_Group::EC_group_info(oid); g != nullptr) {
11✔
124
               /*
125
               * This turned out to be the OID of one of the builtin groups. Verify
126
               * that all of the provided parameters match that builtin group.
127
               */
128
               BOTAN_ARG_CHECK(g->params_match(p, a, b, g_x, g_y, order, cofactor),
×
129
                               "Attempting to register an EC group under OID of hardcoded group");
130

131
               return g;
×
132
            } else {
133
               /*
134
               * This path is taken for an application registering a new EC_Group with an OID specified
135
               */
136
               return EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, oid, source);
11✔
137
            }
11✔
138
         }();
11✔
139

140
         m_registered_curves.push_back(new_group);
11✔
141
         return new_group;
11✔
142
      }
55✔
143

144
      std::shared_ptr<EC_Group_Data> lookup_from_params(const BigInt& p,
68✔
145
                                                        const BigInt& a,
146
                                                        const BigInt& b,
147
                                                        std::span<const uint8_t> base_pt,
148
                                                        const BigInt& order,
149
                                                        const BigInt& cofactor) {
150
         const lock_guard_type<mutex_type> lock(m_mutex);
68✔
151

152
         for(auto i : m_registered_curves) {
834✔
153
            if(i->params_match(p, a, b, base_pt, order, cofactor)) {
818✔
154
               return i;
52✔
155
            }
156
         }
818✔
157

158
         // Try to use the order as a hint to look up the group id
159
         const OID oid_from_order = EC_Group::EC_group_identity_from_order(order);
16✔
160
         if(oid_from_order.has_value()) {
16✔
161
            auto new_group = EC_Group::EC_group_info(oid_from_order);
12✔
162

163
            // Have to check all params in the (unlikely/malicious) event of an order collision
164
            if(new_group && new_group->params_match(p, a, b, base_pt, order, cofactor)) {
12✔
165
               m_registered_curves.push_back(new_group);
4✔
166
               return new_group;
4✔
167
            }
168
         }
12✔
169

170
         return {};
11✔
171
      }
83✔
172

173
      // TODO(Botan4) this entire function can be removed since OIDs will be required
174
      std::shared_ptr<EC_Group_Data> lookup_or_create_without_oid(const BigInt& p,
2✔
175
                                                                  const BigInt& a,
176
                                                                  const BigInt& b,
177
                                                                  const BigInt& g_x,
178
                                                                  const BigInt& g_y,
179
                                                                  const BigInt& order,
180
                                                                  const BigInt& cofactor,
181
                                                                  EC_Group_Source source) {
182
         const lock_guard_type<mutex_type> lock(m_mutex);
2✔
183

184
         for(auto i : m_registered_curves) {
30✔
185
            if(i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
28✔
186
               return i;
×
187
            }
188
         }
28✔
189

190
         // Try to use the order as a hint to look up the group id
191
         const OID oid_from_order = EC_Group::EC_group_identity_from_order(order);
2✔
192
         if(oid_from_order.has_value()) {
2✔
193
            auto new_group = EC_Group::EC_group_info(oid_from_order);
1✔
194

195
            // Have to check all params in the (unlikely/malicious) event of an order collision
196
            if(new_group && new_group->params_match(p, a, b, g_x, g_y, order, cofactor)) {
1✔
197
               m_registered_curves.push_back(new_group);
1✔
198
               return new_group;
1✔
199
            }
200
         }
1✔
201

202
         /*
203
         * At this point we have failed to identify the group; it is not any of
204
         * the builtin values, nor is it a group that the user had previously
205
         * registered explicitly. We create the group data without an OID.
206
         *
207
         * TODO(Botan4) remove this; throw an exception instead
208
         */
209
         auto new_group = EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, OID(), source);
1✔
210
         m_registered_curves.push_back(new_group);
1✔
211
         return new_group;
1✔
212
      }
5✔
213

214
   private:
215
      mutex_type m_mutex;
216
      // TODO(Botan4): Once OID is required we could make this into a map
217
      std::vector<std::shared_ptr<EC_Group_Data>> m_registered_curves;
218
};
219

220
//static
221
EC_Group_Data_Map& EC_Group::ec_group_data() {
24,771✔
222
   /*
223
   * This exists purely to ensure the allocator is constructed before g_ec_data,
224
   * which ensures that its destructor runs after ~g_ec_data is complete.
225
   */
226

227
   static const Allocator_Initializer g_init_allocator;
24,771✔
228
   static EC_Group_Data_Map g_ec_data;
24,771✔
229
   return g_ec_data;
24,771✔
230
}
231

232
//static
233
size_t EC_Group::clear_registered_curve_data() {
1,773✔
234
   return ec_group_data().clear();
1,773✔
235
}
236

237
//static
238
std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(const char* p_str,
627✔
239
                                                            const char* a_str,
240
                                                            const char* b_str,
241
                                                            const char* g_x_str,
242
                                                            const char* g_y_str,
243
                                                            const char* order_str,
244
                                                            const OID& oid) {
245
   BOTAN_ARG_CHECK(oid.has_value(), "EC_Group::load_EC_group_info OID must be set");
627✔
246

247
   const BigInt p(p_str);
627✔
248
   const BigInt a(a_str);
627✔
249
   const BigInt b(b_str);
627✔
250
   const BigInt g_x(g_x_str);
627✔
251
   const BigInt g_y(g_y_str);
627✔
252
   const BigInt order(order_str);
627✔
253
   const BigInt cofactor(1);  // implicit
627✔
254

255
   return EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, oid, EC_Group_Source::Builtin);
1,254✔
256
}
627✔
257

258
//static
259
std::pair<std::shared_ptr<EC_Group_Data>, bool> EC_Group::DER_decode_EC_group(std::span<const uint8_t> der,
6,963✔
260
                                                                              EC_Group_Source source) {
261
   BER_Decoder dec(der, BER_Decoder::Limits::DER());
6,963✔
262

263
   auto next_obj_type = dec.peek_next_object().type_tag();
6,963✔
264

265
   if(next_obj_type == ASN1_Type::ObjectId) {
6,963✔
266
      OID oid;
6,821✔
267
      dec.decode(oid);
6,821✔
268

269
      auto data = ec_group_data().lookup(oid);
6,821✔
270
      if(!data) {
6,821✔
271
         throw Decoding_Error(fmt("Unknown namedCurve OID '{}'", oid.to_string()));
18✔
272
      }
273

274
      return std::make_pair(data, false);
6,812✔
275
   } else if(next_obj_type == ASN1_Type::Sequence) {
6,972✔
276
      BigInt p;
141✔
277
      BigInt a;
141✔
278
      BigInt b;
141✔
279
      BigInt order;
141✔
280
      BigInt cofactor;
141✔
281
      std::vector<uint8_t> base_pt;
141✔
282
      std::vector<uint8_t> seed;
141✔
283

284
      dec.start_sequence()
141✔
285
         .decode_and_check<size_t>(1, "Unknown ECC param version code")
282✔
286
         .start_sequence()
141✔
287
         .decode_and_check(OID({1, 2, 840, 10045, 1, 1}), "Only prime ECC fields supported")
282✔
288
         .decode(p)
112✔
289
         .end_cons()
112✔
290
         .start_sequence()
112✔
291
         .decode_octet_string_bigint(a)
112✔
292
         .decode_octet_string_bigint(b)
112✔
293
         .decode_optional_string(seed, ASN1_Type::BitString, ASN1_Type::BitString, ASN1_Class::Universal)
94✔
294
         .end_cons()
94✔
295
         .decode(base_pt, ASN1_Type::OctetString)
94✔
296
         .decode(order)
92✔
297
         .decode(cofactor)
91✔
298
         .end_cons()
91✔
299
         .verify_end();
91✔
300

301
      // TODO(Botan4) Require cofactor == 1
302
      if(cofactor <= 0 || cofactor >= 16) {
180✔
303
         throw Decoding_Error("Invalid ECC cofactor parameter");
6✔
304
      }
305

306
      if(p.bits() < 112 || p.bits() > 521 || p.signum() < 0) {
85✔
307
         throw Decoding_Error("ECC p parameter is invalid size");
×
308
      }
309

310
      // A can be zero
311
      if(a.signum() < 0 || a >= p) {
170✔
312
         throw Decoding_Error("Invalid ECC a parameter");
4✔
313
      }
314

315
      // B must be > 0
316
      if(b.signum() <= 0 || b >= p) {
161✔
317
         throw Decoding_Error("Invalid ECC b parameter");
10✔
318
      }
319

320
      if(order.signum() <= 0 || order >= 2 * p) {
208✔
321
         throw Decoding_Error("Invalid ECC group order");
3✔
322
      }
323

324
      if(auto data = ec_group_data().lookup_from_params(p, a, b, base_pt, order, cofactor)) {
146✔
325
         return std::make_pair(data, true);
56✔
326
      }
56✔
327

328
      /*
329
      TODO(Botan4) the remaining code is used only to handle the case of decoding an EC_Group
330
      which is neither a builtin group nor a group that was registered by the application.
331
      It can all be removed and replaced with a throw
332
      */
333

334
      auto mod_p = Barrett_Reduction::for_public_modulus(p);
11✔
335
      if(!is_bailie_psw_probable_prime(p, mod_p)) {
11✔
336
         throw Decoding_Error("ECC p parameter is not a prime");
2✔
337
      }
338

339
      auto mod_order = Barrett_Reduction::for_public_modulus(order);
9✔
340
      if(!is_bailie_psw_probable_prime(order, mod_order)) {
9✔
341
         throw Decoding_Error("Invalid ECC order parameter");
2✔
342
      }
343

344
      const size_t p_bytes = p.bytes();
7✔
345
      if(base_pt.size() != 1 + p_bytes && base_pt.size() != 1 + 2 * p_bytes) {
7✔
346
         throw Decoding_Error("Invalid ECC base point encoding");
×
347
      }
348

349
      auto [g_x, g_y] = [&]() {
10✔
350
         const uint8_t hdr = base_pt[0];
7✔
351

352
         if(hdr == 0x04 && base_pt.size() == 1 + 2 * p_bytes) {
7✔
353
            const BigInt x = BigInt::from_bytes(std::span{base_pt}.subspan(1, p_bytes));
7✔
354
            const BigInt y = BigInt::from_bytes(std::span{base_pt}.subspan(1 + p_bytes, p_bytes));
7✔
355

356
            if(x < p && y < p) {
7✔
357
               return std::make_pair(x, y);
7✔
358
            }
359
         } else if((hdr == 0x02 || hdr == 0x03) && base_pt.size() == 1 + p_bytes) {
7✔
360
            // TODO(Botan4) remove this branch; we won't support compressed points
361
            const BigInt x = BigInt::from_bytes(std::span{base_pt}.subspan(1, p_bytes));
×
362
            BigInt y = sqrt_modulo_prime(((x * x + a) * x + b) % p, p);
×
363

364
            if(x < p && y >= 0) {
×
365
               const bool y_mod_2 = (hdr & 0x01) == 1;
×
366
               if(y.get_bit(0) != y_mod_2) {
×
367
                  y = p - y;
×
368
               }
369

370
               return std::make_pair(x, y);
×
371
            }
372
         }
×
373

374
         throw Decoding_Error("Invalid ECC base point encoding");
×
375
      }();
7✔
376

377
      // TODO(Botan4) we can remove this check since we'll only accept pre-registered groups
378
      auto y2 = mod_p.square(g_y);
7✔
379
      auto x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) + b);
7✔
380
      if(y2 != x3_ax_b) {
7✔
381
         throw Decoding_Error("Invalid ECC base point");
1✔
382
      }
383

384
      /*
385
      * Create the group data without registering it in the global map.
386
      *
387
      * Applications that need persistent custom groups should register them
388
      * via the relevant EC_Group constructor
389
      */
390
      auto data = EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, OID(), source);
6✔
391
      return std::make_pair(data, true);
6✔
392
   } else if(next_obj_type == ASN1_Type::Null) {
634✔
393
      throw Decoding_Error("Decoding ImplicitCA ECC parameters is not supported");
×
394
   } else {
395
      throw Decoding_Error(
1✔
396
         fmt("Unexpected tag {} while decoding ECC domain params", asn1_tag_to_string(next_obj_type)));
2✔
397
   }
398
}
6,963✔
399

400
EC_Group::EC_Group() = default;
×
401

402
EC_Group::~EC_Group() = default;
114,865✔
403

404
EC_Group::EC_Group(const EC_Group&) = default;
56,179✔
405

406
EC_Group& EC_Group::operator=(const EC_Group&) = default;
×
407

408
// Internal constructor
409
EC_Group::EC_Group(std::shared_ptr<EC_Group_Data>&& data) : m_data(std::move(data)) {}
15,988✔
410

411
//static
412
bool EC_Group::supports_named_group(std::string_view name) {
15,406✔
413
   if(name.empty()) {
15,406✔
414
      return false;
415
   }
416

417
   // Is it one of the groups compiled into the library?
418
   if(EC_Group::known_named_groups().contains(std::string(name))) {
15,406✔
419
      return true;
420
   }
421

422
   // Is it a custom group registered by the application?
423
   if(auto oid = OID::from_name(name)) {
5✔
424
      try {
3✔
425
         if(ec_group_data().lookup(oid.value()) != nullptr) {
4✔
426
            return true;
1✔
427
         }
428
      } catch(Not_Implemented&) {
×
429
         // This would be thrown for example if the group is a known curve
430
         // but the relevant module that enables it is not compiled in
431
      }
×
432
   }
1✔
433

434
   // Not known
435
   return false;
4✔
436
}
437

438
//static
439
bool EC_Group::supports_application_specific_group() {
26✔
440
#if defined(BOTAN_HAS_LEGACY_EC_POINT) || defined(BOTAN_HAS_PCURVES_GENERIC)
441
   return true;
26✔
442
#else
443
   return false;
444
#endif
445
}
446

447
//static
448
bool EC_Group::supports_application_specific_group_with_cofactor() {
5✔
449
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
450
   return true;
5✔
451
#else
452
   return false;
453
#endif
454
}
455

456
//static
457
EC_Group EC_Group::from_OID(const OID& oid) {
75✔
458
   auto data = ec_group_data().lookup(oid);
75✔
459

460
   if(!data) {
75✔
461
      throw Invalid_Argument(fmt("No EC_Group associated with OID '{}'", oid.to_string()));
4✔
462
   }
463

464
   return EC_Group(std::move(data));
73✔
465
}
73✔
466

467
//static
468
EC_Group EC_Group::from_name(std::string_view name) {
15,916✔
469
   std::shared_ptr<EC_Group_Data> data;
15,916✔
470

471
   if(auto oid = OID::from_name(name)) {
15,916✔
472
      data = ec_group_data().lookup(oid.value());
31,830✔
473
   }
×
474

475
   if(!data) {
15,915✔
476
      throw Invalid_Argument(fmt("Unknown EC_Group '{}'", name));
×
477
   }
478

479
   return EC_Group(std::move(data));
15,915✔
480
}
15,915✔
481

482
EC_Group::EC_Group(std::string_view str) {
56✔
483
   if(str.empty()) {
56✔
484
      return;  // no initialization / uninitialized
485
   }
486

487
   try {
56✔
488
      const OID oid = OID::from_string(str);
56✔
489
      if(oid.has_value()) {
56✔
490
         m_data = ec_group_data().lookup(oid);
56✔
491
      }
492
   } catch(...) {}
56✔
493

494
   if(m_data == nullptr) {
56✔
495
      if(str.size() > 30 && str.starts_with("-----BEGIN EC PARAMETERS-----")) {
×
496
         // OK try it as PEM ...
497
         const auto der = PEM_Code::decode_check_label(str, "EC PARAMETERS");
×
498

499
         auto data = DER_decode_EC_group(der, EC_Group_Source::ExternalSource);
×
500
         this->m_data = data.first;
×
501
         this->m_explicit_encoding = data.second;
×
502
      }
×
503
   }
504

505
   if(m_data == nullptr) {
56✔
506
      throw Invalid_Argument(fmt("Unknown ECC group '{}'", str));
×
507
   }
508
}
×
509

510
//static
511
EC_Group EC_Group::from_PEM(std::string_view pem) {
4✔
512
   const auto der = PEM_Code::decode_check_label(pem, "EC PARAMETERS");
4✔
513
   return EC_Group(der);
4✔
514
}
4✔
515

516
EC_Group::EC_Group(const BigInt& p,
6✔
517
                   const BigInt& a,
518
                   const BigInt& b,
519
                   const BigInt& base_x,
520
                   const BigInt& base_y,
521
                   const BigInt& order,
522
                   const BigInt& cofactor,
523
                   const OID& oid) {
6✔
524
   BOTAN_ARG_CHECK(a >= 0 && a < p, "EC_Group a is invalid");
12✔
525
   BOTAN_ARG_CHECK(b > 0 && b < p, "EC_Group b is invalid");
12✔
526
   BOTAN_ARG_CHECK(base_x >= 0 && base_x < p, "EC_Group base_x is invalid");
12✔
527
   BOTAN_ARG_CHECK(base_y >= 0 && base_y < p, "EC_Group base_y is invalid");
12✔
528

529
   auto mod_p = Barrett_Reduction::for_public_modulus(p);
6✔
530
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(p, mod_p), "EC_Group p is not prime");
6✔
531

532
   auto mod_order = Barrett_Reduction::for_public_modulus(order);
6✔
533
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(order, mod_order), "EC_Group order is not prime");
6✔
534

535
   // Check that 4*a^3 + 27*b^2 != 0
536
   const auto discriminant = mod_p.reduce(mod_p.multiply(BigInt::from_s32(4), mod_p.cube(a)) +
12✔
537
                                          mod_p.multiply(BigInt::from_s32(27), mod_p.square(b)));
20✔
538
   BOTAN_ARG_CHECK(discriminant != 0, "EC_Group discriminant is invalid");
6✔
539

540
   // Check that the generator (base_x,base_y) is on the curve; y^2 = x^3 + a*x + b
541
   auto y2 = mod_p.square(base_y);
6✔
542
   auto x3_ax_b = mod_p.reduce(mod_p.cube(base_x) + mod_p.multiply(a, base_x) + b);
6✔
543
   BOTAN_ARG_CHECK(y2 == x3_ax_b, "EC_Group generator is not on the curve");
6✔
544

545
   if(oid.has_value()) {
4✔
546
      m_data = ec_group_data().lookup_or_create(
4✔
547
         p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
2✔
548
   } else {
549
      m_data = ec_group_data().lookup_or_create_without_oid(
4✔
550
         p, a, b, base_x, base_y, order, cofactor, EC_Group_Source::ExternalSource);
2✔
551
   }
552
}
22✔
553

554
EC_Group::EC_Group(const OID& oid,
51✔
555
                   const BigInt& p,
556
                   const BigInt& a,
557
                   const BigInt& b,
558
                   const BigInt& base_x,
559
                   const BigInt& base_y,
560
                   const BigInt& order) {
51✔
561
   BOTAN_ARG_CHECK(oid.has_value(), "An OID is required for creating an EC_Group");
51✔
562

563
   // TODO(Botan4) remove this and require 192 bits minimum
564
#if defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
565
   constexpr size_t p_bits_lower_bound = 192;
566
#else
567
   constexpr size_t p_bits_lower_bound = 128;
51✔
568
#endif
569

570
   BOTAN_ARG_CHECK(p.bits() >= p_bits_lower_bound, "EC_Group p too small");
51✔
571
   BOTAN_ARG_CHECK(p.bits() <= 521, "EC_Group p too large");
51✔
572

573
   if(p.bits() == 521) {
51✔
574
      const auto p521 = BigInt::power_of_2(521) - 1;
2✔
575
      BOTAN_ARG_CHECK(p == p521, "EC_Group with p of 521 bits must be 2**521-1");
1✔
576
   } else if(p.bits() == 239) {
51✔
577
      const auto x962_p239 = []() {
18✔
578
         BigInt p239;
3✔
579
         for(size_t i = 0; i != 239; ++i) {
720✔
580
            if(i < 47 || ((i >= 94) && (i != 143))) {
717✔
581
               p239.set_bit(i);
717✔
582
            }
583
         }
584
         return p239;
3✔
585
      }();
3✔
586

587
      BOTAN_ARG_CHECK(p == x962_p239, "EC_Group with p of 239 bits must be the X9.62 prime");
3✔
588
   } else {
3✔
589
      BOTAN_ARG_CHECK(p.bits() % 32 == 0, "EC_Group p must be a multiple of 32 bits");
47✔
590
   }
591

592
   BOTAN_ARG_CHECK(p % 4 == 3, "EC_Group p must be congruent to 3 modulo 4");
48✔
593

594
   BOTAN_ARG_CHECK(a >= 0 && a < p, "EC_Group a is invalid");
92✔
595
   BOTAN_ARG_CHECK(b > 0 && b < p, "EC_Group b is invalid");
92✔
596
   BOTAN_ARG_CHECK(base_x >= 0 && base_x < p, "EC_Group base_x is invalid");
92✔
597
   BOTAN_ARG_CHECK(base_y >= 0 && base_y < p, "EC_Group base_y is invalid");
92✔
598
   BOTAN_ARG_CHECK(p.bits() == order.bits(), "EC_Group p and order must have the same number of bits");
46✔
599

600
   auto mod_p = Barrett_Reduction::for_public_modulus(p);
43✔
601
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(p, mod_p), "EC_Group p is not prime");
43✔
602

603
   auto mod_order = Barrett_Reduction::for_public_modulus(order);
43✔
604
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(order, mod_order), "EC_Group order is not prime");
43✔
605

606
   // This catches someone "ignoring" a cofactor and just trying to
607
   // provide the subgroup order
608
   BOTAN_ARG_CHECK((p - order).abs().bits() <= (p.bits() / 2) + 1, "Hasse bound invalid");
43✔
609

610
   // Check that 4*a^3 + 27*b^2 != 0
611
   const auto discriminant = mod_p.reduce(mod_p.multiply(BigInt::from_s32(4), mod_p.cube(a)) +
86✔
612
                                          mod_p.multiply(BigInt::from_s32(27), mod_p.square(b)));
130✔
613
   BOTAN_ARG_CHECK(discriminant != 0, "EC_Group discriminant is invalid");
43✔
614

615
   // Check that the generator (base_x,base_y) is on the curve; y^2 = x^3 + a*x + b
616
   auto y2 = mod_p.square(base_y);
43✔
617
   auto x3_ax_b = mod_p.reduce(mod_p.cube(base_x) + mod_p.multiply(a, base_x) + b);
43✔
618
   BOTAN_ARG_CHECK(y2 == x3_ax_b, "EC_Group generator is not on the curve");
43✔
619

620
   const BigInt cofactor(1);
42✔
621

622
   m_data =
42✔
623
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
42✔
624
}
139✔
625

626
EC_Group::EC_Group(std::span<const uint8_t> der) {
6,963✔
627
   auto data = DER_decode_EC_group(der, EC_Group_Source::ExternalSource);
6,963✔
628
   m_data = data.first;
6,874✔
629
   m_explicit_encoding = data.second;
6,874✔
630
}
6,963✔
631

632
// static
633
bool EC_Group::unregister(const OID& oid) {
14✔
634
   return ec_group_data().unregister(oid);
14✔
635
}
636

637
const EC_Group_Data& EC_Group::data() const {
139,344✔
638
   if(m_data == nullptr) {
139,344✔
639
      throw Invalid_State("EC_Group uninitialized");
×
640
   }
641
   return *m_data;
139,344✔
642
}
643

644
size_t EC_Group::get_p_bits() const {
2,765✔
645
   return data().p_bits();
2,765✔
646
}
647

648
size_t EC_Group::get_p_bytes() const {
14,444✔
649
   return data().p_bytes();
14,444✔
650
}
651

652
size_t EC_Group::get_order_bits() const {
2,838✔
653
   return data().order_bits();
2,838✔
654
}
655

656
size_t EC_Group::get_order_bytes() const {
36,373✔
657
   return data().order_bytes();
36,373✔
658
}
659

660
const BigInt& EC_Group::get_p() const {
27,964✔
661
   return data().p();
27,964✔
662
}
663

664
const BigInt& EC_Group::get_a() const {
547✔
665
   return data().a();
547✔
666
}
667

668
const BigInt& EC_Group::get_b() const {
435✔
669
   return data().b();
435✔
670
}
671

672
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
673
const EC_Point& EC_Group::get_base_point() const {
649✔
674
   return data().base_point();
649✔
675
}
676

677
const EC_Point& EC_Group::generator() const {
×
678
   return data().base_point();
×
679
}
680

681
bool EC_Group::verify_public_element(const EC_Point& point) const {
×
682
   //check that public point is not at infinity
683
   if(point.is_zero()) {
×
684
      return false;
685
   }
686

687
   //check that public point is on the curve
688
   if(point.on_the_curve() == false) {
×
689
      return false;
690
   }
691

692
   //check that public point has order q
693
   if((point * get_order()).is_zero() == false) {
×
694
      return false;
695
   }
696

697
   if(has_cofactor()) {
×
698
      if((point * get_cofactor()).is_zero()) {
×
699
         return false;
700
      }
701
   }
702

703
   return true;
704
}
705

706
#endif
707

708
const BigInt& EC_Group::get_order() const {
7,931✔
709
   return data().order();
7,931✔
710
}
711

712
const BigInt& EC_Group::get_g_x() const {
1,758✔
713
   return data().g_x();
1,758✔
714
}
715

716
const BigInt& EC_Group::get_g_y() const {
1,758✔
717
   return data().g_y();
1,758✔
718
}
719

720
const BigInt& EC_Group::get_cofactor() const {
136✔
721
   return data().cofactor();
136✔
722
}
723

724
bool EC_Group::has_cofactor() const {
11,993✔
725
   return data().has_cofactor();
11,993✔
726
}
727

728
const OID& EC_Group::get_curve_oid() const {
26,742✔
729
   return data().oid();
26,742✔
730
}
731

732
EC_Group_Source EC_Group::source() const {
190✔
733
   return data().source();
190✔
734
}
735

736
EC_Group_Engine EC_Group::engine() const {
2✔
737
   return data().engine();
2✔
738
}
739

740
std::vector<uint8_t> EC_Group::DER_encode() const {
2,819✔
741
   const auto& der_named_curve = data().der_named_curve();
2,819✔
742
   // TODO(Botan4) this can be removed because an OID will always be defined
743
   if(der_named_curve.empty()) {
2,819✔
744
      throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
745
   }
746

747
   return der_named_curve;
2,819✔
748
}
749

750
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
2,814✔
751
   if(form == EC_Group_Encoding::Explicit) {
2,814✔
752
      std::vector<uint8_t> output;
30✔
753
      DER_Encoder der(output);
30✔
754
      const size_t ecpVers1 = 1;
30✔
755
      const OID curve_type("1.2.840.10045.1.1");  // prime field
30✔
756

757
      const size_t p_bytes = get_p_bytes();
30✔
758

759
      const auto generator = EC_AffinePoint::generator(*this).serialize_uncompressed();
30✔
760

761
      der.start_sequence()
30✔
762
         .encode(ecpVers1)
30✔
763
         .start_sequence()
30✔
764
         .encode(curve_type)
30✔
765
         .encode(get_p())
30✔
766
         .end_cons()
30✔
767
         .start_sequence()
30✔
768
         .encode(get_a().serialize(p_bytes), ASN1_Type::OctetString)
30✔
769
         .encode(get_b().serialize(p_bytes), ASN1_Type::OctetString)
60✔
770
         .end_cons()
30✔
771
         .encode(generator, ASN1_Type::OctetString)
30✔
772
         .encode(get_order())
30✔
773
         .encode(get_cofactor())
30✔
774
         .end_cons();
30✔
775
      return output;
30✔
776
   } else if(form == EC_Group_Encoding::NamedCurve) {
2,814✔
777
      return this->DER_encode();
2,784✔
778
   } else if(form == EC_Group_Encoding::ImplicitCA) {
×
779
      return {0x00, 0x05};
×
780
   } else {
781
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
782
   }
783
}
784

785
std::string EC_Group::PEM_encode(EC_Group_Encoding form) const {
3✔
786
   const std::vector<uint8_t> der = DER_encode(form);
3✔
787
   return PEM_Code::encode(der, "EC PARAMETERS");
3✔
788
}
3✔
789

790
bool EC_Group::operator==(const EC_Group& other) const {
61✔
791
   if(m_data == other.m_data) {
61✔
792
      return true;  // same shared rep
793
   }
794

795
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
9✔
796
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
9✔
797
           get_cofactor() == other.get_cofactor());
4✔
798
}
799

800
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
190✔
801
   const bool is_builtin = source() == EC_Group_Source::Builtin;
190✔
802

803
   if(is_builtin && !strong) {
190✔
804
      return true;
805
   }
806

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

811
   const BigInt& p = get_p();
29✔
812
   const BigInt& a = get_a();
29✔
813
   const BigInt& b = get_b();
29✔
814
   const BigInt& order = get_order();
29✔
815

816
   if(p <= 3 || order <= 0) {
58✔
817
      return false;
×
818
   }
819
   if(a < 0 || a >= p) {
58✔
820
      return false;
×
821
   }
822
   if(b <= 0 || b >= p) {
58✔
823
      return false;
×
824
   }
825

826
   const size_t test_prob = 128;
29✔
827
   const bool is_randomly_generated = is_builtin;
29✔
828

829
   //check if field modulus is prime
830
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
29✔
831
      return false;
832
   }
833

834
   //check if order is prime
835
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
29✔
836
      return false;
837
   }
838

839
   //compute the discriminant: 4*a^3 + 27*b^2 which must be nonzero
840
   auto mod_p = Barrett_Reduction::for_public_modulus(p);
29✔
841

842
   const BigInt discriminant = mod_p.reduce(mod_p.multiply(BigInt::from_s32(4), mod_p.cube(a)) +
58✔
843
                                            mod_p.multiply(BigInt::from_s32(27), mod_p.square(b)));
87✔
844

845
   if(discriminant == 0) {
29✔
846
      return false;
847
   }
848

849
   //check for valid cofactor
850
   if(get_cofactor() < 1) {
29✔
851
      return false;
852
   }
853

854
   // Check that the generator (g_x, g_y) is on the curve: y^2 == x^3 + a*x + b
855
   const BigInt& g_x = get_g_x();
29✔
856
   const BigInt& g_y = get_g_y();
29✔
857
   const BigInt y2 = mod_p.square(g_y);
29✔
858
   const BigInt x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) + b);
29✔
859
   if(y2 != x3_ax_b) {
29✔
860
      return false;
861
   }
862

863
   // Check that the generator has the claimed order: [order]G == identity,
864
   auto g_pt = EC_AffinePoint::from_bigint_xy(*this, get_g_x(), get_g_y());
29✔
865
   if(!g_pt) {
29✔
866
      return false;
867
   }
868
   const auto neg_one = EC_Scalar::one(*this).negate();
29✔
869
   const auto n_minus_one_g = EC_AffinePoint::g_mul(neg_one, rng);
29✔
870
   if(n_minus_one_g != g_pt->negate()) {
58✔
871
      return false;
872
   }
873

874
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
875
   // Reject if [cofactor]G is the identity. pcurves does not support cofactor != 1
876
   // so this can only matter when the legacy backend is in use.
877
   if(has_cofactor()) {
29✔
878
      const EC_Point& base_point = get_base_point();
×
879
      if((base_point * get_cofactor()).is_zero()) {
×
880
         return false;
881
      }
882
   }
883
#endif
884

885
   // check the Hasse bound (roughly)
886
   if((p - get_cofactor() * order).abs().bits() > (p.bits() / 2) + 1) {
29✔
887
      return false;
888
   }
889

890
   return true;
891
}
87✔
892

893
EC_Group::Mul2Table::Mul2Table(EC_Group::Mul2Table&& other) noexcept = default;
×
894

895
EC_Group::Mul2Table& EC_Group::Mul2Table::operator=(EC_Group::Mul2Table&& other) noexcept = default;
×
896

897
EC_Group::Mul2Table::Mul2Table(const EC_AffinePoint& h) : m_tbl(h._group()->make_mul2_table(h._inner())) {}
13,606✔
898

899
EC_Group::Mul2Table::~Mul2Table() = default;
13,606✔
900

901
std::optional<EC_AffinePoint> EC_Group::Mul2Table::mul2_vartime(const EC_Scalar& x, const EC_Scalar& y) const {
2,434✔
902
   auto pt = m_tbl->mul2_vartime(x._inner(), y._inner());
2,434✔
903
   if(pt) {
2,434✔
904
      return EC_AffinePoint::_from_inner(std::move(pt));
4,868✔
905
   } else {
906
      return {};
×
907
   }
908
}
2,434✔
909

910
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
33,254✔
911
                                                      const EC_Scalar& x,
912
                                                      const EC_Scalar& y) const {
913
   return m_tbl->mul2_vartime_x_mod_order_eq(v._inner(), x._inner(), y._inner());
33,254✔
914
}
915

916
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
32,857✔
917
                                                      const EC_Scalar& c,
918
                                                      const EC_Scalar& x,
919
                                                      const EC_Scalar& y) const {
920
   return this->mul2_vartime_x_mod_order_eq(v, c * x, c * y);
32,857✔
921
}
922

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