2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
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 #define FFDH_SECONDS 10
21 /* We need to use some deprecated APIs */
22 #define OPENSSL_SUPPRESS_DEPRECATED
30 #include <openssl/crypto.h>
31 #include <openssl/rand.h>
32 #include <openssl/err.h>
33 #include <openssl/evp.h>
34 #include <openssl/objects.h>
35 #include <openssl/async.h>
36 #if !defined(OPENSSL_SYS_MSDOS)
41 # if defined(OPENSSL_TANDEM_FLOSS)
42 # include <floss.h(floss_fork)>
50 #include <openssl/bn.h>
51 #ifndef OPENSSL_NO_DES
52 # include <openssl/des.h>
54 #ifndef OPENSSL_NO_DEPRECATED_3_0
55 #include <openssl/aes.h>
57 #ifndef OPENSSL_NO_CAMELLIA
58 # include <openssl/camellia.h>
60 #ifndef OPENSSL_NO_MD2
61 # include <openssl/md2.h>
63 #ifndef OPENSSL_NO_MDC2
64 # include <openssl/mdc2.h>
66 #ifndef OPENSSL_NO_MD4
67 # include <openssl/md4.h>
69 #ifndef OPENSSL_NO_MD5
70 # include <openssl/md5.h>
72 #include <openssl/hmac.h>
73 #ifndef OPENSSL_NO_CMAC
74 #include <openssl/cmac.h>
76 #include <openssl/sha.h>
77 #ifndef OPENSSL_NO_RMD160
78 # include <openssl/ripemd.h>
80 #ifndef OPENSSL_NO_WHIRLPOOL
81 # include <openssl/whrlpool.h>
83 #ifndef OPENSSL_NO_RC4
84 # include <openssl/rc4.h>
86 #ifndef OPENSSL_NO_RC5
87 # include <openssl/rc5.h>
89 #ifndef OPENSSL_NO_RC2
90 # include <openssl/rc2.h>
92 #ifndef OPENSSL_NO_IDEA
93 # include <openssl/idea.h>
95 #ifndef OPENSSL_NO_SEED
96 # include <openssl/seed.h>
99 # include <openssl/blowfish.h>
101 #ifndef OPENSSL_NO_CAST
102 # include <openssl/cast.h>
104 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
105 # include <openssl/rsa.h>
106 # include "./testrsa.h"
108 #ifndef OPENSSL_NO_DH
109 # include <openssl/dh.h>
111 #include <openssl/x509.h>
112 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
113 # include <openssl/dsa.h>
114 # include "./testdsa.h"
116 #ifndef OPENSSL_NO_EC
117 # include <openssl/ec.h>
119 #include <openssl/modes.h>
122 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
135 #define MAX_MISALIGNMENT 63
136 #define MAX_ECDH_SIZE 256
138 #define MAX_FFDH_SIZE 1024
140 typedef struct openssl_speed_sec_st {
149 } openssl_speed_sec_t;
151 static volatile int run = 0;
153 static int mr = 0; /* machine-readeable output format to merge fork results */
154 static int usertime = 1;
156 static double Time_F(int s);
157 static void print_message(const char *s, long num, int length, int tm);
158 #if (!defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)) \
159 || (!defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)) \
160 || !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
161 static void pkey_print_message(const char *str, const char *str2,
162 long num, unsigned int bits, int sec);
164 static void print_result(int alg, int run_no, int count, double time_used);
166 static int do_multi(int multi, int size_num);
169 static const int lengths_list[] = {
170 16, 64, 256, 1024, 8 * 1024, 16 * 1024
172 #define SIZE_NUM OSSL_NELEM(lengths_list)
173 static const int *lengths = lengths_list;
175 static const int aead_lengths_list[] = {
176 2, 31, 136, 1024, 8 * 1024, 16 * 1024
184 static void alarmed(int sig)
186 signal(SIGALRM, alarmed);
190 static double Time_F(int s)
192 double ret = app_tminterval(s, usertime);
198 #elif defined(_WIN32)
202 static unsigned int lapse;
203 static volatile unsigned int schlock;
204 static void alarm_win32(unsigned int secs)
209 # define alarm alarm_win32
211 static DWORD WINAPI sleepy(VOID * arg)
219 static double Time_F(int s)
226 thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
228 DWORD err = GetLastError();
229 BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
233 Sleep(0); /* scheduler spinlock */
234 ret = app_tminterval(s, usertime);
236 ret = app_tminterval(s, usertime);
238 TerminateThread(thr, 0);
245 static double Time_F(int s)
247 return app_tminterval(s, usertime);
251 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
252 const openssl_speed_sec_t *seconds);
254 static int opt_found(const char *name, unsigned int *result,
255 const OPT_PAIR pairs[], unsigned int nbelem)
259 for (idx = 0; idx < nbelem; ++idx, pairs++)
260 if (strcmp(name, pairs->name) == 0) {
261 *result = pairs->retval;
266 #define opt_found(value, pairs, result)\
267 opt_found(value, result, pairs, OSSL_NELEM(pairs))
269 typedef enum OPTION_choice {
270 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
271 OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
272 OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM, OPT_PROV_ENUM,
273 OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC
276 const OPTIONS speed_options[] = {
277 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [algorithm...]\n"},
279 OPT_SECTION("General"),
280 {"help", OPT_HELP, '-', "Display this summary"},
282 "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
283 {"mr", OPT_MR, '-', "Produce machine readable output"},
285 {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
287 #ifndef OPENSSL_NO_ASYNC
288 {"async_jobs", OPT_ASYNCJOBS, 'p',
289 "Enable async mode and start specified number of jobs"},
291 #ifndef OPENSSL_NO_ENGINE
292 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
294 {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
296 OPT_SECTION("Selection"),
297 {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
298 #ifndef OPENSSL_NO_DEPRECATED_3_0
299 {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
301 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
302 {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
304 {"decrypt", OPT_DECRYPT, '-',
305 "Time decryption instead of encryption (only EVP)"},
306 {"aead", OPT_AEAD, '-',
307 "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
309 OPT_SECTION("Timing"),
310 {"elapsed", OPT_ELAPSED, '-',
311 "Use wall-clock time instead of CPU user time as divisor"},
312 {"seconds", OPT_SECONDS, 'p',
313 "Run benchmarks for specified amount of seconds"},
314 {"bytes", OPT_BYTES, 'p',
315 "Run [non-PKI] benchmarks on custom-sized buffer"},
316 {"misalign", OPT_MISALIGN, 'p',
317 "Use specified offset to mis-align buffers"},
323 {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
328 D_MD2, D_MDC2, D_MD4, D_MD5 , D_HMAC, D_SHA1, D_RMD160, D_RC4,
329 D_CBC_DES, D_EDE3_DES, D_CBC_IDEA, D_CBC_SEED,
330 D_CBC_RC2, D_CBC_RC5, D_CBC_BF, D_CBC_CAST,
331 D_CBC_128_AES, D_CBC_192_AES, D_CBC_256_AES,
332 D_CBC_128_CML, D_CBC_192_CML, D_CBC_256_CML,
333 D_EVP, D_SHA256, D_SHA512, D_WHIRLPOOL,
334 D_IGE_128_AES, D_IGE_192_AES, D_IGE_256_AES,
335 D_GHASH, D_RAND, D_EVP_HMAC, D_EVP_CMAC, ALGOR_NUM
337 /* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
338 static const char *names[ALGOR_NUM] = {
339 "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
340 "des cbc", "des ede3", "idea cbc", "seed cbc",
341 "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
342 "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
343 "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
344 "evp", "sha256", "sha512", "whirlpool",
345 "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
346 "rand", "hmac", "cmac"
349 /* list of configured algorithm (remaining), with some few alias */
350 static const OPT_PAIR doit_choices[] = {
351 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
354 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
357 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
360 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
362 # ifndef OPENSSL_NO_DEPRECATED_3_0
366 #ifndef OPENSSL_NO_DEPRECATED_3_0
368 {"sha256", D_SHA256},
369 {"sha512", D_SHA512},
371 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
372 {"whirlpool", D_WHIRLPOOL},
374 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
375 {"ripemd", D_RMD160},
376 {"rmd160", D_RMD160},
377 {"ripemd160", D_RMD160},
379 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
382 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
383 {"des-cbc", D_CBC_DES},
384 {"des-ede3", D_EDE3_DES},
386 #ifndef OPENSSL_NO_DEPRECATED_3_0
387 {"aes-128-cbc", D_CBC_128_AES},
388 {"aes-192-cbc", D_CBC_192_AES},
389 {"aes-256-cbc", D_CBC_256_AES},
390 {"aes-128-ige", D_IGE_128_AES},
391 {"aes-192-ige", D_IGE_192_AES},
392 {"aes-256-ige", D_IGE_256_AES},
394 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
395 {"rc2-cbc", D_CBC_RC2},
398 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
399 {"rc5-cbc", D_CBC_RC5},
402 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
403 {"idea-cbc", D_CBC_IDEA},
404 {"idea", D_CBC_IDEA},
406 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
407 {"seed-cbc", D_CBC_SEED},
408 {"seed", D_CBC_SEED},
410 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
411 {"bf-cbc", D_CBC_BF},
412 {"blowfish", D_CBC_BF},
415 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
416 {"cast-cbc", D_CBC_CAST},
417 {"cast", D_CBC_CAST},
418 {"cast5", D_CBC_CAST},
424 static double results[ALGOR_NUM][SIZE_NUM];
426 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
427 enum { R_DSA_512, R_DSA_1024, R_DSA_2048, DSA_NUM };
428 static const OPT_PAIR dsa_choices[DSA_NUM] = {
429 {"dsa512", R_DSA_512},
430 {"dsa1024", R_DSA_1024},
431 {"dsa2048", R_DSA_2048}
433 static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
434 #endif /* OPENSSL_NO_DSA */
436 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
438 R_RSA_512, R_RSA_1024, R_RSA_2048, R_RSA_3072, R_RSA_4096, R_RSA_7680,
441 static const OPT_PAIR rsa_choices[RSA_NUM] = {
442 {"rsa512", R_RSA_512},
443 {"rsa1024", R_RSA_1024},
444 {"rsa2048", R_RSA_2048},
445 {"rsa3072", R_RSA_3072},
446 {"rsa4096", R_RSA_4096},
447 {"rsa7680", R_RSA_7680},
448 {"rsa15360", R_RSA_15360}
451 static double rsa_results[RSA_NUM][2]; /* 2 ops: sign then verify */
452 #endif /* OPENSSL_NO_RSA */
454 #ifndef OPENSSL_NO_DH
456 R_FFDH_2048, R_FFDH_3072, R_FFDH_4096, R_FFDH_6144, R_FFDH_8192, FFDH_NUM
459 static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
460 {"ffdh2048", R_FFDH_2048},
461 {"ffdh3072", R_FFDH_3072},
462 {"ffdh4096", R_FFDH_4096},
463 {"ffdh6144", R_FFDH_6144},
464 {"ffdh8192", R_FFDH_8192},
467 static double ffdh_results[FFDH_NUM][1]; /* 1 op: derivation */
468 #endif /* OPENSSL_NO_DH */
470 #ifndef OPENSSL_NO_EC
472 R_EC_P160, R_EC_P192, R_EC_P224, R_EC_P256, R_EC_P384, R_EC_P521,
473 # ifndef OPENSSL_NO_EC2M
474 R_EC_K163, R_EC_K233, R_EC_K283, R_EC_K409, R_EC_K571,
475 R_EC_B163, R_EC_B233, R_EC_B283, R_EC_B409, R_EC_B571,
477 R_EC_BRP256R1, R_EC_BRP256T1, R_EC_BRP384R1, R_EC_BRP384T1,
478 R_EC_BRP512R1, R_EC_BRP512T1, ECDSA_NUM
480 /* list of ecdsa curves */
481 static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
482 {"ecdsap160", R_EC_P160},
483 {"ecdsap192", R_EC_P192},
484 {"ecdsap224", R_EC_P224},
485 {"ecdsap256", R_EC_P256},
486 {"ecdsap384", R_EC_P384},
487 {"ecdsap521", R_EC_P521},
488 # ifndef OPENSSL_NO_EC2M
489 {"ecdsak163", R_EC_K163},
490 {"ecdsak233", R_EC_K233},
491 {"ecdsak283", R_EC_K283},
492 {"ecdsak409", R_EC_K409},
493 {"ecdsak571", R_EC_K571},
494 {"ecdsab163", R_EC_B163},
495 {"ecdsab233", R_EC_B233},
496 {"ecdsab283", R_EC_B283},
497 {"ecdsab409", R_EC_B409},
498 {"ecdsab571", R_EC_B571},
500 {"ecdsabrp256r1", R_EC_BRP256R1},
501 {"ecdsabrp256t1", R_EC_BRP256T1},
502 {"ecdsabrp384r1", R_EC_BRP384R1},
503 {"ecdsabrp384t1", R_EC_BRP384T1},
504 {"ecdsabrp512r1", R_EC_BRP512R1},
505 {"ecdsabrp512t1", R_EC_BRP512T1}
507 enum { R_EC_X25519 = ECDSA_NUM, R_EC_X448, EC_NUM };
508 /* list of ecdh curves, extension of |ecdsa_choices| list above */
509 static const OPT_PAIR ecdh_choices[EC_NUM] = {
510 {"ecdhp160", R_EC_P160},
511 {"ecdhp192", R_EC_P192},
512 {"ecdhp224", R_EC_P224},
513 {"ecdhp256", R_EC_P256},
514 {"ecdhp384", R_EC_P384},
515 {"ecdhp521", R_EC_P521},
516 # ifndef OPENSSL_NO_EC2M
517 {"ecdhk163", R_EC_K163},
518 {"ecdhk233", R_EC_K233},
519 {"ecdhk283", R_EC_K283},
520 {"ecdhk409", R_EC_K409},
521 {"ecdhk571", R_EC_K571},
522 {"ecdhb163", R_EC_B163},
523 {"ecdhb233", R_EC_B233},
524 {"ecdhb283", R_EC_B283},
525 {"ecdhb409", R_EC_B409},
526 {"ecdhb571", R_EC_B571},
528 {"ecdhbrp256r1", R_EC_BRP256R1},
529 {"ecdhbrp256t1", R_EC_BRP256T1},
530 {"ecdhbrp384r1", R_EC_BRP384R1},
531 {"ecdhbrp384t1", R_EC_BRP384T1},
532 {"ecdhbrp512r1", R_EC_BRP512R1},
533 {"ecdhbrp512t1", R_EC_BRP512T1},
534 {"ecdhx25519", R_EC_X25519},
535 {"ecdhx448", R_EC_X448}
538 static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
539 static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
541 enum { R_EC_Ed25519, R_EC_Ed448, EdDSA_NUM };
542 static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
543 {"ed25519", R_EC_Ed25519},
544 {"ed448", R_EC_Ed448}
547 static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
549 # ifndef OPENSSL_NO_SM2
550 enum { R_EC_CURVESM2, SM2_NUM };
551 static const OPT_PAIR sm2_choices[SM2_NUM] = {
552 {"curveSM2", R_EC_CURVESM2}
554 # define SM2_ID "TLSv1.3+GM+Cipher+Suite"
555 # define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
556 static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
557 # endif /* OPENSSL_NO_SM2 */
558 #endif /* OPENSSL_NO_EC */
561 # define COND(d) (count < (d))
562 # define COUNT(d) (d)
564 # define COND(unused_cond) (run && count<0x7fffffff)
565 # define COUNT(d) (count)
568 typedef struct loopargs_st {
569 ASYNC_JOB *inprogress_job;
570 ASYNC_WAIT_CTX *wait_ctx;
573 unsigned char *buf_malloc;
574 unsigned char *buf2_malloc;
578 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
579 RSA *rsa_key[RSA_NUM];
581 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
582 DSA *dsa_key[DSA_NUM];
584 #ifndef OPENSSL_NO_EC
585 EC_KEY *ecdsa[ECDSA_NUM];
586 EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
587 EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
588 EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
589 # ifndef OPENSSL_NO_SM2
590 EVP_MD_CTX *sm2_ctx[SM2_NUM];
591 EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
592 EVP_PKEY *sm2_pkey[SM2_NUM];
594 unsigned char *secret_a;
595 unsigned char *secret_b;
596 size_t outlen[EC_NUM];
598 #ifndef OPENSSL_NO_DH
599 EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
600 unsigned char *secret_ff_a;
601 unsigned char *secret_ff_b;
604 #ifndef OPENSSL_NO_DEPRECATED_3_0
607 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
610 GCM128_CONTEXT *gcm_ctx;
612 static int run_benchmark(int async_jobs, int (*loop_function) (void *),
613 loopargs_t * loopargs);
615 static unsigned int testnum;
617 /* Nb of iterations to do per algorithm and key-size */
618 static long c[ALGOR_NUM][SIZE_NUM];
620 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
621 static int EVP_Digest_MD2_loop(void *args)
623 loopargs_t *tempargs = *(loopargs_t **) args;
624 unsigned char *buf = tempargs->buf;
625 unsigned char md2[MD2_DIGEST_LENGTH];
628 for (count = 0; COND(c[D_MD2][testnum]); count++) {
629 if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
637 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
638 static int EVP_Digest_MDC2_loop(void *args)
640 loopargs_t *tempargs = *(loopargs_t **) args;
641 unsigned char *buf = tempargs->buf;
642 unsigned char mdc2[MDC2_DIGEST_LENGTH];
645 for (count = 0; COND(c[D_MDC2][testnum]); count++) {
646 if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
654 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
655 static int EVP_Digest_MD4_loop(void *args)
657 loopargs_t *tempargs = *(loopargs_t **) args;
658 unsigned char *buf = tempargs->buf;
659 unsigned char md4[MD4_DIGEST_LENGTH];
662 for (count = 0; COND(c[D_MD4][testnum]); count++) {
663 if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
671 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
672 static int MD5_loop(void *args)
674 loopargs_t *tempargs = *(loopargs_t **) args;
675 unsigned char *buf = tempargs->buf;
676 unsigned char md5[MD5_DIGEST_LENGTH];
678 for (count = 0; COND(c[D_MD5][testnum]); count++)
679 MD5(buf, lengths[testnum], md5);
683 # ifndef OPENSSL_NO_DEPRECATED_3_0
684 static int HMAC_loop(void *args)
686 loopargs_t *tempargs = *(loopargs_t **) args;
687 unsigned char *buf = tempargs->buf;
688 HMAC_CTX *hctx = tempargs->hctx;
689 unsigned char hmac[MD5_DIGEST_LENGTH];
692 for (count = 0; COND(c[D_HMAC][testnum]); count++) {
693 HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
694 HMAC_Update(hctx, buf, lengths[testnum]);
695 HMAC_Final(hctx, hmac, NULL);
702 #ifndef OPENSSL_NO_DEPRECATED_3_0
703 static int SHA1_loop(void *args)
705 loopargs_t *tempargs = *(loopargs_t **) args;
706 unsigned char *buf = tempargs->buf;
707 unsigned char sha[SHA_DIGEST_LENGTH];
709 for (count = 0; COND(c[D_SHA1][testnum]); count++)
710 SHA1(buf, lengths[testnum], sha);
714 static int SHA256_loop(void *args)
716 loopargs_t *tempargs = *(loopargs_t **) args;
717 unsigned char *buf = tempargs->buf;
718 unsigned char sha256[SHA256_DIGEST_LENGTH];
720 for (count = 0; COND(c[D_SHA256][testnum]); count++)
721 SHA256(buf, lengths[testnum], sha256);
725 static int SHA512_loop(void *args)
727 loopargs_t *tempargs = *(loopargs_t **) args;
728 unsigned char *buf = tempargs->buf;
729 unsigned char sha512[SHA512_DIGEST_LENGTH];
731 for (count = 0; COND(c[D_SHA512][testnum]); count++)
732 SHA512(buf, lengths[testnum], sha512);
737 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
738 static int WHIRLPOOL_loop(void *args)
740 loopargs_t *tempargs = *(loopargs_t **) args;
741 unsigned char *buf = tempargs->buf;
742 unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
744 for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
745 WHIRLPOOL(buf, lengths[testnum], whirlpool);
750 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
751 static int EVP_Digest_RMD160_loop(void *args)
753 loopargs_t *tempargs = *(loopargs_t **) args;
754 unsigned char *buf = tempargs->buf;
755 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
757 for (count = 0; COND(c[D_RMD160][testnum]); count++) {
758 if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
759 NULL, EVP_ripemd160(), NULL))
766 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
767 static RC4_KEY rc4_ks;
768 static int RC4_loop(void *args)
770 loopargs_t *tempargs = *(loopargs_t **) args;
771 unsigned char *buf = tempargs->buf;
773 for (count = 0; COND(c[D_RC4][testnum]); count++)
774 RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
779 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
780 static unsigned char DES_iv[8];
781 static DES_key_schedule sch[3];
782 static int DES_ncbc_encrypt_loop(void *args)
784 loopargs_t *tempargs = *(loopargs_t **) args;
785 unsigned char *buf = tempargs->buf;
787 for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
788 DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch[0],
789 &DES_iv, DES_ENCRYPT);
793 static int DES_ede3_cbc_encrypt_loop(void *args)
795 loopargs_t *tempargs = *(loopargs_t **) args;
796 unsigned char *buf = tempargs->buf;
798 for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
799 DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
800 &sch[0], &sch[1], &sch[2], &DES_iv, DES_ENCRYPT);
805 #define MAX_BLOCK_SIZE 128
807 static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
809 #ifndef OPENSSL_NO_DEPRECATED_3_0
810 static AES_KEY aes_ks1, aes_ks2, aes_ks3;
811 static int AES_cbc_128_encrypt_loop(void *args)
813 loopargs_t *tempargs = *(loopargs_t **) args;
814 unsigned char *buf = tempargs->buf;
816 for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
817 AES_cbc_encrypt(buf, buf,
818 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
822 static int AES_cbc_192_encrypt_loop(void *args)
824 loopargs_t *tempargs = *(loopargs_t **) args;
825 unsigned char *buf = tempargs->buf;
827 for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
828 AES_cbc_encrypt(buf, buf,
829 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
833 static int AES_cbc_256_encrypt_loop(void *args)
835 loopargs_t *tempargs = *(loopargs_t **) args;
836 unsigned char *buf = tempargs->buf;
838 for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
839 AES_cbc_encrypt(buf, buf,
840 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
844 static int AES_ige_128_encrypt_loop(void *args)
846 loopargs_t *tempargs = *(loopargs_t **) args;
847 unsigned char *buf = tempargs->buf;
848 unsigned char *buf2 = tempargs->buf2;
850 for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
851 AES_ige_encrypt(buf, buf2,
852 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
856 static int AES_ige_192_encrypt_loop(void *args)
858 loopargs_t *tempargs = *(loopargs_t **) args;
859 unsigned char *buf = tempargs->buf;
860 unsigned char *buf2 = tempargs->buf2;
862 for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
863 AES_ige_encrypt(buf, buf2,
864 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
868 static int AES_ige_256_encrypt_loop(void *args)
870 loopargs_t *tempargs = *(loopargs_t **) args;
871 unsigned char *buf = tempargs->buf;
872 unsigned char *buf2 = tempargs->buf2;
874 for (count = 0; COND(c[D_IGE_256_AES][testnum]); count++)
875 AES_ige_encrypt(buf, buf2,
876 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
880 static int CRYPTO_gcm128_aad_loop(void *args)
882 loopargs_t *tempargs = *(loopargs_t **) args;
883 unsigned char *buf = tempargs->buf;
884 GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
886 for (count = 0; COND(c[D_GHASH][testnum]); count++)
887 CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
892 static int RAND_bytes_loop(void *args)
894 loopargs_t *tempargs = *(loopargs_t **) args;
895 unsigned char *buf = tempargs->buf;
898 for (count = 0; COND(c[D_RAND][testnum]); count++)
899 RAND_bytes(buf, lengths[testnum]);
903 static int decrypt = 0;
904 static int EVP_Update_loop(void *args)
906 loopargs_t *tempargs = *(loopargs_t **) args;
907 unsigned char *buf = tempargs->buf;
908 EVP_CIPHER_CTX *ctx = tempargs->ctx;
912 for (count = 0; COND(c[D_EVP][testnum]); count++) {
913 rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
915 /* reset iv in case of counter overflow */
916 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
920 for (count = 0; COND(c[D_EVP][testnum]); count++) {
921 rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
923 /* reset iv in case of counter overflow */
924 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
929 EVP_DecryptFinal_ex(ctx, buf, &outl);
931 EVP_EncryptFinal_ex(ctx, buf, &outl);
936 * CCM does not support streaming. For the purpose of performance measurement,
937 * each message is encrypted using the same (key,iv)-pair. Do not use this
938 * code in your application.
940 static int EVP_Update_loop_ccm(void *args)
942 loopargs_t *tempargs = *(loopargs_t **) args;
943 unsigned char *buf = tempargs->buf;
944 EVP_CIPHER_CTX *ctx = tempargs->ctx;
946 unsigned char tag[12];
949 for (count = 0; COND(c[D_EVP][testnum]); count++) {
950 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
952 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
953 /* counter is reset on every update */
954 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
957 for (count = 0; COND(c[D_EVP][testnum]); count++) {
958 /* restore iv length field */
959 EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
960 /* counter is reset on every update */
961 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
965 EVP_DecryptFinal_ex(ctx, buf, &outl);
967 EVP_EncryptFinal_ex(ctx, buf, &outl);
972 * To make AEAD benchmarking more relevant perform TLS-like operations,
973 * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
974 * payload length is not actually limited by 16KB...
976 static int EVP_Update_loop_aead(void *args)
978 loopargs_t *tempargs = *(loopargs_t **) args;
979 unsigned char *buf = tempargs->buf;
980 EVP_CIPHER_CTX *ctx = tempargs->ctx;
982 unsigned char aad[13] = { 0xcc };
983 unsigned char faketag[16] = { 0xcc };
986 for (count = 0; COND(c[D_EVP][testnum]); count++) {
987 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
988 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
989 sizeof(faketag), faketag);
990 EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
991 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
992 EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
995 for (count = 0; COND(c[D_EVP][testnum]); count++) {
996 EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
997 EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
998 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
999 EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
1005 static const EVP_MD *evp_md = NULL;
1006 static int EVP_Digest_loop(void *args)
1008 loopargs_t *tempargs = *(loopargs_t **) args;
1009 unsigned char *buf = tempargs->buf;
1010 unsigned char md[EVP_MAX_MD_SIZE];
1013 for (count = 0; COND(c[D_EVP][testnum]); count++) {
1014 if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
1020 #ifndef OPENSSL_NO_DEPRECATED_3_0
1021 static const EVP_MD *evp_hmac_md = NULL;
1022 static char *evp_hmac_name = NULL;
1023 static int EVP_HMAC_loop(void *args)
1025 loopargs_t *tempargs = *(loopargs_t **) args;
1026 unsigned char *buf = tempargs->buf;
1027 unsigned char no_key[32];
1030 for (count = 0; COND(c[D_EVP_HMAC][testnum]); count++) {
1031 if (HMAC(evp_hmac_md, no_key, sizeof(no_key), buf, lengths[testnum],
1032 NULL, NULL) == NULL)
1039 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1040 static const EVP_CIPHER *evp_cmac_cipher = NULL;
1041 static char *evp_cmac_name = NULL;
1043 static int EVP_CMAC_loop(void *args)
1045 loopargs_t *tempargs = *(loopargs_t **) args;
1046 unsigned char *buf = tempargs->buf;
1047 CMAC_CTX *cmac_ctx = tempargs->cmac_ctx;
1048 static const char key[16] = "This is a key...";
1049 unsigned char mac[16];
1050 size_t len = sizeof(mac);
1053 for (count = 0; COND(c[D_EVP_CMAC][testnum]); count++) {
1054 if (!CMAC_Init(cmac_ctx, key, sizeof(key), evp_cmac_cipher, NULL)
1055 || !CMAC_Update(cmac_ctx, buf, lengths[testnum])
1056 || !CMAC_Final(cmac_ctx, mac, &len))
1063 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1064 static long rsa_c[RSA_NUM][2]; /* # RSA iteration test */
1066 static int RSA_sign_loop(void *args)
1068 loopargs_t *tempargs = *(loopargs_t **) args;
1069 unsigned char *buf = tempargs->buf;
1070 unsigned char *buf2 = tempargs->buf2;
1071 unsigned int *rsa_num = &tempargs->siglen;
1072 RSA **rsa_key = tempargs->rsa_key;
1074 for (count = 0; COND(rsa_c[testnum][0]); count++) {
1075 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1077 BIO_printf(bio_err, "RSA sign failure\n");
1078 ERR_print_errors(bio_err);
1086 static int RSA_verify_loop(void *args)
1088 loopargs_t *tempargs = *(loopargs_t **) args;
1089 unsigned char *buf = tempargs->buf;
1090 unsigned char *buf2 = tempargs->buf2;
1091 unsigned int rsa_num = tempargs->siglen;
1092 RSA **rsa_key = tempargs->rsa_key;
1094 for (count = 0; COND(rsa_c[testnum][1]); count++) {
1096 RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1098 BIO_printf(bio_err, "RSA verify failure\n");
1099 ERR_print_errors(bio_err);
1108 #ifndef OPENSSL_NO_DH
1109 static long ffdh_c[FFDH_NUM][1];
1111 static int FFDH_derive_key_loop(void *args)
1113 loopargs_t *tempargs = *(loopargs_t **) args;
1114 EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
1115 unsigned char *derived_secret = tempargs->secret_ff_a;
1116 size_t outlen = MAX_FFDH_SIZE;
1119 for (count = 0; COND(ffdh_c[testnum][0]); count++)
1120 EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
1124 #endif /* OPENSSL_NO_DH */
1126 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1127 static long dsa_c[DSA_NUM][2];
1128 static int DSA_sign_loop(void *args)
1130 loopargs_t *tempargs = *(loopargs_t **) args;
1131 unsigned char *buf = tempargs->buf;
1132 unsigned char *buf2 = tempargs->buf2;
1133 DSA **dsa_key = tempargs->dsa_key;
1134 unsigned int *siglen = &tempargs->siglen;
1136 for (count = 0; COND(dsa_c[testnum][0]); count++) {
1137 ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1139 BIO_printf(bio_err, "DSA sign failure\n");
1140 ERR_print_errors(bio_err);
1148 static int DSA_verify_loop(void *args)
1150 loopargs_t *tempargs = *(loopargs_t **) args;
1151 unsigned char *buf = tempargs->buf;
1152 unsigned char *buf2 = tempargs->buf2;
1153 DSA **dsa_key = tempargs->dsa_key;
1154 unsigned int siglen = tempargs->siglen;
1156 for (count = 0; COND(dsa_c[testnum][1]); count++) {
1157 ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1159 BIO_printf(bio_err, "DSA verify failure\n");
1160 ERR_print_errors(bio_err);
1169 #ifndef OPENSSL_NO_EC
1170 # ifndef OPENSSL_NO_DEPRECATED_3_0
1171 static long ecdsa_c[ECDSA_NUM][2];
1172 static int ECDSA_sign_loop(void *args)
1174 loopargs_t *tempargs = *(loopargs_t **) args;
1175 unsigned char *buf = tempargs->buf;
1176 EC_KEY **ecdsa = tempargs->ecdsa;
1177 unsigned char *ecdsasig = tempargs->buf2;
1178 unsigned int *ecdsasiglen = &tempargs->siglen;
1180 for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1181 ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1183 BIO_printf(bio_err, "ECDSA sign failure\n");
1184 ERR_print_errors(bio_err);
1192 static int ECDSA_verify_loop(void *args)
1194 loopargs_t *tempargs = *(loopargs_t **) args;
1195 unsigned char *buf = tempargs->buf;
1196 EC_KEY **ecdsa = tempargs->ecdsa;
1197 unsigned char *ecdsasig = tempargs->buf2;
1198 unsigned int ecdsasiglen = tempargs->siglen;
1200 for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1201 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1203 BIO_printf(bio_err, "ECDSA verify failure\n");
1204 ERR_print_errors(bio_err);
1213 /* ******************************************************************** */
1214 static long ecdh_c[EC_NUM][1];
1216 static int ECDH_EVP_derive_key_loop(void *args)
1218 loopargs_t *tempargs = *(loopargs_t **) args;
1219 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1220 unsigned char *derived_secret = tempargs->secret_a;
1222 size_t *outlen = &(tempargs->outlen[testnum]);
1224 for (count = 0; COND(ecdh_c[testnum][0]); count++)
1225 EVP_PKEY_derive(ctx, derived_secret, outlen);
1230 static long eddsa_c[EdDSA_NUM][2];
1231 static int EdDSA_sign_loop(void *args)
1233 loopargs_t *tempargs = *(loopargs_t **) args;
1234 unsigned char *buf = tempargs->buf;
1235 EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
1236 unsigned char *eddsasig = tempargs->buf2;
1237 size_t *eddsasigsize = &tempargs->sigsize;
1240 for (count = 0; COND(eddsa_c[testnum][0]); count++) {
1241 ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
1243 BIO_printf(bio_err, "EdDSA sign failure\n");
1244 ERR_print_errors(bio_err);
1252 static int EdDSA_verify_loop(void *args)
1254 loopargs_t *tempargs = *(loopargs_t **) args;
1255 unsigned char *buf = tempargs->buf;
1256 EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
1257 unsigned char *eddsasig = tempargs->buf2;
1258 size_t eddsasigsize = tempargs->sigsize;
1261 for (count = 0; COND(eddsa_c[testnum][1]); count++) {
1262 ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
1264 BIO_printf(bio_err, "EdDSA verify failure\n");
1265 ERR_print_errors(bio_err);
1273 # ifndef OPENSSL_NO_SM2
1274 static long sm2_c[SM2_NUM][2];
1275 static int SM2_sign_loop(void *args)
1277 loopargs_t *tempargs = *(loopargs_t **) args;
1278 unsigned char *buf = tempargs->buf;
1279 EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
1280 unsigned char *sm2sig = tempargs->buf2;
1281 size_t sm2sigsize = tempargs->sigsize;
1282 const size_t max_size = tempargs->sigsize;
1284 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1286 for (count = 0; COND(sm2_c[testnum][0]); count++) {
1287 if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
1288 NULL, sm2_pkey[testnum])) {
1289 BIO_printf(bio_err, "SM2 init sign failure\n");
1290 ERR_print_errors(bio_err);
1294 ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
1297 BIO_printf(bio_err, "SM2 sign failure\n");
1298 ERR_print_errors(bio_err);
1302 /* update the latest returned size and always use the fixed buffer size */
1303 tempargs->sigsize = sm2sigsize;
1304 sm2sigsize = max_size;
1310 static int SM2_verify_loop(void *args)
1312 loopargs_t *tempargs = *(loopargs_t **) args;
1313 unsigned char *buf = tempargs->buf;
1314 EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
1315 unsigned char *sm2sig = tempargs->buf2;
1316 size_t sm2sigsize = tempargs->sigsize;
1318 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1320 for (count = 0; COND(sm2_c[testnum][1]); count++) {
1321 if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
1322 NULL, sm2_pkey[testnum])) {
1323 BIO_printf(bio_err, "SM2 verify init failure\n");
1324 ERR_print_errors(bio_err);
1328 ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
1331 BIO_printf(bio_err, "SM2 verify failure\n");
1332 ERR_print_errors(bio_err);
1339 # endif /* OPENSSL_NO_SM2 */
1340 #endif /* OPENSSL_NO_EC */
1342 static int run_benchmark(int async_jobs,
1343 int (*loop_function) (void *), loopargs_t * loopargs)
1345 int job_op_count = 0;
1346 int total_op_count = 0;
1347 int num_inprogress = 0;
1348 int error = 0, i = 0, ret = 0;
1349 OSSL_ASYNC_FD job_fd = 0;
1350 size_t num_job_fds = 0;
1352 if (async_jobs == 0) {
1353 return loop_function((void *)&loopargs);
1356 for (i = 0; i < async_jobs && !error; i++) {
1357 loopargs_t *looparg_item = loopargs + i;
1359 /* Copy pointer content (looparg_t item address) into async context */
1360 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1361 &job_op_count, loop_function,
1362 (void *)&looparg_item, sizeof(looparg_item));
1368 if (job_op_count == -1) {
1371 total_op_count += job_op_count;
1376 BIO_printf(bio_err, "Failure in the job\n");
1377 ERR_print_errors(bio_err);
1383 while (num_inprogress > 0) {
1384 #if defined(OPENSSL_SYS_WINDOWS)
1386 #elif defined(OPENSSL_SYS_UNIX)
1387 int select_result = 0;
1388 OSSL_ASYNC_FD max_fd = 0;
1391 FD_ZERO(&waitfdset);
1393 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1394 if (loopargs[i].inprogress_job == NULL)
1397 if (!ASYNC_WAIT_CTX_get_all_fds
1398 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1399 || num_job_fds > 1) {
1400 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1401 ERR_print_errors(bio_err);
1405 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1407 FD_SET(job_fd, &waitfdset);
1408 if (job_fd > max_fd)
1412 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1414 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1415 "Decrease the value of async_jobs\n",
1416 max_fd, FD_SETSIZE);
1417 ERR_print_errors(bio_err);
1422 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1423 if (select_result == -1 && errno == EINTR)
1426 if (select_result == -1) {
1427 BIO_printf(bio_err, "Failure in the select\n");
1428 ERR_print_errors(bio_err);
1433 if (select_result == 0)
1437 for (i = 0; i < async_jobs; i++) {
1438 if (loopargs[i].inprogress_job == NULL)
1441 if (!ASYNC_WAIT_CTX_get_all_fds
1442 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1443 || num_job_fds > 1) {
1444 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1445 ERR_print_errors(bio_err);
1449 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1452 #if defined(OPENSSL_SYS_UNIX)
1453 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1455 #elif defined(OPENSSL_SYS_WINDOWS)
1456 if (num_job_fds == 1
1457 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
1462 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1463 loopargs[i].wait_ctx, &job_op_count,
1464 loop_function, (void *)(loopargs + i),
1465 sizeof(loopargs_t));
1470 if (job_op_count == -1) {
1473 total_op_count += job_op_count;
1476 loopargs[i].inprogress_job = NULL;
1481 loopargs[i].inprogress_job = NULL;
1482 BIO_printf(bio_err, "Failure in the job\n");
1483 ERR_print_errors(bio_err);
1490 return error ? -1 : total_op_count;
1493 #define stop_it(do_it, test_num)\
1494 memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
1496 int speed_main(int argc, char **argv)
1499 loopargs_t *loopargs = NULL;
1501 const char *engine_id = NULL;
1502 const EVP_CIPHER *evp_cipher = NULL;
1505 int async_init = 0, multiblock = 0, pr_header = 0;
1506 uint8_t doit[ALGOR_NUM] = { 0 };
1507 int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
1509 unsigned int size_num = SIZE_NUM;
1510 unsigned int i, k, loopargs_len = 0, async_jobs = 0;
1516 #if (!defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)) \
1517 || (!defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)) \
1518 || !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1521 openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
1522 ECDSA_SECONDS, ECDH_SECONDS,
1523 EdDSA_SECONDS, SM2_SECONDS,
1526 /* What follows are the buffers and key material. */
1527 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1530 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1533 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1534 IDEA_KEY_SCHEDULE idea_ks;
1536 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1537 SEED_KEY_SCHEDULE seed_ks;
1539 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1542 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1545 #ifndef OPENSSL_NO_DEPRECATED_3_0
1546 static const unsigned char key16[16] = {
1547 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1548 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1550 static const unsigned char key24[24] = {
1551 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1552 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1553 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1555 static const unsigned char key32[32] = {
1556 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1557 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1558 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1559 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1562 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1563 CAMELLIA_KEY camellia_ks[3];
1565 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1566 static const struct {
1567 const unsigned char *data;
1568 unsigned int length;
1571 { test512, sizeof(test512), 512 },
1572 { test1024, sizeof(test1024), 1024 },
1573 { test2048, sizeof(test2048), 2048 },
1574 { test3072, sizeof(test3072), 3072 },
1575 { test4096, sizeof(test4096), 4092 },
1576 { test7680, sizeof(test7680), 7680 },
1577 { test15360, sizeof(test15360), 15360 }
1579 uint8_t rsa_doit[RSA_NUM] = { 0 };
1580 int primes = RSA_DEFAULT_PRIME_NUM;
1582 #ifndef OPENSSL_NO_DH
1583 typedef struct ffdh_params_st {
1589 static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
1590 {"ffdh2048", NID_ffdhe2048, 2048},
1591 {"ffdh3072", NID_ffdhe3072, 3072},
1592 {"ffdh4096", NID_ffdhe4096, 4096},
1593 {"ffdh6144", NID_ffdhe6144, 6144},
1594 {"ffdh8192", NID_ffdhe8192, 8192}
1596 uint8_t ffdh_doit[FFDH_NUM] = { 0 };
1598 #endif /* OPENSSL_NO_DH */
1599 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1600 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1601 uint8_t dsa_doit[DSA_NUM] = { 0 };
1603 #ifndef OPENSSL_NO_EC
1604 typedef struct ec_curve_st {
1608 size_t sigsize; /* only used for EdDSA curves */
1611 * We only test over the following curves as they are representative, To
1612 * add tests over more curves, simply add the curve NID and curve name to
1613 * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
1614 * lists accordingly.
1616 static const EC_CURVE ec_curves[EC_NUM] = {
1618 {"secp160r1", NID_secp160r1, 160},
1619 {"nistp192", NID_X9_62_prime192v1, 192},
1620 {"nistp224", NID_secp224r1, 224},
1621 {"nistp256", NID_X9_62_prime256v1, 256},
1622 {"nistp384", NID_secp384r1, 384},
1623 {"nistp521", NID_secp521r1, 521},
1624 # ifndef OPENSSL_NO_EC2M
1626 {"nistk163", NID_sect163k1, 163},
1627 {"nistk233", NID_sect233k1, 233},
1628 {"nistk283", NID_sect283k1, 283},
1629 {"nistk409", NID_sect409k1, 409},
1630 {"nistk571", NID_sect571k1, 571},
1631 {"nistb163", NID_sect163r2, 163},
1632 {"nistb233", NID_sect233r1, 233},
1633 {"nistb283", NID_sect283r1, 283},
1634 {"nistb409", NID_sect409r1, 409},
1635 {"nistb571", NID_sect571r1, 571},
1637 {"brainpoolP256r1", NID_brainpoolP256r1, 256},
1638 {"brainpoolP256t1", NID_brainpoolP256t1, 256},
1639 {"brainpoolP384r1", NID_brainpoolP384r1, 384},
1640 {"brainpoolP384t1", NID_brainpoolP384t1, 384},
1641 {"brainpoolP512r1", NID_brainpoolP512r1, 512},
1642 {"brainpoolP512t1", NID_brainpoolP512t1, 512},
1643 /* Other and ECDH only ones */
1644 {"X25519", NID_X25519, 253},
1645 {"X448", NID_X448, 448}
1647 static const EC_CURVE ed_curves[EdDSA_NUM] = {
1649 {"Ed25519", NID_ED25519, 253, 64},
1650 {"Ed448", NID_ED448, 456, 114}
1652 # ifndef OPENSSL_NO_SM2
1653 static const EC_CURVE sm2_curves[SM2_NUM] = {
1655 {"CurveSM2", NID_sm2, 256}
1657 uint8_t sm2_doit[SM2_NUM] = { 0 };
1659 uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
1660 uint8_t ecdh_doit[EC_NUM] = { 0 };
1661 uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
1663 /* checks declarated curves against choices list. */
1664 OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
1665 OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
1667 OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
1668 OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
1670 OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
1671 OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
1673 # ifndef OPENSSL_NO_SM2
1674 OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
1675 OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
1677 #endif /* ndef OPENSSL_NO_EC */
1679 prog = opt_init(argc, argv, speed_options);
1680 while ((o = opt_next()) != OPT_EOF) {
1685 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1688 opt_help(speed_options);
1696 evp_cipher = EVP_get_cipherbyname(opt_arg());
1697 if (evp_cipher == NULL)
1698 evp_md = EVP_get_digestbyname(opt_arg());
1699 if (evp_cipher == NULL && evp_md == NULL) {
1701 "%s: %s is an unknown cipher or digest\n",
1708 #ifndef OPENSSL_NO_DEPRECATED_3_0
1709 evp_hmac_md = EVP_get_digestbyname(opt_arg());
1710 if (evp_hmac_md == NULL) {
1711 BIO_printf(bio_err, "%s: %s is an unknown digest\n",
1715 doit[D_EVP_HMAC] = 1;
1719 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1720 evp_cmac_cipher = EVP_get_cipherbyname(opt_arg());
1721 if (evp_cmac_cipher == NULL) {
1722 BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
1726 doit[D_EVP_CMAC] = 1;
1734 * In a forked execution, an engine might need to be
1735 * initialised by each child process, not by the parent.
1736 * So store the name here and run setup_engine() later on.
1738 engine_id = opt_arg();
1742 multi = atoi(opt_arg());
1746 #ifndef OPENSSL_NO_ASYNC
1747 async_jobs = atoi(opt_arg());
1748 if (!ASYNC_is_capable()) {
1750 "%s: async_jobs specified but async not supported\n",
1754 if (async_jobs > 99999) {
1755 BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
1761 if (!opt_int(opt_arg(), &misalign))
1763 if (misalign > MISALIGN) {
1765 "%s: Maximum offset is %d\n", prog, MISALIGN);
1774 #ifdef OPENSSL_NO_MULTIBLOCK
1776 "%s: -mb specified but multi-block support is disabled\n",
1785 case OPT_PROV_CASES:
1786 if (!opt_provider(o))
1790 #ifndef OPENSSL_NO_DEPRECATED_3_0
1791 if (!opt_int(opt_arg(), &primes))
1796 seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1797 = seconds.ecdh = seconds.eddsa
1798 = seconds.sm2 = seconds.ffdh = atoi(opt_arg());
1801 lengths_single = atoi(opt_arg());
1802 lengths = &lengths_single;
1810 argc = opt_num_rest();
1813 /* Remaining arguments are algorithms. */
1814 for (; *argv; argv++) {
1815 const char *algo = *argv;
1817 if (opt_found(algo, doit_choices, &i)) {
1821 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1822 if (strcmp(algo, "des") == 0) {
1823 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1827 if (strcmp(algo, "sha") == 0) {
1828 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1831 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1832 if (strcmp(algo, "openssl") == 0) /* just for compatibility */
1834 if (strncmp(algo, "rsa", 3) == 0) {
1835 if (algo[3] == '\0') {
1836 memset(rsa_doit, 1, sizeof(rsa_doit));
1839 if (opt_found(algo, rsa_choices, &i)) {
1845 #ifndef OPENSSL_NO_DH
1846 if (strncmp(algo, "ffdh", 4) == 0) {
1847 if (algo[4] == '\0') {
1848 memset(ffdh_doit, 1, sizeof(ffdh_doit));
1851 if (opt_found(algo, ffdh_choices, &i)) {
1857 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1858 if (strncmp(algo, "dsa", 3) == 0) {
1859 if (algo[3] == '\0') {
1860 memset(dsa_doit, 1, sizeof(dsa_doit));
1863 if (opt_found(algo, dsa_choices, &i)) {
1869 #ifndef OPENSSL_NO_DEPRECATED_3_0
1870 if (strcmp(algo, "aes") == 0) {
1871 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1875 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1876 if (strcmp(algo, "camellia") == 0) {
1877 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1881 #ifndef OPENSSL_NO_EC
1882 if (strncmp(algo, "ecdsa", 5) == 0) {
1883 if (algo[5] == '\0') {
1884 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
1887 if (opt_found(algo, ecdsa_choices, &i)) {
1892 if (strncmp(algo, "ecdh", 4) == 0) {
1893 if (algo[4] == '\0') {
1894 memset(ecdh_doit, 1, sizeof(ecdh_doit));
1897 if (opt_found(algo, ecdh_choices, &i)) {
1902 if (strcmp(algo, "eddsa") == 0) {
1903 memset(eddsa_doit, 1, sizeof(eddsa_doit));
1906 if (opt_found(algo, eddsa_choices, &i)) {
1910 # ifndef OPENSSL_NO_SM2
1911 if (strcmp(algo, "sm2") == 0) {
1912 memset(sm2_doit, 1, sizeof(sm2_doit));
1915 if (opt_found(algo, sm2_choices, &i)) {
1920 #endif /* OPENSSL_NO_EC */
1921 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
1927 if (evp_cipher == NULL) {
1928 BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
1930 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1931 EVP_CIPH_FLAG_AEAD_CIPHER)) {
1932 BIO_printf(bio_err, "%s is not an AEAD cipher\n",
1933 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1938 if (evp_cipher == NULL) {
1939 BIO_printf(bio_err,"-mb can be used only with a multi-block"
1940 " capable cipher\n");
1942 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1943 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1944 BIO_printf(bio_err, "%s is not a multi-block capable\n",
1945 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1947 } else if (async_jobs > 0) {
1948 BIO_printf(bio_err, "Async mode is not supported with -mb");
1953 /* Initialize the job pool if async mode is enabled */
1954 if (async_jobs > 0) {
1955 async_init = ASYNC_init_thread(async_jobs, async_jobs);
1957 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1962 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1964 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1965 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1967 for (i = 0; i < loopargs_len; i++) {
1968 if (async_jobs > 0) {
1969 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1970 if (loopargs[i].wait_ctx == NULL) {
1971 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1976 buflen = lengths[size_num - 1];
1977 if (buflen < 36) /* size of random vector in RSA benchmark */
1979 buflen += MAX_MISALIGNMENT + 1;
1980 loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
1981 loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
1982 memset(loopargs[i].buf_malloc, 0, buflen);
1983 memset(loopargs[i].buf2_malloc, 0, buflen);
1985 /* Align the start of buffers on a 64 byte boundary */
1986 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1987 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1988 #ifndef OPENSSL_NO_EC
1989 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1990 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1992 #ifndef OPENSSL_NO_DH
1993 loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
1994 loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
1999 if (multi && do_multi(multi, size_num))
2003 /* Initialize the engine after the fork */
2004 e = setup_engine(engine_id, 0);
2006 /* No parameters; turn on everything. */
2007 if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC] && !doit[D_EVP_CMAC]) {
2008 memset(doit, 1, sizeof(doit));
2009 doit[D_EVP] = doit[D_EVP_HMAC] = doit[D_EVP_CMAC] = 0;
2010 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2013 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2016 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2019 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2020 memset(rsa_doit, 1, sizeof(rsa_doit));
2022 #ifndef OPENSSL_NO_DH
2023 memset(ffdh_doit, 1, sizeof(ffdh_doit));
2025 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2026 memset(dsa_doit, 1, sizeof(dsa_doit));
2028 #ifndef OPENSSL_NO_EC
2029 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
2030 memset(ecdh_doit, 1, sizeof(ecdh_doit));
2031 memset(eddsa_doit, 1, sizeof(eddsa_doit));
2032 # ifndef OPENSSL_NO_SM2
2033 memset(sm2_doit, 1, sizeof(sm2_doit));
2037 for (i = 0; i < ALGOR_NUM; i++)
2041 if (usertime == 0 && !mr)
2043 "You have chosen to measure elapsed time "
2044 "instead of user CPU time.\n");
2046 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2047 for (i = 0; i < loopargs_len; i++) {
2048 if (primes > RSA_DEFAULT_PRIME_NUM) {
2049 /* for multi-prime RSA, skip this */
2052 for (k = 0; k < RSA_NUM; k++) {
2053 const unsigned char *p = rsa_keys[k].data;
2055 loopargs[i].rsa_key[k] =
2056 d2i_RSAPrivateKey(NULL, &p, rsa_keys[k].length);
2057 if (loopargs[i].rsa_key[k] == NULL) {
2059 "internal error loading RSA key number %d\n", k);
2065 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2066 for (i = 0; i < loopargs_len; i++) {
2067 loopargs[i].dsa_key[0] = get_dsa(512);
2068 loopargs[i].dsa_key[1] = get_dsa(1024);
2069 loopargs[i].dsa_key[2] = get_dsa(2048);
2072 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2073 if (doit[D_CBC_DES] || doit[D_EDE3_DES]) {
2074 static DES_cblock keys[] = {
2075 { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }, /* keys[0] */
2076 { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }, /* keys[1] */
2077 { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 } /* keys[3] */
2079 DES_set_key_unchecked(&keys[0], &sch[0]);
2080 DES_set_key_unchecked(&keys[1], &sch[1]);
2081 DES_set_key_unchecked(&keys[2], &sch[2]);
2084 #ifndef OPENSSL_NO_DEPRECATED_3_0
2085 AES_set_encrypt_key(key16, 128, &aes_ks1);
2086 AES_set_encrypt_key(key24, 192, &aes_ks2);
2087 AES_set_encrypt_key(key32, 256, &aes_ks3);
2089 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2090 if (doit[D_CBC_128_CML] || doit[D_CBC_192_CML] || doit[D_CBC_256_CML]) {
2091 Camellia_set_key(key16, 128, &camellia_ks[0]);
2092 Camellia_set_key(key24, 192, &camellia_ks[1]);
2093 Camellia_set_key(key32, 256, &camellia_ks[2]);
2096 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2097 if (doit[D_CBC_IDEA])
2098 IDEA_set_encrypt_key(key16, &idea_ks);
2100 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2101 if (doit[D_CBC_SEED])
2102 SEED_set_key(key16, &seed_ks);
2104 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2106 RC4_set_key(&rc4_ks, 16, key16);
2108 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2109 if (doit[D_CBC_RC2])
2110 RC2_set_key(&rc2_ks, 16, key16, 128);
2112 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2113 if (doit[D_CBC_RC5])
2114 if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
2115 BIO_printf(bio_err, "Failed setting RC5 key\n");
2119 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2121 BF_set_key(&bf_ks, 16, key16);
2123 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2124 if (doit[D_CBC_CAST])
2125 CAST_set_key(&cast_ks, 16, key16);
2128 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2129 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
2135 for (it = count; it; it--)
2136 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
2137 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
2140 c[D_MD2][0] = count / 10;
2141 c[D_MDC2][0] = count / 10;
2142 c[D_MD4][0] = count;
2143 c[D_MD5][0] = count;
2144 c[D_HMAC][0] = count;
2145 c[D_SHA1][0] = count;
2146 c[D_RMD160][0] = count;
2147 c[D_RC4][0] = count * 5;
2148 c[D_CBC_DES][0] = count;
2149 c[D_EDE3_DES][0] = count / 3;
2150 c[D_CBC_IDEA][0] = count;
2151 c[D_CBC_SEED][0] = count;
2152 c[D_CBC_RC2][0] = count;
2153 c[D_CBC_RC5][0] = count;
2154 c[D_CBC_BF][0] = count;
2155 c[D_CBC_CAST][0] = count;
2156 c[D_CBC_128_AES][0] = count;
2157 c[D_CBC_192_AES][0] = count;
2158 c[D_CBC_256_AES][0] = count;
2159 c[D_CBC_128_CML][0] = count;
2160 c[D_CBC_192_CML][0] = count;
2161 c[D_CBC_256_CML][0] = count;
2162 c[D_EVP][0] = count;
2163 c[D_SHA256][0] = count;
2164 c[D_SHA512][0] = count;
2165 c[D_WHIRLPOOL][0] = count;
2166 c[D_IGE_128_AES][0] = count;
2167 c[D_IGE_192_AES][0] = count;
2168 c[D_IGE_256_AES][0] = count;
2169 c[D_GHASH][0] = count;
2170 c[D_RAND][0] = count;
2171 c[D_EVP_HMAC][0] = count;
2172 c[D_EVP_CMAC][0] = count;
2174 for (i = 1; i < size_num; i++) {
2175 long l0 = (long)lengths[0];
2176 long l1 = (long)lengths[i];
2178 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
2179 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
2180 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
2181 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
2182 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
2183 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
2184 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
2185 c[D_EVP][i] = = c[D_EVP][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 c[D_EVP_HMAC][i] = = c[D_EVP_HMAC][0] * 4 * l0 / l1;
2192 c[D_EVP_CMAC][i] = = c[D_EVP_CMAC][0] * 4 * l0 / l1;
2194 l0 = (long)lengths[i - 1];
2196 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
2197 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
2198 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
2199 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
2200 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
2201 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
2202 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
2203 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
2204 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
2205 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
2206 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
2207 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
2208 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
2209 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
2210 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
2211 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
2212 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
2213 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
2216 # if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2217 rsa_c[R_RSA_512][0] = count / 2000;
2218 rsa_c[R_RSA_512][1] = count / 400;
2219 for (i = 1; i < RSA_NUM; i++) {
2220 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
2221 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
2222 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
2225 if (rsa_c[i][0] == 0) {
2226 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2233 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2234 dsa_c[R_DSA_512][0] = count / 1000;
2235 dsa_c[R_DSA_512][1] = count / 1000 / 2;
2236 for (i = 1; i < DSA_NUM; i++) {
2237 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
2238 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
2239 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
2242 if (dsa_c[i][0] == 0) {
2243 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2250 # ifndef OPENSSL_NO_EC
2251 ecdsa_c[R_EC_P160][0] = count / 1000;
2252 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
2253 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2254 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2255 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2256 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2259 if (ecdsa_c[i][0] == 0) {
2265 # ifndef OPENSSL_NO_EC2M
2266 ecdsa_c[R_EC_K163][0] = count / 1000;
2267 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
2268 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2269 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2270 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2271 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2274 if (ecdsa_c[i][0] == 0) {
2280 ecdsa_c[R_EC_B163][0] = count / 1000;
2281 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
2282 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2283 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2284 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2285 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2288 if (ecdsa_c[i][0] == 0) {
2296 ecdh_c[R_EC_P160][0] = count / 1000;
2297 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2298 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2299 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2302 if (ecdh_c[i][0] == 0) {
2307 # ifndef OPENSSL_NO_EC2M
2308 ecdh_c[R_EC_K163][0] = count / 1000;
2309 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2310 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2311 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2314 if (ecdh_c[i][0] == 0) {
2319 ecdh_c[R_EC_B163][0] = count / 1000;
2320 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2321 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2322 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2325 if (ecdh_c[i][0] == 0) {
2331 /* repeated code good to factorize */
2332 ecdh_c[R_EC_BRP256R1][0] = count / 1000;
2333 for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
2334 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2335 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2338 if (ecdh_c[i][0] == 0) {
2343 ecdh_c[R_EC_BRP256T1][0] = count / 1000;
2344 for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
2345 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2346 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2349 if (ecdh_c[i][0] == 0) {
2354 /* default iteration count for the last two EC Curves */
2355 ecdh_c[R_EC_X25519][0] = count / 1800;
2356 ecdh_c[R_EC_X448][0] = count / 7200;
2358 eddsa_c[R_EC_Ed25519][0] = count / 1800;
2359 eddsa_c[R_EC_Ed448][0] = count / 7200;
2361 # ifndef OPENSSL_NO_SM2
2362 sm2_c[R_EC_SM2P256][0] = count / 1800;
2364 # endif /* OPENSSL_NO_EC */
2366 # ifndef OPENSSL_NO_DH
2367 ffdh_c[R_FFDH_2048][0] = count / 1000;
2368 for (i = R_FFDH_3072; i <= R_FFDH_8192; i++) {
2369 ffdh_c[i][0] = ffdh_c[i - 1][0] / 2;
2370 if (ffdh_doit[i] <= 1 && ffdh_c[i][0] == 0) {
2373 if (ffdh_c[i][0] == 0)
2377 # endif /* OPENSSL_NO_DH */
2380 /* not worth fixing */
2381 # error "You cannot disable DES on systems without SIGALRM."
2382 # endif /* OPENSSL_NO_DES */
2384 signal(SIGALRM, alarmed);
2385 #endif /* SIGALRM */
2387 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2389 for (testnum = 0; testnum < size_num; testnum++) {
2390 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
2393 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
2395 print_result(D_MD2, testnum, count, d);
2399 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2401 for (testnum = 0; testnum < size_num; testnum++) {
2402 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
2405 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
2407 print_result(D_MDC2, testnum, count, d);
2414 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2416 for (testnum = 0; testnum < size_num; testnum++) {
2417 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
2420 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
2422 print_result(D_MD4, testnum, count, d);
2429 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2431 for (testnum = 0; testnum < size_num; testnum++) {
2432 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
2435 count = run_benchmark(async_jobs, MD5_loop, loopargs);
2437 print_result(D_MD5, testnum, count, d);
2441 # ifndef OPENSSL_NO_DEPRECATED_3_0
2443 static const char hmac_key[] = "This is a key...";
2444 int len = strlen(hmac_key);
2446 for (i = 0; i < loopargs_len; i++) {
2447 loopargs[i].hctx = HMAC_CTX_new();
2448 if (loopargs[i].hctx == NULL) {
2449 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2453 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2455 for (testnum = 0; testnum < size_num; testnum++) {
2456 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
2459 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2461 print_result(D_HMAC, testnum, count, d);
2463 for (i = 0; i < loopargs_len; i++)
2464 HMAC_CTX_free(loopargs[i].hctx);
2468 #ifndef OPENSSL_NO_DEPRECATED_3_0
2470 for (testnum = 0; testnum < size_num; testnum++) {
2471 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2474 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2476 print_result(D_SHA1, testnum, count, d);
2479 if (doit[D_SHA256]) {
2480 for (testnum = 0; testnum < size_num; testnum++) {
2481 print_message(names[D_SHA256], c[D_SHA256][testnum],
2482 lengths[testnum], seconds.sym);
2484 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2486 print_result(D_SHA256, testnum, count, d);
2489 if (doit[D_SHA512]) {
2490 for (testnum = 0; testnum < size_num; testnum++) {
2491 print_message(names[D_SHA512], c[D_SHA512][testnum],
2492 lengths[testnum], seconds.sym);
2494 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2496 print_result(D_SHA512, testnum, count, d);
2500 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2501 if (doit[D_WHIRLPOOL]) {
2502 for (testnum = 0; testnum < size_num; testnum++) {
2503 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2504 lengths[testnum], seconds.sym);
2506 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2508 print_result(D_WHIRLPOOL, testnum, count, d);
2513 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2514 if (doit[D_RMD160]) {
2515 for (testnum = 0; testnum < size_num; testnum++) {
2516 print_message(names[D_RMD160], c[D_RMD160][testnum],
2517 lengths[testnum], seconds.sym);
2519 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2521 print_result(D_RMD160, testnum, count, d);
2527 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2529 for (testnum = 0; testnum < size_num; testnum++) {
2530 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2533 count = run_benchmark(async_jobs, RC4_loop, loopargs);
2535 print_result(D_RC4, testnum, count, d);
2539 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2540 if (doit[D_CBC_DES]) {
2541 for (testnum = 0; testnum < size_num; testnum++) {
2542 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2543 lengths[testnum], seconds.sym);
2545 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2547 print_result(D_CBC_DES, testnum, count, d);
2551 if (doit[D_EDE3_DES]) {
2552 for (testnum = 0; testnum < size_num; testnum++) {
2553 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2554 lengths[testnum], seconds.sym);
2557 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2559 print_result(D_EDE3_DES, testnum, count, d);
2564 #ifndef OPENSSL_NO_DEPRECATED_3_0
2565 if (doit[D_CBC_128_AES]) {
2566 for (testnum = 0; testnum < size_num; testnum++) {
2567 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2568 lengths[testnum], seconds.sym);
2571 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2573 print_result(D_CBC_128_AES, testnum, count, d);
2576 if (doit[D_CBC_192_AES]) {
2577 for (testnum = 0; testnum < size_num; testnum++) {
2578 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2579 lengths[testnum], seconds.sym);
2582 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2584 print_result(D_CBC_192_AES, testnum, count, d);
2587 if (doit[D_CBC_256_AES]) {
2588 for (testnum = 0; testnum < size_num; testnum++) {
2589 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2590 lengths[testnum], seconds.sym);
2593 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2595 print_result(D_CBC_256_AES, testnum, count, d);
2600 if (doit[D_IGE_128_AES]) {
2601 for (testnum = 0; testnum < size_num; testnum++) {
2602 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2603 lengths[testnum], seconds.sym);
2606 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2608 print_result(D_IGE_128_AES, testnum, count, d);
2611 if (doit[D_IGE_192_AES]) {
2612 for (testnum = 0; testnum < size_num; testnum++) {
2613 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2614 lengths[testnum], seconds.sym);
2617 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2619 print_result(D_IGE_192_AES, testnum, count, d);
2622 if (doit[D_IGE_256_AES]) {
2623 for (testnum = 0; testnum < size_num; testnum++) {
2624 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2625 lengths[testnum], seconds.sym);
2628 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2630 print_result(D_IGE_256_AES, testnum, count, d);
2633 if (doit[D_GHASH]) {
2634 for (i = 0; i < loopargs_len; i++) {
2635 loopargs[i].gcm_ctx =
2636 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2637 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2638 (unsigned char *)"0123456789ab", 12);
2641 for (testnum = 0; testnum < size_num; testnum++) {
2642 print_message(names[D_GHASH], c[D_GHASH][testnum],
2643 lengths[testnum], seconds.sym);
2645 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2647 print_result(D_GHASH, testnum, count, d);
2649 for (i = 0; i < loopargs_len; i++)
2650 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2652 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
2653 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2654 if (doit[D_CBC_128_CML]) {
2655 if (async_jobs > 0) {
2656 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2657 names[D_CBC_128_CML]);
2658 doit[D_CBC_128_CML] = 0;
2660 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2661 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2662 lengths[testnum], seconds.sym);
2664 for (count = 0; COND(c[D_CBC_128_CML][testnum]); count++)
2665 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2666 (size_t)lengths[testnum], &camellia_ks[0],
2667 iv, CAMELLIA_ENCRYPT);
2669 print_result(D_CBC_128_CML, testnum, count, d);
2672 if (doit[D_CBC_192_CML]) {
2673 if (async_jobs > 0) {
2674 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2675 names[D_CBC_192_CML]);
2676 doit[D_CBC_192_CML] = 0;
2678 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2679 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2680 lengths[testnum], seconds.sym);
2681 if (async_jobs > 0) {
2682 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2686 for (count = 0; COND(c[D_CBC_192_CML][testnum]); count++)
2687 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2688 (size_t)lengths[testnum], &camellia_ks[1],
2689 iv, CAMELLIA_ENCRYPT);
2691 print_result(D_CBC_192_CML, testnum, count, d);
2694 if (doit[D_CBC_256_CML]) {
2695 if (async_jobs > 0) {
2696 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2697 names[D_CBC_256_CML]);
2698 doit[D_CBC_256_CML] = 0;
2700 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2701 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2702 lengths[testnum], seconds.sym);
2704 for (count = 0; COND(c[D_CBC_256_CML][testnum]); count++)
2705 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2706 (size_t)lengths[testnum], &camellia_ks[2],
2707 iv, CAMELLIA_ENCRYPT);
2709 print_result(D_CBC_256_CML, testnum, count, d);
2713 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2714 if (doit[D_CBC_IDEA]) {
2715 if (async_jobs > 0) {
2716 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2718 doit[D_CBC_IDEA] = 0;
2720 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2721 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2722 lengths[testnum], seconds.sym);
2724 for (count = 0; COND(c[D_CBC_IDEA][testnum]); count++)
2725 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2726 (size_t)lengths[testnum], &idea_ks,
2729 print_result(D_CBC_IDEA, testnum, count, d);
2733 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2734 if (doit[D_CBC_SEED]) {
2735 if (async_jobs > 0) {
2736 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2738 doit[D_CBC_SEED] = 0;
2740 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2741 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2742 lengths[testnum], seconds.sym);
2744 for (count = 0; COND(c[D_CBC_SEED][testnum]); count++)
2745 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2746 (size_t)lengths[testnum], &seed_ks, iv, 1);
2748 print_result(D_CBC_SEED, testnum, count, d);
2752 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2753 if (doit[D_CBC_RC2]) {
2754 if (async_jobs > 0) {
2755 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2757 doit[D_CBC_RC2] = 0;
2759 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2760 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2761 lengths[testnum], seconds.sym);
2762 if (async_jobs > 0) {
2763 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2767 for (count = 0; COND(c[D_CBC_RC2][testnum]); count++)
2768 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2769 (size_t)lengths[testnum], &rc2_ks,
2772 print_result(D_CBC_RC2, testnum, count, d);
2776 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2777 if (doit[D_CBC_RC5]) {
2778 if (async_jobs > 0) {
2779 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2781 doit[D_CBC_RC5] = 0;
2783 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2784 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2785 lengths[testnum], seconds.sym);
2786 if (async_jobs > 0) {
2787 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2791 for (count = 0; COND(c[D_CBC_RC5][testnum]); count++)
2792 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2793 (size_t)lengths[testnum], &rc5_ks,
2796 print_result(D_CBC_RC5, testnum, count, d);
2800 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2801 if (doit[D_CBC_BF]) {
2802 if (async_jobs > 0) {
2803 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2807 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2808 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2809 lengths[testnum], seconds.sym);
2811 for (count = 0; COND(c[D_CBC_BF][testnum]); count++)
2812 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2813 (size_t)lengths[testnum], &bf_ks,
2816 print_result(D_CBC_BF, testnum, count, d);
2820 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2821 if (doit[D_CBC_CAST]) {
2822 if (async_jobs > 0) {
2823 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2825 doit[D_CBC_CAST] = 0;
2827 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2828 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2829 lengths[testnum], seconds.sym);
2831 for (count = 0; COND(c[D_CBC_CAST][testnum]); count++)
2832 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2833 (size_t)lengths[testnum], &cast_ks,
2836 print_result(D_CBC_CAST, testnum, count, d);
2841 for (testnum = 0; testnum < size_num; testnum++) {
2842 print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2845 count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2847 print_result(D_RAND, testnum, count, d);
2852 if (evp_cipher != NULL) {
2853 int (*loopfunc) (void *) = EVP_Update_loop;
2855 if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
2856 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2857 multiblock_speed(evp_cipher, lengths_single, &seconds);
2862 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2864 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
2865 loopfunc = EVP_Update_loop_ccm;
2866 } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
2867 EVP_CIPH_FLAG_AEAD_CIPHER)) {
2868 loopfunc = EVP_Update_loop_aead;
2869 if (lengths == lengths_list) {
2870 lengths = aead_lengths_list;
2871 size_num = OSSL_NELEM(aead_lengths_list);
2875 for (testnum = 0; testnum < size_num; testnum++) {
2876 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2879 for (k = 0; k < loopargs_len; k++) {
2880 loopargs[k].ctx = EVP_CIPHER_CTX_new();
2881 if (loopargs[k].ctx == NULL) {
2882 BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
2885 if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2886 NULL, iv, decrypt ? 0 : 1)) {
2887 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2888 ERR_print_errors(bio_err);
2892 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2894 keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
2895 loopargs[k].key = app_malloc(keylen, "evp_cipher key");
2896 EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
2897 if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
2898 loopargs[k].key, NULL, -1)) {
2899 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2900 ERR_print_errors(bio_err);
2903 OPENSSL_clear_free(loopargs[k].key, keylen);
2905 /* SIV mode only allows for a single Update operation */
2906 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
2907 EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_SET_SPEED, 1, NULL);
2911 count = run_benchmark(async_jobs, loopfunc, loopargs);
2913 for (k = 0; k < loopargs_len; k++) {
2914 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2916 print_result(D_EVP, testnum, count, d);
2918 } else if (evp_md != NULL) {
2919 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2921 for (testnum = 0; testnum < size_num; testnum++) {
2922 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2925 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2927 print_result(D_EVP, testnum, count, d);
2932 #ifndef OPENSSL_NO_DEPRECATED_3_0
2933 if (doit[D_EVP_HMAC] && evp_hmac_md != NULL) {
2934 const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
2936 evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
2938 sprintf(evp_hmac_name, "HMAC(%s)", md_name);
2939 names[D_EVP_HMAC] = evp_hmac_name;
2941 for (testnum = 0; testnum < size_num; testnum++) {
2942 print_message(names[D_EVP_HMAC], c[D_EVP_HMAC][testnum], lengths[testnum],
2945 count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
2947 print_result(D_EVP_HMAC, testnum, count, d);
2952 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2953 if (doit[D_EVP_CMAC] && evp_cmac_cipher != NULL) {
2954 const char *cipher_name = OBJ_nid2ln(EVP_CIPHER_type(evp_cmac_cipher));
2956 evp_cmac_name = app_malloc(sizeof("CMAC()") + strlen(cipher_name),
2958 sprintf(evp_cmac_name, "CMAC(%s)", cipher_name);
2959 names[D_EVP_CMAC] = evp_cmac_name;
2961 for (i = 0; i < loopargs_len; i++) {
2962 loopargs[i].cmac_ctx = CMAC_CTX_new();
2963 if (loopargs[i].cmac_ctx == NULL) {
2964 BIO_printf(bio_err, "CMAC malloc failure, exiting...");
2968 for (testnum = 0; testnum < size_num; testnum++) {
2969 print_message(names[D_EVP_CMAC], c[D_EVP_CMAC][testnum], lengths[testnum],
2972 count = run_benchmark(async_jobs, EVP_CMAC_loop, loopargs);
2974 print_result(D_EVP_CMAC, testnum, count, d);
2976 for (i = 0; i < loopargs_len; i++)
2977 CMAC_CTX_free(loopargs[i].cmac_ctx);
2981 for (i = 0; i < loopargs_len; i++)
2982 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
2985 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2986 for (testnum = 0; testnum < RSA_NUM; testnum++) {
2988 if (!rsa_doit[testnum])
2990 for (i = 0; i < loopargs_len; i++) {
2991 if (primes > RSA_DEFAULT_PRIME_NUM) {
2992 /* we haven't set keys yet, generate multi-prime RSA keys */
2993 BIGNUM *bn = BN_new();
2997 if (!BN_set_word(bn, RSA_F4)) {
3002 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
3003 rsa_choices[testnum].name);
3005 loopargs[i].rsa_key[testnum] = RSA_new();
3006 if (loopargs[i].rsa_key[testnum] == NULL) {
3011 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
3012 rsa_keys[testnum].bits,
3013 primes, bn, NULL)) {
3019 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3020 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3026 "RSA sign failure. No RSA sign will be done.\n");
3027 ERR_print_errors(bio_err);
3030 pkey_print_message("private", "rsa",
3031 rsa_c[testnum][0], rsa_keys[testnum].bits,
3033 /* RSA_blinding_on(rsa_key[testnum],NULL); */
3035 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
3038 mr ? "+R1:%ld:%d:%.2f\n"
3039 : "%ld %u bits private RSA's in %.2fs\n",
3040 count, rsa_keys[testnum].bits, d);
3041 rsa_results[testnum][0] = (double)count / d;
3045 for (i = 0; i < loopargs_len; i++) {
3046 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3047 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3053 "RSA verify failure. No RSA verify will be done.\n");
3054 ERR_print_errors(bio_err);
3055 rsa_doit[testnum] = 0;
3057 pkey_print_message("public", "rsa",
3058 rsa_c[testnum][1], rsa_keys[testnum].bits,
3061 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
3064 mr ? "+R2:%ld:%d:%.2f\n"
3065 : "%ld %u bits public RSA's in %.2fs\n",
3066 count, rsa_keys[testnum].bits, d);
3067 rsa_results[testnum][1] = (double)count / d;
3070 if (rsa_count <= 1) {
3071 /* if longer than 10s, don't do any more */
3072 stop_it(rsa_doit, testnum);
3075 #endif /* OPENSSL_NO_RSA */
3077 for (i = 0; i < loopargs_len; i++)
3078 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3081 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3082 for (testnum = 0; testnum < DSA_NUM; testnum++) {
3084 if (!dsa_doit[testnum])
3087 /* DSA_generate_key(dsa_key[testnum]); */
3088 /* DSA_sign_setup(dsa_key[testnum],NULL); */
3089 for (i = 0; i < loopargs_len; i++) {
3090 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3091 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3097 "DSA sign failure. No DSA sign will be done.\n");
3098 ERR_print_errors(bio_err);
3101 pkey_print_message("sign", "dsa",
3102 dsa_c[testnum][0], dsa_bits[testnum],
3105 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
3108 mr ? "+R3:%ld:%u:%.2f\n"
3109 : "%ld %u bits DSA signs in %.2fs\n",
3110 count, dsa_bits[testnum], d);
3111 dsa_results[testnum][0] = (double)count / d;
3115 for (i = 0; i < loopargs_len; i++) {
3116 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3117 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3123 "DSA verify failure. No DSA verify will be done.\n");
3124 ERR_print_errors(bio_err);
3125 dsa_doit[testnum] = 0;
3127 pkey_print_message("verify", "dsa",
3128 dsa_c[testnum][1], dsa_bits[testnum],
3131 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
3134 mr ? "+R4:%ld:%u:%.2f\n"
3135 : "%ld %u bits DSA verify in %.2fs\n",
3136 count, dsa_bits[testnum], d);
3137 dsa_results[testnum][1] = (double)count / d;
3140 if (rsa_count <= 1) {
3141 /* if longer than 10s, don't do any more */
3142 stop_it(dsa_doit, testnum);
3145 #endif /* OPENSSL_NO_DSA */
3147 #ifndef OPENSSL_NO_EC
3148 # ifndef OPENSSL_NO_DEPRECATED_3_0
3149 for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
3152 if (!ecdsa_doit[testnum])
3153 continue; /* Ignore Curve */
3154 for (i = 0; i < loopargs_len; i++) {
3155 loopargs[i].ecdsa[testnum] =
3156 EC_KEY_new_by_curve_name(ec_curves[testnum].nid);
3157 if (loopargs[i].ecdsa[testnum] == NULL) {
3163 BIO_printf(bio_err, "ECDSA failure.\n");
3164 ERR_print_errors(bio_err);
3167 for (i = 0; i < loopargs_len; i++) {
3168 /* Perform ECDSA signature test */
3169 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
3170 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3171 &loopargs[i].siglen,
3172 loopargs[i].ecdsa[testnum]);
3178 "ECDSA sign failure. No ECDSA sign will be done.\n");
3179 ERR_print_errors(bio_err);
3182 pkey_print_message("sign", "ecdsa",
3183 ecdsa_c[testnum][0],
3184 ec_curves[testnum].bits, seconds.ecdsa);
3186 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
3190 mr ? "+R5:%ld:%u:%.2f\n" :
3191 "%ld %u bits ECDSA signs in %.2fs \n",
3192 count, ec_curves[testnum].bits, d);
3193 ecdsa_results[testnum][0] = (double)count / d;
3197 /* Perform ECDSA verification test */
3198 for (i = 0; i < loopargs_len; i++) {
3199 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3201 loopargs[i].ecdsa[testnum]);
3207 "ECDSA verify failure. No ECDSA verify will be done.\n");
3208 ERR_print_errors(bio_err);
3209 ecdsa_doit[testnum] = 0;
3211 pkey_print_message("verify", "ecdsa",
3212 ecdsa_c[testnum][1],
3213 ec_curves[testnum].bits, seconds.ecdsa);
3215 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
3218 mr ? "+R6:%ld:%u:%.2f\n"
3219 : "%ld %u bits ECDSA verify in %.2fs\n",
3220 count, ec_curves[testnum].bits, d);
3221 ecdsa_results[testnum][1] = (double)count / d;
3224 if (rsa_count <= 1) {
3225 /* if longer than 10s, don't do any more */
3226 stop_it(ecdsa_doit, testnum);
3230 # endif /* OPENSSL_NO_DEPRECATED_3_0 */
3232 for (testnum = 0; testnum < EC_NUM; testnum++) {
3233 int ecdh_checks = 1;
3235 if (!ecdh_doit[testnum])
3238 for (i = 0; i < loopargs_len; i++) {
3239 EVP_PKEY_CTX *kctx = NULL;
3240 EVP_PKEY_CTX *test_ctx = NULL;
3241 EVP_PKEY_CTX *ctx = NULL;
3242 EVP_PKEY *key_A = NULL;
3243 EVP_PKEY *key_B = NULL;
3247 /* Ensure that the error queue is empty */
3248 if (ERR_peek_error()) {
3250 "WARNING: the error queue contains previous unhandled errors.\n");
3251 ERR_print_errors(bio_err);
3254 /* Let's try to create a ctx directly from the NID: this works for
3255 * curves like Curve25519 that are not implemented through the low
3256 * level EC interface.
3257 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
3258 * then we set the curve by NID before deriving the actual keygen
3259 * ctx for that specific curve. */
3260 kctx = EVP_PKEY_CTX_new_id(ec_curves[testnum].nid, NULL); /* keygen ctx from NID */
3262 EVP_PKEY_CTX *pctx = NULL;
3263 EVP_PKEY *params = NULL;
3265 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
3266 * "int_ctx_new:unsupported algorithm" error was added to the
3268 * We remove it from the error queue as we are handling it. */
3269 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
3270 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
3271 /* check that the error origin matches */
3272 ERR_GET_LIB(error) == ERR_LIB_EVP &&
3273 ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
3274 ERR_get_error(); /* pop error from queue */
3275 if (ERR_peek_error()) {
3277 "Unhandled error in the error queue during ECDH init.\n");
3278 ERR_print_errors(bio_err);
3283 /* Create the context for parameter generation */
3284 if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
3285 /* Initialise the parameter generation */
3286 !EVP_PKEY_paramgen_init(pctx) ||
3287 /* Set the curve by NID */
3288 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3291 /* Create the parameter object params */
3292 !EVP_PKEY_paramgen(pctx, ¶ms)) {
3294 BIO_printf(bio_err, "ECDH EC params init failure.\n");
3295 ERR_print_errors(bio_err);
3299 /* Create the context for the key generation */
3300 kctx = EVP_PKEY_CTX_new(params, NULL);
3302 EVP_PKEY_free(params);
3304 EVP_PKEY_CTX_free(pctx);
3307 if (kctx == NULL || /* keygen ctx is not null */
3308 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
3310 BIO_printf(bio_err, "ECDH keygen failure.\n");
3311 ERR_print_errors(bio_err);
3316 if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
3317 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
3318 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
3319 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
3320 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
3321 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
3322 outlen == 0 || /* ensure outlen is a valid size */
3323 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
3325 BIO_printf(bio_err, "ECDH key generation failure.\n");
3326 ERR_print_errors(bio_err);
3331 /* Here we perform a test run, comparing the output of a*B and b*A;
3332 * we try this here and assume that further EVP_PKEY_derive calls
3333 * never fail, so we can skip checks in the actually benchmarked
3334 * code, for maximum performance. */
3335 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
3336 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
3337 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
3338 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
3339 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
3340 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
3341 test_outlen != outlen /* compare output length */ ) {
3343 BIO_printf(bio_err, "ECDH computation failure.\n");
3344 ERR_print_errors(bio_err);
3349 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
3350 if (CRYPTO_memcmp(loopargs[i].secret_a,
3351 loopargs[i].secret_b, outlen)) {
3353 BIO_printf(bio_err, "ECDH computations don't match.\n");
3354 ERR_print_errors(bio_err);
3359 loopargs[i].ecdh_ctx[testnum] = ctx;
3360 loopargs[i].outlen[testnum] = outlen;
3362 EVP_PKEY_free(key_A);
3363 EVP_PKEY_free(key_B);
3364 EVP_PKEY_CTX_free(kctx);
3366 EVP_PKEY_CTX_free(test_ctx);
3369 if (ecdh_checks != 0) {
3370 pkey_print_message("", "ecdh",
3372 ec_curves[testnum].bits, seconds.ecdh);
3375 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
3378 mr ? "+R7:%ld:%d:%.2f\n" :
3379 "%ld %u-bits ECDH ops in %.2fs\n", count,
3380 ec_curves[testnum].bits, d);
3381 ecdh_results[testnum][0] = (double)count / d;
3385 if (rsa_count <= 1) {
3386 /* if longer than 10s, don't do any more */
3387 stop_it(ecdh_doit, testnum);
3391 for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
3393 EVP_PKEY *ed_pkey = NULL;
3394 EVP_PKEY_CTX *ed_pctx = NULL;
3396 if (!eddsa_doit[testnum])
3397 continue; /* Ignore Curve */
3398 for (i = 0; i < loopargs_len; i++) {
3399 loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
3400 if (loopargs[i].eddsa_ctx[testnum] == NULL) {
3404 loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
3405 if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
3410 if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid, NULL))
3412 || EVP_PKEY_keygen_init(ed_pctx) <= 0
3413 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
3415 EVP_PKEY_CTX_free(ed_pctx);
3418 EVP_PKEY_CTX_free(ed_pctx);
3420 if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
3423 EVP_PKEY_free(ed_pkey);
3426 if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
3427 NULL, NULL, ed_pkey)) {
3429 EVP_PKEY_free(ed_pkey);
3433 EVP_PKEY_free(ed_pkey);
3437 BIO_printf(bio_err, "EdDSA failure.\n");
3438 ERR_print_errors(bio_err);
3441 for (i = 0; i < loopargs_len; i++) {
3442 /* Perform EdDSA signature test */
3443 loopargs[i].sigsize = ed_curves[testnum].sigsize;
3444 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
3445 loopargs[i].buf2, &loopargs[i].sigsize,
3446 loopargs[i].buf, 20);
3452 "EdDSA sign failure. No EdDSA sign will be done.\n");
3453 ERR_print_errors(bio_err);
3456 pkey_print_message("sign", ed_curves[testnum].name,
3457 eddsa_c[testnum][0],
3458 ed_curves[testnum].bits, seconds.eddsa);
3460 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
3464 mr ? "+R8:%ld:%u:%s:%.2f\n" :
3465 "%ld %u bits %s signs in %.2fs \n",
3466 count, ed_curves[testnum].bits,
3467 ed_curves[testnum].name, d);
3468 eddsa_results[testnum][0] = (double)count / d;
3471 /* Perform EdDSA verification test */
3472 for (i = 0; i < loopargs_len; i++) {
3473 st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
3474 loopargs[i].buf2, loopargs[i].sigsize,
3475 loopargs[i].buf, 20);
3481 "EdDSA verify failure. No EdDSA verify will be done.\n");
3482 ERR_print_errors(bio_err);
3483 eddsa_doit[testnum] = 0;
3485 pkey_print_message("verify", ed_curves[testnum].name,
3486 eddsa_c[testnum][1],
3487 ed_curves[testnum].bits, seconds.eddsa);
3489 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
3492 mr ? "+R9:%ld:%u:%s:%.2f\n"
3493 : "%ld %u bits %s verify in %.2fs\n",
3494 count, ed_curves[testnum].bits,
3495 ed_curves[testnum].name, d);
3496 eddsa_results[testnum][1] = (double)count / d;
3499 if (rsa_count <= 1) {
3500 /* if longer than 10s, don't do any more */
3501 stop_it(eddsa_doit, testnum);
3506 # ifndef OPENSSL_NO_SM2
3507 for (testnum = 0; testnum < SM2_NUM; testnum++) {
3509 EVP_PKEY *sm2_pkey = NULL;
3511 if (!sm2_doit[testnum])
3512 continue; /* Ignore Curve */
3513 /* Init signing and verification */
3514 for (i = 0; i < loopargs_len; i++) {
3515 EVP_PKEY_CTX *sm2_pctx = NULL;
3516 EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
3517 EVP_PKEY_CTX *pctx = NULL;
3520 loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
3521 loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
3522 if (loopargs[i].sm2_ctx[testnum] == NULL
3523 || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
3526 /* SM2 keys are generated as normal EC keys with a special curve */
3527 st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
3528 || EVP_PKEY_keygen_init(pctx) <= 0
3529 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3530 sm2_curves[testnum].nid) <= 0
3531 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
3532 EVP_PKEY_CTX_free(pctx);
3536 st = 0; /* set back to zero */
3537 /* attach it sooner to rely on main final cleanup */
3538 loopargs[i].sm2_pkey[testnum] = sm2_pkey;
3539 loopargs[i].sigsize = EVP_PKEY_size(sm2_pkey);
3541 sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3542 sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3543 if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
3544 EVP_PKEY_CTX_free(sm2_vfy_pctx);
3548 /* attach them directly to respective ctx */
3549 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
3550 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
3553 * No need to allow user to set an explicit ID here, just use
3554 * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
3556 if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
3557 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
3560 if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
3561 EVP_sm3(), NULL, sm2_pkey))
3563 if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
3564 EVP_sm3(), NULL, sm2_pkey))
3566 st = 1; /* mark loop as succeeded */
3569 BIO_printf(bio_err, "SM2 init failure.\n");
3570 ERR_print_errors(bio_err);
3573 for (i = 0; i < loopargs_len; i++) {
3574 size_t 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);
3585 "SM2 sign failure. No SM2 sign will be done.\n");
3586 ERR_print_errors(bio_err);
3589 pkey_print_message("sign", sm2_curves[testnum].name,
3591 sm2_curves[testnum].bits, seconds.sm2);
3593 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
3597 mr ? "+R10:%ld:%u:%s:%.2f\n" :
3598 "%ld %u bits %s signs in %.2fs \n",
3599 count, sm2_curves[testnum].bits,
3600 sm2_curves[testnum].name, d);
3601 sm2_results[testnum][0] = (double)count / d;
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);
3615 "SM2 verify failure. No SM2 verify will be done.\n");
3616 ERR_print_errors(bio_err);
3617 sm2_doit[testnum] = 0;
3619 pkey_print_message("verify", sm2_curves[testnum].name,
3621 sm2_curves[testnum].bits, seconds.sm2);
3623 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
3626 mr ? "+R11:%ld:%u:%s:%.2f\n"
3627 : "%ld %u bits %s verify in %.2fs\n",
3628 count, sm2_curves[testnum].bits,
3629 sm2_curves[testnum].name, d);
3630 sm2_results[testnum][1] = (double)count / d;
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;
3640 # endif /* OPENSSL_NO_SM2 */
3641 #endif /* OPENSSL_NO_EC */
3643 #ifndef OPENSSL_NO_DH
3644 for (testnum = 0; testnum < FFDH_NUM; testnum++) {
3645 int ffdh_checks = 1;
3647 if (!ffdh_doit[testnum])
3650 for (i = 0; i < loopargs_len; i++) {
3651 EVP_PKEY *pkey_A = NULL;
3652 EVP_PKEY *pkey_B = NULL;
3653 EVP_PKEY_CTX *ffdh_ctx = NULL;
3654 EVP_PKEY_CTX *test_ctx = NULL;
3658 /* Ensure that the error queue is empty */
3659 if (ERR_peek_error()) {
3661 "WARNING: the error queue contains previous unhandled errors.\n");
3662 ERR_print_errors(bio_err);
3665 pkey_A = EVP_PKEY_new();
3667 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3668 ERR_print_errors(bio_err);
3673 pkey_B = EVP_PKEY_new();
3675 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3676 ERR_print_errors(bio_err);
3682 ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
3684 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3685 ERR_print_errors(bio_err);
3691 if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
3692 BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
3693 ERR_print_errors(bio_err);
3698 if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
3699 BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
3700 ERR_print_errors(bio_err);
3706 if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
3707 EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
3708 BIO_printf(bio_err, "FFDH key generation failure.\n");
3709 ERR_print_errors(bio_err);
3715 EVP_PKEY_CTX_free(ffdh_ctx);
3717 /* check if the derivation works correctly both ways so that
3718 * we know if future derive calls will fail, and we can skip
3719 * error checking in benchmarked code */
3720 ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
3722 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3723 ERR_print_errors(bio_err);
3728 if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
3729 BIO_printf(bio_err, "FFDH derivation context init failure.\n");
3730 ERR_print_errors(bio_err);
3735 if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
3736 BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
3737 ERR_print_errors(bio_err);
3742 if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
3743 BIO_printf(bio_err, "Checking size of shared secret failed.\n");
3744 ERR_print_errors(bio_err);
3749 if (secret_size > MAX_FFDH_SIZE) {
3750 BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
3755 if (EVP_PKEY_derive(ffdh_ctx,
3756 loopargs[i].secret_ff_a,
3757 &secret_size) <= 0) {
3758 BIO_printf(bio_err, "Shared secret derive failure.\n");
3759 ERR_print_errors(bio_err);
3764 /* Now check from side B */
3765 test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
3767 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3768 ERR_print_errors(bio_err);
3773 if (!EVP_PKEY_derive_init(test_ctx) ||
3774 !EVP_PKEY_derive_set_peer(test_ctx, pkey_A) ||
3775 !EVP_PKEY_derive(test_ctx, NULL, &test_out) ||
3776 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) ||
3777 test_out != secret_size) {
3778 BIO_printf(bio_err, "FFDH computation failure.\n");
3784 /* compare the computed secrets */
3785 if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
3786 loopargs[i].secret_ff_b, secret_size)) {
3787 BIO_printf(bio_err, "FFDH computations don't match.\n");
3788 ERR_print_errors(bio_err);
3794 loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
3796 EVP_PKEY_free(pkey_A);
3798 EVP_PKEY_free(pkey_B);
3800 EVP_PKEY_CTX_free(test_ctx);
3803 if (ffdh_checks != 0) {
3804 pkey_print_message("", "ffdh", ffdh_c[testnum][0],
3805 ffdh_params[testnum].bits, seconds.ffdh);
3808 run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
3811 mr ? "+R12:%ld:%d:%.2f\n" :
3812 "%ld %u-bits FFDH ops in %.2fs\n", count,
3813 ffdh_params[testnum].bits, d);
3814 ffdh_results[testnum][0] = (double)count / d;
3817 if (rsa_count <= 1) {
3818 /* if longer than 10s, don't do any more */
3819 stop_it(ffdh_doit, testnum);
3822 #endif /* OPENSSL_NO_DH */
3827 printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
3828 printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
3830 printf("%s ", BN_options());
3831 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3832 printf("%s ", MD2_options());
3834 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3835 printf("%s ", RC4_options());
3837 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3838 printf("%s ", DES_options());
3840 #ifndef OPENSSL_NO_DEPRECATED_3_0
3841 printf("%s ", AES_options());
3843 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3844 printf("%s ", IDEA_options());
3846 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3847 printf("%s ", BF_options());
3849 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
3850 printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
3858 ("The 'numbers' are in 1000s of bytes per second processed.\n");
3861 for (testnum = 0; testnum < size_num; testnum++)
3862 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
3866 for (k = 0; k < ALGOR_NUM; k++) {
3870 printf("+F:%u:%s", k, names[k]);
3872 printf("%-13s", names[k]);
3873 for (testnum = 0; testnum < size_num; testnum++) {
3874 if (results[k][testnum] > 10000 && !mr)
3875 printf(" %11.2fk", results[k][testnum] / 1e3);
3877 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
3881 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3883 for (k = 0; k < RSA_NUM; k++) {
3886 if (testnum && !mr) {
3887 printf("%18ssign verify sign/s verify/s\n", " ");
3891 printf("+F2:%u:%u:%f:%f\n",
3892 k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1]);
3894 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3895 rsa_keys[k].bits, 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
3896 rsa_results[k][0], rsa_results[k][1]);
3899 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3901 for (k = 0; k < DSA_NUM; k++) {
3904 if (testnum && !mr) {
3905 printf("%18ssign verify sign/s verify/s\n", " ");
3909 printf("+F3:%u:%u:%f:%f\n",
3910 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
3912 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3913 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
3914 dsa_results[k][0], dsa_results[k][1]);
3917 #ifndef OPENSSL_NO_EC
3919 for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
3922 if (testnum && !mr) {
3923 printf("%30ssign verify sign/s verify/s\n", " ");
3928 printf("+F4:%u:%u:%f:%f\n",
3929 k, ec_curves[k].bits,
3930 ecdsa_results[k][0], ecdsa_results[k][1]);
3932 printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3933 ec_curves[k].bits, ec_curves[k].name,
3934 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
3935 ecdsa_results[k][0], ecdsa_results[k][1]);
3939 for (k = 0; k < EC_NUM; k++) {
3942 if (testnum && !mr) {
3943 printf("%30sop op/s\n", " ");
3947 printf("+F5:%u:%u:%f:%f\n",
3948 k, ec_curves[k].bits,
3949 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3952 printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
3953 ec_curves[k].bits, ec_curves[k].name,
3954 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3958 for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
3961 if (testnum && !mr) {
3962 printf("%30ssign verify sign/s verify/s\n", " ");
3967 printf("+F6:%u:%u:%s:%f:%f\n",
3968 k, ed_curves[k].bits, ed_curves[k].name,
3969 eddsa_results[k][0], eddsa_results[k][1]);
3971 printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3972 ed_curves[k].bits, ed_curves[k].name,
3973 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
3974 eddsa_results[k][0], eddsa_results[k][1]);
3977 # ifndef OPENSSL_NO_SM2
3979 for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
3982 if (testnum && !mr) {
3983 printf("%30ssign verify sign/s verify/s\n", " ");
3988 printf("+F7:%u:%u:%s:%f:%f\n",
3989 k, sm2_curves[k].bits, sm2_curves[k].name,
3990 sm2_results[k][0], sm2_results[k][1]);
3992 printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3993 sm2_curves[k].bits, sm2_curves[k].name,
3994 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
3995 sm2_results[k][0], sm2_results[k][1]);
3998 #endif /* OPENSSL_NO_EC */
3999 #ifndef OPENSSL_NO_DH
4001 for (k = 0; k < FFDH_NUM; k++) {
4004 if (testnum && !mr) {
4005 printf("%23sop op/s\n", " ");
4009 printf("+F8:%u:%u:%f:%f\n",
4010 k, ffdh_params[k].bits,
4011 ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
4014 printf("%4u bits ffdh %8.4fs %8.1f\n",
4015 ffdh_params[k].bits,
4016 1.0 / ffdh_results[k][0], ffdh_results[k][0]);
4018 #endif /* OPENSSL_NO_DH */
4023 ERR_print_errors(bio_err);
4024 for (i = 0; i < loopargs_len; i++) {
4025 OPENSSL_free(loopargs[i].buf_malloc);
4026 OPENSSL_free(loopargs[i].buf2_malloc);
4028 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4029 for (k = 0; k < RSA_NUM; k++)
4030 RSA_free(loopargs[i].rsa_key[k]);
4032 #ifndef OPENSSL_NO_DH
4033 OPENSSL_free(loopargs[i].secret_ff_a);
4034 OPENSSL_free(loopargs[i].secret_ff_b);
4035 for (k = 0; k < FFDH_NUM; k++) {
4036 EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
4039 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4040 for (k = 0; k < DSA_NUM; k++)
4041 DSA_free(loopargs[i].dsa_key[k]);
4043 #ifndef OPENSSL_NO_EC
4044 for (k = 0; k < ECDSA_NUM; k++)
4045 EC_KEY_free(loopargs[i].ecdsa[k]);
4046 for (k = 0; k < EC_NUM; k++)
4047 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
4048 for (k = 0; k < EdDSA_NUM; k++) {
4049 EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
4050 EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
4052 # ifndef OPENSSL_NO_SM2
4053 for (k = 0; k < SM2_NUM; k++) {
4054 EVP_PKEY_CTX *pctx = NULL;
4056 /* free signing ctx */
4057 if (loopargs[i].sm2_ctx[k] != NULL
4058 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
4059 EVP_PKEY_CTX_free(pctx);
4060 EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
4061 /* free verification ctx */
4062 if (loopargs[i].sm2_vfy_ctx[k] != NULL
4063 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
4064 EVP_PKEY_CTX_free(pctx);
4065 EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
4067 EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
4070 OPENSSL_free(loopargs[i].secret_a);
4071 OPENSSL_free(loopargs[i].secret_b);
4074 #ifndef OPENSSL_NO_DEPRECATED_3_0
4075 OPENSSL_free(evp_hmac_name);
4077 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4078 OPENSSL_free(evp_cmac_name);
4081 if (async_jobs > 0) {
4082 for (i = 0; i < loopargs_len; i++)
4083 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
4087 ASYNC_cleanup_thread();
4089 OPENSSL_free(loopargs);
4094 static void print_message(const char *s, long num, int length, int tm)
4098 mr ? "+DT:%s:%d:%d\n"
4099 : "Doing %s for %ds on %d size blocks: ", s, tm, length);
4100 (void)BIO_flush(bio_err);
4105 mr ? "+DN:%s:%ld:%d\n"
4106 : "Doing %s %ld times on %d size blocks: ", s, num, length);
4107 (void)BIO_flush(bio_err);
4111 #if (!defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)) \
4112 || (!defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)) \
4113 || !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
4114 static void pkey_print_message(const char *str, const char *str2, long num,
4115 unsigned int bits, int tm)
4119 mr ? "+DTP:%d:%s:%s:%d\n"
4120 : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
4121 (void)BIO_flush(bio_err);
4126 mr ? "+DNP:%ld:%d:%s:%s\n"
4127 : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
4128 (void)BIO_flush(bio_err);
4133 static void print_result(int alg, int run_no, int count, double time_used)
4136 BIO_printf(bio_err, "%s error!\n", names[alg]);
4137 ERR_print_errors(bio_err);
4138 /* exit(1); disable exit until default provider enabled */
4142 mr ? "+R:%d:%s:%f\n"
4143 : "%d %s's in %.2fs\n", count, names[alg], time_used);
4144 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
4148 static char *sstrsep(char **string, const char *delim)
4151 char *token = *string;
4156 memset(isdelim, 0, sizeof(isdelim));
4160 isdelim[(unsigned char)(*delim)] = 1;
4164 while (!isdelim[(unsigned char)(**string)]) {
4176 static int do_multi(int multi, int size_num)
4181 static char sep[] = ":";
4183 fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
4184 for (n = 0; n < multi; ++n) {
4185 if (pipe(fd) == -1) {
4186 BIO_printf(bio_err, "pipe failure\n");
4190 (void)BIO_flush(bio_err);
4197 if (dup(fd[1]) == -1) {
4198 BIO_printf(bio_err, "dup failed\n");
4207 printf("Forked child %d\n", n);
4210 /* for now, assume the pipe is long enough to take all the output */
4211 for (n = 0; n < multi; ++n) {
4216 f = fdopen(fds[n], "r");
4217 while (fgets(buf, sizeof(buf), f)) {
4218 p = strchr(buf, '\n');
4221 if (buf[0] != '+') {
4223 "Don't understand line '%s' from child %d\n", buf,
4227 printf("Got: %s from %d\n", buf, n);
4228 if (strncmp(buf, "+F:", 3) == 0) {
4233 alg = atoi(sstrsep(&p, sep));
4235 for (j = 0; j < size_num; ++j)
4236 results[alg][j] += atof(sstrsep(&p, sep));
4238 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4239 else if (strncmp(buf, "+F2:", 4) == 0) {
4244 k = atoi(sstrsep(&p, sep));
4247 d = atof(sstrsep(&p, sep));
4248 rsa_results[k][0] += d;
4250 d = atof(sstrsep(&p, sep));
4251 rsa_results[k][1] += d;
4254 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4255 else if (strncmp(buf, "+F3:", 4) == 0) {
4260 k = atoi(sstrsep(&p, sep));
4263 d = atof(sstrsep(&p, sep));
4264 dsa_results[k][0] += d;
4266 d = atof(sstrsep(&p, sep));
4267 dsa_results[k][1] += d;
4270 # ifndef OPENSSL_NO_EC
4271 else if (strncmp(buf, "+F4:", 4) == 0) {
4276 k = atoi(sstrsep(&p, sep));
4279 d = atof(sstrsep(&p, sep));
4280 ecdsa_results[k][0] += d;
4282 d = atof(sstrsep(&p, sep));
4283 ecdsa_results[k][1] += d;
4284 } else if (strncmp(buf, "+F5:", 4) == 0) {
4289 k = atoi(sstrsep(&p, sep));
4292 d = atof(sstrsep(&p, sep));
4293 ecdh_results[k][0] += d;
4294 } else if (strncmp(buf, "+F6:", 4) == 0) {
4299 k = atoi(sstrsep(&p, sep));
4303 d = atof(sstrsep(&p, sep));
4304 eddsa_results[k][0] += d;
4306 d = atof(sstrsep(&p, sep));
4307 eddsa_results[k][1] += d;
4309 # ifndef OPENSSL_NO_SM2
4310 else if (strncmp(buf, "+F7:", 4) == 0) {
4315 k = atoi(sstrsep(&p, sep));
4319 d = atof(sstrsep(&p, sep));
4320 sm2_results[k][0] += d;
4322 d = atof(sstrsep(&p, sep));
4323 sm2_results[k][1] += d;
4325 # endif /* OPENSSL_NO_SM2 */
4326 # endif /* OPENSSL_NO_EC */
4327 # ifndef OPENSSL_NO_DH
4328 else if (strncmp(buf, "+F8:", 4) == 0) {
4333 k = atoi(sstrsep(&p, sep));
4336 d = atof(sstrsep(&p, sep));
4337 ffdh_results[k][0] += d;
4339 # endif /* OPENSSL_NO_DH */
4341 else if (strncmp(buf, "+H:", 3) == 0) {
4344 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
4355 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
4356 const openssl_speed_sec_t *seconds)
4358 static const int mblengths_list[] =
4359 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
4360 const int *mblengths = mblengths_list;
4361 int j, count, keylen, num = OSSL_NELEM(mblengths_list);
4362 const char *alg_name;
4363 unsigned char *inp, *out, *key, no_key[32], no_iv[16];
4364 EVP_CIPHER_CTX *ctx;
4367 if (lengths_single) {
4368 mblengths = &lengths_single;
4372 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
4373 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
4374 ctx = EVP_CIPHER_CTX_new();
4375 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
4377 keylen = EVP_CIPHER_CTX_key_length(ctx);
4378 key = app_malloc(keylen, "evp_cipher key");
4379 EVP_CIPHER_CTX_rand_key(ctx, key);
4380 EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
4381 OPENSSL_clear_free(key, keylen);
4383 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
4384 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
4386 for (j = 0; j < num; j++) {
4387 print_message(alg_name, 0, mblengths[j], seconds->sym);
4389 for (count = 0; run && count < 0x7fffffff; count++) {
4390 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
4391 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
4392 size_t len = mblengths[j];
4395 memset(aad, 0, 8); /* avoid uninitialized values */
4396 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
4397 aad[9] = 3; /* version */
4399 aad[11] = 0; /* length */
4401 mb_param.out = NULL;
4404 mb_param.interleave = 8;
4406 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
4407 sizeof(mb_param), &mb_param);
4413 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
4414 sizeof(mb_param), &mb_param);
4418 RAND_bytes(out, 16);
4420 aad[11] = (unsigned char)(len >> 8);
4421 aad[12] = (unsigned char)(len);
4422 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
4423 EVP_AEAD_TLS1_AAD_LEN, aad);
4424 EVP_Cipher(ctx, out, inp, len + pad);
4428 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
4429 : "%d %s's in %.2fs\n", count, "evp", d);
4430 results[D_EVP][j] = ((double)count) / d * mblengths[j];
4434 fprintf(stdout, "+H");
4435 for (j = 0; j < num; j++)
4436 fprintf(stdout, ":%d", mblengths[j]);
4437 fprintf(stdout, "\n");
4438 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
4439 for (j = 0; j < num; j++)
4440 fprintf(stdout, ":%.2f", results[D_EVP][j]);
4441 fprintf(stdout, "\n");
4444 "The 'numbers' are in 1000s of bytes per second processed.\n");
4445 fprintf(stdout, "type ");
4446 for (j = 0; j < num; j++)
4447 fprintf(stdout, "%7d bytes", mblengths[j]);
4448 fprintf(stdout, "\n");
4449 fprintf(stdout, "%-24s", alg_name);
4451 for (j = 0; j < num; j++) {
4452 if (results[D_EVP][j] > 10000)
4453 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
4455 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
4457 fprintf(stdout, "\n");
4462 EVP_CIPHER_CTX_free(ctx);