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)
44 #include <openssl/bn.h>
45 #ifndef OPENSSL_NO_DES
46 # include <openssl/des.h>
48 #ifndef OPENSSL_NO_DEPRECATED_3_0
49 #include <openssl/aes.h>
51 #ifndef OPENSSL_NO_CAMELLIA
52 # include <openssl/camellia.h>
54 #ifndef OPENSSL_NO_MD2
55 # include <openssl/md2.h>
57 #ifndef OPENSSL_NO_MDC2
58 # include <openssl/mdc2.h>
60 #ifndef OPENSSL_NO_MD4
61 # include <openssl/md4.h>
63 #ifndef OPENSSL_NO_MD5
64 # include <openssl/md5.h>
66 #include <openssl/hmac.h>
67 #ifndef OPENSSL_NO_CMAC
68 #include <openssl/cmac.h>
70 #include <openssl/sha.h>
71 #ifndef OPENSSL_NO_RMD160
72 # include <openssl/ripemd.h>
74 #ifndef OPENSSL_NO_WHIRLPOOL
75 # include <openssl/whrlpool.h>
77 #ifndef OPENSSL_NO_RC4
78 # include <openssl/rc4.h>
80 #ifndef OPENSSL_NO_RC5
81 # include <openssl/rc5.h>
83 #ifndef OPENSSL_NO_RC2
84 # include <openssl/rc2.h>
86 #ifndef OPENSSL_NO_IDEA
87 # include <openssl/idea.h>
89 #ifndef OPENSSL_NO_SEED
90 # include <openssl/seed.h>
93 # include <openssl/blowfish.h>
95 #ifndef OPENSSL_NO_CAST
96 # include <openssl/cast.h>
98 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
99 # include <openssl/rsa.h>
100 # include "./testrsa.h"
102 #ifndef OPENSSL_NO_DH
103 # include <openssl/dh.h>
105 #include <openssl/x509.h>
106 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
107 # include <openssl/dsa.h>
108 # include "./testdsa.h"
110 #ifndef OPENSSL_NO_EC
111 # include <openssl/ec.h>
113 #include <openssl/modes.h>
116 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
129 #define MAX_MISALIGNMENT 63
130 #define MAX_ECDH_SIZE 256
132 #define MAX_FFDH_SIZE 1024
134 typedef struct openssl_speed_sec_st {
143 } openssl_speed_sec_t;
145 static volatile int run = 0;
147 static int mr = 0; /* machine-readeable output format to merge fork results */
148 static int usertime = 1;
150 static double Time_F(int s);
151 static void print_message(const char *s, long num, int length, int tm);
152 static void pkey_print_message(const char *str, const char *str2,
153 long num, unsigned int bits, int sec);
154 static void print_result(int alg, int run_no, int count, double time_used);
156 static int do_multi(int multi, int size_num);
159 static const int lengths_list[] = {
160 16, 64, 256, 1024, 8 * 1024, 16 * 1024
162 #define SIZE_NUM OSSL_NELEM(lengths_list)
163 static const int *lengths = lengths_list;
165 static const int aead_lengths_list[] = {
166 2, 31, 136, 1024, 8 * 1024, 16 * 1024
174 static void alarmed(int sig)
176 signal(SIGALRM, alarmed);
180 static double Time_F(int s)
182 double ret = app_tminterval(s, usertime);
188 #elif defined(_WIN32)
192 static unsigned int lapse;
193 static volatile unsigned int schlock;
194 static void alarm_win32(unsigned int secs)
199 # define alarm alarm_win32
201 static DWORD WINAPI sleepy(VOID * arg)
209 static double Time_F(int s)
216 thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
218 DWORD err = GetLastError();
219 BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
223 Sleep(0); /* scheduler spinlock */
224 ret = app_tminterval(s, usertime);
226 ret = app_tminterval(s, usertime);
228 TerminateThread(thr, 0);
235 static double Time_F(int s)
237 return app_tminterval(s, usertime);
241 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
242 const openssl_speed_sec_t *seconds);
244 static int opt_found(const char *name, unsigned int *result,
245 const OPT_PAIR pairs[], unsigned int nbelem)
249 for (idx = 0; idx < nbelem; ++idx, pairs++)
250 if (strcmp(name, pairs->name) == 0) {
251 *result = pairs->retval;
256 #define opt_found(value, pairs, result)\
257 opt_found(value, result, pairs, OSSL_NELEM(pairs))
259 typedef enum OPTION_choice {
260 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
261 OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
262 OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM, OPT_PROV_ENUM,
263 OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC
266 const OPTIONS speed_options[] = {
267 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [algorithm...]\n"},
269 OPT_SECTION("General"),
270 {"help", OPT_HELP, '-', "Display this summary"},
272 "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
273 {"mr", OPT_MR, '-', "Produce machine readable output"},
275 {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
277 #ifndef OPENSSL_NO_ASYNC
278 {"async_jobs", OPT_ASYNCJOBS, 'p',
279 "Enable async mode and start specified number of jobs"},
281 #ifndef OPENSSL_NO_ENGINE
282 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
284 {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
286 OPT_SECTION("Selection"),
287 {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
288 #ifndef OPENSSL_NO_DEPRECATED_3_0
289 {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
291 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
292 {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
294 {"decrypt", OPT_DECRYPT, '-',
295 "Time decryption instead of encryption (only EVP)"},
296 {"aead", OPT_AEAD, '-',
297 "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
299 OPT_SECTION("Timing"),
300 {"elapsed", OPT_ELAPSED, '-',
301 "Use wall-clock time instead of CPU user time as divisor"},
302 {"seconds", OPT_SECONDS, 'p',
303 "Run benchmarks for specified amount of seconds"},
304 {"bytes", OPT_BYTES, 'p',
305 "Run [non-PKI] benchmarks on custom-sized buffer"},
306 {"misalign", OPT_MISALIGN, 'p',
307 "Use specified offset to mis-align buffers"},
313 {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
318 D_MD2, D_MDC2, D_MD4, D_MD5 , D_HMAC, D_SHA1, D_RMD160, D_RC4,
319 D_CBC_DES, D_EDE3_DES, D_CBC_IDEA, D_CBC_SEED,
320 D_CBC_RC2, D_CBC_RC5, D_CBC_BF, D_CBC_CAST,
321 D_CBC_128_AES, D_CBC_192_AES, D_CBC_256_AES,
322 D_CBC_128_CML, D_CBC_192_CML, D_CBC_256_CML,
323 D_EVP, D_SHA256, D_SHA512, D_WHIRLPOOL,
324 D_IGE_128_AES, D_IGE_192_AES, D_IGE_256_AES,
325 D_GHASH, D_RAND, D_EVP_HMAC, D_EVP_CMAC, ALGOR_NUM
327 /* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
328 static const char *names[ALGOR_NUM] = {
329 "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
330 "des cbc", "des ede3", "idea cbc", "seed cbc",
331 "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
332 "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
333 "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
334 "evp", "sha256", "sha512", "whirlpool",
335 "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
336 "rand", "hmac", "cmac"
339 /* list of configured algorithm (remaining), with some few alias */
340 static const OPT_PAIR doit_choices[] = {
341 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
344 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
347 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
350 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
352 # ifndef OPENSSL_NO_DEPRECATED_3_0
356 #ifndef OPENSSL_NO_DEPRECATED_3_0
358 {"sha256", D_SHA256},
359 {"sha512", D_SHA512},
361 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
362 {"whirlpool", D_WHIRLPOOL},
364 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
365 {"ripemd", D_RMD160},
366 {"rmd160", D_RMD160},
367 {"ripemd160", D_RMD160},
369 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
372 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
373 {"des-cbc", D_CBC_DES},
374 {"des-ede3", D_EDE3_DES},
376 #ifndef OPENSSL_NO_DEPRECATED_3_0
377 {"aes-128-cbc", D_CBC_128_AES},
378 {"aes-192-cbc", D_CBC_192_AES},
379 {"aes-256-cbc", D_CBC_256_AES},
380 {"aes-128-ige", D_IGE_128_AES},
381 {"aes-192-ige", D_IGE_192_AES},
382 {"aes-256-ige", D_IGE_256_AES},
384 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
385 {"rc2-cbc", D_CBC_RC2},
388 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
389 {"rc5-cbc", D_CBC_RC5},
392 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
393 {"idea-cbc", D_CBC_IDEA},
394 {"idea", D_CBC_IDEA},
396 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
397 {"seed-cbc", D_CBC_SEED},
398 {"seed", D_CBC_SEED},
400 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
401 {"bf-cbc", D_CBC_BF},
402 {"blowfish", D_CBC_BF},
405 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
406 {"cast-cbc", D_CBC_CAST},
407 {"cast", D_CBC_CAST},
408 {"cast5", D_CBC_CAST},
414 static double results[ALGOR_NUM][SIZE_NUM];
416 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
417 enum { R_DSA_512, R_DSA_1024, R_DSA_2048, DSA_NUM };
418 static const OPT_PAIR dsa_choices[DSA_NUM] = {
419 {"dsa512", R_DSA_512},
420 {"dsa1024", R_DSA_1024},
421 {"dsa2048", R_DSA_2048}
423 static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
424 #endif /* OPENSSL_NO_DSA */
426 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
428 R_RSA_512, R_RSA_1024, R_RSA_2048, R_RSA_3072, R_RSA_4096, R_RSA_7680,
431 static const OPT_PAIR rsa_choices[RSA_NUM] = {
432 {"rsa512", R_RSA_512},
433 {"rsa1024", R_RSA_1024},
434 {"rsa2048", R_RSA_2048},
435 {"rsa3072", R_RSA_3072},
436 {"rsa4096", R_RSA_4096},
437 {"rsa7680", R_RSA_7680},
438 {"rsa15360", R_RSA_15360}
441 static double rsa_results[RSA_NUM][2]; /* 2 ops: sign then verify */
442 #endif /* OPENSSL_NO_RSA */
444 #ifndef OPENSSL_NO_DH
446 R_FFDH_2048, R_FFDH_3072, R_FFDH_4096, R_FFDH_6144, R_FFDH_8192, FFDH_NUM
449 static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
450 {"ffdh2048", R_FFDH_2048},
451 {"ffdh3072", R_FFDH_3072},
452 {"ffdh4096", R_FFDH_4096},
453 {"ffdh6144", R_FFDH_6144},
454 {"ffdh8192", R_FFDH_8192},
457 static double ffdh_results[FFDH_NUM][1]; /* 1 op: derivation */
458 #endif /* OPENSSL_NO_DH */
460 #ifndef OPENSSL_NO_EC
462 R_EC_P160, R_EC_P192, R_EC_P224, R_EC_P256, R_EC_P384, R_EC_P521,
463 # ifndef OPENSSL_NO_EC2M
464 R_EC_K163, R_EC_K233, R_EC_K283, R_EC_K409, R_EC_K571,
465 R_EC_B163, R_EC_B233, R_EC_B283, R_EC_B409, R_EC_B571,
467 R_EC_BRP256R1, R_EC_BRP256T1, R_EC_BRP384R1, R_EC_BRP384T1,
468 R_EC_BRP512R1, R_EC_BRP512T1, ECDSA_NUM
470 /* list of ecdsa curves */
471 static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
472 {"ecdsap160", R_EC_P160},
473 {"ecdsap192", R_EC_P192},
474 {"ecdsap224", R_EC_P224},
475 {"ecdsap256", R_EC_P256},
476 {"ecdsap384", R_EC_P384},
477 {"ecdsap521", R_EC_P521},
478 # ifndef OPENSSL_NO_EC2M
479 {"ecdsak163", R_EC_K163},
480 {"ecdsak233", R_EC_K233},
481 {"ecdsak283", R_EC_K283},
482 {"ecdsak409", R_EC_K409},
483 {"ecdsak571", R_EC_K571},
484 {"ecdsab163", R_EC_B163},
485 {"ecdsab233", R_EC_B233},
486 {"ecdsab283", R_EC_B283},
487 {"ecdsab409", R_EC_B409},
488 {"ecdsab571", R_EC_B571},
490 {"ecdsabrp256r1", R_EC_BRP256R1},
491 {"ecdsabrp256t1", R_EC_BRP256T1},
492 {"ecdsabrp384r1", R_EC_BRP384R1},
493 {"ecdsabrp384t1", R_EC_BRP384T1},
494 {"ecdsabrp512r1", R_EC_BRP512R1},
495 {"ecdsabrp512t1", R_EC_BRP512T1}
497 enum { R_EC_X25519 = ECDSA_NUM, R_EC_X448, EC_NUM };
498 /* list of ecdh curves, extension of |ecdsa_choices| list above */
499 static const OPT_PAIR ecdh_choices[EC_NUM] = {
500 {"ecdhp160", R_EC_P160},
501 {"ecdhp192", R_EC_P192},
502 {"ecdhp224", R_EC_P224},
503 {"ecdhp256", R_EC_P256},
504 {"ecdhp384", R_EC_P384},
505 {"ecdhp521", R_EC_P521},
506 # ifndef OPENSSL_NO_EC2M
507 {"ecdhk163", R_EC_K163},
508 {"ecdhk233", R_EC_K233},
509 {"ecdhk283", R_EC_K283},
510 {"ecdhk409", R_EC_K409},
511 {"ecdhk571", R_EC_K571},
512 {"ecdhb163", R_EC_B163},
513 {"ecdhb233", R_EC_B233},
514 {"ecdhb283", R_EC_B283},
515 {"ecdhb409", R_EC_B409},
516 {"ecdhb571", R_EC_B571},
518 {"ecdhbrp256r1", R_EC_BRP256R1},
519 {"ecdhbrp256t1", R_EC_BRP256T1},
520 {"ecdhbrp384r1", R_EC_BRP384R1},
521 {"ecdhbrp384t1", R_EC_BRP384T1},
522 {"ecdhbrp512r1", R_EC_BRP512R1},
523 {"ecdhbrp512t1", R_EC_BRP512T1},
524 {"ecdhx25519", R_EC_X25519},
525 {"ecdhx448", R_EC_X448}
528 static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
529 static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
531 enum { R_EC_Ed25519, R_EC_Ed448, EdDSA_NUM };
532 static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
533 {"ed25519", R_EC_Ed25519},
534 {"ed448", R_EC_Ed448}
537 static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
539 # ifndef OPENSSL_NO_SM2
540 enum { R_EC_CURVESM2, SM2_NUM };
541 static const OPT_PAIR sm2_choices[SM2_NUM] = {
542 {"curveSM2", R_EC_CURVESM2}
544 # define SM2_ID "TLSv1.3+GM+Cipher+Suite"
545 # define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
546 static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
547 # endif /* OPENSSL_NO_SM2 */
548 #endif /* OPENSSL_NO_EC */
551 # define COND(d) (count < (d))
552 # define COUNT(d) (d)
554 # define COND(unused_cond) (run && count<0x7fffffff)
555 # define COUNT(d) (count)
558 typedef struct loopargs_st {
559 ASYNC_JOB *inprogress_job;
560 ASYNC_WAIT_CTX *wait_ctx;
563 unsigned char *buf_malloc;
564 unsigned char *buf2_malloc;
568 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
569 RSA *rsa_key[RSA_NUM];
571 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
572 DSA *dsa_key[DSA_NUM];
574 #ifndef OPENSSL_NO_EC
575 EC_KEY *ecdsa[ECDSA_NUM];
576 EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
577 EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
578 EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
579 # ifndef OPENSSL_NO_SM2
580 EVP_MD_CTX *sm2_ctx[SM2_NUM];
581 EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
582 EVP_PKEY *sm2_pkey[SM2_NUM];
584 unsigned char *secret_a;
585 unsigned char *secret_b;
586 size_t outlen[EC_NUM];
588 #ifndef OPENSSL_NO_DH
589 EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
590 unsigned char *secret_ff_a;
591 unsigned char *secret_ff_b;
594 #ifndef OPENSSL_NO_DEPRECATED_3_0
597 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
600 GCM128_CONTEXT *gcm_ctx;
602 static int run_benchmark(int async_jobs, int (*loop_function) (void *),
603 loopargs_t * loopargs);
605 static unsigned int testnum;
607 /* Nb of iterations to do per algorithm and key-size */
608 static long c[ALGOR_NUM][SIZE_NUM];
610 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
611 static int EVP_Digest_MD2_loop(void *args)
613 loopargs_t *tempargs = *(loopargs_t **) args;
614 unsigned char *buf = tempargs->buf;
615 unsigned char md2[MD2_DIGEST_LENGTH];
618 for (count = 0; COND(c[D_MD2][testnum]); count++) {
619 if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
627 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
628 static int EVP_Digest_MDC2_loop(void *args)
630 loopargs_t *tempargs = *(loopargs_t **) args;
631 unsigned char *buf = tempargs->buf;
632 unsigned char mdc2[MDC2_DIGEST_LENGTH];
635 for (count = 0; COND(c[D_MDC2][testnum]); count++) {
636 if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
644 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
645 static int EVP_Digest_MD4_loop(void *args)
647 loopargs_t *tempargs = *(loopargs_t **) args;
648 unsigned char *buf = tempargs->buf;
649 unsigned char md4[MD4_DIGEST_LENGTH];
652 for (count = 0; COND(c[D_MD4][testnum]); count++) {
653 if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
661 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
662 static int MD5_loop(void *args)
664 loopargs_t *tempargs = *(loopargs_t **) args;
665 unsigned char *buf = tempargs->buf;
666 unsigned char md5[MD5_DIGEST_LENGTH];
668 for (count = 0; COND(c[D_MD5][testnum]); count++)
669 MD5(buf, lengths[testnum], md5);
673 # ifndef OPENSSL_NO_DEPRECATED_3_0
674 static int HMAC_loop(void *args)
676 loopargs_t *tempargs = *(loopargs_t **) args;
677 unsigned char *buf = tempargs->buf;
678 HMAC_CTX *hctx = tempargs->hctx;
679 unsigned char hmac[MD5_DIGEST_LENGTH];
682 for (count = 0; COND(c[D_HMAC][testnum]); count++) {
683 HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
684 HMAC_Update(hctx, buf, lengths[testnum]);
685 HMAC_Final(hctx, hmac, NULL);
692 #ifndef OPENSSL_NO_DEPRECATED_3_0
693 static int SHA1_loop(void *args)
695 loopargs_t *tempargs = *(loopargs_t **) args;
696 unsigned char *buf = tempargs->buf;
697 unsigned char sha[SHA_DIGEST_LENGTH];
699 for (count = 0; COND(c[D_SHA1][testnum]); count++)
700 SHA1(buf, lengths[testnum], sha);
704 static int SHA256_loop(void *args)
706 loopargs_t *tempargs = *(loopargs_t **) args;
707 unsigned char *buf = tempargs->buf;
708 unsigned char sha256[SHA256_DIGEST_LENGTH];
710 for (count = 0; COND(c[D_SHA256][testnum]); count++)
711 SHA256(buf, lengths[testnum], sha256);
715 static int SHA512_loop(void *args)
717 loopargs_t *tempargs = *(loopargs_t **) args;
718 unsigned char *buf = tempargs->buf;
719 unsigned char sha512[SHA512_DIGEST_LENGTH];
721 for (count = 0; COND(c[D_SHA512][testnum]); count++)
722 SHA512(buf, lengths[testnum], sha512);
727 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
728 static int WHIRLPOOL_loop(void *args)
730 loopargs_t *tempargs = *(loopargs_t **) args;
731 unsigned char *buf = tempargs->buf;
732 unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
734 for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
735 WHIRLPOOL(buf, lengths[testnum], whirlpool);
740 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
741 static int EVP_Digest_RMD160_loop(void *args)
743 loopargs_t *tempargs = *(loopargs_t **) args;
744 unsigned char *buf = tempargs->buf;
745 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
747 for (count = 0; COND(c[D_RMD160][testnum]); count++) {
748 if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
749 NULL, EVP_ripemd160(), NULL))
756 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
757 static RC4_KEY rc4_ks;
758 static int RC4_loop(void *args)
760 loopargs_t *tempargs = *(loopargs_t **) args;
761 unsigned char *buf = tempargs->buf;
763 for (count = 0; COND(c[D_RC4][testnum]); count++)
764 RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
769 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
770 static unsigned char DES_iv[8];
771 static DES_key_schedule sch[3];
772 static int DES_ncbc_encrypt_loop(void *args)
774 loopargs_t *tempargs = *(loopargs_t **) args;
775 unsigned char *buf = tempargs->buf;
777 for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
778 DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch[0],
779 &DES_iv, DES_ENCRYPT);
783 static int DES_ede3_cbc_encrypt_loop(void *args)
785 loopargs_t *tempargs = *(loopargs_t **) args;
786 unsigned char *buf = tempargs->buf;
788 for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
789 DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
790 &sch[0], &sch[1], &sch[2], &DES_iv, DES_ENCRYPT);
795 #define MAX_BLOCK_SIZE 128
797 static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
799 #ifndef OPENSSL_NO_DEPRECATED_3_0
800 static AES_KEY aes_ks1, aes_ks2, aes_ks3;
801 static int AES_cbc_128_encrypt_loop(void *args)
803 loopargs_t *tempargs = *(loopargs_t **) args;
804 unsigned char *buf = tempargs->buf;
806 for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
807 AES_cbc_encrypt(buf, buf,
808 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
812 static int AES_cbc_192_encrypt_loop(void *args)
814 loopargs_t *tempargs = *(loopargs_t **) args;
815 unsigned char *buf = tempargs->buf;
817 for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
818 AES_cbc_encrypt(buf, buf,
819 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
823 static int AES_cbc_256_encrypt_loop(void *args)
825 loopargs_t *tempargs = *(loopargs_t **) args;
826 unsigned char *buf = tempargs->buf;
828 for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
829 AES_cbc_encrypt(buf, buf,
830 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
834 static int AES_ige_128_encrypt_loop(void *args)
836 loopargs_t *tempargs = *(loopargs_t **) args;
837 unsigned char *buf = tempargs->buf;
838 unsigned char *buf2 = tempargs->buf2;
840 for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
841 AES_ige_encrypt(buf, buf2,
842 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
846 static int AES_ige_192_encrypt_loop(void *args)
848 loopargs_t *tempargs = *(loopargs_t **) args;
849 unsigned char *buf = tempargs->buf;
850 unsigned char *buf2 = tempargs->buf2;
852 for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
853 AES_ige_encrypt(buf, buf2,
854 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
858 static int AES_ige_256_encrypt_loop(void *args)
860 loopargs_t *tempargs = *(loopargs_t **) args;
861 unsigned char *buf = tempargs->buf;
862 unsigned char *buf2 = tempargs->buf2;
864 for (count = 0; COND(c[D_IGE_256_AES][testnum]); count++)
865 AES_ige_encrypt(buf, buf2,
866 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
870 static int CRYPTO_gcm128_aad_loop(void *args)
872 loopargs_t *tempargs = *(loopargs_t **) args;
873 unsigned char *buf = tempargs->buf;
874 GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
876 for (count = 0; COND(c[D_GHASH][testnum]); count++)
877 CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
882 static int RAND_bytes_loop(void *args)
884 loopargs_t *tempargs = *(loopargs_t **) args;
885 unsigned char *buf = tempargs->buf;
888 for (count = 0; COND(c[D_RAND][testnum]); count++)
889 RAND_bytes(buf, lengths[testnum]);
893 static int decrypt = 0;
894 static int EVP_Update_loop(void *args)
896 loopargs_t *tempargs = *(loopargs_t **) args;
897 unsigned char *buf = tempargs->buf;
898 EVP_CIPHER_CTX *ctx = tempargs->ctx;
902 for (count = 0; COND(c[D_EVP][testnum]); count++) {
903 rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
905 /* reset iv in case of counter overflow */
906 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
910 for (count = 0; COND(c[D_EVP][testnum]); count++) {
911 rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
913 /* reset iv in case of counter overflow */
914 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
919 EVP_DecryptFinal_ex(ctx, buf, &outl);
921 EVP_EncryptFinal_ex(ctx, buf, &outl);
926 * CCM does not support streaming. For the purpose of performance measurement,
927 * each message is encrypted using the same (key,iv)-pair. Do not use this
928 * code in your application.
930 static int EVP_Update_loop_ccm(void *args)
932 loopargs_t *tempargs = *(loopargs_t **) args;
933 unsigned char *buf = tempargs->buf;
934 EVP_CIPHER_CTX *ctx = tempargs->ctx;
936 unsigned char tag[12];
939 for (count = 0; COND(c[D_EVP][testnum]); count++) {
940 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
942 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
943 /* counter is reset on every update */
944 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
947 for (count = 0; COND(c[D_EVP][testnum]); count++) {
948 /* restore iv length field */
949 EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
950 /* counter is reset on every update */
951 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
955 EVP_DecryptFinal_ex(ctx, buf, &outl);
957 EVP_EncryptFinal_ex(ctx, buf, &outl);
962 * To make AEAD benchmarking more relevant perform TLS-like operations,
963 * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
964 * payload length is not actually limited by 16KB...
966 static int EVP_Update_loop_aead(void *args)
968 loopargs_t *tempargs = *(loopargs_t **) args;
969 unsigned char *buf = tempargs->buf;
970 EVP_CIPHER_CTX *ctx = tempargs->ctx;
972 unsigned char aad[13] = { 0xcc };
973 unsigned char faketag[16] = { 0xcc };
976 for (count = 0; COND(c[D_EVP][testnum]); count++) {
977 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
978 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
979 sizeof(faketag), faketag);
980 EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
981 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
982 EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
985 for (count = 0; COND(c[D_EVP][testnum]); count++) {
986 EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
987 EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
988 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
989 EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
995 static const EVP_MD *evp_md = NULL;
996 static int EVP_Digest_loop(void *args)
998 loopargs_t *tempargs = *(loopargs_t **) args;
999 unsigned char *buf = tempargs->buf;
1000 unsigned char md[EVP_MAX_MD_SIZE];
1003 for (count = 0; COND(c[D_EVP][testnum]); count++) {
1004 if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
1010 #ifndef OPENSSL_NO_DEPRECATED_3_0
1011 static const EVP_MD *evp_hmac_md = NULL;
1012 static char *evp_hmac_name = NULL;
1013 static int EVP_HMAC_loop(void *args)
1015 loopargs_t *tempargs = *(loopargs_t **) args;
1016 unsigned char *buf = tempargs->buf;
1017 unsigned char no_key[32];
1020 for (count = 0; COND(c[D_EVP_HMAC][testnum]); count++) {
1021 if (HMAC(evp_hmac_md, no_key, sizeof(no_key), buf, lengths[testnum],
1022 NULL, NULL) == NULL)
1029 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1030 static const EVP_CIPHER *evp_cmac_cipher = NULL;
1031 static char *evp_cmac_name = NULL;
1033 static int EVP_CMAC_loop(void *args)
1035 loopargs_t *tempargs = *(loopargs_t **) args;
1036 unsigned char *buf = tempargs->buf;
1037 CMAC_CTX *cmac_ctx = tempargs->cmac_ctx;
1038 static const char key[16] = "This is a key...";
1039 unsigned char mac[16];
1040 size_t len = sizeof(mac);
1043 for (count = 0; COND(c[D_EVP_CMAC][testnum]); count++) {
1044 if (!CMAC_Init(cmac_ctx, key, sizeof(key), evp_cmac_cipher, NULL)
1045 || !CMAC_Update(cmac_ctx, buf, lengths[testnum])
1046 || !CMAC_Final(cmac_ctx, mac, &len))
1053 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1054 static long rsa_c[RSA_NUM][2]; /* # RSA iteration test */
1056 static int RSA_sign_loop(void *args)
1058 loopargs_t *tempargs = *(loopargs_t **) args;
1059 unsigned char *buf = tempargs->buf;
1060 unsigned char *buf2 = tempargs->buf2;
1061 unsigned int *rsa_num = &tempargs->siglen;
1062 RSA **rsa_key = tempargs->rsa_key;
1064 for (count = 0; COND(rsa_c[testnum][0]); count++) {
1065 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1067 BIO_printf(bio_err, "RSA sign failure\n");
1068 ERR_print_errors(bio_err);
1076 static int RSA_verify_loop(void *args)
1078 loopargs_t *tempargs = *(loopargs_t **) args;
1079 unsigned char *buf = tempargs->buf;
1080 unsigned char *buf2 = tempargs->buf2;
1081 unsigned int rsa_num = tempargs->siglen;
1082 RSA **rsa_key = tempargs->rsa_key;
1084 for (count = 0; COND(rsa_c[testnum][1]); count++) {
1086 RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1088 BIO_printf(bio_err, "RSA verify failure\n");
1089 ERR_print_errors(bio_err);
1098 #ifndef OPENSSL_NO_DH
1099 static long ffdh_c[FFDH_NUM][1];
1101 static int FFDH_derive_key_loop(void *args)
1103 loopargs_t *tempargs = *(loopargs_t **) args;
1104 EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
1105 unsigned char *derived_secret = tempargs->secret_ff_a;
1106 size_t outlen = MAX_FFDH_SIZE;
1109 for (count = 0; COND(ffdh_c[testnum][0]); count++)
1110 EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
1114 #endif /* OPENSSL_NO_DH */
1116 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1117 static long dsa_c[DSA_NUM][2];
1118 static int DSA_sign_loop(void *args)
1120 loopargs_t *tempargs = *(loopargs_t **) args;
1121 unsigned char *buf = tempargs->buf;
1122 unsigned char *buf2 = tempargs->buf2;
1123 DSA **dsa_key = tempargs->dsa_key;
1124 unsigned int *siglen = &tempargs->siglen;
1126 for (count = 0; COND(dsa_c[testnum][0]); count++) {
1127 ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1129 BIO_printf(bio_err, "DSA sign failure\n");
1130 ERR_print_errors(bio_err);
1138 static int DSA_verify_loop(void *args)
1140 loopargs_t *tempargs = *(loopargs_t **) args;
1141 unsigned char *buf = tempargs->buf;
1142 unsigned char *buf2 = tempargs->buf2;
1143 DSA **dsa_key = tempargs->dsa_key;
1144 unsigned int siglen = tempargs->siglen;
1146 for (count = 0; COND(dsa_c[testnum][1]); count++) {
1147 ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1149 BIO_printf(bio_err, "DSA verify failure\n");
1150 ERR_print_errors(bio_err);
1159 #ifndef OPENSSL_NO_EC
1160 # ifndef OPENSSL_NO_DEPRECATED_3_0
1161 static long ecdsa_c[ECDSA_NUM][2];
1162 static int ECDSA_sign_loop(void *args)
1164 loopargs_t *tempargs = *(loopargs_t **) args;
1165 unsigned char *buf = tempargs->buf;
1166 EC_KEY **ecdsa = tempargs->ecdsa;
1167 unsigned char *ecdsasig = tempargs->buf2;
1168 unsigned int *ecdsasiglen = &tempargs->siglen;
1170 for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1171 ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1173 BIO_printf(bio_err, "ECDSA sign failure\n");
1174 ERR_print_errors(bio_err);
1182 static int ECDSA_verify_loop(void *args)
1184 loopargs_t *tempargs = *(loopargs_t **) args;
1185 unsigned char *buf = tempargs->buf;
1186 EC_KEY **ecdsa = tempargs->ecdsa;
1187 unsigned char *ecdsasig = tempargs->buf2;
1188 unsigned int ecdsasiglen = tempargs->siglen;
1190 for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1191 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1193 BIO_printf(bio_err, "ECDSA verify failure\n");
1194 ERR_print_errors(bio_err);
1203 /* ******************************************************************** */
1204 static long ecdh_c[EC_NUM][1];
1206 static int ECDH_EVP_derive_key_loop(void *args)
1208 loopargs_t *tempargs = *(loopargs_t **) args;
1209 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1210 unsigned char *derived_secret = tempargs->secret_a;
1212 size_t *outlen = &(tempargs->outlen[testnum]);
1214 for (count = 0; COND(ecdh_c[testnum][0]); count++)
1215 EVP_PKEY_derive(ctx, derived_secret, outlen);
1220 static long eddsa_c[EdDSA_NUM][2];
1221 static int EdDSA_sign_loop(void *args)
1223 loopargs_t *tempargs = *(loopargs_t **) args;
1224 unsigned char *buf = tempargs->buf;
1225 EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
1226 unsigned char *eddsasig = tempargs->buf2;
1227 size_t *eddsasigsize = &tempargs->sigsize;
1230 for (count = 0; COND(eddsa_c[testnum][0]); count++) {
1231 ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
1233 BIO_printf(bio_err, "EdDSA sign failure\n");
1234 ERR_print_errors(bio_err);
1242 static int EdDSA_verify_loop(void *args)
1244 loopargs_t *tempargs = *(loopargs_t **) args;
1245 unsigned char *buf = tempargs->buf;
1246 EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
1247 unsigned char *eddsasig = tempargs->buf2;
1248 size_t eddsasigsize = tempargs->sigsize;
1251 for (count = 0; COND(eddsa_c[testnum][1]); count++) {
1252 ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
1254 BIO_printf(bio_err, "EdDSA verify failure\n");
1255 ERR_print_errors(bio_err);
1263 # ifndef OPENSSL_NO_SM2
1264 static long sm2_c[SM2_NUM][2];
1265 static int SM2_sign_loop(void *args)
1267 loopargs_t *tempargs = *(loopargs_t **) args;
1268 unsigned char *buf = tempargs->buf;
1269 EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
1270 unsigned char *sm2sig = tempargs->buf2;
1271 size_t sm2sigsize = tempargs->sigsize;
1272 const size_t max_size = tempargs->sigsize;
1274 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1276 for (count = 0; COND(sm2_c[testnum][0]); count++) {
1277 if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
1278 NULL, sm2_pkey[testnum])) {
1279 BIO_printf(bio_err, "SM2 init sign failure\n");
1280 ERR_print_errors(bio_err);
1284 ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
1287 BIO_printf(bio_err, "SM2 sign failure\n");
1288 ERR_print_errors(bio_err);
1292 /* update the latest returned size and always use the fixed buffer size */
1293 tempargs->sigsize = sm2sigsize;
1294 sm2sigsize = max_size;
1300 static int SM2_verify_loop(void *args)
1302 loopargs_t *tempargs = *(loopargs_t **) args;
1303 unsigned char *buf = tempargs->buf;
1304 EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
1305 unsigned char *sm2sig = tempargs->buf2;
1306 size_t sm2sigsize = tempargs->sigsize;
1308 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1310 for (count = 0; COND(sm2_c[testnum][1]); count++) {
1311 if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
1312 NULL, sm2_pkey[testnum])) {
1313 BIO_printf(bio_err, "SM2 verify init failure\n");
1314 ERR_print_errors(bio_err);
1318 ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
1321 BIO_printf(bio_err, "SM2 verify failure\n");
1322 ERR_print_errors(bio_err);
1329 # endif /* OPENSSL_NO_SM2 */
1330 #endif /* OPENSSL_NO_EC */
1332 static int run_benchmark(int async_jobs,
1333 int (*loop_function) (void *), loopargs_t * loopargs)
1335 int job_op_count = 0;
1336 int total_op_count = 0;
1337 int num_inprogress = 0;
1338 int error = 0, i = 0, ret = 0;
1339 OSSL_ASYNC_FD job_fd = 0;
1340 size_t num_job_fds = 0;
1342 if (async_jobs == 0) {
1343 return loop_function((void *)&loopargs);
1346 for (i = 0; i < async_jobs && !error; i++) {
1347 loopargs_t *looparg_item = loopargs + i;
1349 /* Copy pointer content (looparg_t item address) into async context */
1350 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1351 &job_op_count, loop_function,
1352 (void *)&looparg_item, sizeof(looparg_item));
1358 if (job_op_count == -1) {
1361 total_op_count += job_op_count;
1366 BIO_printf(bio_err, "Failure in the job\n");
1367 ERR_print_errors(bio_err);
1373 while (num_inprogress > 0) {
1374 #if defined(OPENSSL_SYS_WINDOWS)
1376 #elif defined(OPENSSL_SYS_UNIX)
1377 int select_result = 0;
1378 OSSL_ASYNC_FD max_fd = 0;
1381 FD_ZERO(&waitfdset);
1383 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1384 if (loopargs[i].inprogress_job == NULL)
1387 if (!ASYNC_WAIT_CTX_get_all_fds
1388 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1389 || num_job_fds > 1) {
1390 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1391 ERR_print_errors(bio_err);
1395 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1397 FD_SET(job_fd, &waitfdset);
1398 if (job_fd > max_fd)
1402 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1404 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1405 "Decrease the value of async_jobs\n",
1406 max_fd, FD_SETSIZE);
1407 ERR_print_errors(bio_err);
1412 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1413 if (select_result == -1 && errno == EINTR)
1416 if (select_result == -1) {
1417 BIO_printf(bio_err, "Failure in the select\n");
1418 ERR_print_errors(bio_err);
1423 if (select_result == 0)
1427 for (i = 0; i < async_jobs; i++) {
1428 if (loopargs[i].inprogress_job == NULL)
1431 if (!ASYNC_WAIT_CTX_get_all_fds
1432 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1433 || num_job_fds > 1) {
1434 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1435 ERR_print_errors(bio_err);
1439 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1442 #if defined(OPENSSL_SYS_UNIX)
1443 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1445 #elif defined(OPENSSL_SYS_WINDOWS)
1446 if (num_job_fds == 1
1447 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
1452 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1453 loopargs[i].wait_ctx, &job_op_count,
1454 loop_function, (void *)(loopargs + i),
1455 sizeof(loopargs_t));
1460 if (job_op_count == -1) {
1463 total_op_count += job_op_count;
1466 loopargs[i].inprogress_job = NULL;
1471 loopargs[i].inprogress_job = NULL;
1472 BIO_printf(bio_err, "Failure in the job\n");
1473 ERR_print_errors(bio_err);
1480 return error ? -1 : total_op_count;
1483 #define stop_it(do_it, test_num)\
1484 memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
1486 int speed_main(int argc, char **argv)
1489 loopargs_t *loopargs = NULL;
1491 const char *engine_id = NULL;
1492 const EVP_CIPHER *evp_cipher = NULL;
1495 int async_init = 0, multiblock = 0, pr_header = 0;
1496 uint8_t doit[ALGOR_NUM] = { 0 };
1497 int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
1499 unsigned int size_num = SIZE_NUM;
1500 unsigned int i, k, loopargs_len = 0, async_jobs = 0;
1506 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
1507 || !defined(OPENSSL_NO_EC)
1510 openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
1511 ECDSA_SECONDS, ECDH_SECONDS,
1512 EdDSA_SECONDS, SM2_SECONDS,
1515 /* What follows are the buffers and key material. */
1516 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1519 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1522 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1523 IDEA_KEY_SCHEDULE idea_ks;
1525 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1526 SEED_KEY_SCHEDULE seed_ks;
1528 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1531 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1534 #ifndef OPENSSL_NO_DEPRECATED_3_0
1535 static const unsigned char key16[16] = {
1536 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1537 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1539 static const unsigned char key24[24] = {
1540 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1541 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1542 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1544 static const unsigned char key32[32] = {
1545 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1546 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1547 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1548 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1551 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1552 CAMELLIA_KEY camellia_ks[3];
1554 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1555 static const struct {
1556 const unsigned char *data;
1557 unsigned int length;
1560 { test512, sizeof(test512), 512 },
1561 { test1024, sizeof(test1024), 1024 },
1562 { test2048, sizeof(test2048), 2048 },
1563 { test3072, sizeof(test3072), 3072 },
1564 { test4096, sizeof(test4096), 4092 },
1565 { test7680, sizeof(test7680), 7680 },
1566 { test15360, sizeof(test15360), 15360 }
1568 uint8_t rsa_doit[RSA_NUM] = { 0 };
1569 int primes = RSA_DEFAULT_PRIME_NUM;
1571 #ifndef OPENSSL_NO_DH
1572 typedef struct ffdh_params_st {
1578 static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
1579 {"ffdh2048", NID_ffdhe2048, 2048},
1580 {"ffdh3072", NID_ffdhe3072, 3072},
1581 {"ffdh4096", NID_ffdhe4096, 4096},
1582 {"ffdh6144", NID_ffdhe6144, 6144},
1583 {"ffdh8192", NID_ffdhe8192, 8192}
1585 uint8_t ffdh_doit[FFDH_NUM] = { 0 };
1587 #endif /* OPENSSL_NO_DH */
1588 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1589 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1590 uint8_t dsa_doit[DSA_NUM] = { 0 };
1592 #ifndef OPENSSL_NO_EC
1593 typedef struct ec_curve_st {
1597 size_t sigsize; /* only used for EdDSA curves */
1600 * We only test over the following curves as they are representative, To
1601 * add tests over more curves, simply add the curve NID and curve name to
1602 * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
1603 * lists accordingly.
1605 static const EC_CURVE ec_curves[EC_NUM] = {
1607 {"secp160r1", NID_secp160r1, 160},
1608 {"nistp192", NID_X9_62_prime192v1, 192},
1609 {"nistp224", NID_secp224r1, 224},
1610 {"nistp256", NID_X9_62_prime256v1, 256},
1611 {"nistp384", NID_secp384r1, 384},
1612 {"nistp521", NID_secp521r1, 521},
1613 # ifndef OPENSSL_NO_EC2M
1615 {"nistk163", NID_sect163k1, 163},
1616 {"nistk233", NID_sect233k1, 233},
1617 {"nistk283", NID_sect283k1, 283},
1618 {"nistk409", NID_sect409k1, 409},
1619 {"nistk571", NID_sect571k1, 571},
1620 {"nistb163", NID_sect163r2, 163},
1621 {"nistb233", NID_sect233r1, 233},
1622 {"nistb283", NID_sect283r1, 283},
1623 {"nistb409", NID_sect409r1, 409},
1624 {"nistb571", NID_sect571r1, 571},
1626 {"brainpoolP256r1", NID_brainpoolP256r1, 256},
1627 {"brainpoolP256t1", NID_brainpoolP256t1, 256},
1628 {"brainpoolP384r1", NID_brainpoolP384r1, 384},
1629 {"brainpoolP384t1", NID_brainpoolP384t1, 384},
1630 {"brainpoolP512r1", NID_brainpoolP512r1, 512},
1631 {"brainpoolP512t1", NID_brainpoolP512t1, 512},
1632 /* Other and ECDH only ones */
1633 {"X25519", NID_X25519, 253},
1634 {"X448", NID_X448, 448}
1636 static const EC_CURVE ed_curves[EdDSA_NUM] = {
1638 {"Ed25519", NID_ED25519, 253, 64},
1639 {"Ed448", NID_ED448, 456, 114}
1641 # ifndef OPENSSL_NO_SM2
1642 static const EC_CURVE sm2_curves[SM2_NUM] = {
1644 {"CurveSM2", NID_sm2, 256}
1646 uint8_t sm2_doit[SM2_NUM] = { 0 };
1648 uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
1649 uint8_t ecdh_doit[EC_NUM] = { 0 };
1650 uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
1652 /* checks declarated curves against choices list. */
1653 OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
1654 OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
1656 OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
1657 OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
1659 OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
1660 OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
1662 # ifndef OPENSSL_NO_SM2
1663 OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
1664 OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
1666 #endif /* ndef OPENSSL_NO_EC */
1668 prog = opt_init(argc, argv, speed_options);
1669 while ((o = opt_next()) != OPT_EOF) {
1674 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1677 opt_help(speed_options);
1685 evp_cipher = EVP_get_cipherbyname(opt_arg());
1686 if (evp_cipher == NULL)
1687 evp_md = EVP_get_digestbyname(opt_arg());
1688 if (evp_cipher == NULL && evp_md == NULL) {
1690 "%s: %s is an unknown cipher or digest\n",
1697 #ifndef OPENSSL_NO_DEPRECATED_3_0
1698 evp_hmac_md = EVP_get_digestbyname(opt_arg());
1699 if (evp_hmac_md == NULL) {
1700 BIO_printf(bio_err, "%s: %s is an unknown digest\n",
1704 doit[D_EVP_HMAC] = 1;
1708 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1709 evp_cmac_cipher = EVP_get_cipherbyname(opt_arg());
1710 if (evp_cmac_cipher == NULL) {
1711 BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
1715 doit[D_EVP_CMAC] = 1;
1723 * In a forked execution, an engine might need to be
1724 * initialised by each child process, not by the parent.
1725 * So store the name here and run setup_engine() later on.
1727 engine_id = opt_arg();
1731 multi = atoi(opt_arg());
1735 #ifndef OPENSSL_NO_ASYNC
1736 async_jobs = atoi(opt_arg());
1737 if (!ASYNC_is_capable()) {
1739 "%s: async_jobs specified but async not supported\n",
1743 if (async_jobs > 99999) {
1744 BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
1750 if (!opt_int(opt_arg(), &misalign))
1752 if (misalign > MISALIGN) {
1754 "%s: Maximum offset is %d\n", prog, MISALIGN);
1763 #ifdef OPENSSL_NO_MULTIBLOCK
1765 "%s: -mb specified but multi-block support is disabled\n",
1774 case OPT_PROV_CASES:
1775 if (!opt_provider(o))
1779 #ifndef OPENSSL_NO_DEPRECATED_3_0
1780 if (!opt_int(opt_arg(), &primes))
1785 seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1786 = seconds.ecdh = seconds.eddsa
1787 = seconds.sm2 = seconds.ffdh = atoi(opt_arg());
1790 lengths_single = atoi(opt_arg());
1791 lengths = &lengths_single;
1799 argc = opt_num_rest();
1802 /* Remaining arguments are algorithms. */
1803 for (; *argv; argv++) {
1804 const char *algo = *argv;
1806 if (opt_found(algo, doit_choices, &i)) {
1810 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1811 if (strcmp(algo, "des") == 0) {
1812 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1816 if (strcmp(algo, "sha") == 0) {
1817 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1820 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1821 if (strcmp(algo, "openssl") == 0) /* just for compatibility */
1823 if (strncmp(algo, "rsa", 3) == 0) {
1824 if (algo[3] == '\0') {
1825 memset(rsa_doit, 1, sizeof(rsa_doit));
1828 if (opt_found(algo, rsa_choices, &i)) {
1834 #ifndef OPENSSL_NO_DH
1835 if (strncmp(algo, "ffdh", 4) == 0) {
1836 if (algo[4] == '\0') {
1837 memset(ffdh_doit, 1, sizeof(ffdh_doit));
1840 if (opt_found(algo, ffdh_choices, &i)) {
1846 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1847 if (strncmp(algo, "dsa", 3) == 0) {
1848 if (algo[3] == '\0') {
1849 memset(dsa_doit, 1, sizeof(dsa_doit));
1852 if (opt_found(algo, dsa_choices, &i)) {
1858 #ifndef OPENSSL_NO_DEPRECATED_3_0
1859 if (strcmp(algo, "aes") == 0) {
1860 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1864 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1865 if (strcmp(algo, "camellia") == 0) {
1866 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1870 #ifndef OPENSSL_NO_EC
1871 if (strncmp(algo, "ecdsa", 5) == 0) {
1872 if (algo[5] == '\0') {
1873 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
1876 if (opt_found(algo, ecdsa_choices, &i)) {
1881 if (strncmp(algo, "ecdh", 4) == 0) {
1882 if (algo[4] == '\0') {
1883 memset(ecdh_doit, 1, sizeof(ecdh_doit));
1886 if (opt_found(algo, ecdh_choices, &i)) {
1891 if (strcmp(algo, "eddsa") == 0) {
1892 memset(eddsa_doit, 1, sizeof(eddsa_doit));
1895 if (opt_found(algo, eddsa_choices, &i)) {
1899 # ifndef OPENSSL_NO_SM2
1900 if (strcmp(algo, "sm2") == 0) {
1901 memset(sm2_doit, 1, sizeof(sm2_doit));
1904 if (opt_found(algo, sm2_choices, &i)) {
1909 #endif /* OPENSSL_NO_EC */
1910 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
1916 if (evp_cipher == NULL) {
1917 BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
1919 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1920 EVP_CIPH_FLAG_AEAD_CIPHER)) {
1921 BIO_printf(bio_err, "%s is not an AEAD cipher\n",
1922 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1927 if (evp_cipher == NULL) {
1928 BIO_printf(bio_err,"-mb can be used only with a multi-block"
1929 " capable cipher\n");
1931 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1932 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1933 BIO_printf(bio_err, "%s is not a multi-block capable\n",
1934 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1936 } else if (async_jobs > 0) {
1937 BIO_printf(bio_err, "Async mode is not supported with -mb");
1942 /* Initialize the job pool if async mode is enabled */
1943 if (async_jobs > 0) {
1944 async_init = ASYNC_init_thread(async_jobs, async_jobs);
1946 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1951 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1953 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1954 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1956 for (i = 0; i < loopargs_len; i++) {
1957 if (async_jobs > 0) {
1958 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1959 if (loopargs[i].wait_ctx == NULL) {
1960 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1965 buflen = lengths[size_num - 1];
1966 if (buflen < 36) /* size of random vector in RSA benchmark */
1968 buflen += MAX_MISALIGNMENT + 1;
1969 loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
1970 loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
1971 memset(loopargs[i].buf_malloc, 0, buflen);
1972 memset(loopargs[i].buf2_malloc, 0, buflen);
1974 /* Align the start of buffers on a 64 byte boundary */
1975 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1976 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1977 #ifndef OPENSSL_NO_EC
1978 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1979 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1981 #ifndef OPENSSL_NO_DH
1982 loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
1983 loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
1988 if (multi && do_multi(multi, size_num))
1992 /* Initialize the engine after the fork */
1993 e = setup_engine(engine_id, 0);
1995 /* No parameters; turn on everything. */
1996 if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC] && !doit[D_EVP_CMAC]) {
1997 memset(doit, 1, sizeof(doit));
1998 doit[D_EVP] = doit[D_EVP_HMAC] = doit[D_EVP_CMAC] = 0;
1999 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2002 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2005 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2008 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2009 memset(rsa_doit, 1, sizeof(rsa_doit));
2011 #ifndef OPENSSL_NO_DH
2012 memset(ffdh_doit, 1, sizeof(ffdh_doit));
2014 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2015 memset(dsa_doit, 1, sizeof(dsa_doit));
2017 #ifndef OPENSSL_NO_EC
2018 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
2019 memset(ecdh_doit, 1, sizeof(ecdh_doit));
2020 memset(eddsa_doit, 1, sizeof(eddsa_doit));
2021 # ifndef OPENSSL_NO_SM2
2022 memset(sm2_doit, 1, sizeof(sm2_doit));
2026 for (i = 0; i < ALGOR_NUM; i++)
2030 if (usertime == 0 && !mr)
2032 "You have chosen to measure elapsed time "
2033 "instead of user CPU time.\n");
2035 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2036 for (i = 0; i < loopargs_len; i++) {
2037 if (primes > RSA_DEFAULT_PRIME_NUM) {
2038 /* for multi-prime RSA, skip this */
2041 for (k = 0; k < RSA_NUM; k++) {
2042 const unsigned char *p = rsa_keys[k].data;
2044 loopargs[i].rsa_key[k] =
2045 d2i_RSAPrivateKey(NULL, &p, rsa_keys[k].length);
2046 if (loopargs[i].rsa_key[k] == NULL) {
2048 "internal error loading RSA key number %d\n", k);
2054 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2055 for (i = 0; i < loopargs_len; i++) {
2056 loopargs[i].dsa_key[0] = get_dsa(512);
2057 loopargs[i].dsa_key[1] = get_dsa(1024);
2058 loopargs[i].dsa_key[2] = get_dsa(2048);
2061 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2062 if (doit[D_CBC_DES] || doit[D_EDE3_DES]) {
2063 static DES_cblock keys[] = {
2064 { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }, /* keys[0] */
2065 { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }, /* keys[1] */
2066 { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 } /* keys[3] */
2068 DES_set_key_unchecked(&keys[0], &sch[0]);
2069 DES_set_key_unchecked(&keys[1], &sch[1]);
2070 DES_set_key_unchecked(&keys[2], &sch[2]);
2073 #ifndef OPENSSL_NO_DEPRECATED_3_0
2074 AES_set_encrypt_key(key16, 128, &aes_ks1);
2075 AES_set_encrypt_key(key24, 192, &aes_ks2);
2076 AES_set_encrypt_key(key32, 256, &aes_ks3);
2078 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2079 if (doit[D_CBC_128_CML] || doit[D_CBC_192_CML] || doit[D_CBC_256_CML]) {
2080 Camellia_set_key(key16, 128, &camellia_ks[0]);
2081 Camellia_set_key(key24, 192, &camellia_ks[1]);
2082 Camellia_set_key(key32, 256, &camellia_ks[2]);
2085 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2086 if (doit[D_CBC_IDEA])
2087 IDEA_set_encrypt_key(key16, &idea_ks);
2089 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2090 if (doit[D_CBC_SEED])
2091 SEED_set_key(key16, &seed_ks);
2093 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2095 RC4_set_key(&rc4_ks, 16, key16);
2097 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2098 if (doit[D_CBC_RC2])
2099 RC2_set_key(&rc2_ks, 16, key16, 128);
2101 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2102 if (doit[D_CBC_RC5])
2103 if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
2104 BIO_printf(bio_err, "Failed setting RC5 key\n");
2108 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2110 BF_set_key(&bf_ks, 16, key16);
2112 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2113 if (doit[D_CBC_CAST])
2114 CAST_set_key(&cast_ks, 16, key16);
2117 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2118 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
2124 for (it = count; it; it--)
2125 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
2126 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
2129 c[D_MD2][0] = count / 10;
2130 c[D_MDC2][0] = count / 10;
2131 c[D_MD4][0] = count;
2132 c[D_MD5][0] = count;
2133 c[D_HMAC][0] = count;
2134 c[D_SHA1][0] = count;
2135 c[D_RMD160][0] = count;
2136 c[D_RC4][0] = count * 5;
2137 c[D_CBC_DES][0] = count;
2138 c[D_EDE3_DES][0] = count / 3;
2139 c[D_CBC_IDEA][0] = count;
2140 c[D_CBC_SEED][0] = count;
2141 c[D_CBC_RC2][0] = count;
2142 c[D_CBC_RC5][0] = count;
2143 c[D_CBC_BF][0] = count;
2144 c[D_CBC_CAST][0] = count;
2145 c[D_CBC_128_AES][0] = count;
2146 c[D_CBC_192_AES][0] = count;
2147 c[D_CBC_256_AES][0] = count;
2148 c[D_CBC_128_CML][0] = count;
2149 c[D_CBC_192_CML][0] = count;
2150 c[D_CBC_256_CML][0] = count;
2151 c[D_EVP][0] = count;
2152 c[D_SHA256][0] = count;
2153 c[D_SHA512][0] = count;
2154 c[D_WHIRLPOOL][0] = count;
2155 c[D_IGE_128_AES][0] = count;
2156 c[D_IGE_192_AES][0] = count;
2157 c[D_IGE_256_AES][0] = count;
2158 c[D_GHASH][0] = count;
2159 c[D_RAND][0] = count;
2160 c[D_EVP_HMAC][0] = count;
2161 c[D_EVP_CMAC][0] = count;
2163 for (i = 1; i < size_num; i++) {
2164 long l0 = (long)lengths[0];
2165 long l1 = (long)lengths[i];
2167 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
2168 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
2169 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
2170 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
2171 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
2172 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
2173 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
2174 c[D_EVP][i] = = c[D_EVP][0] * 4 * l0 / l1;
2175 c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
2176 c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
2177 c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
2178 c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
2179 c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
2180 c[D_EVP_HMAC][i] = = c[D_EVP_HMAC][0] * 4 * l0 / l1;
2181 c[D_EVP_CMAC][i] = = c[D_EVP_CMAC][0] * 4 * l0 / l1;
2183 l0 = (long)lengths[i - 1];
2185 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
2186 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
2187 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
2188 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
2189 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
2190 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
2191 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
2192 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
2193 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
2194 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
2195 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
2196 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
2197 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
2198 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
2199 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
2200 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
2201 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
2202 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
2205 # if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2206 rsa_c[R_RSA_512][0] = count / 2000;
2207 rsa_c[R_RSA_512][1] = count / 400;
2208 for (i = 1; i < RSA_NUM; i++) {
2209 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
2210 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
2211 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
2214 if (rsa_c[i][0] == 0) {
2215 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2222 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2223 dsa_c[R_DSA_512][0] = count / 1000;
2224 dsa_c[R_DSA_512][1] = count / 1000 / 2;
2225 for (i = 1; i < DSA_NUM; i++) {
2226 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
2227 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
2228 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
2231 if (dsa_c[i][0] == 0) {
2232 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2239 # ifndef OPENSSL_NO_EC
2240 ecdsa_c[R_EC_P160][0] = count / 1000;
2241 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
2242 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2243 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2244 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2245 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2248 if (ecdsa_c[i][0] == 0) {
2254 # ifndef OPENSSL_NO_EC2M
2255 ecdsa_c[R_EC_K163][0] = count / 1000;
2256 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
2257 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2258 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2259 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2260 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2263 if (ecdsa_c[i][0] == 0) {
2269 ecdsa_c[R_EC_B163][0] = count / 1000;
2270 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
2271 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2272 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2273 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
2274 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2277 if (ecdsa_c[i][0] == 0) {
2285 ecdh_c[R_EC_P160][0] = count / 1000;
2286 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2287 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2288 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2291 if (ecdh_c[i][0] == 0) {
2296 # ifndef OPENSSL_NO_EC2M
2297 ecdh_c[R_EC_K163][0] = count / 1000;
2298 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2299 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
2300 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2303 if (ecdh_c[i][0] == 0) {
2308 ecdh_c[R_EC_B163][0] = count / 1000;
2309 for (i = R_EC_B233; i <= R_EC_B571; 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) {
2320 /* repeated code good to factorize */
2321 ecdh_c[R_EC_BRP256R1][0] = count / 1000;
2322 for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
2323 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2324 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2327 if (ecdh_c[i][0] == 0) {
2332 ecdh_c[R_EC_BRP256T1][0] = count / 1000;
2333 for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; 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 /* default iteration count for the last two EC Curves */
2344 ecdh_c[R_EC_X25519][0] = count / 1800;
2345 ecdh_c[R_EC_X448][0] = count / 7200;
2347 eddsa_c[R_EC_Ed25519][0] = count / 1800;
2348 eddsa_c[R_EC_Ed448][0] = count / 7200;
2350 # ifndef OPENSSL_NO_SM2
2351 sm2_c[R_EC_SM2P256][0] = count / 1800;
2353 # endif /* OPENSSL_NO_EC */
2355 # ifndef OPENSSL_NO_DH
2356 ffdh_c[R_FFDH_2048][0] = count / 1000;
2357 for (i = R_FFDH_3072; i <= R_FFDH_8192; i++) {
2358 ffdh_c[i][0] = ffdh_c[i - 1][0] / 2;
2359 if (ffdh_doit[i] <= 1 && ffdh_c[i][0] == 0) {
2362 if (ffdh_c[i][0] == 0)
2366 # endif /* OPENSSL_NO_DH */
2369 /* not worth fixing */
2370 # error "You cannot disable DES on systems without SIGALRM."
2371 # endif /* OPENSSL_NO_DES */
2373 signal(SIGALRM, alarmed);
2374 #endif /* SIGALRM */
2376 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2378 for (testnum = 0; testnum < size_num; testnum++) {
2379 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
2382 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
2384 print_result(D_MD2, testnum, count, d);
2388 #if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2390 for (testnum = 0; testnum < size_num; testnum++) {
2391 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
2394 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
2396 print_result(D_MDC2, testnum, count, d);
2403 #if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2405 for (testnum = 0; testnum < size_num; testnum++) {
2406 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
2409 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
2411 print_result(D_MD4, testnum, count, d);
2418 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2420 for (testnum = 0; testnum < size_num; testnum++) {
2421 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
2424 count = run_benchmark(async_jobs, MD5_loop, loopargs);
2426 print_result(D_MD5, testnum, count, d);
2430 # ifndef OPENSSL_NO_DEPRECATED_3_0
2432 static const char hmac_key[] = "This is a key...";
2433 int len = strlen(hmac_key);
2435 for (i = 0; i < loopargs_len; i++) {
2436 loopargs[i].hctx = HMAC_CTX_new();
2437 if (loopargs[i].hctx == NULL) {
2438 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2442 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2444 for (testnum = 0; testnum < size_num; testnum++) {
2445 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
2448 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2450 print_result(D_HMAC, testnum, count, d);
2452 for (i = 0; i < loopargs_len; i++)
2453 HMAC_CTX_free(loopargs[i].hctx);
2457 #ifndef OPENSSL_NO_DEPRECATED_3_0
2459 for (testnum = 0; testnum < size_num; testnum++) {
2460 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2463 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2465 print_result(D_SHA1, testnum, count, d);
2468 if (doit[D_SHA256]) {
2469 for (testnum = 0; testnum < size_num; testnum++) {
2470 print_message(names[D_SHA256], c[D_SHA256][testnum],
2471 lengths[testnum], seconds.sym);
2473 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2475 print_result(D_SHA256, testnum, count, d);
2478 if (doit[D_SHA512]) {
2479 for (testnum = 0; testnum < size_num; testnum++) {
2480 print_message(names[D_SHA512], c[D_SHA512][testnum],
2481 lengths[testnum], seconds.sym);
2483 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2485 print_result(D_SHA512, testnum, count, d);
2489 #if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2490 if (doit[D_WHIRLPOOL]) {
2491 for (testnum = 0; testnum < size_num; testnum++) {
2492 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2493 lengths[testnum], seconds.sym);
2495 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2497 print_result(D_WHIRLPOOL, testnum, count, d);
2502 #if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2503 if (doit[D_RMD160]) {
2504 for (testnum = 0; testnum < size_num; testnum++) {
2505 print_message(names[D_RMD160], c[D_RMD160][testnum],
2506 lengths[testnum], seconds.sym);
2508 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2510 print_result(D_RMD160, testnum, count, d);
2516 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2518 for (testnum = 0; testnum < size_num; testnum++) {
2519 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2522 count = run_benchmark(async_jobs, RC4_loop, loopargs);
2524 print_result(D_RC4, testnum, count, d);
2528 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2529 if (doit[D_CBC_DES]) {
2530 for (testnum = 0; testnum < size_num; testnum++) {
2531 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2532 lengths[testnum], seconds.sym);
2534 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2536 print_result(D_CBC_DES, testnum, count, d);
2540 if (doit[D_EDE3_DES]) {
2541 for (testnum = 0; testnum < size_num; testnum++) {
2542 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2543 lengths[testnum], seconds.sym);
2546 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2548 print_result(D_EDE3_DES, testnum, count, d);
2553 #ifndef OPENSSL_NO_DEPRECATED_3_0
2554 if (doit[D_CBC_128_AES]) {
2555 for (testnum = 0; testnum < size_num; testnum++) {
2556 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2557 lengths[testnum], seconds.sym);
2560 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2562 print_result(D_CBC_128_AES, testnum, count, d);
2565 if (doit[D_CBC_192_AES]) {
2566 for (testnum = 0; testnum < size_num; testnum++) {
2567 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2568 lengths[testnum], seconds.sym);
2571 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2573 print_result(D_CBC_192_AES, testnum, count, d);
2576 if (doit[D_CBC_256_AES]) {
2577 for (testnum = 0; testnum < size_num; testnum++) {
2578 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2579 lengths[testnum], seconds.sym);
2582 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2584 print_result(D_CBC_256_AES, testnum, count, d);
2589 if (doit[D_IGE_128_AES]) {
2590 for (testnum = 0; testnum < size_num; testnum++) {
2591 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2592 lengths[testnum], seconds.sym);
2595 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2597 print_result(D_IGE_128_AES, testnum, count, d);
2600 if (doit[D_IGE_192_AES]) {
2601 for (testnum = 0; testnum < size_num; testnum++) {
2602 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2603 lengths[testnum], seconds.sym);
2606 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2608 print_result(D_IGE_192_AES, testnum, count, d);
2611 if (doit[D_IGE_256_AES]) {
2612 for (testnum = 0; testnum < size_num; testnum++) {
2613 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2614 lengths[testnum], seconds.sym);
2617 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2619 print_result(D_IGE_256_AES, testnum, count, d);
2622 if (doit[D_GHASH]) {
2623 for (i = 0; i < loopargs_len; i++) {
2624 loopargs[i].gcm_ctx =
2625 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2626 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2627 (unsigned char *)"0123456789ab", 12);
2630 for (testnum = 0; testnum < size_num; testnum++) {
2631 print_message(names[D_GHASH], c[D_GHASH][testnum],
2632 lengths[testnum], seconds.sym);
2634 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2636 print_result(D_GHASH, testnum, count, d);
2638 for (i = 0; i < loopargs_len; i++)
2639 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2641 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
2642 #if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2643 if (doit[D_CBC_128_CML]) {
2644 if (async_jobs > 0) {
2645 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2646 names[D_CBC_128_CML]);
2647 doit[D_CBC_128_CML] = 0;
2649 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2650 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2651 lengths[testnum], seconds.sym);
2653 for (count = 0; COND(c[D_CBC_128_CML][testnum]); count++)
2654 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2655 (size_t)lengths[testnum], &camellia_ks[0],
2656 iv, CAMELLIA_ENCRYPT);
2658 print_result(D_CBC_128_CML, testnum, count, d);
2661 if (doit[D_CBC_192_CML]) {
2662 if (async_jobs > 0) {
2663 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2664 names[D_CBC_192_CML]);
2665 doit[D_CBC_192_CML] = 0;
2667 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2668 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2669 lengths[testnum], seconds.sym);
2670 if (async_jobs > 0) {
2671 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2675 for (count = 0; COND(c[D_CBC_192_CML][testnum]); count++)
2676 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2677 (size_t)lengths[testnum], &camellia_ks[1],
2678 iv, CAMELLIA_ENCRYPT);
2680 print_result(D_CBC_192_CML, testnum, count, d);
2683 if (doit[D_CBC_256_CML]) {
2684 if (async_jobs > 0) {
2685 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2686 names[D_CBC_256_CML]);
2687 doit[D_CBC_256_CML] = 0;
2689 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2690 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2691 lengths[testnum], seconds.sym);
2693 for (count = 0; COND(c[D_CBC_256_CML][testnum]); count++)
2694 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2695 (size_t)lengths[testnum], &camellia_ks[2],
2696 iv, CAMELLIA_ENCRYPT);
2698 print_result(D_CBC_256_CML, testnum, count, d);
2702 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2703 if (doit[D_CBC_IDEA]) {
2704 if (async_jobs > 0) {
2705 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2707 doit[D_CBC_IDEA] = 0;
2709 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2710 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2711 lengths[testnum], seconds.sym);
2713 for (count = 0; COND(c[D_CBC_IDEA][testnum]); count++)
2714 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2715 (size_t)lengths[testnum], &idea_ks,
2718 print_result(D_CBC_IDEA, testnum, count, d);
2722 #if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2723 if (doit[D_CBC_SEED]) {
2724 if (async_jobs > 0) {
2725 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2727 doit[D_CBC_SEED] = 0;
2729 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2730 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2731 lengths[testnum], seconds.sym);
2733 for (count = 0; COND(c[D_CBC_SEED][testnum]); count++)
2734 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2735 (size_t)lengths[testnum], &seed_ks, iv, 1);
2737 print_result(D_CBC_SEED, testnum, count, d);
2741 #if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2742 if (doit[D_CBC_RC2]) {
2743 if (async_jobs > 0) {
2744 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2746 doit[D_CBC_RC2] = 0;
2748 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2749 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2750 lengths[testnum], seconds.sym);
2751 if (async_jobs > 0) {
2752 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2756 for (count = 0; COND(c[D_CBC_RC2][testnum]); count++)
2757 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2758 (size_t)lengths[testnum], &rc2_ks,
2761 print_result(D_CBC_RC2, testnum, count, d);
2765 #if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2766 if (doit[D_CBC_RC5]) {
2767 if (async_jobs > 0) {
2768 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2770 doit[D_CBC_RC5] = 0;
2772 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2773 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2774 lengths[testnum], seconds.sym);
2775 if (async_jobs > 0) {
2776 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2780 for (count = 0; COND(c[D_CBC_RC5][testnum]); count++)
2781 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2782 (size_t)lengths[testnum], &rc5_ks,
2785 print_result(D_CBC_RC5, testnum, count, d);
2789 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2790 if (doit[D_CBC_BF]) {
2791 if (async_jobs > 0) {
2792 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2796 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2797 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2798 lengths[testnum], seconds.sym);
2800 for (count = 0; COND(c[D_CBC_BF][testnum]); count++)
2801 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2802 (size_t)lengths[testnum], &bf_ks,
2805 print_result(D_CBC_BF, testnum, count, d);
2809 #if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2810 if (doit[D_CBC_CAST]) {
2811 if (async_jobs > 0) {
2812 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2814 doit[D_CBC_CAST] = 0;
2816 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2817 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2818 lengths[testnum], seconds.sym);
2820 for (count = 0; COND(c[D_CBC_CAST][testnum]); count++)
2821 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2822 (size_t)lengths[testnum], &cast_ks,
2825 print_result(D_CBC_CAST, testnum, count, d);
2830 for (testnum = 0; testnum < size_num; testnum++) {
2831 print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2834 count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2836 print_result(D_RAND, testnum, count, d);
2841 if (evp_cipher != NULL) {
2842 int (*loopfunc) (void *) = EVP_Update_loop;
2844 if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
2845 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2846 multiblock_speed(evp_cipher, lengths_single, &seconds);
2851 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2853 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
2854 loopfunc = EVP_Update_loop_ccm;
2855 } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
2856 EVP_CIPH_FLAG_AEAD_CIPHER)) {
2857 loopfunc = EVP_Update_loop_aead;
2858 if (lengths == lengths_list) {
2859 lengths = aead_lengths_list;
2860 size_num = OSSL_NELEM(aead_lengths_list);
2864 for (testnum = 0; testnum < size_num; testnum++) {
2865 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2868 for (k = 0; k < loopargs_len; k++) {
2869 loopargs[k].ctx = EVP_CIPHER_CTX_new();
2870 if (loopargs[k].ctx == NULL) {
2871 BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
2874 if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2875 NULL, iv, decrypt ? 0 : 1)) {
2876 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2877 ERR_print_errors(bio_err);
2881 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2883 keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
2884 loopargs[k].key = app_malloc(keylen, "evp_cipher key");
2885 EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
2886 if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
2887 loopargs[k].key, NULL, -1)) {
2888 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2889 ERR_print_errors(bio_err);
2892 OPENSSL_clear_free(loopargs[k].key, keylen);
2894 /* SIV mode only allows for a single Update operation */
2895 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
2896 EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_SET_SPEED, 1, NULL);
2900 count = run_benchmark(async_jobs, loopfunc, loopargs);
2902 for (k = 0; k < loopargs_len; k++) {
2903 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2905 print_result(D_EVP, testnum, count, d);
2907 } else if (evp_md != NULL) {
2908 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2910 for (testnum = 0; testnum < size_num; testnum++) {
2911 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
2914 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2916 print_result(D_EVP, testnum, count, d);
2921 #ifndef OPENSSL_NO_DEPRECATED_3_0
2922 if (doit[D_EVP_HMAC] && evp_hmac_md != NULL) {
2923 const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
2925 evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
2927 sprintf(evp_hmac_name, "HMAC(%s)", md_name);
2928 names[D_EVP_HMAC] = evp_hmac_name;
2930 for (testnum = 0; testnum < size_num; testnum++) {
2931 print_message(names[D_EVP_HMAC], c[D_EVP_HMAC][testnum], lengths[testnum],
2934 count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
2936 print_result(D_EVP_HMAC, testnum, count, d);
2941 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2942 if (doit[D_EVP_CMAC] && evp_cmac_cipher != NULL) {
2943 const char *cipher_name = OBJ_nid2ln(EVP_CIPHER_type(evp_cmac_cipher));
2945 evp_cmac_name = app_malloc(sizeof("CMAC()") + strlen(cipher_name),
2947 sprintf(evp_cmac_name, "CMAC(%s)", cipher_name);
2948 names[D_EVP_CMAC] = evp_cmac_name;
2950 for (i = 0; i < loopargs_len; i++) {
2951 loopargs[i].cmac_ctx = CMAC_CTX_new();
2952 if (loopargs[i].cmac_ctx == NULL) {
2953 BIO_printf(bio_err, "CMAC malloc failure, exiting...");
2957 for (testnum = 0; testnum < size_num; testnum++) {
2958 print_message(names[D_EVP_CMAC], c[D_EVP_CMAC][testnum], lengths[testnum],
2961 count = run_benchmark(async_jobs, EVP_CMAC_loop, loopargs);
2963 print_result(D_EVP_CMAC, testnum, count, d);
2965 for (i = 0; i < loopargs_len; i++)
2966 CMAC_CTX_free(loopargs[i].cmac_ctx);
2970 for (i = 0; i < loopargs_len; i++)
2971 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
2974 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
2975 for (testnum = 0; testnum < RSA_NUM; testnum++) {
2977 if (!rsa_doit[testnum])
2979 for (i = 0; i < loopargs_len; i++) {
2980 if (primes > RSA_DEFAULT_PRIME_NUM) {
2981 /* we haven't set keys yet, generate multi-prime RSA keys */
2982 BIGNUM *bn = BN_new();
2986 if (!BN_set_word(bn, RSA_F4)) {
2991 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
2992 rsa_choices[testnum].name);
2994 loopargs[i].rsa_key[testnum] = RSA_new();
2995 if (loopargs[i].rsa_key[testnum] == NULL) {
3000 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
3001 rsa_keys[testnum].bits,
3002 primes, bn, NULL)) {
3008 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3009 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3015 "RSA sign failure. No RSA sign will be done.\n");
3016 ERR_print_errors(bio_err);
3019 pkey_print_message("private", "rsa",
3020 rsa_c[testnum][0], rsa_keys[testnum].bits,
3022 /* RSA_blinding_on(rsa_key[testnum],NULL); */
3024 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
3027 mr ? "+R1:%ld:%d:%.2f\n"
3028 : "%ld %u bits private RSA's in %.2fs\n",
3029 count, rsa_keys[testnum].bits, d);
3030 rsa_results[testnum][0] = (double)count / d;
3034 for (i = 0; i < loopargs_len; i++) {
3035 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
3036 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
3042 "RSA verify failure. No RSA verify will be done.\n");
3043 ERR_print_errors(bio_err);
3044 rsa_doit[testnum] = 0;
3046 pkey_print_message("public", "rsa",
3047 rsa_c[testnum][1], rsa_keys[testnum].bits,
3050 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
3053 mr ? "+R2:%ld:%d:%.2f\n"
3054 : "%ld %u bits public RSA's in %.2fs\n",
3055 count, rsa_keys[testnum].bits, d);
3056 rsa_results[testnum][1] = (double)count / d;
3059 if (rsa_count <= 1) {
3060 /* if longer than 10s, don't do any more */
3061 stop_it(rsa_doit, testnum);
3064 #endif /* OPENSSL_NO_RSA */
3066 for (i = 0; i < loopargs_len; i++)
3067 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3070 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3071 for (testnum = 0; testnum < DSA_NUM; testnum++) {
3073 if (!dsa_doit[testnum])
3076 /* DSA_generate_key(dsa_key[testnum]); */
3077 /* DSA_sign_setup(dsa_key[testnum],NULL); */
3078 for (i = 0; i < loopargs_len; i++) {
3079 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3080 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3086 "DSA sign failure. No DSA sign will be done.\n");
3087 ERR_print_errors(bio_err);
3090 pkey_print_message("sign", "dsa",
3091 dsa_c[testnum][0], dsa_bits[testnum],
3094 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
3097 mr ? "+R3:%ld:%u:%.2f\n"
3098 : "%ld %u bits DSA signs in %.2fs\n",
3099 count, dsa_bits[testnum], d);
3100 dsa_results[testnum][0] = (double)count / d;
3104 for (i = 0; i < loopargs_len; i++) {
3105 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3106 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
3112 "DSA verify failure. No DSA verify will be done.\n");
3113 ERR_print_errors(bio_err);
3114 dsa_doit[testnum] = 0;
3116 pkey_print_message("verify", "dsa",
3117 dsa_c[testnum][1], dsa_bits[testnum],
3120 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
3123 mr ? "+R4:%ld:%u:%.2f\n"
3124 : "%ld %u bits DSA verify in %.2fs\n",
3125 count, dsa_bits[testnum], d);
3126 dsa_results[testnum][1] = (double)count / d;
3129 if (rsa_count <= 1) {
3130 /* if longer than 10s, don't do any more */
3131 stop_it(dsa_doit, testnum);
3134 #endif /* OPENSSL_NO_DSA */
3136 #ifndef OPENSSL_NO_EC
3137 # ifndef OPENSSL_NO_DEPRECATED_3_0
3138 for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
3141 if (!ecdsa_doit[testnum])
3142 continue; /* Ignore Curve */
3143 for (i = 0; i < loopargs_len; i++) {
3144 loopargs[i].ecdsa[testnum] =
3145 EC_KEY_new_by_curve_name(ec_curves[testnum].nid);
3146 if (loopargs[i].ecdsa[testnum] == NULL) {
3152 BIO_printf(bio_err, "ECDSA failure.\n");
3153 ERR_print_errors(bio_err);
3156 for (i = 0; i < loopargs_len; i++) {
3157 /* Perform ECDSA signature test */
3158 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
3159 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
3160 &loopargs[i].siglen,
3161 loopargs[i].ecdsa[testnum]);
3167 "ECDSA sign failure. No ECDSA sign will be done.\n");
3168 ERR_print_errors(bio_err);
3171 pkey_print_message("sign", "ecdsa",
3172 ecdsa_c[testnum][0],
3173 ec_curves[testnum].bits, seconds.ecdsa);
3175 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
3179 mr ? "+R5:%ld:%u:%.2f\n" :
3180 "%ld %u bits ECDSA signs in %.2fs \n",
3181 count, ec_curves[testnum].bits, d);
3182 ecdsa_results[testnum][0] = (double)count / d;
3186 /* Perform ECDSA verification test */
3187 for (i = 0; i < loopargs_len; i++) {
3188 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
3190 loopargs[i].ecdsa[testnum]);
3196 "ECDSA verify failure. No ECDSA verify will be done.\n");
3197 ERR_print_errors(bio_err);
3198 ecdsa_doit[testnum] = 0;
3200 pkey_print_message("verify", "ecdsa",
3201 ecdsa_c[testnum][1],
3202 ec_curves[testnum].bits, seconds.ecdsa);
3204 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
3207 mr ? "+R6:%ld:%u:%.2f\n"
3208 : "%ld %u bits ECDSA verify in %.2fs\n",
3209 count, ec_curves[testnum].bits, d);
3210 ecdsa_results[testnum][1] = (double)count / d;
3213 if (rsa_count <= 1) {
3214 /* if longer than 10s, don't do any more */
3215 stop_it(ecdsa_doit, testnum);
3221 for (testnum = 0; testnum < EC_NUM; testnum++) {
3222 int ecdh_checks = 1;
3224 if (!ecdh_doit[testnum])
3227 for (i = 0; i < loopargs_len; i++) {
3228 EVP_PKEY_CTX *kctx = NULL;
3229 EVP_PKEY_CTX *test_ctx = NULL;
3230 EVP_PKEY_CTX *ctx = NULL;
3231 EVP_PKEY *key_A = NULL;
3232 EVP_PKEY *key_B = NULL;
3236 /* Ensure that the error queue is empty */
3237 if (ERR_peek_error()) {
3239 "WARNING: the error queue contains previous unhandled errors.\n");
3240 ERR_print_errors(bio_err);
3243 /* Let's try to create a ctx directly from the NID: this works for
3244 * curves like Curve25519 that are not implemented through the low
3245 * level EC interface.
3246 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
3247 * then we set the curve by NID before deriving the actual keygen
3248 * ctx for that specific curve. */
3249 kctx = EVP_PKEY_CTX_new_id(ec_curves[testnum].nid, NULL); /* keygen ctx from NID */
3251 EVP_PKEY_CTX *pctx = NULL;
3252 EVP_PKEY *params = NULL;
3254 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
3255 * "int_ctx_new:unsupported algorithm" error was added to the
3257 * We remove it from the error queue as we are handling it. */
3258 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
3259 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
3260 /* check that the error origin matches */
3261 ERR_GET_LIB(error) == ERR_LIB_EVP &&
3262 ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
3263 ERR_get_error(); /* pop error from queue */
3264 if (ERR_peek_error()) {
3266 "Unhandled error in the error queue during ECDH init.\n");
3267 ERR_print_errors(bio_err);
3272 /* Create the context for parameter generation */
3273 if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
3274 /* Initialise the parameter generation */
3275 !EVP_PKEY_paramgen_init(pctx) ||
3276 /* Set the curve by NID */
3277 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3280 /* Create the parameter object params */
3281 !EVP_PKEY_paramgen(pctx, ¶ms)) {
3283 BIO_printf(bio_err, "ECDH EC params init failure.\n");
3284 ERR_print_errors(bio_err);
3288 /* Create the context for the key generation */
3289 kctx = EVP_PKEY_CTX_new(params, NULL);
3291 EVP_PKEY_free(params);
3293 EVP_PKEY_CTX_free(pctx);
3296 if (kctx == NULL || /* keygen ctx is not null */
3297 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
3299 BIO_printf(bio_err, "ECDH keygen failure.\n");
3300 ERR_print_errors(bio_err);
3305 if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
3306 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
3307 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
3308 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
3309 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
3310 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
3311 outlen == 0 || /* ensure outlen is a valid size */
3312 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
3314 BIO_printf(bio_err, "ECDH key generation failure.\n");
3315 ERR_print_errors(bio_err);
3320 /* Here we perform a test run, comparing the output of a*B and b*A;
3321 * we try this here and assume that further EVP_PKEY_derive calls
3322 * never fail, so we can skip checks in the actually benchmarked
3323 * code, for maximum performance. */
3324 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
3325 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
3326 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
3327 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
3328 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
3329 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
3330 test_outlen != outlen /* compare output length */ ) {
3332 BIO_printf(bio_err, "ECDH computation failure.\n");
3333 ERR_print_errors(bio_err);
3338 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
3339 if (CRYPTO_memcmp(loopargs[i].secret_a,
3340 loopargs[i].secret_b, outlen)) {
3342 BIO_printf(bio_err, "ECDH computations don't match.\n");
3343 ERR_print_errors(bio_err);
3348 loopargs[i].ecdh_ctx[testnum] = ctx;
3349 loopargs[i].outlen[testnum] = outlen;
3351 EVP_PKEY_free(key_A);
3352 EVP_PKEY_free(key_B);
3353 EVP_PKEY_CTX_free(kctx);
3355 EVP_PKEY_CTX_free(test_ctx);
3358 if (ecdh_checks != 0) {
3359 pkey_print_message("", "ecdh",
3361 ec_curves[testnum].bits, seconds.ecdh);
3364 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
3367 mr ? "+R7:%ld:%d:%.2f\n" :
3368 "%ld %u-bits ECDH ops in %.2fs\n", count,
3369 ec_curves[testnum].bits, d);
3370 ecdh_results[testnum][0] = (double)count / d;
3374 if (rsa_count <= 1) {
3375 /* if longer than 10s, don't do any more */
3376 stop_it(ecdh_doit, testnum);
3380 for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
3382 EVP_PKEY *ed_pkey = NULL;
3383 EVP_PKEY_CTX *ed_pctx = NULL;
3385 if (!eddsa_doit[testnum])
3386 continue; /* Ignore Curve */
3387 for (i = 0; i < loopargs_len; i++) {
3388 loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
3389 if (loopargs[i].eddsa_ctx[testnum] == NULL) {
3393 loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
3394 if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
3399 if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid, NULL))
3401 || EVP_PKEY_keygen_init(ed_pctx) <= 0
3402 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
3404 EVP_PKEY_CTX_free(ed_pctx);
3407 EVP_PKEY_CTX_free(ed_pctx);
3409 if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
3412 EVP_PKEY_free(ed_pkey);
3415 if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
3416 NULL, NULL, ed_pkey)) {
3418 EVP_PKEY_free(ed_pkey);
3422 EVP_PKEY_free(ed_pkey);
3425 BIO_printf(bio_err, "EdDSA failure.\n");
3426 ERR_print_errors(bio_err);
3429 for (i = 0; i < loopargs_len; i++) {
3430 /* Perform EdDSA signature test */
3431 loopargs[i].sigsize = ed_curves[testnum].sigsize;
3432 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
3433 loopargs[i].buf2, &loopargs[i].sigsize,
3434 loopargs[i].buf, 20);
3440 "EdDSA sign failure. No EdDSA sign will be done.\n");
3441 ERR_print_errors(bio_err);
3444 pkey_print_message("sign", ed_curves[testnum].name,
3445 eddsa_c[testnum][0],
3446 ed_curves[testnum].bits, seconds.eddsa);
3448 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
3452 mr ? "+R8:%ld:%u:%s:%.2f\n" :
3453 "%ld %u bits %s signs in %.2fs \n",
3454 count, ed_curves[testnum].bits,
3455 ed_curves[testnum].name, d);
3456 eddsa_results[testnum][0] = (double)count / d;
3459 /* Perform EdDSA verification test */
3460 for (i = 0; i < loopargs_len; i++) {
3461 st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
3462 loopargs[i].buf2, loopargs[i].sigsize,
3463 loopargs[i].buf, 20);
3469 "EdDSA verify failure. No EdDSA verify will be done.\n");
3470 ERR_print_errors(bio_err);
3471 eddsa_doit[testnum] = 0;
3473 pkey_print_message("verify", ed_curves[testnum].name,
3474 eddsa_c[testnum][1],
3475 ed_curves[testnum].bits, seconds.eddsa);
3477 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
3480 mr ? "+R9:%ld:%u:%s:%.2f\n"
3481 : "%ld %u bits %s verify in %.2fs\n",
3482 count, ed_curves[testnum].bits,
3483 ed_curves[testnum].name, d);
3484 eddsa_results[testnum][1] = (double)count / d;
3487 if (rsa_count <= 1) {
3488 /* if longer than 10s, don't do any more */
3489 stop_it(eddsa_doit, testnum);
3494 # ifndef OPENSSL_NO_SM2
3495 for (testnum = 0; testnum < SM2_NUM; testnum++) {
3497 EVP_PKEY *sm2_pkey = NULL;
3499 if (!sm2_doit[testnum])
3500 continue; /* Ignore Curve */
3501 /* Init signing and verification */
3502 for (i = 0; i < loopargs_len; i++) {
3503 EVP_PKEY_CTX *sm2_pctx = NULL;
3504 EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
3505 EVP_PKEY_CTX *pctx = NULL;
3508 loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
3509 loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
3510 if (loopargs[i].sm2_ctx[testnum] == NULL
3511 || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
3514 /* SM2 keys are generated as normal EC keys with a special curve */
3515 st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
3516 || EVP_PKEY_keygen_init(pctx) <= 0
3517 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
3518 sm2_curves[testnum].nid) <= 0
3519 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
3520 EVP_PKEY_CTX_free(pctx);
3524 st = 0; /* set back to zero */
3525 /* attach it sooner to rely on main final cleanup */
3526 loopargs[i].sm2_pkey[testnum] = sm2_pkey;
3527 loopargs[i].sigsize = EVP_PKEY_size(sm2_pkey);
3529 sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3530 sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
3531 if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
3532 EVP_PKEY_CTX_free(sm2_vfy_pctx);
3536 /* attach them directly to respective ctx */
3537 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
3538 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
3541 * No need to allow user to set an explicit ID here, just use
3542 * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
3544 if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
3545 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
3548 if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
3549 EVP_sm3(), NULL, sm2_pkey))
3551 if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
3552 EVP_sm3(), NULL, sm2_pkey))
3554 st = 1; /* mark loop as succeeded */
3557 BIO_printf(bio_err, "SM2 init failure.\n");
3558 ERR_print_errors(bio_err);
3561 for (i = 0; i < loopargs_len; i++) {
3562 size_t sm2_sigsize = loopargs[i].sigsize;
3564 /* Perform SM2 signature test */
3565 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
3566 loopargs[i].buf2, &sm2_sigsize,
3567 loopargs[i].buf, 20);
3573 "SM2 sign failure. No SM2 sign will be done.\n");
3574 ERR_print_errors(bio_err);
3577 pkey_print_message("sign", sm2_curves[testnum].name,
3579 sm2_curves[testnum].bits, seconds.sm2);
3581 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
3585 mr ? "+R10:%ld:%u:%s:%.2f\n" :
3586 "%ld %u bits %s signs in %.2fs \n",
3587 count, sm2_curves[testnum].bits,
3588 sm2_curves[testnum].name, d);
3589 sm2_results[testnum][0] = (double)count / d;
3593 /* Perform SM2 verification test */
3594 for (i = 0; i < loopargs_len; i++) {
3595 st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
3596 loopargs[i].buf2, loopargs[i].sigsize,
3597 loopargs[i].buf, 20);
3603 "SM2 verify failure. No SM2 verify will be done.\n");
3604 ERR_print_errors(bio_err);
3605 sm2_doit[testnum] = 0;
3607 pkey_print_message("verify", sm2_curves[testnum].name,
3609 sm2_curves[testnum].bits, seconds.sm2);
3611 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
3614 mr ? "+R11:%ld:%u:%s:%.2f\n"
3615 : "%ld %u bits %s verify in %.2fs\n",
3616 count, sm2_curves[testnum].bits,
3617 sm2_curves[testnum].name, d);
3618 sm2_results[testnum][1] = (double)count / d;
3621 if (rsa_count <= 1) {
3622 /* if longer than 10s, don't do any more */
3623 for (testnum++; testnum < SM2_NUM; testnum++)
3624 sm2_doit[testnum] = 0;
3628 # endif /* OPENSSL_NO_SM2 */
3629 #endif /* OPENSSL_NO_EC */
3631 #ifndef OPENSSL_NO_DH
3632 for (testnum = 0; testnum < FFDH_NUM; testnum++) {
3633 int ffdh_checks = 1;
3635 if (!ffdh_doit[testnum])
3638 for (i = 0; i < loopargs_len; i++) {
3639 EVP_PKEY *pkey_A = NULL;
3640 EVP_PKEY *pkey_B = NULL;
3641 EVP_PKEY_CTX *ffdh_ctx = NULL;
3642 EVP_PKEY_CTX *test_ctx = NULL;
3646 /* Ensure that the error queue is empty */
3647 if (ERR_peek_error()) {
3649 "WARNING: the error queue contains previous unhandled errors.\n");
3650 ERR_print_errors(bio_err);
3653 pkey_A = EVP_PKEY_new();
3655 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3656 ERR_print_errors(bio_err);
3661 pkey_B = EVP_PKEY_new();
3663 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3664 ERR_print_errors(bio_err);
3670 ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
3672 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3673 ERR_print_errors(bio_err);
3679 if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
3680 BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
3681 ERR_print_errors(bio_err);
3686 if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
3687 BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
3688 ERR_print_errors(bio_err);
3694 if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
3695 EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
3696 BIO_printf(bio_err, "FFDH key generation failure.\n");
3697 ERR_print_errors(bio_err);
3703 EVP_PKEY_CTX_free(ffdh_ctx);
3705 /* check if the derivation works correctly both ways so that
3706 * we know if future derive calls will fail, and we can skip
3707 * error checking in benchmarked code */
3708 ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
3710 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3711 ERR_print_errors(bio_err);
3716 if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
3717 BIO_printf(bio_err, "FFDH derivation context init failure.\n");
3718 ERR_print_errors(bio_err);
3723 if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
3724 BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
3725 ERR_print_errors(bio_err);
3730 if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
3731 BIO_printf(bio_err, "Checking size of shared secret failed.\n");
3732 ERR_print_errors(bio_err);
3737 if (secret_size > MAX_FFDH_SIZE) {
3738 BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
3743 if (EVP_PKEY_derive(ffdh_ctx,
3744 loopargs[i].secret_ff_a,
3745 &secret_size) <= 0) {
3746 BIO_printf(bio_err, "Shared secret derive failure.\n");
3747 ERR_print_errors(bio_err);
3752 /* Now check from side B */
3753 test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
3755 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3756 ERR_print_errors(bio_err);
3761 if (!EVP_PKEY_derive_init(test_ctx) ||
3762 !EVP_PKEY_derive_set_peer(test_ctx, pkey_A) ||
3763 !EVP_PKEY_derive(test_ctx, NULL, &test_out) ||
3764 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) ||
3765 test_out != secret_size) {
3766 BIO_printf(bio_err, "FFDH computation failure.\n");
3772 /* compare the computed secrets */
3773 if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
3774 loopargs[i].secret_ff_b, secret_size)) {
3775 BIO_printf(bio_err, "FFDH computations don't match.\n");
3776 ERR_print_errors(bio_err);
3782 loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
3784 EVP_PKEY_free(pkey_A);
3786 EVP_PKEY_free(pkey_B);
3788 EVP_PKEY_CTX_free(test_ctx);
3791 if (ffdh_checks != 0) {
3792 pkey_print_message("", "ffdh", ffdh_c[testnum][0],
3793 ffdh_params[testnum].bits, seconds.ffdh);
3796 run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
3799 mr ? "+R12:%ld:%d:%.2f\n" :
3800 "%ld %u-bits FFDH ops in %.2fs\n", count,
3801 ffdh_params[testnum].bits, d);
3802 ffdh_results[testnum][0] = (double)count / d;
3805 if (rsa_count <= 1) {
3806 /* if longer than 10s, don't do any more */
3807 stop_it(ffdh_doit, testnum);
3810 #endif /* OPENSSL_NO_DH */
3815 printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
3816 printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
3818 printf("%s ", BN_options());
3819 #if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3820 printf("%s ", MD2_options());
3822 #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3823 printf("%s ", RC4_options());
3825 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3826 printf("%s ", DES_options());
3828 #ifndef OPENSSL_NO_DEPRECATED_3_0
3829 printf("%s ", AES_options());
3831 #if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3832 printf("%s ", IDEA_options());
3834 #if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3835 printf("%s ", BF_options());
3837 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
3838 printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
3846 ("The 'numbers' are in 1000s of bytes per second processed.\n");
3849 for (testnum = 0; testnum < size_num; testnum++)
3850 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
3854 for (k = 0; k < ALGOR_NUM; k++) {
3858 printf("+F:%u:%s", k, names[k]);
3860 printf("%-13s", names[k]);
3861 for (testnum = 0; testnum < size_num; testnum++) {
3862 if (results[k][testnum] > 10000 && !mr)
3863 printf(" %11.2fk", results[k][testnum] / 1e3);
3865 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
3869 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3871 for (k = 0; k < RSA_NUM; k++) {
3874 if (testnum && !mr) {
3875 printf("%18ssign verify sign/s verify/s\n", " ");
3879 printf("+F2:%u:%u:%f:%f\n",
3880 k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1]);
3882 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3883 rsa_keys[k].bits, 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
3884 rsa_results[k][0], rsa_results[k][1]);
3887 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3889 for (k = 0; k < DSA_NUM; k++) {
3892 if (testnum && !mr) {
3893 printf("%18ssign verify sign/s verify/s\n", " ");
3897 printf("+F3:%u:%u:%f:%f\n",
3898 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
3900 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3901 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
3902 dsa_results[k][0], dsa_results[k][1]);
3905 #ifndef OPENSSL_NO_EC
3907 for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
3910 if (testnum && !mr) {
3911 printf("%30ssign verify sign/s verify/s\n", " ");
3916 printf("+F4:%u:%u:%f:%f\n",
3917 k, ec_curves[k].bits,
3918 ecdsa_results[k][0], ecdsa_results[k][1]);
3920 printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3921 ec_curves[k].bits, ec_curves[k].name,
3922 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
3923 ecdsa_results[k][0], ecdsa_results[k][1]);
3927 for (k = 0; k < EC_NUM; k++) {
3930 if (testnum && !mr) {
3931 printf("%30sop op/s\n", " ");
3935 printf("+F5:%u:%u:%f:%f\n",
3936 k, ec_curves[k].bits,
3937 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3940 printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
3941 ec_curves[k].bits, ec_curves[k].name,
3942 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3946 for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
3949 if (testnum && !mr) {
3950 printf("%30ssign verify sign/s verify/s\n", " ");
3955 printf("+F6:%u:%u:%s:%f:%f\n",
3956 k, ed_curves[k].bits, ed_curves[k].name,
3957 eddsa_results[k][0], eddsa_results[k][1]);
3959 printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3960 ed_curves[k].bits, ed_curves[k].name,
3961 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
3962 eddsa_results[k][0], eddsa_results[k][1]);
3965 # ifndef OPENSSL_NO_SM2
3967 for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
3970 if (testnum && !mr) {
3971 printf("%30ssign verify sign/s verify/s\n", " ");
3976 printf("+F7:%u:%u:%s:%f:%f\n",
3977 k, sm2_curves[k].bits, sm2_curves[k].name,
3978 sm2_results[k][0], sm2_results[k][1]);
3980 printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3981 sm2_curves[k].bits, sm2_curves[k].name,
3982 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
3983 sm2_results[k][0], sm2_results[k][1]);
3986 #endif /* OPENSSL_NO_EC */
3987 #ifndef OPENSSL_NO_DH
3989 for (k = 0; k < FFDH_NUM; k++) {
3992 if (testnum && !mr) {
3993 printf("%23sop op/s\n", " ");
3997 printf("+F8:%u:%u:%f:%f\n",
3998 k, ffdh_params[k].bits,
3999 ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
4002 printf("%4u bits ffdh %8.4fs %8.1f\n",
4003 ffdh_params[k].bits,
4004 1.0 / ffdh_results[k][0], ffdh_results[k][0]);
4006 #endif /* OPENSSL_NO_DH */
4011 ERR_print_errors(bio_err);
4012 for (i = 0; i < loopargs_len; i++) {
4013 OPENSSL_free(loopargs[i].buf_malloc);
4014 OPENSSL_free(loopargs[i].buf2_malloc);
4016 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4017 for (k = 0; k < RSA_NUM; k++)
4018 RSA_free(loopargs[i].rsa_key[k]);
4020 #ifndef OPENSSL_NO_DH
4021 OPENSSL_free(loopargs[i].secret_ff_a);
4022 OPENSSL_free(loopargs[i].secret_ff_b);
4023 for (k = 0; k < FFDH_NUM; k++) {
4024 EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
4027 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4028 for (k = 0; k < DSA_NUM; k++)
4029 DSA_free(loopargs[i].dsa_key[k]);
4031 #ifndef OPENSSL_NO_EC
4032 for (k = 0; k < ECDSA_NUM; k++)
4033 EC_KEY_free(loopargs[i].ecdsa[k]);
4034 for (k = 0; k < EC_NUM; k++)
4035 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
4036 for (k = 0; k < EdDSA_NUM; k++) {
4037 EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
4038 EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
4040 # ifndef OPENSSL_NO_SM2
4041 for (k = 0; k < SM2_NUM; k++) {
4042 EVP_PKEY_CTX *pctx = NULL;
4044 /* free signing ctx */
4045 if (loopargs[i].sm2_ctx[k] != NULL
4046 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
4047 EVP_PKEY_CTX_free(pctx);
4048 EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
4049 /* free verification ctx */
4050 if (loopargs[i].sm2_vfy_ctx[k] != NULL
4051 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
4052 EVP_PKEY_CTX_free(pctx);
4053 EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
4055 EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
4058 OPENSSL_free(loopargs[i].secret_a);
4059 OPENSSL_free(loopargs[i].secret_b);
4062 #ifndef OPENSSL_NO_DEPRECATED_3_0
4063 OPENSSL_free(evp_hmac_name);
4065 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4066 OPENSSL_free(evp_cmac_name);
4069 if (async_jobs > 0) {
4070 for (i = 0; i < loopargs_len; i++)
4071 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
4075 ASYNC_cleanup_thread();
4077 OPENSSL_free(loopargs);
4082 static void print_message(const char *s, long num, int length, int tm)
4086 mr ? "+DT:%s:%d:%d\n"
4087 : "Doing %s for %ds on %d size blocks: ", s, tm, length);
4088 (void)BIO_flush(bio_err);
4093 mr ? "+DN:%s:%ld:%d\n"
4094 : "Doing %s %ld times on %d size blocks: ", s, num, length);
4095 (void)BIO_flush(bio_err);
4099 static void pkey_print_message(const char *str, const char *str2, long num,
4100 unsigned int bits, int tm)
4104 mr ? "+DTP:%d:%s:%s:%d\n"
4105 : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
4106 (void)BIO_flush(bio_err);
4111 mr ? "+DNP:%ld:%d:%s:%s\n"
4112 : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
4113 (void)BIO_flush(bio_err);
4117 static void print_result(int alg, int run_no, int count, double time_used)
4120 BIO_printf(bio_err, "%s error!\n", names[alg]);
4121 ERR_print_errors(bio_err);
4122 /* exit(1); disable exit until default provider enabled */
4126 mr ? "+R:%d:%s:%f\n"
4127 : "%d %s's in %.2fs\n", count, names[alg], time_used);
4128 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
4132 static char *sstrsep(char **string, const char *delim)
4135 char *token = *string;
4140 memset(isdelim, 0, sizeof(isdelim));
4144 isdelim[(unsigned char)(*delim)] = 1;
4148 while (!isdelim[(unsigned char)(**string)]) {
4160 static int do_multi(int multi, int size_num)
4165 static char sep[] = ":";
4167 fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
4168 for (n = 0; n < multi; ++n) {
4169 if (pipe(fd) == -1) {
4170 BIO_printf(bio_err, "pipe failure\n");
4174 (void)BIO_flush(bio_err);
4181 if (dup(fd[1]) == -1) {
4182 BIO_printf(bio_err, "dup failed\n");
4191 printf("Forked child %d\n", n);
4194 /* for now, assume the pipe is long enough to take all the output */
4195 for (n = 0; n < multi; ++n) {
4200 f = fdopen(fds[n], "r");
4201 while (fgets(buf, sizeof(buf), f)) {
4202 p = strchr(buf, '\n');
4205 if (buf[0] != '+') {
4207 "Don't understand line '%s' from child %d\n", buf,
4211 printf("Got: %s from %d\n", buf, n);
4212 if (strncmp(buf, "+F:", 3) == 0) {
4217 alg = atoi(sstrsep(&p, sep));
4219 for (j = 0; j < size_num; ++j)
4220 results[alg][j] += atof(sstrsep(&p, sep));
4222 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4223 else if (strncmp(buf, "+F2:", 4) == 0) {
4228 k = atoi(sstrsep(&p, sep));
4231 d = atof(sstrsep(&p, sep));
4232 rsa_results[k][0] += d;
4234 d = atof(sstrsep(&p, sep));
4235 rsa_results[k][1] += d;
4238 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4239 else if (strncmp(buf, "+F3:", 4) == 0) {
4244 k = atoi(sstrsep(&p, sep));
4247 d = atof(sstrsep(&p, sep));
4248 dsa_results[k][0] += d;
4250 d = atof(sstrsep(&p, sep));
4251 dsa_results[k][1] += d;
4254 # ifndef OPENSSL_NO_EC
4255 else if (strncmp(buf, "+F4:", 4) == 0) {
4260 k = atoi(sstrsep(&p, sep));
4263 d = atof(sstrsep(&p, sep));
4264 ecdsa_results[k][0] += d;
4266 d = atof(sstrsep(&p, sep));
4267 ecdsa_results[k][1] += d;
4268 } else if (strncmp(buf, "+F5:", 4) == 0) {
4273 k = atoi(sstrsep(&p, sep));
4276 d = atof(sstrsep(&p, sep));
4277 ecdh_results[k][0] += d;
4278 } else if (strncmp(buf, "+F6:", 4) == 0) {
4283 k = atoi(sstrsep(&p, sep));
4287 d = atof(sstrsep(&p, sep));
4288 eddsa_results[k][0] += d;
4290 d = atof(sstrsep(&p, sep));
4291 eddsa_results[k][1] += d;
4293 # ifndef OPENSSL_NO_SM2
4294 else if (strncmp(buf, "+F7:", 4) == 0) {
4299 k = atoi(sstrsep(&p, sep));
4303 d = atof(sstrsep(&p, sep));
4304 sm2_results[k][0] += d;
4306 d = atof(sstrsep(&p, sep));
4307 sm2_results[k][1] += d;
4309 # endif /* OPENSSL_NO_SM2 */
4310 # endif /* OPENSSL_NO_EC */
4311 # ifndef OPENSSL_NO_DH
4312 else if (strncmp(buf, "+F8:", 4) == 0) {
4317 k = atoi(sstrsep(&p, sep));
4320 d = atof(sstrsep(&p, sep));
4321 ffdh_results[k][0] += d;
4323 # endif /* OPENSSL_NO_DH */
4325 else if (strncmp(buf, "+H:", 3) == 0) {
4328 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
4339 static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
4340 const openssl_speed_sec_t *seconds)
4342 static const int mblengths_list[] =
4343 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
4344 const int *mblengths = mblengths_list;
4345 int j, count, keylen, num = OSSL_NELEM(mblengths_list);
4346 const char *alg_name;
4347 unsigned char *inp, *out, *key, no_key[32], no_iv[16];
4348 EVP_CIPHER_CTX *ctx;
4351 if (lengths_single) {
4352 mblengths = &lengths_single;
4356 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
4357 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
4358 ctx = EVP_CIPHER_CTX_new();
4359 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
4361 keylen = EVP_CIPHER_CTX_key_length(ctx);
4362 key = app_malloc(keylen, "evp_cipher key");
4363 EVP_CIPHER_CTX_rand_key(ctx, key);
4364 EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
4365 OPENSSL_clear_free(key, keylen);
4367 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
4368 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
4370 for (j = 0; j < num; j++) {
4371 print_message(alg_name, 0, mblengths[j], seconds->sym);
4373 for (count = 0; run && count < 0x7fffffff; count++) {
4374 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
4375 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
4376 size_t len = mblengths[j];
4379 memset(aad, 0, 8); /* avoid uninitialized values */
4380 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
4381 aad[9] = 3; /* version */
4383 aad[11] = 0; /* length */
4385 mb_param.out = NULL;
4388 mb_param.interleave = 8;
4390 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
4391 sizeof(mb_param), &mb_param);
4397 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
4398 sizeof(mb_param), &mb_param);
4402 RAND_bytes(out, 16);
4404 aad[11] = (unsigned char)(len >> 8);
4405 aad[12] = (unsigned char)(len);
4406 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
4407 EVP_AEAD_TLS1_AAD_LEN, aad);
4408 EVP_Cipher(ctx, out, inp, len + pad);
4412 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
4413 : "%d %s's in %.2fs\n", count, "evp", d);
4414 results[D_EVP][j] = ((double)count) / d * mblengths[j];
4418 fprintf(stdout, "+H");
4419 for (j = 0; j < num; j++)
4420 fprintf(stdout, ":%d", mblengths[j]);
4421 fprintf(stdout, "\n");
4422 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
4423 for (j = 0; j < num; j++)
4424 fprintf(stdout, ":%.2f", results[D_EVP][j]);
4425 fprintf(stdout, "\n");
4428 "The 'numbers' are in 1000s of bytes per second processed.\n");
4429 fprintf(stdout, "type ");
4430 for (j = 0; j < num; j++)
4431 fprintf(stdout, "%7d bytes", mblengths[j]);
4432 fprintf(stdout, "\n");
4433 fprintf(stdout, "%-24s", alg_name);
4435 for (j = 0; j < num; j++) {
4436 if (results[D_EVP][j] > 10000)
4437 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
4439 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
4441 fprintf(stdout, "\n");
4446 EVP_CIPHER_CTX_free(ctx);