forked from azadkuh/mbedcrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ecp.cpp
431 lines (355 loc) · 13.1 KB
/
test_ecp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#if defined(MBEDTLS_ECP_C)
#include <catch.hpp>
#include "pk_common.hpp"
#include "mbedcrypto/hash.hpp"
#include "src/mbedtls_wrapper.hxx"
#include "../../src/pk_private.hpp"
#include "mbedtls/ecdh.h"
#include "mbedtls/ecdsa.h"
///////////////////////////////////////////////////////////////////////////////
namespace {
using namespace mbedcrypto;
///////////////////////////////////////////////////////////////////////////////
void
mpi_checker(const char*, const mpi& mpi) {
REQUIRE(mpi == true);
REQUIRE(mpi.size() > 0);
REQUIRE(mpi.bitlen() <= (mpi.size() << 3));
auto bin = mpi.dump();
REQUIRE(bin.size() == mpi.size());
auto str = mpi.to_string(16);
REQUIRE(str.size() == (mpi.size() << 1));
REQUIRE(from_hex(str) == bin);
// dumper(name, mpi);
}
///////////////////////////////////////////////////////////////////////////////
} // namespace anon
///////////////////////////////////////////////////////////////////////////////
TEST_CASE("ec type checks", "[types][pk]") {
if (supports(pk_t::eckey) || supports(pk_t::eckey_dh)) {
ecp my_key; // default as eckey
REQUIRE(!my_key.has_private_key()); // no key is provided
REQUIRE(test::icompare(my_key.name(), "EC"));
REQUIRE(my_key.can_do(pk_t::eckey));
REQUIRE(my_key.can_do(pk_t::eckey_dh));
if (supports(pk_t::ecdsa)) {
REQUIRE(my_key.can_do(pk_t::ecdsa));
} else {
REQUIRE(!my_key.can_do(pk_t::ecdsa));
}
auto af = my_key.what_can_do();
// my_key has no key. all capabilities must be false
REQUIRE_FALSE((af.encrypt || af.decrypt || af.sign || af.verify));
REQUIRE_THROWS(my_key.reset_as(pk_t::none));
REQUIRE_THROWS(my_key.reset_as(pk_t::rsa));
REQUIRE_THROWS(my_key.reset_as(pk_t::rsa_alt));
REQUIRE_THROWS(my_key.reset_as(pk_t::rsassa_pss));
REQUIRE_NOTHROW(my_key.reset_as(pk_t::eckey));
REQUIRE_NOTHROW(my_key.reset_as(pk_t::eckey_dh));
if (supports(pk_t::ecdsa)) {
REQUIRE_NOTHROW(my_key.reset_as(pk_t::ecdsa));
} else {
REQUIRE_THROWS(my_key.reset_as(pk_t::ecdsa));
}
my_key.reset_as(pk_t::eckey_dh);
REQUIRE(test::icompare(my_key.name(), "EC_DH"));
REQUIRE(!my_key.has_private_key());
REQUIRE(my_key.can_do(pk_t::eckey_dh));
REQUIRE(my_key.can_do(pk_t::eckey));
REQUIRE(!my_key.can_do(pk_t::ecdsa)); // in any circumstances
// my_key has no key. all capabilities must be false
REQUIRE_FALSE((af.encrypt || af.decrypt || af.sign || af.verify));
// rsa key is not loadable into ecp
REQUIRE_THROWS(my_key.import_key(test::rsa_private_key()));
}
if (supports(pk_t::ecdsa)) {
ecp my_key(pk_t::ecdsa);
REQUIRE(test::icompare(my_key.name(), "ECDSA"));
REQUIRE(!my_key.has_private_key());
REQUIRE(my_key.can_do(pk_t::ecdsa));
REQUIRE(!my_key.can_do(pk_t::eckey));
REQUIRE(!my_key.can_do(pk_t::eckey_dh));
auto af = my_key.what_can_do();
// my_key has no key. all capabilities must be false
REQUIRE_FALSE((af.encrypt || af.decrypt || af.sign || af.verify));
}
}
TEST_CASE("ec key tests", "[pk]") {
if (supports(features::pk_export) && supports(pk_t::eckey)) {
ecp gen;
REQUIRE_THROWS(gen.generate_key(curve_t::none));
// test rsa conversion
{
REQUIRE_NOTHROW(gen.generate_key(curve_t::secp192r1));
auto pri_data = gen.export_key(pk::pem_format);
auto pub_data = gen.export_public_key(pk::pem_format);
rsa rkey;
REQUIRE_THROWS(rkey.import_key(pri_data));
REQUIRE_THROWS(rkey.import_public_key(pub_data));
}
const std::initializer_list<curve_t> Items = {
curve_t::secp192r1,
curve_t::secp224r1,
curve_t::secp256r1,
curve_t::secp384r1,
curve_t::secp521r1,
curve_t::secp192k1,
curve_t::secp224k1,
curve_t::secp256k1,
curve_t::bp256r1,
curve_t::bp384r1,
curve_t::bp512r1,
// curve_t::curve25519, // reported bug in mbedtls!
};
auto key_test = [&gen](curve_t ctype, const auto& afs) {
gen.generate_key(ctype);
auto pri_data = gen.export_key(pk::pem_format);
auto pub_data = gen.export_public_key(pk::pem_format);
ecp pri;
pri.import_key(pri_data);
REQUIRE(pri.type() == gen.type());
REQUIRE((pub_data == pri.export_public_key(pk::pem_format)));
auto ki = pri.key_info();
mpi_checker("Qx: ", ki.Qx);
mpi_checker("Qy: ", ki.Qy);
mpi_checker("Qz: ", ki.Qz);
mpi_checker("d: ", ki.d);
ecp pub;
pub.import_public_key(pub_data);
REQUIRE(pub.type() == gen.type());
ki = pub.key_info();
mpi_checker("Qx: ", ki.Qx);
mpi_checker("Qy: ", ki.Qy);
mpi_checker("Qz: ", ki.Qz);
REQUIRE(ki.d == false);
REQUIRE(check_pair(pub, pri));
REQUIRE((pri.what_can_do() == std::get<0>(afs)));
REQUIRE((pub.what_can_do() == std::get<1>(afs)));
};
auto eckey_afs = []() {
if (supports(pk_t::ecdsa)) {
return std::make_tuple(
pk::action_flags{false, false, true, true},
pk::action_flags{false, false, false, true});
} else {
return std::make_tuple(
pk::action_flags{false, false, false, false},
pk::action_flags{false, false, false, false});
}
};
for (auto i : Items) {
REQUIRE_NOTHROW(key_test(i, eckey_afs()));
}
}
}
///////////////////////////////////////////////////////////////////////////////
// test ecdsa
#if defined(MBEDTLS_ECDSA_C)
namespace mbedtls {
namespace details {
template <>
inline void
initializer(mbedtls_ecdsa_context* ctx) noexcept {
mbedtls_ecdsa_init(ctx);
}
template <>
inline void
cleanup(mbedtls_ecdsa_context* ctx) noexcept {
mbedtls_ecdsa_free(ctx);
}
} // namespace details
using ecdsa = wrapper<mbedtls_ecdsa_context>;
} // namespace mbedtls
///////////////////////////////////////////////////////////////////////////////
TEST_CASE("ecdsa c_api tests", "[pk]") {
constexpr auto hash_type = hash_t::sha256;
mbedcrypto::ecdsa ec_;
ec_.generate_key(curve_t::secp192r1);
std::string message{test::long_text()};
auto hash_value = hash::make(hash_type, message);
auto c_sign = [&]() -> std::string {
const auto* ec_ctx = mbedtls_pk_ec(ec_.context().pk_);
mbedtls::ecdsa signer;
// private key copy
mbedtls_c_call(mbedtls_ecp_group_copy, &signer->grp, &ec_ctx->grp);
mbedtls_c_call(mbedtls_ecp_copy, &signer->Q, &ec_ctx->Q);
mbedtls_c_call(mbedtls_mpi_copy, &signer->d, &ec_ctx->d);
std::string signature((size_t)MBEDTLS_ECDSA_MAX_LEN, '\0');
size_t sig_len = signature.size();
mbedtls_c_call(
mbedtls_ecdsa_write_signature,
signer,
MBEDTLS_MD_SHA256,
to_const_ptr(hash_value),
hash_value.size(),
to_ptr(signature),
&sig_len,
rnd_generator::maker,
&ec_.context().rnd_);
signature.resize(sig_len);
return signature;
};
auto c_verify = [&](const std::string& signature) -> bool {
const auto* ec_ctx = mbedtls_pk_ec(ec_.context().pk_);
mbedtls::ecdsa verifier;
// public key copy
mbedtls_c_call(mbedtls_ecp_group_copy, &verifier->grp, &ec_ctx->grp);
mbedtls_c_call(mbedtls_ecp_copy, &verifier->Q, &ec_ctx->Q);
int ret = mbedtls_ecdsa_read_signature(
verifier,
to_const_ptr(hash_value),
hash_value.size(),
to_const_ptr(signature),
signature.size());
if (ret == 0)
return true;
std::cout << mbedtls_error_string(ret) << std::endl;
return false;
};
auto cpp_sign = [&]() -> std::string {
return ec_.sign(hash_value, hash_type);
};
auto cpp_verify = [&](const std::string& signature) -> bool {
return ec_.verify(signature, hash_value, hash_type);
};
// sign by c_api
auto sig = c_sign();
REQUIRE(c_verify(sig));
REQUIRE(cpp_verify(sig));
sig = cpp_sign();
REQUIRE(c_verify(sig));
REQUIRE(cpp_verify(sig));
}
TEST_CASE("ecdsa tests", "[pk]") {
constexpr auto hash_type = hash_t::sha256;
std::string message{test::long_text()};
auto hash_value = hash::make(hash_type, message);
ecdsa signer;
signer.generate_key(curve_t::secp192k1);
auto sig_h = signer.sign(hash_value, hash_type);
auto sig_m = signer.sign_message(message, hash_type);
// sig_h != sig_m because of random properties
ecdsa verifier;
verifier.import_public_key(signer.export_public_key(pk::pem_format));
REQUIRE(verifier.verify_message(sig_m, message, hash_type));
REQUIRE(verifier.verify(sig_h, hash_value, hash_type));
REQUIRE(verifier.verify_message(sig_h, message, hash_type));
REQUIRE(verifier.verify(sig_m, hash_value, hash_type));
}
///////////////////////////////////////////////////////////////////////////////
#endif // MBEDTLS_ECDSA_C
///////////////////////////////////////////////////////////////////////////////
// test ecdh
#if defined(MBEDTLS_ECDH_C)
namespace mbedtls {
namespace details {
template <>
inline void
initializer(mbedtls_ecdh_context* ctx) noexcept {
mbedtls_ecdh_init(ctx);
}
template <>
inline void
cleanup(mbedtls_ecdh_context* ctx) noexcept {
mbedtls_ecdh_free(ctx);
}
} // namespace details
using ecdh = wrapper<mbedtls_ecdh_context>;
struct ecdh_base {
enum K {
psk_length = 150,
};
mbedcrypto::rnd_generator rnd_{"ecdh generator"};
mbedtls::ecdh ecdh_;
}; // struct ecdh_base
struct peer : public ecdh_base {
auto make_peer_key(curve_t ctype) {
mbedtls_c_call(
mbedtls_ecp_group_load, &ecdh_->grp, to_native(ctype));
mbedtls_c_call(
mbedtls_ecdh_gen_public,
&ecdh_->grp,
&ecdh_->d,
&ecdh_->Q,
rnd_generator::maker,
&rnd_);
std::string mypub(psk_length, '\0');
size_t olen = 0;
mbedtls_c_call(
mbedtls_ecp_tls_write_point,
&ecdh_->grp,
&ecdh_->Q,
ecdh_->point_format,
&olen,
to_ptr(mypub),
psk_length);
mypub.resize(olen);
return mypub;
}
auto shared_secret(const std::string& otherpub) {
const auto* p = to_const_ptr(otherpub);
mbedtls_c_call(
mbedtls_ecp_tls_read_point,
&ecdh_->grp,
&ecdh_->Qp,
&p,
otherpub.size());
std::string secret(psk_length, '\0');
size_t olen = 0;
mbedtls_c_call(
mbedtls_ecdh_calc_secret,
ecdh_,
&olen,
to_ptr(secret),
psk_length,
rnd_generator::maker,
&rnd_);
secret.resize(olen);
return secret;
}
}; // struct peer
} // namespace mbedtls
///////////////////////////////////////////////////////////////////////////////
TEST_CASE("ecdh tests", "[pk]") {
SECTION("calculate shared secret") {
const auto ctype = curve_t::secp192k1;
ecdh server;
auto srv_pub = server.make_peer_key(ctype);
ecdh client;
client.generate_key(ctype); // alternative approach to make_peer_key()
auto cli_pub = client.peer_key();
auto sss = server.shared_secret(cli_pub); // server shared secret
auto css = client.shared_secret(srv_pub); // client shared secret
REQUIRE((sss == css));
{
mbedtls::peer c_cli;
cli_pub = c_cli.make_peer_key(ctype);
sss = server.shared_secret(cli_pub);
css = c_cli.shared_secret(srv_pub);
REQUIRE((sss == css));
}
if (supports(features::pk_export)) {
auto pri_key = client.export_key(pk::pem_format);
ecdh clone;
clone.import_key(pri_key);
cli_pub = clone.peer_key();
sss = server.shared_secret(cli_pub);
css = clone.shared_secret(srv_pub);
REQUIRE((sss == css));
}
}
SECTION("RFC 4492") {
const auto ctype = curve_t::secp224r1;
ecdh server;
auto skex = server.make_server_key_exchange(ctype);
ecdh client;
auto cli_pub = client.make_client_peer_key(skex);
auto css = client.shared_secret();
auto sss = server.shared_secret(cli_pub);
REQUIRE((sss == css));
}
}
///////////////////////////////////////////////////////////////////////////////
#endif // MBEDTLS_ECDH_C
///////////////////////////////////////////////////////////////////////////////
#endif // MBEDTLS_ECP_C