2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #define PKEY_SECONDS 10
15 #define RSA_SECONDS PKEY_SECONDS
16 #define DSA_SECONDS PKEY_SECONDS
17 #define ECDSA_SECONDS PKEY_SECONDS
18 #define ECDH_SECONDS PKEY_SECONDS
19 #define EdDSA_SECONDS PKEY_SECONDS
20 #define SM2_SECONDS PKEY_SECONDS
21 #define FFDH_SECONDS PKEY_SECONDS
23 /* We need to use some deprecated APIs */
24 #define OPENSSL_SUPPRESS_DEPRECATED
32 #include <openssl/crypto.h>
33 #include <openssl/rand.h>
34 #include <openssl/err.h>
35 #include <openssl/evp.h>
36 #include <openssl/objects.h>
37 #include <openssl/core_names.h>
38 #include <openssl/async.h>
39 #if !defined(OPENSSL_SYS_MSDOS)
44 # if defined(OPENSSL_TANDEM_FLOSS)
45 # include <floss.h(floss_fork)>
53 #include <openssl/bn.h>
54 #include <openssl/rsa.h>
55 #include "./testrsa.h"
57 # include <openssl/dh.h>
59 #include <openssl/x509.h>
60 #include <openssl/dsa.h>
61 #include "./testdsa.h"
62 #include <openssl/modes.h>
65 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
78 #define MAX_MISALIGNMENT 63
79 #define MAX_ECDH_SIZE 256
81 #define MAX_FFDH_SIZE 1024
83 #ifndef RSA_DEFAULT_PRIME_NUM
84 # define RSA_DEFAULT_PRIME_NUM 2
87 typedef struct openssl_speed_sec_st
{
96 } openssl_speed_sec_t
;
98 static volatile int run
= 0;
100 static int mr
= 0; /* machine-readeable output format to merge fork results */
101 static int usertime
= 1;
103 static double Time_F(int s
);
104 static void print_message(const char *s
, long num
, int length
, int tm
);
105 static void pkey_print_message(const char *str
, const char *str2
,
106 long num
, unsigned int bits
, int sec
);
107 static void print_result(int alg
, int run_no
, int count
, double time_used
);
109 static int do_multi(int multi
, int size_num
);
112 static const int lengths_list
[] = {
113 16, 64, 256, 1024, 8 * 1024, 16 * 1024
115 #define SIZE_NUM OSSL_NELEM(lengths_list)
116 static const int *lengths
= lengths_list
;
118 static const int aead_lengths_list
[] = {
119 2, 31, 136, 1024, 8 * 1024, 16 * 1024
127 static void alarmed(int sig
)
129 signal(SIGALRM
, alarmed
);
133 static double Time_F(int s
)
135 double ret
= app_tminterval(s
, usertime
);
141 #elif defined(_WIN32)
145 static unsigned int lapse
;
146 static volatile unsigned int schlock
;
147 static void alarm_win32(unsigned int secs
)
152 # define alarm alarm_win32
154 static DWORD WINAPI
sleepy(VOID
* arg
)
162 static double Time_F(int s
)
169 thr
= CreateThread(NULL
, 4096, sleepy
, NULL
, 0, NULL
);
171 DWORD err
= GetLastError();
172 BIO_printf(bio_err
, "unable to CreateThread (%lu)", err
);
176 Sleep(0); /* scheduler spinlock */
177 ret
= app_tminterval(s
, usertime
);
179 ret
= app_tminterval(s
, usertime
);
181 TerminateThread(thr
, 0);
188 # error "SIGALRM not defined and the platform is not Windows"
191 static void multiblock_speed(const EVP_CIPHER
*evp_cipher
, int lengths_single
,
192 const openssl_speed_sec_t
*seconds
);
194 static int opt_found(const char *name
, unsigned int *result
,
195 const OPT_PAIR pairs
[], unsigned int nbelem
)
199 for (idx
= 0; idx
< nbelem
; ++idx
, pairs
++)
200 if (strcmp(name
, pairs
->name
) == 0) {
201 *result
= pairs
->retval
;
206 #define opt_found(value, pairs, result)\
207 opt_found(value, result, pairs, OSSL_NELEM(pairs))
209 typedef enum OPTION_choice
{
211 OPT_ELAPSED
, OPT_EVP
, OPT_HMAC
, OPT_DECRYPT
, OPT_ENGINE
, OPT_MULTI
,
212 OPT_MR
, OPT_MB
, OPT_MISALIGN
, OPT_ASYNCJOBS
, OPT_R_ENUM
, OPT_PROV_ENUM
,
213 OPT_PRIMES
, OPT_SECONDS
, OPT_BYTES
, OPT_AEAD
, OPT_CMAC
216 const OPTIONS speed_options
[] = {
217 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [algorithm...]\n"},
219 OPT_SECTION("General"),
220 {"help", OPT_HELP
, '-', "Display this summary"},
222 "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
223 {"mr", OPT_MR
, '-', "Produce machine readable output"},
225 {"multi", OPT_MULTI
, 'p', "Run benchmarks in parallel"},
227 #ifndef OPENSSL_NO_ASYNC
228 {"async_jobs", OPT_ASYNCJOBS
, 'p',
229 "Enable async mode and start specified number of jobs"},
231 #ifndef OPENSSL_NO_ENGINE
232 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
234 {"primes", OPT_PRIMES
, 'p', "Specify number of primes (for RSA only)"},
236 OPT_SECTION("Selection"),
237 {"evp", OPT_EVP
, 's', "Use EVP-named cipher or digest"},
238 {"hmac", OPT_HMAC
, 's', "HMAC using EVP-named digest"},
239 {"cmac", OPT_CMAC
, 's', "CMAC using EVP-named cipher"},
240 {"decrypt", OPT_DECRYPT
, '-',
241 "Time decryption instead of encryption (only EVP)"},
242 {"aead", OPT_AEAD
, '-',
243 "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
245 OPT_SECTION("Timing"),
246 {"elapsed", OPT_ELAPSED
, '-',
247 "Use wall-clock time instead of CPU user time as divisor"},
248 {"seconds", OPT_SECONDS
, 'p',
249 "Run benchmarks for specified amount of seconds"},
250 {"bytes", OPT_BYTES
, 'p',
251 "Run [non-PKI] benchmarks on custom-sized buffer"},
252 {"misalign", OPT_MISALIGN
, 'p',
253 "Use specified offset to mis-align buffers"},
259 {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
264 D_MD2
, D_MDC2
, D_MD4
, D_MD5
, D_SHA1
, D_RMD160
,
265 D_SHA256
, D_SHA512
, D_WHIRLPOOL
, D_HMAC
,
266 D_CBC_DES
, D_EDE3_DES
, D_RC4
, D_CBC_IDEA
, D_CBC_SEED
,
267 D_CBC_RC2
, D_CBC_RC5
, D_CBC_BF
, D_CBC_CAST
,
268 D_CBC_128_AES
, D_CBC_192_AES
, D_CBC_256_AES
,
269 D_CBC_128_CML
, D_CBC_192_CML
, D_CBC_256_CML
,
270 D_EVP
, D_GHASH
, D_RAND
, D_EVP_CMAC
, ALGOR_NUM
272 /* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
273 static const char *names
[ALGOR_NUM
] = {
274 "md2", "mdc2", "md4", "md5", "sha1", "rmd160",
275 "sha256", "sha512", "whirlpool", "hmac(md5)",
276 "des-cbc", "des-ede3", "rc4", "idea-cbc", "seed-cbc",
277 "rc2-cbc", "rc5-cbc", "blowfish", "cast-cbc",
278 "aes-128-cbc", "aes-192-cbc", "aes-256-cbc",
279 "camellia-128-cbc", "camellia-192-cbc", "camellia-256-cbc",
280 "evp", "ghash", "rand", "cmac"
283 /* list of configured algorithm (remaining), with some few alias */
284 static const OPT_PAIR doit_choices
[] = {
291 {"sha256", D_SHA256
},
292 {"sha512", D_SHA512
},
293 {"whirlpool", D_WHIRLPOOL
},
294 {"ripemd", D_RMD160
},
295 {"rmd160", D_RMD160
},
296 {"ripemd160", D_RMD160
},
298 {"des-cbc", D_CBC_DES
},
299 {"des-ede3", D_EDE3_DES
},
300 {"aes-128-cbc", D_CBC_128_AES
},
301 {"aes-192-cbc", D_CBC_192_AES
},
302 {"aes-256-cbc", D_CBC_256_AES
},
303 {"camellia-128-cbc", D_CBC_128_CML
},
304 {"camellia-192-cbc", D_CBC_192_CML
},
305 {"camellia-256-cbc", D_CBC_256_CML
},
306 {"rc2-cbc", D_CBC_RC2
},
308 {"rc5-cbc", D_CBC_RC5
},
310 {"idea-cbc", D_CBC_IDEA
},
311 {"idea", D_CBC_IDEA
},
312 {"seed-cbc", D_CBC_SEED
},
313 {"seed", D_CBC_SEED
},
314 {"bf-cbc", D_CBC_BF
},
315 {"blowfish", D_CBC_BF
},
317 {"cast-cbc", D_CBC_CAST
},
318 {"cast", D_CBC_CAST
},
319 {"cast5", D_CBC_CAST
},
324 static double results
[ALGOR_NUM
][SIZE_NUM
];
326 enum { R_DSA_512
, R_DSA_1024
, R_DSA_2048
, DSA_NUM
};
327 static const OPT_PAIR dsa_choices
[DSA_NUM
] = {
328 {"dsa512", R_DSA_512
},
329 {"dsa1024", R_DSA_1024
},
330 {"dsa2048", R_DSA_2048
}
332 static double dsa_results
[DSA_NUM
][2]; /* 2 ops: sign then verify */
335 R_RSA_512
, R_RSA_1024
, R_RSA_2048
, R_RSA_3072
, R_RSA_4096
, R_RSA_7680
,
338 static const OPT_PAIR rsa_choices
[RSA_NUM
] = {
339 {"rsa512", R_RSA_512
},
340 {"rsa1024", R_RSA_1024
},
341 {"rsa2048", R_RSA_2048
},
342 {"rsa3072", R_RSA_3072
},
343 {"rsa4096", R_RSA_4096
},
344 {"rsa7680", R_RSA_7680
},
345 {"rsa15360", R_RSA_15360
}
348 static double rsa_results
[RSA_NUM
][2]; /* 2 ops: sign then verify */
350 #ifndef OPENSSL_NO_DH
352 R_FFDH_2048
, R_FFDH_3072
, R_FFDH_4096
, R_FFDH_6144
, R_FFDH_8192
, FFDH_NUM
355 static const OPT_PAIR ffdh_choices
[FFDH_NUM
] = {
356 {"ffdh2048", R_FFDH_2048
},
357 {"ffdh3072", R_FFDH_3072
},
358 {"ffdh4096", R_FFDH_4096
},
359 {"ffdh6144", R_FFDH_6144
},
360 {"ffdh8192", R_FFDH_8192
},
363 static double ffdh_results
[FFDH_NUM
][1]; /* 1 op: derivation */
364 #endif /* OPENSSL_NO_DH */
367 R_EC_P160
, R_EC_P192
, R_EC_P224
, R_EC_P256
, R_EC_P384
, R_EC_P521
,
368 #ifndef OPENSSL_NO_EC2M
369 R_EC_K163
, R_EC_K233
, R_EC_K283
, R_EC_K409
, R_EC_K571
,
370 R_EC_B163
, R_EC_B233
, R_EC_B283
, R_EC_B409
, R_EC_B571
,
372 R_EC_BRP256R1
, R_EC_BRP256T1
, R_EC_BRP384R1
, R_EC_BRP384T1
,
373 R_EC_BRP512R1
, R_EC_BRP512T1
, ECDSA_NUM
375 /* list of ecdsa curves */
376 static const OPT_PAIR ecdsa_choices
[ECDSA_NUM
] = {
377 {"ecdsap160", R_EC_P160
},
378 {"ecdsap192", R_EC_P192
},
379 {"ecdsap224", R_EC_P224
},
380 {"ecdsap256", R_EC_P256
},
381 {"ecdsap384", R_EC_P384
},
382 {"ecdsap521", R_EC_P521
},
383 #ifndef OPENSSL_NO_EC2M
384 {"ecdsak163", R_EC_K163
},
385 {"ecdsak233", R_EC_K233
},
386 {"ecdsak283", R_EC_K283
},
387 {"ecdsak409", R_EC_K409
},
388 {"ecdsak571", R_EC_K571
},
389 {"ecdsab163", R_EC_B163
},
390 {"ecdsab233", R_EC_B233
},
391 {"ecdsab283", R_EC_B283
},
392 {"ecdsab409", R_EC_B409
},
393 {"ecdsab571", R_EC_B571
},
395 {"ecdsabrp256r1", R_EC_BRP256R1
},
396 {"ecdsabrp256t1", R_EC_BRP256T1
},
397 {"ecdsabrp384r1", R_EC_BRP384R1
},
398 {"ecdsabrp384t1", R_EC_BRP384T1
},
399 {"ecdsabrp512r1", R_EC_BRP512R1
},
400 {"ecdsabrp512t1", R_EC_BRP512T1
}
402 enum { R_EC_X25519
= ECDSA_NUM
, R_EC_X448
, EC_NUM
};
403 /* list of ecdh curves, extension of |ecdsa_choices| list above */
404 static const OPT_PAIR ecdh_choices
[EC_NUM
] = {
405 {"ecdhp160", R_EC_P160
},
406 {"ecdhp192", R_EC_P192
},
407 {"ecdhp224", R_EC_P224
},
408 {"ecdhp256", R_EC_P256
},
409 {"ecdhp384", R_EC_P384
},
410 {"ecdhp521", R_EC_P521
},
411 #ifndef OPENSSL_NO_EC2M
412 {"ecdhk163", R_EC_K163
},
413 {"ecdhk233", R_EC_K233
},
414 {"ecdhk283", R_EC_K283
},
415 {"ecdhk409", R_EC_K409
},
416 {"ecdhk571", R_EC_K571
},
417 {"ecdhb163", R_EC_B163
},
418 {"ecdhb233", R_EC_B233
},
419 {"ecdhb283", R_EC_B283
},
420 {"ecdhb409", R_EC_B409
},
421 {"ecdhb571", R_EC_B571
},
423 {"ecdhbrp256r1", R_EC_BRP256R1
},
424 {"ecdhbrp256t1", R_EC_BRP256T1
},
425 {"ecdhbrp384r1", R_EC_BRP384R1
},
426 {"ecdhbrp384t1", R_EC_BRP384T1
},
427 {"ecdhbrp512r1", R_EC_BRP512R1
},
428 {"ecdhbrp512t1", R_EC_BRP512T1
},
429 {"ecdhx25519", R_EC_X25519
},
430 {"ecdhx448", R_EC_X448
}
433 static double ecdh_results
[EC_NUM
][1]; /* 1 op: derivation */
434 static double ecdsa_results
[ECDSA_NUM
][2]; /* 2 ops: sign then verify */
436 enum { R_EC_Ed25519
, R_EC_Ed448
, EdDSA_NUM
};
437 static const OPT_PAIR eddsa_choices
[EdDSA_NUM
] = {
438 {"ed25519", R_EC_Ed25519
},
439 {"ed448", R_EC_Ed448
}
442 static double eddsa_results
[EdDSA_NUM
][2]; /* 2 ops: sign then verify */
444 #ifndef OPENSSL_NO_SM2
445 enum { R_EC_CURVESM2
, SM2_NUM
};
446 static const OPT_PAIR sm2_choices
[SM2_NUM
] = {
447 {"curveSM2", R_EC_CURVESM2
}
449 # define SM2_ID "TLSv1.3+GM+Cipher+Suite"
450 # define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
451 static double sm2_results
[SM2_NUM
][2]; /* 2 ops: sign then verify */
452 #endif /* OPENSSL_NO_SM2 */
454 #define COND(unused_cond) (run && count < 0x7fffffff)
455 #define COUNT(d) (count)
457 typedef struct loopargs_st
{
458 ASYNC_JOB
*inprogress_job
;
459 ASYNC_WAIT_CTX
*wait_ctx
;
462 unsigned char *buf_malloc
;
463 unsigned char *buf2_malloc
;
466 EVP_PKEY_CTX
*rsa_sign_ctx
[RSA_NUM
];
467 EVP_PKEY_CTX
*rsa_verify_ctx
[RSA_NUM
];
468 EVP_PKEY_CTX
*dsa_sign_ctx
[DSA_NUM
];
469 EVP_PKEY_CTX
*dsa_verify_ctx
[DSA_NUM
];
470 EVP_PKEY_CTX
*ecdsa_sign_ctx
[ECDSA_NUM
];
471 EVP_PKEY_CTX
*ecdsa_verify_ctx
[ECDSA_NUM
];
472 EVP_PKEY_CTX
*ecdh_ctx
[EC_NUM
];
473 EVP_MD_CTX
*eddsa_ctx
[EdDSA_NUM
];
474 EVP_MD_CTX
*eddsa_ctx2
[EdDSA_NUM
];
475 #ifndef OPENSSL_NO_SM2
476 EVP_MD_CTX
*sm2_ctx
[SM2_NUM
];
477 EVP_MD_CTX
*sm2_vfy_ctx
[SM2_NUM
];
478 EVP_PKEY
*sm2_pkey
[SM2_NUM
];
480 unsigned char *secret_a
;
481 unsigned char *secret_b
;
482 size_t outlen
[EC_NUM
];
483 #ifndef OPENSSL_NO_DH
484 EVP_PKEY_CTX
*ffdh_ctx
[FFDH_NUM
];
485 unsigned char *secret_ff_a
;
486 unsigned char *secret_ff_b
;
491 static int run_benchmark(int async_jobs
, int (*loop_function
) (void *),
492 loopargs_t
* loopargs
);
494 static unsigned int testnum
;
496 /* Nb of iterations to do per algorithm and key-size */
497 static long c
[ALGOR_NUM
][SIZE_NUM
];
499 static char *evp_mac_mdname
= "md5";
500 static char *evp_hmac_name
= NULL
;
501 static const char *evp_md_name
= NULL
;
502 static char *evp_mac_ciphername
= "aes-128-cbc";
503 static char *evp_cmac_name
= NULL
;
505 static int have_md(const char *name
)
510 if (opt_md_silent(name
, &md
)) {
511 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
513 if (ctx
!= NULL
&& EVP_DigestInit(ctx
, md
) > 0)
515 EVP_MD_CTX_free(ctx
);
521 static int have_cipher(const char *name
)
524 EVP_CIPHER
*cipher
= NULL
;
526 if (opt_cipher_silent(name
, &cipher
)) {
527 EVP_CIPHER_CTX
*ctx
= EVP_CIPHER_CTX_new();
530 && EVP_CipherInit_ex(ctx
, cipher
, NULL
, NULL
, NULL
, 1) > 0)
532 EVP_CIPHER_CTX_free(ctx
);
533 EVP_CIPHER_free(cipher
);
538 static int EVP_Digest_loop(const char *mdname
, int algindex
, void *args
)
540 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
541 unsigned char *buf
= tempargs
->buf
;
542 unsigned char digest
[EVP_MAX_MD_SIZE
];
546 if (!opt_md_silent(mdname
, &md
))
548 for (count
= 0; COND(c
[algindex
][testnum
]); count
++) {
549 if (!EVP_Digest(buf
, (size_t)lengths
[testnum
], digest
, NULL
, md
,
559 static int EVP_Digest_md_loop(void *args
)
561 return EVP_Digest_loop(evp_md_name
, D_EVP
, args
);
564 static int EVP_Digest_MD2_loop(void *args
)
566 return EVP_Digest_loop("md2", D_MD2
, args
);
569 static int EVP_Digest_MDC2_loop(void *args
)
571 return EVP_Digest_loop("mdc2", D_MDC2
, args
);
574 static int EVP_Digest_MD4_loop(void *args
)
576 return EVP_Digest_loop("md4", D_MD4
, args
);
579 static int MD5_loop(void *args
)
581 return EVP_Digest_loop("md5", D_MD5
, args
);
584 static int EVP_MAC_loop(int algindex
, void *args
)
586 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
587 unsigned char *buf
= tempargs
->buf
;
588 EVP_MAC_CTX
*mctx
= tempargs
->mctx
;
589 unsigned char mac
[EVP_MAX_MD_SIZE
];
592 for (count
= 0; COND(c
[algindex
][testnum
]); count
++) {
595 if (!EVP_MAC_init(mctx
, NULL
, 0, NULL
)
596 || !EVP_MAC_update(mctx
, buf
, lengths
[testnum
])
597 || !EVP_MAC_final(mctx
, mac
, &outl
, sizeof(mac
)))
603 static int HMAC_loop(void *args
)
605 return EVP_MAC_loop(D_HMAC
, args
);
608 static int CMAC_loop(void *args
)
610 return EVP_MAC_loop(D_EVP_CMAC
, args
);
613 static int SHA1_loop(void *args
)
615 return EVP_Digest_loop("sha1", D_SHA1
, args
);
618 static int SHA256_loop(void *args
)
620 return EVP_Digest_loop("sha256", D_SHA256
, args
);
623 static int SHA512_loop(void *args
)
625 return EVP_Digest_loop("sha512", D_SHA512
, args
);
628 static int WHIRLPOOL_loop(void *args
)
630 return EVP_Digest_loop("whirlpool", D_WHIRLPOOL
, args
);
633 static int EVP_Digest_RMD160_loop(void *args
)
635 return EVP_Digest_loop("ripemd160", D_RMD160
, args
);
640 static int EVP_Cipher_loop(void *args
)
642 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
643 unsigned char *buf
= tempargs
->buf
;
646 if (tempargs
->ctx
== NULL
)
648 for (count
= 0; COND(c
[algindex
][testnum
]); count
++)
649 if (EVP_Cipher(tempargs
->ctx
, buf
, buf
, (size_t)lengths
[testnum
]) <= 0)
654 static int GHASH_loop(void *args
)
656 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
657 unsigned char *buf
= tempargs
->buf
;
658 EVP_MAC_CTX
*mctx
= tempargs
->mctx
;
661 /* just do the update in the loop to be comparable with 1.1.1 */
662 for (count
= 0; COND(c
[D_GHASH
][testnum
]); count
++) {
663 if (!EVP_MAC_update(mctx
, buf
, lengths
[testnum
]))
669 #define MAX_BLOCK_SIZE 128
671 static unsigned char iv
[2 * MAX_BLOCK_SIZE
/ 8];
673 static EVP_CIPHER_CTX
*init_evp_cipher_ctx(const char *ciphername
,
674 const unsigned char *key
,
677 EVP_CIPHER_CTX
*ctx
= NULL
;
678 EVP_CIPHER
*cipher
= NULL
;
680 if (!opt_cipher_silent(ciphername
, &cipher
))
683 if ((ctx
= EVP_CIPHER_CTX_new()) == NULL
)
686 if (!EVP_CipherInit_ex(ctx
, cipher
, NULL
, NULL
, NULL
, 1)) {
687 EVP_CIPHER_CTX_free(ctx
);
692 if (!EVP_CIPHER_CTX_set_key_length(ctx
, keylen
)) {
693 EVP_CIPHER_CTX_free(ctx
);
698 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, key
, iv
, 1)) {
699 EVP_CIPHER_CTX_free(ctx
);
705 EVP_CIPHER_free(cipher
);
709 static int RAND_bytes_loop(void *args
)
711 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
712 unsigned char *buf
= tempargs
->buf
;
715 for (count
= 0; COND(c
[D_RAND
][testnum
]); count
++)
716 RAND_bytes(buf
, lengths
[testnum
]);
720 static int decrypt
= 0;
721 static int EVP_Update_loop(void *args
)
723 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
724 unsigned char *buf
= tempargs
->buf
;
725 EVP_CIPHER_CTX
*ctx
= tempargs
->ctx
;
729 for (count
= 0; COND(c
[D_EVP
][testnum
]); count
++) {
730 rc
= EVP_DecryptUpdate(ctx
, buf
, &outl
, buf
, lengths
[testnum
]);
732 /* reset iv in case of counter overflow */
733 EVP_CipherInit_ex(ctx
, NULL
, NULL
, NULL
, iv
, -1);
737 for (count
= 0; COND(c
[D_EVP
][testnum
]); count
++) {
738 rc
= EVP_EncryptUpdate(ctx
, buf
, &outl
, buf
, lengths
[testnum
]);
740 /* reset iv in case of counter overflow */
741 EVP_CipherInit_ex(ctx
, NULL
, NULL
, NULL
, iv
, -1);
746 EVP_DecryptFinal_ex(ctx
, buf
, &outl
);
748 EVP_EncryptFinal_ex(ctx
, buf
, &outl
);
753 * CCM does not support streaming. For the purpose of performance measurement,
754 * each message is encrypted using the same (key,iv)-pair. Do not use this
755 * code in your application.
757 static int EVP_Update_loop_ccm(void *args
)
759 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
760 unsigned char *buf
= tempargs
->buf
;
761 EVP_CIPHER_CTX
*ctx
= tempargs
->ctx
;
763 unsigned char tag
[12];
766 for (count
= 0; COND(c
[D_EVP
][testnum
]); count
++) {
767 EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
, sizeof(tag
), tag
);
769 EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
);
770 /* counter is reset on every update */
771 EVP_DecryptUpdate(ctx
, buf
, &outl
, buf
, lengths
[testnum
]);
774 for (count
= 0; COND(c
[D_EVP
][testnum
]); count
++) {
775 /* restore iv length field */
776 EVP_EncryptUpdate(ctx
, NULL
, &outl
, NULL
, lengths
[testnum
]);
777 /* counter is reset on every update */
778 EVP_EncryptUpdate(ctx
, buf
, &outl
, buf
, lengths
[testnum
]);
782 EVP_DecryptFinal_ex(ctx
, buf
, &outl
);
784 EVP_EncryptFinal_ex(ctx
, buf
, &outl
);
789 * To make AEAD benchmarking more relevant perform TLS-like operations,
790 * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
791 * payload length is not actually limited by 16KB...
793 static int EVP_Update_loop_aead(void *args
)
795 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
796 unsigned char *buf
= tempargs
->buf
;
797 EVP_CIPHER_CTX
*ctx
= tempargs
->ctx
;
799 unsigned char aad
[13] = { 0xcc };
800 unsigned char faketag
[16] = { 0xcc };
803 for (count
= 0; COND(c
[D_EVP
][testnum
]); count
++) {
804 (void)EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
);
805 (void)EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
806 sizeof(faketag
), faketag
);
807 (void)EVP_DecryptUpdate(ctx
, NULL
, &outl
, aad
, sizeof(aad
));
808 (void)EVP_DecryptUpdate(ctx
, buf
, &outl
, buf
, lengths
[testnum
]);
809 (void)EVP_DecryptFinal_ex(ctx
, buf
+ outl
, &outl
);
812 for (count
= 0; COND(c
[D_EVP
][testnum
]); count
++) {
813 (void)EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
);
814 (void)EVP_EncryptUpdate(ctx
, NULL
, &outl
, aad
, sizeof(aad
));
815 (void)EVP_EncryptUpdate(ctx
, buf
, &outl
, buf
, lengths
[testnum
]);
816 (void)EVP_EncryptFinal_ex(ctx
, buf
+ outl
, &outl
);
822 static long rsa_c
[RSA_NUM
][2]; /* # RSA iteration test */
824 static int RSA_sign_loop(void *args
)
826 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
827 unsigned char *buf
= tempargs
->buf
;
828 unsigned char *buf2
= tempargs
->buf2
;
829 size_t *rsa_num
= &tempargs
->sigsize
;
830 EVP_PKEY_CTX
**rsa_sign_ctx
= tempargs
->rsa_sign_ctx
;
833 for (count
= 0; COND(rsa_c
[testnum
][0]); count
++) {
834 ret
= EVP_PKEY_sign(rsa_sign_ctx
[testnum
], buf2
, rsa_num
, buf
, 36);
836 BIO_printf(bio_err
, "RSA sign failure\n");
837 ERR_print_errors(bio_err
);
845 static int RSA_verify_loop(void *args
)
847 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
848 unsigned char *buf
= tempargs
->buf
;
849 unsigned char *buf2
= tempargs
->buf2
;
850 size_t rsa_num
= tempargs
->sigsize
;
851 EVP_PKEY_CTX
**rsa_verify_ctx
= tempargs
->rsa_verify_ctx
;
854 for (count
= 0; COND(rsa_c
[testnum
][1]); count
++) {
855 ret
= EVP_PKEY_verify(rsa_verify_ctx
[testnum
], buf2
, rsa_num
, buf
, 36);
857 BIO_printf(bio_err
, "RSA verify failure\n");
858 ERR_print_errors(bio_err
);
866 #ifndef OPENSSL_NO_DH
867 static long ffdh_c
[FFDH_NUM
][1];
869 static int FFDH_derive_key_loop(void *args
)
871 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
872 EVP_PKEY_CTX
*ffdh_ctx
= tempargs
->ffdh_ctx
[testnum
];
873 unsigned char *derived_secret
= tempargs
->secret_ff_a
;
874 size_t outlen
= MAX_FFDH_SIZE
;
877 for (count
= 0; COND(ffdh_c
[testnum
][0]); count
++)
878 EVP_PKEY_derive(ffdh_ctx
, derived_secret
, &outlen
);
881 #endif /* OPENSSL_NO_DH */
883 static long dsa_c
[DSA_NUM
][2];
884 static int DSA_sign_loop(void *args
)
886 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
887 unsigned char *buf
= tempargs
->buf
;
888 unsigned char *buf2
= tempargs
->buf2
;
889 size_t *dsa_num
= &tempargs
->sigsize
;
890 EVP_PKEY_CTX
**dsa_sign_ctx
= tempargs
->dsa_sign_ctx
;
893 for (count
= 0; COND(dsa_c
[testnum
][0]); count
++) {
894 ret
= EVP_PKEY_sign(dsa_sign_ctx
[testnum
], buf2
, dsa_num
, buf
, 20);
896 BIO_printf(bio_err
, "DSA sign failure\n");
897 ERR_print_errors(bio_err
);
905 static int DSA_verify_loop(void *args
)
907 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
908 unsigned char *buf
= tempargs
->buf
;
909 unsigned char *buf2
= tempargs
->buf2
;
910 size_t dsa_num
= tempargs
->sigsize
;
911 EVP_PKEY_CTX
**dsa_verify_ctx
= tempargs
->dsa_verify_ctx
;
914 for (count
= 0; COND(dsa_c
[testnum
][1]); count
++) {
915 ret
= EVP_PKEY_verify(dsa_verify_ctx
[testnum
], buf2
, dsa_num
, buf
, 20);
917 BIO_printf(bio_err
, "DSA verify failure\n");
918 ERR_print_errors(bio_err
);
926 static long ecdsa_c
[ECDSA_NUM
][2];
927 static int ECDSA_sign_loop(void *args
)
929 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
930 unsigned char *buf
= tempargs
->buf
;
931 unsigned char *buf2
= tempargs
->buf2
;
932 size_t *ecdsa_num
= &tempargs
->sigsize
;
933 EVP_PKEY_CTX
**ecdsa_sign_ctx
= tempargs
->ecdsa_sign_ctx
;
936 for (count
= 0; COND(ecdsa_c
[testnum
][0]); count
++) {
937 ret
= EVP_PKEY_sign(ecdsa_sign_ctx
[testnum
], buf2
, ecdsa_num
, buf
, 20);
939 BIO_printf(bio_err
, "ECDSA sign failure\n");
940 ERR_print_errors(bio_err
);
948 static int ECDSA_verify_loop(void *args
)
950 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
951 unsigned char *buf
= tempargs
->buf
;
952 unsigned char *buf2
= tempargs
->buf2
;
953 size_t ecdsa_num
= tempargs
->sigsize
;
954 EVP_PKEY_CTX
**ecdsa_verify_ctx
= tempargs
->ecdsa_verify_ctx
;
957 for (count
= 0; COND(ecdsa_c
[testnum
][1]); count
++) {
958 ret
= EVP_PKEY_verify(ecdsa_verify_ctx
[testnum
], buf2
, ecdsa_num
,
961 BIO_printf(bio_err
, "ECDSA verify failure\n");
962 ERR_print_errors(bio_err
);
970 /* ******************************************************************** */
971 static long ecdh_c
[EC_NUM
][1];
973 static int ECDH_EVP_derive_key_loop(void *args
)
975 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
976 EVP_PKEY_CTX
*ctx
= tempargs
->ecdh_ctx
[testnum
];
977 unsigned char *derived_secret
= tempargs
->secret_a
;
979 size_t *outlen
= &(tempargs
->outlen
[testnum
]);
981 for (count
= 0; COND(ecdh_c
[testnum
][0]); count
++)
982 EVP_PKEY_derive(ctx
, derived_secret
, outlen
);
987 static long eddsa_c
[EdDSA_NUM
][2];
988 static int EdDSA_sign_loop(void *args
)
990 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
991 unsigned char *buf
= tempargs
->buf
;
992 EVP_MD_CTX
**edctx
= tempargs
->eddsa_ctx
;
993 unsigned char *eddsasig
= tempargs
->buf2
;
994 size_t *eddsasigsize
= &tempargs
->sigsize
;
997 for (count
= 0; COND(eddsa_c
[testnum
][0]); count
++) {
998 ret
= EVP_DigestSign(edctx
[testnum
], eddsasig
, eddsasigsize
, buf
, 20);
1000 BIO_printf(bio_err
, "EdDSA sign failure\n");
1001 ERR_print_errors(bio_err
);
1009 static int EdDSA_verify_loop(void *args
)
1011 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
1012 unsigned char *buf
= tempargs
->buf
;
1013 EVP_MD_CTX
**edctx
= tempargs
->eddsa_ctx2
;
1014 unsigned char *eddsasig
= tempargs
->buf2
;
1015 size_t eddsasigsize
= tempargs
->sigsize
;
1018 for (count
= 0; COND(eddsa_c
[testnum
][1]); count
++) {
1019 ret
= EVP_DigestVerify(edctx
[testnum
], eddsasig
, eddsasigsize
, buf
, 20);
1021 BIO_printf(bio_err
, "EdDSA verify failure\n");
1022 ERR_print_errors(bio_err
);
1030 #ifndef OPENSSL_NO_SM2
1031 static long sm2_c
[SM2_NUM
][2];
1032 static int SM2_sign_loop(void *args
)
1034 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
1035 unsigned char *buf
= tempargs
->buf
;
1036 EVP_MD_CTX
**sm2ctx
= tempargs
->sm2_ctx
;
1037 unsigned char *sm2sig
= tempargs
->buf2
;
1040 EVP_PKEY
**sm2_pkey
= tempargs
->sm2_pkey
;
1041 const size_t max_size
= EVP_PKEY_size(sm2_pkey
[testnum
]);
1043 for (count
= 0; COND(sm2_c
[testnum
][0]); count
++) {
1044 sm2sigsize
= max_size
;
1046 if (!EVP_DigestSignInit(sm2ctx
[testnum
], NULL
, EVP_sm3(),
1047 NULL
, sm2_pkey
[testnum
])) {
1048 BIO_printf(bio_err
, "SM2 init sign failure\n");
1049 ERR_print_errors(bio_err
);
1053 ret
= EVP_DigestSign(sm2ctx
[testnum
], sm2sig
, &sm2sigsize
,
1056 BIO_printf(bio_err
, "SM2 sign failure\n");
1057 ERR_print_errors(bio_err
);
1061 /* update the latest returned size and always use the fixed buffer size */
1062 tempargs
->sigsize
= sm2sigsize
;
1068 static int SM2_verify_loop(void *args
)
1070 loopargs_t
*tempargs
= *(loopargs_t
**) args
;
1071 unsigned char *buf
= tempargs
->buf
;
1072 EVP_MD_CTX
**sm2ctx
= tempargs
->sm2_vfy_ctx
;
1073 unsigned char *sm2sig
= tempargs
->buf2
;
1074 size_t sm2sigsize
= tempargs
->sigsize
;
1076 EVP_PKEY
**sm2_pkey
= tempargs
->sm2_pkey
;
1078 for (count
= 0; COND(sm2_c
[testnum
][1]); count
++) {
1079 if (!EVP_DigestVerifyInit(sm2ctx
[testnum
], NULL
, EVP_sm3(),
1080 NULL
, sm2_pkey
[testnum
])) {
1081 BIO_printf(bio_err
, "SM2 verify init failure\n");
1082 ERR_print_errors(bio_err
);
1086 ret
= EVP_DigestVerify(sm2ctx
[testnum
], sm2sig
, sm2sigsize
,
1089 BIO_printf(bio_err
, "SM2 verify failure\n");
1090 ERR_print_errors(bio_err
);
1097 #endif /* OPENSSL_NO_SM2 */
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;
1109 if (async_jobs
== 0) {
1110 return loop_function((void *)&loopargs
);
1113 for (i
= 0; i
< async_jobs
&& !error
; i
++) {
1114 loopargs_t
*looparg_item
= loopargs
+ i
;
1116 /* Copy pointer content (looparg_t item address) into async context */
1117 ret
= ASYNC_start_job(&loopargs
[i
].inprogress_job
, loopargs
[i
].wait_ctx
,
1118 &job_op_count
, loop_function
,
1119 (void *)&looparg_item
, sizeof(looparg_item
));
1125 if (job_op_count
== -1) {
1128 total_op_count
+= job_op_count
;
1133 BIO_printf(bio_err
, "Failure in the job\n");
1134 ERR_print_errors(bio_err
);
1140 while (num_inprogress
> 0) {
1141 #if defined(OPENSSL_SYS_WINDOWS)
1143 #elif defined(OPENSSL_SYS_UNIX)
1144 int select_result
= 0;
1145 OSSL_ASYNC_FD max_fd
= 0;
1148 FD_ZERO(&waitfdset
);
1150 for (i
= 0; i
< async_jobs
&& num_inprogress
> 0; i
++) {
1151 if (loopargs
[i
].inprogress_job
== NULL
)
1154 if (!ASYNC_WAIT_CTX_get_all_fds
1155 (loopargs
[i
].wait_ctx
, NULL
, &num_job_fds
)
1156 || num_job_fds
> 1) {
1157 BIO_printf(bio_err
, "Too many fds in ASYNC_WAIT_CTX\n");
1158 ERR_print_errors(bio_err
);
1162 ASYNC_WAIT_CTX_get_all_fds(loopargs
[i
].wait_ctx
, &job_fd
,
1164 FD_SET(job_fd
, &waitfdset
);
1165 if (job_fd
> max_fd
)
1169 if (max_fd
>= (OSSL_ASYNC_FD
)FD_SETSIZE
) {
1171 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1172 "Decrease the value of async_jobs\n",
1173 max_fd
, FD_SETSIZE
);
1174 ERR_print_errors(bio_err
);
1179 select_result
= select(max_fd
+ 1, &waitfdset
, NULL
, NULL
, NULL
);
1180 if (select_result
== -1 && errno
== EINTR
)
1183 if (select_result
== -1) {
1184 BIO_printf(bio_err
, "Failure in the select\n");
1185 ERR_print_errors(bio_err
);
1190 if (select_result
== 0)
1194 for (i
= 0; i
< async_jobs
; i
++) {
1195 if (loopargs
[i
].inprogress_job
== NULL
)
1198 if (!ASYNC_WAIT_CTX_get_all_fds
1199 (loopargs
[i
].wait_ctx
, NULL
, &num_job_fds
)
1200 || num_job_fds
> 1) {
1201 BIO_printf(bio_err
, "Too many fds in ASYNC_WAIT_CTX\n");
1202 ERR_print_errors(bio_err
);
1206 ASYNC_WAIT_CTX_get_all_fds(loopargs
[i
].wait_ctx
, &job_fd
,
1209 #if defined(OPENSSL_SYS_UNIX)
1210 if (num_job_fds
== 1 && !FD_ISSET(job_fd
, &waitfdset
))
1212 #elif defined(OPENSSL_SYS_WINDOWS)
1213 if (num_job_fds
== 1
1214 && !PeekNamedPipe(job_fd
, NULL
, 0, NULL
, &avail
, NULL
)
1219 ret
= ASYNC_start_job(&loopargs
[i
].inprogress_job
,
1220 loopargs
[i
].wait_ctx
, &job_op_count
,
1221 loop_function
, (void *)(loopargs
+ i
),
1222 sizeof(loopargs_t
));
1227 if (job_op_count
== -1) {
1230 total_op_count
+= job_op_count
;
1233 loopargs
[i
].inprogress_job
= NULL
;
1238 loopargs
[i
].inprogress_job
= NULL
;
1239 BIO_printf(bio_err
, "Failure in the job\n");
1240 ERR_print_errors(bio_err
);
1247 return error
? -1 : total_op_count
;
1250 typedef struct ec_curve_st
{
1254 size_t sigsize
; /* only used for EdDSA curves */
1257 static EVP_PKEY
*get_ecdsa(const EC_CURVE
*curve
)
1259 EVP_PKEY_CTX
*kctx
= NULL
;
1260 EVP_PKEY
*key
= NULL
;
1262 /* Ensure that the error queue is empty */
1263 if (ERR_peek_error()) {
1265 "WARNING: the error queue contains previous unhandled errors.\n");
1266 ERR_print_errors(bio_err
);
1270 * Let's try to create a ctx directly from the NID: this works for
1271 * curves like Curve25519 that are not implemented through the low
1272 * level EC interface.
1273 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
1274 * then we set the curve by NID before deriving the actual keygen
1275 * ctx for that specific curve.
1277 kctx
= EVP_PKEY_CTX_new_id(curve
->nid
, NULL
);
1279 EVP_PKEY_CTX
*pctx
= NULL
;
1280 EVP_PKEY
*params
= NULL
;
1282 * If we reach this code EVP_PKEY_CTX_new_id() failed and a
1283 * "int_ctx_new:unsupported algorithm" error was added to the
1285 * We remove it from the error queue as we are handling it.
1287 unsigned long error
= ERR_peek_error();
1289 if (error
== ERR_peek_last_error() /* oldest and latest errors match */
1290 /* check that the error origin matches */
1291 && ERR_GET_LIB(error
) == ERR_LIB_EVP
1292 && (ERR_GET_REASON(error
) == EVP_R_UNSUPPORTED_ALGORITHM
1293 || ERR_GET_REASON(error
) == ERR_R_UNSUPPORTED
))
1294 ERR_get_error(); /* pop error from queue */
1295 if (ERR_peek_error()) {
1297 "Unhandled error in the error queue during EC key setup.\n");
1298 ERR_print_errors(bio_err
);
1302 /* Create the context for parameter generation */
1303 if ((pctx
= EVP_PKEY_CTX_new_from_name(NULL
, "EC", NULL
)) == NULL
1304 || EVP_PKEY_paramgen_init(pctx
) <= 0
1305 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx
,
1307 || EVP_PKEY_paramgen(pctx
, ¶ms
) <= 0) {
1308 BIO_printf(bio_err
, "EC params init failure.\n");
1309 ERR_print_errors(bio_err
);
1310 EVP_PKEY_CTX_free(pctx
);
1313 EVP_PKEY_CTX_free(pctx
);
1315 /* Create the context for the key generation */
1316 kctx
= EVP_PKEY_CTX_new(params
, NULL
);
1317 EVP_PKEY_free(params
);
1320 || EVP_PKEY_keygen_init(kctx
) <= 0
1321 || EVP_PKEY_keygen(kctx
, &key
) <= 0) {
1322 BIO_printf(bio_err
, "EC key generation failure.\n");
1323 ERR_print_errors(bio_err
);
1326 EVP_PKEY_CTX_free(kctx
);
1330 #define stop_it(do_it, test_num)\
1331 memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
1333 int speed_main(int argc
, char **argv
)
1336 loopargs_t
*loopargs
= NULL
;
1338 const char *engine_id
= NULL
;
1339 EVP_CIPHER
*evp_cipher
= NULL
;
1342 int async_init
= 0, multiblock
= 0, pr_header
= 0;
1343 uint8_t doit
[ALGOR_NUM
] = { 0 };
1344 int ret
= 1, misalign
= 0, lengths_single
= 0, aead
= 0;
1346 unsigned int size_num
= SIZE_NUM
;
1347 unsigned int i
, k
, loopargs_len
= 0, async_jobs
= 0;
1351 EVP_PKEY_CTX
*genctx
= NULL
;
1356 openssl_speed_sec_t seconds
= { SECONDS
, RSA_SECONDS
, DSA_SECONDS
,
1357 ECDSA_SECONDS
, ECDH_SECONDS
,
1358 EdDSA_SECONDS
, SM2_SECONDS
,
1361 static const unsigned char key32
[32] = {
1362 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1363 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1364 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1365 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1367 static const unsigned char deskey
[] = {
1368 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, /* key1 */
1369 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, /* key2 */
1370 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 /* key3 */
1372 static const struct {
1373 const unsigned char *data
;
1374 unsigned int length
;
1377 { test512
, sizeof(test512
), 512 },
1378 { test1024
, sizeof(test1024
), 1024 },
1379 { test2048
, sizeof(test2048
), 2048 },
1380 { test3072
, sizeof(test3072
), 3072 },
1381 { test4096
, sizeof(test4096
), 4096 },
1382 { test7680
, sizeof(test7680
), 7680 },
1383 { test15360
, sizeof(test15360
), 15360 }
1385 uint8_t rsa_doit
[RSA_NUM
] = { 0 };
1386 int primes
= RSA_DEFAULT_PRIME_NUM
;
1387 #ifndef OPENSSL_NO_DH
1388 typedef struct ffdh_params_st
{
1394 static const FFDH_PARAMS ffdh_params
[FFDH_NUM
] = {
1395 {"ffdh2048", NID_ffdhe2048
, 2048},
1396 {"ffdh3072", NID_ffdhe3072
, 3072},
1397 {"ffdh4096", NID_ffdhe4096
, 4096},
1398 {"ffdh6144", NID_ffdhe6144
, 6144},
1399 {"ffdh8192", NID_ffdhe8192
, 8192}
1401 uint8_t ffdh_doit
[FFDH_NUM
] = { 0 };
1403 #endif /* OPENSSL_NO_DH */
1404 static const unsigned int dsa_bits
[DSA_NUM
] = { 512, 1024, 2048 };
1405 uint8_t dsa_doit
[DSA_NUM
] = { 0 };
1407 * We only test over the following curves as they are representative, To
1408 * add tests over more curves, simply add the curve NID and curve name to
1409 * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
1410 * lists accordingly.
1412 static const EC_CURVE ec_curves
[EC_NUM
] = {
1414 {"secp160r1", NID_secp160r1
, 160},
1415 {"nistp192", NID_X9_62_prime192v1
, 192},
1416 {"nistp224", NID_secp224r1
, 224},
1417 {"nistp256", NID_X9_62_prime256v1
, 256},
1418 {"nistp384", NID_secp384r1
, 384},
1419 {"nistp521", NID_secp521r1
, 521},
1420 #ifndef OPENSSL_NO_EC2M
1422 {"nistk163", NID_sect163k1
, 163},
1423 {"nistk233", NID_sect233k1
, 233},
1424 {"nistk283", NID_sect283k1
, 283},
1425 {"nistk409", NID_sect409k1
, 409},
1426 {"nistk571", NID_sect571k1
, 571},
1427 {"nistb163", NID_sect163r2
, 163},
1428 {"nistb233", NID_sect233r1
, 233},
1429 {"nistb283", NID_sect283r1
, 283},
1430 {"nistb409", NID_sect409r1
, 409},
1431 {"nistb571", NID_sect571r1
, 571},
1433 {"brainpoolP256r1", NID_brainpoolP256r1
, 256},
1434 {"brainpoolP256t1", NID_brainpoolP256t1
, 256},
1435 {"brainpoolP384r1", NID_brainpoolP384r1
, 384},
1436 {"brainpoolP384t1", NID_brainpoolP384t1
, 384},
1437 {"brainpoolP512r1", NID_brainpoolP512r1
, 512},
1438 {"brainpoolP512t1", NID_brainpoolP512t1
, 512},
1439 /* Other and ECDH only ones */
1440 {"X25519", NID_X25519
, 253},
1441 {"X448", NID_X448
, 448}
1443 static const EC_CURVE ed_curves
[EdDSA_NUM
] = {
1445 {"Ed25519", NID_ED25519
, 253, 64},
1446 {"Ed448", NID_ED448
, 456, 114}
1448 #ifndef OPENSSL_NO_SM2
1449 static const EC_CURVE sm2_curves
[SM2_NUM
] = {
1451 {"CurveSM2", NID_sm2
, 256}
1453 uint8_t sm2_doit
[SM2_NUM
] = { 0 };
1455 uint8_t ecdsa_doit
[ECDSA_NUM
] = { 0 };
1456 uint8_t ecdh_doit
[EC_NUM
] = { 0 };
1457 uint8_t eddsa_doit
[EdDSA_NUM
] = { 0 };
1459 /* checks declarated curves against choices list. */
1460 OPENSSL_assert(ed_curves
[EdDSA_NUM
- 1].nid
== NID_ED448
);
1461 OPENSSL_assert(strcmp(eddsa_choices
[EdDSA_NUM
- 1].name
, "ed448") == 0);
1463 OPENSSL_assert(ec_curves
[EC_NUM
- 1].nid
== NID_X448
);
1464 OPENSSL_assert(strcmp(ecdh_choices
[EC_NUM
- 1].name
, "ecdhx448") == 0);
1466 OPENSSL_assert(ec_curves
[ECDSA_NUM
- 1].nid
== NID_brainpoolP512t1
);
1467 OPENSSL_assert(strcmp(ecdsa_choices
[ECDSA_NUM
- 1].name
, "ecdsabrp512t1") == 0);
1469 #ifndef OPENSSL_NO_SM2
1470 OPENSSL_assert(sm2_curves
[SM2_NUM
- 1].nid
== NID_sm2
);
1471 OPENSSL_assert(strcmp(sm2_choices
[SM2_NUM
- 1].name
, "curveSM2") == 0);
1474 prog
= opt_init(argc
, argv
, speed_options
);
1475 while ((o
= opt_next()) != OPT_EOF
) {
1480 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1483 opt_help(speed_options
);
1491 BIO_printf(bio_err
, "%s: -evp option cannot be used more than once\n", prog
);
1495 if (!opt_cipher_silent(opt_arg(), &evp_cipher
)) {
1496 if (have_md(opt_arg()))
1497 evp_md_name
= opt_arg();
1499 if (evp_cipher
== NULL
&& evp_md_name
== NULL
) {
1500 ERR_clear_last_mark();
1502 "%s: %s is an unknown cipher or digest\n",
1510 if (!have_md(opt_arg())) {
1511 BIO_printf(bio_err
, "%s: %s is an unknown digest\n",
1515 evp_mac_mdname
= opt_arg();
1519 if (!have_cipher(opt_arg())) {
1520 BIO_printf(bio_err
, "%s: %s is an unknown cipher\n",
1524 evp_mac_ciphername
= opt_arg();
1525 doit
[D_EVP_CMAC
] = 1;
1532 * In a forked execution, an engine might need to be
1533 * initialised by each child process, not by the parent.
1534 * So store the name here and run setup_engine() later on.
1536 engine_id
= opt_arg();
1540 multi
= atoi(opt_arg());
1544 #ifndef OPENSSL_NO_ASYNC
1545 async_jobs
= atoi(opt_arg());
1546 if (!ASYNC_is_capable()) {
1548 "%s: async_jobs specified but async not supported\n",
1552 if (async_jobs
> 99999) {
1553 BIO_printf(bio_err
, "%s: too many async_jobs\n", prog
);
1559 misalign
= opt_int_arg();
1560 if (misalign
> MISALIGN
) {
1562 "%s: Maximum offset is %d\n", prog
, MISALIGN
);
1571 #ifdef OPENSSL_NO_MULTIBLOCK
1573 "%s: -mb specified but multi-block support is disabled\n",
1582 case OPT_PROV_CASES
:
1583 if (!opt_provider(o
))
1587 primes
= opt_int_arg();
1590 seconds
.sym
= seconds
.rsa
= seconds
.dsa
= seconds
.ecdsa
1591 = seconds
.ecdh
= seconds
.eddsa
1592 = seconds
.sm2
= seconds
.ffdh
= atoi(opt_arg());
1595 lengths_single
= atoi(opt_arg());
1596 lengths
= &lengths_single
;
1605 /* Remaining arguments are algorithms. */
1606 argc
= opt_num_rest();
1609 if (!app_RAND_load())
1612 for (; *argv
; argv
++) {
1613 const char *algo
= *argv
;
1615 if (opt_found(algo
, doit_choices
, &i
)) {
1619 if (strcmp(algo
, "des") == 0) {
1620 doit
[D_CBC_DES
] = doit
[D_EDE3_DES
] = 1;
1623 if (strcmp(algo
, "sha") == 0) {
1624 doit
[D_SHA1
] = doit
[D_SHA256
] = doit
[D_SHA512
] = 1;
1627 #ifndef OPENSSL_NO_DEPRECATED_3_0
1628 if (strcmp(algo
, "openssl") == 0) /* just for compatibility */
1631 if (strncmp(algo
, "rsa", 3) == 0) {
1632 if (algo
[3] == '\0') {
1633 memset(rsa_doit
, 1, sizeof(rsa_doit
));
1636 if (opt_found(algo
, rsa_choices
, &i
)) {
1641 #ifndef OPENSSL_NO_DH
1642 if (strncmp(algo
, "ffdh", 4) == 0) {
1643 if (algo
[4] == '\0') {
1644 memset(ffdh_doit
, 1, sizeof(ffdh_doit
));
1647 if (opt_found(algo
, ffdh_choices
, &i
)) {
1653 if (strncmp(algo
, "dsa", 3) == 0) {
1654 if (algo
[3] == '\0') {
1655 memset(dsa_doit
, 1, sizeof(dsa_doit
));
1658 if (opt_found(algo
, dsa_choices
, &i
)) {
1663 if (strcmp(algo
, "aes") == 0) {
1664 doit
[D_CBC_128_AES
] = doit
[D_CBC_192_AES
] = doit
[D_CBC_256_AES
] = 1;
1667 if (strcmp(algo
, "camellia") == 0) {
1668 doit
[D_CBC_128_CML
] = doit
[D_CBC_192_CML
] = doit
[D_CBC_256_CML
] = 1;
1671 if (strncmp(algo
, "ecdsa", 5) == 0) {
1672 if (algo
[5] == '\0') {
1673 memset(ecdsa_doit
, 1, sizeof(ecdsa_doit
));
1676 if (opt_found(algo
, ecdsa_choices
, &i
)) {
1681 if (strncmp(algo
, "ecdh", 4) == 0) {
1682 if (algo
[4] == '\0') {
1683 memset(ecdh_doit
, 1, sizeof(ecdh_doit
));
1686 if (opt_found(algo
, ecdh_choices
, &i
)) {
1691 if (strcmp(algo
, "eddsa") == 0) {
1692 memset(eddsa_doit
, 1, sizeof(eddsa_doit
));
1695 if (opt_found(algo
, eddsa_choices
, &i
)) {
1699 #ifndef OPENSSL_NO_SM2
1700 if (strcmp(algo
, "sm2") == 0) {
1701 memset(sm2_doit
, 1, sizeof(sm2_doit
));
1704 if (opt_found(algo
, sm2_choices
, &i
)) {
1709 BIO_printf(bio_err
, "%s: Unknown algorithm %s\n", prog
, algo
);
1715 if (evp_cipher
== NULL
) {
1716 BIO_printf(bio_err
, "-aead can be used only with an AEAD cipher\n");
1718 } else if (!(EVP_CIPHER_flags(evp_cipher
) &
1719 EVP_CIPH_FLAG_AEAD_CIPHER
)) {
1720 BIO_printf(bio_err
, "%s is not an AEAD cipher\n",
1721 EVP_CIPHER_name(evp_cipher
));
1726 if (evp_cipher
== NULL
) {
1727 BIO_printf(bio_err
, "-mb can be used only with a multi-block"
1728 " capable cipher\n");
1730 } else if (!(EVP_CIPHER_flags(evp_cipher
) &
1731 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
)) {
1732 BIO_printf(bio_err
, "%s is not a multi-block capable\n",
1733 EVP_CIPHER_name(evp_cipher
));
1735 } else if (async_jobs
> 0) {
1736 BIO_printf(bio_err
, "Async mode is not supported with -mb");
1741 /* Initialize the job pool if async mode is enabled */
1742 if (async_jobs
> 0) {
1743 async_init
= ASYNC_init_thread(async_jobs
, async_jobs
);
1745 BIO_printf(bio_err
, "Error creating the ASYNC job pool\n");
1750 loopargs_len
= (async_jobs
== 0 ? 1 : async_jobs
);
1752 app_malloc(loopargs_len
* sizeof(loopargs_t
), "array of loopargs");
1753 memset(loopargs
, 0, loopargs_len
* sizeof(loopargs_t
));
1755 for (i
= 0; i
< loopargs_len
; i
++) {
1756 if (async_jobs
> 0) {
1757 loopargs
[i
].wait_ctx
= ASYNC_WAIT_CTX_new();
1758 if (loopargs
[i
].wait_ctx
== NULL
) {
1759 BIO_printf(bio_err
, "Error creating the ASYNC_WAIT_CTX\n");
1764 buflen
= lengths
[size_num
- 1];
1765 if (buflen
< 36) /* size of random vector in RSA benchmark */
1767 buflen
+= MAX_MISALIGNMENT
+ 1;
1768 loopargs
[i
].buf_malloc
= app_malloc(buflen
, "input buffer");
1769 loopargs
[i
].buf2_malloc
= app_malloc(buflen
, "input buffer");
1770 memset(loopargs
[i
].buf_malloc
, 0, buflen
);
1771 memset(loopargs
[i
].buf2_malloc
, 0, buflen
);
1773 /* Align the start of buffers on a 64 byte boundary */
1774 loopargs
[i
].buf
= loopargs
[i
].buf_malloc
+ misalign
;
1775 loopargs
[i
].buf2
= loopargs
[i
].buf2_malloc
+ misalign
;
1776 loopargs
[i
].secret_a
= app_malloc(MAX_ECDH_SIZE
, "ECDH secret a");
1777 loopargs
[i
].secret_b
= app_malloc(MAX_ECDH_SIZE
, "ECDH secret b");
1778 #ifndef OPENSSL_NO_DH
1779 loopargs
[i
].secret_ff_a
= app_malloc(MAX_FFDH_SIZE
, "FFDH secret a");
1780 loopargs
[i
].secret_ff_b
= app_malloc(MAX_FFDH_SIZE
, "FFDH secret b");
1785 if (multi
&& do_multi(multi
, size_num
))
1789 /* Initialize the engine after the fork */
1790 e
= setup_engine(engine_id
, 0);
1792 /* No parameters; turn on everything. */
1793 if (argc
== 0 && !doit
[D_EVP
] && !doit
[D_HMAC
] && !doit
[D_EVP_CMAC
]) {
1796 memset(doit
, 1, sizeof(doit
));
1797 doit
[D_EVP
] = doit
[D_EVP_CMAC
] = 0;
1799 for (i
= D_MD2
; i
<= D_WHIRLPOOL
; i
++) {
1800 if (!have_md(names
[i
]))
1803 for (i
= D_CBC_DES
; i
<= D_CBC_256_CML
; i
++) {
1804 if (!have_cipher(names
[i
]))
1807 if ((mac
= EVP_MAC_fetch(NULL
, "GMAC", NULL
)) != NULL
)
1811 if ((mac
= EVP_MAC_fetch(NULL
, "HMAC", NULL
)) != NULL
)
1816 memset(rsa_doit
, 1, sizeof(rsa_doit
));
1817 #ifndef OPENSSL_NO_DH
1818 memset(ffdh_doit
, 1, sizeof(ffdh_doit
));
1820 memset(dsa_doit
, 1, sizeof(dsa_doit
));
1821 memset(ecdsa_doit
, 1, sizeof(ecdsa_doit
));
1822 memset(ecdh_doit
, 1, sizeof(ecdh_doit
));
1823 memset(eddsa_doit
, 1, sizeof(eddsa_doit
));
1824 #ifndef OPENSSL_NO_SM2
1825 memset(sm2_doit
, 1, sizeof(sm2_doit
));
1828 for (i
= 0; i
< ALGOR_NUM
; i
++)
1832 if (usertime
== 0 && !mr
)
1834 "You have chosen to measure elapsed time "
1835 "instead of user CPU time.\n");
1838 signal(SIGALRM
, alarmed
);
1842 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1843 print_message(names
[D_MD2
], c
[D_MD2
][testnum
], lengths
[testnum
],
1846 count
= run_benchmark(async_jobs
, EVP_Digest_MD2_loop
, loopargs
);
1848 print_result(D_MD2
, testnum
, count
, d
);
1855 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1856 print_message(names
[D_MDC2
], c
[D_MDC2
][testnum
], lengths
[testnum
],
1859 count
= run_benchmark(async_jobs
, EVP_Digest_MDC2_loop
, loopargs
);
1861 print_result(D_MDC2
, testnum
, count
, d
);
1868 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1869 print_message(names
[D_MD4
], c
[D_MD4
][testnum
], lengths
[testnum
],
1872 count
= run_benchmark(async_jobs
, EVP_Digest_MD4_loop
, loopargs
);
1874 print_result(D_MD4
, testnum
, count
, d
);
1881 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1882 print_message(names
[D_MD5
], c
[D_MD5
][testnum
], lengths
[testnum
],
1885 count
= run_benchmark(async_jobs
, MD5_loop
, loopargs
);
1887 print_result(D_MD5
, testnum
, count
, d
);
1894 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1895 print_message(names
[D_SHA1
], c
[D_SHA1
][testnum
], lengths
[testnum
],
1898 count
= run_benchmark(async_jobs
, SHA1_loop
, loopargs
);
1900 print_result(D_SHA1
, testnum
, count
, d
);
1906 if (doit
[D_SHA256
]) {
1907 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1908 print_message(names
[D_SHA256
], c
[D_SHA256
][testnum
],
1909 lengths
[testnum
], seconds
.sym
);
1911 count
= run_benchmark(async_jobs
, SHA256_loop
, loopargs
);
1913 print_result(D_SHA256
, testnum
, count
, d
);
1919 if (doit
[D_SHA512
]) {
1920 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1921 print_message(names
[D_SHA512
], c
[D_SHA512
][testnum
],
1922 lengths
[testnum
], seconds
.sym
);
1924 count
= run_benchmark(async_jobs
, SHA512_loop
, loopargs
);
1926 print_result(D_SHA512
, testnum
, count
, d
);
1932 if (doit
[D_WHIRLPOOL
]) {
1933 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1934 print_message(names
[D_WHIRLPOOL
], c
[D_WHIRLPOOL
][testnum
],
1935 lengths
[testnum
], seconds
.sym
);
1937 count
= run_benchmark(async_jobs
, WHIRLPOOL_loop
, loopargs
);
1939 print_result(D_WHIRLPOOL
, testnum
, count
, d
);
1945 if (doit
[D_RMD160
]) {
1946 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1947 print_message(names
[D_RMD160
], c
[D_RMD160
][testnum
],
1948 lengths
[testnum
], seconds
.sym
);
1950 count
= run_benchmark(async_jobs
, EVP_Digest_RMD160_loop
, loopargs
);
1952 print_result(D_RMD160
, testnum
, count
, d
);
1959 static const char hmac_key
[] = "This is a key...";
1960 int len
= strlen(hmac_key
);
1961 EVP_MAC
*mac
= EVP_MAC_fetch(NULL
, "HMAC", NULL
);
1962 OSSL_PARAM params
[3];
1964 if (mac
== NULL
|| evp_mac_mdname
== NULL
)
1967 evp_hmac_name
= app_malloc(sizeof("hmac()") + strlen(evp_mac_mdname
),
1969 sprintf(evp_hmac_name
, "hmac(%s)", evp_mac_mdname
);
1970 names
[D_HMAC
] = evp_hmac_name
;
1973 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST
,
1976 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
1977 (char *)hmac_key
, len
);
1978 params
[2] = OSSL_PARAM_construct_end();
1980 for (i
= 0; i
< loopargs_len
; i
++) {
1981 loopargs
[i
].mctx
= EVP_MAC_CTX_new(mac
);
1982 if (loopargs
[i
].mctx
== NULL
)
1985 if (!EVP_MAC_CTX_set_params(loopargs
[i
].mctx
, params
))
1988 for (testnum
= 0; testnum
< size_num
; testnum
++) {
1989 print_message(names
[D_HMAC
], c
[D_HMAC
][testnum
], lengths
[testnum
],
1992 count
= run_benchmark(async_jobs
, HMAC_loop
, loopargs
);
1994 print_result(D_HMAC
, testnum
, count
, d
);
1998 for (i
= 0; i
< loopargs_len
; i
++)
1999 EVP_MAC_CTX_free(loopargs
[i
].mctx
);
2003 if (doit
[D_CBC_DES
]) {
2006 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2007 loopargs
[i
].ctx
= init_evp_cipher_ctx("des-cbc", deskey
,
2008 sizeof(deskey
) / 3);
2009 st
= loopargs
[i
].ctx
!= NULL
;
2011 algindex
= D_CBC_DES
;
2012 for (testnum
= 0; st
&& testnum
< size_num
; testnum
++) {
2013 print_message(names
[D_CBC_DES
], c
[D_CBC_DES
][testnum
],
2014 lengths
[testnum
], seconds
.sym
);
2016 count
= run_benchmark(async_jobs
, EVP_Cipher_loop
, loopargs
);
2018 print_result(D_CBC_DES
, testnum
, count
, d
);
2020 for (i
= 0; i
< loopargs_len
; i
++)
2021 EVP_CIPHER_CTX_free(loopargs
[i
].ctx
);
2024 if (doit
[D_EDE3_DES
]) {
2027 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2028 loopargs
[i
].ctx
= init_evp_cipher_ctx("des-ede3-cbc", deskey
,
2030 st
= loopargs
[i
].ctx
!= NULL
;
2032 algindex
= D_EDE3_DES
;
2033 for (testnum
= 0; st
&& testnum
< size_num
; testnum
++) {
2034 print_message(names
[D_EDE3_DES
], c
[D_EDE3_DES
][testnum
],
2035 lengths
[testnum
], seconds
.sym
);
2038 run_benchmark(async_jobs
, EVP_Cipher_loop
, loopargs
);
2040 print_result(D_EDE3_DES
, testnum
, count
, d
);
2042 for (i
= 0; i
< loopargs_len
; i
++)
2043 EVP_CIPHER_CTX_free(loopargs
[i
].ctx
);
2046 for (k
= 0; k
< 3; k
++) {
2047 algindex
= D_CBC_128_AES
+ k
;
2048 if (doit
[algindex
]) {
2051 keylen
= 16 + k
* 8;
2052 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2053 loopargs
[i
].ctx
= init_evp_cipher_ctx(names
[algindex
],
2055 st
= loopargs
[i
].ctx
!= NULL
;
2058 for (testnum
= 0; st
&& testnum
< size_num
; testnum
++) {
2059 print_message(names
[algindex
], c
[algindex
][testnum
],
2060 lengths
[testnum
], seconds
.sym
);
2063 run_benchmark(async_jobs
, EVP_Cipher_loop
, loopargs
);
2065 print_result(algindex
, testnum
, count
, d
);
2067 for (i
= 0; i
< loopargs_len
; i
++)
2068 EVP_CIPHER_CTX_free(loopargs
[i
].ctx
);
2072 for (k
= 0; k
< 3; k
++) {
2073 algindex
= D_CBC_128_CML
+ k
;
2074 if (doit
[algindex
]) {
2077 keylen
= 16 + k
* 8;
2078 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2079 loopargs
[i
].ctx
= init_evp_cipher_ctx(names
[algindex
],
2081 st
= loopargs
[i
].ctx
!= NULL
;
2084 for (testnum
= 0; st
&& testnum
< size_num
; testnum
++) {
2085 print_message(names
[algindex
], c
[algindex
][testnum
],
2086 lengths
[testnum
], seconds
.sym
);
2089 run_benchmark(async_jobs
, EVP_Cipher_loop
, loopargs
);
2091 print_result(algindex
, testnum
, count
, d
);
2093 for (i
= 0; i
< loopargs_len
; i
++)
2094 EVP_CIPHER_CTX_free(loopargs
[i
].ctx
);
2098 for (algindex
= D_RC4
; algindex
<= D_CBC_CAST
; algindex
++) {
2099 if (doit
[algindex
]) {
2103 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2104 loopargs
[i
].ctx
= init_evp_cipher_ctx(names
[algindex
],
2106 st
= loopargs
[i
].ctx
!= NULL
;
2109 for (testnum
= 0; st
&& testnum
< size_num
; testnum
++) {
2110 print_message(names
[algindex
], c
[algindex
][testnum
],
2111 lengths
[testnum
], seconds
.sym
);
2114 run_benchmark(async_jobs
, EVP_Cipher_loop
, loopargs
);
2116 print_result(algindex
, testnum
, count
, d
);
2118 for (i
= 0; i
< loopargs_len
; i
++)
2119 EVP_CIPHER_CTX_free(loopargs
[i
].ctx
);
2122 if (doit
[D_GHASH
]) {
2123 static const char gmac_iv
[] = "0123456789ab";
2124 EVP_MAC
*mac
= EVP_MAC_fetch(NULL
, "GMAC", NULL
);
2125 OSSL_PARAM params
[3];
2130 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER
,
2132 params
[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV
,
2134 sizeof(gmac_iv
) - 1);
2135 params
[2] = OSSL_PARAM_construct_end();
2137 for (i
= 0; i
< loopargs_len
; i
++) {
2138 loopargs
[i
].mctx
= EVP_MAC_CTX_new(mac
);
2139 if (loopargs
[i
].mctx
== NULL
)
2142 if (!EVP_MAC_init(loopargs
[i
].mctx
, key32
, 16, params
))
2145 for (testnum
= 0; testnum
< size_num
; testnum
++) {
2146 print_message(names
[D_GHASH
], c
[D_GHASH
][testnum
], lengths
[testnum
],
2149 count
= run_benchmark(async_jobs
, GHASH_loop
, loopargs
);
2151 print_result(D_GHASH
, testnum
, count
, d
);
2155 for (i
= 0; i
< loopargs_len
; i
++)
2156 EVP_MAC_CTX_free(loopargs
[i
].mctx
);
2161 for (testnum
= 0; testnum
< size_num
; testnum
++) {
2162 print_message(names
[D_RAND
], c
[D_RAND
][testnum
], lengths
[testnum
],
2165 count
= run_benchmark(async_jobs
, RAND_bytes_loop
, loopargs
);
2167 print_result(D_RAND
, testnum
, count
, d
);
2172 if (evp_cipher
!= NULL
) {
2173 int (*loopfunc
) (void *) = EVP_Update_loop
;
2175 if (multiblock
&& (EVP_CIPHER_flags(evp_cipher
) &
2176 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
)) {
2177 multiblock_speed(evp_cipher
, lengths_single
, &seconds
);
2182 names
[D_EVP
] = EVP_CIPHER_name(evp_cipher
);
2184 if (EVP_CIPHER_mode(evp_cipher
) == EVP_CIPH_CCM_MODE
) {
2185 loopfunc
= EVP_Update_loop_ccm
;
2186 } else if (aead
&& (EVP_CIPHER_flags(evp_cipher
) &
2187 EVP_CIPH_FLAG_AEAD_CIPHER
)) {
2188 loopfunc
= EVP_Update_loop_aead
;
2189 if (lengths
== lengths_list
) {
2190 lengths
= aead_lengths_list
;
2191 size_num
= OSSL_NELEM(aead_lengths_list
);
2195 for (testnum
= 0; testnum
< size_num
; testnum
++) {
2196 print_message(names
[D_EVP
], c
[D_EVP
][testnum
], lengths
[testnum
],
2199 for (k
= 0; k
< loopargs_len
; k
++) {
2200 loopargs
[k
].ctx
= EVP_CIPHER_CTX_new();
2201 if (loopargs
[k
].ctx
== NULL
) {
2202 BIO_printf(bio_err
, "\nEVP_CIPHER_CTX_new failure\n");
2205 if (!EVP_CipherInit_ex(loopargs
[k
].ctx
, evp_cipher
, NULL
,
2206 NULL
, iv
, decrypt
? 0 : 1)) {
2207 BIO_printf(bio_err
, "\nEVP_CipherInit_ex failure\n");
2208 ERR_print_errors(bio_err
);
2212 EVP_CIPHER_CTX_set_padding(loopargs
[k
].ctx
, 0);
2214 keylen
= EVP_CIPHER_CTX_key_length(loopargs
[k
].ctx
);
2215 loopargs
[k
].key
= app_malloc(keylen
, "evp_cipher key");
2216 EVP_CIPHER_CTX_rand_key(loopargs
[k
].ctx
, loopargs
[k
].key
);
2217 if (!EVP_CipherInit_ex(loopargs
[k
].ctx
, NULL
, NULL
,
2218 loopargs
[k
].key
, NULL
, -1)) {
2219 BIO_printf(bio_err
, "\nEVP_CipherInit_ex failure\n");
2220 ERR_print_errors(bio_err
);
2223 OPENSSL_clear_free(loopargs
[k
].key
, keylen
);
2225 /* SIV mode only allows for a single Update operation */
2226 if (EVP_CIPHER_mode(evp_cipher
) == EVP_CIPH_SIV_MODE
)
2227 EVP_CIPHER_CTX_ctrl(loopargs
[k
].ctx
, EVP_CTRL_SET_SPEED
,
2232 count
= run_benchmark(async_jobs
, loopfunc
, loopargs
);
2234 for (k
= 0; k
< loopargs_len
; k
++)
2235 EVP_CIPHER_CTX_free(loopargs
[k
].ctx
);
2236 print_result(D_EVP
, testnum
, count
, d
);
2238 } else if (evp_md_name
!= NULL
) {
2239 names
[D_EVP
] = evp_md_name
;
2241 for (testnum
= 0; testnum
< size_num
; testnum
++) {
2242 print_message(names
[D_EVP
], c
[D_EVP
][testnum
], lengths
[testnum
],
2245 count
= run_benchmark(async_jobs
, EVP_Digest_md_loop
, loopargs
);
2247 print_result(D_EVP
, testnum
, count
, d
);
2254 if (doit
[D_EVP_CMAC
]) {
2255 EVP_MAC
*mac
= EVP_MAC_fetch(NULL
, "CMAC", NULL
);
2256 OSSL_PARAM params
[3];
2257 EVP_CIPHER
*cipher
= NULL
;
2259 if (mac
== NULL
|| evp_mac_ciphername
== NULL
)
2261 if (!opt_cipher(evp_mac_ciphername
, &cipher
))
2264 keylen
= EVP_CIPHER_key_length(cipher
);
2265 EVP_CIPHER_free(cipher
);
2266 if (keylen
<= 0 || keylen
> (int)sizeof(key32
)) {
2267 BIO_printf(bio_err
, "\nRequested CMAC cipher with unsupported key length.\n");
2270 evp_cmac_name
= app_malloc(sizeof("cmac()")
2271 + strlen(evp_mac_ciphername
), "CMAC name");
2272 sprintf(evp_cmac_name
, "cmac(%s)", evp_mac_ciphername
);
2273 names
[D_EVP_CMAC
] = evp_cmac_name
;
2275 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER
,
2276 evp_mac_ciphername
, 0);
2277 params
[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
2278 (char *)key32
, keylen
);
2279 params
[2] = OSSL_PARAM_construct_end();
2281 for (i
= 0; i
< loopargs_len
; i
++) {
2282 loopargs
[i
].mctx
= EVP_MAC_CTX_new(mac
);
2283 if (loopargs
[i
].mctx
== NULL
)
2286 if (!EVP_MAC_CTX_set_params(loopargs
[i
].mctx
, params
))
2290 for (testnum
= 0; testnum
< size_num
; testnum
++) {
2291 print_message(names
[D_EVP_CMAC
], c
[D_EVP_CMAC
][testnum
],
2292 lengths
[testnum
], seconds
.sym
);
2294 count
= run_benchmark(async_jobs
, CMAC_loop
, loopargs
);
2296 print_result(D_EVP_CMAC
, testnum
, count
, d
);
2300 for (i
= 0; i
< loopargs_len
; i
++)
2301 EVP_MAC_CTX_free(loopargs
[i
].mctx
);
2305 for (i
= 0; i
< loopargs_len
; i
++)
2306 if (RAND_bytes(loopargs
[i
].buf
, 36) <= 0)
2309 for (testnum
= 0; testnum
< RSA_NUM
; testnum
++) {
2310 EVP_PKEY
*rsa_key
= NULL
;
2313 if (!rsa_doit
[testnum
])
2316 if (primes
> RSA_DEFAULT_PRIME_NUM
) {
2317 /* we haven't set keys yet, generate multi-prime RSA keys */
2320 && BN_set_word(bn
, RSA_F4
)
2321 && init_gen_str(&genctx
, "RSA", NULL
, 0, NULL
, NULL
)
2322 && EVP_PKEY_CTX_set_rsa_keygen_bits(genctx
, rsa_keys
[testnum
].bits
) > 0
2323 && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(genctx
, bn
) > 0
2324 && EVP_PKEY_CTX_set_rsa_keygen_primes(genctx
, primes
) > 0
2325 && EVP_PKEY_keygen(genctx
, &rsa_key
);
2328 EVP_PKEY_CTX_free(genctx
);
2331 const unsigned char *p
= rsa_keys
[testnum
].data
;
2333 st
= (rsa_key
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, &p
,
2334 rsa_keys
[testnum
].length
)) != NULL
;
2337 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2338 loopargs
[i
].rsa_sign_ctx
[testnum
] = EVP_PKEY_CTX_new(rsa_key
, NULL
);
2339 if (loopargs
[i
].rsa_sign_ctx
[testnum
] == NULL
2340 || EVP_PKEY_sign_init(loopargs
[i
].rsa_sign_ctx
[testnum
]) <= 0
2341 || EVP_PKEY_sign(loopargs
[i
].rsa_sign_ctx
[testnum
],
2343 &loopargs
[i
].sigsize
,
2344 loopargs
[i
].buf
, 36) <= 0)
2349 "RSA sign setup failure. No RSA sign will be done.\n");
2350 ERR_print_errors(bio_err
);
2353 pkey_print_message("private", "rsa",
2354 rsa_c
[testnum
][0], rsa_keys
[testnum
].bits
,
2356 /* RSA_blinding_on(rsa_key[testnum],NULL); */
2358 count
= run_benchmark(async_jobs
, RSA_sign_loop
, loopargs
);
2361 mr
? "+R1:%ld:%d:%.2f\n"
2362 : "%ld %u bits private RSA's in %.2fs\n",
2363 count
, rsa_keys
[testnum
].bits
, d
);
2364 rsa_results
[testnum
][0] = (double)count
/ d
;
2368 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2369 loopargs
[i
].rsa_verify_ctx
[testnum
] = EVP_PKEY_CTX_new(rsa_key
,
2371 if (loopargs
[i
].rsa_verify_ctx
[testnum
] == NULL
2372 || EVP_PKEY_verify_init(loopargs
[i
].rsa_verify_ctx
[testnum
]) <= 0
2373 || EVP_PKEY_verify(loopargs
[i
].rsa_verify_ctx
[testnum
],
2375 loopargs
[i
].sigsize
,
2376 loopargs
[i
].buf
, 36) <= 0)
2381 "RSA verify setup failure. No RSA verify will be done.\n");
2382 ERR_print_errors(bio_err
);
2383 rsa_doit
[testnum
] = 0;
2385 pkey_print_message("public", "rsa",
2386 rsa_c
[testnum
][1], rsa_keys
[testnum
].bits
,
2389 count
= run_benchmark(async_jobs
, RSA_verify_loop
, loopargs
);
2392 mr
? "+R2:%ld:%d:%.2f\n"
2393 : "%ld %u bits public RSA's in %.2fs\n",
2394 count
, rsa_keys
[testnum
].bits
, d
);
2395 rsa_results
[testnum
][1] = (double)count
/ d
;
2398 if (op_count
<= 1) {
2399 /* if longer than 10s, don't do any more */
2400 stop_it(rsa_doit
, testnum
);
2402 EVP_PKEY_free(rsa_key
);
2405 for (testnum
= 0; testnum
< DSA_NUM
; testnum
++) {
2406 EVP_PKEY
*dsa_key
= NULL
;
2409 if (!dsa_doit
[testnum
])
2412 st
= (dsa_key
= get_dsa(dsa_bits
[testnum
])) != NULL
;
2414 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2415 loopargs
[i
].dsa_sign_ctx
[testnum
] = EVP_PKEY_CTX_new(dsa_key
,
2417 if (loopargs
[i
].dsa_sign_ctx
[testnum
] == NULL
2418 || EVP_PKEY_sign_init(loopargs
[i
].dsa_sign_ctx
[testnum
]) <= 0
2420 || EVP_PKEY_sign(loopargs
[i
].dsa_sign_ctx
[testnum
],
2422 &loopargs
[i
].sigsize
,
2423 loopargs
[i
].buf
, 20) <= 0)
2428 "DSA sign setup failure. No DSA sign will be done.\n");
2429 ERR_print_errors(bio_err
);
2432 pkey_print_message("sign", "dsa",
2433 dsa_c
[testnum
][0], dsa_bits
[testnum
],
2436 count
= run_benchmark(async_jobs
, DSA_sign_loop
, loopargs
);
2439 mr
? "+R3:%ld:%u:%.2f\n"
2440 : "%ld %u bits DSA signs in %.2fs\n",
2441 count
, dsa_bits
[testnum
], d
);
2442 dsa_results
[testnum
][0] = (double)count
/ d
;
2446 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2447 loopargs
[i
].dsa_verify_ctx
[testnum
] = EVP_PKEY_CTX_new(dsa_key
,
2449 if (loopargs
[i
].dsa_verify_ctx
[testnum
] == NULL
2450 || EVP_PKEY_verify_init(loopargs
[i
].dsa_verify_ctx
[testnum
]) <= 0
2451 || EVP_PKEY_verify(loopargs
[i
].dsa_verify_ctx
[testnum
],
2453 loopargs
[i
].sigsize
,
2454 loopargs
[i
].buf
, 36) <= 0)
2459 "DSA verify setup failure. No DSA verify will be done.\n");
2460 ERR_print_errors(bio_err
);
2461 dsa_doit
[testnum
] = 0;
2463 pkey_print_message("verify", "dsa",
2464 dsa_c
[testnum
][1], dsa_bits
[testnum
],
2467 count
= run_benchmark(async_jobs
, DSA_verify_loop
, loopargs
);
2470 mr
? "+R4:%ld:%u:%.2f\n"
2471 : "%ld %u bits DSA verify in %.2fs\n",
2472 count
, dsa_bits
[testnum
], d
);
2473 dsa_results
[testnum
][1] = (double)count
/ d
;
2476 if (op_count
<= 1) {
2477 /* if longer than 10s, don't do any more */
2478 stop_it(dsa_doit
, testnum
);
2480 EVP_PKEY_free(dsa_key
);
2483 for (testnum
= 0; testnum
< ECDSA_NUM
; testnum
++) {
2484 EVP_PKEY
*ecdsa_key
= NULL
;
2487 if (!ecdsa_doit
[testnum
])
2490 st
= (ecdsa_key
= get_ecdsa(&ec_curves
[testnum
])) != NULL
;
2492 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2493 loopargs
[i
].ecdsa_sign_ctx
[testnum
] = EVP_PKEY_CTX_new(ecdsa_key
,
2495 if (loopargs
[i
].ecdsa_sign_ctx
[testnum
] == NULL
2496 || EVP_PKEY_sign_init(loopargs
[i
].ecdsa_sign_ctx
[testnum
]) <= 0
2498 || EVP_PKEY_sign(loopargs
[i
].ecdsa_sign_ctx
[testnum
],
2500 &loopargs
[i
].sigsize
,
2501 loopargs
[i
].buf
, 20) <= 0)
2506 "ECDSA sign setup failure. No ECDSA sign will be done.\n");
2507 ERR_print_errors(bio_err
);
2510 pkey_print_message("sign", "ecdsa",
2511 ecdsa_c
[testnum
][0], ec_curves
[testnum
].bits
,
2514 count
= run_benchmark(async_jobs
, ECDSA_sign_loop
, loopargs
);
2517 mr
? "+R5:%ld:%u:%.2f\n"
2518 : "%ld %u bits ECDSA signs in %.2fs\n",
2519 count
, ec_curves
[testnum
].bits
, d
);
2520 ecdsa_results
[testnum
][0] = (double)count
/ d
;
2524 for (i
= 0; st
&& i
< loopargs_len
; i
++) {
2525 loopargs
[i
].ecdsa_verify_ctx
[testnum
] = EVP_PKEY_CTX_new(ecdsa_key
,
2527 if (loopargs
[i
].ecdsa_verify_ctx
[testnum
] == NULL
2528 || EVP_PKEY_verify_init(loopargs
[i
].ecdsa_verify_ctx
[testnum
]) <= 0
2529 || EVP_PKEY_verify(loopargs
[i
].ecdsa_verify_ctx
[testnum
],
2531 loopargs
[i
].sigsize
,
2532 loopargs
[i
].buf
, 20) <= 0)
2537 "ECDSA verify setup failure. No ECDSA verify will be done.\n");
2538 ERR_print_errors(bio_err
);
2539 ecdsa_doit
[testnum
] = 0;
2541 pkey_print_message("verify", "ecdsa",
2542 ecdsa_c
[testnum
][1], ec_curves
[testnum
].bits
,
2545 count
= run_benchmark(async_jobs
, ECDSA_verify_loop
, loopargs
);
2548 mr
? "+R6:%ld:%u:%.2f\n"
2549 : "%ld %u bits ECDSA verify in %.2fs\n",
2550 count
, ec_curves
[testnum
].bits
, d
);
2551 ecdsa_results
[testnum
][1] = (double)count
/ d
;
2554 if (op_count
<= 1) {
2555 /* if longer than 10s, don't do any more */
2556 stop_it(ecdsa_doit
, testnum
);
2560 for (testnum
= 0; testnum
< EC_NUM
; testnum
++) {
2561 int ecdh_checks
= 1;
2563 if (!ecdh_doit
[testnum
])
2566 for (i
= 0; i
< loopargs_len
; i
++) {
2567 EVP_PKEY_CTX
*test_ctx
= NULL
;
2568 EVP_PKEY_CTX
*ctx
= NULL
;
2569 EVP_PKEY
*key_A
= NULL
;
2570 EVP_PKEY
*key_B
= NULL
;
2574 if ((key_A
= get_ecdsa(&ec_curves
[testnum
])) == NULL
/* generate secret key A */
2575 || (key_B
= get_ecdsa(&ec_curves
[testnum
])) == NULL
/* generate secret key B */
2576 || (ctx
= EVP_PKEY_CTX_new(key_A
, NULL
)) == NULL
/* derivation ctx from skeyA */
2577 || EVP_PKEY_derive_init(ctx
) <= 0 /* init derivation ctx */
2578 || EVP_PKEY_derive_set_peer(ctx
, key_B
) <= 0 /* set peer pubkey in ctx */
2579 || EVP_PKEY_derive(ctx
, NULL
, &outlen
) <= 0 /* determine max length */
2580 || outlen
== 0 /* ensure outlen is a valid size */
2581 || outlen
> MAX_ECDH_SIZE
/* avoid buffer overflow */) {
2583 BIO_printf(bio_err
, "ECDH key generation failure.\n");
2584 ERR_print_errors(bio_err
);
2590 * Here we perform a test run, comparing the output of a*B and b*A;
2591 * we try this here and assume that further EVP_PKEY_derive calls
2592 * never fail, so we can skip checks in the actually benchmarked
2593 * code, for maximum performance.
2595 if ((test_ctx
= EVP_PKEY_CTX_new(key_B
, NULL
)) == NULL
/* test ctx from skeyB */
2596 || !EVP_PKEY_derive_init(test_ctx
) /* init derivation test_ctx */
2597 || !EVP_PKEY_derive_set_peer(test_ctx
, key_A
) /* set peer pubkey in test_ctx */
2598 || !EVP_PKEY_derive(test_ctx
, NULL
, &test_outlen
) /* determine max length */
2599 || !EVP_PKEY_derive(ctx
, loopargs
[i
].secret_a
, &outlen
) /* compute a*B */
2600 || !EVP_PKEY_derive(test_ctx
, loopargs
[i
].secret_b
, &test_outlen
) /* compute b*A */
2601 || test_outlen
!= outlen
/* compare output length */) {
2603 BIO_printf(bio_err
, "ECDH computation failure.\n");
2604 ERR_print_errors(bio_err
);
2609 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
2610 if (CRYPTO_memcmp(loopargs
[i
].secret_a
,
2611 loopargs
[i
].secret_b
, outlen
)) {
2613 BIO_printf(bio_err
, "ECDH computations don't match.\n");
2614 ERR_print_errors(bio_err
);
2619 loopargs
[i
].ecdh_ctx
[testnum
] = ctx
;
2620 loopargs
[i
].outlen
[testnum
] = outlen
;
2622 EVP_PKEY_free(key_A
);
2623 EVP_PKEY_free(key_B
);
2624 EVP_PKEY_CTX_free(test_ctx
);
2627 if (ecdh_checks
!= 0) {
2628 pkey_print_message("", "ecdh",
2630 ec_curves
[testnum
].bits
, seconds
.ecdh
);
2633 run_benchmark(async_jobs
, ECDH_EVP_derive_key_loop
, loopargs
);
2636 mr
? "+R7:%ld:%d:%.2f\n" :
2637 "%ld %u-bits ECDH ops in %.2fs\n", count
,
2638 ec_curves
[testnum
].bits
, d
);
2639 ecdh_results
[testnum
][0] = (double)count
/ d
;
2643 if (op_count
<= 1) {
2644 /* if longer than 10s, don't do any more */
2645 stop_it(ecdh_doit
, testnum
);
2649 for (testnum
= 0; testnum
< EdDSA_NUM
; testnum
++) {
2651 EVP_PKEY
*ed_pkey
= NULL
;
2652 EVP_PKEY_CTX
*ed_pctx
= NULL
;
2654 if (!eddsa_doit
[testnum
])
2655 continue; /* Ignore Curve */
2656 for (i
= 0; i
< loopargs_len
; i
++) {
2657 loopargs
[i
].eddsa_ctx
[testnum
] = EVP_MD_CTX_new();
2658 if (loopargs
[i
].eddsa_ctx
[testnum
] == NULL
) {
2662 loopargs
[i
].eddsa_ctx2
[testnum
] = EVP_MD_CTX_new();
2663 if (loopargs
[i
].eddsa_ctx2
[testnum
] == NULL
) {
2668 if ((ed_pctx
= EVP_PKEY_CTX_new_id(ed_curves
[testnum
].nid
,
2670 || EVP_PKEY_keygen_init(ed_pctx
) <= 0
2671 || EVP_PKEY_keygen(ed_pctx
, &ed_pkey
) <= 0) {
2673 EVP_PKEY_CTX_free(ed_pctx
);
2676 EVP_PKEY_CTX_free(ed_pctx
);
2678 if (!EVP_DigestSignInit(loopargs
[i
].eddsa_ctx
[testnum
], NULL
, NULL
,
2681 EVP_PKEY_free(ed_pkey
);
2684 if (!EVP_DigestVerifyInit(loopargs
[i
].eddsa_ctx2
[testnum
], NULL
,
2685 NULL
, NULL
, ed_pkey
)) {
2687 EVP_PKEY_free(ed_pkey
);
2691 EVP_PKEY_free(ed_pkey
);
2695 BIO_printf(bio_err
, "EdDSA failure.\n");
2696 ERR_print_errors(bio_err
);
2699 for (i
= 0; i
< loopargs_len
; i
++) {
2700 /* Perform EdDSA signature test */
2701 loopargs
[i
].sigsize
= ed_curves
[testnum
].sigsize
;
2702 st
= EVP_DigestSign(loopargs
[i
].eddsa_ctx
[testnum
],
2703 loopargs
[i
].buf2
, &loopargs
[i
].sigsize
,
2704 loopargs
[i
].buf
, 20);
2710 "EdDSA sign failure. No EdDSA sign will be done.\n");
2711 ERR_print_errors(bio_err
);
2714 pkey_print_message("sign", ed_curves
[testnum
].name
,
2715 eddsa_c
[testnum
][0],
2716 ed_curves
[testnum
].bits
, seconds
.eddsa
);
2718 count
= run_benchmark(async_jobs
, EdDSA_sign_loop
, loopargs
);
2722 mr
? "+R8:%ld:%u:%s:%.2f\n" :
2723 "%ld %u bits %s signs in %.2fs \n",
2724 count
, ed_curves
[testnum
].bits
,
2725 ed_curves
[testnum
].name
, d
);
2726 eddsa_results
[testnum
][0] = (double)count
/ d
;
2729 /* Perform EdDSA verification test */
2730 for (i
= 0; i
< loopargs_len
; i
++) {
2731 st
= EVP_DigestVerify(loopargs
[i
].eddsa_ctx2
[testnum
],
2732 loopargs
[i
].buf2
, loopargs
[i
].sigsize
,
2733 loopargs
[i
].buf
, 20);
2739 "EdDSA verify failure. No EdDSA verify will be done.\n");
2740 ERR_print_errors(bio_err
);
2741 eddsa_doit
[testnum
] = 0;
2743 pkey_print_message("verify", ed_curves
[testnum
].name
,
2744 eddsa_c
[testnum
][1],
2745 ed_curves
[testnum
].bits
, seconds
.eddsa
);
2747 count
= run_benchmark(async_jobs
, EdDSA_verify_loop
, loopargs
);
2750 mr
? "+R9:%ld:%u:%s:%.2f\n"
2751 : "%ld %u bits %s verify in %.2fs\n",
2752 count
, ed_curves
[testnum
].bits
,
2753 ed_curves
[testnum
].name
, d
);
2754 eddsa_results
[testnum
][1] = (double)count
/ d
;
2757 if (op_count
<= 1) {
2758 /* if longer than 10s, don't do any more */
2759 stop_it(eddsa_doit
, testnum
);
2764 #ifndef OPENSSL_NO_SM2
2765 for (testnum
= 0; testnum
< SM2_NUM
; testnum
++) {
2767 EVP_PKEY
*sm2_pkey
= NULL
;
2769 if (!sm2_doit
[testnum
])
2770 continue; /* Ignore Curve */
2771 /* Init signing and verification */
2772 for (i
= 0; i
< loopargs_len
; i
++) {
2773 EVP_PKEY_CTX
*sm2_pctx
= NULL
;
2774 EVP_PKEY_CTX
*sm2_vfy_pctx
= NULL
;
2775 EVP_PKEY_CTX
*pctx
= NULL
;
2778 loopargs
[i
].sm2_ctx
[testnum
] = EVP_MD_CTX_new();
2779 loopargs
[i
].sm2_vfy_ctx
[testnum
] = EVP_MD_CTX_new();
2780 if (loopargs
[i
].sm2_ctx
[testnum
] == NULL
2781 || loopargs
[i
].sm2_vfy_ctx
[testnum
] == NULL
)
2786 st
= !((pctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_SM2
, NULL
)) == NULL
2787 || EVP_PKEY_keygen_init(pctx
) <= 0
2788 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx
,
2789 sm2_curves
[testnum
].nid
) <= 0
2790 || EVP_PKEY_keygen(pctx
, &sm2_pkey
) <= 0);
2791 EVP_PKEY_CTX_free(pctx
);
2795 st
= 0; /* set back to zero */
2796 /* attach it sooner to rely on main final cleanup */
2797 loopargs
[i
].sm2_pkey
[testnum
] = sm2_pkey
;
2798 loopargs
[i
].sigsize
= EVP_PKEY_size(sm2_pkey
);
2800 sm2_pctx
= EVP_PKEY_CTX_new(sm2_pkey
, NULL
);
2801 sm2_vfy_pctx
= EVP_PKEY_CTX_new(sm2_pkey
, NULL
);
2802 if (sm2_pctx
== NULL
|| sm2_vfy_pctx
== NULL
) {
2803 EVP_PKEY_CTX_free(sm2_vfy_pctx
);
2807 /* attach them directly to respective ctx */
2808 EVP_MD_CTX_set_pkey_ctx(loopargs
[i
].sm2_ctx
[testnum
], sm2_pctx
);
2809 EVP_MD_CTX_set_pkey_ctx(loopargs
[i
].sm2_vfy_ctx
[testnum
], sm2_vfy_pctx
);
2812 * No need to allow user to set an explicit ID here, just use
2813 * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
2815 if (EVP_PKEY_CTX_set1_id(sm2_pctx
, SM2_ID
, SM2_ID_LEN
) != 1
2816 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx
, SM2_ID
, SM2_ID_LEN
) != 1)
2819 if (!EVP_DigestSignInit(loopargs
[i
].sm2_ctx
[testnum
], NULL
,
2820 EVP_sm3(), NULL
, sm2_pkey
))
2822 if (!EVP_DigestVerifyInit(loopargs
[i
].sm2_vfy_ctx
[testnum
], NULL
,
2823 EVP_sm3(), NULL
, sm2_pkey
))
2825 st
= 1; /* mark loop as succeeded */
2828 BIO_printf(bio_err
, "SM2 init failure.\n");
2829 ERR_print_errors(bio_err
);
2832 for (i
= 0; i
< loopargs_len
; i
++) {
2833 /* Perform SM2 signature test */
2834 st
= EVP_DigestSign(loopargs
[i
].sm2_ctx
[testnum
],
2835 loopargs
[i
].buf2
, &loopargs
[i
].sigsize
,
2836 loopargs
[i
].buf
, 20);
2842 "SM2 sign failure. No SM2 sign will be done.\n");
2843 ERR_print_errors(bio_err
);
2846 pkey_print_message("sign", sm2_curves
[testnum
].name
,
2848 sm2_curves
[testnum
].bits
, seconds
.sm2
);
2850 count
= run_benchmark(async_jobs
, SM2_sign_loop
, loopargs
);
2854 mr
? "+R10:%ld:%u:%s:%.2f\n" :
2855 "%ld %u bits %s signs in %.2fs \n",
2856 count
, sm2_curves
[testnum
].bits
,
2857 sm2_curves
[testnum
].name
, d
);
2858 sm2_results
[testnum
][0] = (double)count
/ d
;
2862 /* Perform SM2 verification test */
2863 for (i
= 0; i
< loopargs_len
; i
++) {
2864 st
= EVP_DigestVerify(loopargs
[i
].sm2_vfy_ctx
[testnum
],
2865 loopargs
[i
].buf2
, loopargs
[i
].sigsize
,
2866 loopargs
[i
].buf
, 20);
2872 "SM2 verify failure. No SM2 verify will be done.\n");
2873 ERR_print_errors(bio_err
);
2874 sm2_doit
[testnum
] = 0;
2876 pkey_print_message("verify", sm2_curves
[testnum
].name
,
2878 sm2_curves
[testnum
].bits
, seconds
.sm2
);
2880 count
= run_benchmark(async_jobs
, SM2_verify_loop
, loopargs
);
2883 mr
? "+R11:%ld:%u:%s:%.2f\n"
2884 : "%ld %u bits %s verify in %.2fs\n",
2885 count
, sm2_curves
[testnum
].bits
,
2886 sm2_curves
[testnum
].name
, d
);
2887 sm2_results
[testnum
][1] = (double)count
/ d
;
2890 if (op_count
<= 1) {
2891 /* if longer than 10s, don't do any more */
2892 for (testnum
++; testnum
< SM2_NUM
; testnum
++)
2893 sm2_doit
[testnum
] = 0;
2897 #endif /* OPENSSL_NO_SM2 */
2899 #ifndef OPENSSL_NO_DH
2900 for (testnum
= 0; testnum
< FFDH_NUM
; testnum
++) {
2901 int ffdh_checks
= 1;
2903 if (!ffdh_doit
[testnum
])
2906 for (i
= 0; i
< loopargs_len
; i
++) {
2907 EVP_PKEY
*pkey_A
= NULL
;
2908 EVP_PKEY
*pkey_B
= NULL
;
2909 EVP_PKEY_CTX
*ffdh_ctx
= NULL
;
2910 EVP_PKEY_CTX
*test_ctx
= NULL
;
2914 /* Ensure that the error queue is empty */
2915 if (ERR_peek_error()) {
2917 "WARNING: the error queue contains previous unhandled errors.\n");
2918 ERR_print_errors(bio_err
);
2921 pkey_A
= EVP_PKEY_new();
2923 BIO_printf(bio_err
, "Error while initialising EVP_PKEY (out of memory?).\n");
2924 ERR_print_errors(bio_err
);
2929 pkey_B
= EVP_PKEY_new();
2931 BIO_printf(bio_err
, "Error while initialising EVP_PKEY (out of memory?).\n");
2932 ERR_print_errors(bio_err
);
2938 ffdh_ctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_DH
, NULL
);
2940 BIO_printf(bio_err
, "Error while allocating EVP_PKEY_CTX.\n");
2941 ERR_print_errors(bio_err
);
2947 if (EVP_PKEY_keygen_init(ffdh_ctx
) <= 0) {
2948 BIO_printf(bio_err
, "Error while initialising EVP_PKEY_CTX.\n");
2949 ERR_print_errors(bio_err
);
2954 if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx
, ffdh_params
[testnum
].nid
) <= 0) {
2955 BIO_printf(bio_err
, "Error setting DH key size for keygen.\n");
2956 ERR_print_errors(bio_err
);
2962 if (EVP_PKEY_keygen(ffdh_ctx
, &pkey_A
) <= 0 ||
2963 EVP_PKEY_keygen(ffdh_ctx
, &pkey_B
) <= 0) {
2964 BIO_printf(bio_err
, "FFDH key generation failure.\n");
2965 ERR_print_errors(bio_err
);
2971 EVP_PKEY_CTX_free(ffdh_ctx
);
2974 * check if the derivation works correctly both ways so that
2975 * we know if future derive calls will fail, and we can skip
2976 * error checking in benchmarked code
2978 ffdh_ctx
= EVP_PKEY_CTX_new(pkey_A
, NULL
);
2979 if (ffdh_ctx
== NULL
) {
2980 BIO_printf(bio_err
, "Error while allocating EVP_PKEY_CTX.\n");
2981 ERR_print_errors(bio_err
);
2986 if (EVP_PKEY_derive_init(ffdh_ctx
) <= 0) {
2987 BIO_printf(bio_err
, "FFDH derivation context init failure.\n");
2988 ERR_print_errors(bio_err
);
2993 if (EVP_PKEY_derive_set_peer(ffdh_ctx
, pkey_B
) <= 0) {
2994 BIO_printf(bio_err
, "Assigning peer key for derivation failed.\n");
2995 ERR_print_errors(bio_err
);
3000 if (EVP_PKEY_derive(ffdh_ctx
, NULL
, &secret_size
) <= 0) {
3001 BIO_printf(bio_err
, "Checking size of shared secret failed.\n");
3002 ERR_print_errors(bio_err
);
3007 if (secret_size
> MAX_FFDH_SIZE
) {
3008 BIO_printf(bio_err
, "Assertion failure: shared secret too large.\n");
3013 if (EVP_PKEY_derive(ffdh_ctx
,
3014 loopargs
[i
].secret_ff_a
,
3015 &secret_size
) <= 0) {
3016 BIO_printf(bio_err
, "Shared secret derive failure.\n");
3017 ERR_print_errors(bio_err
);
3022 /* Now check from side B */
3023 test_ctx
= EVP_PKEY_CTX_new(pkey_B
, NULL
);
3025 BIO_printf(bio_err
, "Error while allocating EVP_PKEY_CTX.\n");
3026 ERR_print_errors(bio_err
);
3031 if (!EVP_PKEY_derive_init(test_ctx
) ||
3032 !EVP_PKEY_derive_set_peer(test_ctx
, pkey_A
) ||
3033 !EVP_PKEY_derive(test_ctx
, NULL
, &test_out
) ||
3034 !EVP_PKEY_derive(test_ctx
, loopargs
[i
].secret_ff_b
, &test_out
) ||
3035 test_out
!= secret_size
) {
3036 BIO_printf(bio_err
, "FFDH computation failure.\n");
3042 /* compare the computed secrets */
3043 if (CRYPTO_memcmp(loopargs
[i
].secret_ff_a
,
3044 loopargs
[i
].secret_ff_b
, secret_size
)) {
3045 BIO_printf(bio_err
, "FFDH computations don't match.\n");
3046 ERR_print_errors(bio_err
);
3052 loopargs
[i
].ffdh_ctx
[testnum
] = ffdh_ctx
;
3054 EVP_PKEY_free(pkey_A
);
3056 EVP_PKEY_free(pkey_B
);
3058 EVP_PKEY_CTX_free(test_ctx
);
3061 if (ffdh_checks
!= 0) {
3062 pkey_print_message("", "ffdh", ffdh_c
[testnum
][0],
3063 ffdh_params
[testnum
].bits
, seconds
.ffdh
);
3066 run_benchmark(async_jobs
, FFDH_derive_key_loop
, loopargs
);
3069 mr
? "+R12:%ld:%d:%.2f\n" :
3070 "%ld %u-bits FFDH ops in %.2fs\n", count
,
3071 ffdh_params
[testnum
].bits
, d
);
3072 ffdh_results
[testnum
][0] = (double)count
/ d
;
3075 if (op_count
<= 1) {
3076 /* if longer than 10s, don't do any more */
3077 stop_it(ffdh_doit
, testnum
);
3080 #endif /* OPENSSL_NO_DH */
3085 printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING
));
3086 printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON
));
3088 printf("%s ", BN_options());
3089 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS
));
3090 printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO
));
3097 printf("The 'numbers' are in 1000s of bytes per second processed.\n");
3100 for (testnum
= 0; testnum
< size_num
; testnum
++)
3101 printf(mr
? ":%d" : "%7d bytes", lengths
[testnum
]);
3105 for (k
= 0; k
< ALGOR_NUM
; k
++) {
3109 printf("+F:%u:%s", k
, names
[k
]);
3111 printf("%-13s", names
[k
]);
3112 for (testnum
= 0; testnum
< size_num
; testnum
++) {
3113 if (results
[k
][testnum
] > 10000 && !mr
)
3114 printf(" %11.2fk", results
[k
][testnum
] / 1e3
);
3116 printf(mr
? ":%.2f" : " %11.2f ", results
[k
][testnum
]);
3121 for (k
= 0; k
< RSA_NUM
; k
++) {
3124 if (testnum
&& !mr
) {
3125 printf("%18ssign verify sign/s verify/s\n", " ");
3129 printf("+F2:%u:%u:%f:%f\n",
3130 k
, rsa_keys
[k
].bits
, rsa_results
[k
][0], rsa_results
[k
][1]);
3132 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3133 rsa_keys
[k
].bits
, 1.0 / rsa_results
[k
][0], 1.0 / rsa_results
[k
][1],
3134 rsa_results
[k
][0], rsa_results
[k
][1]);
3137 for (k
= 0; k
< DSA_NUM
; k
++) {
3140 if (testnum
&& !mr
) {
3141 printf("%18ssign verify sign/s verify/s\n", " ");
3145 printf("+F3:%u:%u:%f:%f\n",
3146 k
, dsa_bits
[k
], dsa_results
[k
][0], dsa_results
[k
][1]);
3148 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3149 dsa_bits
[k
], 1.0 / dsa_results
[k
][0], 1.0 / dsa_results
[k
][1],
3150 dsa_results
[k
][0], dsa_results
[k
][1]);
3153 for (k
= 0; k
< OSSL_NELEM(ecdsa_doit
); k
++) {
3156 if (testnum
&& !mr
) {
3157 printf("%30ssign verify sign/s verify/s\n", " ");
3162 printf("+F4:%u:%u:%f:%f\n",
3163 k
, ec_curves
[k
].bits
,
3164 ecdsa_results
[k
][0], ecdsa_results
[k
][1]);
3166 printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3167 ec_curves
[k
].bits
, ec_curves
[k
].name
,
3168 1.0 / ecdsa_results
[k
][0], 1.0 / ecdsa_results
[k
][1],
3169 ecdsa_results
[k
][0], ecdsa_results
[k
][1]);
3173 for (k
= 0; k
< EC_NUM
; k
++) {
3176 if (testnum
&& !mr
) {
3177 printf("%30sop op/s\n", " ");
3181 printf("+F5:%u:%u:%f:%f\n",
3182 k
, ec_curves
[k
].bits
,
3183 ecdh_results
[k
][0], 1.0 / ecdh_results
[k
][0]);
3186 printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
3187 ec_curves
[k
].bits
, ec_curves
[k
].name
,
3188 1.0 / ecdh_results
[k
][0], ecdh_results
[k
][0]);
3192 for (k
= 0; k
< OSSL_NELEM(eddsa_doit
); k
++) {
3195 if (testnum
&& !mr
) {
3196 printf("%30ssign verify sign/s verify/s\n", " ");
3201 printf("+F6:%u:%u:%s:%f:%f\n",
3202 k
, ed_curves
[k
].bits
, ed_curves
[k
].name
,
3203 eddsa_results
[k
][0], eddsa_results
[k
][1]);
3205 printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3206 ed_curves
[k
].bits
, ed_curves
[k
].name
,
3207 1.0 / eddsa_results
[k
][0], 1.0 / eddsa_results
[k
][1],
3208 eddsa_results
[k
][0], eddsa_results
[k
][1]);
3211 #ifndef OPENSSL_NO_SM2
3213 for (k
= 0; k
< OSSL_NELEM(sm2_doit
); k
++) {
3216 if (testnum
&& !mr
) {
3217 printf("%30ssign verify sign/s verify/s\n", " ");
3222 printf("+F7:%u:%u:%s:%f:%f\n",
3223 k
, sm2_curves
[k
].bits
, sm2_curves
[k
].name
,
3224 sm2_results
[k
][0], sm2_results
[k
][1]);
3226 printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
3227 sm2_curves
[k
].bits
, sm2_curves
[k
].name
,
3228 1.0 / sm2_results
[k
][0], 1.0 / sm2_results
[k
][1],
3229 sm2_results
[k
][0], sm2_results
[k
][1]);
3232 #ifndef OPENSSL_NO_DH
3234 for (k
= 0; k
< FFDH_NUM
; k
++) {
3237 if (testnum
&& !mr
) {
3238 printf("%23sop op/s\n", " ");
3242 printf("+F8:%u:%u:%f:%f\n",
3243 k
, ffdh_params
[k
].bits
,
3244 ffdh_results
[k
][0], 1.0 / ffdh_results
[k
][0]);
3247 printf("%4u bits ffdh %8.4fs %8.1f\n",
3248 ffdh_params
[k
].bits
,
3249 1.0 / ffdh_results
[k
][0], ffdh_results
[k
][0]);
3251 #endif /* OPENSSL_NO_DH */
3256 ERR_print_errors(bio_err
);
3257 for (i
= 0; i
< loopargs_len
; i
++) {
3258 OPENSSL_free(loopargs
[i
].buf_malloc
);
3259 OPENSSL_free(loopargs
[i
].buf2_malloc
);
3262 EVP_PKEY_CTX_free(genctx
);
3263 for (k
= 0; k
< RSA_NUM
; k
++) {
3264 EVP_PKEY_CTX_free(loopargs
[i
].rsa_sign_ctx
[k
]);
3265 EVP_PKEY_CTX_free(loopargs
[i
].rsa_verify_ctx
[k
]);
3267 #ifndef OPENSSL_NO_DH
3268 OPENSSL_free(loopargs
[i
].secret_ff_a
);
3269 OPENSSL_free(loopargs
[i
].secret_ff_b
);
3270 for (k
= 0; k
< FFDH_NUM
; k
++)
3271 EVP_PKEY_CTX_free(loopargs
[i
].ffdh_ctx
[k
]);
3273 for (k
= 0; k
< DSA_NUM
; k
++) {
3274 EVP_PKEY_CTX_free(loopargs
[i
].dsa_sign_ctx
[k
]);
3275 EVP_PKEY_CTX_free(loopargs
[i
].dsa_verify_ctx
[k
]);
3277 for (k
= 0; k
< ECDSA_NUM
; k
++) {
3278 EVP_PKEY_CTX_free(loopargs
[i
].ecdsa_sign_ctx
[k
]);
3279 EVP_PKEY_CTX_free(loopargs
[i
].ecdsa_verify_ctx
[k
]);
3281 for (k
= 0; k
< EC_NUM
; k
++)
3282 EVP_PKEY_CTX_free(loopargs
[i
].ecdh_ctx
[k
]);
3283 for (k
= 0; k
< EdDSA_NUM
; k
++) {
3284 EVP_MD_CTX_free(loopargs
[i
].eddsa_ctx
[k
]);
3285 EVP_MD_CTX_free(loopargs
[i
].eddsa_ctx2
[k
]);
3287 #ifndef OPENSSL_NO_SM2
3288 for (k
= 0; k
< SM2_NUM
; k
++) {
3289 EVP_PKEY_CTX
*pctx
= NULL
;
3291 /* free signing ctx */
3292 if (loopargs
[i
].sm2_ctx
[k
] != NULL
3293 && (pctx
= EVP_MD_CTX_pkey_ctx(loopargs
[i
].sm2_ctx
[k
])) != NULL
)
3294 EVP_PKEY_CTX_free(pctx
);
3295 EVP_MD_CTX_free(loopargs
[i
].sm2_ctx
[k
]);
3296 /* free verification ctx */
3297 if (loopargs
[i
].sm2_vfy_ctx
[k
] != NULL
3298 && (pctx
= EVP_MD_CTX_pkey_ctx(loopargs
[i
].sm2_vfy_ctx
[k
])) != NULL
)
3299 EVP_PKEY_CTX_free(pctx
);
3300 EVP_MD_CTX_free(loopargs
[i
].sm2_vfy_ctx
[k
]);
3302 EVP_PKEY_free(loopargs
[i
].sm2_pkey
[k
]);
3305 OPENSSL_free(loopargs
[i
].secret_a
);
3306 OPENSSL_free(loopargs
[i
].secret_b
);
3308 OPENSSL_free(evp_hmac_name
);
3309 OPENSSL_free(evp_cmac_name
);
3311 if (async_jobs
> 0) {
3312 for (i
= 0; i
< loopargs_len
; i
++)
3313 ASYNC_WAIT_CTX_free(loopargs
[i
].wait_ctx
);
3317 ASYNC_cleanup_thread();
3319 OPENSSL_free(loopargs
);
3321 EVP_CIPHER_free(evp_cipher
);
3325 static void print_message(const char *s
, long num
, int length
, int tm
)
3328 mr
? "+DT:%s:%d:%d\n"
3329 : "Doing %s for %ds on %d size blocks: ", s
, tm
, length
);
3330 (void)BIO_flush(bio_err
);
3335 static void pkey_print_message(const char *str
, const char *str2
, long num
,
3336 unsigned int bits
, int tm
)
3339 mr
? "+DTP:%d:%s:%s:%d\n"
3340 : "Doing %u bits %s %s's for %ds: ", bits
, str
, str2
, tm
);
3341 (void)BIO_flush(bio_err
);
3346 static void print_result(int alg
, int run_no
, int count
, double time_used
)
3349 BIO_printf(bio_err
, "%s error!\n", names
[alg
]);
3350 ERR_print_errors(bio_err
);
3354 mr
? "+R:%d:%s:%f\n"
3355 : "%d %s's in %.2fs\n", count
, names
[alg
], time_used
);
3356 results
[alg
][run_no
] = ((double)count
) / time_used
* lengths
[run_no
];
3360 static char *sstrsep(char **string
, const char *delim
)
3363 char *token
= *string
;
3368 memset(isdelim
, 0, sizeof(isdelim
));
3372 isdelim
[(unsigned char)(*delim
)] = 1;
3376 while (!isdelim
[(unsigned char)(**string
)])
3387 static int do_multi(int multi
, int size_num
)
3392 static char sep
[] = ":";
3394 fds
= app_malloc(sizeof(*fds
) * multi
, "fd buffer for do_multi");
3395 for (n
= 0; n
< multi
; ++n
) {
3396 if (pipe(fd
) == -1) {
3397 BIO_printf(bio_err
, "pipe failure\n");
3401 (void)BIO_flush(bio_err
);
3408 if (dup(fd
[1]) == -1) {
3409 BIO_printf(bio_err
, "dup failed\n");
3418 printf("Forked child %d\n", n
);
3421 /* for now, assume the pipe is long enough to take all the output */
3422 for (n
= 0; n
< multi
; ++n
) {
3427 f
= fdopen(fds
[n
], "r");
3428 while (fgets(buf
, sizeof(buf
), f
)) {
3429 p
= strchr(buf
, '\n');
3432 if (buf
[0] != '+') {
3434 "Don't understand line '%s' from child %d\n", buf
,
3438 printf("Got: %s from %d\n", buf
, n
);
3439 if (strncmp(buf
, "+F:", 3) == 0) {
3444 alg
= atoi(sstrsep(&p
, sep
));
3446 for (j
= 0; j
< size_num
; ++j
)
3447 results
[alg
][j
] += atof(sstrsep(&p
, sep
));
3448 } else if (strncmp(buf
, "+F2:", 4) == 0) {
3453 k
= atoi(sstrsep(&p
, sep
));
3456 d
= atof(sstrsep(&p
, sep
));
3457 rsa_results
[k
][0] += d
;
3459 d
= atof(sstrsep(&p
, sep
));
3460 rsa_results
[k
][1] += d
;
3461 } else if (strncmp(buf
, "+F3:", 4) == 0) {
3466 k
= atoi(sstrsep(&p
, sep
));
3469 d
= atof(sstrsep(&p
, sep
));
3470 dsa_results
[k
][0] += d
;
3472 d
= atof(sstrsep(&p
, sep
));
3473 dsa_results
[k
][1] += d
;
3474 } else if (strncmp(buf
, "+F4:", 4) == 0) {
3479 k
= atoi(sstrsep(&p
, sep
));
3482 d
= atof(sstrsep(&p
, sep
));
3483 ecdsa_results
[k
][0] += d
;
3485 d
= atof(sstrsep(&p
, sep
));
3486 ecdsa_results
[k
][1] += d
;
3487 } else if (strncmp(buf
, "+F5:", 4) == 0) {
3492 k
= atoi(sstrsep(&p
, sep
));
3495 d
= atof(sstrsep(&p
, sep
));
3496 ecdh_results
[k
][0] += d
;
3497 } else if (strncmp(buf
, "+F6:", 4) == 0) {
3502 k
= atoi(sstrsep(&p
, sep
));
3506 d
= atof(sstrsep(&p
, sep
));
3507 eddsa_results
[k
][0] += d
;
3509 d
= atof(sstrsep(&p
, sep
));
3510 eddsa_results
[k
][1] += d
;
3511 # ifndef OPENSSL_NO_SM2
3512 } else if (strncmp(buf
, "+F7:", 4) == 0) {
3517 k
= atoi(sstrsep(&p
, sep
));
3521 d
= atof(sstrsep(&p
, sep
));
3522 sm2_results
[k
][0] += d
;
3524 d
= atof(sstrsep(&p
, sep
));
3525 sm2_results
[k
][1] += d
;
3526 # endif /* OPENSSL_NO_SM2 */
3527 # ifndef OPENSSL_NO_DH
3528 } else if (strncmp(buf
, "+F8:", 4) == 0) {
3533 k
= atoi(sstrsep(&p
, sep
));
3536 d
= atof(sstrsep(&p
, sep
));
3537 ffdh_results
[k
][0] += d
;
3538 # endif /* OPENSSL_NO_DH */
3539 } else if (strncmp(buf
, "+H:", 3) == 0) {
3542 BIO_printf(bio_err
, "Unknown type '%s' from child %d\n", buf
,
3554 static void multiblock_speed(const EVP_CIPHER
*evp_cipher
, int lengths_single
,
3555 const openssl_speed_sec_t
*seconds
)
3557 static const int mblengths_list
[] =
3558 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3559 const int *mblengths
= mblengths_list
;
3560 int j
, count
, keylen
, num
= OSSL_NELEM(mblengths_list
);
3561 const char *alg_name
;
3562 unsigned char *inp
= NULL
, *out
= NULL
, *key
, no_key
[32], no_iv
[16];
3563 EVP_CIPHER_CTX
*ctx
= NULL
;
3566 if (lengths_single
) {
3567 mblengths
= &lengths_single
;
3571 inp
= app_malloc(mblengths
[num
- 1], "multiblock input buffer");
3572 out
= app_malloc(mblengths
[num
- 1] + 1024, "multiblock output buffer");
3573 if ((ctx
= EVP_CIPHER_CTX_new()) == NULL
)
3574 app_bail_out("failed to allocate cipher context\n");
3575 if (!EVP_EncryptInit_ex(ctx
, evp_cipher
, NULL
, NULL
, no_iv
))
3576 app_bail_out("failed to initialise cipher context\n");
3578 if ((keylen
= EVP_CIPHER_CTX_key_length(ctx
)) < 0) {
3579 BIO_printf(bio_err
, "Impossible negative key length: %d\n", keylen
);
3582 key
= app_malloc(keylen
, "evp_cipher key");
3583 if (!EVP_CIPHER_CTX_rand_key(ctx
, key
))
3584 app_bail_out("failed to generate random cipher key\n");
3585 if (!EVP_EncryptInit_ex(ctx
, NULL
, NULL
, key
, NULL
))
3586 app_bail_out("failed to set cipher key\n");
3587 OPENSSL_clear_free(key
, keylen
);
3589 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_MAC_KEY
,
3590 sizeof(no_key
), no_key
))
3591 app_bail_out("failed to set AEAD key\n");
3592 if ((alg_name
= EVP_CIPHER_name(evp_cipher
)) == NULL
)
3593 app_bail_out("failed to get cipher name\n");
3595 for (j
= 0; j
< num
; j
++) {
3596 print_message(alg_name
, 0, mblengths
[j
], seconds
->sym
);
3598 for (count
= 0; run
&& count
< 0x7fffffff; count
++) {
3599 unsigned char aad
[EVP_AEAD_TLS1_AAD_LEN
];
3600 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param
;
3601 size_t len
= mblengths
[j
];
3604 memset(aad
, 0, 8); /* avoid uninitialized values */
3605 aad
[8] = 23; /* SSL3_RT_APPLICATION_DATA */
3606 aad
[9] = 3; /* version */
3608 aad
[11] = 0; /* length */
3610 mb_param
.out
= NULL
;
3613 mb_param
.interleave
= 8;
3615 packlen
= EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
,
3616 sizeof(mb_param
), &mb_param
);
3622 EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
,
3623 sizeof(mb_param
), &mb_param
);
3627 RAND_bytes(out
, 16);
3629 aad
[11] = (unsigned char)(len
>> 8);
3630 aad
[12] = (unsigned char)(len
);
3631 pad
= EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_TLS1_AAD
,
3632 EVP_AEAD_TLS1_AAD_LEN
, aad
);
3633 EVP_Cipher(ctx
, out
, inp
, len
+ pad
);
3637 BIO_printf(bio_err
, mr
? "+R:%d:%s:%f\n"
3638 : "%d %s's in %.2fs\n", count
, "evp", d
);
3639 results
[D_EVP
][j
] = ((double)count
) / d
* mblengths
[j
];
3643 fprintf(stdout
, "+H");
3644 for (j
= 0; j
< num
; j
++)
3645 fprintf(stdout
, ":%d", mblengths
[j
]);
3646 fprintf(stdout
, "\n");
3647 fprintf(stdout
, "+F:%d:%s", D_EVP
, alg_name
);
3648 for (j
= 0; j
< num
; j
++)
3649 fprintf(stdout
, ":%.2f", results
[D_EVP
][j
]);
3650 fprintf(stdout
, "\n");
3653 "The 'numbers' are in 1000s of bytes per second processed.\n");
3654 fprintf(stdout
, "type ");
3655 for (j
= 0; j
< num
; j
++)
3656 fprintf(stdout
, "%7d bytes", mblengths
[j
]);
3657 fprintf(stdout
, "\n");
3658 fprintf(stdout
, "%-24s", alg_name
);
3660 for (j
= 0; j
< num
; j
++) {
3661 if (results
[D_EVP
][j
] > 10000)
3662 fprintf(stdout
, " %11.2fk", results
[D_EVP
][j
] / 1e3
);
3664 fprintf(stdout
, " %11.2f ", results
[D_EVP
][j
]);
3666 fprintf(stdout
, "\n");
3672 EVP_CIPHER_CTX_free(ctx
);