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

randombit / botan / 16196862507

10 Jul 2025 12:13PM UTC coverage: 90.573% (+0.001%) from 90.572%
16196862507

push

github

web-flow
Merge pull request #4967 from KaganCanSit/cppCheck-static-analysis-tests

Fix/reduce CppCheck warnings in 'tests' folder

99067 of 109378 relevant lines covered (90.57%)

12438333.97 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
#include "test_rng.h"
11

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

20
namespace Botan_Tests {
21

22
namespace {
23

24
#if defined(BOTAN_HAS_ECDSA)
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

185
   #if defined(BOTAN_HAS_EMSA_RAW)
186

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

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

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

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

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

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

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

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

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

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

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

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

229
   #endif
230

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

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

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

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

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

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

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

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

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

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

270
            result.start_timer();
28✔
271

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

360
#endif
361

362
}  // namespace
363

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