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

randombit / botan / 22038660242

15 Feb 2026 02:49PM UTC coverage: 90.057% (-1.6%) from 91.706%
22038660242

push

github

web-flow
Merge pull request #5340 from randombit/jack/test-h-misc-cmp

Further cleanups of test predicates

102369 of 113671 relevant lines covered (90.06%)

11342188.91 hits per line

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

93.43
/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/rng.h>
19
   #include <botan/x509_key.h>
20
   #include <botan/internal/barrett.h>
21
   #include <botan/internal/ec_inner_data.h>
22
#endif
23

24
namespace Botan_Tests {
25

26
namespace {
27

28
#if defined(BOTAN_HAS_ECC_GROUP)
29

30
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
31

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

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

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

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

60
      if(sample < prob) {
153,480✔
61
         active = !active;
4,974✔
62
      }
63
   }
64

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

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

79
   return x;
560✔
80
}
560✔
81

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

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

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

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

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

110
      result.start_timer();
28✔
111

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

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

116
      try {
28✔
117
         std::vector<Botan::BigInt> blind_ws;
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.xy_bytes(), R.xy_bytes());
560✔
136
            result.test_eq("q + p", A2.xy_bytes(), R.xy_bytes());
560✔
137

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

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

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

151
            P1.force_affine();
280✔
152
            Q1.force_affine();
280✔
153
            R1.force_affine();
280✔
154
            result.test_eq("P1", P1.xy_bytes(), P.xy_bytes());
560✔
155
            result.test_eq("Q1", Q1.xy_bytes(), Q.xy_bytes());
560✔
156
            result.test_eq("R1", R1.xy_bytes(), R.xy_bytes());
560✔
157
         }
280✔
158
      } catch(std::exception& e) {
28✔
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 {
1✔
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.test_is_true("EC_Group is known", group.get_curve_oid().has_value());
28✔
185
            result.test_is_true("EC_Group is considered valid", group.verify_group(this->rng(), true));
28✔
186
            result.test_is_true("EC_Group is not considered explicit encoding", !group.used_explicit_encoding());
28✔
187

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

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

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

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

198
            result.test_is_true("Same group is same", group == Botan::EC_Group::from_name(group_name));
28✔
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.test_is_true("Same group is same even with copy", group == copy);
20✔
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.test_is_true("EC_Group via OID is not considered explicit encoding",
28✔
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.test_is_true("EC_Group via explicit DER is considered explicit encoding",
28✔
220
                                group_via_explicit.used_explicit_encoding());
28✔
221

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

228
            if(group.a_is_zero()) {
28✔
229
               result.test_bn_eq("Group A is zero", group.get_a(), BigInt(0));
4✔
230
            } else {
231
               result.test_bn_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.test_is_true("Multiplying point by the order results in zero point", pt_mult_by_order.is_zero());
28✔
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_bn_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
28✔
248
            result.test_bn_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
28✔
249

250
            q.force_affine();
28✔
251

252
            result.test_bn_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
28✔
253
            result.test_bn_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
28✔
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,
84✔
278
                            Botan::EC_Point_Format::Compressed,
279
                            Botan::EC_Point_Format::Hybrid}) {
112✔
280
            try {
84✔
281
               result.test_eq("encoded/decode rt works", group.OS2ECP(pt.encode(scheme)).xy_bytes(), pt.xy_bytes());
336✔
282
            } catch(Botan::Exception& e) {
×
283
               result.test_failure("Failed to round trip encode a random point", e.what());
×
284
            }
×
285

286
            try {
84✔
287
               result.test_is_true("encoded/decode rt works", group.OS2ECP(zero.encode(scheme)).is_zero());
252✔
288
            } catch(Botan::Exception& e) {
×
289
               result.test_failure("Failed to round trip encode the identity element", e.what());
×
290
            }
×
291
         }
292
      }
28✔
293

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

297
         const auto G2 = G * 2;
56✔
298
         const auto G3 = G * 3;
56✔
299

300
         Botan::EC_Point p1 = G2;
28✔
301
         p1 += G;
28✔
302

303
         result.test_eq("point addition", p1.xy_bytes(), G3.xy_bytes());
56✔
304

305
         p1 -= G2;
28✔
306

307
         result.test_eq("point subtraction", p1.xy_bytes(), G.xy_bytes());
56✔
308

309
         // The scalar multiplication algorithm relies on this being true:
310
         try {
28✔
311
            const Botan::EC_Point zero_coords = group.point(0, 0);
56✔
312
            result.test_is_true("point (0,0) is not on the curve", !zero_coords.on_the_curve());
×
313
         } catch(Botan::Exception&) {
28✔
314
            result.test_success("point (0,0) is rejected");
28✔
315
         }
28✔
316
      }
28✔
317

318
      void test_point_swap(Test::Result& result, const Botan::EC_Group& group) {
28✔
319
         const Botan::EC_Point a(create_random_point(this->rng(), group));
28✔
320
         Botan::EC_Point b(create_random_point(this->rng(), group));
28✔
321
         b *= Botan::BigInt(this->rng(), 20);
28✔
322

323
         Botan::EC_Point c(a);
28✔
324
         Botan::EC_Point d(b);
28✔
325

326
         d.swap(c);
28✔
327
         result.test_eq("swap correct", a.xy_bytes(), d.xy_bytes());
56✔
328
         result.test_eq("swap correct", b.xy_bytes(), c.xy_bytes());
56✔
329
      }
28✔
330

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

334
         result.test_throws("Zero point throws", "Cannot convert zero point to affine", [&]() { zero.get_affine_x(); });
56✔
335
         result.test_throws("Zero point throws", "Cannot convert zero point to affine", [&]() { zero.get_affine_y(); });
56✔
336

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

339
         result.test_is_true("point is on the curve", p1.on_the_curve());
28✔
340
         result.test_is_true("point is not zero", !p1.is_zero());
28✔
341

342
         Botan::EC_Point p2 = p1;
28✔
343
         p2 -= p1;
28✔
344

345
         result.test_is_true("p - q with q = p results in zero", p2.is_zero());
28✔
346

347
         const Botan::EC_Point minus_p1 = -p1;
28✔
348
         result.test_is_true("point is on the curve", minus_p1.on_the_curve());
28✔
349
         const Botan::EC_Point shouldBeZero = p1 + minus_p1;
28✔
350
         result.test_is_true("point is on the curve", shouldBeZero.on_the_curve());
28✔
351
         result.test_is_true("point is zero", shouldBeZero.is_zero());
28✔
352

353
         result.test_bn_eq("minus point x", minus_p1.get_affine_x(), p1.get_affine_x());
28✔
354
         result.test_bn_eq("minus point y", minus_p1.get_affine_y(), group.get_p() - p1.get_affine_y());
28✔
355

356
         result.test_is_true("zero point is zero", zero.is_zero());
28✔
357
         result.test_is_true("zero point is on the curve", zero.on_the_curve());
28✔
358
         result.test_eq("addition of zero does nothing", p1.xy_bytes(), (p1 + zero).xy_bytes());
84✔
359
         result.test_eq("addition of zero does nothing", p1.xy_bytes(), (zero + p1).xy_bytes());
84✔
360
         result.test_eq("addition of zero does nothing", p1.xy_bytes(), (p1 - zero).xy_bytes());
84✔
361
         result.test_is_true("zero times anything is the zero point", (zero * 39193).is_zero());
56✔
362

363
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
196✔
364
                            Botan::EC_Point_Format::Compressed,
365
                            Botan::EC_Point_Format::Hybrid}) {
112✔
366
            const std::vector<uint8_t> v = zero.encode(scheme);
84✔
367
            result.test_is_true("encoded/decode rt works", group.OS2ECP(v).is_zero());
168✔
368
         }
84✔
369
      }
28✔
370
   #endif
371
};
372

373
BOTAN_REGISTER_TEST("pubkey", "ec_group", EC_Group_Tests);
374

375
Test::Result test_decoding_with_seed() {
1✔
376
   Test::Result result("Decode EC_Group with seed");
1✔
377

378
   try {
1✔
379
      if(Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
380
         const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
381
         const auto secp384r1_with_seed =
1✔
382
            Botan::EC_Group::from_PEM(Test::read_data_file("x509/ecc/secp384r1_seed.pem"));
2✔
383
         result.test_is_true("decoding worked", secp384r1_with_seed.initialized());
1✔
384
         result.test_bn_eq("P-384 prime", secp384r1_with_seed.get_p(), secp384r1.get_p());
1✔
385
      }
1✔
386
   } catch(Botan::Exception& e) {
×
387
      result.test_failure(e.what());
×
388
   }
×
389

390
   return result;
1✔
391
}
×
392

393
Test::Result test_mixed_points() {
1✔
394
   Test::Result result("Mixed Point Arithmetic");
1✔
395

396
   if(Botan::EC_Group::supports_named_group("secp256r1") && Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
397
      const auto secp256r1 = Botan::EC_Group::from_name("secp256r1");
1✔
398
      const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
399

400
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
401
      const Botan::EC_Point& G256 = secp256r1.get_base_point();
1✔
402
      const Botan::EC_Point& G384 = secp384r1.get_base_point();
1✔
403

404
      result.test_throws("Mixing points from different groups", [&] { const Botan::EC_Point p = G256 + G384; });
2✔
405
   #endif
406

407
      const auto p1 = Botan::EC_AffinePoint::generator(secp256r1);
1✔
408
      const auto p2 = Botan::EC_AffinePoint::generator(secp384r1);
1✔
409
      result.test_throws("Mixing points from different groups", [&] { auto p3 = p1.add(p2); });
2✔
410
   }
1✔
411

412
   return result;
1✔
413
}
×
414

415
class ECC_Unit_Tests final : public Test {
1✔
416
   public:
417
      std::vector<Test::Result> run() override {
1✔
418
         std::vector<Test::Result> results;
1✔
419

420
         results.push_back(test_decoding_with_seed());
2✔
421
         results.push_back(test_mixed_points());
2✔
422

423
         return results;
1✔
424
      }
×
425
};
426

427
BOTAN_REGISTER_TEST("pubkey", "ecc_unit", ECC_Unit_Tests);
428

429
class EC_Group_Registration_Tests final : public Test {
1✔
430
   public:
431
      std::vector<Test::Result> run() override {
1✔
432
         std::vector<Test::Result> results;
1✔
433

434
         if(Botan::EC_Group::supports_application_specific_group()) {
1✔
435
            results.push_back(test_ecc_registration());
2✔
436
            results.push_back(test_ec_group_from_params());
2✔
437
            results.push_back(test_ec_group_bad_registration());
2✔
438
            results.push_back(test_ec_group_duplicate_orders());
2✔
439
            results.push_back(test_ec_group_registration_with_custom_oid());
2✔
440
         }
441

442
         return results;
1✔
443
      }
×
444

445
   private:
446
      Test::Result test_ecc_registration() {
1✔
447
         Test::Result result("ECC registration");
1✔
448

449
         // numsp256d1
450
         const Botan::BigInt p("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43");
1✔
451
         const Botan::BigInt a("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40");
1✔
452
         const Botan::BigInt b("0x25581");
1✔
453
         const Botan::BigInt order("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE43C8275EA265C6020AB20294751A825");
1✔
454

455
         const Botan::BigInt g_x("0x01");
1✔
456
         const Botan::BigInt g_y("0x696F1853C1E466D7FC82C96CCEEEDD6BD02C2F9375894EC10BF46306C2B56C77");
1✔
457

458
         const Botan::OID oid("1.3.6.1.4.1.25258.4.1");
1✔
459

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

463
         auto group = Botan::EC_Group::from_OID(oid);
1✔
464

465
         result.test_bn_eq("Group registration worked", group.get_p(), p);
1✔
466

467
         // TODO(Botan4) this could change to == Generic
468
         result.test_is_true("Group is not pcurve", group.engine() != Botan::EC_Group_Engine::Optimized);
1✔
469

470
         return result;
1✔
471
      }
1✔
472

473
      Test::Result test_ec_group_from_params() {
1✔
474
         Test::Result result("EC_Group from params");
1✔
475

476
         Botan::EC_Group::clear_registered_curve_data();
1✔
477

478
         // secp256r1
479
         const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
480
         const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
481
         const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
482

483
         const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
484
         const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
485
         const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
486

487
         const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
488

489
         // This uses the deprecated constructor to verify we dedup even without an OID
490
         // This whole test can be removed once explicit curve support is removed
491
         const Botan::EC_Group reg_group(p, a, b, g_x, g_y, order, 1);
1✔
492
         result.test_is_true("Group has correct OID", reg_group.get_curve_oid() == oid);
1✔
493

494
         return result;
1✔
495
      }
1✔
496

497
      Test::Result test_ec_group_bad_registration() {
1✔
498
         Test::Result result("EC_Group registering non-match");
1✔
499

500
         Botan::EC_Group::clear_registered_curve_data();
1✔
501

502
         // secp256r1 params except with a bad B param
503
         const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
504
         const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
505
         const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604C");
1✔
506

507
         const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
508
         const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
509
         const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
510

511
         const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
512

513
         try {
1✔
514
            const Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
515
            result.test_failure("Should have failed");
×
516
         } catch(Botan::Invalid_Argument&) {
1✔
517
            result.test_success("Got expected exception");
1✔
518
         }
1✔
519

520
         return result;
2✔
521
      }
1✔
522

523
      Test::Result test_ec_group_duplicate_orders() {
1✔
524
         Test::Result result("EC_Group with duplicate group order");
1✔
525

526
         Botan::EC_Group::clear_registered_curve_data();
1✔
527

528
         // secp256r1
529
         const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
530
         const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
531
         const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
532

533
         const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
534
         const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
535
         const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
536

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

539
         const Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
540
         result.test_success("Registration success");
1✔
541
         result.test_is_true("Group has correct OID", reg_group.get_curve_oid() == oid);
1✔
542

543
         // We can now get it by OID:
544
         const auto hc_group = Botan::EC_Group::from_OID(oid);
1✔
545
         result.test_is_true("Group has correct OID", hc_group.get_curve_oid() == oid);
1✔
546

547
         // Existing secp256r1 unmodified:
548
         const Botan::OID secp160r1("1.2.840.10045.3.1.7");
1✔
549
         const auto other_group = Botan::EC_Group::from_OID(secp160r1);
1✔
550
         result.test_is_true("Group has correct OID", other_group.get_curve_oid() == secp160r1);
1✔
551

552
         return result;
1✔
553
      }
1✔
554

555
      Test::Result test_ec_group_registration_with_custom_oid() {
1✔
556
         Test::Result result("EC_Group registration of standard group with custom OID");
1✔
557

558
         Botan::EC_Group::clear_registered_curve_data();
1✔
559

560
         const Botan::OID secp256r1_oid("1.2.840.10045.3.1.7");
1✔
561
         const auto secp256r1 = Botan::EC_Group::from_OID(secp256r1_oid);
1✔
562
         result.test_is_true("Group has correct OID", secp256r1.get_curve_oid() == secp256r1_oid);
1✔
563

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

566
         Botan::OID::register_oid(custom_oid, "secp256r1");
1✔
567

568
         const Botan::EC_Group reg_group(custom_oid,
1✔
569
                                         secp256r1.get_p(),
570
                                         secp256r1.get_a(),
571
                                         secp256r1.get_b(),
572
                                         secp256r1.get_g_x(),
573
                                         secp256r1.get_g_y(),
574
                                         secp256r1.get_order());
1✔
575

576
         result.test_success("Registration success");
1✔
577
         result.test_is_true("Group has correct OID", reg_group.get_curve_oid() == custom_oid);
1✔
578

579
         // We can now get it by OID:
580
         result.test_is_true("Group has correct OID",
1✔
581
                             Botan::EC_Group::from_OID(custom_oid).get_curve_oid() == custom_oid);
1✔
582

583
         // In the current data model of EC_Group there is a 1:1 OID:group, so these
584
         // have distinct underlying data
585
         result.test_is_true("Groups have different inner data pointers", reg_group._data() != secp256r1._data());
1✔
586

587
   #if defined(BOTAN_HAS_PCURVES_SECP256R1)
588
         // However we should have gotten a pcurves out of the deal *and* it
589
         // should be the exact same shared_ptr as the official curve
590

591
         result.test_is_true("Group is pcurves based", reg_group.engine() == Botan::EC_Group_Engine::Optimized);
1✔
592

593
         try {
1✔
594
            const auto& pcurve = reg_group._data()->pcurve();
1✔
595
            result.test_is_true("Group with custom OID got the same pcurve pointer",
1✔
596
                                &pcurve == &secp256r1._data()->pcurve());
1✔
597
         } catch(...) {
×
598
            result.test_failure("Group with custom OID did not get a pcurve pointer");
×
599
         }
×
600
   #endif
601

602
         return result;
2✔
603
      }
1✔
604
};
605

606
BOTAN_REGISTER_SERIALIZED_TEST("pubkey", "ec_group_registration", EC_Group_Registration_Tests);
607

608
class EC_PointEnc_Tests final : public Test {
1✔
609
   public:
610
      std::vector<Test::Result> run() override {
1✔
611
         std::vector<Test::Result> results;
1✔
612

613
         auto& rng = Test::rng();
1✔
614

615
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
616
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
617

618
            Result result("EC_AffinePoint encoding " + group_id);
28✔
619

620
            result.start_timer();
28✔
621

622
            for(size_t trial = 0; trial != 100; ++trial) {
2,828✔
623
               const auto scalar = Botan::EC_Scalar::random(group, rng);
2,800✔
624
               const auto pt = Botan::EC_AffinePoint::g_mul(scalar, rng);
2,800✔
625

626
               const auto pt_u = pt.serialize_uncompressed();
2,800✔
627
               result.test_u8_eq("Expected uncompressed header", pt_u[0], 0x04);
2,800✔
628
               const size_t fe_bytes = (pt_u.size() - 1) / 2;
2,800✔
629
               const auto pt_c = pt.serialize_compressed();
2,800✔
630

631
               result.test_sz_eq("Expected compressed size", pt_c.size(), 1 + fe_bytes);
2,800✔
632
               const uint8_t expected_c_header = (pt_u[pt_u.size() - 1] % 2 == 0) ? 0x02 : 0x03;
2,800✔
633
               result.test_is_true("Expected compressed header", pt_c[0] == expected_c_header);
2,800✔
634

635
               result.test_eq(
2,800✔
636
                  "Expected compressed x", std::span{pt_c}.subspan(1), std::span{pt_u}.subspan(1, fe_bytes));
637

638
               if(auto d_pt_u = Botan::EC_AffinePoint::deserialize(group, pt_u)) {
2,800✔
639
                  result.test_eq(
2,800✔
640
                     "Deserializing uncompressed returned correct point", d_pt_u->serialize_uncompressed(), pt_u);
5,600✔
641
               } else {
642
                  result.test_failure("Failed to deserialize uncompressed point");
×
643
               }
×
644

645
               if(auto d_pt_c = Botan::EC_AffinePoint::deserialize(group, pt_c)) {
2,800✔
646
                  result.test_eq(
2,800✔
647
                     "Deserializing compressed returned correct point", d_pt_c->serialize_uncompressed(), pt_u);
5,600✔
648
               } else {
649
                  result.test_failure("Failed to deserialize compressed point");
×
650
               }
×
651

652
               const auto neg_pt_c = [&]() {
2,800✔
653
                  auto x = pt_c;
2,800✔
654
                  x[0] ^= 0x01;
2,800✔
655
                  return x;
2,800✔
656
               }();
2,800✔
657

658
               if(auto d_neg_pt_c = Botan::EC_AffinePoint::deserialize(group, neg_pt_c)) {
2,800✔
659
                  result.test_eq("Deserializing compressed with inverted header returned negated point",
2,800✔
660
                                 d_neg_pt_c->serialize_uncompressed(),
5,600✔
661
                                 pt.negate().serialize_uncompressed());
5,600✔
662
               } else {
663
                  result.test_failure("Failed to deserialize compressed point");
×
664
               }
2,800✔
665
            }
2,800✔
666

667
            result.end_timer();
28✔
668

669
            results.push_back(result);
28✔
670
         }
28✔
671

672
         return results;
1✔
673
      }
2,800✔
674
};
675

676
BOTAN_REGISTER_TEST("pubkey", "ec_point_enc", EC_PointEnc_Tests);
677

678
class EC_Point_Arithmetic_Tests final : public Test {
1✔
679
   public:
680
      std::vector<Test::Result> run() override {
1✔
681
         std::vector<Test::Result> results;
1✔
682

683
         auto& rng = Test::rng();
1✔
684

685
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
686
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
687

688
            Result result("EC_AffinePoint arithmetic " + group_id);
28✔
689

690
            result.start_timer();
28✔
691

692
            const auto one = Botan::EC_Scalar::one(group);
28✔
693
            const auto zero = one - one;  // NOLINT(*-redundant-expression)
28✔
694
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
695
            const auto g_bytes = g.serialize_uncompressed();
28✔
696

697
            const auto id = Botan::EC_AffinePoint::g_mul(zero, rng);
28✔
698
            result.test_is_true("g*zero is point at identity", id.is_identity());
28✔
699

700
            const auto id2 = id.add(id);
28✔
701
            result.test_is_true("identity plus itself is identity", id2.is_identity());
28✔
702

703
            const auto g_one = Botan::EC_AffinePoint::g_mul(one, rng);
28✔
704
            result.test_eq("g*one == generator", g_one.serialize_uncompressed(), g_bytes);
28✔
705

706
            const auto g_plus_id = g_one.add(id);
28✔
707
            result.test_eq("g + id == g", g_plus_id.serialize_uncompressed(), g_bytes);
28✔
708

709
            const auto id_plus_g = id.add(g_one);
28✔
710
            result.test_eq("id + g == g", id_plus_g.serialize_uncompressed(), g_bytes);
28✔
711

712
            const auto g_neg_one = Botan::EC_AffinePoint::g_mul(one.negate(), rng);
28✔
713

714
            const auto id_from_g = g_one.add(g_neg_one);
28✔
715
            result.test_is_true("g - g is identity", id_from_g.is_identity());
28✔
716

717
            const auto g_two = Botan::EC_AffinePoint::g_mul(one + one, rng);
28✔
718
            const auto g_plus_g = g_one.add(g_one);
28✔
719
            result.test_eq("2*g == g+g", g_two.serialize_uncompressed(), g_plus_g.serialize_uncompressed());
56✔
720

721
            result.test_is_true("Scalar::zero is zero", zero.is_zero());
28✔
722
            result.test_is_true("(zero+zero) is zero", (zero + zero).is_zero());
28✔
723
            result.test_is_true("(zero*zero) is zero", (zero * zero).is_zero());
28✔
724
            result.test_is_true("(zero-zero) is zero", (zero - zero).is_zero());  // NOLINT(*-redundant-expression)
28✔
725

726
            const auto neg_zero = zero.negate();
28✔
727
            result.test_is_true("zero.negate() is zero", neg_zero.is_zero());
28✔
728

729
            result.test_is_true("(zero+nz) is zero", (zero + neg_zero).is_zero());
28✔
730
            result.test_is_true("(nz+nz) is zero", (neg_zero + neg_zero).is_zero());
28✔
731
            result.test_is_true("(nz+zero) is zero", (neg_zero + zero).is_zero());
28✔
732

733
            result.test_is_true("Scalar::one is not zero", !one.is_zero());
28✔
734
            result.test_is_true("(one-one) is zero", (one - one).is_zero());  // NOLINT(*-redundant-expression)
28✔
735
            result.test_is_true("(one+one.negate()) is zero", (one + one.negate()).is_zero());
56✔
736
            result.test_is_true("(one.negate()+one) is zero", (one.negate() + one).is_zero());
56✔
737

738
            for(size_t i = 0; i != 16; ++i) {
476✔
739
               const auto pt = Botan::EC_AffinePoint::g_mul(Botan::EC_Scalar::random(group, rng), rng);
448✔
740

741
               const auto a = Botan::EC_Scalar::random(group, rng);
448✔
742
               const auto b = Botan::EC_Scalar::random(group, rng);
448✔
743
               const auto c = a + b;
448✔
744

745
               const auto Pa = pt.mul(a, rng);
448✔
746
               const auto Pb = pt.mul(b, rng);
448✔
747
               const auto Pc = pt.mul(c, rng);
448✔
748

749
               const auto Pc_bytes = Pc.serialize_uncompressed();
448✔
750

751
               const auto Pab = Pa.add(Pb);
448✔
752
               result.test_eq("Pa + Pb == Pc", Pab.serialize_uncompressed(), Pc_bytes);
448✔
753

754
               const auto Pba = Pb.add(Pa);
448✔
755
               result.test_eq("Pb + Pa == Pc", Pba.serialize_uncompressed(), Pc_bytes);
448✔
756
            }
896✔
757

758
            for(size_t i = 0; i != 64; ++i) {
1,820✔
759
               auto h = [&]() {
5,376✔
760
                  const auto s = [&]() {
5,376✔
761
                     if(i == 0) {
1,792✔
762
                        // Test the identity case
763
                        return Botan::EC_Scalar(zero);
28✔
764
                     } else if(i <= 32) {
1,764✔
765
                        // Test cases where the two points have a linear relation
766
                        std::vector<uint8_t> sbytes(group.get_order_bytes());
896✔
767
                        sbytes[sbytes.size() - 1] = static_cast<uint8_t>((i + 1) / 2);
896✔
768
                        auto si = Botan::EC_Scalar::deserialize(group, sbytes).value();
1,792✔
769
                        if(i % 2 == 0) {
896✔
770
                           return si;
448✔
771
                        } else {
772
                           return si.negate();
448✔
773
                        }
774
                     } else {
1,792✔
775
                        return Botan::EC_Scalar::random(group, rng);
868✔
776
                     }
777
                  }();
1,792✔
778
                  auto x = Botan::EC_AffinePoint::g_mul(s, rng);
1,792✔
779
                  return x;
1,792✔
780
               }();
3,584✔
781

782
               const auto s1 = Botan::EC_Scalar::random(group, rng);
1,792✔
783
               const auto s2 = Botan::EC_Scalar::random(group, rng);
1,792✔
784

785
               const Botan::EC_Group::Mul2Table mul2_table(h);
1,792✔
786

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

789
               if(auto mul2pt = mul2_table.mul2_vartime(s1, s2)) {
1,792✔
790
                  result.test_eq("ref == mul2t", ref.serialize_uncompressed(), mul2pt->serialize_uncompressed());
5,376✔
791
               } else {
792
                  result.test_is_true("ref is identity", ref.is_identity());
×
793
               }
×
794
            }
1,792✔
795

796
            result.end_timer();
28✔
797

798
            results.push_back(result);
28✔
799
         }
56✔
800

801
         return results;
1✔
802
      }
×
803
};
804

805
BOTAN_REGISTER_TEST("pubkey", "ec_point_arith", EC_Point_Arithmetic_Tests);
806

807
   #if defined(BOTAN_HAS_ECDSA)
808

809
class ECC_Invalid_Key_Tests final : public Text_Based_Test {
×
810
   public:
811
      ECC_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecc_invalid.vec", "SubjectPublicKey") {}
2✔
812

813
      bool clear_between_callbacks() const override { return false; }
5✔
814

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

818
         const std::string encoded = vars.get_req_str("SubjectPublicKey");
5✔
819
         Botan::DataSource_Memory key_data(Botan::hex_decode(encoded));
10✔
820

821
         try {
5✔
822
            auto key = Botan::X509::load_key(key_data);
5✔
823
            result.test_is_false("public key fails check", key->check_key(this->rng(), false));
×
824
         } catch(Botan::Decoding_Error&) {
5✔
825
            result.test_success("Decoding invalid ECC key results in decoding error exception");
5✔
826
         }
5✔
827

828
         return result;
5✔
829
      }
5✔
830
};
831

832
BOTAN_REGISTER_TEST("pubkey", "ecc_invalid", ECC_Invalid_Key_Tests);
833

834
   #endif
835

836
#endif
837

838
}  // namespace
839

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