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

randombit / botan / 16395825001

19 Jul 2025 11:30PM UTC coverage: 90.635% (-0.07%) from 90.708%
16395825001

push

github

web-flow
Merge pull request #4998 from randombit/jack/fix-clang-tidy-readability-isolate-declaration

Enable and fix clang-tidy warning readability-isolate-declaration

99940 of 110266 relevant lines covered (90.64%)

12341110.13 hits per line

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

87.05
/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,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) {
25,120✔
39
         lock_guard_type<mutex_type> lock(m_mutex);
25,120✔
40

41
         for(auto i : m_registered_curves) {
163,379✔
42
            if(i->oid() == oid) {
162,178✔
43
               return i;
23,919✔
44
            }
45
         }
162,178✔
46

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

50
         if(data) {
1,201✔
51
            m_registered_curves.push_back(data);
1,141✔
52
            return data;
1,141✔
53
         }
54

55
         // Nope, unknown curve
56
         return std::shared_ptr<EC_Group_Data>();
60✔
57
      }
26,321✔
58

59
      std::shared_ptr<EC_Group_Data> lookup_or_create(const BigInt& p,
43✔
60
                                                      const BigInt& a,
61
                                                      const BigInt& b,
62
                                                      const BigInt& g_x,
63
                                                      const BigInt& g_y,
64
                                                      const BigInt& order,
65
                                                      const BigInt& cofactor,
66
                                                      const OID& oid,
67
                                                      EC_Group_Source source) {
68
         BOTAN_ASSERT_NOMSG(oid.has_value());
43✔
69

70
         lock_guard_type<mutex_type> lock(m_mutex);
43✔
71

72
         for(auto i : m_registered_curves) {
838✔
73
            if(i->oid() == oid) {
828✔
74
               /*
75
               * If both OID and params are the same then we are done, just return
76
               * the already registered curve obj.
77
               *
78
               * First verify that the params match, to catch an application
79
               * that is attempting to register a EC_Group under the same OID as
80
               * another group currently in use
81
               */
82
               if(!i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
33✔
83
                  throw Invalid_Argument("Attempting to register a curve using OID " + oid.to_string() +
×
84
                                         " but a distinct curve is already registered using that OID");
×
85
               }
86

87
               return i;
33✔
88
            }
89

90
            /*
91
            * If the same curve was previously created without an OID but is now
92
            * being registered again using an OID, save that OID.
93
            *
94
            * TODO(Botan4) remove this block; this situation won't be possible since
95
            * we will require all groups to have an OID
96
            */
97
            if(i->oid().empty() && i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
795✔
98
               i->set_oid(oid);
×
99
               return i;
×
100
            }
101
         }
828✔
102

103
         /*
104
         * Not found in current list, so we need to create a new entry
105
         */
106
         auto new_group = [&] {
×
107
            if(auto g = EC_Group::EC_group_info(oid); g != nullptr) {
10✔
108
               /*
109
               * This turned out to be the OID of one of the builtin groups. Verify
110
               * that all of the provided parameters match that builtin group.
111
               */
112
               BOTAN_ARG_CHECK(g->params_match(p, a, b, g_x, g_y, order, cofactor),
×
113
                               "Attempting to register an EC group under OID of hardcoded group");
114

115
               return g;
×
116
            } else {
117
               /*
118
               * This path is taken for an application registering a new EC_Group with an OID specified
119
               */
120
               return EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, oid, source);
10✔
121
            }
10✔
122
         }();
10✔
123

124
         m_registered_curves.push_back(new_group);
10✔
125
         return new_group;
10✔
126
      }
53✔
127

128
      std::shared_ptr<EC_Group_Data> lookup_or_create_without_oid(const BigInt& p,
60✔
129
                                                                  const BigInt& a,
130
                                                                  const BigInt& b,
131
                                                                  const BigInt& g_x,
132
                                                                  const BigInt& g_y,
133
                                                                  const BigInt& order,
134
                                                                  const BigInt& cofactor,
135
                                                                  EC_Group_Source source) {
136
         lock_guard_type<mutex_type> lock(m_mutex);
60✔
137

138
         for(auto i : m_registered_curves) {
739✔
139
            if(i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
731✔
140
               return i;
52✔
141
            }
142
         }
731✔
143

144
         // Try to use the order as a hint to look up the group id
145
         const OID oid_from_order = EC_Group::EC_group_identity_from_order(order);
8✔
146
         if(oid_from_order.has_value()) {
8✔
147
            auto new_group = EC_Group::EC_group_info(oid_from_order);
6✔
148

149
            // Have to check all params in the (unlikely/malicious) event of an order collision
150
            if(new_group && new_group->params_match(p, a, b, g_x, g_y, order, cofactor)) {
6✔
151
               m_registered_curves.push_back(new_group);
5✔
152
               return new_group;
5✔
153
            }
154
         }
6✔
155

156
         /*
157
         * At this point we have failed to identify the group; it is not any of
158
         * the builtin values, nor is it a group that the user had previously
159
         * registered explicitly. We create the group data without an OID.
160
         *
161
         * TODO(Botan4) remove this; throw an exception instead
162
         */
163
         auto new_group = EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, OID(), source);
3✔
164
         m_registered_curves.push_back(new_group);
3✔
165
         return new_group;
3✔
166
      }
71✔
167

168
   private:
169
      mutex_type m_mutex;
170
      // TODO(Botan4): Once OID is required we could make this into a map
171
      std::vector<std::shared_ptr<EC_Group_Data>> m_registered_curves;
172
};
173

174
//static
175
EC_Group_Data_Map& EC_Group::ec_group_data() {
26,993✔
176
   /*
177
   * This exists purely to ensure the allocator is constructed before g_ec_data,
178
   * which ensures that its destructor runs after ~g_ec_data is complete.
179
   */
180

181
   static Allocator_Initializer g_init_allocator;
26,993✔
182
   static EC_Group_Data_Map g_ec_data;
26,993✔
183
   return g_ec_data;
26,993✔
184
}
185

186
//static
187
size_t EC_Group::clear_registered_curve_data() {
1,770✔
188
   return ec_group_data().clear();
1,770✔
189
}
190

191
//static
192
std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(const char* p_str,
1,147✔
193
                                                            const char* a_str,
194
                                                            const char* b_str,
195
                                                            const char* g_x_str,
196
                                                            const char* g_y_str,
197
                                                            const char* order_str,
198
                                                            const OID& oid) {
199
   BOTAN_ARG_CHECK(oid.has_value(), "EC_Group::load_EC_group_info OID must be set");
1,147✔
200

201
   const BigInt p(p_str);
1,147✔
202
   const BigInt a(a_str);
1,147✔
203
   const BigInt b(b_str);
1,147✔
204
   const BigInt g_x(g_x_str);
1,147✔
205
   const BigInt g_y(g_y_str);
1,147✔
206
   const BigInt order(order_str);
1,147✔
207
   const BigInt cofactor(1);  // implicit
1,147✔
208

209
   return EC_Group_Data::create(p, a, b, g_x, g_y, order, cofactor, oid, EC_Group_Source::Builtin);
2,294✔
210
}
1,147✔
211

212
//static
213
std::pair<std::shared_ptr<EC_Group_Data>, bool> EC_Group::BER_decode_EC_group(std::span<const uint8_t> bits,
9,490✔
214
                                                                              EC_Group_Source source) {
215
   BER_Decoder ber(bits);
9,490✔
216

217
   auto next_obj_type = ber.peek_next_object().type_tag();
9,490✔
218

219
   if(next_obj_type == ASN1_Type::ObjectId) {
9,482✔
220
      OID oid;
9,146✔
221
      ber.decode(oid);
9,146✔
222

223
      auto data = ec_group_data().lookup(oid);
9,104✔
224
      if(!data) {
9,104✔
225
         throw Decoding_Error(fmt("Unknown namedCurve OID '{}'", oid.to_string()));
120✔
226
      }
227

228
      return std::make_pair(data, false);
9,044✔
229
   } else if(next_obj_type == ASN1_Type::Sequence) {
9,542✔
230
      BigInt p;
332✔
231
      BigInt a;
332✔
232
      BigInt b;
332✔
233
      BigInt order;
332✔
234
      BigInt cofactor;
332✔
235
      std::vector<uint8_t> base_pt;
332✔
236
      std::vector<uint8_t> seed;
332✔
237

238
      ber.start_sequence()
332✔
239
         .decode_and_check<size_t>(1, "Unknown ECC param version code")
641✔
240
         .start_sequence()
310✔
241
         .decode_and_check(OID({1, 2, 840, 10045, 1, 1}), "Only prime ECC fields supported")
620✔
242
         .decode(p)
309✔
243
         .end_cons()
309✔
244
         .start_sequence()
583✔
245
         .decode_octet_string_bigint(a)
309✔
246
         .decode_octet_string_bigint(b)
308✔
247
         .decode_optional_string(seed, ASN1_Type::BitString, ASN1_Type::BitString)
306✔
248
         .end_cons()
306✔
249
         .decode(base_pt, ASN1_Type::OctetString)
303✔
250
         .decode(order)
296✔
251
         .decode(cofactor)
295✔
252
         .end_cons()
295✔
253
         .verify_end();
294✔
254

255
      if(p.bits() < 112 || p.bits() > 521 || p.is_negative()) {
294✔
256
         throw Decoding_Error("ECC p parameter is invalid size");
134✔
257
      }
258

259
      // TODO(Botan4) we can remove this check since we'll only accept pre-registered groups
260
      auto mod_p = Barrett_Reduction::for_public_modulus(p);
160✔
261
      if(!is_bailie_psw_probable_prime(p, mod_p)) {
160✔
262
         throw Decoding_Error("ECC p parameter is not a prime");
32✔
263
      }
264

265
      if(a.is_negative() || a >= p) {
256✔
266
         throw Decoding_Error("Invalid ECC a parameter");
9✔
267
      }
268

269
      if(b <= 0 || b >= p) {
238✔
270
         throw Decoding_Error("Invalid ECC b parameter");
55✔
271
      }
272

273
      if(order.is_negative() || order.is_zero() || order >= 2 * p) {
355✔
274
         throw Decoding_Error("Invalid ECC group order");
2✔
275
      }
276

277
      // TODO(Botan4) we can remove this check since we'll only accept pre-registered groups
278
      auto mod_order = Barrett_Reduction::for_public_modulus(order);
62✔
279
      if(!is_bailie_psw_probable_prime(order, mod_order)) {
62✔
280
         throw Decoding_Error("Invalid ECC order parameter");
×
281
      }
282

283
      // TODO(Botan4) Require cofactor == 1
284
      if(cofactor <= 0 || cofactor >= 16) {
122✔
285
         throw Decoding_Error("Invalid ECC cofactor parameter");
3✔
286
      }
287

288
      const size_t p_bytes = p.bytes();
59✔
289
      if(base_pt.size() != 1 + p_bytes && base_pt.size() != 1 + 2 * p_bytes) {
59✔
290
         throw Decoding_Error("Invalid ECC base point encoding");
×
291
      }
292

293
      auto [g_x, g_y] = [&]() {
62✔
294
         const uint8_t hdr = base_pt[0];
59✔
295

296
         if(hdr == 0x04 && base_pt.size() == 1 + 2 * p_bytes) {
59✔
297
            BigInt x = BigInt::decode(&base_pt[1], p_bytes);
58✔
298
            BigInt y = BigInt::decode(&base_pt[p_bytes + 1], p_bytes);
58✔
299

300
            if(x < p && y < p) {
58✔
301
               return std::make_pair(x, y);
58✔
302
            }
303
         } else if((hdr == 0x02 || hdr == 0x03) && base_pt.size() == 1 + p_bytes) {
59✔
304
            // TODO(Botan4) remove this branch; we won't support compressed points
305
            BigInt x = BigInt::decode(&base_pt[1], p_bytes);
×
306
            BigInt y = sqrt_modulo_prime(((x * x + a) * x + b) % p, p);
×
307

308
            if(x < p && y >= 0) {
×
309
               const bool y_mod_2 = (hdr & 0x01) == 1;
×
310
               if(y.get_bit(0) != y_mod_2) {
×
311
                  y = p - y;
×
312
               }
313

314
               return std::make_pair(x, y);
×
315
            }
316
         }
×
317

318
         throw Decoding_Error("Invalid ECC base point encoding");
1✔
319
      }();
59✔
320

321
      // TODO(Botan4) we can remove this check since we'll only accept pre-registered groups
322
      auto y2 = mod_p.square(g_y);
58✔
323
      auto x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) + b);
58✔
324
      if(y2 != x3_ax_b) {
58✔
325
         throw Decoding_Error("Invalid ECC base point");
×
326
      }
327

328
      auto data = ec_group_data().lookup_or_create_without_oid(p, a, b, g_x, g_y, order, cofactor, source);
58✔
329
      return std::make_pair(data, true);
58✔
330
   } else if(next_obj_type == ASN1_Type::Null) {
2,044✔
331
      throw Decoding_Error("Decoding ImplicitCA ECC parameters is not supported");
1✔
332
   } else {
333
      throw Decoding_Error(
3✔
334
         fmt("Unexpected tag {} while decoding ECC domain params", asn1_tag_to_string(next_obj_type)));
6✔
335
   }
336
}
9,490✔
337

338
EC_Group::EC_Group() = default;
×
339

340
EC_Group::~EC_Group() = default;
147,397✔
341

342
EC_Group::EC_Group(const EC_Group&) = default;
58,857✔
343

344
EC_Group& EC_Group::operator=(const EC_Group&) = default;
×
345

346
// Internal constructor
347
EC_Group::EC_Group(std::shared_ptr<EC_Group_Data>&& data) : m_data(std::move(data)) {}
15,967✔
348

349
//static
350
bool EC_Group::supports_named_group(std::string_view name) {
15,432✔
351
   return EC_Group::known_named_groups().contains(std::string(name));
15,432✔
352
}
353

354
//static
355
bool EC_Group::supports_application_specific_group() {
25✔
356
#if defined(BOTAN_HAS_LEGACY_EC_POINT) || defined(BOTAN_HAS_PCURVES_GENERIC)
357
   return true;
25✔
358
#else
359
   return false;
360
#endif
361
}
362

363
//static
364
bool EC_Group::supports_application_specific_group_with_cofactor() {
5✔
365
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
366
   return true;
5✔
367
#else
368
   return false;
369
#endif
370
}
371

372
//static
373
EC_Group EC_Group::from_OID(const OID& oid) {
73✔
374
   auto data = ec_group_data().lookup(oid);
73✔
375

376
   if(!data) {
73✔
377
      throw Invalid_Argument(fmt("No EC_Group associated with OID '{}'", oid.to_string()));
×
378
   }
379

380
   return EC_Group(std::move(data));
73✔
381
}
73✔
382

383
//static
384
EC_Group EC_Group::from_name(std::string_view name) {
15,895✔
385
   std::shared_ptr<EC_Group_Data> data;
15,895✔
386

387
   if(auto oid = OID::from_name(name)) {
15,895✔
388
      data = ec_group_data().lookup(oid.value());
31,788✔
389
   }
×
390

391
   if(!data) {
15,894✔
392
      throw Invalid_Argument(fmt("Unknown EC_Group '{}'", name));
×
393
   }
394

395
   return EC_Group(std::move(data));
15,894✔
396
}
15,894✔
397

398
EC_Group::EC_Group(std::string_view str) {
49✔
399
   if(str.empty()) {
49✔
400
      return;  // no initialization / uninitialized
401
   }
402

403
   try {
49✔
404
      const OID oid = OID::from_string(str);
49✔
405
      if(oid.has_value()) {
49✔
406
         m_data = ec_group_data().lookup(oid);
49✔
407
      }
408
   } catch(...) {}
49✔
409

410
   if(m_data == nullptr) {
49✔
411
      if(str.size() > 30 && str.starts_with("-----BEGIN EC PARAMETERS-----")) {
×
412
         // OK try it as PEM ...
413
         const auto ber = PEM_Code::decode_check_label(str, "EC PARAMETERS");
×
414

415
         auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
×
416
         this->m_data = data.first;
×
417
         this->m_explicit_encoding = data.second;
×
418
      }
×
419
   }
420

421
   if(m_data == nullptr) {
49✔
422
      throw Invalid_Argument(fmt("Unknown ECC group '{}'", str));
×
423
   }
424
}
×
425

426
//static
427
EC_Group EC_Group::from_PEM(std::string_view pem) {
4✔
428
   const auto ber = PEM_Code::decode_check_label(pem, "EC PARAMETERS");
4✔
429
   return EC_Group(ber);
4✔
430
}
4✔
431

432
EC_Group::EC_Group(const BigInt& p,
4✔
433
                   const BigInt& a,
434
                   const BigInt& b,
435
                   const BigInt& base_x,
436
                   const BigInt& base_y,
437
                   const BigInt& order,
438
                   const BigInt& cofactor,
439
                   const OID& oid) {
4✔
440
   if(oid.has_value()) {
4✔
441
      m_data = ec_group_data().lookup_or_create(
4✔
442
         p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
2✔
443
   } else {
444
      m_data = ec_group_data().lookup_or_create_without_oid(
4✔
445
         p, a, b, base_x, base_y, order, cofactor, EC_Group_Source::ExternalSource);
2✔
446
   }
447
}
4✔
448

449
EC_Group::EC_Group(const OID& oid,
50✔
450
                   const BigInt& p,
451
                   const BigInt& a,
452
                   const BigInt& b,
453
                   const BigInt& base_x,
454
                   const BigInt& base_y,
455
                   const BigInt& order) {
50✔
456
   BOTAN_ARG_CHECK(oid.has_value(), "An OID is required for creating an EC_Group");
50✔
457

458
   // TODO(Botan4) remove this and require 192 bits minimum
459
#if defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
460
   constexpr size_t p_bits_lower_bound = 192;
461
#else
462
   constexpr size_t p_bits_lower_bound = 128;
50✔
463
#endif
464

465
   BOTAN_ARG_CHECK(p.bits() >= p_bits_lower_bound, "EC_Group p too small");
50✔
466
   BOTAN_ARG_CHECK(p.bits() <= 521, "EC_Group p too large");
50✔
467

468
   if(p.bits() == 521) {
50✔
469
      const auto p521 = BigInt::power_of_2(521) - 1;
2✔
470
      BOTAN_ARG_CHECK(p == p521, "EC_Group with p of 521 bits must be 2**521-1");
1✔
471
   } else if(p.bits() == 239) {
50✔
472
      const auto x962_p239 = []() {
18✔
473
         BigInt p239;
3✔
474
         for(size_t i = 0; i != 239; ++i) {
720✔
475
            if(i < 47 || ((i >= 94) && (i != 143))) {
717✔
476
               p239.set_bit(i);
717✔
477
            }
478
         }
479
         return p239;
3✔
480
      }();
3✔
481

482
      BOTAN_ARG_CHECK(p == x962_p239, "EC_Group with p of 239 bits must be the X9.62 prime");
3✔
483
   } else {
3✔
484
      BOTAN_ARG_CHECK(p.bits() % 32 == 0, "EC_Group p must be a multiple of 32 bits");
46✔
485
   }
486

487
   BOTAN_ARG_CHECK(p % 4 == 3, "EC_Group p must be congruent to 3 modulo 4");
47✔
488

489
   BOTAN_ARG_CHECK(a >= 0 && a < p, "EC_Group a is invalid");
90✔
490
   BOTAN_ARG_CHECK(b > 0 && b < p, "EC_Group b is invalid");
90✔
491
   BOTAN_ARG_CHECK(base_x >= 0 && base_x < p, "EC_Group base_x is invalid");
90✔
492
   BOTAN_ARG_CHECK(base_y >= 0 && base_y < p, "EC_Group base_y is invalid");
90✔
493
   BOTAN_ARG_CHECK(p.bits() == order.bits(), "EC_Group p and order must have the same number of bits");
45✔
494

495
   auto mod_p = Barrett_Reduction::for_public_modulus(p);
42✔
496
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(p, mod_p), "EC_Group p is not prime");
42✔
497

498
   auto mod_order = Barrett_Reduction::for_public_modulus(order);
42✔
499
   BOTAN_ARG_CHECK(is_bailie_psw_probable_prime(order, mod_order), "EC_Group order is not prime");
42✔
500

501
   // This catches someone "ignoring" a cofactor and just trying to
502
   // provide the subgroup order
503
   BOTAN_ARG_CHECK((p - order).abs().bits() <= (p.bits() / 2) + 1, "Hasse bound invalid");
42✔
504

505
   // Check that 4*a^3 + 27*b^2 != 0
506
   const auto discriminant = mod_p.reduce(mod_p.multiply(BigInt::from_s32(4), mod_p.cube(a)) +
84✔
507
                                          mod_p.multiply(BigInt::from_s32(27), mod_p.square(b)));
127✔
508
   BOTAN_ARG_CHECK(discriminant != 0, "EC_Group discriminant is invalid");
42✔
509

510
   // Check that the generator (base_x,base_y) is on the curve; y^2 = x^3 + a*x + b
511
   auto y2 = mod_p.square(base_y);
42✔
512
   auto x3_ax_b = mod_p.reduce(mod_p.cube(base_x) + mod_p.multiply(a, base_x) + b);
42✔
513
   BOTAN_ARG_CHECK(y2 == x3_ax_b, "EC_Group generator is not on the curve");
42✔
514

515
   BigInt cofactor(1);
41✔
516

517
   m_data =
41✔
518
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
41✔
519
}
136✔
520

521
EC_Group::EC_Group(std::span<const uint8_t> ber) {
9,490✔
522
   auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
9,490✔
523
   m_data = data.first;
9,102✔
524
   m_explicit_encoding = data.second;
9,102✔
525
}
9,490✔
526

527
const EC_Group_Data& EC_Group::data() const {
136,811✔
528
   if(m_data == nullptr) {
136,811✔
529
      throw Invalid_State("EC_Group uninitialized");
×
530
   }
531
   return *m_data;
136,811✔
532
}
533

534
size_t EC_Group::get_p_bits() const {
1,609✔
535
   return data().p_bits();
1,609✔
536
}
537

538
size_t EC_Group::get_p_bytes() const {
13,648✔
539
   return data().p_bytes();
13,648✔
540
}
541

542
size_t EC_Group::get_order_bits() const {
2,739✔
543
   return data().order_bits();
2,739✔
544
}
545

546
size_t EC_Group::get_order_bytes() const {
37,762✔
547
   return data().order_bytes();
37,762✔
548
}
549

550
const BigInt& EC_Group::get_p() const {
26,580✔
551
   return data().p();
26,580✔
552
}
553

554
const BigInt& EC_Group::get_a() const {
466✔
555
   return data().a();
466✔
556
}
557

558
const BigInt& EC_Group::get_b() const {
354✔
559
   return data().b();
354✔
560
}
561

562
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
563
const EC_Point& EC_Group::get_base_point() const {
677✔
564
   return data().base_point();
677✔
565
}
566

567
const EC_Point& EC_Group::generator() const {
×
568
   return data().base_point();
×
569
}
570

571
bool EC_Group::verify_public_element(const EC_Point& point) const {
×
572
   //check that public point is not at infinity
573
   if(point.is_zero()) {
×
574
      return false;
575
   }
576

577
   //check that public point is on the curve
578
   if(point.on_the_curve() == false) {
×
579
      return false;
580
   }
581

582
   //check that public point has order q
583
   if((point * get_order()).is_zero() == false) {
×
584
      return false;
585
   }
586

587
   if(has_cofactor()) {
×
588
      if((point * get_cofactor()).is_zero()) {
×
589
         return false;
590
      }
591
   }
592

593
   return true;
594
}
595

596
#endif
597

598
const BigInt& EC_Group::get_order() const {
7,904✔
599
   return data().order();
7,904✔
600
}
601

602
const BigInt& EC_Group::get_g_x() const {
986✔
603
   return data().g_x();
986✔
604
}
605

606
const BigInt& EC_Group::get_g_y() const {
986✔
607
   return data().g_y();
986✔
608
}
609

610
const BigInt& EC_Group::get_cofactor() const {
158✔
611
   return data().cofactor();
158✔
612
}
613

614
bool EC_Group::has_cofactor() const {
11,861✔
615
   return data().has_cofactor();
11,861✔
616
}
617

618
const OID& EC_Group::get_curve_oid() const {
28,178✔
619
   return data().oid();
28,178✔
620
}
621

622
EC_Group_Source EC_Group::source() const {
134✔
623
   return data().source();
134✔
624
}
625

626
EC_Group_Engine EC_Group::engine() const {
2✔
627
   return data().engine();
2✔
628
}
629

630
std::vector<uint8_t> EC_Group::DER_encode() const {
2,767✔
631
   const auto& der_named_curve = data().der_named_curve();
2,767✔
632
   // TODO(Botan4) this can be removed because an OID will always be defined
633
   if(der_named_curve.empty()) {
2,767✔
634
      throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
635
   }
636

637
   return der_named_curve;
2,767✔
638
}
639

640
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
2,706✔
641
   if(form == EC_Group_Encoding::Explicit) {
2,706✔
642
      std::vector<uint8_t> output;
30✔
643
      DER_Encoder der(output);
30✔
644
      const size_t ecpVers1 = 1;
30✔
645
      const OID curve_type("1.2.840.10045.1.1");  // prime field
30✔
646

647
      const size_t p_bytes = get_p_bytes();
30✔
648

649
      const auto generator = EC_AffinePoint::generator(*this).serialize_uncompressed();
30✔
650

651
      der.start_sequence()
30✔
652
         .encode(ecpVers1)
30✔
653
         .start_sequence()
30✔
654
         .encode(curve_type)
30✔
655
         .encode(get_p())
30✔
656
         .end_cons()
30✔
657
         .start_sequence()
30✔
658
         .encode(get_a().serialize(p_bytes), ASN1_Type::OctetString)
30✔
659
         .encode(get_b().serialize(p_bytes), ASN1_Type::OctetString)
60✔
660
         .end_cons()
30✔
661
         .encode(generator, ASN1_Type::OctetString)
30✔
662
         .encode(get_order())
30✔
663
         .encode(get_cofactor())
30✔
664
         .end_cons();
30✔
665
      return output;
30✔
666
   } else if(form == EC_Group_Encoding::NamedCurve) {
2,706✔
667
      return this->DER_encode();
2,676✔
668
   } else if(form == EC_Group_Encoding::ImplicitCA) {
×
669
      return {0x00, 0x05};
×
670
   } else {
671
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
672
   }
673
}
674

675
std::string EC_Group::PEM_encode(EC_Group_Encoding form) const {
3✔
676
   const std::vector<uint8_t> der = DER_encode(form);
3✔
677
   return PEM_Code::encode(der, "EC PARAMETERS");
3✔
678
}
3✔
679

680
bool EC_Group::operator==(const EC_Group& other) const {
58✔
681
   if(m_data == other.m_data) {
58✔
682
      return true;  // same shared rep
683
   }
684

685
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
5✔
686
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
5✔
687
           get_cofactor() == other.get_cofactor());
2✔
688
}
689

690
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
134✔
691
   const bool is_builtin = source() == EC_Group_Source::Builtin;
134✔
692

693
   if(is_builtin && !strong) {
134✔
694
      return true;
695
   }
696

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

701
   const BigInt& p = get_p();
28✔
702
   const BigInt& a = get_a();
28✔
703
   const BigInt& b = get_b();
28✔
704
   const BigInt& order = get_order();
28✔
705

706
   if(p <= 3 || order <= 0) {
56✔
707
      return false;
×
708
   }
709
   if(a < 0 || a >= p) {
56✔
710
      return false;
×
711
   }
712
   if(b <= 0 || b >= p) {
56✔
713
      return false;
×
714
   }
715

716
   const size_t test_prob = 128;
28✔
717
   const bool is_randomly_generated = is_builtin;
28✔
718

719
   //check if field modulus is prime
720
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
28✔
721
      return false;
722
   }
723

724
   //check if order is prime
725
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
28✔
726
      return false;
727
   }
728

729
   //compute the discriminant: 4*a^3 + 27*b^2 which must be nonzero
730
   auto mod_p = Barrett_Reduction::for_public_modulus(p);
28✔
731

732
   const BigInt discriminant = mod_p.reduce(mod_p.multiply(BigInt::from_s32(4), mod_p.cube(a)) +
56✔
733
                                            mod_p.multiply(BigInt::from_s32(27), mod_p.square(b)));
84✔
734

735
   if(discriminant == 0) {
28✔
736
      return false;
737
   }
738

739
   //check for valid cofactor
740
   if(get_cofactor() < 1) {
28✔
741
      return false;
742
   }
743

744
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
745
   const EC_Point& base_point = get_base_point();
28✔
746
   //check if the base point is on the curve
747
   if(!base_point.on_the_curve()) {
28✔
748
      return false;
749
   }
750
   if((base_point * get_cofactor()).is_zero()) {
56✔
751
      return false;
752
   }
753
   //check if order of the base point is correct
754
   if(!(base_point * order).is_zero()) {
28✔
755
      return false;
756
   }
757
#endif
758

759
   // check the Hasse bound (roughly)
760
   if((p - get_cofactor() * order).abs().bits() > (p.bits() / 2) + 1) {
28✔
761
      return false;
762
   }
763

764
   return true;
765
}
56✔
766

767
EC_Group::Mul2Table::Mul2Table(EC_Group::Mul2Table&& other) noexcept = default;
×
768

769
EC_Group::Mul2Table& EC_Group::Mul2Table::operator=(EC_Group::Mul2Table&& other) noexcept = default;
×
770

771
EC_Group::Mul2Table::Mul2Table(const EC_AffinePoint& h) : m_tbl(h._group()->make_mul2_table(h._inner())) {}
14,949✔
772

773
EC_Group::Mul2Table::~Mul2Table() = default;
14,949✔
774

775
std::optional<EC_AffinePoint> EC_Group::Mul2Table::mul2_vartime(const EC_Scalar& x, const EC_Scalar& y) const {
2,122✔
776
   auto pt = m_tbl->mul2_vartime(x._inner(), y._inner());
2,122✔
777
   if(pt) {
2,122✔
778
      return EC_AffinePoint::_from_inner(std::move(pt));
4,244✔
779
   } else {
780
      return {};
×
781
   }
782
}
2,122✔
783

784
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
24,672✔
785
                                                      const EC_Scalar& x,
786
                                                      const EC_Scalar& y) const {
787
   return m_tbl->mul2_vartime_x_mod_order_eq(v._inner(), x._inner(), y._inner());
24,672✔
788
}
789

790
bool EC_Group::Mul2Table::mul2_vartime_x_mod_order_eq(const EC_Scalar& v,
24,503✔
791
                                                      const EC_Scalar& c,
792
                                                      const EC_Scalar& x,
793
                                                      const EC_Scalar& y) const {
794
   return this->mul2_vartime_x_mod_order_eq(v, c * x, c * y);
24,503✔
795
}
796

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