speed.c 100.7 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * 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
8
 */
R
Rich Salz 已提交
9

B
Bodo Möller 已提交
10
/* ====================================================================
11
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
B
Bodo Möller 已提交
12
 *
13
 * Portions of the attached software ("Contribution") are developed by
B
Bodo Möller 已提交
14 15 16 17 18
 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
 *
 * The Contribution is licensed pursuant to the OpenSSL open source
 * license provided above.
 *
19
 * The ECDH and ECDSA speed test software is originally written by
B
Bodo Möller 已提交
20 21 22
 * Sumit Gupta of Sun Microsystems Laboratories.
 *
 */
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#undef SECONDS
#define SECONDS                 3
#define PRIME_SECONDS   10
#define RSA_SECONDS             10
#define DSA_SECONDS             10
#define ECDSA_SECONDS   10
#define ECDH_SECONDS    10

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "apps.h"
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
42
#include <openssl/async.h>
43 44 45
#if !defined(OPENSSL_SYS_MSDOS)
# include OPENSSL_UNISTD
#endif
46

47
#if defined(_WIN32)
48 49
# include <windows.h>
#endif
50

51 52 53 54
#include <openssl/bn.h>
#ifndef OPENSSL_NO_DES
# include <openssl/des.h>
#endif
M
Matt Caswell 已提交
55
#include <openssl/aes.h>
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#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
71 72
#include <openssl/hmac.h>
#include <openssl/sha.h>
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#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
109
#ifndef OPENSSL_NO_EC
D
Dr. Stephen Henson 已提交
110
# include <openssl/ec.h>
111 112
#endif
#include <openssl/modes.h>
113

114
#ifndef HAVE_FORK
R
Rich Salz 已提交
115
# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
116
#  define HAVE_FORK 0
117
# else
118
#  define HAVE_FORK 1
119
# endif
120
#endif
121

122 123 124 125 126 127 128
#if HAVE_FORK
# undef NO_FORK
#else
# define NO_FORK
#endif

#undef BUFSIZE
129
#define BUFSIZE (1024*16+1)
130
#define MAX_MISALIGNMENT 63
131

132 133 134 135 136 137 138 139 140 141
#define ALGOR_NUM       30
#define SIZE_NUM        6
#define PRIME_NUM       3
#define RSA_NUM         7
#define DSA_NUM         3

#define EC_NUM          17
#define MAX_ECDH_SIZE   256
#define MISALIGN        64

142
static volatile int run = 0;
143

144 145
static int mr = 0;
static int usertime = 1;
146

147 148
typedef struct loopargs_st {
    ASYNC_JOB *inprogress_job;
149
    ASYNC_WAIT_CTX *wait_ctx;
150 151 152 153
    unsigned char *buf;
    unsigned char *buf2;
    unsigned char *buf_malloc;
    unsigned char *buf2_malloc;
F
FdaSilvaYY 已提交
154
    unsigned int siglen;
155 156 157 158 159 160 161 162
#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[EC_NUM];
163
    EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
164 165
    unsigned char *secret_a;
    unsigned char *secret_b;
166
    size_t outlen[EC_NUM];
167
#endif
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    EVP_CIPHER_CTX *ctx;
    HMAC_CTX *hctx;
    GCM128_CONTEXT *gcm_ctx;
} loopargs_t;

#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);
static int EVP_Update_loop(void *args);
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);
#endif
224 225
static int run_benchmark(int async_jobs, int (*loop_function) (void *),
                         loopargs_t * loopargs);
226

227
static double Time_F(int s);
228
static void print_message(const char *s, long num, int length);
229
static void pkey_print_message(const char *str, const char *str2,
230 231
                               long num, int bits, int sec);
static void print_result(int alg, int run_no, int count, double time_used);
232
#ifndef NO_FORK
233
static int do_multi(int multi);
234
#endif
235 236 237 238 239 240 241 242 243 244

static const char *names[ALGOR_NUM] = {
    "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"
};
245

246
static double results[ALGOR_NUM][SIZE_NUM];
247 248

static const int lengths[SIZE_NUM] = {
249
    16, 64, 256, 1024, 8 * 1024, 16 * 1024
250
};
251

252
#ifndef OPENSSL_NO_RSA
253
static double rsa_results[RSA_NUM][2];
254 255
#endif
#ifndef OPENSSL_NO_DSA
256
static double dsa_results[DSA_NUM][2];
257
#endif
258
#ifndef OPENSSL_NO_EC
B
Bodo Möller 已提交
259 260
static double ecdsa_results[EC_NUM][2];
static double ecdh_results[EC_NUM][1];
261
#endif
B
Bodo Möller 已提交
262

263
#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
264 265
static const char rnd_seed[] =
    "string to make the random number generator think it has entropy";
266
#endif
267

268 269 270 271 272 273
#ifdef SIGALRM
# if defined(__STDC__) || defined(sgi) || defined(_AIX)
#  define SIGRETTYPE void
# else
#  define SIGRETTYPE int
# endif
D
Dr. Stephen Henson 已提交
274

275
static SIGRETTYPE sig_done(int sig);
U
Ulf Möller 已提交
276
static SIGRETTYPE sig_done(int sig)
277 278 279 280
{
    signal(SIGALRM, sig_done);
    run = 0;
}
281
#endif
282

283 284
#define START   0
#define STOP    1
285

286
#if defined(_WIN32)
R
Richard Levitte 已提交
287

288 289 290
# if !defined(SIGALRM)
#  define SIGALRM
# endif
291 292 293 294 295
static unsigned int lapse, schlock;
static void alarm_win32(unsigned int secs)
{
    lapse = secs * 1000;
}
R
Richard Levitte 已提交
296

297
# define alarm alarm_win32
298 299 300 301 302 303 304 305

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

307
static double Time_F(int s)
308 309 310 311 312 313 314 315
{
    double ret;
    static HANDLE thr;

    if (s == START) {
        schlock = 0;
        thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
        if (thr == NULL) {
316 317
            DWORD err = GetLastError();
            BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
318
            ExitProcess(err);
319 320 321 322 323 324 325 326 327 328 329 330 331
        }
        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;
}
332
#else
333

334 335 336 337 338 339 340
static double Time_F(int s)
{
    double ret = app_tminterval(s, usertime);
    if (s == STOP)
        alarm(0);
    return ret;
}
341
#endif
342

343
static void multiblock_speed(const EVP_CIPHER *evp_cipher);
344

F
FdaSilvaYY 已提交
345
static int found(const char *name, const OPT_PAIR *pairs, int *result)
346 347 348 349 350 351 352 353 354 355 356 357
{
    for (; pairs->name; pairs++)
        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,
    OPT_ELAPSED, OPT_EVP, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
358
    OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS
359 360
} OPTION_CHOICE;

F
FdaSilvaYY 已提交
361
const OPTIONS speed_options[] = {
362 363 364
    {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
    {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
    {"help", OPT_HELP, '-', "Display this summary"},
365 366 367 368
    {"evp", OPT_EVP, 's', "Use specified EVP cipher"},
    {"decrypt", OPT_DECRYPT, '-',
     "Time decryption instead of encryption (only EVP)"},
    {"mr", OPT_MR, '-', "Produce machine readable output"},
F
FdaSilvaYY 已提交
369
    {"mb", OPT_MB, '-',
F
FdaSilvaYY 已提交
370
     "Enable (tls1.1) multi-block mode on evp_cipher requested with -evp"},
371
    {"misalign", OPT_MISALIGN, 'n', "Amount to mis-align buffers"},
372 373 374 375 376
    {"elapsed", OPT_ELAPSED, '-',
     "Measure time in real time instead of CPU user time"},
#ifndef NO_FORK
    {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
#endif
377
#ifndef OPENSSL_NO_ASYNC
F
FdaSilvaYY 已提交
378 379
    {"async_jobs", OPT_ASYNCJOBS, 'p',
     "Enable async mode and start pnum jobs"},
380
#endif
381 382 383
#ifndef OPENSSL_NO_ENGINE
    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
#endif
384
    {NULL},
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
};

#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
417
static OPT_PAIR doit_choices[] = {
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
#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 已提交
437
#ifndef OPENSSL_NO_RMD160
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    {"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},
    {NULL}
};

M
Matt Caswell 已提交
485 486 487 488
#ifndef OPENSSL_NO_DSA
# define R_DSA_512       0
# define R_DSA_1024      1
# define R_DSA_2048      2
489 490 491 492 493 494
static OPT_PAIR dsa_choices[] = {
    {"dsa512", R_DSA_512},
    {"dsa1024", R_DSA_1024},
    {"dsa2048", R_DSA_2048},
    {NULL},
};
M
Matt Caswell 已提交
495
#endif
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
#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
static OPT_PAIR rsa_choices[] = {
    {"rsa512", R_RSA_512},
    {"rsa1024", R_RSA_1024},
    {"rsa2048", R_RSA_2048},
    {"rsa3072", R_RSA_3072},
    {"rsa4096", R_RSA_4096},
    {"rsa7680", R_RSA_7680},
    {"rsa15360", R_RSA_15360},
    {NULL}
};

#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
531
#define R_EC_X25519  16
R
Richard Levitte 已提交
532
#ifndef OPENSSL_NO_EC
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
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},
    {"ecdsab571", R_EC_B571},
    {NULL}
};
F
FdaSilvaYY 已提交
552

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
static OPT_PAIR ecdh_choices[] = {
    {"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},
570
    {"ecdhx25519", R_EC_X25519},
571 572 573 574
    {NULL}
};
#endif

575 576 577 578
#ifndef SIGALRM
# define COND(d) (count < (d))
# define COUNT(d) (d)
#else
579
# define COND(unused_cond) (run && count<0x7fffffff)
580
# define COUNT(d) (count)
581
#endif                          /* SIGALRM */
582 583 584

static int testnum;

F
FdaSilvaYY 已提交
585
/* Nb of iterations to do per algorithm and key-size */
586
static long c[ALGOR_NUM][SIZE_NUM];
587

588
#ifndef OPENSSL_NO_MD2
589 590
static int EVP_Digest_MD2_loop(void *args)
{
591
    loopargs_t *tempargs = *(loopargs_t **) args;
592
    unsigned char *buf = tempargs->buf;
593
    unsigned char md2[MD2_DIGEST_LENGTH];
594
    int count;
595

596
    for (count = 0; COND(c[D_MD2][testnum]); count++) {
597
        if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
598
                        NULL))
599 600
            return -1;
    }
601 602
    return count;
}
603
#endif
604

605
#ifndef OPENSSL_NO_MDC2
606 607
static int EVP_Digest_MDC2_loop(void *args)
{
608
    loopargs_t *tempargs = *(loopargs_t **) args;
609
    unsigned char *buf = tempargs->buf;
610
    unsigned char mdc2[MDC2_DIGEST_LENGTH];
611
    int count;
612

613
    for (count = 0; COND(c[D_MDC2][testnum]); count++) {
614
        if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
615
                        NULL))
616 617
            return -1;
    }
618 619
    return count;
}
620
#endif
621

622
#ifndef OPENSSL_NO_MD4
623 624
static int EVP_Digest_MD4_loop(void *args)
{
625
    loopargs_t *tempargs = *(loopargs_t **) args;
626
    unsigned char *buf = tempargs->buf;
627
    unsigned char md4[MD4_DIGEST_LENGTH];
628
    int count;
629

630
    for (count = 0; COND(c[D_MD4][testnum]); count++) {
631
        if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
632
                        NULL))
633 634
            return -1;
    }
635 636
    return count;
}
637
#endif
638

639
#ifndef OPENSSL_NO_MD5
640 641
static int MD5_loop(void *args)
{
642
    loopargs_t *tempargs = *(loopargs_t **) args;
643
    unsigned char *buf = tempargs->buf;
644
    unsigned char md5[MD5_DIGEST_LENGTH];
645 646 647 648 649 650 651 652
    int count;
    for (count = 0; COND(c[D_MD5][testnum]); count++)
        MD5(buf, lengths[testnum], md5);
    return count;
}

static int HMAC_loop(void *args)
{
653
    loopargs_t *tempargs = *(loopargs_t **) args;
654 655
    unsigned char *buf = tempargs->buf;
    HMAC_CTX *hctx = tempargs->hctx;
656
    unsigned char hmac[MD5_DIGEST_LENGTH];
657
    int count;
658

659 660 661
    for (count = 0; COND(c[D_HMAC][testnum]); count++) {
        HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
        HMAC_Update(hctx, buf, lengths[testnum]);
662
        HMAC_Final(hctx, hmac, NULL);
663 664 665
    }
    return count;
}
666
#endif
667 668 669

static int SHA1_loop(void *args)
{
670
    loopargs_t *tempargs = *(loopargs_t **) args;
671
    unsigned char *buf = tempargs->buf;
672
    unsigned char sha[SHA_DIGEST_LENGTH];
673 674 675 676 677 678 679 680
    int count;
    for (count = 0; COND(c[D_SHA1][testnum]); count++)
        SHA1(buf, lengths[testnum], sha);
    return count;
}

static int SHA256_loop(void *args)
{
681
    loopargs_t *tempargs = *(loopargs_t **) args;
682
    unsigned char *buf = tempargs->buf;
683
    unsigned char sha256[SHA256_DIGEST_LENGTH];
684 685 686 687 688 689 690 691
    int count;
    for (count = 0; COND(c[D_SHA256][testnum]); count++)
        SHA256(buf, lengths[testnum], sha256);
    return count;
}

static int SHA512_loop(void *args)
{
692
    loopargs_t *tempargs = *(loopargs_t **) args;
693
    unsigned char *buf = tempargs->buf;
694
    unsigned char sha512[SHA512_DIGEST_LENGTH];
695 696 697 698 699 700
    int count;
    for (count = 0; COND(c[D_SHA512][testnum]); count++)
        SHA512(buf, lengths[testnum], sha512);
    return count;
}

701
#ifndef OPENSSL_NO_WHIRLPOOL
702 703
static int WHIRLPOOL_loop(void *args)
{
704
    loopargs_t *tempargs = *(loopargs_t **) args;
705
    unsigned char *buf = tempargs->buf;
706
    unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
707 708 709 710 711
    int count;
    for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
        WHIRLPOOL(buf, lengths[testnum], whirlpool);
    return count;
}
712
#endif
713

R
Rich Salz 已提交
714
#ifndef OPENSSL_NO_RMD160
715 716
static int EVP_Digest_RMD160_loop(void *args)
{
717
    loopargs_t *tempargs = *(loopargs_t **) args;
718
    unsigned char *buf = tempargs->buf;
719
    unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
720
    int count;
721
    for (count = 0; COND(c[D_RMD160][testnum]); count++) {
722
        if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
723
                        NULL, EVP_ripemd160(), NULL))
724 725
            return -1;
    }
726 727
    return count;
}
728
#endif
729

730
#ifndef OPENSSL_NO_RC4
731 732 733
static RC4_KEY rc4_ks;
static int RC4_loop(void *args)
{
734
    loopargs_t *tempargs = *(loopargs_t **) args;
735 736 737
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_RC4][testnum]); count++)
738
        RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
739 740 741 742 743 744 745 746 747 748 749
    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)
{
750
    loopargs_t *tempargs = *(loopargs_t **) args;
751 752 753 754
    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,
755
                         &DES_iv, DES_ENCRYPT);
756 757 758 759 760
    return count;
}

static int DES_ede3_cbc_encrypt_loop(void *args)
{
761
    loopargs_t *tempargs = *(loopargs_t **) args;
762 763 764 765
    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],
766
                             &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT);
767 768 769 770
    return count;
}
#endif

M
Matt Caswell 已提交
771
#define MAX_BLOCK_SIZE 128
772 773 774 775 776

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)
{
777
    loopargs_t *tempargs = *(loopargs_t **) args;
778 779 780 781
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
782
                        (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
783 784 785 786 787
    return count;
}

static int AES_cbc_192_encrypt_loop(void *args)
{
788
    loopargs_t *tempargs = *(loopargs_t **) args;
789 790 791 792
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
793
                        (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
794 795 796 797 798
    return count;
}

static int AES_cbc_256_encrypt_loop(void *args)
{
799
    loopargs_t *tempargs = *(loopargs_t **) args;
800 801 802 803
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
804
                        (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
805 806 807 808 809
    return count;
}

static int AES_ige_128_encrypt_loop(void *args)
{
810
    loopargs_t *tempargs = *(loopargs_t **) args;
811 812 813 814 815
    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,
816
                        (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
817 818 819 820 821
    return count;
}

static int AES_ige_192_encrypt_loop(void *args)
{
822
    loopargs_t *tempargs = *(loopargs_t **) args;
823 824 825 826 827
    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,
828
                        (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
829 830 831 832 833
    return count;
}

static int AES_ige_256_encrypt_loop(void *args)
{
834
    loopargs_t *tempargs = *(loopargs_t **) args;
835 836 837 838 839
    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,
840
                        (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
841 842 843 844 845
    return count;
}

static int CRYPTO_gcm128_aad_loop(void *args)
{
846
    loopargs_t *tempargs = *(loopargs_t **) args;
847 848 849 850 851 852 853 854
    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;
}

855
static long save_count = 0;
856 857 858
static int decrypt = 0;
static int EVP_Update_loop(void *args)
{
859
    loopargs_t *tempargs = *(loopargs_t **) args;
860 861 862
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
    int outl, count;
863 864 865
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
866
    if (decrypt)
867
        for (count = 0; COND(nb_iter); count++)
868 869
            EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    else
870
        for (count = 0; COND(nb_iter); count++)
871 872 873 874 875 876 877 878 879 880 881
            EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    if (decrypt)
        EVP_DecryptFinal_ex(ctx, buf, &outl);
    else
        EVP_EncryptFinal_ex(ctx, buf, &outl);
    return count;
}

static const EVP_MD *evp_md = NULL;
static int EVP_Digest_loop(void *args)
{
882
    loopargs_t *tempargs = *(loopargs_t **) args;
883 884 885
    unsigned char *buf = tempargs->buf;
    unsigned char md[EVP_MAX_MD_SIZE];
    int count;
886 887 888 889 890 891
#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))
892 893
            return -1;
    }
894 895 896 897
    return count;
}

#ifndef OPENSSL_NO_RSA
F
FdaSilvaYY 已提交
898
static long rsa_c[RSA_NUM][2];  /* # RSA iteration test */
899 900 901

static int RSA_sign_loop(void *args)
{
902
    loopargs_t *tempargs = *(loopargs_t **) args;
903 904
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
F
FdaSilvaYY 已提交
905
    unsigned int *rsa_num = &tempargs->siglen;
906
    RSA **rsa_key = tempargs->rsa_key;
907 908
    int ret, count;
    for (count = 0; COND(rsa_c[testnum][0]); count++) {
909
        ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
910 911 912 913 914 915 916 917 918 919 920 921
        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)
{
922
    loopargs_t *tempargs = *(loopargs_t **) args;
923 924
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
F
FdaSilvaYY 已提交
925
    unsigned int rsa_num = tempargs->siglen;
926
    RSA **rsa_key = tempargs->rsa_key;
927 928
    int ret, count;
    for (count = 0; COND(rsa_c[testnum][1]); count++) {
929 930
        ret =
            RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
        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)
{
946
    loopargs_t *tempargs = *(loopargs_t **) args;
947 948
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
949
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
950
    unsigned int *siglen = &tempargs->siglen;
951 952 953 954 955 956
    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);
957
            count = -1;
958 959 960 961 962 963 964 965
            break;
        }
    }
    return count;
}

static int DSA_verify_loop(void *args)
{
966
    loopargs_t *tempargs = *(loopargs_t **) args;
967 968
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
969
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
970
    unsigned int siglen = tempargs->siglen;
971 972 973 974 975 976
    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);
977
            count = -1;
978 979 980 981 982 983 984 985 986 987 988
            break;
        }
    }
    return count;
}
#endif

#ifndef OPENSSL_NO_EC
static long ecdsa_c[EC_NUM][2];
static int ECDSA_sign_loop(void *args)
{
989
    loopargs_t *tempargs = *(loopargs_t **) args;
990
    unsigned char *buf = tempargs->buf;
991 992
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
993
    unsigned int *ecdsasiglen = &tempargs->siglen;
994 995
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
996
        ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
997 998 999
        if (ret == 0) {
            BIO_printf(bio_err, "ECDSA sign failure\n");
            ERR_print_errors(bio_err);
1000
            count = -1;
1001 1002 1003 1004 1005 1006 1007 1008
            break;
        }
    }
    return count;
}

static int ECDSA_verify_loop(void *args)
{
1009
    loopargs_t *tempargs = *(loopargs_t **) args;
1010
    unsigned char *buf = tempargs->buf;
1011 1012
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
1013
    unsigned int ecdsasiglen = tempargs->siglen;
1014 1015
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1016
        ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1017 1018 1019
        if (ret != 1) {
            BIO_printf(bio_err, "ECDSA verify failure\n");
            ERR_print_errors(bio_err);
1020
            count = -1;
1021 1022 1023 1024 1025 1026
            break;
        }
    }
    return count;
}

1027
/* ******************************************************************** */
1028 1029
static long ecdh_c[EC_NUM][1];

1030 1031 1032 1033 1034
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;
1035
    int count;
1036
    size_t *outlen = &(tempargs->outlen[testnum]);
F
FdaSilvaYY 已提交
1037

1038
    for (count = 0; COND(ecdh_c[testnum][0]); count++)
1039 1040
        EVP_PKEY_derive(ctx, derived_secret, outlen);

1041 1042
    return count;
}
1043

F
FdaSilvaYY 已提交
1044
#endif                          /* OPENSSL_NO_EC */
1045

F
FdaSilvaYY 已提交
1046
static int run_benchmark(int async_jobs,
1047
                         int (*loop_function) (void *), loopargs_t * loopargs)
1048 1049 1050 1051
{
    int job_op_count = 0;
    int total_op_count = 0;
    int num_inprogress = 0;
F
FdaSilvaYY 已提交
1052
    int error = 0, i = 0, ret = 0;
1053 1054
    OSSL_ASYNC_FD job_fd = 0;
    size_t num_job_fds = 0;
1055 1056 1057

    run = 1;

1058
    if (async_jobs == 0) {
1059
        return loop_function((void *)&loopargs);
1060 1061 1062
    }

    for (i = 0; i < async_jobs && !error; i++) {
1063 1064 1065
        loopargs_t *looparg_item = loopargs + i;

        /* Copy pointer content (looparg_t item address) into async context */
F
FdaSilvaYY 已提交
1066 1067
        ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
                              &job_op_count, loop_function,
1068
                              (void *)&looparg_item, sizeof(looparg_item));
F
FdaSilvaYY 已提交
1069
        switch (ret) {
F
FdaSilvaYY 已提交
1070 1071 1072 1073 1074
        case ASYNC_PAUSE:
            ++num_inprogress;
            break;
        case ASYNC_FINISH:
            if (job_op_count == -1) {
1075
                error = 1;
F
FdaSilvaYY 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
            } 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;
1086 1087 1088 1089
        }
    }

    while (num_inprogress > 0) {
1090
#if defined(OPENSSL_SYS_WINDOWS)
1091
        DWORD avail = 0;
1092
#elif defined(OPENSSL_SYS_UNIX)
1093
        int select_result = 0;
1094 1095
        OSSL_ASYNC_FD max_fd = 0;
        fd_set waitfdset;
1096

1097
        FD_ZERO(&waitfdset);
1098

1099 1100 1101
        for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
            if (loopargs[i].inprogress_job == NULL)
                continue;
1102

1103 1104 1105
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1106 1107 1108 1109
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
1110
            }
1111 1112
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1113 1114 1115
            FD_SET(job_fd, &waitfdset);
            if (job_fd > max_fd)
                max_fd = job_fd;
1116 1117
        }

B
Ben Laurie 已提交
1118
        if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1119
            BIO_printf(bio_err,
1120 1121 1122
                       "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
                       "Decrease the value of async_jobs\n",
                       max_fd, FD_SETSIZE);
1123 1124 1125 1126 1127
            ERR_print_errors(bio_err);
            error = 1;
            break;
        }

1128
        select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1129 1130 1131 1132
        if (select_result == -1 && errno == EINTR)
            continue;

        if (select_result == -1) {
1133 1134 1135 1136
            BIO_printf(bio_err, "Failure in the select\n");
            ERR_print_errors(bio_err);
            error = 1;
            break;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
        }

        if (select_result == 0)
            continue;
#endif

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

1147 1148 1149
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1150 1151 1152 1153 1154
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
            }
1155 1156
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1157

1158
#if defined(OPENSSL_SYS_UNIX)
1159
            if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1160
                continue;
1161
#elif defined(OPENSSL_SYS_WINDOWS)
F
FdaSilvaYY 已提交
1162
            if (num_job_fds == 1
F
FdaSilvaYY 已提交
1163
                && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
F
FdaSilvaYY 已提交
1164
                && avail > 0)
1165 1166 1167
                continue;
#endif

1168
            ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1169 1170 1171
                                  loopargs[i].wait_ctx, &job_op_count,
                                  loop_function, (void *)(loopargs + i),
                                  sizeof(loopargs_t));
F
FdaSilvaYY 已提交
1172
            switch (ret) {
F
FdaSilvaYY 已提交
1173 1174 1175 1176
            case ASYNC_PAUSE:
                break;
            case ASYNC_FINISH:
                if (job_op_count == -1) {
1177
                    error = 1;
F
FdaSilvaYY 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
                } 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;
1192 1193 1194 1195 1196 1197 1198 1199 1200
            }
        }
    }

    return error ? -1 : total_op_count;
}

int speed_main(int argc, char **argv)
{
1201
    ENGINE *e = NULL;
1202
    loopargs_t *loopargs = NULL;
1203
    int async_init = 0;
1204 1205
    int loopargs_len = 0;
    char *prog;
1206
    const char *engine_id = NULL;
1207 1208 1209
    const EVP_CIPHER *evp_cipher = NULL;
    double d = 0.0;
    OPTION_CHOICE o;
1210 1211
    int multiblock = 0, pr_header = 0;
    int doit[ALGOR_NUM] = { 0 };
1212
    int ret = 1, i, k, misalign = 0;
1213
    long count = 0;
1214 1215 1216
#ifndef NO_FORK
    int multi = 0;
#endif
1217
    unsigned int async_jobs = 0;
1218 1219
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
    || !defined(OPENSSL_NO_EC)
1220
    long rsa_count = 1;
1221
#endif
1222 1223

    /* What follows are the buffers and key material. */
1224
#ifndef OPENSSL_NO_RC5
1225
    RC5_32_KEY rc5_ks;
1226 1227
#endif
#ifndef OPENSSL_NO_RC2
1228
    RC2_KEY rc2_ks;
1229 1230
#endif
#ifndef OPENSSL_NO_IDEA
1231
    IDEA_KEY_SCHEDULE idea_ks;
1232 1233
#endif
#ifndef OPENSSL_NO_SEED
1234
    SEED_KEY_SCHEDULE seed_ks;
1235 1236
#endif
#ifndef OPENSSL_NO_BF
1237
    BF_KEY bf_ks;
1238 1239
#endif
#ifndef OPENSSL_NO_CAST
1240
    CAST_KEY cast_ks;
1241
#endif
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    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
    };
1257
#ifndef OPENSSL_NO_CAMELLIA
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    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
    };
1269
    CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1270 1271
#endif
#ifndef OPENSSL_NO_DES
1272 1273 1274 1275 1276 1277 1278 1279 1280
    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
    };
1281 1282
#endif
#ifndef OPENSSL_NO_RSA
1283
    static const unsigned int rsa_bits[RSA_NUM] = {
1284 1285
        512, 1024, 2048, 3072, 4096, 7680, 15360
    };
1286
    static const unsigned char *rsa_data[RSA_NUM] = {
1287 1288
        test512, test1024, test2048, test3072, test4096, test7680, test15360
    };
1289
    static const int rsa_data_length[RSA_NUM] = {
1290 1291 1292 1293 1294
        sizeof(test512), sizeof(test1024),
        sizeof(test2048), sizeof(test3072),
        sizeof(test4096), sizeof(test7680),
        sizeof(test15360)
    };
1295
    int rsa_doit[RSA_NUM] = { 0 };
1296 1297
#endif
#ifndef OPENSSL_NO_DSA
1298
    static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1299
    int dsa_doit[DSA_NUM] = { 0 };
1300 1301
#endif
#ifndef OPENSSL_NO_EC
1302 1303 1304 1305 1306
    /*
     * 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
     * the following arrays and increase the EC_NUM value accordingly.
     */
1307
    static const unsigned int test_curves[EC_NUM] = {
1308
        /* Prime Curves */
1309 1310
        NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
        NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
1311
        /* Binary Curves */
1312 1313 1314
        NID_sect163k1, NID_sect233k1, NID_sect283k1,
        NID_sect409k1, NID_sect571k1, NID_sect163r2,
        NID_sect233r1, NID_sect283r1, NID_sect409r1,
1315 1316 1317
        NID_sect571r1,
        /* Other */
        NID_X25519
1318 1319 1320
    };
    static const char *test_curves_names[EC_NUM] = {
        /* Prime Curves */
1321 1322
        "secp160r1", "nistp192", "nistp224",
        "nistp256", "nistp384", "nistp521",
1323
        /* Binary Curves */
1324 1325 1326
        "nistk163", "nistk233", "nistk283",
        "nistk409", "nistk571", "nistb163",
        "nistb233", "nistb283", "nistb409",
1327 1328 1329
        "nistb571",
        /* Other */
        "X25519"
1330
    };
1331
    static const int test_curves_bits[EC_NUM] = {
1332 1333 1334 1335 1336
        160, 192, 224,
        256, 384, 521,
        163, 233, 283,
        409, 571, 163,
        233, 283, 409,
1337
        571, 253                /* X25519 */
1338
    };
1339

1340 1341
    int ecdsa_doit[EC_NUM] = { 0 };
    int ecdh_doit[EC_NUM] = { 0 };
F
FdaSilvaYY 已提交
1342
#endif                          /* ndef OPENSSL_NO_EC */
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

    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:
1357
            usertime = 0;
1358 1359 1360 1361 1362 1363 1364
            break;
        case OPT_EVP:
            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 已提交
1365
                           "%s: %s is an unknown cipher or digest\n",
1366
                           prog, opt_arg());
1367 1368 1369
                goto end;
            }
            doit[D_EVP] = 1;
1370 1371
            break;
        case OPT_DECRYPT:
1372
            decrypt = 1;
1373 1374
            break;
        case OPT_ENGINE:
1375 1376 1377 1378 1379 1380
            /*
             * 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();
1381 1382
            break;
        case OPT_MULTI:
1383
#ifndef NO_FORK
1384
            multi = atoi(opt_arg());
1385 1386 1387
#endif
            break;
        case OPT_ASYNCJOBS:
1388
#ifndef OPENSSL_NO_ASYNC
1389
            async_jobs = atoi(opt_arg());
1390 1391 1392 1393 1394 1395
            if (!ASYNC_is_capable()) {
                BIO_printf(bio_err,
                           "%s: async_jobs specified but async not supported\n",
                           prog);
                goto opterr;
            }
1396 1397 1398 1399 1400 1401
            if (async_jobs > 99999) {
                BIO_printf(bio_err,
                           "%s: too many async_jobs\n",
                           prog);
                goto opterr;
            }
1402
#endif
1403
            break;
1404 1405
        case OPT_MISALIGN:
            if (!opt_int(opt_arg(), &misalign))
1406
                goto end;
1407
            if (misalign > MISALIGN) {
1408
                BIO_printf(bio_err,
1409 1410
                           "%s: Maximum offset is %d\n", prog, MISALIGN);
                goto opterr;
1411
            }
1412 1413 1414 1415 1416 1417
            break;
        case OPT_MR:
            mr = 1;
            break;
        case OPT_MB:
            multiblock = 1;
F
FdaSilvaYY 已提交
1418 1419 1420 1421 1422 1423
#ifdef OPENSSL_NO_MULTIBLOCK
            BIO_printf(bio_err,
                       "%s: -mb specified but multi-block support is disabled\n",
                       prog);
            goto end;
#endif
1424 1425 1426 1427 1428 1429 1430
            break;
        }
    }
    argc = opt_num_rest();
    argv = opt_rest();

    /* Remaining arguments are algorithms. */
1431
    for (; *argv; argv++) {
1432 1433 1434 1435
        if (found(*argv, doit_choices, &i)) {
            doit[i] = 1;
            continue;
        }
1436
#ifndef OPENSSL_NO_DES
1437 1438 1439 1440
        if (strcmp(*argv, "des") == 0) {
            doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
            continue;
        }
1441
#endif
1442 1443 1444 1445
        if (strcmp(*argv, "sha") == 0) {
            doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
            continue;
        }
1446 1447
#ifndef OPENSSL_NO_RSA
# ifndef RSA_NULL
1448
        if (strcmp(*argv, "openssl") == 0) {
R
Rich Salz 已提交
1449
            RSA_set_default_method(RSA_PKCS1_OpenSSL());
1450 1451
            continue;
        }
1452
# endif
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
        if (strcmp(*argv, "rsa") == 0) {
            rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
                rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
                rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
                rsa_doit[R_RSA_15360] = 1;
            continue;
        }
        if (found(*argv, rsa_choices, &i)) {
            rsa_doit[i] = 1;
            continue;
        }
1464
#endif
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
#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;
        }
1475
#endif
1476
        if (strcmp(*argv, "aes") == 0) {
1477
            doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1478 1479
            continue;
        }
1480
#ifndef OPENSSL_NO_CAMELLIA
1481
        if (strcmp(*argv, "camellia") == 0) {
1482
            doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1483 1484
            continue;
        }
1485
#endif
1486
#ifndef OPENSSL_NO_EC
1487
        if (strcmp(*argv, "ecdsa") == 0) {
1488 1489
            for (i = 0; i < EC_NUM; i++)
                ecdsa_doit[i] = 1;
1490 1491 1492 1493 1494 1495 1496
            continue;
        }
        if (found(*argv, ecdsa_choices, &i)) {
            ecdsa_doit[i] = 2;
            continue;
        }
        if (strcmp(*argv, "ecdh") == 0) {
1497 1498
            for (i = 0; i < EC_NUM; i++)
                ecdh_doit[i] = 1;
1499 1500 1501 1502 1503
            continue;
        }
        if (found(*argv, ecdh_choices, &i)) {
            ecdh_doit[i] = 2;
            continue;
1504
        }
1505 1506 1507
#endif
        BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
        goto end;
1508
    }
1509

1510 1511
    /* Initialize the job pool if async mode is enabled */
    if (async_jobs > 0) {
1512 1513
        async_init = ASYNC_init_thread(async_jobs, async_jobs);
        if (!async_init) {
1514 1515 1516 1517 1518 1519
            BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
            goto end;
        }
    }

    loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1520 1521
    loopargs =
        app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1522 1523
    memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));

1524
    for (i = 0; i < loopargs_len; i++) {
1525 1526 1527 1528 1529 1530 1531 1532
        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;
            }
        }

1533 1534 1535 1536
        loopargs[i].buf_malloc =
            app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
        loopargs[i].buf2_malloc =
            app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1537 1538 1539
        /* 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;
1540 1541 1542 1543
#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
1544 1545
    }

1546
#ifndef NO_FORK
1547 1548
    if (multi && do_multi(multi))
        goto show_res;
1549
#endif
1550

1551
    /* Initialize the engine after the fork */
1552
    e = setup_engine(engine_id, 0);
1553

1554
    /* No parameters; turn on everything. */
1555
    if ((argc == 0) && !doit[D_EVP]) {
1556
        for (i = 0; i < ALGOR_NUM; i++)
1557 1558
            if (i != D_EVP)
                doit[i] = 1;
F
FdaSilvaYY 已提交
1559
#ifndef OPENSSL_NO_RSA
1560 1561
        for (i = 0; i < RSA_NUM; i++)
            rsa_doit[i] = 1;
F
FdaSilvaYY 已提交
1562
#endif
M
Matt Caswell 已提交
1563
#ifndef OPENSSL_NO_DSA
1564 1565
        for (i = 0; i < DSA_NUM; i++)
            dsa_doit[i] = 1;
M
Matt Caswell 已提交
1566
#endif
1567
#ifndef OPENSSL_NO_EC
1568 1569 1570 1571
        for (i = 0; i < EC_NUM; i++)
            ecdsa_doit[i] = 1;
        for (i = 0; i < EC_NUM; i++)
            ecdh_doit[i] = 1;
1572
#endif
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    }
    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");

1583
#ifndef OPENSSL_NO_RSA
1584 1585 1586 1587 1588
    for (i = 0; i < loopargs_len; i++) {
        for (k = 0; k < RSA_NUM; k++) {
            const unsigned char *p;

            p = rsa_data[k];
1589 1590
            loopargs[i].rsa_key[k] =
                d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1591
            if (loopargs[i].rsa_key[k] == NULL) {
1592 1593
                BIO_printf(bio_err,
                           "internal error loading RSA key number %d\n", k);
1594 1595
                goto end;
            }
1596
        }
1597 1598 1599
    }
#endif
#ifndef OPENSSL_NO_DSA
1600 1601 1602 1603 1604
    for (i = 0; i < loopargs_len; i++) {
        loopargs[i].dsa_key[0] = get_dsa512();
        loopargs[i].dsa_key[1] = get_dsa1024();
        loopargs[i].dsa_key[2] = get_dsa2048();
    }
1605 1606
#endif
#ifndef OPENSSL_NO_DES
1607 1608 1609
    DES_set_key_unchecked(&key, &sch);
    DES_set_key_unchecked(&key2, &sch2);
    DES_set_key_unchecked(&key3, &sch3);
1610
#endif
1611 1612 1613
    AES_set_encrypt_key(key16, 128, &aes_ks1);
    AES_set_encrypt_key(key24, 192, &aes_ks2);
    AES_set_encrypt_key(key32, 256, &aes_ks3);
1614
#ifndef OPENSSL_NO_CAMELLIA
1615 1616 1617
    Camellia_set_key(key16, 128, &camellia_ks1);
    Camellia_set_key(ckey24, 192, &camellia_ks2);
    Camellia_set_key(ckey32, 256, &camellia_ks3);
1618 1619
#endif
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
1620
    IDEA_set_encrypt_key(key16, &idea_ks);
1621 1622
#endif
#ifndef OPENSSL_NO_SEED
1623
    SEED_set_key(key16, &seed_ks);
1624 1625
#endif
#ifndef OPENSSL_NO_RC4
1626
    RC4_set_key(&rc4_ks, 16, key16);
1627 1628
#endif
#ifndef OPENSSL_NO_RC2
1629
    RC2_set_key(&rc2_ks, 16, key16, 128);
1630 1631
#endif
#ifndef OPENSSL_NO_RC5
1632
    RC5_32_set_key(&rc5_ks, 16, key16, 12);
1633 1634
#endif
#ifndef OPENSSL_NO_BF
1635
    BF_set_key(&bf_ks, 16, key16);
1636 1637
#endif
#ifndef OPENSSL_NO_CAST
1638
    CAST_set_key(&cast_ks, 16, key16);
1639 1640 1641
#endif
#ifndef SIGALRM
# ifndef OPENSSL_NO_DES
1642 1643 1644 1645 1646 1647 1648
    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--)
1649 1650
            DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
                            (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
        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;

    for (i = 1; i < SIZE_NUM; i++) {
        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;
1700
        c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722

        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 已提交
1723

1724
#  ifndef OPENSSL_NO_RSA
1725 1726 1727 1728 1729
    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 已提交
1730
        if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
1731 1732 1733
            rsa_doit[i] = 0;
        else {
            if (rsa_c[i][0] == 0) {
1734
                rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1735 1736 1737 1738
                rsa_c[i][1] = 20;
            }
        }
    }
1739
#  endif
1740

1741
#  ifndef OPENSSL_NO_DSA
1742 1743 1744 1745 1746
    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 已提交
1747
        if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
1748 1749
            dsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1750
            if (dsa_c[i][0] == 0) {
1751
                dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1752 1753 1754 1755
                dsa_c[i][1] = 1;
            }
        }
    }
1756
#  endif
1757

1758
#  ifndef OPENSSL_NO_EC
1759 1760 1761 1762 1763
    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 已提交
1764
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1765 1766
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1767
            if (ecdsa_c[i][0] == 0) {
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
                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 已提交
1778
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1779 1780
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1781
            if (ecdsa_c[i][0] == 0) {
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
                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 已提交
1792
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1793 1794
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1795
            if (ecdsa_c[i][0] == 0) {
1796 1797 1798 1799 1800
                ecdsa_c[i][0] = 1;
                ecdsa_c[i][1] = 1;
            }
        }
    }
1801

1802 1803 1804
    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 已提交
1805
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1806 1807
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1808
            if (ecdh_c[i][0] == 0) {
1809 1810 1811 1812 1813 1814 1815
                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 已提交
1816
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1817 1818
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1819
            if (ecdh_c[i][0] == 0) {
1820 1821 1822 1823 1824 1825 1826
                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 已提交
1827
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1828 1829
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1830
            if (ecdh_c[i][0] == 0) {
1831 1832 1833 1834
                ecdh_c[i][0] = 1;
            }
        }
    }
1835
#  endif
B
Bodo Möller 已提交
1836

1837
# else
1838 1839
/* not worth fixing */
#  error "You cannot disable DES on systems without SIGALRM."
1840
# endif                         /* OPENSSL_NO_DES */
1841 1842
#else
# ifndef _WIN32
1843
    signal(SIGALRM, sig_done);
1844
# endif
1845
#endif                          /* SIGALRM */
1846

1847
#ifndef OPENSSL_NO_MD2
1848
    if (doit[D_MD2]) {
1849 1850
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
1851
            Time_F(START);
1852
            count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1853
            d = Time_F(STOP);
1854
            print_result(D_MD2, testnum, count, d);
1855 1856
        }
    }
1857 1858
#endif
#ifndef OPENSSL_NO_MDC2
1859
    if (doit[D_MDC2]) {
1860 1861
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
1862
            Time_F(START);
1863
            count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1864
            d = Time_F(STOP);
1865
            print_result(D_MDC2, testnum, count, d);
1866 1867
        }
    }
1868
#endif
1869

1870
#ifndef OPENSSL_NO_MD4
1871
    if (doit[D_MD4]) {
1872 1873
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
1874
            Time_F(START);
1875
            count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1876
            d = Time_F(STOP);
1877
            print_result(D_MD4, testnum, count, d);
1878 1879
        }
    }
1880
#endif
1881

1882
#ifndef OPENSSL_NO_MD5
1883
    if (doit[D_MD5]) {
1884 1885
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);
1886
            Time_F(START);
1887
            count = run_benchmark(async_jobs, MD5_loop, loopargs);
1888
            d = Time_F(STOP);
1889
            print_result(D_MD5, testnum, count, d);
1890 1891
        }
    }
1892

1893
    if (doit[D_HMAC]) {
F
FdaSilvaYY 已提交
1894
        static const char hmac_key[] = "This is a key...";
1895 1896
        int len = strlen(hmac_key);

1897
        for (i = 0; i < loopargs_len; i++) {
1898 1899 1900 1901 1902
            loopargs[i].hctx = HMAC_CTX_new();
            if (loopargs[i].hctx == NULL) {
                BIO_printf(bio_err, "HMAC malloc failure, exiting...");
                exit(1);
            }
1903

1904
            HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
1905
        }
1906 1907
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);
1908
            Time_F(START);
1909
            count = run_benchmark(async_jobs, HMAC_loop, loopargs);
1910
            d = Time_F(STOP);
1911 1912
            print_result(D_HMAC, testnum, count, d);
        }
1913
        for (i = 0; i < loopargs_len; i++) {
1914
            HMAC_CTX_free(loopargs[i].hctx);
1915 1916
        }
    }
1917
#endif
1918
    if (doit[D_SHA1]) {
1919 1920
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);
1921
            Time_F(START);
1922
            count = run_benchmark(async_jobs, SHA1_loop, loopargs);
1923
            d = Time_F(STOP);
1924
            print_result(D_SHA1, testnum, count, d);
1925 1926 1927
        }
    }
    if (doit[D_SHA256]) {
1928
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1929 1930
            print_message(names[D_SHA256], c[D_SHA256][testnum],
                          lengths[testnum]);
1931
            Time_F(START);
1932
            count = run_benchmark(async_jobs, SHA256_loop, loopargs);
1933
            d = Time_F(STOP);
1934
            print_result(D_SHA256, testnum, count, d);
1935 1936 1937
        }
    }
    if (doit[D_SHA512]) {
1938
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1939 1940
            print_message(names[D_SHA512], c[D_SHA512][testnum],
                          lengths[testnum]);
1941
            Time_F(START);
1942
            count = run_benchmark(async_jobs, SHA512_loop, loopargs);
1943
            d = Time_F(STOP);
1944
            print_result(D_SHA512, testnum, count, d);
1945 1946
        }
    }
1947
#ifndef OPENSSL_NO_WHIRLPOOL
1948
    if (doit[D_WHIRLPOOL]) {
1949
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1950 1951
            print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
                          lengths[testnum]);
1952
            Time_F(START);
1953
            count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
1954
            d = Time_F(STOP);
1955
            print_result(D_WHIRLPOOL, testnum, count, d);
1956 1957
        }
    }
1958
#endif
1959

1960
#ifndef OPENSSL_NO_RMD160
1961
    if (doit[D_RMD160]) {
1962
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1963 1964
            print_message(names[D_RMD160], c[D_RMD160][testnum],
                          lengths[testnum]);
1965
            Time_F(START);
1966
            count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
1967
            d = Time_F(STOP);
1968
            print_result(D_RMD160, testnum, count, d);
1969 1970
        }
    }
1971 1972
#endif
#ifndef OPENSSL_NO_RC4
1973
    if (doit[D_RC4]) {
1974 1975
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
1976
            Time_F(START);
1977
            count = run_benchmark(async_jobs, RC4_loop, loopargs);
1978
            d = Time_F(STOP);
1979
            print_result(D_RC4, testnum, count, d);
1980 1981
        }
    }
1982 1983
#endif
#ifndef OPENSSL_NO_DES
1984
    if (doit[D_CBC_DES]) {
1985
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1986 1987
            print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
                          lengths[testnum]);
1988
            Time_F(START);
1989
            count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
1990
            d = Time_F(STOP);
1991
            print_result(D_CBC_DES, testnum, count, d);
1992 1993
        }
    }
1994

1995
    if (doit[D_EDE3_DES]) {
1996
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1997 1998
            print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
                          lengths[testnum]);
1999
            Time_F(START);
2000 2001
            count =
                run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2002
            d = Time_F(STOP);
2003
            print_result(D_EDE3_DES, testnum, count, d);
2004 2005
        }
    }
2006
#endif
M
Matt Caswell 已提交
2007

2008
    if (doit[D_CBC_128_AES]) {
2009 2010 2011
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
                          lengths[testnum]);
2012
            Time_F(START);
2013 2014
            count =
                run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2015
            d = Time_F(STOP);
2016
            print_result(D_CBC_128_AES, testnum, count, d);
2017 2018 2019
        }
    }
    if (doit[D_CBC_192_AES]) {
2020 2021 2022
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
                          lengths[testnum]);
2023
            Time_F(START);
2024 2025
            count =
                run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2026
            d = Time_F(STOP);
2027
            print_result(D_CBC_192_AES, testnum, count, d);
2028 2029 2030
        }
    }
    if (doit[D_CBC_256_AES]) {
2031 2032 2033
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
                          lengths[testnum]);
2034
            Time_F(START);
2035 2036
            count =
                run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2037
            d = Time_F(STOP);
2038
            print_result(D_CBC_256_AES, testnum, count, d);
2039 2040
        }
    }
B
Ben Laurie 已提交
2041

2042
    if (doit[D_IGE_128_AES]) {
2043 2044 2045
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
                          lengths[testnum]);
2046
            Time_F(START);
2047 2048
            count =
                run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2049
            d = Time_F(STOP);
2050
            print_result(D_IGE_128_AES, testnum, count, d);
2051 2052 2053
        }
    }
    if (doit[D_IGE_192_AES]) {
2054 2055 2056
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
                          lengths[testnum]);
2057
            Time_F(START);
2058 2059
            count =
                run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2060
            d = Time_F(STOP);
2061
            print_result(D_IGE_192_AES, testnum, count, d);
2062 2063 2064
        }
    }
    if (doit[D_IGE_256_AES]) {
2065 2066 2067
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
                          lengths[testnum]);
2068
            Time_F(START);
2069 2070
            count =
                run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2071
            d = Time_F(STOP);
2072
            print_result(D_IGE_256_AES, testnum, count, d);
2073 2074 2075
        }
    }
    if (doit[D_GHASH]) {
2076
        for (i = 0; i < loopargs_len; i++) {
2077 2078 2079 2080
            loopargs[i].gcm_ctx =
                CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
            CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
                                (unsigned char *)"0123456789ab", 12);
2081
        }
2082

2083
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2084 2085
            print_message(names[D_GHASH], c[D_GHASH][testnum],
                          lengths[testnum]);
2086
            Time_F(START);
2087
            count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2088
            d = Time_F(STOP);
2089
            print_result(D_GHASH, testnum, count, d);
2090
        }
2091
        for (i = 0; i < loopargs_len; i++)
2092
            CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2093
    }
2094
#ifndef OPENSSL_NO_CAMELLIA
2095
    if (doit[D_CBC_128_CML]) {
2096 2097 2098 2099 2100 2101
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2102 2103
            print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
                          lengths[testnum]);
2104
            Time_F(START);
2105 2106
            for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2107
                                     (size_t)lengths[testnum], &camellia_ks1,
2108 2109
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2110
            print_result(D_CBC_128_CML, testnum, count, d);
2111 2112 2113
        }
    }
    if (doit[D_CBC_192_CML]) {
2114 2115 2116 2117 2118 2119
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2120 2121 2122 2123 2124 2125
            print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
                          lengths[testnum]);
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2126
            Time_F(START);
2127 2128
            for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2129
                                     (size_t)lengths[testnum], &camellia_ks2,
2130 2131
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2132
            print_result(D_CBC_192_CML, testnum, count, d);
2133 2134 2135
        }
    }
    if (doit[D_CBC_256_CML]) {
2136 2137 2138 2139 2140 2141
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2142 2143
            print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
                          lengths[testnum]);
2144
            Time_F(START);
2145 2146
            for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2147
                                     (size_t)lengths[testnum], &camellia_ks3,
2148 2149
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2150
            print_result(D_CBC_256_CML, testnum, count, d);
2151 2152
        }
    }
2153 2154
#endif
#ifndef OPENSSL_NO_IDEA
2155
    if (doit[D_CBC_IDEA]) {
2156 2157 2158 2159 2160 2161
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2162 2163
            print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
                          lengths[testnum]);
2164
            Time_F(START);
2165
            for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
R
Rich Salz 已提交
2166
                IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2167
                                 (size_t)lengths[testnum], &idea_ks,
2168 2169
                                 iv, IDEA_ENCRYPT);
            d = Time_F(STOP);
2170
            print_result(D_CBC_IDEA, testnum, count, d);
2171 2172
        }
    }
2173 2174
#endif
#ifndef OPENSSL_NO_SEED
2175
    if (doit[D_CBC_SEED]) {
2176 2177 2178 2179 2180 2181
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2182 2183
            print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
                          lengths[testnum]);
2184
            Time_F(START);
2185 2186
            for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
                SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2187
                                 (size_t)lengths[testnum], &seed_ks, iv, 1);
2188
            d = Time_F(STOP);
2189
            print_result(D_CBC_SEED, testnum, count, d);
2190 2191
        }
    }
2192 2193
#endif
#ifndef OPENSSL_NO_RC2
2194
    if (doit[D_CBC_RC2]) {
2195 2196 2197 2198 2199 2200
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2201 2202
            print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
                          lengths[testnum]);
2203 2204 2205 2206
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2207
            Time_F(START);
2208 2209
            for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
                RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2210
                                (size_t)lengths[testnum], &rc2_ks,
2211 2212
                                iv, RC2_ENCRYPT);
            d = Time_F(STOP);
2213
            print_result(D_CBC_RC2, testnum, count, d);
2214 2215
        }
    }
2216 2217
#endif
#ifndef OPENSSL_NO_RC5
2218
    if (doit[D_CBC_RC5]) {
2219 2220 2221 2222 2223 2224
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2225 2226
            print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
                          lengths[testnum]);
2227 2228 2229 2230
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2231
            Time_F(START);
2232 2233
            for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
                RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2234
                                   (size_t)lengths[testnum], &rc5_ks,
2235 2236
                                   iv, RC5_ENCRYPT);
            d = Time_F(STOP);
2237
            print_result(D_CBC_RC5, testnum, count, d);
2238 2239
        }
    }
2240 2241
#endif
#ifndef OPENSSL_NO_BF
2242
    if (doit[D_CBC_BF]) {
2243 2244 2245 2246 2247 2248
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2249 2250
            print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
                          lengths[testnum]);
2251
            Time_F(START);
2252 2253
            for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
                BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2254
                               (size_t)lengths[testnum], &bf_ks,
2255 2256
                               iv, BF_ENCRYPT);
            d = Time_F(STOP);
2257
            print_result(D_CBC_BF, testnum, count, d);
2258 2259
        }
    }
2260 2261
#endif
#ifndef OPENSSL_NO_CAST
2262
    if (doit[D_CBC_CAST]) {
2263 2264 2265 2266 2267 2268
        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;
        }
        for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
2269 2270
            print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
                          lengths[testnum]);
2271
            Time_F(START);
2272 2273
            for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
                CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2274
                                 (size_t)lengths[testnum], &cast_ks,
2275 2276
                                 iv, CAST_ENCRYPT);
            d = Time_F(STOP);
2277
            print_result(D_CBC_CAST, testnum, count, d);
2278 2279
        }
    }
2280
#endif
2281

2282 2283 2284 2285 2286
    if (doit[D_EVP]) {
        if (multiblock && evp_cipher) {
            if (!
                (EVP_CIPHER_flags(evp_cipher) &
                 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
R
Rich Salz 已提交
2287
                BIO_printf(bio_err, "%s is not multi-block capable\n",
2288
                           OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2289 2290
                goto end;
            }
2291 2292 2293 2294
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2295
            multiblock_speed(evp_cipher);
2296
            ret = 0;
2297 2298
            goto end;
        }
2299
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2300 2301
            if (evp_cipher) {

2302
                names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2303 2304 2305 2306
                /*
                 * -O3 -fschedule-insns messes up an optimization here!
                 * names[D_EVP] somehow becomes NULL
                 */
2307 2308 2309 2310 2311
                print_message(names[D_EVP], save_count, lengths[testnum]);

                for (k = 0; k < loopargs_len; k++) {
                    loopargs[k].ctx = EVP_CIPHER_CTX_new();
                    if (decrypt)
2312 2313
                        EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
                                           key16, iv);
2314
                    else
2315 2316
                        EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
                                           key16, iv);
2317 2318
                    EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
                }
2319 2320

                Time_F(START);
2321
                count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
2322
                d = Time_F(STOP);
2323 2324 2325
                for (k = 0; k < loopargs_len; k++) {
                    EVP_CIPHER_CTX_free(loopargs[k].ctx);
                }
2326 2327
            }
            if (evp_md) {
2328
                names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2329
                print_message(names[D_EVP], save_count, lengths[testnum]);
2330
                Time_F(START);
2331
                count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2332 2333
                d = Time_F(STOP);
            }
2334
            print_result(D_EVP, testnum, count, d);
2335 2336
        }
    }
2337

2338
    for (i = 0; i < loopargs_len; i++)
2339 2340
        RAND_bytes(loopargs[i].buf, 36);

2341
#ifndef OPENSSL_NO_RSA
2342 2343 2344
    for (testnum = 0; testnum < RSA_NUM; testnum++) {
        int st = 0;
        if (!rsa_doit[testnum])
2345
            continue;
2346 2347
        for (i = 0; i < loopargs_len; i++) {
            st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2348
                          &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2349 2350 2351
            if (st == 0)
                break;
        }
2352
        if (st == 0) {
2353 2354 2355 2356 2357 2358
            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",
2359 2360
                               rsa_c[testnum][0], rsa_bits[testnum],
                               RSA_SECONDS);
2361
            /* RSA_blinding_on(rsa_key[testnum],NULL); */
2362
            Time_F(START);
2363
            count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2364 2365 2366 2367
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R1:%ld:%d:%.2f\n"
                       : "%ld %d bit private RSA's in %.2fs\n",
2368
                       count, rsa_bits[testnum], d);
2369
            rsa_results[testnum][0] = (double)count / d;
2370 2371
            rsa_count = count;
        }
2372

2373 2374
        for (i = 0; i < loopargs_len; i++) {
            st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2375
                            loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2376 2377 2378
            if (st <= 0)
                break;
        }
2379
        if (st <= 0) {
2380 2381 2382
            BIO_printf(bio_err,
                       "RSA verify failure.  No RSA verify will be done.\n");
            ERR_print_errors(bio_err);
2383
            rsa_doit[testnum] = 0;
2384 2385
        } else {
            pkey_print_message("public", "rsa",
2386 2387
                               rsa_c[testnum][1], rsa_bits[testnum],
                               RSA_SECONDS);
2388
            Time_F(START);
2389
            count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2390 2391 2392 2393
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R2:%ld:%d:%.2f\n"
                       : "%ld %d bit public RSA's in %.2fs\n",
2394
                       count, rsa_bits[testnum], d);
2395
            rsa_results[testnum][1] = (double)count / d;
2396
        }
2397

2398 2399
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2400 2401
            for (testnum++; testnum < RSA_NUM; testnum++)
                rsa_doit[testnum] = 0;
2402 2403
        }
    }
F
FdaSilvaYY 已提交
2404
#endif                          /* OPENSSL_NO_RSA */
2405

2406
    for (i = 0; i < loopargs_len; i++)
2407 2408
        RAND_bytes(loopargs[i].buf, 36);

2409
#ifndef OPENSSL_NO_DSA
2410 2411 2412
    if (RAND_status() != 1) {
        RAND_seed(rnd_seed, sizeof rnd_seed);
    }
2413 2414 2415
    for (testnum = 0; testnum < DSA_NUM; testnum++) {
        int st = 0;
        if (!dsa_doit[testnum])
2416 2417
            continue;

2418 2419
        /* DSA_generate_key(dsa_key[testnum]); */
        /* DSA_sign_setup(dsa_key[testnum],NULL); */
2420 2421
        for (i = 0; i < loopargs_len; i++) {
            st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2422
                          &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2423 2424 2425
            if (st == 0)
                break;
        }
2426
        if (st == 0) {
2427 2428 2429 2430 2431 2432
            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",
2433 2434
                               dsa_c[testnum][0], dsa_bits[testnum],
                               DSA_SECONDS);
2435
            Time_F(START);
2436
            count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2437 2438 2439 2440
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R3:%ld:%d:%.2f\n"
                       : "%ld %d bit DSA signs in %.2fs\n",
2441
                       count, dsa_bits[testnum], d);
2442
            dsa_results[testnum][0] = (double)count / d;
2443 2444
            rsa_count = count;
        }
B
Bodo Möller 已提交
2445

2446 2447
        for (i = 0; i < loopargs_len; i++) {
            st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2448
                            loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2449 2450 2451
            if (st <= 0)
                break;
        }
2452
        if (st <= 0) {
2453 2454 2455
            BIO_printf(bio_err,
                       "DSA verify failure.  No DSA verify will be done.\n");
            ERR_print_errors(bio_err);
2456
            dsa_doit[testnum] = 0;
2457 2458
        } else {
            pkey_print_message("verify", "dsa",
2459 2460
                               dsa_c[testnum][1], dsa_bits[testnum],
                               DSA_SECONDS);
2461
            Time_F(START);
2462
            count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2463 2464 2465 2466
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R4:%ld:%d:%.2f\n"
                       : "%ld %d bit DSA verify in %.2fs\n",
2467
                       count, dsa_bits[testnum], d);
2468
            dsa_results[testnum][1] = (double)count / d;
2469
        }
B
Bodo Möller 已提交
2470

2471 2472
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2473 2474
            for (testnum++; testnum < DSA_NUM; testnum++)
                dsa_doit[testnum] = 0;
2475 2476
        }
    }
F
FdaSilvaYY 已提交
2477
#endif                          /* OPENSSL_NO_DSA */
B
Bodo Möller 已提交
2478

2479
#ifndef OPENSSL_NO_EC
2480 2481 2482
    if (RAND_status() != 1) {
        RAND_seed(rnd_seed, sizeof rnd_seed);
    }
2483
    for (testnum = 0; testnum < EC_NUM; testnum++) {
2484
        int st = 1;
2485

2486
        if (!ecdsa_doit[testnum])
2487
            continue;           /* Ignore Curve */
2488
        for (i = 0; i < loopargs_len; i++) {
2489 2490
            loopargs[i].ecdsa[testnum] =
                EC_KEY_new_by_curve_name(test_curves[testnum]);
2491 2492 2493 2494 2495 2496
            if (loopargs[i].ecdsa[testnum] == NULL) {
                st = 0;
                break;
            }
        }
        if (st == 0) {
2497 2498 2499 2500
            BIO_printf(bio_err, "ECDSA failure.\n");
            ERR_print_errors(bio_err);
            rsa_count = 1;
        } else {
2501 2502 2503 2504 2505
            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,
2506 2507
                                &loopargs[i].siglen,
                                loopargs[i].ecdsa[testnum]);
2508 2509 2510
                if (st == 0)
                    break;
            }
2511
            if (st == 0) {
2512 2513 2514 2515 2516 2517
                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",
2518 2519
                                   ecdsa_c[testnum][0],
                                   test_curves_bits[testnum], ECDSA_SECONDS);
2520
                Time_F(START);
2521
                count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2522 2523 2524 2525 2526
                d = Time_F(STOP);

                BIO_printf(bio_err,
                           mr ? "+R5:%ld:%d:%.2f\n" :
                           "%ld %d bit ECDSA signs in %.2fs \n",
2527
                           count, test_curves_bits[testnum], d);
2528
                ecdsa_results[testnum][0] = (double)count / d;
2529 2530 2531 2532
                rsa_count = count;
            }

            /* Perform ECDSA verification test */
2533 2534
            for (i = 0; i < loopargs_len; i++) {
                st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2535 2536
                                  loopargs[i].siglen,
                                  loopargs[i].ecdsa[testnum]);
2537 2538 2539
                if (st != 1)
                    break;
            }
2540
            if (st != 1) {
2541 2542 2543
                BIO_printf(bio_err,
                           "ECDSA verify failure.  No ECDSA verify will be done.\n");
                ERR_print_errors(bio_err);
2544
                ecdsa_doit[testnum] = 0;
2545 2546
            } else {
                pkey_print_message("verify", "ecdsa",
2547 2548
                                   ecdsa_c[testnum][1],
                                   test_curves_bits[testnum], ECDSA_SECONDS);
2549
                Time_F(START);
2550
                count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2551 2552 2553 2554
                d = Time_F(STOP);
                BIO_printf(bio_err,
                           mr ? "+R6:%ld:%d:%.2f\n"
                           : "%ld %d bit ECDSA verify in %.2fs\n",
2555
                           count, test_curves_bits[testnum], d);
2556
                ecdsa_results[testnum][1] = (double)count / d;
2557 2558 2559 2560
            }

            if (rsa_count <= 1) {
                /* if longer than 10s, don't do any more */
2561 2562
                for (testnum++; testnum < EC_NUM; testnum++)
                    ecdsa_doit[testnum] = 0;
2563 2564 2565
            }
        }
    }
2566

2567 2568 2569
    if (RAND_status() != 1) {
        RAND_seed(rnd_seed, sizeof rnd_seed);
    }
2570
    for (testnum = 0; testnum < EC_NUM; testnum++) {
2571 2572
        int ecdh_checks = 1;

2573
        if (!ecdh_doit[testnum])
2574
            continue;
2575

2576
        for (i = 0; i < loopargs_len; i++) {
N
Nicola Tuveri 已提交
2577
            EVP_PKEY_CTX *kctx = NULL;
2578
            EVP_PKEY_CTX *test_ctx = NULL;
N
Nicola Tuveri 已提交
2579 2580 2581
            EVP_PKEY_CTX *ctx = NULL;
            EVP_PKEY *key_A = NULL;
            EVP_PKEY *key_B = NULL;
2582
            size_t outlen;
2583
            size_t test_outlen;
2584

2585 2586 2587 2588 2589 2590 2591
            /* 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);
            }

2592 2593 2594 2595 2596 2597 2598 2599
            /* 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. */
            kctx = EVP_PKEY_CTX_new_id(test_curves[testnum], NULL); /* keygen ctx from NID */
            if (!kctx) {
2600 2601 2602
                EVP_PKEY_CTX *pctx = NULL;
                EVP_PKEY *params = NULL;

2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
                /* 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;
                }

2622 2623 2624 2625 2626 2627 2628 2629 2630
                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
                                                               [testnum]) ||
                       /* Create the parameter object params */
N
Nicola Tuveri 已提交
2631
                       !EVP_PKEY_paramgen(pctx, &params)) {
2632
                    ecdh_checks = 0;
2633
                    BIO_printf(bio_err, "ECDH EC params init failure.\n");
2634
                    ERR_print_errors(bio_err);
2635
                    rsa_count = 1;
2636
                    break;
2637
                }
2638 2639 2640
                /* Create the context for the key generation */
                kctx = EVP_PKEY_CTX_new(params, NULL);

2641 2642 2643 2644
                EVP_PKEY_free(params);
                params = NULL;
                EVP_PKEY_CTX_free(pctx);
                pctx = NULL;
A
Andrea Grandi 已提交
2645
            }
2646
            if (!kctx ||        /* keygen ctx is not null */
N
Nicola Tuveri 已提交
2647
                !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
2648 2649 2650 2651 2652
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH keygen failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
2653
            }
2654

2655 2656 2657 2658 2659 2660
            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 */
2661
                outlen == 0 ||  /* ensure outlen is a valid size */
N
Nicola Tuveri 已提交
2662
                outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
2663 2664 2665 2666 2667 2668 2669
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH key generation failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
            /* 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;
            }
2687 2688 2689 2690 2691

            /* 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;
2692 2693 2694 2695 2696 2697
                BIO_printf(bio_err, "ECDH computations don't match.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

2698
            loopargs[i].ecdh_ctx[testnum] = ctx;
2699
            loopargs[i].outlen[testnum] = outlen;
2700

2701 2702
            EVP_PKEY_CTX_free(kctx);
            kctx = NULL;
2703 2704
            EVP_PKEY_CTX_free(test_ctx);
            test_ctx = NULL;
2705 2706 2707
        }
        if (ecdh_checks != 0) {
            pkey_print_message("", "ecdh",
2708 2709
                               ecdh_c[testnum][0],
                               test_curves_bits[testnum], ECDH_SECONDS);
2710
            Time_F(START);
2711 2712
            count =
                run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
2713 2714
            d = Time_F(STOP);
            BIO_printf(bio_err,
2715 2716 2717
                       mr ? "+R7:%ld:%d:%.2f\n" :
                       "%ld %d-bit ECDH ops in %.2fs\n", count,
                       test_curves_bits[testnum], d);
2718
            ecdh_results[testnum][0] = (double)count / d;
2719
            rsa_count = count;
2720
        }
B
Bodo Möller 已提交
2721

2722 2723
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2724 2725
            for (testnum++; testnum < EC_NUM; testnum++)
                ecdh_doit[testnum] = 0;
2726 2727
        }
    }
F
FdaSilvaYY 已提交
2728
#endif                          /* OPENSSL_NO_EC */
2729
#ifndef NO_FORK
2730
 show_res:
2731
#endif
2732
    if (!mr) {
R
Rich Salz 已提交
2733 2734
        printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
        printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2735 2736
        printf("options:");
        printf("%s ", BN_options());
2737
#ifndef OPENSSL_NO_MD2
2738
        printf("%s ", MD2_options());
2739 2740
#endif
#ifndef OPENSSL_NO_RC4
2741
        printf("%s ", RC4_options());
2742 2743
#endif
#ifndef OPENSSL_NO_DES
2744
        printf("%s ", DES_options());
2745
#endif
2746
        printf("%s ", AES_options());
2747
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
2748
        printf("%s ", IDEA_options());
2749 2750
#endif
#ifndef OPENSSL_NO_BF
2751
        printf("%s ", BF_options());
2752
#endif
R
Rich Salz 已提交
2753
        printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2754
    }
B
Bodo Möller 已提交
2755

2756 2757
    if (pr_header) {
        if (mr)
2758
            printf("+H");
2759
        else {
2760 2761 2762
            printf
                ("The 'numbers' are in 1000s of bytes per second processed.\n");
            printf("type        ");
2763
        }
2764 2765
        for (testnum = 0; testnum < SIZE_NUM; testnum++)
            printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2766
        printf("\n");
2767
    }
B
Bodo Möller 已提交
2768

2769 2770 2771 2772
    for (k = 0; k < ALGOR_NUM; k++) {
        if (!doit[k])
            continue;
        if (mr)
2773
            printf("+F:%d:%s", k, names[k]);
2774
        else
2775
            printf("%-13s", names[k]);
2776 2777 2778
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            if (results[k][testnum] > 10000 && !mr)
                printf(" %11.2fk", results[k][testnum] / 1e3);
2779
            else
2780
                printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2781
        }
2782
        printf("\n");
2783
    }
2784
#ifndef OPENSSL_NO_RSA
2785
    testnum = 1;
2786 2787 2788
    for (k = 0; k < RSA_NUM; k++) {
        if (!rsa_doit[k])
            continue;
2789
        if (testnum && !mr) {
2790
            printf("%18ssign    verify    sign/s verify/s\n", " ");
2791
            testnum = 0;
2792 2793
        }
        if (mr)
2794 2795
            printf("+F2:%u:%u:%f:%f\n",
                   k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2796
        else
2797
            printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2798 2799
                   rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
                   rsa_results[k][0], rsa_results[k][1]);
2800
    }
2801 2802
#endif
#ifndef OPENSSL_NO_DSA
2803
    testnum = 1;
2804 2805 2806
    for (k = 0; k < DSA_NUM; k++) {
        if (!dsa_doit[k])
            continue;
2807
        if (testnum && !mr) {
2808
            printf("%18ssign    verify    sign/s verify/s\n", " ");
2809
            testnum = 0;
2810 2811
        }
        if (mr)
2812 2813
            printf("+F3:%u:%u:%f:%f\n",
                   k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2814
        else
2815
            printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2816 2817
                   dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
                   dsa_results[k][0], dsa_results[k][1]);
2818
    }
2819
#endif
2820
#ifndef OPENSSL_NO_EC
2821
    testnum = 1;
2822 2823 2824
    for (k = 0; k < EC_NUM; k++) {
        if (!ecdsa_doit[k])
            continue;
2825
        if (testnum && !mr) {
2826
            printf("%30ssign    verify    sign/s verify/s\n", " ");
2827
            testnum = 0;
2828 2829 2830
        }

        if (mr)
2831 2832 2833
            printf("+F4:%u:%u:%f:%f\n",
                   k, test_curves_bits[k],
                   ecdsa_results[k][0], ecdsa_results[k][1]);
2834
        else
2835 2836 2837
            printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
                   test_curves_bits[k],
                   test_curves_names[k],
2838 2839
                   1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
                   ecdsa_results[k][0], ecdsa_results[k][1]);
2840
    }
2841

2842
    testnum = 1;
2843 2844 2845
    for (k = 0; k < EC_NUM; k++) {
        if (!ecdh_doit[k])
            continue;
2846
        if (testnum && !mr) {
2847
            printf("%30sop      op/s\n", " ");
2848
            testnum = 0;
2849 2850
        }
        if (mr)
2851 2852 2853
            printf("+F5:%u:%u:%f:%f\n",
                   k, test_curves_bits[k],
                   ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2854 2855

        else
2856 2857 2858
            printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
                   test_curves_bits[k],
                   test_curves_names[k],
2859
                   1.0 / ecdh_results[k][0], ecdh_results[k][0]);
2860
    }
2861
#endif
2862

2863
    ret = 0;
2864 2865 2866

 end:
    ERR_print_errors(bio_err);
2867
    for (i = 0; i < loopargs_len; i++) {
2868 2869
        OPENSSL_free(loopargs[i].buf_malloc);
        OPENSSL_free(loopargs[i].buf2_malloc);
2870

2871
#ifndef OPENSSL_NO_RSA
2872 2873
        for (k = 0; k < RSA_NUM; k++)
            RSA_free(loopargs[i].rsa_key[k]);
2874 2875
#endif
#ifndef OPENSSL_NO_DSA
2876 2877
        for (k = 0; k < DSA_NUM; k++)
            DSA_free(loopargs[i].dsa_key[k]);
2878
#endif
2879
#ifndef OPENSSL_NO_EC
2880 2881
        for (k = 0; k < EC_NUM; k++) {
            EC_KEY_free(loopargs[i].ecdsa[k]);
2882
            EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
2883
        }
2884 2885
        OPENSSL_free(loopargs[i].secret_a);
        OPENSSL_free(loopargs[i].secret_b);
2886
#endif
2887 2888
    }

2889 2890 2891
    if (async_jobs > 0) {
        for (i = 0; i < loopargs_len; i++)
            ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
2892
    }
2893

2894
    if (async_init) {
2895
        ASYNC_cleanup_thread();
2896 2897
    }
    OPENSSL_free(loopargs);
2898
    release_engine(e);
2899
    return (ret);
2900
}
2901

2902
static void print_message(const char *s, long num, int length)
2903
{
2904
#ifdef SIGALRM
2905 2906 2907 2908 2909
    BIO_printf(bio_err,
               mr ? "+DT:%s:%d:%d\n"
               : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
    (void)BIO_flush(bio_err);
    alarm(SECONDS);
2910
#else
2911 2912 2913 2914
    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);
2915
#endif
2916
}
2917

2918
static void pkey_print_message(const char *str, const char *str2, long num,
2919 2920
                               int bits, int tm)
{
2921
#ifdef SIGALRM
2922 2923 2924 2925 2926
    BIO_printf(bio_err,
               mr ? "+DTP:%d:%s:%s:%d\n"
               : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
    (void)BIO_flush(bio_err);
    alarm(tm);
2927
#else
2928 2929 2930 2931
    BIO_printf(bio_err,
               mr ? "+DNP:%ld:%d:%s:%s\n"
               : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
    (void)BIO_flush(bio_err);
2932
#endif
2933
}
2934

2935 2936
static void print_result(int alg, int run_no, int count, double time_used)
{
2937 2938 2939 2940
    if (count == -1) {
        BIO_puts(bio_err, "EVP error!\n");
        exit(1);
    }
2941 2942 2943 2944 2945
    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];
}
2946

2947
#ifndef NO_FORK
2948
static char *sstrsep(char **string, const char *delim)
2949
{
2950 2951 2952 2953 2954 2955
    char isdelim[256];
    char *token = *string;

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

2956
    memset(isdelim, 0, sizeof isdelim);
2957 2958
    isdelim[0] = 1;

2959
    while (*delim) {
2960 2961
        isdelim[(unsigned char)(*delim)] = 1;
        delim++;
2962
    }
2963

2964
    while (!isdelim[(unsigned char)(**string)]) {
2965
        (*string)++;
2966
    }
2967

2968
    if (**string) {
2969 2970
        **string = 0;
        (*string)++;
2971
    }
2972 2973

    return token;
2974
}
2975 2976

static int do_multi(int multi)
2977 2978 2979 2980 2981 2982
{
    int n;
    int fd[2];
    int *fds;
    static char sep[] = ":";

R
Rich Salz 已提交
2983
    fds = malloc(sizeof(*fds) * multi);
2984 2985
    for (n = 0; n < multi; ++n) {
        if (pipe(fd) == -1) {
R
Rich Salz 已提交
2986
            BIO_printf(bio_err, "pipe failure\n");
2987 2988 2989
            exit(1);
        }
        fflush(stdout);
R
Rich Salz 已提交
2990
        (void)BIO_flush(bio_err);
2991 2992 2993 2994 2995 2996 2997
        if (fork()) {
            close(fd[1]);
            fds[n] = fd[0];
        } else {
            close(fd[0]);
            close(1);
            if (dup(fd[1]) == -1) {
R
Rich Salz 已提交
2998
                BIO_printf(bio_err, "dup failed\n");
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
                exit(1);
            }
            close(fd[1]);
            mr = 1;
            usertime = 0;
            free(fds);
            return 0;
        }
        printf("Forked child %d\n", n);
    }
B
Bodo Möller 已提交
3009

3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021
    /* 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");
        while (fgets(buf, sizeof buf, f)) {
            p = strchr(buf, '\n');
            if (p)
                *p = '\0';
            if (buf[0] != '+') {
3022 3023 3024
                BIO_printf(bio_err,
                           "Don't understand line '%s' from child %d\n", buf,
                           n);
3025 3026 3027
                continue;
            }
            printf("Got: %s from %d\n", buf, n);
R
Rich Salz 已提交
3028
            if (strncmp(buf, "+F:", 3) == 0) {
3029 3030 3031 3032 3033 3034 3035 3036
                int alg;
                int j;

                p = buf + 3;
                alg = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);
                for (j = 0; j < SIZE_NUM; ++j)
                    results[alg][j] += atof(sstrsep(&p, sep));
R
Rich Salz 已提交
3037
            } else if (strncmp(buf, "+F2:", 4) == 0) {
3038 3039 3040 3041 3042 3043 3044 3045
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3046
                rsa_results[k][0] += d;
3047 3048

                d = atof(sstrsep(&p, sep));
3049
                rsa_results[k][1] += d;
3050
            }
3051
# ifndef OPENSSL_NO_DSA
R
Rich Salz 已提交
3052
            else if (strncmp(buf, "+F3:", 4) == 0) {
3053 3054 3055 3056 3057 3058 3059 3060
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3061
                dsa_results[k][0] += d;
3062 3063

                d = atof(sstrsep(&p, sep));
3064
                dsa_results[k][1] += d;
3065
            }
3066
# endif
3067
# ifndef OPENSSL_NO_EC
R
Rich Salz 已提交
3068
            else if (strncmp(buf, "+F4:", 4) == 0) {
3069 3070 3071 3072 3073 3074 3075 3076
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3077
                ecdsa_results[k][0] += d;
3078 3079

                d = atof(sstrsep(&p, sep));
3080
                ecdsa_results[k][1] += d;
F
FdaSilvaYY 已提交
3081
            } else if (strncmp(buf, "+F5:", 4) == 0) {
3082 3083 3084 3085 3086 3087 3088 3089
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3090
                ecdh_results[k][0] += d;
3091
            }
3092
# endif
3093

R
Rich Salz 已提交
3094
            else if (strncmp(buf, "+H:", 3) == 0) {
3095
                ;
3096
            } else
3097 3098
                BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
                           n);
3099 3100 3101 3102 3103 3104 3105
        }

        fclose(f);
    }
    free(fds);
    return 1;
}
3106
#endif
3107 3108

static void multiblock_speed(const EVP_CIPHER *evp_cipher)
3109 3110 3111
{
    static int mblengths[] =
        { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3112
    int j, count, num = OSSL_NELEM(mblengths);
3113 3114
    const char *alg_name;
    unsigned char *inp, *out, no_key[32], no_iv[16];
3115
    EVP_CIPHER_CTX *ctx;
3116 3117
    double d = 0.0;

R
Rich Salz 已提交
3118 3119
    inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
    out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3120 3121
    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3122
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
3123
    alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3124 3125 3126 3127 3128

    for (j = 0; j < num; j++) {
        print_message(alg_name, 0, mblengths[j]);
        Time_F(START);
        for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3129
            unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
            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;

3145
            packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3146 3147 3148 3149 3150 3151
                                          sizeof(mb_param), &mb_param);

            if (packlen > 0) {
                mb_param.out = out;
                mb_param.inp = inp;
                mb_param.len = len;
3152
                EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3153 3154 3155 3156 3157 3158 3159 3160
                                    sizeof(mb_param), &mb_param);
            } else {
                int pad;

                RAND_bytes(out, 16);
                len += 16;
                aad[11] = len >> 8;
                aad[12] = len;
3161
                pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3162
                                          EVP_AEAD_TLS1_AAD_LEN, aad);
3163
                EVP_Cipher(ctx, out, inp, len + pad);
3164 3165 3166
            }
        }
        d = Time_F(STOP);
3167
        BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198
                   : "%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 已提交
3199 3200
    OPENSSL_free(inp);
    OPENSSL_free(out);
3201
    EVP_CIPHER_CTX_free(ctx);
3202
}