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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

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

95
      size_t p_bytes() const { return (m_p_bits + 7) / 8; }
1,085✔
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,465✔
100

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

103
      const EC_Point& base_point() const { return m_base_point; }
10,670✔
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,386✔
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,344✔
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,692✔
120

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

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

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

168
         if(data) {
899✔
169
            for(auto curve : m_registered_curves) {
2,017✔
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);
830✔
177
            return data;
830✔
178
         }
179

180
         // Nope, unknown curve
181
         return std::shared_ptr<EC_Group_Data>();
899✔
182
      }
16,819✔
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() {
18,670✔
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;
18,670✔
296
   static EC_Group_Data_Map g_ec_data;
18,670✔
297
   return g_ec_data;
18,670✔
298
}
299

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

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

319
   return std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, EC_Group_Source::Builtin);
840✔
320
}
5,880✔
321

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

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

338
      BER_Decoder(bits, len)
589✔
339
         .start_sequence()
618✔
340
         .decode_and_check<size_t>(1, "Unknown ECC param version code")
308✔
341
         .start_sequence()
594✔
342
         .decode_and_check(OID("1.2.840.10045.1.1"), "Only prime ECC fields supported")
572✔
343
         .decode(p)
281✔
344
         .end_cons()
281✔
345
         .start_sequence()
300✔
346
         .decode_octet_string_bigint(a)
281✔
347
         .decode_octet_string_bigint(b)
280✔
348
         .decode_optional_string(seed, ASN1_Type::BitString, ASN1_Type::BitString)
279✔
349
         .end_cons()
279✔
350
         .decode(base_pt, ASN1_Type::OctetString)
276✔
351
         .decode(order)
269✔
352
         .decode(cofactor)
268✔
353
         .end_cons()
268✔
354
         .verify_end();
267✔
355

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

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

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

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

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

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

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

384
EC_Group::EC_Group() = default;
1,923✔
385

386
EC_Group::~EC_Group() = default;
43,074✔
387

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

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

400
   try {
1,120✔
401
      const OID oid = OID::from_string(str);
1,120✔
402
      if(oid.has_value()) {
1,114✔
403
         m_data = ec_group_data().lookup(oid);
1,114✔
404
      }
405
   } catch(...) {}
1,120✔
406

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

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

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

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

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

442
const EC_Group_Data& EC_Group::data() const {
200,161✔
443
   if(m_data == nullptr) {
200,161✔
444
      throw Invalid_State("EC_Group uninitialized");
69✔
445
   }
446
   return *m_data;
200,092✔
447
}
448

449
bool EC_Group::a_is_minus_3() const { return data().a_is_minus_3(); }
27✔
450

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

453
size_t EC_Group::get_p_bits() const { return data().p_bits(); }
1,114✔
454

455
size_t EC_Group::get_p_bytes() const { return data().p_bytes(); }
1,085✔
456

457
size_t EC_Group::get_order_bits() const { return data().order_bits(); }
14,002✔
458

459
size_t EC_Group::get_order_bytes() const { return data().order_bytes(); }
18,465✔
460

461
const BigInt& EC_Group::get_p() const { return data().p(); }
732✔
462

463
const BigInt& EC_Group::get_a() const { return data().a(); }
749✔
464

465
const BigInt& EC_Group::get_b() const { return data().b(); }
695✔
466

467
const EC_Point& EC_Group::get_base_point() const { return data().base_point(); }
10,670✔
468

469
const BigInt& EC_Group::get_order() const { return data().order(); }
41,475✔
470

471
const BigInt& EC_Group::get_g_x() const { return data().g_x(); }
64✔
472

473
const BigInt& EC_Group::get_g_y() const { return data().g_y(); }
64✔
474

475
const BigInt& EC_Group::get_cofactor() const { return data().cofactor(); }
1,229✔
476

477
BigInt EC_Group::mod_order(const BigInt& k) const { return data().mod_order(k); }
26,386✔
478

479
BigInt EC_Group::square_mod_order(const BigInt& x) const { return data().square_mod_order(x); }
888✔
480

481
BigInt EC_Group::multiply_mod_order(const BigInt& x, const BigInt& y) const { return data().multiply_mod_order(x, y); }
25,344✔
482

483
BigInt EC_Group::multiply_mod_order(const BigInt& x, const BigInt& y, const BigInt& z) const {
888✔
484
   return data().multiply_mod_order(x, y, z);
888✔
485
}
486

487
BigInt EC_Group::inverse_mod_order(const BigInt& x) const { return data().inverse_mod_order(x); }
13,692✔
488

489
const OID& EC_Group::get_curve_oid() const { return data().oid(); }
17,070✔
490

491
EC_Group_Source EC_Group::source() const { return data().source(); }
106✔
492

493
size_t EC_Group::point_size(EC_Point_Format format) const {
123✔
494
   // Hybrid and standard format are (x,y), compressed is y, +1 format byte
495
   if(format == EC_Point_Format::Compressed) {
123✔
496
      return (1 + get_p_bytes());
42✔
497
   } else {
498
      return (1 + 2 * get_p_bytes());
81✔
499
   }
500
}
501

502
EC_Point EC_Group::OS2ECP(const uint8_t bits[], size_t len) const { return Botan::OS2ECP(bits, len, data().curve()); }
9,837✔
503

504
EC_Point EC_Group::point(const BigInt& x, const BigInt& y) const {
12,599✔
505
   // TODO: randomize the representation?
506
   return EC_Point(data().curve(), x, y);
12,599✔
507
}
508

509
EC_Point EC_Group::point_multiply(const BigInt& x, const EC_Point& pt, const BigInt& y) const {
×
510
   EC_Point_Multi_Point_Precompute xy_mul(get_base_point(), pt);
×
511
   return xy_mul.multi_exp(x, y);
×
512
}
×
513

514
EC_Point EC_Group::blinded_base_point_multiply(const BigInt& k,
2,255✔
515
                                               RandomNumberGenerator& rng,
516
                                               std::vector<BigInt>& ws) const {
517
   return data().blinded_base_point_multiply(k, rng, ws);
2,255✔
518
}
519

520
BigInt EC_Group::blinded_base_point_multiply_x(const BigInt& k,
644✔
521
                                               RandomNumberGenerator& rng,
522
                                               std::vector<BigInt>& ws) const {
523
   const EC_Point pt = data().blinded_base_point_multiply(k, rng, ws);
644✔
524

525
   if(pt.is_zero()) {
1,288✔
526
      return BigInt::zero();
×
527
   }
528
   return pt.get_affine_x();
644✔
529
}
644✔
530

531
BigInt EC_Group::random_scalar(RandomNumberGenerator& rng) const {
1,103✔
532
   return BigInt::random_integer(rng, BigInt::one(), get_order());
3,309✔
533
}
534

535
EC_Point EC_Group::blinded_var_point_multiply(const EC_Point& point,
2,781✔
536
                                              const BigInt& k,
537
                                              RandomNumberGenerator& rng,
538
                                              std::vector<BigInt>& ws) const {
539
   EC_Point_Var_Point_Precompute mul(point, rng, ws);
2,781✔
540
   return mul.mul(k, rng, get_order(), ws);
5,562✔
541
}
2,781✔
542

543
EC_Point EC_Group::zero_point() const { return EC_Point(data().curve()); }
54✔
544

545
EC_Point EC_Group::hash_to_curve(std::string_view hash_fn,
30✔
546
                                 const uint8_t input[],
547
                                 size_t input_len,
548
                                 std::string_view domain,
549
                                 bool random_oracle) const {
550
   return this->hash_to_curve(
30✔
551
      hash_fn, input, input_len, reinterpret_cast<const uint8_t*>(domain.data()), domain.size(), random_oracle);
30✔
552
}
553

554
EC_Point EC_Group::hash_to_curve(std::string_view hash_fn,
36✔
555
                                 const uint8_t input[],
556
                                 size_t input_len,
557
                                 const uint8_t domain_sep[],
558
                                 size_t domain_sep_len,
559
                                 bool random_oracle) const {
560
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
561

562
   // Only have SSWU currently
563
   if(get_a().is_zero() || get_b().is_zero() || get_p() % 4 == 1) {
36✔
564
      throw Not_Implemented("EC_Group::hash_to_curve not available for this curve type");
×
565
   }
566

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

569
#else
570
   BOTAN_UNUSED(hash_fn, random_oracle, input, input_len, domain_sep, domain_sep_len);
571
   throw Not_Implemented("EC_Group::hash_to_curve functionality not available in this configuration");
572
#endif
573
}
574

575
std::vector<uint8_t> EC_Group::DER_encode(EC_Group_Encoding form) const {
769✔
576
   std::vector<uint8_t> output;
769✔
577

578
   DER_Encoder der(output);
769✔
579

580
   if(form == EC_Group_Encoding::Explicit) {
769✔
581
      const size_t ecpVers1 = 1;
7✔
582
      const OID curve_type("1.2.840.10045.1.1");  // prime field
7✔
583

584
      const size_t p_bytes = get_p_bytes();
7✔
585

586
      der.start_sequence()
7✔
587
         .encode(ecpVers1)
7✔
588
         .start_sequence()
7✔
589
         .encode(curve_type)
7✔
590
         .encode(get_p())
7✔
591
         .end_cons()
7✔
592
         .start_sequence()
7✔
593
         .encode(BigInt::encode_1363(get_a(), p_bytes), ASN1_Type::OctetString)
7✔
594
         .encode(BigInt::encode_1363(get_b(), p_bytes), ASN1_Type::OctetString)
14✔
595
         .end_cons()
7✔
596
         .encode(get_base_point().encode(EC_Point_Format::Uncompressed), ASN1_Type::OctetString)
14✔
597
         .encode(get_order())
7✔
598
         .encode(get_cofactor())
7✔
599
         .end_cons();
7✔
600
   } else if(form == EC_Group_Encoding::NamedCurve) {
769✔
601
      const OID oid = get_curve_oid();
762✔
602
      if(oid.empty()) {
762✔
603
         throw Encoding_Error("Cannot encode EC_Group as OID because OID not set");
×
604
      }
605
      der.encode(oid);
762✔
606
   } else if(form == EC_Group_Encoding::ImplicitCA) {
762✔
607
      der.encode_null();
×
608
   } else {
609
      throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
×
610
   }
611

612
   return output;
769✔
613
}
769✔
614

615
std::string EC_Group::PEM_encode() const {
1✔
616
   const std::vector<uint8_t> der = DER_encode(EC_Group_Encoding::Explicit);
1✔
617
   return PEM_Code::encode(der, "EC PARAMETERS");
1✔
618
}
1✔
619

620
bool EC_Group::operator==(const EC_Group& other) const {
54✔
621
   if(m_data == other.m_data) {
54✔
622
      return true;  // same shared rep
623
   }
624

625
   return (get_p() == other.get_p() && get_a() == other.get_a() && get_b() == other.get_b() &&
×
626
           get_g_x() == other.get_g_x() && get_g_y() == other.get_g_y() && get_order() == other.get_order() &&
×
627
           get_cofactor() == other.get_cofactor());
×
628
}
629

630
bool EC_Group::verify_public_element(const EC_Point& point) const {
79✔
631
   //check that public point is not at infinity
632
   if(point.is_zero()) {
120✔
633
      return false;
634
   }
635

636
   //check that public point is on the curve
637
   if(point.on_the_curve() == false) {
79✔
638
      return false;
639
   }
640

641
   //check that public point has order q
642
   if((point * get_order()).is_zero() == false) {
40✔
643
      return false;
644
   }
645

646
   if(get_cofactor() > 1) {
40✔
647
      if((point * get_cofactor()).is_zero()) {
×
648
         return false;
×
649
      }
650
   }
651

652
   return true;
653
}
654

655
bool EC_Group::verify_group(RandomNumberGenerator& rng, bool strong) const {
106✔
656
   const bool is_builtin = source() == EC_Group_Source::Builtin;
106✔
657

658
   if(is_builtin && !strong) {
106✔
659
      return true;
660
   }
661

662
   const BigInt& p = get_p();
27✔
663
   const BigInt& a = get_a();
27✔
664
   const BigInt& b = get_b();
27✔
665
   const BigInt& order = get_order();
27✔
666
   const EC_Point& base_point = get_base_point();
27✔
667

668
   if(p <= 3 || order <= 0) {
54✔
669
      return false;
×
670
   }
671
   if(a < 0 || a >= p) {
54✔
672
      return false;
×
673
   }
674
   if(b <= 0 || b >= p) {
54✔
675
      return false;
×
676
   }
677

678
   const size_t test_prob = 128;
27✔
679
   const bool is_randomly_generated = is_builtin;
27✔
680

681
   //check if field modulus is prime
682
   if(!is_prime(p, rng, test_prob, is_randomly_generated)) {
27✔
683
      return false;
684
   }
685

686
   //check if order is prime
687
   if(!is_prime(order, rng, test_prob, is_randomly_generated)) {
27✔
688
      return false;
689
   }
690

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

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

696
   if(discriminant == 0) {
27✔
697
      return false;
698
   }
699

700
   //check for valid cofactor
701
   if(get_cofactor() < 1) {
27✔
702
      return false;
703
   }
704

705
   //check if the base point is on the curve
706
   if(!base_point.on_the_curve()) {
27✔
707
      return false;
708
   }
709
   if((base_point * get_cofactor()).is_zero()) {
54✔
710
      return false;
711
   }
712
   //check if order of the base point is correct
713
   if(!(base_point * order).is_zero()) {
27✔
714
      return false;
×
715
   }
716

717
   return true;
718
}
27✔
719

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