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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

96.54
/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,181✔
60
      }
61
   }
62

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

71
   return x;
645✔
72
}
×
73

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

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

85
      if(sqrt_y > 1) {
211✔
86
         BOTAN_ASSERT_EQUAL(mod_p.square(sqrt_y), y, "Square root is correct");
324✔
87
         return group.point(x, sqrt_y);
216✔
88
      }
89
   }
947✔
90
}
108✔
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()) {
28✔
100
      Test::Result result("ECC randomized " + group_name);
27✔
101

102
      result.start_timer();
27✔
103

104
      Botan::EC_Group group(group_name);
27✔
105

106
      const Botan::EC_Point pt = create_random_point(Test::rng(), group);
27✔
107
      const Botan::BigInt& group_order = group.get_order();
27✔
108

109
      std::vector<Botan::BigInt> blind_ws;
27✔
110

111
      try {
27✔
112
         const size_t trials = (Test::run_long_tests() ? 10 : 3);
27✔
113
         for(size_t i = 0; i < trials; ++i) {
297✔
114
            const Botan::BigInt a = Botan::BigInt::random_integer(Test::rng(), 2, group_order);
270✔
115
            const Botan::BigInt b = Botan::BigInt::random_integer(Test::rng(), 2, group_order);
270✔
116
            const Botan::BigInt c = a + b;
270✔
117

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

122
            Botan::EC_Point P1 = group.blinded_var_point_multiply(pt, a, Test::rng(), blind_ws);
270✔
123
            Botan::EC_Point Q1 = group.blinded_var_point_multiply(pt, b, Test::rng(), blind_ws);
270✔
124
            Botan::EC_Point R1 = group.blinded_var_point_multiply(pt, c, Test::rng(), blind_ws);
270✔
125

126
            Botan::EC_Point A1 = P + Q;
270✔
127
            Botan::EC_Point A2 = Q + P;
270✔
128

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

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

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

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

145
            P1.force_affine();
270✔
146
            Q1.force_affine();
270✔
147
            R1.force_affine();
270✔
148
            result.test_eq("P1", P1, P);
270✔
149
            result.test_eq("Q1", Q1, Q);
270✔
150
            result.test_eq("R1", R1, R);
270✔
151
         }
1,080✔
152
      } catch(std::exception& e) {
×
153
         result.test_failure(group_name, e.what());
×
154
      }
×
155
      result.end_timer();
27✔
156
      results.push_back(result);
27✔
157
   }
27✔
158

159
   return results;
1✔
160
}
×
161

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

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

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

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

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

194
         return results;
1✔
195
      }
×
196

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

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

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

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

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

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

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

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

226
         result.end_timer();
5✔
227

228
         return result;
5✔
229
      }
10✔
230
};
231

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

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

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

242
            const Botan::OID oid = Botan::OID::from_string(group_name);
27✔
243

244
            const Botan::EC_Group group(oid);
27✔
245

246
            result.confirm("EC_Group is known", !group.get_curve_oid().empty());
54✔
247
            result.confirm("EC_Group is considered valid", group.verify_group(Test::rng(), true));
54✔
248

249
            result.test_eq("EC_Group has correct bit size", group.get_p().bits(), group.get_p_bits());
27✔
250
            result.test_eq("EC_Group has byte size", group.get_p().bytes(), group.get_p_bytes());
27✔
251

252
            const Botan::OID from_order = Botan::EC_Group::EC_group_identity_from_order(group.get_order());
27✔
253
            result.test_eq("EC_group_identity_from_order works", from_order.to_string(), oid.to_string());
72✔
254

255
            result.confirm("Same group is same", group == Botan::EC_Group(group_name));
81✔
256

257
            const Botan::EC_Group copy(group.get_p(),
27✔
258
                                       group.get_a(),
259
                                       group.get_b(),
260
                                       group.get_g_x(),
261
                                       group.get_g_y(),
262
                                       group.get_order(),
263
                                       group.get_cofactor());
27✔
264

265
            result.confirm("Same group is same even with copy", group == copy);
54✔
266

267
            const auto pt_mult_by_order = group.get_base_point() * group.get_order();
27✔
268
            result.confirm("Multiplying point by the order results in zero point", pt_mult_by_order.is_zero());
54✔
269

270
            if(group.a_is_minus_3()) {
27✔
271
               result.test_eq("Group A equals -3", group.get_a(), group.get_p() - 3);
48✔
272
            } else {
273
               result.test_ne("Group " + group_name + " A does not equal -3", group.get_a(), group.get_p() - 3);
33✔
274
            }
275

276
            if(group.a_is_zero()) {
27✔
277
               result.test_eq("Group A is zero", group.get_a(), BigInt(0));
8✔
278
            } else {
279
               result.test_ne("Group " + group_name + " A does not equal zero", group.get_a(), BigInt(0));
46✔
280
            }
281

282
            // get a valid point
283
            Botan::EC_Point p = group.get_base_point() * Test::rng().next_nonzero_byte();
27✔
284

285
            // get a copy
286
            Botan::EC_Point q = p;
27✔
287

288
            p.randomize_repr(Test::rng());
27✔
289
            q.randomize_repr(Test::rng());
27✔
290

291
            result.test_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
81✔
292
            result.test_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
81✔
293

294
            q.force_affine();
27✔
295

296
            result.test_eq("affine x after copy", p.get_affine_x(), q.get_affine_x());
81✔
297
            result.test_eq("affine y after copy", p.get_affine_y(), q.get_affine_y());
81✔
298

299
            test_ser_der(result, group);
27✔
300
            test_basic_math(result, group);
27✔
301
            test_point_swap(result, group);
27✔
302
            test_zeropoint(result, group);
27✔
303

304
            results.push_back(result);
27✔
305
         }
27✔
306

307
         return results;
1✔
308
      }
×
309

310
   private:
311
      static void test_ser_der(Test::Result& result, const Botan::EC_Group& group) {
27✔
312
         // generate point
313
         const Botan::EC_Point pt = create_random_point(Test::rng(), group);
27✔
314
         const Botan::EC_Point zero = group.zero_point();
27✔
315

316
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
189✔
317
                            Botan::EC_Point_Format::Compressed,
318
                            Botan::EC_Point_Format::Hybrid}) {
108✔
319
            result.test_eq("encoded/decode rt works", group.OS2ECP(pt.encode(scheme)), pt);
162✔
320
            result.test_eq("encoded/decode rt works", group.OS2ECP(zero.encode(scheme)), zero);
243✔
321
         }
322
      }
27✔
323

324
      static void test_basic_math(Test::Result& result, const Botan::EC_Group& group) {
27✔
325
         const Botan::EC_Point& G = group.get_base_point();
27✔
326

327
         Botan::EC_Point p1 = G * 2;
54✔
328
         p1 += G;
27✔
329

330
         result.test_eq("point addition", p1, G * 3);
81✔
331

332
         p1 -= G * 2;
54✔
333

334
         result.test_eq("point subtraction", p1, G);
27✔
335

336
         // The scalar multiplication algorithm relies on this being true:
337
         Botan::EC_Point zero_coords = group.point(0, 0);
54✔
338
         result.confirm("point (0,0) is not on the curve", !zero_coords.on_the_curve());
54✔
339
      }
27✔
340

341
      static void test_point_swap(Test::Result& result, const Botan::EC_Group& group) {
27✔
342
         Botan::EC_Point a(create_random_point(Test::rng(), group));
27✔
343
         Botan::EC_Point b(create_random_point(Test::rng(), group));
27✔
344
         b *= Botan::BigInt(Test::rng(), 20);
27✔
345

346
         Botan::EC_Point c(a);
27✔
347
         Botan::EC_Point d(b);
27✔
348

349
         d.swap(c);
27✔
350
         result.test_eq("swap correct", a, d);
27✔
351
         result.test_eq("swap correct", b, c);
27✔
352
      }
27✔
353

354
      static void test_zeropoint(Test::Result& result, const Botan::EC_Group& group) {
27✔
355
         Botan::EC_Point zero = group.zero_point();
27✔
356

357
         result.test_throws("Zero point throws", "Cannot convert zero point to affine", [&]() { zero.get_affine_x(); });
108✔
358
         result.test_throws("Zero point throws", "Cannot convert zero point to affine", [&]() { zero.get_affine_y(); });
108✔
359

360
         const Botan::EC_Point p1 = group.get_base_point() * 2;
27✔
361

362
         result.confirm("point is on the curve", p1.on_the_curve());
54✔
363
         result.confirm("point is not zero", !p1.is_zero());
54✔
364

365
         Botan::EC_Point p2 = p1;
27✔
366
         p2 -= p1;
27✔
367

368
         result.confirm("p - q with q = p results in zero", p2.is_zero());
54✔
369

370
         const Botan::EC_Point minus_p1 = -p1;
27✔
371
         result.confirm("point is on the curve", minus_p1.on_the_curve());
54✔
372
         const Botan::EC_Point shouldBeZero = p1 + minus_p1;
27✔
373
         result.confirm("point is on the curve", shouldBeZero.on_the_curve());
54✔
374
         result.confirm("point is zero", shouldBeZero.is_zero());
54✔
375

376
         result.test_eq("minus point x", minus_p1.get_affine_x(), p1.get_affine_x());
81✔
377
         result.test_eq("minus point y", minus_p1.get_affine_y(), group.get_p() - p1.get_affine_y());
108✔
378

379
         result.confirm("zero point is zero", zero.is_zero());
54✔
380
         result.confirm("zero point is on the curve", zero.on_the_curve());
54✔
381
         result.test_eq("addition of zero does nothing", p1, p1 + zero);
54✔
382
         result.test_eq("addition of zero does nothing", p1, zero + p1);
54✔
383
         result.test_eq("addition of zero does nothing", p1, p1 - zero);
54✔
384
         result.confirm("zero times anything is the zero point", (zero * 39193).is_zero());
108✔
385

386
         for(auto scheme : {Botan::EC_Point_Format::Uncompressed,
189✔
387
                            Botan::EC_Point_Format::Compressed,
388
                            Botan::EC_Point_Format::Hybrid}) {
108✔
389
            const std::vector<uint8_t> v = zero.encode(scheme);
81✔
390
            result.test_eq("encoded/decode rt works", group.OS2ECP(v), zero);
162✔
391
         }
81✔
392
      }
27✔
393
};
394

395
BOTAN_REGISTER_TEST("pubkey", "ec_group", EC_Group_Tests);
396

397
Test::Result test_decoding_with_seed() {
1✔
398
   Test::Result result("ECC Unit");
1✔
399

400
   Botan::EC_Group secp384r1_with_seed(Test::read_data_file("x509/ecc/secp384r1_seed.pem"));
2✔
401

402
   result.confirm("decoding worked", secp384r1_with_seed.initialized());
2✔
403

404
   Botan::EC_Group secp384r1("secp384r1");
1✔
405

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

408
   return result;
1✔
409
}
1✔
410

411
Test::Result test_coordinates() {
1✔
412
   Test::Result result("ECC Unit");
1✔
413

414
   const Botan::BigInt exp_affine_x("16984103820118642236896513183038186009872590470");
1✔
415
   const Botan::BigInt exp_affine_y("1373093393927139016463695321221277758035357890939");
1✔
416

417
   // precalculation
418
   const Botan::EC_Group secp160r1("secp160r1");
1✔
419
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
420

421
   const Botan::EC_Point point_exp = secp160r1.point(exp_affine_x, exp_affine_y);
1✔
422
   result.confirm("Point is on the curve", point_exp.on_the_curve());
2✔
423

424
   const Botan::EC_Point p1 = p_G * 2;
2✔
425
   result.test_eq("Point affine x", p1.get_affine_x(), exp_affine_x);
2✔
426
   result.test_eq("Point affine y", p1.get_affine_y(), exp_affine_y);
2✔
427
   return result;
2✔
428
}
3✔
429

430
/**
431
Test point multiplication according to
432
--------
433
SEC 2: Test Vectors for SEC 1
434
Certicom Research
435
Working Draft
436
September, 1999
437
Version 0.3;
438
Section 2.1.2
439
--------
440
*/
441
Test::Result test_point_mult() {
1✔
442
   Test::Result result("ECC Unit");
1✔
443

444
   Botan::EC_Group secp160r1("secp160r1");
1✔
445
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
446

447
   Botan::BigInt d_U("0xaa374ffc3ce144e6b073307972cb6d57b2a4e982");
1✔
448
   Botan::EC_Point Q_U = d_U * p_G;
1✔
449

450
   result.test_eq("affine x", Q_U.get_affine_x(), Botan::BigInt("466448783855397898016055842232266600516272889280"));
3✔
451
   result.test_eq("affine y", Q_U.get_affine_y(), Botan::BigInt("1110706324081757720403272427311003102474457754220"));
3✔
452
   return result;
1✔
453
}
2✔
454

455
Test::Result test_point_negative() {
1✔
456
   Test::Result result("ECC Unit");
1✔
457

458
   Botan::EC_Group secp160r1("secp160r1");
1✔
459
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
460

461
   const Botan::EC_Point p1 = p_G * 2;
2✔
462

463
   result.test_eq("affine x", p1.get_affine_x(), Botan::BigInt("16984103820118642236896513183038186009872590470"));
3✔
464
   result.test_eq("affine y", p1.get_affine_y(), Botan::BigInt("1373093393927139016463695321221277758035357890939"));
3✔
465

466
   const Botan::EC_Point p1_neg = -p1;
1✔
467

468
   result.test_eq("affine x", p1_neg.get_affine_x(), p1.get_affine_x());
3✔
469
   result.test_eq("affine y", p1_neg.get_affine_y(), Botan::BigInt("88408243403763901739989511495005261618427168388"));
3✔
470
   return result;
1✔
471
}
1✔
472

473
Test::Result test_mult_point() {
1✔
474
   Test::Result result("ECC Unit");
1✔
475

476
   Botan::EC_Group secp160r1("secp160r1");
1✔
477
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
478

479
   const Botan::EC_Point& p0 = p_G;
1✔
480
   Botan::EC_Point p1 = p_G * 2;
2✔
481

482
   p1 *= p0.get_affine_x();
1✔
483

484
   const Botan::BigInt exp_mult_x(std::string("967697346845926834906555988570157345422864716250"));
1✔
485
   const Botan::BigInt exp_mult_y(std::string("512319768365374654866290830075237814703869061656"));
1✔
486
   Botan::EC_Point expected = secp160r1.point(exp_mult_x, exp_mult_y);
1✔
487

488
   result.test_eq("point mult", p1, expected);
1✔
489
   return result;
1✔
490
}
3✔
491

492
Test::Result test_mixed_points() {
1✔
493
   Test::Result result("ECC Unit");
1✔
494

495
   Botan::EC_Group secp256r1("secp256r1");
1✔
496
   Botan::EC_Group secp384r1("secp384r1");
1✔
497

498
   const Botan::EC_Point& G256 = secp256r1.get_base_point();
1✔
499
   const Botan::EC_Point& G384 = secp384r1.get_base_point();
1✔
500

501
   result.test_throws("Mixing points from different groups", [&] { Botan::EC_Point p = G256 + G384; });
3✔
502
   return result;
1✔
503
}
1✔
504

505
Test::Result test_basic_operations() {
1✔
506
   Test::Result result("ECC Unit");
1✔
507

508
   // precalculation
509
   Botan::EC_Group secp160r1("secp160r1");
1✔
510
   const Botan::EC_Point& p_G = secp160r1.get_base_point();
1✔
511

512
   const Botan::EC_Point& p0 = p_G;
1✔
513
   const Botan::EC_Point p1 = p_G * 2;
2✔
514

515
   result.test_eq("p1 affine x", p1.get_affine_x(), Botan::BigInt("16984103820118642236896513183038186009872590470"));
3✔
516
   result.test_eq("p1 affine y", p1.get_affine_y(), Botan::BigInt("1373093393927139016463695321221277758035357890939"));
3✔
517

518
   const Botan::EC_Point simplePlus = p1 + p0;
1✔
519
   const Botan::EC_Point exp_simplePlus =
1✔
520
      secp160r1.point(Botan::BigInt("704859595002530890444080436569091156047721708633"),
2✔
521
                      Botan::BigInt("1147993098458695153857594941635310323215433166682"));
2✔
522

523
   result.test_eq("point addition", simplePlus, exp_simplePlus);
1✔
524

525
   const Botan::EC_Point simpleMinus = p1 - p0;
1✔
526
   result.test_eq("point subtraction", simpleMinus, p_G);
1✔
527

528
   const Botan::EC_Point simpleMult = p1 * 123456789;
2✔
529

530
   result.test_eq("point mult affine x",
3✔
531
                  simpleMult.get_affine_x(),
2✔
532
                  Botan::BigInt("43638877777452195295055270548491599621118743290"));
1✔
533
   result.test_eq("point mult affine y",
3✔
534
                  simpleMult.get_affine_y(),
2✔
535
                  Botan::BigInt("56841378500012376527163928510402662349220202981"));
1✔
536

537
   return result;
1✔
538
}
1✔
539

540
Test::Result test_enc_dec_compressed_160() {
1✔
541
   Test::Result result("ECC Unit");
1✔
542

543
   // Test for compressed conversion (02/03) 160bit
544
   Botan::EC_Group secp160r1("secp160r1");
1✔
545
   const std::vector<uint8_t> G_comp = Botan::hex_decode("024A96B5688EF573284664698968C38BB913CBFC82");
1✔
546
   const Botan::EC_Point p = secp160r1.OS2ECP(G_comp);
1✔
547
   const std::vector<uint8_t> sv_result = p.encode(Botan::EC_Point_Format::Compressed);
1✔
548

549
   result.test_eq("result", sv_result, G_comp);
1✔
550
   return result;
2✔
551
}
2✔
552

553
Test::Result test_enc_dec_compressed_256() {
1✔
554
   Test::Result result("ECC Unit");
1✔
555

556
   Botan::EC_Group group("secp256r1");
1✔
557

558
   const std::string G_secp_comp = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
1✔
559
   const std::vector<uint8_t> sv_G_secp_comp = Botan::hex_decode(G_secp_comp);
1✔
560

561
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_comp);
1✔
562
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Compressed);
1✔
563

564
   result.test_eq("compressed_256", sv_result, sv_G_secp_comp);
1✔
565
   return result;
2✔
566
}
3✔
567

568
Test::Result test_enc_dec_uncompressed_112() {
1✔
569
   Test::Result result("ECC Unit");
1✔
570

571
   // Test for uncompressed conversion (04) 112bit
572

573
   // Curve is secp112r2
574

575
   const Botan::BigInt p("0xdb7c2abf62e35e668076bead208b");
1✔
576
   const Botan::BigInt a("0x6127C24C05F38A0AAAF65C0EF02C");
1✔
577
   const Botan::BigInt b("0x51DEF1815DB5ED74FCC34C85D709");
1✔
578

579
   const Botan::BigInt g_x("0x4BA30AB5E892B4E1649DD0928643");
1✔
580
   const Botan::BigInt g_y("0xADCD46F5882E3747DEF36E956E97");
1✔
581

582
   const Botan::BigInt order("0x36DF0AAFD8B8D7597CA10520D04B");
1✔
583
   const Botan::BigInt cofactor("4");  // !
1✔
584

585
   const Botan::EC_Group group(p, a, b, g_x, g_y, order, cofactor);
1✔
586

587
   const std::string G_secp_uncomp = "044BA30AB5E892B4E1649DD0928643ADCD46F5882E3747DEF36E956E97";
1✔
588
   const std::vector<uint8_t> sv_G_secp_uncomp = Botan::hex_decode(G_secp_uncomp);
1✔
589

590
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_uncomp);
1✔
591
   std::vector<uint8_t> sv_result = p_G.encode(Botan::EC_Point_Format::Uncompressed);
1✔
592

593
   result.test_eq("uncompressed_112", sv_result, sv_G_secp_uncomp);
1✔
594
   return result;
1✔
595
}
10✔
596

597
Test::Result test_enc_dec_uncompressed_521() {
1✔
598
   Test::Result result("ECC Unit");
1✔
599

600
   // Test for uncompressed conversion(04) with big values(521 bit)
601

602
   const std::string G_secp_uncomp =
1✔
603
      "0400C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2ffA8DE3348B3C1856A429BF97E7E31C2E5BD66011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650";
1✔
604

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

607
   Botan::EC_Group group("secp521r1");
1✔
608

609
   Botan::EC_Point p_G = group.OS2ECP(sv_G_secp_uncomp);
1✔
610

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

613
   result.test_eq("expected", sv_result, sv_G_secp_uncomp);
1✔
614
   return result;
1✔
615
}
3✔
616

617
Test::Result test_ecc_registration() {
1✔
618
   Test::Result result("ECC registration");
1✔
619

620
   // secp112r1
621
   const Botan::BigInt p("0xDB7C2ABF62E35E668076BEAD208B");
1✔
622
   const Botan::BigInt a("0xDB7C2ABF62E35E668076BEAD2088");
1✔
623
   const Botan::BigInt b("0x659EF8BA043916EEDE8911702B22");
1✔
624

625
   const Botan::BigInt g_x("0x09487239995A5EE76B55F9C2F098");
1✔
626
   const Botan::BigInt g_y("0xA89CE5AF8724C0A23E0E0FF77500");
1✔
627
   const Botan::BigInt order("0xDB7C2ABF62E35E7628DFAC6561C5");
1✔
628

629
   const Botan::OID oid("1.3.132.0.6");
1✔
630

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

634
   Botan::EC_Group group(oid);
1✔
635

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

638
   return result;
2✔
639
}
7✔
640

641
Test::Result test_ec_group_from_params() {
1✔
642
   Test::Result result("EC_Group from params");
1✔
643

644
   Botan::EC_Group::clear_registered_curve_data();
1✔
645

646
   // secp160r1 params
647
   const Botan::BigInt p("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF");
1✔
648
   const Botan::BigInt a("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC");
1✔
649
   const Botan::BigInt b("0x1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45");
1✔
650

651
   const Botan::BigInt g_x("0x4A96B5688EF573284664698968C38BB913CBFC82");
1✔
652
   const Botan::BigInt g_y("0x23A628553168947D59DCC912042351377AC5FB32");
1✔
653
   const Botan::BigInt order("0x100000000000000000001F4C8F927AED3CA752257");
1✔
654

655
   const Botan::OID oid("1.3.132.0.8");
1✔
656

657
   Botan::EC_Group reg_group(p, a, b, g_x, g_y, order, 1);
2✔
658
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == oid);
2✔
659

660
   return result;
2✔
661
}
7✔
662

663
Test::Result test_ec_group_bad_registration() {
1✔
664
   Test::Result result("EC_Group registering non-match");
1✔
665

666
   Botan::EC_Group::clear_registered_curve_data();
1✔
667

668
   // secp160r1 params except with a bad B param
669
   const Botan::BigInt p("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF");
1✔
670
   const Botan::BigInt a("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC");
1✔
671
   const Botan::BigInt b("0x1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA47");
1✔
672

673
   const Botan::BigInt g_x("0x4A96B5688EF573284664698968C38BB913CBFC82");
1✔
674
   const Botan::BigInt g_y("0x23A628553168947D59DCC912042351377AC5FB32");
1✔
675
   const Botan::BigInt order("0x100000000000000000001F4C8F927AED3CA752257");
1✔
676

677
   const Botan::OID oid("1.3.132.0.8");
1✔
678

679
   try {
1✔
680
      Botan::EC_Group reg_group(p, a, b, g_x, g_y, order, 1, oid);
1✔
681
      result.test_failure("Should have failed");
×
682
   } catch(Botan::Invalid_Argument&) {
1✔
683
      result.test_success("Got expected exception");
1✔
684
   }
1✔
685

686
   return result;
2✔
687
}
7✔
688

689
Test::Result test_ec_group_duplicate_orders() {
1✔
690
   Test::Result result("EC_Group with duplicate group order");
1✔
691

692
   Botan::EC_Group::clear_registered_curve_data();
1✔
693

694
   // secp160r1 params
695
   const Botan::BigInt p("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF");
1✔
696
   const Botan::BigInt a("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC");
1✔
697
   const Botan::BigInt b("0x1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45");
1✔
698

699
   const Botan::BigInt g_x("0x4A96B5688EF573284664698968C38BB913CBFC82");
1✔
700
   const Botan::BigInt g_y("0x23A628553168947D59DCC912042351377AC5FB32");
1✔
701
   const Botan::BigInt order("0x100000000000000000001F4C8F927AED3CA752257");
1✔
702

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

705
   Botan::EC_Group reg_group(p, a, b, g_x, g_y, order, 1, oid);
1✔
706
   result.test_success("Registration success");
1✔
707
   result.confirm("Group has correct OID", reg_group.get_curve_oid() == oid);
2✔
708

709
   // We can now get it by OID:
710
   Botan::EC_Group hc_group(oid);
1✔
711
   result.confirm("Group has correct OID", hc_group.get_curve_oid() == oid);
2✔
712

713
   // Existing secp160r1 unmodified:
714
   const Botan::OID secp160r1("1.3.132.0.8");
1✔
715
   Botan::EC_Group other_group(secp160r1);
1✔
716
   result.confirm("Group has correct OID", other_group.get_curve_oid() == secp160r1);
2✔
717

718
   return result;
2✔
719
}
7✔
720

721
class ECC_Unit_Tests final : public Test {
×
722
   public:
723
      std::vector<Test::Result> run() override {
1✔
724
         std::vector<Test::Result> results;
1✔
725

726
         results.push_back(test_coordinates());
2✔
727
         results.push_back(test_decoding_with_seed());
2✔
728
         results.push_back(test_point_mult());
2✔
729
         results.push_back(test_point_negative());
2✔
730
         results.push_back(test_mult_point());
2✔
731
         results.push_back(test_mixed_points());
2✔
732
         results.push_back(test_basic_operations());
2✔
733
         results.push_back(test_enc_dec_compressed_160());
2✔
734
         results.push_back(test_enc_dec_compressed_256());
2✔
735
         results.push_back(test_enc_dec_uncompressed_112());
2✔
736
         results.push_back(test_enc_dec_uncompressed_521());
2✔
737
         results.push_back(test_ecc_registration());
2✔
738
         results.push_back(test_ec_group_from_params());
2✔
739
         results.push_back(test_ec_group_bad_registration());
2✔
740
         results.push_back(test_ec_group_duplicate_orders());
2✔
741

742
         return results;
1✔
743
      }
×
744
};
745

746
BOTAN_REGISTER_TEST("pubkey", "ecc_unit", ECC_Unit_Tests);
747

748
   #if defined(BOTAN_HAS_ECDSA)
749

750
class ECC_Invalid_Key_Tests final : public Text_Based_Test {
×
751
   public:
752
      ECC_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecc_invalid.vec", "SubjectPublicKey") {}
3✔
753

754
      bool clear_between_callbacks() const override { return false; }
5✔
755

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

759
         const std::string encoded = vars.get_req_str("SubjectPublicKey");
5✔
760
         Botan::DataSource_Memory key_data(Botan::hex_decode(encoded));
10✔
761

762
         try {
5✔
763
            auto key = Botan::X509::load_key(key_data);
5✔
764
            result.test_eq("public key fails check", key->check_key(Test::rng(), false), false);
×
765
         } catch(Botan::Decoding_Error&) {
5✔
766
            result.test_success("Decoding invalid ECC key results in decoding error exception");
5✔
767
         }
5✔
768

769
         return result;
5✔
770
      }
10✔
771
};
772

773
BOTAN_REGISTER_TEST("pubkey", "ecc_invalid", ECC_Invalid_Key_Tests);
774

775
   #endif
776

777
#endif
778

779
}  // namespace
780

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