]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/speed.c
Add "sections" to -help output
[thirdparty/openssl.git] / apps / speed.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #undef SECONDS
12 #define SECONDS 3
13 #define RSA_SECONDS 10
14 #define DSA_SECONDS 10
15 #define ECDSA_SECONDS 10
16 #define ECDH_SECONDS 10
17 #define EdDSA_SECONDS 10
18 #define SM2_SECONDS 10
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include "apps.h"
25 #include "progs.h"
26 #include <openssl/crypto.h>
27 #include <openssl/rand.h>
28 #include <openssl/err.h>
29 #include <openssl/evp.h>
30 #include <openssl/objects.h>
31 #include <openssl/async.h>
32 #if !defined(OPENSSL_SYS_MSDOS)
33 # include <unistd.h>
34 #endif
35
36 #if defined(_WIN32)
37 # include <windows.h>
38 #endif
39
40 #include <openssl/bn.h>
41 #ifndef OPENSSL_NO_DES
42 # include <openssl/des.h>
43 #endif
44 #include <openssl/aes.h>
45 #ifndef OPENSSL_NO_CAMELLIA
46 # include <openssl/camellia.h>
47 #endif
48 #ifndef OPENSSL_NO_MD2
49 # include <openssl/md2.h>
50 #endif
51 #ifndef OPENSSL_NO_MDC2
52 # include <openssl/mdc2.h>
53 #endif
54 #ifndef OPENSSL_NO_MD4
55 # include <openssl/md4.h>
56 #endif
57 #ifndef OPENSSL_NO_MD5
58 # include <openssl/md5.h>
59 #endif
60 #include <openssl/hmac.h>
61 #ifndef OPENSSL_NO_CMAC
62 #include <openssl/cmac.h>
63 #endif
64 #include <openssl/sha.h>
65 #ifndef OPENSSL_NO_RMD160
66 # include <openssl/ripemd.h>
67 #endif
68 #ifndef OPENSSL_NO_WHIRLPOOL
69 # include <openssl/whrlpool.h>
70 #endif
71 #ifndef OPENSSL_NO_RC4
72 # include <openssl/rc4.h>
73 #endif
74 #ifndef OPENSSL_NO_RC5
75 # include <openssl/rc5.h>
76 #endif
77 #ifndef OPENSSL_NO_RC2
78 # include <openssl/rc2.h>
79 #endif
80 #ifndef OPENSSL_NO_IDEA
81 # include <openssl/idea.h>
82 #endif
83 #ifndef OPENSSL_NO_SEED
84 # include <openssl/seed.h>
85 #endif
86 #ifndef OPENSSL_NO_BF
87 # include <openssl/blowfish.h>
88 #endif
89 #ifndef OPENSSL_NO_CAST
90 # include <openssl/cast.h>
91 #endif
92 #ifndef OPENSSL_NO_RSA
93 # include <openssl/rsa.h>
94 # include "./testrsa.h"
95 #endif
96 #include <openssl/x509.h>
97 #ifndef OPENSSL_NO_DSA
98 # include <openssl/dsa.h>
99 # include "./testdsa.h"
100 #endif
101 #ifndef OPENSSL_NO_EC
102 # include <openssl/ec.h>
103 #endif
104 #include <openssl/modes.h>
105
106 #ifndef HAVE_FORK
107 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
108 # define HAVE_FORK 0
109 # else
110 # define HAVE_FORK 1
111 # endif
112 #endif
113
114 #if HAVE_FORK
115 # undef NO_FORK
116 #else
117 # define NO_FORK
118 #endif
119
120 #define MAX_MISALIGNMENT 63
121 #define MAX_ECDH_SIZE 256
122 #define MISALIGN 64
123
124 typedef struct openssl_speed_sec_st {
125 int sym;
126 int rsa;
127 int dsa;
128 int ecdsa;
129 int ecdh;
130 int eddsa;
131 int sm2;
132 } openssl_speed_sec_t;
133
134 static volatile int run = 0;
135
136 static int mr = 0;
137 static int usertime = 1;
138
139 #ifndef OPENSSL_NO_MD2
140 static int EVP_Digest_MD2_loop(void *args);
141 #endif
142
143 #ifndef OPENSSL_NO_MDC2
144 static int EVP_Digest_MDC2_loop(void *args);
145 #endif
146 #ifndef OPENSSL_NO_MD4
147 static int EVP_Digest_MD4_loop(void *args);
148 #endif
149 #ifndef OPENSSL_NO_MD5
150 static int MD5_loop(void *args);
151 static int HMAC_loop(void *args);
152 #endif
153 static int SHA1_loop(void *args);
154 static int SHA256_loop(void *args);
155 static int SHA512_loop(void *args);
156 #ifndef OPENSSL_NO_WHIRLPOOL
157 static int WHIRLPOOL_loop(void *args);
158 #endif
159 #ifndef OPENSSL_NO_RMD160
160 static int EVP_Digest_RMD160_loop(void *args);
161 #endif
162 #ifndef OPENSSL_NO_RC4
163 static int RC4_loop(void *args);
164 #endif
165 #ifndef OPENSSL_NO_DES
166 static int DES_ncbc_encrypt_loop(void *args);
167 static int DES_ede3_cbc_encrypt_loop(void *args);
168 #endif
169 static int AES_cbc_128_encrypt_loop(void *args);
170 static int AES_cbc_192_encrypt_loop(void *args);
171 static int AES_cbc_256_encrypt_loop(void *args);
172 #ifndef OPENSSL_NO_DEPRECATED_3_0
173 static int AES_ige_128_encrypt_loop(void *args);
174 static int AES_ige_192_encrypt_loop(void *args);
175 static int AES_ige_256_encrypt_loop(void *args);
176 #endif
177 static int CRYPTO_gcm128_aad_loop(void *args);
178 static int RAND_bytes_loop(void *args);
179 static int EVP_Update_loop(void *args);
180 static int EVP_Update_loop_ccm(void *args);
181 static int EVP_Update_loop_aead(void *args);
182 static int EVP_Digest_loop(void *args);
183 #ifndef OPENSSL_NO_RSA
184 static int RSA_sign_loop(void *args);
185 static int RSA_verify_loop(void *args);
186 #endif
187 #ifndef OPENSSL_NO_DSA
188 static int DSA_sign_loop(void *args);
189 static int DSA_verify_loop(void *args);
190 #endif
191 #ifndef OPENSSL_NO_EC
192 static int ECDSA_sign_loop(void *args);
193 static int ECDSA_verify_loop(void *args);
194 static int EdDSA_sign_loop(void *args);
195 static int EdDSA_verify_loop(void *args);
196 # ifndef OPENSSL_NO_SM2
197 static int SM2_sign_loop(void *args);
198 static int SM2_verify_loop(void *args);
199 # endif
200 #endif
201
202 static double Time_F(int s);
203 static void print_message(const char *s, long num, int length, int tm);
204 static void pkey_print_message(const char *str, const char *str2,
205 long num, unsigned int bits, int sec);
206 static void print_result(int alg, int run_no, int count, double time_used);
207 #ifndef NO_FORK
208 static int do_multi(int multi, int size_num);
209 #endif
210
211 static const int lengths_list[] = {
212 16, 64, 256, 1024, 8 * 1024, 16 * 1024
213 };
214 static const int *lengths = lengths_list;
215
216 static const int aead_lengths_list[] = {
217 2, 31, 136, 1024, 8 * 1024, 16 * 1024
218 };
219
220 #define START 0
221 #define STOP 1
222
223 #ifdef SIGALRM
224
225 static void alarmed(int sig)
226 {
227 signal(SIGALRM, alarmed);
228 run = 0;
229 }
230
231 static double Time_F(int s)
232 {
233 double ret = app_tminterval(s, usertime);
234 if (s == STOP)
235 alarm(0);
236 return ret;
237 }
238
239 #elif defined(_WIN32)
240
241 # define SIGALRM -1
242
243 static unsigned int lapse;
244 static volatile unsigned int schlock;
245 static void alarm_win32(unsigned int secs)
246 {
247 lapse = secs * 1000;
248 }
249
250 # define alarm alarm_win32
251
252 static DWORD WINAPI sleepy(VOID * arg)
253 {
254 schlock = 1;
255 Sleep(lapse);
256 run = 0;
257 return 0;
258 }
259
260 static double Time_F(int s)
261 {
262 double ret;
263 static HANDLE thr;
264
265 if (s == START) {
266 schlock = 0;
267 thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
268 if (thr == NULL) {
269 DWORD err = GetLastError();
270 BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
271 ExitProcess(err);
272 }
273 while (!schlock)
274 Sleep(0); /* scheduler spinlock */
275 ret = app_tminterval(s, usertime);
276 } else {
277 ret = app_tminterval(s, usertime);
278 if (run)
279 TerminateThread(thr, 0);
280 CloseHandle(thr);
281 }
282
283 return ret;
284 }
285 #else
286 static double Time_F(int s)
287 {
288 return app_tminterval(s, usertime);
289 }
290 #endif
291
292 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
293 const openssl_speed_sec_t *seconds);
294
295 #define found(value, pairs, result)\
296 opt_found(value, result, pairs, OSSL_NELEM(pairs))
297 static int opt_found(const char *name, unsigned int *result,
298 const OPT_PAIR pairs[], unsigned int nbelem)
299 {
300 unsigned int idx;
301
302 for (idx = 0; idx < nbelem; ++idx, pairs++)
303 if (strcmp(name, pairs->name) == 0) {
304 *result = pairs->retval;
305 return 1;
306 }
307 return 0;
308 }
309
310 typedef enum OPTION_choice {
311 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
312 OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
313 OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM,
314 OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC
315 } OPTION_CHOICE;
316
317 const OPTIONS speed_options[] = {
318 {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
319
320 OPT_SECTION("General"),
321 {"help", OPT_HELP, '-', "Display this summary"},
322 {"mb", OPT_MB, '-',
323 "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
324 {"mr", OPT_MR, '-', "Produce machine readable output"},
325 #ifndef NO_FORK
326 {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
327 #endif
328 #ifndef OPENSSL_NO_ASYNC
329 {"async_jobs", OPT_ASYNCJOBS, 'p',
330 "Enable async mode and start specified number of jobs"},
331 #endif
332 #ifndef OPENSSL_NO_ENGINE
333 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
334 #endif
335 {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
336
337 OPT_SECTION("Selection"),
338 {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
339 {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
340 #ifndef OPENSSL_NO_CMAC
341 {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
342 #endif
343 {"decrypt", OPT_DECRYPT, '-',
344 "Time decryption instead of encryption (only EVP)"},
345 {"aead", OPT_AEAD, '-',
346 "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
347
348 OPT_SECTION("Timing"),
349 {"elapsed", OPT_ELAPSED, '-',
350 "Use wall-clock time instead of CPU user time as divisor"},
351 {"seconds", OPT_SECONDS, 'p',
352 "Run benchmarks for specified amount of seconds"},
353 {"bytes", OPT_BYTES, 'p',
354 "Run [non-PKI] benchmarks on custom-sized buffer"},
355 {"misalign", OPT_MISALIGN, 'p',
356 "Use specified offset to mis-align buffers"},
357
358 OPT_R_OPTIONS,
359 {NULL}
360 };
361
362 #define D_MD2 0
363 #define D_MDC2 1
364 #define D_MD4 2
365 #define D_MD5 3
366 #define D_HMAC 4
367 #define D_SHA1 5
368 #define D_RMD160 6
369 #define D_RC4 7
370 #define D_CBC_DES 8
371 #define D_EDE3_DES 9
372 #define D_CBC_IDEA 10
373 #define D_CBC_SEED 11
374 #define D_CBC_RC2 12
375 #define D_CBC_RC5 13
376 #define D_CBC_BF 14
377 #define D_CBC_CAST 15
378 #define D_CBC_128_AES 16
379 #define D_CBC_192_AES 17
380 #define D_CBC_256_AES 18
381 #define D_CBC_128_CML 19
382 #define D_CBC_192_CML 20
383 #define D_CBC_256_CML 21
384 #define D_EVP 22
385 #define D_SHA256 23
386 #define D_SHA512 24
387 #define D_WHIRLPOOL 25
388 #define D_IGE_128_AES 26
389 #define D_IGE_192_AES 27
390 #define D_IGE_256_AES 28
391 #define D_GHASH 29
392 #define D_RAND 30
393 #define D_EVP_HMAC 31
394 #define D_EVP_CMAC 32
395
396 /* name of algorithms to test */
397 static const char *names[] = {
398 "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
399 "des cbc", "des ede3", "idea cbc", "seed cbc",
400 "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
401 "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
402 "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
403 "evp", "sha256", "sha512", "whirlpool",
404 "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
405 "rand", "hmac", "cmac"
406 };
407 #define ALGOR_NUM OSSL_NELEM(names)
408
409 /* list of configured algorithm (remaining) */
410 static const OPT_PAIR doit_choices[] = {
411 #ifndef OPENSSL_NO_MD2
412 {"md2", D_MD2},
413 #endif
414 #ifndef OPENSSL_NO_MDC2
415 {"mdc2", D_MDC2},
416 #endif
417 #ifndef OPENSSL_NO_MD4
418 {"md4", D_MD4},
419 #endif
420 #ifndef OPENSSL_NO_MD5
421 {"md5", D_MD5},
422 {"hmac", D_HMAC},
423 #endif
424 {"sha1", D_SHA1},
425 {"sha256", D_SHA256},
426 {"sha512", D_SHA512},
427 #ifndef OPENSSL_NO_WHIRLPOOL
428 {"whirlpool", D_WHIRLPOOL},
429 #endif
430 #ifndef OPENSSL_NO_RMD160
431 {"ripemd", D_RMD160},
432 {"rmd160", D_RMD160},
433 {"ripemd160", D_RMD160},
434 #endif
435 #ifndef OPENSSL_NO_RC4
436 {"rc4", D_RC4},
437 #endif
438 #ifndef OPENSSL_NO_DES
439 {"des-cbc", D_CBC_DES},
440 {"des-ede3", D_EDE3_DES},
441 #endif
442 {"aes-128-cbc", D_CBC_128_AES},
443 {"aes-192-cbc", D_CBC_192_AES},
444 {"aes-256-cbc", D_CBC_256_AES},
445 #ifndef OPENSSL_NO_DEPRECATED_3_0
446 {"aes-128-ige", D_IGE_128_AES},
447 {"aes-192-ige", D_IGE_192_AES},
448 {"aes-256-ige", D_IGE_256_AES},
449 #endif
450 #ifndef OPENSSL_NO_RC2
451 {"rc2-cbc", D_CBC_RC2},
452 {"rc2", D_CBC_RC2},
453 #endif
454 #ifndef OPENSSL_NO_RC5
455 {"rc5-cbc", D_CBC_RC5},
456 {"rc5", D_CBC_RC5},
457 #endif
458 #ifndef OPENSSL_NO_IDEA
459 {"idea-cbc", D_CBC_IDEA},
460 {"idea", D_CBC_IDEA},
461 #endif
462 #ifndef OPENSSL_NO_SEED
463 {"seed-cbc", D_CBC_SEED},
464 {"seed", D_CBC_SEED},
465 #endif
466 #ifndef OPENSSL_NO_BF
467 {"bf-cbc", D_CBC_BF},
468 {"blowfish", D_CBC_BF},
469 {"bf", D_CBC_BF},
470 #endif
471 #ifndef OPENSSL_NO_CAST
472 {"cast-cbc", D_CBC_CAST},
473 {"cast", D_CBC_CAST},
474 {"cast5", D_CBC_CAST},
475 #endif
476 {"ghash", D_GHASH},
477 {"rand", D_RAND}
478 };
479
480 static double results[ALGOR_NUM][OSSL_NELEM(lengths_list)];
481
482 #ifndef OPENSSL_NO_DSA
483 # define R_DSA_512 0
484 # define R_DSA_1024 1
485 # define R_DSA_2048 2
486 static const OPT_PAIR dsa_choices[] = {
487 {"dsa512", R_DSA_512},
488 {"dsa1024", R_DSA_1024},
489 {"dsa2048", R_DSA_2048}
490 };
491 # define DSA_NUM OSSL_NELEM(dsa_choices)
492
493 static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
494 #endif /* OPENSSL_NO_DSA */
495
496 #define R_RSA_512 0
497 #define R_RSA_1024 1
498 #define R_RSA_2048 2
499 #define R_RSA_3072 3
500 #define R_RSA_4096 4
501 #define R_RSA_7680 5
502 #define R_RSA_15360 6
503 #ifndef OPENSSL_NO_RSA
504 static const OPT_PAIR rsa_choices[] = {
505 {"rsa512", R_RSA_512},
506 {"rsa1024", R_RSA_1024},
507 {"rsa2048", R_RSA_2048},
508 {"rsa3072", R_RSA_3072},
509 {"rsa4096", R_RSA_4096},
510 {"rsa7680", R_RSA_7680},
511 {"rsa15360", R_RSA_15360}
512 };
513 # define RSA_NUM OSSL_NELEM(rsa_choices)
514
515 static double rsa_results[RSA_NUM][2]; /* 2 ops: sign then verify */
516 #endif /* OPENSSL_NO_RSA */
517
518 enum {
519 R_EC_P160,
520 R_EC_P192,
521 R_EC_P224,
522 R_EC_P256,
523 R_EC_P384,
524 R_EC_P521,
525 #ifndef OPENSSL_NO_EC2M
526 R_EC_K163,
527 R_EC_K233,
528 R_EC_K283,
529 R_EC_K409,
530 R_EC_K571,
531 R_EC_B163,
532 R_EC_B233,
533 R_EC_B283,
534 R_EC_B409,
535 R_EC_B571,
536 #endif
537 R_EC_BRP256R1,
538 R_EC_BRP256T1,
539 R_EC_BRP384R1,
540 R_EC_BRP384T1,
541 R_EC_BRP512R1,
542 R_EC_BRP512T1,
543 R_EC_X25519,
544 R_EC_X448
545 };
546
547 #ifndef OPENSSL_NO_EC
548 static OPT_PAIR ecdsa_choices[] = {
549 {"ecdsap160", R_EC_P160},
550 {"ecdsap192", R_EC_P192},
551 {"ecdsap224", R_EC_P224},
552 {"ecdsap256", R_EC_P256},
553 {"ecdsap384", R_EC_P384},
554 {"ecdsap521", R_EC_P521},
555 # ifndef OPENSSL_NO_EC2M
556 {"ecdsak163", R_EC_K163},
557 {"ecdsak233", R_EC_K233},
558 {"ecdsak283", R_EC_K283},
559 {"ecdsak409", R_EC_K409},
560 {"ecdsak571", R_EC_K571},
561 {"ecdsab163", R_EC_B163},
562 {"ecdsab233", R_EC_B233},
563 {"ecdsab283", R_EC_B283},
564 {"ecdsab409", R_EC_B409},
565 {"ecdsab571", R_EC_B571},
566 # endif
567 {"ecdsabrp256r1", R_EC_BRP256R1},
568 {"ecdsabrp256t1", R_EC_BRP256T1},
569 {"ecdsabrp384r1", R_EC_BRP384R1},
570 {"ecdsabrp384t1", R_EC_BRP384T1},
571 {"ecdsabrp512r1", R_EC_BRP512R1},
572 {"ecdsabrp512t1", R_EC_BRP512T1}
573 };
574 # define ECDSA_NUM OSSL_NELEM(ecdsa_choices)
575
576 static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
577
578 static const OPT_PAIR ecdh_choices[] = {
579 {"ecdhp160", R_EC_P160},
580 {"ecdhp192", R_EC_P192},
581 {"ecdhp224", R_EC_P224},
582 {"ecdhp256", R_EC_P256},
583 {"ecdhp384", R_EC_P384},
584 {"ecdhp521", R_EC_P521},
585 # ifndef OPENSSL_NO_EC2M
586 {"ecdhk163", R_EC_K163},
587 {"ecdhk233", R_EC_K233},
588 {"ecdhk283", R_EC_K283},
589 {"ecdhk409", R_EC_K409},
590 {"ecdhk571", R_EC_K571},
591 {"ecdhb163", R_EC_B163},
592 {"ecdhb233", R_EC_B233},
593 {"ecdhb283", R_EC_B283},
594 {"ecdhb409", R_EC_B409},
595 {"ecdhb571", R_EC_B571},
596 # endif
597 {"ecdhbrp256r1", R_EC_BRP256R1},
598 {"ecdhbrp256t1", R_EC_BRP256T1},
599 {"ecdhbrp384r1", R_EC_BRP384R1},
600 {"ecdhbrp384t1", R_EC_BRP384T1},
601 {"ecdhbrp512r1", R_EC_BRP512R1},
602 {"ecdhbrp512t1", R_EC_BRP512T1},
603 {"ecdhx25519", R_EC_X25519},
604 {"ecdhx448", R_EC_X448}
605 };
606 # define EC_NUM OSSL_NELEM(ecdh_choices)
607
608 static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
609
610 #define R_EC_Ed25519 0
611 #define R_EC_Ed448 1
612 static OPT_PAIR eddsa_choices[] = {
613 {"ed25519", R_EC_Ed25519},
614 {"ed448", R_EC_Ed448}
615 };
616 # define EdDSA_NUM OSSL_NELEM(eddsa_choices)
617
618 static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
619
620 # ifndef OPENSSL_NO_SM2
621 # define R_EC_CURVESM2 0
622 static OPT_PAIR sm2_choices[] = {
623 {"curveSM2", R_EC_CURVESM2}
624 };
625 # define SM2_ID "TLSv1.3+GM+Cipher+Suite"
626 # define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
627 # define SM2_NUM OSSL_NELEM(sm2_choices)
628
629 static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
630 # endif /* OPENSSL_NO_SM2 */
631 #endif /* OPENSSL_NO_EC */
632
633 #ifndef SIGALRM
634 # define COND(d) (count < (d))
635 # define COUNT(d) (d)
636 #else
637 # define COND(unused_cond) (run && count<0x7fffffff)
638 # define COUNT(d) (count)
639 #endif /* SIGALRM */
640
641 typedef struct loopargs_st {
642 ASYNC_JOB *inprogress_job;
643 ASYNC_WAIT_CTX *wait_ctx;
644 unsigned char *buf;
645 unsigned char *buf2;
646 unsigned char *buf_malloc;
647 unsigned char *buf2_malloc;
648 unsigned char *key;
649 unsigned int siglen;
650 size_t sigsize;
651 #ifndef OPENSSL_NO_RSA
652 RSA *rsa_key[RSA_NUM];
653 #endif
654 #ifndef OPENSSL_NO_DSA
655 DSA *dsa_key[DSA_NUM];
656 #endif
657 #ifndef OPENSSL_NO_EC
658 EC_KEY *ecdsa[ECDSA_NUM];
659 EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
660 EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
661 # ifndef OPENSSL_NO_SM2
662 EVP_MD_CTX *sm2_ctx[SM2_NUM];
663 EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
664 EVP_PKEY *sm2_pkey[SM2_NUM];
665 # endif
666 unsigned char *secret_a;
667 unsigned char *secret_b;
668 size_t outlen[EC_NUM];
669 #endif
670 EVP_CIPHER_CTX *ctx;
671 HMAC_CTX *hctx;
672 #ifndef OPENSSL_NO_CMAC
673 CMAC_CTX *cmac_ctx;
674 #endif
675 GCM128_CONTEXT *gcm_ctx;
676 } loopargs_t;
677 static int run_benchmark(int async_jobs, int (*loop_function) (void *),
678 loopargs_t * loopargs);
679
680 static unsigned int testnum;
681
682 /* Nb of iterations to do per algorithm and key-size */
683 static long c[ALGOR_NUM][OSSL_NELEM(lengths_list)];
684
685 #ifndef OPENSSL_NO_MD2
686 static int EVP_Digest_MD2_loop(void *args)
687 {
688 loopargs_t *tempargs = *(loopargs_t **) args;
689 unsigned char *buf = tempargs->buf;
690 unsigned char md2[MD2_DIGEST_LENGTH];
691 int count;
692
693 for (count = 0; COND(c[D_MD2][testnum]); count++) {
694 if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
695 NULL))
696 return -1;
697 }
698 return count;
699 }
700 #endif
701
702 #ifndef OPENSSL_NO_MDC2
703 static int EVP_Digest_MDC2_loop(void *args)
704 {
705 loopargs_t *tempargs = *(loopargs_t **) args;
706 unsigned char *buf = tempargs->buf;
707 unsigned char mdc2[MDC2_DIGEST_LENGTH];
708 int count;
709
710 for (count = 0; COND(c[D_MDC2][testnum]); count++) {
711 if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
712 NULL))
713 return -1;
714 }
715 return count;
716 }
717 #endif
718
719 #ifndef OPENSSL_NO_MD4
720 static int EVP_Digest_MD4_loop(void *args)
721 {
722 loopargs_t *tempargs = *(loopargs_t **) args;
723 unsigned char *buf = tempargs->buf;
724 unsigned char md4[MD4_DIGEST_LENGTH];
725 int count;
726
727 for (count = 0; COND(c[D_MD4][testnum]); count++) {
728 if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
729 NULL))
730 return -1;
731 }
732 return count;
733 }
734 #endif
735
736 #ifndef OPENSSL_NO_MD5
737 static int MD5_loop(void *args)
738 {
739 loopargs_t *tempargs = *(loopargs_t **) args;
740 unsigned char *buf = tempargs->buf;
741 unsigned char md5[MD5_DIGEST_LENGTH];
742 int count;
743 for (count = 0; COND(c[D_MD5][testnum]); count++)
744 MD5(buf, lengths[testnum], md5);
745 return count;
746 }
747
748 static int HMAC_loop(void *args)
749 {
750 loopargs_t *tempargs = *(loopargs_t **) args;
751 unsigned char *buf = tempargs->buf;
752 HMAC_CTX *hctx = tempargs->hctx;
753 unsigned char hmac[MD5_DIGEST_LENGTH];
754 int count;
755
756 for (count = 0; COND(c[D_HMAC][testnum]); count++) {
757 HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
758 HMAC_Update(hctx, buf, lengths[testnum]);
759 HMAC_Final(hctx, hmac, NULL);
760 }
761 return count;
762 }
763 #endif
764
765 static int SHA1_loop(void *args)
766 {
767 loopargs_t *tempargs = *(loopargs_t **) args;
768 unsigned char *buf = tempargs->buf;
769 unsigned char sha[SHA_DIGEST_LENGTH];
770 int count;
771 for (count = 0; COND(c[D_SHA1][testnum]); count++)
772 SHA1(buf, lengths[testnum], sha);
773 return count;
774 }
775
776 static int SHA256_loop(void *args)
777 {
778 loopargs_t *tempargs = *(loopargs_t **) args;
779 unsigned char *buf = tempargs->buf;
780 unsigned char sha256[SHA256_DIGEST_LENGTH];
781 int count;
782 for (count = 0; COND(c[D_SHA256][testnum]); count++)
783 SHA256(buf, lengths[testnum], sha256);
784 return count;
785 }
786
787 static int SHA512_loop(void *args)
788 {
789 loopargs_t *tempargs = *(loopargs_t **) args;
790 unsigned char *buf = tempargs->buf;
791 unsigned char sha512[SHA512_DIGEST_LENGTH];
792 int count;
793 for (count = 0; COND(c[D_SHA512][testnum]); count++)
794 SHA512(buf, lengths[testnum], sha512);
795 return count;
796 }
797
798 #ifndef OPENSSL_NO_WHIRLPOOL
799 static int WHIRLPOOL_loop(void *args)
800 {
801 loopargs_t *tempargs = *(loopargs_t **) args;
802 unsigned char *buf = tempargs->buf;
803 unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
804 int count;
805 for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
806 WHIRLPOOL(buf, lengths[testnum], whirlpool);
807 return count;
808 }
809 #endif
810
811 #ifndef OPENSSL_NO_RMD160
812 static int EVP_Digest_RMD160_loop(void *args)
813 {
814 loopargs_t *tempargs = *(loopargs_t **) args;
815 unsigned char *buf = tempargs->buf;
816 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
817 int count;
818 for (count = 0; COND(c[D_RMD160][testnum]); count++) {
819 if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
820 NULL, EVP_ripemd160(), NULL))
821 return -1;
822 }
823 return count;
824 }
825 #endif
826
827 #ifndef OPENSSL_NO_RC4
828 static RC4_KEY rc4_ks;
829 static int RC4_loop(void *args)
830 {
831 loopargs_t *tempargs = *(loopargs_t **) args;
832 unsigned char *buf = tempargs->buf;
833 int count;
834 for (count = 0; COND(c[D_RC4][testnum]); count++)
835 RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
836 return count;
837 }
838 #endif
839
840 #ifndef OPENSSL_NO_DES
841 static unsigned char DES_iv[8];
842 static DES_key_schedule sch;
843 static DES_key_schedule sch2;
844 static DES_key_schedule sch3;
845 static int DES_ncbc_encrypt_loop(void *args)
846 {
847 loopargs_t *tempargs = *(loopargs_t **) args;
848 unsigned char *buf = tempargs->buf;
849 int count;
850 for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
851 DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch,
852 &DES_iv, DES_ENCRYPT);
853 return count;
854 }
855
856 static int DES_ede3_cbc_encrypt_loop(void *args)
857 {
858 loopargs_t *tempargs = *(loopargs_t **) args;
859 unsigned char *buf = tempargs->buf;
860 int count;
861 for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
862 DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
863 &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT);
864 return count;
865 }
866 #endif
867
868 #define MAX_BLOCK_SIZE 128
869
870 static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
871 static AES_KEY aes_ks1, aes_ks2, aes_ks3;
872 static int AES_cbc_128_encrypt_loop(void *args)
873 {
874 loopargs_t *tempargs = *(loopargs_t **) args;
875 unsigned char *buf = tempargs->buf;
876 int count;
877 for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
878 AES_cbc_encrypt(buf, buf,
879 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
880 return count;
881 }
882
883 static int AES_cbc_192_encrypt_loop(void *args)
884 {
885 loopargs_t *tempargs = *(loopargs_t **) args;
886 unsigned char *buf = tempargs->buf;
887 int count;
888 for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
889 AES_cbc_encrypt(buf, buf,
890 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
891 return count;
892 }
893
894 static int AES_cbc_256_encrypt_loop(void *args)
895 {
896 loopargs_t *tempargs = *(loopargs_t **) args;
897 unsigned char *buf = tempargs->buf;
898 int count;
899 for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
900 AES_cbc_encrypt(buf, buf,
901 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
902 return count;
903 }
904
905 #ifndef OPENSSL_NO_DEPRECATED_3_0
906 static int AES_ige_128_encrypt_loop(void *args)
907 {
908 loopargs_t *tempargs = *(loopargs_t **) args;
909 unsigned char *buf = tempargs->buf;
910 unsigned char *buf2 = tempargs->buf2;
911 int count;
912 for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
913 AES_ige_encrypt(buf, buf2,
914 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
915 return count;
916 }
917
918 static int AES_ige_192_encrypt_loop(void *args)
919 {
920 loopargs_t *tempargs = *(loopargs_t **) args;
921 unsigned char *buf = tempargs->buf;
922 unsigned char *buf2 = tempargs->buf2;
923 int count;
924 for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
925 AES_ige_encrypt(buf, buf2,
926 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
927 return count;
928 }
929
930 static int AES_ige_256_encrypt_loop(void *args)
931 {
932 loopargs_t *tempargs = *(loopargs_t **) args;
933 unsigned char *buf = tempargs->buf;
934 unsigned char *buf2 = tempargs->buf2;
935 int count;
936 for (count = 0; COND(c[D_IGE_256_AES][testnum]); count++)
937 AES_ige_encrypt(buf, buf2,
938 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
939 return count;
940 }
941 #endif
942
943 static int CRYPTO_gcm128_aad_loop(void *args)
944 {
945 loopargs_t *tempargs = *(loopargs_t **) args;
946 unsigned char *buf = tempargs->buf;
947 GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
948 int count;
949 for (count = 0; COND(c[D_GHASH][testnum]); count++)
950 CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
951 return count;
952 }
953
954 static int RAND_bytes_loop(void *args)
955 {
956 loopargs_t *tempargs = *(loopargs_t **) args;
957 unsigned char *buf = tempargs->buf;
958 int count;
959
960 for (count = 0; COND(c[D_RAND][testnum]); count++)
961 RAND_bytes(buf, lengths[testnum]);
962 return count;
963 }
964
965 static long save_count = 0;
966 static int decrypt = 0;
967 static int EVP_Update_loop(void *args)
968 {
969 loopargs_t *tempargs = *(loopargs_t **) args;
970 unsigned char *buf = tempargs->buf;
971 EVP_CIPHER_CTX *ctx = tempargs->ctx;
972 int outl, count, rc;
973 #ifndef SIGALRM
974 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
975 #endif
976 if (decrypt) {
977 for (count = 0; COND(nb_iter); count++) {
978 rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
979 if (rc != 1) {
980 /* reset iv in case of counter overflow */
981 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
982 }
983 }
984 } else {
985 for (count = 0; COND(nb_iter); count++) {
986 rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
987 if (rc != 1) {
988 /* reset iv in case of counter overflow */
989 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
990 }
991 }
992 }
993 if (decrypt)
994 EVP_DecryptFinal_ex(ctx, buf, &outl);
995 else
996 EVP_EncryptFinal_ex(ctx, buf, &outl);
997 return count;
998 }
999
1000 /*
1001 * CCM does not support streaming. For the purpose of performance measurement,
1002 * each message is encrypted using the same (key,iv)-pair. Do not use this
1003 * code in your application.
1004 */
1005 static int EVP_Update_loop_ccm(void *args)
1006 {
1007 loopargs_t *tempargs = *(loopargs_t **) args;
1008 unsigned char *buf = tempargs->buf;
1009 EVP_CIPHER_CTX *ctx = tempargs->ctx;
1010 int outl, count;
1011 unsigned char tag[12];
1012 #ifndef SIGALRM
1013 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
1014 #endif
1015 if (decrypt) {
1016 for (count = 0; COND(nb_iter); count++) {
1017 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
1018 /* reset iv */
1019 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
1020 /* counter is reset on every update */
1021 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
1022 }
1023 } else {
1024 for (count = 0; COND(nb_iter); count++) {
1025 /* restore iv length field */
1026 EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
1027 /* counter is reset on every update */
1028 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
1029 }
1030 }
1031 if (decrypt)
1032 EVP_DecryptFinal_ex(ctx, buf, &outl);
1033 else
1034 EVP_EncryptFinal_ex(ctx, buf, &outl);
1035 return count;
1036 }
1037
1038 /*
1039 * To make AEAD benchmarking more relevant perform TLS-like operations,
1040 * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
1041 * payload length is not actually limited by 16KB...
1042 */
1043 static int EVP_Update_loop_aead(void *args)
1044 {
1045 loopargs_t *tempargs = *(loopargs_t **) args;
1046 unsigned char *buf = tempargs->buf;
1047 EVP_CIPHER_CTX *ctx = tempargs->ctx;
1048 int outl, count;
1049 unsigned char aad[13] = { 0xcc };
1050 unsigned char faketag[16] = { 0xcc };
1051 #ifndef SIGALRM
1052 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
1053 #endif
1054 if (decrypt) {
1055 for (count = 0; COND(nb_iter); count++) {
1056 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
1057 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
1058 sizeof(faketag), faketag);
1059 EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
1060 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
1061 EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
1062 }
1063 } else {
1064 for (count = 0; COND(nb_iter); count++) {
1065 EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
1066 EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
1067 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
1068 EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
1069 }
1070 }
1071 return count;
1072 }
1073
1074 static const EVP_MD *evp_md = NULL;
1075 static int EVP_Digest_loop(void *args)
1076 {
1077 loopargs_t *tempargs = *(loopargs_t **) args;
1078 unsigned char *buf = tempargs->buf;
1079 unsigned char md[EVP_MAX_MD_SIZE];
1080 int count;
1081 #ifndef SIGALRM
1082 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
1083 #endif
1084
1085 for (count = 0; COND(nb_iter); count++) {
1086 if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
1087 return -1;
1088 }
1089 return count;
1090 }
1091
1092 static const EVP_MD *evp_hmac_md = NULL;
1093 static char *evp_hmac_name = NULL;
1094 static int EVP_HMAC_loop(void *args)
1095 {
1096 loopargs_t *tempargs = *(loopargs_t **) args;
1097 unsigned char *buf = tempargs->buf;
1098 unsigned char no_key[32];
1099 int count;
1100 #ifndef SIGALRM
1101 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
1102 #endif
1103
1104 for (count = 0; COND(nb_iter); count++) {
1105 if (HMAC(evp_hmac_md, no_key, sizeof(no_key), buf, lengths[testnum],
1106 NULL, NULL) == NULL)
1107 return -1;
1108 }
1109 return count;
1110 }
1111
1112 #ifndef OPENSSL_NO_CMAC
1113 static const EVP_CIPHER *evp_cmac_cipher = NULL;
1114 static char *evp_cmac_name = NULL;
1115
1116 static int EVP_CMAC_loop(void *args)
1117 {
1118 loopargs_t *tempargs = *(loopargs_t **) args;
1119 unsigned char *buf = tempargs->buf;
1120 CMAC_CTX *cmac_ctx = tempargs->cmac_ctx;
1121 static const char key[16] = "This is a key...";
1122 unsigned char mac[16];
1123 size_t len = sizeof(mac);
1124 int count;
1125 #ifndef SIGALRM
1126 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
1127 #endif
1128
1129 for (count = 0; COND(nb_iter); count++) {
1130 if (!CMAC_Init(cmac_ctx, key, sizeof(key), evp_cmac_cipher, NULL)
1131 || !CMAC_Update(cmac_ctx, buf, lengths[testnum])
1132 || !CMAC_Final(cmac_ctx, mac, &len))
1133 return -1;
1134 }
1135 return count;
1136 }
1137 #endif
1138
1139 #ifndef OPENSSL_NO_RSA
1140 static long rsa_c[RSA_NUM][2]; /* # RSA iteration test */
1141
1142 static int RSA_sign_loop(void *args)
1143 {
1144 loopargs_t *tempargs = *(loopargs_t **) args;
1145 unsigned char *buf = tempargs->buf;
1146 unsigned char *buf2 = tempargs->buf2;
1147 unsigned int *rsa_num = &tempargs->siglen;
1148 RSA **rsa_key = tempargs->rsa_key;
1149 int ret, count;
1150 for (count = 0; COND(rsa_c[testnum][0]); count++) {
1151 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1152 if (ret == 0) {
1153 BIO_printf(bio_err, "RSA sign failure\n");
1154 ERR_print_errors(bio_err);
1155 count = -1;
1156 break;
1157 }
1158 }
1159 return count;
1160 }
1161
1162 static int RSA_verify_loop(void *args)
1163 {
1164 loopargs_t *tempargs = *(loopargs_t **) args;
1165 unsigned char *buf = tempargs->buf;
1166 unsigned char *buf2 = tempargs->buf2;
1167 unsigned int rsa_num = tempargs->siglen;
1168 RSA **rsa_key = tempargs->rsa_key;
1169 int ret, count;
1170 for (count = 0; COND(rsa_c[testnum][1]); count++) {
1171 ret =
1172 RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1173 if (ret <= 0) {
1174 BIO_printf(bio_err, "RSA verify failure\n");
1175 ERR_print_errors(bio_err);
1176 count = -1;
1177 break;
1178 }
1179 }
1180 return count;
1181 }
1182 #endif
1183
1184 #ifndef OPENSSL_NO_DSA
1185 static long dsa_c[DSA_NUM][2];
1186 static int DSA_sign_loop(void *args)
1187 {
1188 loopargs_t *tempargs = *(loopargs_t **) args;
1189 unsigned char *buf = tempargs->buf;
1190 unsigned char *buf2 = tempargs->buf2;
1191 DSA **dsa_key = tempargs->dsa_key;
1192 unsigned int *siglen = &tempargs->siglen;
1193 int ret, count;
1194 for (count = 0; COND(dsa_c[testnum][0]); count++) {
1195 ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1196 if (ret == 0) {
1197 BIO_printf(bio_err, "DSA sign failure\n");
1198 ERR_print_errors(bio_err);
1199 count = -1;
1200 break;
1201 }
1202 }
1203 return count;
1204 }
1205
1206 static int DSA_verify_loop(void *args)
1207 {
1208 loopargs_t *tempargs = *(loopargs_t **) args;
1209 unsigned char *buf = tempargs->buf;
1210 unsigned char *buf2 = tempargs->buf2;
1211 DSA **dsa_key = tempargs->dsa_key;
1212 unsigned int siglen = tempargs->siglen;
1213 int ret, count;
1214 for (count = 0; COND(dsa_c[testnum][1]); count++) {
1215 ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1216 if (ret <= 0) {
1217 BIO_printf(bio_err, "DSA verify failure\n");
1218 ERR_print_errors(bio_err);
1219 count = -1;
1220 break;
1221 }
1222 }
1223 return count;
1224 }
1225 #endif
1226
1227 #ifndef OPENSSL_NO_EC
1228 static long ecdsa_c[ECDSA_NUM][2];
1229 static int ECDSA_sign_loop(void *args)
1230 {
1231 loopargs_t *tempargs = *(loopargs_t **) args;
1232 unsigned char *buf = tempargs->buf;
1233 EC_KEY **ecdsa = tempargs->ecdsa;
1234 unsigned char *ecdsasig = tempargs->buf2;
1235 unsigned int *ecdsasiglen = &tempargs->siglen;
1236 int ret, count;
1237 for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1238 ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1239 if (ret == 0) {
1240 BIO_printf(bio_err, "ECDSA sign failure\n");
1241 ERR_print_errors(bio_err);
1242 count = -1;
1243 break;
1244 }
1245 }
1246 return count;
1247 }
1248
1249 static int ECDSA_verify_loop(void *args)
1250 {
1251 loopargs_t *tempargs = *(loopargs_t **) args;
1252 unsigned char *buf = tempargs->buf;
1253 EC_KEY **ecdsa = tempargs->ecdsa;
1254 unsigned char *ecdsasig = tempargs->buf2;
1255 unsigned int ecdsasiglen = tempargs->siglen;
1256 int ret, count;
1257 for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1258 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1259 if (ret != 1) {
1260 BIO_printf(bio_err, "ECDSA verify failure\n");
1261 ERR_print_errors(bio_err);
1262 count = -1;
1263 break;
1264 }
1265 }
1266 return count;
1267 }
1268
1269 /* ******************************************************************** */
1270 static long ecdh_c[EC_NUM][1];
1271
1272 static int ECDH_EVP_derive_key_loop(void *args)
1273 {
1274 loopargs_t *tempargs = *(loopargs_t **) args;
1275 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1276 unsigned char *derived_secret = tempargs->secret_a;
1277 int count;
1278 size_t *outlen = &(tempargs->outlen[testnum]);
1279
1280 for (count = 0; COND(ecdh_c[testnum][0]); count++)
1281 EVP_PKEY_derive(ctx, derived_secret, outlen);
1282
1283 return count;
1284 }
1285
1286 static long eddsa_c[EdDSA_NUM][2];
1287 static int EdDSA_sign_loop(void *args)
1288 {
1289 loopargs_t *tempargs = *(loopargs_t **) args;
1290 unsigned char *buf = tempargs->buf;
1291 EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
1292 unsigned char *eddsasig = tempargs->buf2;
1293 size_t *eddsasigsize = &tempargs->sigsize;
1294 int ret, count;
1295
1296 for (count = 0; COND(eddsa_c[testnum][0]); count++) {
1297 ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
1298 if (ret == 0) {
1299 BIO_printf(bio_err, "EdDSA sign failure\n");
1300 ERR_print_errors(bio_err);
1301 count = -1;
1302 break;
1303 }
1304 }
1305 return count;
1306 }
1307
1308 static int EdDSA_verify_loop(void *args)
1309 {
1310 loopargs_t *tempargs = *(loopargs_t **) args;
1311 unsigned char *buf = tempargs->buf;
1312 EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
1313 unsigned char *eddsasig = tempargs->buf2;
1314 size_t eddsasigsize = tempargs->sigsize;
1315 int ret, count;
1316
1317 for (count = 0; COND(eddsa_c[testnum][1]); count++) {
1318 ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
1319 if (ret != 1) {
1320 BIO_printf(bio_err, "EdDSA verify failure\n");
1321 ERR_print_errors(bio_err);
1322 count = -1;
1323 break;
1324 }
1325 }
1326 return count;
1327 }
1328
1329 # ifndef OPENSSL_NO_SM2
1330 static long sm2_c[SM2_NUM][2];
1331 static int SM2_sign_loop(void *args)
1332 {
1333 loopargs_t *tempargs = *(loopargs_t **) args;
1334 unsigned char *buf = tempargs->buf;
1335 EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
1336 unsigned char *sm2sig = tempargs->buf2;
1337 size_t sm2sigsize = tempargs->sigsize;
1338 const size_t max_size = tempargs->sigsize;
1339 int ret, count;
1340 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1341
1342 for (count = 0; COND(sm2_c[testnum][0]); count++) {
1343 if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
1344 NULL, sm2_pkey[testnum])) {
1345 BIO_printf(bio_err, "SM2 init sign failure\n");
1346 ERR_print_errors(bio_err);
1347 count = -1;
1348 break;
1349 }
1350 ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
1351 buf, 20);
1352 if (ret == 0) {
1353 BIO_printf(bio_err, "SM2 sign failure\n");
1354 ERR_print_errors(bio_err);
1355 count = -1;
1356 break;
1357 }
1358 /* update the latest returned size and always use the fixed buffer size */
1359 tempargs->sigsize = sm2sigsize;
1360 sm2sigsize = max_size;
1361 }
1362
1363 return count;
1364 }
1365
1366 static int SM2_verify_loop(void *args)
1367 {
1368 loopargs_t *tempargs = *(loopargs_t **) args;
1369 unsigned char *buf = tempargs->buf;
1370 EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
1371 unsigned char *sm2sig = tempargs->buf2;
1372 size_t sm2sigsize = tempargs->sigsize;
1373 int ret, count;
1374 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1375
1376 for (count = 0; COND(sm2_c[testnum][1]); count++) {
1377 if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
1378 NULL, sm2_pkey[testnum])) {
1379 BIO_printf(bio_err, "SM2 verify init failure\n");
1380 ERR_print_errors(bio_err);
1381 count = -1;
1382 break;
1383 }
1384 ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
1385 buf, 20);
1386 if (ret != 1) {
1387 BIO_printf(bio_err, "SM2 verify failure\n");
1388 ERR_print_errors(bio_err);
1389 count = -1;
1390 break;
1391 }
1392 }
1393 return count;
1394 }
1395 # endif /* OPENSSL_NO_SM2 */
1396 #endif /* OPENSSL_NO_EC */
1397
1398 static int run_benchmark(int async_jobs,
1399 int (*loop_function) (void *), loopargs_t * loopargs)
1400 {
1401 int job_op_count = 0;
1402 int total_op_count = 0;
1403 int num_inprogress = 0;
1404 int error = 0, i = 0, ret = 0;
1405 OSSL_ASYNC_FD job_fd = 0;
1406 size_t num_job_fds = 0;
1407
1408 run = 1;
1409
1410 if (async_jobs == 0) {
1411 return loop_function((void *)&loopargs);
1412 }
1413
1414 for (i = 0; i < async_jobs && !error; i++) {
1415 loopargs_t *looparg_item = loopargs + i;
1416
1417 /* Copy pointer content (looparg_t item address) into async context */
1418 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1419 &job_op_count, loop_function,
1420 (void *)&looparg_item, sizeof(looparg_item));
1421 switch (ret) {
1422 case ASYNC_PAUSE:
1423 ++num_inprogress;
1424 break;
1425 case ASYNC_FINISH:
1426 if (job_op_count == -1) {
1427 error = 1;
1428 } else {
1429 total_op_count += job_op_count;
1430 }
1431 break;
1432 case ASYNC_NO_JOBS:
1433 case ASYNC_ERR:
1434 BIO_printf(bio_err, "Failure in the job\n");
1435 ERR_print_errors(bio_err);
1436 error = 1;
1437 break;
1438 }
1439 }
1440
1441 while (num_inprogress > 0) {
1442 #if defined(OPENSSL_SYS_WINDOWS)
1443 DWORD avail = 0;
1444 #elif defined(OPENSSL_SYS_UNIX)
1445 int select_result = 0;
1446 OSSL_ASYNC_FD max_fd = 0;
1447 fd_set waitfdset;
1448
1449 FD_ZERO(&waitfdset);
1450
1451 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1452 if (loopargs[i].inprogress_job == NULL)
1453 continue;
1454
1455 if (!ASYNC_WAIT_CTX_get_all_fds
1456 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1457 || num_job_fds > 1) {
1458 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1459 ERR_print_errors(bio_err);
1460 error = 1;
1461 break;
1462 }
1463 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1464 &num_job_fds);
1465 FD_SET(job_fd, &waitfdset);
1466 if (job_fd > max_fd)
1467 max_fd = job_fd;
1468 }
1469
1470 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1471 BIO_printf(bio_err,
1472 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1473 "Decrease the value of async_jobs\n",
1474 max_fd, FD_SETSIZE);
1475 ERR_print_errors(bio_err);
1476 error = 1;
1477 break;
1478 }
1479
1480 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1481 if (select_result == -1 && errno == EINTR)
1482 continue;
1483
1484 if (select_result == -1) {
1485 BIO_printf(bio_err, "Failure in the select\n");
1486 ERR_print_errors(bio_err);
1487 error = 1;
1488 break;
1489 }
1490
1491 if (select_result == 0)
1492 continue;
1493 #endif
1494
1495 for (i = 0; i < async_jobs; i++) {
1496 if (loopargs[i].inprogress_job == NULL)
1497 continue;
1498
1499 if (!ASYNC_WAIT_CTX_get_all_fds
1500 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1501 || num_job_fds > 1) {
1502 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1503 ERR_print_errors(bio_err);
1504 error = 1;
1505 break;
1506 }
1507 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1508 &num_job_fds);
1509
1510 #if defined(OPENSSL_SYS_UNIX)
1511 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1512 continue;
1513 #elif defined(OPENSSL_SYS_WINDOWS)
1514 if (num_job_fds == 1
1515 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
1516 && avail > 0)
1517 continue;
1518 #endif
1519
1520 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1521 loopargs[i].wait_ctx, &job_op_count,
1522 loop_function, (void *)(loopargs + i),
1523 sizeof(loopargs_t));
1524 switch (ret) {
1525 case ASYNC_PAUSE:
1526 break;
1527 case ASYNC_FINISH:
1528 if (job_op_count == -1) {
1529 error = 1;
1530 } else {
1531 total_op_count += job_op_count;
1532 }
1533 --num_inprogress;
1534 loopargs[i].inprogress_job = NULL;
1535 break;
1536 case ASYNC_NO_JOBS:
1537 case ASYNC_ERR:
1538 --num_inprogress;
1539 loopargs[i].inprogress_job = NULL;
1540 BIO_printf(bio_err, "Failure in the job\n");
1541 ERR_print_errors(bio_err);
1542 error = 1;
1543 break;
1544 }
1545 }
1546 }
1547
1548 return error ? -1 : total_op_count;
1549 }
1550
1551 int speed_main(int argc, char **argv)
1552 {
1553 ENGINE *e = NULL;
1554 loopargs_t *loopargs = NULL;
1555 const char *prog;
1556 const char *engine_id = NULL;
1557 const EVP_CIPHER *evp_cipher = NULL;
1558 double d = 0.0;
1559 OPTION_CHOICE o;
1560 int async_init = 0, multiblock = 0, pr_header = 0;
1561 int doit[ALGOR_NUM] = { 0 };
1562 int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
1563 long count = 0;
1564 unsigned int size_num = OSSL_NELEM(lengths_list);
1565 unsigned int i, k, loop, loopargs_len = 0, async_jobs = 0;
1566 int keylen;
1567 int buflen;
1568 #ifndef NO_FORK
1569 int multi = 0;
1570 #endif
1571 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
1572 || !defined(OPENSSL_NO_EC)
1573 long rsa_count = 1;
1574 #endif
1575 openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
1576 ECDSA_SECONDS, ECDH_SECONDS,
1577 EdDSA_SECONDS, SM2_SECONDS };
1578
1579 /* What follows are the buffers and key material. */
1580 #ifndef OPENSSL_NO_RC5
1581 RC5_32_KEY rc5_ks;
1582 #endif
1583 #ifndef OPENSSL_NO_RC2
1584 RC2_KEY rc2_ks;
1585 #endif
1586 #ifndef OPENSSL_NO_IDEA
1587 IDEA_KEY_SCHEDULE idea_ks;
1588 #endif
1589 #ifndef OPENSSL_NO_SEED
1590 SEED_KEY_SCHEDULE seed_ks;
1591 #endif
1592 #ifndef OPENSSL_NO_BF
1593 BF_KEY bf_ks;
1594 #endif
1595 #ifndef OPENSSL_NO_CAST
1596 CAST_KEY cast_ks;
1597 #endif
1598 static const unsigned char key16[16] = {
1599 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1600 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1601 };
1602 static const unsigned char key24[24] = {
1603 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1604 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1605 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1606 };
1607 static const unsigned char key32[32] = {
1608 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1609 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1610 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1611 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1612 };
1613 #ifndef OPENSSL_NO_CAMELLIA
1614 static const unsigned char ckey24[24] = {
1615 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1616 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1617 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1618 };
1619 static const unsigned char ckey32[32] = {
1620 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1621 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1622 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1623 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1624 };
1625 CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1626 #endif
1627 #ifndef OPENSSL_NO_DES
1628 static DES_cblock key = {
1629 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
1630 };
1631 static DES_cblock key2 = {
1632 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1633 };
1634 static DES_cblock key3 = {
1635 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1636 };
1637 #endif
1638 #ifndef OPENSSL_NO_RSA
1639 static const unsigned int rsa_bits[RSA_NUM] = {
1640 512, 1024, 2048, 3072, 4096, 7680, 15360
1641 };
1642 static const unsigned char *rsa_data[RSA_NUM] = {
1643 test512, test1024, test2048, test3072, test4096, test7680, test15360
1644 };
1645 static const int rsa_data_length[RSA_NUM] = {
1646 sizeof(test512), sizeof(test1024),
1647 sizeof(test2048), sizeof(test3072),
1648 sizeof(test4096), sizeof(test7680),
1649 sizeof(test15360)
1650 };
1651 int rsa_doit[RSA_NUM] = { 0 };
1652 int primes = RSA_DEFAULT_PRIME_NUM;
1653 #endif
1654 #ifndef OPENSSL_NO_DSA
1655 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1656 int dsa_doit[DSA_NUM] = { 0 };
1657 #endif
1658 #ifndef OPENSSL_NO_EC
1659 /*
1660 * We only test over the following curves as they are representative, To
1661 * add tests over more curves, simply add the curve NID and curve name to
1662 * the following arrays and increase the |ecdh_choices| list accordingly.
1663 */
1664 static const struct {
1665 const char *name;
1666 unsigned int nid;
1667 unsigned int bits;
1668 } test_curves[] = {
1669 /* Prime Curves */
1670 {"secp160r1", NID_secp160r1, 160},
1671 {"nistp192", NID_X9_62_prime192v1, 192},
1672 {"nistp224", NID_secp224r1, 224},
1673 {"nistp256", NID_X9_62_prime256v1, 256},
1674 {"nistp384", NID_secp384r1, 384},
1675 {"nistp521", NID_secp521r1, 521},
1676 # ifndef OPENSSL_NO_EC2M
1677 /* Binary Curves */
1678 {"nistk163", NID_sect163k1, 163},
1679 {"nistk233", NID_sect233k1, 233},
1680 {"nistk283", NID_sect283k1, 283},
1681 {"nistk409", NID_sect409k1, 409},
1682 {"nistk571", NID_sect571k1, 571},
1683 {"nistb163", NID_sect163r2, 163},
1684 {"nistb233", NID_sect233r1, 233},
1685 {"nistb283", NID_sect283r1, 283},
1686 {"nistb409", NID_sect409r1, 409},
1687 {"nistb571", NID_sect571r1, 571},
1688 # endif
1689 {"brainpoolP256r1", NID_brainpoolP256r1, 256},
1690 {"brainpoolP256t1", NID_brainpoolP256t1, 256},
1691 {"brainpoolP384r1", NID_brainpoolP384r1, 384},
1692 {"brainpoolP384t1", NID_brainpoolP384t1, 384},
1693 {"brainpoolP512r1", NID_brainpoolP512r1, 512},
1694 {"brainpoolP512t1", NID_brainpoolP512t1, 512},
1695 /* Other and ECDH only ones */
1696 {"X25519", NID_X25519, 253},
1697 {"X448", NID_X448, 448}
1698 };
1699 static const struct {
1700 const char *name;
1701 unsigned int nid;
1702 unsigned int bits;
1703 size_t sigsize;
1704 } test_ed_curves[] = {
1705 /* EdDSA */
1706 {"Ed25519", NID_ED25519, 253, 64},
1707 {"Ed448", NID_ED448, 456, 114}
1708 };
1709 # ifndef OPENSSL_NO_SM2
1710 static const struct {
1711 const char *name;
1712 unsigned int nid;
1713 unsigned int bits;
1714 } test_sm2_curves[] = {
1715 /* SM2 */
1716 {"CurveSM2", NID_sm2, 256}
1717 };
1718 # endif
1719 int ecdsa_doit[ECDSA_NUM] = { 0 };
1720 int ecdh_doit[EC_NUM] = { 0 };
1721 int eddsa_doit[EdDSA_NUM] = { 0 };
1722 # ifndef OPENSSL_NO_SM2
1723 int sm2_doit[SM2_NUM] = { 0 };
1724 # endif
1725 OPENSSL_assert(OSSL_NELEM(test_curves) >= EC_NUM);
1726 OPENSSL_assert(OSSL_NELEM(test_ed_curves) >= EdDSA_NUM);
1727 # ifndef OPENSSL_NO_SM2
1728 OPENSSL_assert(OSSL_NELEM(test_sm2_curves) >= SM2_NUM);
1729 # endif
1730 #endif /* ndef OPENSSL_NO_EC */
1731
1732 prog = opt_init(argc, argv, speed_options);
1733 while ((o = opt_next()) != OPT_EOF) {
1734 switch (o) {
1735 case OPT_EOF:
1736 case OPT_ERR:
1737 opterr:
1738 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1739 goto end;
1740 case OPT_HELP:
1741 opt_help(speed_options);
1742 ret = 0;
1743 goto end;
1744 case OPT_ELAPSED:
1745 usertime = 0;
1746 break;
1747 case OPT_EVP:
1748 evp_md = NULL;
1749 evp_cipher = EVP_get_cipherbyname(opt_arg());
1750 if (evp_cipher == NULL)
1751 evp_md = EVP_get_digestbyname(opt_arg());
1752 if (evp_cipher == NULL && evp_md == NULL) {
1753 BIO_printf(bio_err,
1754 "%s: %s is an unknown cipher or digest\n",
1755 prog, opt_arg());
1756 goto end;
1757 }
1758 doit[D_EVP] = 1;
1759 break;
1760 case OPT_HMAC:
1761 evp_hmac_md = EVP_get_digestbyname(opt_arg());
1762 if (evp_hmac_md == NULL) {
1763 BIO_printf(bio_err, "%s: %s is an unknown digest\n",
1764 prog, opt_arg());
1765 goto end;
1766 }
1767 doit[D_EVP_HMAC] = 1;
1768 break;
1769 case OPT_CMAC:
1770 #ifndef OPENSSL_NO_CMAC
1771 evp_cmac_cipher = EVP_get_cipherbyname(opt_arg());
1772 if (evp_cmac_cipher == NULL) {
1773 BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
1774 prog, opt_arg());
1775 goto end;
1776 }
1777 doit[D_EVP_CMAC] = 1;
1778 #endif
1779 break;
1780 case OPT_DECRYPT:
1781 decrypt = 1;
1782 break;
1783 case OPT_ENGINE:
1784 /*
1785 * In a forked execution, an engine might need to be
1786 * initialised by each child process, not by the parent.
1787 * So store the name here and run setup_engine() later on.
1788 */
1789 engine_id = opt_arg();
1790 break;
1791 case OPT_MULTI:
1792 #ifndef NO_FORK
1793 multi = atoi(opt_arg());
1794 #endif
1795 break;
1796 case OPT_ASYNCJOBS:
1797 #ifndef OPENSSL_NO_ASYNC
1798 async_jobs = atoi(opt_arg());
1799 if (!ASYNC_is_capable()) {
1800 BIO_printf(bio_err,
1801 "%s: async_jobs specified but async not supported\n",
1802 prog);
1803 goto opterr;
1804 }
1805 if (async_jobs > 99999) {
1806 BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
1807 goto opterr;
1808 }
1809 #endif
1810 break;
1811 case OPT_MISALIGN:
1812 if (!opt_int(opt_arg(), &misalign))
1813 goto end;
1814 if (misalign > MISALIGN) {
1815 BIO_printf(bio_err,
1816 "%s: Maximum offset is %d\n", prog, MISALIGN);
1817 goto opterr;
1818 }
1819 break;
1820 case OPT_MR:
1821 mr = 1;
1822 break;
1823 case OPT_MB:
1824 multiblock = 1;
1825 #ifdef OPENSSL_NO_MULTIBLOCK
1826 BIO_printf(bio_err,
1827 "%s: -mb specified but multi-block support is disabled\n",
1828 prog);
1829 goto end;
1830 #endif
1831 break;
1832 case OPT_R_CASES:
1833 if (!opt_rand(o))
1834 goto end;
1835 break;
1836 case OPT_PRIMES:
1837 if (!opt_int(opt_arg(), &primes))
1838 goto end;
1839 break;
1840 case OPT_SECONDS:
1841 seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1842 = seconds.ecdh = seconds.eddsa
1843 = seconds.sm2 = atoi(opt_arg());
1844 break;
1845 case OPT_BYTES:
1846 lengths_single = atoi(opt_arg());
1847 lengths = &lengths_single;
1848 size_num = 1;
1849 break;
1850 case OPT_AEAD:
1851 aead = 1;
1852 break;
1853 }
1854 }
1855 argc = opt_num_rest();
1856 argv = opt_rest();
1857
1858 /* Remaining arguments are algorithms. */
1859 for (; *argv; argv++) {
1860 if (found(*argv, doit_choices, &i)) {
1861 doit[i] = 1;
1862 continue;
1863 }
1864 #ifndef OPENSSL_NO_DES
1865 if (strcmp(*argv, "des") == 0) {
1866 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1867 continue;
1868 }
1869 #endif
1870 if (strcmp(*argv, "sha") == 0) {
1871 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1872 continue;
1873 }
1874 #ifndef OPENSSL_NO_RSA
1875 if (strcmp(*argv, "openssl") == 0)
1876 continue;
1877 if (strcmp(*argv, "rsa") == 0) {
1878 for (loop = 0; loop < OSSL_NELEM(rsa_doit); loop++)
1879 rsa_doit[loop] = 1;
1880 continue;
1881 }
1882 if (found(*argv, rsa_choices, &i)) {
1883 rsa_doit[i] = 1;
1884 continue;
1885 }
1886 #endif
1887 #ifndef OPENSSL_NO_DSA
1888 if (strcmp(*argv, "dsa") == 0) {
1889 dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1890 dsa_doit[R_DSA_2048] = 1;
1891 continue;
1892 }
1893 if (found(*argv, dsa_choices, &i)) {
1894 dsa_doit[i] = 2;
1895 continue;
1896 }
1897 #endif
1898 if (strcmp(*argv, "aes") == 0) {
1899 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1900 continue;
1901 }
1902 #ifndef OPENSSL_NO_CAMELLIA
1903 if (strcmp(*argv, "camellia") == 0) {
1904 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1905 continue;
1906 }
1907 #endif
1908 #ifndef OPENSSL_NO_EC
1909 if (strcmp(*argv, "ecdsa") == 0) {
1910 for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
1911 ecdsa_doit[loop] = 1;
1912 continue;
1913 }
1914 if (found(*argv, ecdsa_choices, &i)) {
1915 ecdsa_doit[i] = 2;
1916 continue;
1917 }
1918 if (strcmp(*argv, "ecdh") == 0) {
1919 for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
1920 ecdh_doit[loop] = 1;
1921 continue;
1922 }
1923 if (found(*argv, ecdh_choices, &i)) {
1924 ecdh_doit[i] = 2;
1925 continue;
1926 }
1927 if (strcmp(*argv, "eddsa") == 0) {
1928 for (loop = 0; loop < OSSL_NELEM(eddsa_doit); loop++)
1929 eddsa_doit[loop] = 1;
1930 continue;
1931 }
1932 if (found(*argv, eddsa_choices, &i)) {
1933 eddsa_doit[i] = 2;
1934 continue;
1935 }
1936 # ifndef OPENSSL_NO_SM2
1937 if (strcmp(*argv, "sm2") == 0) {
1938 for (loop = 0; loop < OSSL_NELEM(sm2_doit); loop++)
1939 sm2_doit[loop] = 1;
1940 continue;
1941 }
1942 if (found(*argv, sm2_choices, &i)) {
1943 sm2_doit[i] = 2;
1944 continue;
1945 }
1946 # endif
1947 #endif
1948 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1949 goto end;
1950 }
1951
1952 /* Sanity checks */
1953 if (aead) {
1954 if (evp_cipher == NULL) {
1955 BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
1956 goto end;
1957 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1958 EVP_CIPH_FLAG_AEAD_CIPHER)) {
1959 BIO_printf(bio_err, "%s is not an AEAD cipher\n",
1960 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1961 goto end;
1962 }
1963 }
1964 if (multiblock) {
1965 if (evp_cipher == NULL) {
1966 BIO_printf(bio_err,"-mb can be used only with a multi-block"
1967 " capable cipher\n");
1968 goto end;
1969 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1970 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1971 BIO_printf(bio_err, "%s is not a multi-block capable\n",
1972 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1973 goto end;
1974 } else if (async_jobs > 0) {
1975 BIO_printf(bio_err, "Async mode is not supported with -mb");
1976 goto end;
1977 }
1978 }
1979
1980 /* Initialize the job pool if async mode is enabled */
1981 if (async_jobs > 0) {
1982 async_init = ASYNC_init_thread(async_jobs, async_jobs);
1983 if (!async_init) {
1984 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1985 goto end;
1986 }
1987 }
1988
1989 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1990 loopargs =
1991 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1992 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1993
1994 for (i = 0; i < loopargs_len; i++) {
1995 if (async_jobs > 0) {
1996 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1997 if (loopargs[i].wait_ctx == NULL) {
1998 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1999 goto end;
2000 }
2001 }
2002
2003 buflen = lengths[size_num - 1];
2004 if (buflen < 36) /* size of random vector in RSA benchmark */
2005 buflen = 36;
2006 buflen += MAX_MISALIGNMENT + 1;
2007 loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
2008 loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
2009 memset(loopargs[i].buf_malloc, 0, buflen);
2010 memset(loopargs[i].buf2_malloc, 0, buflen);
2011
2012 /* Align the start of buffers on a 64 byte boundary */
2013 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
2014 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
2015 #ifndef OPENSSL_NO_EC
2016 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
2017 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
2018 #endif
2019 }
2020
2021 #ifndef NO_FORK
2022 if (multi && do_multi(multi, size_num))
2023 goto show_res;
2024 #endif
2025
2026 /* Initialize the engine after the fork */
2027 e = setup_engine(engine_id, 0);
2028
2029 /* No parameters; turn on everything. */
2030 if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC] && !doit[D_EVP_CMAC]) {
2031 for (i = 0; i < ALGOR_NUM; i++)
2032 if (i != D_EVP && i != D_EVP_HMAC && i != D_EVP_CMAC)
2033 doit[i] = 1;
2034 #ifndef OPENSSL_NO_RSA
2035 for (i = 0; i < RSA_NUM; i++)
2036 rsa_doit[i] = 1;
2037 #endif
2038 #ifndef OPENSSL_NO_DSA
2039 for (i = 0; i < DSA_NUM; i++)
2040 dsa_doit[i] = 1;
2041 #endif
2042 #ifndef OPENSSL_NO_EC
2043 for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
2044 ecdsa_doit[loop] = 1;
2045 for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
2046 ecdh_doit[loop] = 1;
2047 for (loop = 0; loop < OSSL_NELEM(eddsa_doit); loop++)
2048 eddsa_doit[loop] = 1;
2049 # ifndef OPENSSL_NO_SM2
2050 for (loop = 0; loop < OSSL_NELEM(sm2_doit); loop++)
2051 sm2_doit[loop] = 1;
2052 # endif
2053 #endif
2054 }
2055 for (i = 0; i < ALGOR_NUM; i++)
2056 if (doit[i])
2057 pr_header++;
2058
2059 if (usertime == 0 && !mr)
2060 BIO_printf(bio_err,
2061 "You have chosen to measure elapsed time "
2062 "instead of user CPU time.\n");
2063
2064 #ifndef OPENSSL_NO_RSA
2065 for (i = 0; i < loopargs_len; i++) {
2066 if (primes > RSA_DEFAULT_PRIME_NUM) {
2067 /* for multi-prime RSA, skip this */
2068 break;
2069 }
2070 for (k = 0; k < RSA_NUM; k++) {
2071 const unsigned char *p;
2072
2073 p = rsa_data[k];
2074 loopargs[i].rsa_key[k] =
2075 d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
2076 if (loopargs[i].rsa_key[k] == NULL) {
2077 BIO_printf(bio_err,
2078 "internal error loading RSA key number %d\n", k);
2079 goto end;
2080 }
2081 }
2082 }
2083 #endif
2084 #ifndef OPENSSL_NO_DSA
2085 for (i = 0; i < loopargs_len; i++) {
2086 loopargs[i].dsa_key[0] = get_dsa(512);
2087 loopargs[i].dsa_key[1] = get_dsa(1024);
2088 loopargs[i].dsa_key[2] = get_dsa(2048);
2089 }
2090 #endif
2091 #ifndef OPENSSL_NO_DES
2092 DES_set_key_unchecked(&key, &sch);
2093 DES_set_key_unchecked(&key2, &sch2);
2094 DES_set_key_unchecked(&key3, &sch3);
2095 #endif
2096 AES_set_encrypt_key(key16, 128, &aes_ks1);
2097 AES_set_encrypt_key(key24, 192, &aes_ks2);
2098 AES_set_encrypt_key(key32, 256, &aes_ks3);
2099 #ifndef OPENSSL_NO_CAMELLIA
2100 Camellia_set_key(key16, 128, &camellia_ks1);
2101 Camellia_set_key(ckey24, 192, &camellia_ks2);
2102 Camellia_set_key(ckey32, 256, &camellia_ks3);
2103 #endif
2104 #ifndef OPENSSL_NO_IDEA
2105 IDEA_set_encrypt_key(key16, &idea_ks);
2106 #endif
2107 #ifndef OPENSSL_NO_SEED
2108 SEED_set_key(key16, &seed_ks);
2109 #endif
2110 #ifndef OPENSSL_NO_RC4
2111 RC4_set_key(&rc4_ks, 16, key16);
2112 #endif
2113 #ifndef OPENSSL_NO_RC2
2114 RC2_set_key(&rc2_ks, 16, key16, 128);
2115 #endif
2116 #ifndef OPENSSL_NO_RC5
2117 if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
2118 BIO_printf(bio_err, "Failed setting RC5 key\n");
2119 goto end;
2120 }
2121 #endif
2122 #ifndef OPENSSL_NO_BF
2123 BF_set_key(&bf_ks, 16, key16);
2124 #endif
2125 #ifndef OPENSSL_NO_CAST
2126 CAST_set_key(&cast_ks, 16, key16);
2127 #endif
2128 #ifndef SIGALRM
2129 # ifndef OPENSSL_NO_DES
2130 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
2131 count = 10;
2132 do {
2133 long it;
2134 count *= 2;
2135 Time_F(START);
2136 for (it = count; it; it--)
2137 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
2138 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
2139 d = Time_F(STOP);
2140 } while (d < 3);
2141 save_count = count;
2142 c[D_MD2][0] = count / 10;
2143 c[D_MDC2][0] = count / 10;
2144 c[D_MD4][0] = count;
2145 c[D_MD5][0] = count;
2146 c[D_HMAC][0] = count;
2147 c[D_SHA1][0] = count;
2148 c[D_RMD160][0] = count;
2149 c[D_RC4][0] = count * 5;
2150 c[D_CBC_DES][0] = count;
2151 c[D_EDE3_DES][0] = count / 3;
2152 c[D_CBC_IDEA][0] = count;
2153 c[D_CBC_SEED][0] = count;
2154 c[D_CBC_RC2][0] = count;
2155 c[D_CBC_RC5][0] = count;
2156 c[D_CBC_BF][0] = count;
2157 c[D_CBC_CAST][0] = count;
2158 c[D_CBC_128_AES][0] = count;
2159 c[D_CBC_192_AES][0] = count;
2160 c[D_CBC_256_AES][0] = count;
2161 c[D_CBC_128_CML][0] = count;
2162 c[D_CBC_192_CML][0] = count;
2163 c[D_CBC_256_CML][0] = count;
2164 c[D_SHA256][0] = count;
2165 c[D_SHA512][0] = count;
2166 c[D_WHIRLPOOL][0] = count;
2167 c[D_IGE_128_AES][0] = count;
2168 c[D_IGE_192_AES][0] = count;
2169 c[D_IGE_256_AES][0] = count;
2170 c[D_GHASH][0] = count;
2171 c[D_RAND][0] = count;
2172
2173 for (i = 1; i < size_num; i++) {
2174 long l0, l1;
2175
2176 l0 = (long)lengths[0];
2177 l1 = (long)lengths[i];
2178
2179 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
2180 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
2181 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
2182 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
2183 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
2184 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
2185 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
2186 c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
2187 c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
2188 c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
2189 c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
2190 c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
2191
2192 l0 = (long)lengths[i - 1];
2193
2194 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
2195 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
2196 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
2197 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
2198 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
2199 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
2200 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
2201 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
2202 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
2203 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
2204 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
2205 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
2206 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
2207 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
2208 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
2209 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
2210 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
2211 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
2212 }
2213
2214 # ifndef OPENSSL_NO_RSA
2215 rsa_c[R_RSA_512][0] = count / 2000;
2216 rsa_c[R_RSA_512][1] = count / 400;
2217 for (i = 1; i < RSA_NUM; i++) {
2218 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
2219 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
2220 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
2221 rsa_doit[i] = 0;
2222 else {
2223 if (rsa_c[i][0] == 0) {
2224 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2225 rsa_c[i][1] = 20;
2226 }
2227 }
2228 }
2229 # endif
2230
2231 # ifndef OPENSSL_NO_DSA
2232 dsa_c[R_DSA_512][0] = count / 1000;
2233 dsa_c[R_DSA_512][1] = count / 1000 / 2;
2234 for (i = 1; i < DSA_NUM; i++) {
2235 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
2236 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
2237 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
2238 dsa_doit[i] = 0;
2239 else {
2240 if (dsa_c[i][0] == 0) {
2241 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2242 dsa_c[i][1] = 1;
2243 }
2244 }
2245 }
2246 # endif
2247
2248 # ifndef OPENSSL_NO_EC
2249 ecdsa_c[R_EC_P160][0] = count / 1000;
2250 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
2251 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2252 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2253 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2254 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2255 ecdsa_doit[i] = 0;
2256 else {
2257 if (ecdsa_c[i][0] == 0) {
2258 ecdsa_c[i][0] = 1;
2259 ecdsa_c[i][1] = 1;
2260 }
2261 }
2262 }
2263 # ifndef OPENSSL_NO_EC2M
2264 ecdsa_c[R_EC_K163][0] = count / 1000;
2265 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
2266 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2267 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2268 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2269 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2270 ecdsa_doit[i] = 0;
2271 else {
2272 if (ecdsa_c[i][0] == 0) {
2273 ecdsa_c[i][0] = 1;
2274 ecdsa_c[i][1] = 1;
2275 }
2276 }
2277 }
2278 ecdsa_c[R_EC_B163][0] = count / 1000;
2279 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
2280 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2281 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2282 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2283 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2284 ecdsa_doit[i] = 0;
2285 else {
2286 if (ecdsa_c[i][0] == 0) {
2287 ecdsa_c[i][0] = 1;
2288 ecdsa_c[i][1] = 1;
2289 }
2290 }
2291 }
2292 # endif
2293
2294 ecdh_c[R_EC_P160][0] = count / 1000;
2295 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2296 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2297 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2298 ecdh_doit[i] = 0;
2299 else {
2300 if (ecdh_c[i][0] == 0) {
2301 ecdh_c[i][0] = 1;
2302 }
2303 }
2304 }
2305 # ifndef OPENSSL_NO_EC2M
2306 ecdh_c[R_EC_K163][0] = count / 1000;
2307 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2308 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2309 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2310 ecdh_doit[i] = 0;
2311 else {
2312 if (ecdh_c[i][0] == 0) {
2313 ecdh_c[i][0] = 1;
2314 }
2315 }
2316 }
2317 ecdh_c[R_EC_B163][0] = count / 1000;
2318 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2319 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2320 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2321 ecdh_doit[i] = 0;
2322 else {
2323 if (ecdh_c[i][0] == 0) {
2324 ecdh_c[i][0] = 1;
2325 }
2326 }
2327 }
2328 # endif
2329 /* repeated code good to factorize */
2330 ecdh_c[R_EC_BRP256R1][0] = count / 1000;
2331 for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
2332 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2333 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2334 ecdh_doit[i] = 0;
2335 else {
2336 if (ecdh_c[i][0] == 0) {
2337 ecdh_c[i][0] = 1;
2338 }
2339 }
2340 }
2341 ecdh_c[R_EC_BRP256T1][0] = count / 1000;
2342 for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
2343 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2344 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2345 ecdh_doit[i] = 0;
2346 else {
2347 if (ecdh_c[i][0] == 0) {
2348 ecdh_c[i][0] = 1;
2349 }
2350 }
2351 }
2352 /* default iteration count for the last two EC Curves */
2353 ecdh_c[R_EC_X25519][0] = count / 1800;
2354 ecdh_c[R_EC_X448][0] = count / 7200;
2355
2356 eddsa_c[R_EC_Ed25519][0] = count / 1800;
2357 eddsa_c[R_EC_Ed448][0] = count / 7200;
2358
2359 # ifndef OPENSSL_NO_SM2
2360 sm2_c[R_EC_SM2P256][0] = count / 1800;
2361 # endif
2362 # endif
2363
2364 # else
2365 /* not worth fixing */
2366 # error "You cannot disable DES on systems without SIGALRM."
2367 # endif /* OPENSSL_NO_DES */
2368 #elif SIGALRM > 0
2369 signal(SIGALRM, alarmed);
2370 #endif /* SIGALRM */
2371
2372 #ifndef OPENSSL_NO_MD2
2373 if (doit[D_MD2]) {
2374 for (testnum = 0; testnum < size_num; testnum++) {
2375 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
2376 seconds.sym);
2377 Time_F(START);
2378 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
2379 d = Time_F(STOP);
2380 print_result(D_MD2, testnum, count, d);
2381 }
2382 }
2383 #endif
2384 #ifndef OPENSSL_NO_MDC2
2385 if (doit[D_MDC2]) {
2386 for (testnum = 0; testnum < size_num; testnum++) {
2387 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
2388 seconds.sym);
2389 Time_F(START);
2390 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
2391 d = Time_F(STOP);
2392 print_result(D_MDC2, testnum, count, d);
2393 }
2394 }
2395 #endif
2396
2397 #ifndef OPENSSL_NO_MD4
2398 if (doit[D_MD4]) {
2399 for (testnum = 0; testnum < size_num; testnum++) {
2400 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
2401 seconds.sym);
2402 Time_F(START);
2403 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
2404 d = Time_F(STOP);
2405 print_result(D_MD4, testnum, count, d);
2406 }
2407 }
2408 #endif
2409
2410 #ifndef OPENSSL_NO_MD5
2411 if (doit[D_MD5]) {
2412 for (testnum = 0; testnum < size_num; testnum++) {
2413 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
2414 seconds.sym);
2415 Time_F(START);
2416 count = run_benchmark(async_jobs, MD5_loop, loopargs);
2417 d = Time_F(STOP);
2418 print_result(D_MD5, testnum, count, d);
2419 }
2420 }
2421
2422 if (doit[D_HMAC]) {
2423 static const char hmac_key[] = "This is a key...";
2424 int len = strlen(hmac_key);
2425
2426 for (i = 0; i < loopargs_len; i++) {
2427 loopargs[i].hctx = HMAC_CTX_new();
2428 if (loopargs[i].hctx == NULL) {
2429 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2430 exit(1);
2431 }
2432
2433 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2434 }
2435 for (testnum = 0; testnum < size_num; testnum++) {
2436 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
2437 seconds.sym);
2438 Time_F(START);
2439 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2440 d = Time_F(STOP);
2441 print_result(D_HMAC, testnum, count, d);
2442 }
2443 for (i = 0; i < loopargs_len; i++) {
2444 HMAC_CTX_free(loopargs[i].hctx);
2445 }
2446 }
2447 #endif
2448 if (doit[D_SHA1]) {
2449 for (testnum = 0; testnum < size_num; testnum++) {
2450 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2451 seconds.sym);
2452 Time_F(START);
2453 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2454 d = Time_F(STOP);
2455 print_result(D_SHA1, testnum, count, d);
2456 }
2457 }
2458 if (doit[D_SHA256]) {
2459 for (testnum = 0; testnum < size_num; testnum++) {
2460 print_message(names[D_SHA256], c[D_SHA256][testnum],
2461 lengths[testnum], seconds.sym);
2462 Time_F(START);
2463 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2464 d = Time_F(STOP);
2465 print_result(D_SHA256, testnum, count, d);
2466 }
2467 }
2468 if (doit[D_SHA512]) {
2469 for (testnum = 0; testnum < size_num; testnum++) {
2470 print_message(names[D_SHA512], c[D_SHA512][testnum],
2471 lengths[testnum], seconds.sym);
2472 Time_F(START);
2473 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2474 d = Time_F(STOP);
2475 print_result(D_SHA512, testnum, count, d);
2476 }
2477 }
2478 #ifndef OPENSSL_NO_WHIRLPOOL
2479 if (doit[D_WHIRLPOOL]) {
2480 for (testnum = 0; testnum < size_num; testnum++) {
2481 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2482 lengths[testnum], seconds.sym);
2483 Time_F(START);
2484 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2485 d = Time_F(STOP);
2486 print_result(D_WHIRLPOOL, testnum, count, d);
2487 }
2488 }
2489 #endif
2490
2491 #ifndef OPENSSL_NO_RMD160
2492 if (doit[D_RMD160]) {
2493 for (testnum = 0; testnum < size_num; testnum++) {
2494 print_message(names[D_RMD160], c[D_RMD160][testnum],
2495 lengths[testnum], seconds.sym);
2496 Time_F(START);
2497 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2498 d = Time_F(STOP);
2499 print_result(D_RMD160, testnum, count, d);
2500 }
2501 }
2502 #endif
2503 #ifndef OPENSSL_NO_RC4
2504 if (doit[D_RC4]) {
2505 for (testnum = 0; testnum < size_num; testnum++) {
2506 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2507 seconds.sym);
2508 Time_F(START);
2509 count = run_benchmark(async_jobs, RC4_loop, loopargs);
2510 d = Time_F(STOP);
2511 print_result(D_RC4, testnum, count, d);
2512 }
2513 }
2514 #endif
2515 #ifndef OPENSSL_NO_DES
2516 if (doit[D_CBC_DES]) {
2517 for (testnum = 0; testnum < size_num; testnum++) {
2518 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2519 lengths[testnum], seconds.sym);
2520 Time_F(START);
2521 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2522 d = Time_F(STOP);
2523 print_result(D_CBC_DES, testnum, count, d);
2524 }
2525 }
2526
2527 if (doit[D_EDE3_DES]) {
2528 for (testnum = 0; testnum < size_num; testnum++) {
2529 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2530 lengths[testnum], seconds.sym);
2531 Time_F(START);
2532 count =
2533 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2534 d = Time_F(STOP);
2535 print_result(D_EDE3_DES, testnum, count, d);
2536 }
2537 }
2538 #endif
2539
2540 if (doit[D_CBC_128_AES]) {
2541 for (testnum = 0; testnum < size_num; testnum++) {
2542 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2543 lengths[testnum], seconds.sym);
2544 Time_F(START);
2545 count =
2546 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2547 d = Time_F(STOP);
2548 print_result(D_CBC_128_AES, testnum, count, d);
2549 }
2550 }
2551 if (doit[D_CBC_192_AES]) {
2552 for (testnum = 0; testnum < size_num; testnum++) {
2553 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2554 lengths[testnum], seconds.sym);
2555 Time_F(START);
2556 count =
2557 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2558 d = Time_F(STOP);
2559 print_result(D_CBC_192_AES, testnum, count, d);
2560 }
2561 }
2562 if (doit[D_CBC_256_AES]) {
2563 for (testnum = 0; testnum < size_num; testnum++) {
2564 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2565 lengths[testnum], seconds.sym);
2566 Time_F(START);
2567 count =
2568 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2569 d = Time_F(STOP);
2570 print_result(D_CBC_256_AES, testnum, count, d);
2571 }
2572 }
2573
2574 #ifndef OPENSSL_NO_DEPRECATED_3_0
2575 if (doit[D_IGE_128_AES]) {
2576 for (testnum = 0; testnum < size_num; testnum++) {
2577 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2578 lengths[testnum], seconds.sym);
2579 Time_F(START);
2580 count =
2581 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2582 d = Time_F(STOP);
2583 print_result(D_IGE_128_AES, testnum, count, d);
2584 }
2585 }
2586 if (doit[D_IGE_192_AES]) {
2587 for (testnum = 0; testnum < size_num; testnum++) {
2588 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2589 lengths[testnum], seconds.sym);
2590 Time_F(START);
2591 count =
2592 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2593 d = Time_F(STOP);
2594 print_result(D_IGE_192_AES, testnum, count, d);
2595 }
2596 }
2597 if (doit[D_IGE_256_AES]) {
2598 for (testnum = 0; testnum < size_num; testnum++) {
2599 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2600 lengths[testnum], seconds.sym);
2601 Time_F(START);
2602 count =
2603 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2604 d = Time_F(STOP);
2605 print_result(D_IGE_256_AES, testnum, count, d);
2606 }
2607 }
2608 #endif
2609 if (doit[D_GHASH]) {
2610 for (i = 0; i < loopargs_len; i++) {
2611 loopargs[i].gcm_ctx =
2612 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2613 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2614 (unsigned char *)"0123456789ab", 12);
2615 }
2616
2617 for (testnum = 0; testnum < size_num; testnum++) {
2618 print_message(names[D_GHASH], c[D_GHASH][testnum],
2619 lengths[testnum], seconds.sym);
2620 Time_F(START);
2621 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2622 d = Time_F(STOP);
2623 print_result(D_GHASH, testnum, count, d);
2624 }
2625 for (i = 0; i < loopargs_len; i++)
2626 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2627 }
2628 #ifndef OPENSSL_NO_CAMELLIA
2629 if (doit[D_CBC_128_CML]) {
2630 if (async_jobs > 0) {
2631 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2632 names[D_CBC_128_CML]);
2633 doit[D_CBC_128_CML] = 0;
2634 }
2635 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2636 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2637 lengths[testnum], seconds.sym);
2638 Time_F(START);
2639 for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2640 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2641 (size_t)lengths[testnum], &camellia_ks1,
2642 iv, CAMELLIA_ENCRYPT);
2643 d = Time_F(STOP);
2644 print_result(D_CBC_128_CML, testnum, count, d);
2645 }
2646 }
2647 if (doit[D_CBC_192_CML]) {
2648 if (async_jobs > 0) {
2649 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2650 names[D_CBC_192_CML]);
2651 doit[D_CBC_192_CML] = 0;
2652 }
2653 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2654 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2655 lengths[testnum], seconds.sym);
2656 if (async_jobs > 0) {
2657 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2658 exit(1);
2659 }
2660 Time_F(START);
2661 for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2662 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2663 (size_t)lengths[testnum], &camellia_ks2,
2664 iv, CAMELLIA_ENCRYPT);
2665 d = Time_F(STOP);
2666 print_result(D_CBC_192_CML, testnum, count, d);
2667 }
2668 }
2669 if (doit[D_CBC_256_CML]) {
2670 if (async_jobs > 0) {
2671 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2672 names[D_CBC_256_CML]);
2673 doit[D_CBC_256_CML] = 0;
2674 }
2675 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2676 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2677 lengths[testnum], seconds.sym);
2678 Time_F(START);
2679 for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2680 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2681 (size_t)lengths[testnum], &camellia_ks3,
2682 iv, CAMELLIA_ENCRYPT);
2683 d = Time_F(STOP);
2684 print_result(D_CBC_256_CML, testnum, count, d);
2685 }
2686 }
2687 #endif
2688 #ifndef OPENSSL_NO_IDEA
2689 if (doit[D_CBC_IDEA]) {
2690 if (async_jobs > 0) {
2691 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2692 names[D_CBC_IDEA]);
2693 doit[D_CBC_IDEA] = 0;
2694 }
2695 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2696 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2697 lengths[testnum], seconds.sym);
2698 Time_F(START);
2699 for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
2700 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2701 (size_t)lengths[testnum], &idea_ks,
2702 iv, IDEA_ENCRYPT);
2703 d = Time_F(STOP);
2704 print_result(D_CBC_IDEA, testnum, count, d);
2705 }
2706 }
2707 #endif
2708 #ifndef OPENSSL_NO_SEED
2709 if (doit[D_CBC_SEED]) {
2710 if (async_jobs > 0) {
2711 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2712 names[D_CBC_SEED]);
2713 doit[D_CBC_SEED] = 0;
2714 }
2715 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2716 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2717 lengths[testnum], seconds.sym);
2718 Time_F(START);
2719 for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2720 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2721 (size_t)lengths[testnum], &seed_ks, iv, 1);
2722 d = Time_F(STOP);
2723 print_result(D_CBC_SEED, testnum, count, d);
2724 }
2725 }
2726 #endif
2727 #ifndef OPENSSL_NO_RC2
2728 if (doit[D_CBC_RC2]) {
2729 if (async_jobs > 0) {
2730 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2731 names[D_CBC_RC2]);
2732 doit[D_CBC_RC2] = 0;
2733 }
2734 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2735 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2736 lengths[testnum], seconds.sym);
2737 if (async_jobs > 0) {
2738 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2739 exit(1);
2740 }
2741 Time_F(START);
2742 for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2743 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2744 (size_t)lengths[testnum], &rc2_ks,
2745 iv, RC2_ENCRYPT);
2746 d = Time_F(STOP);
2747 print_result(D_CBC_RC2, testnum, count, d);
2748 }
2749 }
2750 #endif
2751 #ifndef OPENSSL_NO_RC5
2752 if (doit[D_CBC_RC5]) {
2753 if (async_jobs > 0) {
2754 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2755 names[D_CBC_RC5]);
2756 doit[D_CBC_RC5] = 0;
2757 }
2758 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2759 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2760 lengths[testnum], seconds.sym);
2761 if (async_jobs > 0) {
2762 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2763 exit(1);
2764 }
2765 Time_F(START);
2766 for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2767 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2768 (size_t)lengths[testnum], &rc5_ks,
2769 iv, RC5_ENCRYPT);
2770 d = Time_F(STOP);
2771 print_result(D_CBC_RC5, testnum, count, d);
2772 }
2773 }
2774 #endif
2775 #ifndef OPENSSL_NO_BF
2776 if (doit[D_CBC_BF]) {
2777 if (async_jobs > 0) {
2778 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2779 names[D_CBC_BF]);
2780 doit[D_CBC_BF] = 0;
2781 }
2782 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2783 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2784 lengths[testnum], seconds.sym);
2785 Time_F(START);
2786 for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2787 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2788 (size_t)lengths[testnum], &bf_ks,
2789 iv, BF_ENCRYPT);
2790 d = Time_F(STOP);
2791 print_result(D_CBC_BF, testnum, count, d);
2792 }
2793 }
2794 #endif
2795 #ifndef OPENSSL_NO_CAST
2796 if (doit[D_CBC_CAST]) {
2797 if (async_jobs > 0) {
2798 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2799 names[D_CBC_CAST]);
2800 doit[D_CBC_CAST] = 0;
2801 }
2802 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2803 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2804 lengths[testnum], seconds.sym);
2805 Time_F(START);
2806 for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2807 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2808 (size_t)lengths[testnum], &cast_ks,
2809 iv, CAST_ENCRYPT);
2810 d = Time_F(STOP);
2811 print_result(D_CBC_CAST, testnum, count, d);
2812 }
2813 }
2814 #endif
2815 if (doit[D_RAND]) {
2816 for (testnum = 0; testnum < size_num; testnum++) {
2817 print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2818 seconds.sym);
2819 Time_F(START);
2820 count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2821 d = Time_F(STOP);
2822 print_result(D_RAND, testnum, count, d);
2823 }
2824 }
2825
2826 if (doit[D_EVP]) {
2827 if (evp_cipher != NULL) {
2828 int (*loopfunc)(void *args) = EVP_Update_loop;
2829
2830 if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
2831 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2832 multiblock_speed(evp_cipher, lengths_single, &seconds);
2833 ret = 0;
2834 goto end;
2835 }
2836
2837 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2838
2839 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
2840 loopfunc = EVP_Update_loop_ccm;
2841 } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
2842 EVP_CIPH_FLAG_AEAD_CIPHER)) {
2843 loopfunc = EVP_Update_loop_aead;
2844 if (lengths == lengths_list) {
2845 lengths = aead_lengths_list;
2846 size_num = OSSL_NELEM(aead_lengths_list);
2847 }
2848 }
2849
2850 for (testnum = 0; testnum < size_num; testnum++) {
2851 print_message(names[D_EVP], save_count, lengths[testnum],
2852 seconds.sym);
2853
2854 for (k = 0; k < loopargs_len; k++) {
2855 loopargs[k].ctx = EVP_CIPHER_CTX_new();
2856 if (loopargs[k].ctx == NULL) {
2857 BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
2858 exit(1);
2859 }
2860 if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2861 NULL, iv, decrypt ? 0 : 1)) {
2862 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2863 ERR_print_errors(bio_err);
2864 exit(1);
2865 }
2866
2867 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2868
2869 keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
2870 loopargs[k].key = app_malloc(keylen, "evp_cipher key");
2871 EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
2872 if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
2873 loopargs[k].key, NULL, -1)) {
2874 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2875 ERR_print_errors(bio_err);
2876 exit(1);
2877 }
2878 OPENSSL_clear_free(loopargs[k].key, keylen);
2879
2880 /* SIV mode only allows for a single Update operation */
2881 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
2882 EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_SET_SPEED, 1, NULL);
2883 }
2884
2885 Time_F(START);
2886 count = run_benchmark(async_jobs, loopfunc, loopargs);
2887 d = Time_F(STOP);
2888 for (k = 0; k < loopargs_len; k++) {
2889 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2890 }
2891 print_result(D_EVP, testnum, count, d);
2892 }
2893 } else if (evp_md != NULL) {
2894 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2895
2896 for (testnum = 0; testnum < size_num; testnum++) {
2897 print_message(names[D_EVP], save_count, lengths[testnum],
2898 seconds.sym);
2899 Time_F(START);
2900 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2901 d = Time_F(STOP);
2902 print_result(D_EVP, testnum, count, d);
2903 }
2904 }
2905 }
2906
2907 if (doit[D_EVP_HMAC]) {
2908 if (evp_hmac_md != NULL) {
2909 const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
2910 evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
2911 "HMAC name");
2912 sprintf(evp_hmac_name, "HMAC(%s)", md_name);
2913 names[D_EVP_HMAC] = evp_hmac_name;
2914
2915 for (testnum = 0; testnum < size_num; testnum++) {
2916 print_message(names[D_EVP_HMAC], save_count, lengths[testnum],
2917 seconds.sym);
2918 Time_F(START);
2919 count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
2920 d = Time_F(STOP);
2921 print_result(D_EVP_HMAC, testnum, count, d);
2922 }
2923 }
2924 }
2925
2926 #ifndef OPENSSL_NO_CMAC
2927 if (doit[D_EVP_CMAC]) {
2928 if (evp_cmac_cipher != NULL) {
2929 const char *cipher_name = OBJ_nid2ln(EVP_CIPHER_type(evp_cmac_cipher));
2930 evp_cmac_name = app_malloc(sizeof("CMAC()") + strlen(cipher_name),
2931 "CMAC name");
2932 sprintf(evp_cmac_name, "CMAC(%s)", cipher_name);
2933 names[D_EVP_CMAC] = evp_cmac_name;
2934
2935 for (i = 0; i < loopargs_len; i++) {
2936 loopargs[i].cmac_ctx = CMAC_CTX_new();
2937 if (loopargs[i].cmac_ctx == NULL) {
2938 BIO_printf(bio_err, "CMAC malloc failure, exiting...");
2939 exit(1);
2940 }
2941 }
2942 for (testnum = 0; testnum < size_num; testnum++) {
2943 print_message(names[D_EVP_CMAC], save_count, lengths[testnum],
2944 seconds.sym);
2945 Time_F(START);
2946 count = run_benchmark(async_jobs, EVP_CMAC_loop, loopargs);
2947 d = Time_F(STOP);
2948 print_result(D_EVP_CMAC, testnum, count, d);
2949 }
2950 for (i = 0; i < loopargs_len; i++)
2951 CMAC_CTX_free(loopargs[i].cmac_ctx);
2952 }
2953 }
2954 #endif
2955
2956 for (i = 0; i < loopargs_len; i++)
2957 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
2958 goto end;
2959
2960 #ifndef OPENSSL_NO_RSA
2961 for (testnum = 0; testnum < RSA_NUM; testnum++) {
2962 int st = 0;
2963 if (!rsa_doit[testnum])
2964 continue;
2965 for (i = 0; i < loopargs_len; i++) {
2966 if (primes > 2) {
2967 /* we haven't set keys yet, generate multi-prime RSA keys */
2968 BIGNUM *bn = BN_new();
2969
2970 if (bn == NULL)
2971 goto end;
2972 if (!BN_set_word(bn, RSA_F4)) {
2973 BN_free(bn);
2974 goto end;
2975 }
2976
2977 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
2978 rsa_choices[testnum].name);
2979
2980 loopargs[i].rsa_key[testnum] = RSA_new();
2981 if (loopargs[i].rsa_key[testnum] == NULL) {
2982 BN_free(bn);
2983 goto end;
2984 }
2985
2986 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
2987 rsa_bits[testnum],
2988 primes, bn, NULL)) {
2989 BN_free(bn);
2990 goto end;
2991 }
2992 BN_free(bn);
2993 }
2994 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2995 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2996 if (st == 0)
2997 break;
2998 }
2999 if (st == 0) {
3000 BIO_printf(bio_err,
3001 "RSA sign failure. No RSA sign will be done.\n");
3002 ERR_print_errors(bio_err);
3003 rsa_count = 1;
3004 } else {
3005 pkey_print_message("private", "rsa",
3006 rsa_c[testnum][0], rsa_bits[testnum],
3007 seconds.rsa);
3008 /* RSA_blinding_on(rsa_key[testnum],NULL); */
3009 Time_F(START);
3010 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
3011 d = Time_F(STOP);
3012 BIO_printf(bio_err,
3013 mr ? "+R1:%ld:%d:%.2f\n"
3014 : "%ld %u bits private RSA's in %.2fs\n",
3015 count, rsa_bits[testnum], d);
3016 rsa_results[testnum][0] = (double)count / d;
3017 rsa_count = count;
3018 }
3019
3020 for (i = 0; i < loopargs_len; i++) {
3021 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3022 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3023 if (st <= 0)
3024 break;
3025 }
3026 if (st <= 0) {
3027 BIO_printf(bio_err,
3028 "RSA verify failure. No RSA verify will be done.\n");
3029 ERR_print_errors(bio_err);
3030 rsa_doit[testnum] = 0;
3031 } else {
3032 pkey_print_message("public", "rsa",
3033 rsa_c[testnum][1], rsa_bits[testnum],
3034 seconds.rsa);
3035 Time_F(START);
3036 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
3037 d = Time_F(STOP);
3038 BIO_printf(bio_err,
3039 mr ? "+R2:%ld:%d:%.2f\n"
3040 : "%ld %u bits public RSA's in %.2fs\n",
3041 count, rsa_bits[testnum], d);
3042 rsa_results[testnum][1] = (double)count / d;
3043 }
3044
3045 if (rsa_count <= 1) {
3046 /* if longer than 10s, don't do any more */
3047 for (testnum++; testnum < RSA_NUM; testnum++)
3048 rsa_doit[testnum] = 0;
3049 }
3050 }
3051 #endif /* OPENSSL_NO_RSA */
3052
3053 for (i = 0; i < loopargs_len; i++)
3054 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3055 goto end;
3056
3057 #ifndef OPENSSL_NO_DSA
3058 for (testnum = 0; testnum < DSA_NUM; testnum++) {
3059 int st = 0;
3060 if (!dsa_doit[testnum])
3061 continue;
3062
3063 /* DSA_generate_key(dsa_key[testnum]); */
3064 /* DSA_sign_setup(dsa_key[testnum],NULL); */
3065 for (i = 0; i < loopargs_len; i++) {
3066 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3067 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3068 if (st == 0)
3069 break;
3070 }
3071 if (st == 0) {
3072 BIO_printf(bio_err,
3073 "DSA sign failure. No DSA sign will be done.\n");
3074 ERR_print_errors(bio_err);
3075 rsa_count = 1;
3076 } else {
3077 pkey_print_message("sign", "dsa",
3078 dsa_c[testnum][0], dsa_bits[testnum],
3079 seconds.dsa);
3080 Time_F(START);
3081 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
3082 d = Time_F(STOP);
3083 BIO_printf(bio_err,
3084 mr ? "+R3:%ld:%u:%.2f\n"
3085 : "%ld %u bits DSA signs in %.2fs\n",
3086 count, dsa_bits[testnum], d);
3087 dsa_results[testnum][0] = (double)count / d;
3088 rsa_count = count;
3089 }
3090
3091 for (i = 0; i < loopargs_len; i++) {
3092 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3093 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3094 if (st <= 0)
3095 break;
3096 }
3097 if (st <= 0) {
3098 BIO_printf(bio_err,
3099 "DSA verify failure. No DSA verify will be done.\n");
3100 ERR_print_errors(bio_err);
3101 dsa_doit[testnum] = 0;
3102 } else {
3103 pkey_print_message("verify", "dsa",
3104 dsa_c[testnum][1], dsa_bits[testnum],
3105 seconds.dsa);
3106 Time_F(START);
3107 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
3108 d = Time_F(STOP);
3109 BIO_printf(bio_err,
3110 mr ? "+R4:%ld:%u:%.2f\n"
3111 : "%ld %u bits DSA verify in %.2fs\n",
3112 count, dsa_bits[testnum], d);
3113 dsa_results[testnum][1] = (double)count / d;
3114 }
3115
3116 if (rsa_count <= 1) {
3117 /* if longer than 10s, don't do any more */
3118 for (testnum++; testnum < DSA_NUM; testnum++)
3119 dsa_doit[testnum] = 0;
3120 }
3121 }
3122 #endif /* OPENSSL_NO_DSA */
3123
3124 #ifndef OPENSSL_NO_EC
3125 for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
3126 int st = 1;
3127
3128 if (!ecdsa_doit[testnum])
3129 continue; /* Ignore Curve */
3130 for (i = 0; i < loopargs_len; i++) {
3131 loopargs[i].ecdsa[testnum] =
3132 EC_KEY_new_by_curve_name(test_curves[testnum].nid);
3133 if (loopargs[i].ecdsa[testnum] == NULL) {
3134 st = 0;
3135 break;
3136 }
3137 }
3138 if (st == 0) {
3139 BIO_printf(bio_err, "ECDSA failure.\n");
3140 ERR_print_errors(bio_err);
3141 rsa_count = 1;
3142 } else {
3143 for (i = 0; i < loopargs_len; i++) {
3144 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
3145 /* Perform ECDSA signature test */
3146 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
3147 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3148 &loopargs[i].siglen,
3149 loopargs[i].ecdsa[testnum]);
3150 if (st == 0)
3151 break;
3152 }
3153 if (st == 0) {
3154 BIO_printf(bio_err,
3155 "ECDSA sign failure. No ECDSA sign will be done.\n");
3156 ERR_print_errors(bio_err);
3157 rsa_count = 1;
3158 } else {
3159 pkey_print_message("sign", "ecdsa",
3160 ecdsa_c[testnum][0],
3161 test_curves[testnum].bits, seconds.ecdsa);
3162 Time_F(START);
3163 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
3164 d = Time_F(STOP);
3165
3166 BIO_printf(bio_err,
3167 mr ? "+R5:%ld:%u:%.2f\n" :
3168 "%ld %u bits ECDSA signs in %.2fs \n",
3169 count, test_curves[testnum].bits, d);
3170 ecdsa_results[testnum][0] = (double)count / d;
3171 rsa_count = count;
3172 }
3173
3174 /* Perform ECDSA verification test */
3175 for (i = 0; i < loopargs_len; i++) {
3176 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3177 loopargs[i].siglen,
3178 loopargs[i].ecdsa[testnum]);
3179 if (st != 1)
3180 break;
3181 }
3182 if (st != 1) {
3183 BIO_printf(bio_err,
3184 "ECDSA verify failure. No ECDSA verify will be done.\n");
3185 ERR_print_errors(bio_err);
3186 ecdsa_doit[testnum] = 0;
3187 } else {
3188 pkey_print_message("verify", "ecdsa",
3189 ecdsa_c[testnum][1],
3190 test_curves[testnum].bits, seconds.ecdsa);
3191 Time_F(START);
3192 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
3193 d = Time_F(STOP);
3194 BIO_printf(bio_err,
3195 mr ? "+R6:%ld:%u:%.2f\n"
3196 : "%ld %u bits ECDSA verify in %.2fs\n",
3197 count, test_curves[testnum].bits, d);
3198 ecdsa_results[testnum][1] = (double)count / d;
3199 }
3200
3201 if (rsa_count <= 1) {
3202 /* if longer than 10s, don't do any more */
3203 for (testnum++; testnum < ECDSA_NUM; testnum++)
3204 ecdsa_doit[testnum] = 0;
3205 }
3206 }
3207 }
3208
3209 for (testnum = 0; testnum < EC_NUM; testnum++) {
3210 int ecdh_checks = 1;
3211
3212 if (!ecdh_doit[testnum])
3213 continue;
3214
3215 for (i = 0; i < loopargs_len; i++) {
3216 EVP_PKEY_CTX *kctx = NULL;
3217 EVP_PKEY_CTX *test_ctx = NULL;
3218 EVP_PKEY_CTX *ctx = NULL;
3219 EVP_PKEY *key_A = NULL;
3220 EVP_PKEY *key_B = NULL;
3221 size_t outlen;
3222 size_t test_outlen;
3223
3224 /* Ensure that the error queue is empty */
3225 if (ERR_peek_error()) {
3226 BIO_printf(bio_err,
3227 "WARNING: the error queue contains previous unhandled errors.\n");
3228 ERR_print_errors(bio_err);
3229 }
3230
3231 /* Let's try to create a ctx directly from the NID: this works for
3232 * curves like Curve25519 that are not implemented through the low
3233 * level EC interface.
3234 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
3235 * then we set the curve by NID before deriving the actual keygen
3236 * ctx for that specific curve. */
3237 kctx = EVP_PKEY_CTX_new_id(test_curves[testnum].nid, NULL); /* keygen ctx from NID */
3238 if (!kctx) {
3239 EVP_PKEY_CTX *pctx = NULL;
3240 EVP_PKEY *params = NULL;
3241
3242 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
3243 * "int_ctx_new:unsupported algorithm" error was added to the
3244 * error queue.
3245 * We remove it from the error queue as we are handling it. */
3246 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
3247 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
3248 /* check that the error origin matches */
3249 ERR_GET_LIB(error) == ERR_LIB_EVP &&
3250 ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
3251 ERR_get_error(); /* pop error from queue */
3252 if (ERR_peek_error()) {
3253 BIO_printf(bio_err,
3254 "Unhandled error in the error queue during ECDH init.\n");
3255 ERR_print_errors(bio_err);
3256 rsa_count = 1;
3257 break;
3258 }
3259
3260 if ( /* Create the context for parameter generation */
3261 !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
3262 /* Initialise the parameter generation */
3263 !EVP_PKEY_paramgen_init(pctx) ||
3264 /* Set the curve by NID */
3265 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3266 test_curves
3267 [testnum].nid) ||
3268 /* Create the parameter object params */
3269 !EVP_PKEY_paramgen(pctx, &params)) {
3270 ecdh_checks = 0;
3271 BIO_printf(bio_err, "ECDH EC params init failure.\n");
3272 ERR_print_errors(bio_err);
3273 rsa_count = 1;
3274 break;
3275 }
3276 /* Create the context for the key generation */
3277 kctx = EVP_PKEY_CTX_new(params, NULL);
3278
3279 EVP_PKEY_free(params);
3280 params = NULL;
3281 EVP_PKEY_CTX_free(pctx);
3282 pctx = NULL;
3283 }
3284 if (kctx == NULL || /* keygen ctx is not null */
3285 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
3286 ecdh_checks = 0;
3287 BIO_printf(bio_err, "ECDH keygen failure.\n");
3288 ERR_print_errors(bio_err);
3289 rsa_count = 1;
3290 break;
3291 }
3292
3293 if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
3294 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
3295 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
3296 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
3297 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
3298 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
3299 outlen == 0 || /* ensure outlen is a valid size */
3300 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
3301 ecdh_checks = 0;
3302 BIO_printf(bio_err, "ECDH key generation failure.\n");
3303 ERR_print_errors(bio_err);
3304 rsa_count = 1;
3305 break;
3306 }
3307
3308 /* Here we perform a test run, comparing the output of a*B and b*A;
3309 * we try this here and assume that further EVP_PKEY_derive calls
3310 * never fail, so we can skip checks in the actually benchmarked
3311 * code, for maximum performance. */
3312 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
3313 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
3314 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
3315 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
3316 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
3317 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
3318 test_outlen != outlen /* compare output length */ ) {
3319 ecdh_checks = 0;
3320 BIO_printf(bio_err, "ECDH computation failure.\n");
3321 ERR_print_errors(bio_err);
3322 rsa_count = 1;
3323 break;
3324 }
3325
3326 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
3327 if (CRYPTO_memcmp(loopargs[i].secret_a,
3328 loopargs[i].secret_b, outlen)) {
3329 ecdh_checks = 0;
3330 BIO_printf(bio_err, "ECDH computations don't match.\n");
3331 ERR_print_errors(bio_err);
3332 rsa_count = 1;
3333 break;
3334 }
3335
3336 loopargs[i].ecdh_ctx[testnum] = ctx;
3337 loopargs[i].outlen[testnum] = outlen;
3338
3339 EVP_PKEY_free(key_A);
3340 EVP_PKEY_free(key_B);
3341 EVP_PKEY_CTX_free(kctx);
3342 kctx = NULL;
3343 EVP_PKEY_CTX_free(test_ctx);
3344 test_ctx = NULL;
3345 }
3346 if (ecdh_checks != 0) {
3347 pkey_print_message("", "ecdh",
3348 ecdh_c[testnum][0],
3349 test_curves[testnum].bits, seconds.ecdh);
3350 Time_F(START);
3351 count =
3352 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
3353 d = Time_F(STOP);
3354 BIO_printf(bio_err,
3355 mr ? "+R7:%ld:%d:%.2f\n" :
3356 "%ld %u-bits ECDH ops in %.2fs\n", count,
3357 test_curves[testnum].bits, d);
3358 ecdh_results[testnum][0] = (double)count / d;
3359 rsa_count = count;
3360 }
3361
3362 if (rsa_count <= 1) {
3363 /* if longer than 10s, don't do any more */
3364 for (testnum++; testnum < OSSL_NELEM(ecdh_doit); testnum++)
3365 ecdh_doit[testnum] = 0;
3366 }
3367 }
3368
3369 for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
3370 int st = 1;
3371 EVP_PKEY *ed_pkey = NULL;
3372 EVP_PKEY_CTX *ed_pctx = NULL;
3373
3374 if (!eddsa_doit[testnum])
3375 continue; /* Ignore Curve */
3376 for (i = 0; i < loopargs_len; i++) {
3377 loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
3378 if (loopargs[i].eddsa_ctx[testnum] == NULL) {
3379 st = 0;
3380 break;
3381 }
3382
3383 if ((ed_pctx = EVP_PKEY_CTX_new_id(test_ed_curves[testnum].nid, NULL))
3384 == NULL
3385 || EVP_PKEY_keygen_init(ed_pctx) <= 0
3386 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
3387 st = 0;
3388 EVP_PKEY_CTX_free(ed_pctx);
3389 break;
3390 }
3391 EVP_PKEY_CTX_free(ed_pctx);
3392
3393 if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
3394 NULL, ed_pkey)) {
3395 st = 0;
3396 EVP_PKEY_free(ed_pkey);
3397 break;
3398 }
3399 EVP_PKEY_free(ed_pkey);
3400 }
3401 if (st == 0) {
3402 BIO_printf(bio_err, "EdDSA failure.\n");
3403 ERR_print_errors(bio_err);
3404 rsa_count = 1;
3405 } else {
3406 for (i = 0; i < loopargs_len; i++) {
3407 /* Perform EdDSA signature test */
3408 loopargs[i].sigsize = test_ed_curves[testnum].sigsize;
3409 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
3410 loopargs[i].buf2, &loopargs[i].sigsize,
3411 loopargs[i].buf, 20);
3412 if (st == 0)
3413 break;
3414 }
3415 if (st == 0) {
3416 BIO_printf(bio_err,
3417 "EdDSA sign failure. No EdDSA sign will be done.\n");
3418 ERR_print_errors(bio_err);
3419 rsa_count = 1;
3420 } else {
3421 pkey_print_message("sign", test_ed_curves[testnum].name,
3422 eddsa_c[testnum][0],
3423 test_ed_curves[testnum].bits, seconds.eddsa);
3424 Time_F(START);
3425 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
3426 d = Time_F(STOP);
3427
3428 BIO_printf(bio_err,
3429 mr ? "+R8:%ld:%u:%s:%.2f\n" :
3430 "%ld %u bits %s signs in %.2fs \n",
3431 count, test_ed_curves[testnum].bits,
3432 test_ed_curves[testnum].name, d);
3433 eddsa_results[testnum][0] = (double)count / d;
3434 rsa_count = count;
3435 }
3436
3437 /* Perform EdDSA verification test */
3438 for (i = 0; i < loopargs_len; i++) {
3439 st = EVP_DigestVerify(loopargs[i].eddsa_ctx[testnum],
3440 loopargs[i].buf2, loopargs[i].sigsize,
3441 loopargs[i].buf, 20);
3442 if (st != 1)
3443 break;
3444 }
3445 if (st != 1) {
3446 BIO_printf(bio_err,
3447 "EdDSA verify failure. No EdDSA verify will be done.\n");
3448 ERR_print_errors(bio_err);
3449 eddsa_doit[testnum] = 0;
3450 } else {
3451 pkey_print_message("verify", test_ed_curves[testnum].name,
3452 eddsa_c[testnum][1],
3453 test_ed_curves[testnum].bits, seconds.eddsa);
3454 Time_F(START);
3455 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
3456 d = Time_F(STOP);
3457 BIO_printf(bio_err,
3458 mr ? "+R9:%ld:%u:%s:%.2f\n"
3459 : "%ld %u bits %s verify in %.2fs\n",
3460 count, test_ed_curves[testnum].bits,
3461 test_ed_curves[testnum].name, d);
3462 eddsa_results[testnum][1] = (double)count / d;
3463 }
3464
3465 if (rsa_count <= 1) {
3466 /* if longer than 10s, don't do any more */
3467 for (testnum++; testnum < EdDSA_NUM; testnum++)
3468 eddsa_doit[testnum] = 0;
3469 }
3470 }
3471 }
3472
3473 # ifndef OPENSSL_NO_SM2
3474 for (testnum = 0; testnum < SM2_NUM; testnum++) {
3475 int st = 1;
3476 EVP_PKEY *sm2_pkey = NULL;
3477 EVP_PKEY_CTX *pctx = NULL;
3478 EVP_PKEY_CTX *sm2_pctx = NULL;
3479 EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
3480 size_t sm2_sigsize = 0;
3481
3482 if (!sm2_doit[testnum])
3483 continue; /* Ignore Curve */
3484 /* Init signing and verification */
3485 for (i = 0; i < loopargs_len; i++) {
3486 loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
3487 if (loopargs[i].sm2_ctx[testnum] == NULL) {
3488 st = 0;
3489 break;
3490 }
3491 loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
3492 if (loopargs[i].sm2_vfy_ctx[testnum] == NULL) {
3493 st = 0;
3494 break;
3495 }
3496
3497 /* SM2 keys are generated as normal EC keys with a special curve */
3498 if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
3499 || EVP_PKEY_keygen_init(pctx) <= 0
3500 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3501 test_sm2_curves[testnum].nid) <= 0
3502 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0) {
3503 st = 0;
3504 EVP_PKEY_CTX_free(pctx);
3505 break;
3506 }
3507 /* free previous one and alloc a new one */
3508 EVP_PKEY_CTX_free(pctx);
3509
3510 loopargs[i].sigsize = sm2_sigsize
3511 = ECDSA_size(EVP_PKEY_get0_EC_KEY(sm2_pkey));
3512
3513 if (!EVP_PKEY_set_alias_type(sm2_pkey, EVP_PKEY_SM2)) {
3514 st = 0;
3515 EVP_PKEY_free(sm2_pkey);
3516 break;
3517 }
3518
3519 sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3520 if (sm2_pctx == NULL) {
3521 st = 0;
3522 EVP_PKEY_free(sm2_pkey);
3523 break;
3524 }
3525 sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3526 if (sm2_vfy_pctx == NULL) {
3527 st = 0;
3528 EVP_PKEY_CTX_free(sm2_pctx);
3529 EVP_PKEY_free(sm2_pkey);
3530 break;
3531 }
3532 /*
3533 * No need to allow user to set an explicit ID here, just use
3534 * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
3535 */
3536 if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1) {
3537 st = 0;
3538 EVP_PKEY_CTX_free(sm2_pctx);
3539 EVP_PKEY_CTX_free(sm2_vfy_pctx);
3540 EVP_PKEY_free(sm2_pkey);
3541 break;
3542 }
3543
3544 if (EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1) {
3545 st = 0;
3546 EVP_PKEY_CTX_free(sm2_pctx);
3547 EVP_PKEY_CTX_free(sm2_vfy_pctx);
3548 EVP_PKEY_free(sm2_pkey);
3549 break;
3550 }
3551
3552 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
3553 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
3554
3555 if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
3556 EVP_sm3(), NULL, sm2_pkey)) {
3557 st = 0;
3558 EVP_PKEY_free(sm2_pkey);
3559 break;
3560 }
3561 if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
3562 EVP_sm3(), NULL, sm2_pkey)) {
3563 st = 0;
3564 EVP_PKEY_free(sm2_pkey);
3565 break;
3566 }
3567 loopargs[i].sm2_pkey[testnum] = sm2_pkey;
3568 }
3569 if (st == 0) {
3570 BIO_printf(bio_err, "SM2 failure.\n");
3571 ERR_print_errors(bio_err);
3572 rsa_count = 1;
3573 } else {
3574 for (i = 0; i < loopargs_len; i++) {
3575 sm2_sigsize = loopargs[i].sigsize;
3576 /* Perform SM2 signature test */
3577 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
3578 loopargs[i].buf2, &sm2_sigsize,
3579 loopargs[i].buf, 20);
3580 if (st == 0)
3581 break;
3582 }
3583 if (st == 0) {
3584 BIO_printf(bio_err,
3585 "SM2 sign failure. No SM2 sign will be done.\n");
3586 ERR_print_errors(bio_err);
3587 rsa_count = 1;
3588 } else {
3589 pkey_print_message("sign", test_sm2_curves[testnum].name,
3590 sm2_c[testnum][0],
3591 test_sm2_curves[testnum].bits, seconds.sm2);
3592 Time_F(START);
3593 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
3594 d = Time_F(STOP);
3595
3596 BIO_printf(bio_err,
3597 mr ? "+R8:%ld:%u:%s:%.2f\n" :
3598 "%ld %u bits %s signs in %.2fs \n",
3599 count, test_sm2_curves[testnum].bits,
3600 test_sm2_curves[testnum].name, d);
3601 sm2_results[testnum][0] = (double)count / d;
3602 rsa_count = count;
3603 }
3604
3605 /* Perform SM2 verification test */
3606 for (i = 0; i < loopargs_len; i++) {
3607 st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
3608 loopargs[i].buf2, loopargs[i].sigsize,
3609 loopargs[i].buf, 20);
3610 if (st != 1)
3611 break;
3612 }
3613 if (st != 1) {
3614 BIO_printf(bio_err,
3615 "SM2 verify failure. No SM2 verify will be done.\n");
3616 ERR_print_errors(bio_err);
3617 sm2_doit[testnum] = 0;
3618 } else {
3619 pkey_print_message("verify", test_sm2_curves[testnum].name,
3620 sm2_c[testnum][1],
3621 test_sm2_curves[testnum].bits, seconds.sm2);
3622 Time_F(START);
3623 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
3624 d = Time_F(STOP);
3625 BIO_printf(bio_err,
3626 mr ? "+R9:%ld:%u:%s:%.2f\n"
3627 : "%ld %u bits %s verify in %.2fs\n",
3628 count, test_sm2_curves[testnum].bits,
3629 test_sm2_curves[testnum].name, d);
3630 sm2_results[testnum][1] = (double)count / d;
3631 }
3632
3633 if (rsa_count <= 1) {
3634 /* if longer than 10s, don't do any more */
3635 for (testnum++; testnum < SM2_NUM; testnum++)
3636 sm2_doit[testnum] = 0;
3637 }
3638 }
3639 }
3640 # endif /* OPENSSL_NO_SM2 */
3641
3642 #endif /* OPENSSL_NO_EC */
3643 #ifndef NO_FORK
3644 show_res:
3645 #endif
3646 if (!mr) {
3647 printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
3648 printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
3649 printf("options:");
3650 printf("%s ", BN_options());
3651 #ifndef OPENSSL_NO_MD2
3652 printf("%s ", MD2_options());
3653 #endif
3654 #ifndef OPENSSL_NO_RC4
3655 printf("%s ", RC4_options());
3656 #endif
3657 #ifndef OPENSSL_NO_DES
3658 printf("%s ", DES_options());
3659 #endif
3660 printf("%s ", AES_options());
3661 #ifndef OPENSSL_NO_IDEA
3662 printf("%s ", IDEA_options());
3663 #endif
3664 #ifndef OPENSSL_NO_BF
3665 printf("%s ", BF_options());
3666 #endif
3667 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
3668 printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
3669 }
3670
3671 if (pr_header) {
3672 if (mr)
3673 printf("+H");
3674 else {
3675 printf
3676 ("The 'numbers' are in 1000s of bytes per second processed.\n");
3677 printf("type ");
3678 }
3679 for (testnum = 0; testnum < size_num; testnum++)
3680 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
3681 printf("\n");
3682 }
3683
3684 for (k = 0; k < ALGOR_NUM; k++) {
3685 if (!doit[k])
3686 continue;
3687 if (mr)
3688 printf("+F:%u:%s", k, names[k]);
3689 else
3690 printf("%-13s", names[k]);
3691 for (testnum = 0; testnum < size_num; testnum++) {
3692 if (results[k][testnum] > 10000 && !mr)
3693 printf(" %11.2fk", results[k][testnum] / 1e3);
3694 else
3695 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
3696 }
3697 printf("\n");
3698 }
3699 #ifndef OPENSSL_NO_RSA
3700 testnum = 1;
3701 for (k = 0; k < RSA_NUM; k++) {
3702 if (!rsa_doit[k])
3703 continue;
3704 if (testnum && !mr) {
3705 printf("%18ssign verify sign/s verify/s\n", " ");
3706 testnum = 0;
3707 }
3708 if (mr)
3709 printf("+F2:%u:%u:%f:%f\n",
3710 k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
3711 else
3712 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3713 rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
3714 rsa_results[k][0], rsa_results[k][1]);
3715 }
3716 #endif
3717 #ifndef OPENSSL_NO_DSA
3718 testnum = 1;
3719 for (k = 0; k < DSA_NUM; k++) {
3720 if (!dsa_doit[k])
3721 continue;
3722 if (testnum && !mr) {
3723 printf("%18ssign verify sign/s verify/s\n", " ");
3724 testnum = 0;
3725 }
3726 if (mr)
3727 printf("+F3:%u:%u:%f:%f\n",
3728 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
3729 else
3730 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3731 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
3732 dsa_results[k][0], dsa_results[k][1]);
3733 }
3734 #endif
3735 #ifndef OPENSSL_NO_EC
3736 testnum = 1;
3737 for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
3738 if (!ecdsa_doit[k])
3739 continue;
3740 if (testnum && !mr) {
3741 printf("%30ssign verify sign/s verify/s\n", " ");
3742 testnum = 0;
3743 }
3744
3745 if (mr)
3746 printf("+F4:%u:%u:%f:%f\n",
3747 k, test_curves[k].bits,
3748 ecdsa_results[k][0], ecdsa_results[k][1]);
3749 else
3750 printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3751 test_curves[k].bits, test_curves[k].name,
3752 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
3753 ecdsa_results[k][0], ecdsa_results[k][1]);
3754 }
3755
3756 testnum = 1;
3757 for (k = 0; k < EC_NUM; k++) {
3758 if (!ecdh_doit[k])
3759 continue;
3760 if (testnum && !mr) {
3761 printf("%30sop op/s\n", " ");
3762 testnum = 0;
3763 }
3764 if (mr)
3765 printf("+F5:%u:%u:%f:%f\n",
3766 k, test_curves[k].bits,
3767 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3768
3769 else
3770 printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
3771 test_curves[k].bits, test_curves[k].name,
3772 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3773 }
3774
3775 testnum = 1;
3776 for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
3777 if (!eddsa_doit[k])
3778 continue;
3779 if (testnum && !mr) {
3780 printf("%30ssign verify sign/s verify/s\n", " ");
3781 testnum = 0;
3782 }
3783
3784 if (mr)
3785 printf("+F6:%u:%u:%s:%f:%f\n",
3786 k, test_ed_curves[k].bits, test_ed_curves[k].name,
3787 eddsa_results[k][0], eddsa_results[k][1]);
3788 else
3789 printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3790 test_ed_curves[k].bits, test_ed_curves[k].name,
3791 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
3792 eddsa_results[k][0], eddsa_results[k][1]);
3793 }
3794
3795 # ifndef OPENSSL_NO_SM2
3796 testnum = 1;
3797 for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
3798 if (!sm2_doit[k])
3799 continue;
3800 if (testnum && !mr) {
3801 printf("%30ssign verify sign/s verify/s\n", " ");
3802 testnum = 0;
3803 }
3804
3805 if (mr)
3806 printf("+F6:%u:%u:%s:%f:%f\n",
3807 k, test_sm2_curves[k].bits, test_sm2_curves[k].name,
3808 sm2_results[k][0], sm2_results[k][1]);
3809 else
3810 printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3811 test_sm2_curves[k].bits, test_sm2_curves[k].name,
3812 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
3813 sm2_results[k][0], sm2_results[k][1]);
3814 }
3815 # endif
3816 #endif
3817
3818 ret = 0;
3819
3820 end:
3821 ERR_print_errors(bio_err);
3822 for (i = 0; i < loopargs_len; i++) {
3823 OPENSSL_free(loopargs[i].buf_malloc);
3824 OPENSSL_free(loopargs[i].buf2_malloc);
3825
3826 #ifndef OPENSSL_NO_RSA
3827 for (k = 0; k < RSA_NUM; k++)
3828 RSA_free(loopargs[i].rsa_key[k]);
3829 #endif
3830 #ifndef OPENSSL_NO_DSA
3831 for (k = 0; k < DSA_NUM; k++)
3832 DSA_free(loopargs[i].dsa_key[k]);
3833 #endif
3834 #ifndef OPENSSL_NO_EC
3835 for (k = 0; k < ECDSA_NUM; k++)
3836 EC_KEY_free(loopargs[i].ecdsa[k]);
3837 for (k = 0; k < EC_NUM; k++)
3838 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
3839 for (k = 0; k < EdDSA_NUM; k++)
3840 EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
3841 # ifndef OPENSSL_NO_SM2
3842 for (k = 0; k < SM2_NUM; k++) {
3843 EVP_PKEY_CTX *pctx = NULL;
3844
3845 /* free signing ctx */
3846 if (loopargs[i].sm2_ctx[k] != NULL
3847 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
3848 EVP_PKEY_CTX_free(pctx);
3849 EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
3850 /* free verification ctx */
3851 if (loopargs[i].sm2_vfy_ctx[k] != NULL
3852 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
3853 EVP_PKEY_CTX_free(pctx);
3854 EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
3855 /* free pkey */
3856 EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
3857 }
3858 # endif
3859 OPENSSL_free(loopargs[i].secret_a);
3860 OPENSSL_free(loopargs[i].secret_b);
3861 #endif
3862 }
3863 OPENSSL_free(evp_hmac_name);
3864 #ifndef OPENSSL_NO_CMAC
3865 OPENSSL_free(evp_cmac_name);
3866 #endif
3867
3868 if (async_jobs > 0) {
3869 for (i = 0; i < loopargs_len; i++)
3870 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
3871 }
3872
3873 if (async_init) {
3874 ASYNC_cleanup_thread();
3875 }
3876 OPENSSL_free(loopargs);
3877 release_engine(e);
3878 return ret;
3879 }
3880
3881 static void print_message(const char *s, long num, int length, int tm)
3882 {
3883 #ifdef SIGALRM
3884 BIO_printf(bio_err,
3885 mr ? "+DT:%s:%d:%d\n"
3886 : "Doing %s for %ds on %d size blocks: ", s, tm, length);
3887 (void)BIO_flush(bio_err);
3888 alarm(tm);
3889 #else
3890 BIO_printf(bio_err,
3891 mr ? "+DN:%s:%ld:%d\n"
3892 : "Doing %s %ld times on %d size blocks: ", s, num, length);
3893 (void)BIO_flush(bio_err);
3894 #endif
3895 }
3896
3897 static void pkey_print_message(const char *str, const char *str2, long num,
3898 unsigned int bits, int tm)
3899 {
3900 #ifdef SIGALRM
3901 BIO_printf(bio_err,
3902 mr ? "+DTP:%d:%s:%s:%d\n"
3903 : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
3904 (void)BIO_flush(bio_err);
3905 alarm(tm);
3906 #else
3907 BIO_printf(bio_err,
3908 mr ? "+DNP:%ld:%d:%s:%s\n"
3909 : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
3910 (void)BIO_flush(bio_err);
3911 #endif
3912 }
3913
3914 static void print_result(int alg, int run_no, int count, double time_used)
3915 {
3916 if (count == -1) {
3917 BIO_puts(bio_err, "EVP error!\n");
3918 exit(1);
3919 }
3920 BIO_printf(bio_err,
3921 mr ? "+R:%d:%s:%f\n"
3922 : "%d %s's in %.2fs\n", count, names[alg], time_used);
3923 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
3924 }
3925
3926 #ifndef NO_FORK
3927 static char *sstrsep(char **string, const char *delim)
3928 {
3929 char isdelim[256];
3930 char *token = *string;
3931
3932 if (**string == 0)
3933 return NULL;
3934
3935 memset(isdelim, 0, sizeof(isdelim));
3936 isdelim[0] = 1;
3937
3938 while (*delim) {
3939 isdelim[(unsigned char)(*delim)] = 1;
3940 delim++;
3941 }
3942
3943 while (!isdelim[(unsigned char)(**string)]) {
3944 (*string)++;
3945 }
3946
3947 if (**string) {
3948 **string = 0;
3949 (*string)++;
3950 }
3951
3952 return token;
3953 }
3954
3955 static int do_multi(int multi, int size_num)
3956 {
3957 int n;
3958 int fd[2];
3959 int *fds;
3960 static char sep[] = ":";
3961
3962 fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
3963 for (n = 0; n < multi; ++n) {
3964 if (pipe(fd) == -1) {
3965 BIO_printf(bio_err, "pipe failure\n");
3966 exit(1);
3967 }
3968 fflush(stdout);
3969 (void)BIO_flush(bio_err);
3970 if (fork()) {
3971 close(fd[1]);
3972 fds[n] = fd[0];
3973 } else {
3974 close(fd[0]);
3975 close(1);
3976 if (dup(fd[1]) == -1) {
3977 BIO_printf(bio_err, "dup failed\n");
3978 exit(1);
3979 }
3980 close(fd[1]);
3981 mr = 1;
3982 usertime = 0;
3983 OPENSSL_free(fds);
3984 return 0;
3985 }
3986 printf("Forked child %d\n", n);
3987 }
3988
3989 /* for now, assume the pipe is long enough to take all the output */
3990 for (n = 0; n < multi; ++n) {
3991 FILE *f;
3992 char buf[1024];
3993 char *p;
3994
3995 f = fdopen(fds[n], "r");
3996 while (fgets(buf, sizeof(buf), f)) {
3997 p = strchr(buf, '\n');
3998 if (p)
3999 *p = '\0';
4000 if (buf[0] != '+') {
4001 BIO_printf(bio_err,
4002 "Don't understand line '%s' from child %d\n", buf,
4003 n);
4004 continue;
4005 }
4006 printf("Got: %s from %d\n", buf, n);
4007 if (strncmp(buf, "+F:", 3) == 0) {
4008 int alg;
4009 int j;
4010
4011 p = buf + 3;
4012 alg = atoi(sstrsep(&p, sep));
4013 sstrsep(&p, sep);
4014 for (j = 0; j < size_num; ++j)
4015 results[alg][j] += atof(sstrsep(&p, sep));
4016 } else if (strncmp(buf, "+F2:", 4) == 0) {
4017 int k;
4018 double d;
4019
4020 p = buf + 4;
4021 k = atoi(sstrsep(&p, sep));
4022 sstrsep(&p, sep);
4023
4024 d = atof(sstrsep(&p, sep));
4025 rsa_results[k][0] += d;
4026
4027 d = atof(sstrsep(&p, sep));
4028 rsa_results[k][1] += d;
4029 }
4030 # ifndef OPENSSL_NO_DSA
4031 else if (strncmp(buf, "+F3:", 4) == 0) {
4032 int k;
4033 double d;
4034
4035 p = buf + 4;
4036 k = atoi(sstrsep(&p, sep));
4037 sstrsep(&p, sep);
4038
4039 d = atof(sstrsep(&p, sep));
4040 dsa_results[k][0] += d;
4041
4042 d = atof(sstrsep(&p, sep));
4043 dsa_results[k][1] += d;
4044 }
4045 # endif
4046 # ifndef OPENSSL_NO_EC
4047 else if (strncmp(buf, "+F4:", 4) == 0) {
4048 int k;
4049 double d;
4050
4051 p = buf + 4;
4052 k = atoi(sstrsep(&p, sep));
4053 sstrsep(&p, sep);
4054
4055 d = atof(sstrsep(&p, sep));
4056 ecdsa_results[k][0] += d;
4057
4058 d = atof(sstrsep(&p, sep));
4059 ecdsa_results[k][1] += d;
4060 } else if (strncmp(buf, "+F5:", 4) == 0) {
4061 int k;
4062 double d;
4063
4064 p = buf + 4;
4065 k = atoi(sstrsep(&p, sep));
4066 sstrsep(&p, sep);
4067
4068 d = atof(sstrsep(&p, sep));
4069 ecdh_results[k][0] += d;
4070 } else if (strncmp(buf, "+F6:", 4) == 0) {
4071 int k;
4072 double d;
4073
4074 p = buf + 4;
4075 k = atoi(sstrsep(&p, sep));
4076 sstrsep(&p, sep);
4077
4078 d = atof(sstrsep(&p, sep));
4079 eddsa_results[k][0] += d;
4080
4081 d = atof(sstrsep(&p, sep));
4082 eddsa_results[k][1] += d;
4083 }
4084 # ifndef OPENSSL_NO_SM2
4085 else if (strncmp(buf, "+F7:", 4) == 0) {
4086 int k;
4087 double d;
4088
4089 p = buf + 4;
4090 k = atoi(sstrsep(&p, sep));
4091 sstrsep(&p, sep);
4092
4093 d = atof(sstrsep(&p, sep));
4094 sm2_results[k][0] += d;
4095
4096 d = atof(sstrsep(&p, sep));
4097 sm2_results[k][1] += d;
4098 }
4099 # endif /* OPENSSL_NO_SM2 */
4100 # endif
4101
4102 else if (strncmp(buf, "+H:", 3) == 0) {
4103 ;
4104 } else
4105 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
4106 n);
4107 }
4108
4109 fclose(f);
4110 }
4111 OPENSSL_free(fds);
4112 return 1;
4113 }
4114 #endif
4115
4116 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
4117 const openssl_speed_sec_t *seconds)
4118 {
4119 static const int mblengths_list[] =
4120 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
4121 const int *mblengths = mblengths_list;
4122 int j, count, keylen, num = OSSL_NELEM(mblengths_list);
4123 const char *alg_name;
4124 unsigned char *inp, *out, *key, no_key[32], no_iv[16];
4125 EVP_CIPHER_CTX *ctx;
4126 double d = 0.0;
4127
4128 if (lengths_single) {
4129 mblengths = &lengths_single;
4130 num = 1;
4131 }
4132
4133 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
4134 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
4135 ctx = EVP_CIPHER_CTX_new();
4136 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
4137
4138 keylen = EVP_CIPHER_CTX_key_length(ctx);
4139 key = app_malloc(keylen, "evp_cipher key");
4140 EVP_CIPHER_CTX_rand_key(ctx, key);
4141 EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
4142 OPENSSL_clear_free(key, keylen);
4143
4144 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
4145 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
4146
4147 for (j = 0; j < num; j++) {
4148 print_message(alg_name, 0, mblengths[j], seconds->sym);
4149 Time_F(START);
4150 for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
4151 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
4152 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
4153 size_t len = mblengths[j];
4154 int packlen;
4155
4156 memset(aad, 0, 8); /* avoid uninitialized values */
4157 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
4158 aad[9] = 3; /* version */
4159 aad[10] = 2;
4160 aad[11] = 0; /* length */
4161 aad[12] = 0;
4162 mb_param.out = NULL;
4163 mb_param.inp = aad;
4164 mb_param.len = len;
4165 mb_param.interleave = 8;
4166
4167 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
4168 sizeof(mb_param), &mb_param);
4169
4170 if (packlen > 0) {
4171 mb_param.out = out;
4172 mb_param.inp = inp;
4173 mb_param.len = len;
4174 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
4175 sizeof(mb_param), &mb_param);
4176 } else {
4177 int pad;
4178
4179 RAND_bytes(out, 16);
4180 len += 16;
4181 aad[11] = (unsigned char)(len >> 8);
4182 aad[12] = (unsigned char)(len);
4183 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
4184 EVP_AEAD_TLS1_AAD_LEN, aad);
4185 EVP_Cipher(ctx, out, inp, len + pad);
4186 }
4187 }
4188 d = Time_F(STOP);
4189 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
4190 : "%d %s's in %.2fs\n", count, "evp", d);
4191 results[D_EVP][j] = ((double)count) / d * mblengths[j];
4192 }
4193
4194 if (mr) {
4195 fprintf(stdout, "+H");
4196 for (j = 0; j < num; j++)
4197 fprintf(stdout, ":%d", mblengths[j]);
4198 fprintf(stdout, "\n");
4199 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
4200 for (j = 0; j < num; j++)
4201 fprintf(stdout, ":%.2f", results[D_EVP][j]);
4202 fprintf(stdout, "\n");
4203 } else {
4204 fprintf(stdout,
4205 "The 'numbers' are in 1000s of bytes per second processed.\n");
4206 fprintf(stdout, "type ");
4207 for (j = 0; j < num; j++)
4208 fprintf(stdout, "%7d bytes", mblengths[j]);
4209 fprintf(stdout, "\n");
4210 fprintf(stdout, "%-24s", alg_name);
4211
4212 for (j = 0; j < num; j++) {
4213 if (results[D_EVP][j] > 10000)
4214 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
4215 else
4216 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
4217 }
4218 fprintf(stdout, "\n");
4219 }
4220
4221 OPENSSL_free(inp);
4222 OPENSSL_free(out);
4223 EVP_CIPHER_CTX_free(ctx);
4224 }