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

11 12 13 14 15 16 17 18 19 20 21 22
#undef SECONDS
#define SECONDS                 3
#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"
23
#include "progs.h"
24 25 26 27 28
#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

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

119
typedef struct openssl_speed_sec_st {
120 121 122 123 124
    int sym;
    int rsa;
    int dsa;
    int ecdsa;
    int ecdh;
125
} openssl_speed_sec_t;
126

127
static volatile int run = 0;
128

129 130
static int mr = 0;
static int usertime = 1;
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#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);
169
static int RAND_bytes_loop(void *args);
170
static int EVP_Update_loop(void *args);
171
static int EVP_Update_loop_ccm(void *args);
172 173 174 175 176 177 178 179 180 181 182 183 184 185
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

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

195 196 197 198 199
static const int lengths_list[] = {
    16, 64, 256, 1024, 8 * 1024, 16 * 1024
};
static const int *lengths = lengths_list;

200 201 202 203 204 205
#ifdef SIGALRM
# if defined(__STDC__) || defined(sgi) || defined(_AIX)
#  define SIGRETTYPE void
# else
#  define SIGRETTYPE int
# endif
D
Dr. Stephen Henson 已提交
206

207
static SIGRETTYPE sig_done(int sig);
U
Ulf Möller 已提交
208
static SIGRETTYPE sig_done(int sig)
209 210 211 212
{
    signal(SIGALRM, sig_done);
    run = 0;
}
213
#endif
214

215 216
#define START   0
#define STOP    1
217

218
#if defined(_WIN32)
R
Richard Levitte 已提交
219

220 221 222
# if !defined(SIGALRM)
#  define SIGALRM
# endif
223 224
static unsigned int lapse;
static volatile unsigned int schlock;
225 226 227 228
static void alarm_win32(unsigned int secs)
{
    lapse = secs * 1000;
}
R
Richard Levitte 已提交
229

230
# define alarm alarm_win32
231 232 233 234 235 236 237 238

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

240
static double Time_F(int s)
241 242 243 244 245 246 247 248
{
    double ret;
    static HANDLE thr;

    if (s == START) {
        schlock = 0;
        thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
        if (thr == NULL) {
249 250
            DWORD err = GetLastError();
            BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
251
            ExitProcess(err);
252 253 254 255 256 257 258 259 260 261 262 263 264
        }
        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;
}
265
#else
266

267 268 269 270 271 272 273
static double Time_F(int s)
{
    double ret = app_tminterval(s, usertime);
    if (s == STOP)
        alarm(0);
    return ret;
}
274
#endif
275

276
static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
277
                             const openssl_speed_sec_t *seconds);
278

279 280 281 282
#define found(value, pairs, result)\
    opt_found(value, result, pairs, OSSL_NELEM(pairs))
static int opt_found(const char *name, unsigned int *result,
                     const OPT_PAIR pairs[], unsigned int nbelem)
283
{
284 285 286
    unsigned int idx;

    for (idx = 0; idx < nbelem; ++idx, pairs++)
287 288 289 290 291 292 293 294 295 296
        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
Paul Yang 已提交
297
    OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM,
298
    OPT_PRIMES, OPT_SECONDS, OPT_BYTES
299 300
} OPTION_CHOICE;

F
FdaSilvaYY 已提交
301
const OPTIONS speed_options[] = {
302 303 304
    {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
    {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
    {"help", OPT_HELP, '-', "Display this summary"},
305 306 307 308
    {"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 已提交
309
    {"mb", OPT_MB, '-',
F
FdaSilvaYY 已提交
310
     "Enable (tls1.1) multi-block mode on evp_cipher requested with -evp"},
311
    {"misalign", OPT_MISALIGN, 'n', "Amount to mis-align buffers"},
312 313 314 315 316
    {"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
317
#ifndef OPENSSL_NO_ASYNC
F
FdaSilvaYY 已提交
318 319
    {"async_jobs", OPT_ASYNCJOBS, 'p',
     "Enable async mode and start pnum jobs"},
320
#endif
R
Rich Salz 已提交
321
    OPT_R_OPTIONS,
322 323 324
#ifndef OPENSSL_NO_ENGINE
    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
#endif
P
Paul Yang 已提交
325
    {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
326 327 328 329
    {"seconds", OPT_SECONDS, 'p',
     "Run benchmarks for pnum seconds"},
    {"bytes", OPT_BYTES, 'p',
     "Run cipher, digest and rand benchmarks on pnum bytes"},
330
    {NULL}
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
};

#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
363
#define D_RAND          30
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/* name of algorithms to test */
static const char *names[] = {
    "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
    "des cbc", "des ede3", "idea cbc", "seed cbc",
    "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
    "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
    "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
    "evp", "sha256", "sha512", "whirlpool",
    "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
    "rand"
};
#define ALGOR_NUM       OSSL_NELEM(names)

/* list of configured algorithm (remaining) */
static const OPT_PAIR doit_choices[] = {
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
#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 已提交
398
#ifndef OPENSSL_NO_RMD160
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    {"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},
443
    {"rand", D_RAND}
444 445
};

446 447
static double results[ALGOR_NUM][OSSL_NELEM(lengths_list)];

M
Matt Caswell 已提交
448 449 450 451
#ifndef OPENSSL_NO_DSA
# define R_DSA_512       0
# define R_DSA_1024      1
# define R_DSA_2048      2
452
static const OPT_PAIR dsa_choices[] = {
453 454
    {"dsa512", R_DSA_512},
    {"dsa1024", R_DSA_1024},
455
    {"dsa2048", R_DSA_2048}
456
};
457 458 459 460
# define DSA_NUM         OSSL_NELEM(dsa_choices)

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

462 463 464 465 466 467 468
#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
469 470
#ifndef OPENSSL_NO_RSA
static const OPT_PAIR rsa_choices[] = {
471 472 473 474 475 476
    {"rsa512", R_RSA_512},
    {"rsa1024", R_RSA_1024},
    {"rsa2048", R_RSA_2048},
    {"rsa3072", R_RSA_3072},
    {"rsa4096", R_RSA_4096},
    {"rsa7680", R_RSA_7680},
477
    {"rsa15360", R_RSA_15360}
478
};
479 480 481 482
# define RSA_NUM OSSL_NELEM(rsa_choices)

static double rsa_results[RSA_NUM][2];  /* 2 ops: sign then verify */
#endif /* OPENSSL_NO_RSA */
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

#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
500
#define R_EC_X25519  16
501
#define R_EC_X448    17
R
Richard Levitte 已提交
502
#ifndef OPENSSL_NO_EC
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
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},
519
    {"ecdsab571", R_EC_B571}
520
};
521 522 523
# define ECDSA_NUM       OSSL_NELEM(ecdsa_choices)

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

525
static const OPT_PAIR ecdh_choices[] = {
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    {"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},
542
    {"ecdhx25519", R_EC_X25519},
543
    {"ecdhx448", R_EC_X448}
544
};
545 546 547 548
# define EC_NUM       OSSL_NELEM(ecdh_choices)

static double ecdh_results[EC_NUM][1];  /* 1 op: derivation */
#endif /* OPENSSL_NO_EC */
549

550 551 552 553
#ifndef SIGALRM
# define COND(d) (count < (d))
# define COUNT(d) (d)
#else
554
# define COND(unused_cond) (run && count<0x7fffffff)
555
# define COUNT(d) (count)
556
#endif                          /* SIGALRM */
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
typedef struct loopargs_st {
    ASYNC_JOB *inprogress_job;
    ASYNC_WAIT_CTX *wait_ctx;
    unsigned char *buf;
    unsigned char *buf2;
    unsigned char *buf_malloc;
    unsigned char *buf2_malloc;
    unsigned char *key;
    unsigned int siglen;
#ifndef OPENSSL_NO_RSA
    RSA *rsa_key[RSA_NUM];
#endif
#ifndef OPENSSL_NO_DSA
    DSA *dsa_key[DSA_NUM];
#endif
#ifndef OPENSSL_NO_EC
    EC_KEY *ecdsa[ECDSA_NUM];
    EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
    unsigned char *secret_a;
    unsigned char *secret_b;
    size_t outlen[EC_NUM];
#endif
    EVP_CIPHER_CTX *ctx;
    HMAC_CTX *hctx;
    GCM128_CONTEXT *gcm_ctx;
} loopargs_t;
static int run_benchmark(int async_jobs, int (*loop_function) (void *),
                         loopargs_t * loopargs);

static unsigned int testnum;
588

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

592
#ifndef OPENSSL_NO_MD2
593 594
static int EVP_Digest_MD2_loop(void *args)
{
595
    loopargs_t *tempargs = *(loopargs_t **) args;
596
    unsigned char *buf = tempargs->buf;
597
    unsigned char md2[MD2_DIGEST_LENGTH];
598
    int count;
599

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

609
#ifndef OPENSSL_NO_MDC2
610 611
static int EVP_Digest_MDC2_loop(void *args)
{
612
    loopargs_t *tempargs = *(loopargs_t **) args;
613
    unsigned char *buf = tempargs->buf;
614
    unsigned char mdc2[MDC2_DIGEST_LENGTH];
615
    int count;
616

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

626
#ifndef OPENSSL_NO_MD4
627 628
static int EVP_Digest_MD4_loop(void *args)
{
629
    loopargs_t *tempargs = *(loopargs_t **) args;
630
    unsigned char *buf = tempargs->buf;
631
    unsigned char md4[MD4_DIGEST_LENGTH];
632
    int count;
633

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

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

static int HMAC_loop(void *args)
{
657
    loopargs_t *tempargs = *(loopargs_t **) args;
658 659
    unsigned char *buf = tempargs->buf;
    HMAC_CTX *hctx = tempargs->hctx;
660
    unsigned char hmac[MD5_DIGEST_LENGTH];
661
    int count;
662

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

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

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

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

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

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

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

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

M
Matt Caswell 已提交
775
#define MAX_BLOCK_SIZE 128
776 777 778 779 780

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

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

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

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

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

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

static int CRYPTO_gcm128_aad_loop(void *args)
{
850
    loopargs_t *tempargs = *(loopargs_t **) args;
851 852 853 854 855 856 857 858
    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;
}

859 860 861 862 863 864 865 866 867 868 869
static int RAND_bytes_loop(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    int count;

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

870
static long save_count = 0;
871 872 873
static int decrypt = 0;
static int EVP_Update_loop(void *args)
{
874
    loopargs_t *tempargs = *(loopargs_t **) args;
875 876
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
877
    int outl, count, rc;
878 879 880
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
881 882 883 884 885 886 887 888 889 890 891 892 893
    if (decrypt) {
        for (count = 0; COND(nb_iter); count++) {
            rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
            if (rc != 1)
                EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
        }
    } else {
        for (count = 0; COND(nb_iter); count++) {
            rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
            if (rc != 1)
                EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
        }
    }
894 895 896 897 898 899
    if (decrypt)
        EVP_DecryptFinal_ex(ctx, buf, &outl);
    else
        EVP_EncryptFinal_ex(ctx, buf, &outl);
    return count;
}
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
/*
 * CCM does not support streaming. For the purpose of performance measurement,
 * each message is encrypted using the same (key,iv)-pair. Do not use this
 * code in your application.
 */
static int EVP_Update_loop_ccm(void *args)
{
    loopargs_t *tempargs = *(loopargs_t **) args;
    unsigned char *buf = tempargs->buf;
    EVP_CIPHER_CTX *ctx = tempargs->ctx;
    int outl, count;
    unsigned char tag[12];
#ifndef SIGALRM
    int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
    if (decrypt) {
        for (count = 0; COND(nb_iter); count++) {
            EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
            EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
            EVP_DecryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
            EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
            EVP_DecryptFinal_ex(ctx, buf, &outl);
        }
    } else {
        for (count = 0; COND(nb_iter); count++) {
            EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
            EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
            EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
            EVP_EncryptFinal_ex(ctx, buf, &outl);
        }
    }
    return count;
}
933 934 935 936

static const EVP_MD *evp_md = NULL;
static int EVP_Digest_loop(void *args)
{
937
    loopargs_t *tempargs = *(loopargs_t **) args;
938 939 940
    unsigned char *buf = tempargs->buf;
    unsigned char md[EVP_MAX_MD_SIZE];
    int count;
941 942 943 944 945 946
#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))
947 948
            return -1;
    }
949 950 951 952
    return count;
}

#ifndef OPENSSL_NO_RSA
F
FdaSilvaYY 已提交
953
static long rsa_c[RSA_NUM][2];  /* # RSA iteration test */
954 955 956

static int RSA_sign_loop(void *args)
{
957
    loopargs_t *tempargs = *(loopargs_t **) args;
958 959
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
F
FdaSilvaYY 已提交
960
    unsigned int *rsa_num = &tempargs->siglen;
961
    RSA **rsa_key = tempargs->rsa_key;
962 963
    int ret, count;
    for (count = 0; COND(rsa_c[testnum][0]); count++) {
964
        ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
965 966 967 968 969 970 971 972 973 974 975 976
        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)
{
977
    loopargs_t *tempargs = *(loopargs_t **) args;
978 979
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
F
FdaSilvaYY 已提交
980
    unsigned int rsa_num = tempargs->siglen;
981
    RSA **rsa_key = tempargs->rsa_key;
982 983
    int ret, count;
    for (count = 0; COND(rsa_c[testnum][1]); count++) {
984 985
        ret =
            RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
        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)
{
1001
    loopargs_t *tempargs = *(loopargs_t **) args;
1002 1003
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
1004
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
1005
    unsigned int *siglen = &tempargs->siglen;
1006 1007 1008 1009 1010 1011
    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);
1012
            count = -1;
1013 1014 1015 1016 1017 1018 1019 1020
            break;
        }
    }
    return count;
}

static int DSA_verify_loop(void *args)
{
1021
    loopargs_t *tempargs = *(loopargs_t **) args;
1022 1023
    unsigned char *buf = tempargs->buf;
    unsigned char *buf2 = tempargs->buf2;
1024
    DSA **dsa_key = tempargs->dsa_key;
F
FdaSilvaYY 已提交
1025
    unsigned int siglen = tempargs->siglen;
1026 1027 1028 1029 1030 1031
    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);
1032
            count = -1;
1033 1034 1035 1036 1037 1038 1039 1040
            break;
        }
    }
    return count;
}
#endif

#ifndef OPENSSL_NO_EC
1041
static long ecdsa_c[ECDSA_NUM][2];
1042 1043
static int ECDSA_sign_loop(void *args)
{
1044
    loopargs_t *tempargs = *(loopargs_t **) args;
1045
    unsigned char *buf = tempargs->buf;
1046 1047
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
1048
    unsigned int *ecdsasiglen = &tempargs->siglen;
1049 1050
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
1051
        ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1052 1053 1054
        if (ret == 0) {
            BIO_printf(bio_err, "ECDSA sign failure\n");
            ERR_print_errors(bio_err);
1055
            count = -1;
1056 1057 1058 1059 1060 1061 1062 1063
            break;
        }
    }
    return count;
}

static int ECDSA_verify_loop(void *args)
{
1064
    loopargs_t *tempargs = *(loopargs_t **) args;
1065
    unsigned char *buf = tempargs->buf;
1066 1067
    EC_KEY **ecdsa = tempargs->ecdsa;
    unsigned char *ecdsasig = tempargs->buf2;
F
FdaSilvaYY 已提交
1068
    unsigned int ecdsasiglen = tempargs->siglen;
1069 1070
    int ret, count;
    for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
1071
        ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
1072 1073 1074
        if (ret != 1) {
            BIO_printf(bio_err, "ECDSA verify failure\n");
            ERR_print_errors(bio_err);
1075
            count = -1;
1076 1077 1078 1079 1080 1081
            break;
        }
    }
    return count;
}

1082
/* ******************************************************************** */
1083 1084
static long ecdh_c[EC_NUM][1];

1085 1086 1087 1088 1089
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;
1090
    int count;
1091
    size_t *outlen = &(tempargs->outlen[testnum]);
F
FdaSilvaYY 已提交
1092

1093
    for (count = 0; COND(ecdh_c[testnum][0]); count++)
1094 1095
        EVP_PKEY_derive(ctx, derived_secret, outlen);

1096 1097
    return count;
}
1098

F
FdaSilvaYY 已提交
1099
#endif                          /* OPENSSL_NO_EC */
1100

F
FdaSilvaYY 已提交
1101
static int run_benchmark(int async_jobs,
1102
                         int (*loop_function) (void *), loopargs_t * loopargs)
1103 1104 1105 1106
{
    int job_op_count = 0;
    int total_op_count = 0;
    int num_inprogress = 0;
F
FdaSilvaYY 已提交
1107
    int error = 0, i = 0, ret = 0;
1108 1109
    OSSL_ASYNC_FD job_fd = 0;
    size_t num_job_fds = 0;
1110 1111 1112

    run = 1;

1113
    if (async_jobs == 0) {
1114
        return loop_function((void *)&loopargs);
1115 1116 1117
    }

    for (i = 0; i < async_jobs && !error; i++) {
1118 1119 1120
        loopargs_t *looparg_item = loopargs + i;

        /* Copy pointer content (looparg_t item address) into async context */
F
FdaSilvaYY 已提交
1121 1122
        ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
                              &job_op_count, loop_function,
1123
                              (void *)&looparg_item, sizeof(looparg_item));
F
FdaSilvaYY 已提交
1124
        switch (ret) {
F
FdaSilvaYY 已提交
1125 1126 1127 1128 1129
        case ASYNC_PAUSE:
            ++num_inprogress;
            break;
        case ASYNC_FINISH:
            if (job_op_count == -1) {
1130
                error = 1;
F
FdaSilvaYY 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
            } 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;
1141 1142 1143 1144
        }
    }

    while (num_inprogress > 0) {
1145
#if defined(OPENSSL_SYS_WINDOWS)
1146
        DWORD avail = 0;
1147
#elif defined(OPENSSL_SYS_UNIX)
1148
        int select_result = 0;
1149 1150
        OSSL_ASYNC_FD max_fd = 0;
        fd_set waitfdset;
1151

1152
        FD_ZERO(&waitfdset);
1153

1154 1155 1156
        for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
            if (loopargs[i].inprogress_job == NULL)
                continue;
1157

1158 1159 1160
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1161 1162 1163 1164
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
1165
            }
1166 1167
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1168 1169 1170
            FD_SET(job_fd, &waitfdset);
            if (job_fd > max_fd)
                max_fd = job_fd;
1171 1172
        }

B
Ben Laurie 已提交
1173
        if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
1174
            BIO_printf(bio_err,
1175 1176 1177
                       "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
                       "Decrease the value of async_jobs\n",
                       max_fd, FD_SETSIZE);
1178 1179 1180 1181 1182
            ERR_print_errors(bio_err);
            error = 1;
            break;
        }

1183
        select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
1184 1185 1186 1187
        if (select_result == -1 && errno == EINTR)
            continue;

        if (select_result == -1) {
1188 1189 1190 1191
            BIO_printf(bio_err, "Failure in the select\n");
            ERR_print_errors(bio_err);
            error = 1;
            break;
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        }

        if (select_result == 0)
            continue;
#endif

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

1202 1203 1204
            if (!ASYNC_WAIT_CTX_get_all_fds
                (loopargs[i].wait_ctx, NULL, &num_job_fds)
                || num_job_fds > 1) {
1205 1206 1207 1208 1209
                BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
                ERR_print_errors(bio_err);
                error = 1;
                break;
            }
1210 1211
            ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
                                       &num_job_fds);
1212

1213
#if defined(OPENSSL_SYS_UNIX)
1214
            if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
1215
                continue;
1216
#elif defined(OPENSSL_SYS_WINDOWS)
F
FdaSilvaYY 已提交
1217
            if (num_job_fds == 1
F
FdaSilvaYY 已提交
1218
                && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
F
FdaSilvaYY 已提交
1219
                && avail > 0)
1220 1221 1222
                continue;
#endif

1223
            ret = ASYNC_start_job(&loopargs[i].inprogress_job,
1224 1225 1226
                                  loopargs[i].wait_ctx, &job_op_count,
                                  loop_function, (void *)(loopargs + i),
                                  sizeof(loopargs_t));
F
FdaSilvaYY 已提交
1227
            switch (ret) {
F
FdaSilvaYY 已提交
1228 1229 1230 1231
            case ASYNC_PAUSE:
                break;
            case ASYNC_FINISH:
                if (job_op_count == -1) {
1232
                    error = 1;
F
FdaSilvaYY 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
                } 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;
1247 1248 1249 1250 1251 1252 1253 1254 1255
            }
        }
    }

    return error ? -1 : total_op_count;
}

int speed_main(int argc, char **argv)
{
1256
    ENGINE *e = NULL;
1257
    int (*loopfunc)(void *args);
1258
    loopargs_t *loopargs = NULL;
1259
    const char *prog;
1260
    const char *engine_id = NULL;
1261 1262 1263
    const EVP_CIPHER *evp_cipher = NULL;
    double d = 0.0;
    OPTION_CHOICE o;
1264
    int async_init = 0, multiblock = 0, pr_header = 0;
1265
    int doit[ALGOR_NUM] = { 0 };
1266
    int ret = 1, misalign = 0, lengths_single = 0;
1267
    long count = 0;
1268 1269
    unsigned int size_num = OSSL_NELEM(lengths_list);
    unsigned int i, k, loop, loopargs_len = 0, async_jobs = 0;
1270
    int keylen;
P
Patrick Steuer 已提交
1271
    int buflen;
1272 1273 1274
#ifndef NO_FORK
    int multi = 0;
#endif
1275 1276
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
    || !defined(OPENSSL_NO_EC)
1277
    long rsa_count = 1;
1278
#endif
1279 1280
    openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
                                    ECDSA_SECONDS, ECDH_SECONDS };
1281 1282

    /* What follows are the buffers and key material. */
1283
#ifndef OPENSSL_NO_RC5
1284
    RC5_32_KEY rc5_ks;
1285 1286
#endif
#ifndef OPENSSL_NO_RC2
1287
    RC2_KEY rc2_ks;
1288 1289
#endif
#ifndef OPENSSL_NO_IDEA
1290
    IDEA_KEY_SCHEDULE idea_ks;
1291 1292
#endif
#ifndef OPENSSL_NO_SEED
1293
    SEED_KEY_SCHEDULE seed_ks;
1294 1295
#endif
#ifndef OPENSSL_NO_BF
1296
    BF_KEY bf_ks;
1297 1298
#endif
#ifndef OPENSSL_NO_CAST
1299
    CAST_KEY cast_ks;
1300
#endif
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    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
    };
1316
#ifndef OPENSSL_NO_CAMELLIA
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    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
    };
1328
    CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
1329 1330
#endif
#ifndef OPENSSL_NO_DES
1331 1332 1333 1334 1335 1336 1337 1338 1339
    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
    };
1340 1341
#endif
#ifndef OPENSSL_NO_RSA
1342
    static const unsigned int rsa_bits[RSA_NUM] = {
1343 1344
        512, 1024, 2048, 3072, 4096, 7680, 15360
    };
1345
    static const unsigned char *rsa_data[RSA_NUM] = {
1346 1347
        test512, test1024, test2048, test3072, test4096, test7680, test15360
    };
1348
    static const int rsa_data_length[RSA_NUM] = {
1349 1350 1351 1352 1353
        sizeof(test512), sizeof(test1024),
        sizeof(test2048), sizeof(test3072),
        sizeof(test4096), sizeof(test7680),
        sizeof(test15360)
    };
1354
    int rsa_doit[RSA_NUM] = { 0 };
P
Paul Yang 已提交
1355
    int primes = RSA_DEFAULT_PRIME_NUM;
1356 1357
#endif
#ifndef OPENSSL_NO_DSA
1358
    static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
1359
    int dsa_doit[DSA_NUM] = { 0 };
1360 1361
#endif
#ifndef OPENSSL_NO_EC
1362 1363 1364
    /*
     * 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
1365
     * the following arrays and increase the |ecdh_choices| list accordingly.
1366
     */
1367 1368 1369 1370 1371
    static const struct {
        const char *name;
        unsigned int nid;
        unsigned int bits;
    } test_curves[] = {
1372
        /* Prime Curves */
1373 1374 1375 1376 1377 1378
        {"secp160r1", NID_secp160r1, 160},
        {"nistp192", NID_X9_62_prime192v1, 192},
        {"nistp224", NID_secp224r1, 224},
        {"nistp256", NID_X9_62_prime256v1, 256},
        {"nistp384", NID_secp384r1, 384}, 
        {"nistp521", NID_secp521r1, 521},
1379
        /* Binary Curves */
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
        {"nistk163", NID_sect163k1, 163},
        {"nistk233", NID_sect233k1, 233}, 
        {"nistk283", NID_sect283k1, 283},
        {"nistk409", NID_sect409k1, 409},
        {"nistk571", NID_sect571k1, 571},
        {"nistb163", NID_sect163r2, 163},
        {"nistb233", NID_sect233r1, 233},
        {"nistb283", NID_sect283r1, 283},
        {"nistb409", NID_sect409r1, 409},
        {"nistb571", NID_sect571r1, 571},
1390
        /* Other and ECDH only ones */
1391 1392
        {"X25519", NID_X25519, 253},
        {"X448", NID_X448, 448}
1393
    };
1394
    int ecdsa_doit[ECDSA_NUM] = { 0 };
1395
    int ecdh_doit[EC_NUM] = { 0 };
1396
    OPENSSL_assert(OSSL_NELEM(test_curves) >= EC_NUM);
F
FdaSilvaYY 已提交
1397
#endif                          /* ndef OPENSSL_NO_EC */
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411

    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:
1412
            usertime = 0;
1413 1414
            break;
        case OPT_EVP:
1415
            evp_md = NULL;
1416 1417 1418 1419 1420
            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 已提交
1421
                           "%s: %s is an unknown cipher or digest\n",
1422
                           prog, opt_arg());
1423 1424 1425
                goto end;
            }
            doit[D_EVP] = 1;
1426 1427
            break;
        case OPT_DECRYPT:
1428
            decrypt = 1;
1429 1430
            break;
        case OPT_ENGINE:
1431 1432 1433 1434 1435 1436
            /*
             * 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();
1437 1438
            break;
        case OPT_MULTI:
1439
#ifndef NO_FORK
1440
            multi = atoi(opt_arg());
1441 1442 1443
#endif
            break;
        case OPT_ASYNCJOBS:
1444
#ifndef OPENSSL_NO_ASYNC
1445
            async_jobs = atoi(opt_arg());
1446 1447 1448 1449 1450 1451
            if (!ASYNC_is_capable()) {
                BIO_printf(bio_err,
                           "%s: async_jobs specified but async not supported\n",
                           prog);
                goto opterr;
            }
1452
            if (async_jobs > 99999) {
1453
                BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
1454 1455
                goto opterr;
            }
1456
#endif
1457
            break;
1458 1459
        case OPT_MISALIGN:
            if (!opt_int(opt_arg(), &misalign))
1460
                goto end;
1461
            if (misalign > MISALIGN) {
1462
                BIO_printf(bio_err,
1463 1464
                           "%s: Maximum offset is %d\n", prog, MISALIGN);
                goto opterr;
1465
            }
1466 1467 1468 1469 1470 1471
            break;
        case OPT_MR:
            mr = 1;
            break;
        case OPT_MB:
            multiblock = 1;
F
FdaSilvaYY 已提交
1472 1473 1474 1475 1476 1477
#ifdef OPENSSL_NO_MULTIBLOCK
            BIO_printf(bio_err,
                       "%s: -mb specified but multi-block support is disabled\n",
                       prog);
            goto end;
#endif
1478
            break;
R
Rich Salz 已提交
1479 1480 1481 1482
        case OPT_R_CASES:
            if (!opt_rand(o))
                goto end;
            break;
P
Paul Yang 已提交
1483 1484 1485 1486
        case OPT_PRIMES:
            if (!opt_int(opt_arg(), &primes))
                goto end;
            break;
1487 1488 1489 1490 1491 1492 1493 1494 1495
        case OPT_SECONDS:
            seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
                        = seconds.ecdh = atoi(opt_arg());
            break;
        case OPT_BYTES:
            lengths_single = atoi(opt_arg());
            lengths = &lengths_single;
            size_num = 1;
            break;
1496 1497 1498 1499 1500 1501
        }
    }
    argc = opt_num_rest();
    argv = opt_rest();

    /* Remaining arguments are algorithms. */
1502
    for (; *argv; argv++) {
1503 1504 1505 1506
        if (found(*argv, doit_choices, &i)) {
            doit[i] = 1;
            continue;
        }
1507
#ifndef OPENSSL_NO_DES
1508 1509 1510 1511
        if (strcmp(*argv, "des") == 0) {
            doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
            continue;
        }
1512
#endif
1513 1514 1515 1516
        if (strcmp(*argv, "sha") == 0) {
            doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
            continue;
        }
1517
#ifndef OPENSSL_NO_RSA
1518
        if (strcmp(*argv, "openssl") == 0)
1519 1520
            continue;
        if (strcmp(*argv, "rsa") == 0) {
1521 1522
            for (loop = 0; loop < OSSL_NELEM(rsa_doit); loop++)
                rsa_doit[loop] = 1;
1523 1524 1525 1526 1527 1528
            continue;
        }
        if (found(*argv, rsa_choices, &i)) {
            rsa_doit[i] = 1;
            continue;
        }
1529
#endif
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
#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;
        }
1540
#endif
1541
        if (strcmp(*argv, "aes") == 0) {
1542
            doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
1543 1544
            continue;
        }
1545
#ifndef OPENSSL_NO_CAMELLIA
1546
        if (strcmp(*argv, "camellia") == 0) {
1547
            doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
1548 1549
            continue;
        }
1550
#endif
1551
#ifndef OPENSSL_NO_EC
1552
        if (strcmp(*argv, "ecdsa") == 0) {
1553 1554
            for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
                ecdsa_doit[loop] = 1;
1555 1556 1557 1558 1559 1560 1561
            continue;
        }
        if (found(*argv, ecdsa_choices, &i)) {
            ecdsa_doit[i] = 2;
            continue;
        }
        if (strcmp(*argv, "ecdh") == 0) {
1562 1563
            for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
                ecdh_doit[loop] = 1;
1564 1565 1566 1567 1568
            continue;
        }
        if (found(*argv, ecdh_choices, &i)) {
            ecdh_doit[i] = 2;
            continue;
1569
        }
1570 1571 1572
#endif
        BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
        goto end;
1573
    }
1574

1575 1576
    /* Initialize the job pool if async mode is enabled */
    if (async_jobs > 0) {
1577 1578
        async_init = ASYNC_init_thread(async_jobs, async_jobs);
        if (!async_init) {
1579 1580 1581 1582 1583 1584
            BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
            goto end;
        }
    }

    loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
1585 1586
    loopargs =
        app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
1587 1588
    memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));

1589
    for (i = 0; i < loopargs_len; i++) {
1590 1591 1592 1593 1594 1595 1596 1597
        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;
            }
        }

P
Patrick Steuer 已提交
1598 1599 1600 1601 1602 1603
        buflen = lengths[size_num - 1] + MAX_MISALIGNMENT + 1;
        loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
        loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
        memset(loopargs[i].buf_malloc, 0, buflen);
        memset(loopargs[i].buf2_malloc, 0, buflen);

1604 1605 1606
        /* 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;
1607 1608 1609 1610
#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
1611 1612
    }

1613
#ifndef NO_FORK
1614
    if (multi && do_multi(multi, size_num))
1615
        goto show_res;
1616
#endif
1617

1618
    /* Initialize the engine after the fork */
1619
    e = setup_engine(engine_id, 0);
1620

1621
    /* No parameters; turn on everything. */
1622
    if ((argc == 0) && !doit[D_EVP]) {
1623
        for (i = 0; i < ALGOR_NUM; i++)
1624 1625
            if (i != D_EVP)
                doit[i] = 1;
F
FdaSilvaYY 已提交
1626
#ifndef OPENSSL_NO_RSA
1627 1628
        for (i = 0; i < RSA_NUM; i++)
            rsa_doit[i] = 1;
F
FdaSilvaYY 已提交
1629
#endif
M
Matt Caswell 已提交
1630
#ifndef OPENSSL_NO_DSA
1631 1632
        for (i = 0; i < DSA_NUM; i++)
            dsa_doit[i] = 1;
M
Matt Caswell 已提交
1633
#endif
1634
#ifndef OPENSSL_NO_EC
1635 1636 1637 1638
        for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
            ecdsa_doit[loop] = 1;
        for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
            ecdh_doit[loop] = 1;
1639
#endif
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
    }
    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");

1650
#ifndef OPENSSL_NO_RSA
1651
    for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
1652 1653 1654 1655
        if (primes > RSA_DEFAULT_PRIME_NUM) {
            /* for multi-prime RSA, skip this */
            break;
        }
1656 1657 1658 1659
        for (k = 0; k < RSA_NUM; k++) {
            const unsigned char *p;

            p = rsa_data[k];
1660 1661
            loopargs[i].rsa_key[k] =
                d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
1662
            if (loopargs[i].rsa_key[k] == NULL) {
1663 1664
                BIO_printf(bio_err,
                           "internal error loading RSA key number %d\n", k);
1665 1666
                goto end;
            }
1667
        }
1668 1669 1670
    }
#endif
#ifndef OPENSSL_NO_DSA
1671
    for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
1672 1673 1674
        loopargs[i].dsa_key[0] = get_dsa(512);
        loopargs[i].dsa_key[1] = get_dsa(1024);
        loopargs[i].dsa_key[2] = get_dsa(2048);
1675
    }
1676 1677
#endif
#ifndef OPENSSL_NO_DES
1678 1679 1680
    DES_set_key_unchecked(&key, &sch);
    DES_set_key_unchecked(&key2, &sch2);
    DES_set_key_unchecked(&key3, &sch3);
1681
#endif
1682 1683 1684
    AES_set_encrypt_key(key16, 128, &aes_ks1);
    AES_set_encrypt_key(key24, 192, &aes_ks2);
    AES_set_encrypt_key(key32, 256, &aes_ks3);
1685
#ifndef OPENSSL_NO_CAMELLIA
1686 1687 1688
    Camellia_set_key(key16, 128, &camellia_ks1);
    Camellia_set_key(ckey24, 192, &camellia_ks2);
    Camellia_set_key(ckey32, 256, &camellia_ks3);
1689 1690
#endif
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
1691
    IDEA_set_encrypt_key(key16, &idea_ks);
1692 1693
#endif
#ifndef OPENSSL_NO_SEED
1694
    SEED_set_key(key16, &seed_ks);
1695 1696
#endif
#ifndef OPENSSL_NO_RC4
1697
    RC4_set_key(&rc4_ks, 16, key16);
1698 1699
#endif
#ifndef OPENSSL_NO_RC2
1700
    RC2_set_key(&rc2_ks, 16, key16, 128);
1701 1702
#endif
#ifndef OPENSSL_NO_RC5
1703
    RC5_32_set_key(&rc5_ks, 16, key16, 12);
1704 1705
#endif
#ifndef OPENSSL_NO_BF
1706
    BF_set_key(&bf_ks, 16, key16);
1707 1708
#endif
#ifndef OPENSSL_NO_CAST
1709
    CAST_set_key(&cast_ks, 16, key16);
1710 1711 1712
#endif
#ifndef SIGALRM
# ifndef OPENSSL_NO_DES
1713 1714 1715 1716 1717 1718 1719
    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--)
1720 1721
            DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
                            (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
        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;
1754
    c[D_RAND][0] = count;
1755

1756
    for (i = 1; i < size_num; i++) {
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
        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;
1772
        c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
1773
        c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795

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

1797
#  ifndef OPENSSL_NO_RSA
1798 1799 1800 1801 1802
    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 已提交
1803
        if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
1804 1805 1806
            rsa_doit[i] = 0;
        else {
            if (rsa_c[i][0] == 0) {
1807
                rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1808 1809 1810 1811
                rsa_c[i][1] = 20;
            }
        }
    }
1812
#  endif
1813

1814
#  ifndef OPENSSL_NO_DSA
1815 1816 1817 1818 1819
    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 已提交
1820
        if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
1821 1822
            dsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1823
            if (dsa_c[i][0] == 0) {
1824
                dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
1825 1826 1827 1828
                dsa_c[i][1] = 1;
            }
        }
    }
1829
#  endif
1830

1831
#  ifndef OPENSSL_NO_EC
1832 1833 1834 1835 1836
    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 已提交
1837
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1838 1839
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1840
            if (ecdsa_c[i][0] == 0) {
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
                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 已提交
1851
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1852 1853
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1854
            if (ecdsa_c[i][0] == 0) {
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
                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 已提交
1865
        if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
1866 1867
            ecdsa_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1868
            if (ecdsa_c[i][0] == 0) {
1869 1870 1871 1872 1873
                ecdsa_c[i][0] = 1;
                ecdsa_c[i][1] = 1;
            }
        }
    }
1874

1875 1876 1877
    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 已提交
1878
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1879 1880
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1881
            if (ecdh_c[i][0] == 0) {
1882 1883 1884 1885 1886 1887 1888
                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 已提交
1889
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1890 1891
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1892
            if (ecdh_c[i][0] == 0) {
1893 1894 1895 1896 1897 1898 1899
                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 已提交
1900
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
1901 1902
            ecdh_doit[i] = 0;
        else {
F
FdaSilvaYY 已提交
1903
            if (ecdh_c[i][0] == 0) {
1904 1905 1906 1907
                ecdh_c[i][0] = 1;
            }
        }
    }
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
    /* repeated code good to factorize */
    ecdh_c[R_EC_BRP256R1][0] = count / 1000;
    for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
        ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
            ecdh_doit[i] = 0;
        else {
            if (ecdh_c[i][0] == 0) {
                ecdh_c[i][0] = 1;
            }
        }
    }
    ecdh_c[R_EC_BRP256T1][0] = count / 1000;
    for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
        ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
        if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
            ecdh_doit[i] = 0;
        else {
            if (ecdh_c[i][0] == 0) {
                ecdh_c[i][0] = 1;
            }
        }
    }
    /* default iteration count for the last two EC Curves */
    ecdh_c[R_EC_X25519][0] = count / 1800;
    ecdh_c[R_EC_X448][0] = count / 7200;
1934
#  endif
B
Bodo Möller 已提交
1935

1936
# else
1937 1938
/* not worth fixing */
#  error "You cannot disable DES on systems without SIGALRM."
1939
# endif                         /* OPENSSL_NO_DES */
1940 1941
#else
# ifndef _WIN32
1942
    signal(SIGALRM, sig_done);
1943
# endif
1944
#endif                          /* SIGALRM */
1945

1946
#ifndef OPENSSL_NO_MD2
1947
    if (doit[D_MD2]) {
1948 1949 1950
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
                          seconds.sym);
1951
            Time_F(START);
1952
            count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
1953
            d = Time_F(STOP);
1954
            print_result(D_MD2, testnum, count, d);
1955 1956
        }
    }
1957 1958
#endif
#ifndef OPENSSL_NO_MDC2
1959
    if (doit[D_MDC2]) {
1960 1961 1962
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
                          seconds.sym);
1963
            Time_F(START);
1964
            count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
1965
            d = Time_F(STOP);
1966
            print_result(D_MDC2, testnum, count, d);
1967 1968
        }
    }
1969
#endif
1970

1971
#ifndef OPENSSL_NO_MD4
1972
    if (doit[D_MD4]) {
1973 1974 1975
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
                          seconds.sym);
1976
            Time_F(START);
1977
            count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
1978
            d = Time_F(STOP);
1979
            print_result(D_MD4, testnum, count, d);
1980 1981
        }
    }
1982
#endif
1983

1984
#ifndef OPENSSL_NO_MD5
1985
    if (doit[D_MD5]) {
1986 1987 1988
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
                          seconds.sym);
1989
            Time_F(START);
1990
            count = run_benchmark(async_jobs, MD5_loop, loopargs);
1991
            d = Time_F(STOP);
1992
            print_result(D_MD5, testnum, count, d);
1993 1994
        }
    }
1995

1996
    if (doit[D_HMAC]) {
F
FdaSilvaYY 已提交
1997
        static const char hmac_key[] = "This is a key...";
1998 1999
        int len = strlen(hmac_key);

2000
        for (i = 0; i < loopargs_len; i++) {
2001 2002 2003 2004 2005
            loopargs[i].hctx = HMAC_CTX_new();
            if (loopargs[i].hctx == NULL) {
                BIO_printf(bio_err, "HMAC malloc failure, exiting...");
                exit(1);
            }
2006

2007
            HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
2008
        }
2009 2010 2011
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
                          seconds.sym);
2012
            Time_F(START);
2013
            count = run_benchmark(async_jobs, HMAC_loop, loopargs);
2014
            d = Time_F(STOP);
2015 2016
            print_result(D_HMAC, testnum, count, d);
        }
2017
        for (i = 0; i < loopargs_len; i++) {
2018
            HMAC_CTX_free(loopargs[i].hctx);
2019 2020
        }
    }
2021
#endif
2022
    if (doit[D_SHA1]) {
2023 2024 2025
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
                          seconds.sym);
2026
            Time_F(START);
2027
            count = run_benchmark(async_jobs, SHA1_loop, loopargs);
2028
            d = Time_F(STOP);
2029
            print_result(D_SHA1, testnum, count, d);
2030 2031 2032
        }
    }
    if (doit[D_SHA256]) {
2033
        for (testnum = 0; testnum < size_num; testnum++) {
2034
            print_message(names[D_SHA256], c[D_SHA256][testnum],
2035
                          lengths[testnum], seconds.sym);
2036
            Time_F(START);
2037
            count = run_benchmark(async_jobs, SHA256_loop, loopargs);
2038
            d = Time_F(STOP);
2039
            print_result(D_SHA256, testnum, count, d);
2040 2041 2042
        }
    }
    if (doit[D_SHA512]) {
2043
        for (testnum = 0; testnum < size_num; testnum++) {
2044
            print_message(names[D_SHA512], c[D_SHA512][testnum],
2045
                          lengths[testnum], seconds.sym);
2046
            Time_F(START);
2047
            count = run_benchmark(async_jobs, SHA512_loop, loopargs);
2048
            d = Time_F(STOP);
2049
            print_result(D_SHA512, testnum, count, d);
2050 2051
        }
    }
2052
#ifndef OPENSSL_NO_WHIRLPOOL
2053
    if (doit[D_WHIRLPOOL]) {
2054
        for (testnum = 0; testnum < size_num; testnum++) {
2055
            print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
2056
                          lengths[testnum], seconds.sym);
2057
            Time_F(START);
2058
            count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
2059
            d = Time_F(STOP);
2060
            print_result(D_WHIRLPOOL, testnum, count, d);
2061 2062
        }
    }
2063
#endif
2064

2065
#ifndef OPENSSL_NO_RMD160
2066
    if (doit[D_RMD160]) {
2067
        for (testnum = 0; testnum < size_num; testnum++) {
2068
            print_message(names[D_RMD160], c[D_RMD160][testnum],
2069
                          lengths[testnum], seconds.sym);
2070
            Time_F(START);
2071
            count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
2072
            d = Time_F(STOP);
2073
            print_result(D_RMD160, testnum, count, d);
2074 2075
        }
    }
2076 2077
#endif
#ifndef OPENSSL_NO_RC4
2078
    if (doit[D_RC4]) {
2079 2080 2081
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
                          seconds.sym);
2082
            Time_F(START);
2083
            count = run_benchmark(async_jobs, RC4_loop, loopargs);
2084
            d = Time_F(STOP);
2085
            print_result(D_RC4, testnum, count, d);
2086 2087
        }
    }
2088 2089
#endif
#ifndef OPENSSL_NO_DES
2090
    if (doit[D_CBC_DES]) {
2091
        for (testnum = 0; testnum < size_num; testnum++) {
2092
            print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
2093
                          lengths[testnum], seconds.sym);
2094
            Time_F(START);
2095
            count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
2096
            d = Time_F(STOP);
2097
            print_result(D_CBC_DES, testnum, count, d);
2098 2099
        }
    }
2100

2101
    if (doit[D_EDE3_DES]) {
2102
        for (testnum = 0; testnum < size_num; testnum++) {
2103
            print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
2104
                          lengths[testnum], seconds.sym);
2105
            Time_F(START);
2106 2107
            count =
                run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
2108
            d = Time_F(STOP);
2109
            print_result(D_EDE3_DES, testnum, count, d);
2110 2111
        }
    }
2112
#endif
M
Matt Caswell 已提交
2113

2114
    if (doit[D_CBC_128_AES]) {
2115
        for (testnum = 0; testnum < size_num; testnum++) {
2116
            print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2117
                          lengths[testnum], seconds.sym);
2118
            Time_F(START);
2119 2120
            count =
                run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
2121
            d = Time_F(STOP);
2122
            print_result(D_CBC_128_AES, testnum, count, d);
2123 2124 2125
        }
    }
    if (doit[D_CBC_192_AES]) {
2126
        for (testnum = 0; testnum < size_num; testnum++) {
2127
            print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2128
                          lengths[testnum], seconds.sym);
2129
            Time_F(START);
2130 2131
            count =
                run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
2132
            d = Time_F(STOP);
2133
            print_result(D_CBC_192_AES, testnum, count, d);
2134 2135 2136
        }
    }
    if (doit[D_CBC_256_AES]) {
2137
        for (testnum = 0; testnum < size_num; testnum++) {
2138
            print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2139
                          lengths[testnum], seconds.sym);
2140
            Time_F(START);
2141 2142
            count =
                run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
2143
            d = Time_F(STOP);
2144
            print_result(D_CBC_256_AES, testnum, count, d);
2145 2146
        }
    }
B
Ben Laurie 已提交
2147

2148
    if (doit[D_IGE_128_AES]) {
2149
        for (testnum = 0; testnum < size_num; testnum++) {
2150
            print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2151
                          lengths[testnum], seconds.sym);
2152
            Time_F(START);
2153 2154
            count =
                run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
2155
            d = Time_F(STOP);
2156
            print_result(D_IGE_128_AES, testnum, count, d);
2157 2158 2159
        }
    }
    if (doit[D_IGE_192_AES]) {
2160
        for (testnum = 0; testnum < size_num; testnum++) {
2161
            print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2162
                          lengths[testnum], seconds.sym);
2163
            Time_F(START);
2164 2165
            count =
                run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
2166
            d = Time_F(STOP);
2167
            print_result(D_IGE_192_AES, testnum, count, d);
2168 2169 2170
        }
    }
    if (doit[D_IGE_256_AES]) {
2171
        for (testnum = 0; testnum < size_num; testnum++) {
2172
            print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2173
                          lengths[testnum], seconds.sym);
2174
            Time_F(START);
2175 2176
            count =
                run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
2177
            d = Time_F(STOP);
2178
            print_result(D_IGE_256_AES, testnum, count, d);
2179 2180 2181
        }
    }
    if (doit[D_GHASH]) {
2182
        for (i = 0; i < loopargs_len; i++) {
2183 2184 2185 2186
            loopargs[i].gcm_ctx =
                CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
            CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
                                (unsigned char *)"0123456789ab", 12);
2187
        }
2188

2189
        for (testnum = 0; testnum < size_num; testnum++) {
2190
            print_message(names[D_GHASH], c[D_GHASH][testnum],
2191
                          lengths[testnum], seconds.sym);
2192
            Time_F(START);
2193
            count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
2194
            d = Time_F(STOP);
2195
            print_result(D_GHASH, testnum, count, d);
2196
        }
2197
        for (i = 0; i < loopargs_len; i++)
2198
            CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
2199
    }
2200
#ifndef OPENSSL_NO_CAMELLIA
2201
    if (doit[D_CBC_128_CML]) {
2202 2203 2204 2205 2206
        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;
        }
2207
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2208
            print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2209
                          lengths[testnum], seconds.sym);
2210
            Time_F(START);
2211 2212
            for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2213
                                     (size_t)lengths[testnum], &camellia_ks1,
2214 2215
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2216
            print_result(D_CBC_128_CML, testnum, count, d);
2217 2218 2219
        }
    }
    if (doit[D_CBC_192_CML]) {
2220 2221 2222 2223 2224
        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;
        }
2225
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2226
            print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2227
                          lengths[testnum], seconds.sym);
2228 2229 2230 2231
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2232
            Time_F(START);
2233 2234
            for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2235
                                     (size_t)lengths[testnum], &camellia_ks2,
2236 2237
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2238
            print_result(D_CBC_192_CML, testnum, count, d);
2239 2240 2241
        }
    }
    if (doit[D_CBC_256_CML]) {
2242 2243 2244 2245 2246
        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;
        }
2247
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2248
            print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2249
                          lengths[testnum], seconds.sym);
2250
            Time_F(START);
2251 2252
            for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
                Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2253
                                     (size_t)lengths[testnum], &camellia_ks3,
2254 2255
                                     iv, CAMELLIA_ENCRYPT);
            d = Time_F(STOP);
2256
            print_result(D_CBC_256_CML, testnum, count, d);
2257 2258
        }
    }
2259 2260
#endif
#ifndef OPENSSL_NO_IDEA
2261
    if (doit[D_CBC_IDEA]) {
2262 2263 2264 2265 2266
        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;
        }
2267
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2268
            print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2269
                          lengths[testnum], seconds.sym);
2270
            Time_F(START);
2271
            for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
R
Rich Salz 已提交
2272
                IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2273
                                 (size_t)lengths[testnum], &idea_ks,
2274 2275
                                 iv, IDEA_ENCRYPT);
            d = Time_F(STOP);
2276
            print_result(D_CBC_IDEA, testnum, count, d);
2277 2278
        }
    }
2279 2280
#endif
#ifndef OPENSSL_NO_SEED
2281
    if (doit[D_CBC_SEED]) {
2282 2283 2284 2285 2286
        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;
        }
2287
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2288
            print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2289
                          lengths[testnum], seconds.sym);
2290
            Time_F(START);
2291 2292
            for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
                SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2293
                                 (size_t)lengths[testnum], &seed_ks, iv, 1);
2294
            d = Time_F(STOP);
2295
            print_result(D_CBC_SEED, testnum, count, d);
2296 2297
        }
    }
2298 2299
#endif
#ifndef OPENSSL_NO_RC2
2300
    if (doit[D_CBC_RC2]) {
2301 2302 2303 2304 2305
        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;
        }
2306
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2307
            print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2308
                          lengths[testnum], seconds.sym);
2309 2310 2311 2312
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2313
            Time_F(START);
2314 2315
            for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
                RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2316
                                (size_t)lengths[testnum], &rc2_ks,
2317 2318
                                iv, RC2_ENCRYPT);
            d = Time_F(STOP);
2319
            print_result(D_CBC_RC2, testnum, count, d);
2320 2321
        }
    }
2322 2323
#endif
#ifndef OPENSSL_NO_RC5
2324
    if (doit[D_CBC_RC5]) {
2325 2326 2327 2328 2329
        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;
        }
2330
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2331
            print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2332
                          lengths[testnum], seconds.sym);
2333 2334 2335 2336
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2337
            Time_F(START);
2338 2339
            for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
                RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2340
                                   (size_t)lengths[testnum], &rc5_ks,
2341 2342
                                   iv, RC5_ENCRYPT);
            d = Time_F(STOP);
2343
            print_result(D_CBC_RC5, testnum, count, d);
2344 2345
        }
    }
2346 2347
#endif
#ifndef OPENSSL_NO_BF
2348
    if (doit[D_CBC_BF]) {
2349 2350 2351 2352 2353
        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;
        }
2354
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2355
            print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2356
                          lengths[testnum], seconds.sym);
2357
            Time_F(START);
2358 2359
            for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
                BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2360
                               (size_t)lengths[testnum], &bf_ks,
2361 2362
                               iv, BF_ENCRYPT);
            d = Time_F(STOP);
2363
            print_result(D_CBC_BF, testnum, count, d);
2364 2365
        }
    }
2366 2367
#endif
#ifndef OPENSSL_NO_CAST
2368
    if (doit[D_CBC_CAST]) {
2369 2370 2371 2372 2373
        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;
        }
2374
        for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
2375
            print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2376
                          lengths[testnum], seconds.sym);
2377
            Time_F(START);
2378 2379
            for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
                CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
2380
                                 (size_t)lengths[testnum], &cast_ks,
2381 2382
                                 iv, CAST_ENCRYPT);
            d = Time_F(STOP);
2383
            print_result(D_CBC_CAST, testnum, count, d);
2384 2385
        }
    }
2386
#endif
2387
    if (doit[D_RAND]) {
2388 2389 2390
        for (testnum = 0; testnum < size_num; testnum++) {
            print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
                          seconds.sym);
2391 2392 2393 2394 2395 2396
            Time_F(START);
            count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
            d = Time_F(STOP);
            print_result(D_RAND, testnum, count, d);
        }
    }
2397

2398 2399 2400 2401 2402
    if (doit[D_EVP]) {
        if (multiblock && evp_cipher) {
            if (!
                (EVP_CIPHER_flags(evp_cipher) &
                 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
R
Rich Salz 已提交
2403
                BIO_printf(bio_err, "%s is not multi-block capable\n",
2404
                           OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
2405 2406
                goto end;
            }
2407 2408 2409 2410
            if (async_jobs > 0) {
                BIO_printf(bio_err, "Async mode is not supported, exiting...");
                exit(1);
            }
2411
            multiblock_speed(evp_cipher, lengths_single, &seconds);
2412
            ret = 0;
2413 2414
            goto end;
        }
2415
        for (testnum = 0; testnum < size_num; testnum++) {
2416 2417
            if (evp_cipher) {

2418
                names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2419 2420 2421 2422
                /*
                 * -O3 -fschedule-insns messes up an optimization here!
                 * names[D_EVP] somehow becomes NULL
                 */
2423 2424
                print_message(names[D_EVP], save_count, lengths[testnum],
                              seconds.sym);
2425 2426 2427

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

2431
                    EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2432 2433 2434 2435 2436 2437 2438

                    keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
                    loopargs[k].key = app_malloc(keylen, "evp_cipher key");
                    EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
                    EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
                                      loopargs[k].key, NULL, -1);
                    OPENSSL_clear_free(loopargs[k].key, keylen);
2439
                }
2440 2441 2442 2443 2444 2445 2446
                switch (EVP_CIPHER_mode(evp_cipher)) {
                case EVP_CIPH_CCM_MODE:
                    loopfunc = EVP_Update_loop_ccm;
                    break;
                default:
                    loopfunc = EVP_Update_loop;
                }
2447 2448

                Time_F(START);
2449
                count = run_benchmark(async_jobs, loopfunc, loopargs);
2450
                d = Time_F(STOP);
2451 2452 2453
                for (k = 0; k < loopargs_len; k++) {
                    EVP_CIPHER_CTX_free(loopargs[k].ctx);
                }
2454 2455
            }
            if (evp_md) {
2456
                names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2457 2458
                print_message(names[D_EVP], save_count, lengths[testnum],
                              seconds.sym);
2459
                Time_F(START);
2460
                count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
2461 2462
                d = Time_F(STOP);
            }
2463
            print_result(D_EVP, testnum, count, d);
2464 2465
        }
    }
2466

2467
    for (i = 0; i < loopargs_len; i++)
2468 2469
        RAND_bytes(loopargs[i].buf, 36);

2470
#ifndef OPENSSL_NO_RSA
2471 2472 2473
    for (testnum = 0; testnum < RSA_NUM; testnum++) {
        int st = 0;
        if (!rsa_doit[testnum])
2474
            continue;
2475
        for (i = 0; i < loopargs_len; i++) {
P
Paul Yang 已提交
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
            if (primes > 2) {
                /* we haven't set keys yet,  generate multi-prime RSA keys */
                BIGNUM *bn = BN_new();

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

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

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

                if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
                                                  rsa_bits[testnum],
                                                  primes, bn, NULL)) {
                    BN_free(bn);
                    goto end;
                }
                BN_free(bn);
            }
2504
            st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2505
                          &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2506 2507 2508
            if (st == 0)
                break;
        }
2509
        if (st == 0) {
2510 2511 2512 2513 2514 2515
            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",
2516
                               rsa_c[testnum][0], rsa_bits[testnum],
2517
                               seconds.rsa);
2518
            /* RSA_blinding_on(rsa_key[testnum],NULL); */
2519
            Time_F(START);
2520
            count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
2521 2522 2523
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R1:%ld:%d:%.2f\n"
2524
                       : "%ld %u bits private RSA's in %.2fs\n",
2525
                       count, rsa_bits[testnum], d);
2526
            rsa_results[testnum][0] = (double)count / d;
2527 2528
            rsa_count = count;
        }
2529

2530 2531
        for (i = 0; i < loopargs_len; i++) {
            st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2532
                            loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
2533 2534 2535
            if (st <= 0)
                break;
        }
2536
        if (st <= 0) {
2537 2538 2539
            BIO_printf(bio_err,
                       "RSA verify failure.  No RSA verify will be done.\n");
            ERR_print_errors(bio_err);
2540
            rsa_doit[testnum] = 0;
2541 2542
        } else {
            pkey_print_message("public", "rsa",
2543
                               rsa_c[testnum][1], rsa_bits[testnum],
2544
                               seconds.rsa);
2545
            Time_F(START);
2546
            count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
2547 2548 2549
            d = Time_F(STOP);
            BIO_printf(bio_err,
                       mr ? "+R2:%ld:%d:%.2f\n"
2550
                       : "%ld %u bits public RSA's in %.2fs\n",
2551
                       count, rsa_bits[testnum], d);
2552
            rsa_results[testnum][1] = (double)count / d;
2553
        }
2554

2555 2556
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2557 2558
            for (testnum++; testnum < RSA_NUM; testnum++)
                rsa_doit[testnum] = 0;
2559 2560
        }
    }
F
FdaSilvaYY 已提交
2561
#endif                          /* OPENSSL_NO_RSA */
2562

2563
    for (i = 0; i < loopargs_len; i++)
2564 2565
        RAND_bytes(loopargs[i].buf, 36);

2566
#ifndef OPENSSL_NO_DSA
2567 2568 2569
    for (testnum = 0; testnum < DSA_NUM; testnum++) {
        int st = 0;
        if (!dsa_doit[testnum])
2570 2571
            continue;

2572 2573
        /* DSA_generate_key(dsa_key[testnum]); */
        /* DSA_sign_setup(dsa_key[testnum],NULL); */
2574 2575
        for (i = 0; i < loopargs_len; i++) {
            st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2576
                          &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2577 2578 2579
            if (st == 0)
                break;
        }
2580
        if (st == 0) {
2581 2582 2583 2584 2585 2586
            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",
2587
                               dsa_c[testnum][0], dsa_bits[testnum],
2588
                               seconds.dsa);
2589
            Time_F(START);
2590
            count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
2591 2592
            d = Time_F(STOP);
            BIO_printf(bio_err,
2593 2594
                       mr ? "+R3:%ld:%u:%.2f\n"
                       : "%ld %u bits DSA signs in %.2fs\n",
2595
                       count, dsa_bits[testnum], d);
2596
            dsa_results[testnum][0] = (double)count / d;
2597 2598
            rsa_count = count;
        }
B
Bodo Möller 已提交
2599

2600 2601
        for (i = 0; i < loopargs_len; i++) {
            st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
F
FdaSilvaYY 已提交
2602
                            loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
2603 2604 2605
            if (st <= 0)
                break;
        }
2606
        if (st <= 0) {
2607 2608 2609
            BIO_printf(bio_err,
                       "DSA verify failure.  No DSA verify will be done.\n");
            ERR_print_errors(bio_err);
2610
            dsa_doit[testnum] = 0;
2611 2612
        } else {
            pkey_print_message("verify", "dsa",
2613
                               dsa_c[testnum][1], dsa_bits[testnum],
2614
                               seconds.dsa);
2615
            Time_F(START);
2616
            count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
2617 2618
            d = Time_F(STOP);
            BIO_printf(bio_err,
2619 2620
                       mr ? "+R4:%ld:%u:%.2f\n"
                       : "%ld %u bits DSA verify in %.2fs\n",
2621
                       count, dsa_bits[testnum], d);
2622
            dsa_results[testnum][1] = (double)count / d;
2623
        }
B
Bodo Möller 已提交
2624

2625 2626
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2627 2628
            for (testnum++; testnum < DSA_NUM; testnum++)
                dsa_doit[testnum] = 0;
2629 2630
        }
    }
F
FdaSilvaYY 已提交
2631
#endif                          /* OPENSSL_NO_DSA */
B
Bodo Möller 已提交
2632

2633
#ifndef OPENSSL_NO_EC
2634
    for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
2635
        int st = 1;
2636

2637
        if (!ecdsa_doit[testnum])
2638
            continue;           /* Ignore Curve */
2639
        for (i = 0; i < loopargs_len; i++) {
2640
            loopargs[i].ecdsa[testnum] =
2641
                EC_KEY_new_by_curve_name(test_curves[testnum].nid);
2642 2643 2644 2645 2646 2647
            if (loopargs[i].ecdsa[testnum] == NULL) {
                st = 0;
                break;
            }
        }
        if (st == 0) {
2648 2649 2650 2651
            BIO_printf(bio_err, "ECDSA failure.\n");
            ERR_print_errors(bio_err);
            rsa_count = 1;
        } else {
2652 2653 2654 2655 2656
            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,
2657 2658
                                &loopargs[i].siglen,
                                loopargs[i].ecdsa[testnum]);
2659 2660 2661
                if (st == 0)
                    break;
            }
2662
            if (st == 0) {
2663 2664 2665 2666 2667 2668
                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",
2669
                                   ecdsa_c[testnum][0],
2670
                                   test_curves[testnum].bits, seconds.ecdsa);
2671
                Time_F(START);
2672
                count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
2673 2674 2675
                d = Time_F(STOP);

                BIO_printf(bio_err,
2676 2677 2678
                           mr ? "+R5:%ld:%u:%.2f\n" :
                           "%ld %u bits ECDSA signs in %.2fs \n",
                           count, test_curves[testnum].bits, d);
2679
                ecdsa_results[testnum][0] = (double)count / d;
2680 2681 2682 2683
                rsa_count = count;
            }

            /* Perform ECDSA verification test */
2684 2685
            for (i = 0; i < loopargs_len; i++) {
                st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
2686 2687
                                  loopargs[i].siglen,
                                  loopargs[i].ecdsa[testnum]);
2688 2689 2690
                if (st != 1)
                    break;
            }
2691
            if (st != 1) {
2692 2693 2694
                BIO_printf(bio_err,
                           "ECDSA verify failure.  No ECDSA verify will be done.\n");
                ERR_print_errors(bio_err);
2695
                ecdsa_doit[testnum] = 0;
2696 2697
            } else {
                pkey_print_message("verify", "ecdsa",
2698
                                   ecdsa_c[testnum][1],
2699
                                   test_curves[testnum].bits, seconds.ecdsa);
2700
                Time_F(START);
2701
                count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
2702 2703
                d = Time_F(STOP);
                BIO_printf(bio_err,
2704 2705 2706
                           mr ? "+R6:%ld:%u:%.2f\n"
                           : "%ld %u bits ECDSA verify in %.2fs\n",
                           count, test_curves[testnum].bits, d);
2707
                ecdsa_results[testnum][1] = (double)count / d;
2708 2709 2710 2711
            }

            if (rsa_count <= 1) {
                /* if longer than 10s, don't do any more */
2712 2713
                for (testnum++; testnum < EC_NUM; testnum++)
                    ecdsa_doit[testnum] = 0;
2714 2715 2716
            }
        }
    }
2717

2718
    for (testnum = 0; testnum < EC_NUM; testnum++) {
2719 2720
        int ecdh_checks = 1;

2721
        if (!ecdh_doit[testnum])
2722
            continue;
2723

2724
        for (i = 0; i < loopargs_len; i++) {
N
Nicola Tuveri 已提交
2725
            EVP_PKEY_CTX *kctx = NULL;
2726
            EVP_PKEY_CTX *test_ctx = NULL;
N
Nicola Tuveri 已提交
2727 2728 2729
            EVP_PKEY_CTX *ctx = NULL;
            EVP_PKEY *key_A = NULL;
            EVP_PKEY *key_B = NULL;
2730
            size_t outlen;
2731
            size_t test_outlen;
2732

2733 2734 2735 2736 2737 2738 2739
            /* 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);
            }

2740 2741 2742 2743 2744 2745
            /* 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. */
2746
            kctx = EVP_PKEY_CTX_new_id(test_curves[testnum].nid, NULL); /* keygen ctx from NID */
2747
            if (!kctx) {
2748 2749 2750
                EVP_PKEY_CTX *pctx = NULL;
                EVP_PKEY *params = NULL;

2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
                /* 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;
                }

2770 2771 2772 2773 2774 2775 2776
                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
2777
                                                               [testnum].nid) ||
2778
                       /* Create the parameter object params */
N
Nicola Tuveri 已提交
2779
                       !EVP_PKEY_paramgen(pctx, &params)) {
2780
                    ecdh_checks = 0;
2781
                    BIO_printf(bio_err, "ECDH EC params init failure.\n");
2782
                    ERR_print_errors(bio_err);
2783
                    rsa_count = 1;
2784
                    break;
2785
                }
2786 2787 2788
                /* Create the context for the key generation */
                kctx = EVP_PKEY_CTX_new(params, NULL);

2789 2790 2791 2792
                EVP_PKEY_free(params);
                params = NULL;
                EVP_PKEY_CTX_free(pctx);
                pctx = NULL;
A
Andrea Grandi 已提交
2793
            }
2794
            if (kctx == NULL ||      /* keygen ctx is not null */
N
Nicola Tuveri 已提交
2795
                !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
2796 2797 2798 2799 2800
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH keygen failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
2801
            }
2802

2803 2804 2805 2806 2807 2808
            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 */
2809
                outlen == 0 ||  /* ensure outlen is a valid size */
N
Nicola Tuveri 已提交
2810
                outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
2811 2812 2813 2814 2815 2816 2817
                ecdh_checks = 0;
                BIO_printf(bio_err, "ECDH key generation failure.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
            /* 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;
            }
2835 2836 2837 2838 2839

            /* 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;
2840 2841 2842 2843 2844 2845
                BIO_printf(bio_err, "ECDH computations don't match.\n");
                ERR_print_errors(bio_err);
                rsa_count = 1;
                break;
            }

2846
            loopargs[i].ecdh_ctx[testnum] = ctx;
2847
            loopargs[i].outlen[testnum] = outlen;
2848

2849 2850
            EVP_PKEY_free(key_A);
            EVP_PKEY_free(key_B);
2851 2852
            EVP_PKEY_CTX_free(kctx);
            kctx = NULL;
2853 2854
            EVP_PKEY_CTX_free(test_ctx);
            test_ctx = NULL;
2855 2856 2857
        }
        if (ecdh_checks != 0) {
            pkey_print_message("", "ecdh",
2858
                               ecdh_c[testnum][0],
2859
                               test_curves[testnum].bits, seconds.ecdh);
2860
            Time_F(START);
2861 2862
            count =
                run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
2863 2864
            d = Time_F(STOP);
            BIO_printf(bio_err,
2865
                       mr ? "+R7:%ld:%d:%.2f\n" :
2866 2867
                       "%ld %u-bits ECDH ops in %.2fs\n", count,
                       test_curves[testnum].bits, d);
2868
            ecdh_results[testnum][0] = (double)count / d;
2869
            rsa_count = count;
2870
        }
B
Bodo Möller 已提交
2871

2872 2873
        if (rsa_count <= 1) {
            /* if longer than 10s, don't do any more */
2874
            for (testnum++; testnum < OSSL_NELEM(ecdh_doit); testnum++)
2875
                ecdh_doit[testnum] = 0;
2876 2877
        }
    }
F
FdaSilvaYY 已提交
2878
#endif                          /* OPENSSL_NO_EC */
2879
#ifndef NO_FORK
2880
 show_res:
2881
#endif
2882
    if (!mr) {
R
Rich Salz 已提交
2883 2884
        printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
        printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
2885 2886
        printf("options:");
        printf("%s ", BN_options());
2887
#ifndef OPENSSL_NO_MD2
2888
        printf("%s ", MD2_options());
2889 2890
#endif
#ifndef OPENSSL_NO_RC4
2891
        printf("%s ", RC4_options());
2892 2893
#endif
#ifndef OPENSSL_NO_DES
2894
        printf("%s ", DES_options());
2895
#endif
2896
        printf("%s ", AES_options());
2897
#ifndef OPENSSL_NO_IDEA
R
Rich Salz 已提交
2898
        printf("%s ", IDEA_options());
2899 2900
#endif
#ifndef OPENSSL_NO_BF
2901
        printf("%s ", BF_options());
2902
#endif
R
Rich Salz 已提交
2903
        printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
2904
    }
B
Bodo Möller 已提交
2905

2906 2907
    if (pr_header) {
        if (mr)
2908
            printf("+H");
2909
        else {
2910 2911 2912
            printf
                ("The 'numbers' are in 1000s of bytes per second processed.\n");
            printf("type        ");
2913
        }
2914
        for (testnum = 0; testnum < size_num; testnum++)
2915
            printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
2916
        printf("\n");
2917
    }
B
Bodo Möller 已提交
2918

2919 2920 2921 2922
    for (k = 0; k < ALGOR_NUM; k++) {
        if (!doit[k])
            continue;
        if (mr)
2923
            printf("+F:%u:%s", k, names[k]);
2924
        else
2925
            printf("%-13s", names[k]);
2926
        for (testnum = 0; testnum < size_num; testnum++) {
2927 2928
            if (results[k][testnum] > 10000 && !mr)
                printf(" %11.2fk", results[k][testnum] / 1e3);
2929
            else
2930
                printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
2931
        }
2932
        printf("\n");
2933
    }
2934
#ifndef OPENSSL_NO_RSA
2935
    testnum = 1;
2936 2937 2938
    for (k = 0; k < RSA_NUM; k++) {
        if (!rsa_doit[k])
            continue;
2939
        if (testnum && !mr) {
2940
            printf("%18ssign    verify    sign/s verify/s\n", " ");
2941
            testnum = 0;
2942 2943
        }
        if (mr)
2944 2945
            printf("+F2:%u:%u:%f:%f\n",
                   k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
2946
        else
2947
            printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2948 2949
                   rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
                   rsa_results[k][0], rsa_results[k][1]);
2950
    }
2951 2952
#endif
#ifndef OPENSSL_NO_DSA
2953
    testnum = 1;
2954 2955 2956
    for (k = 0; k < DSA_NUM; k++) {
        if (!dsa_doit[k])
            continue;
2957
        if (testnum && !mr) {
2958
            printf("%18ssign    verify    sign/s verify/s\n", " ");
2959
            testnum = 0;
2960 2961
        }
        if (mr)
2962 2963
            printf("+F3:%u:%u:%f:%f\n",
                   k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
2964
        else
2965
            printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2966 2967
                   dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
                   dsa_results[k][0], dsa_results[k][1]);
2968
    }
2969
#endif
2970
#ifndef OPENSSL_NO_EC
2971
    testnum = 1;
2972
    for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
2973 2974
        if (!ecdsa_doit[k])
            continue;
2975
        if (testnum && !mr) {
2976
            printf("%30ssign    verify    sign/s verify/s\n", " ");
2977
            testnum = 0;
2978 2979 2980
        }

        if (mr)
2981
            printf("+F4:%u:%u:%f:%f\n",
2982
                   k, test_curves[k].bits,
2983
                   ecdsa_results[k][0], ecdsa_results[k][1]);
2984
        else
2985 2986
            printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
                   test_curves[k].bits, test_curves[k].name,
2987 2988
                   1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
                   ecdsa_results[k][0], ecdsa_results[k][1]);
2989
    }
2990

2991
    testnum = 1;
2992 2993 2994
    for (k = 0; k < EC_NUM; k++) {
        if (!ecdh_doit[k])
            continue;
2995
        if (testnum && !mr) {
2996
            printf("%30sop      op/s\n", " ");
2997
            testnum = 0;
2998 2999
        }
        if (mr)
3000
            printf("+F5:%u:%u:%f:%f\n",
3001
                   k, test_curves[k].bits,
3002
                   ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
3003 3004

        else
3005 3006
            printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
                   test_curves[k].bits, test_curves[k].name,
3007
                   1.0 / ecdh_results[k][0], ecdh_results[k][0]);
3008
    }
3009
#endif
3010

3011
    ret = 0;
3012 3013 3014

 end:
    ERR_print_errors(bio_err);
3015
    for (i = 0; i < loopargs_len; i++) {
3016 3017
        OPENSSL_free(loopargs[i].buf_malloc);
        OPENSSL_free(loopargs[i].buf2_malloc);
3018

3019
#ifndef OPENSSL_NO_RSA
3020 3021
        for (k = 0; k < RSA_NUM; k++)
            RSA_free(loopargs[i].rsa_key[k]);
3022 3023
#endif
#ifndef OPENSSL_NO_DSA
3024 3025
        for (k = 0; k < DSA_NUM; k++)
            DSA_free(loopargs[i].dsa_key[k]);
3026
#endif
3027
#ifndef OPENSSL_NO_EC
3028
        for (k = 0; k < ECDSA_NUM; k++)
3029
            EC_KEY_free(loopargs[i].ecdsa[k]);
3030
        for (k = 0; k < EC_NUM; k++)
3031
            EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
3032 3033
        OPENSSL_free(loopargs[i].secret_a);
        OPENSSL_free(loopargs[i].secret_b);
3034
#endif
3035 3036
    }

3037 3038 3039
    if (async_jobs > 0) {
        for (i = 0; i < loopargs_len; i++)
            ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
3040
    }
3041

3042
    if (async_init) {
3043
        ASYNC_cleanup_thread();
3044 3045
    }
    OPENSSL_free(loopargs);
3046
    release_engine(e);
K
KaoruToda 已提交
3047
    return ret;
3048
}
3049

3050
static void print_message(const char *s, long num, int length, int tm)
3051
{
3052
#ifdef SIGALRM
3053 3054
    BIO_printf(bio_err,
               mr ? "+DT:%s:%d:%d\n"
3055
               : "Doing %s for %ds on %d size blocks: ", s, tm, length);
3056
    (void)BIO_flush(bio_err);
3057
    alarm(tm);
3058
#else
3059 3060 3061 3062
    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);
3063
#endif
3064
}
3065

3066
static void pkey_print_message(const char *str, const char *str2, long num,
3067
                               unsigned int bits, int tm)
3068
{
3069
#ifdef SIGALRM
3070 3071
    BIO_printf(bio_err,
               mr ? "+DTP:%d:%s:%s:%d\n"
3072
               : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
3073 3074
    (void)BIO_flush(bio_err);
    alarm(tm);
3075
#else
3076 3077
    BIO_printf(bio_err,
               mr ? "+DNP:%ld:%d:%s:%s\n"
3078
               : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
3079
    (void)BIO_flush(bio_err);
3080
#endif
3081
}
3082

3083 3084
static void print_result(int alg, int run_no, int count, double time_used)
{
3085 3086 3087 3088
    if (count == -1) {
        BIO_puts(bio_err, "EVP error!\n");
        exit(1);
    }
3089 3090 3091 3092 3093
    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];
}
3094

3095
#ifndef NO_FORK
3096
static char *sstrsep(char **string, const char *delim)
3097
{
3098 3099 3100 3101 3102 3103
    char isdelim[256];
    char *token = *string;

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

R
Rich Salz 已提交
3104
    memset(isdelim, 0, sizeof(isdelim));
3105 3106
    isdelim[0] = 1;

3107
    while (*delim) {
3108 3109
        isdelim[(unsigned char)(*delim)] = 1;
        delim++;
3110
    }
3111

3112
    while (!isdelim[(unsigned char)(**string)]) {
3113
        (*string)++;
3114
    }
3115

3116
    if (**string) {
3117 3118
        **string = 0;
        (*string)++;
3119
    }
3120 3121

    return token;
3122
}
3123

3124
static int do_multi(int multi, int size_num)
3125 3126 3127 3128 3129 3130
{
    int n;
    int fd[2];
    int *fds;
    static char sep[] = ":";

R
Rich Salz 已提交
3131
    fds = malloc(sizeof(*fds) * multi);
3132 3133
    for (n = 0; n < multi; ++n) {
        if (pipe(fd) == -1) {
R
Rich Salz 已提交
3134
            BIO_printf(bio_err, "pipe failure\n");
3135 3136 3137
            exit(1);
        }
        fflush(stdout);
R
Rich Salz 已提交
3138
        (void)BIO_flush(bio_err);
3139 3140 3141 3142 3143 3144 3145
        if (fork()) {
            close(fd[1]);
            fds[n] = fd[0];
        } else {
            close(fd[0]);
            close(1);
            if (dup(fd[1]) == -1) {
R
Rich Salz 已提交
3146
                BIO_printf(bio_err, "dup failed\n");
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156
                exit(1);
            }
            close(fd[1]);
            mr = 1;
            usertime = 0;
            free(fds);
            return 0;
        }
        printf("Forked child %d\n", n);
    }
B
Bodo Möller 已提交
3157

3158 3159 3160 3161 3162 3163 3164
    /* for now, assume the pipe is long enough to take all the output */
    for (n = 0; n < multi; ++n) {
        FILE *f;
        char buf[1024];
        char *p;

        f = fdopen(fds[n], "r");
R
Rich Salz 已提交
3165
        while (fgets(buf, sizeof(buf), f)) {
3166 3167 3168 3169
            p = strchr(buf, '\n');
            if (p)
                *p = '\0';
            if (buf[0] != '+') {
3170 3171 3172
                BIO_printf(bio_err,
                           "Don't understand line '%s' from child %d\n", buf,
                           n);
3173 3174 3175
                continue;
            }
            printf("Got: %s from %d\n", buf, n);
R
Rich Salz 已提交
3176
            if (strncmp(buf, "+F:", 3) == 0) {
3177 3178 3179 3180 3181 3182
                int alg;
                int j;

                p = buf + 3;
                alg = atoi(sstrsep(&p, sep));
                sstrsep(&p, sep);
3183
                for (j = 0; j < size_num; ++j)
3184
                    results[alg][j] += atof(sstrsep(&p, sep));
R
Rich Salz 已提交
3185
            } else if (strncmp(buf, "+F2:", 4) == 0) {
3186 3187 3188 3189 3190 3191 3192 3193
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3194
                rsa_results[k][0] += d;
3195 3196

                d = atof(sstrsep(&p, sep));
3197
                rsa_results[k][1] += d;
3198
            }
3199
# ifndef OPENSSL_NO_DSA
R
Rich Salz 已提交
3200
            else if (strncmp(buf, "+F3:", 4) == 0) {
3201 3202 3203 3204 3205 3206 3207 3208
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3209
                dsa_results[k][0] += d;
3210 3211

                d = atof(sstrsep(&p, sep));
3212
                dsa_results[k][1] += d;
3213
            }
3214
# endif
3215
# ifndef OPENSSL_NO_EC
R
Rich Salz 已提交
3216
            else if (strncmp(buf, "+F4:", 4) == 0) {
3217 3218 3219 3220 3221 3222 3223 3224
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3225
                ecdsa_results[k][0] += d;
3226 3227

                d = atof(sstrsep(&p, sep));
3228
                ecdsa_results[k][1] += d;
F
FdaSilvaYY 已提交
3229
            } else if (strncmp(buf, "+F5:", 4) == 0) {
3230 3231 3232 3233 3234 3235 3236 3237
                int k;
                double d;

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

                d = atof(sstrsep(&p, sep));
3238
                ecdh_results[k][0] += d;
3239
            }
3240
# endif
3241

R
Rich Salz 已提交
3242
            else if (strncmp(buf, "+H:", 3) == 0) {
3243
                ;
3244
            } else
3245 3246
                BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
                           n);
3247 3248 3249 3250 3251 3252 3253
        }

        fclose(f);
    }
    free(fds);
    return 1;
}
3254
#endif
3255

3256
static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
3257
                             const openssl_speed_sec_t *seconds)
3258
{
3259
    static const int mblengths_list[] =
3260
        { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
3261
    const int *mblengths = mblengths_list;
3262
    int j, count, keylen, num = OSSL_NELEM(mblengths_list);
3263
    const char *alg_name;
3264
    unsigned char *inp, *out, *key, no_key[32], no_iv[16];
3265
    EVP_CIPHER_CTX *ctx;
3266 3267
    double d = 0.0;

3268 3269 3270 3271 3272
    if (lengths_single) {
        mblengths = &lengths_single;
        num = 1;
    }

R
Rich Salz 已提交
3273 3274
    inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
    out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
3275
    ctx = EVP_CIPHER_CTX_new();
3276 3277 3278 3279 3280 3281 3282 3283
    EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);

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

3284
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
3285
    alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
3286 3287

    for (j = 0; j < num; j++) {
3288
        print_message(alg_name, 0, mblengths[j], seconds->sym);
3289 3290
        Time_F(START);
        for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
3291
            unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306
            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;

3307
            packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
3308 3309 3310 3311 3312 3313
                                          sizeof(mb_param), &mb_param);

            if (packlen > 0) {
                mb_param.out = out;
                mb_param.inp = inp;
                mb_param.len = len;
3314
                EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
3315 3316 3317 3318 3319 3320
                                    sizeof(mb_param), &mb_param);
            } else {
                int pad;

                RAND_bytes(out, 16);
                len += 16;
3321 3322
                aad[11] = (unsigned char)(len >> 8);
                aad[12] = (unsigned char)(len);
3323
                pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
3324
                                          EVP_AEAD_TLS1_AAD_LEN, aad);
3325
                EVP_Cipher(ctx, out, inp, len + pad);
3326 3327 3328
            }
        }
        d = Time_F(STOP);
3329
        BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
                   : "%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 已提交
3361 3362
    OPENSSL_free(inp);
    OPENSSL_free(out);
3363
    EVP_CIPHER_CTX_free(ctx);
3364
}