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

randombit / botan / 5890505464

17 Aug 2023 11:45AM UTC coverage: 91.716% (+0.007%) from 91.709%
5890505464

push

github

randombit
Merge GH #3667 Add Python argon2 interface

78581 of 85679 relevant lines covered (91.72%)

8394432.28 hits per line

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

91.85
/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,139✔
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,139✔
41
            m_curve(p, a, b),
1,139✔
42
            m_base_point(m_curve, g_x, g_y),
1,139✔
43
            m_g_x(g_x),
1,138✔
44
            m_g_y(g_y),
1,138✔
45
            m_order(order),
1,138✔
46
            m_cofactor(cofactor),
1,138✔
47
            m_mod_order(order),
1,138✔
48
            m_base_mult(m_base_point, m_mod_order),
1,138✔
49
            m_oid(oid),
1,138✔
50
            m_p_bits(p.bits()),
1,138✔
51
            m_order_bits(order.bits()),
1,138✔
52
            m_a_is_minus_3(a == p - 3),
2,276✔
53
            m_a_is_zero(a.is_zero()),
1,138✔
54
            m_source(source) {}
1,139✔
55

56
      bool params_match(const BigInt& p,
1,718✔
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 &&
334✔
64
                 this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
2,011✔
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; }
137,734✔
78

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

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

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

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

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

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

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

93
      size_t p_bits() const { return m_p_bits; }
950✔
94

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

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

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

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

103
      const EC_Point& base_point() const { return m_base_point; }
11,358✔
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); }
27,149✔
110

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

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

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

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

121
      EC_Point blinded_base_point_multiply(const BigInt& k, RandomNumberGenerator& rng, std::vector<BigInt>& ws) const {
2,686✔
122
         return m_base_mult.mul(k, rng, m_order, ws);
2,686✔
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;
343✔
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,283✔
157
         lock_guard_type<mutex_type> lock(m_mutex);
17,283✔
158

159
         for(auto i : m_registered_curves) {
137,776✔
160
            if(i->oid() == oid) {
136,598✔
161
               return i;
16,105✔
162
            }
163
         }
136,598✔
164

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

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

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

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

184
      std::shared_ptr<EC_Group_Data> lookup_or_create(const BigInt& p,
109✔
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);
109✔
194

195
         for(auto i : m_registered_curves) {
1,781✔
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,760✔
202
               continue;
112✔
203
            }
204

205
            const bool same_oid = !oid.empty() && i->oid() == oid;
1,648✔
206
            const bool same_params = i->params_match(p, a, b, g_x, g_y, order, cofactor);
1,648✔
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,648✔
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,647✔
220
               return i;
87✔
221
            }
222

223
            /*
224
            * Check for someone trying to reuse an already in-use OID
225
            */
226
            if(same_oid && !same_params) {
1,560✔
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,560✔
236
               i->set_oid(oid);
×
237
               return i;
88✔
238
            }
239
         }
1,760✔
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
      }
108✔
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,161✔
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,161✔
296
   static EC_Group_Data_Map g_ec_data;
19,161✔
297
   return g_ec_data;
19,161✔
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,118✔
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,118✔
314
   const BigInt a(a_str);
1,118✔
315
   const BigInt b(b_str);
1,118✔
316
   const BigInt g_x(g_x_str);
1,118✔
317
   const BigInt g_y(g_y_str);
1,118✔
318
   const BigInt order(order_str);
1,118✔
319
   const BigInt cofactor(1);  // implicit
1,118✔
320

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

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

331
   if(obj.type() == ASN1_Type::ObjectId) {
4,163✔
332
      OID dom_par_oid;
3,821✔
333
      BER_Decoder(bits, len).decode(dom_par_oid);
3,821✔
334
      return std::make_pair(ec_group_data().lookup(dom_par_oid), false);
3,818✔
335
   }
3,821✔
336

337
   if(obj.type() == ASN1_Type::Sequence) {
342✔
338
      BigInt p, a, b, order, cofactor;
338✔
339
      std::vector<uint8_t> base_pt;
338✔
340
      std::vector<uint8_t> seed;
338✔
341

342
      BER_Decoder(bits, len)
618✔
343
         .start_sequence()
674✔
344
         .decode_and_check<size_t>(1, "Unknown ECC param version code")
336✔
345
         .start_sequence()
650✔
346
         .decode_and_check(OID("1.2.840.10045.1.1"), "Only prime ECC fields supported")
628✔
347
         .decode(p)
309✔
348
         .end_cons()
309✔
349
         .start_sequence()
329✔
350
         .decode_octet_string_bigint(a)
309✔
351
         .decode_octet_string_bigint(b)
308✔
352
         .decode_optional_string(seed, ASN1_Type::BitString, ASN1_Type::BitString)
306✔
353
         .end_cons()
306✔
354
         .decode(base_pt, ASN1_Type::OctetString)
303✔
355
         .decode(order)
296✔
356
         .decode(cofactor)
295✔
357
         .end_cons()
295✔
358
         .verify_end();
294✔
359

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

364
      if(a.is_negative() || a >= p) {
264✔
365
         throw Decoding_Error("Invalid ECC a parameter");
13✔
366
      }
367

368
      if(b <= 0 || b >= p) {
238✔
369
         throw Decoding_Error("Invalid ECC b parameter");
55✔
370
      }
371

372
      if(order <= 0 || !is_bailie_psw_probable_prime(order)) {
64✔
373
         throw Decoding_Error("Invalid ECC order parameter");
2✔
374
      }
375

376
      if(cofactor <= 0 || cofactor >= 16) {
122✔
377
         throw Decoding_Error("Invalid ECC cofactor parameter");
3✔
378
      }
379

380
      std::pair<BigInt, BigInt> base_xy = Botan::OS2ECP(base_pt.data(), base_pt.size(), p, a, b);
59✔
381

382
      auto data =
59✔
383
         ec_group_data().lookup_or_create(p, a, b, base_xy.first, base_xy.second, order, cofactor, OID(), source);
59✔
384
      return std::make_pair(data, true);
58✔
385
   }
2,367✔
386

387
   if(obj.type() == ASN1_Type::Null) {
4✔
388
      throw Decoding_Error("Cannot handle ImplicitCA ECC parameters");
2✔
389
   } else {
390
      throw Decoding_Error(fmt("Unexpected tag {} while decoding ECC domain params", asn1_tag_to_string(obj.type())));
4✔
391
   }
392
}
4,171✔
393

394
EC_Group::EC_Group() = default;
2,149✔
395

396
EC_Group::~EC_Group() = default;
43,835✔
397

398
EC_Group::EC_Group(const OID& domain_oid) {
12,739✔
399
   this->m_data = ec_group_data().lookup(domain_oid);
12,739✔
400
   if(!this->m_data) {
12,739✔
401
      throw Invalid_Argument("Unknown EC_Group " + domain_oid.to_string());
×
402
   }
403
}
12,739✔
404

405
EC_Group::EC_Group(std::string_view str) {
732✔
406
   if(str.empty()) {
732✔
407
      return;  // no initialization / uninitialized
408
   }
409

410
   try {
732✔
411
      const OID oid = OID::from_string(str);
732✔
412
      if(oid.has_value()) {
726✔
413
         m_data = ec_group_data().lookup(oid);
726✔
414
      }
415
   } catch(...) {}
732✔
416

417
   if(m_data == nullptr) {
732✔
418
      if(str.size() > 30 && str.substr(0, 29) == "-----BEGIN EC PARAMETERS-----") {
6✔
419
         // OK try it as PEM ...
420
         secure_vector<uint8_t> ber = PEM_Code::decode_check_label(str, "EC PARAMETERS");
6✔
421

422
         auto data = BER_decode_EC_group(ber.data(), ber.size(), EC_Group_Source::ExternalSource);
6✔
423
         this->m_data = data.first;
6✔
424
         this->m_explicit_encoding = data.second;
6✔
425
      }
12✔
426
   }
427

428
   if(m_data == nullptr) {
732✔
429
      throw Invalid_Argument(fmt("Unknown ECC group '{}'", str));
×
430
   }
431
}
×
432

433
//static
434
EC_Group EC_Group::EC_Group_from_PEM(std::string_view pem) {
×
435
   const auto ber = PEM_Code::decode_check_label(pem, "EC PARAMETERS");
×
436
   return EC_Group(ber.data(), ber.size());
×
437
}
×
438

439
EC_Group::EC_Group(const BigInt& p,
50✔
440
                   const BigInt& a,
441
                   const BigInt& b,
442
                   const BigInt& base_x,
443
                   const BigInt& base_y,
444
                   const BigInt& order,
445
                   const BigInt& cofactor,
446
                   const OID& oid) {
50✔
447
   m_data =
50✔
448
      ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource);
50✔
449
}
50✔
450

451
EC_Group::EC_Group(const uint8_t ber[], size_t ber_len) {
4,165✔
452
   auto data = BER_decode_EC_group(ber, ber_len, EC_Group_Source::ExternalSource);
4,165✔
453
   m_data = data.first;
3,870✔
454
   m_explicit_encoding = data.second;
3,870✔
455
}
4,165✔
456

457
const EC_Group_Data& EC_Group::data() const {
204,009✔
458
   if(m_data == nullptr) {
204,009✔
459
      throw Invalid_State("EC_Group uninitialized");
70✔
460
   }
461
   return *m_data;
203,939✔
462
}
463

464
bool EC_Group::a_is_minus_3() const {
27✔
465
   return data().a_is_minus_3();
27✔
466
}
467

468
bool EC_Group::a_is_zero() const {
27✔
469
   return data().a_is_zero();
27✔
470
}
471

472
size_t EC_Group::get_p_bits() const {
950✔
473
   return data().p_bits();
950✔
474
}
475

476
size_t EC_Group::get_p_bytes() const {
942✔
477
   return data().p_bytes();
942✔
478
}
479

480
size_t EC_Group::get_order_bits() const {
14,388✔
481
   return data().order_bits();
14,388✔
482
}
483

484
size_t EC_Group::get_order_bytes() const {
18,854✔
485
   return data().order_bytes();
18,854✔
486
}
487

488
const BigInt& EC_Group::get_p() const {
759✔
489
   return data().p();
759✔
490
}
491

492
const BigInt& EC_Group::get_a() const {
740✔
493
   return data().a();
740✔
494
}
495

496
const BigInt& EC_Group::get_b() const {
686✔
497
   return data().b();
686✔
498
}
499

500
const EC_Point& EC_Group::get_base_point() const {
11,358✔
501
   return data().base_point();
11,358✔
502
}
503

504
const BigInt& EC_Group::get_order() const {
42,269✔
505
   return data().order();
42,269✔
506
}
507

508
const BigInt& EC_Group::get_g_x() const {
64✔
509
   return data().g_x();
64✔
510
}
511

512
const BigInt& EC_Group::get_g_y() const {
64✔
513
   return data().g_y();
64✔
514
}
515

516
const BigInt& EC_Group::get_cofactor() const {
916✔
517
   return data().cofactor();
916✔
518
}
519

520
BigInt EC_Group::mod_order(const BigInt& k) const {
27,149✔
521
   return data().mod_order(k);
27,149✔
522
}
523

524
BigInt EC_Group::square_mod_order(const BigInt& x) const {
892✔
525
   return data().square_mod_order(x);
892✔
526
}
527

528
BigInt EC_Group::multiply_mod_order(const BigInt& x, const BigInt& y) const {
26,101✔
529
   return data().multiply_mod_order(x, y);
26,101✔
530
}
531

532
BigInt EC_Group::multiply_mod_order(const BigInt& x, const BigInt& y, const BigInt& z) const {
892✔
533
   return data().multiply_mod_order(x, y, z);
892✔
534
}
535

536
BigInt EC_Group::inverse_mod_order(const BigInt& x) const {
13,901✔
537
   return data().inverse_mod_order(x);
13,901✔
538
}
539

540
const OID& EC_Group::get_curve_oid() const {
17,534✔
541
   return data().oid();
17,534✔
542
}
543

544
EC_Group_Source EC_Group::source() const {
106✔
545
   return data().source();
106✔
546
}
547

548
size_t EC_Group::point_size(EC_Point_Format format) const {
123✔
549
   // Hybrid and standard format are (x,y), compressed is y, +1 format byte
550
   if(format == EC_Point_Format::Compressed) {
123✔
551
      return (1 + get_p_bytes());
42✔
552
   } else {
553
      return (1 + 2 * get_p_bytes());
81✔
554
   }
555
}
556

557
EC_Point EC_Group::OS2ECP(const uint8_t bits[], size_t len) const {
10,063✔
558
   return Botan::OS2ECP(bits, len, data().curve());
10,063✔
559
}
560

561
EC_Point EC_Group::point(const BigInt& x, const BigInt& y) const {
12,587✔
562
   // TODO: randomize the representation?
563
   return EC_Point(data().curve(), x, y);
12,587✔
564
}
565

566
EC_Point EC_Group::point_multiply(const BigInt& x, const EC_Point& pt, const BigInt& y) const {
×
567
   EC_Point_Multi_Point_Precompute xy_mul(get_base_point(), pt);
×
568
   return xy_mul.multi_exp(x, y);
×
569
}
×
570

571
EC_Point EC_Group::blinded_base_point_multiply(const BigInt& k,
2,037✔
572
                                               RandomNumberGenerator& rng,
573
                                               std::vector<BigInt>& ws) const {
574
   return data().blinded_base_point_multiply(k, rng, ws);
2,037✔
575
}
576

577
BigInt EC_Group::blinded_base_point_multiply_x(const BigInt& k,
649✔
578
                                               RandomNumberGenerator& rng,
579
                                               std::vector<BigInt>& ws) const {
580
   const EC_Point pt = data().blinded_base_point_multiply(k, rng, ws);
649✔
581

582
   if(pt.is_zero()) {
1,298✔
583
      return BigInt::zero();
×
584
   }
585
   return pt.get_affine_x();
649✔
586
}
649✔
587

588
BigInt EC_Group::random_scalar(RandomNumberGenerator& rng) const {
890✔
589
   return BigInt::random_integer(rng, BigInt::one(), get_order());
2,670✔
590
}
591

592
EC_Point EC_Group::blinded_var_point_multiply(const EC_Point& point,
2,612✔
593
                                              const BigInt& k,
594
                                              RandomNumberGenerator& rng,
595
                                              std::vector<BigInt>& ws) const {
596
   EC_Point_Var_Point_Precompute mul(point, rng, ws);
2,612✔
597
   return mul.mul(k, rng, get_order(), ws);
5,224✔
598
}
2,612✔
599

600
EC_Point EC_Group::zero_point() const {
54✔
601
   return EC_Point(data().curve());
54✔
602
}
603

604
EC_Point EC_Group::hash_to_curve(std::string_view hash_fn,
30✔
605
                                 const uint8_t input[],
606
                                 size_t input_len,
607
                                 std::string_view domain,
608
                                 bool random_oracle) const {
609
   return this->hash_to_curve(
30✔
610
      hash_fn, input, input_len, reinterpret_cast<const uint8_t*>(domain.data()), domain.size(), random_oracle);
30✔
611
}
612

613
EC_Point EC_Group::hash_to_curve(std::string_view hash_fn,
36✔
614
                                 const uint8_t input[],
615
                                 size_t input_len,
616
                                 const uint8_t domain_sep[],
617
                                 size_t domain_sep_len,
618
                                 bool random_oracle) const {
619
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
620

621
   // Only have SSWU currently
622
   if(get_a().is_zero() || get_b().is_zero() || get_p() % 4 == 1) {
36✔
623
      throw Not_Implemented("EC_Group::hash_to_curve not available for this curve type");
×
624
   }
625

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

628
#else
629
   BOTAN_UNUSED(hash_fn, random_oracle, input, input_len, domain_sep, domain_sep_len);
630
   throw Not_Implemented("EC_Group::hash_to_curve functionality not available in this configuration");
631
#endif
632
}
633

634
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
823✔
635
   std::vector<uint8_t> output;
823✔
636

637
   DER_Encoder der(output);
823✔
638

639
   if(form == EC_Group_Encoding::Explicit) {
823✔
640
      const size_t ecpVers1 = 1;
34✔
641
      const OID curve_type("1.2.840.10045.1.1");  // prime field
34✔
642

643
      const size_t p_bytes = get_p_bytes();
34✔
644

645
      der.start_sequence()
34✔
646
         .encode(ecpVers1)
34✔
647
         .start_sequence()
34✔
648
         .encode(curve_type)
34✔
649
         .encode(get_p())
34✔
650
         .end_cons()
34✔
651
         .start_sequence()
34✔
652
         .encode(BigInt::encode_1363(get_a(), p_bytes), ASN1_Type::OctetString)
34✔
653
         .encode(BigInt::encode_1363(get_b(), p_bytes), ASN1_Type::OctetString)
68✔
654
         .end_cons()
34✔
655
         .encode(get_base_point().encode(EC_Point_Format::Uncompressed), ASN1_Type::OctetString)
68✔
656
         .encode(get_order())
34✔
657
         .encode(get_cofactor())
34✔
658
         .end_cons();
34✔
659
   } else if(form == EC_Group_Encoding::NamedCurve) {
823✔
660
      const OID oid = get_curve_oid();
789✔
661
      if(oid.empty()) {
789✔
662
         throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
663
      }
664
      der.encode(oid);
789✔
665
   } else if(form == EC_Group_Encoding::ImplicitCA) {
789✔
666
      der.encode_null();
×
667
   } else {
668
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
669
   }
670

671
   return output;
823✔
672
}
823✔
673

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

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

684
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
×
685
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
×
686
           get_cofactor() == other.get_cofactor());
×
687
}
688

689
bool EC_Group::verify_public_element(const EC_Point& point) const {
79✔
690
   //check that public point is not at infinity
691
   if(point.is_zero()) {
120✔
692
      return false;
693
   }
694

695
   //check that public point is on the curve
696
   if(point.on_the_curve() == false) {
79✔
697
      return false;
698
   }
699

700
   //check that public point has order q
701
   if((point * get_order()).is_zero() == false) {
40✔
702
      return false;
703
   }
704

705
   if(get_cofactor() > 1) {
40✔
706
      if((point * get_cofactor()).is_zero()) {
×
707
         return false;
×
708
      }
709
   }
710

711
   return true;
712
}
713

714
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
106✔
715
   const bool is_builtin = source() == EC_Group_Source::Builtin;
106✔
716

717
   if(is_builtin && !strong) {
106✔
718
      return true;
719
   }
720

721
   const BigInt& p = get_p();
27✔
722
   const BigInt& a = get_a();
27✔
723
   const BigInt& b = get_b();
27✔
724
   const BigInt& order = get_order();
27✔
725
   const EC_Point& base_point = get_base_point();
27✔
726

727
   if(p <= 3 || order <= 0) {
54✔
728
      return false;
×
729
   }
730
   if(a < 0 || a >= p) {
54✔
731
      return false;
×
732
   }
733
   if(b <= 0 || b >= p) {
54✔
734
      return false;
×
735
   }
736

737
   const size_t test_prob = 128;
27✔
738
   const bool is_randomly_generated = is_builtin;
27✔
739

740
   //check if field modulus is prime
741
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
27✔
742
      return false;
743
   }
744

745
   //check if order is prime
746
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
27✔
747
      return false;
748
   }
749

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

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

755
   if(discriminant == 0) {
27✔
756
      return false;
757
   }
758

759
   //check for valid cofactor
760
   if(get_cofactor() < 1) {
27✔
761
      return false;
762
   }
763

764
   //check if the base point is on the curve
765
   if(!base_point.on_the_curve()) {
27✔
766
      return false;
767
   }
768
   if((base_point * get_cofactor()).is_zero()) {
54✔
769
      return false;
770
   }
771
   //check if order of the base point is correct
772
   if(!(base_point * order).is_zero()) {
27✔
773
      return false;
×
774
   }
775

776
   return true;
777
}
27✔
778

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