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

randombit / botan / 12661312886

07 Jan 2025 11:55PM UTC coverage: 91.251% (-0.02%) from 91.267%
12661312886

push

github

web-flow
Merge pull request #4518 from randombit/jack/ec-point-cleanups

Move EC_Point and related code to deprecated submodule

93407 of 102363 relevant lines covered (91.25%)

11518305.49 hits per line

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

91.37
/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
#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
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
15✔
33
         const std::string group_id = vars.get_req_str("Group");
15✔
34
         const BigInt px = vars.get_req_bn("Px");
15✔
35
         const BigInt py = vars.get_req_bn("Py");
15✔
36
         const auto group = Botan::EC_Group::from_name(group_id);
15✔
37

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

40
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_key);
30✔
41
      }
45✔
42

43
      std::string default_padding(const VarMap& /*unused*/) const override { return "Raw"; }
15✔
44
};
45

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

52
      bool clear_between_callbacks() const override { return false; }
11,864✔
53

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

56
      bool test_random_invalid_sigs() const override { return false; }
3,475✔
57

58
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
11,864✔
59
         const std::string group_id = vars.get_req_str("Group");
11,864✔
60
         const BigInt px = vars.get_req_bn("Px");
11,864✔
61
         const BigInt py = vars.get_req_bn("Py");
11,864✔
62
         const auto group = Botan::EC_Group::from_name(group_id);
11,864✔
63

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

66
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_key);
23,728✔
67
      }
35,592✔
68

69
      std::string default_padding(const VarMap& vars) const override { return vars.get_req_str("Hash"); }
23,728✔
70
};
71

72
class ECDSA_Signature_KAT_Tests final : public PK_Signature_Generation_Test {
73
   public:
74
      ECDSA_Signature_KAT_Tests() :
1✔
75
            PK_Signature_Generation_Test("ECDSA",
76
   #if defined(BOTAN_HAS_RFC6979_GENERATOR)
77
                                         "pubkey/ecdsa_rfc6979.vec",
78
                                         "Group,X,Hash,Msg,Signature") {
2✔
79
      }
1✔
80
   #else
81
                                         "pubkey/ecdsa_prob.vec",
82
                                         "Group,X,Hash,Msg,Nonce,Signature") {
83
      }
84
   #endif
85

86
      bool clear_between_callbacks() const override { return false; }
104✔
87

88
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
104✔
89
         const std::string group_id = vars.get_req_str("Group");
104✔
90
         const BigInt x = vars.get_req_bn("X");
104✔
91
         const auto group = Botan::EC_Group::from_name(group_id);
104✔
92

93
         return std::make_unique<Botan::ECDSA_PrivateKey>(this->rng(), group, x);
208✔
94
      }
208✔
95

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

98
   #if !defined(BOTAN_HAS_RFC6979_GENERATOR)
99
      std::unique_ptr<Botan::RandomNumberGenerator> test_rng(const std::vector<uint8_t>& nonce) const override {
100
         // probabilistic ecdsa signature generation extracts more random than just the nonce,
101
         // but the nonce is extracted first
102
         return std::make_unique<Fixed_Output_Position_RNG>(nonce, 1, this->rng());
103
      }
104
   #endif
105
};
106

107
class ECDSA_KAT_Verification_Tests final : public PK_Signature_Verification_Test {
108
   public:
109
      ECDSA_KAT_Verification_Tests() :
1✔
110
            PK_Signature_Verification_Test("ECDSA",
111
   #if !defined(BOTAN_HAS_RFC6979_GENERATOR)
112
                                           "pubkey/ecdsa_rfc6979.vec",
113
                                           "Group,X,Hash,Msg,Signature") {
114
      }
115
   #else
116
                                           "pubkey/ecdsa_prob.vec",
117
                                           "Group,X,Hash,Msg,Nonce,Signature") {
2✔
118
      }
1✔
119
   #endif
120

121
      bool clear_between_callbacks() const override { return false; }
251✔
122

123
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
251✔
124
         const std::string group_id = vars.get_req_str("Group");
251✔
125
         const BigInt x = vars.get_req_bn("X");
251✔
126
         const auto group = Botan::EC_Group::from_name(group_id);
251✔
127

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

130
         return priv_key.public_key();
251✔
131
      }
502✔
132

133
      std::string default_padding(const VarMap& vars) const override { return vars.get_req_str("Hash"); }
502✔
134
};
135

136
class ECDSA_Sign_Verify_DER_Test final : public PK_Sign_Verify_DER_Test {
×
137
   public:
138
      ECDSA_Sign_Verify_DER_Test() : PK_Sign_Verify_DER_Test("ECDSA", "SHA-512") {}
2✔
139

140
      std::unique_ptr<Botan::Private_Key> key() override {
1✔
141
         return Botan::create_private_key("ECDSA", this->rng(), "secp256r1");
1✔
142
      }
143
};
144

145
class ECDSA_Keygen_Tests final : public PK_Key_Generation_Test {
×
146
   public:
147
      std::vector<std::string> keygen_params() const override {
1✔
148
         auto grp = Botan::EC_Group::known_named_groups();
1✔
149
         return std::vector<std::string>(grp.begin(), grp.end());
1✔
150
      }
1✔
151

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

154
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
28✔
155
                                                             std::string_view /* provider */,
156
                                                             std::span<const uint8_t> raw_pk) const override {
157
         const auto group = Botan::EC_Group(keygen_params);
28✔
158
         const auto public_key = Botan::EC_AffinePoint(group, raw_pk);
28✔
159
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_key);
84✔
160
      }
28✔
161
};
162

163
class ECDSA_Keygen_Stability_Tests final : public PK_Key_Generation_Stability_Test {
164
   public:
165
      ECDSA_Keygen_Stability_Tests() : PK_Key_Generation_Stability_Test("ECDSA", "pubkey/ecdsa_keygen.vec") {}
2✔
166
};
167

168
   #if defined(BOTAN_HAS_EMSA_RAW)
169

170
class ECDSA_Key_Recovery_Tests final : public Text_Based_Test {
×
171
   public:
172
      ECDSA_Key_Recovery_Tests() : Text_Based_Test("pubkey/ecdsa_key_recovery.vec", "Group,Msg,R,S,V,Pubkey") {}
2✔
173

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

177
         const std::string group_id = vars.get_req_str("Group");
2✔
178
         const auto group = Botan::EC_Group::from_name(group_id);
2✔
179

180
         const BigInt R = vars.get_req_bn("R");
2✔
181
         const BigInt S = vars.get_req_bn("S");
2✔
182
         const uint8_t V = vars.get_req_u8("V");
2✔
183
         const std::vector<uint8_t> msg = vars.get_req_bin("Msg");
2✔
184
         const auto expected_pubkey = vars.get_req_bin("Pubkey");
2✔
185

186
         try {
2✔
187
            Botan::ECDSA_PublicKey pubkey(group, msg, R, S, V);
2✔
188
            result.test_eq("Pubkey X coordinate", pubkey.public_key_bits(), expected_pubkey);
4✔
189

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

193
            Botan::PK_Verifier verifier(pubkey, "Raw");
2✔
194

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

197
            result.confirm("Signature verifies", verifier.verify_message(msg, sig));
4✔
198
         } catch(Botan::Exception& e) {
2✔
199
            result.test_failure("Failed to recover ECDSA public key", e.what());
×
200
         }
×
201

202
         return result;
2✔
203
      }
8✔
204
};
205

206
BOTAN_REGISTER_TEST("pubkey", "ecdsa_key_recovery", ECDSA_Key_Recovery_Tests);
207

208
   #endif
209

210
class ECDSA_Invalid_Key_Tests final : public Text_Based_Test {
×
211
   public:
212
      ECDSA_Invalid_Key_Tests() : Text_Based_Test("pubkey/ecdsa_invalid.vec", "Group,InvalidKeyX,InvalidKeyY") {}
2✔
213

214
      bool clear_between_callbacks() const override { return false; }
78✔
215

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

219
         const std::string group_id = vars.get_req_str("Group");
78✔
220
         const auto group = Botan::EC_Group::from_name(group_id);
78✔
221
         const Botan::BigInt x = vars.get_req_bn("InvalidKeyX");
78✔
222
         const Botan::BigInt y = vars.get_req_bn("InvalidKeyY");
78✔
223

224
         if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, x, y)) {
78✔
225
            result.test_failure("Invalid public key was deserialized");
×
226
         } else {
227
            result.test_success("Invalid public key was rejected");
156✔
228
         }
×
229

230
         return result;
78✔
231
      }
156✔
232
};
233

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

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

242
         for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
243
            Test::Result result("ECDSA " + group_name);
28✔
244

245
            result.start_timer();
28✔
246

247
            const auto group = Botan::EC_Group::from_name(group_name);
28✔
248

249
            const Botan::ECDSA_PrivateKey priv(rng(), group);
28✔
250
            const auto pub = priv.public_key();
28✔
251

252
            for(const auto& hash : hash_fn) {
196✔
253
               if(!Botan::HashFunction::create(hash)) {
336✔
254
                  continue;
×
255
               }
256

257
               try {
168✔
258
                  Botan::PK_Signer signer(priv, rng(), hash);
168✔
259
                  Botan::PK_Verifier verifier(*pub, hash);
168✔
260

261
                  for(size_t i = 0; i != 16; ++i) {
2,856✔
262
                     auto message = rng().random_vec(rng().next_byte());
2,688✔
263
                     auto sig = signer.sign_message(message, rng());
2,688✔
264
                     result.test_eq("Expected signature size", sig.size(), 2 * group.get_order_bytes());
2,688✔
265

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

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

271
                     const auto corrupted_sig = mutate_vec(sig, rng(), true);
2,688✔
272
                     result.confirm("Modified signature rejected", !verifier.verify_message(message, corrupted_sig));
5,376✔
273
                  }
10,737✔
274
               } catch(std::exception& e) {
168✔
275
                  result.test_failure("Exception", e.what());
×
276
               }
×
277
            }
278

279
            result.end_timer();
28✔
280
            results.push_back(result);
28✔
281
         }
28✔
282

283
         return results;
1✔
284
      }
1✔
285
};
286

287
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify", ECDSA_Verification_Tests);
288
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify_wycheproof", ECDSA_Wycheproof_Verification_Tests);
289
BOTAN_REGISTER_TEST("pubkey", "ecdsa_sign", ECDSA_Signature_KAT_Tests);
290
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify_kat", ECDSA_KAT_Verification_Tests);
291
BOTAN_REGISTER_TEST("pubkey", "ecdsa_sign_verify_der", ECDSA_Sign_Verify_DER_Test);
292
BOTAN_REGISTER_TEST("pubkey", "ecdsa_keygen", ECDSA_Keygen_Tests);
293
BOTAN_REGISTER_TEST("pubkey", "ecdsa_keygen_stability", ECDSA_Keygen_Stability_Tests);
294
BOTAN_REGISTER_TEST("pubkey", "ecdsa_invalid", ECDSA_Invalid_Key_Tests);
295
BOTAN_REGISTER_TEST("pubkey", "ecdsa_all_groups", ECDSA_AllGroups_Test);
296

297
#endif
298

299
}  // namespace
300

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