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

randombit / botan / 13021208024

28 Jan 2025 11:07PM UTC coverage: 91.241% (-0.02%) from 91.258%
13021208024

push

github

web-flow
Merge pull request #4604 from randombit/jack/support-minimal-curves

Avoid requiring legacy_ec_group in order to run the tests

94137 of 103174 relevant lines covered (91.24%)

11291073.45 hits per line

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

80.0
/src/tests/unit_ecdsa.cpp
1
/*
2
* ECDSA Tests
3
*
4
* (C) 2007 Falko Strenzke
5
*     2007 Manuel Hartl
6
*     2008,2015 Jack Lloyd
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include "tests.h"
12

13
#if defined(BOTAN_HAS_ECDSA)
14
   #include <botan/data_src.h>
15
   #include <botan/ec_group.h>
16
   #include <botan/ecdsa.h>
17
   #include <botan/hash.h>
18
   #include <botan/pkcs8.h>
19
   #include <botan/pubkey.h>
20
#endif
21

22
#if defined(BOTAN_HAS_X509_CERTIFICATES)
23
   #include <botan/x509cert.h>
24
#endif
25

26
namespace Botan_Tests {
27

28
namespace {
29

30
#if defined(BOTAN_HAS_ECDSA)
31

32
   #if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
33
Test::Result test_decode_ecdsa_X509() {
1✔
34
   Test::Result result("Parse CSCA cert");
1✔
35

36
   if(Botan::EC_Group::supports_application_specific_group()) {
1✔
37
      try {
1✔
38
         Botan::X509_Certificate cert(Test::data_file("x509/ecc/CSCA.CSCA.csca-germany.1.crt"));
2✔
39

40
         result.test_eq(
3✔
41
            "correct signature oid", cert.signature_algorithm().oid().to_formatted_string(), "ECDSA/SHA-224");
2✔
42

43
         result.test_eq("serial number", cert.serial_number(), std::vector<uint8_t>{1});
2✔
44
         result.test_eq("authority key id", cert.authority_key_id(), cert.subject_key_id());
2✔
45
         result.test_eq(
3✔
46
            "key fingerprint",
47
            cert.fingerprint("SHA-256"),
2✔
48
            "3B:6C:99:1C:D6:5A:51:FC:EB:17:E3:AA:F6:3C:1A:DA:14:1F:82:41:30:6F:64:EE:FF:63:F3:1F:D6:07:14:9F");
49

50
         auto pubkey = cert.subject_public_key();
1✔
51
         result.test_eq("verify self-signed signature", cert.check_signature(*pubkey), true);
2✔
52
      } catch(Botan::Exception& e) {
1✔
53
         result.test_failure(e.what());
×
54
      }
×
55
   }
56

57
   return result;
1✔
58
}
×
59

60
Test::Result test_decode_ver_link_SHA256() {
1✔
61
   Test::Result result("Check ECDSA signature");
1✔
62

63
   if(Botan::EC_Group::supports_application_specific_group()) {
1✔
64
      try {
1✔
65
         Botan::X509_Certificate root_cert(Test::data_file("x509/ecc/root2_SHA256.cer"));
2✔
66
         Botan::X509_Certificate link_cert(Test::data_file("x509/ecc/link_SHA256.cer"));
2✔
67

68
         auto pubkey = root_cert.subject_public_key();
1✔
69
         result.confirm("verified self-signed signature", link_cert.check_signature(*pubkey));
2✔
70
      } catch(Botan::Exception& e) {
1✔
71
         result.test_failure(e.what());
×
72
      }
×
73
   }
74

75
   return result;
1✔
76
}
×
77

78
Test::Result test_decode_ver_link_SHA1() {
1✔
79
   Test::Result result("Check ECDSA signature SHA-1");
1✔
80

81
   if(Botan::EC_Group::supports_application_specific_group()) {
1✔
82
      try {
1✔
83
         Botan::X509_Certificate root_cert(Test::data_file("x509/ecc/root_SHA1.163.crt"));
2✔
84
         Botan::X509_Certificate link_cert(Test::data_file("x509/ecc/link_SHA1.166.crt"));
2✔
85

86
         auto pubkey = root_cert.subject_public_key();
1✔
87

88
         auto sha1 = Botan::HashFunction::create("SHA-1");
1✔
89

90
         if(!sha1) {
1✔
91
            result.confirm("verification of self-signed signature failed due to missing SHA-1",
×
92
                           !link_cert.check_signature(*pubkey));
×
93
            return result;
×
94
         }
95
         result.confirm("verified self-signed signature", link_cert.check_signature(*pubkey));
2✔
96
      } catch(Botan::Exception& e) {
2✔
97
         result.test_failure(e.what());
×
98
      }
×
99
   }
100

101
   return result;
102
}
×
103
   #endif
104

105
Test::Result test_encoding_options() {
1✔
106
   Test::Result result("ECDSA encoding");
1✔
107

108
   try {
1✔
109
      auto rng = Test::new_rng("ecdsa_encoding_options");
1✔
110

111
      const auto group = Botan::EC_Group::from_name("secp256r1");
1✔
112
      Botan::ECDSA_PrivateKey key(*rng, group);
1✔
113

114
      result.confirm("Default encoding is uncompressed", key.point_encoding() == Botan::EC_Point_Format::Uncompressed);
2✔
115

116
      const std::vector<uint8_t> enc_uncompressed = key.public_key_bits();
1✔
117

118
      key.set_point_encoding(Botan::EC_Point_Format::Compressed);
1✔
119

120
      result.confirm("set_point_encoding works", key.point_encoding() == Botan::EC_Point_Format::Compressed);
2✔
121

122
      const std::vector<uint8_t> enc_compressed = key.public_key_bits();
1✔
123

124
      result.test_lt("Compressed points are smaller", enc_compressed.size(), enc_uncompressed.size());
1✔
125

126
      size_t size_diff = enc_uncompressed.size() - enc_compressed.size();
1✔
127

128
      result.test_gte("Compressed points smaller by group size", size_diff, 32);
1✔
129

130
      key.set_point_encoding(Botan::EC_Point_Format::Hybrid);
1✔
131

132
      result.confirm("set_point_encoding works", key.point_encoding() == Botan::EC_Point_Format::Hybrid);
2✔
133

134
      const std::vector<uint8_t> enc_hybrid = key.public_key_bits();
1✔
135

136
      result.test_eq("Hybrid point same size as uncompressed", enc_uncompressed.size(), enc_hybrid.size());
1✔
137

138
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
139
      // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
140
      auto invalid_format = static_cast<Botan::EC_Point_Format>(99);
1✔
141

142
      result.test_throws("Invalid point format throws", "Invalid point encoding for EC_PublicKey", [&] {
3✔
143
         key.set_point_encoding(invalid_format);
1✔
144
      });
145
   #endif
146
   } catch(Botan::Exception& e) {
4✔
147
      result.test_failure(e.what());
×
148
   }
×
149

150
   return result;
1✔
151
}
×
152

153
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
154

155
Test::Result test_read_pkcs8() {
1✔
156
   Test::Result result("ECDSA read_pkcs8");
1✔
157

158
   if(Botan::EC_Group::supports_application_specific_group()) {
1✔
159
      try {
1✔
160
         auto rng = Test::new_rng("ecdsa_read_pkcs8");
1✔
161

162
         Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/nodompar_private.pkcs8.pem"));
2✔
163
         auto loaded_key_nodp = Botan::PKCS8::load_key(key_stream);
1✔
164
         // anew in each test with unregistered domain-parameters
165
         Botan::ECDSA_PrivateKey* ecdsa_nodp = dynamic_cast<Botan::ECDSA_PrivateKey*>(loaded_key_nodp.get());
1✔
166
         if(!ecdsa_nodp) {
1✔
167
            throw Test_Error("Unable to load valid PKCS8 ECDSA key");
×
168
         }
169

170
         result.confirm("EC_Group is marked as explicit encoding", ecdsa_nodp->domain().used_explicit_encoding());
2✔
171

172
         Botan::PK_Signer signer(*ecdsa_nodp, *rng, "SHA-256");
1✔
173
         Botan::PK_Verifier verifier(*ecdsa_nodp, "SHA-256");
1✔
174

175
         const auto msg = rng->random_vec(48);
1✔
176

177
         std::vector<uint8_t> signature_nodp = signer.sign_message(msg, *rng);
1✔
178

179
         result.confirm("signature valid", verifier.verify_message(msg, signature_nodp));
2✔
180

181
         try {
1✔
182
            Botan::DataSource_Stream key_stream2(Test::data_file("x509/ecc/withdompar_private.pkcs8.pem"));
2✔
183
            auto should_fail = Botan::PKCS8::load_key(key_stream2);
1✔
184
            result.test_failure("loaded key with unknown OID");
×
185
         } catch(std::exception&) {
2✔
186
            result.test_note("rejected key with unknown OID");
1✔
187
         }
1✔
188
      } catch(std::exception& e) {
4✔
189
         result.test_failure("read_pkcs8", e.what());
×
190
      }
×
191
   }
192

193
   return result;
1✔
194
}
×
195

196
Test::Result test_ecc_key_with_rfc5915_extensions() {
1✔
197
   Test::Result result("ECDSA Unit");
1✔
198

199
   try {
1✔
200
      Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/ecc_private_with_rfc5915_ext.pem"));
2✔
201
      auto pkcs8 = Botan::PKCS8::load_key(key_stream);
1✔
202

203
      result.confirm("loaded RFC 5915 key", pkcs8 != nullptr);
2✔
204
      result.test_eq("key is ECDSA", pkcs8->algo_name(), "ECDSA");
2✔
205
      result.confirm("key type is ECDSA", dynamic_cast<Botan::ECDSA_PrivateKey*>(pkcs8.get()) != nullptr);
2✔
206
   } catch(std::exception& e) {
1✔
207
      result.test_failure("load_rfc5915_ext", e.what());
×
208
   }
×
209

210
   return result;
1✔
211
}
×
212

213
Test::Result test_ecc_key_with_rfc5915_parameters() {
1✔
214
   Test::Result result("ECDSA Unit");
1✔
215

216
   try {
1✔
217
      Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/ecc_private_with_rfc5915_parameters.pem"));
2✔
218
      auto pkcs8 = Botan::PKCS8::load_key(key_stream);
1✔
219

220
      result.confirm("loaded RFC 5915 key", pkcs8 != nullptr);
2✔
221
      result.test_eq("key is ECDSA", pkcs8->algo_name(), "ECDSA");
2✔
222
      result.confirm("key type is ECDSA", dynamic_cast<Botan::ECDSA_PrivateKey*>(pkcs8.get()) != nullptr);
2✔
223
   } catch(std::exception& e) {
1✔
224
      result.test_failure("load_rfc5915_params", e.what());
×
225
   }
×
226

227
   return result;
1✔
228
}
×
229

230
   #endif
231

232
class ECDSA_Unit_Tests final : public Test {
×
233
   public:
234
      std::vector<Test::Result> run() override {
1✔
235
         std::vector<Test::Result> results;
1✔
236

237
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
238
         results.push_back(test_read_pkcs8());
2✔
239
         results.push_back(test_ecc_key_with_rfc5915_extensions());
2✔
240
         results.push_back(test_ecc_key_with_rfc5915_parameters());
2✔
241

242
      #if defined(BOTAN_HAS_X509_CERTIFICATES)
243
         results.push_back(test_decode_ecdsa_X509());
2✔
244
         results.push_back(test_decode_ver_link_SHA256());
2✔
245
         results.push_back(test_decode_ver_link_SHA1());
2✔
246
      #endif
247

248
   #endif
249

250
         results.push_back(test_encoding_options());
2✔
251
         return results;
1✔
252
      }
×
253
};
254

255
BOTAN_REGISTER_TEST("pubkey", "ecdsa_unit", ECDSA_Unit_Tests);
256
#endif
257

258
}  // namespace
259

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