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

randombit / botan / 13629762785

03 Mar 2025 11:24AM UTC coverage: 91.686% (-0.008%) from 91.694%
13629762785

push

github

web-flow
Merge pull request #4740 from randombit/jack/more-build-h-cleanup

Small build.h cleanups/removals

95827 of 104516 relevant lines covered (91.69%)

11258003.53 hits per line

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

78.26
/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
      for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
112
         const auto group = Botan::EC_Group::from_name(group_id);
28✔
113
         Botan::ECDSA_PrivateKey key(*rng, group);
28✔
114

115
         result.confirm("Default encoding is uncompressed",
56✔
116
                        key.point_encoding() == Botan::EC_Point_Format::Uncompressed);
28✔
117

118
         const std::vector<uint8_t> enc_uncompressed = key.public_key_bits();
28✔
119
         key.set_point_encoding(Botan::EC_Point_Format::Compressed);
28✔
120

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

123
         const std::vector<uint8_t> enc_compressed = key.public_key_bits();
28✔
124
         result.test_lt("Compressed points are smaller", enc_compressed.size(), enc_uncompressed.size());
28✔
125
         size_t size_diff = enc_uncompressed.size() - enc_compressed.size();
28✔
126
         result.test_gte("Compressed points smaller by group size", size_diff, group.get_p_bytes());
28✔
127
         key.set_point_encoding(Botan::EC_Point_Format::Hybrid);
28✔
128
         result.confirm("set_point_encoding works", key.point_encoding() == Botan::EC_Point_Format::Hybrid);
56✔
129
         const std::vector<uint8_t> enc_hybrid = key.public_key_bits();
28✔
130
         result.test_eq("Hybrid point same size as uncompressed", enc_uncompressed.size(), enc_hybrid.size());
56✔
131
      }
84✔
132
   } catch(Botan::Exception& e) {
1✔
133
      result.test_failure(e.what());
×
134
   }
×
135

136
   return result;
1✔
137
}
×
138

139
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
140

141
Test::Result test_ecc_key_with_rfc5915_extensions() {
1✔
142
   Test::Result result("ECDSA Unit");
1✔
143

144
   try {
1✔
145
      if(Botan::EC_Group::supports_named_group("secp256r1")) {
1✔
146
         Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/ecc_private_with_rfc5915_ext.pem"));
2✔
147
         auto pkcs8 = Botan::PKCS8::load_key(key_stream);
1✔
148

149
         result.confirm("loaded RFC 5915 key", pkcs8 != nullptr);
2✔
150
         result.test_eq("key is ECDSA", pkcs8->algo_name(), "ECDSA");
2✔
151
         result.confirm("key type is ECDSA", dynamic_cast<Botan::ECDSA_PrivateKey*>(pkcs8.get()) != nullptr);
2✔
152
      }
1✔
153
   } catch(std::exception& e) {
×
154
      result.test_failure("load_rfc5915_ext", e.what());
×
155
   }
×
156

157
   return result;
1✔
158
}
×
159

160
Test::Result test_ecc_key_with_rfc5915_parameters() {
1✔
161
   Test::Result result("ECDSA Unit");
1✔
162

163
   try {
1✔
164
      if(Botan::EC_Group::supports_named_group("secp256r1")) {
1✔
165
         Botan::DataSource_Stream key_stream(Test::data_file("x509/ecc/ecc_private_with_rfc5915_parameters.pem"));
2✔
166
         auto pkcs8 = Botan::PKCS8::load_key(key_stream);
1✔
167

168
         result.confirm("loaded RFC 5915 key", pkcs8 != nullptr);
2✔
169
         result.test_eq("key is ECDSA", pkcs8->algo_name(), "ECDSA");
2✔
170
         result.confirm("key type is ECDSA", dynamic_cast<Botan::ECDSA_PrivateKey*>(pkcs8.get()) != nullptr);
2✔
171
      }
1✔
172
   } catch(std::exception& e) {
×
173
      result.test_failure("load_rfc5915_params", e.what());
×
174
   }
×
175

176
   return result;
1✔
177
}
×
178

179
   #endif
180

181
class ECDSA_Unit_Tests final : public Test {
×
182
   public:
183
      std::vector<Test::Result> run() override {
1✔
184
         std::vector<Test::Result> results;
1✔
185

186
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
187
         results.push_back(test_ecc_key_with_rfc5915_extensions());
2✔
188
         results.push_back(test_ecc_key_with_rfc5915_parameters());
2✔
189

190
      #if defined(BOTAN_HAS_X509_CERTIFICATES)
191
         results.push_back(test_decode_ecdsa_X509());
2✔
192
         results.push_back(test_decode_ver_link_SHA256());
2✔
193
         results.push_back(test_decode_ver_link_SHA1());
2✔
194
      #endif
195

196
   #endif
197

198
         results.push_back(test_encoding_options());
2✔
199
         return results;
1✔
200
      }
×
201
};
202

203
BOTAN_REGISTER_TEST("pubkey", "ecdsa_unit", ECDSA_Unit_Tests);
204
#endif
205

206
}  // namespace
207

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