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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

91.69
/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 Jack Lloyd
6
* (C) 2018 Tobias Niemann
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include <botan/ec_group.h>
12

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

24
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
25
   #include <botan/internal/ec_h2c.h>
26
#endif
27

28
namespace Botan {
29

30
class EC_Group_Data final {
31
   public:
32
      EC_Group_Data(const BigInt& p,
1,303✔
33
                    const BigInt& a,
34
                    const BigInt& b,
35
                    const BigInt& g_x,
36
                    const BigInt& g_y,
37
                    const BigInt& order,
38
                    const BigInt& cofactor,
39
                    const OID& oid,
40
                    EC_Group_Source source) :
1,303✔
41
            m_curve(p, a, b),
1,303✔
42
            m_base_point(m_curve, g_x, g_y),
1,303✔
43
            m_g_x(g_x),
1,302✔
44
            m_g_y(g_y),
1,302✔
45
            m_order(order),
1,302✔
46
            m_cofactor(cofactor),
1,302✔
47
            m_mod_order(order),
1,302✔
48
            m_base_mult(m_base_point, m_mod_order),
1,302✔
49
            m_oid(oid),
1,302✔
50
            m_p_bits(p.bits()),
1,302✔
51
            m_order_bits(order.bits()),
1,302✔
52
            m_a_is_minus_3(a == p - 3),
2,604✔
53
            m_a_is_zero(a.is_zero()),
1,302✔
54
            m_source(source) {}
1,303✔
55

56
      bool params_match(const BigInt& p,
1,344✔
57
                        const BigInt& a,
58
                        const BigInt& b,
59
                        const BigInt& g_x,
60
                        const BigInt& g_y,
61
                        const BigInt& order,
62
                        const BigInt& cofactor) const {
63
         return (this->p() == p && this->a() == a && this->b() == b && this->order() == order &&
240✔
64
                 this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
1,556✔
65
      }
66

67
      bool params_match(const EC_Group_Data& other) const {
70✔
68
         return params_match(
280✔
69
            other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
70✔
70
      }
71

72
      void set_oid(const OID& oid) {
7✔
73
         BOTAN_STATE_CHECK(m_oid.empty());
7✔
74
         m_oid = oid;
7✔
75
      }
7✔
76

77
      const OID& oid() const { return m_oid; }
129,457✔
78

79
      const BigInt& p() const { return m_curve.get_p(); }
1,414✔
80

81
      const BigInt& a() const { return m_curve.get_a(); }
155✔
82

83
      const BigInt& b() const { return m_curve.get_b(); }
151✔
84

85
      const BigInt& order() const { return m_order; }
74✔
86

87
      const BigInt& cofactor() const { return m_cofactor; }
72✔
88

89
      const BigInt& g_x() const { return m_g_x; }
72✔
90

91
      const BigInt& g_y() const { return m_g_y; }
138✔
92

93
      size_t p_bits() const { return m_p_bits; }
1,112✔
94

95
      size_t p_bytes() const { return (m_p_bits + 7) / 8; }
1,083✔
96

97
      size_t order_bits() const { return m_order_bits; }
14,002✔
98

99
      size_t order_bytes() const { return (m_order_bits + 7) / 8; }
18,469✔
100

101
      const CurveGFp& curve() const { return m_curve; }
22,693✔
102

103
      const EC_Point& base_point() const { return m_base_point; }
10,949✔
104

105
      bool a_is_minus_3() const { return m_a_is_minus_3; }
27✔
106

107
      bool a_is_zero() const { return m_a_is_zero; }
27✔
108

109
      BigInt mod_order(const BigInt& x) const { return m_mod_order.reduce(x); }
26,372✔
110

111
      BigInt square_mod_order(const BigInt& x) const { return m_mod_order.square(x); }
888✔
112

113
      BigInt multiply_mod_order(const BigInt& x, const BigInt& y) const { return m_mod_order.multiply(x, y); }
25,329✔
114

115
      BigInt multiply_mod_order(const BigInt& x, const BigInt& y, const BigInt& z) const {
888✔
116
         return m_mod_order.multiply(m_mod_order.multiply(x, y), z);
1,776✔
117
      }
118

119
      BigInt inverse_mod_order(const BigInt& x) const { return inverse_mod(x, m_order); }
13,680✔
120

121
      EC_Point blinded_base_point_multiply(const BigInt& k, RandomNumberGenerator& rng, std::vector<BigInt>& ws) const {
2,900✔
122
         return m_base_mult.mul(k, rng, m_order, ws);
2,900✔
123
      }
124

125
      EC_Group_Source source() const { return m_source; }
106✔
126

127
   private:
128
      CurveGFp m_curve;
129
      EC_Point m_base_point;
130

131
      BigInt m_g_x;
132
      BigInt m_g_y;
133
      BigInt m_order;
134
      BigInt m_cofactor;
135
      Modular_Reducer m_mod_order;
136
      EC_Point_Base_Point_Precompute m_base_mult;
137
      OID m_oid;
138
      size_t m_p_bits;
139
      size_t m_order_bits;
140
      bool m_a_is_minus_3;
141
      bool m_a_is_zero;
142
      EC_Group_Source m_source;
143
};
144

145
class EC_Group_Data_Map final {
146
   public:
147
      EC_Group_Data_Map() = default;
456✔
148

149
      size_t clear() {
1,769✔
150
         lock_guard_type<mutex_type> lock(m_mutex);
1,769✔
151
         size_t count = m_registered_curves.size();
1,769✔
152
         m_registered_curves.clear();
1,769✔
153
         return count;
1,769✔
154
      }
1,769✔
155

156
      std::shared_ptr<EC_Group_Data> lookup(const OID& oid) {
17,258✔
157
         lock_guard_type<mutex_type> lock(m_mutex);
17,258✔
158

159
         for(auto i : m_registered_curves) {
129,612✔
160
            if(i->oid() == oid) {
128,270✔
161
               return i;
15,916✔
162
            }
163
         }
128,270✔
164

165
         // Not found, check hardcoded data
166
         std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid);
1,342✔
167

168
         if(data) {
1,342✔
169
            for(auto curve : m_registered_curves) {
2,459✔
170
               if(curve->oid().empty() == true && curve->params_match(*data)) {
1,187✔
171
                  curve->set_oid(oid);
×
172
                  return curve;
×
173
               }
174
            }
1,187✔
175

176
            m_registered_curves.push_back(data);
1,272✔
177
            return data;
1,272✔
178
         }
179

180
         // Nope, unknown curve
181
         return std::shared_ptr<EC_Group_Data>();
1,342✔
182
      }
17,258✔
183

184
      std::shared_ptr<EC_Group_Data> lookup_or_create(const BigInt& p,
82✔
185
                                                      const BigInt& a,
186
                                                      const BigInt& b,
187
                                                      const BigInt& g_x,
188
                                                      const BigInt& g_y,
189
                                                      const BigInt& order,
190
                                                      const BigInt& cofactor,
191
                                                      const OID& oid,
192
                                                      EC_Group_Source source) {
193
         lock_guard_type<mutex_type> lock(m_mutex);
82✔
194

195
         for(auto i : m_registered_curves) {
1,407✔
196
            /*
197
            * The params may be the same but you are trying to register under a
198
            * different OID than the one we are using, so using a different
199
            * group, since EC_Group's model assumes a single OID per group.
200
            */
201
            if(!oid.empty() && !i->oid().empty() && i->oid() != oid) {
1,386✔
202
               continue;
112✔
203
            }
204

205
            const bool same_oid = !oid.empty() && i->oid() == oid;
1,274✔
206
            const bool same_params = i->params_match(p, a, b, g_x, g_y, order, cofactor);
1,274✔
207

208
            /*
209
            * If the params and OID are the same then we are done, just return
210
            * the already registered curve obj.
211
            */
212
            if(same_params && same_oid) {
1,274✔
213
               return i;
1✔
214
            }
215

216
            /*
217
            * If same params and the new OID is empty, then that's ok too
218
            */
219
            if(same_params && oid.empty()) {
1,273✔
220
               return i;
60✔
221
            }
222

223
            /*
224
            * Check for someone trying to reuse an already in-use OID
225
            */
226
            if(same_oid && !same_params) {
1,213✔
227
               throw Invalid_Argument("Attempting to register a curve using OID " + oid.to_string() +
×
228
                                      " but a distinct curve is already registered using that OID");
×
229
            }
230

231
            /*
232
            * If the same curve was previously created without an OID but is now
233
            * being registered again using an OID, save that OID.
234
            */
235
            if(same_params && i->oid().empty() && !oid.empty()) {
1,213✔
236
               i->set_oid(oid);
×
237
               return i;
61✔
238
            }
239
         }
1,386✔
240

241
         /*
242
         Not found in current list, so we need to create a new entry
243

244
         If an OID is set, try to look up relative our static tables to detect a duplicate
245
         registration under an OID
246
         */
247

248
         std::shared_ptr<EC_Group_Data> new_group =
21✔
249
            std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, source);
20✔
250

251
         if(oid.has_value()) {
20✔
252
            std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid);
5✔
253
            if(data != nullptr && !new_group->params_match(*data)) {
5✔
254
               throw Invalid_Argument("Attempting to register an EC group under OID of hardcoded group");
1✔
255
            }
256
         } else {
5✔
257
            // Here try to use the order as a hint to look up the group id, to identify common groups
258
            const OID oid_from_store = EC_Group::EC_group_identity_from_order(order);
15✔
259
            if(oid_from_store.has_value()) {
15✔
260
               std::shared_ptr<EC_Group_Data> data = EC_Group::EC_group_info(oid_from_store);
9✔
261

262
               /*
263
               If EC_group_identity_from_order returned an OID then looking up that OID
264
               must always return a result.
265
               */
266
               BOTAN_ASSERT_NOMSG(data != nullptr);
9✔
267

268
               /*
269
               It is possible (if unlikely) that someone is registering another group
270
               that happens to have an order equal to that of a well known group -
271
               so verify all values before assigning the OID.
272
               */
273
               if(new_group->params_match(*data)) {
9✔
274
                  new_group->set_oid(oid_from_store);
7✔
275
               }
276
            }
9✔
277
         }
15✔
278

279
         m_registered_curves.push_back(new_group);
19✔
280
         return new_group;
19✔
281
      }
81✔
282

283
   private:
284
      mutex_type m_mutex;
285
      std::vector<std::shared_ptr<EC_Group_Data>> m_registered_curves;
286
};
287

288
//static
289
EC_Group_Data_Map& EC_Group::ec_group_data() {
19,109✔
290
   /*
291
   * This exists purely to ensure the allocator is constructed before g_ec_data,
292
   * which ensures that its destructor runs after ~g_ec_data is complete.
293
   */
294

295
   static Allocator_Initializer g_init_allocator;
19,109✔
296
   static EC_Group_Data_Map g_ec_data;
19,109✔
297
   return g_ec_data;
19,109✔
298
}
299

300
//static
301
size_t EC_Group::clear_registered_curve_data() {
1,769✔
302
   return ec_group_data().clear();
1,769✔
303
}
304

305
//static
306
std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(const char* p_str,
1,282✔
307
                                                            const char* a_str,
308
                                                            const char* b_str,
309
                                                            const char* g_x_str,
310
                                                            const char* g_y_str,
311
                                                            const char* order_str,
312
                                                            const OID& oid) {
313
   const BigInt p(p_str);
1,282✔
314
   const BigInt a(a_str);
1,282✔
315
   const BigInt b(b_str);
1,282✔
316
   const BigInt g_x(g_x_str);
1,282✔
317
   const BigInt g_y(g_y_str);
1,282✔
318
   const BigInt order(order_str);
1,282✔
319
   const BigInt cofactor(1);  // implicit
1,282✔
320

321
   return std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, EC_Group_Source::Builtin);
1,282✔
322
}
8,974✔
323

324
//static
325
std::shared_ptr<EC_Group_Data> EC_Group::BER_decode_EC_group(const uint8_t bits[], size_t len, EC_Group_Source source) {
3,735✔
326
   BER_Decoder ber(bits, len);
3,735✔
327
   BER_Object obj = ber.get_next_object();
3,735✔
328

329
   if(obj.type() == ASN1_Type::Null) [[unlikely]] {
3,727✔
330
      throw Decoding_Error("Cannot handle ImplicitCA ECC parameters");
2✔
331
   } else if(obj.type() == ASN1_Type::ObjectId) {
3,725✔
332
      OID dom_par_oid;
3,412✔
333
      BER_Decoder(bits, len).decode(dom_par_oid);
3,412✔
334
      return ec_group_data().lookup(dom_par_oid);
3,409✔
335
   } else if(obj.type() == ASN1_Type::Sequence) {
3,725✔
336
      BigInt p, a, b, order, cofactor;
311✔
337
      std::vector<uint8_t> base_pt;
311✔
338
      std::vector<uint8_t> seed;
311✔
339

340
      BER_Decoder(bits, len)
591✔
341
         .start_sequence()
620✔
342
         .decode_and_check<size_t>(1, "Unknown ECC param version code")
309✔
343
         .start_sequence()
596✔
344
         .decode_and_check(OID("1.2.840.10045.1.1"), "Only prime ECC fields supported")
574✔
345
         .decode(p)
282✔
346
         .end_cons()
282✔
347
         .start_sequence()
302✔
348
         .decode_octet_string_bigint(a)
282✔
349
         .decode_octet_string_bigint(b)
281✔
350
         .decode_optional_string(seed, ASN1_Type::BitString, ASN1_Type::BitString)
279✔
351
         .end_cons()
279✔
352
         .decode(base_pt, ASN1_Type::OctetString)
276✔
353
         .decode(order)
269✔
354
         .decode(cofactor)
268✔
355
         .end_cons()
268✔
356
         .verify_end();
267✔
357

358
      if(p.bits() < 64 || p.is_negative() || !is_bailie_psw_probable_prime(p)) {
267✔
359
         throw Decoding_Error("Invalid ECC p parameter");
162✔
360
      }
361

362
      if(a.is_negative() || a >= p) {
210✔
363
         throw Decoding_Error("Invalid ECC a parameter");
13✔
364
      }
365

366
      if(b <= 0 || b >= p) {
184✔
367
         throw Decoding_Error("Invalid ECC b parameter");
55✔
368
      }
369

370
      if(order <= 0 || !is_bailie_psw_probable_prime(order)) {
37✔
371
         throw Decoding_Error("Invalid ECC order parameter");
2✔
372
      }
373

374
      if(cofactor <= 0 || cofactor >= 16) {
68✔
375
         throw Decoding_Error("Invalid ECC cofactor parameter");
3✔
376
      }
377

378
      std::pair<BigInt, BigInt> base_xy = Botan::OS2ECP(base_pt.data(), base_pt.size(), p, a, b);
32✔
379

380
      return ec_group_data().lookup_or_create(p, a, b, base_xy.first, base_xy.second, order, cofactor, OID(), source);
32✔
381
   } else {
2,178✔
382
      throw Decoding_Error("Unexpected tag while decoding ECC domain params");
2✔
383
   }
384
}
3,735✔
385

386
EC_Group::EC_Group() = default;
2,365✔
387

388
EC_Group::~EC_Group() = default;
43,951✔
389

390
EC_Group::EC_Group(const OID& domain_oid) {
12,739✔
391
   this->m_data = ec_group_data().lookup(domain_oid);
12,739✔
392
   if(!this->m_data) {
12,739✔
393
      throw Invalid_Argument("Unknown EC_Group " + domain_oid.to_string());
×
394
   }
395
}
12,739✔
396

397
EC_Group::EC_Group(std::string_view str) {
1,116✔
398
   if(str.empty()) {
1,116✔
399
      return;  // no initialization / uninitialized
400
   }
401

402
   try {
1,116✔
403
      const OID oid = OID::from_string(str);
1,116✔
404
      if(oid.has_value()) {
1,110✔
405
         m_data = ec_group_data().lookup(oid);
1,110✔
406
      }
407
   } catch(...) {}
1,116✔
408

409
   if(m_data == nullptr) {
1,116✔
410
      if(str.size() > 30 && str.substr(0, 29) == "-----BEGIN EC PARAMETERS-----") {
6✔
411
         // OK try it as PEM ...
412
         secure_vector<uint8_t> ber = PEM_Code::decode_check_label(str, "EC PARAMETERS");
6✔
413
         this->m_data = BER_decode_EC_group(ber.data(), ber.size(), EC_Group_Source::ExternalSource);
6✔
414
      }
6✔
415
   }
416

417
   if(m_data == nullptr) {
1,116✔
418
      throw Invalid_Argument(fmt("Unknown ECC group '{}'", str));
×
419
   }
420
}
×
421

422
//static
423
EC_Group EC_Group::EC_Group_from_PEM(std::string_view pem) {
×
424
   const auto ber = PEM_Code::decode_check_label(pem, "EC PARAMETERS");
×
425
   return EC_Group(ber.data(), ber.size());
×
426
}
×
427

428
EC_Group::EC_Group(const BigInt& p,
50✔
429
                   const BigInt& a,
430
                   const BigInt& b,
431
                   const BigInt& base_x,
432
                   const BigInt& base_y,
433
                   const BigInt& order,
434
                   const BigInt& cofactor,
435
                   const OID& oid) {
50✔
436
   m_data =
50✔
437
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
50✔
438
}
50✔
439

440
EC_Group::EC_Group(const uint8_t ber[], size_t ber_len) {
3,729✔
441
   m_data = BER_decode_EC_group(ber, ber_len, EC_Group_Source::ExternalSource);
3,729✔
442
}
3,729✔
443

444
const EC_Group_Data& EC_Group::data() const {
201,003✔
445
   if(m_data == nullptr) {
201,003✔
446
      throw Invalid_State("EC_Group uninitialized");
70✔
447
   }
448
   return *m_data;
200,933✔
449
}
450

451
bool EC_Group::a_is_minus_3() const {
27✔
452
   return data().a_is_minus_3();
27✔
453
}
454

455
bool EC_Group::a_is_zero() const {
27✔
456
   return data().a_is_zero();
27✔
457
}
458

459
size_t EC_Group::get_p_bits() const {
1,112✔
460
   return data().p_bits();
1,112✔
461
}
462

463
size_t EC_Group::get_p_bytes() const {
1,083✔
464
   return data().p_bytes();
1,083✔
465
}
466

467
size_t EC_Group::get_order_bits() const {
14,002✔
468
   return data().order_bits();
14,002✔
469
}
470

471
size_t EC_Group::get_order_bytes() const {
18,469✔
472
   return data().order_bytes();
18,469✔
473
}
474

475
const BigInt& EC_Group::get_p() const {
732✔
476
   return data().p();
732✔
477
}
478

479
const BigInt& EC_Group::get_a() const {
726✔
480
   return data().a();
726✔
481
}
482

483
const BigInt& EC_Group::get_b() const {
672✔
484
   return data().b();
672✔
485
}
486

487
const EC_Point& EC_Group::get_base_point() const {
10,949✔
488
   return data().base_point();
10,949✔
489
}
490

491
const BigInt& EC_Group::get_order() const {
41,478✔
492
   return data().order();
41,478✔
493
}
494

495
const BigInt& EC_Group::get_g_x() const {
64✔
496
   return data().g_x();
64✔
497
}
498

499
const BigInt& EC_Group::get_g_y() const {
64✔
500
   return data().g_y();
64✔
501
}
502

503
const BigInt& EC_Group::get_cofactor() const {
1,225✔
504
   return data().cofactor();
1,225✔
505
}
506

507
BigInt EC_Group::mod_order(const BigInt& k) const {
26,372✔
508
   return data().mod_order(k);
26,372✔
509
}
510

511
BigInt EC_Group::square_mod_order(const BigInt& x) const {
888✔
512
   return data().square_mod_order(x);
888✔
513
}
514

515
BigInt EC_Group::multiply_mod_order(const BigInt& x, const BigInt& y) const {
25,329✔
516
   return data().multiply_mod_order(x, y);
25,329✔
517
}
518

519
BigInt EC_Group::multiply_mod_order(const BigInt& x, const BigInt& y, const BigInt& z) const {
888✔
520
   return data().multiply_mod_order(x, y, z);
888✔
521
}
522

523
BigInt EC_Group::inverse_mod_order(const BigInt& x) const {
13,680✔
524
   return data().inverse_mod_order(x);
13,680✔
525
}
526

527
const OID& EC_Group::get_curve_oid() const {
17,509✔
528
   return data().oid();
17,509✔
529
}
530

531
EC_Group_Source EC_Group::source() const {
106✔
532
   return data().source();
106✔
533
}
534

535
size_t EC_Group::point_size(EC_Point_Format format) const {
123✔
536
   // Hybrid and standard format are (x,y), compressed is y, +1 format byte
537
   if(format == EC_Point_Format::Compressed) {
123✔
538
      return (1 + get_p_bytes());
42✔
539
   } else {
540
      return (1 + 2 * get_p_bytes());
81✔
541
   }
542
}
543

544
EC_Point EC_Group::OS2ECP(const uint8_t bits[], size_t len) const {
10,017✔
545
   return Botan::OS2ECP(bits, len, data().curve());
10,017✔
546
}
547

548
EC_Point EC_Group::point(const BigInt& x, const BigInt& y) const {
12,630✔
549
   // TODO: randomize the representation?
550
   return EC_Point(data().curve(), x, y);
12,630✔
551
}
552

553
EC_Point EC_Group::point_multiply(const BigInt& x, const EC_Point& pt, const BigInt& y) const {
×
554
   EC_Point_Multi_Point_Precompute xy_mul(get_base_point(), pt);
×
555
   return xy_mul.multi_exp(x, y);
×
556
}
×
557

558
EC_Point EC_Group::blinded_base_point_multiply(const BigInt& k,
2,253✔
559
                                               RandomNumberGenerator& rng,
560
                                               std::vector<BigInt>& ws) const {
561
   return data().blinded_base_point_multiply(k, rng, ws);
2,253✔
562
}
563

564
BigInt EC_Group::blinded_base_point_multiply_x(const BigInt& k,
647✔
565
                                               RandomNumberGenerator& rng,
566
                                               std::vector<BigInt>& ws) const {
567
   const EC_Point pt = data().blinded_base_point_multiply(k, rng, ws);
647✔
568

569
   if(pt.is_zero()) {
1,294✔
570
      return BigInt::zero();
×
571
   }
572
   return pt.get_affine_x();
647✔
573
}
647✔
574

575
BigInt EC_Group::random_scalar(RandomNumberGenerator& rng) const {
1,104✔
576
   return BigInt::random_integer(rng, BigInt::one(), get_order());
3,312✔
577
}
578

579
EC_Point EC_Group::blinded_var_point_multiply(const EC_Point& point,
2,787✔
580
                                              const BigInt& k,
581
                                              RandomNumberGenerator& rng,
582
                                              std::vector<BigInt>& ws) const {
583
   EC_Point_Var_Point_Precompute mul(point, rng, ws);
2,787✔
584
   return mul.mul(k, rng, get_order(), ws);
5,574✔
585
}
2,787✔
586

587
EC_Point EC_Group::zero_point() const {
54✔
588
   return EC_Point(data().curve());
54✔
589
}
590

591
EC_Point EC_Group::hash_to_curve(std::string_view hash_fn,
30✔
592
                                 const uint8_t input[],
593
                                 size_t input_len,
594
                                 std::string_view domain,
595
                                 bool random_oracle) const {
596
   return this->hash_to_curve(
30✔
597
      hash_fn, input, input_len, reinterpret_cast<const uint8_t*>(domain.data()), domain.size(), random_oracle);
30✔
598
}
599

600
EC_Point EC_Group::hash_to_curve(std::string_view hash_fn,
36✔
601
                                 const uint8_t input[],
602
                                 size_t input_len,
603
                                 const uint8_t domain_sep[],
604
                                 size_t domain_sep_len,
605
                                 bool random_oracle) const {
606
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
607

608
   // Only have SSWU currently
609
   if(get_a().is_zero() || get_b().is_zero() || get_p() % 4 == 1) {
36✔
610
      throw Not_Implemented("EC_Group::hash_to_curve not available for this curve type");
×
611
   }
612

613
   return hash_to_curve_sswu(*this, hash_fn, input, input_len, domain_sep, domain_sep_len, random_oracle);
36✔
614

615
#else
616
   BOTAN_UNUSED(hash_fn, random_oracle, input, input_len, domain_sep, domain_sep_len);
617
   throw Not_Implemented("EC_Group::hash_to_curve functionality not available in this configuration");
618
#endif
619
}
620

621
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
769✔
622
   std::vector<uint8_t> output;
769✔
623

624
   DER_Encoder der(output);
769✔
625

626
   if(form == EC_Group_Encoding::Explicit) {
769✔
627
      const size_t ecpVers1 = 1;
7✔
628
      const OID curve_type("1.2.840.10045.1.1");  // prime field
7✔
629

630
      const size_t p_bytes = get_p_bytes();
7✔
631

632
      der.start_sequence()
7✔
633
         .encode(ecpVers1)
7✔
634
         .start_sequence()
7✔
635
         .encode(curve_type)
7✔
636
         .encode(get_p())
7✔
637
         .end_cons()
7✔
638
         .start_sequence()
7✔
639
         .encode(BigInt::encode_1363(get_a(), p_bytes), ASN1_Type::OctetString)
7✔
640
         .encode(BigInt::encode_1363(get_b(), p_bytes), ASN1_Type::OctetString)
14✔
641
         .end_cons()
7✔
642
         .encode(get_base_point().encode(EC_Point_Format::Uncompressed), ASN1_Type::OctetString)
14✔
643
         .encode(get_order())
7✔
644
         .encode(get_cofactor())
7✔
645
         .end_cons();
7✔
646
   } else if(form == EC_Group_Encoding::NamedCurve) {
769✔
647
      const OID oid = get_curve_oid();
762✔
648
      if(oid.empty()) {
762✔
649
         throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
650
      }
651
      der.encode(oid);
762✔
652
   } else if(form == EC_Group_Encoding::ImplicitCA) {
762✔
653
      der.encode_null();
×
654
   } else {
655
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
656
   }
657

658
   return output;
769✔
659
}
769✔
660

661
std::string EC_Group::PEM_encode() const {
1✔
662
   const std::vector<uint8_t> der = DER_encode(EC_Group_Encoding::Explicit);
1✔
663
   return PEM_Code::encode(der, "EC PARAMETERS");
1✔
664
}
1✔
665

666
bool EC_Group::operator==(const EC_Group& other) const {
54✔
667
   if(m_data == other.m_data) {
54✔
668
      return true;  // same shared rep
669
   }
670

671
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
×
672
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
×
673
           get_cofactor() == other.get_cofactor());
×
674
}
675

676
bool EC_Group::verify_public_element(const EC_Point& point) const {
79✔
677
   //check that public point is not at infinity
678
   if(point.is_zero()) {
120✔
679
      return false;
680
   }
681

682
   //check that public point is on the curve
683
   if(point.on_the_curve() == false) {
79✔
684
      return false;
685
   }
686

687
   //check that public point has order q
688
   if((point * get_order()).is_zero() == false) {
40✔
689
      return false;
690
   }
691

692
   if(get_cofactor() > 1) {
40✔
693
      if((point * get_cofactor()).is_zero()) {
×
694
         return false;
×
695
      }
696
   }
697

698
   return true;
699
}
700

701
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
106✔
702
   const bool is_builtin = source() == EC_Group_Source::Builtin;
106✔
703

704
   if(is_builtin && !strong) {
106✔
705
      return true;
706
   }
707

708
   const BigInt& p = get_p();
27✔
709
   const BigInt& a = get_a();
27✔
710
   const BigInt& b = get_b();
27✔
711
   const BigInt& order = get_order();
27✔
712
   const EC_Point& base_point = get_base_point();
27✔
713

714
   if(p <= 3 || order <= 0) {
54✔
715
      return false;
×
716
   }
717
   if(a < 0 || a >= p) {
54✔
718
      return false;
×
719
   }
720
   if(b <= 0 || b >= p) {
54✔
721
      return false;
×
722
   }
723

724
   const size_t test_prob = 128;
27✔
725
   const bool is_randomly_generated = is_builtin;
27✔
726

727
   //check if field modulus is prime
728
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
27✔
729
      return false;
730
   }
731

732
   //check if order is prime
733
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
27✔
734
      return false;
735
   }
736

737
   //compute the discriminant: 4*a^3 + 27*b^2 which must be nonzero
738
   const Modular_Reducer mod_p(p);
27✔
739

740
   const BigInt discriminant = mod_p.reduce(mod_p.multiply(4, mod_p.cube(a)) + mod_p.multiply(27, mod_p.square(b)));
189✔
741

742
   if(discriminant == 0) {
27✔
743
      return false;
744
   }
745

746
   //check for valid cofactor
747
   if(get_cofactor() < 1) {
27✔
748
      return false;
749
   }
750

751
   //check if the base point is on the curve
752
   if(!base_point.on_the_curve()) {
27✔
753
      return false;
754
   }
755
   if((base_point * get_cofactor()).is_zero()) {
54✔
756
      return false;
757
   }
758
   //check if order of the base point is correct
759
   if(!(base_point * order).is_zero()) {
27✔
760
      return false;
×
761
   }
762

763
   return true;
764
}
27✔
765

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