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

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#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>
29
#include <openssl/async.h>
30 31 32
#if !defined(OPENSSL_SYS_MSDOS)
# include OPENSSL_UNISTD
#endif
33

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

38 39 40 41
#include <openssl/bn.h>
#ifndef OPENSSL_NO_DES
# include <openssl/des.h>
#endif
M
Matt Caswell 已提交
42
#include <openssl/aes.h>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#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
58 59
#include <openssl/hmac.h>
#include <openssl/sha.h>
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#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
96
#ifndef OPENSSL_NO_EC
D
Dr. Stephen Henson 已提交
97
# include <openssl/ec.h>
98 99
#endif
#include <openssl/modes.h>
100

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

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

#undef BUFSIZE
116
#define BUFSIZE (1024*16+1)
117
#define MAX_MISALIGNMENT 63
118

119 120 121 122 123 124 125 126 127 128
#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

129
static volatile int run = 0;
130

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

134 135
typedef struct loopargs_st {
    ASYNC_JOB *inprogress_job;
136
    ASYNC_WAIT_CTX *wait_ctx;
137 138 139 140
    unsigned char *buf;
    unsigned char *buf2;
    unsigned char *buf_malloc;
    unsigned char *buf2_malloc;
F
FdaSilvaYY 已提交
141
    unsigned int siglen;
142 143 144 145 146 147 148 149
#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];
150
    EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
151 152
    unsigned char *secret_a;
    unsigned char *secret_b;
153
    size_t outlen[EC_NUM];
154
#endif
155 156 157 158 159 160 161 162 163 164 165 166 167 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
    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
211 212
static int run_benchmark(int async_jobs, int (*loop_function) (void *),
                         loopargs_t * loopargs);
213

214
static double Time_F(int s);
215
static void print_message(const char *s, long num, int length);
216
static void pkey_print_message(const char *str, const char *str2,
217 218
                               long num, int bits, int sec);
static void print_result(int alg, int run_no, int count, double time_used);
219
#ifndef NO_FORK
220
static int do_multi(int multi);
221
#endif
222 223 224 225 226 227 228 229 230 231

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"
};
232

233
static double results[ALGOR_NUM][SIZE_NUM];
234 235

static const int lengths[SIZE_NUM] = {
236
    16, 64, 256, 1024, 8 * 1024, 16 * 1024
237
};
238

239
#ifndef OPENSSL_NO_RSA
240
static double rsa_results[RSA_NUM][2];
241 242
#endif
#ifndef OPENSSL_NO_DSA
243
static double dsa_results[DSA_NUM][2];
244
#endif
245
#ifndef OPENSSL_NO_EC
B
Bodo Möller 已提交
246 247
static double ecdsa_results[EC_NUM][2];
static double ecdh_results[EC_NUM][1];
248
#endif
B
Bodo Möller 已提交
249

250
#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
251
static const char rnd_seed[] =
R
Rich Salz 已提交
252
    "string to make the random number generator think it has randomness";
253
#endif
254

255 256 257 258 259 260
#ifdef SIGALRM
# if defined(__STDC__) || defined(sgi) || defined(_AIX)
#  define SIGRETTYPE void
# else
#  define SIGRETTYPE int
# endif
D
Dr. Stephen Henson 已提交
261

262
static SIGRETTYPE sig_done(int sig);
U
Ulf Möller 已提交
263
static SIGRETTYPE sig_done(int sig)
264 265 266 267
{
    signal(SIGALRM, sig_done);
    run = 0;
}
268
#endif
269

270 271
#define START   0
#define STOP    1
272

273
#if defined(_WIN32)
R
Richard Levitte 已提交
274

275 276 277
# if !defined(SIGALRM)
#  define SIGALRM
# endif
278 279
static unsigned int lapse;
static volatile unsigned int schlock;
280 281 282 283
static void alarm_win32(unsigned int secs)
{
    lapse = secs * 1000;
}
R
Richard Levitte 已提交
284

285
# define alarm alarm_win32
286 287 288 289 290 291 292 293

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

295
static double Time_F(int s)
296 297 298 299 300 301 302 303
{
    double ret;
    static HANDLE thr;

    if (s == START) {
        schlock = 0;
        thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
        if (thr == NULL) {
304 305
            DWORD err = GetLastError();
            BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
306
            ExitProcess(err);
307 308 309 310 311 312 313 314 315 316 317 318 319
        }
        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;
}
320
#else
321

322 323 324 325 326 327 328
static double Time_F(int s)
{
    double ret = app_tminterval(s, usertime);
    if (s == STOP)
        alarm(0);
    return ret;
}
329
#endif
330

331
static void multiblock_speed(const EVP_CIPHER *evp_cipher);
332

F
FdaSilvaYY 已提交
333
static int found(const char *name, const OPT_PAIR *pairs, int *result)
334 335 336 337 338 339 340 341 342 343 344 345
{
    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,
P
Pauli 已提交
346
    OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM
347 348
} OPTION_CHOICE;

F
FdaSilvaYY 已提交
349
const OPTIONS speed_options[] = {
350 351 352
    {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
    {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
    {"help", OPT_HELP, '-', "Display this summary"},
353 354 355 356
    {"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 已提交
357
    {"mb", OPT_MB, '-',
F
FdaSilvaYY 已提交
358
     "Enable (tls1.1) multi-block mode on evp_cipher requested with -evp"},
359
    {"misalign", OPT_MISALIGN, 'n', "Amount to mis-align buffers"},
360 361 362 363 364
    {"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
365
#ifndef OPENSSL_NO_ASYNC
F
FdaSilvaYY 已提交
366 367
    {"async_jobs", OPT_ASYNCJOBS, 'p',
     "Enable async mode and start pnum jobs"},
368
#endif
R
Rich Salz 已提交
369
    OPT_R_OPTIONS,
370 371 372
#ifndef OPENSSL_NO_ENGINE
    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
#endif
373
    {NULL},
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
};

#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
406
static OPT_PAIR doit_choices[] = {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
#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 已提交
426
#ifndef OPENSSL_NO_RMD160
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
    {"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 已提交
474 475 476 477
#ifndef OPENSSL_NO_DSA
# define R_DSA_512       0
# define R_DSA_1024      1
# define R_DSA_2048      2
478 479 480 481 482 483
static OPT_PAIR dsa_choices[] = {
    {"dsa512", R_DSA_512},
    {"dsa1024", R_DSA_1024},
    {"dsa2048", R_DSA_2048},
    {NULL},
};
M
Matt Caswell 已提交
484
#endif
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
#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
520
#define R_EC_X25519  16
R
Richard Levitte 已提交
521
#ifndef OPENSSL_NO_EC
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
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 已提交
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
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},
559
    {"ecdhx25519", R_EC_X25519},
560 561 562 563
    {NULL}
};
#endif

564 565 566 567
#ifndef SIGALRM
# define COND(d) (count < (d))
# define COUNT(d) (d)
#else
568
# define COND(unused_cond) (run && count<0x7fffffff)
569
# define COUNT(d) (count)
570
#endif                          /* SIGALRM */
571 572 573

static int testnum;

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

577
#ifndef OPENSSL_NO_MD2
578 579
static int EVP_Digest_MD2_loop(void *args)
{
580
    loopargs_t *tempargs = *(loopargs_t **) args;
581
    unsigned char *buf = tempargs->buf;
582
    unsigned char md2[MD2_DIGEST_LENGTH];
583
    int count;
584

585
    for (count = 0; COND(c[D_MD2][testnum]); count++) {
586
        if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
587
                        NULL))
588 589
            return -1;
    }
590 591
    return count;
}
592
#endif
593

594
#ifndef OPENSSL_NO_MDC2
595 596
static int EVP_Digest_MDC2_loop(void *args)
{
597
    loopargs_t *tempargs = *(loopargs_t **) args;
598
    unsigned char *buf = tempargs->buf;
599
    unsigned char mdc2[MDC2_DIGEST_LENGTH];
600
    int count;
601

602
    for (count = 0; COND(c[D_MDC2][testnum]); count++) {
603
        if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
604
                        NULL))
605 606
            return -1;
    }
607 608
    return count;
}
609
#endif
610

611
#ifndef OPENSSL_NO_MD4
612 613
static int EVP_Digest_MD4_loop(void *args)
{
614
    loopargs_t *tempargs = *(loopargs_t **) args;
615
    unsigned char *buf = tempargs->buf;
616
    unsigned char md4[MD4_DIGEST_LENGTH];
617
    int count;
618

619
    for (count = 0; COND(c[D_MD4][testnum]); count++) {
620
        if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
621
                        NULL))
622 623
            return -1;
    }
624 625
    return count;
}
626
#endif
627

628
#ifndef OPENSSL_NO_MD5
629 630
static int MD5_loop(void *args)
{
631
    loopargs_t *tempargs = *(loopargs_t **) args;
632
    unsigned char *buf = tempargs->buf;
633
    unsigned char md5[MD5_DIGEST_LENGTH];
634 635 636 637 638 639 640 641
    int count;
    for (count = 0; COND(c[D_MD5][testnum]); count++)
        MD5(buf, lengths[testnum], md5);
    return count;
}

static int HMAC_loop(void *args)
{
642
    loopargs_t *tempargs = *(loopargs_t **) args;
643 644
    unsigned char *buf = tempargs->buf;
    HMAC_CTX *hctx = tempargs->hctx;
645
    unsigned char hmac[MD5_DIGEST_LENGTH];
646
    int count;
647

648 649 650
    for (count = 0; COND(c[D_HMAC][testnum]); count++) {
        HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
        HMAC_Update(hctx, buf, lengths[testnum]);
651
        HMAC_Final(hctx, hmac, NULL);
652 653 654
    }
    return count;
}
655
#endif
656 657 658

static int SHA1_loop(void *args)
{
659
    loopargs_t *tempargs = *(loopargs_t **) args;
660
    unsigned char *buf = tempargs->buf;
661
    unsigned char sha[SHA_DIGEST_LENGTH];
662 663 664 665 666 667 668 669
    int count;
    for (count = 0; COND(c[D_SHA1][testnum]); count++)
        SHA1(buf, lengths[testnum], sha);
    return count;
}

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

static int SHA512_loop(void *args)
{
681
    loopargs_t *tempargs = *(loopargs_t **) args;
682
    unsigned char *buf = tempargs->buf;
683
    unsigned char sha512[SHA512_DIGEST_LENGTH];
684 685 686 687 688 689
    int count;
    for (count = 0; COND(c[D_SHA512][testnum]); count++)
        SHA512(buf, lengths[testnum], sha512);
    return count;
}

690
#ifndef OPENSSL_NO_WHIRLPOOL
691 692
static int WHIRLPOOL_loop(void *args)
{
693
    loopargs_t *tempargs = *(loopargs_t **) args;
694
    unsigned char *buf = tempargs->buf;
695
    unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
696 697 698 699 700
    int count;
    for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
        WHIRLPOOL(buf, lengths[testnum], whirlpool);
    return count;
}
701
#endif
702

R
Rich Salz 已提交
703
#ifndef OPENSSL_NO_RMD160
704 705
static int EVP_Digest_RMD160_loop(void *args)
{
706
    loopargs_t *tempargs = *(loopargs_t **) args;
707
    unsigned char *buf = tempargs->buf;
708
    unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
709
    int count;
710
    for (count = 0; COND(c[D_RMD160][testnum]); count++) {
711
        if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
712
                        NULL, EVP_ripemd160(), NULL))
713 714
            return -1;
    }
715 716
    return count;
}
717
#endif
718

719
#ifndef OPENSSL_NO_RC4
720 721 722
static RC4_KEY rc4_ks;
static int RC4_loop(void *args)
{
723
    loopargs_t *tempargs = *(loopargs_t **) args;
724 725 726
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_RC4][testnum]); count++)
727
        RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
728 729 730 731 732 733 734 735 736 737 738
    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)
{
739
    loopargs_t *tempargs = *(loopargs_t **) args;
740 741 742 743
    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,
744
                         &DES_iv, DES_ENCRYPT);
745 746 747 748 749
    return count;
}

static int DES_ede3_cbc_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_EDE3_DES][testnum]); count++)
        DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
755
                             &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT);
756 757 758 759
    return count;
}
#endif

M
Matt Caswell 已提交
760
#define MAX_BLOCK_SIZE 128
761 762 763 764 765

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)
{
766
    loopargs_t *tempargs = *(loopargs_t **) args;
767 768 769 770
    unsigned char *buf = tempargs->buf;
    int count;
    for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
771
                        (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
772 773 774 775 776
    return count;
}

static int AES_cbc_192_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_192_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
782
                        (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
783 784 785 786 787
    return count;
}

static int AES_cbc_256_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_256_AES][testnum]); count++)
        AES_cbc_encrypt(buf, buf,
793
                        (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
794 795 796 797 798
    return count;
}

static int AES_ige_128_encrypt_loop(void *args)
{
799
    loopargs_t *tempargs = *(loopargs_t **) args;
800 801 802 803 804
    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,
805
                        (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
806 807 808 809 810
    return count;
}

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

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

static int CRYPTO_gcm128_aad_loop(void *args)
{
835
    loopargs_t *tempargs = *(loopargs_t **) args;
836 837 838 839 840 841 842 843
    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;
}

844
static long save_count = 0;
845 846 847
static int decrypt = 0;
static int EVP_Update_loop(void *args)
{
848
    loopargs_t *tempargs = *(loopargs_t **) args;
849 850 851
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
    int outl, count;
852 853 854
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
855
    if (decrypt)
856
        for (count = 0; COND(nb_iter); count++)
857 858
            EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    else
859
        for (count = 0; COND(nb_iter); count++)
860 861 862 863 864 865 866 867 868 869 870
            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)
{
871
    loopargs_t *tempargs = *(loopargs_t **) args;
872 873 874
    unsigned char *buf = tempargs->buf;
    unsigned char md[EVP_MAX_MD_SIZE];
    int count;
875 876 877 878 879 880
#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))
881 882
            return -1;
    }
883 884 885 886
    return count;
}

#ifndef OPENSSL_NO_RSA
F
FdaSilvaYY 已提交
887
static long rsa_c[RSA_NUM][2];  /* # RSA iteration test */
888 889 890

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

static int DSA_verify_loop(void *args)
{
955
    loopargs_t *tempargs = *(loopargs_t **) args;
956 957
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
958
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
959
    unsigned int siglen = tempargs->siglen;
960 961 962 963 964 965
    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);
966
            count = -1;
967 968 969 970 971 972 973 974 975 976 977
            break;
        }
    }
    return count;
}
#endif

#ifndef OPENSSL_NO_EC
static long ecdsa_c[EC_NUM][2];
static int ECDSA_sign_loop(void *args)
{
978
    loopargs_t *tempargs = *(loopargs_t **) args;
979
    unsigned char *buf = tempargs->buf;
980 981
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
982
    unsigned int *ecdsasiglen = &tempargs->siglen;
983 984
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
985
        ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
986 987 988
        if (ret == 0) {
            BIO_printf(bio_err, "ECDSA sign failure\n");
            ERR_print_errors(bio_err);
989
            count = -1;
990 991 992 993 994 995 996 997
            break;
        }
    }
    return count;
}

static int ECDSA_verify_loop(void *args)
{
998
    loopargs_t *tempargs = *(loopargs_t **) args;
999
    unsigned char *buf = tempargs->buf;
1000 1001
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
1002
    unsigned int ecdsasiglen = tempargs->siglen;
1003 1004
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1005
        ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1006 1007 1008
        if (ret != 1) {
            BIO_printf(bio_err, "ECDSA verify failure\n");
            ERR_print_errors(bio_err);
1009
            count = -1;
1010 1011 1012 1013 1014 1015
            break;
        }
    }
    return count;
}

1016
/* ******************************************************************** */
1017 1018
static long ecdh_c[EC_NUM][1];

1019 1020 1021 1022 1023
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;
1024
    int count;
1025
    size_t *outlen = &(tempargs->outlen[testnum]);
F
FdaSilvaYY 已提交
1026

1027
    for (count = 0; COND(ecdh_c[testnum][0]); count++)
1028 1029
        EVP_PKEY_derive(ctx, derived_secret, outlen);

1030 1031
    return count;
}
1032

F
FdaSilvaYY 已提交
1033
#endif                          /* OPENSSL_NO_EC */
1034

F
FdaSilvaYY 已提交
1035
static int run_benchmark(int async_jobs,
1036
                         int (*loop_function) (void *), loopargs_t * loopargs)
1037 1038 1039 1040
{
    int job_op_count = 0;
    int total_op_count = 0;
    int num_inprogress = 0;
F
FdaSilvaYY 已提交
1041
    int error = 0, i = 0, ret = 0;
1042 1043
    OSSL_ASYNC_FD job_fd = 0;
    size_t num_job_fds = 0;
1044 1045 1046

    run = 1;

1047
    if (async_jobs == 0) {
1048
        return loop_function((void *)&loopargs);
1049 1050 1051
    }

    for (i = 0; i < async_jobs && !error; i++) {
1052 1053 1054
        loopargs_t *looparg_item = loopargs + i;

        /* Copy pointer content (looparg_t item address) into async context */
F
FdaSilvaYY 已提交
1055 1056
        ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
                              &job_op_count, loop_function,
1057
                              (void *)&looparg_item, sizeof(looparg_item));
F
FdaSilvaYY 已提交
1058
        switch (ret) {
F
FdaSilvaYY 已提交
1059 1060 1061 1062 1063
        case ASYNC_PAUSE:
            ++num_inprogress;
            break;
        case ASYNC_FINISH:
            if (job_op_count == -1) {
1064
                error = 1;
F
FdaSilvaYY 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
            } 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;
1075 1076 1077 1078
        }
    }

    while (num_inprogress > 0) {
1079
#if defined(OPENSSL_SYS_WINDOWS)
1080
        DWORD avail = 0;
1081
#elif defined(OPENSSL_SYS_UNIX)
1082
        int select_result = 0;
1083 1084
        OSSL_ASYNC_FD max_fd = 0;
        fd_set waitfdset;
1085

1086
        FD_ZERO(&waitfdset);
1087

1088 1089 1090
        for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
            if (loopargs[i].inprogress_job == NULL)
                continue;
1091

1092 1093 1094
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1095 1096 1097 1098
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
1099
            }
1100 1101
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1102 1103 1104
            FD_SET(job_fd, &waitfdset);
            if (job_fd > max_fd)
                max_fd = job_fd;
1105 1106
        }

B
Ben Laurie 已提交
1107
        if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1108
            BIO_printf(bio_err,
1109 1110 1111
                       "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
                       "Decrease the value of async_jobs\n",
                       max_fd, FD_SETSIZE);
1112 1113 1114 1115 1116
            ERR_print_errors(bio_err);
            error = 1;
            break;
        }

1117
        select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1118 1119 1120 1121
        if (select_result == -1 && errno == EINTR)
            continue;

        if (select_result == -1) {
1122 1123 1124 1125
            BIO_printf(bio_err, "Failure in the select\n");
            ERR_print_errors(bio_err);
            error = 1;
            break;
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
        }

        if (select_result == 0)
            continue;
#endif

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

1136 1137 1138
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1139 1140 1141 1142 1143
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
            }
1144 1145
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1146

1147
#if defined(OPENSSL_SYS_UNIX)
1148
            if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1149
                continue;
1150
#elif defined(OPENSSL_SYS_WINDOWS)
F
FdaSilvaYY 已提交
1151
            if (num_job_fds == 1
F
FdaSilvaYY 已提交
1152
                && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
F
FdaSilvaYY 已提交
1153
                && avail > 0)
1154 1155 1156
                continue;
#endif

1157
            ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1158 1159 1160
                                  loopargs[i].wait_ctx, &job_op_count,
                                  loop_function, (void *)(loopargs + i),
                                  sizeof(loopargs_t));
F
FdaSilvaYY 已提交
1161
            switch (ret) {
F
FdaSilvaYY 已提交
1162 1163 1164 1165
            case ASYNC_PAUSE:
                break;
            case ASYNC_FINISH:
                if (job_op_count == -1) {
1166
                    error = 1;
F
FdaSilvaYY 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
                } 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;
1181 1182 1183 1184 1185 1186 1187 1188 1189
            }
        }
    }

    return error ? -1 : total_op_count;
}

int speed_main(int argc, char **argv)
{
1190
    ENGINE *e = NULL;
1191
    loopargs_t *loopargs = NULL;
1192
    int async_init = 0;
1193 1194
    int loopargs_len = 0;
    char *prog;
1195
    const char *engine_id = NULL;
1196 1197 1198
    const EVP_CIPHER *evp_cipher = NULL;
    double d = 0.0;
    OPTION_CHOICE o;
1199 1200
    int multiblock = 0, pr_header = 0;
    int doit[ALGOR_NUM] = { 0 };
1201
    int ret = 1, i, k, misalign = 0;
1202
    long count = 0;
1203 1204 1205
#ifndef NO_FORK
    int multi = 0;
#endif
1206
    unsigned int async_jobs = 0;
1207 1208
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
    || !defined(OPENSSL_NO_EC)
1209
    long rsa_count = 1;
1210
#endif
1211 1212

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

1329 1330
    int ecdsa_doit[EC_NUM] = { 0 };
    int ecdh_doit[EC_NUM] = { 0 };
F
FdaSilvaYY 已提交
1331
#endif                          /* ndef OPENSSL_NO_EC */
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

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

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

1500 1501
    /* Initialize the job pool if async mode is enabled */
    if (async_jobs > 0) {
1502 1503
        async_init = ASYNC_init_thread(async_jobs, async_jobs);
        if (!async_init) {
1504 1505 1506 1507 1508 1509
            BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
            goto end;
        }
    }

    loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1510 1511
    loopargs =
        app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1512 1513
    memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));

1514
    for (i = 0; i < loopargs_len; i++) {
1515 1516 1517 1518 1519 1520 1521 1522
        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;
            }
        }

1523 1524 1525 1526
        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");
1527 1528 1529
        /* 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;
1530 1531 1532 1533
#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
1534 1535
    }

1536
#ifndef NO_FORK
1537 1538
    if (multi && do_multi(multi))
        goto show_res;
1539
#endif
1540

1541
    /* Initialize the engine after the fork */
1542
    e = setup_engine(engine_id, 0);
1543

1544
    /* No parameters; turn on everything. */
1545
    if ((argc == 0) && !doit[D_EVP]) {
1546
        for (i = 0; i < ALGOR_NUM; i++)
1547 1548
            if (i != D_EVP)
                doit[i] = 1;
F
FdaSilvaYY 已提交
1549
#ifndef OPENSSL_NO_RSA
1550 1551
        for (i = 0; i < RSA_NUM; i++)
            rsa_doit[i] = 1;
F
FdaSilvaYY 已提交
1552
#endif
M
Matt Caswell 已提交
1553
#ifndef OPENSSL_NO_DSA
1554 1555
        for (i = 0; i < DSA_NUM; i++)
            dsa_doit[i] = 1;
M
Matt Caswell 已提交
1556
#endif
1557
#ifndef OPENSSL_NO_EC
1558 1559 1560 1561
        for (i = 0; i < EC_NUM; i++)
            ecdsa_doit[i] = 1;
        for (i = 0; i < EC_NUM; i++)
            ecdh_doit[i] = 1;
1562
#endif
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
    }
    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");

1573
#ifndef OPENSSL_NO_RSA
1574 1575 1576 1577 1578
    for (i = 0; i < loopargs_len; i++) {
        for (k = 0; k < RSA_NUM; k++) {
            const unsigned char *p;

            p = rsa_data[k];
1579 1580
            loopargs[i].rsa_key[k] =
                d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1581
            if (loopargs[i].rsa_key[k] == NULL) {
1582 1583
                BIO_printf(bio_err,
                           "internal error loading RSA key number %d\n", k);
1584 1585
                goto end;
            }
1586
        }
1587 1588 1589
    }
#endif
#ifndef OPENSSL_NO_DSA
1590
    for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
1591 1592 1593
        loopargs[i].dsa_key[0] = get_dsa(512);
        loopargs[i].dsa_key[1] = get_dsa(1024);
        loopargs[i].dsa_key[2] = get_dsa(2048);
1594
    }
1595 1596
#endif
#ifndef OPENSSL_NO_DES
1597 1598 1599
    DES_set_key_unchecked(&key, &sch);
    DES_set_key_unchecked(&key2, &sch2);
    DES_set_key_unchecked(&key3, &sch3);
1600
#endif
1601 1602 1603
    AES_set_encrypt_key(key16, 128, &aes_ks1);
    AES_set_encrypt_key(key24, 192, &aes_ks2);
    AES_set_encrypt_key(key32, 256, &aes_ks3);
1604
#ifndef OPENSSL_NO_CAMELLIA
1605 1606 1607
    Camellia_set_key(key16, 128, &camellia_ks1);
    Camellia_set_key(ckey24, 192, &camellia_ks2);
    Camellia_set_key(ckey32, 256, &camellia_ks3);
1608 1609
#endif
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
1610
    IDEA_set_encrypt_key(key16, &idea_ks);
1611 1612
#endif
#ifndef OPENSSL_NO_SEED
1613
    SEED_set_key(key16, &seed_ks);
1614 1615
#endif
#ifndef OPENSSL_NO_RC4
1616
    RC4_set_key(&rc4_ks, 16, key16);
1617 1618
#endif
#ifndef OPENSSL_NO_RC2
1619
    RC2_set_key(&rc2_ks, 16, key16, 128);
1620 1621
#endif
#ifndef OPENSSL_NO_RC5
1622
    RC5_32_set_key(&rc5_ks, 16, key16, 12);
1623 1624
#endif
#ifndef OPENSSL_NO_BF
1625
    BF_set_key(&bf_ks, 16, key16);
1626 1627
#endif
#ifndef OPENSSL_NO_CAST
1628
    CAST_set_key(&cast_ks, 16, key16);
1629 1630 1631
#endif
#ifndef SIGALRM
# ifndef OPENSSL_NO_DES
1632 1633 1634 1635 1636 1637 1638
    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--)
1639 1640
            DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
                            (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 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
        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;
1690
        c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712

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

1714
#  ifndef OPENSSL_NO_RSA
1715 1716 1717 1718 1719
    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 已提交
1720
        if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
1721 1722 1723
            rsa_doit[i] = 0;
        else {
            if (rsa_c[i][0] == 0) {
1724
                rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1725 1726 1727 1728
                rsa_c[i][1] = 20;
            }
        }
    }
1729
#  endif
1730

1731
#  ifndef OPENSSL_NO_DSA
1732 1733 1734 1735 1736
    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 已提交
1737
        if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
1738 1739
            dsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1740
            if (dsa_c[i][0] == 0) {
1741
                dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1742 1743 1744 1745
                dsa_c[i][1] = 1;
            }
        }
    }
1746
#  endif
1747

1748
#  ifndef OPENSSL_NO_EC
1749 1750 1751 1752 1753
    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 已提交
1754
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1755 1756
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1757
            if (ecdsa_c[i][0] == 0) {
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
                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 已提交
1768
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1769 1770
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1771
            if (ecdsa_c[i][0] == 0) {
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
                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 已提交
1782
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1783 1784
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1785
            if (ecdsa_c[i][0] == 0) {
1786 1787 1788 1789 1790
                ecdsa_c[i][0] = 1;
                ecdsa_c[i][1] = 1;
            }
        }
    }
1791

1792 1793 1794
    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 已提交
1795
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1796 1797
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1798
            if (ecdh_c[i][0] == 0) {
1799 1800 1801 1802 1803 1804 1805
                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 已提交
1806
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1807 1808
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1809
            if (ecdh_c[i][0] == 0) {
1810 1811 1812 1813 1814 1815 1816
                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 已提交
1817
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1818 1819
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1820
            if (ecdh_c[i][0] == 0) {
1821 1822 1823 1824
                ecdh_c[i][0] = 1;
            }
        }
    }
1825
#  endif
B
Bodo Möller 已提交
1826

1827
# else
1828 1829
/* not worth fixing */
#  error "You cannot disable DES on systems without SIGALRM."
1830
# endif                         /* OPENSSL_NO_DES */
1831 1832
#else
# ifndef _WIN32
1833
    signal(SIGALRM, sig_done);
1834
# endif
1835
#endif                          /* SIGALRM */
1836

1837
#ifndef OPENSSL_NO_MD2
1838
    if (doit[D_MD2]) {
1839 1840
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
1841
            Time_F(START);
1842
            count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1843
            d = Time_F(STOP);
1844
            print_result(D_MD2, testnum, count, d);
1845 1846
        }
    }
1847 1848
#endif
#ifndef OPENSSL_NO_MDC2
1849
    if (doit[D_MDC2]) {
1850 1851
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
1852
            Time_F(START);
1853
            count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1854
            d = Time_F(STOP);
1855
            print_result(D_MDC2, testnum, count, d);
1856 1857
        }
    }
1858
#endif
1859

1860
#ifndef OPENSSL_NO_MD4
1861
    if (doit[D_MD4]) {
1862 1863
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
1864
            Time_F(START);
1865
            count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1866
            d = Time_F(STOP);
1867
            print_result(D_MD4, testnum, count, d);
1868 1869
        }
    }
1870
#endif
1871

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

1883
    if (doit[D_HMAC]) {
F
FdaSilvaYY 已提交
1884
        static const char hmac_key[] = "This is a key...";
1885 1886
        int len = strlen(hmac_key);

1887
        for (i = 0; i < loopargs_len; i++) {
1888 1889 1890 1891 1892
            loopargs[i].hctx = HMAC_CTX_new();
            if (loopargs[i].hctx == NULL) {
                BIO_printf(bio_err, "HMAC malloc failure, exiting...");
                exit(1);
            }
1893

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

1950
#ifndef OPENSSL_NO_RMD160
1951
    if (doit[D_RMD160]) {
1952
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1953 1954
            print_message(names[D_RMD160], c[D_RMD160][testnum],
                          lengths[testnum]);
1955
            Time_F(START);
1956
            count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
1957
            d = Time_F(STOP);
1958
            print_result(D_RMD160, testnum, count, d);
1959 1960
        }
    }
1961 1962
#endif
#ifndef OPENSSL_NO_RC4
1963
    if (doit[D_RC4]) {
1964 1965
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
1966
            Time_F(START);
1967
            count = run_benchmark(async_jobs, RC4_loop, loopargs);
1968
            d = Time_F(STOP);
1969
            print_result(D_RC4, testnum, count, d);
1970 1971
        }
    }
1972 1973
#endif
#ifndef OPENSSL_NO_DES
1974
    if (doit[D_CBC_DES]) {
1975
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1976 1977
            print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
                          lengths[testnum]);
1978
            Time_F(START);
1979
            count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
1980
            d = Time_F(STOP);
1981
            print_result(D_CBC_DES, testnum, count, d);
1982 1983
        }
    }
1984

1985
    if (doit[D_EDE3_DES]) {
1986
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1987 1988
            print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
                          lengths[testnum]);
1989
            Time_F(START);
1990 1991
            count =
                run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
1992
            d = Time_F(STOP);
1993
            print_result(D_EDE3_DES, testnum, count, d);
1994 1995
        }
    }
1996
#endif
M
Matt Caswell 已提交
1997

1998
    if (doit[D_CBC_128_AES]) {
1999 2000 2001
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
                          lengths[testnum]);
2002
            Time_F(START);
2003 2004
            count =
                run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2005
            d = Time_F(STOP);
2006
            print_result(D_CBC_128_AES, testnum, count, d);
2007 2008 2009
        }
    }
    if (doit[D_CBC_192_AES]) {
2010 2011 2012
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
                          lengths[testnum]);
2013
            Time_F(START);
2014 2015
            count =
                run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2016
            d = Time_F(STOP);
2017
            print_result(D_CBC_192_AES, testnum, count, d);
2018 2019 2020
        }
    }
    if (doit[D_CBC_256_AES]) {
2021 2022 2023
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
            print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
                          lengths[testnum]);
2024
            Time_F(START);
2025 2026
            count =
                run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2027
            d = Time_F(STOP);
2028
            print_result(D_CBC_256_AES, testnum, count, d);
2029 2030
        }
    }
B
Ben Laurie 已提交
2031

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

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

2272 2273 2274 2275 2276
    if (doit[D_EVP]) {
        if (multiblock && evp_cipher) {
            if (!
                (EVP_CIPHER_flags(evp_cipher) &
                 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
R
Rich Salz 已提交
2277
                BIO_printf(bio_err, "%s is not multi-block capable\n",
2278
                           OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2279 2280
                goto end;
            }
2281 2282 2283 2284
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2285
            multiblock_speed(evp_cipher);
2286
            ret = 0;
2287 2288
            goto end;
        }
2289
        for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2290 2291
            if (evp_cipher) {

2292
                names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2293 2294 2295 2296
                /*
                 * -O3 -fschedule-insns messes up an optimization here!
                 * names[D_EVP] somehow becomes NULL
                 */
2297 2298 2299 2300 2301
                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)
2302 2303
                        EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
                                           key16, iv);
2304
                    else
2305 2306
                        EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
                                           key16, iv);
2307 2308
                    EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
                }
2309 2310

                Time_F(START);
2311
                count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
2312
                d = Time_F(STOP);
2313 2314 2315
                for (k = 0; k < loopargs_len; k++) {
                    EVP_CIPHER_CTX_free(loopargs[k].ctx);
                }
2316 2317
            }
            if (evp_md) {
2318
                names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2319
                print_message(names[D_EVP], save_count, lengths[testnum]);
2320
                Time_F(START);
2321
                count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2322 2323
                d = Time_F(STOP);
            }
2324
            print_result(D_EVP, testnum, count, d);
2325 2326
        }
    }
2327

2328
    for (i = 0; i < loopargs_len; i++)
2329 2330
        RAND_bytes(loopargs[i].buf, 36);

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

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

2388 2389
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2390 2391
            for (testnum++; testnum < RSA_NUM; testnum++)
                rsa_doit[testnum] = 0;
2392 2393
        }
    }
F
FdaSilvaYY 已提交
2394
#endif                          /* OPENSSL_NO_RSA */
2395

2396
    for (i = 0; i < loopargs_len; i++)
2397 2398
        RAND_bytes(loopargs[i].buf, 36);

2399
#ifndef OPENSSL_NO_DSA
2400 2401 2402
    if (RAND_status() != 1) {
        RAND_seed(rnd_seed, sizeof rnd_seed);
    }
2403 2404 2405
    for (testnum = 0; testnum < DSA_NUM; testnum++) {
        int st = 0;
        if (!dsa_doit[testnum])
2406 2407
            continue;

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

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

2461 2462
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2463 2464
            for (testnum++; testnum < DSA_NUM; testnum++)
                dsa_doit[testnum] = 0;
2465 2466
        }
    }
F
FdaSilvaYY 已提交
2467
#endif                          /* OPENSSL_NO_DSA */
B
Bodo Möller 已提交
2468

2469
#ifndef OPENSSL_NO_EC
2470 2471 2472
    if (RAND_status() != 1) {
        RAND_seed(rnd_seed, sizeof rnd_seed);
    }
2473
    for (testnum = 0; testnum < EC_NUM; testnum++) {
2474
        int st = 1;
2475

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

                BIO_printf(bio_err,
                           mr ? "+R5:%ld:%d:%.2f\n" :
                           "%ld %d bit ECDSA signs in %.2fs \n",
2517
                           count, test_curves_bits[testnum], d);
2518
                ecdsa_results[testnum][0] = (double)count / d;
2519 2520 2521 2522
                rsa_count = count;
            }

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

            if (rsa_count <= 1) {
                /* if longer than 10s, don't do any more */
2551 2552
                for (testnum++; testnum < EC_NUM; testnum++)
                    ecdsa_doit[testnum] = 0;
2553 2554 2555
            }
        }
    }
2556

2557 2558 2559
    if (RAND_status() != 1) {
        RAND_seed(rnd_seed, sizeof rnd_seed);
    }
2560
    for (testnum = 0; testnum < EC_NUM; testnum++) {
2561 2562
        int ecdh_checks = 1;

2563
        if (!ecdh_doit[testnum])
2564
            continue;
2565

2566
        for (i = 0; i < loopargs_len; i++) {
N
Nicola Tuveri 已提交
2567
            EVP_PKEY_CTX *kctx = NULL;
2568
            EVP_PKEY_CTX *test_ctx = NULL;
N
Nicola Tuveri 已提交
2569 2570 2571
            EVP_PKEY_CTX *ctx = NULL;
            EVP_PKEY *key_A = NULL;
            EVP_PKEY *key_B = NULL;
2572
            size_t outlen;
2573
            size_t test_outlen;
2574

2575 2576 2577 2578 2579 2580 2581
            /* 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);
            }

2582 2583 2584 2585 2586 2587 2588 2589
            /* 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) {
2590 2591 2592
                EVP_PKEY_CTX *pctx = NULL;
                EVP_PKEY *params = NULL;

2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
                /* 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;
                }

2612 2613 2614 2615 2616 2617 2618 2619 2620
                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 已提交
2621
                       !EVP_PKEY_paramgen(pctx, &params)) {
2622
                    ecdh_checks = 0;
2623
                    BIO_printf(bio_err, "ECDH EC params init failure.\n");
2624
                    ERR_print_errors(bio_err);
2625
                    rsa_count = 1;
2626
                    break;
2627
                }
2628 2629 2630
                /* Create the context for the key generation */
                kctx = EVP_PKEY_CTX_new(params, NULL);

2631 2632 2633 2634
                EVP_PKEY_free(params);
                params = NULL;
                EVP_PKEY_CTX_free(pctx);
                pctx = NULL;
A
Andrea Grandi 已提交
2635
            }
2636
            if (kctx == NULL ||      /* keygen ctx is not null */
N
Nicola Tuveri 已提交
2637
                !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
2638 2639 2640 2641 2642
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH keygen failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
2643
            }
2644

2645 2646 2647 2648 2649 2650
            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 */
2651
                outlen == 0 ||  /* ensure outlen is a valid size */
N
Nicola Tuveri 已提交
2652
                outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
2653 2654 2655 2656 2657 2658 2659
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH key generation failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
            /* 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;
            }
2677 2678 2679 2680 2681

            /* 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;
2682 2683 2684 2685 2686 2687
                BIO_printf(bio_err, "ECDH computations don't match.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

2688
            loopargs[i].ecdh_ctx[testnum] = ctx;
2689
            loopargs[i].outlen[testnum] = outlen;
2690

2691 2692
            EVP_PKEY_CTX_free(kctx);
            kctx = NULL;
2693 2694
            EVP_PKEY_CTX_free(test_ctx);
            test_ctx = NULL;
2695 2696 2697
        }
        if (ecdh_checks != 0) {
            pkey_print_message("", "ecdh",
2698 2699
                               ecdh_c[testnum][0],
                               test_curves_bits[testnum], ECDH_SECONDS);
2700
            Time_F(START);
2701 2702
            count =
                run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
2703 2704
            d = Time_F(STOP);
            BIO_printf(bio_err,
2705 2706 2707
                       mr ? "+R7:%ld:%d:%.2f\n" :
                       "%ld %d-bit ECDH ops in %.2fs\n", count,
                       test_curves_bits[testnum], d);
2708
            ecdh_results[testnum][0] = (double)count / d;
2709
            rsa_count = count;
2710
        }
B
Bodo Möller 已提交
2711

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

2746 2747
    if (pr_header) {
        if (mr)
2748
            printf("+H");
2749
        else {
2750 2751 2752
            printf
                ("The 'numbers' are in 1000s of bytes per second processed.\n");
            printf("type        ");
2753
        }
2754 2755
        for (testnum = 0; testnum < SIZE_NUM; testnum++)
            printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2756
        printf("\n");
2757
    }
B
Bodo Möller 已提交
2758

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

        if (mr)
2821 2822 2823
            printf("+F4:%u:%u:%f:%f\n",
                   k, test_curves_bits[k],
                   ecdsa_results[k][0], ecdsa_results[k][1]);
2824
        else
2825 2826 2827
            printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
                   test_curves_bits[k],
                   test_curves_names[k],
2828 2829
                   1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
                   ecdsa_results[k][0], ecdsa_results[k][1]);
2830
    }
2831

2832
    testnum = 1;
2833 2834 2835
    for (k = 0; k < EC_NUM; k++) {
        if (!ecdh_doit[k])
            continue;
2836
        if (testnum && !mr) {
2837
            printf("%30sop      op/s\n", " ");
2838
            testnum = 0;
2839 2840
        }
        if (mr)
2841 2842 2843
            printf("+F5:%u:%u:%f:%f\n",
                   k, test_curves_bits[k],
                   ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
2844 2845

        else
2846 2847 2848
            printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
                   test_curves_bits[k],
                   test_curves_names[k],
2849
                   1.0 / ecdh_results[k][0], ecdh_results[k][0]);
2850
    }
2851
#endif
2852

2853
    ret = 0;
2854 2855 2856

 end:
    ERR_print_errors(bio_err);
2857
    for (i = 0; i < loopargs_len; i++) {
2858 2859
        OPENSSL_free(loopargs[i].buf_malloc);
        OPENSSL_free(loopargs[i].buf2_malloc);
2860

2861
#ifndef OPENSSL_NO_RSA
2862 2863
        for (k = 0; k < RSA_NUM; k++)
            RSA_free(loopargs[i].rsa_key[k]);
2864 2865
#endif
#ifndef OPENSSL_NO_DSA
2866 2867
        for (k = 0; k < DSA_NUM; k++)
            DSA_free(loopargs[i].dsa_key[k]);
2868
#endif
2869
#ifndef OPENSSL_NO_EC
2870 2871
        for (k = 0; k < EC_NUM; k++) {
            EC_KEY_free(loopargs[i].ecdsa[k]);
2872
            EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
2873
        }
2874 2875
        OPENSSL_free(loopargs[i].secret_a);
        OPENSSL_free(loopargs[i].secret_b);
2876
#endif
2877 2878
    }

2879 2880 2881
    if (async_jobs > 0) {
        for (i = 0; i < loopargs_len; i++)
            ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
2882
    }
2883

2884
    if (async_init) {
2885
        ASYNC_cleanup_thread();
2886 2887
    }
    OPENSSL_free(loopargs);
2888
    release_engine(e);
2889
    return (ret);
2890
}
2891

2892
static void print_message(const char *s, long num, int length)
2893
{
2894
#ifdef SIGALRM
2895 2896 2897 2898 2899
    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);
2900
#else
2901 2902 2903 2904
    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);
2905
#endif
2906
}
2907

2908
static void pkey_print_message(const char *str, const char *str2, long num,
2909 2910
                               int bits, int tm)
{
2911
#ifdef SIGALRM
2912 2913 2914 2915 2916
    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);
2917
#else
2918 2919 2920 2921
    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);
2922
#endif
2923
}
2924

2925 2926
static void print_result(int alg, int run_no, int count, double time_used)
{
2927 2928 2929 2930
    if (count == -1) {
        BIO_puts(bio_err, "EVP error!\n");
        exit(1);
    }
2931 2932 2933 2934 2935
    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];
}
2936

2937
#ifndef NO_FORK
2938
static char *sstrsep(char **string, const char *delim)
2939
{
2940 2941 2942 2943 2944 2945
    char isdelim[256];
    char *token = *string;

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

2946
    memset(isdelim, 0, sizeof isdelim);
2947 2948
    isdelim[0] = 1;

2949
    while (*delim) {
2950 2951
        isdelim[(unsigned char)(*delim)] = 1;
        delim++;
2952
    }
2953

2954
    while (!isdelim[(unsigned char)(**string)]) {
2955
        (*string)++;
2956
    }
2957

2958
    if (**string) {
2959 2960
        **string = 0;
        (*string)++;
2961
    }
2962 2963

    return token;
2964
}
2965 2966

static int do_multi(int multi)
2967 2968 2969 2970 2971 2972
{
    int n;
    int fd[2];
    int *fds;
    static char sep[] = ":";

R
Rich Salz 已提交
2973
    fds = malloc(sizeof(*fds) * multi);
2974 2975
    for (n = 0; n < multi; ++n) {
        if (pipe(fd) == -1) {
R
Rich Salz 已提交
2976
            BIO_printf(bio_err, "pipe failure\n");
2977 2978 2979
            exit(1);
        }
        fflush(stdout);
R
Rich Salz 已提交
2980
        (void)BIO_flush(bio_err);
2981 2982 2983 2984 2985 2986 2987
        if (fork()) {
            close(fd[1]);
            fds[n] = fd[0];
        } else {
            close(fd[0]);
            close(1);
            if (dup(fd[1]) == -1) {
R
Rich Salz 已提交
2988
                BIO_printf(bio_err, "dup failed\n");
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
                exit(1);
            }
            close(fd[1]);
            mr = 1;
            usertime = 0;
            free(fds);
            return 0;
        }
        printf("Forked child %d\n", n);
    }
B
Bodo Möller 已提交
2999

3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
    /* 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] != '+') {
3012 3013 3014
                BIO_printf(bio_err,
                           "Don't understand line '%s' from child %d\n", buf,
                           n);
3015 3016 3017
                continue;
            }
            printf("Got: %s from %d\n", buf, n);
R
Rich Salz 已提交
3018
            if (strncmp(buf, "+F:", 3) == 0) {
3019 3020 3021 3022 3023 3024 3025 3026
                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 已提交
3027
            } else if (strncmp(buf, "+F2:", 4) == 0) {
3028 3029 3030 3031 3032 3033 3034 3035
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3036
                rsa_results[k][0] += d;
3037 3038

                d = atof(sstrsep(&p, sep));
3039
                rsa_results[k][1] += d;
3040
            }
3041
# ifndef OPENSSL_NO_DSA
R
Rich Salz 已提交
3042
            else if (strncmp(buf, "+F3:", 4) == 0) {
3043 3044 3045 3046 3047 3048 3049 3050
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3051
                dsa_results[k][0] += d;
3052 3053

                d = atof(sstrsep(&p, sep));
3054
                dsa_results[k][1] += d;
3055
            }
3056
# endif
3057
# ifndef OPENSSL_NO_EC
R
Rich Salz 已提交
3058
            else if (strncmp(buf, "+F4:", 4) == 0) {
3059 3060 3061 3062 3063 3064 3065 3066
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3067
                ecdsa_results[k][0] += d;
3068 3069

                d = atof(sstrsep(&p, sep));
3070
                ecdsa_results[k][1] += d;
F
FdaSilvaYY 已提交
3071
            } else if (strncmp(buf, "+F5:", 4) == 0) {
3072 3073 3074 3075 3076 3077 3078 3079
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3080
                ecdh_results[k][0] += d;
3081
            }
3082
# endif
3083

R
Rich Salz 已提交
3084
            else if (strncmp(buf, "+H:", 3) == 0) {
3085
                ;
3086
            } else
3087 3088
                BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
                           n);
3089 3090 3091 3092 3093 3094 3095
        }

        fclose(f);
    }
    free(fds);
    return 1;
}
3096
#endif
3097 3098

static void multiblock_speed(const EVP_CIPHER *evp_cipher)
3099 3100 3101
{
    static int mblengths[] =
        { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3102
    int j, count, num = OSSL_NELEM(mblengths);
3103 3104
    const char *alg_name;
    unsigned char *inp, *out, no_key[32], no_iv[16];
3105
    EVP_CIPHER_CTX *ctx;
3106 3107
    double d = 0.0;

R
Rich Salz 已提交
3108 3109
    inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
    out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3110 3111
    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
3112
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
3113
    alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3114 3115 3116 3117 3118

    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++) {
3119
            unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
            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;

3135
            packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3136 3137 3138 3139 3140 3141
                                          sizeof(mb_param), &mb_param);

            if (packlen > 0) {
                mb_param.out = out;
                mb_param.inp = inp;
                mb_param.len = len;
3142
                EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3143 3144 3145 3146 3147 3148 3149 3150
                                    sizeof(mb_param), &mb_param);
            } else {
                int pad;

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