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

randombit / botan / 16581714815

28 Jul 2025 10:25PM UTC coverage: 90.475% (-0.2%) from 90.685%
16581714815

Pull #5021

github

web-flow
Merge 072983077 into 1eacc5b05
Pull Request #5021: Add PK_Signature_Options

99366 of 109827 relevant lines covered (90.48%)

12349417.34 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
   #include <botan/internal/pk_options.h>
19
#endif
20

21
namespace Botan_Tests {
22

23
namespace {
24

25
#if defined(BOTAN_HAS_ECDSA)
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

186
   #if defined(BOTAN_HAS_EMSA_RAW)
187

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

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

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

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

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

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

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

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

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

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

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

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

230
   #endif
231

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

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

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

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

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

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

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

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

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

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

271
            result.start_timer();
28✔
272

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

361
#endif
362

363
}  // namespace
364

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