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

randombit / botan / 10329059925

10 Aug 2024 04:49AM UTC coverage: 91.276% (-0.02%) from 91.298%
10329059925

push

github

web-flow
Merge pull request #4143 from randombit/jack/bridge-pcurves-and-ec

Bridge pcurves into the main elliptic curve arithmetic

87809 of 96202 relevant lines covered (91.28%)

9299919.33 hits per line

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

92.67
/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 Botan::EC_Point public_point = group.point(px, py);
15✔
39

40
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_point);
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 Botan::EC_Point public_point = group.point(px, py);
11,864✔
65

66
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_point);
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"; }
84✔
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_point = group.OS2ECP(raw_pk);
28✔
159
         return std::make_unique<Botan::ECDSA_PublicKey>(group, public_point);
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() :
1✔
173
            Text_Based_Test("pubkey/ecdsa_key_recovery.vec", "Group,Msg,R,S,V,PubkeyX,PubkeyY") {}
2✔
174

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

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

181
         const BigInt R = vars.get_req_bn("R");
2✔
182
         const BigInt S = vars.get_req_bn("S");
2✔
183
         const uint8_t V = vars.get_req_u8("V");
2✔
184
         const std::vector<uint8_t> msg = vars.get_req_bin("Msg");
2✔
185
         const BigInt pubkey_x = vars.get_req_bn("PubkeyX");
2✔
186
         const BigInt pubkey_y = vars.get_req_bn("PubkeyY");
2✔
187

188
         try {
2✔
189
            Botan::ECDSA_PublicKey pubkey(group, msg, R, S, V);
2✔
190
            result.test_eq("Pubkey X coordinate", pubkey.public_point().get_affine_x(), pubkey_x);
4✔
191
            result.test_eq("Pubkey Y coordinate", pubkey.public_point().get_affine_y(), pubkey_y);
4✔
192

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

196
            Botan::PK_Verifier verifier(pubkey, "Raw");
2✔
197

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

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

205
         return result;
2✔
206
      }
10✔
207
};
208

209
BOTAN_REGISTER_TEST("pubkey", "ecdsa_key_recovery", ECDSA_Key_Recovery_Tests);
210

211
   #endif
212

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

217
      bool clear_between_callbacks() const override { return false; }
78✔
218

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

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

227
         std::unique_ptr<Botan::EC_Point> public_point;
78✔
228

229
         try {
78✔
230
            public_point = std::make_unique<Botan::EC_Point>(group.point(x, y));
117✔
231
         } catch(Botan::Invalid_Argument&) {
39✔
232
            // EC_Point() performs a range check on x, y in [0, p−1],
233
            // which is also part of the EC public key checks, e.g.,
234
            // in NIST SP800-56A rev2, sec. 5.6.2.3.2
235
            result.test_success("public key fails check");
39✔
236
            return result;
39✔
237
         }
39✔
238

239
         try {
39✔
240
            auto key = std::make_unique<Botan::ECDSA_PublicKey>(group, *public_point);
39✔
241
            result.test_failure("Invalid public key was deserialized");
×
242
         } catch(Botan::Decoding_Error&) {
39✔
243
            result.test_success("public key fails to deserialize");
39✔
244
         }
39✔
245
         return result;
246
      }
234✔
247
};
248

249
class ECDSA_AllGroups_Test : public Test {
×
250
   public:
251
      std::vector<Test::Result> run() override {
1✔
252
         std::vector<Test::Result> results;
1✔
253

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

257
         for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
258
            Test::Result result("ECDSA " + group_name);
28✔
259

260
            result.start_timer();
28✔
261

262
            const auto group = Botan::EC_Group::from_name(group_name);
28✔
263

264
            const Botan::ECDSA_PrivateKey priv(rng(), group);
28✔
265
            const auto pub = priv.public_key();
28✔
266

267
            for(const auto& hash : hash_fn) {
196✔
268
               if(!Botan::HashFunction::create(hash)) {
336✔
269
                  continue;
×
270
               }
271

272
               try {
168✔
273
                  Botan::PK_Signer signer(priv, rng(), hash);
168✔
274
                  Botan::PK_Verifier verifier(*pub, hash);
168✔
275

276
                  for(size_t i = 0; i != 16; ++i) {
2,856✔
277
                     auto message = rng().random_vec(rng().next_byte());
2,688✔
278
                     auto sig = signer.sign_message(message, rng());
2,688✔
279
                     result.test_eq("Expected signature size", sig.size(), 2 * group.get_order_bytes());
2,688✔
280

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

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

286
                     const auto corrupted_sig = mutate_vec(sig, rng(), true);
2,688✔
287
                     result.confirm("Modified signature rejected", !verifier.verify_message(message, corrupted_sig));
5,376✔
288
                  }
10,740✔
289
               } catch(std::exception& e) {
168✔
290
                  result.test_failure("Exception", e.what());
×
291
               }
×
292
            }
293

294
            result.end_timer();
28✔
295
            results.push_back(result);
28✔
296
         }
28✔
297

298
         return results;
1✔
299
      }
1✔
300
};
301

302
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify", ECDSA_Verification_Tests);
303
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify_wycheproof", ECDSA_Wycheproof_Verification_Tests);
304
BOTAN_REGISTER_TEST("pubkey", "ecdsa_sign", ECDSA_Signature_KAT_Tests);
305
BOTAN_REGISTER_TEST("pubkey", "ecdsa_verify_kat", ECDSA_KAT_Verification_Tests);
306
BOTAN_REGISTER_TEST("pubkey", "ecdsa_sign_verify_der", ECDSA_Sign_Verify_DER_Test);
307
BOTAN_REGISTER_TEST("pubkey", "ecdsa_keygen", ECDSA_Keygen_Tests);
308
BOTAN_REGISTER_TEST("pubkey", "ecdsa_keygen_stability", ECDSA_Keygen_Stability_Tests);
309
BOTAN_REGISTER_TEST("pubkey", "ecdsa_invalid", ECDSA_Invalid_Key_Tests);
310
BOTAN_REGISTER_TEST("pubkey", "ecdsa_all_groups", ECDSA_AllGroups_Test);
311

312
#endif
313

314
}  // namespace
315

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