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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

90.75
/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
#endif
17

18
namespace Botan_Tests {
19

20
namespace {
21

22
#if defined(BOTAN_HAS_ECDSA)
23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

183
   #if defined(BOTAN_HAS_EMSA_RAW)
184

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

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

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

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

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

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

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

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

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

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

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

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

227
   #endif
228

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

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

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

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

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

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

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

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

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

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

268
            result.start_timer();
28✔
269

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

358
#endif
359

360
}  // namespace
361

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