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 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
883 for (count = 0; COND(nb_iter); count++)
884 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
886 EVP_DecryptFinal_ex(ctx, buf, &outl);
888 EVP_EncryptFinal_ex(ctx, buf, &outl);
892 * CCM does not support streaming. For the purpose of performance measurement,
893 * each message is encrypted using the same (key,iv)-pair. Do not use this
894 * code in your application.
896 static int EVP_Update_loop_ccm(void *args)
898 loopargs_t *tempargs = *(loopargs_t **) args;
899 unsigned char *buf = tempargs->buf;
900 EVP_CIPHER_CTX *ctx = tempargs->ctx;
902 unsigned char tag[12];
904 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
907 for (count = 0; COND(nb_iter); count++) {
908 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
909 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
910 EVP_DecryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
911 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
912 EVP_DecryptFinal_ex(ctx, buf, &outl);
915 for (count = 0; COND(nb_iter); count++) {
916 EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
917 EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
918 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
919 EVP_EncryptFinal_ex(ctx, buf, &outl);
925 static const EVP_MD *evp_md = NULL;
926 static int EVP_Digest_loop(void *args)
928 loopargs_t *tempargs = *(loopargs_t **) args;
929 unsigned char *buf = tempargs->buf;
930 unsigned char md[EVP_MAX_MD_SIZE];
933 int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
936 for (count = 0; COND(nb_iter); count++) {
937 if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
943 #ifndef OPENSSL_NO_RSA
944 static long rsa_c[RSA_NUM][2]; /* # RSA iteration test */
946 static int RSA_sign_loop(void *args)
948 loopargs_t *tempargs = *(loopargs_t **) args;
949 unsigned char *buf = tempargs->buf;
950 unsigned char *buf2 = tempargs->buf2;
951 unsigned int *rsa_num = &tempargs->siglen;
952 RSA **rsa_key = tempargs->rsa_key;
954 for (count = 0; COND(rsa_c[testnum][0]); count++) {
955 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
957 BIO_printf(bio_err, "RSA sign failure\n");
958 ERR_print_errors(bio_err);
966 static int RSA_verify_loop(void *args)
968 loopargs_t *tempargs = *(loopargs_t **) args;
969 unsigned char *buf = tempargs->buf;
970 unsigned char *buf2 = tempargs->buf2;
971 unsigned int rsa_num = tempargs->siglen;
972 RSA **rsa_key = tempargs->rsa_key;
974 for (count = 0; COND(rsa_c[testnum][1]); count++) {
976 RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
978 BIO_printf(bio_err, "RSA verify failure\n");
979 ERR_print_errors(bio_err);
988 #ifndef OPENSSL_NO_DSA
989 static long dsa_c[DSA_NUM][2];
990 static int DSA_sign_loop(void *args)
992 loopargs_t *tempargs = *(loopargs_t **) args;
993 unsigned char *buf = tempargs->buf;
994 unsigned char *buf2 = tempargs->buf2;
995 DSA **dsa_key = tempargs->dsa_key;
996 unsigned int *siglen = &tempargs->siglen;
998 for (count = 0; COND(dsa_c[testnum][0]); count++) {
999 ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1001 BIO_printf(bio_err, "DSA sign failure\n");
1002 ERR_print_errors(bio_err);
1010 static int DSA_verify_loop(void *args)
1012 loopargs_t *tempargs = *(loopargs_t **) args;
1013 unsigned char *buf = tempargs->buf;
1014 unsigned char *buf2 = tempargs->buf2;
1015 DSA **dsa_key = tempargs->dsa_key;
1016 unsigned int siglen = tempargs->siglen;
1018 for (count = 0; COND(dsa_c[testnum][1]); count++) {
1019 ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1021 BIO_printf(bio_err, "DSA verify failure\n");
1022 ERR_print_errors(bio_err);
1031 #ifndef OPENSSL_NO_EC
1032 static long ecdsa_c[EC_NUM][2];
1033 static int ECDSA_sign_loop(void *args)
1035 loopargs_t *tempargs = *(loopargs_t **) args;
1036 unsigned char *buf = tempargs->buf;
1037 EC_KEY **ecdsa = tempargs->ecdsa;
1038 unsigned char *ecdsasig = tempargs->buf2;
1039 unsigned int *ecdsasiglen = &tempargs->siglen;
1041 for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1042 ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1044 BIO_printf(bio_err, "ECDSA sign failure\n");
1045 ERR_print_errors(bio_err);
1053 static int ECDSA_verify_loop(void *args)
1055 loopargs_t *tempargs = *(loopargs_t **) args;
1056 unsigned char *buf = tempargs->buf;
1057 EC_KEY **ecdsa = tempargs->ecdsa;
1058 unsigned char *ecdsasig = tempargs->buf2;
1059 unsigned int ecdsasiglen = tempargs->siglen;
1061 for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1062 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1064 BIO_printf(bio_err, "ECDSA verify failure\n");
1065 ERR_print_errors(bio_err);
1073 /* ******************************************************************** */
1074 static long ecdh_c[EC_NUM][1];
1076 static int ECDH_EVP_derive_key_loop(void *args)
1078 loopargs_t *tempargs = *(loopargs_t **) args;
1079 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1080 unsigned char *derived_secret = tempargs->secret_a;
1082 size_t *outlen = &(tempargs->outlen[testnum]);
1084 for (count = 0; COND(ecdh_c[testnum][0]); count++)
1085 EVP_PKEY_derive(ctx, derived_secret, outlen);
1090 #endif /* OPENSSL_NO_EC */
1092 static int run_benchmark(int async_jobs,
1093 int (*loop_function) (void *), loopargs_t * loopargs)
1095 int job_op_count = 0;
1096 int total_op_count = 0;
1097 int num_inprogress = 0;
1098 int error = 0, i = 0, ret = 0;
1099 OSSL_ASYNC_FD job_fd = 0;
1100 size_t num_job_fds = 0;
1104 if (async_jobs == 0) {
1105 return loop_function((void *)&loopargs);
1108 for (i = 0; i < async_jobs && !error; i++) {
1109 loopargs_t *looparg_item = loopargs + i;
1111 /* Copy pointer content (looparg_t item address) into async context */
1112 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1113 &job_op_count, loop_function,
1114 (void *)&looparg_item, sizeof(looparg_item));
1120 if (job_op_count == -1) {
1123 total_op_count += job_op_count;
1128 BIO_printf(bio_err, "Failure in the job\n");
1129 ERR_print_errors(bio_err);
1135 while (num_inprogress > 0) {
1136 #if defined(OPENSSL_SYS_WINDOWS)
1138 #elif defined(OPENSSL_SYS_UNIX)
1139 int select_result = 0;
1140 OSSL_ASYNC_FD max_fd = 0;
1143 FD_ZERO(&waitfdset);
1145 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1146 if (loopargs[i].inprogress_job == NULL)
1149 if (!ASYNC_WAIT_CTX_get_all_fds
1150 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1151 || num_job_fds > 1) {
1152 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1153 ERR_print_errors(bio_err);
1157 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1159 FD_SET(job_fd, &waitfdset);
1160 if (job_fd > max_fd)
1164 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1166 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1167 "Decrease the value of async_jobs\n",
1168 max_fd, FD_SETSIZE);
1169 ERR_print_errors(bio_err);
1174 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1175 if (select_result == -1 && errno == EINTR)
1178 if (select_result == -1) {
1179 BIO_printf(bio_err, "Failure in the select\n");
1180 ERR_print_errors(bio_err);
1185 if (select_result == 0)
1189 for (i = 0; i < async_jobs; i++) {
1190 if (loopargs[i].inprogress_job == NULL)
1193 if (!ASYNC_WAIT_CTX_get_all_fds
1194 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1195 || num_job_fds > 1) {
1196 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1197 ERR_print_errors(bio_err);
1201 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1204 #if defined(OPENSSL_SYS_UNIX)
1205 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1207 #elif defined(OPENSSL_SYS_WINDOWS)
1208 if (num_job_fds == 1
1209 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
1214 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1215 loopargs[i].wait_ctx, &job_op_count,
1216 loop_function, (void *)(loopargs + i),
1217 sizeof(loopargs_t));
1222 if (job_op_count == -1) {
1225 total_op_count += job_op_count;
1228 loopargs[i].inprogress_job = NULL;
1233 loopargs[i].inprogress_job = NULL;
1234 BIO_printf(bio_err, "Failure in the job\n");
1235 ERR_print_errors(bio_err);
1242 return error ? -1 : total_op_count;
1245 int speed_main(int argc, char **argv)
1248 int (*loopfunc)(void *args);
1249 loopargs_t *loopargs = NULL;
1251 int loopargs_len = 0;
1253 const char *engine_id = NULL;
1254 const EVP_CIPHER *evp_cipher = NULL;
1257 int multiblock = 0, pr_header = 0;
1258 int doit[ALGOR_NUM] = { 0 };
1259 int ret = 1, i, k, misalign = 0;
1261 int size_num = OSSL_NELEM(lengths_list);
1265 unsigned int async_jobs = 0;
1266 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
1267 || !defined(OPENSSL_NO_EC)
1271 /* What follows are the buffers and key material. */
1272 #ifndef OPENSSL_NO_RC5
1275 #ifndef OPENSSL_NO_RC2
1278 #ifndef OPENSSL_NO_IDEA
1279 IDEA_KEY_SCHEDULE idea_ks;
1281 #ifndef OPENSSL_NO_SEED
1282 SEED_KEY_SCHEDULE seed_ks;
1284 #ifndef OPENSSL_NO_BF
1287 #ifndef OPENSSL_NO_CAST
1290 static const unsigned char key16[16] = {
1291 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1292 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1294 static const unsigned char key24[24] = {
1295 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1296 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1297 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1299 static const unsigned char key32[32] = {
1300 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1301 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1302 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1303 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1305 #ifndef OPENSSL_NO_CAMELLIA
1306 static const unsigned char ckey24[24] = {
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
1311 static const unsigned char ckey32[32] = {
1312 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1313 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1314 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1315 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1317 CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1319 #ifndef OPENSSL_NO_DES
1320 static DES_cblock key = {
1321 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
1323 static DES_cblock key2 = {
1324 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1326 static DES_cblock key3 = {
1327 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1330 #ifndef OPENSSL_NO_RSA
1331 static const unsigned int rsa_bits[RSA_NUM] = {
1332 512, 1024, 2048, 3072, 4096, 7680, 15360
1334 static const unsigned char *rsa_data[RSA_NUM] = {
1335 test512, test1024, test2048, test3072, test4096, test7680, test15360
1337 static const int rsa_data_length[RSA_NUM] = {
1338 sizeof(test512), sizeof(test1024),
1339 sizeof(test2048), sizeof(test3072),
1340 sizeof(test4096), sizeof(test7680),
1343 int rsa_doit[RSA_NUM] = { 0 };
1344 int primes = RSA_DEFAULT_PRIME_NUM;
1346 #ifndef OPENSSL_NO_DSA
1347 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1348 int dsa_doit[DSA_NUM] = { 0 };
1350 #ifndef OPENSSL_NO_EC
1352 * We only test over the following curves as they are representative, To
1353 * add tests over more curves, simply add the curve NID and curve name to
1354 * the following arrays and increase the EC_NUM value accordingly.
1356 static const unsigned int test_curves[EC_NUM] = {
1358 NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
1359 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
1361 NID_sect163k1, NID_sect233k1, NID_sect283k1,
1362 NID_sect409k1, NID_sect571k1, NID_sect163r2,
1363 NID_sect233r1, NID_sect283r1, NID_sect409r1,
1368 static const char *test_curves_names[EC_NUM] = {
1370 "secp160r1", "nistp192", "nistp224",
1371 "nistp256", "nistp384", "nistp521",
1373 "nistk163", "nistk233", "nistk283",
1374 "nistk409", "nistk571", "nistb163",
1375 "nistb233", "nistb283", "nistb409",
1380 static const int test_curves_bits[EC_NUM] = {
1386 571, 253 /* X25519 */
1389 int ecdsa_doit[EC_NUM] = { 0 };
1390 int ecdh_doit[EC_NUM] = { 0 };
1391 #endif /* ndef OPENSSL_NO_EC */
1393 SEC seconds = {SECONDS, RSA_SECONDS, DSA_SECONDS, ECDSA_SECONDS,
1396 prog = opt_init(argc, argv, speed_options);
1397 while ((o = opt_next()) != OPT_EOF) {
1402 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1405 opt_help(speed_options);
1413 evp_cipher = EVP_get_cipherbyname(opt_arg());
1414 if (evp_cipher == NULL)
1415 evp_md = EVP_get_digestbyname(opt_arg());
1416 if (evp_cipher == NULL && evp_md == NULL) {
1418 "%s: %s is an unknown cipher or digest\n",
1429 * In a forked execution, an engine might need to be
1430 * initialised by each child process, not by the parent.
1431 * So store the name here and run setup_engine() later on.
1433 engine_id = opt_arg();
1437 multi = atoi(opt_arg());
1441 #ifndef OPENSSL_NO_ASYNC
1442 async_jobs = atoi(opt_arg());
1443 if (!ASYNC_is_capable()) {
1445 "%s: async_jobs specified but async not supported\n",
1449 if (async_jobs > 99999) {
1451 "%s: too many async_jobs\n",
1458 if (!opt_int(opt_arg(), &misalign))
1460 if (misalign > MISALIGN) {
1462 "%s: Maximum offset is %d\n", prog, MISALIGN);
1471 #ifdef OPENSSL_NO_MULTIBLOCK
1473 "%s: -mb specified but multi-block support is disabled\n",
1483 if (!opt_int(opt_arg(), &primes))
1487 seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
1488 = seconds.ecdh = atoi(opt_arg());
1491 lengths_single = atoi(opt_arg());
1492 lengths = &lengths_single;
1497 argc = opt_num_rest();
1500 /* Remaining arguments are algorithms. */
1501 for (; *argv; argv++) {
1502 if (found(*argv, doit_choices, &i)) {
1506 #ifndef OPENSSL_NO_DES
1507 if (strcmp(*argv, "des") == 0) {
1508 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1512 if (strcmp(*argv, "sha") == 0) {
1513 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1516 #ifndef OPENSSL_NO_RSA
1517 if (strcmp(*argv, "openssl") == 0)
1519 if (strcmp(*argv, "rsa") == 0) {
1520 rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
1521 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
1522 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
1523 rsa_doit[R_RSA_15360] = 1;
1526 if (found(*argv, rsa_choices, &i)) {
1531 #ifndef OPENSSL_NO_DSA
1532 if (strcmp(*argv, "dsa") == 0) {
1533 dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1534 dsa_doit[R_DSA_2048] = 1;
1537 if (found(*argv, dsa_choices, &i)) {
1542 if (strcmp(*argv, "aes") == 0) {
1543 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1546 #ifndef OPENSSL_NO_CAMELLIA
1547 if (strcmp(*argv, "camellia") == 0) {
1548 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1552 #ifndef OPENSSL_NO_EC
1553 if (strcmp(*argv, "ecdsa") == 0) {
1554 for (i = 0; i < EC_NUM; i++)
1558 if (found(*argv, ecdsa_choices, &i)) {
1562 if (strcmp(*argv, "ecdh") == 0) {
1563 for (i = 0; i < EC_NUM; i++)
1567 if (found(*argv, ecdh_choices, &i)) {
1572 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1576 /* Initialize the job pool if async mode is enabled */
1577 if (async_jobs > 0) {
1578 async_init = ASYNC_init_thread(async_jobs, async_jobs);
1580 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1585 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1587 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1588 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1590 for (i = 0; i < loopargs_len; i++) {
1591 if (async_jobs > 0) {
1592 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1593 if (loopargs[i].wait_ctx == NULL) {
1594 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1599 loopargs[i].buf_malloc =
1600 app_malloc(lengths[size_num - 1] + MAX_MISALIGNMENT + 1,
1602 loopargs[i].buf2_malloc =
1603 app_malloc(lengths[size_num - 1] + MAX_MISALIGNMENT + 1,
1605 /* Align the start of buffers on a 64 byte boundary */
1606 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1607 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1608 #ifndef OPENSSL_NO_EC
1609 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1610 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1615 if (multi && do_multi(multi, size_num))
1619 /* Initialize the engine after the fork */
1620 e = setup_engine(engine_id, 0);
1622 /* No parameters; turn on everything. */
1623 if ((argc == 0) && !doit[D_EVP]) {
1624 for (i = 0; i < ALGOR_NUM; i++)
1627 #ifndef OPENSSL_NO_RSA
1628 for (i = 0; i < RSA_NUM; i++)
1631 #ifndef OPENSSL_NO_DSA
1632 for (i = 0; i < DSA_NUM; i++)
1635 #ifndef OPENSSL_NO_EC
1636 for (i = 0; i < EC_NUM; i++)
1638 for (i = 0; i < EC_NUM; i++)
1642 for (i = 0; i < ALGOR_NUM; i++)
1646 if (usertime == 0 && !mr)
1648 "You have chosen to measure elapsed time "
1649 "instead of user CPU time.\n");
1651 #ifndef OPENSSL_NO_RSA
1652 for (i = 0; i < loopargs_len; i++) {
1653 if (primes > RSA_DEFAULT_PRIME_NUM) {
1654 /* for multi-prime RSA, skip this */
1657 for (k = 0; k < RSA_NUM; k++) {
1658 const unsigned char *p;
1661 loopargs[i].rsa_key[k] =
1662 d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1663 if (loopargs[i].rsa_key[k] == NULL) {
1665 "internal error loading RSA key number %d\n", k);
1671 #ifndef OPENSSL_NO_DSA
1672 for (i = 0; i < loopargs_len; i++) {
1673 loopargs[i].dsa_key[0] = get_dsa(512);
1674 loopargs[i].dsa_key[1] = get_dsa(1024);
1675 loopargs[i].dsa_key[2] = get_dsa(2048);
1678 #ifndef OPENSSL_NO_DES
1679 DES_set_key_unchecked(&key, &sch);
1680 DES_set_key_unchecked(&key2, &sch2);
1681 DES_set_key_unchecked(&key3, &sch3);
1683 AES_set_encrypt_key(key16, 128, &aes_ks1);
1684 AES_set_encrypt_key(key24, 192, &aes_ks2);
1685 AES_set_encrypt_key(key32, 256, &aes_ks3);
1686 #ifndef OPENSSL_NO_CAMELLIA
1687 Camellia_set_key(key16, 128, &camellia_ks1);
1688 Camellia_set_key(ckey24, 192, &camellia_ks2);
1689 Camellia_set_key(ckey32, 256, &camellia_ks3);
1691 #ifndef OPENSSL_NO_IDEA
1692 IDEA_set_encrypt_key(key16, &idea_ks);
1694 #ifndef OPENSSL_NO_SEED
1695 SEED_set_key(key16, &seed_ks);
1697 #ifndef OPENSSL_NO_RC4
1698 RC4_set_key(&rc4_ks, 16, key16);
1700 #ifndef OPENSSL_NO_RC2
1701 RC2_set_key(&rc2_ks, 16, key16, 128);
1703 #ifndef OPENSSL_NO_RC5
1704 RC5_32_set_key(&rc5_ks, 16, key16, 12);
1706 #ifndef OPENSSL_NO_BF
1707 BF_set_key(&bf_ks, 16, key16);
1709 #ifndef OPENSSL_NO_CAST
1710 CAST_set_key(&cast_ks, 16, key16);
1713 # ifndef OPENSSL_NO_DES
1714 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1720 for (it = count; it; it--)
1721 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
1722 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1726 c[D_MD2][0] = count / 10;
1727 c[D_MDC2][0] = count / 10;
1728 c[D_MD4][0] = count;
1729 c[D_MD5][0] = count;
1730 c[D_HMAC][0] = count;
1731 c[D_SHA1][0] = count;
1732 c[D_RMD160][0] = count;
1733 c[D_RC4][0] = count * 5;
1734 c[D_CBC_DES][0] = count;
1735 c[D_EDE3_DES][0] = count / 3;
1736 c[D_CBC_IDEA][0] = count;
1737 c[D_CBC_SEED][0] = count;
1738 c[D_CBC_RC2][0] = count;
1739 c[D_CBC_RC5][0] = count;
1740 c[D_CBC_BF][0] = count;
1741 c[D_CBC_CAST][0] = count;
1742 c[D_CBC_128_AES][0] = count;
1743 c[D_CBC_192_AES][0] = count;
1744 c[D_CBC_256_AES][0] = count;
1745 c[D_CBC_128_CML][0] = count;
1746 c[D_CBC_192_CML][0] = count;
1747 c[D_CBC_256_CML][0] = count;
1748 c[D_SHA256][0] = count;
1749 c[D_SHA512][0] = count;
1750 c[D_WHIRLPOOL][0] = count;
1751 c[D_IGE_128_AES][0] = count;
1752 c[D_IGE_192_AES][0] = count;
1753 c[D_IGE_256_AES][0] = count;
1754 c[D_GHASH][0] = count;
1755 c[D_RAND][0] = count;
1757 for (i = 1; i < size_num; i++) {
1760 l0 = (long)lengths[0];
1761 l1 = (long)lengths[i];
1763 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1764 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1765 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1766 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1767 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1768 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1769 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1770 c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1771 c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1772 c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1773 c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1774 c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
1776 l0 = (long)lengths[i - 1];
1778 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1779 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1780 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1781 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1782 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1783 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1784 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1785 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1786 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1787 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1788 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1789 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1790 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1791 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1792 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1793 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1794 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1795 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1798 # ifndef OPENSSL_NO_RSA
1799 rsa_c[R_RSA_512][0] = count / 2000;
1800 rsa_c[R_RSA_512][1] = count / 400;
1801 for (i = 1; i < RSA_NUM; i++) {
1802 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1803 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
1804 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
1807 if (rsa_c[i][0] == 0) {
1808 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1815 # ifndef OPENSSL_NO_DSA
1816 dsa_c[R_DSA_512][0] = count / 1000;
1817 dsa_c[R_DSA_512][1] = count / 1000 / 2;
1818 for (i = 1; i < DSA_NUM; i++) {
1819 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1820 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
1821 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
1824 if (dsa_c[i][0] == 0) {
1825 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1832 # ifndef OPENSSL_NO_EC
1833 ecdsa_c[R_EC_P160][0] = count / 1000;
1834 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1835 for (i = R_EC_P192; i <= R_EC_P521; i++) {
1836 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1837 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1838 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1841 if (ecdsa_c[i][0] == 0) {
1847 ecdsa_c[R_EC_K163][0] = count / 1000;
1848 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1849 for (i = R_EC_K233; i <= R_EC_K571; i++) {
1850 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1851 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1852 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1855 if (ecdsa_c[i][0] == 0) {
1861 ecdsa_c[R_EC_B163][0] = count / 1000;
1862 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1863 for (i = R_EC_B233; i <= R_EC_B571; i++) {
1864 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1865 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
1866 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1869 if (ecdsa_c[i][0] == 0) {
1876 ecdh_c[R_EC_P160][0] = count / 1000;
1877 for (i = R_EC_P192; i <= R_EC_P521; i++) {
1878 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1879 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1882 if (ecdh_c[i][0] == 0) {
1887 ecdh_c[R_EC_K163][0] = count / 1000;
1888 for (i = R_EC_K233; i <= R_EC_K571; i++) {
1889 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1890 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1893 if (ecdh_c[i][0] == 0) {
1898 ecdh_c[R_EC_B163][0] = count / 1000;
1899 for (i = R_EC_B233; i <= R_EC_B571; i++) {
1900 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
1901 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1904 if (ecdh_c[i][0] == 0) {
1912 /* not worth fixing */
1913 # error "You cannot disable DES on systems without SIGALRM."
1914 # endif /* OPENSSL_NO_DES */
1917 signal(SIGALRM, sig_done);
1919 #endif /* SIGALRM */
1921 #ifndef OPENSSL_NO_MD2
1923 for (testnum = 0; testnum < size_num; testnum++) {
1924 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
1927 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1929 print_result(D_MD2, testnum, count, d);
1933 #ifndef OPENSSL_NO_MDC2
1935 for (testnum = 0; testnum < size_num; testnum++) {
1936 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
1939 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1941 print_result(D_MDC2, testnum, count, d);
1946 #ifndef OPENSSL_NO_MD4
1948 for (testnum = 0; testnum < size_num; testnum++) {
1949 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
1952 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1954 print_result(D_MD4, testnum, count, d);
1959 #ifndef OPENSSL_NO_MD5
1961 for (testnum = 0; testnum < size_num; testnum++) {
1962 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
1965 count = run_benchmark(async_jobs, MD5_loop, loopargs);
1967 print_result(D_MD5, testnum, count, d);
1972 static const char hmac_key[] = "This is a key...";
1973 int len = strlen(hmac_key);
1975 for (i = 0; i < loopargs_len; i++) {
1976 loopargs[i].hctx = HMAC_CTX_new();
1977 if (loopargs[i].hctx == NULL) {
1978 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
1982 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
1984 for (testnum = 0; testnum < size_num; testnum++) {
1985 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
1988 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
1990 print_result(D_HMAC, testnum, count, d);
1992 for (i = 0; i < loopargs_len; i++) {
1993 HMAC_CTX_free(loopargs[i].hctx);
1998 for (testnum = 0; testnum < size_num; testnum++) {
1999 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2002 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2004 print_result(D_SHA1, testnum, count, d);
2007 if (doit[D_SHA256]) {
2008 for (testnum = 0; testnum < size_num; testnum++) {
2009 print_message(names[D_SHA256], c[D_SHA256][testnum],
2010 lengths[testnum], seconds.sym);
2012 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2014 print_result(D_SHA256, testnum, count, d);
2017 if (doit[D_SHA512]) {
2018 for (testnum = 0; testnum < size_num; testnum++) {
2019 print_message(names[D_SHA512], c[D_SHA512][testnum],
2020 lengths[testnum], seconds.sym);
2022 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2024 print_result(D_SHA512, testnum, count, d);
2027 #ifndef OPENSSL_NO_WHIRLPOOL
2028 if (doit[D_WHIRLPOOL]) {
2029 for (testnum = 0; testnum < size_num; testnum++) {
2030 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2031 lengths[testnum], seconds.sym);
2033 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2035 print_result(D_WHIRLPOOL, testnum, count, d);
2040 #ifndef OPENSSL_NO_RMD160
2041 if (doit[D_RMD160]) {
2042 for (testnum = 0; testnum < size_num; testnum++) {
2043 print_message(names[D_RMD160], c[D_RMD160][testnum],
2044 lengths[testnum], seconds.sym);
2046 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2048 print_result(D_RMD160, testnum, count, d);
2052 #ifndef OPENSSL_NO_RC4
2054 for (testnum = 0; testnum < size_num; testnum++) {
2055 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2058 count = run_benchmark(async_jobs, RC4_loop, loopargs);
2060 print_result(D_RC4, testnum, count, d);
2064 #ifndef OPENSSL_NO_DES
2065 if (doit[D_CBC_DES]) {
2066 for (testnum = 0; testnum < size_num; testnum++) {
2067 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2068 lengths[testnum], seconds.sym);
2070 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2072 print_result(D_CBC_DES, testnum, count, d);
2076 if (doit[D_EDE3_DES]) {
2077 for (testnum = 0; testnum < size_num; testnum++) {
2078 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2079 lengths[testnum], seconds.sym);
2082 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2084 print_result(D_EDE3_DES, testnum, count, d);
2089 if (doit[D_CBC_128_AES]) {
2090 for (testnum = 0; testnum < size_num; testnum++) {
2091 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2092 lengths[testnum], seconds.sym);
2095 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2097 print_result(D_CBC_128_AES, testnum, count, d);
2100 if (doit[D_CBC_192_AES]) {
2101 for (testnum = 0; testnum < size_num; testnum++) {
2102 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2103 lengths[testnum], seconds.sym);
2106 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2108 print_result(D_CBC_192_AES, testnum, count, d);
2111 if (doit[D_CBC_256_AES]) {
2112 for (testnum = 0; testnum < size_num; testnum++) {
2113 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2114 lengths[testnum], seconds.sym);
2117 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2119 print_result(D_CBC_256_AES, testnum, count, d);
2123 if (doit[D_IGE_128_AES]) {
2124 for (testnum = 0; testnum < size_num; testnum++) {
2125 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2126 lengths[testnum], seconds.sym);
2129 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2131 print_result(D_IGE_128_AES, testnum, count, d);
2134 if (doit[D_IGE_192_AES]) {
2135 for (testnum = 0; testnum < size_num; testnum++) {
2136 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2137 lengths[testnum], seconds.sym);
2140 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2142 print_result(D_IGE_192_AES, testnum, count, d);
2145 if (doit[D_IGE_256_AES]) {
2146 for (testnum = 0; testnum < size_num; testnum++) {
2147 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2148 lengths[testnum], seconds.sym);
2151 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2153 print_result(D_IGE_256_AES, testnum, count, d);
2156 if (doit[D_GHASH]) {
2157 for (i = 0; i < loopargs_len; i++) {
2158 loopargs[i].gcm_ctx =
2159 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2160 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2161 (unsigned char *)"0123456789ab", 12);
2164 for (testnum = 0; testnum < size_num; testnum++) {
2165 print_message(names[D_GHASH], c[D_GHASH][testnum],
2166 lengths[testnum], seconds.sym);
2168 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2170 print_result(D_GHASH, testnum, count, d);
2172 for (i = 0; i < loopargs_len; i++)
2173 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2175 #ifndef OPENSSL_NO_CAMELLIA
2176 if (doit[D_CBC_128_CML]) {
2177 if (async_jobs > 0) {
2178 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2179 names[D_CBC_128_CML]);
2180 doit[D_CBC_128_CML] = 0;
2182 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2183 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2184 lengths[testnum], seconds.sym);
2186 for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2187 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2188 (size_t)lengths[testnum], &camellia_ks1,
2189 iv, CAMELLIA_ENCRYPT);
2191 print_result(D_CBC_128_CML, testnum, count, d);
2194 if (doit[D_CBC_192_CML]) {
2195 if (async_jobs > 0) {
2196 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2197 names[D_CBC_192_CML]);
2198 doit[D_CBC_192_CML] = 0;
2200 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2201 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2202 lengths[testnum], seconds.sym);
2203 if (async_jobs > 0) {
2204 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2208 for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2209 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2210 (size_t)lengths[testnum], &camellia_ks2,
2211 iv, CAMELLIA_ENCRYPT);
2213 print_result(D_CBC_192_CML, testnum, count, d);
2216 if (doit[D_CBC_256_CML]) {
2217 if (async_jobs > 0) {
2218 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2219 names[D_CBC_256_CML]);
2220 doit[D_CBC_256_CML] = 0;
2222 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2223 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2224 lengths[testnum], seconds.sym);
2226 for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2227 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2228 (size_t)lengths[testnum], &camellia_ks3,
2229 iv, CAMELLIA_ENCRYPT);
2231 print_result(D_CBC_256_CML, testnum, count, d);
2235 #ifndef OPENSSL_NO_IDEA
2236 if (doit[D_CBC_IDEA]) {
2237 if (async_jobs > 0) {
2238 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2240 doit[D_CBC_IDEA] = 0;
2242 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2243 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2244 lengths[testnum], seconds.sym);
2246 for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
2247 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2248 (size_t)lengths[testnum], &idea_ks,
2251 print_result(D_CBC_IDEA, testnum, count, d);
2255 #ifndef OPENSSL_NO_SEED
2256 if (doit[D_CBC_SEED]) {
2257 if (async_jobs > 0) {
2258 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2260 doit[D_CBC_SEED] = 0;
2262 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2263 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2264 lengths[testnum], seconds.sym);
2266 for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2267 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2268 (size_t)lengths[testnum], &seed_ks, iv, 1);
2270 print_result(D_CBC_SEED, testnum, count, d);
2274 #ifndef OPENSSL_NO_RC2
2275 if (doit[D_CBC_RC2]) {
2276 if (async_jobs > 0) {
2277 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2279 doit[D_CBC_RC2] = 0;
2281 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2282 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2283 lengths[testnum], seconds.sym);
2284 if (async_jobs > 0) {
2285 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2289 for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2290 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2291 (size_t)lengths[testnum], &rc2_ks,
2294 print_result(D_CBC_RC2, testnum, count, d);
2298 #ifndef OPENSSL_NO_RC5
2299 if (doit[D_CBC_RC5]) {
2300 if (async_jobs > 0) {
2301 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2303 doit[D_CBC_RC5] = 0;
2305 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2306 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2307 lengths[testnum], seconds.sym);
2308 if (async_jobs > 0) {
2309 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2313 for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2314 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2315 (size_t)lengths[testnum], &rc5_ks,
2318 print_result(D_CBC_RC5, testnum, count, d);
2322 #ifndef OPENSSL_NO_BF
2323 if (doit[D_CBC_BF]) {
2324 if (async_jobs > 0) {
2325 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2329 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2330 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2331 lengths[testnum], seconds.sym);
2333 for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2334 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2335 (size_t)lengths[testnum], &bf_ks,
2338 print_result(D_CBC_BF, testnum, count, d);
2342 #ifndef OPENSSL_NO_CAST
2343 if (doit[D_CBC_CAST]) {
2344 if (async_jobs > 0) {
2345 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2347 doit[D_CBC_CAST] = 0;
2349 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2350 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2351 lengths[testnum], seconds.sym);
2353 for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2354 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2355 (size_t)lengths[testnum], &cast_ks,
2358 print_result(D_CBC_CAST, testnum, count, d);
2363 for (testnum = 0; testnum < size_num; testnum++) {
2364 print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2367 count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2369 print_result(D_RAND, testnum, count, d);
2374 if (multiblock && evp_cipher) {
2376 (EVP_CIPHER_flags(evp_cipher) &
2377 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2378 BIO_printf(bio_err, "%s is not multi-block capable\n",
2379 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2382 if (async_jobs > 0) {
2383 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2386 multiblock_speed(evp_cipher, &seconds);
2390 for (testnum = 0; testnum < size_num; testnum++) {
2393 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2395 * -O3 -fschedule-insns messes up an optimization here!
2396 * names[D_EVP] somehow becomes NULL
2398 print_message(names[D_EVP], save_count, lengths[testnum],
2401 for (k = 0; k < loopargs_len; k++) {
2402 loopargs[k].ctx = EVP_CIPHER_CTX_new();
2404 EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2407 EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2409 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2411 switch (EVP_CIPHER_mode(evp_cipher)) {
2412 case EVP_CIPH_CCM_MODE:
2413 loopfunc = EVP_Update_loop_ccm;
2416 loopfunc = EVP_Update_loop;
2420 count = run_benchmark(async_jobs, loopfunc, loopargs);
2422 for (k = 0; k < loopargs_len; k++) {
2423 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2427 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2428 print_message(names[D_EVP], save_count, lengths[testnum],
2431 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2434 print_result(D_EVP, testnum, count, d);
2438 for (i = 0; i < loopargs_len; i++)
2439 RAND_bytes(loopargs[i].buf, 36);
2441 #ifndef OPENSSL_NO_RSA
2442 for (testnum = 0; testnum < RSA_NUM; testnum++) {
2444 if (!rsa_doit[testnum])
2446 for (i = 0; i < loopargs_len; i++) {
2448 /* we haven't set keys yet, generate multi-prime RSA keys */
2449 BIGNUM *bn = BN_new();
2453 if (!BN_set_word(bn, RSA_F4)) {
2458 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
2459 rsa_choices[testnum].name);
2461 loopargs[i].rsa_key[testnum] = RSA_new();
2462 if (loopargs[i].rsa_key[testnum] == NULL) {
2467 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
2469 primes, bn, NULL)) {
2475 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2476 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2482 "RSA sign failure. No RSA sign will be done.\n");
2483 ERR_print_errors(bio_err);
2486 pkey_print_message("private", "rsa",
2487 rsa_c[testnum][0], rsa_bits[testnum],
2489 /* RSA_blinding_on(rsa_key[testnum],NULL); */
2491 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2494 mr ? "+R1:%ld:%d:%.2f\n"
2495 : "%ld %d bit private RSA's in %.2fs\n",
2496 count, rsa_bits[testnum], d);
2497 rsa_results[testnum][0] = (double)count / d;
2501 for (i = 0; i < loopargs_len; i++) {
2502 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
2503 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2509 "RSA verify failure. No RSA verify will be done.\n");
2510 ERR_print_errors(bio_err);
2511 rsa_doit[testnum] = 0;
2513 pkey_print_message("public", "rsa",
2514 rsa_c[testnum][1], rsa_bits[testnum],
2517 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2520 mr ? "+R2:%ld:%d:%.2f\n"
2521 : "%ld %d bit public RSA's in %.2fs\n",
2522 count, rsa_bits[testnum], d);
2523 rsa_results[testnum][1] = (double)count / d;
2526 if (rsa_count <= 1) {
2527 /* if longer than 10s, don't do any more */
2528 for (testnum++; testnum < RSA_NUM; testnum++)
2529 rsa_doit[testnum] = 0;
2532 #endif /* OPENSSL_NO_RSA */
2534 for (i = 0; i < loopargs_len; i++)
2535 RAND_bytes(loopargs[i].buf, 36);
2537 #ifndef OPENSSL_NO_DSA
2538 for (testnum = 0; testnum < DSA_NUM; testnum++) {
2540 if (!dsa_doit[testnum])
2543 /* DSA_generate_key(dsa_key[testnum]); */
2544 /* DSA_sign_setup(dsa_key[testnum],NULL); */
2545 for (i = 0; i < loopargs_len; i++) {
2546 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2547 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2553 "DSA sign failure. No DSA sign will be done.\n");
2554 ERR_print_errors(bio_err);
2557 pkey_print_message("sign", "dsa",
2558 dsa_c[testnum][0], dsa_bits[testnum],
2561 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2564 mr ? "+R3:%ld:%d:%.2f\n"
2565 : "%ld %d bit DSA signs in %.2fs\n",
2566 count, dsa_bits[testnum], d);
2567 dsa_results[testnum][0] = (double)count / d;
2571 for (i = 0; i < loopargs_len; i++) {
2572 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2573 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2579 "DSA verify failure. No DSA verify will be done.\n");
2580 ERR_print_errors(bio_err);
2581 dsa_doit[testnum] = 0;
2583 pkey_print_message("verify", "dsa",
2584 dsa_c[testnum][1], dsa_bits[testnum],
2587 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2590 mr ? "+R4:%ld:%d:%.2f\n"
2591 : "%ld %d bit DSA verify in %.2fs\n",
2592 count, dsa_bits[testnum], d);
2593 dsa_results[testnum][1] = (double)count / d;
2596 if (rsa_count <= 1) {
2597 /* if longer than 10s, don't do any more */
2598 for (testnum++; testnum < DSA_NUM; testnum++)
2599 dsa_doit[testnum] = 0;
2602 #endif /* OPENSSL_NO_DSA */
2604 #ifndef OPENSSL_NO_EC
2605 for (testnum = 0; testnum < EC_NUM; testnum++) {
2608 if (!ecdsa_doit[testnum])
2609 continue; /* Ignore Curve */
2610 for (i = 0; i < loopargs_len; i++) {
2611 loopargs[i].ecdsa[testnum] =
2612 EC_KEY_new_by_curve_name(test_curves[testnum]);
2613 if (loopargs[i].ecdsa[testnum] == NULL) {
2619 BIO_printf(bio_err, "ECDSA failure.\n");
2620 ERR_print_errors(bio_err);
2623 for (i = 0; i < loopargs_len; i++) {
2624 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
2625 /* Perform ECDSA signature test */
2626 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
2627 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2628 &loopargs[i].siglen,
2629 loopargs[i].ecdsa[testnum]);
2635 "ECDSA sign failure. No ECDSA sign will be done.\n");
2636 ERR_print_errors(bio_err);
2639 pkey_print_message("sign", "ecdsa",
2640 ecdsa_c[testnum][0],
2641 test_curves_bits[testnum],
2644 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2648 mr ? "+R5:%ld:%d:%.2f\n" :
2649 "%ld %d bit ECDSA signs in %.2fs \n",
2650 count, test_curves_bits[testnum], d);
2651 ecdsa_results[testnum][0] = (double)count / d;
2655 /* Perform ECDSA verification test */
2656 for (i = 0; i < loopargs_len; i++) {
2657 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2659 loopargs[i].ecdsa[testnum]);
2665 "ECDSA verify failure. No ECDSA verify will be done.\n");
2666 ERR_print_errors(bio_err);
2667 ecdsa_doit[testnum] = 0;
2669 pkey_print_message("verify", "ecdsa",
2670 ecdsa_c[testnum][1],
2671 test_curves_bits[testnum],
2674 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2677 mr ? "+R6:%ld:%d:%.2f\n"
2678 : "%ld %d bit ECDSA verify in %.2fs\n",
2679 count, test_curves_bits[testnum], d);
2680 ecdsa_results[testnum][1] = (double)count / d;
2683 if (rsa_count <= 1) {
2684 /* if longer than 10s, don't do any more */
2685 for (testnum++; testnum < EC_NUM; testnum++)
2686 ecdsa_doit[testnum] = 0;
2691 for (testnum = 0; testnum < EC_NUM; testnum++) {
2692 int ecdh_checks = 1;
2694 if (!ecdh_doit[testnum])
2697 for (i = 0; i < loopargs_len; i++) {
2698 EVP_PKEY_CTX *kctx = NULL;
2699 EVP_PKEY_CTX *test_ctx = NULL;
2700 EVP_PKEY_CTX *ctx = NULL;
2701 EVP_PKEY *key_A = NULL;
2702 EVP_PKEY *key_B = NULL;
2706 /* Ensure that the error queue is empty */
2707 if (ERR_peek_error()) {
2709 "WARNING: the error queue contains previous unhandled errors.\n");
2710 ERR_print_errors(bio_err);
2713 /* Let's try to create a ctx directly from the NID: this works for
2714 * curves like Curve25519 that are not implemented through the low
2715 * level EC interface.
2716 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
2717 * then we set the curve by NID before deriving the actual keygen
2718 * ctx for that specific curve. */
2719 kctx = EVP_PKEY_CTX_new_id(test_curves[testnum], NULL); /* keygen ctx from NID */
2721 EVP_PKEY_CTX *pctx = NULL;
2722 EVP_PKEY *params = NULL;
2724 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
2725 * "int_ctx_new:unsupported algorithm" error was added to the
2727 * We remove it from the error queue as we are handling it. */
2728 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
2729 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
2730 /* check that the error origin matches */
2731 ERR_GET_LIB(error) == ERR_LIB_EVP &&
2732 ERR_GET_FUNC(error) == EVP_F_INT_CTX_NEW &&
2733 ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
2734 ERR_get_error(); /* pop error from queue */
2735 if (ERR_peek_error()) {
2737 "Unhandled error in the error queue during ECDH init.\n");
2738 ERR_print_errors(bio_err);
2743 if ( /* Create the context for parameter generation */
2744 !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
2745 /* Initialise the parameter generation */
2746 !EVP_PKEY_paramgen_init(pctx) ||
2747 /* Set the curve by NID */
2748 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
2751 /* Create the parameter object params */
2752 !EVP_PKEY_paramgen(pctx, ¶ms)) {
2754 BIO_printf(bio_err, "ECDH EC params init failure.\n");
2755 ERR_print_errors(bio_err);
2759 /* Create the context for the key generation */
2760 kctx = EVP_PKEY_CTX_new(params, NULL);
2762 EVP_PKEY_free(params);
2764 EVP_PKEY_CTX_free(pctx);
2767 if (kctx == NULL || /* keygen ctx is not null */
2768 !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
2770 BIO_printf(bio_err, "ECDH keygen failure.\n");
2771 ERR_print_errors(bio_err);
2776 if (!EVP_PKEY_keygen(kctx, &key_A) || /* generate secret key A */
2777 !EVP_PKEY_keygen(kctx, &key_B) || /* generate secret key B */
2778 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
2779 !EVP_PKEY_derive_init(ctx) || /* init derivation ctx */
2780 !EVP_PKEY_derive_set_peer(ctx, key_B) || /* set peer pubkey in ctx */
2781 !EVP_PKEY_derive(ctx, NULL, &outlen) || /* determine max length */
2782 outlen == 0 || /* ensure outlen is a valid size */
2783 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
2785 BIO_printf(bio_err, "ECDH key generation failure.\n");
2786 ERR_print_errors(bio_err);
2791 /* Here we perform a test run, comparing the output of a*B and b*A;
2792 * we try this here and assume that further EVP_PKEY_derive calls
2793 * never fail, so we can skip checks in the actually benchmarked
2794 * code, for maximum performance. */
2795 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
2796 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
2797 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
2798 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
2799 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
2800 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
2801 test_outlen != outlen /* compare output length */ ) {
2803 BIO_printf(bio_err, "ECDH computation failure.\n");
2804 ERR_print_errors(bio_err);
2809 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
2810 if (CRYPTO_memcmp(loopargs[i].secret_a,
2811 loopargs[i].secret_b, outlen)) {
2813 BIO_printf(bio_err, "ECDH computations don't match.\n");
2814 ERR_print_errors(bio_err);
2819 loopargs[i].ecdh_ctx[testnum] = ctx;
2820 loopargs[i].outlen[testnum] = outlen;
2822 EVP_PKEY_free(key_A);
2823 EVP_PKEY_free(key_B);
2824 EVP_PKEY_CTX_free(kctx);
2826 EVP_PKEY_CTX_free(test_ctx);
2829 if (ecdh_checks != 0) {
2830 pkey_print_message("", "ecdh",
2832 test_curves_bits[testnum],
2836 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
2839 mr ? "+R7:%ld:%d:%.2f\n" :
2840 "%ld %d-bit ECDH ops in %.2fs\n", count,
2841 test_curves_bits[testnum], d);
2842 ecdh_results[testnum][0] = (double)count / d;
2846 if (rsa_count <= 1) {
2847 /* if longer than 10s, don't do any more */
2848 for (testnum++; testnum < EC_NUM; testnum++)
2849 ecdh_doit[testnum] = 0;
2852 #endif /* OPENSSL_NO_EC */
2857 printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
2858 printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2860 printf("%s ", BN_options());
2861 #ifndef OPENSSL_NO_MD2
2862 printf("%s ", MD2_options());
2864 #ifndef OPENSSL_NO_RC4
2865 printf("%s ", RC4_options());
2867 #ifndef OPENSSL_NO_DES
2868 printf("%s ", DES_options());
2870 printf("%s ", AES_options());
2871 #ifndef OPENSSL_NO_IDEA
2872 printf("%s ", IDEA_options());
2874 #ifndef OPENSSL_NO_BF
2875 printf("%s ", BF_options());
2877 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2885 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2888 for (testnum = 0; testnum < size_num; testnum++)
2889 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2893 for (k = 0; k < ALGOR_NUM; k++) {
2897 printf("+F:%d:%s", k, names[k]);
2899 printf("%-13s", names[k]);
2900 for (testnum = 0; testnum < size_num; testnum++) {
2901 if (results[k][testnum] > 10000 && !mr)
2902 printf(" %11.2fk", results[k][testnum] / 1e3);
2904 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2908 #ifndef OPENSSL_NO_RSA
2910 for (k = 0; k < RSA_NUM; k++) {
2913 if (testnum && !mr) {
2914 printf("%18ssign verify sign/s verify/s\n", " ");
2918 printf("+F2:%u:%u:%f:%f\n",
2919 k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2921 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2922 rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
2923 rsa_results[k][0], rsa_results[k][1]);
2926 #ifndef OPENSSL_NO_DSA
2928 for (k = 0; k < DSA_NUM; k++) {
2931 if (testnum && !mr) {
2932 printf("%18ssign verify sign/s verify/s\n", " ");
2936 printf("+F3:%u:%u:%f:%f\n",
2937 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2939 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2940 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
2941 dsa_results[k][0], dsa_results[k][1]);
2944 #ifndef OPENSSL_NO_EC
2946 for (k = 0; k < EC_NUM; k++) {
2949 if (testnum && !mr) {
2950 printf("%30ssign verify sign/s verify/s\n", " ");
2955 printf("+F4:%u:%u:%f:%f\n",
2956 k, test_curves_bits[k],
2957 ecdsa_results[k][0], ecdsa_results[k][1]);
2959 printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2960 test_curves_bits[k],
2961 test_curves_names[k],
2962 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
2963 ecdsa_results[k][0], ecdsa_results[k][1]);
2967 for (k = 0; k < EC_NUM; k++) {
2970 if (testnum && !mr) {
2971 printf("%30sop op/s\n", " ");
2975 printf("+F5:%u:%u:%f:%f\n",
2976 k, test_curves_bits[k],
2977 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2980 printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2981 test_curves_bits[k],
2982 test_curves_names[k],
2983 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
2990 ERR_print_errors(bio_err);
2991 for (i = 0; i < loopargs_len; i++) {
2992 OPENSSL_free(loopargs[i].buf_malloc);
2993 OPENSSL_free(loopargs[i].buf2_malloc);
2995 #ifndef OPENSSL_NO_RSA
2996 for (k = 0; k < RSA_NUM; k++)
2997 RSA_free(loopargs[i].rsa_key[k]);
2999 #ifndef OPENSSL_NO_DSA
3000 for (k = 0; k < DSA_NUM; k++)
3001 DSA_free(loopargs[i].dsa_key[k]);
3003 #ifndef OPENSSL_NO_EC
3004 for (k = 0; k < EC_NUM; k++) {
3005 EC_KEY_free(loopargs[i].ecdsa[k]);
3006 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
3008 OPENSSL_free(loopargs[i].secret_a);
3009 OPENSSL_free(loopargs[i].secret_b);
3013 if (async_jobs > 0) {
3014 for (i = 0; i < loopargs_len; i++)
3015 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
3019 ASYNC_cleanup_thread();
3021 OPENSSL_free(loopargs);
3026 static void print_message(const char *s, long num, int length, int tm)
3030 mr ? "+DT:%s:%d:%d\n"
3031 : "Doing %s for %ds on %d size blocks: ", s, tm, length);
3032 (void)BIO_flush(bio_err);
3036 mr ? "+DN:%s:%ld:%d\n"
3037 : "Doing %s %ld times on %d size blocks: ", s, num, length);
3038 (void)BIO_flush(bio_err);
3042 static void pkey_print_message(const char *str, const char *str2, long num,
3047 mr ? "+DTP:%d:%s:%s:%d\n"
3048 : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
3049 (void)BIO_flush(bio_err);
3053 mr ? "+DNP:%ld:%d:%s:%s\n"
3054 : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
3055 (void)BIO_flush(bio_err);
3059 static void print_result(int alg, int run_no, int count, double time_used)
3062 BIO_puts(bio_err, "EVP error!\n");
3066 mr ? "+R:%d:%s:%f\n"
3067 : "%d %s's in %.2fs\n", count, names[alg], time_used);
3068 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
3072 static char *sstrsep(char **string, const char *delim)
3075 char *token = *string;
3080 memset(isdelim, 0, sizeof isdelim);
3084 isdelim[(unsigned char)(*delim)] = 1;
3088 while (!isdelim[(unsigned char)(**string)]) {
3100 static int do_multi(int multi, int size_num)
3105 static char sep[] = ":";
3107 fds = malloc(sizeof(*fds) * multi);
3108 for (n = 0; n < multi; ++n) {
3109 if (pipe(fd) == -1) {
3110 BIO_printf(bio_err, "pipe failure\n");
3114 (void)BIO_flush(bio_err);
3121 if (dup(fd[1]) == -1) {
3122 BIO_printf(bio_err, "dup failed\n");
3131 printf("Forked child %d\n", n);
3134 /* for now, assume the pipe is long enough to take all the output */
3135 for (n = 0; n < multi; ++n) {
3140 f = fdopen(fds[n], "r");
3141 while (fgets(buf, sizeof buf, f)) {
3142 p = strchr(buf, '\n');
3145 if (buf[0] != '+') {
3147 "Don't understand line '%s' from child %d\n", buf,
3151 printf("Got: %s from %d\n", buf, n);
3152 if (strncmp(buf, "+F:", 3) == 0) {
3157 alg = atoi(sstrsep(&p, sep));
3159 for (j = 0; j < size_num; ++j)
3160 results[alg][j] += atof(sstrsep(&p, sep));
3161 } else if (strncmp(buf, "+F2:", 4) == 0) {
3166 k = atoi(sstrsep(&p, sep));
3169 d = atof(sstrsep(&p, sep));
3170 rsa_results[k][0] += d;
3172 d = atof(sstrsep(&p, sep));
3173 rsa_results[k][1] += d;
3175 # ifndef OPENSSL_NO_DSA
3176 else if (strncmp(buf, "+F3:", 4) == 0) {
3181 k = atoi(sstrsep(&p, sep));
3184 d = atof(sstrsep(&p, sep));
3185 dsa_results[k][0] += d;
3187 d = atof(sstrsep(&p, sep));
3188 dsa_results[k][1] += d;
3191 # ifndef OPENSSL_NO_EC
3192 else if (strncmp(buf, "+F4:", 4) == 0) {
3197 k = atoi(sstrsep(&p, sep));
3200 d = atof(sstrsep(&p, sep));
3201 ecdsa_results[k][0] += d;
3203 d = atof(sstrsep(&p, sep));
3204 ecdsa_results[k][1] += d;
3205 } else if (strncmp(buf, "+F5:", 4) == 0) {
3210 k = atoi(sstrsep(&p, sep));
3213 d = atof(sstrsep(&p, sep));
3214 ecdh_results[k][0] += d;
3218 else if (strncmp(buf, "+H:", 3) == 0) {
3221 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
3232 static void multiblock_speed(const EVP_CIPHER *evp_cipher, const SEC *seconds)
3234 static const int mblengths_list[] =
3235 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3236 const int *mblengths = mblengths_list;
3237 int j, count, num = OSSL_NELEM(mblengths_list);
3238 const char *alg_name;
3239 unsigned char *inp, *out, no_key[32], no_iv[16];
3240 EVP_CIPHER_CTX *ctx;
3243 if (lengths_single) {
3244 mblengths = &lengths_single;
3248 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
3249 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3250 ctx = EVP_CIPHER_CTX_new();
3251 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3252 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
3253 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3255 for (j = 0; j < num; j++) {
3256 print_message(alg_name, 0, mblengths[j], seconds->sym);
3258 for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3259 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3260 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
3261 size_t len = mblengths[j];
3264 memset(aad, 0, 8); /* avoid uninitialized values */
3265 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
3266 aad[9] = 3; /* version */
3268 aad[11] = 0; /* length */
3270 mb_param.out = NULL;
3273 mb_param.interleave = 8;
3275 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3276 sizeof(mb_param), &mb_param);
3282 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3283 sizeof(mb_param), &mb_param);
3287 RAND_bytes(out, 16);
3289 aad[11] = (unsigned char)(len >> 8);
3290 aad[12] = (unsigned char)(len);
3291 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3292 EVP_AEAD_TLS1_AAD_LEN, aad);
3293 EVP_Cipher(ctx, out, inp, len + pad);
3297 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3298 : "%d %s's in %.2fs\n", count, "evp", d);
3299 results[D_EVP][j] = ((double)count) / d * mblengths[j];
3303 fprintf(stdout, "+H");
3304 for (j = 0; j < num; j++)
3305 fprintf(stdout, ":%d", mblengths[j]);
3306 fprintf(stdout, "\n");
3307 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
3308 for (j = 0; j < num; j++)
3309 fprintf(stdout, ":%.2f", results[D_EVP][j]);
3310 fprintf(stdout, "\n");
3313 "The 'numbers' are in 1000s of bytes per second processed.\n");
3314 fprintf(stdout, "type ");
3315 for (j = 0; j < num; j++)
3316 fprintf(stdout, "%7d bytes", mblengths[j]);
3317 fprintf(stdout, "\n");
3318 fprintf(stdout, "%-24s", alg_name);
3320 for (j = 0; j < num; j++) {
3321 if (results[D_EVP][j] > 10000)
3322 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
3324 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
3326 fprintf(stdout, "\n");
3331 EVP_CIPHER_CTX_free(ctx);