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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

91.91
/src/tests/test_ecdsa.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_ECDSA)
11
   #include "test_pubkey.h"
12
   #include <botan/ecdsa.h>
13
   #include <botan/hash.h>
14
   #include <botan/pk_algs.h>
15
   #include <botan/pkcs8.h>
16
   #include <botan/rng.h>
17
#endif
18

19
namespace Botan_Tests {
20

21
namespace {
22

23
#if defined(BOTAN_HAS_ECDSA)
24

25
class ECDSA_Verification_Tests final : public PK_Signature_Verification_Test {
26
   public:
27
      ECDSA_Verification_Tests() :
1✔
28
            PK_Signature_Verification_Test("ECDSA", "pubkey/ecdsa_verify.vec", "Group,Px,Py,Msg,Signature", "Valid") {}
2✔
29

30
      bool clear_between_callbacks() const override { return false; }
15✔
31

32
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
15✔
33
         return !Botan::EC_Group::supports_named_group(vars.get_req_str("Group"));
15✔
34
      }
35

36
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
15✔
37
         const std::string group_id = vars.get_req_str("Group");
15✔
38
         const BigInt px = vars.get_req_bn("Px");
15✔
39
         const BigInt py = vars.get_req_bn("Py");
15✔
40
         const auto group = Botan::EC_Group::from_name(group_id);
15✔
41

42
         const auto public_key = Botan::EC_AffinePoint::from_bigint_xy(group, px, py).value();
30✔
43

44
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_key);
30✔
45
      }
15✔
46

47
      std::string default_padding(const VarMap& /*unused*/) const override { return "Raw"; }
15✔
48
};
49

50
class ECDSA_Wycheproof_Verification_Tests final : public PK_Signature_Verification_Test {
51
   public:
52
      ECDSA_Wycheproof_Verification_Tests() :
1✔
53
            PK_Signature_Verification_Test(
54
               "ECDSA", "pubkey/ecdsa_wycheproof.vec", "Group,Px,Py,Hash,Msg,Signature,Valid") {}
2✔
55

56
      bool clear_between_callbacks() const override { return false; }
11,864✔
57

58
      Botan::Signature_Format sig_format() const override { return Botan::Signature_Format::DerSequence; }
47,456✔
59

60
      bool test_random_invalid_sigs() const override { return false; }
3,475✔
61

62
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
11,864✔
63
         return !Botan::EC_Group::supports_named_group(vars.get_req_str("Group"));
11,864✔
64
      }
65

66
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
11,864✔
67
         const std::string group_id = vars.get_req_str("Group");
11,864✔
68
         const BigInt px = vars.get_req_bn("Px");
11,864✔
69
         const BigInt py = vars.get_req_bn("Py");
11,864✔
70
         const auto group = Botan::EC_Group::from_name(group_id);
11,864✔
71

72
         const auto public_key = Botan::EC_AffinePoint::from_bigint_xy(group, px, py).value();
23,728✔
73

74
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_key);
23,728✔
75
      }
11,864✔
76

77
      std::string default_padding(const VarMap& vars) const override { return vars.get_req_str("Hash"); }
23,728✔
78
};
79

80
class ECDSA_Signature_KAT_Tests final : public PK_Signature_Generation_Test {
81
   public:
82
      ECDSA_Signature_KAT_Tests() :
1✔
83
            PK_Signature_Generation_Test("ECDSA",
84
   #if defined(BOTAN_HAS_RFC6979_GENERATOR)
85
                                         "pubkey/ecdsa_rfc6979.vec",
86
                                         "Group,X,Hash,Msg,Signature") {
2✔
87
      }
1✔
88
   #else
89
                                         "pubkey/ecdsa_prob.vec",
90
                                         "Group,X,Hash,Msg,Nonce,Signature") {
91
      }
92
   #endif
93

94
      bool clear_between_callbacks() const override { return false; }
104✔
95

96
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
104✔
97
         return !Botan::EC_Group::supports_named_group(vars.get_req_str("Group"));
104✔
98
      }
99

100
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
104✔
101
         const std::string group_id = vars.get_req_str("Group");
104✔
102
         const BigInt x = vars.get_req_bn("X");
104✔
103
         const auto group = Botan::EC_Group::from_name(group_id);
104✔
104

105
         return std::make_unique<Botan::ECDSA_PrivateKey>(this->rng(), group, x);
208✔
106
      }
104✔
107

108
      std::string default_padding(const VarMap& vars) const override { return vars.get_req_str("Hash"); }
208✔
109

110
   #if !defined(BOTAN_HAS_RFC6979_GENERATOR)
111
      std::unique_ptr<Botan::RandomNumberGenerator> test_rng(const std::vector<uint8_t>& nonce) const override {
112
         // probabilistic ecdsa signature generation extracts more random than just the nonce,
113
         // but the nonce is extracted first
114
         return std::make_unique<Fixed_Output_Position_RNG>(nonce, 1, this->rng());
115
      }
116
   #endif
117
};
118

119
class ECDSA_KAT_Verification_Tests final : public PK_Signature_Verification_Test {
120
   public:
121
      ECDSA_KAT_Verification_Tests() :
1✔
122
            PK_Signature_Verification_Test("ECDSA",
123
   #if !defined(BOTAN_HAS_RFC6979_GENERATOR)
124
                                           "pubkey/ecdsa_rfc6979.vec",
125
                                           "Group,X,Hash,Msg,Signature") {
126
      }
127
   #else
128
                                           "pubkey/ecdsa_prob.vec",
129
                                           "Group,X,Hash,Msg,Nonce,Signature") {
2✔
130
      }
1✔
131
   #endif
132

133
      bool clear_between_callbacks() const override { return false; }
251✔
134

135
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
251✔
136
         return !Botan::EC_Group::supports_named_group(vars.get_req_str("Group"));
251✔
137
      }
138

139
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
251✔
140
         const std::string group_id = vars.get_req_str("Group");
251✔
141
         const BigInt x = vars.get_req_bn("X");
251✔
142
         const auto group = Botan::EC_Group::from_name(group_id);
251✔
143

144
         const Botan::ECDSA_PrivateKey priv_key(this->rng(), group, x);
251✔
145

146
         return priv_key.public_key();
251✔
147
      }
251✔
148

149
      std::string default_padding(const VarMap& vars) const override { return vars.get_req_str("Hash"); }
502✔
150
};
151

152
class ECDSA_Sign_Verify_DER_Test final : public PK_Sign_Verify_DER_Test {
×
153
   public:
154
      ECDSA_Sign_Verify_DER_Test() : PK_Sign_Verify_DER_Test("ECDSA", "SHA-512") {}
2✔
155

156
      std::unique_ptr<Botan::Private_Key> key() override {
1✔
157
         return Botan::create_private_key("ECDSA", this->rng(), "secp256r1");
1✔
158
      }
159
};
160

161
class ECDSA_Keygen_Tests final : public PK_Key_Generation_Test {
1✔
162
   public:
163
      std::vector<std::string> keygen_params() const override {
1✔
164
         const auto& grp = Botan::EC_Group::known_named_groups();
1✔
165
         return std::vector<std::string>(grp.begin(), grp.end());
1✔
166
      }
167

168
      std::string algo_name() const override { return "ECDSA"; }
28✔
169

170
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
28✔
171
                                                             std::string_view /* provider */,
172
                                                             std::span<const uint8_t> raw_pk) const override {
173
         const auto group = Botan::EC_Group(keygen_params);
28✔
174
         const auto public_key = Botan::EC_AffinePoint(group, raw_pk);
28✔
175
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_key);
84✔
176
      }
28✔
177
};
178

179
class ECDSA_Keygen_Stability_Tests final : public PK_Key_Generation_Stability_Test {
180
   public:
181
      ECDSA_Keygen_Stability_Tests() : PK_Key_Generation_Stability_Test("ECDSA", "pubkey/ecdsa_keygen.vec") {}
2✔
182
};
183

184
   #if defined(BOTAN_HAS_EMSA_RAW)
185

186
class ECDSA_Key_Recovery_Tests final : public Text_Based_Test {
×
187
   public:
188
      ECDSA_Key_Recovery_Tests() : Text_Based_Test("pubkey/ecdsa_key_recovery.vec", "Group,Msg,R,S,V,Pubkey") {}
2✔
189

190
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
2✔
191
         return !Botan::EC_Group::supports_named_group(vars.get_req_str("Group"));
2✔
192
      }
193

194
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
2✔
195
         Test::Result result("ECDSA key recovery");
2✔
196

197
         const std::string group_id = vars.get_req_str("Group");
2✔
198
         const auto group = Botan::EC_Group::from_name(group_id);
2✔
199

200
         const BigInt R = vars.get_req_bn("R");
2✔
201
         const BigInt S = vars.get_req_bn("S");
2✔
202
         const uint8_t V = vars.get_req_u8("V");
2✔
203
         const std::vector<uint8_t> msg = vars.get_req_bin("Msg");
2✔
204
         const auto expected_pubkey = vars.get_req_bin("Pubkey");
2✔
205

206
         try {
2✔
207
            const Botan::ECDSA_PublicKey pubkey(group, msg, R, S, V);
2✔
208
            result.test_eq("Pubkey X coordinate", pubkey.public_key_bits(), expected_pubkey);
4✔
209

210
            const uint8_t computed_V = pubkey.recovery_param(msg, R, S);
2✔
211
            result.test_eq("Recovery param is correct", static_cast<size_t>(computed_V), static_cast<size_t>(V));
2✔
212

213
            Botan::PK_Verifier verifier(pubkey, "Raw");
2✔
214

215
            auto sig = Botan::BigInt::encode_fixed_length_int_pair(R, S, group.get_order_bytes());
2✔
216

217
            result.confirm("Signature verifies", verifier.verify_message(msg, sig));
4✔
218
         } catch(Botan::Exception& e) {
2✔
219
            result.test_failure("Failed to recover ECDSA public key", e.what());
×
220
         }
×
221

222
         return result;
2✔
223
      }
4✔
224
};
225

226
BOTAN_REGISTER_TEST("pubkey", "ecdsa_key_recovery", ECDSA_Key_Recovery_Tests);
227

228
   #endif
229

230
class ECDSA_Invalid_Key_Tests final : public Text_Based_Test {
×
231
   public:
232
      ECDSA_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecdsa_invalid.vec", "Group,InvalidKeyX,InvalidKeyY") {}
2✔
233

234
      bool clear_between_callbacks() const override { return false; }
78✔
235

236
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
78✔
237
         return !Botan::EC_Group::supports_named_group(vars.get_req_str("Group"));
78✔
238
      }
239

240
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
78✔
241
         Test::Result result("ECDSA invalid keys");
78✔
242

243
         const std::string group_id = vars.get_req_str("Group");
78✔
244
         const auto group = Botan::EC_Group::from_name(group_id);
78✔
245
         const Botan::BigInt x = vars.get_req_bn("InvalidKeyX");
78✔
246
         const Botan::BigInt y = vars.get_req_bn("InvalidKeyY");
78✔
247

248
         if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, x, y)) {
78✔
249
            result.test_failure("Invalid public key was deserialized");
×
250
         } else {
251
            result.test_success("Invalid public key was rejected");
156✔
252
         }
×
253

254
         return result;
156✔
255
      }
78✔
256
};
257

258
class ECDSA_AllGroups_Test : public Test {
1✔
259
   public:
260
      std::vector<Test::Result> run() override {
1✔
261
         std::vector<Test::Result> results;
1✔
262

263
         const std::vector<std::string> hash_fn = {
1✔
264
            "SHA-256", "SHA-384", "SHA-512", "SHAKE-128(208)", "SHAKE-128(520)", "SHAKE-128(1032)"};
1✔
265

266
         for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
267
            Test::Result result("ECDSA " + group_name);
28✔
268

269
            result.start_timer();
28✔
270

271
            const auto group = Botan::EC_Group::from_name(group_name);
28✔
272

273
            const Botan::ECDSA_PrivateKey priv(rng(), group);
28✔
274
            const auto pub = priv.public_key();
28✔
275

276
            for(const auto& hash : hash_fn) {
196✔
277
               if(!Botan::HashFunction::create(hash)) {
336✔
278
                  continue;
×
279
               }
280

281
               try {
168✔
282
                  Botan::PK_Signer signer(priv, rng(), hash);
168✔
283
                  Botan::PK_Verifier verifier(*pub, hash);
168✔
284

285
                  for(size_t i = 0; i != 16; ++i) {
2,856✔
286
                     auto message = Botan::unlock(rng().random_vec(rng().next_byte()));
5,376✔
287
                     auto sig = signer.sign_message(message, rng());
2,688✔
288
                     result.test_eq("Expected signature size", sig.size(), 2 * group.get_order_bytes());
2,688✔
289

290
                     result.confirm("Signature accepted", verifier.verify_message(message, sig));
5,376✔
291

292
                     const auto corrupted_message = mutate_vec(message, rng(), true);
2,688✔
293
                     result.confirm("Modified message rejected", !verifier.verify_message(corrupted_message, sig));
5,376✔
294

295
                     const auto corrupted_sig = mutate_vec(sig, rng(), true);
2,688✔
296
                     result.confirm("Modified signature rejected", !verifier.verify_message(message, corrupted_sig));
5,376✔
297
                  }
10,743✔
298
               } catch(std::exception& e) {
168✔
299
                  result.test_failure("Exception", e.what());
×
300
               }
×
301
            }
302

303
            result.end_timer();
28✔
304
            results.push_back(result);
28✔
305
         }
28✔
306

307
         return results;
1✔
308
      }
1✔
309
};
310

311
class ECDSA_ExplicitCurveKey_Test : public Text_Based_Test {
×
312
   public:
313
      ECDSA_ExplicitCurveKey_Test() : Text_Based_Test("pubkey/ecdsa_explicit.vec", "Key") {}
2✔
314

315
      bool clear_between_callbacks() const override { return false; }
13✔
316

317
      bool skip_this_test(const std::string& group, const VarMap& /*vars*/) override {
13✔
318
         return !Botan::EC_Group::supports_named_group(group);
13✔
319
      }
320

321
      Test::Result run_one_test(const std::string& group_name, const VarMap& vars) override {
13✔
322
         Test::Result result("ECDSA explicit key " + group_name);
13✔
323

324
         const auto key_bytes = vars.get_req_bin("Key");
13✔
325

326
         try {
13✔
327
            const auto expected_oid = Botan::OID::from_name(group_name).value();
26✔
328

329
            auto key = Botan::PKCS8::load_key(key_bytes);
13✔
330
            const auto* ecdsa = dynamic_cast<const Botan::ECDSA_PrivateKey*>(key.get());
13✔
331
            if(ecdsa != nullptr) {
13✔
332
               result.test_success("Returned key was ECDSA");
13✔
333

334
               const auto& group = ecdsa->domain();
13✔
335
               result.test_eq("Key is marked as explicit encoding", group.used_explicit_encoding(), true);
13✔
336
               result.confirm("Group has expected OID", group.get_curve_oid() == expected_oid);
26✔
337
            } else {
338
               result.test_failure("Returned key was some other type");
×
339
            }
340
         } catch(Botan::Exception& e) {
13✔
341
            result.test_failure("Failed to parse key", e.what());
×
342
         }
×
343

344
         return result;
13✔
345
      }
13✔
346
};
347

348
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify", ECDSA_Verification_Tests);
349
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify_wycheproof", ECDSA_Wycheproof_Verification_Tests);
350
BOTAN_REGISTER_TEST("pubkey", "ecdsa_sign", ECDSA_Signature_KAT_Tests);
351
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify_kat", ECDSA_KAT_Verification_Tests);
352
BOTAN_REGISTER_TEST("pubkey", "ecdsa_sign_verify_der", ECDSA_Sign_Verify_DER_Test);
353
BOTAN_REGISTER_TEST("pubkey", "ecdsa_keygen", ECDSA_Keygen_Tests);
354
BOTAN_REGISTER_TEST("pubkey", "ecdsa_keygen_stability", ECDSA_Keygen_Stability_Tests);
355
BOTAN_REGISTER_TEST("pubkey", "ecdsa_invalid", ECDSA_Invalid_Key_Tests);
356
BOTAN_REGISTER_TEST("pubkey", "ecdsa_all_groups", ECDSA_AllGroups_Test);
357
BOTAN_REGISTER_TEST("pubkey", "ecdsa_explicit_curve_key", ECDSA_ExplicitCurveKey_Test);
358

359
#endif
360

361
}  // namespace
362

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