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

randombit / botan / 11588848456

26 Oct 2024 11:00AM UTC coverage: 91.13% (+0.06%) from 91.073%
11588848456

push

github

randombit
Update for 3.6.1 release

91073 of 99937 relevant lines covered (91.13%)

9313911.32 hits per line

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

96.04
/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/ec_point.h>
16
   #include <botan/hex.h>
17
   #include <botan/numthry.h>
18
   #include <botan/pk_keys.h>
19
   #include <botan/reducer.h>
20
   #include <botan/x509_key.h>
21
   #include <botan/internal/curve_nistp.h>
22
#endif
23

24
namespace Botan_Tests {
25

26
namespace {
27

28
#if defined(BOTAN_HAS_ECC_GROUP)
29

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

37
   auto flip_prob = [](size_t i) -> double {
407,511✔
38
      if(i % 64 == 0) {
406,866✔
39
         return .5;
40
      }
41
      if(i % 32 == 0) {
400,416✔
42
         return .4;
43
      }
44
      if(i % 8 == 0) {
394,095✔
45
         return .05;
38,184✔
46
      }
47
      return .01;
48
   };
49

50
   bool active = (rng.next_byte() > 128) ? true : false;
645✔
51
   for(size_t i = 0; i != bits; ++i) {
407,511✔
52
      x <<= 1;
406,866✔
53
      x += static_cast<int>(active);
406,866✔
54

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

58
      if(sample < prob) {
406,866✔
59
         active = !active;
13,042✔
60
      }
61
   }
62

63
   if(max > 0) {
645✔
64
      while(x >= max) {
660✔
65
         const size_t b = x.bits() - 1;
15✔
66
         BOTAN_ASSERT(x.get_bit(b) == true, "Set");
30✔
67
         x.clear_bit(b);
15✔
68
      }
69
   }
70

71
   return x;
645✔
72
}
×
73

74
Botan::EC_Point create_random_point(Botan::RandomNumberGenerator& rng, const Botan::EC_Group& group) {
112✔
75
   const Botan::BigInt& p = group.get_p();
112✔
76
   const Botan::Modular_Reducer mod_p(p);
112✔
77

78
   for(;;) {
376✔
79
      const Botan::BigInt x = Botan::BigInt::random_integer(rng, 1, p);
244✔
80
      const Botan::BigInt x3 = mod_p.multiply(x, mod_p.square(x));
244✔
81
      const Botan::BigInt ax = mod_p.multiply(group.get_a(), x);
244✔
82
      const Botan::BigInt y = mod_p.reduce(x3 + ax + group.get_b());
488✔
83
      const Botan::BigInt sqrt_y = Botan::sqrt_modulo_prime(y, p);
244✔
84

85
      if(sqrt_y > 1) {
244✔
86
         BOTAN_ASSERT_EQUAL(mod_p.square(sqrt_y), y, "Square root is correct");
336✔
87
         return group.point(x, sqrt_y);
224✔
88
      }
89
   }
1,108✔
90
}
112✔
91

92
class ECC_Randomized_Tests final : public Test {
×
93
   public:
94
      std::vector<Test::Result> run() override;
95
};
96

97
std::vector<Test::Result> ECC_Randomized_Tests::run() {
1✔
98
   std::vector<Test::Result> results;
1✔
99
   for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
100
      Test::Result result("ECC randomized " + group_name);
28✔
101

102
      result.start_timer();
28✔
103

104
      auto group = Botan::EC_Group::from_name(group_name);
28✔
105

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

108
      std::vector<Botan::BigInt> blind_ws;
28✔
109

110
      try {
28✔
111
         const size_t trials = (Test::run_long_tests() ? 10 : 3);
28✔
112
         for(size_t i = 0; i < trials; ++i) {
308✔
113
            const Botan::BigInt a = group.random_scalar(rng());
280✔
114
            const Botan::BigInt b = group.random_scalar(rng());
280✔
115
            const Botan::BigInt c = group.mod_order(a + b);
280✔
116

117
            const Botan::EC_Point P = pt * a;
280✔
118
            const Botan::EC_Point Q = pt * b;
280✔
119
            const Botan::EC_Point R = pt * c;
280✔
120

121
            Botan::EC_Point P1 = group.blinded_var_point_multiply(pt, a, this->rng(), blind_ws);
280✔
122
            Botan::EC_Point Q1 = group.blinded_var_point_multiply(pt, b, this->rng(), blind_ws);
280✔
123
            Botan::EC_Point R1 = group.blinded_var_point_multiply(pt, c, this->rng(), blind_ws);
280✔
124

125
            Botan::EC_Point A1 = P + Q;
280✔
126
            Botan::EC_Point A2 = Q + P;
280✔
127

128
            result.test_eq("p + q", A1, R);
280✔
129
            result.test_eq("q + p", A2, R);
280✔
130

131
            A1.force_affine();
280✔
132
            A2.force_affine();
280✔
133
            result.test_eq("p + q", A1, R);
280✔
134
            result.test_eq("q + p", A2, R);
280✔
135

136
            result.test_eq("p on the curve", P.on_the_curve(), true);
280✔
137
            result.test_eq("q on the curve", Q.on_the_curve(), true);
280✔
138
            result.test_eq("r on the curve", R.on_the_curve(), true);
280✔
139

140
            result.test_eq("P1", P1, P);
280✔
141
            result.test_eq("Q1", Q1, Q);
280✔
142
            result.test_eq("R1", R1, R);
280✔
143

144
            P1.force_affine();
280✔
145
            Q1.force_affine();
280✔
146
            R1.force_affine();
280✔
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
         }
1,120✔
151
      } catch(std::exception& e) {
×
152
         result.test_failure(group_name, e.what());
×
153
      }
×
154
      result.end_timer();
28✔
155
      results.push_back(result);
28✔
156
   }
28✔
157

158
   return results;
1✔
159
}
×
160

161
BOTAN_REGISTER_TEST("pubkey", "ecc_randomized", ECC_Randomized_Tests);
162

163
class NIST_Curve_Reduction_Tests final : public Test {
×
164
   public:
165
      typedef std::function<void(Botan::BigInt&, Botan::secure_vector<Botan::word>&)> reducer_fn;
166

167
      std::vector<Test::Result> run() override {
1✔
168
         std::vector<Test::Result> results;
1✔
169

170
         // Using lambdas here to avoid strange UbSan warning (#1370)
171

172
         results.push_back(random_redc_test(
2✔
173
            "P-384", Botan::prime_p384(), [](Botan::BigInt& p, Botan::secure_vector<Botan::word>& ws) -> void {
129✔
174
               Botan::redc_p384(p, ws);
129✔
175
            }));
176
         results.push_back(random_redc_test(
2✔
177
            "P-256", Botan::prime_p256(), [](Botan::BigInt& p, Botan::secure_vector<Botan::word>& ws) -> void {
129✔
178
               Botan::redc_p256(p, ws);
129✔
179
            }));
180
         results.push_back(random_redc_test(
2✔
181
            "P-224", Botan::prime_p224(), [](Botan::BigInt& p, Botan::secure_vector<Botan::word>& ws) -> void {
129✔
182
               Botan::redc_p224(p, ws);
129✔
183
            }));
184
         results.push_back(random_redc_test(
2✔
185
            "P-192", Botan::prime_p192(), [](Botan::BigInt& p, Botan::secure_vector<Botan::word>& ws) -> void {
129✔
186
               Botan::redc_p192(p, ws);
129✔
187
            }));
188
         results.push_back(random_redc_test(
2✔
189
            "P-521", Botan::prime_p521(), [](Botan::BigInt& p, Botan::secure_vector<Botan::word>& ws) -> void {
129✔
190
               Botan::redc_p521(p, ws);
129✔
191
            }));
192

193
         return results;
1✔
194
      }
×
195

196
      static Test::Result random_redc_test(const std::string& prime_name,
5✔
197
                                           const Botan::BigInt& p,
198
                                           const reducer_fn& redc_fn) {
199
         const Botan::BigInt p2 = p * p;
5✔
200
         const size_t p_bits = p.bits();
5✔
201

202
         Botan::Modular_Reducer p_redc(p);
5✔
203
         Botan::secure_vector<Botan::word> ws;
5✔
204

205
         auto rng = Test::new_rng("random_redc " + prime_name);
5✔
206

207
         Test::Result result("NIST " + prime_name + " reduction");
10✔
208
         result.start_timer();
5✔
209

210
         const size_t trials = (Test::run_long_tests() ? 128 : 16);
5✔
211

212
         for(size_t i = 0; i <= trials; ++i) {
650✔
213
            const Botan::BigInt x = test_integer(*rng, 2 * p_bits, p2);
645✔
214

215
            // TODO: time and report all three approaches
216
            const Botan::BigInt v1 = x % p;
645✔
217
            const Botan::BigInt v2 = p_redc.reduce(x);
645✔
218

219
            Botan::BigInt v3 = x;
645✔
220
            redc_fn(v3, ws);
645✔
221

222
            if(!result.test_eq("reference redc", v1, v2) || !result.test_eq("specialized redc", v2, v3)) {
1,290✔
223
               result.test_note("failing input" + Botan::hex_encode(Botan::BigInt::encode(x)));
×
224
            }
225
         }
2,580✔
226

227
         result.end_timer();
5✔
228

229
         return result;
5✔
230
      }
11✔
231
};
232

233
BOTAN_REGISTER_TEST("pubkey", "nist_redc", NIST_Curve_Reduction_Tests);
234

235
class EC_Group_Tests : public Test {
×
236
   public:
237
      std::vector<Test::Result> run() override {
1✔
238
         std::vector<Test::Result> results;
1✔
239

240
         for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
241
            Test::Result result("EC_Group " + group_name);
28✔
242

243
            result.start_timer();
28✔
244

245
            const auto group = Botan::EC_Group::from_name(group_name);
28✔
246

247
            result.confirm("EC_Group is known", group.get_curve_oid().has_value());
56✔
248
            result.confirm("EC_Group is considered valid", group.verify_group(this->rng(), true));
56✔
249
            result.confirm("EC_Group is not considered explict encoding", !group.used_explicit_encoding());
56✔
250

251
            result.test_eq("EC_Group has correct bit size", group.get_p().bits(), group.get_p_bits());
28✔
252
            result.test_eq("EC_Group has byte size", group.get_p().bytes(), group.get_p_bytes());
28✔
253

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

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

258
            result.test_eq(
56✔
259
               "EC_group_identity_from_order works", from_order.to_string(), group.get_curve_oid().to_string());
56✔
260

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

263
            try {
28✔
264
               const Botan::EC_Group copy(group.get_curve_oid(),
28✔
265
                                          group.get_p(),
266
                                          group.get_a(),
267
                                          group.get_b(),
268
                                          group.get_g_x(),
269
                                          group.get_g_y(),
270
                                          group.get_order());
28✔
271

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

275
            const auto group_der_oid = group.DER_encode();
28✔
276
            const Botan::EC_Group group_via_oid(group_der_oid);
28✔
277
            result.confirm("EC_Group via OID is not considered explict encoding",
56✔
278
                           !group_via_oid.used_explicit_encoding());
28✔
279

280
            const auto group_der_explicit = group.DER_encode(Botan::EC_Group_Encoding::Explicit);
28✔
281
            const Botan::EC_Group group_via_explicit(group_der_explicit);
28✔
282
            result.confirm("EC_Group via explicit DER is considered explict encoding",
56✔
283
                           group_via_explicit.used_explicit_encoding());
28✔
284

285
            const auto pt_mult_by_order = group.get_base_point() * group.get_order();
28✔
286
            result.confirm("Multiplying point by the order results in zero point", pt_mult_by_order.is_zero());
56✔
287

288
            if(group.a_is_minus_3()) {
28✔
289
               result.test_eq("Group A equals -3", group.get_a(), group.get_p() - 3);
51✔
290
            } else {
291
               result.test_ne("Group " + group_name + " A does not equal -3", group.get_a(), group.get_p() - 3);
44✔
292
            }
293

294
            if(group.a_is_zero()) {
28✔
295
               result.test_eq("Group A is zero", group.get_a(), BigInt(0));
8✔
296
            } else {
297
               result.test_ne("Group " + group_name + " A does not equal zero", group.get_a(), BigInt(0));
72✔
298
            }
299

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

303
            // get a copy
304
            Botan::EC_Point q = p;
28✔
305

306
            p.randomize_repr(this->rng());
28✔
307
            q.randomize_repr(this->rng());
28✔
308

309
            result.test_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
84✔
310
            result.test_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
84✔
311

312
            q.force_affine();
28✔
313

314
            result.test_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
84✔
315
            result.test_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
84✔
316

317
            test_ser_der(result, group);
28✔
318
            test_basic_math(result, group);
28✔
319
            test_point_swap(result, group);
28✔
320
            test_zeropoint(result, group);
28✔
321

322
            result.end_timer();
28✔
323

324
            results.push_back(result);
28✔
325
         }
84✔
326

327
         return results;
1✔
328
      }
×
329

330
   private:
331
      void test_ser_der(Test::Result& result, const Botan::EC_Group& group) {
28✔
332
         // generate point
333
         const Botan::EC_Point pt = create_random_point(this->rng(), group);
28✔
334
         const Botan::EC_Point zero = group.zero_point();
28✔
335

336
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
196✔
337
                            Botan::EC_Point_Format::Compressed,
338
                            Botan::EC_Point_Format::Hybrid}) {
112✔
339
            result.test_eq("encoded/decode rt works", group.OS2ECP(pt.encode(scheme)), pt);
168✔
340
            result.test_eq("encoded/decode rt works", group.OS2ECP(zero.encode(scheme)), zero);
252✔
341
         }
342
      }
28✔
343

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

347
         Botan::EC_Point p1 = G * 2;
56✔
348
         p1 += G;
28✔
349

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

352
         p1 -= G * 2;
56✔
353

354
         result.test_eq("point subtraction", p1, G);
28✔
355

356
         // The scalar multiplication algorithm relies on this being true:
357
         try {
28✔
358
            Botan::EC_Point zero_coords = group.point(0, 0);
56✔
359
            result.confirm("point (0,0) is not on the curve", !zero_coords.on_the_curve());
×
360
         } catch(Botan::Exception&) {
28✔
361
            result.test_success("point (0,0) is rejected");
28✔
362
         }
28✔
363
      }
28✔
364

365
      void test_point_swap(Test::Result& result, const Botan::EC_Group& group) {
28✔
366
         Botan::EC_Point a(create_random_point(this->rng(), group));
28✔
367
         Botan::EC_Point b(create_random_point(this->rng(), group));
28✔
368
         b *= Botan::BigInt(this->rng(), 20);
28✔
369

370
         Botan::EC_Point c(a);
28✔
371
         Botan::EC_Point d(b);
28✔
372

373
         d.swap(c);
28✔
374
         result.test_eq("swap correct", a, d);
28✔
375
         result.test_eq("swap correct", b, c);
28✔
376
      }
28✔
377

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

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

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

386
         result.confirm("point is on the curve", p1.on_the_curve());
56✔
387
         result.confirm("point is not zero", !p1.is_zero());
56✔
388

389
         Botan::EC_Point p2 = p1;
28✔
390
         p2 -= p1;
28✔
391

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

394
         const Botan::EC_Point minus_p1 = -p1;
28✔
395
         result.confirm("point is on the curve", minus_p1.on_the_curve());
56✔
396
         const Botan::EC_Point shouldBeZero = p1 + minus_p1;
28✔
397
         result.confirm("point is on the curve", shouldBeZero.on_the_curve());
56✔
398
         result.confirm("point is zero", shouldBeZero.is_zero());
56✔
399

400
         result.test_eq("minus point x", minus_p1.get_affine_x(), p1.get_affine_x());
84✔
401
         result.test_eq("minus point y", minus_p1.get_affine_y(), group.get_p() - p1.get_affine_y());
112✔
402

403
         result.confirm("zero point is zero", zero.is_zero());
56✔
404
         result.confirm("zero point is on the curve", zero.on_the_curve());
56✔
405
         result.test_eq("addition of zero does nothing", p1, p1 + zero);
56✔
406
         result.test_eq("addition of zero does nothing", p1, zero + p1);
56✔
407
         result.test_eq("addition of zero does nothing", p1, p1 - zero);
56✔
408
         result.confirm("zero times anything is the zero point", (zero * 39193).is_zero());
84✔
409

410
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
196✔
411
                            Botan::EC_Point_Format::Compressed,
412
                            Botan::EC_Point_Format::Hybrid}) {
112✔
413
            const std::vector<uint8_t> v = zero.encode(scheme);
84✔
414
            result.test_eq("encoded/decode rt works", group.OS2ECP(v), zero);
168✔
415
         }
84✔
416
      }
28✔
417
};
418

419
BOTAN_REGISTER_TEST("pubkey", "ec_group", EC_Group_Tests);
420

421
Test::Result test_decoding_with_seed() {
1✔
422
   Test::Result result("ECC Unit");
1✔
423

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

426
   result.confirm("decoding worked", secp384r1_with_seed.initialized());
2✔
427

428
   const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
429

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

432
   return result;
1✔
433
}
1✔
434

435
Test::Result test_mixed_points() {
1✔
436
   Test::Result result("ECC Unit");
1✔
437

438
   const auto secp256r1 = Botan::EC_Group::from_name("secp256r1");
1✔
439
   const auto secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
440

441
   const Botan::EC_Point& G256 = secp256r1.get_base_point();
1✔
442
   const Botan::EC_Point& G384 = secp384r1.get_base_point();
1✔
443

444
   result.test_throws("Mixing points from different groups", [&] { Botan::EC_Point p = G256 + G384; });
3✔
445
   return result;
1✔
446
}
1✔
447

448
Test::Result test_basic_operations() {
1✔
449
   Test::Result result("ECC Unit");
1✔
450

451
   // precalculation
452
   const auto secp160r1 = Botan::EC_Group::from_name("secp160r1");
1✔
453
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
454

455
   const Botan::EC_Point& p0 = p_G;
1✔
456
   const Botan::EC_Point p1 = p_G * 2;
2✔
457

458
   result.test_eq("p1 affine x", p1.get_affine_x(), Botan::BigInt("16984103820118642236896513183038186009872590470"));
3✔
459
   result.test_eq("p1 affine y", p1.get_affine_y(), Botan::BigInt("1373093393927139016463695321221277758035357890939"));
3✔
460

461
   const Botan::EC_Point simplePlus = p1 + p0;
1✔
462
   const Botan::EC_Point exp_simplePlus =
1✔
463
      secp160r1.point(Botan::BigInt("704859595002530890444080436569091156047721708633"),
2✔
464
                      Botan::BigInt("1147993098458695153857594941635310323215433166682"));
2✔
465

466
   result.test_eq("point addition", simplePlus, exp_simplePlus);
1✔
467

468
   const Botan::EC_Point simpleMinus = p1 - p0;
1✔
469
   result.test_eq("point subtraction", simpleMinus, p_G);
1✔
470

471
   const Botan::EC_Point simpleMult = p1 * 123456789;
2✔
472

473
   result.test_eq("point mult affine x",
3✔
474
                  simpleMult.get_affine_x(),
2✔
475
                  Botan::BigInt("43638877777452195295055270548491599621118743290"));
1✔
476
   result.test_eq("point mult affine y",
3✔
477
                  simpleMult.get_affine_y(),
2✔
478
                  Botan::BigInt("56841378500012376527163928510402662349220202981"));
1✔
479

480
   return result;
1✔
481
}
1✔
482

483
Test::Result test_enc_dec_compressed_160() {
1✔
484
   Test::Result result("ECC Unit");
1✔
485

486
   // Test for compressed conversion (02/03) 160bit
487
   const auto secp160r1 = Botan::EC_Group::from_name("secp160r1");
1✔
488
   const std::vector<uint8_t> G_comp = Botan::hex_decode("024A96B5688EF573284664698968C38BB913CBFC82");
1✔
489
   const Botan::EC_Point p = secp160r1.OS2ECP(G_comp);
1✔
490
   const std::vector<uint8_t> sv_result = p.encode(Botan::EC_Point_Format::Compressed);
1✔
491

492
   result.test_eq("result", sv_result, G_comp);
1✔
493
   return result;
2✔
494
}
2✔
495

496
Test::Result test_enc_dec_compressed_256() {
1✔
497
   Test::Result result("ECC Unit");
1✔
498

499
   const auto group = Botan::EC_Group::from_name("secp256r1");
1✔
500

501
   const std::string G_secp_comp = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
1✔
502
   const std::vector<uint8_t> sv_G_secp_comp = Botan::hex_decode(G_secp_comp);
1✔
503

504
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_comp);
1✔
505
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Compressed);
1✔
506

507
   result.test_eq("compressed_256", sv_result, sv_G_secp_comp);
1✔
508
   return result;
2✔
509
}
2✔
510

511
Test::Result test_enc_dec_uncompressed_112() {
1✔
512
   Test::Result result("ECC Unit");
1✔
513

514
   // Test for uncompressed conversion (04) 112bit
515

516
   // Curve is secp112r2
517

518
   const Botan::BigInt p("0xdb7c2abf62e35e668076bead208b");
1✔
519
   const Botan::BigInt a("0x6127C24C05F38A0AAAF65C0EF02C");
1✔
520
   const Botan::BigInt b("0x51DEF1815DB5ED74FCC34C85D709");
1✔
521

522
   const Botan::BigInt g_x("0x4BA30AB5E892B4E1649DD0928643");
1✔
523
   const Botan::BigInt g_y("0xADCD46F5882E3747DEF36E956E97");
1✔
524

525
   const Botan::BigInt order("0x36DF0AAFD8B8D7597CA10520D04B");
1✔
526
   const Botan::BigInt cofactor("4");  // !
1✔
527

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

531
   const std::string G_secp_uncomp = "044BA30AB5E892B4E1649DD0928643ADCD46F5882E3747DEF36E956E97";
1✔
532
   const std::vector<uint8_t> sv_G_secp_uncomp = Botan::hex_decode(G_secp_uncomp);
1✔
533

534
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_uncomp);
1✔
535
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Uncompressed);
1✔
536

537
   result.test_eq("uncompressed_112", sv_result, sv_G_secp_uncomp);
1✔
538
   return result;
1✔
539
}
9✔
540

541
Test::Result test_enc_dec_uncompressed_521() {
1✔
542
   Test::Result result("ECC Unit");
1✔
543

544
   // Test for uncompressed conversion(04) with big values(521 bit)
545

546
   const std::string G_secp_uncomp =
1✔
547
      "0400C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2ffA8DE3348B3C1856A429BF97E7E31C2E5BD66011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650";
1✔
548

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

551
   const auto group = Botan::EC_Group::from_name("secp521r1");
1✔
552

553
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_uncomp);
1✔
554

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

557
   result.test_eq("expected", sv_result, sv_G_secp_uncomp);
1✔
558
   return result;
1✔
559
}
2✔
560

561
Test::Result test_ecc_registration() {
1✔
562
   Test::Result result("ECC registration");
1✔
563

564
   // secp128r1
565
   const Botan::BigInt p("0xfffffffdffffffffffffffffffffffff");
1✔
566
   const Botan::BigInt a("0xfffffffdfffffffffffffffffffffffc");
1✔
567
   const Botan::BigInt b("0xe87579c11079f43dd824993c2cee5ed3");
1✔
568

569
   const Botan::BigInt g_x("0x161ff7528b899b2d0c28607ca52c5b86");
1✔
570
   const Botan::BigInt g_y("0xcf5ac8395bafeb13c02da292dded7a83");
1✔
571
   const Botan::BigInt order("0xfffffffe0000000075a30d1b9038a115");
1✔
572

573
   const Botan::OID oid("1.3.132.0.28");
1✔
574

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

578
   auto group = Botan::EC_Group::from_OID(oid);
1✔
579

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

582
   return result;
2✔
583
}
7✔
584

585
Test::Result test_ec_group_from_params() {
1✔
586
   Test::Result result("EC_Group from params");
1✔
587

588
   Botan::EC_Group::clear_registered_curve_data();
1✔
589

590
   // secp256r1
591
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
592
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
593
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
594

595
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
596
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
597
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
598

599
   const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
600

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

606
   return result;
2✔
607
}
7✔
608

609
Test::Result test_ec_group_bad_registration() {
1✔
610
   Test::Result result("EC_Group registering non-match");
1✔
611

612
   Botan::EC_Group::clear_registered_curve_data();
1✔
613

614
   // secp256r1 params except with a bad B param
615
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
616
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
617
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604C");
1✔
618

619
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
620
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
621
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
622

623
   const Botan::OID oid("1.2.840.10045.3.1.7");
1✔
624

625
   try {
1✔
626
      Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
627
      result.test_failure("Should have failed");
×
628
   } catch(Botan::Invalid_Argument&) {
1✔
629
      result.test_success("Got expected exception");
1✔
630
   }
1✔
631

632
   return result;
1✔
633
}
7✔
634

635
Test::Result test_ec_group_duplicate_orders() {
1✔
636
   Test::Result result("EC_Group with duplicate group order");
1✔
637

638
   Botan::EC_Group::clear_registered_curve_data();
1✔
639

640
   // secp256r1
641
   const Botan::BigInt p("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
642
   const Botan::BigInt a("0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
643
   const Botan::BigInt b("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
644

645
   const Botan::BigInt g_x("0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
1✔
646
   const Botan::BigInt g_y("0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
1✔
647
   const Botan::BigInt order("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
1✔
648

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

651
   Botan::EC_Group reg_group(oid, p, a, b, g_x, g_y, order);
1✔
652
   result.test_success("Registration success");
1✔
653
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == oid);
2✔
654

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

659
   // Existing secp256r1 unmodified:
660
   const Botan::OID secp160r1("1.2.840.10045.3.1.7");
1✔
661
   const auto other_group = Botan::EC_Group::from_OID(secp160r1);
1✔
662
   result.confirm("Group has correct OID", other_group.get_curve_oid() == secp160r1);
2✔
663

664
   return result;
2✔
665
}
7✔
666

667
class ECC_Unit_Tests final : public Test {
×
668
   public:
669
      std::vector<Test::Result> run() override {
1✔
670
         std::vector<Test::Result> results;
1✔
671

672
         results.push_back(test_decoding_with_seed());
2✔
673
         results.push_back(test_mixed_points());
2✔
674
         results.push_back(test_basic_operations());
2✔
675
         results.push_back(test_enc_dec_compressed_160());
2✔
676
         results.push_back(test_enc_dec_compressed_256());
2✔
677
         results.push_back(test_enc_dec_uncompressed_112());
2✔
678
         results.push_back(test_enc_dec_uncompressed_521());
2✔
679
         results.push_back(test_ecc_registration());
2✔
680
         results.push_back(test_ec_group_from_params());
2✔
681
         results.push_back(test_ec_group_bad_registration());
2✔
682
         results.push_back(test_ec_group_duplicate_orders());
2✔
683

684
         return results;
1✔
685
      }
×
686
};
687

688
BOTAN_REGISTER_SERIALIZED_TEST("pubkey", "ecc_unit", ECC_Unit_Tests);
689

690
   #if defined(BOTAN_HAS_ECDSA)
691

692
class ECC_Invalid_Key_Tests final : public Text_Based_Test {
×
693
   public:
694
      ECC_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecc_invalid.vec", "SubjectPublicKey") {}
2✔
695

696
      bool clear_between_callbacks() const override { return false; }
5✔
697

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

701
         const std::string encoded = vars.get_req_str("SubjectPublicKey");
5✔
702
         Botan::DataSource_Memory key_data(Botan::hex_decode(encoded));
10✔
703

704
         try {
5✔
705
            auto key = Botan::X509::load_key(key_data);
5✔
706
            result.test_eq("public key fails check", key->check_key(this->rng(), false), false);
×
707
         } catch(Botan::Decoding_Error&) {
5✔
708
            result.test_success("Decoding invalid ECC key results in decoding error exception");
5✔
709
         }
5✔
710

711
         return result;
5✔
712
      }
5✔
713
};
714

715
BOTAN_REGISTER_TEST("pubkey", "ecc_invalid", ECC_Invalid_Key_Tests);
716

717
   #endif
718

719
#endif
720

721
}  // namespace
722

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

© 2025 Coveralls, Inc