VirtualBox

Ignore:
Timestamp:
Mar 3, 2022 7:17:34 PM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
150325
Message:

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

Location:
trunk/src/libs/openssl-3.0.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/openssl-3.0.1

    • Property svn:mergeinfo
      •  

        old new  
        1212/vendor/openssl/1.1.1c:131722-131725
        1313/vendor/openssl/1.1.1k:145841-145843
         14/vendor/openssl/3.0.1:150323-150324
         15/vendor/openssl/current:147554-150322
  • trunk/src/libs/openssl-3.0.1/apps/speed.c

    r91772 r94082  
    11/*
    2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1010
    1111#undef SECONDS
    12 #define SECONDS                 3
    13 #define RSA_SECONDS             10
    14 #define DSA_SECONDS             10
    15 #define ECDSA_SECONDS   10
    16 #define ECDH_SECONDS    10
    17 #define EdDSA_SECONDS   10
     12#define SECONDS          3
     13#define PKEY_SECONDS    10
     14
     15#define RSA_SECONDS     PKEY_SECONDS
     16#define DSA_SECONDS     PKEY_SECONDS
     17#define ECDSA_SECONDS   PKEY_SECONDS
     18#define ECDH_SECONDS    PKEY_SECONDS
     19#define EdDSA_SECONDS   PKEY_SECONDS
     20#define SM2_SECONDS     PKEY_SECONDS
     21#define FFDH_SECONDS    PKEY_SECONDS
     22
     23/* We need to use some deprecated APIs */
     24#define OPENSSL_SUPPRESS_DEPRECATED
    1825
    1926#include <stdio.h>
     
    2835#include <openssl/evp.h>
    2936#include <openssl/objects.h>
     37#include <openssl/core_names.h>
    3038#include <openssl/async.h>
    3139#if !defined(OPENSSL_SYS_MSDOS)
    32 # include OPENSSL_UNISTD
     40# include <unistd.h>
     41#endif
     42
     43#if defined(__TANDEM)
     44# if defined(OPENSSL_TANDEM_FLOSS)
     45#  include <floss.h(floss_fork)>
     46# endif
    3347#endif
    3448
     
    3852
    3953#include <openssl/bn.h>
    40 #ifndef OPENSSL_NO_DES
    41 # include <openssl/des.h>
    42 #endif
    43 #include <openssl/aes.h>
    44 #ifndef OPENSSL_NO_CAMELLIA
    45 # include <openssl/camellia.h>
    46 #endif
    47 #ifndef OPENSSL_NO_MD2
    48 # include <openssl/md2.h>
    49 #endif
    50 #ifndef OPENSSL_NO_MDC2
    51 # include <openssl/mdc2.h>
    52 #endif
    53 #ifndef OPENSSL_NO_MD4
    54 # include <openssl/md4.h>
    55 #endif
    56 #ifndef OPENSSL_NO_MD5
    57 # include <openssl/md5.h>
    58 #endif
    59 #include <openssl/hmac.h>
    60 #include <openssl/sha.h>
    61 #ifndef OPENSSL_NO_RMD160
    62 # include <openssl/ripemd.h>
    63 #endif
    64 #ifndef OPENSSL_NO_WHIRLPOOL
    65 # include <openssl/whrlpool.h>
    66 #endif
    67 #ifndef OPENSSL_NO_RC4
    68 # include <openssl/rc4.h>
    69 #endif
    70 #ifndef OPENSSL_NO_RC5
    71 # include <openssl/rc5.h>
    72 #endif
    73 #ifndef OPENSSL_NO_RC2
    74 # include <openssl/rc2.h>
    75 #endif
    76 #ifndef OPENSSL_NO_IDEA
    77 # include <openssl/idea.h>
    78 #endif
    79 #ifndef OPENSSL_NO_SEED
    80 # include <openssl/seed.h>
    81 #endif
    82 #ifndef OPENSSL_NO_BF
    83 # include <openssl/blowfish.h>
    84 #endif
    85 #ifndef OPENSSL_NO_CAST
    86 # include <openssl/cast.h>
    87 #endif
    88 #ifndef OPENSSL_NO_RSA
    89 # include <openssl/rsa.h>
    90 # include "./testrsa.h"
     54#include <openssl/rsa.h>
     55#include "./testrsa.h"
     56#ifndef OPENSSL_NO_DH
     57# include <openssl/dh.h>
    9158#endif
    9259#include <openssl/x509.h>
    93 #ifndef OPENSSL_NO_DSA
    94 # include <openssl/dsa.h>
    95 # include "./testdsa.h"
    96 #endif
    97 #ifndef OPENSSL_NO_EC
    98 # include <openssl/ec.h>
    99 #endif
     60#include <openssl/dsa.h>
     61#include "./testdsa.h"
    10062#include <openssl/modes.h>
    10163
     
    11779#define MAX_ECDH_SIZE   256
    11880#define MISALIGN        64
     81#define MAX_FFDH_SIZE 1024
     82
     83#ifndef RSA_DEFAULT_PRIME_NUM
     84# define RSA_DEFAULT_PRIME_NUM 2
     85#endif
    11986
    12087typedef struct openssl_speed_sec_st {
     
    12592    int ecdh;
    12693    int eddsa;
     94    int sm2;
     95    int ffdh;
    12796} openssl_speed_sec_t;
    12897
    12998static volatile int run = 0;
    13099
    131 static int mr = 0;
     100static int mr = 0;  /* machine-readeable output format to merge fork results */
    132101static int usertime = 1;
    133 
    134 #ifndef OPENSSL_NO_MD2
    135 static int EVP_Digest_MD2_loop(void *args);
    136 #endif
    137 
    138 #ifndef OPENSSL_NO_MDC2
    139 static int EVP_Digest_MDC2_loop(void *args);
    140 #endif
    141 #ifndef OPENSSL_NO_MD4
    142 static int EVP_Digest_MD4_loop(void *args);
    143 #endif
    144 #ifndef OPENSSL_NO_MD5
    145 static int MD5_loop(void *args);
    146 static int HMAC_loop(void *args);
    147 #endif
    148 static int SHA1_loop(void *args);
    149 static int SHA256_loop(void *args);
    150 static int SHA512_loop(void *args);
    151 #ifndef OPENSSL_NO_WHIRLPOOL
    152 static int WHIRLPOOL_loop(void *args);
    153 #endif
    154 #ifndef OPENSSL_NO_RMD160
    155 static int EVP_Digest_RMD160_loop(void *args);
    156 #endif
    157 #ifndef OPENSSL_NO_RC4
    158 static int RC4_loop(void *args);
    159 #endif
    160 #ifndef OPENSSL_NO_DES
    161 static int DES_ncbc_encrypt_loop(void *args);
    162 static int DES_ede3_cbc_encrypt_loop(void *args);
    163 #endif
    164 static int AES_cbc_128_encrypt_loop(void *args);
    165 static int AES_cbc_192_encrypt_loop(void *args);
    166 static int AES_ige_128_encrypt_loop(void *args);
    167 static int AES_cbc_256_encrypt_loop(void *args);
    168 static int AES_ige_192_encrypt_loop(void *args);
    169 static int AES_ige_256_encrypt_loop(void *args);
    170 static int CRYPTO_gcm128_aad_loop(void *args);
    171 static int RAND_bytes_loop(void *args);
    172 static int EVP_Update_loop(void *args);
    173 static int EVP_Update_loop_ccm(void *args);
    174 static int EVP_Update_loop_aead(void *args);
    175 static int EVP_Digest_loop(void *args);
    176 #ifndef OPENSSL_NO_RSA
    177 static int RSA_sign_loop(void *args);
    178 static int RSA_verify_loop(void *args);
    179 #endif
    180 #ifndef OPENSSL_NO_DSA
    181 static int DSA_sign_loop(void *args);
    182 static int DSA_verify_loop(void *args);
    183 #endif
    184 #ifndef OPENSSL_NO_EC
    185 static int ECDSA_sign_loop(void *args);
    186 static int ECDSA_verify_loop(void *args);
    187 static int EdDSA_sign_loop(void *args);
    188 static int EdDSA_verify_loop(void *args);
    189 #endif
    190102
    191103static double Time_F(int s);
     
    201113    16, 64, 256, 1024, 8 * 1024, 16 * 1024
    202114};
     115#define SIZE_NUM         OSSL_NELEM(lengths_list)
    203116static const int *lengths = lengths_list;
    204117
     
    273186}
    274187#else
    275 static double Time_F(int s)
    276 {
    277     return app_tminterval(s, usertime);
    278 }
     188# error "SIGALRM not defined and the platform is not Windows"
    279189#endif
    280190
     
    282192                             const openssl_speed_sec_t *seconds);
    283193
    284 #define found(value, pairs, result)\
    285     opt_found(value, result, pairs, OSSL_NELEM(pairs))
    286194static int opt_found(const char *name, unsigned int *result,
    287195                     const OPT_PAIR pairs[], unsigned int nbelem)
     
    296204    return 0;
    297205}
     206#define opt_found(value, pairs, result)\
     207    opt_found(value, result, pairs, OSSL_NELEM(pairs))
    298208
    299209typedef enum OPTION_choice {
    300     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
    301     OPT_ELAPSED, OPT_EVP, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
    302     OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM,
    303     OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD
     210    OPT_COMMON,
     211    OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
     212    OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM, OPT_PROV_ENUM,
     213    OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC
    304214} OPTION_CHOICE;
    305215
    306216const OPTIONS speed_options[] = {
    307     {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
    308     {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
     217    {OPT_HELP_STR, 1, '-', "Usage: %s [options] [algorithm...]\n"},
     218
     219    OPT_SECTION("General"),
    309220    {"help", OPT_HELP, '-', "Display this summary"},
    310     {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
    311     {"decrypt", OPT_DECRYPT, '-',
    312      "Time decryption instead of encryption (only EVP)"},
    313     {"aead", OPT_AEAD, '-',
    314      "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
    315221    {"mb", OPT_MB, '-',
    316222     "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
     
    323229     "Enable async mode and start specified number of jobs"},
    324230#endif
    325     OPT_R_OPTIONS,
    326231#ifndef OPENSSL_NO_ENGINE
    327232    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
    328233#endif
     234    {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
     235
     236    OPT_SECTION("Selection"),
     237    {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
     238    {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
     239    {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
     240    {"decrypt", OPT_DECRYPT, '-',
     241     "Time decryption instead of encryption (only EVP)"},
     242    {"aead", OPT_AEAD, '-',
     243     "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
     244
     245    OPT_SECTION("Timing"),
    329246    {"elapsed", OPT_ELAPSED, '-',
    330247     "Use wall-clock time instead of CPU user time as divisor"},
    331     {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
    332248    {"seconds", OPT_SECONDS, 'p',
    333249     "Run benchmarks for specified amount of seconds"},
     
    336252    {"misalign", OPT_MISALIGN, 'p',
    337253     "Use specified offset to mis-align buffers"},
     254
     255    OPT_R_OPTIONS,
     256    OPT_PROV_OPTIONS,
     257
     258    OPT_PARAMETERS(),
     259    {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
    338260    {NULL}
    339261};
    340262
    341 #define D_MD2           0
    342 #define D_MDC2          1
    343 #define D_MD4           2
    344 #define D_MD5           3
    345 #define D_HMAC          4
    346 #define D_SHA1          5
    347 #define D_RMD160        6
    348 #define D_RC4           7
    349 #define D_CBC_DES       8
    350 #define D_EDE3_DES      9
    351 #define D_CBC_IDEA      10
    352 #define D_CBC_SEED      11
    353 #define D_CBC_RC2       12
    354 #define D_CBC_RC5       13
    355 #define D_CBC_BF        14
    356 #define D_CBC_CAST      15
    357 #define D_CBC_128_AES   16
    358 #define D_CBC_192_AES   17
    359 #define D_CBC_256_AES   18
    360 #define D_CBC_128_CML   19
    361 #define D_CBC_192_CML   20
    362 #define D_CBC_256_CML   21
    363 #define D_EVP           22
    364 #define D_SHA256        23
    365 #define D_SHA512        24
    366 #define D_WHIRLPOOL     25
    367 #define D_IGE_128_AES   26
    368 #define D_IGE_192_AES   27
    369 #define D_IGE_256_AES   28
    370 #define D_GHASH         29
    371 #define D_RAND          30
    372 /* name of algorithms to test */
    373 static const char *names[] = {
    374     "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
    375     "des cbc", "des ede3", "idea cbc", "seed cbc",
    376     "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
    377     "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
    378     "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
    379     "evp", "sha256", "sha512", "whirlpool",
    380     "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
    381     "rand"
     263enum {
     264    D_MD2, D_MDC2, D_MD4, D_MD5, D_SHA1, D_RMD160,
     265    D_SHA256, D_SHA512, D_WHIRLPOOL, D_HMAC,
     266    D_CBC_DES, D_EDE3_DES, D_RC4, D_CBC_IDEA, D_CBC_SEED,
     267    D_CBC_RC2, D_CBC_RC5, D_CBC_BF, D_CBC_CAST,
     268    D_CBC_128_AES, D_CBC_192_AES, D_CBC_256_AES,
     269    D_CBC_128_CML, D_CBC_192_CML, D_CBC_256_CML,
     270    D_EVP, D_GHASH, D_RAND, D_EVP_CMAC, ALGOR_NUM
    382271};
    383 #define ALGOR_NUM       OSSL_NELEM(names)
    384 
    385 /* list of configured algorithm (remaining) */
     272/* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
     273static const char *names[ALGOR_NUM] = {
     274    "md2", "mdc2", "md4", "md5", "sha1", "rmd160",
     275    "sha256", "sha512", "whirlpool", "hmac(md5)",
     276    "des-cbc", "des-ede3", "rc4", "idea-cbc", "seed-cbc",
     277    "rc2-cbc", "rc5-cbc", "blowfish", "cast-cbc",
     278    "aes-128-cbc", "aes-192-cbc", "aes-256-cbc",
     279    "camellia-128-cbc", "camellia-192-cbc", "camellia-256-cbc",
     280    "evp", "ghash", "rand", "cmac"
     281};
     282
     283/* list of configured algorithm (remaining), with some few alias */
    386284static const OPT_PAIR doit_choices[] = {
    387 #ifndef OPENSSL_NO_MD2
    388285    {"md2", D_MD2},
    389 #endif
    390 #ifndef OPENSSL_NO_MDC2
    391286    {"mdc2", D_MDC2},
    392 #endif
    393 #ifndef OPENSSL_NO_MD4
    394287    {"md4", D_MD4},
    395 #endif
    396 #ifndef OPENSSL_NO_MD5
    397288    {"md5", D_MD5},
    398289    {"hmac", D_HMAC},
    399 #endif
    400290    {"sha1", D_SHA1},
    401291    {"sha256", D_SHA256},
    402292    {"sha512", D_SHA512},
    403 #ifndef OPENSSL_NO_WHIRLPOOL
    404293    {"whirlpool", D_WHIRLPOOL},
    405 #endif
    406 #ifndef OPENSSL_NO_RMD160
    407294    {"ripemd", D_RMD160},
    408295    {"rmd160", D_RMD160},
    409296    {"ripemd160", D_RMD160},
    410 #endif
    411 #ifndef OPENSSL_NO_RC4
    412297    {"rc4", D_RC4},
    413 #endif
    414 #ifndef OPENSSL_NO_DES
    415298    {"des-cbc", D_CBC_DES},
    416299    {"des-ede3", D_EDE3_DES},
    417 #endif
    418300    {"aes-128-cbc", D_CBC_128_AES},
    419301    {"aes-192-cbc", D_CBC_192_AES},
    420302    {"aes-256-cbc", D_CBC_256_AES},
    421     {"aes-128-ige", D_IGE_128_AES},
    422     {"aes-192-ige", D_IGE_192_AES},
    423     {"aes-256-ige", D_IGE_256_AES},
    424 #ifndef OPENSSL_NO_RC2
     303    {"camellia-128-cbc", D_CBC_128_CML},
     304    {"camellia-192-cbc", D_CBC_192_CML},
     305    {"camellia-256-cbc", D_CBC_256_CML},
    425306    {"rc2-cbc", D_CBC_RC2},
    426307    {"rc2", D_CBC_RC2},
    427 #endif
    428 #ifndef OPENSSL_NO_RC5
    429308    {"rc5-cbc", D_CBC_RC5},
    430309    {"rc5", D_CBC_RC5},
    431 #endif
    432 #ifndef OPENSSL_NO_IDEA
    433310    {"idea-cbc", D_CBC_IDEA},
    434311    {"idea", D_CBC_IDEA},
    435 #endif
    436 #ifndef OPENSSL_NO_SEED
    437312    {"seed-cbc", D_CBC_SEED},
    438313    {"seed", D_CBC_SEED},
    439 #endif
    440 #ifndef OPENSSL_NO_BF
    441314    {"bf-cbc", D_CBC_BF},
    442315    {"blowfish", D_CBC_BF},
    443316    {"bf", D_CBC_BF},
    444 #endif
    445 #ifndef OPENSSL_NO_CAST
    446317    {"cast-cbc", D_CBC_CAST},
    447318    {"cast", D_CBC_CAST},
    448319    {"cast5", D_CBC_CAST},
    449 #endif
    450320    {"ghash", D_GHASH},
    451321    {"rand", D_RAND}
    452322};
    453323
    454 static double results[ALGOR_NUM][OSSL_NELEM(lengths_list)];
    455 
    456 #ifndef OPENSSL_NO_DSA
    457 # define R_DSA_512       0
    458 # define R_DSA_1024      1
    459 # define R_DSA_2048      2
    460 static const OPT_PAIR dsa_choices[] = {
     324static double results[ALGOR_NUM][SIZE_NUM];
     325
     326enum { R_DSA_512, R_DSA_1024, R_DSA_2048, DSA_NUM };
     327static const OPT_PAIR dsa_choices[DSA_NUM] = {
    461328    {"dsa512", R_DSA_512},
    462329    {"dsa1024", R_DSA_1024},
    463330    {"dsa2048", R_DSA_2048}
    464331};
    465 # define DSA_NUM         OSSL_NELEM(dsa_choices)
    466 
    467332static double dsa_results[DSA_NUM][2];  /* 2 ops: sign then verify */
    468 #endif  /* OPENSSL_NO_DSA */
    469 
    470 #define R_RSA_512       0
    471 #define R_RSA_1024      1
    472 #define R_RSA_2048      2
    473 #define R_RSA_3072      3
    474 #define R_RSA_4096      4
    475 #define R_RSA_7680      5
    476 #define R_RSA_15360     6
    477 #ifndef OPENSSL_NO_RSA
    478 static const OPT_PAIR rsa_choices[] = {
     333
     334enum {
     335    R_RSA_512, R_RSA_1024, R_RSA_2048, R_RSA_3072, R_RSA_4096, R_RSA_7680,
     336    R_RSA_15360, RSA_NUM
     337};
     338static const OPT_PAIR rsa_choices[RSA_NUM] = {
    479339    {"rsa512", R_RSA_512},
    480340    {"rsa1024", R_RSA_1024},
     
    485345    {"rsa15360", R_RSA_15360}
    486346};
    487 # define RSA_NUM OSSL_NELEM(rsa_choices)
    488347
    489348static double rsa_results[RSA_NUM][2];  /* 2 ops: sign then verify */
    490 #endif /* OPENSSL_NO_RSA */
    491 
    492 enum {
    493     R_EC_P160,
    494     R_EC_P192,
    495     R_EC_P224,
    496     R_EC_P256,
    497     R_EC_P384,
    498     R_EC_P521,
     349
     350#ifndef OPENSSL_NO_DH
     351enum ff_params_t {
     352    R_FFDH_2048, R_FFDH_3072, R_FFDH_4096, R_FFDH_6144, R_FFDH_8192, FFDH_NUM
     353};
     354
     355static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
     356    {"ffdh2048", R_FFDH_2048},
     357    {"ffdh3072", R_FFDH_3072},
     358    {"ffdh4096", R_FFDH_4096},
     359    {"ffdh6144", R_FFDH_6144},
     360    {"ffdh8192", R_FFDH_8192},
     361};
     362
     363static double ffdh_results[FFDH_NUM][1];  /* 1 op: derivation */
     364#endif /* OPENSSL_NO_DH */
     365
     366enum ec_curves_t {
     367    R_EC_P160, R_EC_P192, R_EC_P224, R_EC_P256, R_EC_P384, R_EC_P521,
    499368#ifndef OPENSSL_NO_EC2M
    500     R_EC_K163,
    501     R_EC_K233,
    502     R_EC_K283,
    503     R_EC_K409,
    504     R_EC_K571,
    505     R_EC_B163,
    506     R_EC_B233,
    507     R_EC_B283,
    508     R_EC_B409,
    509     R_EC_B571,
    510 #endif
    511     R_EC_BRP256R1,
    512     R_EC_BRP256T1,
    513     R_EC_BRP384R1,
    514     R_EC_BRP384T1,
    515     R_EC_BRP512R1,
    516     R_EC_BRP512T1,
    517     R_EC_X25519,
    518     R_EC_X448
     369    R_EC_K163, R_EC_K233, R_EC_K283, R_EC_K409, R_EC_K571,
     370    R_EC_B163, R_EC_B233, R_EC_B283, R_EC_B409, R_EC_B571,
     371#endif
     372    R_EC_BRP256R1, R_EC_BRP256T1, R_EC_BRP384R1, R_EC_BRP384T1,
     373    R_EC_BRP512R1, R_EC_BRP512T1, ECDSA_NUM
    519374};
    520 
    521 #ifndef OPENSSL_NO_EC
    522 static OPT_PAIR ecdsa_choices[] = {
     375/* list of ecdsa curves */
     376static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
    523377    {"ecdsap160", R_EC_P160},
    524378    {"ecdsap192", R_EC_P192},
     
    527381    {"ecdsap384", R_EC_P384},
    528382    {"ecdsap521", R_EC_P521},
    529 # ifndef OPENSSL_NO_EC2M
     383#ifndef OPENSSL_NO_EC2M
    530384    {"ecdsak163", R_EC_K163},
    531385    {"ecdsak233", R_EC_K233},
     
    538392    {"ecdsab409", R_EC_B409},
    539393    {"ecdsab571", R_EC_B571},
    540 # endif
     394#endif
    541395    {"ecdsabrp256r1", R_EC_BRP256R1},
    542396    {"ecdsabrp256t1", R_EC_BRP256T1},
     
    546400    {"ecdsabrp512t1", R_EC_BRP512T1}
    547401};
    548 # define ECDSA_NUM       OSSL_NELEM(ecdsa_choices)
    549 
    550 static double ecdsa_results[ECDSA_NUM][2];    /* 2 ops: sign then verify */
    551 
    552 static const OPT_PAIR ecdh_choices[] = {
     402enum { R_EC_X25519 = ECDSA_NUM, R_EC_X448, EC_NUM };
     403/* list of ecdh curves, extension of |ecdsa_choices| list above */
     404static const OPT_PAIR ecdh_choices[EC_NUM] = {
    553405    {"ecdhp160", R_EC_P160},
    554406    {"ecdhp192", R_EC_P192},
     
    557409    {"ecdhp384", R_EC_P384},
    558410    {"ecdhp521", R_EC_P521},
    559 # ifndef OPENSSL_NO_EC2M
     411#ifndef OPENSSL_NO_EC2M
    560412    {"ecdhk163", R_EC_K163},
    561413    {"ecdhk233", R_EC_K233},
     
    568420    {"ecdhb409", R_EC_B409},
    569421    {"ecdhb571", R_EC_B571},
    570 # endif
     422#endif
    571423    {"ecdhbrp256r1", R_EC_BRP256R1},
    572424    {"ecdhbrp256t1", R_EC_BRP256T1},
     
    578430    {"ecdhx448", R_EC_X448}
    579431};
    580 # define EC_NUM       OSSL_NELEM(ecdh_choices)
    581 
    582 static double ecdh_results[EC_NUM][1];  /* 1 op: derivation */
    583 
    584 #define R_EC_Ed25519    0
    585 #define R_EC_Ed448      1
    586 static OPT_PAIR eddsa_choices[] = {
     432
     433static double ecdh_results[EC_NUM][1];      /* 1 op: derivation */
     434static double ecdsa_results[ECDSA_NUM][2];  /* 2 ops: sign then verify */
     435
     436enum { R_EC_Ed25519, R_EC_Ed448, EdDSA_NUM };
     437static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
    587438    {"ed25519", R_EC_Ed25519},
    588439    {"ed448", R_EC_Ed448}
     440
    589441};
    590 # define EdDSA_NUM       OSSL_NELEM(eddsa_choices)
    591 
    592442static double eddsa_results[EdDSA_NUM][2];    /* 2 ops: sign then verify */
    593 #endif /* OPENSSL_NO_EC */
    594 
    595 #ifndef SIGALRM
    596 # define COND(d) (count < (d))
    597 # define COUNT(d) (d)
    598 #else
    599 # define COND(unused_cond) (run && count<0x7fffffff)
    600 # define COUNT(d) (count)
    601 #endif                          /* SIGALRM */
     443
     444#ifndef OPENSSL_NO_SM2
     445enum { R_EC_CURVESM2, SM2_NUM };
     446static const OPT_PAIR sm2_choices[SM2_NUM] = {
     447    {"curveSM2", R_EC_CURVESM2}
     448};
     449# define SM2_ID        "TLSv1.3+GM+Cipher+Suite"
     450# define SM2_ID_LEN    sizeof("TLSv1.3+GM+Cipher+Suite") - 1
     451static double sm2_results[SM2_NUM][2];    /* 2 ops: sign then verify */
     452#endif /* OPENSSL_NO_SM2 */
     453
     454#define COND(unused_cond) (run && count < 0x7fffffff)
     455#define COUNT(d) (count)
    602456
    603457typedef struct loopargs_st {
     
    609463    unsigned char *buf2_malloc;
    610464    unsigned char *key;
    611     unsigned int siglen;
     465    size_t buflen;
    612466    size_t sigsize;
    613 #ifndef OPENSSL_NO_RSA
    614     RSA *rsa_key[RSA_NUM];
    615 #endif
    616 #ifndef OPENSSL_NO_DSA
    617     DSA *dsa_key[DSA_NUM];
    618 #endif
    619 #ifndef OPENSSL_NO_EC
    620     EC_KEY *ecdsa[ECDSA_NUM];
     467    EVP_PKEY_CTX *rsa_sign_ctx[RSA_NUM];
     468    EVP_PKEY_CTX *rsa_verify_ctx[RSA_NUM];
     469    EVP_PKEY_CTX *dsa_sign_ctx[DSA_NUM];
     470    EVP_PKEY_CTX *dsa_verify_ctx[DSA_NUM];
     471    EVP_PKEY_CTX *ecdsa_sign_ctx[ECDSA_NUM];
     472    EVP_PKEY_CTX *ecdsa_verify_ctx[ECDSA_NUM];
    621473    EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
    622474    EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
     475    EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
     476#ifndef OPENSSL_NO_SM2
     477    EVP_MD_CTX *sm2_ctx[SM2_NUM];
     478    EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
     479    EVP_PKEY *sm2_pkey[SM2_NUM];
     480#endif
    623481    unsigned char *secret_a;
    624482    unsigned char *secret_b;
    625483    size_t outlen[EC_NUM];
     484#ifndef OPENSSL_NO_DH
     485    EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
     486    unsigned char *secret_ff_a;
     487    unsigned char *secret_ff_b;
    626488#endif
    627489    EVP_CIPHER_CTX *ctx;
    628     HMAC_CTX *hctx;
    629     GCM128_CONTEXT *gcm_ctx;
     490    EVP_MAC_CTX *mctx;
    630491} loopargs_t;
    631492static int run_benchmark(int async_jobs, int (*loop_function) (void *),
     
    635496
    636497/* Nb of iterations to do per algorithm and key-size */
    637 static long c[ALGOR_NUM][OSSL_NELEM(lengths_list)];
    638 
    639 #ifndef OPENSSL_NO_MD2
    640 static int EVP_Digest_MD2_loop(void *args)
     498static long c[ALGOR_NUM][SIZE_NUM];
     499
     500static char *evp_mac_mdname = "md5";
     501static char *evp_hmac_name = NULL;
     502static const char *evp_md_name = NULL;
     503static char *evp_mac_ciphername = "aes-128-cbc";
     504static char *evp_cmac_name = NULL;
     505
     506static int have_md(const char *name)
     507{
     508    int ret = 0;
     509    EVP_MD *md = NULL;
     510
     511    if (opt_md_silent(name, &md)) {
     512        EVP_MD_CTX *ctx = EVP_MD_CTX_new();
     513
     514        if (ctx != NULL && EVP_DigestInit(ctx, md) > 0)
     515            ret = 1;
     516        EVP_MD_CTX_free(ctx);
     517        EVP_MD_free(md);
     518    }
     519    return ret;
     520}
     521
     522static int have_cipher(const char *name)
     523{
     524    int ret = 0;
     525    EVP_CIPHER *cipher = NULL;
     526
     527    if (opt_cipher_silent(name, &cipher)) {
     528        EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
     529
     530        if (ctx != NULL
     531            && EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) > 0)
     532            ret = 1;
     533        EVP_CIPHER_CTX_free(ctx);
     534        EVP_CIPHER_free(cipher);
     535    }
     536    return ret;
     537}
     538
     539static int EVP_Digest_loop(const char *mdname, int algindex, void *args)
    641540{
    642541    loopargs_t *tempargs = *(loopargs_t **) args;
    643542    unsigned char *buf = tempargs->buf;
    644     unsigned char md2[MD2_DIGEST_LENGTH];
     543    unsigned char digest[EVP_MAX_MD_SIZE];
    645544    int count;
    646 
    647     for (count = 0; COND(c[D_MD2][testnum]); count++) {
    648         if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
    649                         NULL))
    650             return -1;
    651     }
     545    EVP_MD *md = NULL;
     546
     547    if (!opt_md_silent(mdname, &md))
     548        return -1;
     549    for (count = 0; COND(c[algindex][testnum]); count++) {
     550        if (!EVP_Digest(buf, (size_t)lengths[testnum], digest, NULL, md,
     551                        NULL)) {
     552            count = -1;
     553            break;
     554        }
     555    }
     556    EVP_MD_free(md);
    652557    return count;
    653558}
    654 #endif
    655 
    656 #ifndef OPENSSL_NO_MDC2
     559
     560static int EVP_Digest_md_loop(void *args)
     561{
     562    return EVP_Digest_loop(evp_md_name, D_EVP, args);
     563}
     564
     565static int EVP_Digest_MD2_loop(void *args)
     566{
     567    return EVP_Digest_loop("md2", D_MD2, args);
     568}
     569
    657570static int EVP_Digest_MDC2_loop(void *args)
     571{
     572    return EVP_Digest_loop("mdc2", D_MDC2, args);
     573}
     574
     575static int EVP_Digest_MD4_loop(void *args)
     576{
     577    return EVP_Digest_loop("md4", D_MD4, args);
     578}
     579
     580static int MD5_loop(void *args)
     581{
     582    return EVP_Digest_loop("md5", D_MD5, args);
     583}
     584
     585static int EVP_MAC_loop(int algindex, void *args)
    658586{
    659587    loopargs_t *tempargs = *(loopargs_t **) args;
    660588    unsigned char *buf = tempargs->buf;
    661     unsigned char mdc2[MDC2_DIGEST_LENGTH];
     589    EVP_MAC_CTX *mctx = tempargs->mctx;
     590    unsigned char mac[EVP_MAX_MD_SIZE];
    662591    int count;
    663592
    664     for (count = 0; COND(c[D_MDC2][testnum]); count++) {
    665         if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
    666                         NULL))
     593    for (count = 0; COND(c[algindex][testnum]); count++) {
     594        size_t outl;
     595
     596        if (!EVP_MAC_init(mctx, NULL, 0, NULL)
     597            || !EVP_MAC_update(mctx, buf, lengths[testnum])
     598            || !EVP_MAC_final(mctx, mac, &outl, sizeof(mac)))
    667599            return -1;
    668600    }
    669601    return count;
    670602}
    671 #endif
    672 
    673 #ifndef OPENSSL_NO_MD4
    674 static int EVP_Digest_MD4_loop(void *args)
    675 {
    676     loopargs_t *tempargs = *(loopargs_t **) args;
    677     unsigned char *buf = tempargs->buf;
    678     unsigned char md4[MD4_DIGEST_LENGTH];
    679     int count;
    680 
    681     for (count = 0; COND(c[D_MD4][testnum]); count++) {
    682         if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
    683                         NULL))
    684             return -1;
    685     }
    686     return count;
    687 }
    688 #endif
    689 
    690 #ifndef OPENSSL_NO_MD5
    691 static int MD5_loop(void *args)
    692 {
    693     loopargs_t *tempargs = *(loopargs_t **) args;
    694     unsigned char *buf = tempargs->buf;
    695     unsigned char md5[MD5_DIGEST_LENGTH];
    696     int count;
    697     for (count = 0; COND(c[D_MD5][testnum]); count++)
    698         MD5(buf, lengths[testnum], md5);
    699     return count;
    700 }
    701603
    702604static int HMAC_loop(void *args)
    703605{
    704     loopargs_t *tempargs = *(loopargs_t **) args;
    705     unsigned char *buf = tempargs->buf;
    706     HMAC_CTX *hctx = tempargs->hctx;
    707     unsigned char hmac[MD5_DIGEST_LENGTH];
    708     int count;
    709 
    710     for (count = 0; COND(c[D_HMAC][testnum]); count++) {
    711         HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
    712         HMAC_Update(hctx, buf, lengths[testnum]);
    713         HMAC_Final(hctx, hmac, NULL);
    714     }
    715     return count;
    716 }
    717 #endif
     606    return EVP_MAC_loop(D_HMAC, args);
     607}
     608
     609static int CMAC_loop(void *args)
     610{
     611    return EVP_MAC_loop(D_EVP_CMAC, args);
     612}
    718613
    719614static int SHA1_loop(void *args)
    720615{
    721     loopargs_t *tempargs = *(loopargs_t **) args;
    722     unsigned char *buf = tempargs->buf;
    723     unsigned char sha[SHA_DIGEST_LENGTH];
    724     int count;
    725     for (count = 0; COND(c[D_SHA1][testnum]); count++)
    726         SHA1(buf, lengths[testnum], sha);
    727     return count;
     616    return EVP_Digest_loop("sha1", D_SHA1, args);
    728617}
    729618
    730619static int SHA256_loop(void *args)
    731620{
    732     loopargs_t *tempargs = *(loopargs_t **) args;
    733     unsigned char *buf = tempargs->buf;
    734     unsigned char sha256[SHA256_DIGEST_LENGTH];
    735     int count;
    736     for (count = 0; COND(c[D_SHA256][testnum]); count++)
    737         SHA256(buf, lengths[testnum], sha256);
    738     return count;
     621    return EVP_Digest_loop("sha256", D_SHA256, args);
    739622}
    740623
    741624static int SHA512_loop(void *args)
    742625{
    743     loopargs_t *tempargs = *(loopargs_t **) args;
    744     unsigned char *buf = tempargs->buf;
    745     unsigned char sha512[SHA512_DIGEST_LENGTH];
    746     int count;
    747     for (count = 0; COND(c[D_SHA512][testnum]); count++)
    748         SHA512(buf, lengths[testnum], sha512);
    749     return count;
    750 }
    751 
    752 #ifndef OPENSSL_NO_WHIRLPOOL
     626    return EVP_Digest_loop("sha512", D_SHA512, args);
     627}
     628
    753629static int WHIRLPOOL_loop(void *args)
    754630{
    755     loopargs_t *tempargs = *(loopargs_t **) args;
    756     unsigned char *buf = tempargs->buf;
    757     unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
    758     int count;
    759     for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
    760         WHIRLPOOL(buf, lengths[testnum], whirlpool);
    761     return count;
    762 }
    763 #endif
    764 
    765 #ifndef OPENSSL_NO_RMD160
     631    return EVP_Digest_loop("whirlpool", D_WHIRLPOOL, args);
     632}
     633
    766634static int EVP_Digest_RMD160_loop(void *args)
    767635{
    768     loopargs_t *tempargs = *(loopargs_t **) args;
    769     unsigned char *buf = tempargs->buf;
    770     unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
    771     int count;
    772     for (count = 0; COND(c[D_RMD160][testnum]); count++) {
    773         if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
    774                         NULL, EVP_ripemd160(), NULL))
    775             return -1;
    776     }
    777     return count;
    778 }
    779 #endif
    780 
    781 #ifndef OPENSSL_NO_RC4
    782 static RC4_KEY rc4_ks;
    783 static int RC4_loop(void *args)
     636    return EVP_Digest_loop("ripemd160", D_RMD160, args);
     637}
     638
     639static int algindex;
     640
     641static int EVP_Cipher_loop(void *args)
    784642{
    785643    loopargs_t *tempargs = *(loopargs_t **) args;
    786644    unsigned char *buf = tempargs->buf;
    787645    int count;
    788     for (count = 0; COND(c[D_RC4][testnum]); count++)
    789         RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
     646
     647    if (tempargs->ctx == NULL)
     648        return -1;
     649    for (count = 0; COND(c[algindex][testnum]); count++)
     650        if (EVP_Cipher(tempargs->ctx, buf, buf, (size_t)lengths[testnum]) <= 0)
     651            return -1;
    790652    return count;
    791653}
    792 #endif
    793 
    794 #ifndef OPENSSL_NO_DES
    795 static unsigned char DES_iv[8];
    796 static DES_key_schedule sch;
    797 static DES_key_schedule sch2;
    798 static DES_key_schedule sch3;
    799 static int DES_ncbc_encrypt_loop(void *args)
     654
     655static int GHASH_loop(void *args)
     656{
     657    loopargs_t *tempargs = *(loopargs_t **) args;
     658    unsigned char *buf = tempargs->buf;
     659    EVP_MAC_CTX *mctx = tempargs->mctx;
     660    int count;
     661
     662    /* just do the update in the loop to be comparable with 1.1.1 */
     663    for (count = 0; COND(c[D_GHASH][testnum]); count++) {
     664        if (!EVP_MAC_update(mctx, buf, lengths[testnum]))
     665            return -1;
     666    }
     667    return count;
     668}
     669
     670#define MAX_BLOCK_SIZE 128
     671
     672static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
     673
     674static EVP_CIPHER_CTX *init_evp_cipher_ctx(const char *ciphername,
     675                                           const unsigned char *key,
     676                                           int keylen)
     677{
     678    EVP_CIPHER_CTX *ctx = NULL;
     679    EVP_CIPHER *cipher = NULL;
     680
     681    if (!opt_cipher_silent(ciphername, &cipher))
     682        return NULL;
     683
     684    if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
     685        goto end;
     686
     687    if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1)) {
     688        EVP_CIPHER_CTX_free(ctx);
     689        ctx = NULL;
     690        goto end;
     691    }
     692
     693    if (!EVP_CIPHER_CTX_set_key_length(ctx, keylen)) {
     694        EVP_CIPHER_CTX_free(ctx);
     695        ctx = NULL;
     696        goto end;
     697    }
     698
     699    if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1)) {
     700        EVP_CIPHER_CTX_free(ctx);
     701        ctx = NULL;
     702        goto end;
     703    }
     704
     705end:
     706    EVP_CIPHER_free(cipher);
     707    return ctx;
     708}
     709
     710static int RAND_bytes_loop(void *args)
    800711{
    801712    loopargs_t *tempargs = *(loopargs_t **) args;
    802713    unsigned char *buf = tempargs->buf;
    803714    int count;
    804     for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
    805         DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch,
    806                          &DES_iv, DES_ENCRYPT);
    807     return count;
    808 }
    809 
    810 static int DES_ede3_cbc_encrypt_loop(void *args)
    811 {
    812     loopargs_t *tempargs = *(loopargs_t **) args;
    813     unsigned char *buf = tempargs->buf;
    814     int count;
    815     for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
    816         DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
    817                              &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT);
    818     return count;
    819 }
    820 #endif
    821 
    822 #define MAX_BLOCK_SIZE 128
    823 
    824 static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
    825 static AES_KEY aes_ks1, aes_ks2, aes_ks3;
    826 static int AES_cbc_128_encrypt_loop(void *args)
    827 {
    828     loopargs_t *tempargs = *(loopargs_t **) args;
    829     unsigned char *buf = tempargs->buf;
    830     int count;
    831     for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
    832         AES_cbc_encrypt(buf, buf,
    833                         (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
    834     return count;
    835 }
    836 
    837 static int AES_cbc_192_encrypt_loop(void *args)
    838 {
    839     loopargs_t *tempargs = *(loopargs_t **) args;
    840     unsigned char *buf = tempargs->buf;
    841     int count;
    842     for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
    843         AES_cbc_encrypt(buf, buf,
    844                         (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
    845     return count;
    846 }
    847 
    848 static int AES_cbc_256_encrypt_loop(void *args)
    849 {
    850     loopargs_t *tempargs = *(loopargs_t **) args;
    851     unsigned char *buf = tempargs->buf;
    852     int count;
    853     for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
    854         AES_cbc_encrypt(buf, buf,
    855                         (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
    856     return count;
    857 }
    858 
    859 static int AES_ige_128_encrypt_loop(void *args)
    860 {
    861     loopargs_t *tempargs = *(loopargs_t **) args;
    862     unsigned char *buf = tempargs->buf;
    863     unsigned char *buf2 = tempargs->buf2;
    864     int count;
    865     for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
    866         AES_ige_encrypt(buf, buf2,
    867                         (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
    868     return count;
    869 }
    870 
    871 static int AES_ige_192_encrypt_loop(void *args)
    872 {
    873     loopargs_t *tempargs = *(loopargs_t **) args;
    874     unsigned char *buf = tempargs->buf;
    875     unsigned char *buf2 = tempargs->buf2;
    876     int count;
    877     for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
    878         AES_ige_encrypt(buf, buf2,
    879                         (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
    880     return count;
    881 }
    882 
    883 static int AES_ige_256_encrypt_loop(void *args)
    884 {
    885     loopargs_t *tempargs = *(loopargs_t **) args;
    886     unsigned char *buf = tempargs->buf;
    887     unsigned char *buf2 = tempargs->buf2;
    888     int count;
    889     for (count = 0; COND(c[D_IGE_256_AES][testnum]); count++)
    890         AES_ige_encrypt(buf, buf2,
    891                         (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
    892     return count;
    893 }
    894 
    895 static int CRYPTO_gcm128_aad_loop(void *args)
    896 {
    897     loopargs_t *tempargs = *(loopargs_t **) args;
    898     unsigned char *buf = tempargs->buf;
    899     GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
    900     int count;
    901     for (count = 0; COND(c[D_GHASH][testnum]); count++)
    902         CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
    903     return count;
    904 }
    905 
    906 static int RAND_bytes_loop(void *args)
    907 {
    908     loopargs_t *tempargs = *(loopargs_t **) args;
    909     unsigned char *buf = tempargs->buf;
    910     int count;
    911715
    912716    for (count = 0; COND(c[D_RAND][testnum]); count++)
     
    915719}
    916720
    917 static long save_count = 0;
    918721static int decrypt = 0;
    919722static int EVP_Update_loop(void *args)
     
    923726    EVP_CIPHER_CTX *ctx = tempargs->ctx;
    924727    int outl, count, rc;
    925 #ifndef SIGALRM
    926     int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
    927 #endif
     728
    928729    if (decrypt) {
    929         for (count = 0; COND(nb_iter); count++) {
     730        for (count = 0; COND(c[D_EVP][testnum]); count++) {
    930731            rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    931732            if (rc != 1) {
     
    935736        }
    936737    } else {
    937         for (count = 0; COND(nb_iter); count++) {
     738        for (count = 0; COND(c[D_EVP][testnum]); count++) {
    938739            rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    939740            if (rc != 1) {
     
    962763    int outl, count;
    963764    unsigned char tag[12];
    964 #ifndef SIGALRM
    965     int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
    966 #endif
     765
    967766    if (decrypt) {
    968         for (count = 0; COND(nb_iter); count++) {
    969             EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
     767        for (count = 0; COND(c[D_EVP][testnum]); count++) {
     768            (void)EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag),
     769                                      tag);
    970770            /* reset iv */
    971             EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
     771            (void)EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
    972772            /* counter is reset on every update */
    973             EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
     773            (void)EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    974774        }
    975775    } else {
    976         for (count = 0; COND(nb_iter); count++) {
     776        for (count = 0; COND(c[D_EVP][testnum]); count++) {
    977777            /* restore iv length field */
    978             EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
     778            (void)EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
    979779            /* counter is reset on every update */
    980             EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
     780            (void)EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    981781        }
    982782    }
    983783    if (decrypt)
    984         EVP_DecryptFinal_ex(ctx, buf, &outl);
     784        (void)EVP_DecryptFinal_ex(ctx, buf, &outl);
    985785    else
    986         EVP_EncryptFinal_ex(ctx, buf, &outl);
     786        (void)EVP_EncryptFinal_ex(ctx, buf, &outl);
    987787    return count;
    988788}
     
    1001801    unsigned char aad[13] = { 0xcc };
    1002802    unsigned char faketag[16] = { 0xcc };
    1003 #ifndef SIGALRM
    1004     int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
    1005 #endif
     803
    1006804    if (decrypt) {
    1007         for (count = 0; COND(nb_iter); count++) {
    1008             EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
    1009             EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
    1010                                 sizeof(faketag), faketag);
    1011             EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
    1012             EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    1013             EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
     805        for (count = 0; COND(c[D_EVP][testnum]); count++) {
     806            (void)EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
     807            (void)EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
     808                                      sizeof(faketag), faketag);
     809            (void)EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
     810            (void)EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
     811            (void)EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
    1014812        }
    1015813    } else {
    1016         for (count = 0; COND(nb_iter); count++) {
    1017             EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
    1018             EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
    1019             EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    1020             EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
     814        for (count = 0; COND(c[D_EVP][testnum]); count++) {
     815            (void)EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
     816            (void)EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
     817            (void)EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
     818            (void)EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
    1021819        }
    1022820    }
     
    1024822}
    1025823
    1026 static const EVP_MD *evp_md = NULL;
    1027 static int EVP_Digest_loop(void *args)
    1028 {
    1029     loopargs_t *tempargs = *(loopargs_t **) args;
    1030     unsigned char *buf = tempargs->buf;
    1031     unsigned char md[EVP_MAX_MD_SIZE];
    1032     int count;
    1033 #ifndef SIGALRM
    1034     int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
    1035 #endif
    1036 
    1037     for (count = 0; COND(nb_iter); count++) {
    1038         if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
    1039             return -1;
    1040     }
    1041     return count;
    1042 }
    1043 
    1044 #ifndef OPENSSL_NO_RSA
    1045824static long rsa_c[RSA_NUM][2];  /* # RSA iteration test */
    1046825
     
    1050829    unsigned char *buf = tempargs->buf;
    1051830    unsigned char *buf2 = tempargs->buf2;
    1052     unsigned int *rsa_num = &tempargs->siglen;
    1053     RSA **rsa_key = tempargs->rsa_key;
     831    size_t *rsa_num = &tempargs->sigsize;
     832    EVP_PKEY_CTX **rsa_sign_ctx = tempargs->rsa_sign_ctx;
    1054833    int ret, count;
     834
    1055835    for (count = 0; COND(rsa_c[testnum][0]); count++) {
    1056         ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
    1057         if (ret == 0) {
     836        *rsa_num = tempargs->buflen;
     837        ret = EVP_PKEY_sign(rsa_sign_ctx[testnum], buf2, rsa_num, buf, 36);
     838        if (ret <= 0) {
    1058839            BIO_printf(bio_err, "RSA sign failure\n");
    1059840            ERR_print_errors(bio_err);
     
    1070851    unsigned char *buf = tempargs->buf;
    1071852    unsigned char *buf2 = tempargs->buf2;
    1072     unsigned int rsa_num = tempargs->siglen;
    1073     RSA **rsa_key = tempargs->rsa_key;
     853    size_t rsa_num = tempargs->sigsize;
     854    EVP_PKEY_CTX **rsa_verify_ctx = tempargs->rsa_verify_ctx;
    1074855    int ret, count;
     856
    1075857    for (count = 0; COND(rsa_c[testnum][1]); count++) {
    1076         ret =
    1077             RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
     858        ret = EVP_PKEY_verify(rsa_verify_ctx[testnum], buf2, rsa_num, buf, 36);
    1078859        if (ret <= 0) {
    1079860            BIO_printf(bio_err, "RSA verify failure\n");
     
    1085866    return count;
    1086867}
    1087 #endif
    1088 
    1089 #ifndef OPENSSL_NO_DSA
     868
     869#ifndef OPENSSL_NO_DH
     870static long ffdh_c[FFDH_NUM][1];
     871
     872static int FFDH_derive_key_loop(void *args)
     873{
     874    loopargs_t *tempargs = *(loopargs_t **) args;
     875    EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
     876    unsigned char *derived_secret = tempargs->secret_ff_a;
     877    size_t outlen = MAX_FFDH_SIZE;
     878    int count;
     879
     880    for (count = 0; COND(ffdh_c[testnum][0]); count++)
     881        EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
     882    return count;
     883}
     884#endif /* OPENSSL_NO_DH */
     885
    1090886static long dsa_c[DSA_NUM][2];
    1091887static int DSA_sign_loop(void *args)
     
    1094890    unsigned char *buf = tempargs->buf;
    1095891    unsigned char *buf2 = tempargs->buf2;
    1096     DSA **dsa_key = tempargs->dsa_key;
    1097     unsigned int *siglen = &tempargs->siglen;
     892    size_t *dsa_num = &tempargs->sigsize;
     893    EVP_PKEY_CTX **dsa_sign_ctx = tempargs->dsa_sign_ctx;
    1098894    int ret, count;
     895
    1099896    for (count = 0; COND(dsa_c[testnum][0]); count++) {
    1100         ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
    1101         if (ret == 0) {
     897        *dsa_num = tempargs->buflen;
     898        ret = EVP_PKEY_sign(dsa_sign_ctx[testnum], buf2, dsa_num, buf, 20);
     899        if (ret <= 0) {
    1102900            BIO_printf(bio_err, "DSA sign failure\n");
    1103901            ERR_print_errors(bio_err);
     
    1114912    unsigned char *buf = tempargs->buf;
    1115913    unsigned char *buf2 = tempargs->buf2;
    1116     DSA **dsa_key = tempargs->dsa_key;
    1117     unsigned int siglen = tempargs->siglen;
     914    size_t dsa_num = tempargs->sigsize;
     915    EVP_PKEY_CTX **dsa_verify_ctx = tempargs->dsa_verify_ctx;
    1118916    int ret, count;
     917
    1119918    for (count = 0; COND(dsa_c[testnum][1]); count++) {
    1120         ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
     919        ret = EVP_PKEY_verify(dsa_verify_ctx[testnum], buf2, dsa_num, buf, 20);
    1121920        if (ret <= 0) {
    1122921            BIO_printf(bio_err, "DSA verify failure\n");
     
    1128927    return count;
    1129928}
    1130 #endif
    1131 
    1132 #ifndef OPENSSL_NO_EC
     929
    1133930static long ecdsa_c[ECDSA_NUM][2];
    1134931static int ECDSA_sign_loop(void *args)
     
    1136933    loopargs_t *tempargs = *(loopargs_t **) args;
    1137934    unsigned char *buf = tempargs->buf;
    1138     EC_KEY **ecdsa = tempargs->ecdsa;
    1139     unsigned char *ecdsasig = tempargs->buf2;
    1140     unsigned int *ecdsasiglen = &tempargs->siglen;
     935    unsigned char *buf2 = tempargs->buf2;
     936    size_t *ecdsa_num = &tempargs->sigsize;
     937    EVP_PKEY_CTX **ecdsa_sign_ctx = tempargs->ecdsa_sign_ctx;
    1141938    int ret, count;
     939
    1142940    for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
    1143         ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
    1144         if (ret == 0) {
     941        *ecdsa_num = tempargs->buflen;
     942        ret = EVP_PKEY_sign(ecdsa_sign_ctx[testnum], buf2, ecdsa_num, buf, 20);
     943        if (ret <= 0) {
    1145944            BIO_printf(bio_err, "ECDSA sign failure\n");
    1146945            ERR_print_errors(bio_err);
     
    1156955    loopargs_t *tempargs = *(loopargs_t **) args;
    1157956    unsigned char *buf = tempargs->buf;
    1158     EC_KEY **ecdsa = tempargs->ecdsa;
    1159     unsigned char *ecdsasig = tempargs->buf2;
    1160     unsigned int ecdsasiglen = tempargs->siglen;
     957    unsigned char *buf2 = tempargs->buf2;
     958    size_t ecdsa_num = tempargs->sigsize;
     959    EVP_PKEY_CTX **ecdsa_verify_ctx = tempargs->ecdsa_verify_ctx;
    1161960    int ret, count;
     961
    1162962    for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
    1163         ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
    1164         if (ret != 1) {
     963        ret = EVP_PKEY_verify(ecdsa_verify_ctx[testnum], buf2, ecdsa_num,
     964                              buf, 20);
     965        if (ret <= 0) {
    1165966            BIO_printf(bio_err, "ECDSA verify failure\n");
    1166967            ERR_print_errors(bio_err);
     
    12151016    loopargs_t *tempargs = *(loopargs_t **) args;
    12161017    unsigned char *buf = tempargs->buf;
    1217     EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
     1018    EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
    12181019    unsigned char *eddsasig = tempargs->buf2;
    12191020    size_t eddsasigsize = tempargs->sigsize;
     
    12311032    return count;
    12321033}
    1233 #endif                          /* OPENSSL_NO_EC */
     1034
     1035#ifndef OPENSSL_NO_SM2
     1036static long sm2_c[SM2_NUM][2];
     1037static int SM2_sign_loop(void *args)
     1038{
     1039    loopargs_t *tempargs = *(loopargs_t **) args;
     1040    unsigned char *buf = tempargs->buf;
     1041    EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
     1042    unsigned char *sm2sig = tempargs->buf2;
     1043    size_t sm2sigsize;
     1044    int ret, count;
     1045    EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
     1046    const size_t max_size = EVP_PKEY_get_size(sm2_pkey[testnum]);
     1047
     1048    for (count = 0; COND(sm2_c[testnum][0]); count++) {
     1049        sm2sigsize = max_size;
     1050
     1051        if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
     1052                                NULL, sm2_pkey[testnum])) {
     1053            BIO_printf(bio_err, "SM2 init sign failure\n");
     1054            ERR_print_errors(bio_err);
     1055            count = -1;
     1056            break;
     1057        }
     1058        ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
     1059                             buf, 20);
     1060        if (ret == 0) {
     1061            BIO_printf(bio_err, "SM2 sign failure\n");
     1062            ERR_print_errors(bio_err);
     1063            count = -1;
     1064            break;
     1065        }
     1066        /* update the latest returned size and always use the fixed buffer size */
     1067        tempargs->sigsize = sm2sigsize;
     1068    }
     1069
     1070    return count;
     1071}
     1072
     1073static int SM2_verify_loop(void *args)
     1074{
     1075    loopargs_t *tempargs = *(loopargs_t **) args;
     1076    unsigned char *buf = tempargs->buf;
     1077    EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
     1078    unsigned char *sm2sig = tempargs->buf2;
     1079    size_t sm2sigsize = tempargs->sigsize;
     1080    int ret, count;
     1081    EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
     1082
     1083    for (count = 0; COND(sm2_c[testnum][1]); count++) {
     1084        if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
     1085                                  NULL, sm2_pkey[testnum])) {
     1086            BIO_printf(bio_err, "SM2 verify init failure\n");
     1087            ERR_print_errors(bio_err);
     1088            count = -1;
     1089            break;
     1090        }
     1091        ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
     1092                               buf, 20);
     1093        if (ret != 1) {
     1094            BIO_printf(bio_err, "SM2 verify failure\n");
     1095            ERR_print_errors(bio_err);
     1096            count = -1;
     1097            break;
     1098        }
     1099    }
     1100    return count;
     1101}
     1102#endif                         /* OPENSSL_NO_SM2 */
    12341103
    12351104static int run_benchmark(int async_jobs,
     
    13841253}
    13851254
     1255typedef struct ec_curve_st {
     1256    const char *name;
     1257    unsigned int nid;
     1258    unsigned int bits;
     1259    size_t sigsize; /* only used for EdDSA curves */
     1260} EC_CURVE;
     1261
     1262static EVP_PKEY *get_ecdsa(const EC_CURVE *curve)
     1263{
     1264    EVP_PKEY_CTX *kctx = NULL;
     1265    EVP_PKEY *key = NULL;
     1266
     1267    /* Ensure that the error queue is empty */
     1268    if (ERR_peek_error()) {
     1269        BIO_printf(bio_err,
     1270                   "WARNING: the error queue contains previous unhandled errors.\n");
     1271        ERR_print_errors(bio_err);
     1272    }
     1273
     1274    /*
     1275     * Let's try to create a ctx directly from the NID: this works for
     1276     * curves like Curve25519 that are not implemented through the low
     1277     * level EC interface.
     1278     * If this fails we try creating a EVP_PKEY_EC generic param ctx,
     1279     * then we set the curve by NID before deriving the actual keygen
     1280     * ctx for that specific curve.
     1281     */
     1282    kctx = EVP_PKEY_CTX_new_id(curve->nid, NULL);
     1283    if (kctx == NULL) {
     1284        EVP_PKEY_CTX *pctx = NULL;
     1285        EVP_PKEY *params = NULL;
     1286        /*
     1287         * If we reach this code EVP_PKEY_CTX_new_id() failed and a
     1288         * "int_ctx_new:unsupported algorithm" error was added to the
     1289         * error queue.
     1290         * We remove it from the error queue as we are handling it.
     1291         */
     1292        unsigned long error = ERR_peek_error();
     1293
     1294        if (error == ERR_peek_last_error() /* oldest and latest errors match */
     1295            /* check that the error origin matches */
     1296            && ERR_GET_LIB(error) == ERR_LIB_EVP
     1297            && (ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM
     1298                || ERR_GET_REASON(error) == ERR_R_UNSUPPORTED))
     1299            ERR_get_error(); /* pop error from queue */
     1300        if (ERR_peek_error()) {
     1301            BIO_printf(bio_err,
     1302                       "Unhandled error in the error queue during EC key setup.\n");
     1303            ERR_print_errors(bio_err);
     1304            return NULL;
     1305        }
     1306
     1307        /* Create the context for parameter generation */
     1308        if ((pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL
     1309            || EVP_PKEY_paramgen_init(pctx) <= 0
     1310            || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
     1311                                                      curve->nid) <= 0
     1312            || EVP_PKEY_paramgen(pctx, &params) <= 0) {
     1313            BIO_printf(bio_err, "EC params init failure.\n");
     1314            ERR_print_errors(bio_err);
     1315            EVP_PKEY_CTX_free(pctx);
     1316            return NULL;
     1317        }
     1318        EVP_PKEY_CTX_free(pctx);
     1319
     1320        /* Create the context for the key generation */
     1321        kctx = EVP_PKEY_CTX_new(params, NULL);
     1322        EVP_PKEY_free(params);
     1323    }
     1324    if (kctx == NULL
     1325        || EVP_PKEY_keygen_init(kctx) <= 0
     1326        || EVP_PKEY_keygen(kctx, &key) <= 0) {
     1327        BIO_printf(bio_err, "EC key generation failure.\n");
     1328        ERR_print_errors(bio_err);
     1329        key = NULL;
     1330    }
     1331    EVP_PKEY_CTX_free(kctx);
     1332    return key;
     1333}
     1334
     1335#define stop_it(do_it, test_num)\
     1336    memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
     1337
    13861338int speed_main(int argc, char **argv)
    13871339{
     
    13901342    const char *prog;
    13911343    const char *engine_id = NULL;
    1392     const EVP_CIPHER *evp_cipher = NULL;
     1344    EVP_CIPHER *evp_cipher = NULL;
     1345    EVP_MAC *mac = NULL;
    13931346    double d = 0.0;
    13941347    OPTION_CHOICE o;
    13951348    int async_init = 0, multiblock = 0, pr_header = 0;
    1396     int doit[ALGOR_NUM] = { 0 };
     1349    uint8_t doit[ALGOR_NUM] = { 0 };
    13971350    int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
    13981351    long count = 0;
    1399     unsigned int size_num = OSSL_NELEM(lengths_list);
    1400     unsigned int i, k, loop, loopargs_len = 0, async_jobs = 0;
     1352    unsigned int size_num = SIZE_NUM;
     1353    unsigned int i, k, loopargs_len = 0, async_jobs = 0;
    14011354    int keylen;
    14021355    int buflen;
     1356    BIGNUM *bn = NULL;
     1357    EVP_PKEY_CTX *genctx = NULL;
    14031358#ifndef NO_FORK
    14041359    int multi = 0;
    14051360#endif
    1406 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
    1407     || !defined(OPENSSL_NO_EC)
    1408     long rsa_count = 1;
    1409 #endif
     1361    long op_count = 1;
    14101362    openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
    14111363                                    ECDSA_SECONDS, ECDH_SECONDS,
    1412                                     EdDSA_SECONDS };
    1413 
    1414     /* What follows are the buffers and key material. */
    1415 #ifndef OPENSSL_NO_RC5
    1416     RC5_32_KEY rc5_ks;
    1417 #endif
    1418 #ifndef OPENSSL_NO_RC2
    1419     RC2_KEY rc2_ks;
    1420 #endif
    1421 #ifndef OPENSSL_NO_IDEA
    1422     IDEA_KEY_SCHEDULE idea_ks;
    1423 #endif
    1424 #ifndef OPENSSL_NO_SEED
    1425     SEED_KEY_SCHEDULE seed_ks;
    1426 #endif
    1427 #ifndef OPENSSL_NO_BF
    1428     BF_KEY bf_ks;
    1429 #endif
    1430 #ifndef OPENSSL_NO_CAST
    1431     CAST_KEY cast_ks;
    1432 #endif
    1433     static const unsigned char key16[16] = {
    1434         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
    1435         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
    1436     };
    1437     static const unsigned char key24[24] = {
    1438         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
    1439         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
    1440         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
    1441     };
     1364                                    EdDSA_SECONDS, SM2_SECONDS,
     1365                                    FFDH_SECONDS };
     1366
    14421367    static const unsigned char key32[32] = {
    14431368        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
     
    14461371        0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
    14471372    };
    1448 #ifndef OPENSSL_NO_CAMELLIA
    1449     static const unsigned char ckey24[24] = {
    1450         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
    1451         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
    1452         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
     1373    static const unsigned char deskey[] = {
     1374        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, /* key1 */
     1375        0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, /* key2 */
     1376        0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34  /* key3 */
    14531377    };
    1454     static const unsigned char ckey32[32] = {
    1455         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
    1456         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
    1457         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
    1458         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
     1378    static const struct {
     1379        const unsigned char *data;
     1380        unsigned int length;
     1381        unsigned int bits;
     1382    } rsa_keys[] = {
     1383        {   test512,   sizeof(test512),   512 },
     1384        {  test1024,  sizeof(test1024),  1024 },
     1385        {  test2048,  sizeof(test2048),  2048 },
     1386        {  test3072,  sizeof(test3072),  3072 },
     1387        {  test4096,  sizeof(test4096),  4096 },
     1388        {  test7680,  sizeof(test7680),  7680 },
     1389        { test15360, sizeof(test15360), 15360 }
    14591390    };
    1460     CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
    1461 #endif
    1462 #ifndef OPENSSL_NO_DES
    1463     static DES_cblock key = {
    1464         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
     1391    uint8_t rsa_doit[RSA_NUM] = { 0 };
     1392    int primes = RSA_DEFAULT_PRIME_NUM;
     1393#ifndef OPENSSL_NO_DH
     1394    typedef struct ffdh_params_st {
     1395        const char *name;
     1396        unsigned int nid;
     1397        unsigned int bits;
     1398    } FFDH_PARAMS;
     1399
     1400    static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
     1401        {"ffdh2048", NID_ffdhe2048, 2048},
     1402        {"ffdh3072", NID_ffdhe3072, 3072},
     1403        {"ffdh4096", NID_ffdhe4096, 4096},
     1404        {"ffdh6144", NID_ffdhe6144, 6144},
     1405        {"ffdh8192", NID_ffdhe8192, 8192}
    14651406    };
    1466     static DES_cblock key2 = {
    1467         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
    1468     };
    1469     static DES_cblock key3 = {
    1470         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
    1471     };
    1472 #endif
    1473 #ifndef OPENSSL_NO_RSA
    1474     static const unsigned int rsa_bits[RSA_NUM] = {
    1475         512, 1024, 2048, 3072, 4096, 7680, 15360
    1476     };
    1477     static const unsigned char *rsa_data[RSA_NUM] = {
    1478         test512, test1024, test2048, test3072, test4096, test7680, test15360
    1479     };
    1480     static const int rsa_data_length[RSA_NUM] = {
    1481         sizeof(test512), sizeof(test1024),
    1482         sizeof(test2048), sizeof(test3072),
    1483         sizeof(test4096), sizeof(test7680),
    1484         sizeof(test15360)
    1485     };
    1486     int rsa_doit[RSA_NUM] = { 0 };
    1487     int primes = RSA_DEFAULT_PRIME_NUM;
    1488 #endif
    1489 #ifndef OPENSSL_NO_DSA
     1407    uint8_t ffdh_doit[FFDH_NUM] = { 0 };
     1408
     1409#endif /* OPENSSL_NO_DH */
    14901410    static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
    1491     int dsa_doit[DSA_NUM] = { 0 };
    1492 #endif
    1493 #ifndef OPENSSL_NO_EC
     1411    uint8_t dsa_doit[DSA_NUM] = { 0 };
    14941412    /*
    14951413     * We only test over the following curves as they are representative, To
    14961414     * add tests over more curves, simply add the curve NID and curve name to
    1497      * the following arrays and increase the |ecdh_choices| list accordingly.
     1415     * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
     1416     * lists accordingly.
    14981417     */
    1499     static const struct {
    1500         const char *name;
    1501         unsigned int nid;
    1502         unsigned int bits;
    1503     } test_curves[] = {
     1418    static const EC_CURVE ec_curves[EC_NUM] = {
    15041419        /* Prime Curves */
    15051420        {"secp160r1", NID_secp160r1, 160},
     
    15091424        {"nistp384", NID_secp384r1, 384},
    15101425        {"nistp521", NID_secp521r1, 521},
    1511 # ifndef OPENSSL_NO_EC2M
     1426#ifndef OPENSSL_NO_EC2M
    15121427        /* Binary Curves */
    15131428        {"nistk163", NID_sect163k1, 163},
     
    15211436        {"nistb409", NID_sect409r1, 409},
    15221437        {"nistb571", NID_sect571r1, 571},
    1523 # endif
     1438#endif
    15241439        {"brainpoolP256r1", NID_brainpoolP256r1, 256},
    15251440        {"brainpoolP256t1", NID_brainpoolP256t1, 256},
     
    15321447        {"X448", NID_X448, 448}
    15331448    };
    1534     static const struct {
    1535         const char *name;
    1536         unsigned int nid;
    1537         unsigned int bits;
    1538         size_t sigsize;
    1539     } test_ed_curves[] = {
     1449    static const EC_CURVE ed_curves[EdDSA_NUM] = {
    15401450        /* EdDSA */
    15411451        {"Ed25519", NID_ED25519, 253, 64},
    15421452        {"Ed448", NID_ED448, 456, 114}
    15431453    };
    1544     int ecdsa_doit[ECDSA_NUM] = { 0 };
    1545     int ecdh_doit[EC_NUM] = { 0 };
    1546     int eddsa_doit[EdDSA_NUM] = { 0 };
    1547     OPENSSL_assert(OSSL_NELEM(test_curves) >= EC_NUM);
    1548     OPENSSL_assert(OSSL_NELEM(test_ed_curves) >= EdDSA_NUM);
    1549 #endif                          /* ndef OPENSSL_NO_EC */
     1454#ifndef OPENSSL_NO_SM2
     1455    static const EC_CURVE sm2_curves[SM2_NUM] = {
     1456        /* SM2 */
     1457        {"CurveSM2", NID_sm2, 256}
     1458    };
     1459    uint8_t sm2_doit[SM2_NUM] = { 0 };
     1460#endif
     1461    uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
     1462    uint8_t ecdh_doit[EC_NUM] = { 0 };
     1463    uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
     1464
     1465    /* checks declarated curves against choices list. */
     1466    OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
     1467    OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
     1468
     1469    OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
     1470    OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
     1471
     1472    OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
     1473    OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
     1474
     1475#ifndef OPENSSL_NO_SM2
     1476    OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
     1477    OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
     1478#endif
    15501479
    15511480    prog = opt_init(argc, argv, speed_options);
     
    15651494            break;
    15661495        case OPT_EVP:
    1567             evp_md = NULL;
    1568             evp_cipher = EVP_get_cipherbyname(opt_arg());
    1569             if (evp_cipher == NULL)
    1570                 evp_md = EVP_get_digestbyname(opt_arg());
    1571             if (evp_cipher == NULL && evp_md == NULL) {
     1496            if (doit[D_EVP]) {
     1497                BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog);
     1498                goto opterr;
     1499            }
     1500            ERR_set_mark();
     1501            if (!opt_cipher_silent(opt_arg(), &evp_cipher)) {
     1502                if (have_md(opt_arg()))
     1503                    evp_md_name = opt_arg();
     1504            }
     1505            if (evp_cipher == NULL && evp_md_name == NULL) {
     1506                ERR_clear_last_mark();
    15721507                BIO_printf(bio_err,
    15731508                           "%s: %s is an unknown cipher or digest\n",
     
    15751510                goto end;
    15761511            }
     1512            ERR_pop_to_mark();
    15771513            doit[D_EVP] = 1;
     1514            break;
     1515        case OPT_HMAC:
     1516            if (!have_md(opt_arg())) {
     1517                BIO_printf(bio_err, "%s: %s is an unknown digest\n",
     1518                           prog, opt_arg());
     1519                goto end;
     1520            }
     1521            evp_mac_mdname = opt_arg();
     1522            doit[D_HMAC] = 1;
     1523            break;
     1524        case OPT_CMAC:
     1525            if (!have_cipher(opt_arg())) {
     1526                BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
     1527                           prog, opt_arg());
     1528                goto end;
     1529            }
     1530            evp_mac_ciphername = opt_arg();
     1531            doit[D_EVP_CMAC] = 1;
    15781532            break;
    15791533        case OPT_DECRYPT:
     
    15911545#ifndef NO_FORK
    15921546            multi = atoi(opt_arg());
     1547            if ((size_t)multi >= SIZE_MAX / sizeof(int)) {
     1548                BIO_printf(bio_err, "%s: multi argument too large\n", prog);
     1549                return 0;
     1550            }
    15931551#endif
    15941552            break;
     
    16091567            break;
    16101568        case OPT_MISALIGN:
    1611             if (!opt_int(opt_arg(), &misalign))
    1612                 goto end;
     1569            misalign = opt_int_arg();
    16131570            if (misalign > MISALIGN) {
    16141571                BIO_printf(bio_err,
     
    16331590                goto end;
    16341591            break;
     1592        case OPT_PROV_CASES:
     1593            if (!opt_provider(o))
     1594                goto end;
     1595            break;
    16351596        case OPT_PRIMES:
    1636             if (!opt_int(opt_arg(), &primes))
    1637                 goto end;
     1597            primes = opt_int_arg();
    16381598            break;
    16391599        case OPT_SECONDS:
    16401600            seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
    1641                         = seconds.ecdh = seconds.eddsa = atoi(opt_arg());
     1601                        = seconds.ecdh = seconds.eddsa
     1602                        = seconds.sm2 = seconds.ffdh = atoi(opt_arg());
    16421603            break;
    16431604        case OPT_BYTES:
     
    16511612        }
    16521613    }
     1614
     1615    /* Remaining arguments are algorithms. */
    16531616    argc = opt_num_rest();
    16541617    argv = opt_rest();
    16551618
    1656     /* Remaining arguments are algorithms. */
     1619    if (!app_RAND_load())
     1620        goto end;
     1621
    16571622    for (; *argv; argv++) {
    1658         if (found(*argv, doit_choices, &i)) {
     1623        const char *algo = *argv;
     1624
     1625        if (opt_found(algo, doit_choices, &i)) {
    16591626            doit[i] = 1;
    16601627            continue;
    16611628        }
    1662 #ifndef OPENSSL_NO_DES
    1663         if (strcmp(*argv, "des") == 0) {
     1629        if (strcmp(algo, "des") == 0) {
    16641630            doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
    16651631            continue;
    16661632        }
    1667 #endif
    1668         if (strcmp(*argv, "sha") == 0) {
     1633        if (strcmp(algo, "sha") == 0) {
    16691634            doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
    16701635            continue;
    16711636        }
    1672 #ifndef OPENSSL_NO_RSA
    1673         if (strcmp(*argv, "openssl") == 0)
     1637#ifndef OPENSSL_NO_DEPRECATED_3_0
     1638        if (strcmp(algo, "openssl") == 0) /* just for compatibility */
    16741639            continue;
    1675         if (strcmp(*argv, "rsa") == 0) {
    1676             for (loop = 0; loop < OSSL_NELEM(rsa_doit); loop++)
    1677                 rsa_doit[loop] = 1;
    1678             continue;
    1679         }
    1680         if (found(*argv, rsa_choices, &i)) {
    1681             rsa_doit[i] = 1;
    1682             continue;
    1683         }
    1684 #endif
    1685 #ifndef OPENSSL_NO_DSA
    1686         if (strcmp(*argv, "dsa") == 0) {
    1687             dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
    1688                 dsa_doit[R_DSA_2048] = 1;
    1689             continue;
    1690         }
    1691         if (found(*argv, dsa_choices, &i)) {
    1692             dsa_doit[i] = 2;
    1693             continue;
    1694         }
    1695 #endif
    1696         if (strcmp(*argv, "aes") == 0) {
     1640#endif
     1641        if (strncmp(algo, "rsa", 3) == 0) {
     1642            if (algo[3] == '\0') {
     1643                memset(rsa_doit, 1, sizeof(rsa_doit));
     1644                continue;
     1645            }
     1646            if (opt_found(algo, rsa_choices, &i)) {
     1647                rsa_doit[i] = 1;
     1648                continue;
     1649            }
     1650        }
     1651#ifndef OPENSSL_NO_DH
     1652        if (strncmp(algo, "ffdh", 4) == 0) {
     1653            if (algo[4] == '\0') {
     1654                memset(ffdh_doit, 1, sizeof(ffdh_doit));
     1655                continue;
     1656            }
     1657            if (opt_found(algo, ffdh_choices, &i)) {
     1658                ffdh_doit[i] = 2;
     1659                continue;
     1660            }
     1661        }
     1662#endif
     1663        if (strncmp(algo, "dsa", 3) == 0) {
     1664            if (algo[3] == '\0') {
     1665                memset(dsa_doit, 1, sizeof(dsa_doit));
     1666                continue;
     1667            }
     1668            if (opt_found(algo, dsa_choices, &i)) {
     1669                dsa_doit[i] = 2;
     1670                continue;
     1671            }
     1672        }
     1673        if (strcmp(algo, "aes") == 0) {
    16971674            doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
    16981675            continue;
    16991676        }
    1700 #ifndef OPENSSL_NO_CAMELLIA
    1701         if (strcmp(*argv, "camellia") == 0) {
     1677        if (strcmp(algo, "camellia") == 0) {
    17021678            doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
    17031679            continue;
    17041680        }
    1705 #endif
    1706 #ifndef OPENSSL_NO_EC
    1707         if (strcmp(*argv, "ecdsa") == 0) {
    1708             for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
    1709                 ecdsa_doit[loop] = 1;
     1681        if (strncmp(algo, "ecdsa", 5) == 0) {
     1682            if (algo[5] == '\0') {
     1683                memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
     1684                continue;
     1685            }
     1686            if (opt_found(algo, ecdsa_choices, &i)) {
     1687                ecdsa_doit[i] = 2;
     1688                continue;
     1689            }
     1690        }
     1691        if (strncmp(algo, "ecdh", 4) == 0) {
     1692            if (algo[4] == '\0') {
     1693                memset(ecdh_doit, 1, sizeof(ecdh_doit));
     1694                continue;
     1695            }
     1696            if (opt_found(algo, ecdh_choices, &i)) {
     1697                ecdh_doit[i] = 2;
     1698                continue;
     1699            }
     1700        }
     1701        if (strcmp(algo, "eddsa") == 0) {
     1702            memset(eddsa_doit, 1, sizeof(eddsa_doit));
    17101703            continue;
    17111704        }
    1712         if (found(*argv, ecdsa_choices, &i)) {
    1713             ecdsa_doit[i] = 2;
    1714             continue;
    1715         }
    1716         if (strcmp(*argv, "ecdh") == 0) {
    1717             for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
    1718                 ecdh_doit[loop] = 1;
    1719             continue;
    1720         }
    1721         if (found(*argv, ecdh_choices, &i)) {
    1722             ecdh_doit[i] = 2;
    1723             continue;
    1724         }
    1725         if (strcmp(*argv, "eddsa") == 0) {
    1726             for (loop = 0; loop < OSSL_NELEM(eddsa_doit); loop++)
    1727                 eddsa_doit[loop] = 1;
    1728             continue;
    1729         }
    1730         if (found(*argv, eddsa_choices, &i)) {
     1705        if (opt_found(algo, eddsa_choices, &i)) {
    17311706            eddsa_doit[i] = 2;
    17321707            continue;
    17331708        }
    1734 #endif
    1735         BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
     1709#ifndef OPENSSL_NO_SM2
     1710        if (strcmp(algo, "sm2") == 0) {
     1711            memset(sm2_doit, 1, sizeof(sm2_doit));
     1712            continue;
     1713        }
     1714        if (opt_found(algo, sm2_choices, &i)) {
     1715            sm2_doit[i] = 2;
     1716            continue;
     1717        }
     1718#endif
     1719        BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
    17361720        goto end;
    17371721    }
     
    17421726            BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
    17431727            goto end;
    1744         } else if (!(EVP_CIPHER_flags(evp_cipher) &
     1728        } else if (!(EVP_CIPHER_get_flags(evp_cipher) &
    17451729                     EVP_CIPH_FLAG_AEAD_CIPHER)) {
    17461730            BIO_printf(bio_err, "%s is not an AEAD cipher\n",
    1747                        OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
     1731                       EVP_CIPHER_get0_name(evp_cipher));
    17481732            goto end;
    17491733        }
     
    17511735    if (multiblock) {
    17521736        if (evp_cipher == NULL) {
    1753             BIO_printf(bio_err,"-mb can be used only with a multi-block"
    1754                                " capable cipher\n");
     1737            BIO_printf(bio_err, "-mb can be used only with a multi-block"
     1738                                " capable cipher\n");
    17551739            goto end;
    1756         } else if (!(EVP_CIPHER_flags(evp_cipher) &
     1740        } else if (!(EVP_CIPHER_get_flags(evp_cipher) &
    17571741                     EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
    17581742            BIO_printf(bio_err, "%s is not a multi-block capable\n",
    1759                        OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
     1743                       EVP_CIPHER_get0_name(evp_cipher));
    17601744            goto end;
    17611745        } else if (async_jobs > 0) {
     
    18001784        loopargs[i].buf = loopargs[i].buf_malloc + misalign;
    18011785        loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
    1802 #ifndef OPENSSL_NO_EC
     1786        loopargs[i].buflen = buflen - misalign;
     1787        loopargs[i].sigsize = buflen - misalign;
    18031788        loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
    18041789        loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
     1790#ifndef OPENSSL_NO_DH
     1791        loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
     1792        loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
    18051793#endif
    18061794    }
     
    18151803
    18161804    /* No parameters; turn on everything. */
    1817     if ((argc == 0) && !doit[D_EVP]) {
    1818         for (i = 0; i < ALGOR_NUM; i++)
    1819             if (i != D_EVP)
    1820                 doit[i] = 1;
    1821 #ifndef OPENSSL_NO_RSA
    1822         for (i = 0; i < RSA_NUM; i++)
    1823             rsa_doit[i] = 1;
    1824 #endif
    1825 #ifndef OPENSSL_NO_DSA
    1826         for (i = 0; i < DSA_NUM; i++)
    1827             dsa_doit[i] = 1;
    1828 #endif
    1829 #ifndef OPENSSL_NO_EC
    1830         for (loop = 0; loop < OSSL_NELEM(ecdsa_doit); loop++)
    1831             ecdsa_doit[loop] = 1;
    1832         for (loop = 0; loop < OSSL_NELEM(ecdh_doit); loop++)
    1833             ecdh_doit[loop] = 1;
    1834         for (loop = 0; loop < OSSL_NELEM(eddsa_doit); loop++)
    1835             eddsa_doit[loop] = 1;
     1805    if (argc == 0 && !doit[D_EVP] && !doit[D_HMAC] && !doit[D_EVP_CMAC]) {
     1806        memset(doit, 1, sizeof(doit));
     1807        doit[D_EVP] = doit[D_EVP_CMAC] = 0;
     1808        ERR_set_mark();
     1809        for (i = D_MD2; i <= D_WHIRLPOOL; i++) {
     1810            if (!have_md(names[i]))
     1811                doit[i] = 0;
     1812        }
     1813        for (i = D_CBC_DES; i <= D_CBC_256_CML; i++) {
     1814            if (!have_cipher(names[i]))
     1815                doit[i] = 0;
     1816        }
     1817        if ((mac = EVP_MAC_fetch(app_get0_libctx(), "GMAC",
     1818                                 app_get0_propq())) != NULL) {
     1819            EVP_MAC_free(mac);
     1820            mac = NULL;
     1821        } else {
     1822            doit[D_GHASH] = 0;
     1823        }
     1824        if ((mac = EVP_MAC_fetch(app_get0_libctx(), "HMAC",
     1825                                 app_get0_propq())) != NULL) {
     1826            EVP_MAC_free(mac);
     1827            mac = NULL;
     1828        } else {
     1829            doit[D_HMAC] = 0;
     1830        }
     1831        ERR_pop_to_mark();
     1832        memset(rsa_doit, 1, sizeof(rsa_doit));
     1833#ifndef OPENSSL_NO_DH
     1834        memset(ffdh_doit, 1, sizeof(ffdh_doit));
     1835#endif
     1836        memset(dsa_doit, 1, sizeof(dsa_doit));
     1837        memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
     1838        memset(ecdh_doit, 1, sizeof(ecdh_doit));
     1839        memset(eddsa_doit, 1, sizeof(eddsa_doit));
     1840#ifndef OPENSSL_NO_SM2
     1841        memset(sm2_doit, 1, sizeof(sm2_doit));
    18361842#endif
    18371843    }
     
    18451851                   "instead of user CPU time.\n");
    18461852
    1847 #ifndef OPENSSL_NO_RSA
    1848     for (i = 0; i < loopargs_len; i++) {
    1849         if (primes > RSA_DEFAULT_PRIME_NUM) {
    1850             /* for multi-prime RSA, skip this */
    1851             break;
    1852         }
    1853         for (k = 0; k < RSA_NUM; k++) {
    1854             const unsigned char *p;
    1855 
    1856             p = rsa_data[k];
    1857             loopargs[i].rsa_key[k] =
    1858                 d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
    1859             if (loopargs[i].rsa_key[k] == NULL) {
    1860                 BIO_printf(bio_err,
    1861                            "internal error loading RSA key number %d\n", k);
    1862                 goto end;
    1863             }
    1864         }
    1865     }
    1866 #endif
    1867 #ifndef OPENSSL_NO_DSA
    1868     for (i = 0; i < loopargs_len; i++) {
    1869         loopargs[i].dsa_key[0] = get_dsa(512);
    1870         loopargs[i].dsa_key[1] = get_dsa(1024);
    1871         loopargs[i].dsa_key[2] = get_dsa(2048);
    1872     }
    1873 #endif
    1874 #ifndef OPENSSL_NO_DES
    1875     DES_set_key_unchecked(&key, &sch);
    1876     DES_set_key_unchecked(&key2, &sch2);
    1877     DES_set_key_unchecked(&key3, &sch3);
    1878 #endif
    1879     AES_set_encrypt_key(key16, 128, &aes_ks1);
    1880     AES_set_encrypt_key(key24, 192, &aes_ks2);
    1881     AES_set_encrypt_key(key32, 256, &aes_ks3);
    1882 #ifndef OPENSSL_NO_CAMELLIA
    1883     Camellia_set_key(key16, 128, &camellia_ks1);
    1884     Camellia_set_key(ckey24, 192, &camellia_ks2);
    1885     Camellia_set_key(ckey32, 256, &camellia_ks3);
    1886 #endif
    1887 #ifndef OPENSSL_NO_IDEA
    1888     IDEA_set_encrypt_key(key16, &idea_ks);
    1889 #endif
    1890 #ifndef OPENSSL_NO_SEED
    1891     SEED_set_key(key16, &seed_ks);
    1892 #endif
    1893 #ifndef OPENSSL_NO_RC4
    1894     RC4_set_key(&rc4_ks, 16, key16);
    1895 #endif
    1896 #ifndef OPENSSL_NO_RC2
    1897     RC2_set_key(&rc2_ks, 16, key16, 128);
    1898 #endif
    1899 #ifndef OPENSSL_NO_RC5
    1900     RC5_32_set_key(&rc5_ks, 16, key16, 12);
    1901 #endif
    1902 #ifndef OPENSSL_NO_BF
    1903     BF_set_key(&bf_ks, 16, key16);
    1904 #endif
    1905 #ifndef OPENSSL_NO_CAST
    1906     CAST_set_key(&cast_ks, 16, key16);
    1907 #endif
    1908 #ifndef SIGALRM
    1909 # ifndef OPENSSL_NO_DES
    1910     BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
    1911     count = 10;
    1912     do {
    1913         long it;
    1914         count *= 2;
    1915         Time_F(START);
    1916         for (it = count; it; it--)
    1917             DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
    1918                             (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
    1919         d = Time_F(STOP);
    1920     } while (d < 3);
    1921     save_count = count;
    1922     c[D_MD2][0] = count / 10;
    1923     c[D_MDC2][0] = count / 10;
    1924     c[D_MD4][0] = count;
    1925     c[D_MD5][0] = count;
    1926     c[D_HMAC][0] = count;
    1927     c[D_SHA1][0] = count;
    1928     c[D_RMD160][0] = count;
    1929     c[D_RC4][0] = count * 5;
    1930     c[D_CBC_DES][0] = count;
    1931     c[D_EDE3_DES][0] = count / 3;
    1932     c[D_CBC_IDEA][0] = count;
    1933     c[D_CBC_SEED][0] = count;
    1934     c[D_CBC_RC2][0] = count;
    1935     c[D_CBC_RC5][0] = count;
    1936     c[D_CBC_BF][0] = count;
    1937     c[D_CBC_CAST][0] = count;
    1938     c[D_CBC_128_AES][0] = count;
    1939     c[D_CBC_192_AES][0] = count;
    1940     c[D_CBC_256_AES][0] = count;
    1941     c[D_CBC_128_CML][0] = count;
    1942     c[D_CBC_192_CML][0] = count;
    1943     c[D_CBC_256_CML][0] = count;
    1944     c[D_SHA256][0] = count;
    1945     c[D_SHA512][0] = count;
    1946     c[D_WHIRLPOOL][0] = count;
    1947     c[D_IGE_128_AES][0] = count;
    1948     c[D_IGE_192_AES][0] = count;
    1949     c[D_IGE_256_AES][0] = count;
    1950     c[D_GHASH][0] = count;
    1951     c[D_RAND][0] = count;
    1952 
    1953     for (i = 1; i < size_num; i++) {
    1954         long l0, l1;
    1955 
    1956         l0 = (long)lengths[0];
    1957         l1 = (long)lengths[i];
    1958 
    1959         c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
    1960         c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
    1961         c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
    1962         c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
    1963         c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
    1964         c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
    1965         c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
    1966         c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
    1967         c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
    1968         c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
    1969         c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
    1970         c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
    1971 
    1972         l0 = (long)lengths[i - 1];
    1973 
    1974         c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
    1975         c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
    1976         c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
    1977         c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
    1978         c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
    1979         c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
    1980         c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
    1981         c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
    1982         c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
    1983         c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
    1984         c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
    1985         c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
    1986         c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
    1987         c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
    1988         c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
    1989         c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
    1990         c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
    1991         c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
    1992     }
    1993 
    1994 #  ifndef OPENSSL_NO_RSA
    1995     rsa_c[R_RSA_512][0] = count / 2000;
    1996     rsa_c[R_RSA_512][1] = count / 400;
    1997     for (i = 1; i < RSA_NUM; i++) {
    1998         rsa_c[i][0] = rsa_c[i - 1][0] / 8;
    1999         rsa_c[i][1] = rsa_c[i - 1][1] / 4;
    2000         if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
    2001             rsa_doit[i] = 0;
    2002         else {
    2003             if (rsa_c[i][0] == 0) {
    2004                 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
    2005                 rsa_c[i][1] = 20;
    2006             }
    2007         }
    2008     }
    2009 #  endif
    2010 
    2011 #  ifndef OPENSSL_NO_DSA
    2012     dsa_c[R_DSA_512][0] = count / 1000;
    2013     dsa_c[R_DSA_512][1] = count / 1000 / 2;
    2014     for (i = 1; i < DSA_NUM; i++) {
    2015         dsa_c[i][0] = dsa_c[i - 1][0] / 4;
    2016         dsa_c[i][1] = dsa_c[i - 1][1] / 4;
    2017         if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
    2018             dsa_doit[i] = 0;
    2019         else {
    2020             if (dsa_c[i][0] == 0) {
    2021                 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
    2022                 dsa_c[i][1] = 1;
    2023             }
    2024         }
    2025     }
    2026 #  endif
    2027 
    2028 #  ifndef OPENSSL_NO_EC
    2029     ecdsa_c[R_EC_P160][0] = count / 1000;
    2030     ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
    2031     for (i = R_EC_P192; i <= R_EC_P521; i++) {
    2032         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
    2033         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
    2034         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
    2035             ecdsa_doit[i] = 0;
    2036         else {
    2037             if (ecdsa_c[i][0] == 0) {
    2038                 ecdsa_c[i][0] = 1;
    2039                 ecdsa_c[i][1] = 1;
    2040             }
    2041         }
    2042     }
    2043 #   ifndef OPENSSL_NO_EC2M
    2044     ecdsa_c[R_EC_K163][0] = count / 1000;
    2045     ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
    2046     for (i = R_EC_K233; i <= R_EC_K571; i++) {
    2047         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
    2048         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
    2049         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
    2050             ecdsa_doit[i] = 0;
    2051         else {
    2052             if (ecdsa_c[i][0] == 0) {
    2053                 ecdsa_c[i][0] = 1;
    2054                 ecdsa_c[i][1] = 1;
    2055             }
    2056         }
    2057     }
    2058     ecdsa_c[R_EC_B163][0] = count / 1000;
    2059     ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
    2060     for (i = R_EC_B233; i <= R_EC_B571; i++) {
    2061         ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
    2062         ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
    2063         if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
    2064             ecdsa_doit[i] = 0;
    2065         else {
    2066             if (ecdsa_c[i][0] == 0) {
    2067                 ecdsa_c[i][0] = 1;
    2068                 ecdsa_c[i][1] = 1;
    2069             }
    2070         }
    2071     }
    2072 #   endif
    2073 
    2074     ecdh_c[R_EC_P160][0] = count / 1000;
    2075     for (i = R_EC_P192; i <= R_EC_P521; i++) {
    2076         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
    2077         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
    2078             ecdh_doit[i] = 0;
    2079         else {
    2080             if (ecdh_c[i][0] == 0) {
    2081                 ecdh_c[i][0] = 1;
    2082             }
    2083         }
    2084     }
    2085 #   ifndef OPENSSL_NO_EC2M
    2086     ecdh_c[R_EC_K163][0] = count / 1000;
    2087     for (i = R_EC_K233; i <= R_EC_K571; i++) {
    2088         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
    2089         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
    2090             ecdh_doit[i] = 0;
    2091         else {
    2092             if (ecdh_c[i][0] == 0) {
    2093                 ecdh_c[i][0] = 1;
    2094             }
    2095         }
    2096     }
    2097     ecdh_c[R_EC_B163][0] = count / 1000;
    2098     for (i = R_EC_B233; i <= R_EC_B571; i++) {
    2099         ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
    2100         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
    2101             ecdh_doit[i] = 0;
    2102         else {
    2103             if (ecdh_c[i][0] == 0) {
    2104                 ecdh_c[i][0] = 1;
    2105             }
    2106         }
    2107     }
    2108 #   endif
    2109     /* repeated code good to factorize */
    2110     ecdh_c[R_EC_BRP256R1][0] = count / 1000;
    2111     for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
    2112         ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
    2113         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
    2114             ecdh_doit[i] = 0;
    2115         else {
    2116             if (ecdh_c[i][0] == 0) {
    2117                 ecdh_c[i][0] = 1;
    2118             }
    2119         }
    2120     }
    2121     ecdh_c[R_EC_BRP256T1][0] = count / 1000;
    2122     for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
    2123         ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
    2124         if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
    2125             ecdh_doit[i] = 0;
    2126         else {
    2127             if (ecdh_c[i][0] == 0) {
    2128                 ecdh_c[i][0] = 1;
    2129             }
    2130         }
    2131     }
    2132     /* default iteration count for the last two EC Curves */
    2133     ecdh_c[R_EC_X25519][0] = count / 1800;
    2134     ecdh_c[R_EC_X448][0] = count / 7200;
    2135 
    2136     eddsa_c[R_EC_Ed25519][0] = count / 1800;
    2137     eddsa_c[R_EC_Ed448][0] = count / 7200;
    2138 #  endif
    2139 
    2140 # else
    2141 /* not worth fixing */
    2142 #  error "You cannot disable DES on systems without SIGALRM."
    2143 # endif                         /* OPENSSL_NO_DES */
    2144 #elif SIGALRM > 0
     1853#if SIGALRM > 0
    21451854    signal(SIGALRM, alarmed);
    2146 #endif                          /* SIGALRM */
    2147 
    2148 #ifndef OPENSSL_NO_MD2
     1855#endif
     1856
    21491857    if (doit[D_MD2]) {
    21501858        for (testnum = 0; testnum < size_num; testnum++) {
     
    21551863            d = Time_F(STOP);
    21561864            print_result(D_MD2, testnum, count, d);
    2157         }
    2158     }
    2159 #endif
    2160 #ifndef OPENSSL_NO_MDC2
     1865            if (count < 0)
     1866                break;
     1867        }
     1868    }
     1869
    21611870    if (doit[D_MDC2]) {
    21621871        for (testnum = 0; testnum < size_num; testnum++) {
     
    21671876            d = Time_F(STOP);
    21681877            print_result(D_MDC2, testnum, count, d);
    2169         }
    2170     }
    2171 #endif
    2172 
    2173 #ifndef OPENSSL_NO_MD4
     1878            if (count < 0)
     1879                break;
     1880        }
     1881    }
     1882
    21741883    if (doit[D_MD4]) {
    21751884        for (testnum = 0; testnum < size_num; testnum++) {
     
    21801889            d = Time_F(STOP);
    21811890            print_result(D_MD4, testnum, count, d);
    2182         }
    2183     }
    2184 #endif
    2185 
    2186 #ifndef OPENSSL_NO_MD5
     1891            if (count < 0)
     1892                break;
     1893        }
     1894    }
     1895
    21871896    if (doit[D_MD5]) {
    21881897        for (testnum = 0; testnum < size_num; testnum++) {
     
    21931902            d = Time_F(STOP);
    21941903            print_result(D_MD5, testnum, count, d);
    2195         }
    2196     }
    2197 
    2198     if (doit[D_HMAC]) {
    2199         static const char hmac_key[] = "This is a key...";
    2200         int len = strlen(hmac_key);
    2201 
    2202         for (i = 0; i < loopargs_len; i++) {
    2203             loopargs[i].hctx = HMAC_CTX_new();
    2204             if (loopargs[i].hctx == NULL) {
    2205                 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
    2206                 exit(1);
    2207             }
    2208 
    2209             HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
    2210         }
    2211         for (testnum = 0; testnum < size_num; testnum++) {
    2212             print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
    2213                           seconds.sym);
    2214             Time_F(START);
    2215             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
    2216             d = Time_F(STOP);
    2217             print_result(D_HMAC, testnum, count, d);
    2218         }
    2219         for (i = 0; i < loopargs_len; i++) {
    2220             HMAC_CTX_free(loopargs[i].hctx);
    2221         }
    2222     }
    2223 #endif
     1904            if (count < 0)
     1905                break;
     1906        }
     1907    }
     1908
    22241909    if (doit[D_SHA1]) {
    22251910        for (testnum = 0; testnum < size_num; testnum++) {
     
    22301915            d = Time_F(STOP);
    22311916            print_result(D_SHA1, testnum, count, d);
    2232         }
    2233     }
     1917            if (count < 0)
     1918                break;
     1919        }
     1920    }
     1921
    22341922    if (doit[D_SHA256]) {
    22351923        for (testnum = 0; testnum < size_num; testnum++) {
     
    22401928            d = Time_F(STOP);
    22411929            print_result(D_SHA256, testnum, count, d);
    2242         }
    2243     }
     1930            if (count < 0)
     1931                break;
     1932        }
     1933    }
     1934
    22441935    if (doit[D_SHA512]) {
    22451936        for (testnum = 0; testnum < size_num; testnum++) {
     
    22501941            d = Time_F(STOP);
    22511942            print_result(D_SHA512, testnum, count, d);
    2252         }
    2253     }
    2254 #ifndef OPENSSL_NO_WHIRLPOOL
     1943            if (count < 0)
     1944                break;
     1945        }
     1946    }
     1947
    22551948    if (doit[D_WHIRLPOOL]) {
    22561949        for (testnum = 0; testnum < size_num; testnum++) {
     
    22611954            d = Time_F(STOP);
    22621955            print_result(D_WHIRLPOOL, testnum, count, d);
    2263         }
    2264     }
    2265 #endif
    2266 
    2267 #ifndef OPENSSL_NO_RMD160
     1956            if (count < 0)
     1957                break;
     1958        }
     1959    }
     1960
    22681961    if (doit[D_RMD160]) {
    22691962        for (testnum = 0; testnum < size_num; testnum++) {
     
    22741967            d = Time_F(STOP);
    22751968            print_result(D_RMD160, testnum, count, d);
    2276         }
    2277     }
    2278 #endif
    2279 #ifndef OPENSSL_NO_RC4
    2280     if (doit[D_RC4]) {
     1969            if (count < 0)
     1970                break;
     1971        }
     1972    }
     1973
     1974    if (doit[D_HMAC]) {
     1975        static const char hmac_key[] = "This is a key...";
     1976        int len = strlen(hmac_key);
     1977        OSSL_PARAM params[3];
     1978
     1979        mac = EVP_MAC_fetch(app_get0_libctx(), "HMAC", app_get0_propq());
     1980        if (mac == NULL || evp_mac_mdname == NULL)
     1981            goto end;
     1982
     1983        evp_hmac_name = app_malloc(sizeof("hmac()") + strlen(evp_mac_mdname),
     1984                                   "HMAC name");
     1985        sprintf(evp_hmac_name, "hmac(%s)", evp_mac_mdname);
     1986        names[D_HMAC] = evp_hmac_name;
     1987
     1988        params[0] =
     1989            OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
     1990                                             evp_mac_mdname, 0);
     1991        params[1] =
     1992            OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
     1993                                              (char *)hmac_key, len);
     1994        params[2] = OSSL_PARAM_construct_end();
     1995
     1996        for (i = 0; i < loopargs_len; i++) {
     1997            loopargs[i].mctx = EVP_MAC_CTX_new(mac);
     1998            if (loopargs[i].mctx == NULL)
     1999                goto end;
     2000
     2001            if (!EVP_MAC_CTX_set_params(loopargs[i].mctx, params))
     2002                goto end;
     2003        }
    22812004        for (testnum = 0; testnum < size_num; testnum++) {
    2282             print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
     2005            print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
    22832006                          seconds.sym);
    22842007            Time_F(START);
    2285             count = run_benchmark(async_jobs, RC4_loop, loopargs);
     2008            count = run_benchmark(async_jobs, HMAC_loop, loopargs);
    22862009            d = Time_F(STOP);
    2287             print_result(D_RC4, testnum, count, d);
    2288         }
    2289     }
    2290 #endif
    2291 #ifndef OPENSSL_NO_DES
     2010            print_result(D_HMAC, testnum, count, d);
     2011            if (count < 0)
     2012                break;
     2013        }
     2014        for (i = 0; i < loopargs_len; i++)
     2015            EVP_MAC_CTX_free(loopargs[i].mctx);
     2016        EVP_MAC_free(mac);
     2017        mac = NULL;
     2018    }
     2019
    22922020    if (doit[D_CBC_DES]) {
    2293         for (testnum = 0; testnum < size_num; testnum++) {
     2021        int st = 1;
     2022
     2023        for (i = 0; st && i < loopargs_len; i++) {
     2024            loopargs[i].ctx = init_evp_cipher_ctx("des-cbc", deskey,
     2025                                                  sizeof(deskey) / 3);
     2026            st = loopargs[i].ctx != NULL;
     2027        }
     2028        algindex = D_CBC_DES;
     2029        for (testnum = 0; st && testnum < size_num; testnum++) {
    22942030            print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
    22952031                          lengths[testnum], seconds.sym);
    22962032            Time_F(START);
    2297             count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
     2033            count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
    22982034            d = Time_F(STOP);
    22992035            print_result(D_CBC_DES, testnum, count, d);
    23002036        }
     2037        for (i = 0; i < loopargs_len; i++)
     2038            EVP_CIPHER_CTX_free(loopargs[i].ctx);
    23012039    }
    23022040
    23032041    if (doit[D_EDE3_DES]) {
    2304         for (testnum = 0; testnum < size_num; testnum++) {
     2042        int st = 1;
     2043
     2044        for (i = 0; st && i < loopargs_len; i++) {
     2045            loopargs[i].ctx = init_evp_cipher_ctx("des-ede3-cbc", deskey,
     2046                                                  sizeof(deskey));
     2047            st = loopargs[i].ctx != NULL;
     2048        }
     2049        algindex = D_EDE3_DES;
     2050        for (testnum = 0; st && testnum < size_num; testnum++) {
    23052051            print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
    23062052                          lengths[testnum], seconds.sym);
    23072053            Time_F(START);
    23082054            count =
    2309                 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
     2055                run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
    23102056            d = Time_F(STOP);
    23112057            print_result(D_EDE3_DES, testnum, count, d);
    23122058        }
    2313     }
    2314 #endif
    2315 
    2316     if (doit[D_CBC_128_AES]) {
     2059        for (i = 0; i < loopargs_len; i++)
     2060            EVP_CIPHER_CTX_free(loopargs[i].ctx);
     2061    }
     2062
     2063    for (k = 0; k < 3; k++) {
     2064        algindex = D_CBC_128_AES + k;
     2065        if (doit[algindex]) {
     2066            int st = 1;
     2067
     2068            keylen = 16 + k * 8;
     2069            for (i = 0; st && i < loopargs_len; i++) {
     2070                loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
     2071                                                      key32, keylen);
     2072                st = loopargs[i].ctx != NULL;
     2073            }
     2074
     2075            for (testnum = 0; st && testnum < size_num; testnum++) {
     2076                print_message(names[algindex], c[algindex][testnum],
     2077                              lengths[testnum], seconds.sym);
     2078                Time_F(START);
     2079                count =
     2080                    run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
     2081                d = Time_F(STOP);
     2082                print_result(algindex, testnum, count, d);
     2083            }
     2084            for (i = 0; i < loopargs_len; i++)
     2085                EVP_CIPHER_CTX_free(loopargs[i].ctx);
     2086        }
     2087    }
     2088
     2089    for (k = 0; k < 3; k++) {
     2090        algindex = D_CBC_128_CML + k;
     2091        if (doit[algindex]) {
     2092            int st = 1;
     2093
     2094            keylen = 16 + k * 8;
     2095            for (i = 0; st && i < loopargs_len; i++) {
     2096                loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
     2097                                                      key32, keylen);
     2098                st = loopargs[i].ctx != NULL;
     2099            }
     2100
     2101            for (testnum = 0; st && testnum < size_num; testnum++) {
     2102                print_message(names[algindex], c[algindex][testnum],
     2103                              lengths[testnum], seconds.sym);
     2104                Time_F(START);
     2105                count =
     2106                    run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
     2107                d = Time_F(STOP);
     2108                print_result(algindex, testnum, count, d);
     2109            }
     2110            for (i = 0; i < loopargs_len; i++)
     2111                EVP_CIPHER_CTX_free(loopargs[i].ctx);
     2112        }
     2113    }
     2114
     2115    for (algindex = D_RC4; algindex <= D_CBC_CAST; algindex++) {
     2116        if (doit[algindex]) {
     2117            int st = 1;
     2118
     2119            keylen = 16;
     2120            for (i = 0; st && i < loopargs_len; i++) {
     2121                loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
     2122                                                      key32, keylen);
     2123                st = loopargs[i].ctx != NULL;
     2124            }
     2125
     2126            for (testnum = 0; st && testnum < size_num; testnum++) {
     2127                print_message(names[algindex], c[algindex][testnum],
     2128                              lengths[testnum], seconds.sym);
     2129                Time_F(START);
     2130                count =
     2131                    run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
     2132                d = Time_F(STOP);
     2133                print_result(algindex, testnum, count, d);
     2134            }
     2135            for (i = 0; i < loopargs_len; i++)
     2136                EVP_CIPHER_CTX_free(loopargs[i].ctx);
     2137        }
     2138    }
     2139    if (doit[D_GHASH]) {
     2140        static const char gmac_iv[] = "0123456789ab";
     2141        OSSL_PARAM params[3];
     2142
     2143        mac = EVP_MAC_fetch(app_get0_libctx(), "GMAC", app_get0_propq());
     2144        if (mac == NULL)
     2145            goto end;
     2146
     2147        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
     2148                                                     "aes-128-gcm", 0);
     2149        params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
     2150                                                      (char *)gmac_iv,
     2151                                                      sizeof(gmac_iv) - 1);
     2152        params[2] = OSSL_PARAM_construct_end();
     2153
     2154        for (i = 0; i < loopargs_len; i++) {
     2155            loopargs[i].mctx = EVP_MAC_CTX_new(mac);
     2156            if (loopargs[i].mctx == NULL)
     2157                goto end;
     2158
     2159            if (!EVP_MAC_init(loopargs[i].mctx, key32, 16, params))
     2160                goto end;
     2161        }
    23172162        for (testnum = 0; testnum < size_num; testnum++) {
    2318             print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
    2319                           lengths[testnum], seconds.sym);
     2163            print_message(names[D_GHASH], c[D_GHASH][testnum], lengths[testnum],
     2164                          seconds.sym);
    23202165            Time_F(START);
    2321             count =
    2322                 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
    2323             d = Time_F(STOP);
    2324             print_result(D_CBC_128_AES, testnum, count, d);
    2325         }
    2326     }
    2327     if (doit[D_CBC_192_AES]) {
    2328         for (testnum = 0; testnum < size_num; testnum++) {
    2329             print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
    2330                           lengths[testnum], seconds.sym);
    2331             Time_F(START);
    2332             count =
    2333                 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
    2334             d = Time_F(STOP);
    2335             print_result(D_CBC_192_AES, testnum, count, d);
    2336         }
    2337     }
    2338     if (doit[D_CBC_256_AES]) {
    2339         for (testnum = 0; testnum < size_num; testnum++) {
    2340             print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
    2341                           lengths[testnum], seconds.sym);
    2342             Time_F(START);
    2343             count =
    2344                 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
    2345             d = Time_F(STOP);
    2346             print_result(D_CBC_256_AES, testnum, count, d);
    2347         }
    2348     }
    2349 
    2350     if (doit[D_IGE_128_AES]) {
    2351         for (testnum = 0; testnum < size_num; testnum++) {
    2352             print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
    2353                           lengths[testnum], seconds.sym);
    2354             Time_F(START);
    2355             count =
    2356                 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
    2357             d = Time_F(STOP);
    2358             print_result(D_IGE_128_AES, testnum, count, d);
    2359         }
    2360     }
    2361     if (doit[D_IGE_192_AES]) {
    2362         for (testnum = 0; testnum < size_num; testnum++) {
    2363             print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
    2364                           lengths[testnum], seconds.sym);
    2365             Time_F(START);
    2366             count =
    2367                 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
    2368             d = Time_F(STOP);
    2369             print_result(D_IGE_192_AES, testnum, count, d);
    2370         }
    2371     }
    2372     if (doit[D_IGE_256_AES]) {
    2373         for (testnum = 0; testnum < size_num; testnum++) {
    2374             print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
    2375                           lengths[testnum], seconds.sym);
    2376             Time_F(START);
    2377             count =
    2378                 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
    2379             d = Time_F(STOP);
    2380             print_result(D_IGE_256_AES, testnum, count, d);
    2381         }
    2382     }
    2383     if (doit[D_GHASH]) {
    2384         for (i = 0; i < loopargs_len; i++) {
    2385             loopargs[i].gcm_ctx =
    2386                 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
    2387             CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
    2388                                 (unsigned char *)"0123456789ab", 12);
    2389         }
    2390 
    2391         for (testnum = 0; testnum < size_num; testnum++) {
    2392             print_message(names[D_GHASH], c[D_GHASH][testnum],
    2393                           lengths[testnum], seconds.sym);
    2394             Time_F(START);
    2395             count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
     2166            count = run_benchmark(async_jobs, GHASH_loop, loopargs);
    23962167            d = Time_F(STOP);
    23972168            print_result(D_GHASH, testnum, count, d);
     2169            if (count < 0)
     2170                break;
    23982171        }
    23992172        for (i = 0; i < loopargs_len; i++)
    2400             CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
    2401     }
    2402 #ifndef OPENSSL_NO_CAMELLIA
    2403     if (doit[D_CBC_128_CML]) {
    2404         if (async_jobs > 0) {
    2405             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2406                        names[D_CBC_128_CML]);
    2407             doit[D_CBC_128_CML] = 0;
    2408         }
    2409         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2410             print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
    2411                           lengths[testnum], seconds.sym);
    2412             Time_F(START);
    2413             for (count = 0; COND(c[D_CBC_128_CML][testnum]); count++)
    2414                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2415                                      (size_t)lengths[testnum], &camellia_ks1,
    2416                                      iv, CAMELLIA_ENCRYPT);
    2417             d = Time_F(STOP);
    2418             print_result(D_CBC_128_CML, testnum, count, d);
    2419         }
    2420     }
    2421     if (doit[D_CBC_192_CML]) {
    2422         if (async_jobs > 0) {
    2423             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2424                        names[D_CBC_192_CML]);
    2425             doit[D_CBC_192_CML] = 0;
    2426         }
    2427         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2428             print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
    2429                           lengths[testnum], seconds.sym);
    2430             if (async_jobs > 0) {
    2431                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
    2432                 exit(1);
    2433             }
    2434             Time_F(START);
    2435             for (count = 0; COND(c[D_CBC_192_CML][testnum]); count++)
    2436                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2437                                      (size_t)lengths[testnum], &camellia_ks2,
    2438                                      iv, CAMELLIA_ENCRYPT);
    2439             d = Time_F(STOP);
    2440             print_result(D_CBC_192_CML, testnum, count, d);
    2441         }
    2442     }
    2443     if (doit[D_CBC_256_CML]) {
    2444         if (async_jobs > 0) {
    2445             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2446                        names[D_CBC_256_CML]);
    2447             doit[D_CBC_256_CML] = 0;
    2448         }
    2449         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2450             print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
    2451                           lengths[testnum], seconds.sym);
    2452             Time_F(START);
    2453             for (count = 0; COND(c[D_CBC_256_CML][testnum]); count++)
    2454                 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2455                                      (size_t)lengths[testnum], &camellia_ks3,
    2456                                      iv, CAMELLIA_ENCRYPT);
    2457             d = Time_F(STOP);
    2458             print_result(D_CBC_256_CML, testnum, count, d);
    2459         }
    2460     }
    2461 #endif
    2462 #ifndef OPENSSL_NO_IDEA
    2463     if (doit[D_CBC_IDEA]) {
    2464         if (async_jobs > 0) {
    2465             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2466                        names[D_CBC_IDEA]);
    2467             doit[D_CBC_IDEA] = 0;
    2468         }
    2469         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2470             print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
    2471                           lengths[testnum], seconds.sym);
    2472             Time_F(START);
    2473             for (count = 0; COND(c[D_CBC_IDEA][testnum]); count++)
    2474                 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2475                                  (size_t)lengths[testnum], &idea_ks,
    2476                                  iv, IDEA_ENCRYPT);
    2477             d = Time_F(STOP);
    2478             print_result(D_CBC_IDEA, testnum, count, d);
    2479         }
    2480     }
    2481 #endif
    2482 #ifndef OPENSSL_NO_SEED
    2483     if (doit[D_CBC_SEED]) {
    2484         if (async_jobs > 0) {
    2485             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2486                        names[D_CBC_SEED]);
    2487             doit[D_CBC_SEED] = 0;
    2488         }
    2489         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2490             print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
    2491                           lengths[testnum], seconds.sym);
    2492             Time_F(START);
    2493             for (count = 0; COND(c[D_CBC_SEED][testnum]); count++)
    2494                 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2495                                  (size_t)lengths[testnum], &seed_ks, iv, 1);
    2496             d = Time_F(STOP);
    2497             print_result(D_CBC_SEED, testnum, count, d);
    2498         }
    2499     }
    2500 #endif
    2501 #ifndef OPENSSL_NO_RC2
    2502     if (doit[D_CBC_RC2]) {
    2503         if (async_jobs > 0) {
    2504             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2505                        names[D_CBC_RC2]);
    2506             doit[D_CBC_RC2] = 0;
    2507         }
    2508         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2509             print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
    2510                           lengths[testnum], seconds.sym);
    2511             if (async_jobs > 0) {
    2512                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
    2513                 exit(1);
    2514             }
    2515             Time_F(START);
    2516             for (count = 0; COND(c[D_CBC_RC2][testnum]); count++)
    2517                 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2518                                 (size_t)lengths[testnum], &rc2_ks,
    2519                                 iv, RC2_ENCRYPT);
    2520             d = Time_F(STOP);
    2521             print_result(D_CBC_RC2, testnum, count, d);
    2522         }
    2523     }
    2524 #endif
    2525 #ifndef OPENSSL_NO_RC5
    2526     if (doit[D_CBC_RC5]) {
    2527         if (async_jobs > 0) {
    2528             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2529                        names[D_CBC_RC5]);
    2530             doit[D_CBC_RC5] = 0;
    2531         }
    2532         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2533             print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
    2534                           lengths[testnum], seconds.sym);
    2535             if (async_jobs > 0) {
    2536                 BIO_printf(bio_err, "Async mode is not supported, exiting...");
    2537                 exit(1);
    2538             }
    2539             Time_F(START);
    2540             for (count = 0; COND(c[D_CBC_RC5][testnum]); count++)
    2541                 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2542                                    (size_t)lengths[testnum], &rc5_ks,
    2543                                    iv, RC5_ENCRYPT);
    2544             d = Time_F(STOP);
    2545             print_result(D_CBC_RC5, testnum, count, d);
    2546         }
    2547     }
    2548 #endif
    2549 #ifndef OPENSSL_NO_BF
    2550     if (doit[D_CBC_BF]) {
    2551         if (async_jobs > 0) {
    2552             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2553                        names[D_CBC_BF]);
    2554             doit[D_CBC_BF] = 0;
    2555         }
    2556         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2557             print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
    2558                           lengths[testnum], seconds.sym);
    2559             Time_F(START);
    2560             for (count = 0; COND(c[D_CBC_BF][testnum]); count++)
    2561                 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2562                                (size_t)lengths[testnum], &bf_ks,
    2563                                iv, BF_ENCRYPT);
    2564             d = Time_F(STOP);
    2565             print_result(D_CBC_BF, testnum, count, d);
    2566         }
    2567     }
    2568 #endif
    2569 #ifndef OPENSSL_NO_CAST
    2570     if (doit[D_CBC_CAST]) {
    2571         if (async_jobs > 0) {
    2572             BIO_printf(bio_err, "Async mode is not supported with %s\n",
    2573                        names[D_CBC_CAST]);
    2574             doit[D_CBC_CAST] = 0;
    2575         }
    2576         for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
    2577             print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
    2578                           lengths[testnum], seconds.sym);
    2579             Time_F(START);
    2580             for (count = 0; COND(c[D_CBC_CAST][testnum]); count++)
    2581                 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
    2582                                  (size_t)lengths[testnum], &cast_ks,
    2583                                  iv, CAST_ENCRYPT);
    2584             d = Time_F(STOP);
    2585             print_result(D_CBC_CAST, testnum, count, d);
    2586         }
    2587     }
    2588 #endif
     2173            EVP_MAC_CTX_free(loopargs[i].mctx);
     2174        EVP_MAC_free(mac);
     2175        mac = NULL;
     2176    }
     2177
    25892178    if (doit[D_RAND]) {
    25902179        for (testnum = 0; testnum < size_num; testnum++) {
     
    26002189    if (doit[D_EVP]) {
    26012190        if (evp_cipher != NULL) {
    2602             int (*loopfunc)(void *args) = EVP_Update_loop;
    2603 
    2604             if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
     2191            int (*loopfunc) (void *) = EVP_Update_loop;
     2192
     2193            if (multiblock && (EVP_CIPHER_get_flags(evp_cipher) &
    26052194                               EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
    26062195                multiblock_speed(evp_cipher, lengths_single, &seconds);
     
    26092198            }
    26102199
    2611             names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
    2612 
    2613             if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
     2200            names[D_EVP] = EVP_CIPHER_get0_name(evp_cipher);
     2201
     2202            if (EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
    26142203                loopfunc = EVP_Update_loop_ccm;
    2615             } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
     2204            } else if (aead && (EVP_CIPHER_get_flags(evp_cipher) &
    26162205                                EVP_CIPH_FLAG_AEAD_CIPHER)) {
    26172206                loopfunc = EVP_Update_loop_aead;
     
    26232212
    26242213            for (testnum = 0; testnum < size_num; testnum++) {
    2625                 print_message(names[D_EVP], save_count, lengths[testnum],
     2214                print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
    26262215                              seconds.sym);
    26272216
     
    26412230                    EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
    26422231
    2643                     keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
     2232                    keylen = EVP_CIPHER_CTX_get_key_length(loopargs[k].ctx);
    26442233                    loopargs[k].key = app_malloc(keylen, "evp_cipher key");
    26452234                    EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
     
    26512240                    }
    26522241                    OPENSSL_clear_free(loopargs[k].key, keylen);
     2242
     2243                    /* SIV mode only allows for a single Update operation */
     2244                    if (EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
     2245                        (void)EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
     2246                                                  EVP_CTRL_SET_SPEED, 1, NULL);
    26532247                }
    26542248
     
    26562250                count = run_benchmark(async_jobs, loopfunc, loopargs);
    26572251                d = Time_F(STOP);
    2658                 for (k = 0; k < loopargs_len; k++) {
     2252                for (k = 0; k < loopargs_len; k++)
    26592253                    EVP_CIPHER_CTX_free(loopargs[k].ctx);
    2660                 }
    26612254                print_result(D_EVP, testnum, count, d);
    26622255            }
    2663         } else if (evp_md != NULL) {
    2664             names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
     2256        } else if (evp_md_name != NULL) {
     2257            names[D_EVP] = evp_md_name;
    26652258
    26662259            for (testnum = 0; testnum < size_num; testnum++) {
    2667                 print_message(names[D_EVP], save_count, lengths[testnum],
     2260                print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
    26682261                              seconds.sym);
    26692262                Time_F(START);
    2670                 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
     2263                count = run_benchmark(async_jobs, EVP_Digest_md_loop, loopargs);
    26712264                d = Time_F(STOP);
    26722265                print_result(D_EVP, testnum, count, d);
    2673             }
    2674         }
     2266                if (count < 0)
     2267                    break;
     2268            }
     2269        }
     2270    }
     2271
     2272    if (doit[D_EVP_CMAC]) {
     2273        OSSL_PARAM params[3];
     2274        EVP_CIPHER *cipher = NULL;
     2275
     2276        mac = EVP_MAC_fetch(app_get0_libctx(), "CMAC", app_get0_propq());
     2277        if (mac == NULL || evp_mac_ciphername == NULL)
     2278            goto end;
     2279        if (!opt_cipher(evp_mac_ciphername, &cipher))
     2280            goto end;
     2281
     2282        keylen = EVP_CIPHER_get_key_length(cipher);
     2283        EVP_CIPHER_free(cipher);
     2284        if (keylen <= 0 || keylen > (int)sizeof(key32)) {
     2285            BIO_printf(bio_err, "\nRequested CMAC cipher with unsupported key length.\n");
     2286            goto end;
     2287        }
     2288        evp_cmac_name = app_malloc(sizeof("cmac()")
     2289                                   + strlen(evp_mac_ciphername), "CMAC name");
     2290        sprintf(evp_cmac_name, "cmac(%s)", evp_mac_ciphername);
     2291        names[D_EVP_CMAC] = evp_cmac_name;
     2292
     2293        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
     2294                                                     evp_mac_ciphername, 0);
     2295        params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
     2296                                                      (char *)key32, keylen);
     2297        params[2] = OSSL_PARAM_construct_end();
     2298
     2299        for (i = 0; i < loopargs_len; i++) {
     2300            loopargs[i].mctx = EVP_MAC_CTX_new(mac);
     2301            if (loopargs[i].mctx == NULL)
     2302                goto end;
     2303
     2304            if (!EVP_MAC_CTX_set_params(loopargs[i].mctx, params))
     2305                goto end;
     2306        }
     2307
     2308        for (testnum = 0; testnum < size_num; testnum++) {
     2309            print_message(names[D_EVP_CMAC], c[D_EVP_CMAC][testnum],
     2310                          lengths[testnum], seconds.sym);
     2311            Time_F(START);
     2312            count = run_benchmark(async_jobs, CMAC_loop, loopargs);
     2313            d = Time_F(STOP);
     2314            print_result(D_EVP_CMAC, testnum, count, d);
     2315            if (count < 0)
     2316                break;
     2317        }
     2318        for (i = 0; i < loopargs_len; i++)
     2319            EVP_MAC_CTX_free(loopargs[i].mctx);
     2320        EVP_MAC_free(mac);
     2321        mac = NULL;
    26752322    }
    26762323
     
    26792326            goto end;
    26802327
    2681 #ifndef OPENSSL_NO_RSA
    26822328    for (testnum = 0; testnum < RSA_NUM; testnum++) {
     2329        EVP_PKEY *rsa_key = NULL;
    26832330        int st = 0;
     2331
    26842332        if (!rsa_doit[testnum])
    26852333            continue;
    2686         for (i = 0; i < loopargs_len; i++) {
    2687             if (primes > 2) {
    2688                 /* we haven't set keys yet,  generate multi-prime RSA keys */
    2689                 BIGNUM *bn = BN_new();
    2690 
    2691                 if (bn == NULL)
    2692                     goto end;
    2693                 if (!BN_set_word(bn, RSA_F4)) {
    2694                     BN_free(bn);
    2695                     goto end;
    2696                 }
    2697 
    2698                 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
    2699                            rsa_choices[testnum].name);
    2700 
    2701                 loopargs[i].rsa_key[testnum] = RSA_new();
    2702                 if (loopargs[i].rsa_key[testnum] == NULL) {
    2703                     BN_free(bn);
    2704                     goto end;
    2705                 }
    2706 
    2707                 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
    2708                                                   rsa_bits[testnum],
    2709                                                   primes, bn, NULL)) {
    2710                     BN_free(bn);
    2711                     goto end;
    2712                 }
    2713                 BN_free(bn);
    2714             }
    2715             st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
    2716                           &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
    2717             if (st == 0)
    2718                 break;
    2719         }
    2720         if (st == 0) {
     2334
     2335        if (primes > RSA_DEFAULT_PRIME_NUM) {
     2336            /* we haven't set keys yet,  generate multi-prime RSA keys */
     2337            bn = BN_new();
     2338            st = bn != NULL
     2339                && BN_set_word(bn, RSA_F4)
     2340                && init_gen_str(&genctx, "RSA", NULL, 0, NULL, NULL)
     2341                && EVP_PKEY_CTX_set_rsa_keygen_bits(genctx, rsa_keys[testnum].bits) > 0
     2342                && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(genctx, bn) > 0
     2343                && EVP_PKEY_CTX_set_rsa_keygen_primes(genctx, primes) > 0
     2344                && EVP_PKEY_keygen(genctx, &rsa_key);
     2345            BN_free(bn);
     2346            bn = NULL;
     2347            EVP_PKEY_CTX_free(genctx);
     2348            genctx = NULL;
     2349        } else {
     2350            const unsigned char *p = rsa_keys[testnum].data;
     2351
     2352            st = (rsa_key = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p,
     2353                                           rsa_keys[testnum].length)) != NULL;
     2354        }
     2355
     2356        for (i = 0; st && i < loopargs_len; i++) {
     2357            loopargs[i].rsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
     2358            loopargs[i].sigsize = loopargs[i].buflen;
     2359            if (loopargs[i].rsa_sign_ctx[testnum] == NULL
     2360                || EVP_PKEY_sign_init(loopargs[i].rsa_sign_ctx[testnum]) <= 0
     2361                || EVP_PKEY_sign(loopargs[i].rsa_sign_ctx[testnum],
     2362                                 loopargs[i].buf2,
     2363                                 &loopargs[i].sigsize,
     2364                                 loopargs[i].buf, 36) <= 0)
     2365                st = 0;
     2366        }
     2367        if (!st) {
    27212368            BIO_printf(bio_err,
    2722                        "RSA sign failure.  No RSA sign will be done.\n");
     2369                       "RSA sign setup failure.  No RSA sign will be done.\n");
    27232370            ERR_print_errors(bio_err);
    2724             rsa_count = 1;
     2371            op_count = 1;
    27252372        } else {
    27262373            pkey_print_message("private", "rsa",
    2727                                rsa_c[testnum][0], rsa_bits[testnum],
     2374                               rsa_c[testnum][0], rsa_keys[testnum].bits,
    27282375                               seconds.rsa);
    27292376            /* RSA_blinding_on(rsa_key[testnum],NULL); */
     
    27342381                       mr ? "+R1:%ld:%d:%.2f\n"
    27352382                       : "%ld %u bits private RSA's in %.2fs\n",
    2736                        count, rsa_bits[testnum], d);
     2383                       count, rsa_keys[testnum].bits, d);
    27372384            rsa_results[testnum][0] = (double)count / d;
    2738             rsa_count = count;
    2739         }
    2740 
    2741         for (i = 0; i < loopargs_len; i++) {
    2742             st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
    2743                             loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
    2744             if (st <= 0)
    2745                 break;
    2746         }
    2747         if (st <= 0) {
     2385            op_count = count;
     2386        }
     2387
     2388        for (i = 0; st && i < loopargs_len; i++) {
     2389            loopargs[i].rsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key,
     2390                                                                   NULL);
     2391            if (loopargs[i].rsa_verify_ctx[testnum] == NULL
     2392                || EVP_PKEY_verify_init(loopargs[i].rsa_verify_ctx[testnum]) <= 0
     2393                || EVP_PKEY_verify(loopargs[i].rsa_verify_ctx[testnum],
     2394                                   loopargs[i].buf2,
     2395                                   loopargs[i].sigsize,
     2396                                   loopargs[i].buf, 36) <= 0)
     2397                st = 0;
     2398        }
     2399        if (!st) {
    27482400            BIO_printf(bio_err,
    2749                        "RSA verify failure.  No RSA verify will be done.\n");
     2401                       "RSA verify setup failure.  No RSA verify will be done.\n");
    27502402            ERR_print_errors(bio_err);
    27512403            rsa_doit[testnum] = 0;
    27522404        } else {
    27532405            pkey_print_message("public", "rsa",
    2754                                rsa_c[testnum][1], rsa_bits[testnum],
     2406                               rsa_c[testnum][1], rsa_keys[testnum].bits,
    27552407                               seconds.rsa);
    27562408            Time_F(START);
     
    27602412                       mr ? "+R2:%ld:%d:%.2f\n"
    27612413                       : "%ld %u bits public RSA's in %.2fs\n",
    2762                        count, rsa_bits[testnum], d);
     2414                       count, rsa_keys[testnum].bits, d);
    27632415            rsa_results[testnum][1] = (double)count / d;
    27642416        }
    27652417
    2766         if (rsa_count <= 1) {
     2418        if (op_count <= 1) {
    27672419            /* if longer than 10s, don't do any more */
    2768             for (testnum++; testnum < RSA_NUM; testnum++)
    2769                 rsa_doit[testnum] = 0;
    2770         }
    2771     }
    2772 #endif                          /* OPENSSL_NO_RSA */
    2773 
    2774     for (i = 0; i < loopargs_len; i++)
    2775         if (RAND_bytes(loopargs[i].buf, 36) <= 0)
    2776             goto end;
    2777 
    2778 #ifndef OPENSSL_NO_DSA
     2420            stop_it(rsa_doit, testnum);
     2421        }
     2422        EVP_PKEY_free(rsa_key);
     2423    }
     2424
    27792425    for (testnum = 0; testnum < DSA_NUM; testnum++) {
    2780         int st = 0;
     2426        EVP_PKEY *dsa_key = NULL;
     2427        int st;
     2428
    27812429        if (!dsa_doit[testnum])
    27822430            continue;
    27832431
    2784         /* DSA_generate_key(dsa_key[testnum]); */
    2785         /* DSA_sign_setup(dsa_key[testnum],NULL); */
    2786         for (i = 0; i < loopargs_len; i++) {
    2787             st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
    2788                           &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
    2789             if (st == 0)
    2790                 break;
    2791         }
    2792         if (st == 0) {
     2432        st = (dsa_key = get_dsa(dsa_bits[testnum])) != NULL;
     2433
     2434        for (i = 0; st && i < loopargs_len; i++) {
     2435            loopargs[i].dsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
     2436                                                                 NULL);
     2437            loopargs[i].sigsize = loopargs[i].buflen;
     2438            if (loopargs[i].dsa_sign_ctx[testnum] == NULL
     2439                || EVP_PKEY_sign_init(loopargs[i].dsa_sign_ctx[testnum]) <= 0
     2440
     2441                || EVP_PKEY_sign(loopargs[i].dsa_sign_ctx[testnum],
     2442                                 loopargs[i].buf2,
     2443                                 &loopargs[i].sigsize,
     2444                                 loopargs[i].buf, 20) <= 0)
     2445                st = 0;
     2446        }
     2447        if (!st) {
    27932448            BIO_printf(bio_err,
    2794                        "DSA sign failure.  No DSA sign will be done.\n");
     2449                       "DSA sign setup failure.  No DSA sign will be done.\n");
    27952450            ERR_print_errors(bio_err);
    2796             rsa_count = 1;
     2451            op_count = 1;
    27972452        } else {
    27982453            pkey_print_message("sign", "dsa",
     
    28072462                       count, dsa_bits[testnum], d);
    28082463            dsa_results[testnum][0] = (double)count / d;
    2809             rsa_count = count;
    2810         }
    2811 
    2812         for (i = 0; i < loopargs_len; i++) {
    2813             st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
    2814                             loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
    2815             if (st <= 0)
    2816                 break;
    2817         }
    2818         if (st <= 0) {
     2464            op_count = count;
     2465        }
     2466
     2467        for (i = 0; st && i < loopargs_len; i++) {
     2468            loopargs[i].dsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
     2469                                                                   NULL);
     2470            if (loopargs[i].dsa_verify_ctx[testnum] == NULL
     2471                || EVP_PKEY_verify_init(loopargs[i].dsa_verify_ctx[testnum]) <= 0
     2472                || EVP_PKEY_verify(loopargs[i].dsa_verify_ctx[testnum],
     2473                                   loopargs[i].buf2,
     2474                                   loopargs[i].sigsize,
     2475                                   loopargs[i].buf, 36) <= 0)
     2476                st = 0;
     2477        }
     2478        if (!st) {
    28192479            BIO_printf(bio_err,
    2820                        "DSA verify failure.  No DSA verify will be done.\n");
     2480                       "DSA verify setup failure.  No DSA verify will be done.\n");
    28212481            ERR_print_errors(bio_err);
    28222482            dsa_doit[testnum] = 0;
     
    28352495        }
    28362496
    2837         if (rsa_count <= 1) {
     2497        if (op_count <= 1) {
    28382498            /* if longer than 10s, don't do any more */
    2839             for (testnum++; testnum < DSA_NUM; testnum++)
    2840                 dsa_doit[testnum] = 0;
    2841         }
    2842     }
    2843 #endif                          /* OPENSSL_NO_DSA */
    2844 
    2845 #ifndef OPENSSL_NO_EC
     2499            stop_it(dsa_doit, testnum);
     2500        }
     2501        EVP_PKEY_free(dsa_key);
     2502    }
     2503
    28462504    for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
    2847         int st = 1;
     2505        EVP_PKEY *ecdsa_key = NULL;
     2506        int st;
    28482507
    28492508        if (!ecdsa_doit[testnum])
    2850             continue;           /* Ignore Curve */
    2851         for (i = 0; i < loopargs_len; i++) {
    2852             loopargs[i].ecdsa[testnum] =
    2853                 EC_KEY_new_by_curve_name(test_curves[testnum].nid);
    2854             if (loopargs[i].ecdsa[testnum] == NULL) {
     2509            continue;
     2510
     2511        st = (ecdsa_key = get_ecdsa(&ec_curves[testnum])) != NULL;
     2512
     2513        for (i = 0; st && i < loopargs_len; i++) {
     2514            loopargs[i].ecdsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
     2515                                                                   NULL);
     2516            loopargs[i].sigsize = loopargs[i].buflen;
     2517            if (loopargs[i].ecdsa_sign_ctx[testnum] == NULL
     2518                || EVP_PKEY_sign_init(loopargs[i].ecdsa_sign_ctx[testnum]) <= 0
     2519
     2520                || EVP_PKEY_sign(loopargs[i].ecdsa_sign_ctx[testnum],
     2521                                 loopargs[i].buf2,
     2522                                 &loopargs[i].sigsize,
     2523                                 loopargs[i].buf, 20) <= 0)
    28552524                st = 0;
    2856                 break;
    2857             }
    2858         }
    2859         if (st == 0) {
    2860             BIO_printf(bio_err, "ECDSA failure.\n");
     2525        }
     2526        if (!st) {
     2527            BIO_printf(bio_err,
     2528                       "ECDSA sign setup failure.  No ECDSA sign will be done.\n");
    28612529            ERR_print_errors(bio_err);
    2862             rsa_count = 1;
     2530            op_count = 1;
    28632531        } else {
    2864             for (i = 0; i < loopargs_len; i++) {
    2865                 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
    2866                 /* Perform ECDSA signature test */
    2867                 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
    2868                 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
    2869                                 &loopargs[i].siglen,
    2870                                 loopargs[i].ecdsa[testnum]);
    2871                 if (st == 0)
    2872                     break;
    2873             }
    2874             if (st == 0) {
    2875                 BIO_printf(bio_err,
    2876                            "ECDSA sign failure.  No ECDSA sign will be done.\n");
    2877                 ERR_print_errors(bio_err);
    2878                 rsa_count = 1;
    2879             } else {
    2880                 pkey_print_message("sign", "ecdsa",
    2881                                    ecdsa_c[testnum][0],
    2882                                    test_curves[testnum].bits, seconds.ecdsa);
    2883                 Time_F(START);
    2884                 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
    2885                 d = Time_F(STOP);
    2886 
    2887                 BIO_printf(bio_err,
    2888                            mr ? "+R5:%ld:%u:%.2f\n" :
    2889                            "%ld %u bits ECDSA signs in %.2fs \n",
    2890                            count, test_curves[testnum].bits, d);
    2891                 ecdsa_results[testnum][0] = (double)count / d;
    2892                 rsa_count = count;
    2893             }
    2894 
    2895             /* Perform ECDSA verification test */
    2896             for (i = 0; i < loopargs_len; i++) {
    2897                 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
    2898                                   loopargs[i].siglen,
    2899                                   loopargs[i].ecdsa[testnum]);
    2900                 if (st != 1)
    2901                     break;
    2902             }
    2903             if (st != 1) {
    2904                 BIO_printf(bio_err,
    2905                            "ECDSA verify failure.  No ECDSA verify will be done.\n");
    2906                 ERR_print_errors(bio_err);
    2907                 ecdsa_doit[testnum] = 0;
    2908             } else {
    2909                 pkey_print_message("verify", "ecdsa",
    2910                                    ecdsa_c[testnum][1],
    2911                                    test_curves[testnum].bits, seconds.ecdsa);
    2912                 Time_F(START);
    2913                 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
    2914                 d = Time_F(STOP);
    2915                 BIO_printf(bio_err,
    2916                            mr ? "+R6:%ld:%u:%.2f\n"
    2917                            : "%ld %u bits ECDSA verify in %.2fs\n",
    2918                            count, test_curves[testnum].bits, d);
    2919                 ecdsa_results[testnum][1] = (double)count / d;
    2920             }
    2921 
    2922             if (rsa_count <= 1) {
    2923                 /* if longer than 10s, don't do any more */
    2924                 for (testnum++; testnum < ECDSA_NUM; testnum++)
    2925                     ecdsa_doit[testnum] = 0;
    2926             }
     2532            pkey_print_message("sign", "ecdsa",
     2533                               ecdsa_c[testnum][0], ec_curves[testnum].bits,
     2534                               seconds.ecdsa);
     2535            Time_F(START);
     2536            count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
     2537            d = Time_F(STOP);
     2538            BIO_printf(bio_err,
     2539                       mr ? "+R5:%ld:%u:%.2f\n"
     2540                       : "%ld %u bits ECDSA signs in %.2fs\n",
     2541                       count, ec_curves[testnum].bits, d);
     2542            ecdsa_results[testnum][0] = (double)count / d;
     2543            op_count = count;
     2544        }
     2545
     2546        for (i = 0; st && i < loopargs_len; i++) {
     2547            loopargs[i].ecdsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
     2548                                                                     NULL);
     2549            if (loopargs[i].ecdsa_verify_ctx[testnum] == NULL
     2550                || EVP_PKEY_verify_init(loopargs[i].ecdsa_verify_ctx[testnum]) <= 0
     2551                || EVP_PKEY_verify(loopargs[i].ecdsa_verify_ctx[testnum],
     2552                                   loopargs[i].buf2,
     2553                                   loopargs[i].sigsize,
     2554                                   loopargs[i].buf, 20) <= 0)
     2555                st = 0;
     2556        }
     2557        if (!st) {
     2558            BIO_printf(bio_err,
     2559                       "ECDSA verify setup failure.  No ECDSA verify will be done.\n");
     2560            ERR_print_errors(bio_err);
     2561            ecdsa_doit[testnum] = 0;
     2562        } else {
     2563            pkey_print_message("verify", "ecdsa",
     2564                               ecdsa_c[testnum][1], ec_curves[testnum].bits,
     2565                               seconds.ecdsa);
     2566            Time_F(START);
     2567            count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
     2568            d = Time_F(STOP);
     2569            BIO_printf(bio_err,
     2570                       mr ? "+R6:%ld:%u:%.2f\n"
     2571                       : "%ld %u bits ECDSA verify in %.2fs\n",
     2572                       count, ec_curves[testnum].bits, d);
     2573            ecdsa_results[testnum][1] = (double)count / d;
     2574        }
     2575
     2576        if (op_count <= 1) {
     2577            /* if longer than 10s, don't do any more */
     2578            stop_it(ecdsa_doit, testnum);
    29272579        }
    29282580    }
     
    29352587
    29362588        for (i = 0; i < loopargs_len; i++) {
    2937             EVP_PKEY_CTX *kctx = NULL;
    29382589            EVP_PKEY_CTX *test_ctx = NULL;
    29392590            EVP_PKEY_CTX *ctx = NULL;
     
    29432594            size_t test_outlen;
    29442595
    2945             /* Ensure that the error queue is empty */
    2946             if (ERR_peek_error()) {
    2947                 BIO_printf(bio_err,
    2948                            "WARNING: the error queue contains previous unhandled errors.\n");
    2949                 ERR_print_errors(bio_err);
    2950             }
    2951 
    2952             /* Let's try to create a ctx directly from the NID: this works for
    2953              * curves like Curve25519 that are not implemented through the low
    2954              * level EC interface.
    2955              * If this fails we try creating a EVP_PKEY_EC generic param ctx,
    2956              * then we set the curve by NID before deriving the actual keygen
    2957              * ctx for that specific curve. */
    2958             kctx = EVP_PKEY_CTX_new_id(test_curves[testnum].nid, NULL); /* keygen ctx from NID */
    2959             if (!kctx) {
    2960                 EVP_PKEY_CTX *pctx = NULL;
    2961                 EVP_PKEY *params = NULL;
    2962 
    2963                 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
    2964                  * "int_ctx_new:unsupported algorithm" error was added to the
    2965                  * error queue.
    2966                  * We remove it from the error queue as we are handling it. */
    2967                 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
    2968                 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
    2969                     /* check that the error origin matches */
    2970                     ERR_GET_LIB(error) == ERR_LIB_EVP &&
    2971                     ERR_GET_FUNC(error) == EVP_F_INT_CTX_NEW &&
    2972                     ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
    2973                     ERR_get_error(); /* pop error from queue */
    2974                 if (ERR_peek_error()) {
    2975                     BIO_printf(bio_err,
    2976                                "Unhandled error in the error queue during ECDH init.\n");
    2977                     ERR_print_errors(bio_err);
    2978                     rsa_count = 1;
    2979                     break;
    2980                 }
    2981 
    2982                 if (            /* Create the context for parameter generation */
    2983                        !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
    2984                        /* Initialise the parameter generation */
    2985                        !EVP_PKEY_paramgen_init(pctx) ||
    2986                        /* Set the curve by NID */
    2987                        !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
    2988                                                                test_curves
    2989                                                                [testnum].nid) ||
    2990                        /* Create the parameter object params */
    2991                        !EVP_PKEY_paramgen(pctx, &params)) {
    2992                     ecdh_checks = 0;
    2993                     BIO_printf(bio_err, "ECDH EC params init failure.\n");
    2994                     ERR_print_errors(bio_err);
    2995                     rsa_count = 1;
    2996                     break;
    2997                 }
    2998                 /* Create the context for the key generation */
    2999                 kctx = EVP_PKEY_CTX_new(params, NULL);
    3000 
    3001                 EVP_PKEY_free(params);
    3002                 params = NULL;
    3003                 EVP_PKEY_CTX_free(pctx);
    3004                 pctx = NULL;
    3005             }
    3006             if (kctx == NULL ||      /* keygen ctx is not null */
    3007                 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
    3008                 ecdh_checks = 0;
    3009                 BIO_printf(bio_err, "ECDH keygen failure.\n");
    3010                 ERR_print_errors(bio_err);
    3011                 rsa_count = 1;
    3012                 break;
    3013             }
    3014 
    3015             if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
    3016                 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
    3017                 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
    3018                 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
    3019                 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
    3020                 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
    3021                 outlen == 0 ||  /* ensure outlen is a valid size */
    3022                 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
     2596            if ((key_A = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key A */
     2597                || (key_B = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key B */
     2598                || (ctx = EVP_PKEY_CTX_new(key_A, NULL)) == NULL /* derivation ctx from skeyA */
     2599                || EVP_PKEY_derive_init(ctx) <= 0 /* init derivation ctx */
     2600                || EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 /* set peer pubkey in ctx */
     2601                || EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 /* determine max length */
     2602                || outlen == 0 /* ensure outlen is a valid size */
     2603                || outlen > MAX_ECDH_SIZE /* avoid buffer overflow */) {
    30232604                ecdh_checks = 0;
    30242605                BIO_printf(bio_err, "ECDH key generation failure.\n");
    30252606                ERR_print_errors(bio_err);
    3026                 rsa_count = 1;
    3027                 break;
    3028             }
    3029 
    3030             /* Here we perform a test run, comparing the output of a*B and b*A;
     2607                op_count = 1;
     2608                break;
     2609            }
     2610
     2611            /*
     2612             * Here we perform a test run, comparing the output of a*B and b*A;
    30312613             * we try this here and assume that further EVP_PKEY_derive calls
    30322614             * never fail, so we can skip checks in the actually benchmarked
    3033              * code, for maximum performance. */
    3034             if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
    3035                 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
    3036                 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
    3037                 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
    3038                 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
    3039                 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
    3040                 test_outlen != outlen /* compare output length */ ) {
     2615             * code, for maximum performance.
     2616             */
     2617            if ((test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) == NULL /* test ctx from skeyB */
     2618                || !EVP_PKEY_derive_init(test_ctx) /* init derivation test_ctx */
     2619                || !EVP_PKEY_derive_set_peer(test_ctx, key_A) /* set peer pubkey in test_ctx */
     2620                || !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) /* determine max length */
     2621                || !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) /* compute a*B */
     2622                || !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) /* compute b*A */
     2623                || test_outlen != outlen /* compare output length */) {
    30412624                ecdh_checks = 0;
    30422625                BIO_printf(bio_err, "ECDH computation failure.\n");
    30432626                ERR_print_errors(bio_err);
    3044                 rsa_count = 1;
     2627                op_count = 1;
    30452628                break;
    30462629            }
     
    30522635                BIO_printf(bio_err, "ECDH computations don't match.\n");
    30532636                ERR_print_errors(bio_err);
    3054                 rsa_count = 1;
     2637                op_count = 1;
    30552638                break;
    30562639            }
     
    30612644            EVP_PKEY_free(key_A);
    30622645            EVP_PKEY_free(key_B);
    3063             EVP_PKEY_CTX_free(kctx);
    3064             kctx = NULL;
    30652646            EVP_PKEY_CTX_free(test_ctx);
    30662647            test_ctx = NULL;
     
    30692650            pkey_print_message("", "ecdh",
    30702651                               ecdh_c[testnum][0],
    3071                                test_curves[testnum].bits, seconds.ecdh);
     2652                               ec_curves[testnum].bits, seconds.ecdh);
    30722653            Time_F(START);
    30732654            count =
     
    30772658                       mr ? "+R7:%ld:%d:%.2f\n" :
    30782659                       "%ld %u-bits ECDH ops in %.2fs\n", count,
    3079                        test_curves[testnum].bits, d);
     2660                       ec_curves[testnum].bits, d);
    30802661            ecdh_results[testnum][0] = (double)count / d;
    3081             rsa_count = count;
    3082         }
    3083 
    3084         if (rsa_count <= 1) {
     2662            op_count = count;
     2663        }
     2664
     2665        if (op_count <= 1) {
    30852666            /* if longer than 10s, don't do any more */
    3086             for (testnum++; testnum < OSSL_NELEM(ecdh_doit); testnum++)
    3087                 ecdh_doit[testnum] = 0;
     2667            stop_it(ecdh_doit, testnum);
    30882668        }
    30892669    }
     
    31022682                break;
    31032683            }
    3104 
    3105             if ((ed_pctx = EVP_PKEY_CTX_new_id(test_ed_curves[testnum].nid, NULL))
    3106                     == NULL
     2684            loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
     2685            if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
     2686                st = 0;
     2687                break;
     2688            }
     2689
     2690            if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid,
     2691                                               NULL)) == NULL
    31072692                || EVP_PKEY_keygen_init(ed_pctx) <= 0
    31082693                || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
     
    31192704                break;
    31202705            }
     2706            if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
     2707                                      NULL, NULL, ed_pkey)) {
     2708                st = 0;
     2709                EVP_PKEY_free(ed_pkey);
     2710                break;
     2711            }
     2712
    31212713            EVP_PKEY_free(ed_pkey);
     2714            ed_pkey = NULL;
    31222715        }
    31232716        if (st == 0) {
    31242717            BIO_printf(bio_err, "EdDSA failure.\n");
    31252718            ERR_print_errors(bio_err);
    3126             rsa_count = 1;
     2719            op_count = 1;
    31272720        } else {
    31282721            for (i = 0; i < loopargs_len; i++) {
    31292722                /* Perform EdDSA signature test */
    3130                 loopargs[i].sigsize = test_ed_curves[testnum].sigsize;
     2723                loopargs[i].sigsize = ed_curves[testnum].sigsize;
    31312724                st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
    31322725                                    loopargs[i].buf2, &loopargs[i].sigsize,
     
    31392732                           "EdDSA sign failure.  No EdDSA sign will be done.\n");
    31402733                ERR_print_errors(bio_err);
    3141                 rsa_count = 1;
     2734                op_count = 1;
    31422735            } else {
    3143                 pkey_print_message("sign", test_ed_curves[testnum].name,
     2736                pkey_print_message("sign", ed_curves[testnum].name,
    31442737                                   eddsa_c[testnum][0],
    3145                                    test_ed_curves[testnum].bits, seconds.eddsa);
     2738                                   ed_curves[testnum].bits, seconds.eddsa);
    31462739                Time_F(START);
    31472740                count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
     
    31512744                           mr ? "+R8:%ld:%u:%s:%.2f\n" :
    31522745                           "%ld %u bits %s signs in %.2fs \n",
    3153                            count, test_ed_curves[testnum].bits,
    3154                            test_ed_curves[testnum].name, d);
     2746                           count, ed_curves[testnum].bits,
     2747                           ed_curves[testnum].name, d);
    31552748                eddsa_results[testnum][0] = (double)count / d;
    3156                 rsa_count = count;
    3157             }
    3158 
     2749                op_count = count;
     2750            }
    31592751            /* Perform EdDSA verification test */
    31602752            for (i = 0; i < loopargs_len; i++) {
    3161                 st = EVP_DigestVerify(loopargs[i].eddsa_ctx[testnum],
     2753                st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
    31622754                                      loopargs[i].buf2, loopargs[i].sigsize,
    31632755                                      loopargs[i].buf, 20);
     
    31712763                eddsa_doit[testnum] = 0;
    31722764            } else {
    3173                 pkey_print_message("verify", test_ed_curves[testnum].name,
     2765                pkey_print_message("verify", ed_curves[testnum].name,
    31742766                                   eddsa_c[testnum][1],
    3175                                    test_ed_curves[testnum].bits, seconds.eddsa);
     2767                                   ed_curves[testnum].bits, seconds.eddsa);
    31762768                Time_F(START);
    31772769                count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
     
    31802772                           mr ? "+R9:%ld:%u:%s:%.2f\n"
    31812773                           : "%ld %u bits %s verify in %.2fs\n",
    3182                            count, test_ed_curves[testnum].bits,
    3183                            test_ed_curves[testnum].name, d);
     2774                           count, ed_curves[testnum].bits,
     2775                           ed_curves[testnum].name, d);
    31842776                eddsa_results[testnum][1] = (double)count / d;
    31852777            }
    31862778
    3187             if (rsa_count <= 1) {
     2779            if (op_count <= 1) {
    31882780                /* if longer than 10s, don't do any more */
    3189                 for (testnum++; testnum < EdDSA_NUM; testnum++)
    3190                     eddsa_doit[testnum] = 0;
    3191             }
    3192         }
    3193     }
    3194 
    3195 #endif                          /* OPENSSL_NO_EC */
     2781                stop_it(eddsa_doit, testnum);
     2782            }
     2783        }
     2784    }
     2785
     2786#ifndef OPENSSL_NO_SM2
     2787    for (testnum = 0; testnum < SM2_NUM; testnum++) {
     2788        int st = 1;
     2789        EVP_PKEY *sm2_pkey = NULL;
     2790
     2791        if (!sm2_doit[testnum])
     2792            continue;           /* Ignore Curve */
     2793        /* Init signing and verification */
     2794        for (i = 0; i < loopargs_len; i++) {
     2795            EVP_PKEY_CTX *sm2_pctx = NULL;
     2796            EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
     2797            EVP_PKEY_CTX *pctx = NULL;
     2798            st = 0;
     2799
     2800            loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
     2801            loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
     2802            if (loopargs[i].sm2_ctx[testnum] == NULL
     2803                    || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
     2804                break;
     2805
     2806            sm2_pkey = NULL;
     2807
     2808            st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL)) == NULL
     2809                || EVP_PKEY_keygen_init(pctx) <= 0
     2810                || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
     2811                    sm2_curves[testnum].nid) <= 0
     2812                || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
     2813            EVP_PKEY_CTX_free(pctx);
     2814            if (st == 0)
     2815                break;
     2816
     2817            st = 0; /* set back to zero */
     2818            /* attach it sooner to rely on main final cleanup */
     2819            loopargs[i].sm2_pkey[testnum] = sm2_pkey;
     2820            loopargs[i].sigsize = EVP_PKEY_get_size(sm2_pkey);
     2821
     2822            sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
     2823            sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
     2824            if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
     2825                EVP_PKEY_CTX_free(sm2_vfy_pctx);
     2826                break;
     2827            }
     2828
     2829            /* attach them directly to respective ctx */
     2830            EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
     2831            EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
     2832
     2833            /*
     2834             * No need to allow user to set an explicit ID here, just use
     2835             * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
     2836             */
     2837            if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
     2838                || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
     2839                break;
     2840
     2841            if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
     2842                                    EVP_sm3(), NULL, sm2_pkey))
     2843                break;
     2844            if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
     2845                                      EVP_sm3(), NULL, sm2_pkey))
     2846                break;
     2847            st = 1;         /* mark loop as succeeded */
     2848        }
     2849        if (st == 0) {
     2850            BIO_printf(bio_err, "SM2 init failure.\n");
     2851            ERR_print_errors(bio_err);
     2852            op_count = 1;
     2853        } else {
     2854            for (i = 0; i < loopargs_len; i++) {
     2855                /* Perform SM2 signature test */
     2856                st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
     2857                                    loopargs[i].buf2, &loopargs[i].sigsize,
     2858                                    loopargs[i].buf, 20);
     2859                if (st == 0)
     2860                    break;
     2861            }
     2862            if (st == 0) {
     2863                BIO_printf(bio_err,
     2864                           "SM2 sign failure.  No SM2 sign will be done.\n");
     2865                ERR_print_errors(bio_err);
     2866                op_count = 1;
     2867            } else {
     2868                pkey_print_message("sign", sm2_curves[testnum].name,
     2869                                   sm2_c[testnum][0],
     2870                                   sm2_curves[testnum].bits, seconds.sm2);
     2871                Time_F(START);
     2872                count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
     2873                d = Time_F(STOP);
     2874
     2875                BIO_printf(bio_err,
     2876                           mr ? "+R10:%ld:%u:%s:%.2f\n" :
     2877                           "%ld %u bits %s signs in %.2fs \n",
     2878                           count, sm2_curves[testnum].bits,
     2879                           sm2_curves[testnum].name, d);
     2880                sm2_results[testnum][0] = (double)count / d;
     2881                op_count = count;
     2882            }
     2883
     2884            /* Perform SM2 verification test */
     2885            for (i = 0; i < loopargs_len; i++) {
     2886                st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
     2887                                      loopargs[i].buf2, loopargs[i].sigsize,
     2888                                      loopargs[i].buf, 20);
     2889                if (st != 1)
     2890                    break;
     2891            }
     2892            if (st != 1) {
     2893                BIO_printf(bio_err,
     2894                           "SM2 verify failure.  No SM2 verify will be done.\n");
     2895                ERR_print_errors(bio_err);
     2896                sm2_doit[testnum] = 0;
     2897            } else {
     2898                pkey_print_message("verify", sm2_curves[testnum].name,
     2899                                   sm2_c[testnum][1],
     2900                                   sm2_curves[testnum].bits, seconds.sm2);
     2901                Time_F(START);
     2902                count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
     2903                d = Time_F(STOP);
     2904                BIO_printf(bio_err,
     2905                           mr ? "+R11:%ld:%u:%s:%.2f\n"
     2906                           : "%ld %u bits %s verify in %.2fs\n",
     2907                           count, sm2_curves[testnum].bits,
     2908                           sm2_curves[testnum].name, d);
     2909                sm2_results[testnum][1] = (double)count / d;
     2910            }
     2911
     2912            if (op_count <= 1) {
     2913                /* if longer than 10s, don't do any more */
     2914                for (testnum++; testnum < SM2_NUM; testnum++)
     2915                    sm2_doit[testnum] = 0;
     2916            }
     2917        }
     2918    }
     2919#endif                         /* OPENSSL_NO_SM2 */
     2920
     2921#ifndef OPENSSL_NO_DH
     2922    for (testnum = 0; testnum < FFDH_NUM; testnum++) {
     2923        int ffdh_checks = 1;
     2924
     2925        if (!ffdh_doit[testnum])
     2926            continue;
     2927
     2928        for (i = 0; i < loopargs_len; i++) {
     2929            EVP_PKEY *pkey_A = NULL;
     2930            EVP_PKEY *pkey_B = NULL;
     2931            EVP_PKEY_CTX *ffdh_ctx = NULL;
     2932            EVP_PKEY_CTX *test_ctx = NULL;
     2933            size_t secret_size;
     2934            size_t test_out;
     2935
     2936            /* Ensure that the error queue is empty */
     2937            if (ERR_peek_error()) {
     2938                BIO_printf(bio_err,
     2939                           "WARNING: the error queue contains previous unhandled errors.\n");
     2940                ERR_print_errors(bio_err);
     2941            }
     2942
     2943            pkey_A = EVP_PKEY_new();
     2944            if (!pkey_A) {
     2945                BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
     2946                ERR_print_errors(bio_err);
     2947                op_count = 1;
     2948                ffdh_checks = 0;
     2949                break;
     2950            }
     2951            pkey_B = EVP_PKEY_new();
     2952            if (!pkey_B) {
     2953                BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
     2954                ERR_print_errors(bio_err);
     2955                op_count = 1;
     2956                ffdh_checks = 0;
     2957                break;
     2958            }
     2959
     2960            ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
     2961            if (!ffdh_ctx) {
     2962                BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
     2963                ERR_print_errors(bio_err);
     2964                op_count = 1;
     2965                ffdh_checks = 0;
     2966                break;
     2967            }
     2968
     2969            if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
     2970                BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
     2971                ERR_print_errors(bio_err);
     2972                op_count = 1;
     2973                ffdh_checks = 0;
     2974                break;
     2975            }
     2976            if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
     2977                BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
     2978                ERR_print_errors(bio_err);
     2979                op_count = 1;
     2980                ffdh_checks = 0;
     2981                break;
     2982            }
     2983
     2984            if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
     2985                EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
     2986                BIO_printf(bio_err, "FFDH key generation failure.\n");
     2987                ERR_print_errors(bio_err);
     2988                op_count = 1;
     2989                ffdh_checks = 0;
     2990                break;
     2991            }
     2992
     2993            EVP_PKEY_CTX_free(ffdh_ctx);
     2994
     2995            /*
     2996             * check if the derivation works correctly both ways so that
     2997             * we know if future derive calls will fail, and we can skip
     2998             * error checking in benchmarked code
     2999             */
     3000            ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
     3001            if (ffdh_ctx == NULL) {
     3002                BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
     3003                ERR_print_errors(bio_err);
     3004                op_count = 1;
     3005                ffdh_checks = 0;
     3006                break;
     3007            }
     3008            if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
     3009                BIO_printf(bio_err, "FFDH derivation context init failure.\n");
     3010                ERR_print_errors(bio_err);
     3011                op_count = 1;
     3012                ffdh_checks = 0;
     3013                break;
     3014            }
     3015            if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
     3016                BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
     3017                ERR_print_errors(bio_err);
     3018                op_count = 1;
     3019                ffdh_checks = 0;
     3020                break;
     3021            }
     3022            if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
     3023                BIO_printf(bio_err, "Checking size of shared secret failed.\n");
     3024                ERR_print_errors(bio_err);
     3025                op_count = 1;
     3026                ffdh_checks = 0;
     3027                break;
     3028            }
     3029            if (secret_size > MAX_FFDH_SIZE) {
     3030                BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
     3031                op_count = 1;
     3032                ffdh_checks = 0;
     3033                break;
     3034            }
     3035            if (EVP_PKEY_derive(ffdh_ctx,
     3036                                loopargs[i].secret_ff_a,
     3037                                &secret_size) <= 0) {
     3038                BIO_printf(bio_err, "Shared secret derive failure.\n");
     3039                ERR_print_errors(bio_err);
     3040                op_count = 1;
     3041                ffdh_checks = 0;
     3042                break;
     3043            }
     3044            /* Now check from side B */
     3045            test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
     3046            if (!test_ctx) {
     3047                BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
     3048                ERR_print_errors(bio_err);
     3049                op_count = 1;
     3050                ffdh_checks = 0;
     3051                break;
     3052            }
     3053            if (!EVP_PKEY_derive_init(test_ctx) ||
     3054                !EVP_PKEY_derive_set_peer(test_ctx, pkey_A) ||
     3055                !EVP_PKEY_derive(test_ctx, NULL, &test_out) ||
     3056                !EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) ||
     3057                test_out != secret_size) {
     3058                BIO_printf(bio_err, "FFDH computation failure.\n");
     3059                op_count = 1;
     3060                ffdh_checks = 0;
     3061                break;
     3062            }
     3063
     3064            /* compare the computed secrets */
     3065            if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
     3066                              loopargs[i].secret_ff_b, secret_size)) {
     3067                BIO_printf(bio_err, "FFDH computations don't match.\n");
     3068                ERR_print_errors(bio_err);
     3069                op_count = 1;
     3070                ffdh_checks = 0;
     3071                break;
     3072            }
     3073
     3074            loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
     3075
     3076            EVP_PKEY_free(pkey_A);
     3077            pkey_A = NULL;
     3078            EVP_PKEY_free(pkey_B);
     3079            pkey_B = NULL;
     3080            EVP_PKEY_CTX_free(test_ctx);
     3081            test_ctx = NULL;
     3082        }
     3083        if (ffdh_checks != 0) {
     3084            pkey_print_message("", "ffdh", ffdh_c[testnum][0],
     3085                               ffdh_params[testnum].bits, seconds.ffdh);
     3086            Time_F(START);
     3087            count =
     3088                run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
     3089            d = Time_F(STOP);
     3090            BIO_printf(bio_err,
     3091                       mr ? "+R12:%ld:%d:%.2f\n" :
     3092                       "%ld %u-bits FFDH ops in %.2fs\n", count,
     3093                       ffdh_params[testnum].bits, d);
     3094            ffdh_results[testnum][0] = (double)count / d;
     3095            op_count = count;
     3096        }
     3097        if (op_count <= 1) {
     3098            /* if longer than 10s, don't do any more */
     3099            stop_it(ffdh_doit, testnum);
     3100        }
     3101    }
     3102#endif  /* OPENSSL_NO_DH */
    31963103#ifndef NO_FORK
    31973104 show_res:
    31983105#endif
    31993106    if (!mr) {
    3200         printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
     3107        printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
    32013108        printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
    3202         printf("options:");
    3203         printf("%s ", BN_options());
    3204 #ifndef OPENSSL_NO_MD2
    3205         printf("%s ", MD2_options());
    3206 #endif
    3207 #ifndef OPENSSL_NO_RC4
    3208         printf("%s ", RC4_options());
    3209 #endif
    3210 #ifndef OPENSSL_NO_DES
    3211         printf("%s ", DES_options());
    3212 #endif
    3213         printf("%s ", AES_options());
    3214 #ifndef OPENSSL_NO_IDEA
    3215         printf("%s ", IDEA_options());
    3216 #endif
    3217 #ifndef OPENSSL_NO_BF
    3218         printf("%s ", BF_options());
    3219 #endif
    3220         printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
     3109        printf("options: %s\n", BN_options());
     3110        printf("%s\n", OpenSSL_version(OPENSSL_CFLAGS));
     3111        printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
    32213112    }
    32223113
    32233114    if (pr_header) {
    3224         if (mr)
     3115        if (mr) {
    32253116            printf("+H");
    3226         else {
    3227             printf
    3228                 ("The 'numbers' are in 1000s of bytes per second processed.\n");
     3117        } else {
     3118            printf("The 'numbers' are in 1000s of bytes per second processed.\n");
    32293119            printf("type        ");
    32303120        }
     
    32493139        printf("\n");
    32503140    }
    3251 #ifndef OPENSSL_NO_RSA
    32523141    testnum = 1;
    32533142    for (k = 0; k < RSA_NUM; k++) {
     
    32603149        if (mr)
    32613150            printf("+F2:%u:%u:%f:%f\n",
    3262                    k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
     3151                   k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1]);
    32633152        else
    32643153            printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
    3265                    rsa_bits[k], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
     3154                   rsa_keys[k].bits, 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
    32663155                   rsa_results[k][0], rsa_results[k][1]);
    32673156    }
    3268 #endif
    3269 #ifndef OPENSSL_NO_DSA
    32703157    testnum = 1;
    32713158    for (k = 0; k < DSA_NUM; k++) {
     
    32843171                   dsa_results[k][0], dsa_results[k][1]);
    32853172    }
    3286 #endif
    3287 #ifndef OPENSSL_NO_EC
    32883173    testnum = 1;
    32893174    for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
     
    32973182        if (mr)
    32983183            printf("+F4:%u:%u:%f:%f\n",
    3299                    k, test_curves[k].bits,
     3184                   k, ec_curves[k].bits,
    33003185                   ecdsa_results[k][0], ecdsa_results[k][1]);
    33013186        else
    33023187            printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
    3303                    test_curves[k].bits, test_curves[k].name,
     3188                   ec_curves[k].bits, ec_curves[k].name,
    33043189                   1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
    33053190                   ecdsa_results[k][0], ecdsa_results[k][1]);
     
    33163201        if (mr)
    33173202            printf("+F5:%u:%u:%f:%f\n",
    3318                    k, test_curves[k].bits,
     3203                   k, ec_curves[k].bits,
    33193204                   ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
    33203205
    33213206        else
    33223207            printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
    3323                    test_curves[k].bits, test_curves[k].name,
     3208                   ec_curves[k].bits, ec_curves[k].name,
    33243209                   1.0 / ecdh_results[k][0], ecdh_results[k][0]);
    33253210    }
     
    33363221        if (mr)
    33373222            printf("+F6:%u:%u:%s:%f:%f\n",
    3338                    k, test_ed_curves[k].bits, test_ed_curves[k].name,
     3223                   k, ed_curves[k].bits, ed_curves[k].name,
    33393224                   eddsa_results[k][0], eddsa_results[k][1]);
    33403225        else
    33413226            printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
    3342                    test_ed_curves[k].bits, test_ed_curves[k].name,
     3227                   ed_curves[k].bits, ed_curves[k].name,
    33433228                   1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
    33443229                   eddsa_results[k][0], eddsa_results[k][1]);
    33453230    }
    3346 #endif
     3231
     3232#ifndef OPENSSL_NO_SM2
     3233    testnum = 1;
     3234    for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
     3235        if (!sm2_doit[k])
     3236            continue;
     3237        if (testnum && !mr) {
     3238            printf("%30ssign    verify    sign/s verify/s\n", " ");
     3239            testnum = 0;
     3240        }
     3241
     3242        if (mr)
     3243            printf("+F7:%u:%u:%s:%f:%f\n",
     3244                   k, sm2_curves[k].bits, sm2_curves[k].name,
     3245                   sm2_results[k][0], sm2_results[k][1]);
     3246        else
     3247            printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
     3248                   sm2_curves[k].bits, sm2_curves[k].name,
     3249                   1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
     3250                   sm2_results[k][0], sm2_results[k][1]);
     3251    }
     3252#endif
     3253#ifndef OPENSSL_NO_DH
     3254    testnum = 1;
     3255    for (k = 0; k < FFDH_NUM; k++) {
     3256        if (!ffdh_doit[k])
     3257            continue;
     3258        if (testnum && !mr) {
     3259            printf("%23sop     op/s\n", " ");
     3260            testnum = 0;
     3261        }
     3262        if (mr)
     3263            printf("+F8:%u:%u:%f:%f\n",
     3264                   k, ffdh_params[k].bits,
     3265                   ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
     3266
     3267        else
     3268            printf("%4u bits ffdh %8.4fs %8.1f\n",
     3269                   ffdh_params[k].bits,
     3270                   1.0 / ffdh_results[k][0], ffdh_results[k][0]);
     3271    }
     3272#endif /* OPENSSL_NO_DH */
    33473273
    33483274    ret = 0;
     
    33543280        OPENSSL_free(loopargs[i].buf2_malloc);
    33553281
    3356 #ifndef OPENSSL_NO_RSA
    3357         for (k = 0; k < RSA_NUM; k++)
    3358             RSA_free(loopargs[i].rsa_key[k]);
    3359 #endif
    3360 #ifndef OPENSSL_NO_DSA
    3361         for (k = 0; k < DSA_NUM; k++)
    3362             DSA_free(loopargs[i].dsa_key[k]);
    3363 #endif
    3364 #ifndef OPENSSL_NO_EC
    3365         for (k = 0; k < ECDSA_NUM; k++)
    3366             EC_KEY_free(loopargs[i].ecdsa[k]);
     3282        BN_free(bn);
     3283        EVP_PKEY_CTX_free(genctx);
     3284        for (k = 0; k < RSA_NUM; k++) {
     3285            EVP_PKEY_CTX_free(loopargs[i].rsa_sign_ctx[k]);
     3286            EVP_PKEY_CTX_free(loopargs[i].rsa_verify_ctx[k]);
     3287        }
     3288#ifndef OPENSSL_NO_DH
     3289        OPENSSL_free(loopargs[i].secret_ff_a);
     3290        OPENSSL_free(loopargs[i].secret_ff_b);
     3291        for (k = 0; k < FFDH_NUM; k++)
     3292            EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
     3293#endif
     3294        for (k = 0; k < DSA_NUM; k++) {
     3295            EVP_PKEY_CTX_free(loopargs[i].dsa_sign_ctx[k]);
     3296            EVP_PKEY_CTX_free(loopargs[i].dsa_verify_ctx[k]);
     3297        }
     3298        for (k = 0; k < ECDSA_NUM; k++) {
     3299            EVP_PKEY_CTX_free(loopargs[i].ecdsa_sign_ctx[k]);
     3300            EVP_PKEY_CTX_free(loopargs[i].ecdsa_verify_ctx[k]);
     3301        }
    33673302        for (k = 0; k < EC_NUM; k++)
    33683303            EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
    3369         for (k = 0; k < EdDSA_NUM; k++)
     3304        for (k = 0; k < EdDSA_NUM; k++) {
    33703305            EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
     3306            EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
     3307        }
     3308#ifndef OPENSSL_NO_SM2
     3309        for (k = 0; k < SM2_NUM; k++) {
     3310            EVP_PKEY_CTX *pctx = NULL;
     3311
     3312            /* free signing ctx */
     3313            if (loopargs[i].sm2_ctx[k] != NULL
     3314                && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
     3315                EVP_PKEY_CTX_free(pctx);
     3316            EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
     3317            /* free verification ctx */
     3318            if (loopargs[i].sm2_vfy_ctx[k] != NULL
     3319                && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
     3320                EVP_PKEY_CTX_free(pctx);
     3321            EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
     3322            /* free pkey */
     3323            EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
     3324        }
     3325#endif
    33713326        OPENSSL_free(loopargs[i].secret_a);
    33723327        OPENSSL_free(loopargs[i].secret_b);
    3373 #endif
    3374     }
     3328    }
     3329    OPENSSL_free(evp_hmac_name);
     3330    OPENSSL_free(evp_cmac_name);
    33753331
    33763332    if (async_jobs > 0) {
     
    33843340    OPENSSL_free(loopargs);
    33853341    release_engine(e);
     3342    EVP_CIPHER_free(evp_cipher);
     3343    EVP_MAC_free(mac);
    33863344    return ret;
    33873345}
     
    33893347static void print_message(const char *s, long num, int length, int tm)
    33903348{
    3391 #ifdef SIGALRM
    33923349    BIO_printf(bio_err,
    33933350               mr ? "+DT:%s:%d:%d\n"
     
    33963353    run = 1;
    33973354    alarm(tm);
    3398 #else
    3399     BIO_printf(bio_err,
    3400                mr ? "+DN:%s:%ld:%d\n"
    3401                : "Doing %s %ld times on %d size blocks: ", s, num, length);
    3402     (void)BIO_flush(bio_err);
    3403 #endif
    34043355}
    34053356
     
    34073358                               unsigned int bits, int tm)
    34083359{
    3409 #ifdef SIGALRM
    34103360    BIO_printf(bio_err,
    34113361               mr ? "+DTP:%d:%s:%s:%d\n"
     
    34143364    run = 1;
    34153365    alarm(tm);
    3416 #else
    3417     BIO_printf(bio_err,
    3418                mr ? "+DNP:%ld:%d:%s:%s\n"
    3419                : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
    3420     (void)BIO_flush(bio_err);
    3421 #endif
    34223366}
    34233367
     
    34253369{
    34263370    if (count == -1) {
    3427         BIO_puts(bio_err, "EVP error!\n");
    3428         exit(1);
     3371        BIO_printf(bio_err, "%s error!\n", names[alg]);
     3372        ERR_print_errors(bio_err);
     3373        return;
    34293374    }
    34303375    BIO_printf(bio_err,
     
    34513396    }
    34523397
    3453     while (!isdelim[(unsigned char)(**string)]) {
     3398    while (!isdelim[(unsigned char)(**string)])
    34543399        (*string)++;
    3455     }
    34563400
    34573401    if (**string) {
     
    34913435            mr = 1;
    34923436            usertime = 0;
    3493             free(fds);
     3437            OPENSSL_free(fds);
    34943438            return 0;
    34953439        }
     
    35373481                d = atof(sstrsep(&p, sep));
    35383482                rsa_results[k][1] += d;
    3539             }
    3540 # ifndef OPENSSL_NO_DSA
    3541             else if (strncmp(buf, "+F3:", 4) == 0) {
     3483            } else if (strncmp(buf, "+F3:", 4) == 0) {
    35423484                int k;
    35433485                double d;
     
    35523494                d = atof(sstrsep(&p, sep));
    35533495                dsa_results[k][1] += d;
    3554             }
    3555 # endif
    3556 # ifndef OPENSSL_NO_EC
    3557             else if (strncmp(buf, "+F4:", 4) == 0) {
     3496            } else if (strncmp(buf, "+F4:", 4) == 0) {
    35583497                int k;
    35593498                double d;
     
    35923531                d = atof(sstrsep(&p, sep));
    35933532                eddsa_results[k][1] += d;
    3594             }
    3595 # endif
    3596 
    3597             else if (strncmp(buf, "+H:", 3) == 0) {
     3533# ifndef OPENSSL_NO_SM2
     3534            } else if (strncmp(buf, "+F7:", 4) == 0) {
     3535                int k;
     3536                double d;
     3537
     3538                p = buf + 4;
     3539                k = atoi(sstrsep(&p, sep));
     3540                sstrsep(&p, sep);
     3541                sstrsep(&p, sep);
     3542
     3543                d = atof(sstrsep(&p, sep));
     3544                sm2_results[k][0] += d;
     3545
     3546                d = atof(sstrsep(&p, sep));
     3547                sm2_results[k][1] += d;
     3548# endif /* OPENSSL_NO_SM2 */
     3549# ifndef OPENSSL_NO_DH
     3550            } else if (strncmp(buf, "+F8:", 4) == 0) {
     3551                int k;
     3552                double d;
     3553
     3554                p = buf + 4;
     3555                k = atoi(sstrsep(&p, sep));
     3556                sstrsep(&p, sep);
     3557
     3558                d = atof(sstrsep(&p, sep));
     3559                ffdh_results[k][0] += d;
     3560# endif /* OPENSSL_NO_DH */
     3561            } else if (strncmp(buf, "+H:", 3) == 0) {
    35983562                ;
    3599             } else
     3563            } else {
    36003564                BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
    36013565                           n);
     3566            }
    36023567        }
    36033568
    36043569        fclose(f);
    36053570    }
    3606     free(fds);
     3571    OPENSSL_free(fds);
    36073572    return 1;
    36083573}
     
    36173582    int j, count, keylen, num = OSSL_NELEM(mblengths_list);
    36183583    const char *alg_name;
    3619     unsigned char *inp, *out, *key, no_key[32], no_iv[16];
    3620     EVP_CIPHER_CTX *ctx;
     3584    unsigned char *inp = NULL, *out = NULL, *key, no_key[32], no_iv[16];
     3585    EVP_CIPHER_CTX *ctx = NULL;
    36213586    double d = 0.0;
    36223587
     
    36283593    inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
    36293594    out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
    3630     ctx = EVP_CIPHER_CTX_new();
    3631     EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
    3632 
    3633     keylen = EVP_CIPHER_CTX_key_length(ctx);
     3595    if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
     3596        app_bail_out("failed to allocate cipher context\n");
     3597    if (!EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv))
     3598        app_bail_out("failed to initialise cipher context\n");
     3599
     3600    if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) < 0) {
     3601        BIO_printf(bio_err, "Impossible negative key length: %d\n", keylen);
     3602        goto err;
     3603    }
    36343604    key = app_malloc(keylen, "evp_cipher key");
    3635     EVP_CIPHER_CTX_rand_key(ctx, key);
    3636     EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
     3605    if (!EVP_CIPHER_CTX_rand_key(ctx, key))
     3606        app_bail_out("failed to generate random cipher key\n");
     3607    if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
     3608        app_bail_out("failed to set cipher key\n");
    36373609    OPENSSL_clear_free(key, keylen);
    36383610
    3639     EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
    3640     alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
     3611    if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
     3612                             sizeof(no_key), no_key))
     3613        app_bail_out("failed to set AEAD key\n");
     3614    if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
     3615        app_bail_out("failed to get cipher name\n");
    36413616
    36423617    for (j = 0; j < num; j++) {
     
    36673642                mb_param.inp = inp;
    36683643                mb_param.len = len;
    3669                 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
    3670                                     sizeof(mb_param), &mb_param);
     3644                (void)EVP_CIPHER_CTX_ctrl(ctx,
     3645                                          EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
     3646                                          sizeof(mb_param), &mb_param);
    36713647            } else {
    36723648                int pad;
     
    37143690    }
    37153691
     3692 err:
    37163693    OPENSSL_free(inp);
    37173694    OPENSSL_free(out);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette