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

randombit / botan / 12777311287

14 Jan 2025 10:12PM UTC coverage: 91.203% (-0.04%) from 91.247%
12777311287

push

github

web-flow
Merge pull request #4549 from randombit/jack/pcurves-test-cleanup

Remove the pcurves specific tests

93416 of 102426 relevant lines covered (91.2%)

11527204.15 hits per line

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

95.7
/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
#endif
21

22
namespace Botan_Tests {
23

24
namespace {
25

26
#if defined(BOTAN_HAS_ECC_GROUP)
27

28
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
29

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

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

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

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

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

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

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

77
   return x;
560✔
78
}
560✔
79

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

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

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

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

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

108
      result.start_timer();
28✔
109

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

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

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

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

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

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

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

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

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

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

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

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

164
   return results;
1✔
165
}
×
166

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

169
   #endif
170

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

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

179
            result.start_timer();
28✔
180

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

249
            q.force_affine();
28✔
250

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

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

260
            result.end_timer();
28✔
261

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

265
         return results;
1✔
266
      }
×
267

268
   private:
269
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
270

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

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

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

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

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

292
         p1 -= G * 2;
56✔
293

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

362
Test::Result test_decoding_with_seed() {
1✔
363
   Test::Result result("ECC Unit");
1✔
364

365
   const auto secp384r1_with_seed = Botan::EC_Group::from_PEM(Test::read_data_file("x509/ecc/secp384r1_seed.pem"));
2✔
366

367
   result.confirm("decoding worked", secp384r1_with_seed.initialized());
2✔
368

369
   const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
370

371
   result.test_eq("P-384 prime", secp384r1_with_seed.get_p(), secp384r1.get_p());
1✔
372

373
   return result;
1✔
374
}
1✔
375

376
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
377
Test::Result test_mixed_points() {
1✔
378
   Test::Result result("ECC Unit");
1✔
379

380
   const auto secp256r1 = Botan::EC_Group::from_name("secp256r1");
1✔
381
   const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
382

383
   const Botan::EC_Point& G256 = secp256r1.get_base_point();
1✔
384
   const Botan::EC_Point& G384 = secp384r1.get_base_point();
1✔
385

386
   result.test_throws("Mixing points from different groups", [&] { Botan::EC_Point p = G256 + G384; });
3✔
387
   return result;
1✔
388
}
1✔
389

390
Test::Result test_basic_operations() {
1✔
391
   Test::Result result("ECC Unit");
1✔
392

393
   // precalculation
394
   const auto secp160r1 = Botan::EC_Group::from_name("secp160r1");
1✔
395
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
396

397
   const Botan::EC_Point& p0 = p_G;
1✔
398
   const Botan::EC_Point p1 = p_G * 2;
2✔
399

400
   result.test_eq("p1 affine x", p1.get_affine_x(), Botan::BigInt("16984103820118642236896513183038186009872590470"));
3✔
401
   result.test_eq("p1 affine y", p1.get_affine_y(), Botan::BigInt("1373093393927139016463695321221277758035357890939"));
3✔
402

403
   const Botan::EC_Point simplePlus = p1 + p0;
1✔
404
   const Botan::EC_Point exp_simplePlus =
1✔
405
      secp160r1.point(Botan::BigInt("704859595002530890444080436569091156047721708633"),
2✔
406
                      Botan::BigInt("1147993098458695153857594941635310323215433166682"));
2✔
407

408
   result.test_eq("point addition", simplePlus, exp_simplePlus);
1✔
409

410
   const Botan::EC_Point simpleMinus = p1 - p0;
1✔
411
   result.test_eq("point subtraction", simpleMinus, p_G);
1✔
412

413
   const Botan::EC_Point simpleMult = p1 * 123456789;
2✔
414

415
   result.test_eq("point mult affine x",
3✔
416
                  simpleMult.get_affine_x(),
2✔
417
                  Botan::BigInt("43638877777452195295055270548491599621118743290"));
1✔
418
   result.test_eq("point mult affine y",
3✔
419
                  simpleMult.get_affine_y(),
2✔
420
                  Botan::BigInt("56841378500012376527163928510402662349220202981"));
1✔
421

422
   return result;
1✔
423
}
1✔
424

425
Test::Result test_enc_dec_compressed_160() {
1✔
426
   Test::Result result("ECC Unit");
1✔
427

428
   // Test for compressed conversion (02/03) 160bit
429
   const auto secp160r1 = Botan::EC_Group::from_name("secp160r1");
1✔
430
   const std::vector<uint8_t> G_comp = Botan::hex_decode("024A96B5688EF573284664698968C38BB913CBFC82");
1✔
431
   const Botan::EC_Point p = secp160r1.OS2ECP(G_comp);
1✔
432
   const std::vector<uint8_t> sv_result = p.encode(Botan::EC_Point_Format::Compressed);
1✔
433

434
   result.test_eq("result", sv_result, G_comp);
1✔
435
   return result;
2✔
436
}
2✔
437

438
Test::Result test_enc_dec_compressed_256() {
1✔
439
   Test::Result result("ECC Unit");
1✔
440

441
   const auto group = Botan::EC_Group::from_name("secp256r1");
1✔
442

443
   const std::string G_secp_comp = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
1✔
444
   const std::vector<uint8_t> sv_G_secp_comp = Botan::hex_decode(G_secp_comp);
1✔
445

446
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_comp);
1✔
447
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Compressed);
1✔
448

449
   result.test_eq("compressed_256", sv_result, sv_G_secp_comp);
1✔
450
   return result;
2✔
451
}
2✔
452

453
Test::Result test_enc_dec_uncompressed_112() {
1✔
454
   Test::Result result("ECC Unit");
1✔
455

456
   // Test for uncompressed conversion (04) 112bit
457

458
   // Curve is secp112r2
459

460
   const Botan::BigInt p("0xdb7c2abf62e35e668076bead208b");
1✔
461
   const Botan::BigInt a("0x6127C24C05F38A0AAAF65C0EF02C");
1✔
462
   const Botan::BigInt b("0x51DEF1815DB5ED74FCC34C85D709");
1✔
463

464
   const Botan::BigInt g_x("0x4BA30AB5E892B4E1649DD0928643");
1✔
465
   const Botan::BigInt g_y("0xADCD46F5882E3747DEF36E956E97");
1✔
466

467
   const Botan::BigInt order("0x36DF0AAFD8B8D7597CA10520D04B");
1✔
468
   const Botan::BigInt cofactor("4");  // !
1✔
469

470
   // This uses the deprecated constructor due to making use of cofactor > 1
471
   const Botan::EC_Group group(p, a, b, g_x, g_y, order, cofactor);
1✔
472

473
   const std::string G_secp_uncomp = "044BA30AB5E892B4E1649DD0928643ADCD46F5882E3747DEF36E956E97";
1✔
474
   const std::vector<uint8_t> sv_G_secp_uncomp = Botan::hex_decode(G_secp_uncomp);
1✔
475

476
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_uncomp);
1✔
477
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Uncompressed);
1✔
478

479
   result.test_eq("uncompressed_112", sv_result, sv_G_secp_uncomp);
1✔
480

481
   return result;
1✔
482
}
9✔
483

484
Test::Result test_enc_dec_uncompressed_521() {
1✔
485
   Test::Result result("ECC Unit");
1✔
486

487
   // Test for uncompressed conversion(04) with big values(521 bit)
488

489
   const std::string G_secp_uncomp =
1✔
490
      "0400C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2ffA8DE3348B3C1856A429BF97E7E31C2E5BD66011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650";
1✔
491

492
   const std::vector<uint8_t> sv_G_secp_uncomp = Botan::hex_decode(G_secp_uncomp);
1✔
493

494
   const auto group = Botan::EC_Group::from_name("secp521r1");
1✔
495

496
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_uncomp);
1✔
497

498
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Uncompressed);
1✔
499

500
   result.test_eq("expected", sv_result, sv_G_secp_uncomp);
1✔
501
   return result;
1✔
502
}
2✔
503
   #endif
504

505
Test::Result test_ecc_registration() {
1✔
506
   Test::Result result("ECC registration");
1✔
507

508
   // numsp256d1
509
   const Botan::BigInt p("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43");
1✔
510
   const Botan::BigInt a("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40");
1✔
511
   const Botan::BigInt b("0x25581");
1✔
512
   const Botan::BigInt order("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE43C8275EA265C6020AB20294751A825");
1✔
513

514
   const Botan::BigInt g_x("0x01");
1✔
515
   const Botan::BigInt g_y("0x696F1853C1E466D7FC82C96CCEEEDD6BD02C2F9375894EC10BF46306C2B56C77");
1✔
516

517
   const Botan::OID oid("1.3.6.1.4.1.25258.4.1");
1✔
518

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

522
   auto group = Botan::EC_Group::from_OID(oid);
1✔
523

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

526
   return result;
2✔
527
}
7✔
528

529
Test::Result test_ec_group_from_params() {
1✔
530
   Test::Result result("EC_Group from params");
1✔
531

532
   Botan::EC_Group::clear_registered_curve_data();
1✔
533

534
   // secp256r1
535
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
536
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
537
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
538

539
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
540
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
541
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
542

543
   const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
544

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

550
   return result;
2✔
551
}
7✔
552

553
Test::Result test_ec_group_bad_registration() {
1✔
554
   Test::Result result("EC_Group registering non-match");
1✔
555

556
   Botan::EC_Group::clear_registered_curve_data();
1✔
557

558
   // secp256r1 params except with a bad B param
559
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
560
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
561
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604C");
1✔
562

563
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
564
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
565
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
566

567
   const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
568

569
   try {
1✔
570
      Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
571
      result.test_failure("Should have failed");
×
572
   } catch(Botan::Invalid_Argument&) {
1✔
573
      result.test_success("Got expected exception");
1✔
574
   }
1✔
575

576
   return result;
1✔
577
}
7✔
578

579
Test::Result test_ec_group_duplicate_orders() {
1✔
580
   Test::Result result("EC_Group with duplicate group order");
1✔
581

582
   Botan::EC_Group::clear_registered_curve_data();
1✔
583

584
   // secp256r1
585
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
586
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
587
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
588

589
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
590
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
591
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
592

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

595
   Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
596
   result.test_success("Registration success");
1✔
597
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == oid);
2✔
598

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

603
   // Existing secp256r1 unmodified:
604
   const Botan::OID secp160r1("1.2.840.10045.3.1.7");
1✔
605
   const auto other_group = Botan::EC_Group::from_OID(secp160r1);
1✔
606
   result.confirm("Group has correct OID", other_group.get_curve_oid() == secp160r1);
2✔
607

608
   return result;
2✔
609
}
7✔
610

611
class ECC_Unit_Tests final : public Test {
×
612
   public:
613
      std::vector<Test::Result> run() override {
1✔
614
         std::vector<Test::Result> results;
1✔
615

616
         results.push_back(test_decoding_with_seed());
2✔
617

618
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
619
         results.push_back(test_mixed_points());
2✔
620
         results.push_back(test_basic_operations());
2✔
621
         results.push_back(test_enc_dec_compressed_160());
2✔
622
         results.push_back(test_enc_dec_compressed_256());
2✔
623
         results.push_back(test_enc_dec_uncompressed_112());
2✔
624
         results.push_back(test_enc_dec_uncompressed_521());
2✔
625
   #endif
626

627
         results.push_back(test_ecc_registration());
2✔
628
         results.push_back(test_ec_group_from_params());
2✔
629
         results.push_back(test_ec_group_bad_registration());
2✔
630
         results.push_back(test_ec_group_duplicate_orders());
2✔
631

632
         return results;
1✔
633
      }
×
634
};
635

636
BOTAN_REGISTER_SERIALIZED_TEST("pubkey", "ecc_unit", ECC_Unit_Tests);
637

638
class EC_PointEnc_Tests final : public Test {
×
639
   public:
640
      std::vector<Test::Result> run() override {
1✔
641
         std::vector<Test::Result> results;
1✔
642

643
         auto& rng = Test::rng();
1✔
644

645
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
646
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
647

648
            Result result("EC_AffinePoint encoding " + group_id);
28✔
649

650
            result.start_timer();
28✔
651

652
            std::vector<Botan::BigInt> ws;
28✔
653

654
            for(size_t trial = 0; trial != 100; ++trial) {
2,828✔
655
               const auto scalar = Botan::EC_Scalar::random(group, rng);
2,800✔
656
               const auto pt = Botan::EC_AffinePoint::g_mul(scalar, rng, ws);
2,800✔
657

658
               const auto pt_u = pt.serialize_uncompressed();
2,800✔
659
               result.test_eq("Expected uncompressed header", static_cast<size_t>(pt_u[0]), 0x04);
2,800✔
660
               const size_t fe_bytes = (pt_u.size() - 1) / 2;
2,800✔
661
               const auto pt_c = pt.serialize_compressed();
2,800✔
662

663
               result.test_eq("Expected compressed size", pt_c.size(), 1 + fe_bytes);
2,800✔
664
               result.confirm("Expected compressed header", pt_c[0] == 0x02 || pt_c[0] == 0x03);
8,400✔
665

666
               if(auto d_pt_u = Botan::EC_AffinePoint::deserialize(group, pt_u)) {
2,800✔
667
                  result.test_eq(
5,600✔
668
                     "Deserializing uncompressed returned correct point", d_pt_u->serialize_uncompressed(), pt_u);
5,600✔
669
               } else {
670
                  result.test_failure("Failed to deserialize uncompressed point");
×
671
               }
×
672

673
               if(auto d_pt_c = Botan::EC_AffinePoint::deserialize(group, pt_c)) {
2,800✔
674
                  result.test_eq(
5,600✔
675
                     "Deserializing compressed returned correct point", d_pt_c->serialize_uncompressed(), pt_u);
5,600✔
676
               } else {
677
                  result.test_failure("Failed to deserialize compressed point");
×
678
               }
2,800✔
679
            }
2,800✔
680

681
            result.end_timer();
28✔
682

683
            results.push_back(result);
28✔
684
         }
28✔
685

686
         return results;
1✔
687
      }
×
688
};
689

690
BOTAN_REGISTER_TEST("pcurves", "ec_point_enc", EC_PointEnc_Tests);
691

692
class EC_Point_Arithmetic_Tests final : public Test {
×
693
   public:
694
      std::vector<Test::Result> run() override {
1✔
695
         std::vector<Test::Result> results;
1✔
696

697
         auto& rng = Test::rng();
1✔
698

699
         std::vector<Botan::BigInt> ws;
1✔
700

701
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
702
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
703

704
            Result result("EC_AffinePoint arithmetic " + group_id);
28✔
705

706
            result.start_timer();
28✔
707

708
            const auto one = Botan::EC_Scalar::one(group);
28✔
709
            const auto zero = one - one;
28✔
710
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
711
            const auto g_bytes = g.serialize_uncompressed();
28✔
712

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

716
            const auto id2 = id.add(id);
28✔
717
            result.confirm("identity plus itself is identity", id2.is_identity());
56✔
718

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

722
            const auto g_plus_id = g_one.add(id);
28✔
723
            result.test_eq("g + id == g", g_plus_id.serialize_uncompressed(), g_bytes);
56✔
724

725
            const auto id_plus_g = id.add(g_one);
28✔
726
            result.test_eq("id + g == g", id_plus_g.serialize_uncompressed(), g_bytes);
56✔
727

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

730
            const auto id_from_g = g_one.add(g_neg_one);
28✔
731
            result.confirm("g - g is identity", id_from_g.is_identity());
56✔
732

733
            const auto g_two = Botan::EC_AffinePoint::g_mul(one + one, rng, ws);
28✔
734
            const auto g_plus_g = g_one.add(g_one);
28✔
735
            result.test_eq("2*g == g+g", g_two.serialize_uncompressed(), g_plus_g.serialize_uncompressed());
84✔
736

737
            result.confirm("Scalar::zero is zero", zero.is_zero());
56✔
738
            result.confirm("(zero+zero) is zero", (zero + zero).is_zero());
56✔
739
            result.confirm("(zero*zero) is zero", (zero * zero).is_zero());
56✔
740
            result.confirm("(zero-zero) is zero", (zero - zero).is_zero());
56✔
741

742
            const auto neg_zero = zero.negate();
28✔
743
            result.confirm("zero.negate() is zero", neg_zero.is_zero());
56✔
744

745
            result.confirm("(zero+nz) is zero", (zero + neg_zero).is_zero());
56✔
746
            result.confirm("(nz+nz) is zero", (neg_zero + neg_zero).is_zero());
56✔
747
            result.confirm("(nz+zero) is zero", (neg_zero + zero).is_zero());
56✔
748

749
            result.confirm("Scalar::one is not zero", !one.is_zero());
56✔
750
            result.confirm("(one-one) is zero", (one - one).is_zero());
56✔
751
            result.confirm("(one+one.negate()) is zero", (one + one.negate()).is_zero());
84✔
752
            result.confirm("(one.negate()+one) is zero", (one.negate() + one).is_zero());
84✔
753

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

757
               const auto a = Botan::EC_Scalar::random(group, rng);
448✔
758
               const auto b = Botan::EC_Scalar::random(group, rng);
448✔
759
               const auto c = a + b;
448✔
760

761
               const auto Pa = pt.mul(a, rng, ws);
448✔
762
               const auto Pb = pt.mul(b, rng, ws);
448✔
763
               const auto Pc = pt.mul(c, rng, ws);
448✔
764

765
               const auto Pc_bytes = Pc.serialize_uncompressed();
448✔
766

767
               const auto Pab = Pa.add(Pb);
448✔
768
               result.test_eq("Pa + Pb == Pc", Pab.serialize_uncompressed(), Pc_bytes);
896✔
769

770
               const auto Pba = Pb.add(Pa);
448✔
771
               result.test_eq("Pb + Pa == Pc", Pba.serialize_uncompressed(), Pc_bytes);
896✔
772
            }
896✔
773

774
            for(size_t i = 0; i != 64; ++i) {
1,820✔
775
               auto h = [&]() {
5,376✔
776
                  const auto s = [&]() {
5,376✔
777
                     if(i == 0) {
1,792✔
778
                        // Test the identity case
779
                        return Botan::EC_Scalar(zero);
28✔
780
                     } else if(i <= 32) {
1,764✔
781
                        // Test cases where the two points have a linear relation
782
                        std::vector<uint8_t> sbytes(group.get_order_bytes());
896✔
783
                        sbytes[sbytes.size() - 1] = static_cast<uint8_t>((i + 1) / 2);
896✔
784
                        auto si = Botan::EC_Scalar::deserialize(group, sbytes).value();
1,792✔
785
                        if(i % 2 == 0) {
896✔
786
                           return si;
448✔
787
                        } else {
788
                           return si.negate();
448✔
789
                        }
790
                     } else {
1,792✔
791
                        return Botan::EC_Scalar::random(group, rng);
868✔
792
                     }
793
                  }();
1,792✔
794
                  auto x = Botan::EC_AffinePoint::g_mul(s, rng, ws);
1,792✔
795
                  return x;
1,792✔
796
               }();
3,584✔
797

798
               const auto s1 = Botan::EC_Scalar::random(group, rng);
1,792✔
799
               const auto s2 = Botan::EC_Scalar::random(group, rng);
1,792✔
800

801
               const Botan::EC_Group::Mul2Table mul2_table(h);
1,792✔
802

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

805
               if(auto mul2pt = mul2_table.mul2_vartime(s1, s2)) {
1,792✔
806
                  result.test_eq("ref == mul2t", ref.serialize_uncompressed(), mul2pt->serialize_uncompressed());
7,168✔
807
               } else {
808
                  result.confirm("ref is identity", ref.is_identity());
×
809
               }
×
810
            }
1,792✔
811

812
            result.end_timer();
28✔
813

814
            results.push_back(result);
28✔
815
         }
56✔
816

817
         return results;
1✔
818
      }
1✔
819
};
820

821
BOTAN_REGISTER_TEST("pcurves", "ec_point_arith", EC_Point_Arithmetic_Tests);
822

823
   #if defined(BOTAN_HAS_ECDSA)
824

825
class ECC_Invalid_Key_Tests final : public Text_Based_Test {
×
826
   public:
827
      ECC_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecc_invalid.vec", "SubjectPublicKey") {}
2✔
828

829
      bool clear_between_callbacks() const override { return false; }
5✔
830

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

834
         const std::string encoded = vars.get_req_str("SubjectPublicKey");
5✔
835
         Botan::DataSource_Memory key_data(Botan::hex_decode(encoded));
10✔
836

837
         try {
5✔
838
            auto key = Botan::X509::load_key(key_data);
5✔
839
            result.test_eq("public key fails check", key->check_key(this->rng(), false), false);
×
840
         } catch(Botan::Decoding_Error&) {
5✔
841
            result.test_success("Decoding invalid ECC key results in decoding error exception");
5✔
842
         }
5✔
843

844
         return result;
5✔
845
      }
5✔
846
};
847

848
BOTAN_REGISTER_TEST("pubkey", "ecc_invalid", ECC_Invalid_Key_Tests);
849

850
   #endif
851

852
#endif
853

854
}  // namespace
855

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