2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (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
23 #include <openssl/crypto.h>
24 #include <openssl/rand.h>
25 #include <openssl/err.h>
26 #include <openssl/evp.h>
27 #include <openssl/objects.h>
28 #include <openssl/async.h>
29 #if !defined(OPENSSL_SYS_MSDOS)
30 # include OPENSSL_UNISTD
37 #include <openssl/bn.h>
38 #ifndef OPENSSL_NO_DES
39 # include <openssl/des.h>
41 #include <openssl/aes.h>
42 #ifndef OPENSSL_NO_CAMELLIA
43 # include <openssl/camellia.h>
45 #ifndef OPENSSL_NO_MD2
46 # include <openssl/md2.h>
48 #ifndef OPENSSL_NO_MDC2
49 # include <openssl/mdc2.h>
51 #ifndef OPENSSL_NO_MD4
52 # include <openssl/md4.h>
54 #ifndef OPENSSL_NO_MD5
55 # include <openssl/md5.h>
57 #include <openssl/hmac.h>
58 #include <openssl/sha.h>
59 #ifndef OPENSSL_NO_RMD160
60 # include <openssl/ripemd.h>
62 #ifndef OPENSSL_NO_WHIRLPOOL
63 # include <openssl/whrlpool.h>
65 #ifndef OPENSSL_NO_RC4
66 # include <openssl/rc4.h>
68 #ifndef OPENSSL_NO_RC5
69 # include <openssl/rc5.h>
71 #ifndef OPENSSL_NO_RC2
72 # include <openssl/rc2.h>
74 #ifndef OPENSSL_NO_IDEA
75 # include <openssl/idea.h>
77 #ifndef OPENSSL_NO_SEED
78 # include <openssl/seed.h>
81 # include <openssl/blowfish.h>
83 #ifndef OPENSSL_NO_CAST
84 # include <openssl/cast.h>
86 #ifndef OPENSSL_NO_RSA
87 # include <openssl/rsa.h>
88 # include "./testrsa.h"
90 #include <openssl/x509.h>
91 #ifndef OPENSSL_NO_DSA
92 # include <openssl/dsa.h>
93 # include "./testdsa.h"
96 # include <openssl/ec.h>
98 #include <openssl/modes.h>
101 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
114 #define MAX_MISALIGNMENT 63
121 #define MAX_ECDH_SIZE 256
124 typedef struct sec_st {
132 static volatile int run = 0;
135 static int usertime = 1;
137 typedef struct loopargs_st {
138 ASYNC_JOB *inprogress_job;
139 ASYNC_WAIT_CTX *wait_ctx;
142 unsigned char *buf_malloc;
143 unsigned char *buf2_malloc;
145 #ifndef OPENSSL_NO_RSA
146 RSA *rsa_key[RSA_NUM];
148 #ifndef OPENSSL_NO_DSA
149 DSA *dsa_key[DSA_NUM];
151 #ifndef OPENSSL_NO_EC
152 EC_KEY *ecdsa[EC_NUM];
153 EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
154 unsigned char *secret_a;
155 unsigned char *secret_b;
156 size_t outlen[EC_NUM];
160 GCM128_CONTEXT *gcm_ctx;
163 #ifndef OPENSSL_NO_MD2
164 static int EVP_Digest_MD2_loop(void *args);
167 #ifndef OPENSSL_NO_MDC2
168 static int EVP_Digest_MDC2_loop(void *args);
170 #ifndef OPENSSL_NO_MD4
171 static int EVP_Digest_MD4_loop(void *args);
173 #ifndef OPENSSL_NO_MD5
174 static int MD5_loop(void *args);
175 static int HMAC_loop(void *args);
177 static int SHA1_loop(void *args);
178 static int SHA256_loop(void *args);
179 static int SHA512_loop(void *args);
180 #ifndef OPENSSL_NO_WHIRLPOOL
181 static int WHIRLPOOL_loop(void *args);
183 #ifndef OPENSSL_NO_RMD160
184 static int EVP_Digest_RMD160_loop(void *args);
186 #ifndef OPENSSL_NO_RC4
187 static int RC4_loop(void *args);
189 #ifndef OPENSSL_NO_DES
190 static int DES_ncbc_encrypt_loop(void *args);
191 static int DES_ede3_cbc_encrypt_loop(void *args);
193 static int AES_cbc_128_encrypt_loop(void *args);
194 static int AES_cbc_192_encrypt_loop(void *args);
195 static int AES_ige_128_encrypt_loop(void *args);
196 static int AES_cbc_256_encrypt_loop(void *args);
197 static int AES_ige_192_encrypt_loop(void *args);
198 static int AES_ige_256_encrypt_loop(void *args);
199 static int CRYPTO_gcm128_aad_loop(void *args);
200 static int RAND_bytes_loop(void *args);
201 static int EVP_Update_loop(void *args);
202 static int EVP_Update_loop_ccm(void *args);
203 static int EVP_Digest_loop(void *args);
204 #ifndef OPENSSL_NO_RSA
205 static int RSA_sign_loop(void *args);
206 static int RSA_verify_loop(void *args);
208 #ifndef OPENSSL_NO_DSA
209 static int DSA_sign_loop(void *args);
210 static int DSA_verify_loop(void *args);
212 #ifndef OPENSSL_NO_EC
213 static int ECDSA_sign_loop(void *args);
214 static int ECDSA_verify_loop(void *args);
216 static int run_benchmark(int async_jobs, int (*loop_function) (void *),
217 loopargs_t * loopargs);
219 static double Time_F(int s);
220 static void print_message(const char *s, long num, int length, int tm);
221 static void pkey_print_message(const char *str, const char *str2,
222 long num, int bits, int sec);
223 static void print_result(int alg, int run_no, int count, double time_used);
225 static int do_multi(int multi, int size_num);
228 static const int lengths_list[] = {
229 16, 64, 256, 1024, 8 * 1024, 16 * 1024
231 static int lengths_single = 0;
233 static const int *lengths = lengths_list;
235 static const char *names[ALGOR_NUM] = {
236 "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
237 "des cbc", "des ede3", "idea cbc", "seed cbc",
238 "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
239 "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
240 "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
241 "evp", "sha256", "sha512", "whirlpool",
242 "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
246 static double results[ALGOR_NUM][OSSL_NELEM(lengths_list)];
248 #ifndef OPENSSL_NO_RSA
249 static double rsa_results[RSA_NUM][2];
251 #ifndef OPENSSL_NO_DSA
252 static double dsa_results[DSA_NUM][2];
254 #ifndef OPENSSL_NO_EC
255 static double ecdsa_results[EC_NUM][2];
256 static double ecdh_results[EC_NUM][1];
260 # if defined(__STDC__) || defined(sgi) || defined(_AIX)
261 # define SIGRETTYPE void
263 # define SIGRETTYPE int
266 static SIGRETTYPE sig_done(int sig);
267 static SIGRETTYPE sig_done(int sig)
269 signal(SIGALRM, sig_done);
279 # if !defined(SIGALRM)
282 static unsigned int lapse;
283 static volatile unsigned int schlock;
284 static void alarm_win32(unsigned int secs)
289 # define alarm alarm_win32
291 static DWORD WINAPI sleepy(VOID * arg)
299 static double Time_F(int s)
306 thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
308 DWORD err = GetLastError();
309 BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
313 Sleep(0); /* scheduler spinlock */
314 ret = app_tminterval(s, usertime);
316 ret = app_tminterval(s, usertime);
318 TerminateThread(thr, 0);
326 static double Time_F(int s)
328 double ret = app_tminterval(s, usertime);
335 static void multiblock_speed(const EVP_CIPHER *evp_cipher,
338 static int found(const char *name, const OPT_PAIR *pairs, int *result)
340 for (; pairs->name; pairs++)
341 if (strcmp(name, pairs->name) == 0) {
342 *result = pairs->retval;
348 typedef enum OPTION_choice {
349 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
350 OPT_ELAPSED, OPT_EVP, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
351 OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM,
352 OPT_PRIMES, OPT_SECONDS, OPT_BYTES
355 const OPTIONS speed_options[] = {
356 {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
357 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
358 {"help", OPT_HELP, '-', "Display this summary"},
359 {"evp", OPT_EVP, 's', "Use specified EVP cipher"},
360 {"decrypt", OPT_DECRYPT, '-',
361 "Time decryption instead of encryption (only EVP)"},
362 {"mr", OPT_MR, '-', "Produce machine readable output"},
364 "Enable (tls1.1) multi-block mode on evp_cipher requested with -evp"},
365 {"misalign", OPT_MISALIGN, 'n', "Amount to mis-align buffers"},
366 {"elapsed", OPT_ELAPSED, '-',
367 "Measure time in real time instead of CPU user time"},
369 {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
371 #ifndef OPENSSL_NO_ASYNC
372 {"async_jobs", OPT_ASYNCJOBS, 'p',
373 "Enable async mode and start pnum jobs"},
376 #ifndef OPENSSL_NO_ENGINE
377 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
379 {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
380 {"seconds", OPT_SECONDS, 'p',
381 "Run benchmarks for pnum seconds"},
382 {"bytes", OPT_BYTES, 'p',
383 "Run cipher, digest and rand benchmarks on pnum bytes"},
397 #define D_CBC_IDEA 10
398 #define D_CBC_SEED 11
402 #define D_CBC_CAST 15
403 #define D_CBC_128_AES 16
404 #define D_CBC_192_AES 17
405 #define D_CBC_256_AES 18
406 #define D_CBC_128_CML 19
407 #define D_CBC_192_CML 20
408 #define D_CBC_256_CML 21
412 #define D_WHIRLPOOL 25
413 #define D_IGE_128_AES 26
414 #define D_IGE_192_AES 27
415 #define D_IGE_256_AES 28
418 static OPT_PAIR doit_choices[] = {
419 #ifndef OPENSSL_NO_MD2
422 #ifndef OPENSSL_NO_MDC2
425 #ifndef OPENSSL_NO_MD4
428 #ifndef OPENSSL_NO_MD5
433 {"sha256", D_SHA256},
434 {"sha512", D_SHA512},
435 #ifndef OPENSSL_NO_WHIRLPOOL
436 {"whirlpool", D_WHIRLPOOL},
438 #ifndef OPENSSL_NO_RMD160
439 {"ripemd", D_RMD160},
440 {"rmd160", D_RMD160},
441 {"ripemd160", D_RMD160},
443 #ifndef OPENSSL_NO_RC4
446 #ifndef OPENSSL_NO_DES
447 {"des-cbc", D_CBC_DES},
448 {"des-ede3", D_EDE3_DES},
450 {"aes-128-cbc", D_CBC_128_AES},
451 {"aes-192-cbc", D_CBC_192_AES},
452 {"aes-256-cbc", D_CBC_256_AES},
453 {"aes-128-ige", D_IGE_128_AES},
454 {"aes-192-ige", D_IGE_192_AES},
455 {"aes-256-ige", D_IGE_256_AES},
456 #ifndef OPENSSL_NO_RC2
457 {"rc2-cbc", D_CBC_RC2},
460 #ifndef OPENSSL_NO_RC5
461 {"rc5-cbc", D_CBC_RC5},
464 #ifndef OPENSSL_NO_IDEA
465 {"idea-cbc", D_CBC_IDEA},
466 {"idea", D_CBC_IDEA},
468 #ifndef OPENSSL_NO_SEED
469 {"seed-cbc", D_CBC_SEED},
470 {"seed", D_CBC_SEED},
472 #ifndef OPENSSL_NO_BF
473 {"bf-cbc", D_CBC_BF},
474 {"blowfish", D_CBC_BF},
477 #ifndef OPENSSL_NO_CAST
478 {"cast-cbc", D_CBC_CAST},
479 {"cast", D_CBC_CAST},
480 {"cast5", D_CBC_CAST},
487 #ifndef OPENSSL_NO_DSA
489 # define R_DSA_1024 1
490 # define R_DSA_2048 2
491 static OPT_PAIR dsa_choices[] = {
492 {"dsa512", R_DSA_512},
493 {"dsa1024", R_DSA_1024},
494 {"dsa2048", R_DSA_2048},
505 #define R_RSA_15360 6
506 static OPT_PAIR rsa_choices[] = {
507 {"rsa512", R_RSA_512},
508 {"rsa1024", R_RSA_1024},
509 {"rsa2048", R_RSA_2048},
510 {"rsa3072", R_RSA_3072},
511 {"rsa4096", R_RSA_4096},
512 {"rsa7680", R_RSA_7680},
513 {"rsa15360", R_RSA_15360},
533 #define R_EC_X25519 16
534 #ifndef OPENSSL_NO_EC
535 static OPT_PAIR ecdsa_choices[] = {
536 {"ecdsap160", R_EC_P160},
537 {"ecdsap192", R_EC_P192},
538 {"ecdsap224", R_EC_P224},
539 {"ecdsap256", R_EC_P256},
540 {"ecdsap384", R_EC_P384},
541 {"ecdsap521", R_EC_P521},
542 {"ecdsak163", R_EC_K163},
543 {"ecdsak233", R_EC_K233},
544 {"ecdsak283", R_EC_K283},
545 {"ecdsak409", R_EC_K409},
546 {"ecdsak571", R_EC_K571},
547 {"ecdsab163", R_EC_B163},
548 {"ecdsab233", R_EC_B233},
549 {"ecdsab283", R_EC_B283},
550 {"ecdsab409", R_EC_B409},
551 {"ecdsab571", R_EC_B571},
555 static OPT_PAIR ecdh_choices[] = {
556 {"ecdhp160", R_EC_P160},
557 {"ecdhp192", R_EC_P192},
558 {"ecdhp224", R_EC_P224},
559 {"ecdhp256", R_EC_P256},
560 {"ecdhp384", R_EC_P384},
561 {"ecdhp521", R_EC_P521},
562 {"ecdhk163", R_EC_K163},
563 {"ecdhk233", R_EC_K233},
564 {"ecdhk283", R_EC_K283},
565 {"ecdhk409", R_EC_K409},
566 {"ecdhk571", R_EC_K571},
567 {"ecdhb163", R_EC_B163},
568 {"ecdhb233", R_EC_B233},
569 {"ecdhb283", R_EC_B283},
570 {"ecdhb409", R_EC_B409},
571 {"ecdhb571", R_EC_B571},
572 {"ecdhx25519", R_EC_X25519},
578 # define COND(d) (count < (d))
579 # define COUNT(d) (d)
581 # define COND(unused_cond) (run && count<0x7fffffff)
582 # define COUNT(d) (count)
587 /* Nb of iterations to do per algorithm and key-size */
588 static long c[ALGOR_NUM][OSSL_NELEM(lengths_list)];
590 #ifndef OPENSSL_NO_MD2
591 static int EVP_Digest_MD2_loop(void *args)
593 loopargs_t *tempargs = *(loopargs_t **) args;
594 unsigned char *buf = tempargs->buf;
595 unsigned char md2[MD2_DIGEST_LENGTH];
598 for (count = 0; COND(c[D_MD2][testnum]); count++) {
599 if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
607 #ifndef OPENSSL_NO_MDC2
608 static int EVP_Digest_MDC2_loop(void *args)
610 loopargs_t *tempargs = *(loopargs_t **) args;
611 unsigned char *buf = tempargs->buf;
612 unsigned char mdc2[MDC2_DIGEST_LENGTH];
615 for (count = 0; COND(c[D_MDC2][testnum]); count++) {
616 if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
624 #ifndef OPENSSL_NO_MD4
625 static int EVP_Digest_MD4_loop(void *args)
627 loopargs_t *tempargs = *(loopargs_t **) args;
628 unsigned char *buf = tempargs->buf;
629 unsigned char md4[MD4_DIGEST_LENGTH];
632 for (count = 0; COND(c[D_MD4][testnum]); count++) {
633 if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
641 #ifndef OPENSSL_NO_MD5
642 static int MD5_loop(void *args)
644 loopargs_t *tempargs = *(loopargs_t **) args;
645 unsigned char *buf = tempargs->buf;
646 unsigned char md5[MD5_DIGEST_LENGTH];
648 for (count = 0; COND(c[D_MD5][testnum]); count++)
649 MD5(buf, lengths[testnum], md5);
653 static int HMAC_loop(void *args)
655 loopargs_t *tempargs = *(loopargs_t **) args;
656 unsigned char *buf = tempargs->buf;
657 HMAC_CTX *hctx = tempargs->hctx;
658 unsigned char hmac[MD5_DIGEST_LENGTH];
661 for (count = 0; COND(c[D_HMAC][testnum]); count++) {
662 HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
663 HMAC_Update(hctx, buf, lengths[testnum]);
664 HMAC_Final(hctx, hmac, NULL);
670 static int SHA1_loop(void *args)
672 loopargs_t *tempargs = *(loopargs_t **) args;
673 unsigned char *buf = tempargs->buf;
674 unsigned char sha[SHA_DIGEST_LENGTH];
676 for (count = 0; COND(c[D_SHA1][testnum]); count++)
677 SHA1(buf, lengths[testnum], sha);
681 static int SHA256_loop(void *args)
683 loopargs_t *tempargs = *(loopargs_t **) args;
684 unsigned char *buf = tempargs->buf;
685 unsigned char sha256[SHA256_DIGEST_LENGTH];
687 for (count = 0; COND(c[D_SHA256][testnum]); count++)
688 SHA256(buf, lengths[testnum], sha256);
692 static int SHA512_loop(void *args)
694 loopargs_t *tempargs = *(loopargs_t **) args;
695 unsigned char *buf = tempargs->buf;
696 unsigned char sha512[SHA512_DIGEST_LENGTH];
698 for (count = 0; COND(c[D_SHA512][testnum]); count++)
699 SHA512(buf, lengths[testnum], sha512);
703 #ifndef OPENSSL_NO_WHIRLPOOL
704 static int WHIRLPOOL_loop(void *args)
706 loopargs_t *tempargs = *(loopargs_t **) args;
707 unsigned char *buf = tempargs->buf;
708 unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
710 for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
711 WHIRLPOOL(buf, lengths[testnum], whirlpool);
716 #ifndef OPENSSL_NO_RMD160
717 static int EVP_Digest_RMD160_loop(void *args)
719 loopargs_t *tempargs = *(loopargs_t **) args;
720 unsigned char *buf = tempargs->buf;
721 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
723 for (count = 0; COND(c[D_RMD160][testnum]); count++) {
724 if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
725 NULL, EVP_ripemd160(), NULL))
732 #ifndef OPENSSL_NO_RC4
733 static RC4_KEY rc4_ks;
734 static int RC4_loop(void *args)
736 loopargs_t *tempargs = *(loopargs_t **) args;
737 unsigned char *buf = tempargs->buf;
739 for (count = 0; COND(c[D_RC4][testnum]); count++)
740 RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
745 #ifndef OPENSSL_NO_DES
746 static unsigned char DES_iv[8];
747 static DES_key_schedule sch;
748 static DES_key_schedule sch2;
749 static DES_key_schedule sch3;
750 static int DES_ncbc_encrypt_loop(void *args)
752 loopargs_t *tempargs = *(loopargs_t **) args;
753 unsigned char *buf = tempargs->buf;
755 for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
756 DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch,
757 &DES_iv, DES_ENCRYPT);
761 static int DES_ede3_cbc_encrypt_loop(void *args)
763 loopargs_t *tempargs = *(loopargs_t **) args;
764 unsigned char *buf = tempargs->buf;
766 for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
767 DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
768 &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT);
773 #define MAX_BLOCK_SIZE 128
775 static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
776 static AES_KEY aes_ks1, aes_ks2, aes_ks3;
777 static int AES_cbc_128_encrypt_loop(void *args)
779 loopargs_t *tempargs = *(loopargs_t **) args;
780 unsigned char *buf = tempargs->buf;
782 for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
783 AES_cbc_encrypt(buf, buf,
784 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
788 static int AES_cbc_192_encrypt_loop(void *args)
790 loopargs_t *tempargs = *(loopargs_t **) args;
791 unsigned char *buf = tempargs->buf;
793 for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
794 AES_cbc_encrypt(buf, buf,
795 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
799 static int AES_cbc_256_encrypt_loop(void *args)
801 loopargs_t *tempargs = *(loopargs_t **) args;
802 unsigned char *buf = tempargs->buf;
804 for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
805 AES_cbc_encrypt(buf, buf,
806 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
810 static int AES_ige_128_encrypt_loop(void *args)
812 loopargs_t *tempargs = *(loopargs_t **) args;
813 unsigned char *buf = tempargs->buf;
814 unsigned char *buf2 = tempargs->buf2;
816 for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
817 AES_ige_encrypt(buf, buf2,
818 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
822 static int AES_ige_192_encrypt_loop(void *args)
824 loopargs_t *tempargs = *(loopargs_t **) args;
825 unsigned char *buf = tempargs->buf;
826 unsigned char *buf2 = tempargs->buf2;
828 for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
829 AES_ige_encrypt(buf, buf2,
830 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
834 static int AES_ige_256_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_256_AES][testnum]); count++)
841 AES_ige_encrypt(buf, buf2,
842 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
846 static int CRYPTO_gcm128_aad_loop(void *args)
848 loopargs_t *tempargs = *(loopargs_t **) args;
849 unsigned char *buf = tempargs->buf;
850 GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
852 for (count = 0; COND(c[D_GHASH][testnum]); count++)
853 CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
857 static int RAND_bytes_loop(void *args)
859 loopargs_t *tempargs = *(loopargs_t **) args;
860 unsigned char *buf = tempargs->buf;
863 for (count = 0; COND(c[D_RAND][testnum]); count++)
864 RAND_bytes(buf, lengths[testnum]);
868 static long save_count = 0;
869 static int decrypt = 0;
870 static int EVP_Update_loop(void *args)
872 loopargs_t *tempargs = *(loopargs_t **) args;
873 unsigned char *buf = tempargs->buf;
874 EVP_CIPHER_CTX *ctx = tempargs->ctx;
877 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
880 for (count = 0; COND(nb_iter); count++) {
881 rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
883 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
886 for (count = 0; COND(nb_iter); count++) {
887 rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
889 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
893 EVP_DecryptFinal_ex(ctx, buf, &outl);
895 EVP_EncryptFinal_ex(ctx, buf, &outl);
899 * CCM does not support streaming. For the purpose of performance measurement,
900 * each message is encrypted using the same (key,iv)-pair. Do not use this
901 * code in your application.
903 static int EVP_Update_loop_ccm(void *args)
905 loopargs_t *tempargs = *(loopargs_t **) args;
906 unsigned char *buf = tempargs->buf;
907 EVP_CIPHER_CTX *ctx = tempargs->ctx;
909 unsigned char tag[12];
911 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
914 for (count = 0; COND(nb_iter); count++) {
915 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
916 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
917 EVP_DecryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
918 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
919 EVP_DecryptFinal_ex(ctx, buf, &outl);
922 for (count = 0; COND(nb_iter); count++) {
923 EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
924 EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
925 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
926 EVP_EncryptFinal_ex(ctx, buf, &outl);
932 static const EVP_MD *evp_md = NULL;
933 static int EVP_Digest_loop(void *args)
935 loopargs_t *tempargs = *(loopargs_t **) args;
936 unsigned char *buf = tempargs->buf;
937 unsigned char md[EVP_MAX_MD_SIZE];
940 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
943 for (count = 0; COND(nb_iter); count++) {
944 if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
950 #ifndef OPENSSL_NO_RSA
951 static long rsa_c[RSA_NUM][2]; /* # RSA iteration test */
953 static int RSA_sign_loop(void *args)
955 loopargs_t *tempargs = *(loopargs_t **) args;
956 unsigned char *buf = tempargs->buf;
957 unsigned char *buf2 = tempargs->buf2;
958 unsigned int *rsa_num = &tempargs->siglen;
959 RSA **rsa_key = tempargs->rsa_key;
961 for (count = 0; COND(rsa_c[testnum][0]); count++) {
962 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
964 BIO_printf(bio_err, "RSA sign failure\n");
965 ERR_print_errors(bio_err);
973 static int RSA_verify_loop(void *args)
975 loopargs_t *tempargs = *(loopargs_t **) args;
976 unsigned char *buf = tempargs->buf;
977 unsigned char *buf2 = tempargs->buf2;
978 unsigned int rsa_num = tempargs->siglen;
979 RSA **rsa_key = tempargs->rsa_key;
981 for (count = 0; COND(rsa_c[testnum][1]); count++) {
983 RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
985 BIO_printf(bio_err, "RSA verify failure\n");
986 ERR_print_errors(bio_err);
995 #ifndef OPENSSL_NO_DSA
996 static long dsa_c[DSA_NUM][2];
997 static int DSA_sign_loop(void *args)
999 loopargs_t *tempargs = *(loopargs_t **) args;
1000 unsigned char *buf = tempargs->buf;
1001 unsigned char *buf2 = tempargs->buf2;
1002 DSA **dsa_key = tempargs->dsa_key;
1003 unsigned int *siglen = &tempargs->siglen;
1005 for (count = 0; COND(dsa_c[testnum][0]); count++) {
1006 ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1008 BIO_printf(bio_err, "DSA sign failure\n");
1009 ERR_print_errors(bio_err);
1017 static int DSA_verify_loop(void *args)
1019 loopargs_t *tempargs = *(loopargs_t **) args;
1020 unsigned char *buf = tempargs->buf;
1021 unsigned char *buf2 = tempargs->buf2;
1022 DSA **dsa_key = tempargs->dsa_key;
1023 unsigned int siglen = tempargs->siglen;
1025 for (count = 0; COND(dsa_c[testnum][1]); count++) {
1026 ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1028 BIO_printf(bio_err, "DSA verify failure\n");
1029 ERR_print_errors(bio_err);
1038 #ifndef OPENSSL_NO_EC
1039 static long ecdsa_c[EC_NUM][2];
1040 static int ECDSA_sign_loop(void *args)
1042 loopargs_t *tempargs = *(loopargs_t **) args;
1043 unsigned char *buf = tempargs->buf;
1044 EC_KEY **ecdsa = tempargs->ecdsa;
1045 unsigned char *ecdsasig = tempargs->buf2;
1046 unsigned int *ecdsasiglen = &tempargs->siglen;
1048 for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1049 ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1051 BIO_printf(bio_err, "ECDSA sign failure\n");
1052 ERR_print_errors(bio_err);
1060 static int ECDSA_verify_loop(void *args)
1062 loopargs_t *tempargs = *(loopargs_t **) args;
1063 unsigned char *buf = tempargs->buf;
1064 EC_KEY **ecdsa = tempargs->ecdsa;
1065 unsigned char *ecdsasig = tempargs->buf2;
1066 unsigned int ecdsasiglen = tempargs->siglen;
1068 for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1069 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1071 BIO_printf(bio_err, "ECDSA verify failure\n");
1072 ERR_print_errors(bio_err);
1080 /* ******************************************************************** */
1081 static long ecdh_c[EC_NUM][1];
1083 static int ECDH_EVP_derive_key_loop(void *args)
1085 loopargs_t *tempargs = *(loopargs_t **) args;
1086 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1087 unsigned char *derived_secret = tempargs->secret_a;
1089 size_t *outlen = &(tempargs->outlen[testnum]);
1091 for (count = 0; COND(ecdh_c[testnum][0]); count++)
1092 EVP_PKEY_derive(ctx, derived_secret, outlen);
1097 #endif /* OPENSSL_NO_EC */
1099 static int run_benchmark(int async_jobs,
1100 int (*loop_function) (void *), loopargs_t * loopargs)
1102 int job_op_count = 0;
1103 int total_op_count = 0;
1104 int num_inprogress = 0;
1105 int error = 0, i = 0, ret = 0;
1106 OSSL_ASYNC_FD job_fd = 0;
1107 size_t num_job_fds = 0;
1111 if (async_jobs == 0) {
1112 return loop_function((void *)&loopargs);
1115 for (i = 0; i < async_jobs && !error; i++) {
1116 loopargs_t *looparg_item = loopargs + i;
1118 /* Copy pointer content (looparg_t item address) into async context */
1119 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1120 &job_op_count, loop_function,
1121 (void *)&looparg_item, sizeof(looparg_item));
1127 if (job_op_count == -1) {
1130 total_op_count += job_op_count;
1135 BIO_printf(bio_err, "Failure in the job\n");
1136 ERR_print_errors(bio_err);
1142 while (num_inprogress > 0) {
1143 #if defined(OPENSSL_SYS_WINDOWS)
1145 #elif defined(OPENSSL_SYS_UNIX)
1146 int select_result = 0;
1147 OSSL_ASYNC_FD max_fd = 0;
1150 FD_ZERO(&waitfdset);
1152 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1153 if (loopargs[i].inprogress_job == NULL)
1156 if (!ASYNC_WAIT_CTX_get_all_fds
1157 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1158 || num_job_fds > 1) {
1159 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1160 ERR_print_errors(bio_err);
1164 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1166 FD_SET(job_fd, &waitfdset);
1167 if (job_fd > max_fd)
1171 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1173 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1174 "Decrease the value of async_jobs\n",
1175 max_fd, FD_SETSIZE);
1176 ERR_print_errors(bio_err);
1181 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1182 if (select_result == -1 && errno == EINTR)
1185 if (select_result == -1) {
1186 BIO_printf(bio_err, "Failure in the select\n");
1187 ERR_print_errors(bio_err);
1192 if (select_result == 0)
1196 for (i = 0; i < async_jobs; i++) {
1197 if (loopargs[i].inprogress_job == NULL)
1200 if (!ASYNC_WAIT_CTX_get_all_fds
1201 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1202 || num_job_fds > 1) {
1203 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1204 ERR_print_errors(bio_err);
1208 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1211 #if defined(OPENSSL_SYS_UNIX)
1212 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1214 #elif defined(OPENSSL_SYS_WINDOWS)
1215 if (num_job_fds == 1
1216 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
1221 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1222 loopargs[i].wait_ctx, &job_op_count,
1223 loop_function, (void *)(loopargs + i),
1224 sizeof(loopargs_t));
1229 if (job_op_count == -1) {
1232 total_op_count += job_op_count;
1235 loopargs[i].inprogress_job = NULL;
1240 loopargs[i].inprogress_job = NULL;
1241 BIO_printf(bio_err, "Failure in the job\n");
1242 ERR_print_errors(bio_err);
1249 return error ? -1 : total_op_count;
1252 int speed_main(int argc, char **argv)
1255 int (*loopfunc)(void *args);
1256 loopargs_t *loopargs = NULL;
1258 int loopargs_len = 0;
1260 const char *engine_id = NULL;
1261 const EVP_CIPHER *evp_cipher = NULL;
1264 int multiblock = 0, pr_header = 0;
1265 int doit[ALGOR_NUM] = { 0 };
1266 int ret = 1, i, k, misalign = 0;
1268 int size_num = OSSL_NELEM(lengths_list);
1272 unsigned int async_jobs = 0;
1273 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
1274 || !defined(OPENSSL_NO_EC)
1278 /* What follows are the buffers and key material. */
1279 #ifndef OPENSSL_NO_RC5
1282 #ifndef OPENSSL_NO_RC2
1285 #ifndef OPENSSL_NO_IDEA
1286 IDEA_KEY_SCHEDULE idea_ks;
1288 #ifndef OPENSSL_NO_SEED
1289 SEED_KEY_SCHEDULE seed_ks;
1291 #ifndef OPENSSL_NO_BF
1294 #ifndef OPENSSL_NO_CAST
1297 static const unsigned char key16[16] = {
1298 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1299 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1301 static const unsigned char key24[24] = {
1302 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1303 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1304 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1306 static const unsigned char key32[32] = {
1307 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1308 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1309 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1310 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1312 #ifndef OPENSSL_NO_CAMELLIA
1313 static const unsigned char ckey24[24] = {
1314 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1315 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1316 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1318 static const unsigned char ckey32[32] = {
1319 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1320 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1321 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1322 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1324 CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1326 #ifndef OPENSSL_NO_DES
1327 static DES_cblock key = {
1328 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
1330 static DES_cblock key2 = {
1331 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1333 static DES_cblock key3 = {
1334 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1337 #ifndef OPENSSL_NO_RSA
1338 static const unsigned int rsa_bits[RSA_NUM] = {
1339 512, 1024, 2048, 3072, 4096, 7680, 15360
1341 static const unsigned char *rsa_data[RSA_NUM] = {
1342 test512, test1024, test2048, test3072, test4096, test7680, test15360
1344 static const int rsa_data_length[RSA_NUM] = {
1345 sizeof(test512), sizeof(test1024),
1346 sizeof(test2048), sizeof(test3072),
1347 sizeof(test4096), sizeof(test7680),
1350 int rsa_doit[RSA_NUM] = { 0 };
1351 int primes = RSA_DEFAULT_PRIME_NUM;
1353 #ifndef OPENSSL_NO_DSA
1354 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1355 int dsa_doit[DSA_NUM] = { 0 };
1357 #ifndef OPENSSL_NO_EC
1359 * We only test over the following curves as they are representative, To
1360 * add tests over more curves, simply add the curve NID and curve name to
1361 * the following arrays and increase the EC_NUM value accordingly.
1363 static const unsigned int test_curves[EC_NUM] = {
1365 NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
1366 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
1368 NID_sect163k1, NID_sect233k1, NID_sect283k1,
1369 NID_sect409k1, NID_sect571k1, NID_sect163r2,
1370 NID_sect233r1, NID_sect283r1, NID_sect409r1,
1375 static const char *test_curves_names[EC_NUM] = {
1377 "secp160r1", "nistp192", "nistp224",
1378 "nistp256", "nistp384", "nistp521",
1380 "nistk163", "nistk233", "nistk283",
1381 "nistk409", "nistk571", "nistb163",
1382 "nistb233", "nistb283", "nistb409",
1387 static const int test_curves_bits[EC_NUM] = {
1393 571, 253 /* X25519 */
1396 int ecdsa_doit[EC_NUM] = { 0 };
1397 int ecdh_doit[EC_NUM] = { 0 };
1398 #endif /* ndef OPENSSL_NO_EC */
1400 SEC seconds = {SECONDS, RSA_SECONDS, DSA_SECONDS, ECDSA_SECONDS,
1403 prog = opt_init(argc, argv, speed_options);
1404 while ((o = opt_next()) != OPT_EOF) {
1409 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1412 opt_help(speed_options);
1420 evp_cipher = EVP_get_cipherbyname(opt_arg());
1421 if (evp_cipher == NULL)
1422 evp_md = EVP_get_digestbyname(opt_arg());
1423 if (evp_cipher == NULL && evp_md == NULL) {
1425 "%s: %s is an unknown cipher or digest\n",
1436 * In a forked execution, an engine might need to be
1437 * initialised by each child process, not by the parent.
1438 * So store the name here and run setup_engine() later on.
1440 engine_id = opt_arg();
1444 multi = atoi(opt_arg());
1448 #ifndef OPENSSL_NO_ASYNC
1449 async_jobs = atoi(opt_arg());
1450 if (!ASYNC_is_capable()) {
1452 "%s: async_jobs specified but async not supported\n",
1456 if (async_jobs > 99999) {
1458 "%s: too many async_jobs\n",
1465 if (!opt_int(opt_arg(), &misalign))
1467 if (misalign > MISALIGN) {
1469 "%s: Maximum offset is %d\n", prog, MISALIGN);
1478 #ifdef OPENSSL_NO_MULTIBLOCK
1480 "%s: -mb specified but multi-block support is disabled\n",
1490 if (!opt_int(opt_arg(), &primes))
1494 seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1495 = seconds.ecdh = atoi(opt_arg());
1498 lengths_single = atoi(opt_arg());
1499 lengths = &lengths_single;
1504 argc = opt_num_rest();
1507 /* Remaining arguments are algorithms. */
1508 for (; *argv; argv++) {
1509 if (found(*argv, doit_choices, &i)) {
1513 #ifndef OPENSSL_NO_DES
1514 if (strcmp(*argv, "des") == 0) {
1515 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1519 if (strcmp(*argv, "sha") == 0) {
1520 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1523 #ifndef OPENSSL_NO_RSA
1524 if (strcmp(*argv, "openssl") == 0)
1526 if (strcmp(*argv, "rsa") == 0) {
1527 rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
1528 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
1529 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
1530 rsa_doit[R_RSA_15360] = 1;
1533 if (found(*argv, rsa_choices, &i)) {
1538 #ifndef OPENSSL_NO_DSA
1539 if (strcmp(*argv, "dsa") == 0) {
1540 dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1541 dsa_doit[R_DSA_2048] = 1;
1544 if (found(*argv, dsa_choices, &i)) {
1549 if (strcmp(*argv, "aes") == 0) {
1550 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1553 #ifndef OPENSSL_NO_CAMELLIA
1554 if (strcmp(*argv, "camellia") == 0) {
1555 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1559 #ifndef OPENSSL_NO_EC
1560 if (strcmp(*argv, "ecdsa") == 0) {
1561 for (i = 0; i < EC_NUM; i++)
1565 if (found(*argv, ecdsa_choices, &i)) {
1569 if (strcmp(*argv, "ecdh") == 0) {
1570 for (i = 0; i < EC_NUM; i++)
1574 if (found(*argv, ecdh_choices, &i)) {
1579 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1583 /* Initialize the job pool if async mode is enabled */
1584 if (async_jobs > 0) {
1585 async_init = ASYNC_init_thread(async_jobs, async_jobs);
1587 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1592 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1594 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1595 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1597 for (i = 0; i < loopargs_len; i++) {
1598 if (async_jobs > 0) {
1599 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1600 if (loopargs[i].wait_ctx == NULL) {
1601 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1606 loopargs[i].buf_malloc =
1607 app_malloc(lengths[size_num - 1] + MAX_MISALIGNMENT + 1,
1609 loopargs[i].buf2_malloc =
1610 app_malloc(lengths[size_num - 1] + MAX_MISALIGNMENT + 1,
1612 /* Align the start of buffers on a 64 byte boundary */
1613 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1614 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1615 #ifndef OPENSSL_NO_EC
1616 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1617 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1622 if (multi && do_multi(multi, size_num))
1626 /* Initialize the engine after the fork */
1627 e = setup_engine(engine_id, 0);
1629 /* No parameters; turn on everything. */
1630 if ((argc == 0) && !doit[D_EVP]) {
1631 for (i = 0; i < ALGOR_NUM; i++)
1634 #ifndef OPENSSL_NO_RSA
1635 for (i = 0; i < RSA_NUM; i++)
1638 #ifndef OPENSSL_NO_DSA
1639 for (i = 0; i < DSA_NUM; i++)
1642 #ifndef OPENSSL_NO_EC
1643 for (i = 0; i < EC_NUM; i++)
1645 for (i = 0; i < EC_NUM; i++)
1649 for (i = 0; i < ALGOR_NUM; i++)
1653 if (usertime == 0 && !mr)
1655 "You have chosen to measure elapsed time "
1656 "instead of user CPU time.\n");
1658 #ifndef OPENSSL_NO_RSA
1659 for (i = 0; i < loopargs_len; i++) {
1660 if (primes > RSA_DEFAULT_PRIME_NUM) {
1661 /* for multi-prime RSA, skip this */
1664 for (k = 0; k < RSA_NUM; k++) {
1665 const unsigned char *p;
1668 loopargs[i].rsa_key[k] =
1669 d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1670 if (loopargs[i].rsa_key[k] == NULL) {
1672 "internal error loading RSA key number %d\n", k);
1678 #ifndef OPENSSL_NO_DSA
1679 for (i = 0; i < loopargs_len; i++) {
1680 loopargs[i].dsa_key[0] = get_dsa(512);
1681 loopargs[i].dsa_key[1] = get_dsa(1024);
1682 loopargs[i].dsa_key[2] = get_dsa(2048);
1685 #ifndef OPENSSL_NO_DES
1686 DES_set_key_unchecked(&key, &sch);
1687 DES_set_key_unchecked(&key2, &sch2);
1688 DES_set_key_unchecked(&key3, &sch3);
1690 AES_set_encrypt_key(key16, 128, &aes_ks1);
1691 AES_set_encrypt_key(key24, 192, &aes_ks2);
1692 AES_set_encrypt_key(key32, 256, &aes_ks3);
1693 #ifndef OPENSSL_NO_CAMELLIA
1694 Camellia_set_key(key16, 128, &camellia_ks1);
1695 Camellia_set_key(ckey24, 192, &camellia_ks2);
1696 Camellia_set_key(ckey32, 256, &camellia_ks3);
1698 #ifndef OPENSSL_NO_IDEA
1699 IDEA_set_encrypt_key(key16, &idea_ks);
1701 #ifndef OPENSSL_NO_SEED
1702 SEED_set_key(key16, &seed_ks);
1704 #ifndef OPENSSL_NO_RC4
1705 RC4_set_key(&rc4_ks, 16, key16);
1707 #ifndef OPENSSL_NO_RC2
1708 RC2_set_key(&rc2_ks, 16, key16, 128);
1710 #ifndef OPENSSL_NO_RC5
1711 RC5_32_set_key(&rc5_ks, 16, key16, 12);
1713 #ifndef OPENSSL_NO_BF
1714 BF_set_key(&bf_ks, 16, key16);
1716 #ifndef OPENSSL_NO_CAST
1717 CAST_set_key(&cast_ks, 16, key16);
1720 # ifndef OPENSSL_NO_DES
1721 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1727 for (it = count; it; it--)
1728 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
1729 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1733 c[D_MD2][0] = count / 10;
1734 c[D_MDC2][0] = count / 10;
1735 c[D_MD4][0] = count;
1736 c[D_MD5][0] = count;
1737 c[D_HMAC][0] = count;
1738 c[D_SHA1][0] = count;
1739 c[D_RMD160][0] = count;
1740 c[D_RC4][0] = count * 5;
1741 c[D_CBC_DES][0] = count;
1742 c[D_EDE3_DES][0] = count / 3;
1743 c[D_CBC_IDEA][0] = count;
1744 c[D_CBC_SEED][0] = count;
1745 c[D_CBC_RC2][0] = count;
1746 c[D_CBC_RC5][0] = count;
1747 c[D_CBC_BF][0] = count;
1748 c[D_CBC_CAST][0] = count;
1749 c[D_CBC_128_AES][0] = count;
1750 c[D_CBC_192_AES][0] = count;
1751 c[D_CBC_256_AES][0] = count;
1752 c[D_CBC_128_CML][0] = count;
1753 c[D_CBC_192_CML][0] = count;
1754 c[D_CBC_256_CML][0] = count;
1755 c[D_SHA256][0] = count;
1756 c[D_SHA512][0] = count;
1757 c[D_WHIRLPOOL][0] = count;
1758 c[D_IGE_128_AES][0] = count;
1759 c[D_IGE_192_AES][0] = count;
1760 c[D_IGE_256_AES][0] = count;
1761 c[D_GHASH][0] = count;
1762 c[D_RAND][0] = count;
1764 for (i = 1; i < size_num; i++) {
1767 l0 = (long)lengths[0];
1768 l1 = (long)lengths[i];
1770 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1771 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1772 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1773 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1774 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1775 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1776 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1777 c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1778 c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1779 c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1780 c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1781 c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
1783 l0 = (long)lengths[i - 1];
1785 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1786 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1787 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1788 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1789 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1790 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1791 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1792 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1793 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1794 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1795 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1796 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1797 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1798 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1799 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1800 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1801 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1802 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1805 # ifndef OPENSSL_NO_RSA
1806 rsa_c[R_RSA_512][0] = count / 2000;
1807 rsa_c[R_RSA_512][1] = count / 400;
1808 for (i = 1; i < RSA_NUM; i++) {
1809 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1810 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1811 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
1814 if (rsa_c[i][0] == 0) {
1815 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1822 # ifndef OPENSSL_NO_DSA
1823 dsa_c[R_DSA_512][0] = count / 1000;
1824 dsa_c[R_DSA_512][1] = count / 1000 / 2;
1825 for (i = 1; i < DSA_NUM; i++) {
1826 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1827 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1828 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
1831 if (dsa_c[i][0] == 0) {
1832 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1839 # ifndef OPENSSL_NO_EC
1840 ecdsa_c[R_EC_P160][0] = count / 1000;
1841 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1842 for (i = R_EC_P192; i <= R_EC_P521; i++) {
1843 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1844 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1845 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1848 if (ecdsa_c[i][0] == 0) {
1854 ecdsa_c[R_EC_K163][0] = count / 1000;
1855 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1856 for (i = R_EC_K233; i <= R_EC_K571; i++) {
1857 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1858 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1859 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1862 if (ecdsa_c[i][0] == 0) {
1868 ecdsa_c[R_EC_B163][0] = count / 1000;
1869 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1870 for (i = R_EC_B233; i <= R_EC_B571; i++) {
1871 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1872 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1873 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1876 if (ecdsa_c[i][0] == 0) {
1883 ecdh_c[R_EC_P160][0] = count / 1000;
1884 for (i = R_EC_P192; i <= R_EC_P521; i++) {
1885 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1886 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1889 if (ecdh_c[i][0] == 0) {
1894 ecdh_c[R_EC_K163][0] = count / 1000;
1895 for (i = R_EC_K233; i <= R_EC_K571; i++) {
1896 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1897 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1900 if (ecdh_c[i][0] == 0) {
1905 ecdh_c[R_EC_B163][0] = count / 1000;
1906 for (i = R_EC_B233; i <= R_EC_B571; i++) {
1907 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1908 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1911 if (ecdh_c[i][0] == 0) {
1919 /* not worth fixing */
1920 # error "You cannot disable DES on systems without SIGALRM."
1921 # endif /* OPENSSL_NO_DES */
1924 signal(SIGALRM, sig_done);
1926 #endif /* SIGALRM */
1928 #ifndef OPENSSL_NO_MD2
1930 for (testnum = 0; testnum < size_num; testnum++) {
1931 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
1934 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1936 print_result(D_MD2, testnum, count, d);
1940 #ifndef OPENSSL_NO_MDC2
1942 for (testnum = 0; testnum < size_num; testnum++) {
1943 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
1946 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1948 print_result(D_MDC2, testnum, count, d);
1953 #ifndef OPENSSL_NO_MD4
1955 for (testnum = 0; testnum < size_num; testnum++) {
1956 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
1959 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1961 print_result(D_MD4, testnum, count, d);
1966 #ifndef OPENSSL_NO_MD5
1968 for (testnum = 0; testnum < size_num; testnum++) {
1969 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
1972 count = run_benchmark(async_jobs, MD5_loop, loopargs);
1974 print_result(D_MD5, testnum, count, d);
1979 static const char hmac_key[] = "This is a key...";
1980 int len = strlen(hmac_key);
1982 for (i = 0; i < loopargs_len; i++) {
1983 loopargs[i].hctx = HMAC_CTX_new();
1984 if (loopargs[i].hctx == NULL) {
1985 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
1989 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
1991 for (testnum = 0; testnum < size_num; testnum++) {
1992 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
1995 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
1997 print_result(D_HMAC, testnum, count, d);
1999 for (i = 0; i < loopargs_len; i++) {
2000 HMAC_CTX_free(loopargs[i].hctx);
2005 for (testnum = 0; testnum < size_num; testnum++) {
2006 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2009 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2011 print_result(D_SHA1, testnum, count, d);
2014 if (doit[D_SHA256]) {
2015 for (testnum = 0; testnum < size_num; testnum++) {
2016 print_message(names[D_SHA256], c[D_SHA256][testnum],
2017 lengths[testnum], seconds.sym);
2019 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2021 print_result(D_SHA256, testnum, count, d);
2024 if (doit[D_SHA512]) {
2025 for (testnum = 0; testnum < size_num; testnum++) {
2026 print_message(names[D_SHA512], c[D_SHA512][testnum],
2027 lengths[testnum], seconds.sym);
2029 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2031 print_result(D_SHA512, testnum, count, d);
2034 #ifndef OPENSSL_NO_WHIRLPOOL
2035 if (doit[D_WHIRLPOOL]) {
2036 for (testnum = 0; testnum < size_num; testnum++) {
2037 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2038 lengths[testnum], seconds.sym);
2040 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2042 print_result(D_WHIRLPOOL, testnum, count, d);
2047 #ifndef OPENSSL_NO_RMD160
2048 if (doit[D_RMD160]) {
2049 for (testnum = 0; testnum < size_num; testnum++) {
2050 print_message(names[D_RMD160], c[D_RMD160][testnum],
2051 lengths[testnum], seconds.sym);
2053 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2055 print_result(D_RMD160, testnum, count, d);
2059 #ifndef OPENSSL_NO_RC4
2061 for (testnum = 0; testnum < size_num; testnum++) {
2062 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2065 count = run_benchmark(async_jobs, RC4_loop, loopargs);
2067 print_result(D_RC4, testnum, count, d);
2071 #ifndef OPENSSL_NO_DES
2072 if (doit[D_CBC_DES]) {
2073 for (testnum = 0; testnum < size_num; testnum++) {
2074 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2075 lengths[testnum], seconds.sym);
2077 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2079 print_result(D_CBC_DES, testnum, count, d);
2083 if (doit[D_EDE3_DES]) {
2084 for (testnum = 0; testnum < size_num; testnum++) {
2085 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2086 lengths[testnum], seconds.sym);
2089 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2091 print_result(D_EDE3_DES, testnum, count, d);
2096 if (doit[D_CBC_128_AES]) {
2097 for (testnum = 0; testnum < size_num; testnum++) {
2098 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2099 lengths[testnum], seconds.sym);
2102 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2104 print_result(D_CBC_128_AES, testnum, count, d);
2107 if (doit[D_CBC_192_AES]) {
2108 for (testnum = 0; testnum < size_num; testnum++) {
2109 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2110 lengths[testnum], seconds.sym);
2113 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2115 print_result(D_CBC_192_AES, testnum, count, d);
2118 if (doit[D_CBC_256_AES]) {
2119 for (testnum = 0; testnum < size_num; testnum++) {
2120 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2121 lengths[testnum], seconds.sym);
2124 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2126 print_result(D_CBC_256_AES, testnum, count, d);
2130 if (doit[D_IGE_128_AES]) {
2131 for (testnum = 0; testnum < size_num; testnum++) {
2132 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2133 lengths[testnum], seconds.sym);
2136 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2138 print_result(D_IGE_128_AES, testnum, count, d);
2141 if (doit[D_IGE_192_AES]) {
2142 for (testnum = 0; testnum < size_num; testnum++) {
2143 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2144 lengths[testnum], seconds.sym);
2147 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2149 print_result(D_IGE_192_AES, testnum, count, d);
2152 if (doit[D_IGE_256_AES]) {
2153 for (testnum = 0; testnum < size_num; testnum++) {
2154 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2155 lengths[testnum], seconds.sym);
2158 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2160 print_result(D_IGE_256_AES, testnum, count, d);
2163 if (doit[D_GHASH]) {
2164 for (i = 0; i < loopargs_len; i++) {
2165 loopargs[i].gcm_ctx =
2166 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2167 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2168 (unsigned char *)"0123456789ab", 12);
2171 for (testnum = 0; testnum < size_num; testnum++) {
2172 print_message(names[D_GHASH], c[D_GHASH][testnum],
2173 lengths[testnum], seconds.sym);
2175 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2177 print_result(D_GHASH, testnum, count, d);
2179 for (i = 0; i < loopargs_len; i++)
2180 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2182 #ifndef OPENSSL_NO_CAMELLIA
2183 if (doit[D_CBC_128_CML]) {
2184 if (async_jobs > 0) {
2185 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2186 names[D_CBC_128_CML]);
2187 doit[D_CBC_128_CML] = 0;
2189 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2190 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2191 lengths[testnum], seconds.sym);
2193 for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2194 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2195 (size_t)lengths[testnum], &camellia_ks1,
2196 iv, CAMELLIA_ENCRYPT);
2198 print_result(D_CBC_128_CML, testnum, count, d);
2201 if (doit[D_CBC_192_CML]) {
2202 if (async_jobs > 0) {
2203 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2204 names[D_CBC_192_CML]);
2205 doit[D_CBC_192_CML] = 0;
2207 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2208 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2209 lengths[testnum], seconds.sym);
2210 if (async_jobs > 0) {
2211 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2215 for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2216 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2217 (size_t)lengths[testnum], &camellia_ks2,
2218 iv, CAMELLIA_ENCRYPT);
2220 print_result(D_CBC_192_CML, testnum, count, d);
2223 if (doit[D_CBC_256_CML]) {
2224 if (async_jobs > 0) {
2225 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2226 names[D_CBC_256_CML]);
2227 doit[D_CBC_256_CML] = 0;
2229 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2230 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2231 lengths[testnum], seconds.sym);
2233 for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2234 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2235 (size_t)lengths[testnum], &camellia_ks3,
2236 iv, CAMELLIA_ENCRYPT);
2238 print_result(D_CBC_256_CML, testnum, count, d);
2242 #ifndef OPENSSL_NO_IDEA
2243 if (doit[D_CBC_IDEA]) {
2244 if (async_jobs > 0) {
2245 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2247 doit[D_CBC_IDEA] = 0;
2249 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2250 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2251 lengths[testnum], seconds.sym);
2253 for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
2254 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2255 (size_t)lengths[testnum], &idea_ks,
2258 print_result(D_CBC_IDEA, testnum, count, d);
2262 #ifndef OPENSSL_NO_SEED
2263 if (doit[D_CBC_SEED]) {
2264 if (async_jobs > 0) {
2265 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2267 doit[D_CBC_SEED] = 0;
2269 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2270 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2271 lengths[testnum], seconds.sym);
2273 for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2274 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2275 (size_t)lengths[testnum], &seed_ks, iv, 1);
2277 print_result(D_CBC_SEED, testnum, count, d);
2281 #ifndef OPENSSL_NO_RC2
2282 if (doit[D_CBC_RC2]) {
2283 if (async_jobs > 0) {
2284 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2286 doit[D_CBC_RC2] = 0;
2288 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2289 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2290 lengths[testnum], seconds.sym);
2291 if (async_jobs > 0) {
2292 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2296 for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2297 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2298 (size_t)lengths[testnum], &rc2_ks,
2301 print_result(D_CBC_RC2, testnum, count, d);
2305 #ifndef OPENSSL_NO_RC5
2306 if (doit[D_CBC_RC5]) {
2307 if (async_jobs > 0) {
2308 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2310 doit[D_CBC_RC5] = 0;
2312 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2313 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2314 lengths[testnum], seconds.sym);
2315 if (async_jobs > 0) {
2316 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2320 for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2321 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2322 (size_t)lengths[testnum], &rc5_ks,
2325 print_result(D_CBC_RC5, testnum, count, d);
2329 #ifndef OPENSSL_NO_BF
2330 if (doit[D_CBC_BF]) {
2331 if (async_jobs > 0) {
2332 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2336 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2337 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2338 lengths[testnum], seconds.sym);
2340 for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2341 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2342 (size_t)lengths[testnum], &bf_ks,
2345 print_result(D_CBC_BF, testnum, count, d);
2349 #ifndef OPENSSL_NO_CAST
2350 if (doit[D_CBC_CAST]) {
2351 if (async_jobs > 0) {
2352 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2354 doit[D_CBC_CAST] = 0;
2356 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2357 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2358 lengths[testnum], seconds.sym);
2360 for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2361 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2362 (size_t)lengths[testnum], &cast_ks,
2365 print_result(D_CBC_CAST, testnum, count, d);
2370 for (testnum = 0; testnum < size_num; testnum++) {
2371 print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2374 count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2376 print_result(D_RAND, testnum, count, d);
2381 if (multiblock && evp_cipher) {
2383 (EVP_CIPHER_flags(evp_cipher) &
2384 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2385 BIO_printf(bio_err, "%s is not multi-block capable\n",
2386 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2389 if (async_jobs > 0) {
2390 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2393 multiblock_speed(evp_cipher, &seconds);
2397 for (testnum = 0; testnum < size_num; testnum++) {
2400 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2402 * -O3 -fschedule-insns messes up an optimization here!
2403 * names[D_EVP] somehow becomes NULL
2405 print_message(names[D_EVP], save_count, lengths[testnum],
2408 for (k = 0; k < loopargs_len; k++) {
2409 loopargs[k].ctx = EVP_CIPHER_CTX_new();
2411 EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2414 EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2416 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2418 switch (EVP_CIPHER_mode(evp_cipher)) {
2419 case EVP_CIPH_CCM_MODE:
2420 loopfunc = EVP_Update_loop_ccm;
2423 loopfunc = EVP_Update_loop;
2427 count = run_benchmark(async_jobs, loopfunc, loopargs);
2429 for (k = 0; k < loopargs_len; k++) {
2430 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2434 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2435 print_message(names[D_EVP], save_count, lengths[testnum],
2438 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2441 print_result(D_EVP, testnum, count, d);
2445 for (i = 0; i < loopargs_len; i++)
2446 RAND_bytes(loopargs[i].buf, 36);
2448 #ifndef OPENSSL_NO_RSA
2449 for (testnum = 0; testnum < RSA_NUM; testnum++) {
2451 if (!rsa_doit[testnum])
2453 for (i = 0; i < loopargs_len; i++) {
2455 /* we haven't set keys yet, generate multi-prime RSA keys */
2456 BIGNUM *bn = BN_new();
2460 if (!BN_set_word(bn, RSA_F4)) {
2465 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
2466 rsa_choices[testnum].name);
2468 loopargs[i].rsa_key[testnum] = RSA_new();
2469 if (loopargs[i].rsa_key[testnum] == NULL) {
2474 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
2476 primes, bn, NULL)) {
2482 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2483 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2489 "RSA sign failure. No RSA sign will be done.\n");
2490 ERR_print_errors(bio_err);
2493 pkey_print_message("private", "rsa",
2494 rsa_c[testnum][0], rsa_bits[testnum],
2496 /* RSA_blinding_on(rsa_key[testnum],NULL); */
2498 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2501 mr ? "+R1:%ld:%d:%.2f\n"
2502 : "%ld %d bit private RSA's in %.2fs\n",
2503 count, rsa_bits[testnum], d);
2504 rsa_results[testnum][0] = (double)count / d;
2508 for (i = 0; i < loopargs_len; i++) {
2509 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2510 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2516 "RSA verify failure. No RSA verify will be done.\n");
2517 ERR_print_errors(bio_err);
2518 rsa_doit[testnum] = 0;
2520 pkey_print_message("public", "rsa",
2521 rsa_c[testnum][1], rsa_bits[testnum],
2524 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2527 mr ? "+R2:%ld:%d:%.2f\n"
2528 : "%ld %d bit public RSA's in %.2fs\n",
2529 count, rsa_bits[testnum], d);
2530 rsa_results[testnum][1] = (double)count / d;
2533 if (rsa_count <= 1) {
2534 /* if longer than 10s, don't do any more */
2535 for (testnum++; testnum < RSA_NUM; testnum++)
2536 rsa_doit[testnum] = 0;
2539 #endif /* OPENSSL_NO_RSA */
2541 for (i = 0; i < loopargs_len; i++)
2542 RAND_bytes(loopargs[i].buf, 36);
2544 #ifndef OPENSSL_NO_DSA
2545 for (testnum = 0; testnum < DSA_NUM; testnum++) {
2547 if (!dsa_doit[testnum])
2550 /* DSA_generate_key(dsa_key[testnum]); */
2551 /* DSA_sign_setup(dsa_key[testnum],NULL); */
2552 for (i = 0; i < loopargs_len; i++) {
2553 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2554 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2560 "DSA sign failure. No DSA sign will be done.\n");
2561 ERR_print_errors(bio_err);
2564 pkey_print_message("sign", "dsa",
2565 dsa_c[testnum][0], dsa_bits[testnum],
2568 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2571 mr ? "+R3:%ld:%d:%.2f\n"
2572 : "%ld %d bit DSA signs in %.2fs\n",
2573 count, dsa_bits[testnum], d);
2574 dsa_results[testnum][0] = (double)count / d;
2578 for (i = 0; i < loopargs_len; i++) {
2579 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2580 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2586 "DSA verify failure. No DSA verify will be done.\n");
2587 ERR_print_errors(bio_err);
2588 dsa_doit[testnum] = 0;
2590 pkey_print_message("verify", "dsa",
2591 dsa_c[testnum][1], dsa_bits[testnum],
2594 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2597 mr ? "+R4:%ld:%d:%.2f\n"
2598 : "%ld %d bit DSA verify in %.2fs\n",
2599 count, dsa_bits[testnum], d);
2600 dsa_results[testnum][1] = (double)count / d;
2603 if (rsa_count <= 1) {
2604 /* if longer than 10s, don't do any more */
2605 for (testnum++; testnum < DSA_NUM; testnum++)
2606 dsa_doit[testnum] = 0;
2609 #endif /* OPENSSL_NO_DSA */
2611 #ifndef OPENSSL_NO_EC
2612 for (testnum = 0; testnum < EC_NUM; testnum++) {
2615 if (!ecdsa_doit[testnum])
2616 continue; /* Ignore Curve */
2617 for (i = 0; i < loopargs_len; i++) {
2618 loopargs[i].ecdsa[testnum] =
2619 EC_KEY_new_by_curve_name(test_curves[testnum]);
2620 if (loopargs[i].ecdsa[testnum] == NULL) {
2626 BIO_printf(bio_err, "ECDSA failure.\n");
2627 ERR_print_errors(bio_err);
2630 for (i = 0; i < loopargs_len; i++) {
2631 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
2632 /* Perform ECDSA signature test */
2633 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
2634 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2635 &loopargs[i].siglen,
2636 loopargs[i].ecdsa[testnum]);
2642 "ECDSA sign failure. No ECDSA sign will be done.\n");
2643 ERR_print_errors(bio_err);
2646 pkey_print_message("sign", "ecdsa",
2647 ecdsa_c[testnum][0],
2648 test_curves_bits[testnum],
2651 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2655 mr ? "+R5:%ld:%d:%.2f\n" :
2656 "%ld %d bit ECDSA signs in %.2fs \n",
2657 count, test_curves_bits[testnum], d);
2658 ecdsa_results[testnum][0] = (double)count / d;
2662 /* Perform ECDSA verification test */
2663 for (i = 0; i < loopargs_len; i++) {
2664 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2666 loopargs[i].ecdsa[testnum]);
2672 "ECDSA verify failure. No ECDSA verify will be done.\n");
2673 ERR_print_errors(bio_err);
2674 ecdsa_doit[testnum] = 0;
2676 pkey_print_message("verify", "ecdsa",
2677 ecdsa_c[testnum][1],
2678 test_curves_bits[testnum],
2681 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2684 mr ? "+R6:%ld:%d:%.2f\n"
2685 : "%ld %d bit ECDSA verify in %.2fs\n",
2686 count, test_curves_bits[testnum], d);
2687 ecdsa_results[testnum][1] = (double)count / d;
2690 if (rsa_count <= 1) {
2691 /* if longer than 10s, don't do any more */
2692 for (testnum++; testnum < EC_NUM; testnum++)
2693 ecdsa_doit[testnum] = 0;
2698 for (testnum = 0; testnum < EC_NUM; testnum++) {
2699 int ecdh_checks = 1;
2701 if (!ecdh_doit[testnum])
2704 for (i = 0; i < loopargs_len; i++) {
2705 EVP_PKEY_CTX *kctx = NULL;
2706 EVP_PKEY_CTX *test_ctx = NULL;
2707 EVP_PKEY_CTX *ctx = NULL;
2708 EVP_PKEY *key_A = NULL;
2709 EVP_PKEY *key_B = NULL;
2713 /* Ensure that the error queue is empty */
2714 if (ERR_peek_error()) {
2716 "WARNING: the error queue contains previous unhandled errors.\n");
2717 ERR_print_errors(bio_err);
2720 /* Let's try to create a ctx directly from the NID: this works for
2721 * curves like Curve25519 that are not implemented through the low
2722 * level EC interface.
2723 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
2724 * then we set the curve by NID before deriving the actual keygen
2725 * ctx for that specific curve. */
2726 kctx = EVP_PKEY_CTX_new_id(test_curves[testnum], NULL); /* keygen ctx from NID */
2728 EVP_PKEY_CTX *pctx = NULL;
2729 EVP_PKEY *params = NULL;
2731 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
2732 * "int_ctx_new:unsupported algorithm" error was added to the
2734 * We remove it from the error queue as we are handling it. */
2735 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
2736 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
2737 /* check that the error origin matches */
2738 ERR_GET_LIB(error) == ERR_LIB_EVP &&
2739 ERR_GET_FUNC(error) == EVP_F_INT_CTX_NEW &&
2740 ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
2741 ERR_get_error(); /* pop error from queue */
2742 if (ERR_peek_error()) {
2744 "Unhandled error in the error queue during ECDH init.\n");
2745 ERR_print_errors(bio_err);
2750 if ( /* Create the context for parameter generation */
2751 !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
2752 /* Initialise the parameter generation */
2753 !EVP_PKEY_paramgen_init(pctx) ||
2754 /* Set the curve by NID */
2755 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
2758 /* Create the parameter object params */
2759 !EVP_PKEY_paramgen(pctx, ¶ms)) {
2761 BIO_printf(bio_err, "ECDH EC params init failure.\n");
2762 ERR_print_errors(bio_err);
2766 /* Create the context for the key generation */
2767 kctx = EVP_PKEY_CTX_new(params, NULL);
2769 EVP_PKEY_free(params);
2771 EVP_PKEY_CTX_free(pctx);
2774 if (kctx == NULL || /* keygen ctx is not null */
2775 !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
2777 BIO_printf(bio_err, "ECDH keygen failure.\n");
2778 ERR_print_errors(bio_err);
2783 if (!EVP_PKEY_keygen(kctx, &key_A) || /* generate secret key A */
2784 !EVP_PKEY_keygen(kctx, &key_B) || /* generate secret key B */
2785 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
2786 !EVP_PKEY_derive_init(ctx) || /* init derivation ctx */
2787 !EVP_PKEY_derive_set_peer(ctx, key_B) || /* set peer pubkey in ctx */
2788 !EVP_PKEY_derive(ctx, NULL, &outlen) || /* determine max length */
2789 outlen == 0 || /* ensure outlen is a valid size */
2790 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
2792 BIO_printf(bio_err, "ECDH key generation failure.\n");
2793 ERR_print_errors(bio_err);
2798 /* Here we perform a test run, comparing the output of a*B and b*A;
2799 * we try this here and assume that further EVP_PKEY_derive calls
2800 * never fail, so we can skip checks in the actually benchmarked
2801 * code, for maximum performance. */
2802 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
2803 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
2804 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
2805 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
2806 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
2807 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
2808 test_outlen != outlen /* compare output length */ ) {
2810 BIO_printf(bio_err, "ECDH computation failure.\n");
2811 ERR_print_errors(bio_err);
2816 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
2817 if (CRYPTO_memcmp(loopargs[i].secret_a,
2818 loopargs[i].secret_b, outlen)) {
2820 BIO_printf(bio_err, "ECDH computations don't match.\n");
2821 ERR_print_errors(bio_err);
2826 loopargs[i].ecdh_ctx[testnum] = ctx;
2827 loopargs[i].outlen[testnum] = outlen;
2829 EVP_PKEY_free(key_A);
2830 EVP_PKEY_free(key_B);
2831 EVP_PKEY_CTX_free(kctx);
2833 EVP_PKEY_CTX_free(test_ctx);
2836 if (ecdh_checks != 0) {
2837 pkey_print_message("", "ecdh",
2839 test_curves_bits[testnum],
2843 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
2846 mr ? "+R7:%ld:%d:%.2f\n" :
2847 "%ld %d-bit ECDH ops in %.2fs\n", count,
2848 test_curves_bits[testnum], d);
2849 ecdh_results[testnum][0] = (double)count / d;
2853 if (rsa_count <= 1) {
2854 /* if longer than 10s, don't do any more */
2855 for (testnum++; testnum < EC_NUM; testnum++)
2856 ecdh_doit[testnum] = 0;
2859 #endif /* OPENSSL_NO_EC */
2864 printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
2865 printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2867 printf("%s ", BN_options());
2868 #ifndef OPENSSL_NO_MD2
2869 printf("%s ", MD2_options());
2871 #ifndef OPENSSL_NO_RC4
2872 printf("%s ", RC4_options());
2874 #ifndef OPENSSL_NO_DES
2875 printf("%s ", DES_options());
2877 printf("%s ", AES_options());
2878 #ifndef OPENSSL_NO_IDEA
2879 printf("%s ", IDEA_options());
2881 #ifndef OPENSSL_NO_BF
2882 printf("%s ", BF_options());
2884 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2892 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2895 for (testnum = 0; testnum < size_num; testnum++)
2896 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2900 for (k = 0; k < ALGOR_NUM; k++) {
2904 printf("+F:%d:%s", k, names[k]);
2906 printf("%-13s", names[k]);
2907 for (testnum = 0; testnum < size_num; testnum++) {
2908 if (results[k][testnum] > 10000 && !mr)
2909 printf(" %11.2fk", results[k][testnum] / 1e3);
2911 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2915 #ifndef OPENSSL_NO_RSA
2917 for (k = 0; k < RSA_NUM; k++) {
2920 if (testnum && !mr) {
2921 printf("%18ssign verify sign/s verify/s\n", " ");
2925 printf("+F2:%u:%u:%f:%f\n",
2926 k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2928 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2929 rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
2930 rsa_results[k][0], rsa_results[k][1]);
2933 #ifndef OPENSSL_NO_DSA
2935 for (k = 0; k < DSA_NUM; k++) {
2938 if (testnum && !mr) {
2939 printf("%18ssign verify sign/s verify/s\n", " ");
2943 printf("+F3:%u:%u:%f:%f\n",
2944 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2946 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2947 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
2948 dsa_results[k][0], dsa_results[k][1]);
2951 #ifndef OPENSSL_NO_EC
2953 for (k = 0; k < EC_NUM; k++) {
2956 if (testnum && !mr) {
2957 printf("%30ssign verify sign/s verify/s\n", " ");
2962 printf("+F4:%u:%u:%f:%f\n",
2963 k, test_curves_bits[k],
2964 ecdsa_results[k][0], ecdsa_results[k][1]);
2966 printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2967 test_curves_bits[k],
2968 test_curves_names[k],
2969 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
2970 ecdsa_results[k][0], ecdsa_results[k][1]);
2974 for (k = 0; k < EC_NUM; k++) {
2977 if (testnum && !mr) {
2978 printf("%30sop op/s\n", " ");
2982 printf("+F5:%u:%u:%f:%f\n",
2983 k, test_curves_bits[k],
2984 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2987 printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2988 test_curves_bits[k],
2989 test_curves_names[k],
2990 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
2997 ERR_print_errors(bio_err);
2998 for (i = 0; i < loopargs_len; i++) {
2999 OPENSSL_free(loopargs[i].buf_malloc);
3000 OPENSSL_free(loopargs[i].buf2_malloc);
3002 #ifndef OPENSSL_NO_RSA
3003 for (k = 0; k < RSA_NUM; k++)
3004 RSA_free(loopargs[i].rsa_key[k]);
3006 #ifndef OPENSSL_NO_DSA
3007 for (k = 0; k < DSA_NUM; k++)
3008 DSA_free(loopargs[i].dsa_key[k]);
3010 #ifndef OPENSSL_NO_EC
3011 for (k = 0; k < EC_NUM; k++) {
3012 EC_KEY_free(loopargs[i].ecdsa[k]);
3013 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
3015 OPENSSL_free(loopargs[i].secret_a);
3016 OPENSSL_free(loopargs[i].secret_b);
3020 if (async_jobs > 0) {
3021 for (i = 0; i < loopargs_len; i++)
3022 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
3026 ASYNC_cleanup_thread();
3028 OPENSSL_free(loopargs);
3033 static void print_message(const char *s, long num, int length, int tm)
3037 mr ? "+DT:%s:%d:%d\n"
3038 : "Doing %s for %ds on %d size blocks: ", s, tm, length);
3039 (void)BIO_flush(bio_err);
3043 mr ? "+DN:%s:%ld:%d\n"
3044 : "Doing %s %ld times on %d size blocks: ", s, num, length);
3045 (void)BIO_flush(bio_err);
3049 static void pkey_print_message(const char *str, const char *str2, long num,
3054 mr ? "+DTP:%d:%s:%s:%d\n"
3055 : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
3056 (void)BIO_flush(bio_err);
3060 mr ? "+DNP:%ld:%d:%s:%s\n"
3061 : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
3062 (void)BIO_flush(bio_err);
3066 static void print_result(int alg, int run_no, int count, double time_used)
3069 BIO_puts(bio_err, "EVP error!\n");
3073 mr ? "+R:%d:%s:%f\n"
3074 : "%d %s's in %.2fs\n", count, names[alg], time_used);
3075 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
3079 static char *sstrsep(char **string, const char *delim)
3082 char *token = *string;
3087 memset(isdelim, 0, sizeof isdelim);
3091 isdelim[(unsigned char)(*delim)] = 1;
3095 while (!isdelim[(unsigned char)(**string)]) {
3107 static int do_multi(int multi, int size_num)
3112 static char sep[] = ":";
3114 fds = malloc(sizeof(*fds) * multi);
3115 for (n = 0; n < multi; ++n) {
3116 if (pipe(fd) == -1) {
3117 BIO_printf(bio_err, "pipe failure\n");
3121 (void)BIO_flush(bio_err);
3128 if (dup(fd[1]) == -1) {
3129 BIO_printf(bio_err, "dup failed\n");
3138 printf("Forked child %d\n", n);
3141 /* for now, assume the pipe is long enough to take all the output */
3142 for (n = 0; n < multi; ++n) {
3147 f = fdopen(fds[n], "r");
3148 while (fgets(buf, sizeof buf, f)) {
3149 p = strchr(buf, '\n');
3152 if (buf[0] != '+') {
3154 "Don't understand line '%s' from child %d\n", buf,
3158 printf("Got: %s from %d\n", buf, n);
3159 if (strncmp(buf, "+F:", 3) == 0) {
3164 alg = atoi(sstrsep(&p, sep));
3166 for (j = 0; j < size_num; ++j)
3167 results[alg][j] += atof(sstrsep(&p, sep));
3168 } else if (strncmp(buf, "+F2:", 4) == 0) {
3173 k = atoi(sstrsep(&p, sep));
3176 d = atof(sstrsep(&p, sep));
3177 rsa_results[k][0] += d;
3179 d = atof(sstrsep(&p, sep));
3180 rsa_results[k][1] += d;
3182 # ifndef OPENSSL_NO_DSA
3183 else if (strncmp(buf, "+F3:", 4) == 0) {
3188 k = atoi(sstrsep(&p, sep));
3191 d = atof(sstrsep(&p, sep));
3192 dsa_results[k][0] += d;
3194 d = atof(sstrsep(&p, sep));
3195 dsa_results[k][1] += d;
3198 # ifndef OPENSSL_NO_EC
3199 else if (strncmp(buf, "+F4:", 4) == 0) {
3204 k = atoi(sstrsep(&p, sep));
3207 d = atof(sstrsep(&p, sep));
3208 ecdsa_results[k][0] += d;
3210 d = atof(sstrsep(&p, sep));
3211 ecdsa_results[k][1] += d;
3212 } else if (strncmp(buf, "+F5:", 4) == 0) {
3217 k = atoi(sstrsep(&p, sep));
3220 d = atof(sstrsep(&p, sep));
3221 ecdh_results[k][0] += d;
3225 else if (strncmp(buf, "+H:", 3) == 0) {
3228 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
3239 static void multiblock_speed(const EVP_CIPHER *evp_cipher, const SEC *seconds)
3241 static const int mblengths_list[] =
3242 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3243 const int *mblengths = mblengths_list;
3244 int j, count, num = OSSL_NELEM(mblengths_list);
3245 const char *alg_name;
3246 unsigned char *inp, *out, no_key[32], no_iv[16];
3247 EVP_CIPHER_CTX *ctx;
3250 if (lengths_single) {
3251 mblengths = &lengths_single;
3255 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
3256 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3257 ctx = EVP_CIPHER_CTX_new();
3258 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3259 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
3260 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3262 for (j = 0; j < num; j++) {
3263 print_message(alg_name, 0, mblengths[j], seconds->sym);
3265 for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3266 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3267 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
3268 size_t len = mblengths[j];
3271 memset(aad, 0, 8); /* avoid uninitialized values */
3272 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
3273 aad[9] = 3; /* version */
3275 aad[11] = 0; /* length */
3277 mb_param.out = NULL;
3280 mb_param.interleave = 8;
3282 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3283 sizeof(mb_param), &mb_param);
3289 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3290 sizeof(mb_param), &mb_param);
3294 RAND_bytes(out, 16);
3296 aad[11] = (unsigned char)(len >> 8);
3297 aad[12] = (unsigned char)(len);
3298 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3299 EVP_AEAD_TLS1_AAD_LEN, aad);
3300 EVP_Cipher(ctx, out, inp, len + pad);
3304 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3305 : "%d %s's in %.2fs\n", count, "evp", d);
3306 results[D_EVP][j] = ((double)count) / d * mblengths[j];
3310 fprintf(stdout, "+H");
3311 for (j = 0; j < num; j++)
3312 fprintf(stdout, ":%d", mblengths[j]);
3313 fprintf(stdout, "\n");
3314 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
3315 for (j = 0; j < num; j++)
3316 fprintf(stdout, ":%.2f", results[D_EVP][j]);
3317 fprintf(stdout, "\n");
3320 "The 'numbers' are in 1000s of bytes per second processed.\n");
3321 fprintf(stdout, "type ");
3322 for (j = 0; j < num; j++)
3323 fprintf(stdout, "%7d bytes", mblengths[j]);
3324 fprintf(stdout, "\n");
3325 fprintf(stdout, "%-24s", alg_name);
3327 for (j = 0; j < num; j++) {
3328 if (results[D_EVP][j] > 10000)
3329 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
3331 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
3333 fprintf(stdout, "\n");
3338 EVP_CIPHER_CTX_free(ctx);