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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

88.72
/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("ECDSA Unit");
1✔
35
   result.start_timer();
1✔
36

37
   Botan::X509_Certificate cert(Test::data_file("x509/ecc/CSCA.CSCA.csca-germany.1.crt"));
2✔
38

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

41
   result.test_eq("serial number", cert.serial_number(), std::vector<uint8_t>{1});
2✔
42
   result.test_eq("authority key id", cert.authority_key_id(), cert.subject_key_id());
1✔
43
   result.test_eq("key fingerprint",
3✔
44
                  cert.fingerprint("SHA-256"),
2✔
45
                  "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");
46

47
   auto pubkey = cert.subject_public_key();
1✔
48
   result.test_eq("verify self-signed signature", cert.check_signature(*pubkey), true);
1✔
49

50
   result.end_timer();
1✔
51
   return result;
2✔
52
}
1✔
53

54
Test::Result test_decode_ver_link_SHA256() {
1✔
55
   Test::Result result("ECDSA Unit");
1✔
56
   result.start_timer();
1✔
57

58
   Botan::X509_Certificate root_cert(Test::data_file("x509/ecc/root2_SHA256.cer"));
2✔
59
   Botan::X509_Certificate link_cert(Test::data_file("x509/ecc/link_SHA256.cer"));
2✔
60

61
   auto pubkey = root_cert.subject_public_key();
1✔
62
   result.confirm("verified self-signed signature", link_cert.check_signature(*pubkey));
2✔
63

64
   result.end_timer();
1✔
65
   return result;
2✔
66
}
1✔
67

68
Test::Result test_decode_ver_link_SHA1() {
1✔
69
   Botan::X509_Certificate root_cert(Test::data_file("x509/ecc/root_SHA1.163.crt"));
2✔
70
   Botan::X509_Certificate link_cert(Test::data_file("x509/ecc/link_SHA1.166.crt"));
2✔
71

72
   Test::Result result("ECDSA Unit");
1✔
73
   result.start_timer();
1✔
74

75
   auto pubkey = root_cert.subject_public_key();
1✔
76

77
   auto sha1 = Botan::HashFunction::create("SHA-1");
1✔
78

79
   if(!sha1) {
1✔
80
      result.confirm("verification of self-signed signature failed due to missing SHA-1",
×
81
                     !link_cert.check_signature(*pubkey));
×
82
      return result;
×
83
   }
84
   result.confirm("verified self-signed signature", link_cert.check_signature(*pubkey));
2✔
85

86
   result.end_timer();
1✔
87
   return result;
88
}
2✔
89
   #endif
90

91
Test::Result test_encoding_options() {
1✔
92
   Test::Result result("ECDSA Unit");
1✔
93
   result.start_timer();
1✔
94

95
   auto rng = Test::new_rng("ecdsa_encoding_options");
1✔
96

97
   const auto group = Botan::EC_Group::from_name("secp256r1");
1✔
98
   Botan::ECDSA_PrivateKey key(*rng, group);
1✔
99

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

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

104
   key.set_point_encoding(Botan::EC_Point_Format::Compressed);
1✔
105

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

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

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

112
   size_t size_diff = enc_uncompressed.size() - enc_compressed.size();
1✔
113

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

116
   key.set_point_encoding(Botan::EC_Point_Format::Hybrid);
1✔
117

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

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

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

124
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
125
   // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
126
   auto invalid_format = static_cast<Botan::EC_Point_Format>(99);
1✔
127

128
   result.test_throws("Invalid point format throws", "Invalid point encoding for EC_PublicKey", [&] {
2✔
129
      key.set_point_encoding(invalid_format);
1✔
130
   });
131
   #endif
132

133
   result.end_timer();
1✔
134
   return result;
1✔
135
}
4✔
136

137
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
138

139
Test::Result test_read_pkcs8() {
1✔
140
   Test::Result result("ECDSA Unit");
1✔
141
   result.start_timer();
1✔
142

143
   auto rng = Test::new_rng("ecdsa_read_pkcs8");
1✔
144

145
   try {
1✔
146
      Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/nodompar_private.pkcs8.pem"));
2✔
147
      auto loaded_key_nodp = Botan::PKCS8::load_key(key_stream);
1✔
148
      // anew in each test with unregistered domain-parameters
149
      Botan::ECDSA_PrivateKey* ecdsa_nodp = dynamic_cast<Botan::ECDSA_PrivateKey*>(loaded_key_nodp.get());
1✔
150
      if(!ecdsa_nodp) {
1✔
151
         throw Test_Error("Unable to load valid PKCS8 ECDSA key");
×
152
      }
153

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

156
      Botan::PK_Signer signer(*ecdsa_nodp, *rng, "SHA-256");
1✔
157
      Botan::PK_Verifier verifier(*ecdsa_nodp, "SHA-256");
1✔
158

159
      const auto msg = rng->random_vec(48);
1✔
160

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

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

165
      try {
1✔
166
         Botan::DataSource_Stream key_stream2(Test::data_file("x509/ecc/withdompar_private.pkcs8.pem"));
2✔
167
         auto should_fail = Botan::PKCS8::load_key(key_stream2);
1✔
168
         result.test_failure("loaded key with unknown OID");
×
169
      } catch(std::exception&) {
2✔
170
         result.test_note("rejected key with unknown OID");
1✔
171
      }
1✔
172
   } catch(std::exception& e) {
3✔
173
      result.test_failure("read_pkcs8", e.what());
×
174
   }
×
175

176
   result.end_timer();
1✔
177
   return result;
1✔
178
}
1✔
179

180
Test::Result test_ecc_key_with_rfc5915_extensions() {
1✔
181
   Test::Result result("ECDSA Unit");
1✔
182
   result.start_timer();
1✔
183

184
   try {
1✔
185
      Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/ecc_private_with_rfc5915_ext.pem"));
2✔
186
      auto pkcs8 = Botan::PKCS8::load_key(key_stream);
1✔
187

188
      result.confirm("loaded RFC 5915 key", pkcs8 != nullptr);
2✔
189
      result.test_eq("key is ECDSA", pkcs8->algo_name(), "ECDSA");
2✔
190
      result.confirm("key type is ECDSA", dynamic_cast<Botan::ECDSA_PrivateKey*>(pkcs8.get()) != nullptr);
2✔
191
   } catch(std::exception& e) {
1✔
192
      result.test_failure("load_rfc5915_ext", e.what());
×
193
   }
×
194

195
   result.end_timer();
1✔
196
   return result;
1✔
197
}
×
198

199
Test::Result test_ecc_key_with_rfc5915_parameters() {
1✔
200
   Test::Result result("ECDSA Unit");
1✔
201
   result.start_timer();
1✔
202

203
   try {
1✔
204
      Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/ecc_private_with_rfc5915_parameters.pem"));
2✔
205
      auto pkcs8 = Botan::PKCS8::load_key(key_stream);
1✔
206

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

214
   result.end_timer();
1✔
215
   return result;
1✔
216
}
×
217

218
   #endif
219

220
class ECDSA_Unit_Tests final : public Test {
×
221
   public:
222
      std::vector<Test::Result> run() override {
1✔
223
         std::vector<Test::Result> results;
1✔
224

225
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
226
         results.push_back(test_read_pkcs8());
2✔
227
         results.push_back(test_ecc_key_with_rfc5915_extensions());
2✔
228
         results.push_back(test_ecc_key_with_rfc5915_parameters());
2✔
229

230
      #if defined(BOTAN_HAS_X509_CERTIFICATES)
231
         results.push_back(test_decode_ecdsa_X509());
2✔
232
         results.push_back(test_decode_ver_link_SHA256());
2✔
233
         results.push_back(test_decode_ver_link_SHA1());
2✔
234
      #endif
235

236
   #endif
237

238
         results.push_back(test_encoding_options());
2✔
239
         return results;
1✔
240
      }
×
241
};
242

243
BOTAN_REGISTER_TEST("pubkey", "ecdsa_unit", ECDSA_Unit_Tests);
244
#endif
245

246
}  // namespace
247

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

© 2025 Coveralls, Inc