speed.c 121.4 KB
Newer Older
R
Rich Salz 已提交
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
R
Rich Salz 已提交
5 6 7 8
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
9
 */
R
Rich Salz 已提交
10

11 12 13 14 15 16
#undef SECONDS
#define SECONDS                 3
#define RSA_SECONDS             10
#define DSA_SECONDS             10
#define ECDSA_SECONDS   10
#define ECDH_SECONDS    10
P
Paul Yang 已提交
17
#define EdDSA_SECONDS   10
18 19 20 21 22 23

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "apps.h"
24
#include "progs.h"
25 26 27 28 29
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
30
#include <openssl/async.h>
31 32 33
#if !defined(OPENSSL_SYS_MSDOS)
# include OPENSSL_UNISTD
#endif
34

35
#if defined(_WIN32)
36 37
# include <windows.h>
#endif
38

39 40 41 42
#include <openssl/bn.h>
#ifndef OPENSSL_NO_DES
# include <openssl/des.h>
#endif
M
Matt Caswell 已提交
43
#include <openssl/aes.h>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#ifndef OPENSSL_NO_CAMELLIA
# include <openssl/camellia.h>
#endif
#ifndef OPENSSL_NO_MD2
# include <openssl/md2.h>
#endif
#ifndef OPENSSL_NO_MDC2
# include <openssl/mdc2.h>
#endif
#ifndef OPENSSL_NO_MD4
# include <openssl/md4.h>
#endif
#ifndef OPENSSL_NO_MD5
# include <openssl/md5.h>
#endif
59 60
#include <openssl/hmac.h>
#include <openssl/sha.h>
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#ifndef OPENSSL_NO_RMD160
# include <openssl/ripemd.h>
#endif
#ifndef OPENSSL_NO_WHIRLPOOL
# include <openssl/whrlpool.h>
#endif
#ifndef OPENSSL_NO_RC4
# include <openssl/rc4.h>
#endif
#ifndef OPENSSL_NO_RC5
# include <openssl/rc5.h>
#endif
#ifndef OPENSSL_NO_RC2
# include <openssl/rc2.h>
#endif
#ifndef OPENSSL_NO_IDEA
# include <openssl/idea.h>
#endif
#ifndef OPENSSL_NO_SEED
# include <openssl/seed.h>
#endif
#ifndef OPENSSL_NO_BF
# include <openssl/blowfish.h>
#endif
#ifndef OPENSSL_NO_CAST
# include <openssl/cast.h>
#endif
#ifndef OPENSSL_NO_RSA
# include <openssl/rsa.h>
# include "./testrsa.h"
#endif
#include <openssl/x509.h>
#ifndef OPENSSL_NO_DSA
# include <openssl/dsa.h>
# include "./testdsa.h"
#endif
97
#ifndef OPENSSL_NO_EC
D
Dr. Stephen Henson 已提交
98
# include <openssl/ec.h>
99 100
#endif
#include <openssl/modes.h>
101

102
#ifndef HAVE_FORK
R
Rich Salz 已提交
103
# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
104
#  define HAVE_FORK 0
105
# else
106
#  define HAVE_FORK 1
107
# endif
108
#endif
109

110 111 112 113 114 115 116
#if HAVE_FORK
# undef NO_FORK
#else
# define NO_FORK
#endif

#define MAX_MISALIGNMENT 63
117 118 119
#define MAX_ECDH_SIZE   256
#define MISALIGN        64

120
typedef struct openssl_speed_sec_st {
121 122 123 124 125
    int sym;
    int rsa;
    int dsa;
    int ecdsa;
    int ecdh;
P
Paul Yang 已提交
126
    int eddsa;
127
} openssl_speed_sec_t;
128

129
static volatile int run = 0;
130

131 132
static int mr = 0;
static int usertime = 1;
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#ifndef OPENSSL_NO_MD2
static int EVP_Digest_MD2_loop(void *args);
#endif

#ifndef OPENSSL_NO_MDC2
static int EVP_Digest_MDC2_loop(void *args);
#endif
#ifndef OPENSSL_NO_MD4
static int EVP_Digest_MD4_loop(void *args);
#endif
#ifndef OPENSSL_NO_MD5
static int MD5_loop(void *args);
static int HMAC_loop(void *args);
#endif
static int SHA1_loop(void *args);
static int SHA256_loop(void *args);
static int SHA512_loop(void *args);
#ifndef OPENSSL_NO_WHIRLPOOL
static int WHIRLPOOL_loop(void *args);
#endif
#ifndef OPENSSL_NO_RMD160
static int EVP_Digest_RMD160_loop(void *args);
#endif
#ifndef OPENSSL_NO_RC4
static int RC4_loop(void *args);
#endif
#ifndef OPENSSL_NO_DES
static int DES_ncbc_encrypt_loop(void *args);
static int DES_ede3_cbc_encrypt_loop(void *args);
#endif
static int AES_cbc_128_encrypt_loop(void *args);
static int AES_cbc_192_encrypt_loop(void *args);
static int AES_ige_128_encrypt_loop(void *args);
static int AES_cbc_256_encrypt_loop(void *args);
static int AES_ige_192_encrypt_loop(void *args);
static int AES_ige_256_encrypt_loop(void *args);
static int CRYPTO_gcm128_aad_loop(void *args);
171
static int RAND_bytes_loop(void *args);
172
static int EVP_Update_loop(void *args);
173
static int EVP_Update_loop_ccm(void *args);
A
Andy Polyakov 已提交
174
static int EVP_Update_loop_aead(void *args);
175 176 177 178 179 180 181 182 183 184 185 186
static int EVP_Digest_loop(void *args);
#ifndef OPENSSL_NO_RSA
static int RSA_sign_loop(void *args);
static int RSA_verify_loop(void *args);
#endif
#ifndef OPENSSL_NO_DSA
static int DSA_sign_loop(void *args);
static int DSA_verify_loop(void *args);
#endif
#ifndef OPENSSL_NO_EC
static int ECDSA_sign_loop(void *args);
static int ECDSA_verify_loop(void *args);
P
Paul Yang 已提交
187 188
static int EdDSA_sign_loop(void *args);
static int EdDSA_verify_loop(void *args);
189 190
#endif

191
static double Time_F(int s);
192
static void print_message(const char *s, long num, int length, int tm);
193
static void pkey_print_message(const char *str, const char *str2,
194
                               long num, unsigned int bits, int sec);
195
static void print_result(int alg, int run_no, int count, double time_used);
196
#ifndef NO_FORK
197
static int do_multi(int multi, int size_num);
198
#endif
199

200 201 202 203 204
static const int lengths_list[] = {
    16, 64, 256, 1024, 8 * 1024, 16 * 1024
};
static const int *lengths = lengths_list;

A
Andy Polyakov 已提交
205 206 207 208
static const int aead_lengths_list[] = {
    2, 31, 136, 1024, 8 * 1024, 16 * 1024
};

209 210 211
#define START   0
#define STOP    1

212
#ifdef SIGALRM
D
Dr. Stephen Henson 已提交
213

214
static void alarmed(int sig)
215
{
216
    signal(SIGALRM, alarmed);
217 218
    run = 0;
}
219

220 221 222 223 224 225 226
static double Time_F(int s)
{
    double ret = app_tminterval(s, usertime);
    if (s == STOP)
        alarm(0);
    return ret;
}
227

228 229 230
#elif defined(_WIN32)

# define SIGALRM -1
R
Richard Levitte 已提交
231

232 233
static unsigned int lapse;
static volatile unsigned int schlock;
234 235 236 237
static void alarm_win32(unsigned int secs)
{
    lapse = secs * 1000;
}
R
Richard Levitte 已提交
238

239
# define alarm alarm_win32
240 241 242 243 244 245 246 247

static DWORD WINAPI sleepy(VOID * arg)
{
    schlock = 1;
    Sleep(lapse);
    run = 0;
    return 0;
}
248

249
static double Time_F(int s)
250 251 252 253 254 255 256 257
{
    double ret;
    static HANDLE thr;

    if (s == START) {
        schlock = 0;
        thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
        if (thr == NULL) {
258 259
            DWORD err = GetLastError();
            BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
260
            ExitProcess(err);
261 262 263 264 265 266 267 268 269 270 271 272 273
        }
        while (!schlock)
            Sleep(0);           /* scheduler spinlock */
        ret = app_tminterval(s, usertime);
    } else {
        ret = app_tminterval(s, usertime);
        if (run)
            TerminateThread(thr, 0);
        CloseHandle(thr);
    }

    return ret;
}
274
#else
275 276
static double Time_F(int s)
{
277
    return app_tminterval(s, usertime);
278
}
279
#endif
280

281
static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
282
                             const openssl_speed_sec_t *seconds);
283

284 285 286 287
#define found(value, pairs, result)\
    opt_found(value, result, pairs, OSSL_NELEM(pairs))
static int opt_found(const char *name, unsigned int *result,
                     const OPT_PAIR pairs[], unsigned int nbelem)
288
{
289 290 291
    unsigned int idx;

    for (idx = 0; idx < nbelem; ++idx, pairs++)
292 293 294 295 296 297 298 299 300
        if (strcmp(name, pairs->name) == 0) {
            *result = pairs->retval;
            return 1;
        }
    return 0;
}

typedef enum OPTION_choice {
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
P
Pauli 已提交
301
    OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
P
Paul Yang 已提交
302
    OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM,
A
Andy Polyakov 已提交
303
    OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD
304 305
} OPTION_CHOICE;

F
FdaSilvaYY 已提交
306
const OPTIONS speed_options[] = {
307 308 309
    {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
    {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
    {"help", OPT_HELP, '-', "Display this summary"},
A
Andy Polyakov 已提交
310
    {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
P
Pauli 已提交
311
    {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
312 313
    {"decrypt", OPT_DECRYPT, '-',
     "Time decryption instead of encryption (only EVP)"},
A
Andy Polyakov 已提交
314 315
    {"aead", OPT_AEAD, '-',
     "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
F
FdaSilvaYY 已提交
316
    {"mb", OPT_MB, '-',
A
Andy Polyakov 已提交
317 318
     "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
    {"mr", OPT_MR, '-', "Produce machine readable output"},
319 320 321
#ifndef NO_FORK
    {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
#endif
322
#ifndef OPENSSL_NO_ASYNC
F
FdaSilvaYY 已提交
323
    {"async_jobs", OPT_ASYNCJOBS, 'p',
A
Andy Polyakov 已提交
324
     "Enable async mode and start specified number of jobs"},
325
#endif
R
Rich Salz 已提交
326
    OPT_R_OPTIONS,
327 328 329
#ifndef OPENSSL_NO_ENGINE
    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
#endif
A
Andy Polyakov 已提交
330 331
    {"elapsed", OPT_ELAPSED, '-',
     "Use wall-clock time instead of CPU user time as divisor"},
P
Paul Yang 已提交
332
    {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
333
    {"seconds", OPT_SECONDS, 'p',
A
Andy Polyakov 已提交
334
     "Run benchmarks for specified amount of seconds"},
335
    {"bytes", OPT_BYTES, 'p',
A
Andy Polyakov 已提交
336 337 338
     "Run [non-PKI] benchmarks on custom-sized buffer"},
    {"misalign", OPT_MISALIGN, 'p',
     "Use specified offset to mis-align buffers"},
339
    {NULL}
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
};

#define D_MD2           0
#define D_MDC2          1
#define D_MD4           2
#define D_MD5           3
#define D_HMAC          4
#define D_SHA1          5
#define D_RMD160        6
#define D_RC4           7
#define D_CBC_DES       8
#define D_EDE3_DES      9
#define D_CBC_IDEA      10
#define D_CBC_SEED      11
#define D_CBC_RC2       12
#define D_CBC_RC5       13
#define D_CBC_BF        14
#define D_CBC_CAST      15
#define D_CBC_128_AES   16
#define D_CBC_192_AES   17
#define D_CBC_256_AES   18
#define D_CBC_128_CML   19
#define D_CBC_192_CML   20
#define D_CBC_256_CML   21
#define D_EVP           22
#define D_SHA256        23
#define D_SHA512        24
#define D_WHIRLPOOL     25
#define D_IGE_128_AES   26
#define D_IGE_192_AES   27
#define D_IGE_256_AES   28
#define D_GHASH         29
372
#define D_RAND          30
P
Pauli 已提交
373 374
#define D_EVP_HMAC      31

375 376 377 378 379 380 381 382 383
/* name of algorithms to test */
static const char *names[] = {
    "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
    "des cbc", "des ede3", "idea cbc", "seed cbc",
    "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
    "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
    "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
    "evp", "sha256", "sha512", "whirlpool",
    "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
P
Pauli 已提交
384
    "rand", "hmac"
385 386 387 388 389
};
#define ALGOR_NUM       OSSL_NELEM(names)

/* list of configured algorithm (remaining) */
static const OPT_PAIR doit_choices[] = {
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
#ifndef OPENSSL_NO_MD2
    {"md2", D_MD2},
#endif
#ifndef OPENSSL_NO_MDC2
    {"mdc2", D_MDC2},
#endif
#ifndef OPENSSL_NO_MD4
    {"md4", D_MD4},
#endif
#ifndef OPENSSL_NO_MD5
    {"md5", D_MD5},
    {"hmac", D_HMAC},
#endif
    {"sha1", D_SHA1},
    {"sha256", D_SHA256},
    {"sha512", D_SHA512},
#ifndef OPENSSL_NO_WHIRLPOOL
    {"whirlpool", D_WHIRLPOOL},
#endif
R
Rich Salz 已提交
409
#ifndef OPENSSL_NO_RMD160
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    {"ripemd", D_RMD160},
    {"rmd160", D_RMD160},
    {"ripemd160", D_RMD160},
#endif
#ifndef OPENSSL_NO_RC4
    {"rc4", D_RC4},
#endif
#ifndef OPENSSL_NO_DES
    {"des-cbc", D_CBC_DES},
    {"des-ede3", D_EDE3_DES},
#endif
    {"aes-128-cbc", D_CBC_128_AES},
    {"aes-192-cbc", D_CBC_192_AES},
    {"aes-256-cbc", D_CBC_256_AES},
    {"aes-128-ige", D_IGE_128_AES},
    {"aes-192-ige", D_IGE_192_AES},
    {"aes-256-ige", D_IGE_256_AES},
#ifndef OPENSSL_NO_RC2
    {"rc2-cbc", D_CBC_RC2},
    {"rc2", D_CBC_RC2},
#endif
#ifndef OPENSSL_NO_RC5
    {"rc5-cbc", D_CBC_RC5},
    {"rc5", D_CBC_RC5},
#endif
#ifndef OPENSSL_NO_IDEA
    {"idea-cbc", D_CBC_IDEA},
    {"idea", D_CBC_IDEA},
#endif
#ifndef OPENSSL_NO_SEED
    {"seed-cbc", D_CBC_SEED},
    {"seed", D_CBC_SEED},
#endif
#ifndef OPENSSL_NO_BF
    {"bf-cbc", D_CBC_BF},
    {"blowfish", D_CBC_BF},
    {"bf", D_CBC_BF},
#endif
#ifndef OPENSSL_NO_CAST
    {"cast-cbc", D_CBC_CAST},
    {"cast", D_CBC_CAST},
    {"cast5", D_CBC_CAST},
#endif
    {"ghash", D_GHASH},
454
    {"rand", D_RAND}
455 456
};

457 458
static double results[ALGOR_NUM][OSSL_NELEM(lengths_list)];

M
Matt Caswell 已提交
459 460 461 462
#ifndef OPENSSL_NO_DSA
# define R_DSA_512       0
# define R_DSA_1024      1
# define R_DSA_2048      2
463
static const OPT_PAIR dsa_choices[] = {
464 465
    {"dsa512", R_DSA_512},
    {"dsa1024", R_DSA_1024},
466
    {"dsa2048", R_DSA_2048}
467
};
468 469 470 471
# define DSA_NUM         OSSL_NELEM(dsa_choices)

static double dsa_results[DSA_NUM][2];  /* 2 ops: sign then verify */
#endif  /* OPENSSL_NO_DSA */
472

473 474 475 476 477 478 479
#define R_RSA_512       0
#define R_RSA_1024      1
#define R_RSA_2048      2
#define R_RSA_3072      3
#define R_RSA_4096      4
#define R_RSA_7680      5
#define R_RSA_15360     6
480 481
#ifndef OPENSSL_NO_RSA
static const OPT_PAIR rsa_choices[] = {
482 483 484 485 486 487
    {"rsa512", R_RSA_512},
    {"rsa1024", R_RSA_1024},
    {"rsa2048", R_RSA_2048},
    {"rsa3072", R_RSA_3072},
    {"rsa4096", R_RSA_4096},
    {"rsa7680", R_RSA_7680},
488
    {"rsa15360", R_RSA_15360}
489
};
490 491 492 493
# define RSA_NUM OSSL_NELEM(rsa_choices)

static double rsa_results[RSA_NUM][2];  /* 2 ops: sign then verify */
#endif /* OPENSSL_NO_RSA */
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

#define R_EC_P160    0
#define R_EC_P192    1
#define R_EC_P224    2
#define R_EC_P256    3
#define R_EC_P384    4
#define R_EC_P521    5
#define R_EC_K163    6
#define R_EC_K233    7
#define R_EC_K283    8
#define R_EC_K409    9
#define R_EC_K571    10
#define R_EC_B163    11
#define R_EC_B233    12
#define R_EC_B283    13
#define R_EC_B409    14
#define R_EC_B571    15
511 512 513 514 515 516 517 518
#define R_EC_BRP256R1  16
#define R_EC_BRP256T1  17
#define R_EC_BRP384R1  18
#define R_EC_BRP384T1  19
#define R_EC_BRP512R1  20
#define R_EC_BRP512T1  21
#define R_EC_X25519  22
#define R_EC_X448    23
R
Richard Levitte 已提交
519
#ifndef OPENSSL_NO_EC
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
static OPT_PAIR ecdsa_choices[] = {
    {"ecdsap160", R_EC_P160},
    {"ecdsap192", R_EC_P192},
    {"ecdsap224", R_EC_P224},
    {"ecdsap256", R_EC_P256},
    {"ecdsap384", R_EC_P384},
    {"ecdsap521", R_EC_P521},
    {"ecdsak163", R_EC_K163},
    {"ecdsak233", R_EC_K233},
    {"ecdsak283", R_EC_K283},
    {"ecdsak409", R_EC_K409},
    {"ecdsak571", R_EC_K571},
    {"ecdsab163", R_EC_B163},
    {"ecdsab233", R_EC_B233},
    {"ecdsab283", R_EC_B283},
    {"ecdsab409", R_EC_B409},
536 537 538 539 540 541 542
    {"ecdsab571", R_EC_B571},
    {"ecdsabrp256r1", R_EC_BRP256R1},
    {"ecdsabrp256t1", R_EC_BRP256T1},
    {"ecdsabrp384r1", R_EC_BRP384R1},
    {"ecdsabrp384t1", R_EC_BRP384T1},
    {"ecdsabrp512r1", R_EC_BRP512R1},
    {"ecdsabrp512t1", R_EC_BRP512T1}
543
};
544 545 546
# define ECDSA_NUM       OSSL_NELEM(ecdsa_choices)

static double ecdsa_results[ECDSA_NUM][2];    /* 2 ops: sign then verify */
F
FdaSilvaYY 已提交
547

548
static const OPT_PAIR ecdh_choices[] = {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    {"ecdhp160", R_EC_P160},
    {"ecdhp192", R_EC_P192},
    {"ecdhp224", R_EC_P224},
    {"ecdhp256", R_EC_P256},
    {"ecdhp384", R_EC_P384},
    {"ecdhp521", R_EC_P521},
    {"ecdhk163", R_EC_K163},
    {"ecdhk233", R_EC_K233},
    {"ecdhk283", R_EC_K283},
    {"ecdhk409", R_EC_K409},
    {"ecdhk571", R_EC_K571},
    {"ecdhb163", R_EC_B163},
    {"ecdhb233", R_EC_B233},
    {"ecdhb283", R_EC_B283},
    {"ecdhb409", R_EC_B409},
    {"ecdhb571", R_EC_B571},
565 566 567 568 569 570
    {"ecdhbrp256r1", R_EC_BRP256R1},
    {"ecdhbrp256t1", R_EC_BRP256T1},
    {"ecdhbrp384r1", R_EC_BRP384R1},
    {"ecdhbrp384t1", R_EC_BRP384T1},
    {"ecdhbrp512r1", R_EC_BRP512R1},
    {"ecdhbrp512t1", R_EC_BRP512T1},
571
    {"ecdhx25519", R_EC_X25519},
572
    {"ecdhx448", R_EC_X448}
573
};
574 575 576
# define EC_NUM       OSSL_NELEM(ecdh_choices)

static double ecdh_results[EC_NUM][1];  /* 1 op: derivation */
P
Paul Yang 已提交
577 578 579 580 581 582 583 584 585 586

#define R_EC_Ed25519    0
#define R_EC_Ed448      1
static OPT_PAIR eddsa_choices[] = {
    {"ed25519", R_EC_Ed25519},
    {"ed448", R_EC_Ed448}
};
# define EdDSA_NUM       OSSL_NELEM(eddsa_choices)

static double eddsa_results[EdDSA_NUM][2];    /* 2 ops: sign then verify */
587
#endif /* OPENSSL_NO_EC */
588

589 590 591 592
#ifndef SIGALRM
# define COND(d) (count < (d))
# define COUNT(d) (d)
#else
593
# define COND(unused_cond) (run && count<0x7fffffff)
594
# define COUNT(d) (count)
595
#endif                          /* SIGALRM */
596

597 598 599 600 601 602 603 604 605
typedef struct loopargs_st {
    ASYNC_JOB *inprogress_job;
    ASYNC_WAIT_CTX *wait_ctx;
    unsigned char *buf;
    unsigned char *buf2;
    unsigned char *buf_malloc;
    unsigned char *buf2_malloc;
    unsigned char *key;
    unsigned int siglen;
M
Matt Caswell 已提交
606
    size_t sigsize;
607 608 609 610 611 612 613 614 615
#ifndef OPENSSL_NO_RSA
    RSA *rsa_key[RSA_NUM];
#endif
#ifndef OPENSSL_NO_DSA
    DSA *dsa_key[DSA_NUM];
#endif
#ifndef OPENSSL_NO_EC
    EC_KEY *ecdsa[ECDSA_NUM];
    EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
P
Paul Yang 已提交
616
    EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
617 618 619 620 621 622 623 624 625 626 627 628
    unsigned char *secret_a;
    unsigned char *secret_b;
    size_t outlen[EC_NUM];
#endif
    EVP_CIPHER_CTX *ctx;
    HMAC_CTX *hctx;
    GCM128_CONTEXT *gcm_ctx;
} loopargs_t;
static int run_benchmark(int async_jobs, int (*loop_function) (void *),
                         loopargs_t * loopargs);

static unsigned int testnum;
629

F
FdaSilvaYY 已提交
630
/* Nb of iterations to do per algorithm and key-size */
631
static long c[ALGOR_NUM][OSSL_NELEM(lengths_list)];
632

633
#ifndef OPENSSL_NO_MD2
634 635
static int EVP_Digest_MD2_loop(void *args)
{
636
    loopargs_t *tempargs = *(loopargs_t **) args;
637
    unsigned char *buf = tempargs->buf;
638
    unsigned char md2[MD2_DIGEST_LENGTH];
639
    int count;
640

641
    for (count = 0; COND(c[D_MD2][testnum]); count++) {
642
        if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
643
                        NULL))
644 645
            return -1;
    }
646 647
    return count;
}
648
#endif
649

650
#ifndef OPENSSL_NO_MDC2
651 652
static int EVP_Digest_MDC2_loop(void *args)
{
653
    loopargs_t *tempargs = *(loopargs_t **) args;
654
    unsigned char *buf = tempargs->buf;
655
    unsigned char mdc2[MDC2_DIGEST_LENGTH];
656
    int count;
657

658
    for (count = 0; COND(c[D_MDC2][testnum]); count++) {
659
        if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
660
                        NULL))
661 662
            return -1;
    }
663 664
    return count;
}
665
#endif
666

667
#ifndef OPENSSL_NO_MD4
668 669
static int EVP_Digest_MD4_loop(void *args)
{
670
    loopargs_t *tempargs = *(loopargs_t **) args;
671
    unsigned char *buf = tempargs->buf;
672
    unsigned char md4[MD4_DIGEST_LENGTH];
673
    int count;
674

675
    for (count = 0; COND(c[D_MD4][testnum]); count++) {
676
        if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
677
                        NULL))
678 679
            return -1;
    }
680 681
    return count;
}
682
#endif
683

684
#ifndef OPENSSL_NO_MD5
685 686
static int MD5_loop(void *args)
{
687
    loopargs_t *tempargs = *(loopargs_t **) args;
688
    unsigned char *buf = tempargs->buf;
689
    unsigned char md5[MD5_DIGEST_LENGTH];
690 691 692 693 694 695 696 697
    int count;
    for (count = 0; COND(c[D_MD5][testnum]); count++)
        MD5(buf, lengths[testnum], md5);
    return count;
}

static int HMAC_loop(void *args)
{
698
    loopargs_t *tempargs = *(loopargs_t **) args;
699 700
    unsigned char *buf = tempargs->buf;
    HMAC_CTX *hctx = tempargs->hctx;
701
    unsigned char hmac[MD5_DIGEST_LENGTH];
702
    int count;
703

704 705 706
    for (count = 0; COND(c[D_HMAC][testnum]); count++) {
        HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
        HMAC_Update(hctx, buf, lengths[testnum]);
707
        HMAC_Final(hctx, hmac, NULL);
708 709 710
    }
    return count;
}
711
#endif
712 713 714

static int SHA1_loop(void *args)
{
715
    loopargs_t *tempargs = *(loopargs_t **) args;
716
    unsigned char *buf = tempargs->buf;
717
    unsigned char sha[SHA_DIGEST_LENGTH];
718 719 720 721 722 723 724 725
    int count;
    for (count = 0; COND(c[D_SHA1][testnum]); count++)
        SHA1(buf, lengths[testnum], sha);
    return count;
}

static int SHA256_loop(void *args)
{
726
    loopargs_t *tempargs = *(loopargs_t **) args;
727
    unsigned char *buf = tempargs->buf;
728
    unsigned char sha256[SHA256_DIGEST_LENGTH];
729 730 731 732 733 734 735 736
    int count;
    for (count = 0; COND(c[D_SHA256][testnum]); count++)
        SHA256(buf, lengths[testnum], sha256);
    return count;
}

static int SHA512_loop(void *args)
{
737
    loopargs_t *tempargs = *(loopargs_t **) args;
738
    unsigned char *buf = tempargs->buf;
739
    unsigned char sha512[SHA512_DIGEST_LENGTH];
740 741 742 743 744 745
    int count;
    for (count = 0; COND(c[D_SHA512][testnum]); count++)
        SHA512(buf, lengths[testnum], sha512);
    return count;
}

746
#ifndef OPENSSL_NO_WHIRLPOOL
747 748
static int WHIRLPOOL_loop(void *args)
{
749
    loopargs_t *tempargs = *(loopargs_t **) args;
750
    unsigned char *buf = tempargs->buf;
751
    unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
752 753 754 755 756
    int count;
    for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
        WHIRLPOOL(buf, lengths[testnum], whirlpool);
    return count;
}
757
#endif
758

R
Rich Salz 已提交
759
#ifndef OPENSSL_NO_RMD160
760 761
static int EVP_Digest_RMD160_loop(void *args)
{
762
    loopargs_t *tempargs = *(loopargs_t **) args;
763
    unsigned char *buf = tempargs->buf;
764
    unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
765
    int count;
766
    for (count = 0; COND(c[D_RMD160][testnum]); count++) {
767
        if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
768
                        NULL, EVP_ripemd160(), NULL))
769 770
            return -1;
    }
771 772
    return count;
}
773
#endif
774

775
#ifndef OPENSSL_NO_RC4
776 777 778
static RC4_KEY rc4_ks;
static int RC4_loop(void *args)
{
779
    loopargs_t *tempargs = *(loopargs_t **) args;
780 781 782
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_RC4][testnum]); count++)
783
        RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
784 785 786 787 788 789 790 791 792 793 794
    return count;
}
#endif

#ifndef OPENSSL_NO_DES
static unsigned char DES_iv[8];
static DES_key_schedule sch;
static DES_key_schedule sch2;
static DES_key_schedule sch3;
static int DES_ncbc_encrypt_loop(void *args)
{
795
    loopargs_t *tempargs = *(loopargs_t **) args;
796 797 798 799
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
        DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch,
800
                         &DES_iv, DES_ENCRYPT);
801 802 803 804 805
    return count;
}

static int DES_ede3_cbc_encrypt_loop(void *args)
{
806
    loopargs_t *tempargs = *(loopargs_t **) args;
807 808 809 810
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
        DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
811
                             &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT);
812 813 814 815
    return count;
}
#endif

M
Matt Caswell 已提交
816
#define MAX_BLOCK_SIZE 128
817 818 819 820 821

static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
static AES_KEY aes_ks1, aes_ks2, aes_ks3;
static int AES_cbc_128_encrypt_loop(void *args)
{
822
    loopargs_t *tempargs = *(loopargs_t **) args;
823 824 825 826
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
827
                        (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
828 829 830 831 832
    return count;
}

static int AES_cbc_192_encrypt_loop(void *args)
{
833
    loopargs_t *tempargs = *(loopargs_t **) args;
834 835 836 837
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
838
                        (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
839 840 841 842 843
    return count;
}

static int AES_cbc_256_encrypt_loop(void *args)
{
844
    loopargs_t *tempargs = *(loopargs_t **) args;
845 846 847 848
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
849
                        (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
850 851 852 853 854
    return count;
}

static int AES_ige_128_encrypt_loop(void *args)
{
855
    loopargs_t *tempargs = *(loopargs_t **) args;
856 857 858 859 860
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
    int count;
    for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
        AES_ige_encrypt(buf, buf2,
861
                        (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
862 863 864 865 866
    return count;
}

static int AES_ige_192_encrypt_loop(void *args)
{
867
    loopargs_t *tempargs = *(loopargs_t **) args;
868 869 870 871 872
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
    int count;
    for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
        AES_ige_encrypt(buf, buf2,
873
                        (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
874 875 876 877 878
    return count;
}

static int AES_ige_256_encrypt_loop(void *args)
{
879
    loopargs_t *tempargs = *(loopargs_t **) args;
880 881 882 883 884
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
    int count;
    for (count = 0; COND(c[D_IGE_256_AES][testnum]); count++)
        AES_ige_encrypt(buf, buf2,
885
                        (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
886 887 888 889 890
    return count;
}

static int CRYPTO_gcm128_aad_loop(void *args)
{
891
    loopargs_t *tempargs = *(loopargs_t **) args;
892 893 894 895 896 897 898 899
    unsigned char *buf = tempargs->buf;
    GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
    int count;
    for (count = 0; COND(c[D_GHASH][testnum]); count++)
        CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
    return count;
}

900 901 902 903 904 905 906 907 908 909 910
static int RAND_bytes_loop(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    int count;

    for (count = 0; COND(c[D_RAND][testnum]); count++)
        RAND_bytes(buf, lengths[testnum]);
    return count;
}

911
static long save_count = 0;
912 913 914
static int decrypt = 0;
static int EVP_Update_loop(void *args)
{
915
    loopargs_t *tempargs = *(loopargs_t **) args;
916 917
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
918
    int outl, count, rc;
919 920 921
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
922 923 924
    if (decrypt) {
        for (count = 0; COND(nb_iter); count++) {
            rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
925 926
            if (rc != 1) {
                /* reset iv in case of counter overflow */
927
                EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
928
            }
929 930 931 932
        }
    } else {
        for (count = 0; COND(nb_iter); count++) {
            rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
933 934
            if (rc != 1) {
                /* reset iv in case of counter overflow */
935
                EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
936
            }
937 938
        }
    }
939 940 941 942 943 944
    if (decrypt)
        EVP_DecryptFinal_ex(ctx, buf, &outl);
    else
        EVP_EncryptFinal_ex(ctx, buf, &outl);
    return count;
}
A
Andy Polyakov 已提交
945

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
/*
 * CCM does not support streaming. For the purpose of performance measurement,
 * each message is encrypted using the same (key,iv)-pair. Do not use this
 * code in your application.
 */
static int EVP_Update_loop_ccm(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
    int outl, count;
    unsigned char tag[12];
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
    if (decrypt) {
        for (count = 0; COND(nb_iter); count++) {
            EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
964 965 966
            /* reset iv */
            EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
            /* counter is reset on every update */
967 968 969 970
            EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
        }
    } else {
        for (count = 0; COND(nb_iter); count++) {
971
            /* restore iv length field */
972
            EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
973
            /* counter is reset on every update */
974 975 976
            EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
        }
    }
977 978 979 980
    if (decrypt)
        EVP_DecryptFinal_ex(ctx, buf, &outl);
    else
        EVP_EncryptFinal_ex(ctx, buf, &outl);
981 982
    return count;
}
983

A
Andy Polyakov 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
/*
 * To make AEAD benchmarking more relevant perform TLS-like operations,
 * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
 * payload length is not actually limited by 16KB...
 */
static int EVP_Update_loop_aead(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
    int outl, count;
    unsigned char aad[13] = { 0xcc };
    unsigned char faketag[16] = { 0xcc };
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
    if (decrypt) {
        for (count = 0; COND(nb_iter); count++) {
            EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
            EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
                                sizeof(faketag), faketag);
            EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
            EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
            EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
        }
    } else {
        for (count = 0; COND(nb_iter); count++) {
            EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
            EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
            EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
            EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
        }
    }
    return count;
}

1020 1021 1022
static const EVP_MD *evp_md = NULL;
static int EVP_Digest_loop(void *args)
{
1023
    loopargs_t *tempargs = *(loopargs_t **) args;
1024 1025 1026
    unsigned char *buf = tempargs->buf;
    unsigned char md[EVP_MAX_MD_SIZE];
    int count;
1027 1028 1029 1030 1031 1032
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif

    for (count = 0; COND(nb_iter); count++) {
        if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
1033 1034
            return -1;
    }
1035 1036 1037
    return count;
}

P
Pauli 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
static const EVP_MD *evp_hmac_md = NULL;
static char *evp_hmac_name = NULL;
static int EVP_HMAC_loop(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    unsigned char no_key[32];
    int count;
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif

    for (count = 0; COND(nb_iter); count++) {
        if (HMAC(evp_hmac_md, no_key, sizeof(no_key), buf, lengths[testnum],
                 NULL, NULL) == NULL)
            return -1;
    }
    return count;
}

1058
#ifndef OPENSSL_NO_RSA
F
FdaSilvaYY 已提交
1059
static long rsa_c[RSA_NUM][2];  /* # RSA iteration test */
1060 1061 1062

static int RSA_sign_loop(void *args)
{
1063
    loopargs_t *tempargs = *(loopargs_t **) args;
1064 1065
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
F
FdaSilvaYY 已提交
1066
    unsigned int *rsa_num = &tempargs->siglen;
1067
    RSA **rsa_key = tempargs->rsa_key;
1068 1069
    int ret, count;
    for (count = 0; COND(rsa_c[testnum][0]); count++) {
1070
        ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        if (ret == 0) {
            BIO_printf(bio_err, "RSA sign failure\n");
            ERR_print_errors(bio_err);
            count = -1;
            break;
        }
    }
    return count;
}

static int RSA_verify_loop(void *args)
{
1083
    loopargs_t *tempargs = *(loopargs_t **) args;
1084 1085
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
F
FdaSilvaYY 已提交
1086
    unsigned int rsa_num = tempargs->siglen;
1087
    RSA **rsa_key = tempargs->rsa_key;
1088 1089
    int ret, count;
    for (count = 0; COND(rsa_c[testnum][1]); count++) {
1090 1091
        ret =
            RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
        if (ret <= 0) {
            BIO_printf(bio_err, "RSA verify failure\n");
            ERR_print_errors(bio_err);
            count = -1;
            break;
        }
    }
    return count;
}
#endif

#ifndef OPENSSL_NO_DSA
static long dsa_c[DSA_NUM][2];
static int DSA_sign_loop(void *args)
{
1107
    loopargs_t *tempargs = *(loopargs_t **) args;
1108 1109
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
1110
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
1111
    unsigned int *siglen = &tempargs->siglen;
1112 1113 1114 1115 1116 1117
    int ret, count;
    for (count = 0; COND(dsa_c[testnum][0]); count++) {
        ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
        if (ret == 0) {
            BIO_printf(bio_err, "DSA sign failure\n");
            ERR_print_errors(bio_err);
1118
            count = -1;
1119 1120 1121 1122 1123 1124 1125 1126
            break;
        }
    }
    return count;
}

static int DSA_verify_loop(void *args)
{
1127
    loopargs_t *tempargs = *(loopargs_t **) args;
1128 1129
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
1130
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
1131
    unsigned int siglen = tempargs->siglen;
1132 1133 1134 1135 1136 1137
    int ret, count;
    for (count = 0; COND(dsa_c[testnum][1]); count++) {
        ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
        if (ret <= 0) {
            BIO_printf(bio_err, "DSA verify failure\n");
            ERR_print_errors(bio_err);
1138
            count = -1;
1139 1140 1141 1142 1143 1144 1145 1146
            break;
        }
    }
    return count;
}
#endif

#ifndef OPENSSL_NO_EC
1147
static long ecdsa_c[ECDSA_NUM][2];
1148 1149
static int ECDSA_sign_loop(void *args)
{
1150
    loopargs_t *tempargs = *(loopargs_t **) args;
1151
    unsigned char *buf = tempargs->buf;
1152 1153
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
1154
    unsigned int *ecdsasiglen = &tempargs->siglen;
1155 1156
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1157
        ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1158 1159 1160
        if (ret == 0) {
            BIO_printf(bio_err, "ECDSA sign failure\n");
            ERR_print_errors(bio_err);
1161
            count = -1;
1162 1163 1164 1165 1166 1167 1168 1169
            break;
        }
    }
    return count;
}

static int ECDSA_verify_loop(void *args)
{
1170
    loopargs_t *tempargs = *(loopargs_t **) args;
1171
    unsigned char *buf = tempargs->buf;
1172 1173
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
1174
    unsigned int ecdsasiglen = tempargs->siglen;
1175 1176
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1177
        ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1178 1179 1180
        if (ret != 1) {
            BIO_printf(bio_err, "ECDSA verify failure\n");
            ERR_print_errors(bio_err);
1181
            count = -1;
1182 1183 1184 1185 1186 1187
            break;
        }
    }
    return count;
}

1188
/* ******************************************************************** */
1189 1190
static long ecdh_c[EC_NUM][1];

1191 1192 1193 1194 1195
static int ECDH_EVP_derive_key_loop(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
    unsigned char *derived_secret = tempargs->secret_a;
1196
    int count;
1197
    size_t *outlen = &(tempargs->outlen[testnum]);
F
FdaSilvaYY 已提交
1198

1199
    for (count = 0; COND(ecdh_c[testnum][0]); count++)
1200 1201
        EVP_PKEY_derive(ctx, derived_secret, outlen);

1202 1203
    return count;
}
1204

P
Paul Yang 已提交
1205 1206 1207 1208 1209 1210 1211
static long eddsa_c[EdDSA_NUM][2];
static int EdDSA_sign_loop(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
    unsigned char *eddsasig = tempargs->buf2;
M
Matt Caswell 已提交
1212
    size_t *eddsasigsize = &tempargs->sigsize;
P
Paul Yang 已提交
1213 1214 1215
    int ret, count;

    for (count = 0; COND(eddsa_c[testnum][0]); count++) {
M
Matt Caswell 已提交
1216
        ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
P
Paul Yang 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
        if (ret == 0) {
            BIO_printf(bio_err, "EdDSA sign failure\n");
            ERR_print_errors(bio_err);
            count = -1;
            break;
        }
    }
    return count;
}

static int EdDSA_verify_loop(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
    unsigned char *eddsasig = tempargs->buf2;
M
Matt Caswell 已提交
1233
    size_t eddsasigsize = tempargs->sigsize;
P
Paul Yang 已提交
1234 1235 1236
    int ret, count;

    for (count = 0; COND(eddsa_c[testnum][1]); count++) {
M
Matt Caswell 已提交
1237
        ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
P
Paul Yang 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246
        if (ret != 1) {
            BIO_printf(bio_err, "EdDSA verify failure\n");
            ERR_print_errors(bio_err);
            count = -1;
            break;
        }
    }
    return count;
}
F
FdaSilvaYY 已提交
1247
#endif                          /* OPENSSL_NO_EC */
1248

F
FdaSilvaYY 已提交
1249
static int run_benchmark(int async_jobs,
1250
                         int (*loop_function) (void *), loopargs_t * loopargs)
1251 1252 1253 1254
{
    int job_op_count = 0;
    int total_op_count = 0;
    int num_inprogress = 0;
F
FdaSilvaYY 已提交
1255
    int error = 0, i = 0, ret = 0;
1256 1257
    OSSL_ASYNC_FD job_fd = 0;
    size_t num_job_fds = 0;
1258 1259 1260

    run = 1;

1261
    if (async_jobs == 0) {
1262
        return loop_function((void *)&loopargs);
1263 1264 1265
    }

    for (i = 0; i < async_jobs && !error; i++) {
1266 1267 1268
        loopargs_t *looparg_item = loopargs + i;

        /* Copy pointer content (looparg_t item address) into async context */
F
FdaSilvaYY 已提交
1269 1270
        ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
                              &job_op_count, loop_function,
1271
                              (void *)&looparg_item, sizeof(looparg_item));
F
FdaSilvaYY 已提交
1272
        switch (ret) {
F
FdaSilvaYY 已提交
1273 1274 1275 1276 1277
        case ASYNC_PAUSE:
            ++num_inprogress;
            break;
        case ASYNC_FINISH:
            if (job_op_count == -1) {
1278
                error = 1;
F
FdaSilvaYY 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
            } else {
                total_op_count += job_op_count;
            }
            break;
        case ASYNC_NO_JOBS:
        case ASYNC_ERR:
            BIO_printf(bio_err, "Failure in the job\n");
            ERR_print_errors(bio_err);
            error = 1;
            break;
1289 1290 1291 1292
        }
    }

    while (num_inprogress > 0) {
1293
#if defined(OPENSSL_SYS_WINDOWS)
1294
        DWORD avail = 0;
1295
#elif defined(OPENSSL_SYS_UNIX)
1296
        int select_result = 0;
1297 1298
        OSSL_ASYNC_FD max_fd = 0;
        fd_set waitfdset;
1299

1300
        FD_ZERO(&waitfdset);
1301

1302 1303 1304
        for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
            if (loopargs[i].inprogress_job == NULL)
                continue;
1305

1306 1307 1308
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1309 1310 1311 1312
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
1313
            }
1314 1315
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1316 1317 1318
            FD_SET(job_fd, &waitfdset);
            if (job_fd > max_fd)
                max_fd = job_fd;
1319 1320
        }

B
Ben Laurie 已提交
1321
        if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1322
            BIO_printf(bio_err,
1323 1324 1325
                       "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
                       "Decrease the value of async_jobs\n",
                       max_fd, FD_SETSIZE);
1326 1327 1328 1329 1330
            ERR_print_errors(bio_err);
            error = 1;
            break;
        }

1331
        select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1332 1333 1334 1335
        if (select_result == -1 && errno == EINTR)
            continue;

        if (select_result == -1) {
1336 1337 1338 1339
            BIO_printf(bio_err, "Failure in the select\n");
            ERR_print_errors(bio_err);
            error = 1;
            break;
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
        }

        if (select_result == 0)
            continue;
#endif

        for (i = 0; i < async_jobs; i++) {
            if (loopargs[i].inprogress_job == NULL)
                continue;

1350 1351 1352
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1353 1354 1355 1356 1357
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
            }
1358 1359
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1360

1361
#if defined(OPENSSL_SYS_UNIX)
1362
            if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1363
                continue;
1364
#elif defined(OPENSSL_SYS_WINDOWS)
F
FdaSilvaYY 已提交
1365
            if (num_job_fds == 1
F
FdaSilvaYY 已提交
1366
                && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
F
FdaSilvaYY 已提交
1367
                && avail > 0)
1368 1369 1370
                continue;
#endif

1371
            ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1372 1373 1374
                                  loopargs[i].wait_ctx, &job_op_count,
                                  loop_function, (void *)(loopargs + i),
                                  sizeof(loopargs_t));
F
FdaSilvaYY 已提交
1375
            switch (ret) {
F
FdaSilvaYY 已提交
1376 1377 1378 1379
            case ASYNC_PAUSE:
                break;
            case ASYNC_FINISH:
                if (job_op_count == -1) {
1380
                    error = 1;
F
FdaSilvaYY 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
                } else {
                    total_op_count += job_op_count;
                }
                --num_inprogress;
                loopargs[i].inprogress_job = NULL;
                break;
            case ASYNC_NO_JOBS:
            case ASYNC_ERR:
                --num_inprogress;
                loopargs[i].inprogress_job = NULL;
                BIO_printf(bio_err, "Failure in the job\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
1395 1396 1397 1398 1399 1400 1401 1402 1403
            }
        }
    }

    return error ? -1 : total_op_count;
}

int speed_main(int argc, char **argv)
{
1404
    ENGINE *e = NULL;
1405
    loopargs_t *loopargs = NULL;
1406
    const char *prog;
1407
    const char *engine_id = NULL;
1408 1409 1410
    const EVP_CIPHER *evp_cipher = NULL;
    double d = 0.0;
    OPTION_CHOICE o;
1411
    int async_init = 0, multiblock = 0, pr_header = 0;
1412
    int doit[ALGOR_NUM] = { 0 };
A
Andy Polyakov 已提交
1413
    int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
1414
    long count = 0;
1415 1416
    unsigned int size_num = OSSL_NELEM(lengths_list);
    unsigned int i, k, loop, loopargs_len = 0, async_jobs = 0;
1417
    int keylen;
P
Patrick Steuer 已提交
1418
    int buflen;
1419 1420 1421
#ifndef NO_FORK
    int multi = 0;
#endif
1422 1423
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
    || !defined(OPENSSL_NO_EC)
1424
    long rsa_count = 1;
1425
#endif
1426
    openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
P
Paul Yang 已提交
1427 1428
                                    ECDSA_SECONDS, ECDH_SECONDS,
                                    EdDSA_SECONDS };
1429 1430

    /* What follows are the buffers and key material. */
1431
#ifndef OPENSSL_NO_RC5
1432
    RC5_32_KEY rc5_ks;
1433 1434
#endif
#ifndef OPENSSL_NO_RC2
1435
    RC2_KEY rc2_ks;
1436 1437
#endif
#ifndef OPENSSL_NO_IDEA
1438
    IDEA_KEY_SCHEDULE idea_ks;
1439 1440
#endif
#ifndef OPENSSL_NO_SEED
1441
    SEED_KEY_SCHEDULE seed_ks;
1442 1443
#endif
#ifndef OPENSSL_NO_BF
1444
    BF_KEY bf_ks;
1445 1446
#endif
#ifndef OPENSSL_NO_CAST
1447
    CAST_KEY cast_ks;
1448
#endif
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    static const unsigned char key16[16] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
    };
    static const unsigned char key24[24] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
        0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
    };
    static const unsigned char key32[32] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
        0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
        0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
    };
1464
#ifndef OPENSSL_NO_CAMELLIA
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    static const unsigned char ckey24[24] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
        0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
    };
    static const unsigned char ckey32[32] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
        0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
        0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
    };
1476
    CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1477 1478
#endif
#ifndef OPENSSL_NO_DES
1479 1480 1481 1482 1483 1484 1485 1486 1487
    static DES_cblock key = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
    };
    static DES_cblock key2 = {
        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
    };
    static DES_cblock key3 = {
        0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
    };
1488 1489
#endif
#ifndef OPENSSL_NO_RSA
1490
    static const unsigned int rsa_bits[RSA_NUM] = {
1491 1492
        512, 1024, 2048, 3072, 4096, 7680, 15360
    };
1493
    static const unsigned char *rsa_data[RSA_NUM] = {
1494 1495
        test512, test1024, test2048, test3072, test4096, test7680, test15360
    };
1496
    static const int rsa_data_length[RSA_NUM] = {
1497 1498 1499 1500 1501
        sizeof(test512), sizeof(test1024),
        sizeof(test2048), sizeof(test3072),
        sizeof(test4096), sizeof(test7680),
        sizeof(test15360)
    };
1502
    int rsa_doit[RSA_NUM] = { 0 };
P
Paul Yang 已提交
1503
    int primes = RSA_DEFAULT_PRIME_NUM;
1504 1505
#endif
#ifndef OPENSSL_NO_DSA
1506
    static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1507
    int dsa_doit[DSA_NUM] = { 0 };
1508 1509
#endif
#ifndef OPENSSL_NO_EC
1510 1511 1512
    /*
     * We only test over the following curves as they are representative, To
     * add tests over more curves, simply add the curve NID and curve name to
1513
     * the following arrays and increase the |ecdh_choices| list accordingly.
1514
     */
1515 1516 1517 1518 1519
    static const struct {
        const char *name;
        unsigned int nid;
        unsigned int bits;
    } test_curves[] = {
1520
        /* Prime Curves */
1521 1522 1523 1524 1525 1526
        {"secp160r1", NID_secp160r1, 160},
        {"nistp192", NID_X9_62_prime192v1, 192},
        {"nistp224", NID_secp224r1, 224},
        {"nistp256", NID_X9_62_prime256v1, 256},
        {"nistp384", NID_secp384r1, 384}, 
        {"nistp521", NID_secp521r1, 521},
1527
        /* Binary Curves */
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
        {"nistk163", NID_sect163k1, 163},
        {"nistk233", NID_sect233k1, 233}, 
        {"nistk283", NID_sect283k1, 283},
        {"nistk409", NID_sect409k1, 409},
        {"nistk571", NID_sect571k1, 571},
        {"nistb163", NID_sect163r2, 163},
        {"nistb233", NID_sect233r1, 233},
        {"nistb283", NID_sect283r1, 283},
        {"nistb409", NID_sect409r1, 409},
        {"nistb571", NID_sect571r1, 571},
1538 1539 1540 1541 1542 1543
        {"brainpoolP256r1", NID_brainpoolP256r1, 256},
        {"brainpoolP256t1", NID_brainpoolP256t1, 256},
        {"brainpoolP384r1", NID_brainpoolP384r1, 384},
        {"brainpoolP384t1", NID_brainpoolP384t1, 384},
        {"brainpoolP512r1", NID_brainpoolP512r1, 512},
        {"brainpoolP512t1", NID_brainpoolP512t1, 512},
1544
        /* Other and ECDH only ones */
1545 1546
        {"X25519", NID_X25519, 253},
        {"X448", NID_X448, 448}
1547
    };
P
Paul Yang 已提交
1548 1549 1550 1551
    static const struct {
        const char *name;
        unsigned int nid;
        unsigned int bits;
M
Matt Caswell 已提交
1552
        size_t sigsize;
P
Paul Yang 已提交
1553 1554 1555 1556 1557
    } test_ed_curves[] = {
        /* EdDSA */
        {"Ed25519", NID_ED25519, 253, 64},
        {"Ed448", NID_ED448, 456, 114}
    };
1558
    int ecdsa_doit[ECDSA_NUM] = { 0 };
1559
    int ecdh_doit[EC_NUM] = { 0 };
P
Paul Yang 已提交
1560
    int eddsa_doit[EdDSA_NUM] = { 0 };
1561
    OPENSSL_assert(OSSL_NELEM(test_curves) >= EC_NUM);
P
Paul Yang 已提交
1562
    OPENSSL_assert(OSSL_NELEM(test_ed_curves) >= EdDSA_NUM);
F
FdaSilvaYY 已提交
1563
#endif                          /* ndef OPENSSL_NO_EC */
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577

    prog = opt_init(argc, argv, speed_options);
    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        case OPT_EOF:
        case OPT_ERR:
 opterr:
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
            goto end;
        case OPT_HELP:
            opt_help(speed_options);
            ret = 0;
            goto end;
        case OPT_ELAPSED:
1578
            usertime = 0;
1579 1580
            break;
        case OPT_EVP:
1581
            evp_md = NULL;
1582 1583 1584 1585 1586
            evp_cipher = EVP_get_cipherbyname(opt_arg());
            if (evp_cipher == NULL)
                evp_md = EVP_get_digestbyname(opt_arg());
            if (evp_cipher == NULL && evp_md == NULL) {
                BIO_printf(bio_err,
F
FdaSilvaYY 已提交
1587
                           "%s: %s is an unknown cipher or digest\n",
1588
                           prog, opt_arg());
1589 1590 1591
                goto end;
            }
            doit[D_EVP] = 1;
1592
            break;
P
Pauli 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601
        case OPT_HMAC:
            evp_hmac_md = EVP_get_digestbyname(opt_arg());
            if (evp_hmac_md == NULL) {
                BIO_printf(bio_err, "%s: %s is an unknown digest\n",
                           prog, opt_arg());
                goto end;
            }
            doit[D_EVP_HMAC] = 1;
            break;
1602
        case OPT_DECRYPT:
1603
            decrypt = 1;
1604 1605
            break;
        case OPT_ENGINE:
1606 1607 1608 1609 1610 1611
            /*
             * In a forked execution, an engine might need to be
             * initialised by each child process, not by the parent.
             * So store the name here and run setup_engine() later on.
             */
            engine_id = opt_arg();
1612 1613
            break;
        case OPT_MULTI:
1614
#ifndef NO_FORK
1615
            multi = atoi(opt_arg());
1616 1617 1618
#endif
            break;
        case OPT_ASYNCJOBS:
1619
#ifndef OPENSSL_NO_ASYNC
1620
            async_jobs = atoi(opt_arg());
1621 1622 1623 1624 1625 1626
            if (!ASYNC_is_capable()) {
                BIO_printf(bio_err,
                           "%s: async_jobs specified but async not supported\n",
                           prog);
                goto opterr;
            }
1627
            if (async_jobs > 99999) {
1628
                BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
1629 1630
                goto opterr;
            }
1631
#endif
1632
            break;
1633 1634
        case OPT_MISALIGN:
            if (!opt_int(opt_arg(), &misalign))
1635
                goto end;
1636
            if (misalign > MISALIGN) {
1637
                BIO_printf(bio_err,
1638 1639
                           "%s: Maximum offset is %d\n", prog, MISALIGN);
                goto opterr;
1640
            }
1641 1642 1643 1644 1645 1646
            break;
        case OPT_MR:
            mr = 1;
            break;
        case OPT_MB:
            multiblock = 1;
F
FdaSilvaYY 已提交
1647 1648 1649 1650 1651 1652
#ifdef OPENSSL_NO_MULTIBLOCK
            BIO_printf(bio_err,
                       "%s: -mb specified but multi-block support is disabled\n",
                       prog);
            goto end;
#endif
1653
            break;
R
Rich Salz 已提交
1654 1655 1656 1657
        case OPT_R_CASES:
            if (!opt_rand(o))
                goto end;
            break;
P
Paul Yang 已提交
1658 1659 1660 1661
        case OPT_PRIMES:
            if (!opt_int(opt_arg(), &primes))
                goto end;
            break;
1662 1663
        case OPT_SECONDS:
            seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
P
Paul Yang 已提交
1664
                        = seconds.ecdh = seconds.eddsa = atoi(opt_arg());
1665 1666 1667 1668 1669 1670
            break;
        case OPT_BYTES:
            lengths_single = atoi(opt_arg());
            lengths = &lengths_single;
            size_num = 1;
            break;
A
Andy Polyakov 已提交
1671 1672 1673
        case OPT_AEAD:
            aead = 1;
            break;
1674 1675 1676 1677 1678 1679
        }
    }
    argc = opt_num_rest();
    argv = opt_rest();

    /* Remaining arguments are algorithms. */
1680
    for (; *argv; argv++) {
1681 1682 1683 1684
        if (found(*argv, doit_choices, &i)) {
            doit[i] = 1;
            continue;
        }
1685
#ifndef OPENSSL_NO_DES
1686 1687 1688 1689
        if (strcmp(*argv, "des") == 0) {
            doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
            continue;
        }
1690
#endif
1691 1692 1693 1694
        if (strcmp(*argv, "sha") == 0) {
            doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
            continue;
        }
1695
#ifndef OPENSSL_NO_RSA
1696
        if (strcmp(*argv, "openssl") == 0)
1697 1698
            continue;
        if (strcmp(*argv, "rsa") == 0) {
1699 1700
            for (loop = 0; loop < OSSL_NELEM(rsa_doit); loop++)
                rsa_doit[loop] = 1;
1701 1702 1703 1704 1705 1706
            continue;
        }
        if (found(*argv, rsa_choices, &i)) {
            rsa_doit[i] = 1;
            continue;
        }
1707
#endif
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
#ifndef OPENSSL_NO_DSA
        if (strcmp(*argv, "dsa") == 0) {
            dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
                dsa_doit[R_DSA_2048] = 1;
            continue;
        }
        if (found(*argv, dsa_choices, &i)) {
            dsa_doit[i] = 2;
            continue;
        }
1718
#endif
1719
        if (strcmp(*argv, "aes") == 0) {
1720
            doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1721 1722
            continue;
        }
1723
#ifndef OPENSSL_NO_CAMELLIA
1724
        if (strcmp(*argv, "camellia") == 0) {
1725
            doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1726 1727
            continue;
        }
1728
#endif
1729
#ifndef OPENSSL_NO_EC
1730
        if (strcmp(*argv, "ecdsa") == 0) {
1731 1732
            for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
                ecdsa_doit[loop] = 1;
1733 1734 1735 1736 1737 1738 1739
            continue;
        }
        if (found(*argv, ecdsa_choices, &i)) {
            ecdsa_doit[i] = 2;
            continue;
        }
        if (strcmp(*argv, "ecdh") == 0) {
1740 1741
            for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
                ecdh_doit[loop] = 1;
1742 1743 1744 1745 1746
            continue;
        }
        if (found(*argv, ecdh_choices, &i)) {
            ecdh_doit[i] = 2;
            continue;
1747
        }
P
Paul Yang 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756
        if (strcmp(*argv, "eddsa") == 0) {
            for (loop = 0; loop < OSSL_NELEM(eddsa_doit); loop++)
                eddsa_doit[loop] = 1;
            continue;
        }
        if (found(*argv, eddsa_choices, &i)) {
            eddsa_doit[i] = 2;
            continue;
        }
1757 1758 1759
#endif
        BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
        goto end;
1760
    }
1761

A
Andy Polyakov 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
    /* Sanity checks */
    if (aead) {
        if (evp_cipher == NULL) {
            BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
            goto end;
        } else if (!(EVP_CIPHER_flags(evp_cipher) &
                     EVP_CIPH_FLAG_AEAD_CIPHER)) {
            BIO_printf(bio_err, "%s is not an AEAD cipher\n",
                       OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
            goto end;
        }
    }
    if (multiblock) {
        if (evp_cipher == NULL) {
            BIO_printf(bio_err,"-mb can be used only with a multi-block"
                               " capable cipher\n");
            goto end;
        } else if (!(EVP_CIPHER_flags(evp_cipher) &
                     EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
            BIO_printf(bio_err, "%s is not a multi-block capable\n",
                       OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
            goto end;
        } else if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with -mb");
            goto end;
        }
    }

1790 1791
    /* Initialize the job pool if async mode is enabled */
    if (async_jobs > 0) {
1792 1793
        async_init = ASYNC_init_thread(async_jobs, async_jobs);
        if (!async_init) {
1794 1795 1796 1797 1798 1799
            BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
            goto end;
        }
    }

    loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1800 1801
    loopargs =
        app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1802 1803
    memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));

1804
    for (i = 0; i < loopargs_len; i++) {
1805 1806 1807 1808 1809 1810 1811 1812
        if (async_jobs > 0) {
            loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
            if (loopargs[i].wait_ctx == NULL) {
                BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
                goto end;
            }
        }

1813 1814 1815 1816
        buflen = lengths[size_num - 1];
        if (buflen < 36)    /* size of random vector in RSA bencmark */
            buflen = 36;
        buflen += MAX_MISALIGNMENT + 1;
P
Patrick Steuer 已提交
1817 1818 1819 1820 1821
        loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
        loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
        memset(loopargs[i].buf_malloc, 0, buflen);
        memset(loopargs[i].buf2_malloc, 0, buflen);

1822 1823 1824
        /* Align the start of buffers on a 64 byte boundary */
        loopargs[i].buf = loopargs[i].buf_malloc + misalign;
        loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
1825 1826 1827 1828
#ifndef OPENSSL_NO_EC
        loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
        loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
#endif
1829 1830
    }

1831
#ifndef NO_FORK
1832
    if (multi && do_multi(multi, size_num))
1833
        goto show_res;
1834
#endif
1835

1836
    /* Initialize the engine after the fork */
1837
    e = setup_engine(engine_id, 0);
1838

1839
    /* No parameters; turn on everything. */
P
Pauli 已提交
1840
    if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC]) {
1841
        for (i = 0; i < ALGOR_NUM; i++)
P
Pauli 已提交
1842
            if (i != D_EVP && i != D_EVP_HMAC)
1843
                doit[i] = 1;
F
FdaSilvaYY 已提交
1844
#ifndef OPENSSL_NO_RSA
1845 1846
        for (i = 0; i < RSA_NUM; i++)
            rsa_doit[i] = 1;
F
FdaSilvaYY 已提交
1847
#endif
M
Matt Caswell 已提交
1848
#ifndef OPENSSL_NO_DSA
1849 1850
        for (i = 0; i < DSA_NUM; i++)
            dsa_doit[i] = 1;
M
Matt Caswell 已提交
1851
#endif
1852
#ifndef OPENSSL_NO_EC
1853 1854 1855 1856
        for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
            ecdsa_doit[loop] = 1;
        for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
            ecdh_doit[loop] = 1;
P
Paul Yang 已提交
1857 1858
        for (loop = 0; loop < OSSL_NELEM(eddsa_doit); loop++)
            eddsa_doit[loop] = 1;
1859
#endif
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
    }
    for (i = 0; i < ALGOR_NUM; i++)
        if (doit[i])
            pr_header++;

    if (usertime == 0 && !mr)
        BIO_printf(bio_err,
                   "You have chosen to measure elapsed time "
                   "instead of user CPU time.\n");

1870
#ifndef OPENSSL_NO_RSA
1871
    for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
1872 1873 1874 1875
        if (primes > RSA_DEFAULT_PRIME_NUM) {
            /* for multi-prime RSA, skip this */
            break;
        }
1876 1877 1878 1879
        for (k = 0; k < RSA_NUM; k++) {
            const unsigned char *p;

            p = rsa_data[k];
1880 1881
            loopargs[i].rsa_key[k] =
                d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1882
            if (loopargs[i].rsa_key[k] == NULL) {
1883 1884
                BIO_printf(bio_err,
                           "internal error loading RSA key number %d\n", k);
1885 1886
                goto end;
            }
1887
        }
1888 1889 1890
    }
#endif
#ifndef OPENSSL_NO_DSA
1891
    for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
1892 1893 1894
        loopargs[i].dsa_key[0] = get_dsa(512);
        loopargs[i].dsa_key[1] = get_dsa(1024);
        loopargs[i].dsa_key[2] = get_dsa(2048);
1895
    }
1896 1897
#endif
#ifndef OPENSSL_NO_DES
1898 1899 1900
    DES_set_key_unchecked(&key, &sch);
    DES_set_key_unchecked(&key2, &sch2);
    DES_set_key_unchecked(&key3, &sch3);
1901
#endif
1902 1903 1904
    AES_set_encrypt_key(key16, 128, &aes_ks1);
    AES_set_encrypt_key(key24, 192, &aes_ks2);
    AES_set_encrypt_key(key32, 256, &aes_ks3);
1905
#ifndef OPENSSL_NO_CAMELLIA
1906 1907 1908
    Camellia_set_key(key16, 128, &camellia_ks1);
    Camellia_set_key(ckey24, 192, &camellia_ks2);
    Camellia_set_key(ckey32, 256, &camellia_ks3);
1909 1910
#endif
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
1911
    IDEA_set_encrypt_key(key16, &idea_ks);
1912 1913
#endif
#ifndef OPENSSL_NO_SEED
1914
    SEED_set_key(key16, &seed_ks);
1915 1916
#endif
#ifndef OPENSSL_NO_RC4
1917
    RC4_set_key(&rc4_ks, 16, key16);
1918 1919
#endif
#ifndef OPENSSL_NO_RC2
1920
    RC2_set_key(&rc2_ks, 16, key16, 128);
1921 1922
#endif
#ifndef OPENSSL_NO_RC5
1923
    RC5_32_set_key(&rc5_ks, 16, key16, 12);
1924 1925
#endif
#ifndef OPENSSL_NO_BF
1926
    BF_set_key(&bf_ks, 16, key16);
1927 1928
#endif
#ifndef OPENSSL_NO_CAST
1929
    CAST_set_key(&cast_ks, 16, key16);
1930 1931 1932
#endif
#ifndef SIGALRM
# ifndef OPENSSL_NO_DES
1933 1934 1935 1936 1937 1938 1939
    BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
    count = 10;
    do {
        long it;
        count *= 2;
        Time_F(START);
        for (it = count; it; it--)
1940 1941
            DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
                            (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
        d = Time_F(STOP);
    } while (d < 3);
    save_count = count;
    c[D_MD2][0] = count / 10;
    c[D_MDC2][0] = count / 10;
    c[D_MD4][0] = count;
    c[D_MD5][0] = count;
    c[D_HMAC][0] = count;
    c[D_SHA1][0] = count;
    c[D_RMD160][0] = count;
    c[D_RC4][0] = count * 5;
    c[D_CBC_DES][0] = count;
    c[D_EDE3_DES][0] = count / 3;
    c[D_CBC_IDEA][0] = count;
    c[D_CBC_SEED][0] = count;
    c[D_CBC_RC2][0] = count;
    c[D_CBC_RC5][0] = count;
    c[D_CBC_BF][0] = count;
    c[D_CBC_CAST][0] = count;
    c[D_CBC_128_AES][0] = count;
    c[D_CBC_192_AES][0] = count;
    c[D_CBC_256_AES][0] = count;
    c[D_CBC_128_CML][0] = count;
    c[D_CBC_192_CML][0] = count;
    c[D_CBC_256_CML][0] = count;
    c[D_SHA256][0] = count;
    c[D_SHA512][0] = count;
    c[D_WHIRLPOOL][0] = count;
    c[D_IGE_128_AES][0] = count;
    c[D_IGE_192_AES][0] = count;
    c[D_IGE_256_AES][0] = count;
    c[D_GHASH][0] = count;
1974
    c[D_RAND][0] = count;
1975

1976
    for (i = 1; i < size_num; i++) {
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
        long l0, l1;

        l0 = (long)lengths[0];
        l1 = (long)lengths[i];

        c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
        c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
        c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
        c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
        c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
        c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
        c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
        c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
        c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
        c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
1992
        c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1993
        c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015

        l0 = (long)lengths[i - 1];

        c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
        c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
        c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
        c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
        c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
        c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
        c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
        c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
        c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
        c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
        c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
        c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
        c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
        c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
        c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
        c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
        c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
        c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
    }
B
Bodo Möller 已提交
2016

2017
#  ifndef OPENSSL_NO_RSA
2018 2019 2020 2021 2022
    rsa_c[R_RSA_512][0] = count / 2000;
    rsa_c[R_RSA_512][1] = count / 400;
    for (i = 1; i < RSA_NUM; i++) {
        rsa_c[i][0] = rsa_c[i - 1][0] / 8;
        rsa_c[i][1] = rsa_c[i - 1][1] / 4;
F
FdaSilvaYY 已提交
2023
        if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
2024 2025 2026
            rsa_doit[i] = 0;
        else {
            if (rsa_c[i][0] == 0) {
2027
                rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2028 2029 2030 2031
                rsa_c[i][1] = 20;
            }
        }
    }
2032
#  endif
2033

2034
#  ifndef OPENSSL_NO_DSA
2035 2036 2037 2038 2039
    dsa_c[R_DSA_512][0] = count / 1000;
    dsa_c[R_DSA_512][1] = count / 1000 / 2;
    for (i = 1; i < DSA_NUM; i++) {
        dsa_c[i][0] = dsa_c[i - 1][0] / 4;
        dsa_c[i][1] = dsa_c[i - 1][1] / 4;
F
FdaSilvaYY 已提交
2040
        if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
2041 2042
            dsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2043
            if (dsa_c[i][0] == 0) {
2044
                dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
2045 2046 2047 2048
                dsa_c[i][1] = 1;
            }
        }
    }
2049
#  endif
2050

2051
#  ifndef OPENSSL_NO_EC
2052 2053 2054 2055 2056
    ecdsa_c[R_EC_P160][0] = count / 1000;
    ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
    for (i = R_EC_P192; i <= R_EC_P521; i++) {
        ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
        ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
F
FdaSilvaYY 已提交
2057
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2058 2059
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2060
            if (ecdsa_c[i][0] == 0) {
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
                ecdsa_c[i][0] = 1;
                ecdsa_c[i][1] = 1;
            }
        }
    }
    ecdsa_c[R_EC_K163][0] = count / 1000;
    ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
    for (i = R_EC_K233; i <= R_EC_K571; i++) {
        ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
        ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
F
FdaSilvaYY 已提交
2071
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2072 2073
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2074
            if (ecdsa_c[i][0] == 0) {
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
                ecdsa_c[i][0] = 1;
                ecdsa_c[i][1] = 1;
            }
        }
    }
    ecdsa_c[R_EC_B163][0] = count / 1000;
    ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
    for (i = R_EC_B233; i <= R_EC_B571; i++) {
        ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
        ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
F
FdaSilvaYY 已提交
2085
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
2086 2087
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2088
            if (ecdsa_c[i][0] == 0) {
2089 2090 2091 2092 2093
                ecdsa_c[i][0] = 1;
                ecdsa_c[i][1] = 1;
            }
        }
    }
2094

2095 2096 2097
    ecdh_c[R_EC_P160][0] = count / 1000;
    for (i = R_EC_P192; i <= R_EC_P521; i++) {
        ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
F
FdaSilvaYY 已提交
2098
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2099 2100
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2101
            if (ecdh_c[i][0] == 0) {
2102 2103 2104 2105 2106 2107 2108
                ecdh_c[i][0] = 1;
            }
        }
    }
    ecdh_c[R_EC_K163][0] = count / 1000;
    for (i = R_EC_K233; i <= R_EC_K571; i++) {
        ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
F
FdaSilvaYY 已提交
2109
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2110 2111
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2112
            if (ecdh_c[i][0] == 0) {
2113 2114 2115 2116 2117 2118 2119
                ecdh_c[i][0] = 1;
            }
        }
    }
    ecdh_c[R_EC_B163][0] = count / 1000;
    for (i = R_EC_B233; i <= R_EC_B571; i++) {
        ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
F
FdaSilvaYY 已提交
2120
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2121 2122
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
2123
            if (ecdh_c[i][0] == 0) {
2124 2125 2126 2127
                ecdh_c[i][0] = 1;
            }
        }
    }
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
    /* repeated code good to factorize */
    ecdh_c[R_EC_BRP256R1][0] = count / 1000;
    for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
        ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
            ecdh_doit[i] = 0;
        else {
            if (ecdh_c[i][0] == 0) {
                ecdh_c[i][0] = 1;
            }
        }
    }
    ecdh_c[R_EC_BRP256T1][0] = count / 1000;
    for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
        ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
            ecdh_doit[i] = 0;
        else {
            if (ecdh_c[i][0] == 0) {
                ecdh_c[i][0] = 1;
            }
        }
    }
    /* default iteration count for the last two EC Curves */
    ecdh_c[R_EC_X25519][0] = count / 1800;
    ecdh_c[R_EC_X448][0] = count / 7200;
P
Paul Yang 已提交
2154 2155 2156

    eddsa_c[R_EC_Ed25519][0] = count / 1800;
    eddsa_c[R_EC_Ed448][0] = count / 7200;
2157
#  endif
B
Bodo Möller 已提交
2158

2159
# else
2160 2161
/* not worth fixing */
#  error "You cannot disable DES on systems without SIGALRM."
2162
# endif                         /* OPENSSL_NO_DES */
2163 2164
#elif SIGALRM > 0
    signal(SIGALRM, alarmed);
2165
#endif                          /* SIGALRM */
2166

2167
#ifndef OPENSSL_NO_MD2
2168
    if (doit[D_MD2]) {
2169 2170 2171
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
                          seconds.sym);
2172
            Time_F(START);
2173
            count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
2174
            d = Time_F(STOP);
2175
            print_result(D_MD2, testnum, count, d);
2176 2177
        }
    }
2178 2179
#endif
#ifndef OPENSSL_NO_MDC2
2180
    if (doit[D_MDC2]) {
2181 2182 2183
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
                          seconds.sym);
2184
            Time_F(START);
2185
            count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
2186
            d = Time_F(STOP);
2187
            print_result(D_MDC2, testnum, count, d);
2188 2189
        }
    }
2190
#endif
2191

2192
#ifndef OPENSSL_NO_MD4
2193
    if (doit[D_MD4]) {
2194 2195 2196
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
                          seconds.sym);
2197
            Time_F(START);
2198
            count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
2199
            d = Time_F(STOP);
2200
            print_result(D_MD4, testnum, count, d);
2201 2202
        }
    }
2203
#endif
2204

2205
#ifndef OPENSSL_NO_MD5
2206
    if (doit[D_MD5]) {
2207 2208 2209
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
                          seconds.sym);
2210
            Time_F(START);
2211
            count = run_benchmark(async_jobs, MD5_loop, loopargs);
2212
            d = Time_F(STOP);
2213
            print_result(D_MD5, testnum, count, d);
2214 2215
        }
    }
2216

2217
    if (doit[D_HMAC]) {
F
FdaSilvaYY 已提交
2218
        static const char hmac_key[] = "This is a key...";
2219 2220
        int len = strlen(hmac_key);

2221
        for (i = 0; i < loopargs_len; i++) {
2222 2223 2224 2225 2226
            loopargs[i].hctx = HMAC_CTX_new();
            if (loopargs[i].hctx == NULL) {
                BIO_printf(bio_err, "HMAC malloc failure, exiting...");
                exit(1);
            }
2227

2228
            HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2229
        }
2230 2231 2232
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
                          seconds.sym);
2233
            Time_F(START);
2234
            count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2235
            d = Time_F(STOP);
2236 2237
            print_result(D_HMAC, testnum, count, d);
        }
2238
        for (i = 0; i < loopargs_len; i++) {
2239
            HMAC_CTX_free(loopargs[i].hctx);
2240 2241
        }
    }
2242
#endif
2243
    if (doit[D_SHA1]) {
2244 2245 2246
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
                          seconds.sym);
2247
            Time_F(START);
2248
            count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2249
            d = Time_F(STOP);
2250
            print_result(D_SHA1, testnum, count, d);
2251 2252 2253
        }
    }
    if (doit[D_SHA256]) {
2254
        for (testnum = 0; testnum < size_num; testnum++) {
2255
            print_message(names[D_SHA256], c[D_SHA256][testnum],
2256
                          lengths[testnum], seconds.sym);
2257
            Time_F(START);
2258
            count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2259
            d = Time_F(STOP);
2260
            print_result(D_SHA256, testnum, count, d);
2261 2262 2263
        }
    }
    if (doit[D_SHA512]) {
2264
        for (testnum = 0; testnum < size_num; testnum++) {
2265
            print_message(names[D_SHA512], c[D_SHA512][testnum],
2266
                          lengths[testnum], seconds.sym);
2267
            Time_F(START);
2268
            count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2269
            d = Time_F(STOP);
2270
            print_result(D_SHA512, testnum, count, d);
2271 2272
        }
    }
2273
#ifndef OPENSSL_NO_WHIRLPOOL
2274
    if (doit[D_WHIRLPOOL]) {
2275
        for (testnum = 0; testnum < size_num; testnum++) {
2276
            print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2277
                          lengths[testnum], seconds.sym);
2278
            Time_F(START);
2279
            count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2280
            d = Time_F(STOP);
2281
            print_result(D_WHIRLPOOL, testnum, count, d);
2282 2283
        }
    }
2284
#endif
2285

2286
#ifndef OPENSSL_NO_RMD160
2287
    if (doit[D_RMD160]) {
2288
        for (testnum = 0; testnum < size_num; testnum++) {
2289
            print_message(names[D_RMD160], c[D_RMD160][testnum],
2290
                          lengths[testnum], seconds.sym);
2291
            Time_F(START);
2292
            count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2293
            d = Time_F(STOP);
2294
            print_result(D_RMD160, testnum, count, d);
2295 2296
        }
    }
2297 2298
#endif
#ifndef OPENSSL_NO_RC4
2299
    if (doit[D_RC4]) {
2300 2301 2302
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
                          seconds.sym);
2303
            Time_F(START);
2304
            count = run_benchmark(async_jobs, RC4_loop, loopargs);
2305
            d = Time_F(STOP);
2306
            print_result(D_RC4, testnum, count, d);
2307 2308
        }
    }
2309 2310
#endif
#ifndef OPENSSL_NO_DES
2311
    if (doit[D_CBC_DES]) {
2312
        for (testnum = 0; testnum < size_num; testnum++) {
2313
            print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2314
                          lengths[testnum], seconds.sym);
2315
            Time_F(START);
2316
            count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2317
            d = Time_F(STOP);
2318
            print_result(D_CBC_DES, testnum, count, d);
2319 2320
        }
    }
2321

2322
    if (doit[D_EDE3_DES]) {
2323
        for (testnum = 0; testnum < size_num; testnum++) {
2324
            print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2325
                          lengths[testnum], seconds.sym);
2326
            Time_F(START);
2327 2328
            count =
                run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2329
            d = Time_F(STOP);
2330
            print_result(D_EDE3_DES, testnum, count, d);
2331 2332
        }
    }
2333
#endif
M
Matt Caswell 已提交
2334

2335
    if (doit[D_CBC_128_AES]) {
2336
        for (testnum = 0; testnum < size_num; testnum++) {
2337
            print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2338
                          lengths[testnum], seconds.sym);
2339
            Time_F(START);
2340 2341
            count =
                run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2342
            d = Time_F(STOP);
2343
            print_result(D_CBC_128_AES, testnum, count, d);
2344 2345 2346
        }
    }
    if (doit[D_CBC_192_AES]) {
2347
        for (testnum = 0; testnum < size_num; testnum++) {
2348
            print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2349
                          lengths[testnum], seconds.sym);
2350
            Time_F(START);
2351 2352
            count =
                run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2353
            d = Time_F(STOP);
2354
            print_result(D_CBC_192_AES, testnum, count, d);
2355 2356 2357
        }
    }
    if (doit[D_CBC_256_AES]) {
2358
        for (testnum = 0; testnum < size_num; testnum++) {
2359
            print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2360
                          lengths[testnum], seconds.sym);
2361
            Time_F(START);
2362 2363
            count =
                run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2364
            d = Time_F(STOP);
2365
            print_result(D_CBC_256_AES, testnum, count, d);
2366 2367
        }
    }
B
Ben Laurie 已提交
2368

2369
    if (doit[D_IGE_128_AES]) {
2370
        for (testnum = 0; testnum < size_num; testnum++) {
2371
            print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2372
                          lengths[testnum], seconds.sym);
2373
            Time_F(START);
2374 2375
            count =
                run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2376
            d = Time_F(STOP);
2377
            print_result(D_IGE_128_AES, testnum, count, d);
2378 2379 2380
        }
    }
    if (doit[D_IGE_192_AES]) {
2381
        for (testnum = 0; testnum < size_num; testnum++) {
2382
            print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2383
                          lengths[testnum], seconds.sym);
2384
            Time_F(START);
2385 2386
            count =
                run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2387
            d = Time_F(STOP);
2388
            print_result(D_IGE_192_AES, testnum, count, d);
2389 2390 2391
        }
    }
    if (doit[D_IGE_256_AES]) {
2392
        for (testnum = 0; testnum < size_num; testnum++) {
2393
            print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2394
                          lengths[testnum], seconds.sym);
2395
            Time_F(START);
2396 2397
            count =
                run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2398
            d = Time_F(STOP);
2399
            print_result(D_IGE_256_AES, testnum, count, d);
2400 2401 2402
        }
    }
    if (doit[D_GHASH]) {
2403
        for (i = 0; i < loopargs_len; i++) {
2404 2405 2406 2407
            loopargs[i].gcm_ctx =
                CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
            CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
                                (unsigned char *)"0123456789ab", 12);
2408
        }
2409

2410
        for (testnum = 0; testnum < size_num; testnum++) {
2411
            print_message(names[D_GHASH], c[D_GHASH][testnum],
2412
                          lengths[testnum], seconds.sym);
2413
            Time_F(START);
2414
            count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2415
            d = Time_F(STOP);
2416
            print_result(D_GHASH, testnum, count, d);
2417
        }
2418
        for (i = 0; i < loopargs_len; i++)
2419
            CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2420
    }
2421
#ifndef OPENSSL_NO_CAMELLIA
2422
    if (doit[D_CBC_128_CML]) {
2423 2424 2425 2426 2427
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_128_CML]);
            doit[D_CBC_128_CML] = 0;
        }
2428
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2429
            print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2430
                          lengths[testnum], seconds.sym);
2431
            Time_F(START);
2432 2433
            for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2434
                                     (size_t)lengths[testnum], &camellia_ks1,
2435 2436
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2437
            print_result(D_CBC_128_CML, testnum, count, d);
2438 2439 2440
        }
    }
    if (doit[D_CBC_192_CML]) {
2441 2442 2443 2444 2445
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_192_CML]);
            doit[D_CBC_192_CML] = 0;
        }
2446
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2447
            print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2448
                          lengths[testnum], seconds.sym);
2449 2450 2451 2452
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2453
            Time_F(START);
2454 2455
            for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2456
                                     (size_t)lengths[testnum], &camellia_ks2,
2457 2458
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2459
            print_result(D_CBC_192_CML, testnum, count, d);
2460 2461 2462
        }
    }
    if (doit[D_CBC_256_CML]) {
2463 2464 2465 2466 2467
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_256_CML]);
            doit[D_CBC_256_CML] = 0;
        }
2468
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2469
            print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2470
                          lengths[testnum], seconds.sym);
2471
            Time_F(START);
2472 2473
            for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2474
                                     (size_t)lengths[testnum], &camellia_ks3,
2475 2476
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2477
            print_result(D_CBC_256_CML, testnum, count, d);
2478 2479
        }
    }
2480 2481
#endif
#ifndef OPENSSL_NO_IDEA
2482
    if (doit[D_CBC_IDEA]) {
2483 2484 2485 2486 2487
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_IDEA]);
            doit[D_CBC_IDEA] = 0;
        }
2488
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2489
            print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2490
                          lengths[testnum], seconds.sym);
2491
            Time_F(START);
2492
            for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
R
Rich Salz 已提交
2493
                IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2494
                                 (size_t)lengths[testnum], &idea_ks,
2495 2496
                                 iv, IDEA_ENCRYPT);
            d = Time_F(STOP);
2497
            print_result(D_CBC_IDEA, testnum, count, d);
2498 2499
        }
    }
2500 2501
#endif
#ifndef OPENSSL_NO_SEED
2502
    if (doit[D_CBC_SEED]) {
2503 2504 2505 2506 2507
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_SEED]);
            doit[D_CBC_SEED] = 0;
        }
2508
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2509
            print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2510
                          lengths[testnum], seconds.sym);
2511
            Time_F(START);
2512 2513
            for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
                SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2514
                                 (size_t)lengths[testnum], &seed_ks, iv, 1);
2515
            d = Time_F(STOP);
2516
            print_result(D_CBC_SEED, testnum, count, d);
2517 2518
        }
    }
2519 2520
#endif
#ifndef OPENSSL_NO_RC2
2521
    if (doit[D_CBC_RC2]) {
2522 2523 2524 2525 2526
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_RC2]);
            doit[D_CBC_RC2] = 0;
        }
2527
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2528
            print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2529
                          lengths[testnum], seconds.sym);
2530 2531 2532 2533
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2534
            Time_F(START);
2535 2536
            for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
                RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2537
                                (size_t)lengths[testnum], &rc2_ks,
2538 2539
                                iv, RC2_ENCRYPT);
            d = Time_F(STOP);
2540
            print_result(D_CBC_RC2, testnum, count, d);
2541 2542
        }
    }
2543 2544
#endif
#ifndef OPENSSL_NO_RC5
2545
    if (doit[D_CBC_RC5]) {
2546 2547 2548 2549 2550
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_RC5]);
            doit[D_CBC_RC5] = 0;
        }
2551
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2552
            print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2553
                          lengths[testnum], seconds.sym);
2554 2555 2556 2557
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2558
            Time_F(START);
2559 2560
            for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
                RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2561
                                   (size_t)lengths[testnum], &rc5_ks,
2562 2563
                                   iv, RC5_ENCRYPT);
            d = Time_F(STOP);
2564
            print_result(D_CBC_RC5, testnum, count, d);
2565 2566
        }
    }
2567 2568
#endif
#ifndef OPENSSL_NO_BF
2569
    if (doit[D_CBC_BF]) {
2570 2571 2572 2573 2574
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_BF]);
            doit[D_CBC_BF] = 0;
        }
2575
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2576
            print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2577
                          lengths[testnum], seconds.sym);
2578
            Time_F(START);
2579 2580
            for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
                BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2581
                               (size_t)lengths[testnum], &bf_ks,
2582 2583
                               iv, BF_ENCRYPT);
            d = Time_F(STOP);
2584
            print_result(D_CBC_BF, testnum, count, d);
2585 2586
        }
    }
2587 2588
#endif
#ifndef OPENSSL_NO_CAST
2589
    if (doit[D_CBC_CAST]) {
2590 2591 2592 2593 2594
        if (async_jobs > 0) {
            BIO_printf(bio_err, "Async mode is not supported with %s\n",
                       names[D_CBC_CAST]);
            doit[D_CBC_CAST] = 0;
        }
2595
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2596
            print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2597
                          lengths[testnum], seconds.sym);
2598
            Time_F(START);
2599 2600
            for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
                CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2601
                                 (size_t)lengths[testnum], &cast_ks,
2602 2603
                                 iv, CAST_ENCRYPT);
            d = Time_F(STOP);
2604
            print_result(D_CBC_CAST, testnum, count, d);
2605 2606
        }
    }
2607
#endif
2608
    if (doit[D_RAND]) {
2609 2610 2611
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
                          seconds.sym);
2612 2613 2614 2615 2616 2617
            Time_F(START);
            count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
            d = Time_F(STOP);
            print_result(D_RAND, testnum, count, d);
        }
    }
2618

2619
    if (doit[D_EVP]) {
A
Andy Polyakov 已提交
2620 2621 2622 2623 2624 2625 2626
        if (evp_cipher != NULL) {
            int (*loopfunc)(void *args) = EVP_Update_loop;

            if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
                               EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
                multiblock_speed(evp_cipher, lengths_single, &seconds);
                ret = 0;
2627 2628
                goto end;
            }
A
Andy Polyakov 已提交
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640

            names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));

            if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
                loopfunc = EVP_Update_loop_ccm;
            } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
                                EVP_CIPH_FLAG_AEAD_CIPHER)) {
                loopfunc = EVP_Update_loop_aead;
                if (lengths == lengths_list) {
                    lengths = aead_lengths_list;
                    size_num = OSSL_NELEM(aead_lengths_list);
                }
2641
            }
2642

A
Andy Polyakov 已提交
2643
            for (testnum = 0; testnum < size_num; testnum++) {
2644 2645
                print_message(names[D_EVP], save_count, lengths[testnum],
                              seconds.sym);
2646 2647 2648

                for (k = 0; k < loopargs_len; k++) {
                    loopargs[k].ctx = EVP_CIPHER_CTX_new();
2649 2650 2651
                    EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL, NULL,
                                      iv, decrypt ? 0 : 1);

2652
                    EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2653 2654 2655 2656 2657 2658 2659

                    keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
                    loopargs[k].key = app_malloc(keylen, "evp_cipher key");
                    EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
                    EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
                                      loopargs[k].key, NULL, -1);
                    OPENSSL_clear_free(loopargs[k].key, keylen);
2660
                }
2661 2662

                Time_F(START);
2663
                count = run_benchmark(async_jobs, loopfunc, loopargs);
2664
                d = Time_F(STOP);
2665 2666 2667
                for (k = 0; k < loopargs_len; k++) {
                    EVP_CIPHER_CTX_free(loopargs[k].ctx);
                }
A
Andy Polyakov 已提交
2668
                print_result(D_EVP, testnum, count, d);
2669
            }
A
Andy Polyakov 已提交
2670 2671 2672 2673
        } else if (evp_md != NULL) {
            names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));

            for (testnum = 0; testnum < size_num; testnum++) {
2674 2675
                print_message(names[D_EVP], save_count, lengths[testnum],
                              seconds.sym);
2676
                Time_F(START);
2677
                count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2678
                d = Time_F(STOP);
A
Andy Polyakov 已提交
2679
                print_result(D_EVP, testnum, count, d);
2680 2681 2682
            }
        }
    }
2683

P
Pauli 已提交
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
    if (doit[D_EVP_HMAC]) {
        if (evp_hmac_md != NULL) {
            const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
            evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
                                       "HMAC name");
            sprintf(evp_hmac_name, "HMAC(%s)", md_name);
            names[D_EVP_HMAC] = evp_hmac_name;

            for (testnum = 0; testnum < size_num; testnum++) {
                print_message(names[D_EVP_HMAC], save_count, lengths[testnum],
                              seconds.sym);
                Time_F(START);
                count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
                d = Time_F(STOP);
                print_result(D_EVP_HMAC, testnum, count, d);
            }
        }
    }

2703
    for (i = 0; i < loopargs_len; i++)
2704 2705
        if (RAND_bytes(loopargs[i].buf, 36) <= 0)
            goto end;
2706

2707
#ifndef OPENSSL_NO_RSA
2708 2709 2710
    for (testnum = 0; testnum < RSA_NUM; testnum++) {
        int st = 0;
        if (!rsa_doit[testnum])
2711
            continue;
2712
        for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
            if (primes > 2) {
                /* we haven't set keys yet,  generate multi-prime RSA keys */
                BIGNUM *bn = BN_new();

                if (bn == NULL)
                    goto end;
                if (!BN_set_word(bn, RSA_F4)) {
                    BN_free(bn);
                    goto end;
                }

                BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
                           rsa_choices[testnum].name);

                loopargs[i].rsa_key[testnum] = RSA_new();
                if (loopargs[i].rsa_key[testnum] == NULL) {
                    BN_free(bn);
                    goto end;
                }

                if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
                                                  rsa_bits[testnum],
                                                  primes, bn, NULL)) {
                    BN_free(bn);
                    goto end;
                }
                BN_free(bn);
            }
2741
            st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2742
                          &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2743 2744 2745
            if (st == 0)
                break;
        }
2746
        if (st == 0) {
2747 2748 2749 2750 2751 2752
            BIO_printf(bio_err,
                       "RSA sign failure.  No RSA sign will be done.\n");
            ERR_print_errors(bio_err);
            rsa_count = 1;
        } else {
            pkey_print_message("private", "rsa",
2753
                               rsa_c[testnum][0], rsa_bits[testnum],
2754
                               seconds.rsa);
2755
            /* RSA_blinding_on(rsa_key[testnum],NULL); */
2756
            Time_F(START);
2757
            count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2758 2759 2760
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R1:%ld:%d:%.2f\n"
2761
                       : "%ld %u bits private RSA's in %.2fs\n",
2762
                       count, rsa_bits[testnum], d);
2763
            rsa_results[testnum][0] = (double)count / d;
2764 2765
            rsa_count = count;
        }
2766

2767 2768
        for (i = 0; i < loopargs_len; i++) {
            st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2769
                            loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2770 2771 2772
            if (st <= 0)
                break;
        }
2773
        if (st <= 0) {
2774 2775 2776
            BIO_printf(bio_err,
                       "RSA verify failure.  No RSA verify will be done.\n");
            ERR_print_errors(bio_err);
2777
            rsa_doit[testnum] = 0;
2778 2779
        } else {
            pkey_print_message("public", "rsa",
2780
                               rsa_c[testnum][1], rsa_bits[testnum],
2781
                               seconds.rsa);
2782
            Time_F(START);
2783
            count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2784 2785 2786
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R2:%ld:%d:%.2f\n"
2787
                       : "%ld %u bits public RSA's in %.2fs\n",
2788
                       count, rsa_bits[testnum], d);
2789
            rsa_results[testnum][1] = (double)count / d;
2790
        }
2791

2792 2793
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2794 2795
            for (testnum++; testnum < RSA_NUM; testnum++)
                rsa_doit[testnum] = 0;
2796 2797
        }
    }
F
FdaSilvaYY 已提交
2798
#endif                          /* OPENSSL_NO_RSA */
2799

2800
    for (i = 0; i < loopargs_len; i++)
2801 2802
        if (RAND_bytes(loopargs[i].buf, 36) <= 0)
            goto end;
2803

2804
#ifndef OPENSSL_NO_DSA
2805 2806 2807
    for (testnum = 0; testnum < DSA_NUM; testnum++) {
        int st = 0;
        if (!dsa_doit[testnum])
2808 2809
            continue;

2810 2811
        /* DSA_generate_key(dsa_key[testnum]); */
        /* DSA_sign_setup(dsa_key[testnum],NULL); */
2812 2813
        for (i = 0; i < loopargs_len; i++) {
            st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2814
                          &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2815 2816 2817
            if (st == 0)
                break;
        }
2818
        if (st == 0) {
2819 2820 2821 2822 2823 2824
            BIO_printf(bio_err,
                       "DSA sign failure.  No DSA sign will be done.\n");
            ERR_print_errors(bio_err);
            rsa_count = 1;
        } else {
            pkey_print_message("sign", "dsa",
2825
                               dsa_c[testnum][0], dsa_bits[testnum],
2826
                               seconds.dsa);
2827
            Time_F(START);
2828
            count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2829 2830
            d = Time_F(STOP);
            BIO_printf(bio_err,
2831 2832
                       mr ? "+R3:%ld:%u:%.2f\n"
                       : "%ld %u bits DSA signs in %.2fs\n",
2833
                       count, dsa_bits[testnum], d);
2834
            dsa_results[testnum][0] = (double)count / d;
2835 2836
            rsa_count = count;
        }
B
Bodo Möller 已提交
2837

2838 2839
        for (i = 0; i < loopargs_len; i++) {
            st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2840
                            loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2841 2842 2843
            if (st <= 0)
                break;
        }
2844
        if (st <= 0) {
2845 2846 2847
            BIO_printf(bio_err,
                       "DSA verify failure.  No DSA verify will be done.\n");
            ERR_print_errors(bio_err);
2848
            dsa_doit[testnum] = 0;
2849 2850
        } else {
            pkey_print_message("verify", "dsa",
2851
                               dsa_c[testnum][1], dsa_bits[testnum],
2852
                               seconds.dsa);
2853
            Time_F(START);
2854
            count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2855 2856
            d = Time_F(STOP);
            BIO_printf(bio_err,
2857 2858
                       mr ? "+R4:%ld:%u:%.2f\n"
                       : "%ld %u bits DSA verify in %.2fs\n",
2859
                       count, dsa_bits[testnum], d);
2860
            dsa_results[testnum][1] = (double)count / d;
2861
        }
B
Bodo Möller 已提交
2862

2863 2864
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2865 2866
            for (testnum++; testnum < DSA_NUM; testnum++)
                dsa_doit[testnum] = 0;
2867 2868
        }
    }
F
FdaSilvaYY 已提交
2869
#endif                          /* OPENSSL_NO_DSA */
B
Bodo Möller 已提交
2870

2871
#ifndef OPENSSL_NO_EC
2872
    for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
2873
        int st = 1;
2874

2875
        if (!ecdsa_doit[testnum])
2876
            continue;           /* Ignore Curve */
2877
        for (i = 0; i < loopargs_len; i++) {
2878
            loopargs[i].ecdsa[testnum] =
2879
                EC_KEY_new_by_curve_name(test_curves[testnum].nid);
2880 2881 2882 2883 2884 2885
            if (loopargs[i].ecdsa[testnum] == NULL) {
                st = 0;
                break;
            }
        }
        if (st == 0) {
2886 2887 2888 2889
            BIO_printf(bio_err, "ECDSA failure.\n");
            ERR_print_errors(bio_err);
            rsa_count = 1;
        } else {
2890 2891 2892 2893 2894
            for (i = 0; i < loopargs_len; i++) {
                EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
                /* Perform ECDSA signature test */
                EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
                st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
2895 2896
                                &loopargs[i].siglen,
                                loopargs[i].ecdsa[testnum]);
2897 2898 2899
                if (st == 0)
                    break;
            }
2900
            if (st == 0) {
2901 2902 2903 2904 2905 2906
                BIO_printf(bio_err,
                           "ECDSA sign failure.  No ECDSA sign will be done.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
            } else {
                pkey_print_message("sign", "ecdsa",
2907
                                   ecdsa_c[testnum][0],
2908
                                   test_curves[testnum].bits, seconds.ecdsa);
2909
                Time_F(START);
2910
                count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2911 2912 2913
                d = Time_F(STOP);

                BIO_printf(bio_err,
2914 2915 2916
                           mr ? "+R5:%ld:%u:%.2f\n" :
                           "%ld %u bits ECDSA signs in %.2fs \n",
                           count, test_curves[testnum].bits, d);
2917
                ecdsa_results[testnum][0] = (double)count / d;
2918 2919 2920 2921
                rsa_count = count;
            }

            /* Perform ECDSA verification test */
2922 2923
            for (i = 0; i < loopargs_len; i++) {
                st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2924 2925
                                  loopargs[i].siglen,
                                  loopargs[i].ecdsa[testnum]);
2926 2927 2928
                if (st != 1)
                    break;
            }
2929
            if (st != 1) {
2930 2931 2932
                BIO_printf(bio_err,
                           "ECDSA verify failure.  No ECDSA verify will be done.\n");
                ERR_print_errors(bio_err);
2933
                ecdsa_doit[testnum] = 0;
2934 2935
            } else {
                pkey_print_message("verify", "ecdsa",
2936
                                   ecdsa_c[testnum][1],
2937
                                   test_curves[testnum].bits, seconds.ecdsa);
2938
                Time_F(START);
2939
                count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2940 2941
                d = Time_F(STOP);
                BIO_printf(bio_err,
2942 2943 2944
                           mr ? "+R6:%ld:%u:%.2f\n"
                           : "%ld %u bits ECDSA verify in %.2fs\n",
                           count, test_curves[testnum].bits, d);
2945
                ecdsa_results[testnum][1] = (double)count / d;
2946 2947 2948 2949
            }

            if (rsa_count <= 1) {
                /* if longer than 10s, don't do any more */
2950
                for (testnum++; testnum < ECDSA_NUM; testnum++)
2951
                    ecdsa_doit[testnum] = 0;
2952 2953 2954
            }
        }
    }
2955

2956
    for (testnum = 0; testnum < EC_NUM; testnum++) {
2957 2958
        int ecdh_checks = 1;

2959
        if (!ecdh_doit[testnum])
2960
            continue;
2961

2962
        for (i = 0; i < loopargs_len; i++) {
N
Nicola Tuveri 已提交
2963
            EVP_PKEY_CTX *kctx = NULL;
2964
            EVP_PKEY_CTX *test_ctx = NULL;
N
Nicola Tuveri 已提交
2965 2966 2967
            EVP_PKEY_CTX *ctx = NULL;
            EVP_PKEY *key_A = NULL;
            EVP_PKEY *key_B = NULL;
2968
            size_t outlen;
2969
            size_t test_outlen;
2970

2971 2972 2973 2974 2975 2976 2977
            /* Ensure that the error queue is empty */
            if (ERR_peek_error()) {
                BIO_printf(bio_err,
                           "WARNING: the error queue contains previous unhandled errors.\n");
                ERR_print_errors(bio_err);
            }

2978 2979 2980 2981 2982 2983
            /* Let's try to create a ctx directly from the NID: this works for
             * curves like Curve25519 that are not implemented through the low
             * level EC interface.
             * If this fails we try creating a EVP_PKEY_EC generic param ctx,
             * then we set the curve by NID before deriving the actual keygen
             * ctx for that specific curve. */
2984
            kctx = EVP_PKEY_CTX_new_id(test_curves[testnum].nid, NULL); /* keygen ctx from NID */
2985
            if (!kctx) {
2986 2987 2988
                EVP_PKEY_CTX *pctx = NULL;
                EVP_PKEY *params = NULL;

2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007
                /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
                 * "int_ctx_new:unsupported algorithm" error was added to the
                 * error queue.
                 * We remove it from the error queue as we are handling it. */
                unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
                if (error == ERR_peek_last_error() && /* oldest and latest errors match */
                    /* check that the error origin matches */
                    ERR_GET_LIB(error) == ERR_LIB_EVP &&
                    ERR_GET_FUNC(error) == EVP_F_INT_CTX_NEW &&
                    ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
                    ERR_get_error(); /* pop error from queue */
                if (ERR_peek_error()) {
                    BIO_printf(bio_err,
                               "Unhandled error in the error queue during ECDH init.\n");
                    ERR_print_errors(bio_err);
                    rsa_count = 1;
                    break;
                }

3008 3009 3010 3011 3012 3013 3014
                if (            /* Create the context for parameter generation */
                       !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
                       /* Initialise the parameter generation */
                       !EVP_PKEY_paramgen_init(pctx) ||
                       /* Set the curve by NID */
                       !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
                                                               test_curves
3015
                                                               [testnum].nid) ||
3016
                       /* Create the parameter object params */
N
Nicola Tuveri 已提交
3017
                       !EVP_PKEY_paramgen(pctx, &params)) {
3018
                    ecdh_checks = 0;
3019
                    BIO_printf(bio_err, "ECDH EC params init failure.\n");
3020
                    ERR_print_errors(bio_err);
3021
                    rsa_count = 1;
3022
                    break;
3023
                }
3024 3025 3026
                /* Create the context for the key generation */
                kctx = EVP_PKEY_CTX_new(params, NULL);

3027 3028 3029 3030
                EVP_PKEY_free(params);
                params = NULL;
                EVP_PKEY_CTX_free(pctx);
                pctx = NULL;
A
Andrea Grandi 已提交
3031
            }
3032
            if (kctx == NULL ||      /* keygen ctx is not null */
N
Nicola Tuveri 已提交
3033
                !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
3034 3035 3036 3037 3038
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH keygen failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
3039
            }
3040

3041 3042 3043 3044 3045 3046
            if (!EVP_PKEY_keygen(kctx, &key_A) || /* generate secret key A */
                !EVP_PKEY_keygen(kctx, &key_B) || /* generate secret key B */
                !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
                !EVP_PKEY_derive_init(ctx) || /* init derivation ctx */
                !EVP_PKEY_derive_set_peer(ctx, key_B) || /* set peer pubkey in ctx */
                !EVP_PKEY_derive(ctx, NULL, &outlen) || /* determine max length */
3047
                outlen == 0 ||  /* ensure outlen is a valid size */
N
Nicola Tuveri 已提交
3048
                outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
3049 3050 3051 3052 3053 3054 3055
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH key generation failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
            /* Here we perform a test run, comparing the output of a*B and b*A;
             * we try this here and assume that further EVP_PKEY_derive calls
             * never fail, so we can skip checks in the actually benchmarked
             * code, for maximum performance. */
            if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
                !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
                !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
                !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
                !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
                !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
                test_outlen != outlen /* compare output length */ ) {
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH computation failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }
3073 3074 3075 3076 3077

            /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
            if (CRYPTO_memcmp(loopargs[i].secret_a,
                              loopargs[i].secret_b, outlen)) {
                ecdh_checks = 0;
3078 3079 3080 3081 3082 3083
                BIO_printf(bio_err, "ECDH computations don't match.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

3084
            loopargs[i].ecdh_ctx[testnum] = ctx;
3085
            loopargs[i].outlen[testnum] = outlen;
3086

3087 3088
            EVP_PKEY_free(key_A);
            EVP_PKEY_free(key_B);
3089 3090
            EVP_PKEY_CTX_free(kctx);
            kctx = NULL;
3091 3092
            EVP_PKEY_CTX_free(test_ctx);
            test_ctx = NULL;
3093 3094 3095
        }
        if (ecdh_checks != 0) {
            pkey_print_message("", "ecdh",
3096
                               ecdh_c[testnum][0],
3097
                               test_curves[testnum].bits, seconds.ecdh);
3098
            Time_F(START);
3099 3100
            count =
                run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
3101 3102
            d = Time_F(STOP);
            BIO_printf(bio_err,
3103
                       mr ? "+R7:%ld:%d:%.2f\n" :
3104 3105
                       "%ld %u-bits ECDH ops in %.2fs\n", count,
                       test_curves[testnum].bits, d);
3106
            ecdh_results[testnum][0] = (double)count / d;
3107
            rsa_count = count;
3108
        }
B
Bodo Möller 已提交
3109

3110 3111
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
3112
            for (testnum++; testnum < OSSL_NELEM(ecdh_doit); testnum++)
3113
                ecdh_doit[testnum] = 0;
3114 3115
        }
    }
P
Paul Yang 已提交
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155

    for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
        int st = 1;
        EVP_PKEY *ed_pkey = NULL;
        EVP_PKEY_CTX *ed_pctx = NULL;

        if (!eddsa_doit[testnum])
            continue;           /* Ignore Curve */
        for (i = 0; i < loopargs_len; i++) {
            loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
            if (loopargs[i].eddsa_ctx[testnum] == NULL) {
                st = 0;
                break;
            }

            if ((ed_pctx = EVP_PKEY_CTX_new_id(test_ed_curves[testnum].nid, NULL))
                    == NULL
                || !EVP_PKEY_keygen_init(ed_pctx)
                || !EVP_PKEY_keygen(ed_pctx, &ed_pkey)) {
                st = 0;
                EVP_PKEY_CTX_free(ed_pctx);
                break;
            }
            EVP_PKEY_CTX_free(ed_pctx);

            if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
                                    NULL, ed_pkey)) {
                st = 0;
                EVP_PKEY_free(ed_pkey);
                break;
            }
            EVP_PKEY_free(ed_pkey);
        }
        if (st == 0) {
            BIO_printf(bio_err, "EdDSA failure.\n");
            ERR_print_errors(bio_err);
            rsa_count = 1;
        } else {
            for (i = 0; i < loopargs_len; i++) {
                /* Perform EdDSA signature test */
M
Matt Caswell 已提交
3156
                loopargs[i].sigsize = test_ed_curves[testnum].sigsize;
P
Paul Yang 已提交
3157
                st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
M
Matt Caswell 已提交
3158
                                    loopargs[i].buf2, &loopargs[i].sigsize,
P
Paul Yang 已提交
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187
                                    loopargs[i].buf, 20);
                if (st == 0)
                    break;
            }
            if (st == 0) {
                BIO_printf(bio_err,
                           "EdDSA sign failure.  No EdDSA sign will be done.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
            } else {
                pkey_print_message("sign", test_ed_curves[testnum].name,
                                   eddsa_c[testnum][0],
                                   test_ed_curves[testnum].bits, seconds.eddsa);
                Time_F(START);
                count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
                d = Time_F(STOP);

                BIO_printf(bio_err,
                           mr ? "+R8:%ld:%u:%s:%.2f\n" :
                           "%ld %u bits %s signs in %.2fs \n",
                           count, test_ed_curves[testnum].bits,
                           test_ed_curves[testnum].name, d);
                eddsa_results[testnum][0] = (double)count / d;
                rsa_count = count;
            }

            /* Perform EdDSA verification test */
            for (i = 0; i < loopargs_len; i++) {
                st = EVP_DigestVerify(loopargs[i].eddsa_ctx[testnum],
M
Matt Caswell 已提交
3188
                                      loopargs[i].buf2, loopargs[i].sigsize,
P
Paul Yang 已提交
3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
                                      loopargs[i].buf, 20);
                if (st != 1)
                    break;
            }
            if (st != 1) {
                BIO_printf(bio_err,
                           "EdDSA verify failure.  No EdDSA verify will be done.\n");
                ERR_print_errors(bio_err);
                eddsa_doit[testnum] = 0;
            } else {
                pkey_print_message("verify", test_ed_curves[testnum].name,
                                   eddsa_c[testnum][1],
                                   test_ed_curves[testnum].bits, seconds.eddsa);
                Time_F(START);
                count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
                d = Time_F(STOP);
                BIO_printf(bio_err,
                           mr ? "+R9:%ld:%u:%s:%.2f\n"
                           : "%ld %u bits %s verify in %.2fs\n",
                           count, test_ed_curves[testnum].bits,
                           test_ed_curves[testnum].name, d);
                eddsa_results[testnum][1] = (double)count / d;
            }

            if (rsa_count <= 1) {
                /* if longer than 10s, don't do any more */
                for (testnum++; testnum < EdDSA_NUM; testnum++)
                    eddsa_doit[testnum] = 0;
            }
        }
    }

F
FdaSilvaYY 已提交
3221
#endif                          /* OPENSSL_NO_EC */
3222
#ifndef NO_FORK
3223
 show_res:
3224
#endif
3225
    if (!mr) {
3226 3227
        printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
        printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
3228 3229
        printf("options:");
        printf("%s ", BN_options());
3230
#ifndef OPENSSL_NO_MD2
3231
        printf("%s ", MD2_options());
3232 3233
#endif
#ifndef OPENSSL_NO_RC4
3234
        printf("%s ", RC4_options());
3235 3236
#endif
#ifndef OPENSSL_NO_DES
3237
        printf("%s ", DES_options());
3238
#endif
3239
        printf("%s ", AES_options());
3240
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
3241
        printf("%s ", IDEA_options());
3242 3243
#endif
#ifndef OPENSSL_NO_BF
3244
        printf("%s ", BF_options());
3245
#endif
R
Rich Salz 已提交
3246
        printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
3247
    }
B
Bodo Möller 已提交
3248

3249 3250
    if (pr_header) {
        if (mr)
3251
            printf("+H");
3252
        else {
3253 3254 3255
            printf
                ("The 'numbers' are in 1000s of bytes per second processed.\n");
            printf("type        ");
3256
        }
3257
        for (testnum = 0; testnum < size_num; testnum++)
3258
            printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
3259
        printf("\n");
3260
    }
B
Bodo Möller 已提交
3261

3262 3263 3264 3265
    for (k = 0; k < ALGOR_NUM; k++) {
        if (!doit[k])
            continue;
        if (mr)
3266
            printf("+F:%u:%s", k, names[k]);
3267
        else
3268
            printf("%-13s", names[k]);
3269
        for (testnum = 0; testnum < size_num; testnum++) {
3270 3271
            if (results[k][testnum] > 10000 && !mr)
                printf(" %11.2fk", results[k][testnum] / 1e3);
3272
            else
3273
                printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
3274
        }
3275
        printf("\n");
3276
    }
3277
#ifndef OPENSSL_NO_RSA
3278
    testnum = 1;
3279 3280 3281
    for (k = 0; k < RSA_NUM; k++) {
        if (!rsa_doit[k])
            continue;
3282
        if (testnum && !mr) {
3283
            printf("%18ssign    verify    sign/s verify/s\n", " ");
3284
            testnum = 0;
3285 3286
        }
        if (mr)
3287 3288
            printf("+F2:%u:%u:%f:%f\n",
                   k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
3289
        else
3290
            printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3291 3292
                   rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
                   rsa_results[k][0], rsa_results[k][1]);
3293
    }
3294 3295
#endif
#ifndef OPENSSL_NO_DSA
3296
    testnum = 1;
3297 3298 3299
    for (k = 0; k < DSA_NUM; k++) {
        if (!dsa_doit[k])
            continue;
3300
        if (testnum && !mr) {
3301
            printf("%18ssign    verify    sign/s verify/s\n", " ");
3302
            testnum = 0;
3303 3304
        }
        if (mr)
3305 3306
            printf("+F3:%u:%u:%f:%f\n",
                   k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
3307
        else
3308
            printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
3309 3310
                   dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
                   dsa_results[k][0], dsa_results[k][1]);
3311
    }
3312
#endif
3313
#ifndef OPENSSL_NO_EC
3314
    testnum = 1;
3315
    for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
3316 3317
        if (!ecdsa_doit[k])
            continue;
3318
        if (testnum && !mr) {
3319
            printf("%30ssign    verify    sign/s verify/s\n", " ");
3320
            testnum = 0;
3321 3322 3323
        }

        if (mr)
3324
            printf("+F4:%u:%u:%f:%f\n",
3325
                   k, test_curves[k].bits,
3326
                   ecdsa_results[k][0], ecdsa_results[k][1]);
3327
        else
3328 3329
            printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
                   test_curves[k].bits, test_curves[k].name,
3330 3331
                   1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
                   ecdsa_results[k][0], ecdsa_results[k][1]);
3332
    }
3333

3334
    testnum = 1;
3335 3336 3337
    for (k = 0; k < EC_NUM; k++) {
        if (!ecdh_doit[k])
            continue;
3338
        if (testnum && !mr) {
3339
            printf("%30sop      op/s\n", " ");
3340
            testnum = 0;
3341 3342
        }
        if (mr)
3343
            printf("+F5:%u:%u:%f:%f\n",
3344
                   k, test_curves[k].bits,
3345
                   ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3346 3347

        else
3348 3349
            printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
                   test_curves[k].bits, test_curves[k].name,
3350
                   1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3351
    }
P
Paul Yang 已提交
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371

    testnum = 1;
    for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
        if (!eddsa_doit[k])
            continue;
        if (testnum && !mr) {
            printf("%30ssign    verify    sign/s verify/s\n", " ");
            testnum = 0;
        }

        if (mr)
            printf("+F6:%u:%u:%s:%f:%f\n",
                   k, test_ed_curves[k].bits, test_ed_curves[k].name,
                   eddsa_results[k][0], eddsa_results[k][1]);
        else
            printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
                   test_ed_curves[k].bits, test_ed_curves[k].name,
                   1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
                   eddsa_results[k][0], eddsa_results[k][1]);
    }
3372
#endif
3373

3374
    ret = 0;
3375 3376 3377

 end:
    ERR_print_errors(bio_err);
3378
    for (i = 0; i < loopargs_len; i++) {
3379 3380
        OPENSSL_free(loopargs[i].buf_malloc);
        OPENSSL_free(loopargs[i].buf2_malloc);
3381

3382
#ifndef OPENSSL_NO_RSA
3383 3384
        for (k = 0; k < RSA_NUM; k++)
            RSA_free(loopargs[i].rsa_key[k]);
3385 3386
#endif
#ifndef OPENSSL_NO_DSA
3387 3388
        for (k = 0; k < DSA_NUM; k++)
            DSA_free(loopargs[i].dsa_key[k]);
3389
#endif
3390
#ifndef OPENSSL_NO_EC
3391
        for (k = 0; k < ECDSA_NUM; k++)
3392
            EC_KEY_free(loopargs[i].ecdsa[k]);
3393
        for (k = 0; k < EC_NUM; k++)
3394
            EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
P
Paul Yang 已提交
3395 3396
        for (k = 0; k < EdDSA_NUM; k++)
            EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
3397 3398
        OPENSSL_free(loopargs[i].secret_a);
        OPENSSL_free(loopargs[i].secret_b);
3399
#endif
3400
    }
P
Pauli 已提交
3401
    OPENSSL_free(evp_hmac_name);
3402

3403 3404 3405
    if (async_jobs > 0) {
        for (i = 0; i < loopargs_len; i++)
            ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
3406
    }
3407

3408
    if (async_init) {
3409
        ASYNC_cleanup_thread();
3410 3411
    }
    OPENSSL_free(loopargs);
3412
    release_engine(e);
K
KaoruToda 已提交
3413
    return ret;
3414
}
3415

3416
static void print_message(const char *s, long num, int length, int tm)
3417
{
3418
#ifdef SIGALRM
3419 3420
    BIO_printf(bio_err,
               mr ? "+DT:%s:%d:%d\n"
3421
               : "Doing %s for %ds on %d size blocks: ", s, tm, length);
3422
    (void)BIO_flush(bio_err);
3423
    alarm(tm);
3424
#else
3425 3426 3427 3428
    BIO_printf(bio_err,
               mr ? "+DN:%s:%ld:%d\n"
               : "Doing %s %ld times on %d size blocks: ", s, num, length);
    (void)BIO_flush(bio_err);
3429
#endif
3430
}
3431

3432
static void pkey_print_message(const char *str, const char *str2, long num,
3433
                               unsigned int bits, int tm)
3434
{
3435
#ifdef SIGALRM
3436 3437
    BIO_printf(bio_err,
               mr ? "+DTP:%d:%s:%s:%d\n"
3438
               : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
3439 3440
    (void)BIO_flush(bio_err);
    alarm(tm);
3441
#else
3442 3443
    BIO_printf(bio_err,
               mr ? "+DNP:%ld:%d:%s:%s\n"
3444
               : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
3445
    (void)BIO_flush(bio_err);
3446
#endif
3447
}
3448

3449 3450
static void print_result(int alg, int run_no, int count, double time_used)
{
3451 3452 3453 3454
    if (count == -1) {
        BIO_puts(bio_err, "EVP error!\n");
        exit(1);
    }
3455 3456 3457 3458 3459
    BIO_printf(bio_err,
               mr ? "+R:%d:%s:%f\n"
               : "%d %s's in %.2fs\n", count, names[alg], time_used);
    results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
}
3460

3461
#ifndef NO_FORK
3462
static char *sstrsep(char **string, const char *delim)
3463
{
3464 3465 3466 3467 3468 3469
    char isdelim[256];
    char *token = *string;

    if (**string == 0)
        return NULL;

R
Rich Salz 已提交
3470
    memset(isdelim, 0, sizeof(isdelim));
3471 3472
    isdelim[0] = 1;

3473
    while (*delim) {
3474 3475
        isdelim[(unsigned char)(*delim)] = 1;
        delim++;
3476
    }
3477

3478
    while (!isdelim[(unsigned char)(**string)]) {
3479
        (*string)++;
3480
    }
3481

3482
    if (**string) {
3483 3484
        **string = 0;
        (*string)++;
3485
    }
3486 3487

    return token;
3488
}
3489

3490
static int do_multi(int multi, int size_num)
3491 3492 3493 3494 3495 3496
{
    int n;
    int fd[2];
    int *fds;
    static char sep[] = ":";

R
Rich Salz 已提交
3497
    fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
3498 3499
    for (n = 0; n < multi; ++n) {
        if (pipe(fd) == -1) {
R
Rich Salz 已提交
3500
            BIO_printf(bio_err, "pipe failure\n");
3501 3502 3503
            exit(1);
        }
        fflush(stdout);
R
Rich Salz 已提交
3504
        (void)BIO_flush(bio_err);
3505 3506 3507 3508 3509 3510 3511
        if (fork()) {
            close(fd[1]);
            fds[n] = fd[0];
        } else {
            close(fd[0]);
            close(1);
            if (dup(fd[1]) == -1) {
R
Rich Salz 已提交
3512
                BIO_printf(bio_err, "dup failed\n");
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
                exit(1);
            }
            close(fd[1]);
            mr = 1;
            usertime = 0;
            free(fds);
            return 0;
        }
        printf("Forked child %d\n", n);
    }
B
Bodo Möller 已提交
3523

3524 3525 3526 3527 3528 3529 3530
    /* for now, assume the pipe is long enough to take all the output */
    for (n = 0; n < multi; ++n) {
        FILE *f;
        char buf[1024];
        char *p;

        f = fdopen(fds[n], "r");
R
Rich Salz 已提交
3531
        while (fgets(buf, sizeof(buf), f)) {
3532 3533 3534 3535
            p = strchr(buf, '\n');
            if (p)
                *p = '\0';
            if (buf[0] != '+') {
3536 3537 3538
                BIO_printf(bio_err,
                           "Don't understand line '%s' from child %d\n", buf,
                           n);
3539 3540 3541
                continue;
            }
            printf("Got: %s from %d\n", buf, n);
R
Rich Salz 已提交
3542
            if (strncmp(buf, "+F:", 3) == 0) {
3543 3544 3545 3546 3547 3548
                int alg;
                int j;

                p = buf + 3;
                alg = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);
3549
                for (j = 0; j < size_num; ++j)
3550
                    results[alg][j] += atof(sstrsep(&p, sep));
R
Rich Salz 已提交
3551
            } else if (strncmp(buf, "+F2:", 4) == 0) {
3552 3553 3554 3555 3556 3557 3558 3559
                int k;
                double d;

                p = buf + 4;
                k = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);

                d = atof(sstrsep(&p, sep));
3560
                rsa_results[k][0] += d;
3561 3562

                d = atof(sstrsep(&p, sep));
3563
                rsa_results[k][1] += d;
3564
            }
3565
# ifndef OPENSSL_NO_DSA
R
Rich Salz 已提交
3566
            else if (strncmp(buf, "+F3:", 4) == 0) {
3567 3568 3569 3570 3571 3572 3573 3574
                int k;
                double d;

                p = buf + 4;
                k = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);

                d = atof(sstrsep(&p, sep));
3575
                dsa_results[k][0] += d;
3576 3577

                d = atof(sstrsep(&p, sep));
3578
                dsa_results[k][1] += d;
3579
            }
3580
# endif
3581
# ifndef OPENSSL_NO_EC
R
Rich Salz 已提交
3582
            else if (strncmp(buf, "+F4:", 4) == 0) {
3583 3584 3585 3586 3587 3588 3589 3590
                int k;
                double d;

                p = buf + 4;
                k = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);

                d = atof(sstrsep(&p, sep));
3591
                ecdsa_results[k][0] += d;
3592 3593

                d = atof(sstrsep(&p, sep));
3594
                ecdsa_results[k][1] += d;
F
FdaSilvaYY 已提交
3595
            } else if (strncmp(buf, "+F5:", 4) == 0) {
3596 3597 3598 3599 3600 3601 3602 3603
                int k;
                double d;

                p = buf + 4;
                k = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);

                d = atof(sstrsep(&p, sep));
3604
                ecdh_results[k][0] += d;
P
Paul Yang 已提交
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
            } else if (strncmp(buf, "+F6:", 4) == 0) {
                int k;
                double d;

                p = buf + 4;
                k = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);

                d = atof(sstrsep(&p, sep));
                eddsa_results[k][0] += d;

                d = atof(sstrsep(&p, sep));
                eddsa_results[k][1] += d;
3618
            }
3619
# endif
3620

R
Rich Salz 已提交
3621
            else if (strncmp(buf, "+H:", 3) == 0) {
3622
                ;
3623
            } else
3624 3625
                BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
                           n);
3626 3627 3628 3629 3630 3631 3632
        }

        fclose(f);
    }
    free(fds);
    return 1;
}
3633
#endif
3634

3635
static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
3636
                             const openssl_speed_sec_t *seconds)
3637
{
3638
    static const int mblengths_list[] =
3639
        { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3640
    const int *mblengths = mblengths_list;
3641
    int j, count, keylen, num = OSSL_NELEM(mblengths_list);
3642
    const char *alg_name;
3643
    unsigned char *inp, *out, *key, no_key[32], no_iv[16];
3644
    EVP_CIPHER_CTX *ctx;
3645 3646
    double d = 0.0;

3647 3648 3649 3650 3651
    if (lengths_single) {
        mblengths = &lengths_single;
        num = 1;
    }

R
Rich Salz 已提交
3652 3653
    inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
    out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3654
    ctx = EVP_CIPHER_CTX_new();
3655 3656 3657 3658 3659 3660 3661 3662
    EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);

    keylen = EVP_CIPHER_CTX_key_length(ctx);
    key = app_malloc(keylen, "evp_cipher key");
    EVP_CIPHER_CTX_rand_key(ctx, key);
    EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
    OPENSSL_clear_free(key, keylen);

3663
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
3664
    alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3665 3666

    for (j = 0; j < num; j++) {
3667
        print_message(alg_name, 0, mblengths[j], seconds->sym);
3668 3669
        Time_F(START);
        for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3670
            unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
            EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
            size_t len = mblengths[j];
            int packlen;

            memset(aad, 0, 8);  /* avoid uninitialized values */
            aad[8] = 23;        /* SSL3_RT_APPLICATION_DATA */
            aad[9] = 3;         /* version */
            aad[10] = 2;
            aad[11] = 0;        /* length */
            aad[12] = 0;
            mb_param.out = NULL;
            mb_param.inp = aad;
            mb_param.len = len;
            mb_param.interleave = 8;

3686
            packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3687 3688 3689 3690 3691 3692
                                          sizeof(mb_param), &mb_param);

            if (packlen > 0) {
                mb_param.out = out;
                mb_param.inp = inp;
                mb_param.len = len;
3693
                EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3694 3695 3696 3697 3698 3699
                                    sizeof(mb_param), &mb_param);
            } else {
                int pad;

                RAND_bytes(out, 16);
                len += 16;
3700 3701
                aad[11] = (unsigned char)(len >> 8);
                aad[12] = (unsigned char)(len);
3702
                pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3703
                                          EVP_AEAD_TLS1_AAD_LEN, aad);
3704
                EVP_Cipher(ctx, out, inp, len + pad);
3705 3706 3707
            }
        }
        d = Time_F(STOP);
3708
        BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
                   : "%d %s's in %.2fs\n", count, "evp", d);
        results[D_EVP][j] = ((double)count) / d * mblengths[j];
    }

    if (mr) {
        fprintf(stdout, "+H");
        for (j = 0; j < num; j++)
            fprintf(stdout, ":%d", mblengths[j]);
        fprintf(stdout, "\n");
        fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
        for (j = 0; j < num; j++)
            fprintf(stdout, ":%.2f", results[D_EVP][j]);
        fprintf(stdout, "\n");
    } else {
        fprintf(stdout,
                "The 'numbers' are in 1000s of bytes per second processed.\n");
        fprintf(stdout, "type                    ");
        for (j = 0; j < num; j++)
            fprintf(stdout, "%7d bytes", mblengths[j]);
        fprintf(stdout, "\n");
        fprintf(stdout, "%-24s", alg_name);

        for (j = 0; j < num; j++) {
            if (results[D_EVP][j] > 10000)
                fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
            else
                fprintf(stdout, " %11.2f ", results[D_EVP][j]);
        }
        fprintf(stdout, "\n");
    }

R
Rich Salz 已提交
3740 3741
    OPENSSL_free(inp);
    OPENSSL_free(out);
3742
    EVP_CIPHER_CTX_free(ctx);
3743
}