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

randombit / botan / 13073795340

31 Jan 2025 01:32PM UTC coverage: 91.224% (-0.009%) from 91.233%
13073795340

push

github

web-flow
Merge pull request #4617 from randombit/jack/fix-build-configs

Fix build/test errors caught by test_all_configs.py

94150 of 103208 relevant lines covered (91.22%)

11427437.8 hits per line

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

93.61
/src/tests/test_ec_group.cpp
1
/*
2
* (C) 2007 Falko Strenzke
3
*     2007 Manuel Hartl
4
*     2009,2015,2018,2021 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_ECC_GROUP)
12
   #include <botan/bigint.h>
13
   #include <botan/data_src.h>
14
   #include <botan/ec_group.h>
15
   #include <botan/hex.h>
16
   #include <botan/numthry.h>
17
   #include <botan/pk_keys.h>
18
   #include <botan/reducer.h>
19
   #include <botan/x509_key.h>
20
   #include <botan/internal/ec_inner_data.h>
21
#endif
22

23
namespace Botan_Tests {
24

25
namespace {
26

27
#if defined(BOTAN_HAS_ECC_GROUP)
28

29
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
30

31
Botan::BigInt test_integer(Botan::RandomNumberGenerator& rng, size_t bits, const BigInt& max) {
560✔
32
   /*
33
   Produces integers with long runs of ones and zeros, for testing for
34
   carry handling problems.
35
   */
36
   Botan::BigInt x = 0;
560✔
37

38
   auto flip_prob = [](size_t i) -> double {
154,040✔
39
      if(i % 64 == 0) {
153,480✔
40
         return .5;
41
      }
42
      if(i % 32 == 0) {
150,980✔
43
         return .4;
44
      }
45
      if(i % 8 == 0) {
148,560✔
46
         return .05;
14,360✔
47
      }
48
      return .01;
49
   };
50

51
   bool active = (rng.next_byte() > 128) ? true : false;
560✔
52
   for(size_t i = 0; i != bits; ++i) {
154,040✔
53
      x <<= 1;
153,480✔
54
      x += static_cast<int>(active);
153,480✔
55

56
      const double prob = flip_prob(i);
153,480✔
57
      const double sample = double(rng.next_byte() % 100) / 100.0;  // biased
153,480✔
58

59
      if(sample < prob) {
153,480✔
60
         active = !active;
5,087✔
61
      }
62
   }
63

64
   if(x == 0) {
560✔
65
      // EC_Scalar rejects zero as an input, if we hit this case instead
66
      // test with a completely randomized scalar
67
      return BigInt::random_integer(rng, 1, max);
×
68
   }
69

70
   if(max > 0) {
560✔
71
      while(x >= max) {
637✔
72
         const size_t b = x.bits() - 1;
77✔
73
         BOTAN_ASSERT(x.get_bit(b) == true, "Set");
154✔
74
         x.clear_bit(b);
77✔
75
      }
76
   }
77

78
   return x;
560✔
79
}
560✔
80

81
Botan::EC_Point create_random_point(Botan::RandomNumberGenerator& rng, const Botan::EC_Group& group) {
112✔
82
   const Botan::BigInt& p = group.get_p();
112✔
83
   auto mod_p = Botan::Modular_Reducer::for_public_modulus(p);
112✔
84

85
   for(;;) {
306✔
86
      const Botan::BigInt x = Botan::BigInt::random_integer(rng, 1, p);
209✔
87
      const Botan::BigInt x3 = mod_p.multiply(x, mod_p.square(x));
209✔
88
      const Botan::BigInt ax = mod_p.multiply(group.get_a(), x);
209✔
89
      const Botan::BigInt y = mod_p.reduce(x3 + ax + group.get_b());
418✔
90
      const Botan::BigInt sqrt_y = Botan::sqrt_modulo_prime(y, p);
209✔
91

92
      if(sqrt_y > 1) {
209✔
93
         BOTAN_ASSERT_EQUAL(mod_p.square(sqrt_y), y, "Square root is correct");
336✔
94
         return group.point(x, sqrt_y);
224✔
95
      }
96
   }
933✔
97
}
112✔
98

99
class ECC_Randomized_Tests final : public Test {
×
100
   public:
101
      std::vector<Test::Result> run() override;
102
};
103

104
std::vector<Test::Result> ECC_Randomized_Tests::run() {
1✔
105
   std::vector<Test::Result> results;
1✔
106
   for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
107
      Test::Result result("ECC randomized " + group_name);
28✔
108

109
      result.start_timer();
28✔
110

111
      auto group = Botan::EC_Group::from_name(group_name);
28✔
112

113
      const Botan::EC_Point pt = create_random_point(this->rng(), group);
28✔
114

115
      std::vector<Botan::BigInt> blind_ws;
28✔
116

117
      try {
28✔
118
         const size_t trials = (Test::run_long_tests() ? 10 : 3);
28✔
119
         for(size_t i = 0; i < trials; ++i) {
308✔
120
            const Botan::BigInt a = test_integer(rng(), group.get_order_bits(), group.get_order());
280✔
121
            const Botan::BigInt b = test_integer(rng(), group.get_order_bits(), group.get_order());
280✔
122
            const Botan::BigInt c = group.mod_order(a + b);
280✔
123

124
            const Botan::EC_Point P = pt * a;
280✔
125
            const Botan::EC_Point Q = pt * b;
280✔
126
            const Botan::EC_Point R = pt * c;
280✔
127

128
            Botan::EC_Point P1 = group.blinded_var_point_multiply(pt, a, this->rng(), blind_ws);
280✔
129
            Botan::EC_Point Q1 = group.blinded_var_point_multiply(pt, b, this->rng(), blind_ws);
280✔
130
            Botan::EC_Point R1 = group.blinded_var_point_multiply(pt, c, this->rng(), blind_ws);
280✔
131

132
            Botan::EC_Point A1 = P + Q;
280✔
133
            Botan::EC_Point A2 = Q + P;
280✔
134

135
            result.test_eq("p + q", A1, R);
280✔
136
            result.test_eq("q + p", A2, R);
280✔
137

138
            A1.force_affine();
280✔
139
            A2.force_affine();
280✔
140
            result.test_eq("p + q", A1, R);
280✔
141
            result.test_eq("q + p", A2, R);
280✔
142

143
            result.test_eq("p on the curve", P.on_the_curve(), true);
280✔
144
            result.test_eq("q on the curve", Q.on_the_curve(), true);
280✔
145
            result.test_eq("r on the curve", R.on_the_curve(), true);
280✔
146

147
            result.test_eq("P1", P1, P);
280✔
148
            result.test_eq("Q1", Q1, Q);
280✔
149
            result.test_eq("R1", R1, R);
280✔
150

151
            P1.force_affine();
280✔
152
            Q1.force_affine();
280✔
153
            R1.force_affine();
280✔
154
            result.test_eq("P1", P1, P);
280✔
155
            result.test_eq("Q1", Q1, Q);
280✔
156
            result.test_eq("R1", R1, R);
280✔
157
         }
1,120✔
158
      } catch(std::exception& e) {
×
159
         result.test_failure(group_name, e.what());
×
160
      }
×
161
      result.end_timer();
28✔
162
      results.push_back(result);
28✔
163
   }
28✔
164

165
   return results;
1✔
166
}
×
167

168
BOTAN_REGISTER_TEST("pubkey", "ecc_randomized", ECC_Randomized_Tests);
169

170
   #endif
171

172
class EC_Group_Tests : public Test {
×
173
   public:
174
      std::vector<Test::Result> run() override {
1✔
175
         std::vector<Test::Result> results;
1✔
176

177
         for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
178
            Test::Result result("EC_Group " + group_name);
28✔
179

180
            result.start_timer();
28✔
181

182
            const auto group = Botan::EC_Group::from_name(group_name);
28✔
183

184
            result.confirm("EC_Group is known", group.get_curve_oid().has_value());
56✔
185
            result.confirm("EC_Group is considered valid", group.verify_group(this->rng(), true));
56✔
186
            result.confirm("EC_Group is not considered explict encoding", !group.used_explicit_encoding());
56✔
187

188
            result.test_eq("EC_Group has correct bit size", group.get_p().bits(), group.get_p_bits());
28✔
189
            result.test_eq("EC_Group has byte size", group.get_p().bytes(), group.get_p_bytes());
28✔
190

191
            result.test_eq("EC_Group has cofactor == 1", group.get_cofactor(), 1);
56✔
192

193
            const Botan::OID from_order = Botan::EC_Group::EC_group_identity_from_order(group.get_order());
28✔
194

195
            result.test_eq(
56✔
196
               "EC_group_identity_from_order works", from_order.to_string(), group.get_curve_oid().to_string());
56✔
197

198
            result.confirm("Same group is same", group == Botan::EC_Group::from_name(group_name));
56✔
199

200
            try {
28✔
201
               const Botan::EC_Group copy(group.get_curve_oid(),
28✔
202
                                          group.get_p(),
203
                                          group.get_a(),
204
                                          group.get_b(),
205
                                          group.get_g_x(),
206
                                          group.get_g_y(),
207
                                          group.get_order());
28✔
208

209
               result.confirm("Same group is same even with copy", group == copy);
40✔
210
            } catch(Botan::Invalid_Argument&) {}
28✔
211

212
            const auto group_der_oid = group.DER_encode();
28✔
213
            const Botan::EC_Group group_via_oid(group_der_oid);
28✔
214
            result.confirm("EC_Group via OID is not considered explict encoding",
56✔
215
                           !group_via_oid.used_explicit_encoding());
28✔
216

217
            const auto group_der_explicit = group.DER_encode(Botan::EC_Group_Encoding::Explicit);
28✔
218
            const Botan::EC_Group group_via_explicit(group_der_explicit);
28✔
219
            result.confirm("EC_Group via explicit DER is considered explict encoding",
56✔
220
                           group_via_explicit.used_explicit_encoding());
28✔
221

222
            if(group.a_is_minus_3()) {
28✔
223
               result.test_eq("Group A equals -3", group.get_a(), group.get_p() - 3);
51✔
224
            } else {
225
               result.test_ne("Group " + group_name + " A does not equal -3", group.get_a(), group.get_p() - 3);
44✔
226
            }
227

228
            if(group.a_is_zero()) {
28✔
229
               result.test_eq("Group A is zero", group.get_a(), BigInt(0));
8✔
230
            } else {
231
               result.test_ne("Group " + group_name + " A does not equal zero", group.get_a(), BigInt(0));
72✔
232
            }
233

234
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
235
            const auto pt_mult_by_order = group.get_base_point() * group.get_order();
28✔
236
            result.confirm("Multiplying point by the order results in zero point", pt_mult_by_order.is_zero());
56✔
237

238
            // get a valid point
239
            Botan::EC_Point p = group.get_base_point() * this->rng().next_nonzero_byte();
28✔
240

241
            // get a copy
242
            Botan::EC_Point q = p;
28✔
243

244
            p.randomize_repr(this->rng());
28✔
245
            q.randomize_repr(this->rng());
28✔
246

247
            result.test_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
84✔
248
            result.test_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
84✔
249

250
            q.force_affine();
28✔
251

252
            result.test_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
84✔
253
            result.test_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
84✔
254

255
            test_ser_der(result, group);
28✔
256
            test_basic_math(result, group);
28✔
257
            test_point_swap(result, group);
28✔
258
            test_zeropoint(result, group);
28✔
259
   #endif
260

261
            result.end_timer();
28✔
262

263
            results.push_back(result);
28✔
264
         }
84✔
265

266
         return results;
1✔
267
      }
×
268

269
   private:
270
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
271

272
      void test_ser_der(Test::Result& result, const Botan::EC_Group& group) {
28✔
273
         // generate point
274
         const Botan::EC_Point pt = create_random_point(this->rng(), group);
28✔
275
         const Botan::EC_Point zero = group.zero_point();
28✔
276

277
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
196✔
278
                            Botan::EC_Point_Format::Compressed,
279
                            Botan::EC_Point_Format::Hybrid}) {
112✔
280
            result.test_eq("encoded/decode rt works", group.OS2ECP(pt.encode(scheme)), pt);
168✔
281
            result.test_eq("encoded/decode rt works", group.OS2ECP(zero.encode(scheme)), zero);
252✔
282
         }
283
      }
28✔
284

285
      static void test_basic_math(Test::Result& result, const Botan::EC_Group& group) {
28✔
286
         const Botan::EC_Point& G = group.get_base_point();
28✔
287

288
         Botan::EC_Point p1 = G * 2;
56✔
289
         p1 += G;
28✔
290

291
         result.test_eq("point addition", p1, G * 3);
84✔
292

293
         p1 -= G * 2;
56✔
294

295
         result.test_eq("point subtraction", p1, G);
28✔
296

297
         // The scalar multiplication algorithm relies on this being true:
298
         try {
28✔
299
            Botan::EC_Point zero_coords = group.point(0, 0);
56✔
300
            result.confirm("point (0,0) is not on the curve", !zero_coords.on_the_curve());
×
301
         } catch(Botan::Exception&) {
28✔
302
            result.test_success("point (0,0) is rejected");
28✔
303
         }
28✔
304
      }
28✔
305

306
      void test_point_swap(Test::Result& result, const Botan::EC_Group& group) {
28✔
307
         Botan::EC_Point a(create_random_point(this->rng(), group));
28✔
308
         Botan::EC_Point b(create_random_point(this->rng(), group));
28✔
309
         b *= Botan::BigInt(this->rng(), 20);
28✔
310

311
         Botan::EC_Point c(a);
28✔
312
         Botan::EC_Point d(b);
28✔
313

314
         d.swap(c);
28✔
315
         result.test_eq("swap correct", a, d);
28✔
316
         result.test_eq("swap correct", b, c);
28✔
317
      }
28✔
318

319
      static void test_zeropoint(Test::Result& result, const Botan::EC_Group& group) {
28✔
320
         Botan::EC_Point zero = group.zero_point();
28✔
321

322
         result.test_throws("Zero point throws", "Cannot convert zero point to affine", [&]() { zero.get_affine_x(); });
84✔
323
         result.test_throws("Zero point throws", "Cannot convert zero point to affine", [&]() { zero.get_affine_y(); });
84✔
324

325
         const Botan::EC_Point p1 = group.get_base_point() * 2;
28✔
326

327
         result.confirm("point is on the curve", p1.on_the_curve());
56✔
328
         result.confirm("point is not zero", !p1.is_zero());
56✔
329

330
         Botan::EC_Point p2 = p1;
28✔
331
         p2 -= p1;
28✔
332

333
         result.confirm("p - q with q = p results in zero", p2.is_zero());
56✔
334

335
         const Botan::EC_Point minus_p1 = -p1;
28✔
336
         result.confirm("point is on the curve", minus_p1.on_the_curve());
56✔
337
         const Botan::EC_Point shouldBeZero = p1 + minus_p1;
28✔
338
         result.confirm("point is on the curve", shouldBeZero.on_the_curve());
56✔
339
         result.confirm("point is zero", shouldBeZero.is_zero());
56✔
340

341
         result.test_eq("minus point x", minus_p1.get_affine_x(), p1.get_affine_x());
84✔
342
         result.test_eq("minus point y", minus_p1.get_affine_y(), group.get_p() - p1.get_affine_y());
112✔
343

344
         result.confirm("zero point is zero", zero.is_zero());
56✔
345
         result.confirm("zero point is on the curve", zero.on_the_curve());
56✔
346
         result.test_eq("addition of zero does nothing", p1, p1 + zero);
56✔
347
         result.test_eq("addition of zero does nothing", p1, zero + p1);
56✔
348
         result.test_eq("addition of zero does nothing", p1, p1 - zero);
56✔
349
         result.confirm("zero times anything is the zero point", (zero * 39193).is_zero());
84✔
350

351
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
196✔
352
                            Botan::EC_Point_Format::Compressed,
353
                            Botan::EC_Point_Format::Hybrid}) {
112✔
354
            const std::vector<uint8_t> v = zero.encode(scheme);
84✔
355
            result.test_eq("encoded/decode rt works", group.OS2ECP(v), zero);
168✔
356
         }
84✔
357
      }
28✔
358
   #endif
359
};
360

361
BOTAN_REGISTER_TEST("pubkey", "ec_group", EC_Group_Tests);
362

363
Test::Result test_decoding_with_seed() {
1✔
364
   Test::Result result("Decode EC_Group with seed");
1✔
365

366
   try {
1✔
367
      if(Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
368
         const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
369
         const auto secp384r1_with_seed =
1✔
370
            Botan::EC_Group::from_PEM(Test::read_data_file("x509/ecc/secp384r1_seed.pem"));
2✔
371
         result.confirm("decoding worked", secp384r1_with_seed.initialized());
2✔
372
         result.test_eq("P-384 prime", secp384r1_with_seed.get_p(), secp384r1.get_p());
1✔
373
      }
1✔
374
   } catch(Botan::Exception& e) {
×
375
      result.test_failure(e.what());
×
376
   }
×
377

378
   return result;
1✔
379
}
×
380

381
Test::Result test_mixed_points() {
1✔
382
   Test::Result result("Mixed Point Arithmetic");
1✔
383

384
   if(Botan::EC_Group::supports_named_group("secp256r1") && Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
385
      const auto secp256r1 = Botan::EC_Group::from_name("secp256r1");
1✔
386
      const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
387

388
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
389
      const Botan::EC_Point& G256 = secp256r1.get_base_point();
1✔
390
      const Botan::EC_Point& G384 = secp384r1.get_base_point();
1✔
391

392
      result.test_throws("Mixing points from different groups", [&] { Botan::EC_Point p = G256 + G384; });
3✔
393
   #endif
394

395
      const auto p1 = Botan::EC_AffinePoint::generator(secp256r1);
1✔
396
      const auto p2 = Botan::EC_AffinePoint::generator(secp384r1);
1✔
397
      result.test_throws("Mixing points from different groups", [&] { auto p3 = p1.add(p2); });
3✔
398
   }
1✔
399

400
   return result;
1✔
401
}
×
402

403
Test::Result test_ecc_registration() {
1✔
404
   Test::Result result("ECC registration");
1✔
405

406
   // numsp256d1
407
   const Botan::BigInt p("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43");
1✔
408
   const Botan::BigInt a("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40");
1✔
409
   const Botan::BigInt b("0x25581");
1✔
410
   const Botan::BigInt order("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE43C8275EA265C6020AB20294751A825");
1✔
411

412
   const Botan::BigInt g_x("0x01");
1✔
413
   const Botan::BigInt g_y("0x696F1853C1E466D7FC82C96CCEEEDD6BD02C2F9375894EC10BF46306C2B56C77");
1✔
414

415
   const Botan::OID oid("1.3.6.1.4.1.25258.4.1");
1✔
416

417
   // Creating this object implicitly registers the curve for future use ...
418
   Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
419

420
   auto group = Botan::EC_Group::from_OID(oid);
1✔
421

422
   result.test_eq("Group registration worked", group.get_p(), p);
1✔
423

424
   return result;
2✔
425
}
7✔
426

427
Test::Result test_ec_group_from_params() {
1✔
428
   Test::Result result("EC_Group from params");
1✔
429

430
   Botan::EC_Group::clear_registered_curve_data();
1✔
431

432
   // secp256r1
433
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
434
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
435
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
436

437
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
438
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
439
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
440

441
   const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
442

443
   // This uses the deprecated constructor to verify we dedup even without an OID
444
   // This whole test can be removed once explicit curve support is removed
445
   Botan::EC_Group reg_group(p, a, b, g_x, g_y, order, 1);
2✔
446
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == oid);
2✔
447

448
   return result;
2✔
449
}
7✔
450

451
Test::Result test_ec_group_bad_registration() {
1✔
452
   Test::Result result("EC_Group registering non-match");
1✔
453

454
   Botan::EC_Group::clear_registered_curve_data();
1✔
455

456
   // secp256r1 params except with a bad B param
457
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
458
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
459
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604C");
1✔
460

461
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
462
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
463
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
464

465
   const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
466

467
   try {
1✔
468
      Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
469
      result.test_failure("Should have failed");
×
470
   } catch(Botan::Invalid_Argument&) {
1✔
471
      result.test_success("Got expected exception");
1✔
472
   }
1✔
473

474
   return result;
1✔
475
}
7✔
476

477
Test::Result test_ec_group_duplicate_orders() {
1✔
478
   Test::Result result("EC_Group with duplicate group order");
1✔
479

480
   Botan::EC_Group::clear_registered_curve_data();
1✔
481

482
   // secp256r1
483
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
484
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
485
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
486

487
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
488
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
489
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
490

491
   const Botan::OID oid("1.3.6.1.4.1.25258.100.0");  // some other random OID
1✔
492

493
   Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
494
   result.test_success("Registration success");
1✔
495
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == oid);
2✔
496

497
   // We can now get it by OID:
498
   const auto hc_group = Botan::EC_Group::from_OID(oid);
1✔
499
   result.confirm("Group has correct OID", hc_group.get_curve_oid() == oid);
2✔
500

501
   // Existing secp256r1 unmodified:
502
   const Botan::OID secp160r1("1.2.840.10045.3.1.7");
1✔
503
   const auto other_group = Botan::EC_Group::from_OID(secp160r1);
1✔
504
   result.confirm("Group has correct OID", other_group.get_curve_oid() == secp160r1);
2✔
505

506
   return result;
2✔
507
}
7✔
508

509
Test::Result test_ec_group_registration_with_custom_oid() {
1✔
510
   Test::Result result("EC_Group registration of standard group with custom OID");
1✔
511

512
   Botan::EC_Group::clear_registered_curve_data();
1✔
513

514
   const Botan::OID secp256r1_oid("1.2.840.10045.3.1.7");
1✔
515
   const auto secp256r1 = Botan::EC_Group::from_OID(secp256r1_oid);
1✔
516
   result.confirm("Group has correct OID", secp256r1.get_curve_oid() == secp256r1_oid);
2✔
517

518
   const Botan::OID custom_oid("1.3.6.1.4.1.25258.100.99");  // some other random OID
1✔
519

520
   Botan::OID::register_oid(custom_oid, "secp256r1");
1✔
521

522
   Botan::EC_Group reg_group(custom_oid,
1✔
523
                             secp256r1.get_p(),
524
                             secp256r1.get_a(),
525
                             secp256r1.get_b(),
526
                             secp256r1.get_g_x(),
527
                             secp256r1.get_g_y(),
528
                             secp256r1.get_order());
1✔
529

530
   result.test_success("Registration success");
1✔
531
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == custom_oid);
2✔
532

533
   // We can now get it by OID:
534
   result.confirm("Group has correct OID", Botan::EC_Group::from_OID(custom_oid).get_curve_oid() == custom_oid);
2✔
535

536
   // In the current data model of EC_Group there is a 1:1 OID:group, so these
537
   // have distinct underlying data
538
   result.confirm("Groups have different inner data pointers", reg_group._data() != secp256r1._data());
2✔
539

540
   #if defined(BOTAN_HAS_PCURVES_SECP256R1)
541
   // However we should have gotten a pcurves out of the deal *and* it
542
   // should be the exact same shared_ptr as the official curve
543

544
   try {
1✔
545
      const auto& pcurve = reg_group._data()->pcurve();
1✔
546
      result.confirm("Group with custom OID got the same pcurve pointer", &pcurve == &secp256r1._data()->pcurve());
2✔
547
   } catch(...) {
×
548
      result.test_failure("Group with custom OID did not get a pcurve pointer");
×
549
   }
×
550
   #endif
551

552
   return result;
2✔
553
}
1✔
554

555
class ECC_Unit_Tests final : public Test {
×
556
   public:
557
      std::vector<Test::Result> run() override {
1✔
558
         std::vector<Test::Result> results;
1✔
559

560
         results.push_back(test_decoding_with_seed());
2✔
561
         results.push_back(test_mixed_points());
2✔
562

563
         if(Botan::EC_Group::supports_application_specific_group()) {
1✔
564
            results.push_back(test_ecc_registration());
2✔
565
            results.push_back(test_ec_group_from_params());
2✔
566
            results.push_back(test_ec_group_bad_registration());
2✔
567
            results.push_back(test_ec_group_duplicate_orders());
2✔
568
            results.push_back(test_ec_group_registration_with_custom_oid());
2✔
569
         }
570

571
         return results;
1✔
572
      }
×
573
};
574

575
BOTAN_REGISTER_SERIALIZED_TEST("pubkey", "ecc_unit", ECC_Unit_Tests);
576

577
class EC_PointEnc_Tests final : public Test {
×
578
   public:
579
      std::vector<Test::Result> run() override {
1✔
580
         std::vector<Test::Result> results;
1✔
581

582
         auto& rng = Test::rng();
1✔
583

584
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
585
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
586

587
            Result result("EC_AffinePoint encoding " + group_id);
28✔
588

589
            result.start_timer();
28✔
590

591
            std::vector<Botan::BigInt> ws;
28✔
592

593
            for(size_t trial = 0; trial != 100; ++trial) {
2,828✔
594
               const auto scalar = Botan::EC_Scalar::random(group, rng);
2,800✔
595
               const auto pt = Botan::EC_AffinePoint::g_mul(scalar, rng, ws);
2,800✔
596

597
               const auto pt_u = pt.serialize_uncompressed();
2,800✔
598
               result.test_eq("Expected uncompressed header", static_cast<size_t>(pt_u[0]), 0x04);
2,800✔
599
               const size_t fe_bytes = (pt_u.size() - 1) / 2;
2,800✔
600
               const auto pt_c = pt.serialize_compressed();
2,800✔
601

602
               result.test_eq("Expected compressed size", pt_c.size(), 1 + fe_bytes);
2,800✔
603
               const uint8_t expected_c_header = (pt_u[pt_u.size() - 1] % 2 == 0) ? 0x02 : 0x03;
2,800✔
604
               result.confirm("Expected compressed header", pt_c[0] == expected_c_header);
5,600✔
605

606
               result.test_eq(
5,600✔
607
                  "Expected compressed x", std::span{pt_c}.subspan(1), std::span{pt_u}.subspan(1, fe_bytes));
608

609
               if(auto d_pt_u = Botan::EC_AffinePoint::deserialize(group, pt_u)) {
2,800✔
610
                  result.test_eq(
5,600✔
611
                     "Deserializing uncompressed returned correct point", d_pt_u->serialize_uncompressed(), pt_u);
5,600✔
612
               } else {
613
                  result.test_failure("Failed to deserialize uncompressed point");
×
614
               }
×
615

616
               if(auto d_pt_c = Botan::EC_AffinePoint::deserialize(group, pt_c)) {
2,800✔
617
                  result.test_eq(
5,600✔
618
                     "Deserializing compressed returned correct point", d_pt_c->serialize_uncompressed(), pt_u);
5,600✔
619
               } else {
620
                  result.test_failure("Failed to deserialize compressed point");
×
621
               }
×
622

623
               const auto neg_pt_c = [&]() {
2,800✔
624
                  auto x = pt_c;
2,800✔
625
                  x[0] ^= 0x01;
2,800✔
626
                  return x;
2,800✔
627
               }();
2,800✔
628

629
               if(auto d_neg_pt_c = Botan::EC_AffinePoint::deserialize(group, neg_pt_c)) {
2,800✔
630
                  result.test_eq("Deserializing compressed with inverted header returned negated point",
5,600✔
631
                                 d_neg_pt_c->serialize_uncompressed(),
5,600✔
632
                                 pt.negate().serialize_uncompressed());
5,600✔
633
               } else {
634
                  result.test_failure("Failed to deserialize compressed point");
×
635
               }
2,800✔
636
            }
2,800✔
637

638
            result.end_timer();
28✔
639

640
            results.push_back(result);
28✔
641
         }
28✔
642

643
         return results;
1✔
644
      }
2,800✔
645
};
646

647
BOTAN_REGISTER_TEST("pubkey", "ec_point_enc", EC_PointEnc_Tests);
648

649
class EC_Point_Arithmetic_Tests final : public Test {
×
650
   public:
651
      std::vector<Test::Result> run() override {
1✔
652
         std::vector<Test::Result> results;
1✔
653

654
         auto& rng = Test::rng();
1✔
655

656
         std::vector<Botan::BigInt> ws;
1✔
657

658
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
659
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
660

661
            Result result("EC_AffinePoint arithmetic " + group_id);
28✔
662

663
            result.start_timer();
28✔
664

665
            const auto one = Botan::EC_Scalar::one(group);
28✔
666
            const auto zero = one - one;
28✔
667
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
668
            const auto g_bytes = g.serialize_uncompressed();
28✔
669

670
            const auto id = Botan::EC_AffinePoint::g_mul(zero, rng, ws);
28✔
671
            result.confirm("g*zero is point at identity", id.is_identity());
56✔
672

673
            const auto id2 = id.add(id);
28✔
674
            result.confirm("identity plus itself is identity", id2.is_identity());
56✔
675

676
            const auto g_one = Botan::EC_AffinePoint::g_mul(one, rng, ws);
28✔
677
            result.test_eq("g*one == generator", g_one.serialize_uncompressed(), g_bytes);
56✔
678

679
            const auto g_plus_id = g_one.add(id);
28✔
680
            result.test_eq("g + id == g", g_plus_id.serialize_uncompressed(), g_bytes);
56✔
681

682
            const auto id_plus_g = id.add(g_one);
28✔
683
            result.test_eq("id + g == g", id_plus_g.serialize_uncompressed(), g_bytes);
56✔
684

685
            const auto g_neg_one = Botan::EC_AffinePoint::g_mul(one.negate(), rng, ws);
28✔
686

687
            const auto id_from_g = g_one.add(g_neg_one);
28✔
688
            result.confirm("g - g is identity", id_from_g.is_identity());
56✔
689

690
            const auto g_two = Botan::EC_AffinePoint::g_mul(one + one, rng, ws);
28✔
691
            const auto g_plus_g = g_one.add(g_one);
28✔
692
            result.test_eq("2*g == g+g", g_two.serialize_uncompressed(), g_plus_g.serialize_uncompressed());
84✔
693

694
            result.confirm("Scalar::zero is zero", zero.is_zero());
56✔
695
            result.confirm("(zero+zero) is zero", (zero + zero).is_zero());
56✔
696
            result.confirm("(zero*zero) is zero", (zero * zero).is_zero());
56✔
697
            result.confirm("(zero-zero) is zero", (zero - zero).is_zero());
56✔
698

699
            const auto neg_zero = zero.negate();
28✔
700
            result.confirm("zero.negate() is zero", neg_zero.is_zero());
56✔
701

702
            result.confirm("(zero+nz) is zero", (zero + neg_zero).is_zero());
56✔
703
            result.confirm("(nz+nz) is zero", (neg_zero + neg_zero).is_zero());
56✔
704
            result.confirm("(nz+zero) is zero", (neg_zero + zero).is_zero());
56✔
705

706
            result.confirm("Scalar::one is not zero", !one.is_zero());
56✔
707
            result.confirm("(one-one) is zero", (one - one).is_zero());
56✔
708
            result.confirm("(one+one.negate()) is zero", (one + one.negate()).is_zero());
84✔
709
            result.confirm("(one.negate()+one) is zero", (one.negate() + one).is_zero());
84✔
710

711
            for(size_t i = 0; i != 16; ++i) {
476✔
712
               const auto pt = Botan::EC_AffinePoint::g_mul(Botan::EC_Scalar::random(group, rng), rng, ws);
448✔
713

714
               const auto a = Botan::EC_Scalar::random(group, rng);
448✔
715
               const auto b = Botan::EC_Scalar::random(group, rng);
448✔
716
               const auto c = a + b;
448✔
717

718
               const auto Pa = pt.mul(a, rng, ws);
448✔
719
               const auto Pb = pt.mul(b, rng, ws);
448✔
720
               const auto Pc = pt.mul(c, rng, ws);
448✔
721

722
               const auto Pc_bytes = Pc.serialize_uncompressed();
448✔
723

724
               const auto Pab = Pa.add(Pb);
448✔
725
               result.test_eq("Pa + Pb == Pc", Pab.serialize_uncompressed(), Pc_bytes);
896✔
726

727
               const auto Pba = Pb.add(Pa);
448✔
728
               result.test_eq("Pb + Pa == Pc", Pba.serialize_uncompressed(), Pc_bytes);
896✔
729
            }
896✔
730

731
            for(size_t i = 0; i != 64; ++i) {
1,820✔
732
               auto h = [&]() {
5,376✔
733
                  const auto s = [&]() {
5,376✔
734
                     if(i == 0) {
1,792✔
735
                        // Test the identity case
736
                        return Botan::EC_Scalar(zero);
28✔
737
                     } else if(i <= 32) {
1,764✔
738
                        // Test cases where the two points have a linear relation
739
                        std::vector<uint8_t> sbytes(group.get_order_bytes());
896✔
740
                        sbytes[sbytes.size() - 1] = static_cast<uint8_t>((i + 1) / 2);
896✔
741
                        auto si = Botan::EC_Scalar::deserialize(group, sbytes).value();
1,792✔
742
                        if(i % 2 == 0) {
896✔
743
                           return si;
448✔
744
                        } else {
745
                           return si.negate();
448✔
746
                        }
747
                     } else {
1,792✔
748
                        return Botan::EC_Scalar::random(group, rng);
868✔
749
                     }
750
                  }();
1,792✔
751
                  auto x = Botan::EC_AffinePoint::g_mul(s, rng, ws);
1,792✔
752
                  return x;
1,792✔
753
               }();
3,584✔
754

755
               const auto s1 = Botan::EC_Scalar::random(group, rng);
1,792✔
756
               const auto s2 = Botan::EC_Scalar::random(group, rng);
1,792✔
757

758
               const Botan::EC_Group::Mul2Table mul2_table(h);
1,792✔
759

760
               const auto ref = Botan::EC_AffinePoint::g_mul(s1, rng, ws).add(h.mul(s2, rng, ws));
1,792✔
761

762
               if(auto mul2pt = mul2_table.mul2_vartime(s1, s2)) {
1,792✔
763
                  result.test_eq("ref == mul2t", ref.serialize_uncompressed(), mul2pt->serialize_uncompressed());
7,168✔
764
               } else {
765
                  result.confirm("ref is identity", ref.is_identity());
×
766
               }
×
767
            }
1,792✔
768

769
            result.end_timer();
28✔
770

771
            results.push_back(result);
28✔
772
         }
56✔
773

774
         return results;
1✔
775
      }
1✔
776
};
777

778
BOTAN_REGISTER_TEST("pubkey", "ec_point_arith", EC_Point_Arithmetic_Tests);
779

780
   #if defined(BOTAN_HAS_ECDSA)
781

782
class ECC_Invalid_Key_Tests final : public Text_Based_Test {
×
783
   public:
784
      ECC_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecc_invalid.vec", "SubjectPublicKey") {}
2✔
785

786
      bool clear_between_callbacks() const override { return false; }
5✔
787

788
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
5✔
789
         Test::Result result("ECC invalid keys");
5✔
790

791
         const std::string encoded = vars.get_req_str("SubjectPublicKey");
5✔
792
         Botan::DataSource_Memory key_data(Botan::hex_decode(encoded));
10✔
793

794
         try {
5✔
795
            auto key = Botan::X509::load_key(key_data);
5✔
796
            result.test_eq("public key fails check", key->check_key(this->rng(), false), false);
×
797
         } catch(Botan::Decoding_Error&) {
5✔
798
            result.test_success("Decoding invalid ECC key results in decoding error exception");
5✔
799
         }
5✔
800

801
         return result;
5✔
802
      }
5✔
803
};
804

805
BOTAN_REGISTER_TEST("pubkey", "ecc_invalid", ECC_Invalid_Key_Tests);
806

807
   #endif
808

809
#endif
810

811
}  // namespace
812

813
}  // namespace Botan_Tests
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