-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_bench.cpp
More file actions
380 lines (296 loc) · 9.24 KB
/
crypto_bench.cpp
File metadata and controls
380 lines (296 loc) · 9.24 KB
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
#include "crypto_bench.h"
#include <stdio.h>
#include <string.h>
#include <chrono>
using namespace std::chrono;
int b_add_cert_ext(X509 *cert, int nid, char *value)
{
X509_EXTENSION *ex;
X509V3_CTX ctx;
/* This sets the 'context' of the extensions. */
/* No configuration database */
X509V3_set_ctx_nodb(&ctx);
/* Issuer and subject certs: both the target since it is self signed,
* no request and no CRL
*/
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
if (!ex)
return 0;
X509_add_ext(cert, ex, -1);
X509_EXTENSION_free(ex);
return 1;
}
bool b_create_ca_cert(int alg)
{
EVP_PKEY* pkey;
switch (alg)
{
case CERT_RSA:
pkey = EVP_RSA_gen(3072);
break;
case CERT_ECDSA:
pkey = EVP_EC_gen("prime256v1");
break;
case CERT_ED25519:
pkey = EVP_PKEY_Q_keygen(NULL, NULL, "ED25519");
break;
default:
return false;
}
if (!pkey)
{
fprintf(stderr, "EVP_PKEY_Q_keygen failed\n");
return false;
}
X509* x509 = X509_new();
ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
X509_gmtime_adj(X509_get_notBefore(x509), 0);
X509_gmtime_adj(X509_get_notAfter(x509), 31536000L);
X509_set_version(x509, 2);
if (!X509_set_pubkey(x509, pkey))
{
fprintf(stderr, "X509_set_pubkey failed\n");
EVP_PKEY_free(pkey);
X509_free(x509);
return false;
}
X509_NAME* name = X509_get_subject_name(x509);
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"UA", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char *)"CA", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)"CA", -1, -1, 0);
X509_set_issuer_name(x509, name);
if (!b_add_cert_ext(x509, NID_basic_constraints, (char*)"CA:TRUE")
|| !b_add_cert_ext(x509, NID_key_usage, (char*)"keyCertSign,cRLSign")
|| !b_add_cert_ext(x509, NID_subject_key_identifier, (char*)"hash")
|| !b_add_cert_ext(x509, NID_authority_key_identifier, (char*)"keyid:always"))
{
fprintf(stderr, "add_cert_ext failed\n");
EVP_PKEY_free(pkey);
X509_free(x509);
return false;
}
int err = 0;
switch (alg)
{
case CERT_RSA:
case CERT_ECDSA:
err = X509_sign(x509, pkey, EVP_sha256());
break;
case CERT_ED25519:
err = X509_sign(x509, pkey, NULL);
break;
default:
return false;
}
if (!err)
{
fprintf(stderr, "X509_sign failed\n");
EVP_PKEY_free(pkey);
X509_free(x509);
return false;
}
system("mkdir cert");
FILE * f;
fopen_s(&f, "cert\\test-ca-key.pem", "wb");
PEM_write_PrivateKey(
f, /* write the key to the file we've opened */
pkey, /* our key from earlier */
NULL, /* default cipher for encrypting the key on disk */
NULL, /* passphrase required for decrypting the key on disk */
10, /* length of the passphrase string */
NULL, /* callback for requesting a password */
NULL /* data to pass to the callback */
);
fclose(f);
fopen_s(&f, "cert\\test-ca-cert.pem", "wb");
PEM_write_X509(
f, /* write the certificate to the file we've opened */
x509 /* our certificate */
);
fclose(f);
EVP_PKEY_free(pkey);
X509_free(x509);
return true;
}
X509_REQ* b_create_server_req(int alg)
{
EVP_PKEY* pkey;
switch (alg)
{
case CERT_RSA:
pkey = EVP_RSA_gen(3072);
break;
case CERT_ECDSA:
pkey = EVP_EC_gen("prime256v1");
break;
case CERT_ED25519:
pkey = EVP_PKEY_Q_keygen(NULL, NULL, "ED25519");
break;
default:
return NULL;
}
if (!pkey)
{
fprintf(stderr, "EVP_PKEY_Q_keygen failed\n");
return NULL;
}
X509_REQ* x509req = X509_REQ_new();
X509_REQ_set_version(x509req, 2);
X509_NAME* name = X509_NAME_new();
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"UA", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)"TLS 1.3 Server", -1, -1, 0);
if (!X509_REQ_set_subject_name(x509req, name))
{
fprintf(stderr, "X509_REQ_set_subject_name failed\n");
EVP_PKEY_free(pkey);
X509_REQ_free(x509req);
return NULL;
}
X509_NAME_free(name);
if (!X509_REQ_set_pubkey(x509req, pkey))
{
fprintf(stderr, "X509_REQ_set_pubkey failed\n");
EVP_PKEY_free(pkey);
X509_REQ_free(x509req);
return NULL;
}
int err = 0;
switch (alg)
{
case CERT_RSA:
case CERT_ECDSA:
err = X509_REQ_sign(x509req, pkey, EVP_sha256());
break;
case CERT_ED25519:
err = X509_REQ_sign(x509req, pkey, NULL);
break;
default:
return NULL;
}
if (!err)
{
fprintf(stderr, "X509_REQ_sign failed\n");
EVP_PKEY_free(pkey);
X509_REQ_free(x509req);
return NULL;
}
return x509req;
}
int b_randSerial(ASN1_INTEGER *ai)
{
BIGNUM *p_bignum = NULL;
if (NULL == (p_bignum = BN_new()))
{
return -1;
}
if (!BN_rand(p_bignum, 64, 0, 0))
{
BN_free(p_bignum);
return -1;
}
if (ai && !BN_to_ASN1_INTEGER(p_bignum, ai))
{
BN_free(p_bignum);
return -1;
}
return 1;
}
bool b_create_server_cert(X509_REQ* x509req, int alg, FILE *p_ca_cert_file, FILE *p_ca_key_file, long long &time_sign, long long &time_verify)
{
X509 *p_ca_cert = NULL;
EVP_PKEY *p_ca_pkey = NULL;
EVP_PKEY *p_ca_key_pkey = NULL;
X509 *p_generated_cert = NULL;
ASN1_INTEGER *p_serial_number = NULL;
EVP_PKEY *p_cert_req_pkey = NULL;
if (NULL == (p_ca_cert = PEM_read_X509(p_ca_cert_file, NULL, 0, NULL)))
{
return false;
}
if (NULL == (p_ca_pkey = X509_get_pubkey(p_ca_cert)))
{
return false;
}
if (NULL == (p_ca_key_pkey = PEM_read_PrivateKey(p_ca_key_file, NULL, NULL, NULL)))
{
return false;
}
if (NULL == (p_generated_cert = X509_new()))
{
return false;
}
X509_set_version(p_generated_cert, 2);
p_serial_number = ASN1_INTEGER_new();
b_randSerial(p_serial_number);
X509_set_serialNumber(p_generated_cert, p_serial_number);
X509_set_issuer_name(p_generated_cert, X509_REQ_get_subject_name(x509req));
X509_set_subject_name(p_generated_cert, X509_REQ_get_subject_name(x509req));
X509_gmtime_adj(X509_get_notBefore(p_generated_cert), 0L);
X509_gmtime_adj(X509_get_notAfter(p_generated_cert), 31536000L);
if (!b_add_cert_ext(p_generated_cert, NID_key_usage, (char*)"digitalSignature")
|| !b_add_cert_ext(p_generated_cert, NID_subject_key_identifier, (char*)"hash"))
{
X509_free(p_generated_cert);
p_generated_cert = NULL;
return false;
}
if (NULL == (p_cert_req_pkey = X509_REQ_get_pubkey(x509req)))
{
X509_free(p_generated_cert);
p_generated_cert = NULL;
return false;
}
if (0 > X509_set_pubkey(p_generated_cert, p_cert_req_pkey))
{
X509_free(p_generated_cert);
p_generated_cert = NULL;
return false;
}
if (0 > EVP_PKEY_copy_parameters(p_ca_pkey, p_ca_key_pkey))
{
X509_free(p_generated_cert);
p_generated_cert = NULL;
return false;
}
X509_set_issuer_name(p_generated_cert, X509_get_subject_name(p_ca_cert));
int err = 0;
steady_clock::time_point start, stop;
switch (alg)
{
case CERT_RSA:
case CERT_ECDSA:
start = high_resolution_clock::now();
err = X509_sign(p_generated_cert, p_ca_key_pkey, EVP_sha256());
stop = high_resolution_clock::now();
break;
case CERT_ED25519:
start = high_resolution_clock::now();
err = X509_sign(p_generated_cert, p_ca_key_pkey, NULL);
stop = high_resolution_clock::now();
break;
default:
return false;
}
if (0 > err)
{
X509_free(p_generated_cert);
p_generated_cert = NULL;
return false;
}
auto duration = duration_cast<microseconds>(stop - start);
time_sign = duration.count();
start = high_resolution_clock::now();
int verif = X509_verify(p_generated_cert, p_ca_pkey);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
time_verify = duration.count();
if (verif == -1)
time_verify = 0;
X509_free(p_ca_cert);
EVP_PKEY_free(p_ca_pkey);
EVP_PKEY_free(p_ca_key_pkey);
ASN1_INTEGER_free(p_serial_number);
EVP_PKEY_free(p_cert_req_pkey);
return true;
}