Home | History | Annotate | Line # | Download | only in apps
      1      1.1  christos /*
      2      1.1  christos  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
      4      1.1  christos  *
      5      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      7      1.1  christos  * in the file LICENSE in the source distribution or at
      8      1.1  christos  * https://www.openssl.org/source/license.html
      9      1.1  christos  */
     10      1.1  christos 
     11      1.1  christos #undef SECONDS
     12  1.1.1.2  christos #define SECONDS 3
     13  1.1.1.2  christos #define PKEY_SECONDS 10
     14      1.1  christos 
     15  1.1.1.2  christos #define RSA_SECONDS PKEY_SECONDS
     16  1.1.1.2  christos #define DSA_SECONDS PKEY_SECONDS
     17  1.1.1.2  christos #define ECDSA_SECONDS PKEY_SECONDS
     18  1.1.1.2  christos #define ECDH_SECONDS PKEY_SECONDS
     19  1.1.1.2  christos #define EdDSA_SECONDS PKEY_SECONDS
     20  1.1.1.2  christos #define SM2_SECONDS PKEY_SECONDS
     21  1.1.1.2  christos #define FFDH_SECONDS PKEY_SECONDS
     22  1.1.1.2  christos #define KEM_SECONDS PKEY_SECONDS
     23  1.1.1.2  christos #define SIG_SECONDS PKEY_SECONDS
     24      1.1  christos 
     25      1.1  christos #define MAX_ALGNAME_SUFFIX 100
     26      1.1  christos 
     27      1.1  christos /* We need to use some deprecated APIs */
     28      1.1  christos #define OPENSSL_SUPPRESS_DEPRECATED
     29      1.1  christos #include "internal/e_os.h"
     30      1.1  christos 
     31      1.1  christos #include <stdio.h>
     32      1.1  christos #include <stdlib.h>
     33      1.1  christos #include <string.h>
     34      1.1  christos #include <math.h>
     35      1.1  christos #include "apps.h"
     36      1.1  christos #include "progs.h"
     37      1.1  christos #include "internal/nelem.h"
     38      1.1  christos #include "internal/numbers.h"
     39      1.1  christos #include <openssl/crypto.h>
     40      1.1  christos #include <openssl/rand.h>
     41      1.1  christos #include <openssl/err.h>
     42      1.1  christos #include <openssl/evp.h>
     43      1.1  christos #include <openssl/objects.h>
     44      1.1  christos #include <openssl/core_names.h>
     45      1.1  christos #include <openssl/async.h>
     46      1.1  christos #include <openssl/provider.h>
     47      1.1  christos #if !defined(OPENSSL_SYS_MSDOS)
     48  1.1.1.2  christos #include <unistd.h>
     49      1.1  christos #endif
     50      1.1  christos 
     51      1.1  christos #if defined(_WIN32)
     52  1.1.1.2  christos #include <windows.h>
     53      1.1  christos /*
     54      1.1  christos  * While VirtualLock is available under the app partition (e.g. UWP),
     55      1.1  christos  * the headers do not define the API. Define it ourselves instead.
     56      1.1  christos  */
     57      1.1  christos WINBASEAPI
     58      1.1  christos BOOL
     59  1.1.1.2  christos     WINAPI
     60  1.1.1.2  christos     VirtualLock(
     61  1.1.1.2  christos         _In_ LPVOID lpAddress,
     62  1.1.1.2  christos         _In_ SIZE_T dwSize);
     63      1.1  christos #endif
     64      1.1  christos 
     65      1.1  christos #if defined(OPENSSL_SYS_LINUX)
     66  1.1.1.2  christos #include <sys/mman.h>
     67      1.1  christos #endif
     68      1.1  christos 
     69      1.1  christos #include <openssl/bn.h>
     70      1.1  christos #include <openssl/rsa.h>
     71      1.1  christos #include "./testrsa.h"
     72      1.1  christos #ifndef OPENSSL_NO_DH
     73  1.1.1.2  christos #include <openssl/dh.h>
     74      1.1  christos #endif
     75      1.1  christos #include <openssl/x509.h>
     76      1.1  christos #include <openssl/dsa.h>
     77      1.1  christos #include "./testdsa.h"
     78      1.1  christos #include <openssl/modes.h>
     79      1.1  christos 
     80      1.1  christos #ifndef HAVE_FORK
     81  1.1.1.2  christos #if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
     82  1.1.1.2  christos #define HAVE_FORK 0
     83  1.1.1.2  christos #else
     84  1.1.1.2  christos #define HAVE_FORK 1
     85  1.1.1.2  christos #include <sys/wait.h>
     86  1.1.1.2  christos #endif
     87      1.1  christos #endif
     88      1.1  christos 
     89      1.1  christos #if HAVE_FORK
     90  1.1.1.2  christos #undef NO_FORK
     91      1.1  christos #else
     92  1.1.1.2  christos #define NO_FORK
     93      1.1  christos #endif
     94      1.1  christos 
     95      1.1  christos #define MAX_MISALIGNMENT 63
     96  1.1.1.2  christos #define MAX_ECDH_SIZE 256
     97  1.1.1.2  christos #define MISALIGN 64
     98      1.1  christos #define MAX_FFDH_SIZE 1024
     99      1.1  christos 
    100      1.1  christos #ifndef RSA_DEFAULT_PRIME_NUM
    101  1.1.1.2  christos #define RSA_DEFAULT_PRIME_NUM 2
    102      1.1  christos #endif
    103      1.1  christos 
    104      1.1  christos typedef struct openssl_speed_sec_st {
    105      1.1  christos     int sym;
    106      1.1  christos     int rsa;
    107      1.1  christos     int dsa;
    108      1.1  christos     int ecdsa;
    109      1.1  christos     int ecdh;
    110      1.1  christos     int eddsa;
    111      1.1  christos     int sm2;
    112      1.1  christos     int ffdh;
    113      1.1  christos     int kem;
    114      1.1  christos     int sig;
    115      1.1  christos } openssl_speed_sec_t;
    116      1.1  christos 
    117      1.1  christos static volatile int run = 0;
    118      1.1  christos 
    119  1.1.1.2  christos static int mr = 0; /* machine-readeable output format to merge fork results */
    120      1.1  christos static int usertime = 1;
    121      1.1  christos 
    122      1.1  christos static double Time_F(int s);
    123      1.1  christos static void print_message(const char *s, int length, int tm);
    124      1.1  christos static void pkey_print_message(const char *str, const char *str2,
    125  1.1.1.2  christos     unsigned int bits, int sec);
    126      1.1  christos static void kskey_print_message(const char *str, const char *str2, int tm);
    127      1.1  christos static void print_result(int alg, int run_no, int count, double time_used);
    128      1.1  christos #ifndef NO_FORK
    129      1.1  christos static int do_multi(int multi, int size_num);
    130      1.1  christos #endif
    131      1.1  christos 
    132      1.1  christos static int domlock = 0;
    133      1.1  christos static int testmode = 0;
    134      1.1  christos static int testmoderesult = 0;
    135      1.1  christos 
    136      1.1  christos static const int lengths_list[] = {
    137      1.1  christos     16, 64, 256, 1024, 8 * 1024, 16 * 1024
    138      1.1  christos };
    139  1.1.1.2  christos #define SIZE_NUM OSSL_NELEM(lengths_list)
    140      1.1  christos static const int *lengths = lengths_list;
    141      1.1  christos 
    142      1.1  christos static const int aead_lengths_list[] = {
    143      1.1  christos     2, 31, 136, 1024, 8 * 1024, 16 * 1024
    144      1.1  christos };
    145      1.1  christos 
    146  1.1.1.2  christos #define START 0
    147  1.1.1.2  christos #define STOP 1
    148      1.1  christos 
    149      1.1  christos #ifdef SIGALRM
    150      1.1  christos 
    151      1.1  christos static void alarmed(ossl_unused int sig)
    152      1.1  christos {
    153      1.1  christos     signal(SIGALRM, alarmed);
    154      1.1  christos     run = 0;
    155      1.1  christos }
    156      1.1  christos 
    157      1.1  christos static double Time_F(int s)
    158      1.1  christos {
    159      1.1  christos     double ret = app_tminterval(s, usertime);
    160      1.1  christos     if (s == STOP)
    161      1.1  christos         alarm(0);
    162      1.1  christos     return ret;
    163      1.1  christos }
    164      1.1  christos 
    165      1.1  christos #elif defined(_WIN32)
    166      1.1  christos 
    167  1.1.1.2  christos #define SIGALRM -1
    168      1.1  christos 
    169      1.1  christos static unsigned int lapse;
    170      1.1  christos static volatile unsigned int schlock;
    171      1.1  christos static void alarm_win32(unsigned int secs)
    172      1.1  christos {
    173      1.1  christos     lapse = secs * 1000;
    174      1.1  christos }
    175      1.1  christos 
    176  1.1.1.2  christos #define alarm alarm_win32
    177      1.1  christos 
    178  1.1.1.2  christos static DWORD WINAPI sleepy(VOID *arg)
    179      1.1  christos {
    180      1.1  christos     schlock = 1;
    181      1.1  christos     Sleep(lapse);
    182      1.1  christos     run = 0;
    183      1.1  christos     return 0;
    184      1.1  christos }
    185      1.1  christos 
    186      1.1  christos static double Time_F(int s)
    187      1.1  christos {
    188      1.1  christos     double ret;
    189      1.1  christos     static HANDLE thr;
    190      1.1  christos 
    191      1.1  christos     if (s == START) {
    192      1.1  christos         schlock = 0;
    193      1.1  christos         thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
    194      1.1  christos         if (thr == NULL) {
    195      1.1  christos             DWORD err = GetLastError();
    196      1.1  christos             BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
    197      1.1  christos             ExitProcess(err);
    198      1.1  christos         }
    199      1.1  christos         while (!schlock)
    200  1.1.1.2  christos             Sleep(0); /* scheduler spinlock */
    201      1.1  christos         ret = app_tminterval(s, usertime);
    202      1.1  christos     } else {
    203      1.1  christos         ret = app_tminterval(s, usertime);
    204      1.1  christos         if (run)
    205      1.1  christos             TerminateThread(thr, 0);
    206      1.1  christos         CloseHandle(thr);
    207      1.1  christos     }
    208      1.1  christos 
    209      1.1  christos     return ret;
    210      1.1  christos }
    211      1.1  christos #else
    212  1.1.1.2  christos #error "SIGALRM not defined and the platform is not Windows"
    213      1.1  christos #endif
    214      1.1  christos 
    215      1.1  christos static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
    216  1.1.1.2  christos     const openssl_speed_sec_t *seconds);
    217      1.1  christos 
    218      1.1  christos static int opt_found(const char *name, unsigned int *result,
    219  1.1.1.2  christos     const OPT_PAIR pairs[], unsigned int nbelem)
    220      1.1  christos {
    221      1.1  christos     unsigned int idx;
    222      1.1  christos 
    223      1.1  christos     for (idx = 0; idx < nbelem; ++idx, pairs++)
    224      1.1  christos         if (strcmp(name, pairs->name) == 0) {
    225      1.1  christos             *result = pairs->retval;
    226      1.1  christos             return 1;
    227      1.1  christos         }
    228      1.1  christos     return 0;
    229      1.1  christos }
    230  1.1.1.2  christos #define opt_found(value, pairs, result) \
    231      1.1  christos     opt_found(value, result, pairs, OSSL_NELEM(pairs))
    232      1.1  christos 
    233      1.1  christos typedef enum OPTION_choice {
    234      1.1  christos     OPT_COMMON,
    235  1.1.1.2  christos     OPT_ELAPSED,
    236  1.1.1.2  christos     OPT_EVP,
    237  1.1.1.2  christos     OPT_HMAC,
    238  1.1.1.2  christos     OPT_DECRYPT,
    239  1.1.1.2  christos     OPT_ENGINE,
    240  1.1.1.2  christos     OPT_MULTI,
    241  1.1.1.2  christos     OPT_MR,
    242  1.1.1.2  christos     OPT_MB,
    243  1.1.1.2  christos     OPT_MISALIGN,
    244  1.1.1.2  christos     OPT_ASYNCJOBS,
    245  1.1.1.2  christos     OPT_R_ENUM,
    246  1.1.1.2  christos     OPT_PROV_ENUM,
    247  1.1.1.2  christos     OPT_CONFIG,
    248  1.1.1.2  christos     OPT_PRIMES,
    249  1.1.1.2  christos     OPT_SECONDS,
    250  1.1.1.2  christos     OPT_BYTES,
    251  1.1.1.2  christos     OPT_AEAD,
    252  1.1.1.2  christos     OPT_CMAC,
    253  1.1.1.2  christos     OPT_MLOCK,
    254  1.1.1.2  christos     OPT_TESTMODE,
    255  1.1.1.2  christos     OPT_KEM,
    256  1.1.1.2  christos     OPT_SIG
    257      1.1  christos } OPTION_CHOICE;
    258      1.1  christos 
    259      1.1  christos const OPTIONS speed_options[] = {
    260  1.1.1.2  christos     { OPT_HELP_STR, 1, '-',
    261  1.1.1.2  christos         "Usage: %s [options] [algorithm...]\n"
    262  1.1.1.2  christos         "All +int options consider prefix '0' as base-8 input, "
    263  1.1.1.2  christos         "prefix '0x'/'0X' as base-16 input.\n" },
    264      1.1  christos 
    265      1.1  christos     OPT_SECTION("General"),
    266  1.1.1.2  christos     { "help", OPT_HELP, '-', "Display this summary" },
    267  1.1.1.2  christos     { "mb", OPT_MB, '-',
    268  1.1.1.2  christos         "Enable (tls1>=1) multi-block mode on EVP-named cipher" },
    269  1.1.1.2  christos     { "mr", OPT_MR, '-', "Produce machine readable output" },
    270      1.1  christos #ifndef NO_FORK
    271  1.1.1.2  christos     { "multi", OPT_MULTI, 'p', "Run benchmarks in parallel" },
    272      1.1  christos #endif
    273      1.1  christos #ifndef OPENSSL_NO_ASYNC
    274  1.1.1.2  christos     { "async_jobs", OPT_ASYNCJOBS, 'p',
    275  1.1.1.2  christos         "Enable async mode and start specified number of jobs" },
    276      1.1  christos #endif
    277      1.1  christos #ifndef OPENSSL_NO_ENGINE
    278  1.1.1.2  christos     { "engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device" },
    279      1.1  christos #endif
    280  1.1.1.2  christos     { "primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)" },
    281  1.1.1.2  christos     { "mlock", OPT_MLOCK, '-', "Lock memory for better result determinism" },
    282  1.1.1.2  christos     { "testmode", OPT_TESTMODE, '-', "Run the speed command in test mode" },
    283      1.1  christos     OPT_CONFIG_OPTION,
    284      1.1  christos 
    285      1.1  christos     OPT_SECTION("Selection"),
    286  1.1.1.2  christos     { "evp", OPT_EVP, 's', "Use EVP-named cipher or digest" },
    287  1.1.1.2  christos     { "hmac", OPT_HMAC, 's', "HMAC using EVP-named digest" },
    288  1.1.1.2  christos     { "cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher" },
    289  1.1.1.2  christos     { "decrypt", OPT_DECRYPT, '-',
    290  1.1.1.2  christos         "Time decryption instead of encryption (only EVP)" },
    291  1.1.1.2  christos     { "aead", OPT_AEAD, '-',
    292  1.1.1.2  christos         "Benchmark EVP-named AEAD cipher in TLS-like sequence" },
    293  1.1.1.2  christos     { "kem-algorithms", OPT_KEM, '-',
    294  1.1.1.2  christos         "Benchmark KEM algorithms" },
    295  1.1.1.2  christos     { "signature-algorithms", OPT_SIG, '-',
    296  1.1.1.2  christos         "Benchmark signature algorithms" },
    297      1.1  christos 
    298      1.1  christos     OPT_SECTION("Timing"),
    299  1.1.1.2  christos     { "elapsed", OPT_ELAPSED, '-',
    300  1.1.1.2  christos         "Use wall-clock time instead of CPU user time as divisor" },
    301  1.1.1.2  christos     { "seconds", OPT_SECONDS, 'p',
    302  1.1.1.2  christos         "Run benchmarks for specified amount of seconds" },
    303  1.1.1.2  christos     { "bytes", OPT_BYTES, 'p',
    304  1.1.1.2  christos         "Run [non-PKI] benchmarks on custom-sized buffer" },
    305  1.1.1.2  christos     { "misalign", OPT_MISALIGN, 'p',
    306  1.1.1.2  christos         "Use specified offset to mis-align buffers" },
    307      1.1  christos 
    308      1.1  christos     OPT_R_OPTIONS,
    309      1.1  christos     OPT_PROV_OPTIONS,
    310      1.1  christos 
    311      1.1  christos     OPT_PARAMETERS(),
    312  1.1.1.2  christos     { "algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)" },
    313  1.1.1.2  christos     { NULL }
    314      1.1  christos };
    315      1.1  christos 
    316      1.1  christos enum {
    317  1.1.1.2  christos     D_MD2,
    318  1.1.1.2  christos     D_MDC2,
    319  1.1.1.2  christos     D_MD4,
    320  1.1.1.2  christos     D_MD5,
    321  1.1.1.2  christos     D_SHA1,
    322  1.1.1.2  christos     D_RMD160,
    323  1.1.1.2  christos     D_SHA256,
    324  1.1.1.2  christos     D_SHA512,
    325  1.1.1.2  christos     D_WHIRLPOOL,
    326  1.1.1.2  christos     D_HMAC,
    327  1.1.1.2  christos     D_CBC_DES,
    328  1.1.1.2  christos     D_EDE3_DES,
    329  1.1.1.2  christos     D_RC4,
    330  1.1.1.2  christos     D_CBC_IDEA,
    331  1.1.1.2  christos     D_CBC_SEED,
    332  1.1.1.2  christos     D_CBC_RC2,
    333  1.1.1.2  christos     D_CBC_RC5,
    334  1.1.1.2  christos     D_CBC_BF,
    335  1.1.1.2  christos     D_CBC_CAST,
    336  1.1.1.2  christos     D_CBC_128_AES,
    337  1.1.1.2  christos     D_CBC_192_AES,
    338  1.1.1.2  christos     D_CBC_256_AES,
    339  1.1.1.2  christos     D_CBC_128_CML,
    340  1.1.1.2  christos     D_CBC_192_CML,
    341  1.1.1.2  christos     D_CBC_256_CML,
    342  1.1.1.2  christos     D_EVP,
    343  1.1.1.2  christos     D_GHASH,
    344  1.1.1.2  christos     D_RAND,
    345  1.1.1.2  christos     D_EVP_CMAC,
    346  1.1.1.2  christos     D_KMAC128,
    347  1.1.1.2  christos     D_KMAC256,
    348      1.1  christos     ALGOR_NUM
    349      1.1  christos };
    350      1.1  christos /* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
    351      1.1  christos static const char *names[ALGOR_NUM] = {
    352      1.1  christos     "md2", "mdc2", "md4", "md5", "sha1", "rmd160",
    353      1.1  christos     "sha256", "sha512", "whirlpool", "hmac(sha256)",
    354      1.1  christos     "des-cbc", "des-ede3", "rc4", "idea-cbc", "seed-cbc",
    355      1.1  christos     "rc2-cbc", "rc5-cbc", "blowfish", "cast-cbc",
    356      1.1  christos     "aes-128-cbc", "aes-192-cbc", "aes-256-cbc",
    357      1.1  christos     "camellia-128-cbc", "camellia-192-cbc", "camellia-256-cbc",
    358      1.1  christos     "evp", "ghash", "rand", "cmac", "kmac128", "kmac256"
    359      1.1  christos };
    360      1.1  christos 
    361      1.1  christos /* list of configured algorithm (remaining), with some few alias */
    362      1.1  christos static const OPT_PAIR doit_choices[] = {
    363  1.1.1.2  christos     { "md2", D_MD2 },
    364  1.1.1.2  christos     { "mdc2", D_MDC2 },
    365  1.1.1.2  christos     { "md4", D_MD4 },
    366  1.1.1.2  christos     { "md5", D_MD5 },
    367  1.1.1.2  christos     { "hmac", D_HMAC },
    368  1.1.1.2  christos     { "sha1", D_SHA1 },
    369  1.1.1.2  christos     { "sha256", D_SHA256 },
    370  1.1.1.2  christos     { "sha512", D_SHA512 },
    371  1.1.1.2  christos     { "whirlpool", D_WHIRLPOOL },
    372  1.1.1.2  christos     { "ripemd", D_RMD160 },
    373  1.1.1.2  christos     { "rmd160", D_RMD160 },
    374  1.1.1.2  christos     { "ripemd160", D_RMD160 },
    375  1.1.1.2  christos     { "rc4", D_RC4 },
    376  1.1.1.2  christos     { "des-cbc", D_CBC_DES },
    377  1.1.1.2  christos     { "des-ede3", D_EDE3_DES },
    378  1.1.1.2  christos     { "aes-128-cbc", D_CBC_128_AES },
    379  1.1.1.2  christos     { "aes-192-cbc", D_CBC_192_AES },
    380  1.1.1.2  christos     { "aes-256-cbc", D_CBC_256_AES },
    381  1.1.1.2  christos     { "camellia-128-cbc", D_CBC_128_CML },
    382  1.1.1.2  christos     { "camellia-192-cbc", D_CBC_192_CML },
    383  1.1.1.2  christos     { "camellia-256-cbc", D_CBC_256_CML },
    384  1.1.1.2  christos     { "rc2-cbc", D_CBC_RC2 },
    385  1.1.1.2  christos     { "rc2", D_CBC_RC2 },
    386  1.1.1.2  christos     { "rc5-cbc", D_CBC_RC5 },
    387  1.1.1.2  christos     { "rc5", D_CBC_RC5 },
    388  1.1.1.2  christos     { "idea-cbc", D_CBC_IDEA },
    389  1.1.1.2  christos     { "idea", D_CBC_IDEA },
    390  1.1.1.2  christos     { "seed-cbc", D_CBC_SEED },
    391  1.1.1.2  christos     { "seed", D_CBC_SEED },
    392  1.1.1.2  christos     { "bf-cbc", D_CBC_BF },
    393  1.1.1.2  christos     { "blowfish", D_CBC_BF },
    394  1.1.1.2  christos     { "bf", D_CBC_BF },
    395  1.1.1.2  christos     { "cast-cbc", D_CBC_CAST },
    396  1.1.1.2  christos     { "cast", D_CBC_CAST },
    397  1.1.1.2  christos     { "cast5", D_CBC_CAST },
    398  1.1.1.2  christos     { "ghash", D_GHASH },
    399  1.1.1.2  christos     { "rand", D_RAND },
    400  1.1.1.2  christos     { "kmac128", D_KMAC128 },
    401  1.1.1.2  christos     { "kmac256", D_KMAC256 },
    402      1.1  christos };
    403      1.1  christos 
    404      1.1  christos static double results[ALGOR_NUM][SIZE_NUM];
    405      1.1  christos 
    406      1.1  christos #ifndef OPENSSL_NO_DSA
    407  1.1.1.2  christos enum { R_DSA_1024,
    408  1.1.1.2  christos     R_DSA_2048,
    409  1.1.1.2  christos     DSA_NUM };
    410      1.1  christos static const OPT_PAIR dsa_choices[DSA_NUM] = {
    411  1.1.1.2  christos     { "dsa1024", R_DSA_1024 },
    412  1.1.1.2  christos     { "dsa2048", R_DSA_2048 }
    413      1.1  christos };
    414  1.1.1.2  christos static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
    415      1.1  christos #endif /* OPENSSL_NO_DSA */
    416      1.1  christos 
    417      1.1  christos enum {
    418  1.1.1.2  christos     R_RSA_512,
    419  1.1.1.2  christos     R_RSA_1024,
    420  1.1.1.2  christos     R_RSA_2048,
    421  1.1.1.2  christos     R_RSA_3072,
    422  1.1.1.2  christos     R_RSA_4096,
    423  1.1.1.2  christos     R_RSA_7680,
    424  1.1.1.2  christos     R_RSA_15360,
    425  1.1.1.2  christos     RSA_NUM
    426      1.1  christos };
    427      1.1  christos static const OPT_PAIR rsa_choices[RSA_NUM] = {
    428  1.1.1.2  christos     { "rsa512", R_RSA_512 },
    429  1.1.1.2  christos     { "rsa1024", R_RSA_1024 },
    430  1.1.1.2  christos     { "rsa2048", R_RSA_2048 },
    431  1.1.1.2  christos     { "rsa3072", R_RSA_3072 },
    432  1.1.1.2  christos     { "rsa4096", R_RSA_4096 },
    433  1.1.1.2  christos     { "rsa7680", R_RSA_7680 },
    434  1.1.1.2  christos     { "rsa15360", R_RSA_15360 }
    435      1.1  christos };
    436      1.1  christos 
    437  1.1.1.2  christos static double rsa_results[RSA_NUM][4]; /* 4 ops: sign, verify, encrypt, decrypt */
    438      1.1  christos 
    439      1.1  christos #ifndef OPENSSL_NO_DH
    440      1.1  christos enum ff_params_t {
    441  1.1.1.2  christos     R_FFDH_2048,
    442  1.1.1.2  christos     R_FFDH_3072,
    443  1.1.1.2  christos     R_FFDH_4096,
    444  1.1.1.2  christos     R_FFDH_6144,
    445  1.1.1.2  christos     R_FFDH_8192,
    446  1.1.1.2  christos     FFDH_NUM
    447      1.1  christos };
    448      1.1  christos 
    449      1.1  christos static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
    450  1.1.1.2  christos     { "ffdh2048", R_FFDH_2048 },
    451  1.1.1.2  christos     { "ffdh3072", R_FFDH_3072 },
    452  1.1.1.2  christos     { "ffdh4096", R_FFDH_4096 },
    453  1.1.1.2  christos     { "ffdh6144", R_FFDH_6144 },
    454  1.1.1.2  christos     { "ffdh8192", R_FFDH_8192 },
    455      1.1  christos };
    456      1.1  christos 
    457  1.1.1.2  christos static double ffdh_results[FFDH_NUM][1]; /* 1 op: derivation */
    458      1.1  christos #endif /* OPENSSL_NO_DH */
    459      1.1  christos 
    460      1.1  christos enum ec_curves_t {
    461  1.1.1.2  christos     R_EC_P160,
    462  1.1.1.2  christos     R_EC_P192,
    463  1.1.1.2  christos     R_EC_P224,
    464  1.1.1.2  christos     R_EC_P256,
    465  1.1.1.2  christos     R_EC_P384,
    466  1.1.1.2  christos     R_EC_P521,
    467      1.1  christos #ifndef OPENSSL_NO_EC2M
    468  1.1.1.2  christos     R_EC_K163,
    469  1.1.1.2  christos     R_EC_K233,
    470  1.1.1.2  christos     R_EC_K283,
    471  1.1.1.2  christos     R_EC_K409,
    472  1.1.1.2  christos     R_EC_K571,
    473  1.1.1.2  christos     R_EC_B163,
    474  1.1.1.2  christos     R_EC_B233,
    475  1.1.1.2  christos     R_EC_B283,
    476  1.1.1.2  christos     R_EC_B409,
    477  1.1.1.2  christos     R_EC_B571,
    478  1.1.1.2  christos #endif
    479  1.1.1.2  christos     R_EC_BRP256R1,
    480  1.1.1.2  christos     R_EC_BRP256T1,
    481  1.1.1.2  christos     R_EC_BRP384R1,
    482  1.1.1.2  christos     R_EC_BRP384T1,
    483  1.1.1.2  christos     R_EC_BRP512R1,
    484  1.1.1.2  christos     R_EC_BRP512T1,
    485  1.1.1.2  christos     ECDSA_NUM
    486      1.1  christos };
    487      1.1  christos /* list of ecdsa curves */
    488      1.1  christos static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
    489  1.1.1.2  christos     { "ecdsap160", R_EC_P160 },
    490  1.1.1.2  christos     { "ecdsap192", R_EC_P192 },
    491  1.1.1.2  christos     { "ecdsap224", R_EC_P224 },
    492  1.1.1.2  christos     { "ecdsap256", R_EC_P256 },
    493  1.1.1.2  christos     { "ecdsap384", R_EC_P384 },
    494  1.1.1.2  christos     { "ecdsap521", R_EC_P521 },
    495      1.1  christos #ifndef OPENSSL_NO_EC2M
    496  1.1.1.2  christos     { "ecdsak163", R_EC_K163 },
    497  1.1.1.2  christos     { "ecdsak233", R_EC_K233 },
    498  1.1.1.2  christos     { "ecdsak283", R_EC_K283 },
    499  1.1.1.2  christos     { "ecdsak409", R_EC_K409 },
    500  1.1.1.2  christos     { "ecdsak571", R_EC_K571 },
    501  1.1.1.2  christos     { "ecdsab163", R_EC_B163 },
    502  1.1.1.2  christos     { "ecdsab233", R_EC_B233 },
    503  1.1.1.2  christos     { "ecdsab283", R_EC_B283 },
    504  1.1.1.2  christos     { "ecdsab409", R_EC_B409 },
    505  1.1.1.2  christos     { "ecdsab571", R_EC_B571 },
    506  1.1.1.2  christos #endif
    507  1.1.1.2  christos     { "ecdsabrp256r1", R_EC_BRP256R1 },
    508  1.1.1.2  christos     { "ecdsabrp256t1", R_EC_BRP256T1 },
    509  1.1.1.2  christos     { "ecdsabrp384r1", R_EC_BRP384R1 },
    510  1.1.1.2  christos     { "ecdsabrp384t1", R_EC_BRP384T1 },
    511  1.1.1.2  christos     { "ecdsabrp512r1", R_EC_BRP512R1 },
    512  1.1.1.2  christos     { "ecdsabrp512t1", R_EC_BRP512T1 }
    513      1.1  christos };
    514      1.1  christos enum {
    515      1.1  christos #ifndef OPENSSL_NO_ECX
    516  1.1.1.2  christos     R_EC_X25519 = ECDSA_NUM,
    517  1.1.1.2  christos     R_EC_X448,
    518  1.1.1.2  christos     EC_NUM
    519      1.1  christos #else
    520      1.1  christos     EC_NUM = ECDSA_NUM
    521      1.1  christos #endif
    522      1.1  christos };
    523      1.1  christos /* list of ecdh curves, extension of |ecdsa_choices| list above */
    524      1.1  christos static const OPT_PAIR ecdh_choices[EC_NUM] = {
    525  1.1.1.2  christos     { "ecdhp160", R_EC_P160 },
    526  1.1.1.2  christos     { "ecdhp192", R_EC_P192 },
    527  1.1.1.2  christos     { "ecdhp224", R_EC_P224 },
    528  1.1.1.2  christos     { "ecdhp256", R_EC_P256 },
    529  1.1.1.2  christos     { "ecdhp384", R_EC_P384 },
    530  1.1.1.2  christos     { "ecdhp521", R_EC_P521 },
    531      1.1  christos #ifndef OPENSSL_NO_EC2M
    532  1.1.1.2  christos     { "ecdhk163", R_EC_K163 },
    533  1.1.1.2  christos     { "ecdhk233", R_EC_K233 },
    534  1.1.1.2  christos     { "ecdhk283", R_EC_K283 },
    535  1.1.1.2  christos     { "ecdhk409", R_EC_K409 },
    536  1.1.1.2  christos     { "ecdhk571", R_EC_K571 },
    537  1.1.1.2  christos     { "ecdhb163", R_EC_B163 },
    538  1.1.1.2  christos     { "ecdhb233", R_EC_B233 },
    539  1.1.1.2  christos     { "ecdhb283", R_EC_B283 },
    540  1.1.1.2  christos     { "ecdhb409", R_EC_B409 },
    541  1.1.1.2  christos     { "ecdhb571", R_EC_B571 },
    542  1.1.1.2  christos #endif
    543  1.1.1.2  christos     { "ecdhbrp256r1", R_EC_BRP256R1 },
    544  1.1.1.2  christos     { "ecdhbrp256t1", R_EC_BRP256T1 },
    545  1.1.1.2  christos     { "ecdhbrp384r1", R_EC_BRP384R1 },
    546  1.1.1.2  christos     { "ecdhbrp384t1", R_EC_BRP384T1 },
    547  1.1.1.2  christos     { "ecdhbrp512r1", R_EC_BRP512R1 },
    548  1.1.1.2  christos     { "ecdhbrp512t1", R_EC_BRP512T1 },
    549      1.1  christos #ifndef OPENSSL_NO_ECX
    550  1.1.1.2  christos     { "ecdhx25519", R_EC_X25519 },
    551  1.1.1.2  christos     { "ecdhx448", R_EC_X448 }
    552      1.1  christos #endif
    553      1.1  christos };
    554      1.1  christos 
    555  1.1.1.2  christos static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
    556  1.1.1.2  christos static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
    557      1.1  christos 
    558      1.1  christos #ifndef OPENSSL_NO_ECX
    559  1.1.1.2  christos enum { R_EC_Ed25519,
    560  1.1.1.2  christos     R_EC_Ed448,
    561  1.1.1.2  christos     EdDSA_NUM };
    562      1.1  christos static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
    563  1.1.1.2  christos     { "ed25519", R_EC_Ed25519 },
    564  1.1.1.2  christos     { "ed448", R_EC_Ed448 }
    565      1.1  christos 
    566      1.1  christos };
    567  1.1.1.2  christos static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
    568      1.1  christos #endif /* OPENSSL_NO_ECX */
    569      1.1  christos 
    570      1.1  christos #ifndef OPENSSL_NO_SM2
    571  1.1.1.2  christos enum { R_EC_CURVESM2,
    572  1.1.1.2  christos     SM2_NUM };
    573      1.1  christos static const OPT_PAIR sm2_choices[SM2_NUM] = {
    574  1.1.1.2  christos     { "curveSM2", R_EC_CURVESM2 }
    575      1.1  christos };
    576  1.1.1.2  christos #define SM2_ID "TLSv1.3+GM+Cipher+Suite"
    577  1.1.1.2  christos #define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
    578  1.1.1.2  christos static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
    579      1.1  christos #endif /* OPENSSL_NO_SM2 */
    580      1.1  christos 
    581      1.1  christos #define MAX_KEM_NUM 111
    582      1.1  christos static size_t kems_algs_len = 0;
    583      1.1  christos static char *kems_algname[MAX_KEM_NUM] = { NULL };
    584  1.1.1.2  christos static double kems_results[MAX_KEM_NUM][3]; /* keygen, encaps, decaps */
    585      1.1  christos 
    586      1.1  christos #define MAX_SIG_NUM 256
    587      1.1  christos static size_t sigs_algs_len = 0;
    588      1.1  christos static char *sigs_algname[MAX_SIG_NUM] = { NULL };
    589  1.1.1.2  christos static double sigs_results[MAX_SIG_NUM][3]; /* keygen, sign, verify */
    590      1.1  christos 
    591      1.1  christos #define COND(unused_cond) (run && count < (testmode ? 1 : INT_MAX))
    592      1.1  christos #define COUNT(d) (count)
    593      1.1  christos 
    594      1.1  christos #define TAG_LEN 16 /* 16 bytes tag length works for all AEAD modes */
    595      1.1  christos #define AEAD_IVLEN 12 /* 12 bytes iv length works for all AEAD modes */
    596      1.1  christos 
    597      1.1  christos static unsigned int mode_op; /* AE Mode of operation */
    598      1.1  christos static unsigned int aead = 0; /* AEAD flag */
    599      1.1  christos static unsigned char aead_iv[AEAD_IVLEN]; /* For AEAD modes */
    600      1.1  christos static unsigned char aad[EVP_AEAD_TLS1_AAD_LEN] = { 0xcc };
    601      1.1  christos 
    602      1.1  christos typedef struct loopargs_st {
    603      1.1  christos     ASYNC_JOB *inprogress_job;
    604      1.1  christos     ASYNC_WAIT_CTX *wait_ctx;
    605      1.1  christos     unsigned char *buf;
    606      1.1  christos     unsigned char *buf2;
    607      1.1  christos     unsigned char *buf_malloc;
    608      1.1  christos     unsigned char *buf2_malloc;
    609      1.1  christos     unsigned char *key;
    610      1.1  christos     unsigned char tag[TAG_LEN];
    611      1.1  christos     size_t buflen;
    612      1.1  christos     size_t sigsize;
    613      1.1  christos     size_t encsize;
    614      1.1  christos     EVP_PKEY_CTX *rsa_sign_ctx[RSA_NUM];
    615      1.1  christos     EVP_PKEY_CTX *rsa_verify_ctx[RSA_NUM];
    616      1.1  christos     EVP_PKEY_CTX *rsa_encrypt_ctx[RSA_NUM];
    617      1.1  christos     EVP_PKEY_CTX *rsa_decrypt_ctx[RSA_NUM];
    618      1.1  christos #ifndef OPENSSL_NO_DSA
    619      1.1  christos     EVP_PKEY_CTX *dsa_sign_ctx[DSA_NUM];
    620      1.1  christos     EVP_PKEY_CTX *dsa_verify_ctx[DSA_NUM];
    621      1.1  christos #endif
    622      1.1  christos     EVP_PKEY_CTX *ecdsa_sign_ctx[ECDSA_NUM];
    623      1.1  christos     EVP_PKEY_CTX *ecdsa_verify_ctx[ECDSA_NUM];
    624      1.1  christos     EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
    625      1.1  christos #ifndef OPENSSL_NO_ECX
    626      1.1  christos     EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
    627      1.1  christos     EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
    628      1.1  christos #endif /* OPENSSL_NO_ECX */
    629      1.1  christos #ifndef OPENSSL_NO_SM2
    630      1.1  christos     EVP_MD_CTX *sm2_ctx[SM2_NUM];
    631      1.1  christos     EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
    632      1.1  christos     EVP_PKEY *sm2_pkey[SM2_NUM];
    633      1.1  christos #endif
    634      1.1  christos     unsigned char *secret_a;
    635      1.1  christos     unsigned char *secret_b;
    636      1.1  christos     size_t outlen[EC_NUM];
    637      1.1  christos #ifndef OPENSSL_NO_DH
    638      1.1  christos     EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
    639      1.1  christos     unsigned char *secret_ff_a;
    640      1.1  christos     unsigned char *secret_ff_b;
    641      1.1  christos #endif
    642      1.1  christos     EVP_CIPHER_CTX *ctx;
    643      1.1  christos     EVP_MAC_CTX *mctx;
    644      1.1  christos     EVP_PKEY_CTX *kem_gen_ctx[MAX_KEM_NUM];
    645      1.1  christos     EVP_PKEY_CTX *kem_encaps_ctx[MAX_KEM_NUM];
    646      1.1  christos     EVP_PKEY_CTX *kem_decaps_ctx[MAX_KEM_NUM];
    647      1.1  christos     size_t kem_out_len[MAX_KEM_NUM];
    648      1.1  christos     size_t kem_secret_len[MAX_KEM_NUM];
    649      1.1  christos     unsigned char *kem_out[MAX_KEM_NUM];
    650      1.1  christos     unsigned char *kem_send_secret[MAX_KEM_NUM];
    651      1.1  christos     unsigned char *kem_rcv_secret[MAX_KEM_NUM];
    652      1.1  christos     EVP_PKEY_CTX *sig_gen_ctx[MAX_SIG_NUM];
    653      1.1  christos     EVP_PKEY_CTX *sig_sign_ctx[MAX_SIG_NUM];
    654      1.1  christos     EVP_PKEY_CTX *sig_verify_ctx[MAX_SIG_NUM];
    655      1.1  christos     size_t sig_max_sig_len[MAX_SIG_NUM];
    656      1.1  christos     size_t sig_act_sig_len[MAX_SIG_NUM];
    657      1.1  christos     unsigned char *sig_sig[MAX_SIG_NUM];
    658      1.1  christos } loopargs_t;
    659  1.1.1.2  christos static int run_benchmark(int async_jobs, int (*loop_function)(void *),
    660  1.1.1.2  christos     loopargs_t *loopargs);
    661      1.1  christos 
    662      1.1  christos static unsigned int testnum;
    663      1.1  christos 
    664      1.1  christos static char *evp_mac_mdname = "sha256";
    665      1.1  christos static char *evp_hmac_name = NULL;
    666      1.1  christos static const char *evp_md_name = NULL;
    667      1.1  christos static char *evp_mac_ciphername = "aes-128-cbc";
    668      1.1  christos static char *evp_cmac_name = NULL;
    669      1.1  christos 
    670      1.1  christos static void dofail(void)
    671      1.1  christos {
    672      1.1  christos     ERR_print_errors(bio_err);
    673      1.1  christos     testmoderesult = 1;
    674      1.1  christos }
    675      1.1  christos 
    676      1.1  christos static int have_md(const char *name)
    677      1.1  christos {
    678      1.1  christos     int ret = 0;
    679      1.1  christos     EVP_MD *md = NULL;
    680      1.1  christos 
    681      1.1  christos     if (opt_md_silent(name, &md)) {
    682      1.1  christos         EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    683      1.1  christos 
    684      1.1  christos         if (ctx != NULL && EVP_DigestInit(ctx, md) > 0)
    685      1.1  christos             ret = 1;
    686      1.1  christos         EVP_MD_CTX_free(ctx);
    687      1.1  christos         EVP_MD_free(md);
    688      1.1  christos     }
    689      1.1  christos     return ret;
    690      1.1  christos }
    691      1.1  christos 
    692      1.1  christos static int have_cipher(const char *name)
    693      1.1  christos {
    694      1.1  christos     int ret = 0;
    695      1.1  christos     EVP_CIPHER *cipher = NULL;
    696      1.1  christos 
    697      1.1  christos     if (opt_cipher_silent(name, &cipher)) {
    698      1.1  christos         EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    699      1.1  christos 
    700      1.1  christos         if (ctx != NULL
    701      1.1  christos             && EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) > 0)
    702      1.1  christos             ret = 1;
    703      1.1  christos         EVP_CIPHER_CTX_free(ctx);
    704      1.1  christos         EVP_CIPHER_free(cipher);
    705      1.1  christos     }
    706      1.1  christos     return ret;
    707      1.1  christos }
    708      1.1  christos 
    709      1.1  christos static int EVP_Digest_loop(const char *mdname, ossl_unused int algindex, void *args)
    710      1.1  christos {
    711  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
    712      1.1  christos     unsigned char *buf = tempargs->buf;
    713      1.1  christos     unsigned char digest[EVP_MAX_MD_SIZE];
    714      1.1  christos     int count;
    715      1.1  christos     EVP_MD *md = NULL;
    716      1.1  christos     EVP_MD_CTX *ctx = NULL;
    717      1.1  christos 
    718      1.1  christos     if (!opt_md_silent(mdname, &md))
    719      1.1  christos         return -1;
    720      1.1  christos     if (EVP_MD_xof(md)) {
    721      1.1  christos         ctx = EVP_MD_CTX_new();
    722      1.1  christos         if (ctx == NULL) {
    723      1.1  christos             count = -1;
    724      1.1  christos             goto out;
    725      1.1  christos         }
    726      1.1  christos 
    727      1.1  christos         for (count = 0; COND(c[algindex][testnum]); count++) {
    728  1.1.1.2  christos             if (!EVP_DigestInit_ex2(ctx, md, NULL)
    729  1.1.1.2  christos                 || !EVP_DigestUpdate(ctx, buf, (size_t)lengths[testnum])
    730  1.1.1.2  christos                 || !EVP_DigestFinalXOF(ctx, digest, sizeof(digest))) {
    731      1.1  christos                 count = -1;
    732      1.1  christos                 break;
    733      1.1  christos             }
    734      1.1  christos         }
    735      1.1  christos     } else {
    736      1.1  christos         for (count = 0; COND(c[algindex][testnum]); count++) {
    737      1.1  christos             if (!EVP_Digest(buf, (size_t)lengths[testnum], digest, NULL, md,
    738  1.1.1.2  christos                     NULL)) {
    739      1.1  christos                 count = -1;
    740      1.1  christos                 break;
    741      1.1  christos             }
    742      1.1  christos         }
    743      1.1  christos     }
    744      1.1  christos out:
    745      1.1  christos     EVP_MD_free(md);
    746      1.1  christos     EVP_MD_CTX_free(ctx);
    747      1.1  christos     return count;
    748      1.1  christos }
    749      1.1  christos 
    750      1.1  christos static int EVP_Digest_md_loop(void *args)
    751      1.1  christos {
    752      1.1  christos     return EVP_Digest_loop(evp_md_name, D_EVP, args);
    753      1.1  christos }
    754      1.1  christos 
    755      1.1  christos static int EVP_Digest_MD2_loop(void *args)
    756      1.1  christos {
    757      1.1  christos     return EVP_Digest_loop("md2", D_MD2, args);
    758      1.1  christos }
    759      1.1  christos 
    760      1.1  christos static int EVP_Digest_MDC2_loop(void *args)
    761      1.1  christos {
    762      1.1  christos     return EVP_Digest_loop("mdc2", D_MDC2, args);
    763      1.1  christos }
    764      1.1  christos 
    765      1.1  christos static int EVP_Digest_MD4_loop(void *args)
    766      1.1  christos {
    767      1.1  christos     return EVP_Digest_loop("md4", D_MD4, args);
    768      1.1  christos }
    769      1.1  christos 
    770      1.1  christos static int MD5_loop(void *args)
    771      1.1  christos {
    772      1.1  christos     return EVP_Digest_loop("md5", D_MD5, args);
    773      1.1  christos }
    774      1.1  christos 
    775      1.1  christos static int mac_setup(const char *name,
    776  1.1.1.2  christos     EVP_MAC **mac, OSSL_PARAM params[],
    777  1.1.1.2  christos     loopargs_t *loopargs, unsigned int loopargs_len)
    778      1.1  christos {
    779      1.1  christos     unsigned int i;
    780      1.1  christos 
    781      1.1  christos     *mac = EVP_MAC_fetch(app_get0_libctx(), name, app_get0_propq());
    782      1.1  christos     if (*mac == NULL)
    783      1.1  christos         return 0;
    784      1.1  christos 
    785      1.1  christos     for (i = 0; i < loopargs_len; i++) {
    786      1.1  christos         loopargs[i].mctx = EVP_MAC_CTX_new(*mac);
    787      1.1  christos         if (loopargs[i].mctx == NULL)
    788      1.1  christos             return 0;
    789      1.1  christos 
    790      1.1  christos         if (!EVP_MAC_CTX_set_params(loopargs[i].mctx, params))
    791      1.1  christos             return 0;
    792      1.1  christos     }
    793      1.1  christos 
    794      1.1  christos     return 1;
    795      1.1  christos }
    796      1.1  christos 
    797      1.1  christos static void mac_teardown(EVP_MAC **mac,
    798  1.1.1.2  christos     loopargs_t *loopargs, unsigned int loopargs_len)
    799      1.1  christos {
    800      1.1  christos     unsigned int i;
    801      1.1  christos 
    802      1.1  christos     for (i = 0; i < loopargs_len; i++)
    803      1.1  christos         EVP_MAC_CTX_free(loopargs[i].mctx);
    804      1.1  christos     EVP_MAC_free(*mac);
    805      1.1  christos     *mac = NULL;
    806      1.1  christos 
    807      1.1  christos     return;
    808      1.1  christos }
    809      1.1  christos 
    810      1.1  christos static int EVP_MAC_loop(ossl_unused int algindex, void *args)
    811      1.1  christos {
    812  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
    813      1.1  christos     unsigned char *buf = tempargs->buf;
    814      1.1  christos     EVP_MAC_CTX *mctx = tempargs->mctx;
    815      1.1  christos     unsigned char mac[EVP_MAX_MD_SIZE];
    816      1.1  christos     int count;
    817      1.1  christos 
    818      1.1  christos     for (count = 0; COND(c[algindex][testnum]); count++) {
    819      1.1  christos         size_t outl;
    820      1.1  christos 
    821      1.1  christos         if (!EVP_MAC_init(mctx, NULL, 0, NULL)
    822      1.1  christos             || !EVP_MAC_update(mctx, buf, lengths[testnum])
    823      1.1  christos             || !EVP_MAC_final(mctx, mac, &outl, sizeof(mac)))
    824      1.1  christos             return -1;
    825      1.1  christos     }
    826      1.1  christos     return count;
    827      1.1  christos }
    828      1.1  christos 
    829      1.1  christos static int HMAC_loop(void *args)
    830      1.1  christos {
    831      1.1  christos     return EVP_MAC_loop(D_HMAC, args);
    832      1.1  christos }
    833      1.1  christos 
    834      1.1  christos static int CMAC_loop(void *args)
    835      1.1  christos {
    836      1.1  christos     return EVP_MAC_loop(D_EVP_CMAC, args);
    837      1.1  christos }
    838      1.1  christos 
    839      1.1  christos static int KMAC128_loop(void *args)
    840      1.1  christos {
    841      1.1  christos     return EVP_MAC_loop(D_KMAC128, args);
    842      1.1  christos }
    843      1.1  christos 
    844      1.1  christos static int KMAC256_loop(void *args)
    845      1.1  christos {
    846      1.1  christos     return EVP_MAC_loop(D_KMAC256, args);
    847      1.1  christos }
    848      1.1  christos 
    849      1.1  christos static int SHA1_loop(void *args)
    850      1.1  christos {
    851      1.1  christos     return EVP_Digest_loop("sha1", D_SHA1, args);
    852      1.1  christos }
    853      1.1  christos 
    854      1.1  christos static int SHA256_loop(void *args)
    855      1.1  christos {
    856      1.1  christos     return EVP_Digest_loop("sha256", D_SHA256, args);
    857      1.1  christos }
    858      1.1  christos 
    859      1.1  christos static int SHA512_loop(void *args)
    860      1.1  christos {
    861      1.1  christos     return EVP_Digest_loop("sha512", D_SHA512, args);
    862      1.1  christos }
    863      1.1  christos 
    864      1.1  christos static int WHIRLPOOL_loop(void *args)
    865      1.1  christos {
    866      1.1  christos     return EVP_Digest_loop("whirlpool", D_WHIRLPOOL, args);
    867      1.1  christos }
    868      1.1  christos 
    869      1.1  christos static int EVP_Digest_RMD160_loop(void *args)
    870      1.1  christos {
    871      1.1  christos     return EVP_Digest_loop("ripemd160", D_RMD160, args);
    872      1.1  christos }
    873      1.1  christos 
    874      1.1  christos static int algindex;
    875      1.1  christos 
    876      1.1  christos static int EVP_Cipher_loop(void *args)
    877      1.1  christos {
    878  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
    879      1.1  christos     unsigned char *buf = tempargs->buf;
    880      1.1  christos     int count;
    881      1.1  christos 
    882      1.1  christos     if (tempargs->ctx == NULL)
    883      1.1  christos         return -1;
    884      1.1  christos     for (count = 0; COND(c[algindex][testnum]); count++)
    885      1.1  christos         if (EVP_Cipher(tempargs->ctx, buf, buf, (size_t)lengths[testnum]) <= 0)
    886      1.1  christos             return -1;
    887      1.1  christos     return count;
    888      1.1  christos }
    889      1.1  christos 
    890      1.1  christos static int GHASH_loop(void *args)
    891      1.1  christos {
    892  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
    893      1.1  christos     unsigned char *buf = tempargs->buf;
    894      1.1  christos     EVP_MAC_CTX *mctx = tempargs->mctx;
    895      1.1  christos     int count;
    896      1.1  christos 
    897      1.1  christos     /* just do the update in the loop to be comparable with 1.1.1 */
    898      1.1  christos     for (count = 0; COND(c[D_GHASH][testnum]); count++) {
    899      1.1  christos         if (!EVP_MAC_update(mctx, buf, lengths[testnum]))
    900      1.1  christos             return -1;
    901      1.1  christos     }
    902      1.1  christos     return count;
    903      1.1  christos }
    904      1.1  christos 
    905      1.1  christos #define MAX_BLOCK_SIZE 128
    906      1.1  christos 
    907      1.1  christos static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
    908      1.1  christos 
    909      1.1  christos static EVP_CIPHER_CTX *init_evp_cipher_ctx(const char *ciphername,
    910  1.1.1.2  christos     const unsigned char *key,
    911  1.1.1.2  christos     int keylen)
    912      1.1  christos {
    913      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
    914      1.1  christos     EVP_CIPHER *cipher = NULL;
    915      1.1  christos 
    916      1.1  christos     if (!opt_cipher_silent(ciphername, &cipher))
    917      1.1  christos         return NULL;
    918      1.1  christos 
    919      1.1  christos     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
    920      1.1  christos         goto end;
    921      1.1  christos 
    922      1.1  christos     if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1)) {
    923      1.1  christos         EVP_CIPHER_CTX_free(ctx);
    924      1.1  christos         ctx = NULL;
    925      1.1  christos         goto end;
    926      1.1  christos     }
    927      1.1  christos 
    928      1.1  christos     if (EVP_CIPHER_CTX_set_key_length(ctx, keylen) <= 0) {
    929      1.1  christos         EVP_CIPHER_CTX_free(ctx);
    930      1.1  christos         ctx = NULL;
    931      1.1  christos         goto end;
    932      1.1  christos     }
    933      1.1  christos 
    934      1.1  christos     if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1)) {
    935      1.1  christos         EVP_CIPHER_CTX_free(ctx);
    936      1.1  christos         ctx = NULL;
    937      1.1  christos         goto end;
    938      1.1  christos     }
    939      1.1  christos 
    940      1.1  christos end:
    941      1.1  christos     EVP_CIPHER_free(cipher);
    942      1.1  christos     return ctx;
    943      1.1  christos }
    944      1.1  christos 
    945      1.1  christos static int RAND_bytes_loop(void *args)
    946      1.1  christos {
    947  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
    948      1.1  christos     unsigned char *buf = tempargs->buf;
    949      1.1  christos     int count;
    950      1.1  christos 
    951      1.1  christos     for (count = 0; COND(c[D_RAND][testnum]); count++)
    952      1.1  christos         RAND_bytes(buf, lengths[testnum]);
    953      1.1  christos     return count;
    954      1.1  christos }
    955      1.1  christos 
    956      1.1  christos static int decrypt = 0;
    957      1.1  christos static int EVP_Update_loop(void *args)
    958      1.1  christos {
    959  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
    960      1.1  christos     unsigned char *buf = tempargs->buf;
    961      1.1  christos     EVP_CIPHER_CTX *ctx = tempargs->ctx;
    962      1.1  christos     int outl, count, rc;
    963      1.1  christos 
    964      1.1  christos     if (decrypt) {
    965      1.1  christos         for (count = 0; COND(c[D_EVP][testnum]); count++) {
    966      1.1  christos             rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    967      1.1  christos             if (rc != 1) {
    968      1.1  christos                 /* reset iv in case of counter overflow */
    969      1.1  christos                 rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
    970      1.1  christos             }
    971      1.1  christos         }
    972      1.1  christos     } else {
    973      1.1  christos         for (count = 0; COND(c[D_EVP][testnum]); count++) {
    974      1.1  christos             rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
    975      1.1  christos             if (rc != 1) {
    976      1.1  christos                 /* reset iv in case of counter overflow */
    977      1.1  christos                 rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
    978      1.1  christos             }
    979      1.1  christos         }
    980      1.1  christos     }
    981      1.1  christos     if (decrypt)
    982      1.1  christos         rc = EVP_DecryptFinal_ex(ctx, buf, &outl);
    983      1.1  christos     else
    984      1.1  christos         rc = EVP_EncryptFinal_ex(ctx, buf, &outl);
    985      1.1  christos 
    986      1.1  christos     if (rc == 0)
    987      1.1  christos         BIO_printf(bio_err, "Error finalizing cipher loop\n");
    988      1.1  christos     return count;
    989      1.1  christos }
    990      1.1  christos 
    991      1.1  christos /*
    992      1.1  christos  * To make AEAD benchmarking more relevant perform TLS-like operations,
    993      1.1  christos  * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
    994      1.1  christos  * payload length is not actually limited by 16KB...
    995      1.1  christos  * CCM does not support streaming. For the purpose of performance measurement,
    996      1.1  christos  * each message is encrypted using the same (key,iv)-pair. Do not use this
    997      1.1  christos  * code in your application.
    998      1.1  christos  */
    999      1.1  christos static int EVP_Update_loop_aead_enc(void *args)
   1000      1.1  christos {
   1001  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1002      1.1  christos     unsigned char *buf = tempargs->buf;
   1003      1.1  christos     unsigned char *key = tempargs->key;
   1004      1.1  christos     EVP_CIPHER_CTX *ctx = tempargs->ctx;
   1005      1.1  christos     int outl, count, realcount = 0;
   1006      1.1  christos 
   1007      1.1  christos     for (count = 0; COND(c[D_EVP][testnum]); count++) {
   1008      1.1  christos         /* Set length of iv (Doesn't apply to SIV mode) */
   1009      1.1  christos         if (mode_op != EVP_CIPH_SIV_MODE) {
   1010      1.1  christos             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
   1011  1.1.1.2  christos                     sizeof(aead_iv), NULL)) {
   1012      1.1  christos                 BIO_printf(bio_err, "\nFailed to set iv length\n");
   1013      1.1  christos                 dofail();
   1014      1.1  christos                 exit(1);
   1015      1.1  christos             }
   1016      1.1  christos         }
   1017      1.1  christos         /* Set tag_len (Not for GCM/SIV at encryption stage) */
   1018      1.1  christos         if (mode_op != EVP_CIPH_GCM_MODE
   1019      1.1  christos             && mode_op != EVP_CIPH_SIV_MODE
   1020      1.1  christos             && mode_op != EVP_CIPH_GCM_SIV_MODE) {
   1021      1.1  christos             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
   1022  1.1.1.2  christos                     TAG_LEN, NULL)) {
   1023      1.1  christos                 BIO_printf(bio_err, "\nFailed to set tag length\n");
   1024      1.1  christos                 dofail();
   1025      1.1  christos                 exit(1);
   1026      1.1  christos             }
   1027      1.1  christos         }
   1028      1.1  christos         if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, aead_iv, -1)) {
   1029      1.1  christos             BIO_printf(bio_err, "\nFailed to set key and iv\n");
   1030      1.1  christos             dofail();
   1031      1.1  christos             exit(1);
   1032      1.1  christos         }
   1033      1.1  christos         /* Set total length of input. Only required for CCM */
   1034      1.1  christos         if (mode_op == EVP_CIPH_CCM_MODE) {
   1035      1.1  christos             if (!EVP_EncryptUpdate(ctx, NULL, &outl,
   1036  1.1.1.2  christos                     NULL, lengths[testnum])) {
   1037      1.1  christos                 BIO_printf(bio_err, "\nCouldn't set input text length\n");
   1038      1.1  christos                 dofail();
   1039      1.1  christos                 exit(1);
   1040      1.1  christos             }
   1041      1.1  christos         }
   1042      1.1  christos         if (aead) {
   1043      1.1  christos             if (!EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad))) {
   1044      1.1  christos                 BIO_printf(bio_err, "\nCouldn't insert AAD when encrypting\n");
   1045      1.1  christos                 dofail();
   1046      1.1  christos                 exit(1);
   1047      1.1  christos             }
   1048      1.1  christos         }
   1049      1.1  christos         if (!EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum])) {
   1050      1.1  christos             BIO_printf(bio_err, "\nFailed to encrypt the data\n");
   1051      1.1  christos             dofail();
   1052      1.1  christos             exit(1);
   1053      1.1  christos         }
   1054      1.1  christos         if (EVP_EncryptFinal_ex(ctx, buf, &outl))
   1055      1.1  christos             realcount++;
   1056      1.1  christos     }
   1057      1.1  christos     return realcount;
   1058      1.1  christos }
   1059      1.1  christos 
   1060      1.1  christos /*
   1061      1.1  christos  * To make AEAD benchmarking more relevant perform TLS-like operations,
   1062      1.1  christos  * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
   1063      1.1  christos  * payload length is not actually limited by 16KB...
   1064      1.1  christos  * CCM does not support streaming. For the purpose of performance measurement,
   1065      1.1  christos  * each message is decrypted using the same (key,iv)-pair. Do not use this
   1066      1.1  christos  * code in your application.
   1067      1.1  christos  * For decryption, we will use buf2 to preserve the input text in buf.
   1068      1.1  christos  */
   1069      1.1  christos static int EVP_Update_loop_aead_dec(void *args)
   1070      1.1  christos {
   1071  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1072      1.1  christos     unsigned char *buf = tempargs->buf;
   1073      1.1  christos     unsigned char *outbuf = tempargs->buf2;
   1074      1.1  christos     unsigned char *key = tempargs->key;
   1075      1.1  christos     unsigned char tag[TAG_LEN];
   1076      1.1  christos     EVP_CIPHER_CTX *ctx = tempargs->ctx;
   1077      1.1  christos     int outl, count, realcount = 0;
   1078      1.1  christos 
   1079      1.1  christos     for (count = 0; COND(c[D_EVP][testnum]); count++) {
   1080      1.1  christos         /* Set the length of iv (Doesn't apply to SIV mode) */
   1081      1.1  christos         if (mode_op != EVP_CIPH_SIV_MODE) {
   1082      1.1  christos             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
   1083  1.1.1.2  christos                     sizeof(aead_iv), NULL)) {
   1084      1.1  christos                 BIO_printf(bio_err, "\nFailed to set iv length\n");
   1085      1.1  christos                 dofail();
   1086      1.1  christos                 exit(1);
   1087      1.1  christos             }
   1088      1.1  christos         }
   1089      1.1  christos 
   1090      1.1  christos         /* Set the tag length (Doesn't apply to SIV mode) */
   1091      1.1  christos         if (mode_op != EVP_CIPH_SIV_MODE
   1092      1.1  christos             && mode_op != EVP_CIPH_GCM_MODE
   1093      1.1  christos             && mode_op != EVP_CIPH_GCM_SIV_MODE) {
   1094      1.1  christos             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
   1095  1.1.1.2  christos                     TAG_LEN, NULL)) {
   1096      1.1  christos                 BIO_printf(bio_err, "\nFailed to set tag length\n");
   1097      1.1  christos                 dofail();
   1098      1.1  christos                 exit(1);
   1099      1.1  christos             }
   1100      1.1  christos         }
   1101      1.1  christos         if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, aead_iv, -1)) {
   1102      1.1  christos             BIO_printf(bio_err, "\nFailed to set key and iv\n");
   1103      1.1  christos             dofail();
   1104      1.1  christos             exit(1);
   1105      1.1  christos         }
   1106      1.1  christos         /* Set iv before decryption (Doesn't apply to SIV mode) */
   1107      1.1  christos         if (mode_op != EVP_CIPH_SIV_MODE) {
   1108      1.1  christos             if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, aead_iv)) {
   1109      1.1  christos                 BIO_printf(bio_err, "\nFailed to set iv\n");
   1110      1.1  christos                 dofail();
   1111      1.1  christos                 exit(1);
   1112      1.1  christos             }
   1113      1.1  christos         }
   1114      1.1  christos         memcpy(tag, tempargs->tag, TAG_LEN);
   1115      1.1  christos 
   1116      1.1  christos         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
   1117  1.1.1.2  christos                 TAG_LEN, tag)) {
   1118      1.1  christos             BIO_printf(bio_err, "\nFailed to set tag\n");
   1119      1.1  christos             dofail();
   1120      1.1  christos             exit(1);
   1121      1.1  christos         }
   1122      1.1  christos         /* Set the total length of cipher text. Only required for CCM */
   1123      1.1  christos         if (mode_op == EVP_CIPH_CCM_MODE) {
   1124      1.1  christos             if (!EVP_DecryptUpdate(ctx, NULL, &outl,
   1125  1.1.1.2  christos                     NULL, lengths[testnum])) {
   1126      1.1  christos                 BIO_printf(bio_err, "\nCouldn't set cipher text length\n");
   1127      1.1  christos                 dofail();
   1128      1.1  christos                 exit(1);
   1129      1.1  christos             }
   1130      1.1  christos         }
   1131      1.1  christos         if (aead) {
   1132      1.1  christos             if (!EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad))) {
   1133      1.1  christos                 BIO_printf(bio_err, "\nCouldn't insert AAD when decrypting\n");
   1134      1.1  christos                 dofail();
   1135      1.1  christos                 exit(1);
   1136      1.1  christos             }
   1137      1.1  christos         }
   1138      1.1  christos         if (!EVP_DecryptUpdate(ctx, outbuf, &outl, buf, lengths[testnum])) {
   1139      1.1  christos             BIO_printf(bio_err, "\nFailed to decrypt the data\n");
   1140      1.1  christos             dofail();
   1141      1.1  christos             exit(1);
   1142      1.1  christos         }
   1143      1.1  christos         if (EVP_DecryptFinal_ex(ctx, outbuf, &outl))
   1144      1.1  christos             realcount++;
   1145      1.1  christos     }
   1146      1.1  christos     return realcount;
   1147      1.1  christos }
   1148      1.1  christos 
   1149      1.1  christos static int RSA_sign_loop(void *args)
   1150      1.1  christos {
   1151  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1152      1.1  christos     unsigned char *buf = tempargs->buf;
   1153      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1154      1.1  christos     size_t *rsa_num = &tempargs->sigsize;
   1155      1.1  christos     EVP_PKEY_CTX **rsa_sign_ctx = tempargs->rsa_sign_ctx;
   1156      1.1  christos     int ret, count;
   1157      1.1  christos 
   1158      1.1  christos     for (count = 0; COND(rsa_c[testnum][0]); count++) {
   1159      1.1  christos         *rsa_num = tempargs->buflen;
   1160      1.1  christos         ret = EVP_PKEY_sign(rsa_sign_ctx[testnum], buf2, rsa_num, buf, 36);
   1161      1.1  christos         if (ret <= 0) {
   1162      1.1  christos             BIO_printf(bio_err, "RSA sign failure\n");
   1163      1.1  christos             dofail();
   1164      1.1  christos             count = -1;
   1165      1.1  christos             break;
   1166      1.1  christos         }
   1167      1.1  christos     }
   1168      1.1  christos     return count;
   1169      1.1  christos }
   1170      1.1  christos 
   1171      1.1  christos static int RSA_verify_loop(void *args)
   1172      1.1  christos {
   1173  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1174      1.1  christos     unsigned char *buf = tempargs->buf;
   1175      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1176      1.1  christos     size_t rsa_num = tempargs->sigsize;
   1177      1.1  christos     EVP_PKEY_CTX **rsa_verify_ctx = tempargs->rsa_verify_ctx;
   1178      1.1  christos     int ret, count;
   1179      1.1  christos 
   1180      1.1  christos     for (count = 0; COND(rsa_c[testnum][1]); count++) {
   1181      1.1  christos         ret = EVP_PKEY_verify(rsa_verify_ctx[testnum], buf2, rsa_num, buf, 36);
   1182      1.1  christos         if (ret <= 0) {
   1183      1.1  christos             BIO_printf(bio_err, "RSA verify failure\n");
   1184      1.1  christos             dofail();
   1185      1.1  christos             count = -1;
   1186      1.1  christos             break;
   1187      1.1  christos         }
   1188      1.1  christos     }
   1189      1.1  christos     return count;
   1190      1.1  christos }
   1191      1.1  christos 
   1192      1.1  christos static int RSA_encrypt_loop(void *args)
   1193      1.1  christos {
   1194  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1195      1.1  christos     unsigned char *buf = tempargs->buf;
   1196      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1197      1.1  christos     size_t *rsa_num = &tempargs->encsize;
   1198      1.1  christos     EVP_PKEY_CTX **rsa_encrypt_ctx = tempargs->rsa_encrypt_ctx;
   1199      1.1  christos     int ret, count;
   1200      1.1  christos 
   1201      1.1  christos     for (count = 0; COND(rsa_c[testnum][2]); count++) {
   1202      1.1  christos         *rsa_num = tempargs->buflen;
   1203      1.1  christos         ret = EVP_PKEY_encrypt(rsa_encrypt_ctx[testnum], buf2, rsa_num, buf, 36);
   1204      1.1  christos         if (ret <= 0) {
   1205      1.1  christos             BIO_printf(bio_err, "RSA encrypt failure\n");
   1206      1.1  christos             dofail();
   1207      1.1  christos             count = -1;
   1208      1.1  christos             break;
   1209      1.1  christos         }
   1210      1.1  christos     }
   1211      1.1  christos     return count;
   1212      1.1  christos }
   1213      1.1  christos 
   1214      1.1  christos static int RSA_decrypt_loop(void *args)
   1215      1.1  christos {
   1216  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1217      1.1  christos     unsigned char *buf = tempargs->buf;
   1218      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1219      1.1  christos     size_t rsa_num;
   1220      1.1  christos     EVP_PKEY_CTX **rsa_decrypt_ctx = tempargs->rsa_decrypt_ctx;
   1221      1.1  christos     int ret, count;
   1222      1.1  christos 
   1223      1.1  christos     for (count = 0; COND(rsa_c[testnum][3]); count++) {
   1224      1.1  christos         rsa_num = tempargs->buflen;
   1225      1.1  christos         ret = EVP_PKEY_decrypt(rsa_decrypt_ctx[testnum], buf, &rsa_num, buf2, tempargs->encsize);
   1226      1.1  christos         if (ret <= 0) {
   1227      1.1  christos             BIO_printf(bio_err, "RSA decrypt failure\n");
   1228      1.1  christos             dofail();
   1229      1.1  christos             count = -1;
   1230      1.1  christos             break;
   1231      1.1  christos         }
   1232      1.1  christos     }
   1233      1.1  christos     return count;
   1234      1.1  christos }
   1235      1.1  christos 
   1236      1.1  christos #ifndef OPENSSL_NO_DH
   1237      1.1  christos 
   1238      1.1  christos static int FFDH_derive_key_loop(void *args)
   1239      1.1  christos {
   1240  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1241      1.1  christos     EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
   1242      1.1  christos     unsigned char *derived_secret = tempargs->secret_ff_a;
   1243      1.1  christos     int count;
   1244      1.1  christos 
   1245      1.1  christos     for (count = 0; COND(ffdh_c[testnum][0]); count++) {
   1246      1.1  christos         /* outlen can be overwritten with a too small value (no padding used) */
   1247      1.1  christos         size_t outlen = MAX_FFDH_SIZE;
   1248      1.1  christos 
   1249      1.1  christos         EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
   1250      1.1  christos     }
   1251      1.1  christos     return count;
   1252      1.1  christos }
   1253      1.1  christos #endif /* OPENSSL_NO_DH */
   1254      1.1  christos 
   1255      1.1  christos #ifndef OPENSSL_NO_DSA
   1256      1.1  christos static int DSA_sign_loop(void *args)
   1257      1.1  christos {
   1258  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1259      1.1  christos     unsigned char *buf = tempargs->buf;
   1260      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1261      1.1  christos     size_t *dsa_num = &tempargs->sigsize;
   1262      1.1  christos     EVP_PKEY_CTX **dsa_sign_ctx = tempargs->dsa_sign_ctx;
   1263      1.1  christos     int ret, count;
   1264      1.1  christos 
   1265      1.1  christos     for (count = 0; COND(dsa_c[testnum][0]); count++) {
   1266      1.1  christos         *dsa_num = tempargs->buflen;
   1267      1.1  christos         ret = EVP_PKEY_sign(dsa_sign_ctx[testnum], buf2, dsa_num, buf, 20);
   1268      1.1  christos         if (ret <= 0) {
   1269      1.1  christos             BIO_printf(bio_err, "DSA sign failure\n");
   1270      1.1  christos             dofail();
   1271      1.1  christos             count = -1;
   1272      1.1  christos             break;
   1273      1.1  christos         }
   1274      1.1  christos     }
   1275      1.1  christos     return count;
   1276      1.1  christos }
   1277      1.1  christos 
   1278      1.1  christos static int DSA_verify_loop(void *args)
   1279      1.1  christos {
   1280  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1281      1.1  christos     unsigned char *buf = tempargs->buf;
   1282      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1283      1.1  christos     size_t dsa_num = tempargs->sigsize;
   1284      1.1  christos     EVP_PKEY_CTX **dsa_verify_ctx = tempargs->dsa_verify_ctx;
   1285      1.1  christos     int ret, count;
   1286      1.1  christos 
   1287      1.1  christos     for (count = 0; COND(dsa_c[testnum][1]); count++) {
   1288      1.1  christos         ret = EVP_PKEY_verify(dsa_verify_ctx[testnum], buf2, dsa_num, buf, 20);
   1289      1.1  christos         if (ret <= 0) {
   1290      1.1  christos             BIO_printf(bio_err, "DSA verify failure\n");
   1291      1.1  christos             dofail();
   1292      1.1  christos             count = -1;
   1293      1.1  christos             break;
   1294      1.1  christos         }
   1295      1.1  christos     }
   1296      1.1  christos     return count;
   1297      1.1  christos }
   1298      1.1  christos #endif /* OPENSSL_NO_DSA */
   1299      1.1  christos 
   1300      1.1  christos static int ECDSA_sign_loop(void *args)
   1301      1.1  christos {
   1302  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1303      1.1  christos     unsigned char *buf = tempargs->buf;
   1304      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1305      1.1  christos     size_t *ecdsa_num = &tempargs->sigsize;
   1306      1.1  christos     EVP_PKEY_CTX **ecdsa_sign_ctx = tempargs->ecdsa_sign_ctx;
   1307      1.1  christos     int ret, count;
   1308      1.1  christos 
   1309      1.1  christos     for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
   1310      1.1  christos         *ecdsa_num = tempargs->buflen;
   1311      1.1  christos         ret = EVP_PKEY_sign(ecdsa_sign_ctx[testnum], buf2, ecdsa_num, buf, 20);
   1312      1.1  christos         if (ret <= 0) {
   1313      1.1  christos             BIO_printf(bio_err, "ECDSA sign failure\n");
   1314      1.1  christos             dofail();
   1315      1.1  christos             count = -1;
   1316      1.1  christos             break;
   1317      1.1  christos         }
   1318      1.1  christos     }
   1319      1.1  christos     return count;
   1320      1.1  christos }
   1321      1.1  christos 
   1322      1.1  christos static int ECDSA_verify_loop(void *args)
   1323      1.1  christos {
   1324  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1325      1.1  christos     unsigned char *buf = tempargs->buf;
   1326      1.1  christos     unsigned char *buf2 = tempargs->buf2;
   1327      1.1  christos     size_t ecdsa_num = tempargs->sigsize;
   1328      1.1  christos     EVP_PKEY_CTX **ecdsa_verify_ctx = tempargs->ecdsa_verify_ctx;
   1329      1.1  christos     int ret, count;
   1330      1.1  christos 
   1331      1.1  christos     for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
   1332      1.1  christos         ret = EVP_PKEY_verify(ecdsa_verify_ctx[testnum], buf2, ecdsa_num,
   1333  1.1.1.2  christos             buf, 20);
   1334      1.1  christos         if (ret <= 0) {
   1335      1.1  christos             BIO_printf(bio_err, "ECDSA verify failure\n");
   1336      1.1  christos             dofail();
   1337      1.1  christos             count = -1;
   1338      1.1  christos             break;
   1339      1.1  christos         }
   1340      1.1  christos     }
   1341      1.1  christos     return count;
   1342      1.1  christos }
   1343      1.1  christos 
   1344      1.1  christos /* ******************************************************************** */
   1345      1.1  christos 
   1346      1.1  christos static int ECDH_EVP_derive_key_loop(void *args)
   1347      1.1  christos {
   1348  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1349      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
   1350      1.1  christos     unsigned char *derived_secret = tempargs->secret_a;
   1351      1.1  christos     int count;
   1352      1.1  christos     size_t *outlen = &(tempargs->outlen[testnum]);
   1353      1.1  christos 
   1354      1.1  christos     for (count = 0; COND(ecdh_c[testnum][0]); count++)
   1355      1.1  christos         EVP_PKEY_derive(ctx, derived_secret, outlen);
   1356      1.1  christos 
   1357      1.1  christos     return count;
   1358      1.1  christos }
   1359      1.1  christos 
   1360      1.1  christos #ifndef OPENSSL_NO_ECX
   1361      1.1  christos static int EdDSA_sign_loop(void *args)
   1362      1.1  christos {
   1363  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1364      1.1  christos     unsigned char *buf = tempargs->buf;
   1365      1.1  christos     EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
   1366      1.1  christos     unsigned char *eddsasig = tempargs->buf2;
   1367      1.1  christos     size_t *eddsasigsize = &tempargs->sigsize;
   1368      1.1  christos     int ret, count;
   1369      1.1  christos 
   1370      1.1  christos     for (count = 0; COND(eddsa_c[testnum][0]); count++) {
   1371      1.1  christos         ret = EVP_DigestSignInit(edctx[testnum], NULL, NULL, NULL, NULL);
   1372      1.1  christos         if (ret == 0) {
   1373      1.1  christos             BIO_printf(bio_err, "EdDSA sign init failure\n");
   1374      1.1  christos             dofail();
   1375      1.1  christos             count = -1;
   1376      1.1  christos             break;
   1377      1.1  christos         }
   1378      1.1  christos         ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
   1379      1.1  christos         if (ret == 0) {
   1380      1.1  christos             BIO_printf(bio_err, "EdDSA sign failure\n");
   1381      1.1  christos             dofail();
   1382      1.1  christos             count = -1;
   1383      1.1  christos             break;
   1384      1.1  christos         }
   1385      1.1  christos     }
   1386      1.1  christos     return count;
   1387      1.1  christos }
   1388      1.1  christos 
   1389      1.1  christos static int EdDSA_verify_loop(void *args)
   1390      1.1  christos {
   1391  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1392      1.1  christos     unsigned char *buf = tempargs->buf;
   1393      1.1  christos     EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
   1394      1.1  christos     unsigned char *eddsasig = tempargs->buf2;
   1395      1.1  christos     size_t eddsasigsize = tempargs->sigsize;
   1396      1.1  christos     int ret, count;
   1397      1.1  christos 
   1398      1.1  christos     for (count = 0; COND(eddsa_c[testnum][1]); count++) {
   1399      1.1  christos         ret = EVP_DigestVerifyInit(edctx[testnum], NULL, NULL, NULL, NULL);
   1400      1.1  christos         if (ret == 0) {
   1401      1.1  christos             BIO_printf(bio_err, "EdDSA verify init failure\n");
   1402      1.1  christos             dofail();
   1403      1.1  christos             count = -1;
   1404      1.1  christos             break;
   1405      1.1  christos         }
   1406      1.1  christos         ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
   1407      1.1  christos         if (ret != 1) {
   1408      1.1  christos             BIO_printf(bio_err, "EdDSA verify failure\n");
   1409      1.1  christos             dofail();
   1410      1.1  christos             count = -1;
   1411      1.1  christos             break;
   1412      1.1  christos         }
   1413      1.1  christos     }
   1414      1.1  christos     return count;
   1415      1.1  christos }
   1416      1.1  christos #endif /* OPENSSL_NO_ECX */
   1417      1.1  christos 
   1418      1.1  christos #ifndef OPENSSL_NO_SM2
   1419      1.1  christos static int SM2_sign_loop(void *args)
   1420      1.1  christos {
   1421  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1422      1.1  christos     unsigned char *buf = tempargs->buf;
   1423      1.1  christos     EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
   1424      1.1  christos     unsigned char *sm2sig = tempargs->buf2;
   1425      1.1  christos     size_t sm2sigsize;
   1426      1.1  christos     int ret, count;
   1427      1.1  christos     EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
   1428      1.1  christos     const size_t max_size = EVP_PKEY_get_size(sm2_pkey[testnum]);
   1429      1.1  christos 
   1430      1.1  christos     for (count = 0; COND(sm2_c[testnum][0]); count++) {
   1431      1.1  christos         sm2sigsize = max_size;
   1432      1.1  christos 
   1433      1.1  christos         if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
   1434  1.1.1.2  christos                 NULL, sm2_pkey[testnum])) {
   1435      1.1  christos             BIO_printf(bio_err, "SM2 init sign failure\n");
   1436      1.1  christos             dofail();
   1437      1.1  christos             count = -1;
   1438      1.1  christos             break;
   1439      1.1  christos         }
   1440      1.1  christos         ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
   1441  1.1.1.2  christos             buf, 20);
   1442      1.1  christos         if (ret == 0) {
   1443      1.1  christos             BIO_printf(bio_err, "SM2 sign failure\n");
   1444      1.1  christos             dofail();
   1445      1.1  christos             count = -1;
   1446      1.1  christos             break;
   1447      1.1  christos         }
   1448      1.1  christos         /* update the latest returned size and always use the fixed buffer size */
   1449      1.1  christos         tempargs->sigsize = sm2sigsize;
   1450      1.1  christos     }
   1451      1.1  christos 
   1452      1.1  christos     return count;
   1453      1.1  christos }
   1454      1.1  christos 
   1455      1.1  christos static int SM2_verify_loop(void *args)
   1456      1.1  christos {
   1457  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1458      1.1  christos     unsigned char *buf = tempargs->buf;
   1459      1.1  christos     EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
   1460      1.1  christos     unsigned char *sm2sig = tempargs->buf2;
   1461      1.1  christos     size_t sm2sigsize = tempargs->sigsize;
   1462      1.1  christos     int ret, count;
   1463      1.1  christos     EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
   1464      1.1  christos 
   1465      1.1  christos     for (count = 0; COND(sm2_c[testnum][1]); count++) {
   1466      1.1  christos         if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
   1467  1.1.1.2  christos                 NULL, sm2_pkey[testnum])) {
   1468      1.1  christos             BIO_printf(bio_err, "SM2 verify init failure\n");
   1469      1.1  christos             dofail();
   1470      1.1  christos             count = -1;
   1471      1.1  christos             break;
   1472      1.1  christos         }
   1473      1.1  christos         ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
   1474  1.1.1.2  christos             buf, 20);
   1475      1.1  christos         if (ret != 1) {
   1476      1.1  christos             BIO_printf(bio_err, "SM2 verify failure\n");
   1477      1.1  christos             dofail();
   1478      1.1  christos             count = -1;
   1479      1.1  christos             break;
   1480      1.1  christos         }
   1481      1.1  christos     }
   1482      1.1  christos     return count;
   1483      1.1  christos }
   1484  1.1.1.2  christos #endif /* OPENSSL_NO_SM2 */
   1485      1.1  christos 
   1486      1.1  christos static int KEM_keygen_loop(void *args)
   1487      1.1  christos {
   1488  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1489      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->kem_gen_ctx[testnum];
   1490      1.1  christos     EVP_PKEY *pkey = NULL;
   1491      1.1  christos     int count;
   1492      1.1  christos 
   1493      1.1  christos     for (count = 0; COND(kems_c[testnum][0]); count++) {
   1494      1.1  christos         if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
   1495      1.1  christos             return -1;
   1496      1.1  christos         /*
   1497      1.1  christos          * runtime defined to quite some degree by randomness,
   1498      1.1  christos          * so performance overhead of _free doesn't impact
   1499      1.1  christos          * results significantly. In any case this test is
   1500      1.1  christos          * meant to permit relative algorithm performance
   1501      1.1  christos          * comparison.
   1502      1.1  christos          */
   1503      1.1  christos         EVP_PKEY_free(pkey);
   1504      1.1  christos         pkey = NULL;
   1505      1.1  christos     }
   1506      1.1  christos     return count;
   1507      1.1  christos }
   1508      1.1  christos 
   1509      1.1  christos static int KEM_encaps_loop(void *args)
   1510      1.1  christos {
   1511  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1512      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->kem_encaps_ctx[testnum];
   1513      1.1  christos     size_t out_len = tempargs->kem_out_len[testnum];
   1514      1.1  christos     size_t secret_len = tempargs->kem_secret_len[testnum];
   1515      1.1  christos     unsigned char *out = tempargs->kem_out[testnum];
   1516      1.1  christos     unsigned char *secret = tempargs->kem_send_secret[testnum];
   1517      1.1  christos     int count;
   1518      1.1  christos 
   1519      1.1  christos     for (count = 0; COND(kems_c[testnum][1]); count++) {
   1520      1.1  christos         if (EVP_PKEY_encapsulate(ctx, out, &out_len, secret, &secret_len) <= 0)
   1521      1.1  christos             return -1;
   1522      1.1  christos     }
   1523      1.1  christos     return count;
   1524      1.1  christos }
   1525      1.1  christos 
   1526      1.1  christos static int KEM_decaps_loop(void *args)
   1527      1.1  christos {
   1528  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1529      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->kem_decaps_ctx[testnum];
   1530      1.1  christos     size_t out_len = tempargs->kem_out_len[testnum];
   1531      1.1  christos     size_t secret_len = tempargs->kem_secret_len[testnum];
   1532      1.1  christos     unsigned char *out = tempargs->kem_out[testnum];
   1533      1.1  christos     unsigned char *secret = tempargs->kem_send_secret[testnum];
   1534      1.1  christos     int count;
   1535      1.1  christos 
   1536      1.1  christos     for (count = 0; COND(kems_c[testnum][2]); count++) {
   1537      1.1  christos         if (EVP_PKEY_decapsulate(ctx, secret, &secret_len, out, out_len) <= 0)
   1538      1.1  christos             return -1;
   1539      1.1  christos     }
   1540      1.1  christos     return count;
   1541      1.1  christos }
   1542      1.1  christos 
   1543      1.1  christos static int SIG_keygen_loop(void *args)
   1544      1.1  christos {
   1545  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1546      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->sig_gen_ctx[testnum];
   1547      1.1  christos     EVP_PKEY *pkey = NULL;
   1548      1.1  christos     int count;
   1549      1.1  christos 
   1550      1.1  christos     for (count = 0; COND(kems_c[testnum][0]); count++) {
   1551      1.1  christos         EVP_PKEY_keygen(ctx, &pkey);
   1552      1.1  christos         /* TBD: How much does free influence runtime? */
   1553      1.1  christos         EVP_PKEY_free(pkey);
   1554      1.1  christos         pkey = NULL;
   1555      1.1  christos     }
   1556      1.1  christos     return count;
   1557      1.1  christos }
   1558      1.1  christos 
   1559      1.1  christos static int SIG_sign_loop(void *args)
   1560      1.1  christos {
   1561  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1562      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->sig_sign_ctx[testnum];
   1563      1.1  christos     /* be sure to not change stored sig: */
   1564      1.1  christos     unsigned char *sig = app_malloc(tempargs->sig_max_sig_len[testnum],
   1565  1.1.1.2  christos         "sig sign loop");
   1566      1.1  christos     unsigned char md[SHA256_DIGEST_LENGTH] = { 0 };
   1567      1.1  christos     size_t md_len = SHA256_DIGEST_LENGTH;
   1568      1.1  christos     int count;
   1569      1.1  christos 
   1570      1.1  christos     for (count = 0; COND(kems_c[testnum][1]); count++) {
   1571      1.1  christos         size_t sig_len = tempargs->sig_max_sig_len[testnum];
   1572      1.1  christos         int ret = EVP_PKEY_sign(ctx, sig, &sig_len, md, md_len);
   1573      1.1  christos 
   1574      1.1  christos         if (ret <= 0) {
   1575      1.1  christos             BIO_printf(bio_err, "SIG sign failure at count %d\n", count);
   1576      1.1  christos             dofail();
   1577      1.1  christos             count = -1;
   1578      1.1  christos             break;
   1579      1.1  christos         }
   1580      1.1  christos     }
   1581      1.1  christos     OPENSSL_free(sig);
   1582      1.1  christos     return count;
   1583      1.1  christos }
   1584      1.1  christos 
   1585      1.1  christos static int SIG_verify_loop(void *args)
   1586      1.1  christos {
   1587  1.1.1.2  christos     loopargs_t *tempargs = *(loopargs_t **)args;
   1588      1.1  christos     EVP_PKEY_CTX *ctx = tempargs->sig_verify_ctx[testnum];
   1589      1.1  christos     size_t sig_len = tempargs->sig_act_sig_len[testnum];
   1590      1.1  christos     unsigned char *sig = tempargs->sig_sig[testnum];
   1591      1.1  christos     unsigned char md[SHA256_DIGEST_LENGTH] = { 0 };
   1592      1.1  christos     size_t md_len = SHA256_DIGEST_LENGTH;
   1593      1.1  christos     int count;
   1594      1.1  christos 
   1595      1.1  christos     for (count = 0; COND(kems_c[testnum][2]); count++) {
   1596      1.1  christos         int ret = EVP_PKEY_verify(ctx, sig, sig_len, md, md_len);
   1597      1.1  christos 
   1598      1.1  christos         if (ret <= 0) {
   1599      1.1  christos             BIO_printf(bio_err, "SIG verify failure at count %d\n", count);
   1600      1.1  christos             dofail();
   1601      1.1  christos             count = -1;
   1602      1.1  christos             break;
   1603      1.1  christos         }
   1604      1.1  christos     }
   1605      1.1  christos     return count;
   1606      1.1  christos }
   1607      1.1  christos 
   1608      1.1  christos static int check_block_size(EVP_CIPHER_CTX *ctx, int length)
   1609      1.1  christos {
   1610      1.1  christos     const EVP_CIPHER *ciph = EVP_CIPHER_CTX_get0_cipher(ctx);
   1611      1.1  christos     int blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
   1612      1.1  christos 
   1613      1.1  christos     if (ciph == NULL || blocksize <= 0) {
   1614      1.1  christos         BIO_printf(bio_err, "\nInvalid cipher!\n");
   1615      1.1  christos         return 0;
   1616      1.1  christos     }
   1617      1.1  christos     if (length % blocksize != 0) {
   1618      1.1  christos         BIO_printf(bio_err,
   1619  1.1.1.2  christos             "\nRequested encryption length not a multiple of block size for %s!\n",
   1620  1.1.1.2  christos             EVP_CIPHER_get0_name(ciph));
   1621      1.1  christos         return 0;
   1622      1.1  christos     }
   1623      1.1  christos     return 1;
   1624      1.1  christos }
   1625      1.1  christos 
   1626      1.1  christos static int run_benchmark(int async_jobs,
   1627  1.1.1.2  christos     int (*loop_function)(void *), loopargs_t *loopargs)
   1628      1.1  christos {
   1629      1.1  christos     int job_op_count = 0;
   1630      1.1  christos     int total_op_count = 0;
   1631      1.1  christos     int num_inprogress = 0;
   1632      1.1  christos     int error = 0, i = 0, ret = 0;
   1633      1.1  christos     OSSL_ASYNC_FD job_fd = 0;
   1634      1.1  christos     size_t num_job_fds = 0;
   1635      1.1  christos 
   1636      1.1  christos     if (async_jobs == 0) {
   1637      1.1  christos         return loop_function((void *)&loopargs);
   1638      1.1  christos     }
   1639      1.1  christos 
   1640      1.1  christos     for (i = 0; i < async_jobs && !error; i++) {
   1641      1.1  christos         loopargs_t *looparg_item = loopargs + i;
   1642      1.1  christos 
   1643      1.1  christos         /* Copy pointer content (looparg_t item address) into async context */
   1644      1.1  christos         ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
   1645  1.1.1.2  christos             &job_op_count, loop_function,
   1646  1.1.1.2  christos             (void *)&looparg_item, sizeof(looparg_item));
   1647      1.1  christos         switch (ret) {
   1648      1.1  christos         case ASYNC_PAUSE:
   1649      1.1  christos             ++num_inprogress;
   1650      1.1  christos             break;
   1651      1.1  christos         case ASYNC_FINISH:
   1652      1.1  christos             if (job_op_count == -1) {
   1653      1.1  christos                 error = 1;
   1654      1.1  christos             } else {
   1655      1.1  christos                 total_op_count += job_op_count;
   1656      1.1  christos             }
   1657      1.1  christos             break;
   1658      1.1  christos         case ASYNC_NO_JOBS:
   1659      1.1  christos         case ASYNC_ERR:
   1660      1.1  christos             BIO_printf(bio_err, "Failure in the job\n");
   1661      1.1  christos             dofail();
   1662      1.1  christos             error = 1;
   1663      1.1  christos             break;
   1664      1.1  christos         }
   1665      1.1  christos     }
   1666      1.1  christos 
   1667      1.1  christos     while (num_inprogress > 0) {
   1668      1.1  christos #if defined(OPENSSL_SYS_WINDOWS)
   1669      1.1  christos         DWORD avail = 0;
   1670      1.1  christos #elif defined(OPENSSL_SYS_UNIX)
   1671      1.1  christos         int select_result = 0;
   1672      1.1  christos         OSSL_ASYNC_FD max_fd = 0;
   1673      1.1  christos         fd_set waitfdset;
   1674      1.1  christos 
   1675      1.1  christos         FD_ZERO(&waitfdset);
   1676      1.1  christos 
   1677      1.1  christos         for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
   1678      1.1  christos             if (loopargs[i].inprogress_job == NULL)
   1679      1.1  christos                 continue;
   1680      1.1  christos 
   1681  1.1.1.2  christos             if (!ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, NULL, &num_job_fds)
   1682      1.1  christos                 || num_job_fds > 1) {
   1683      1.1  christos                 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
   1684      1.1  christos                 dofail();
   1685      1.1  christos                 error = 1;
   1686      1.1  christos                 break;
   1687      1.1  christos             }
   1688      1.1  christos             ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
   1689  1.1.1.2  christos                 &num_job_fds);
   1690      1.1  christos             FD_SET(job_fd, &waitfdset);
   1691      1.1  christos             if (job_fd > max_fd)
   1692      1.1  christos                 max_fd = job_fd;
   1693      1.1  christos         }
   1694      1.1  christos 
   1695      1.1  christos         if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
   1696      1.1  christos             BIO_printf(bio_err,
   1697  1.1.1.2  christos                 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
   1698  1.1.1.2  christos                 "Decrease the value of async_jobs\n",
   1699  1.1.1.2  christos                 max_fd, FD_SETSIZE);
   1700      1.1  christos             dofail();
   1701      1.1  christos             error = 1;
   1702      1.1  christos             break;
   1703      1.1  christos         }
   1704      1.1  christos 
   1705      1.1  christos         select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
   1706      1.1  christos         if (select_result == -1 && errno == EINTR)
   1707      1.1  christos             continue;
   1708      1.1  christos 
   1709      1.1  christos         if (select_result == -1) {
   1710      1.1  christos             BIO_printf(bio_err, "Failure in the select\n");
   1711      1.1  christos             dofail();
   1712      1.1  christos             error = 1;
   1713      1.1  christos             break;
   1714      1.1  christos         }
   1715      1.1  christos 
   1716      1.1  christos         if (select_result == 0)
   1717      1.1  christos             continue;
   1718      1.1  christos #endif
   1719      1.1  christos 
   1720      1.1  christos         for (i = 0; i < async_jobs; i++) {
   1721      1.1  christos             if (loopargs[i].inprogress_job == NULL)
   1722      1.1  christos                 continue;
   1723      1.1  christos 
   1724  1.1.1.2  christos             if (!ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, NULL, &num_job_fds)
   1725      1.1  christos                 || num_job_fds > 1) {
   1726      1.1  christos                 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
   1727      1.1  christos                 dofail();
   1728      1.1  christos                 error = 1;
   1729      1.1  christos                 break;
   1730      1.1  christos             }
   1731      1.1  christos             ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
   1732  1.1.1.2  christos                 &num_job_fds);
   1733      1.1  christos 
   1734      1.1  christos #if defined(OPENSSL_SYS_UNIX)
   1735      1.1  christos             if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
   1736      1.1  christos                 continue;
   1737      1.1  christos #elif defined(OPENSSL_SYS_WINDOWS)
   1738      1.1  christos             if (num_job_fds == 1
   1739      1.1  christos                 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
   1740      1.1  christos                 && avail > 0)
   1741      1.1  christos                 continue;
   1742      1.1  christos #endif
   1743      1.1  christos 
   1744      1.1  christos             ret = ASYNC_start_job(&loopargs[i].inprogress_job,
   1745  1.1.1.2  christos                 loopargs[i].wait_ctx, &job_op_count,
   1746  1.1.1.2  christos                 loop_function, (void *)(loopargs + i),
   1747  1.1.1.2  christos                 sizeof(loopargs_t));
   1748      1.1  christos             switch (ret) {
   1749      1.1  christos             case ASYNC_PAUSE:
   1750      1.1  christos                 break;
   1751      1.1  christos             case ASYNC_FINISH:
   1752      1.1  christos                 if (job_op_count == -1) {
   1753      1.1  christos                     error = 1;
   1754      1.1  christos                 } else {
   1755      1.1  christos                     total_op_count += job_op_count;
   1756      1.1  christos                 }
   1757      1.1  christos                 --num_inprogress;
   1758      1.1  christos                 loopargs[i].inprogress_job = NULL;
   1759      1.1  christos                 break;
   1760      1.1  christos             case ASYNC_NO_JOBS:
   1761      1.1  christos             case ASYNC_ERR:
   1762      1.1  christos                 --num_inprogress;
   1763      1.1  christos                 loopargs[i].inprogress_job = NULL;
   1764      1.1  christos                 BIO_printf(bio_err, "Failure in the job\n");
   1765      1.1  christos                 dofail();
   1766      1.1  christos                 error = 1;
   1767      1.1  christos                 break;
   1768      1.1  christos             }
   1769      1.1  christos         }
   1770      1.1  christos     }
   1771      1.1  christos 
   1772      1.1  christos     return error ? -1 : total_op_count;
   1773      1.1  christos }
   1774      1.1  christos 
   1775      1.1  christos typedef struct ec_curve_st {
   1776      1.1  christos     const char *name;
   1777      1.1  christos     unsigned int nid;
   1778      1.1  christos     unsigned int bits;
   1779      1.1  christos     size_t sigsize; /* only used for EdDSA curves */
   1780      1.1  christos } EC_CURVE;
   1781      1.1  christos 
   1782      1.1  christos static EVP_PKEY *get_ecdsa(const EC_CURVE *curve)
   1783      1.1  christos {
   1784      1.1  christos     EVP_PKEY_CTX *kctx = NULL;
   1785      1.1  christos     EVP_PKEY *key = NULL;
   1786      1.1  christos 
   1787      1.1  christos     /* Ensure that the error queue is empty */
   1788      1.1  christos     if (ERR_peek_error()) {
   1789      1.1  christos         BIO_printf(bio_err,
   1790  1.1.1.2  christos             "WARNING: the error queue contains previous unhandled errors.\n");
   1791      1.1  christos         dofail();
   1792      1.1  christos     }
   1793      1.1  christos 
   1794      1.1  christos     /*
   1795      1.1  christos      * Let's try to create a ctx directly from the NID: this works for
   1796      1.1  christos      * curves like Curve25519 that are not implemented through the low
   1797      1.1  christos      * level EC interface.
   1798      1.1  christos      * If this fails we try creating a EVP_PKEY_EC generic param ctx,
   1799      1.1  christos      * then we set the curve by NID before deriving the actual keygen
   1800      1.1  christos      * ctx for that specific curve.
   1801      1.1  christos      */
   1802      1.1  christos     kctx = EVP_PKEY_CTX_new_id(curve->nid, NULL);
   1803      1.1  christos     if (kctx == NULL) {
   1804      1.1  christos         EVP_PKEY_CTX *pctx = NULL;
   1805      1.1  christos         EVP_PKEY *params = NULL;
   1806      1.1  christos         /*
   1807      1.1  christos          * If we reach this code EVP_PKEY_CTX_new_id() failed and a
   1808      1.1  christos          * "int_ctx_new:unsupported algorithm" error was added to the
   1809      1.1  christos          * error queue.
   1810      1.1  christos          * We remove it from the error queue as we are handling it.
   1811      1.1  christos          */
   1812      1.1  christos         unsigned long error = ERR_peek_error();
   1813      1.1  christos 
   1814      1.1  christos         if (error == ERR_peek_last_error() /* oldest and latest errors match */
   1815      1.1  christos             /* check that the error origin matches */
   1816      1.1  christos             && ERR_GET_LIB(error) == ERR_LIB_EVP
   1817      1.1  christos             && (ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM
   1818      1.1  christos                 || ERR_GET_REASON(error) == ERR_R_UNSUPPORTED))
   1819      1.1  christos             ERR_get_error(); /* pop error from queue */
   1820      1.1  christos         if (ERR_peek_error()) {
   1821      1.1  christos             BIO_printf(bio_err,
   1822  1.1.1.2  christos                 "Unhandled error in the error queue during EC key setup.\n");
   1823      1.1  christos             dofail();
   1824      1.1  christos             return NULL;
   1825      1.1  christos         }
   1826      1.1  christos 
   1827      1.1  christos         /* Create the context for parameter generation */
   1828      1.1  christos         if ((pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL
   1829      1.1  christos             || EVP_PKEY_paramgen_init(pctx) <= 0
   1830      1.1  christos             || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
   1831  1.1.1.2  christos                    curve->nid)
   1832  1.1.1.2  christos                 <= 0
   1833      1.1  christos             || EVP_PKEY_paramgen(pctx, &params) <= 0) {
   1834      1.1  christos             BIO_printf(bio_err, "EC params init failure.\n");
   1835      1.1  christos             dofail();
   1836      1.1  christos             EVP_PKEY_CTX_free(pctx);
   1837      1.1  christos             return NULL;
   1838      1.1  christos         }
   1839      1.1  christos         EVP_PKEY_CTX_free(pctx);
   1840      1.1  christos 
   1841      1.1  christos         /* Create the context for the key generation */
   1842      1.1  christos         kctx = EVP_PKEY_CTX_new(params, NULL);
   1843      1.1  christos         EVP_PKEY_free(params);
   1844      1.1  christos     }
   1845      1.1  christos     if (kctx == NULL
   1846      1.1  christos         || EVP_PKEY_keygen_init(kctx) <= 0
   1847      1.1  christos         || EVP_PKEY_keygen(kctx, &key) <= 0) {
   1848      1.1  christos         BIO_printf(bio_err, "EC key generation failure.\n");
   1849      1.1  christos         dofail();
   1850      1.1  christos         key = NULL;
   1851      1.1  christos     }
   1852      1.1  christos     EVP_PKEY_CTX_free(kctx);
   1853      1.1  christos     return key;
   1854      1.1  christos }
   1855      1.1  christos 
   1856  1.1.1.2  christos #define stop_it(do_it, test_num) \
   1857      1.1  christos     memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
   1858      1.1  christos 
   1859      1.1  christos /* Checks to see if algorithms are fetchable */
   1860  1.1.1.2  christos #define IS_FETCHABLE(type, TYPE)                      \
   1861  1.1.1.2  christos     static int is_##type##_fetchable(const TYPE *alg) \
   1862  1.1.1.2  christos     {                                                 \
   1863  1.1.1.2  christos         TYPE *impl;                                   \
   1864  1.1.1.2  christos         const char *propq = app_get0_propq();         \
   1865  1.1.1.2  christos         OSSL_LIB_CTX *libctx = app_get0_libctx();     \
   1866  1.1.1.2  christos         const char *name = TYPE##_get0_name(alg);     \
   1867  1.1.1.2  christos                                                       \
   1868  1.1.1.2  christos         ERR_set_mark();                               \
   1869  1.1.1.2  christos         impl = TYPE##_fetch(libctx, name, propq);     \
   1870  1.1.1.2  christos         ERR_pop_to_mark();                            \
   1871  1.1.1.2  christos         if (impl == NULL)                             \
   1872  1.1.1.2  christos             return 0;                                 \
   1873  1.1.1.2  christos         TYPE##_free(impl);                            \
   1874  1.1.1.2  christos         return 1;                                     \
   1875      1.1  christos     }
   1876      1.1  christos 
   1877      1.1  christos IS_FETCHABLE(signature, EVP_SIGNATURE)
   1878      1.1  christos IS_FETCHABLE(kem, EVP_KEM)
   1879      1.1  christos 
   1880      1.1  christos DEFINE_STACK_OF(EVP_KEM)
   1881      1.1  christos 
   1882  1.1.1.2  christos static int kems_cmp(const EVP_KEM *const *a,
   1883  1.1.1.2  christos     const EVP_KEM *const *b)
   1884      1.1  christos {
   1885      1.1  christos     return strcmp(OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*a)),
   1886  1.1.1.2  christos         OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*b)));
   1887      1.1  christos }
   1888      1.1  christos 
   1889      1.1  christos static void collect_kem(EVP_KEM *kem, void *stack)
   1890      1.1  christos {
   1891      1.1  christos     STACK_OF(EVP_KEM) *kem_stack = stack;
   1892      1.1  christos 
   1893      1.1  christos     if (is_kem_fetchable(kem)
   1894  1.1.1.2  christos         && EVP_KEM_up_ref(kem)
   1895  1.1.1.2  christos         && sk_EVP_KEM_push(kem_stack, kem) <= 0)
   1896      1.1  christos         EVP_KEM_free(kem); /* up-ref successful but push to stack failed */
   1897      1.1  christos }
   1898      1.1  christos 
   1899      1.1  christos static int kem_locate(const char *algo, unsigned int *idx)
   1900      1.1  christos {
   1901      1.1  christos     unsigned int i;
   1902      1.1  christos 
   1903      1.1  christos     for (i = 0; i < kems_algs_len; i++) {
   1904      1.1  christos         if (strcmp(kems_algname[i], algo) == 0) {
   1905      1.1  christos             *idx = i;
   1906      1.1  christos             return 1;
   1907      1.1  christos         }
   1908      1.1  christos     }
   1909      1.1  christos     return 0;
   1910      1.1  christos }
   1911      1.1  christos 
   1912      1.1  christos DEFINE_STACK_OF(EVP_SIGNATURE)
   1913      1.1  christos 
   1914  1.1.1.2  christos static int signatures_cmp(const EVP_SIGNATURE *const *a,
   1915  1.1.1.2  christos     const EVP_SIGNATURE *const *b)
   1916      1.1  christos {
   1917      1.1  christos     return strcmp(OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*a)),
   1918  1.1.1.2  christos         OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*b)));
   1919      1.1  christos }
   1920      1.1  christos 
   1921      1.1  christos static void collect_signatures(EVP_SIGNATURE *sig, void *stack)
   1922      1.1  christos {
   1923      1.1  christos     STACK_OF(EVP_SIGNATURE) *sig_stack = stack;
   1924      1.1  christos 
   1925      1.1  christos     if (is_signature_fetchable(sig)
   1926  1.1.1.2  christos         && EVP_SIGNATURE_up_ref(sig)
   1927  1.1.1.2  christos         && sk_EVP_SIGNATURE_push(sig_stack, sig) <= 0)
   1928      1.1  christos         EVP_SIGNATURE_free(sig); /* up-ref successful but push to stack failed */
   1929      1.1  christos }
   1930      1.1  christos 
   1931      1.1  christos static int sig_locate(const char *algo, unsigned int *idx)
   1932      1.1  christos {
   1933      1.1  christos     unsigned int i;
   1934      1.1  christos 
   1935      1.1  christos     for (i = 0; i < sigs_algs_len; i++) {
   1936      1.1  christos         if (strcmp(sigs_algname[i], algo) == 0) {
   1937      1.1  christos             *idx = i;
   1938      1.1  christos             return 1;
   1939      1.1  christos         }
   1940      1.1  christos     }
   1941      1.1  christos     return 0;
   1942      1.1  christos }
   1943      1.1  christos 
   1944  1.1.1.2  christos static int get_max(const uint8_t doit[], size_t algs_len)
   1945  1.1.1.2  christos {
   1946      1.1  christos     size_t i = 0;
   1947      1.1  christos     int maxcnt = 0;
   1948      1.1  christos 
   1949      1.1  christos     for (i = 0; i < algs_len; i++)
   1950  1.1.1.2  christos         if (maxcnt < doit[i])
   1951  1.1.1.2  christos             maxcnt = doit[i];
   1952      1.1  christos     return maxcnt;
   1953      1.1  christos }
   1954      1.1  christos 
   1955      1.1  christos int speed_main(int argc, char **argv)
   1956      1.1  christos {
   1957      1.1  christos     CONF *conf = NULL;
   1958      1.1  christos     ENGINE *e = NULL;
   1959      1.1  christos     loopargs_t *loopargs = NULL;
   1960      1.1  christos     const char *prog;
   1961      1.1  christos     const char *engine_id = NULL;
   1962      1.1  christos     EVP_CIPHER *evp_cipher = NULL;
   1963      1.1  christos     EVP_MAC *mac = NULL;
   1964      1.1  christos     double d = 0.0;
   1965      1.1  christos     OPTION_CHOICE o;
   1966      1.1  christos     int async_init = 0, multiblock = 0, pr_header = 0;
   1967      1.1  christos     uint8_t doit[ALGOR_NUM] = { 0 };
   1968      1.1  christos     int ret = 1, misalign = 0, lengths_single = 0;
   1969      1.1  christos     STACK_OF(EVP_KEM) *kem_stack = NULL;
   1970      1.1  christos     STACK_OF(EVP_SIGNATURE) *sig_stack = NULL;
   1971      1.1  christos     long count = 0;
   1972      1.1  christos     unsigned int size_num = SIZE_NUM;
   1973      1.1  christos     unsigned int i, k, loopargs_len = 0, async_jobs = 0;
   1974      1.1  christos     unsigned int idx;
   1975      1.1  christos     int keylen = 0;
   1976      1.1  christos     int buflen;
   1977      1.1  christos     size_t declen;
   1978      1.1  christos     BIGNUM *bn = NULL;
   1979      1.1  christos     EVP_PKEY_CTX *genctx = NULL;
   1980      1.1  christos #ifndef NO_FORK
   1981      1.1  christos     int multi = 0;
   1982      1.1  christos #endif
   1983      1.1  christos     long op_count = 1;
   1984      1.1  christos     openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
   1985  1.1.1.2  christos         ECDSA_SECONDS, ECDH_SECONDS,
   1986  1.1.1.2  christos         EdDSA_SECONDS, SM2_SECONDS,
   1987  1.1.1.2  christos         FFDH_SECONDS, KEM_SECONDS,
   1988  1.1.1.2  christos         SIG_SECONDS };
   1989      1.1  christos 
   1990      1.1  christos     static const unsigned char key32[32] = {
   1991      1.1  christos         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
   1992      1.1  christos         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
   1993      1.1  christos         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
   1994      1.1  christos         0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
   1995      1.1  christos     };
   1996      1.1  christos     static const unsigned char deskey[] = {
   1997      1.1  christos         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, /* key1 */
   1998      1.1  christos         0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, /* key2 */
   1999  1.1.1.2  christos         0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 /* key3 */
   2000      1.1  christos     };
   2001      1.1  christos     static const struct {
   2002      1.1  christos         const unsigned char *data;
   2003      1.1  christos         unsigned int length;
   2004      1.1  christos         unsigned int bits;
   2005      1.1  christos     } rsa_keys[] = {
   2006  1.1.1.2  christos         { test512, sizeof(test512), 512 },
   2007  1.1.1.2  christos         { test1024, sizeof(test1024), 1024 },
   2008  1.1.1.2  christos         { test2048, sizeof(test2048), 2048 },
   2009  1.1.1.2  christos         { test3072, sizeof(test3072), 3072 },
   2010  1.1.1.2  christos         { test4096, sizeof(test4096), 4096 },
   2011  1.1.1.2  christos         { test7680, sizeof(test7680), 7680 },
   2012      1.1  christos         { test15360, sizeof(test15360), 15360 }
   2013      1.1  christos     };
   2014      1.1  christos     uint8_t rsa_doit[RSA_NUM] = { 0 };
   2015      1.1  christos     int primes = RSA_DEFAULT_PRIME_NUM;
   2016      1.1  christos #ifndef OPENSSL_NO_DH
   2017      1.1  christos     typedef struct ffdh_params_st {
   2018      1.1  christos         const char *name;
   2019      1.1  christos         unsigned int nid;
   2020      1.1  christos         unsigned int bits;
   2021      1.1  christos     } FFDH_PARAMS;
   2022      1.1  christos 
   2023      1.1  christos     static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
   2024  1.1.1.2  christos         { "ffdh2048", NID_ffdhe2048, 2048 },
   2025  1.1.1.2  christos         { "ffdh3072", NID_ffdhe3072, 3072 },
   2026  1.1.1.2  christos         { "ffdh4096", NID_ffdhe4096, 4096 },
   2027  1.1.1.2  christos         { "ffdh6144", NID_ffdhe6144, 6144 },
   2028  1.1.1.2  christos         { "ffdh8192", NID_ffdhe8192, 8192 }
   2029      1.1  christos     };
   2030      1.1  christos     uint8_t ffdh_doit[FFDH_NUM] = { 0 };
   2031      1.1  christos 
   2032      1.1  christos #endif /* OPENSSL_NO_DH */
   2033      1.1  christos #ifndef OPENSSL_NO_DSA
   2034      1.1  christos     static const unsigned int dsa_bits[DSA_NUM] = { 1024, 2048 };
   2035      1.1  christos     uint8_t dsa_doit[DSA_NUM] = { 0 };
   2036      1.1  christos #endif /* OPENSSL_NO_DSA */
   2037      1.1  christos     /*
   2038      1.1  christos      * We only test over the following curves as they are representative, To
   2039      1.1  christos      * add tests over more curves, simply add the curve NID and curve name to
   2040      1.1  christos      * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
   2041      1.1  christos      * lists accordingly.
   2042      1.1  christos      */
   2043      1.1  christos     static const EC_CURVE ec_curves[EC_NUM] = {
   2044      1.1  christos         /* Prime Curves */
   2045  1.1.1.2  christos         { "secp160r1", NID_secp160r1, 160 },
   2046  1.1.1.2  christos         { "nistp192", NID_X9_62_prime192v1, 192 },
   2047  1.1.1.2  christos         { "nistp224", NID_secp224r1, 224 },
   2048  1.1.1.2  christos         { "nistp256", NID_X9_62_prime256v1, 256 },
   2049  1.1.1.2  christos         { "nistp384", NID_secp384r1, 384 },
   2050  1.1.1.2  christos         { "nistp521", NID_secp521r1, 521 },
   2051      1.1  christos #ifndef OPENSSL_NO_EC2M
   2052      1.1  christos         /* Binary Curves */
   2053  1.1.1.2  christos         { "nistk163", NID_sect163k1, 163 },
   2054  1.1.1.2  christos         { "nistk233", NID_sect233k1, 233 },
   2055  1.1.1.2  christos         { "nistk283", NID_sect283k1, 283 },
   2056  1.1.1.2  christos         { "nistk409", NID_sect409k1, 409 },
   2057  1.1.1.2  christos         { "nistk571", NID_sect571k1, 571 },
   2058  1.1.1.2  christos         { "nistb163", NID_sect163r2, 163 },
   2059  1.1.1.2  christos         { "nistb233", NID_sect233r1, 233 },
   2060  1.1.1.2  christos         { "nistb283", NID_sect283r1, 283 },
   2061  1.1.1.2  christos         { "nistb409", NID_sect409r1, 409 },
   2062  1.1.1.2  christos         { "nistb571", NID_sect571r1, 571 },
   2063  1.1.1.2  christos #endif
   2064  1.1.1.2  christos         { "brainpoolP256r1", NID_brainpoolP256r1, 256 },
   2065  1.1.1.2  christos         { "brainpoolP256t1", NID_brainpoolP256t1, 256 },
   2066  1.1.1.2  christos         { "brainpoolP384r1", NID_brainpoolP384r1, 384 },
   2067  1.1.1.2  christos         { "brainpoolP384t1", NID_brainpoolP384t1, 384 },
   2068  1.1.1.2  christos         { "brainpoolP512r1", NID_brainpoolP512r1, 512 },
   2069  1.1.1.2  christos         { "brainpoolP512t1", NID_brainpoolP512t1, 512 },
   2070      1.1  christos #ifndef OPENSSL_NO_ECX
   2071      1.1  christos         /* Other and ECDH only ones */
   2072  1.1.1.2  christos         { "X25519", NID_X25519, 253 },
   2073  1.1.1.2  christos         { "X448", NID_X448, 448 }
   2074      1.1  christos #endif
   2075      1.1  christos     };
   2076      1.1  christos #ifndef OPENSSL_NO_ECX
   2077      1.1  christos     static const EC_CURVE ed_curves[EdDSA_NUM] = {
   2078      1.1  christos         /* EdDSA */
   2079  1.1.1.2  christos         { "Ed25519", NID_ED25519, 253, 64 },
   2080  1.1.1.2  christos         { "Ed448", NID_ED448, 456, 114 }
   2081      1.1  christos     };
   2082      1.1  christos #endif /* OPENSSL_NO_ECX */
   2083      1.1  christos #ifndef OPENSSL_NO_SM2
   2084      1.1  christos     static const EC_CURVE sm2_curves[SM2_NUM] = {
   2085      1.1  christos         /* SM2 */
   2086  1.1.1.2  christos         { "CurveSM2", NID_sm2, 256 }
   2087      1.1  christos     };
   2088      1.1  christos     uint8_t sm2_doit[SM2_NUM] = { 0 };
   2089      1.1  christos #endif
   2090      1.1  christos     uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
   2091      1.1  christos     uint8_t ecdh_doit[EC_NUM] = { 0 };
   2092      1.1  christos #ifndef OPENSSL_NO_ECX
   2093      1.1  christos     uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
   2094      1.1  christos #endif /* OPENSSL_NO_ECX */
   2095      1.1  christos 
   2096      1.1  christos     uint8_t kems_doit[MAX_KEM_NUM] = { 0 };
   2097      1.1  christos     uint8_t sigs_doit[MAX_SIG_NUM] = { 0 };
   2098      1.1  christos 
   2099      1.1  christos     uint8_t do_kems = 0;
   2100      1.1  christos     uint8_t do_sigs = 0;
   2101      1.1  christos 
   2102      1.1  christos     /* checks declared curves against choices list. */
   2103      1.1  christos #ifndef OPENSSL_NO_ECX
   2104      1.1  christos     OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
   2105      1.1  christos     OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
   2106      1.1  christos 
   2107      1.1  christos     OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
   2108      1.1  christos     OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
   2109      1.1  christos 
   2110      1.1  christos     OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
   2111      1.1  christos     OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
   2112      1.1  christos #endif /* OPENSSL_NO_ECX */
   2113      1.1  christos 
   2114      1.1  christos #ifndef OPENSSL_NO_SM2
   2115      1.1  christos     OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
   2116      1.1  christos     OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
   2117      1.1  christos #endif
   2118      1.1  christos 
   2119      1.1  christos     prog = opt_init(argc, argv, speed_options);
   2120      1.1  christos     while ((o = opt_next()) != OPT_EOF) {
   2121      1.1  christos         switch (o) {
   2122      1.1  christos         case OPT_EOF:
   2123      1.1  christos         case OPT_ERR:
   2124  1.1.1.2  christos         opterr:
   2125      1.1  christos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
   2126      1.1  christos             goto end;
   2127      1.1  christos         case OPT_HELP:
   2128      1.1  christos             opt_help(speed_options);
   2129      1.1  christos             ret = 0;
   2130      1.1  christos             goto end;
   2131      1.1  christos         case OPT_ELAPSED:
   2132      1.1  christos             usertime = 0;
   2133      1.1  christos             break;
   2134      1.1  christos         case OPT_EVP:
   2135      1.1  christos             if (doit[D_EVP]) {
   2136      1.1  christos                 BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog);
   2137      1.1  christos                 goto opterr;
   2138      1.1  christos             }
   2139      1.1  christos             ERR_set_mark();
   2140      1.1  christos             if (!opt_cipher_silent(opt_arg(), &evp_cipher)) {
   2141      1.1  christos                 if (have_md(opt_arg()))
   2142      1.1  christos                     evp_md_name = opt_arg();
   2143      1.1  christos             }
   2144      1.1  christos             if (evp_cipher == NULL && evp_md_name == NULL) {
   2145      1.1  christos                 ERR_clear_last_mark();
   2146      1.1  christos                 BIO_printf(bio_err,
   2147  1.1.1.2  christos                     "%s: %s is an unknown cipher or digest\n",
   2148  1.1.1.2  christos                     prog, opt_arg());
   2149      1.1  christos                 goto end;
   2150      1.1  christos             }
   2151      1.1  christos             ERR_pop_to_mark();
   2152      1.1  christos             doit[D_EVP] = 1;
   2153      1.1  christos             break;
   2154      1.1  christos         case OPT_HMAC:
   2155      1.1  christos             if (!have_md(opt_arg())) {
   2156      1.1  christos                 BIO_printf(bio_err, "%s: %s is an unknown digest\n",
   2157  1.1.1.2  christos                     prog, opt_arg());
   2158      1.1  christos                 goto end;
   2159      1.1  christos             }
   2160      1.1  christos             evp_mac_mdname = opt_arg();
   2161      1.1  christos             doit[D_HMAC] = 1;
   2162      1.1  christos             break;
   2163      1.1  christos         case OPT_CMAC:
   2164      1.1  christos             if (!have_cipher(opt_arg())) {
   2165      1.1  christos                 BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
   2166  1.1.1.2  christos                     prog, opt_arg());
   2167      1.1  christos                 goto end;
   2168      1.1  christos             }
   2169      1.1  christos             evp_mac_ciphername = opt_arg();
   2170      1.1  christos             doit[D_EVP_CMAC] = 1;
   2171      1.1  christos             break;
   2172      1.1  christos         case OPT_DECRYPT:
   2173      1.1  christos             decrypt = 1;
   2174      1.1  christos             break;
   2175      1.1  christos         case OPT_ENGINE:
   2176      1.1  christos             /*
   2177      1.1  christos              * In a forked execution, an engine might need to be
   2178      1.1  christos              * initialised by each child process, not by the parent.
   2179      1.1  christos              * So store the name here and run setup_engine() later on.
   2180      1.1  christos              */
   2181      1.1  christos             engine_id = opt_arg();
   2182      1.1  christos             break;
   2183      1.1  christos         case OPT_MULTI:
   2184      1.1  christos #ifndef NO_FORK
   2185      1.1  christos             multi = opt_int_arg();
   2186      1.1  christos             if ((size_t)multi >= SIZE_MAX / sizeof(int)) {
   2187      1.1  christos                 BIO_printf(bio_err, "%s: multi argument too large\n", prog);
   2188      1.1  christos                 return 0;
   2189      1.1  christos             }
   2190      1.1  christos #endif
   2191      1.1  christos             break;
   2192      1.1  christos         case OPT_ASYNCJOBS:
   2193      1.1  christos #ifndef OPENSSL_NO_ASYNC
   2194      1.1  christos             async_jobs = opt_int_arg();
   2195      1.1  christos             if (async_jobs > 99999) {
   2196      1.1  christos                 BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
   2197      1.1  christos                 goto opterr;
   2198      1.1  christos             }
   2199      1.1  christos             if (!ASYNC_is_capable()) {
   2200      1.1  christos                 BIO_printf(bio_err,
   2201  1.1.1.2  christos                     "%s: async_jobs specified but async not supported\n",
   2202  1.1.1.2  christos                     prog);
   2203      1.1  christos                 if (testmode)
   2204      1.1  christos                     /* Return success in the testmode. */
   2205      1.1  christos                     return 0;
   2206      1.1  christos                 goto opterr;
   2207      1.1  christos             }
   2208      1.1  christos #endif
   2209      1.1  christos             break;
   2210      1.1  christos         case OPT_MISALIGN:
   2211      1.1  christos             misalign = opt_int_arg();
   2212      1.1  christos             if (misalign > MISALIGN) {
   2213      1.1  christos                 BIO_printf(bio_err,
   2214  1.1.1.2  christos                     "%s: Maximum offset is %d\n", prog, MISALIGN);
   2215      1.1  christos                 goto opterr;
   2216      1.1  christos             }
   2217      1.1  christos             break;
   2218      1.1  christos         case OPT_MR:
   2219      1.1  christos             mr = 1;
   2220      1.1  christos             break;
   2221      1.1  christos         case OPT_MB:
   2222      1.1  christos             multiblock = 1;
   2223      1.1  christos #ifdef OPENSSL_NO_MULTIBLOCK
   2224      1.1  christos             BIO_printf(bio_err,
   2225  1.1.1.2  christos                 "%s: -mb specified but multi-block support is disabled\n",
   2226  1.1.1.2  christos                 prog);
   2227      1.1  christos             goto end;
   2228      1.1  christos #endif
   2229      1.1  christos             break;
   2230      1.1  christos         case OPT_R_CASES:
   2231      1.1  christos             if (!opt_rand(o))
   2232      1.1  christos                 goto end;
   2233      1.1  christos             break;
   2234      1.1  christos         case OPT_PROV_CASES:
   2235      1.1  christos             if (!opt_provider(o))
   2236      1.1  christos                 goto end;
   2237      1.1  christos             break;
   2238      1.1  christos         case OPT_CONFIG:
   2239      1.1  christos             conf = app_load_config_modules(opt_arg());
   2240      1.1  christos             if (conf == NULL)
   2241      1.1  christos                 goto end;
   2242      1.1  christos             break;
   2243      1.1  christos         case OPT_PRIMES:
   2244      1.1  christos             primes = opt_int_arg();
   2245      1.1  christos             break;
   2246      1.1  christos         case OPT_SECONDS:
   2247      1.1  christos             seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
   2248  1.1.1.2  christos                 = seconds.ecdh = seconds.eddsa
   2249  1.1.1.2  christos                 = seconds.sm2 = seconds.ffdh
   2250  1.1.1.2  christos                 = seconds.kem = seconds.sig = opt_int_arg();
   2251      1.1  christos             break;
   2252      1.1  christos         case OPT_BYTES:
   2253      1.1  christos             lengths_single = opt_int_arg();
   2254      1.1  christos             lengths = &lengths_single;
   2255      1.1  christos             size_num = 1;
   2256      1.1  christos             break;
   2257      1.1  christos         case OPT_AEAD:
   2258      1.1  christos             aead = 1;
   2259      1.1  christos             break;
   2260      1.1  christos         case OPT_KEM:
   2261      1.1  christos             do_kems = 1;
   2262      1.1  christos             break;
   2263      1.1  christos         case OPT_SIG:
   2264      1.1  christos             do_sigs = 1;
   2265      1.1  christos             break;
   2266      1.1  christos         case OPT_MLOCK:
   2267      1.1  christos             domlock = 1;
   2268      1.1  christos #if !defined(_WIN32) && !defined(OPENSSL_SYS_LINUX)
   2269      1.1  christos             BIO_printf(bio_err,
   2270  1.1.1.2  christos                 "%s: -mlock not supported on this platform\n",
   2271  1.1.1.2  christos                 prog);
   2272      1.1  christos             goto end;
   2273      1.1  christos #endif
   2274      1.1  christos             break;
   2275      1.1  christos         case OPT_TESTMODE:
   2276      1.1  christos             testmode = 1;
   2277      1.1  christos             break;
   2278      1.1  christos         }
   2279      1.1  christos     }
   2280      1.1  christos 
   2281      1.1  christos     /* find all KEMs currently available */
   2282      1.1  christos     kem_stack = sk_EVP_KEM_new(kems_cmp);
   2283      1.1  christos     EVP_KEM_do_all_provided(app_get0_libctx(), collect_kem, kem_stack);
   2284      1.1  christos 
   2285      1.1  christos     kems_algs_len = 0;
   2286      1.1  christos 
   2287      1.1  christos     for (idx = 0; idx < (unsigned int)sk_EVP_KEM_num(kem_stack); idx++) {
   2288      1.1  christos         EVP_KEM *kem = sk_EVP_KEM_value(kem_stack, idx);
   2289      1.1  christos 
   2290      1.1  christos         if (strcmp(EVP_KEM_get0_name(kem), "RSA") == 0) {
   2291      1.1  christos             if (kems_algs_len + OSSL_NELEM(rsa_choices) >= MAX_KEM_NUM) {
   2292      1.1  christos                 BIO_printf(bio_err,
   2293  1.1.1.2  christos                     "Too many KEMs registered. Change MAX_KEM_NUM.\n");
   2294      1.1  christos                 goto end;
   2295      1.1  christos             }
   2296      1.1  christos             for (i = 0; i < OSSL_NELEM(rsa_choices); i++) {
   2297      1.1  christos                 kems_doit[kems_algs_len] = 1;
   2298      1.1  christos                 kems_algname[kems_algs_len++] = OPENSSL_strdup(rsa_choices[i].name);
   2299      1.1  christos             }
   2300      1.1  christos         } else if (strcmp(EVP_KEM_get0_name(kem), "EC") == 0) {
   2301      1.1  christos             if (kems_algs_len + 3 >= MAX_KEM_NUM) {
   2302      1.1  christos                 BIO_printf(bio_err,
   2303  1.1.1.2  christos                     "Too many KEMs registered. Change MAX_KEM_NUM.\n");
   2304      1.1  christos                 goto end;
   2305      1.1  christos             }
   2306      1.1  christos             kems_doit[kems_algs_len] = 1;
   2307      1.1  christos             kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-256");
   2308      1.1  christos             kems_doit[kems_algs_len] = 1;
   2309      1.1  christos             kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-384");
   2310      1.1  christos             kems_doit[kems_algs_len] = 1;
   2311      1.1  christos             kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-521");
   2312      1.1  christos         } else {
   2313      1.1  christos             if (kems_algs_len + 1 >= MAX_KEM_NUM) {
   2314      1.1  christos                 BIO_printf(bio_err,
   2315  1.1.1.2  christos                     "Too many KEMs registered. Change MAX_KEM_NUM.\n");
   2316      1.1  christos                 goto end;
   2317      1.1  christos             }
   2318      1.1  christos             kems_doit[kems_algs_len] = 1;
   2319      1.1  christos             kems_algname[kems_algs_len++] = OPENSSL_strdup(EVP_KEM_get0_name(kem));
   2320      1.1  christos         }
   2321      1.1  christos     }
   2322      1.1  christos     sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
   2323      1.1  christos     kem_stack = NULL;
   2324      1.1  christos 
   2325      1.1  christos     /* find all SIGNATUREs currently available */
   2326      1.1  christos     sig_stack = sk_EVP_SIGNATURE_new(signatures_cmp);
   2327      1.1  christos     EVP_SIGNATURE_do_all_provided(app_get0_libctx(), collect_signatures, sig_stack);
   2328      1.1  christos 
   2329      1.1  christos     sigs_algs_len = 0;
   2330      1.1  christos 
   2331      1.1  christos     for (idx = 0; idx < (unsigned int)sk_EVP_SIGNATURE_num(sig_stack); idx++) {
   2332      1.1  christos         EVP_SIGNATURE *s = sk_EVP_SIGNATURE_value(sig_stack, idx);
   2333      1.1  christos         const char *sig_name = EVP_SIGNATURE_get0_name(s);
   2334      1.1  christos 
   2335      1.1  christos         if (strcmp(sig_name, "RSA") == 0) {
   2336      1.1  christos             if (sigs_algs_len + OSSL_NELEM(rsa_choices) >= MAX_SIG_NUM) {
   2337      1.1  christos                 BIO_printf(bio_err,
   2338  1.1.1.2  christos                     "Too many signatures registered. Change MAX_SIG_NUM.\n");
   2339      1.1  christos                 goto end;
   2340      1.1  christos             }
   2341      1.1  christos             for (i = 0; i < OSSL_NELEM(rsa_choices); i++) {
   2342      1.1  christos                 sigs_doit[sigs_algs_len] = 1;
   2343      1.1  christos                 sigs_algname[sigs_algs_len++] = OPENSSL_strdup(rsa_choices[i].name);
   2344      1.1  christos             }
   2345      1.1  christos         }
   2346      1.1  christos #ifndef OPENSSL_NO_DSA
   2347      1.1  christos         else if (strcmp(sig_name, "DSA") == 0) {
   2348      1.1  christos             if (sigs_algs_len + DSA_NUM >= MAX_SIG_NUM) {
   2349      1.1  christos                 BIO_printf(bio_err,
   2350  1.1.1.2  christos                     "Too many signatures registered. Change MAX_SIG_NUM.\n");
   2351      1.1  christos                 goto end;
   2352      1.1  christos             }
   2353      1.1  christos             for (i = 0; i < DSA_NUM; i++) {
   2354      1.1  christos                 sigs_doit[sigs_algs_len] = 1;
   2355      1.1  christos                 sigs_algname[sigs_algs_len++] = OPENSSL_strdup(dsa_choices[i].name);
   2356      1.1  christos             }
   2357      1.1  christos         }
   2358      1.1  christos #endif /* OPENSSL_NO_DSA */
   2359      1.1  christos         /* skipping these algs as tested elsewhere - and b/o setup is a pain */
   2360  1.1.1.2  christos         else if (strncmp(sig_name, "RSA", 3) && strncmp(sig_name, "DSA", 3) && strncmp(sig_name, "ED25519", 7) && strncmp(sig_name, "ED448", 5) && strncmp(sig_name, "ECDSA", 5) && strcmp(sig_name, "HMAC") && strcmp(sig_name, "SIPHASH") && strcmp(sig_name, "POLY1305") && strcmp(sig_name, "CMAC") && strcmp(sig_name, "SM2")) { /* skip alg */
   2361      1.1  christos             if (sigs_algs_len + 1 >= MAX_SIG_NUM) {
   2362      1.1  christos                 BIO_printf(bio_err,
   2363  1.1.1.2  christos                     "Too many signatures registered. Change MAX_SIG_NUM.\n");
   2364      1.1  christos                 goto end;
   2365      1.1  christos             }
   2366      1.1  christos             /* activate this provider algorithm */
   2367      1.1  christos             sigs_doit[sigs_algs_len] = 1;
   2368      1.1  christos             sigs_algname[sigs_algs_len++] = OPENSSL_strdup(sig_name);
   2369      1.1  christos         }
   2370      1.1  christos     }
   2371      1.1  christos     sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
   2372      1.1  christos     sig_stack = NULL;
   2373      1.1  christos 
   2374      1.1  christos     /* Remaining arguments are algorithms. */
   2375      1.1  christos     argc = opt_num_rest();
   2376      1.1  christos     argv = opt_rest();
   2377      1.1  christos 
   2378      1.1  christos     if (!app_RAND_load())
   2379      1.1  christos         goto end;
   2380      1.1  christos 
   2381      1.1  christos     for (; *argv; argv++) {
   2382      1.1  christos         const char *algo = *argv;
   2383      1.1  christos         int algo_found = 0;
   2384      1.1  christos 
   2385      1.1  christos         if (opt_found(algo, doit_choices, &i)) {
   2386      1.1  christos             doit[i] = 1;
   2387      1.1  christos             algo_found = 1;
   2388      1.1  christos         }
   2389      1.1  christos         if (strcmp(algo, "des") == 0) {
   2390      1.1  christos             doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
   2391      1.1  christos             algo_found = 1;
   2392      1.1  christos         }
   2393      1.1  christos         if (strcmp(algo, "sha") == 0) {
   2394      1.1  christos             doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
   2395      1.1  christos             algo_found = 1;
   2396      1.1  christos         }
   2397      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   2398      1.1  christos         if (strcmp(algo, "openssl") == 0) /* just for compatibility */
   2399      1.1  christos             algo_found = 1;
   2400      1.1  christos #endif
   2401      1.1  christos         if (HAS_PREFIX(algo, "rsa")) {
   2402      1.1  christos             if (algo[sizeof("rsa") - 1] == '\0') {
   2403      1.1  christos                 memset(rsa_doit, 1, sizeof(rsa_doit));
   2404      1.1  christos                 algo_found = 1;
   2405      1.1  christos             }
   2406      1.1  christos             if (opt_found(algo, rsa_choices, &i)) {
   2407      1.1  christos                 rsa_doit[i] = 1;
   2408      1.1  christos                 algo_found = 1;
   2409      1.1  christos             }
   2410      1.1  christos         }
   2411      1.1  christos #ifndef OPENSSL_NO_DH
   2412      1.1  christos         if (HAS_PREFIX(algo, "ffdh")) {
   2413      1.1  christos             if (algo[sizeof("ffdh") - 1] == '\0') {
   2414      1.1  christos                 memset(ffdh_doit, 1, sizeof(ffdh_doit));
   2415      1.1  christos                 algo_found = 1;
   2416      1.1  christos             }
   2417      1.1  christos             if (opt_found(algo, ffdh_choices, &i)) {
   2418      1.1  christos                 ffdh_doit[i] = 2;
   2419      1.1  christos                 algo_found = 1;
   2420      1.1  christos             }
   2421      1.1  christos         }
   2422      1.1  christos #endif
   2423      1.1  christos #ifndef OPENSSL_NO_DSA
   2424      1.1  christos         if (HAS_PREFIX(algo, "dsa")) {
   2425      1.1  christos             if (algo[sizeof("dsa") - 1] == '\0') {
   2426      1.1  christos                 memset(dsa_doit, 1, sizeof(dsa_doit));
   2427      1.1  christos                 algo_found = 1;
   2428      1.1  christos             }
   2429      1.1  christos             if (opt_found(algo, dsa_choices, &i)) {
   2430      1.1  christos                 dsa_doit[i] = 2;
   2431      1.1  christos                 algo_found = 1;
   2432      1.1  christos             }
   2433      1.1  christos         }
   2434      1.1  christos #endif
   2435      1.1  christos         if (strcmp(algo, "aes") == 0) {
   2436      1.1  christos             doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
   2437      1.1  christos             algo_found = 1;
   2438      1.1  christos         }
   2439      1.1  christos         if (strcmp(algo, "camellia") == 0) {
   2440      1.1  christos             doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
   2441      1.1  christos             algo_found = 1;
   2442      1.1  christos         }
   2443      1.1  christos         if (HAS_PREFIX(algo, "ecdsa")) {
   2444      1.1  christos             if (algo[sizeof("ecdsa") - 1] == '\0') {
   2445      1.1  christos                 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
   2446      1.1  christos                 algo_found = 1;
   2447      1.1  christos             }
   2448      1.1  christos             if (opt_found(algo, ecdsa_choices, &i)) {
   2449      1.1  christos                 ecdsa_doit[i] = 2;
   2450      1.1  christos                 algo_found = 1;
   2451      1.1  christos             }
   2452      1.1  christos         }
   2453      1.1  christos         if (HAS_PREFIX(algo, "ecdh")) {
   2454      1.1  christos             if (algo[sizeof("ecdh") - 1] == '\0') {
   2455      1.1  christos                 memset(ecdh_doit, 1, sizeof(ecdh_doit));
   2456      1.1  christos                 algo_found = 1;
   2457      1.1  christos             }
   2458      1.1  christos             if (opt_found(algo, ecdh_choices, &i)) {
   2459      1.1  christos                 ecdh_doit[i] = 2;
   2460      1.1  christos                 algo_found = 1;
   2461      1.1  christos             }
   2462      1.1  christos         }
   2463      1.1  christos #ifndef OPENSSL_NO_ECX
   2464      1.1  christos         if (strcmp(algo, "eddsa") == 0) {
   2465      1.1  christos             memset(eddsa_doit, 1, sizeof(eddsa_doit));
   2466      1.1  christos             algo_found = 1;
   2467      1.1  christos         }
   2468      1.1  christos         if (opt_found(algo, eddsa_choices, &i)) {
   2469      1.1  christos             eddsa_doit[i] = 2;
   2470      1.1  christos             algo_found = 1;
   2471      1.1  christos         }
   2472      1.1  christos #endif /* OPENSSL_NO_ECX */
   2473      1.1  christos #ifndef OPENSSL_NO_SM2
   2474      1.1  christos         if (strcmp(algo, "sm2") == 0) {
   2475      1.1  christos             memset(sm2_doit, 1, sizeof(sm2_doit));
   2476      1.1  christos             algo_found = 1;
   2477      1.1  christos         }
   2478      1.1  christos         if (opt_found(algo, sm2_choices, &i)) {
   2479      1.1  christos             sm2_doit[i] = 2;
   2480      1.1  christos             algo_found = 1;
   2481      1.1  christos         }
   2482      1.1  christos #endif
   2483      1.1  christos         if (kem_locate(algo, &idx)) {
   2484      1.1  christos             kems_doit[idx]++;
   2485      1.1  christos             do_kems = 1;
   2486      1.1  christos             algo_found = 1;
   2487      1.1  christos         }
   2488      1.1  christos         if (sig_locate(algo, &idx)) {
   2489      1.1  christos             sigs_doit[idx]++;
   2490      1.1  christos             do_sigs = 1;
   2491      1.1  christos             algo_found = 1;
   2492      1.1  christos         }
   2493      1.1  christos         if (strcmp(algo, "kmac") == 0) {
   2494      1.1  christos             doit[D_KMAC128] = doit[D_KMAC256] = 1;
   2495      1.1  christos             algo_found = 1;
   2496      1.1  christos         }
   2497      1.1  christos         if (strcmp(algo, "cmac") == 0) {
   2498      1.1  christos             doit[D_EVP_CMAC] = 1;
   2499      1.1  christos             algo_found = 1;
   2500      1.1  christos         }
   2501      1.1  christos 
   2502      1.1  christos         if (!algo_found) {
   2503      1.1  christos             BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
   2504      1.1  christos             goto end;
   2505      1.1  christos         }
   2506      1.1  christos     }
   2507      1.1  christos 
   2508      1.1  christos     /* Sanity checks */
   2509      1.1  christos     if (aead) {
   2510      1.1  christos         if (evp_cipher == NULL) {
   2511      1.1  christos             BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
   2512      1.1  christos             goto end;
   2513  1.1.1.2  christos         } else if (!(EVP_CIPHER_get_flags(evp_cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
   2514      1.1  christos             BIO_printf(bio_err, "%s is not an AEAD cipher\n",
   2515  1.1.1.2  christos                 EVP_CIPHER_get0_name(evp_cipher));
   2516      1.1  christos             goto end;
   2517      1.1  christos         }
   2518      1.1  christos     }
   2519      1.1  christos     if (kems_algs_len > 0) {
   2520      1.1  christos         int maxcnt = get_max(kems_doit, kems_algs_len);
   2521      1.1  christos 
   2522      1.1  christos         if (maxcnt > 1) {
   2523      1.1  christos             /* some algs explicitly selected */
   2524      1.1  christos             for (i = 0; i < kems_algs_len; i++) {
   2525      1.1  christos                 /* disable the rest */
   2526      1.1  christos                 kems_doit[i]--;
   2527      1.1  christos             }
   2528      1.1  christos         }
   2529      1.1  christos     }
   2530      1.1  christos     if (sigs_algs_len > 0) {
   2531      1.1  christos         int maxcnt = get_max(sigs_doit, sigs_algs_len);
   2532      1.1  christos 
   2533      1.1  christos         if (maxcnt > 1) {
   2534      1.1  christos             /* some algs explicitly selected */
   2535      1.1  christos             for (i = 0; i < sigs_algs_len; i++) {
   2536      1.1  christos                 /* disable the rest */
   2537      1.1  christos                 sigs_doit[i]--;
   2538      1.1  christos             }
   2539      1.1  christos         }
   2540      1.1  christos     }
   2541      1.1  christos     if (multiblock) {
   2542      1.1  christos         if (evp_cipher == NULL) {
   2543      1.1  christos             BIO_printf(bio_err, "-mb can be used only with a multi-block"
   2544      1.1  christos                                 " capable cipher\n");
   2545      1.1  christos             goto end;
   2546  1.1.1.2  christos         } else if (!(EVP_CIPHER_get_flags(evp_cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
   2547      1.1  christos             BIO_printf(bio_err, "%s is not a multi-block capable\n",
   2548  1.1.1.2  christos                 EVP_CIPHER_get0_name(evp_cipher));
   2549      1.1  christos             goto end;
   2550      1.1  christos         } else if (async_jobs > 0) {
   2551      1.1  christos             BIO_printf(bio_err, "Async mode is not supported with -mb");
   2552      1.1  christos             goto end;
   2553      1.1  christos         }
   2554      1.1  christos     }
   2555      1.1  christos 
   2556      1.1  christos     /* Initialize the job pool if async mode is enabled */
   2557      1.1  christos     if (async_jobs > 0) {
   2558      1.1  christos         async_init = ASYNC_init_thread(async_jobs, async_jobs);
   2559      1.1  christos         if (!async_init) {
   2560      1.1  christos             BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
   2561      1.1  christos             goto end;
   2562      1.1  christos         }
   2563      1.1  christos     }
   2564      1.1  christos 
   2565      1.1  christos     loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
   2566  1.1.1.2  christos     loopargs = app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
   2567      1.1  christos     memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
   2568      1.1  christos 
   2569      1.1  christos     buflen = lengths[size_num - 1];
   2570  1.1.1.2  christos     if (buflen < 36) /* size of random vector in RSA benchmark */
   2571      1.1  christos         buflen = 36;
   2572      1.1  christos     if (INT_MAX - (MAX_MISALIGNMENT + 1) < buflen) {
   2573      1.1  christos         BIO_printf(bio_err, "Error: buffer size too large\n");
   2574      1.1  christos         goto end;
   2575      1.1  christos     }
   2576      1.1  christos     buflen += MAX_MISALIGNMENT + 1;
   2577      1.1  christos     for (i = 0; i < loopargs_len; i++) {
   2578      1.1  christos         if (async_jobs > 0) {
   2579      1.1  christos             loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
   2580      1.1  christos             if (loopargs[i].wait_ctx == NULL) {
   2581      1.1  christos                 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
   2582      1.1  christos                 goto end;
   2583      1.1  christos             }
   2584      1.1  christos         }
   2585      1.1  christos 
   2586      1.1  christos         loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
   2587      1.1  christos         loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
   2588      1.1  christos 
   2589      1.1  christos         /* Align the start of buffers on a 64 byte boundary */
   2590      1.1  christos         loopargs[i].buf = loopargs[i].buf_malloc + misalign;
   2591      1.1  christos         loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
   2592      1.1  christos         loopargs[i].buflen = buflen - misalign;
   2593      1.1  christos         loopargs[i].sigsize = buflen - misalign;
   2594      1.1  christos         loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
   2595      1.1  christos         loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
   2596      1.1  christos #ifndef OPENSSL_NO_DH
   2597      1.1  christos         loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
   2598      1.1  christos         loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
   2599      1.1  christos #endif
   2600      1.1  christos     }
   2601      1.1  christos 
   2602      1.1  christos #ifndef NO_FORK
   2603      1.1  christos     if (multi && do_multi(multi, size_num))
   2604      1.1  christos         goto show_res;
   2605      1.1  christos #endif
   2606      1.1  christos 
   2607      1.1  christos     for (i = 0; i < loopargs_len; ++i) {
   2608      1.1  christos         if (domlock) {
   2609      1.1  christos #if defined(_WIN32)
   2610      1.1  christos             (void)VirtualLock(loopargs[i].buf_malloc, buflen);
   2611      1.1  christos             (void)VirtualLock(loopargs[i].buf2_malloc, buflen);
   2612      1.1  christos #elif defined(OPENSSL_SYS_LINUX)
   2613      1.1  christos             (void)mlock(loopargs[i].buf_malloc, buflen);
   2614      1.1  christos             (void)mlock(loopargs[i].buf_malloc, buflen);
   2615      1.1  christos #endif
   2616      1.1  christos         }
   2617      1.1  christos         memset(loopargs[i].buf_malloc, 0, buflen);
   2618      1.1  christos         memset(loopargs[i].buf2_malloc, 0, buflen);
   2619      1.1  christos     }
   2620      1.1  christos 
   2621      1.1  christos     /* Initialize the engine after the fork */
   2622      1.1  christos     e = setup_engine(engine_id, 0);
   2623      1.1  christos 
   2624      1.1  christos     /* No parameters; turn on everything. */
   2625      1.1  christos     if (argc == 0 && !doit[D_EVP] && !doit[D_HMAC]
   2626      1.1  christos         && !doit[D_EVP_CMAC] && !do_kems && !do_sigs) {
   2627      1.1  christos         memset(doit, 1, sizeof(doit));
   2628      1.1  christos         doit[D_EVP] = doit[D_EVP_CMAC] = 0;
   2629      1.1  christos         ERR_set_mark();
   2630      1.1  christos         for (i = D_MD2; i <= D_WHIRLPOOL; i++) {
   2631      1.1  christos             if (!have_md(names[i]))
   2632      1.1  christos                 doit[i] = 0;
   2633      1.1  christos         }
   2634      1.1  christos         for (i = D_CBC_DES; i <= D_CBC_256_CML; i++) {
   2635      1.1  christos             if (!have_cipher(names[i]))
   2636      1.1  christos                 doit[i] = 0;
   2637      1.1  christos         }
   2638      1.1  christos         if ((mac = EVP_MAC_fetch(app_get0_libctx(), "GMAC",
   2639  1.1.1.2  christos                  app_get0_propq()))
   2640  1.1.1.2  christos             != NULL) {
   2641      1.1  christos             EVP_MAC_free(mac);
   2642      1.1  christos             mac = NULL;
   2643      1.1  christos         } else {
   2644      1.1  christos             doit[D_GHASH] = 0;
   2645      1.1  christos         }
   2646      1.1  christos         if ((mac = EVP_MAC_fetch(app_get0_libctx(), "HMAC",
   2647  1.1.1.2  christos                  app_get0_propq()))
   2648  1.1.1.2  christos             != NULL) {
   2649      1.1  christos             EVP_MAC_free(mac);
   2650      1.1  christos             mac = NULL;
   2651      1.1  christos         } else {
   2652      1.1  christos             doit[D_HMAC] = 0;
   2653      1.1  christos         }
   2654      1.1  christos         ERR_pop_to_mark();
   2655      1.1  christos         memset(rsa_doit, 1, sizeof(rsa_doit));
   2656      1.1  christos #ifndef OPENSSL_NO_DH
   2657      1.1  christos         memset(ffdh_doit, 1, sizeof(ffdh_doit));
   2658      1.1  christos #endif
   2659      1.1  christos #ifndef OPENSSL_NO_DSA
   2660      1.1  christos         memset(dsa_doit, 1, sizeof(dsa_doit));
   2661      1.1  christos #endif
   2662      1.1  christos #ifndef OPENSSL_NO_ECX
   2663      1.1  christos         memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
   2664      1.1  christos         memset(ecdh_doit, 1, sizeof(ecdh_doit));
   2665      1.1  christos         memset(eddsa_doit, 1, sizeof(eddsa_doit));
   2666      1.1  christos #endif /* OPENSSL_NO_ECX */
   2667      1.1  christos #ifndef OPENSSL_NO_SM2
   2668      1.1  christos         memset(sm2_doit, 1, sizeof(sm2_doit));
   2669      1.1  christos #endif
   2670      1.1  christos         memset(kems_doit, 1, sizeof(kems_doit));
   2671      1.1  christos         do_kems = 1;
   2672      1.1  christos         memset(sigs_doit, 1, sizeof(sigs_doit));
   2673      1.1  christos         do_sigs = 1;
   2674      1.1  christos     }
   2675      1.1  christos     for (i = 0; i < ALGOR_NUM; i++)
   2676      1.1  christos         if (doit[i])
   2677      1.1  christos             pr_header++;
   2678      1.1  christos 
   2679      1.1  christos     if (usertime == 0 && !mr)
   2680      1.1  christos         BIO_printf(bio_err,
   2681  1.1.1.2  christos             "You have chosen to measure elapsed time "
   2682  1.1.1.2  christos             "instead of user CPU time.\n");
   2683      1.1  christos 
   2684      1.1  christos #if SIGALRM > 0
   2685      1.1  christos     signal(SIGALRM, alarmed);
   2686      1.1  christos #endif
   2687      1.1  christos 
   2688      1.1  christos     if (doit[D_MD2]) {
   2689      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2690      1.1  christos             print_message(names[D_MD2], lengths[testnum], seconds.sym);
   2691      1.1  christos             Time_F(START);
   2692      1.1  christos             count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
   2693      1.1  christos             d = Time_F(STOP);
   2694      1.1  christos             print_result(D_MD2, testnum, count, d);
   2695      1.1  christos             if (count < 0)
   2696      1.1  christos                 break;
   2697      1.1  christos         }
   2698      1.1  christos     }
   2699      1.1  christos 
   2700      1.1  christos     if (doit[D_MDC2]) {
   2701      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2702      1.1  christos             print_message(names[D_MDC2], lengths[testnum], seconds.sym);
   2703      1.1  christos             Time_F(START);
   2704      1.1  christos             count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
   2705      1.1  christos             d = Time_F(STOP);
   2706      1.1  christos             print_result(D_MDC2, testnum, count, d);
   2707      1.1  christos             if (count < 0)
   2708      1.1  christos                 break;
   2709      1.1  christos         }
   2710      1.1  christos     }
   2711      1.1  christos 
   2712      1.1  christos     if (doit[D_MD4]) {
   2713      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2714      1.1  christos             print_message(names[D_MD4], lengths[testnum], seconds.sym);
   2715      1.1  christos             Time_F(START);
   2716      1.1  christos             count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
   2717      1.1  christos             d = Time_F(STOP);
   2718      1.1  christos             print_result(D_MD4, testnum, count, d);
   2719      1.1  christos             if (count < 0)
   2720      1.1  christos                 break;
   2721      1.1  christos         }
   2722      1.1  christos     }
   2723      1.1  christos 
   2724      1.1  christos     if (doit[D_MD5]) {
   2725      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2726      1.1  christos             print_message(names[D_MD5], lengths[testnum], seconds.sym);
   2727      1.1  christos             Time_F(START);
   2728      1.1  christos             count = run_benchmark(async_jobs, MD5_loop, loopargs);
   2729      1.1  christos             d = Time_F(STOP);
   2730      1.1  christos             print_result(D_MD5, testnum, count, d);
   2731      1.1  christos             if (count < 0)
   2732      1.1  christos                 break;
   2733      1.1  christos         }
   2734      1.1  christos     }
   2735      1.1  christos 
   2736      1.1  christos     if (doit[D_SHA1]) {
   2737      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2738      1.1  christos             print_message(names[D_SHA1], lengths[testnum], seconds.sym);
   2739      1.1  christos             Time_F(START);
   2740      1.1  christos             count = run_benchmark(async_jobs, SHA1_loop, loopargs);
   2741      1.1  christos             d = Time_F(STOP);
   2742      1.1  christos             print_result(D_SHA1, testnum, count, d);
   2743      1.1  christos             if (count < 0)
   2744      1.1  christos                 break;
   2745      1.1  christos         }
   2746      1.1  christos     }
   2747      1.1  christos 
   2748      1.1  christos     if (doit[D_SHA256]) {
   2749      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2750      1.1  christos             print_message(names[D_SHA256], lengths[testnum], seconds.sym);
   2751      1.1  christos             Time_F(START);
   2752      1.1  christos             count = run_benchmark(async_jobs, SHA256_loop, loopargs);
   2753      1.1  christos             d = Time_F(STOP);
   2754      1.1  christos             print_result(D_SHA256, testnum, count, d);
   2755      1.1  christos             if (count < 0)
   2756      1.1  christos                 break;
   2757      1.1  christos         }
   2758      1.1  christos     }
   2759      1.1  christos 
   2760      1.1  christos     if (doit[D_SHA512]) {
   2761      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2762      1.1  christos             print_message(names[D_SHA512], lengths[testnum], seconds.sym);
   2763      1.1  christos             Time_F(START);
   2764      1.1  christos             count = run_benchmark(async_jobs, SHA512_loop, loopargs);
   2765      1.1  christos             d = Time_F(STOP);
   2766      1.1  christos             print_result(D_SHA512, testnum, count, d);
   2767      1.1  christos             if (count < 0)
   2768      1.1  christos                 break;
   2769      1.1  christos         }
   2770      1.1  christos     }
   2771      1.1  christos 
   2772      1.1  christos     if (doit[D_WHIRLPOOL]) {
   2773      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2774      1.1  christos             print_message(names[D_WHIRLPOOL], lengths[testnum], seconds.sym);
   2775      1.1  christos             Time_F(START);
   2776      1.1  christos             count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
   2777      1.1  christos             d = Time_F(STOP);
   2778      1.1  christos             print_result(D_WHIRLPOOL, testnum, count, d);
   2779      1.1  christos             if (count < 0)
   2780      1.1  christos                 break;
   2781      1.1  christos         }
   2782      1.1  christos     }
   2783      1.1  christos 
   2784      1.1  christos     if (doit[D_RMD160]) {
   2785      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2786      1.1  christos             print_message(names[D_RMD160], lengths[testnum], seconds.sym);
   2787      1.1  christos             Time_F(START);
   2788      1.1  christos             count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
   2789      1.1  christos             d = Time_F(STOP);
   2790      1.1  christos             print_result(D_RMD160, testnum, count, d);
   2791      1.1  christos             if (count < 0)
   2792      1.1  christos                 break;
   2793      1.1  christos         }
   2794      1.1  christos     }
   2795      1.1  christos 
   2796      1.1  christos     if (doit[D_HMAC]) {
   2797      1.1  christos         static const char hmac_key[] = "This is a key...";
   2798      1.1  christos         int len = strlen(hmac_key);
   2799      1.1  christos         size_t hmac_name_len = sizeof("hmac()") + strlen(evp_mac_mdname);
   2800      1.1  christos         OSSL_PARAM params[3];
   2801      1.1  christos 
   2802      1.1  christos         if (evp_mac_mdname == NULL)
   2803      1.1  christos             goto end;
   2804      1.1  christos         evp_hmac_name = app_malloc(hmac_name_len, "HMAC name");
   2805      1.1  christos         BIO_snprintf(evp_hmac_name, hmac_name_len, "hmac(%s)", evp_mac_mdname);
   2806      1.1  christos         names[D_HMAC] = evp_hmac_name;
   2807      1.1  christos 
   2808  1.1.1.2  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
   2809  1.1.1.2  christos             evp_mac_mdname, 0);
   2810  1.1.1.2  christos         params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
   2811  1.1.1.2  christos             (char *)hmac_key, len);
   2812      1.1  christos         params[2] = OSSL_PARAM_construct_end();
   2813      1.1  christos 
   2814      1.1  christos         if (mac_setup("HMAC", &mac, params, loopargs, loopargs_len) < 1)
   2815      1.1  christos             goto end;
   2816      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2817      1.1  christos             print_message(names[D_HMAC], lengths[testnum], seconds.sym);
   2818      1.1  christos             Time_F(START);
   2819      1.1  christos             count = run_benchmark(async_jobs, HMAC_loop, loopargs);
   2820      1.1  christos             d = Time_F(STOP);
   2821      1.1  christos             print_result(D_HMAC, testnum, count, d);
   2822      1.1  christos             if (count < 0)
   2823      1.1  christos                 break;
   2824      1.1  christos         }
   2825      1.1  christos         mac_teardown(&mac, loopargs, loopargs_len);
   2826      1.1  christos     }
   2827      1.1  christos 
   2828      1.1  christos     if (doit[D_CBC_DES]) {
   2829      1.1  christos         int st = 1;
   2830      1.1  christos 
   2831      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   2832      1.1  christos             loopargs[i].ctx = init_evp_cipher_ctx("des-cbc", deskey,
   2833  1.1.1.2  christos                 sizeof(deskey) / 3);
   2834      1.1  christos             st = loopargs[i].ctx != NULL;
   2835      1.1  christos         }
   2836      1.1  christos         algindex = D_CBC_DES;
   2837      1.1  christos         for (testnum = 0; st && testnum < size_num; testnum++) {
   2838      1.1  christos             if (!check_block_size(loopargs[0].ctx, lengths[testnum]))
   2839      1.1  christos                 break;
   2840      1.1  christos             print_message(names[D_CBC_DES], lengths[testnum], seconds.sym);
   2841      1.1  christos             Time_F(START);
   2842      1.1  christos             count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
   2843      1.1  christos             d = Time_F(STOP);
   2844      1.1  christos             print_result(D_CBC_DES, testnum, count, d);
   2845      1.1  christos         }
   2846      1.1  christos         for (i = 0; i < loopargs_len; i++)
   2847      1.1  christos             EVP_CIPHER_CTX_free(loopargs[i].ctx);
   2848      1.1  christos     }
   2849      1.1  christos 
   2850      1.1  christos     if (doit[D_EDE3_DES]) {
   2851      1.1  christos         int st = 1;
   2852      1.1  christos 
   2853      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   2854      1.1  christos             loopargs[i].ctx = init_evp_cipher_ctx("des-ede3-cbc", deskey,
   2855  1.1.1.2  christos                 sizeof(deskey));
   2856      1.1  christos             st = loopargs[i].ctx != NULL;
   2857      1.1  christos         }
   2858      1.1  christos         algindex = D_EDE3_DES;
   2859      1.1  christos         for (testnum = 0; st && testnum < size_num; testnum++) {
   2860      1.1  christos             if (!check_block_size(loopargs[0].ctx, lengths[testnum]))
   2861      1.1  christos                 break;
   2862      1.1  christos             print_message(names[D_EDE3_DES], lengths[testnum], seconds.sym);
   2863      1.1  christos             Time_F(START);
   2864  1.1.1.2  christos             count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
   2865      1.1  christos             d = Time_F(STOP);
   2866      1.1  christos             print_result(D_EDE3_DES, testnum, count, d);
   2867      1.1  christos         }
   2868      1.1  christos         for (i = 0; i < loopargs_len; i++)
   2869      1.1  christos             EVP_CIPHER_CTX_free(loopargs[i].ctx);
   2870      1.1  christos     }
   2871      1.1  christos 
   2872      1.1  christos     for (k = 0; k < 3; k++) {
   2873      1.1  christos         algindex = D_CBC_128_AES + k;
   2874      1.1  christos         if (doit[algindex]) {
   2875      1.1  christos             int st = 1;
   2876      1.1  christos 
   2877      1.1  christos             keylen = 16 + k * 8;
   2878      1.1  christos             for (i = 0; st && i < loopargs_len; i++) {
   2879      1.1  christos                 loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
   2880  1.1.1.2  christos                     key32, keylen);
   2881      1.1  christos                 st = loopargs[i].ctx != NULL;
   2882      1.1  christos             }
   2883      1.1  christos 
   2884      1.1  christos             for (testnum = 0; st && testnum < size_num; testnum++) {
   2885      1.1  christos                 if (!check_block_size(loopargs[0].ctx, lengths[testnum]))
   2886      1.1  christos                     break;
   2887      1.1  christos                 print_message(names[algindex], lengths[testnum], seconds.sym);
   2888      1.1  christos                 Time_F(START);
   2889  1.1.1.2  christos                 count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
   2890      1.1  christos                 d = Time_F(STOP);
   2891      1.1  christos                 print_result(algindex, testnum, count, d);
   2892      1.1  christos             }
   2893      1.1  christos             for (i = 0; i < loopargs_len; i++)
   2894      1.1  christos                 EVP_CIPHER_CTX_free(loopargs[i].ctx);
   2895      1.1  christos         }
   2896      1.1  christos     }
   2897      1.1  christos 
   2898      1.1  christos     for (k = 0; k < 3; k++) {
   2899      1.1  christos         algindex = D_CBC_128_CML + k;
   2900      1.1  christos         if (doit[algindex]) {
   2901      1.1  christos             int st = 1;
   2902      1.1  christos 
   2903      1.1  christos             keylen = 16 + k * 8;
   2904      1.1  christos             for (i = 0; st && i < loopargs_len; i++) {
   2905      1.1  christos                 loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
   2906  1.1.1.2  christos                     key32, keylen);
   2907      1.1  christos                 st = loopargs[i].ctx != NULL;
   2908      1.1  christos             }
   2909      1.1  christos 
   2910      1.1  christos             for (testnum = 0; st && testnum < size_num; testnum++) {
   2911      1.1  christos                 if (!check_block_size(loopargs[0].ctx, lengths[testnum]))
   2912      1.1  christos                     break;
   2913      1.1  christos                 print_message(names[algindex], lengths[testnum], seconds.sym);
   2914      1.1  christos                 Time_F(START);
   2915  1.1.1.2  christos                 count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
   2916      1.1  christos                 d = Time_F(STOP);
   2917      1.1  christos                 print_result(algindex, testnum, count, d);
   2918      1.1  christos             }
   2919      1.1  christos             for (i = 0; i < loopargs_len; i++)
   2920      1.1  christos                 EVP_CIPHER_CTX_free(loopargs[i].ctx);
   2921      1.1  christos         }
   2922      1.1  christos     }
   2923      1.1  christos 
   2924      1.1  christos     for (algindex = D_RC4; algindex <= D_CBC_CAST; algindex++) {
   2925      1.1  christos         if (doit[algindex]) {
   2926      1.1  christos             int st = 1;
   2927      1.1  christos 
   2928      1.1  christos             keylen = 16;
   2929      1.1  christos             for (i = 0; st && i < loopargs_len; i++) {
   2930      1.1  christos                 loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
   2931  1.1.1.2  christos                     key32, keylen);
   2932      1.1  christos                 st = loopargs[i].ctx != NULL;
   2933      1.1  christos             }
   2934      1.1  christos 
   2935      1.1  christos             for (testnum = 0; st && testnum < size_num; testnum++) {
   2936      1.1  christos                 if (!check_block_size(loopargs[0].ctx, lengths[testnum]))
   2937      1.1  christos                     break;
   2938      1.1  christos                 print_message(names[algindex], lengths[testnum], seconds.sym);
   2939      1.1  christos                 Time_F(START);
   2940  1.1.1.2  christos                 count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
   2941      1.1  christos                 d = Time_F(STOP);
   2942      1.1  christos                 print_result(algindex, testnum, count, d);
   2943      1.1  christos             }
   2944      1.1  christos             for (i = 0; i < loopargs_len; i++)
   2945      1.1  christos                 EVP_CIPHER_CTX_free(loopargs[i].ctx);
   2946      1.1  christos         }
   2947      1.1  christos     }
   2948      1.1  christos     if (doit[D_GHASH]) {
   2949      1.1  christos         static const char gmac_iv[] = "0123456789ab";
   2950      1.1  christos         OSSL_PARAM params[4];
   2951      1.1  christos 
   2952      1.1  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
   2953  1.1.1.2  christos             "aes-128-gcm", 0);
   2954      1.1  christos         params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
   2955  1.1.1.2  christos             (char *)gmac_iv,
   2956  1.1.1.2  christos             sizeof(gmac_iv) - 1);
   2957      1.1  christos         params[2] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
   2958  1.1.1.2  christos             (void *)key32, 16);
   2959      1.1  christos         params[3] = OSSL_PARAM_construct_end();
   2960      1.1  christos 
   2961      1.1  christos         if (mac_setup("GMAC", &mac, params, loopargs, loopargs_len) < 1)
   2962      1.1  christos             goto end;
   2963      1.1  christos         /* b/c of the definition of GHASH_loop(), init() calls are needed here */
   2964      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   2965      1.1  christos             if (!EVP_MAC_init(loopargs[i].mctx, NULL, 0, NULL))
   2966      1.1  christos                 goto end;
   2967      1.1  christos         }
   2968      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2969      1.1  christos             print_message(names[D_GHASH], lengths[testnum], seconds.sym);
   2970      1.1  christos             Time_F(START);
   2971      1.1  christos             count = run_benchmark(async_jobs, GHASH_loop, loopargs);
   2972      1.1  christos             d = Time_F(STOP);
   2973      1.1  christos             print_result(D_GHASH, testnum, count, d);
   2974      1.1  christos             if (count < 0)
   2975      1.1  christos                 break;
   2976      1.1  christos         }
   2977      1.1  christos         mac_teardown(&mac, loopargs, loopargs_len);
   2978      1.1  christos     }
   2979      1.1  christos 
   2980      1.1  christos     if (doit[D_RAND]) {
   2981      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   2982      1.1  christos             print_message(names[D_RAND], lengths[testnum], seconds.sym);
   2983      1.1  christos             Time_F(START);
   2984      1.1  christos             count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
   2985      1.1  christos             d = Time_F(STOP);
   2986      1.1  christos             print_result(D_RAND, testnum, count, d);
   2987      1.1  christos         }
   2988      1.1  christos     }
   2989      1.1  christos 
   2990      1.1  christos     /*-
   2991      1.1  christos      * There are three scenarios for D_EVP:
   2992      1.1  christos      * 1- Using authenticated encryption (AE) e.g. CCM, GCM, OCB etc.
   2993      1.1  christos      * 2- Using AE + associated data (AD) i.e. AEAD using CCM, GCM, OCB etc.
   2994      1.1  christos      * 3- Not using AE or AD e.g. ECB, CBC, CFB etc.
   2995      1.1  christos      */
   2996      1.1  christos     if (doit[D_EVP]) {
   2997      1.1  christos         if (evp_cipher != NULL) {
   2998  1.1.1.2  christos             int (*loopfunc)(void *);
   2999      1.1  christos             int outlen = 0;
   3000      1.1  christos             unsigned int ae_mode = 0;
   3001      1.1  christos 
   3002  1.1.1.2  christos             if (multiblock && (EVP_CIPHER_get_flags(evp_cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
   3003      1.1  christos                 multiblock_speed(evp_cipher, lengths_single, &seconds);
   3004      1.1  christos                 ret = 0;
   3005      1.1  christos                 goto end;
   3006      1.1  christos             }
   3007      1.1  christos 
   3008      1.1  christos             names[D_EVP] = EVP_CIPHER_get0_name(evp_cipher);
   3009      1.1  christos 
   3010      1.1  christos             mode_op = EVP_CIPHER_get_mode(evp_cipher);
   3011      1.1  christos 
   3012      1.1  christos             if (aead) {
   3013      1.1  christos                 if (lengths == lengths_list) {
   3014      1.1  christos                     lengths = aead_lengths_list;
   3015      1.1  christos                     size_num = OSSL_NELEM(aead_lengths_list);
   3016      1.1  christos                 }
   3017      1.1  christos             }
   3018      1.1  christos             if (mode_op == EVP_CIPH_GCM_MODE
   3019      1.1  christos                 || mode_op == EVP_CIPH_CCM_MODE
   3020      1.1  christos                 || mode_op == EVP_CIPH_OCB_MODE
   3021      1.1  christos                 || mode_op == EVP_CIPH_SIV_MODE
   3022      1.1  christos                 || mode_op == EVP_CIPH_GCM_SIV_MODE) {
   3023      1.1  christos                 ae_mode = 1;
   3024      1.1  christos                 if (decrypt)
   3025      1.1  christos                     loopfunc = EVP_Update_loop_aead_dec;
   3026      1.1  christos                 else
   3027      1.1  christos                     loopfunc = EVP_Update_loop_aead_enc;
   3028      1.1  christos             } else {
   3029      1.1  christos                 loopfunc = EVP_Update_loop;
   3030      1.1  christos             }
   3031      1.1  christos 
   3032      1.1  christos             for (testnum = 0; testnum < size_num; testnum++) {
   3033      1.1  christos                 print_message(names[D_EVP], lengths[testnum], seconds.sym);
   3034      1.1  christos 
   3035      1.1  christos                 for (k = 0; k < loopargs_len; k++) {
   3036      1.1  christos                     loopargs[k].ctx = EVP_CIPHER_CTX_new();
   3037      1.1  christos                     if (loopargs[k].ctx == NULL) {
   3038      1.1  christos                         BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
   3039      1.1  christos                         exit(1);
   3040      1.1  christos                     }
   3041      1.1  christos 
   3042      1.1  christos                     /*
   3043      1.1  christos                      * For AE modes, we must first encrypt the data to get
   3044      1.1  christos                      * a valid tag that enables us to decrypt. If we don't
   3045      1.1  christos                      * encrypt first, we won't have a valid tag that enables
   3046      1.1  christos                      * authenticity and hence decryption will fail.
   3047      1.1  christos                      */
   3048      1.1  christos                     if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
   3049  1.1.1.2  christos                             NULL, NULL, ae_mode ? 1 : !decrypt)) {
   3050      1.1  christos                         BIO_printf(bio_err, "\nCouldn't init the context\n");
   3051      1.1  christos                         dofail();
   3052      1.1  christos                         exit(1);
   3053      1.1  christos                     }
   3054      1.1  christos 
   3055      1.1  christos                     /* Padding isn't needed */
   3056      1.1  christos                     EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
   3057      1.1  christos 
   3058      1.1  christos                     keylen = EVP_CIPHER_CTX_get_key_length(loopargs[k].ctx);
   3059      1.1  christos                     loopargs[k].key = app_malloc(keylen, "evp_cipher key");
   3060      1.1  christos                     EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
   3061      1.1  christos 
   3062      1.1  christos                     if (!ae_mode) {
   3063      1.1  christos                         if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
   3064  1.1.1.2  christos                                 loopargs[k].key, iv, -1)) {
   3065      1.1  christos                             BIO_printf(bio_err, "\nFailed to set the key\n");
   3066      1.1  christos                             dofail();
   3067      1.1  christos                             exit(1);
   3068      1.1  christos                         }
   3069      1.1  christos                     } else if (mode_op == EVP_CIPH_SIV_MODE
   3070  1.1.1.2  christos                         || mode_op == EVP_CIPH_GCM_SIV_MODE) {
   3071      1.1  christos                         EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
   3072  1.1.1.2  christos                             EVP_CTRL_SET_SPEED, 1, NULL);
   3073      1.1  christos                     }
   3074      1.1  christos                     if (ae_mode && decrypt) {
   3075      1.1  christos                         /* Set length of iv (Doesn't apply to SIV mode) */
   3076      1.1  christos                         if (mode_op != EVP_CIPH_SIV_MODE) {
   3077      1.1  christos                             if (!EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
   3078  1.1.1.2  christos                                     EVP_CTRL_AEAD_SET_IVLEN,
   3079  1.1.1.2  christos                                     sizeof(aead_iv), NULL)) {
   3080      1.1  christos                                 BIO_printf(bio_err, "\nFailed to set iv length\n");
   3081      1.1  christos                                 dofail();
   3082      1.1  christos                                 exit(1);
   3083      1.1  christos                             }
   3084      1.1  christos                         }
   3085      1.1  christos                         /* Set tag_len (Not for GCM/SIV at encryption stage) */
   3086      1.1  christos                         if (mode_op != EVP_CIPH_GCM_MODE
   3087      1.1  christos                             && mode_op != EVP_CIPH_SIV_MODE
   3088      1.1  christos                             && mode_op != EVP_CIPH_GCM_SIV_MODE) {
   3089      1.1  christos                             if (!EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
   3090  1.1.1.2  christos                                     EVP_CTRL_AEAD_SET_TAG,
   3091  1.1.1.2  christos                                     TAG_LEN, NULL)) {
   3092      1.1  christos                                 BIO_printf(bio_err,
   3093  1.1.1.2  christos                                     "\nFailed to set tag length\n");
   3094      1.1  christos                                 dofail();
   3095      1.1  christos                                 exit(1);
   3096      1.1  christos                             }
   3097      1.1  christos                         }
   3098      1.1  christos                         if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
   3099  1.1.1.2  christos                                 loopargs[k].key, aead_iv, -1)) {
   3100      1.1  christos                             BIO_printf(bio_err, "\nFailed to set the key\n");
   3101      1.1  christos                             dofail();
   3102      1.1  christos                             exit(1);
   3103      1.1  christos                         }
   3104      1.1  christos                         /* Set total length of input. Only required for CCM */
   3105      1.1  christos                         if (mode_op == EVP_CIPH_CCM_MODE) {
   3106      1.1  christos                             if (!EVP_EncryptUpdate(loopargs[k].ctx, NULL,
   3107  1.1.1.2  christos                                     &outlen, NULL,
   3108  1.1.1.2  christos                                     lengths[testnum])) {
   3109      1.1  christos                                 BIO_printf(bio_err,
   3110  1.1.1.2  christos                                     "\nCouldn't set input text length\n");
   3111      1.1  christos                                 dofail();
   3112      1.1  christos                                 exit(1);
   3113      1.1  christos                             }
   3114      1.1  christos                         }
   3115      1.1  christos                         if (aead) {
   3116      1.1  christos                             if (!EVP_EncryptUpdate(loopargs[k].ctx, NULL,
   3117  1.1.1.2  christos                                     &outlen, aad, sizeof(aad))) {
   3118      1.1  christos                                 BIO_printf(bio_err,
   3119  1.1.1.2  christos                                     "\nCouldn't insert AAD when encrypting\n");
   3120      1.1  christos                                 dofail();
   3121      1.1  christos                                 exit(1);
   3122      1.1  christos                             }
   3123      1.1  christos                         }
   3124      1.1  christos                         if (!EVP_EncryptUpdate(loopargs[k].ctx, loopargs[k].buf,
   3125  1.1.1.2  christos                                 &outlen, loopargs[k].buf,
   3126  1.1.1.2  christos                                 lengths[testnum])) {
   3127      1.1  christos                             BIO_printf(bio_err,
   3128  1.1.1.2  christos                                 "\nFailed to to encrypt the data\n");
   3129      1.1  christos                             dofail();
   3130      1.1  christos                             exit(1);
   3131      1.1  christos                         }
   3132      1.1  christos 
   3133      1.1  christos                         if (!EVP_EncryptFinal_ex(loopargs[k].ctx,
   3134  1.1.1.2  christos                                 loopargs[k].buf, &outlen)) {
   3135      1.1  christos                             BIO_printf(bio_err,
   3136  1.1.1.2  christos                                 "\nFailed finalize the encryption\n");
   3137      1.1  christos                             dofail();
   3138      1.1  christos                             exit(1);
   3139      1.1  christos                         }
   3140      1.1  christos 
   3141      1.1  christos                         if (!EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_AEAD_GET_TAG,
   3142  1.1.1.2  christos                                 TAG_LEN, &loopargs[k].tag)) {
   3143      1.1  christos                             BIO_printf(bio_err, "\nFailed to get the tag\n");
   3144      1.1  christos                             dofail();
   3145      1.1  christos                             exit(1);
   3146      1.1  christos                         }
   3147      1.1  christos 
   3148      1.1  christos                         EVP_CIPHER_CTX_free(loopargs[k].ctx);
   3149      1.1  christos                         loopargs[k].ctx = EVP_CIPHER_CTX_new();
   3150      1.1  christos                         if (loopargs[k].ctx == NULL) {
   3151      1.1  christos                             BIO_printf(bio_err,
   3152  1.1.1.2  christos                                 "\nEVP_CIPHER_CTX_new failure\n");
   3153      1.1  christos                             exit(1);
   3154      1.1  christos                         }
   3155      1.1  christos                         if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher,
   3156  1.1.1.2  christos                                 NULL, NULL, NULL, 0)) {
   3157      1.1  christos                             BIO_printf(bio_err,
   3158  1.1.1.2  christos                                 "\nFailed initializing the context\n");
   3159      1.1  christos                             dofail();
   3160      1.1  christos                             exit(1);
   3161      1.1  christos                         }
   3162      1.1  christos 
   3163      1.1  christos                         EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
   3164      1.1  christos 
   3165      1.1  christos                         /* GCM-SIV/SIV only allows for a single Update operation */
   3166      1.1  christos                         if (mode_op == EVP_CIPH_SIV_MODE
   3167      1.1  christos                             || mode_op == EVP_CIPH_GCM_SIV_MODE)
   3168      1.1  christos                             EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
   3169  1.1.1.2  christos                                 EVP_CTRL_SET_SPEED, 1, NULL);
   3170      1.1  christos                     }
   3171      1.1  christos                 }
   3172      1.1  christos 
   3173      1.1  christos                 Time_F(START);
   3174      1.1  christos                 count = run_benchmark(async_jobs, loopfunc, loopargs);
   3175      1.1  christos                 d = Time_F(STOP);
   3176      1.1  christos                 for (k = 0; k < loopargs_len; k++) {
   3177      1.1  christos                     OPENSSL_clear_free(loopargs[k].key, keylen);
   3178      1.1  christos                     EVP_CIPHER_CTX_free(loopargs[k].ctx);
   3179      1.1  christos                 }
   3180      1.1  christos                 print_result(D_EVP, testnum, count, d);
   3181      1.1  christos             }
   3182      1.1  christos         } else if (evp_md_name != NULL) {
   3183      1.1  christos             names[D_EVP] = evp_md_name;
   3184      1.1  christos 
   3185      1.1  christos             for (testnum = 0; testnum < size_num; testnum++) {
   3186      1.1  christos                 print_message(names[D_EVP], lengths[testnum], seconds.sym);
   3187      1.1  christos                 Time_F(START);
   3188      1.1  christos                 count = run_benchmark(async_jobs, EVP_Digest_md_loop, loopargs);
   3189      1.1  christos                 d = Time_F(STOP);
   3190      1.1  christos                 print_result(D_EVP, testnum, count, d);
   3191      1.1  christos                 if (count < 0)
   3192      1.1  christos                     break;
   3193      1.1  christos             }
   3194      1.1  christos         }
   3195      1.1  christos     }
   3196      1.1  christos 
   3197      1.1  christos     if (doit[D_EVP_CMAC]) {
   3198      1.1  christos         size_t len = sizeof("cmac()") + strlen(evp_mac_ciphername);
   3199      1.1  christos         OSSL_PARAM params[3];
   3200      1.1  christos         EVP_CIPHER *cipher = NULL;
   3201      1.1  christos 
   3202      1.1  christos         if (!opt_cipher(evp_mac_ciphername, &cipher))
   3203      1.1  christos             goto end;
   3204      1.1  christos 
   3205      1.1  christos         keylen = EVP_CIPHER_get_key_length(cipher);
   3206      1.1  christos         EVP_CIPHER_free(cipher);
   3207      1.1  christos         if (keylen <= 0 || keylen > (int)sizeof(key32)) {
   3208      1.1  christos             BIO_printf(bio_err, "\nRequested CMAC cipher with unsupported key length.\n");
   3209      1.1  christos             goto end;
   3210      1.1  christos         }
   3211      1.1  christos         evp_cmac_name = app_malloc(len, "CMAC name");
   3212      1.1  christos         BIO_snprintf(evp_cmac_name, len, "cmac(%s)", evp_mac_ciphername);
   3213      1.1  christos         names[D_EVP_CMAC] = evp_cmac_name;
   3214      1.1  christos 
   3215      1.1  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
   3216  1.1.1.2  christos             evp_mac_ciphername, 0);
   3217      1.1  christos         params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
   3218  1.1.1.2  christos             (char *)key32, keylen);
   3219      1.1  christos         params[2] = OSSL_PARAM_construct_end();
   3220      1.1  christos 
   3221      1.1  christos         if (mac_setup("CMAC", &mac, params, loopargs, loopargs_len) < 1)
   3222      1.1  christos             goto end;
   3223      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   3224      1.1  christos             print_message(names[D_EVP_CMAC], lengths[testnum], seconds.sym);
   3225      1.1  christos             Time_F(START);
   3226      1.1  christos             count = run_benchmark(async_jobs, CMAC_loop, loopargs);
   3227      1.1  christos             d = Time_F(STOP);
   3228      1.1  christos             print_result(D_EVP_CMAC, testnum, count, d);
   3229      1.1  christos             if (count < 0)
   3230      1.1  christos                 break;
   3231      1.1  christos         }
   3232      1.1  christos         mac_teardown(&mac, loopargs, loopargs_len);
   3233      1.1  christos     }
   3234      1.1  christos 
   3235      1.1  christos     if (doit[D_KMAC128]) {
   3236      1.1  christos         OSSL_PARAM params[2];
   3237      1.1  christos 
   3238      1.1  christos         params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
   3239  1.1.1.2  christos             (void *)key32, 16);
   3240      1.1  christos         params[1] = OSSL_PARAM_construct_end();
   3241      1.1  christos 
   3242      1.1  christos         if (mac_setup("KMAC-128", &mac, params, loopargs, loopargs_len) < 1)
   3243      1.1  christos             goto end;
   3244      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   3245      1.1  christos             print_message(names[D_KMAC128], lengths[testnum], seconds.sym);
   3246      1.1  christos             Time_F(START);
   3247      1.1  christos             count = run_benchmark(async_jobs, KMAC128_loop, loopargs);
   3248      1.1  christos             d = Time_F(STOP);
   3249      1.1  christos             print_result(D_KMAC128, testnum, count, d);
   3250      1.1  christos             if (count < 0)
   3251      1.1  christos                 break;
   3252      1.1  christos         }
   3253      1.1  christos         mac_teardown(&mac, loopargs, loopargs_len);
   3254      1.1  christos     }
   3255      1.1  christos 
   3256      1.1  christos     if (doit[D_KMAC256]) {
   3257      1.1  christos         OSSL_PARAM params[2];
   3258      1.1  christos 
   3259      1.1  christos         params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
   3260  1.1.1.2  christos             (void *)key32, 32);
   3261      1.1  christos         params[1] = OSSL_PARAM_construct_end();
   3262      1.1  christos 
   3263      1.1  christos         if (mac_setup("KMAC-256", &mac, params, loopargs, loopargs_len) < 1)
   3264      1.1  christos             goto end;
   3265      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   3266      1.1  christos             print_message(names[D_KMAC256], lengths[testnum], seconds.sym);
   3267      1.1  christos             Time_F(START);
   3268      1.1  christos             count = run_benchmark(async_jobs, KMAC256_loop, loopargs);
   3269      1.1  christos             d = Time_F(STOP);
   3270      1.1  christos             print_result(D_KMAC256, testnum, count, d);
   3271      1.1  christos             if (count < 0)
   3272      1.1  christos                 break;
   3273      1.1  christos         }
   3274      1.1  christos         mac_teardown(&mac, loopargs, loopargs_len);
   3275      1.1  christos     }
   3276      1.1  christos 
   3277      1.1  christos     for (i = 0; i < loopargs_len; i++)
   3278      1.1  christos         if (RAND_bytes(loopargs[i].buf, 36) <= 0)
   3279      1.1  christos             goto end;
   3280      1.1  christos 
   3281      1.1  christos     for (testnum = 0; testnum < RSA_NUM; testnum++) {
   3282      1.1  christos         EVP_PKEY *rsa_key = NULL;
   3283      1.1  christos         int st = 0;
   3284      1.1  christos 
   3285      1.1  christos         if (!rsa_doit[testnum])
   3286      1.1  christos             continue;
   3287      1.1  christos 
   3288      1.1  christos         if (primes > RSA_DEFAULT_PRIME_NUM) {
   3289      1.1  christos             /* we haven't set keys yet,  generate multi-prime RSA keys */
   3290      1.1  christos             bn = BN_new();
   3291      1.1  christos             st = bn != NULL
   3292      1.1  christos                 && BN_set_word(bn, RSA_F4)
   3293      1.1  christos                 && init_gen_str(&genctx, "RSA", NULL, 0, NULL, NULL)
   3294      1.1  christos                 && EVP_PKEY_CTX_set_rsa_keygen_bits(genctx, rsa_keys[testnum].bits) > 0
   3295      1.1  christos                 && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(genctx, bn) > 0
   3296      1.1  christos                 && EVP_PKEY_CTX_set_rsa_keygen_primes(genctx, primes) > 0
   3297      1.1  christos                 && EVP_PKEY_keygen(genctx, &rsa_key) > 0;
   3298      1.1  christos             BN_free(bn);
   3299      1.1  christos             bn = NULL;
   3300      1.1  christos             EVP_PKEY_CTX_free(genctx);
   3301      1.1  christos             genctx = NULL;
   3302      1.1  christos         } else {
   3303      1.1  christos             const unsigned char *p = rsa_keys[testnum].data;
   3304      1.1  christos 
   3305      1.1  christos             st = (rsa_key = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p,
   3306  1.1.1.2  christos                       rsa_keys[testnum].length))
   3307  1.1.1.2  christos                 != NULL;
   3308      1.1  christos         }
   3309      1.1  christos 
   3310      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3311      1.1  christos             loopargs[i].rsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
   3312      1.1  christos             loopargs[i].sigsize = loopargs[i].buflen;
   3313      1.1  christos             if (loopargs[i].rsa_sign_ctx[testnum] == NULL
   3314      1.1  christos                 || EVP_PKEY_sign_init(loopargs[i].rsa_sign_ctx[testnum]) <= 0
   3315      1.1  christos                 || EVP_PKEY_sign(loopargs[i].rsa_sign_ctx[testnum],
   3316  1.1.1.2  christos                        loopargs[i].buf2,
   3317  1.1.1.2  christos                        &loopargs[i].sigsize,
   3318  1.1.1.2  christos                        loopargs[i].buf, 36)
   3319  1.1.1.2  christos                     <= 0)
   3320      1.1  christos                 st = 0;
   3321      1.1  christos         }
   3322      1.1  christos         if (!st) {
   3323      1.1  christos             BIO_printf(bio_err,
   3324  1.1.1.2  christos                 "RSA sign setup failure.  No RSA sign will be done.\n");
   3325      1.1  christos             dofail();
   3326      1.1  christos             op_count = 1;
   3327      1.1  christos         } else {
   3328      1.1  christos             pkey_print_message("private", "rsa sign",
   3329  1.1.1.2  christos                 rsa_keys[testnum].bits, seconds.rsa);
   3330      1.1  christos             /* RSA_blinding_on(rsa_key[testnum],NULL); */
   3331      1.1  christos             Time_F(START);
   3332      1.1  christos             count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
   3333      1.1  christos             d = Time_F(STOP);
   3334      1.1  christos             BIO_printf(bio_err,
   3335  1.1.1.2  christos                 mr ? "+R1:%ld:%d:%.2f\n"
   3336  1.1.1.2  christos                    : "%ld %u bits private RSA sign ops in %.2fs\n",
   3337  1.1.1.2  christos                 count, rsa_keys[testnum].bits, d);
   3338      1.1  christos             rsa_results[testnum][0] = (double)count / d;
   3339      1.1  christos             op_count = count;
   3340      1.1  christos         }
   3341      1.1  christos 
   3342      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3343      1.1  christos             loopargs[i].rsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key,
   3344  1.1.1.2  christos                 NULL);
   3345      1.1  christos             if (loopargs[i].rsa_verify_ctx[testnum] == NULL
   3346      1.1  christos                 || EVP_PKEY_verify_init(loopargs[i].rsa_verify_ctx[testnum]) <= 0
   3347      1.1  christos                 || EVP_PKEY_verify(loopargs[i].rsa_verify_ctx[testnum],
   3348  1.1.1.2  christos                        loopargs[i].buf2,
   3349  1.1.1.2  christos                        loopargs[i].sigsize,
   3350  1.1.1.2  christos                        loopargs[i].buf, 36)
   3351  1.1.1.2  christos                     <= 0)
   3352      1.1  christos                 st = 0;
   3353      1.1  christos         }
   3354      1.1  christos         if (!st) {
   3355      1.1  christos             BIO_printf(bio_err,
   3356  1.1.1.2  christos                 "RSA verify setup failure.  No RSA verify will be done.\n");
   3357      1.1  christos             dofail();
   3358      1.1  christos             rsa_doit[testnum] = 0;
   3359      1.1  christos         } else {
   3360      1.1  christos             pkey_print_message("public", "rsa verify",
   3361  1.1.1.2  christos                 rsa_keys[testnum].bits, seconds.rsa);
   3362      1.1  christos             Time_F(START);
   3363      1.1  christos             count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
   3364      1.1  christos             d = Time_F(STOP);
   3365      1.1  christos             BIO_printf(bio_err,
   3366  1.1.1.2  christos                 mr ? "+R2:%ld:%d:%.2f\n"
   3367  1.1.1.2  christos                    : "%ld %u bits public RSA verify ops in %.2fs\n",
   3368  1.1.1.2  christos                 count, rsa_keys[testnum].bits, d);
   3369      1.1  christos             rsa_results[testnum][1] = (double)count / d;
   3370      1.1  christos         }
   3371      1.1  christos 
   3372      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3373      1.1  christos             loopargs[i].rsa_encrypt_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
   3374      1.1  christos             loopargs[i].encsize = loopargs[i].buflen;
   3375      1.1  christos             if (loopargs[i].rsa_encrypt_ctx[testnum] == NULL
   3376      1.1  christos                 || EVP_PKEY_encrypt_init(loopargs[i].rsa_encrypt_ctx[testnum]) <= 0
   3377      1.1  christos                 || EVP_PKEY_encrypt(loopargs[i].rsa_encrypt_ctx[testnum],
   3378  1.1.1.2  christos                        loopargs[i].buf2,
   3379  1.1.1.2  christos                        &loopargs[i].encsize,
   3380  1.1.1.2  christos                        loopargs[i].buf, 36)
   3381  1.1.1.2  christos                     <= 0)
   3382      1.1  christos                 st = 0;
   3383      1.1  christos         }
   3384      1.1  christos         if (!st) {
   3385      1.1  christos             BIO_printf(bio_err,
   3386  1.1.1.2  christos                 "RSA encrypt setup failure.  No RSA encrypt will be done.\n");
   3387      1.1  christos             dofail();
   3388      1.1  christos             op_count = 1;
   3389      1.1  christos         } else {
   3390      1.1  christos             pkey_print_message("public", "rsa encrypt",
   3391  1.1.1.2  christos                 rsa_keys[testnum].bits, seconds.rsa);
   3392      1.1  christos             /* RSA_blinding_on(rsa_key[testnum],NULL); */
   3393      1.1  christos             Time_F(START);
   3394      1.1  christos             count = run_benchmark(async_jobs, RSA_encrypt_loop, loopargs);
   3395      1.1  christos             d = Time_F(STOP);
   3396      1.1  christos             BIO_printf(bio_err,
   3397  1.1.1.2  christos                 mr ? "+R3:%ld:%d:%.2f\n"
   3398  1.1.1.2  christos                    : "%ld %u bits public RSA encrypt ops in %.2fs\n",
   3399  1.1.1.2  christos                 count, rsa_keys[testnum].bits, d);
   3400      1.1  christos             rsa_results[testnum][2] = (double)count / d;
   3401      1.1  christos             op_count = count;
   3402      1.1  christos         }
   3403      1.1  christos 
   3404      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3405      1.1  christos             loopargs[i].rsa_decrypt_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
   3406      1.1  christos             declen = loopargs[i].buflen;
   3407      1.1  christos             if (loopargs[i].rsa_decrypt_ctx[testnum] == NULL
   3408      1.1  christos                 || EVP_PKEY_decrypt_init(loopargs[i].rsa_decrypt_ctx[testnum]) <= 0
   3409      1.1  christos                 || EVP_PKEY_decrypt(loopargs[i].rsa_decrypt_ctx[testnum],
   3410  1.1.1.2  christos                        loopargs[i].buf,
   3411  1.1.1.2  christos                        &declen,
   3412  1.1.1.2  christos                        loopargs[i].buf2,
   3413  1.1.1.2  christos                        loopargs[i].encsize)
   3414  1.1.1.2  christos                     <= 0)
   3415      1.1  christos                 st = 0;
   3416      1.1  christos         }
   3417      1.1  christos         if (!st) {
   3418      1.1  christos             BIO_printf(bio_err,
   3419  1.1.1.2  christos                 "RSA decrypt setup failure.  No RSA decrypt will be done.\n");
   3420      1.1  christos             dofail();
   3421      1.1  christos             op_count = 1;
   3422      1.1  christos         } else {
   3423      1.1  christos             pkey_print_message("private", "rsa decrypt",
   3424  1.1.1.2  christos                 rsa_keys[testnum].bits, seconds.rsa);
   3425      1.1  christos             /* RSA_blinding_on(rsa_key[testnum],NULL); */
   3426      1.1  christos             Time_F(START);
   3427      1.1  christos             count = run_benchmark(async_jobs, RSA_decrypt_loop, loopargs);
   3428      1.1  christos             d = Time_F(STOP);
   3429      1.1  christos             BIO_printf(bio_err,
   3430  1.1.1.2  christos                 mr ? "+R4:%ld:%d:%.2f\n"
   3431  1.1.1.2  christos                    : "%ld %u bits private RSA decrypt ops in %.2fs\n",
   3432  1.1.1.2  christos                 count, rsa_keys[testnum].bits, d);
   3433      1.1  christos             rsa_results[testnum][3] = (double)count / d;
   3434      1.1  christos             op_count = count;
   3435      1.1  christos         }
   3436      1.1  christos 
   3437      1.1  christos         if (op_count <= 1) {
   3438      1.1  christos             /* if longer than 10s, don't do any more */
   3439      1.1  christos             stop_it(rsa_doit, testnum);
   3440      1.1  christos         }
   3441      1.1  christos         EVP_PKEY_free(rsa_key);
   3442      1.1  christos     }
   3443      1.1  christos 
   3444      1.1  christos #ifndef OPENSSL_NO_DSA
   3445      1.1  christos     for (testnum = 0; testnum < DSA_NUM; testnum++) {
   3446      1.1  christos         EVP_PKEY *dsa_key = NULL;
   3447      1.1  christos         int st;
   3448      1.1  christos 
   3449      1.1  christos         if (!dsa_doit[testnum])
   3450      1.1  christos             continue;
   3451      1.1  christos 
   3452      1.1  christos         st = (dsa_key = get_dsa(dsa_bits[testnum])) != NULL;
   3453      1.1  christos 
   3454      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3455      1.1  christos             loopargs[i].dsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
   3456  1.1.1.2  christos                 NULL);
   3457      1.1  christos             loopargs[i].sigsize = loopargs[i].buflen;
   3458      1.1  christos             if (loopargs[i].dsa_sign_ctx[testnum] == NULL
   3459      1.1  christos                 || EVP_PKEY_sign_init(loopargs[i].dsa_sign_ctx[testnum]) <= 0
   3460      1.1  christos                 || EVP_PKEY_sign(loopargs[i].dsa_sign_ctx[testnum],
   3461  1.1.1.2  christos                        loopargs[i].buf2,
   3462  1.1.1.2  christos                        &loopargs[i].sigsize,
   3463  1.1.1.2  christos                        loopargs[i].buf, 20)
   3464  1.1.1.2  christos                     <= 0)
   3465      1.1  christos                 st = 0;
   3466      1.1  christos         }
   3467      1.1  christos         if (!st) {
   3468      1.1  christos             BIO_printf(bio_err,
   3469  1.1.1.2  christos                 "DSA sign setup failure.  No DSA sign will be done.\n");
   3470      1.1  christos             dofail();
   3471      1.1  christos             op_count = 1;
   3472      1.1  christos         } else {
   3473      1.1  christos             pkey_print_message("sign", "dsa",
   3474  1.1.1.2  christos                 dsa_bits[testnum], seconds.dsa);
   3475      1.1  christos             Time_F(START);
   3476      1.1  christos             count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
   3477      1.1  christos             d = Time_F(STOP);
   3478      1.1  christos             BIO_printf(bio_err,
   3479  1.1.1.2  christos                 mr ? "+R5:%ld:%u:%.2f\n"
   3480  1.1.1.2  christos                    : "%ld %u bits DSA sign ops in %.2fs\n",
   3481  1.1.1.2  christos                 count, dsa_bits[testnum], d);
   3482      1.1  christos             dsa_results[testnum][0] = (double)count / d;
   3483      1.1  christos             op_count = count;
   3484      1.1  christos         }
   3485      1.1  christos 
   3486      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3487      1.1  christos             loopargs[i].dsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
   3488  1.1.1.2  christos                 NULL);
   3489      1.1  christos             if (loopargs[i].dsa_verify_ctx[testnum] == NULL
   3490      1.1  christos                 || EVP_PKEY_verify_init(loopargs[i].dsa_verify_ctx[testnum]) <= 0
   3491      1.1  christos                 || EVP_PKEY_verify(loopargs[i].dsa_verify_ctx[testnum],
   3492  1.1.1.2  christos                        loopargs[i].buf2,
   3493  1.1.1.2  christos                        loopargs[i].sigsize,
   3494  1.1.1.2  christos                        loopargs[i].buf, 36)
   3495  1.1.1.2  christos                     <= 0)
   3496      1.1  christos                 st = 0;
   3497      1.1  christos         }
   3498      1.1  christos         if (!st) {
   3499      1.1  christos             BIO_printf(bio_err,
   3500  1.1.1.2  christos                 "DSA verify setup failure.  No DSA verify will be done.\n");
   3501      1.1  christos             dofail();
   3502      1.1  christos             dsa_doit[testnum] = 0;
   3503      1.1  christos         } else {
   3504      1.1  christos             pkey_print_message("verify", "dsa",
   3505  1.1.1.2  christos                 dsa_bits[testnum], seconds.dsa);
   3506      1.1  christos             Time_F(START);
   3507      1.1  christos             count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
   3508      1.1  christos             d = Time_F(STOP);
   3509      1.1  christos             BIO_printf(bio_err,
   3510  1.1.1.2  christos                 mr ? "+R6:%ld:%u:%.2f\n"
   3511  1.1.1.2  christos                    : "%ld %u bits DSA verify ops in %.2fs\n",
   3512  1.1.1.2  christos                 count, dsa_bits[testnum], d);
   3513      1.1  christos             dsa_results[testnum][1] = (double)count / d;
   3514      1.1  christos         }
   3515      1.1  christos 
   3516      1.1  christos         if (op_count <= 1) {
   3517      1.1  christos             /* if longer than 10s, don't do any more */
   3518      1.1  christos             stop_it(dsa_doit, testnum);
   3519      1.1  christos         }
   3520      1.1  christos         EVP_PKEY_free(dsa_key);
   3521      1.1  christos     }
   3522      1.1  christos #endif /* OPENSSL_NO_DSA */
   3523      1.1  christos 
   3524      1.1  christos     for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
   3525      1.1  christos         EVP_PKEY *ecdsa_key = NULL;
   3526      1.1  christos         int st;
   3527      1.1  christos 
   3528      1.1  christos         if (!ecdsa_doit[testnum])
   3529      1.1  christos             continue;
   3530      1.1  christos 
   3531      1.1  christos         st = (ecdsa_key = get_ecdsa(&ec_curves[testnum])) != NULL;
   3532      1.1  christos 
   3533      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3534      1.1  christos             loopargs[i].ecdsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
   3535  1.1.1.2  christos                 NULL);
   3536      1.1  christos             loopargs[i].sigsize = loopargs[i].buflen;
   3537      1.1  christos             if (loopargs[i].ecdsa_sign_ctx[testnum] == NULL
   3538      1.1  christos                 || EVP_PKEY_sign_init(loopargs[i].ecdsa_sign_ctx[testnum]) <= 0
   3539      1.1  christos                 || EVP_PKEY_sign(loopargs[i].ecdsa_sign_ctx[testnum],
   3540  1.1.1.2  christos                        loopargs[i].buf2,
   3541  1.1.1.2  christos                        &loopargs[i].sigsize,
   3542  1.1.1.2  christos                        loopargs[i].buf, 20)
   3543  1.1.1.2  christos                     <= 0)
   3544      1.1  christos                 st = 0;
   3545      1.1  christos         }
   3546      1.1  christos         if (!st) {
   3547      1.1  christos             BIO_printf(bio_err,
   3548  1.1.1.2  christos                 "ECDSA sign setup failure.  No ECDSA sign will be done.\n");
   3549      1.1  christos             dofail();
   3550      1.1  christos             op_count = 1;
   3551      1.1  christos         } else {
   3552      1.1  christos             pkey_print_message("sign", "ecdsa",
   3553  1.1.1.2  christos                 ec_curves[testnum].bits, seconds.ecdsa);
   3554      1.1  christos             Time_F(START);
   3555      1.1  christos             count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
   3556      1.1  christos             d = Time_F(STOP);
   3557      1.1  christos             BIO_printf(bio_err,
   3558  1.1.1.2  christos                 mr ? "+R7:%ld:%u:%.2f\n"
   3559  1.1.1.2  christos                    : "%ld %u bits ECDSA sign ops in %.2fs\n",
   3560  1.1.1.2  christos                 count, ec_curves[testnum].bits, d);
   3561      1.1  christos             ecdsa_results[testnum][0] = (double)count / d;
   3562      1.1  christos             op_count = count;
   3563      1.1  christos         }
   3564      1.1  christos 
   3565      1.1  christos         for (i = 0; st && i < loopargs_len; i++) {
   3566      1.1  christos             loopargs[i].ecdsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
   3567  1.1.1.2  christos                 NULL);
   3568      1.1  christos             if (loopargs[i].ecdsa_verify_ctx[testnum] == NULL
   3569      1.1  christos                 || EVP_PKEY_verify_init(loopargs[i].ecdsa_verify_ctx[testnum]) <= 0
   3570      1.1  christos                 || EVP_PKEY_verify(loopargs[i].ecdsa_verify_ctx[testnum],
   3571  1.1.1.2  christos                        loopargs[i].buf2,
   3572  1.1.1.2  christos                        loopargs[i].sigsize,
   3573  1.1.1.2  christos                        loopargs[i].buf, 20)
   3574  1.1.1.2  christos                     <= 0)
   3575      1.1  christos                 st = 0;
   3576      1.1  christos         }
   3577      1.1  christos         if (!st) {
   3578      1.1  christos             BIO_printf(bio_err,
   3579  1.1.1.2  christos                 "ECDSA verify setup failure.  No ECDSA verify will be done.\n");
   3580      1.1  christos             dofail();
   3581      1.1  christos             ecdsa_doit[testnum] = 0;
   3582      1.1  christos         } else {
   3583      1.1  christos             pkey_print_message("verify", "ecdsa",
   3584  1.1.1.2  christos                 ec_curves[testnum].bits, seconds.ecdsa);
   3585      1.1  christos             Time_F(START);
   3586      1.1  christos             count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
   3587      1.1  christos             d = Time_F(STOP);
   3588      1.1  christos             BIO_printf(bio_err,
   3589  1.1.1.2  christos                 mr ? "+R8:%ld:%u:%.2f\n"
   3590  1.1.1.2  christos                    : "%ld %u bits ECDSA verify ops in %.2fs\n",
   3591  1.1.1.2  christos                 count, ec_curves[testnum].bits, d);
   3592      1.1  christos             ecdsa_results[testnum][1] = (double)count / d;
   3593      1.1  christos         }
   3594      1.1  christos 
   3595      1.1  christos         if (op_count <= 1) {
   3596      1.1  christos             /* if longer than 10s, don't do any more */
   3597      1.1  christos             stop_it(ecdsa_doit, testnum);
   3598      1.1  christos         }
   3599      1.1  christos         EVP_PKEY_free(ecdsa_key);
   3600      1.1  christos     }
   3601      1.1  christos 
   3602      1.1  christos     for (testnum = 0; testnum < EC_NUM; testnum++) {
   3603      1.1  christos         int ecdh_checks = 1;
   3604      1.1  christos 
   3605      1.1  christos         if (!ecdh_doit[testnum])
   3606      1.1  christos             continue;
   3607      1.1  christos 
   3608      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   3609      1.1  christos             EVP_PKEY_CTX *test_ctx = NULL;
   3610      1.1  christos             EVP_PKEY_CTX *ctx = NULL;
   3611      1.1  christos             EVP_PKEY *key_A = NULL;
   3612      1.1  christos             EVP_PKEY *key_B = NULL;
   3613      1.1  christos             size_t outlen;
   3614      1.1  christos             size_t test_outlen;
   3615      1.1  christos 
   3616      1.1  christos             if ((key_A = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key A */
   3617      1.1  christos                 || (key_B = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key B */
   3618      1.1  christos                 || (ctx = EVP_PKEY_CTX_new(key_A, NULL)) == NULL /* derivation ctx from skeyA */
   3619      1.1  christos                 || EVP_PKEY_derive_init(ctx) <= 0 /* init derivation ctx */
   3620      1.1  christos                 || EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 /* set peer pubkey in ctx */
   3621      1.1  christos                 || EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 /* determine max length */
   3622      1.1  christos                 || outlen == 0 /* ensure outlen is a valid size */
   3623      1.1  christos                 || outlen > MAX_ECDH_SIZE /* avoid buffer overflow */) {
   3624      1.1  christos                 ecdh_checks = 0;
   3625      1.1  christos                 BIO_printf(bio_err, "ECDH key generation failure.\n");
   3626      1.1  christos                 dofail();
   3627      1.1  christos                 op_count = 1;
   3628      1.1  christos                 break;
   3629      1.1  christos             }
   3630      1.1  christos 
   3631      1.1  christos             /*
   3632      1.1  christos              * Here we perform a test run, comparing the output of a*B and b*A;
   3633      1.1  christos              * we try this here and assume that further EVP_PKEY_derive calls
   3634      1.1  christos              * never fail, so we can skip checks in the actually benchmarked
   3635      1.1  christos              * code, for maximum performance.
   3636      1.1  christos              */
   3637      1.1  christos             if ((test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) == NULL /* test ctx from skeyB */
   3638      1.1  christos                 || EVP_PKEY_derive_init(test_ctx) <= 0 /* init derivation test_ctx */
   3639      1.1  christos                 || EVP_PKEY_derive_set_peer(test_ctx, key_A) <= 0 /* set peer pubkey in test_ctx */
   3640      1.1  christos                 || EVP_PKEY_derive(test_ctx, NULL, &test_outlen) <= 0 /* determine max length */
   3641      1.1  christos                 || EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) <= 0 /* compute a*B */
   3642      1.1  christos                 || EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) <= 0 /* compute b*A */
   3643      1.1  christos                 || test_outlen != outlen /* compare output length */) {
   3644      1.1  christos                 ecdh_checks = 0;
   3645      1.1  christos                 BIO_printf(bio_err, "ECDH computation failure.\n");
   3646      1.1  christos                 dofail();
   3647      1.1  christos                 op_count = 1;
   3648      1.1  christos                 break;
   3649      1.1  christos             }
   3650      1.1  christos 
   3651      1.1  christos             /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
   3652      1.1  christos             if (CRYPTO_memcmp(loopargs[i].secret_a,
   3653  1.1.1.2  christos                     loopargs[i].secret_b, outlen)) {
   3654      1.1  christos                 ecdh_checks = 0;
   3655      1.1  christos                 BIO_printf(bio_err, "ECDH computations don't match.\n");
   3656      1.1  christos                 dofail();
   3657      1.1  christos                 op_count = 1;
   3658      1.1  christos                 break;
   3659      1.1  christos             }
   3660      1.1  christos 
   3661      1.1  christos             loopargs[i].ecdh_ctx[testnum] = ctx;
   3662      1.1  christos             loopargs[i].outlen[testnum] = outlen;
   3663      1.1  christos 
   3664      1.1  christos             EVP_PKEY_free(key_A);
   3665      1.1  christos             EVP_PKEY_free(key_B);
   3666      1.1  christos             EVP_PKEY_CTX_free(test_ctx);
   3667      1.1  christos             test_ctx = NULL;
   3668      1.1  christos         }
   3669      1.1  christos         if (ecdh_checks != 0) {
   3670      1.1  christos             pkey_print_message("", "ecdh",
   3671  1.1.1.2  christos                 ec_curves[testnum].bits, seconds.ecdh);
   3672      1.1  christos             Time_F(START);
   3673  1.1.1.2  christos             count = run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
   3674      1.1  christos             d = Time_F(STOP);
   3675      1.1  christos             BIO_printf(bio_err,
   3676  1.1.1.2  christos                 mr ? "+R9:%ld:%d:%.2f\n" : "%ld %u-bits ECDH ops in %.2fs\n", count,
   3677  1.1.1.2  christos                 ec_curves[testnum].bits, d);
   3678      1.1  christos             ecdh_results[testnum][0] = (double)count / d;
   3679      1.1  christos             op_count = count;
   3680      1.1  christos         }
   3681      1.1  christos 
   3682      1.1  christos         if (op_count <= 1) {
   3683      1.1  christos             /* if longer than 10s, don't do any more */
   3684      1.1  christos             stop_it(ecdh_doit, testnum);
   3685      1.1  christos         }
   3686      1.1  christos     }
   3687      1.1  christos 
   3688      1.1  christos #ifndef OPENSSL_NO_ECX
   3689      1.1  christos     for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
   3690      1.1  christos         int st = 1;
   3691      1.1  christos         EVP_PKEY *ed_pkey = NULL;
   3692      1.1  christos         EVP_PKEY_CTX *ed_pctx = NULL;
   3693      1.1  christos 
   3694      1.1  christos         if (!eddsa_doit[testnum])
   3695  1.1.1.2  christos             continue; /* Ignore Curve */
   3696      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   3697      1.1  christos             loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
   3698      1.1  christos             if (loopargs[i].eddsa_ctx[testnum] == NULL) {
   3699      1.1  christos                 st = 0;
   3700      1.1  christos                 break;
   3701      1.1  christos             }
   3702      1.1  christos             loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
   3703      1.1  christos             if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
   3704      1.1  christos                 st = 0;
   3705      1.1  christos                 break;
   3706      1.1  christos             }
   3707      1.1  christos 
   3708      1.1  christos             if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid,
   3709  1.1.1.2  christos                      NULL))
   3710  1.1.1.2  christos                     == NULL
   3711      1.1  christos                 || EVP_PKEY_keygen_init(ed_pctx) <= 0
   3712      1.1  christos                 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
   3713      1.1  christos                 st = 0;
   3714      1.1  christos                 EVP_PKEY_CTX_free(ed_pctx);
   3715      1.1  christos                 break;
   3716      1.1  christos             }
   3717      1.1  christos             EVP_PKEY_CTX_free(ed_pctx);
   3718      1.1  christos 
   3719      1.1  christos             if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
   3720  1.1.1.2  christos                     NULL, ed_pkey)) {
   3721      1.1  christos                 st = 0;
   3722      1.1  christos                 EVP_PKEY_free(ed_pkey);
   3723      1.1  christos                 break;
   3724      1.1  christos             }
   3725      1.1  christos             if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
   3726  1.1.1.2  christos                     NULL, NULL, ed_pkey)) {
   3727      1.1  christos                 st = 0;
   3728      1.1  christos                 EVP_PKEY_free(ed_pkey);
   3729      1.1  christos                 break;
   3730      1.1  christos             }
   3731      1.1  christos 
   3732      1.1  christos             EVP_PKEY_free(ed_pkey);
   3733      1.1  christos             ed_pkey = NULL;
   3734      1.1  christos         }
   3735      1.1  christos         if (st == 0) {
   3736      1.1  christos             BIO_printf(bio_err, "EdDSA failure.\n");
   3737      1.1  christos             dofail();
   3738      1.1  christos             op_count = 1;
   3739      1.1  christos         } else {
   3740      1.1  christos             for (i = 0; i < loopargs_len; i++) {
   3741      1.1  christos                 /* Perform EdDSA signature test */
   3742      1.1  christos                 loopargs[i].sigsize = ed_curves[testnum].sigsize;
   3743      1.1  christos                 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
   3744  1.1.1.2  christos                     loopargs[i].buf2, &loopargs[i].sigsize,
   3745  1.1.1.2  christos                     loopargs[i].buf, 20);
   3746      1.1  christos                 if (st == 0)
   3747      1.1  christos                     break;
   3748      1.1  christos             }
   3749      1.1  christos             if (st == 0) {
   3750      1.1  christos                 BIO_printf(bio_err,
   3751  1.1.1.2  christos                     "EdDSA sign failure.  No EdDSA sign will be done.\n");
   3752      1.1  christos                 dofail();
   3753      1.1  christos                 op_count = 1;
   3754      1.1  christos             } else {
   3755      1.1  christos                 pkey_print_message("sign", ed_curves[testnum].name,
   3756  1.1.1.2  christos                     ed_curves[testnum].bits, seconds.eddsa);
   3757      1.1  christos                 Time_F(START);
   3758      1.1  christos                 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
   3759      1.1  christos                 d = Time_F(STOP);
   3760      1.1  christos 
   3761      1.1  christos                 BIO_printf(bio_err,
   3762  1.1.1.2  christos                     mr ? "+R10:%ld:%u:%s:%.2f\n" : "%ld %u bits %s sign ops in %.2fs \n",
   3763  1.1.1.2  christos                     count, ed_curves[testnum].bits,
   3764  1.1.1.2  christos                     ed_curves[testnum].name, d);
   3765      1.1  christos                 eddsa_results[testnum][0] = (double)count / d;
   3766      1.1  christos                 op_count = count;
   3767      1.1  christos             }
   3768      1.1  christos             /* Perform EdDSA verification test */
   3769      1.1  christos             for (i = 0; i < loopargs_len; i++) {
   3770      1.1  christos                 st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
   3771  1.1.1.2  christos                     loopargs[i].buf2, loopargs[i].sigsize,
   3772  1.1.1.2  christos                     loopargs[i].buf, 20);
   3773      1.1  christos                 if (st != 1)
   3774      1.1  christos                     break;
   3775      1.1  christos             }
   3776      1.1  christos             if (st != 1) {
   3777      1.1  christos                 BIO_printf(bio_err,
   3778  1.1.1.2  christos                     "EdDSA verify failure.  No EdDSA verify will be done.\n");
   3779      1.1  christos                 dofail();
   3780      1.1  christos                 eddsa_doit[testnum] = 0;
   3781      1.1  christos             } else {
   3782      1.1  christos                 pkey_print_message("verify", ed_curves[testnum].name,
   3783  1.1.1.2  christos                     ed_curves[testnum].bits, seconds.eddsa);
   3784      1.1  christos                 Time_F(START);
   3785      1.1  christos                 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
   3786      1.1  christos                 d = Time_F(STOP);
   3787      1.1  christos                 BIO_printf(bio_err,
   3788  1.1.1.2  christos                     mr ? "+R11:%ld:%u:%s:%.2f\n"
   3789  1.1.1.2  christos                        : "%ld %u bits %s verify ops in %.2fs\n",
   3790  1.1.1.2  christos                     count, ed_curves[testnum].bits,
   3791  1.1.1.2  christos                     ed_curves[testnum].name, d);
   3792      1.1  christos                 eddsa_results[testnum][1] = (double)count / d;
   3793      1.1  christos             }
   3794      1.1  christos 
   3795      1.1  christos             if (op_count <= 1) {
   3796      1.1  christos                 /* if longer than 10s, don't do any more */
   3797      1.1  christos                 stop_it(eddsa_doit, testnum);
   3798      1.1  christos             }
   3799      1.1  christos         }
   3800      1.1  christos     }
   3801      1.1  christos #endif /* OPENSSL_NO_ECX */
   3802      1.1  christos 
   3803      1.1  christos #ifndef OPENSSL_NO_SM2
   3804      1.1  christos     for (testnum = 0; testnum < SM2_NUM; testnum++) {
   3805      1.1  christos         int st = 1;
   3806      1.1  christos         EVP_PKEY *sm2_pkey = NULL;
   3807      1.1  christos 
   3808      1.1  christos         if (!sm2_doit[testnum])
   3809  1.1.1.2  christos             continue; /* Ignore Curve */
   3810      1.1  christos         /* Init signing and verification */
   3811      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   3812      1.1  christos             EVP_PKEY_CTX *sm2_pctx = NULL;
   3813      1.1  christos             EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
   3814      1.1  christos             EVP_PKEY_CTX *pctx = NULL;
   3815      1.1  christos             st = 0;
   3816      1.1  christos 
   3817      1.1  christos             loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
   3818      1.1  christos             loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
   3819      1.1  christos             if (loopargs[i].sm2_ctx[testnum] == NULL
   3820  1.1.1.2  christos                 || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
   3821      1.1  christos                 break;
   3822      1.1  christos 
   3823      1.1  christos             sm2_pkey = NULL;
   3824      1.1  christos 
   3825      1.1  christos             st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL)) == NULL
   3826      1.1  christos                 || EVP_PKEY_keygen_init(pctx) <= 0
   3827      1.1  christos                 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
   3828  1.1.1.2  christos                        sm2_curves[testnum].nid)
   3829  1.1.1.2  christos                     <= 0
   3830      1.1  christos                 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
   3831      1.1  christos             EVP_PKEY_CTX_free(pctx);
   3832      1.1  christos             if (st == 0)
   3833      1.1  christos                 break;
   3834      1.1  christos 
   3835      1.1  christos             st = 0; /* set back to zero */
   3836      1.1  christos             /* attach it sooner to rely on main final cleanup */
   3837      1.1  christos             loopargs[i].sm2_pkey[testnum] = sm2_pkey;
   3838      1.1  christos             loopargs[i].sigsize = EVP_PKEY_get_size(sm2_pkey);
   3839      1.1  christos 
   3840      1.1  christos             sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
   3841      1.1  christos             sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
   3842      1.1  christos             if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
   3843      1.1  christos                 EVP_PKEY_CTX_free(sm2_vfy_pctx);
   3844      1.1  christos                 break;
   3845      1.1  christos             }
   3846      1.1  christos 
   3847      1.1  christos             /* attach them directly to respective ctx */
   3848      1.1  christos             EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
   3849      1.1  christos             EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
   3850      1.1  christos 
   3851      1.1  christos             /*
   3852      1.1  christos              * No need to allow user to set an explicit ID here, just use
   3853      1.1  christos              * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
   3854      1.1  christos              */
   3855      1.1  christos             if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
   3856      1.1  christos                 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
   3857      1.1  christos                 break;
   3858      1.1  christos 
   3859      1.1  christos             if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
   3860  1.1.1.2  christos                     EVP_sm3(), NULL, sm2_pkey))
   3861      1.1  christos                 break;
   3862      1.1  christos             if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
   3863  1.1.1.2  christos                     EVP_sm3(), NULL, sm2_pkey))
   3864      1.1  christos                 break;
   3865  1.1.1.2  christos             st = 1; /* mark loop as succeeded */
   3866      1.1  christos         }
   3867      1.1  christos         if (st == 0) {
   3868      1.1  christos             BIO_printf(bio_err, "SM2 init failure.\n");
   3869      1.1  christos             dofail();
   3870      1.1  christos             op_count = 1;
   3871      1.1  christos         } else {
   3872      1.1  christos             for (i = 0; i < loopargs_len; i++) {
   3873      1.1  christos                 /* Perform SM2 signature test */
   3874      1.1  christos                 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
   3875  1.1.1.2  christos                     loopargs[i].buf2, &loopargs[i].sigsize,
   3876  1.1.1.2  christos                     loopargs[i].buf, 20);
   3877      1.1  christos                 if (st == 0)
   3878      1.1  christos                     break;
   3879      1.1  christos             }
   3880      1.1  christos             if (st == 0) {
   3881      1.1  christos                 BIO_printf(bio_err,
   3882  1.1.1.2  christos                     "SM2 sign failure.  No SM2 sign will be done.\n");
   3883      1.1  christos                 dofail();
   3884      1.1  christos                 op_count = 1;
   3885      1.1  christos             } else {
   3886      1.1  christos                 pkey_print_message("sign", sm2_curves[testnum].name,
   3887  1.1.1.2  christos                     sm2_curves[testnum].bits, seconds.sm2);
   3888      1.1  christos                 Time_F(START);
   3889      1.1  christos                 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
   3890      1.1  christos                 d = Time_F(STOP);
   3891      1.1  christos 
   3892      1.1  christos                 BIO_printf(bio_err,
   3893  1.1.1.2  christos                     mr ? "+R12:%ld:%u:%s:%.2f\n" : "%ld %u bits %s sign ops in %.2fs \n",
   3894  1.1.1.2  christos                     count, sm2_curves[testnum].bits,
   3895  1.1.1.2  christos                     sm2_curves[testnum].name, d);
   3896      1.1  christos                 sm2_results[testnum][0] = (double)count / d;
   3897      1.1  christos                 op_count = count;
   3898      1.1  christos             }
   3899      1.1  christos 
   3900      1.1  christos             /* Perform SM2 verification test */
   3901      1.1  christos             for (i = 0; i < loopargs_len; i++) {
   3902      1.1  christos                 st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
   3903  1.1.1.2  christos                     loopargs[i].buf2, loopargs[i].sigsize,
   3904  1.1.1.2  christos                     loopargs[i].buf, 20);
   3905      1.1  christos                 if (st != 1)
   3906      1.1  christos                     break;
   3907      1.1  christos             }
   3908      1.1  christos             if (st != 1) {
   3909      1.1  christos                 BIO_printf(bio_err,
   3910  1.1.1.2  christos                     "SM2 verify failure.  No SM2 verify will be done.\n");
   3911      1.1  christos                 dofail();
   3912      1.1  christos                 sm2_doit[testnum] = 0;
   3913      1.1  christos             } else {
   3914      1.1  christos                 pkey_print_message("verify", sm2_curves[testnum].name,
   3915  1.1.1.2  christos                     sm2_curves[testnum].bits, seconds.sm2);
   3916      1.1  christos                 Time_F(START);
   3917      1.1  christos                 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
   3918      1.1  christos                 d = Time_F(STOP);
   3919      1.1  christos                 BIO_printf(bio_err,
   3920  1.1.1.2  christos                     mr ? "+R13:%ld:%u:%s:%.2f\n"
   3921  1.1.1.2  christos                        : "%ld %u bits %s verify ops in %.2fs\n",
   3922  1.1.1.2  christos                     count, sm2_curves[testnum].bits,
   3923  1.1.1.2  christos                     sm2_curves[testnum].name, d);
   3924      1.1  christos                 sm2_results[testnum][1] = (double)count / d;
   3925      1.1  christos             }
   3926      1.1  christos 
   3927      1.1  christos             if (op_count <= 1) {
   3928      1.1  christos                 /* if longer than 10s, don't do any more */
   3929      1.1  christos                 for (testnum++; testnum < SM2_NUM; testnum++)
   3930      1.1  christos                     sm2_doit[testnum] = 0;
   3931      1.1  christos             }
   3932      1.1  christos         }
   3933      1.1  christos     }
   3934  1.1.1.2  christos #endif /* OPENSSL_NO_SM2 */
   3935      1.1  christos 
   3936      1.1  christos #ifndef OPENSSL_NO_DH
   3937      1.1  christos     for (testnum = 0; testnum < FFDH_NUM; testnum++) {
   3938      1.1  christos         int ffdh_checks = 1;
   3939      1.1  christos 
   3940      1.1  christos         if (!ffdh_doit[testnum])
   3941      1.1  christos             continue;
   3942      1.1  christos 
   3943      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   3944      1.1  christos             EVP_PKEY *pkey_A = NULL;
   3945      1.1  christos             EVP_PKEY *pkey_B = NULL;
   3946      1.1  christos             EVP_PKEY_CTX *ffdh_ctx = NULL;
   3947      1.1  christos             EVP_PKEY_CTX *test_ctx = NULL;
   3948      1.1  christos             size_t secret_size;
   3949      1.1  christos             size_t test_out;
   3950      1.1  christos 
   3951      1.1  christos             /* Ensure that the error queue is empty */
   3952      1.1  christos             if (ERR_peek_error()) {
   3953      1.1  christos                 BIO_printf(bio_err,
   3954  1.1.1.2  christos                     "WARNING: the error queue contains previous unhandled errors.\n");
   3955      1.1  christos                 dofail();
   3956      1.1  christos             }
   3957      1.1  christos 
   3958      1.1  christos             pkey_A = EVP_PKEY_new();
   3959      1.1  christos             if (!pkey_A) {
   3960      1.1  christos                 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
   3961      1.1  christos                 dofail();
   3962      1.1  christos                 op_count = 1;
   3963      1.1  christos                 ffdh_checks = 0;
   3964      1.1  christos                 break;
   3965      1.1  christos             }
   3966      1.1  christos             pkey_B = EVP_PKEY_new();
   3967      1.1  christos             if (!pkey_B) {
   3968      1.1  christos                 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
   3969      1.1  christos                 dofail();
   3970      1.1  christos                 op_count = 1;
   3971      1.1  christos                 ffdh_checks = 0;
   3972      1.1  christos                 break;
   3973      1.1  christos             }
   3974      1.1  christos 
   3975      1.1  christos             ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
   3976      1.1  christos             if (!ffdh_ctx) {
   3977      1.1  christos                 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
   3978      1.1  christos                 dofail();
   3979      1.1  christos                 op_count = 1;
   3980      1.1  christos                 ffdh_checks = 0;
   3981      1.1  christos                 break;
   3982      1.1  christos             }
   3983      1.1  christos 
   3984      1.1  christos             if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
   3985      1.1  christos                 BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
   3986      1.1  christos                 dofail();
   3987      1.1  christos                 op_count = 1;
   3988      1.1  christos                 ffdh_checks = 0;
   3989      1.1  christos                 break;
   3990      1.1  christos             }
   3991      1.1  christos             if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
   3992      1.1  christos                 BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
   3993      1.1  christos                 dofail();
   3994      1.1  christos                 op_count = 1;
   3995      1.1  christos                 ffdh_checks = 0;
   3996      1.1  christos                 break;
   3997      1.1  christos             }
   3998      1.1  christos 
   3999  1.1.1.2  christos             if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 || EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
   4000      1.1  christos                 BIO_printf(bio_err, "FFDH key generation failure.\n");
   4001      1.1  christos                 dofail();
   4002      1.1  christos                 op_count = 1;
   4003      1.1  christos                 ffdh_checks = 0;
   4004      1.1  christos                 break;
   4005      1.1  christos             }
   4006      1.1  christos 
   4007      1.1  christos             EVP_PKEY_CTX_free(ffdh_ctx);
   4008      1.1  christos 
   4009      1.1  christos             /*
   4010      1.1  christos              * check if the derivation works correctly both ways so that
   4011      1.1  christos              * we know if future derive calls will fail, and we can skip
   4012      1.1  christos              * error checking in benchmarked code
   4013      1.1  christos              */
   4014      1.1  christos             ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
   4015      1.1  christos             if (ffdh_ctx == NULL) {
   4016      1.1  christos                 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
   4017      1.1  christos                 dofail();
   4018      1.1  christos                 op_count = 1;
   4019      1.1  christos                 ffdh_checks = 0;
   4020      1.1  christos                 break;
   4021      1.1  christos             }
   4022      1.1  christos             if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
   4023      1.1  christos                 BIO_printf(bio_err, "FFDH derivation context init failure.\n");
   4024      1.1  christos                 dofail();
   4025      1.1  christos                 op_count = 1;
   4026      1.1  christos                 ffdh_checks = 0;
   4027      1.1  christos                 break;
   4028      1.1  christos             }
   4029      1.1  christos             if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
   4030      1.1  christos                 BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
   4031      1.1  christos                 dofail();
   4032      1.1  christos                 op_count = 1;
   4033      1.1  christos                 ffdh_checks = 0;
   4034      1.1  christos                 break;
   4035      1.1  christos             }
   4036      1.1  christos             if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
   4037      1.1  christos                 BIO_printf(bio_err, "Checking size of shared secret failed.\n");
   4038      1.1  christos                 dofail();
   4039      1.1  christos                 op_count = 1;
   4040      1.1  christos                 ffdh_checks = 0;
   4041      1.1  christos                 break;
   4042      1.1  christos             }
   4043      1.1  christos             if (secret_size > MAX_FFDH_SIZE) {
   4044      1.1  christos                 BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
   4045      1.1  christos                 op_count = 1;
   4046      1.1  christos                 ffdh_checks = 0;
   4047      1.1  christos                 break;
   4048      1.1  christos             }
   4049      1.1  christos             if (EVP_PKEY_derive(ffdh_ctx,
   4050  1.1.1.2  christos                     loopargs[i].secret_ff_a,
   4051  1.1.1.2  christos                     &secret_size)
   4052  1.1.1.2  christos                 <= 0) {
   4053      1.1  christos                 BIO_printf(bio_err, "Shared secret derive failure.\n");
   4054      1.1  christos                 dofail();
   4055      1.1  christos                 op_count = 1;
   4056      1.1  christos                 ffdh_checks = 0;
   4057      1.1  christos                 break;
   4058      1.1  christos             }
   4059      1.1  christos             /* Now check from side B */
   4060      1.1  christos             test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
   4061      1.1  christos             if (!test_ctx) {
   4062      1.1  christos                 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
   4063      1.1  christos                 dofail();
   4064      1.1  christos                 op_count = 1;
   4065      1.1  christos                 ffdh_checks = 0;
   4066      1.1  christos                 break;
   4067      1.1  christos             }
   4068  1.1.1.2  christos             if (EVP_PKEY_derive_init(test_ctx) <= 0 || EVP_PKEY_derive_set_peer(test_ctx, pkey_A) <= 0 || EVP_PKEY_derive(test_ctx, NULL, &test_out) <= 0 || EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) <= 0 || test_out != secret_size) {
   4069      1.1  christos                 BIO_printf(bio_err, "FFDH computation failure.\n");
   4070      1.1  christos                 op_count = 1;
   4071      1.1  christos                 ffdh_checks = 0;
   4072      1.1  christos                 break;
   4073      1.1  christos             }
   4074      1.1  christos 
   4075      1.1  christos             /* compare the computed secrets */
   4076      1.1  christos             if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
   4077  1.1.1.2  christos                     loopargs[i].secret_ff_b, secret_size)) {
   4078      1.1  christos                 BIO_printf(bio_err, "FFDH computations don't match.\n");
   4079      1.1  christos                 dofail();
   4080      1.1  christos                 op_count = 1;
   4081      1.1  christos                 ffdh_checks = 0;
   4082      1.1  christos                 break;
   4083      1.1  christos             }
   4084      1.1  christos 
   4085      1.1  christos             loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
   4086      1.1  christos 
   4087      1.1  christos             EVP_PKEY_free(pkey_A);
   4088      1.1  christos             pkey_A = NULL;
   4089      1.1  christos             EVP_PKEY_free(pkey_B);
   4090      1.1  christos             pkey_B = NULL;
   4091      1.1  christos             EVP_PKEY_CTX_free(test_ctx);
   4092      1.1  christos             test_ctx = NULL;
   4093      1.1  christos         }
   4094      1.1  christos         if (ffdh_checks != 0) {
   4095      1.1  christos             pkey_print_message("", "ffdh",
   4096  1.1.1.2  christos                 ffdh_params[testnum].bits, seconds.ffdh);
   4097      1.1  christos             Time_F(START);
   4098  1.1.1.2  christos             count = run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
   4099      1.1  christos             d = Time_F(STOP);
   4100      1.1  christos             BIO_printf(bio_err,
   4101  1.1.1.2  christos                 mr ? "+R14:%ld:%d:%.2f\n" : "%ld %u-bits FFDH ops in %.2fs\n", count,
   4102  1.1.1.2  christos                 ffdh_params[testnum].bits, d);
   4103      1.1  christos             ffdh_results[testnum][0] = (double)count / d;
   4104      1.1  christos             op_count = count;
   4105      1.1  christos         }
   4106      1.1  christos         if (op_count <= 1) {
   4107      1.1  christos             /* if longer than 10s, don't do any more */
   4108      1.1  christos             stop_it(ffdh_doit, testnum);
   4109      1.1  christos         }
   4110      1.1  christos     }
   4111  1.1.1.2  christos #endif /* OPENSSL_NO_DH */
   4112      1.1  christos 
   4113      1.1  christos     for (testnum = 0; testnum < kems_algs_len; testnum++) {
   4114      1.1  christos         int kem_checks = 1;
   4115      1.1  christos         const char *kem_name = kems_algname[testnum];
   4116      1.1  christos 
   4117      1.1  christos         if (!kems_doit[testnum] || !do_kems)
   4118      1.1  christos             continue;
   4119      1.1  christos 
   4120      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   4121      1.1  christos             EVP_PKEY *pkey = NULL;
   4122      1.1  christos             EVP_PKEY_CTX *kem_gen_ctx = NULL;
   4123      1.1  christos             EVP_PKEY_CTX *kem_encaps_ctx = NULL;
   4124      1.1  christos             EVP_PKEY_CTX *kem_decaps_ctx = NULL;
   4125      1.1  christos             size_t send_secret_len, out_len;
   4126      1.1  christos             size_t rcv_secret_len;
   4127      1.1  christos             unsigned char *out = NULL, *send_secret = NULL, *rcv_secret;
   4128      1.1  christos             unsigned int bits;
   4129      1.1  christos             char *name;
   4130      1.1  christos             char sfx[MAX_ALGNAME_SUFFIX];
   4131      1.1  christos             OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
   4132      1.1  christos             int use_params = 0;
   4133  1.1.1.2  christos             enum kem_type_t { KEM_RSA = 1,
   4134  1.1.1.2  christos                 KEM_EC,
   4135  1.1.1.2  christos                 KEM_X25519,
   4136  1.1.1.2  christos                 KEM_X448 } kem_type;
   4137      1.1  christos 
   4138      1.1  christos             /* no string after rsa<bitcnt> permitted: */
   4139      1.1  christos             if (strlen(kem_name) < MAX_ALGNAME_SUFFIX + 4 /* rsa+digit */
   4140      1.1  christos                 && sscanf(kem_name, "rsa%u%s", &bits, sfx) == 1)
   4141      1.1  christos                 kem_type = KEM_RSA;
   4142      1.1  christos             else if (strncmp(kem_name, "EC", 2) == 0)
   4143      1.1  christos                 kem_type = KEM_EC;
   4144      1.1  christos             else if (strcmp(kem_name, "X25519") == 0)
   4145      1.1  christos                 kem_type = KEM_X25519;
   4146      1.1  christos             else if (strcmp(kem_name, "X448") == 0)
   4147      1.1  christos                 kem_type = KEM_X448;
   4148  1.1.1.2  christos             else
   4149  1.1.1.2  christos                 kem_type = 0;
   4150      1.1  christos 
   4151      1.1  christos             if (ERR_peek_error()) {
   4152      1.1  christos                 BIO_printf(bio_err,
   4153  1.1.1.2  christos                     "WARNING: the error queue contains previous unhandled errors.\n");
   4154      1.1  christos                 dofail();
   4155      1.1  christos             }
   4156      1.1  christos 
   4157      1.1  christos             if (kem_type == KEM_RSA) {
   4158      1.1  christos                 params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_RSA_BITS,
   4159  1.1.1.2  christos                     &bits);
   4160      1.1  christos                 use_params = 1;
   4161      1.1  christos             } else if (kem_type == KEM_EC) {
   4162      1.1  christos                 name = (char *)(kem_name + 2);
   4163      1.1  christos                 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
   4164  1.1.1.2  christos                     name, 0);
   4165      1.1  christos                 use_params = 1;
   4166      1.1  christos             }
   4167      1.1  christos 
   4168      1.1  christos             kem_gen_ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
   4169  1.1.1.2  christos                 (kem_type == KEM_RSA) ? "RSA" : (kem_type == KEM_EC) ? "EC"
   4170  1.1.1.2  christos                                                                      : kem_name,
   4171  1.1.1.2  christos                 app_get0_propq());
   4172      1.1  christos 
   4173      1.1  christos             if ((!kem_gen_ctx || EVP_PKEY_keygen_init(kem_gen_ctx) <= 0)
   4174      1.1  christos                 || (use_params
   4175      1.1  christos                     && EVP_PKEY_CTX_set_params(kem_gen_ctx, params) <= 0)) {
   4176      1.1  christos                 BIO_printf(bio_err, "Error initializing keygen ctx for %s.\n",
   4177  1.1.1.2  christos                     kem_name);
   4178      1.1  christos                 goto kem_err_break;
   4179      1.1  christos             }
   4180      1.1  christos             if (EVP_PKEY_keygen(kem_gen_ctx, &pkey) <= 0) {
   4181      1.1  christos                 BIO_printf(bio_err, "Error while generating KEM EVP_PKEY.\n");
   4182      1.1  christos                 goto kem_err_break;
   4183      1.1  christos             }
   4184      1.1  christos             /* Now prepare encaps data structs */
   4185      1.1  christos             kem_encaps_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
   4186  1.1.1.2  christos                 pkey,
   4187  1.1.1.2  christos                 app_get0_propq());
   4188      1.1  christos             if (kem_encaps_ctx == NULL
   4189      1.1  christos                 || EVP_PKEY_encapsulate_init(kem_encaps_ctx, NULL) <= 0
   4190      1.1  christos                 || (kem_type == KEM_RSA
   4191      1.1  christos                     && EVP_PKEY_CTX_set_kem_op(kem_encaps_ctx, "RSASVE") <= 0)
   4192      1.1  christos                 || ((kem_type == KEM_EC
   4193  1.1.1.2  christos                         || kem_type == KEM_X25519
   4194  1.1.1.2  christos                         || kem_type == KEM_X448)
   4195  1.1.1.2  christos                     && EVP_PKEY_CTX_set_kem_op(kem_encaps_ctx, "DHKEM") <= 0)
   4196      1.1  christos                 || EVP_PKEY_encapsulate(kem_encaps_ctx, NULL, &out_len,
   4197  1.1.1.2  christos                        NULL, &send_secret_len)
   4198  1.1.1.2  christos                     <= 0) {
   4199      1.1  christos                 BIO_printf(bio_err,
   4200  1.1.1.2  christos                     "Error while initializing encaps data structs for %s.\n",
   4201  1.1.1.2  christos                     kem_name);
   4202      1.1  christos                 goto kem_err_break;
   4203      1.1  christos             }
   4204      1.1  christos             out = app_malloc(out_len, "encaps result");
   4205      1.1  christos             send_secret = app_malloc(send_secret_len, "encaps secret");
   4206      1.1  christos             if (out == NULL || send_secret == NULL) {
   4207      1.1  christos                 BIO_printf(bio_err, "MemAlloc error in encaps for %s.\n", kem_name);
   4208      1.1  christos                 goto kem_err_break;
   4209      1.1  christos             }
   4210      1.1  christos             if (EVP_PKEY_encapsulate(kem_encaps_ctx, out, &out_len,
   4211  1.1.1.2  christos                     send_secret, &send_secret_len)
   4212  1.1.1.2  christos                 <= 0) {
   4213      1.1  christos                 BIO_printf(bio_err, "Encaps error for %s.\n", kem_name);
   4214      1.1  christos                 goto kem_err_break;
   4215      1.1  christos             }
   4216      1.1  christos             /* Now prepare decaps data structs */
   4217      1.1  christos             kem_decaps_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
   4218  1.1.1.2  christos                 pkey,
   4219  1.1.1.2  christos                 app_get0_propq());
   4220      1.1  christos             if (kem_decaps_ctx == NULL
   4221      1.1  christos                 || EVP_PKEY_decapsulate_init(kem_decaps_ctx, NULL) <= 0
   4222      1.1  christos                 || (kem_type == KEM_RSA
   4223  1.1.1.2  christos                     && EVP_PKEY_CTX_set_kem_op(kem_decaps_ctx, "RSASVE") <= 0)
   4224      1.1  christos                 || ((kem_type == KEM_EC
   4225  1.1.1.2  christos                         || kem_type == KEM_X25519
   4226  1.1.1.2  christos                         || kem_type == KEM_X448)
   4227  1.1.1.2  christos                     && EVP_PKEY_CTX_set_kem_op(kem_decaps_ctx, "DHKEM") <= 0)
   4228      1.1  christos                 || EVP_PKEY_decapsulate(kem_decaps_ctx, NULL, &rcv_secret_len,
   4229  1.1.1.2  christos                        out, out_len)
   4230  1.1.1.2  christos                     <= 0) {
   4231      1.1  christos                 BIO_printf(bio_err,
   4232  1.1.1.2  christos                     "Error while initializing decaps data structs for %s.\n",
   4233  1.1.1.2  christos                     kem_name);
   4234      1.1  christos                 goto kem_err_break;
   4235      1.1  christos             }
   4236      1.1  christos             rcv_secret = app_malloc(rcv_secret_len, "KEM decaps secret");
   4237      1.1  christos             if (rcv_secret == NULL) {
   4238      1.1  christos                 BIO_printf(bio_err, "MemAlloc failure in decaps for %s.\n",
   4239  1.1.1.2  christos                     kem_name);
   4240      1.1  christos                 goto kem_err_break;
   4241      1.1  christos             }
   4242      1.1  christos             if (EVP_PKEY_decapsulate(kem_decaps_ctx, rcv_secret,
   4243  1.1.1.2  christos                     &rcv_secret_len, out, out_len)
   4244  1.1.1.2  christos                     <= 0
   4245      1.1  christos                 || rcv_secret_len != send_secret_len
   4246      1.1  christos                 || memcmp(send_secret, rcv_secret, send_secret_len)) {
   4247      1.1  christos                 BIO_printf(bio_err, "Decaps error for %s.\n", kem_name);
   4248      1.1  christos                 goto kem_err_break;
   4249      1.1  christos             }
   4250      1.1  christos             loopargs[i].kem_gen_ctx[testnum] = kem_gen_ctx;
   4251      1.1  christos             loopargs[i].kem_encaps_ctx[testnum] = kem_encaps_ctx;
   4252      1.1  christos             loopargs[i].kem_decaps_ctx[testnum] = kem_decaps_ctx;
   4253      1.1  christos             loopargs[i].kem_out_len[testnum] = out_len;
   4254      1.1  christos             loopargs[i].kem_secret_len[testnum] = send_secret_len;
   4255      1.1  christos             loopargs[i].kem_out[testnum] = out;
   4256      1.1  christos             loopargs[i].kem_send_secret[testnum] = send_secret;
   4257      1.1  christos             loopargs[i].kem_rcv_secret[testnum] = rcv_secret;
   4258      1.1  christos             EVP_PKEY_free(pkey);
   4259      1.1  christos             pkey = NULL;
   4260      1.1  christos             continue;
   4261      1.1  christos 
   4262      1.1  christos         kem_err_break:
   4263      1.1  christos             dofail();
   4264      1.1  christos             EVP_PKEY_free(pkey);
   4265      1.1  christos             op_count = 1;
   4266      1.1  christos             kem_checks = 0;
   4267      1.1  christos             break;
   4268      1.1  christos         }
   4269      1.1  christos         if (kem_checks != 0) {
   4270      1.1  christos             kskey_print_message(kem_name, "keygen", seconds.kem);
   4271      1.1  christos             Time_F(START);
   4272  1.1.1.2  christos             count = run_benchmark(async_jobs, KEM_keygen_loop, loopargs);
   4273      1.1  christos             d = Time_F(STOP);
   4274      1.1  christos             BIO_printf(bio_err,
   4275  1.1.1.2  christos                 mr ? "+R15:%ld:%s:%.2f\n" : "%ld %s KEM keygen ops in %.2fs\n", count,
   4276  1.1.1.2  christos                 kem_name, d);
   4277      1.1  christos             kems_results[testnum][0] = (double)count / d;
   4278      1.1  christos             op_count = count;
   4279      1.1  christos             kskey_print_message(kem_name, "encaps", seconds.kem);
   4280      1.1  christos             Time_F(START);
   4281  1.1.1.2  christos             count = run_benchmark(async_jobs, KEM_encaps_loop, loopargs);
   4282      1.1  christos             d = Time_F(STOP);
   4283      1.1  christos             BIO_printf(bio_err,
   4284  1.1.1.2  christos                 mr ? "+R16:%ld:%s:%.2f\n" : "%ld %s KEM encaps ops in %.2fs\n", count,
   4285  1.1.1.2  christos                 kem_name, d);
   4286      1.1  christos             kems_results[testnum][1] = (double)count / d;
   4287      1.1  christos             op_count = count;
   4288      1.1  christos             kskey_print_message(kem_name, "decaps", seconds.kem);
   4289      1.1  christos             Time_F(START);
   4290  1.1.1.2  christos             count = run_benchmark(async_jobs, KEM_decaps_loop, loopargs);
   4291      1.1  christos             d = Time_F(STOP);
   4292      1.1  christos             BIO_printf(bio_err,
   4293  1.1.1.2  christos                 mr ? "+R17:%ld:%s:%.2f\n" : "%ld %s KEM decaps ops in %.2fs\n", count,
   4294  1.1.1.2  christos                 kem_name, d);
   4295      1.1  christos             kems_results[testnum][2] = (double)count / d;
   4296      1.1  christos             op_count = count;
   4297      1.1  christos         }
   4298      1.1  christos         if (op_count <= 1) {
   4299      1.1  christos             /* if longer than 10s, don't do any more */
   4300      1.1  christos             stop_it(kems_doit, testnum);
   4301      1.1  christos         }
   4302      1.1  christos     }
   4303      1.1  christos 
   4304      1.1  christos     for (testnum = 0; testnum < sigs_algs_len; testnum++) {
   4305      1.1  christos         int sig_checks = 1;
   4306      1.1  christos         const char *sig_name = sigs_algname[testnum];
   4307      1.1  christos 
   4308      1.1  christos         if (!sigs_doit[testnum] || !do_sigs)
   4309      1.1  christos             continue;
   4310      1.1  christos 
   4311      1.1  christos         for (i = 0; i < loopargs_len; i++) {
   4312      1.1  christos             EVP_PKEY *pkey = NULL;
   4313      1.1  christos             EVP_PKEY_CTX *ctx_params = NULL;
   4314  1.1.1.2  christos             EVP_PKEY *pkey_params = NULL;
   4315      1.1  christos             EVP_PKEY_CTX *sig_gen_ctx = NULL;
   4316      1.1  christos             EVP_PKEY_CTX *sig_sign_ctx = NULL;
   4317      1.1  christos             EVP_PKEY_CTX *sig_verify_ctx = NULL;
   4318  1.1.1.2  christos             EVP_SIGNATURE *alg = NULL;
   4319      1.1  christos             unsigned char md[SHA256_DIGEST_LENGTH];
   4320      1.1  christos             unsigned char *sig;
   4321      1.1  christos             char sfx[MAX_ALGNAME_SUFFIX];
   4322      1.1  christos             size_t md_len = SHA256_DIGEST_LENGTH;
   4323      1.1  christos             size_t max_sig_len, sig_len;
   4324      1.1  christos             unsigned int bits;
   4325      1.1  christos             OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
   4326      1.1  christos             int use_params = 0;
   4327      1.1  christos 
   4328      1.1  christos             /* only sign little data to avoid measuring digest performance */
   4329      1.1  christos             memset(md, 0, SHA256_DIGEST_LENGTH);
   4330      1.1  christos 
   4331      1.1  christos             if (ERR_peek_error()) {
   4332      1.1  christos                 BIO_printf(bio_err,
   4333  1.1.1.2  christos                     "WARNING: the error queue contains previous unhandled errors.\n");
   4334      1.1  christos                 dofail();
   4335      1.1  christos             }
   4336      1.1  christos 
   4337      1.1  christos             /* no string after rsa<bitcnt> permitted: */
   4338      1.1  christos             if (strlen(sig_name) < MAX_ALGNAME_SUFFIX + 4 /* rsa+digit */
   4339      1.1  christos                 && sscanf(sig_name, "rsa%u%s", &bits, sfx) == 1) {
   4340      1.1  christos                 params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_RSA_BITS,
   4341  1.1.1.2  christos                     &bits);
   4342      1.1  christos                 use_params = 1;
   4343      1.1  christos             }
   4344      1.1  christos 
   4345      1.1  christos             if (strncmp(sig_name, "dsa", 3) == 0) {
   4346      1.1  christos                 ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL);
   4347      1.1  christos                 if (ctx_params == NULL
   4348      1.1  christos                     || EVP_PKEY_paramgen_init(ctx_params) <= 0
   4349      1.1  christos                     || EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx_params,
   4350  1.1.1.2  christos                            atoi(sig_name + 3))
   4351  1.1.1.2  christos                         <= 0
   4352      1.1  christos                     || EVP_PKEY_paramgen(ctx_params, &pkey_params) <= 0
   4353      1.1  christos                     || (sig_gen_ctx = EVP_PKEY_CTX_new(pkey_params, NULL)) == NULL
   4354      1.1  christos                     || EVP_PKEY_keygen_init(sig_gen_ctx) <= 0) {
   4355      1.1  christos                     BIO_printf(bio_err,
   4356  1.1.1.2  christos                         "Error initializing classic keygen ctx for %s.\n",
   4357  1.1.1.2  christos                         sig_name);
   4358      1.1  christos                     goto sig_err_break;
   4359      1.1  christos                 }
   4360      1.1  christos             }
   4361      1.1  christos 
   4362      1.1  christos             if (sig_gen_ctx == NULL)
   4363      1.1  christos                 sig_gen_ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
   4364  1.1.1.2  christos                     use_params == 1 ? "RSA" : sig_name,
   4365  1.1.1.2  christos                     app_get0_propq());
   4366      1.1  christos 
   4367      1.1  christos             if (!sig_gen_ctx || EVP_PKEY_keygen_init(sig_gen_ctx) <= 0
   4368  1.1.1.2  christos                 || (use_params && EVP_PKEY_CTX_set_params(sig_gen_ctx, params) <= 0)) {
   4369      1.1  christos                 BIO_printf(bio_err, "Error initializing keygen ctx for %s.\n",
   4370  1.1.1.2  christos                     sig_name);
   4371      1.1  christos                 goto sig_err_break;
   4372      1.1  christos             }
   4373      1.1  christos             if (EVP_PKEY_keygen(sig_gen_ctx, &pkey) <= 0) {
   4374      1.1  christos                 BIO_printf(bio_err,
   4375  1.1.1.2  christos                     "Error while generating signature EVP_PKEY for %s.\n",
   4376  1.1.1.2  christos                     sig_name);
   4377      1.1  christos                 goto sig_err_break;
   4378      1.1  christos             }
   4379  1.1.1.2  christos 
   4380  1.1.1.2  christos             /*
   4381  1.1.1.2  christos              * Try explicitly fetching the signature algorithm implementation to
   4382  1.1.1.2  christos              * use in case the algorithm does not support EVP_PKEY_sign_init
   4383  1.1.1.2  christos              */
   4384  1.1.1.2  christos             ERR_set_mark();
   4385  1.1.1.2  christos             alg = EVP_SIGNATURE_fetch(app_get0_libctx(), sig_name, app_get0_propq());
   4386  1.1.1.2  christos             ERR_pop_to_mark();
   4387  1.1.1.2  christos 
   4388      1.1  christos             /* Now prepare signature data structs */
   4389      1.1  christos             sig_sign_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
   4390  1.1.1.2  christos                 pkey,
   4391  1.1.1.2  christos                 app_get0_propq());
   4392  1.1.1.2  christos             if (sig_sign_ctx == NULL) {
   4393  1.1.1.2  christos                 BIO_printf(bio_err,
   4394  1.1.1.2  christos                     "Error while initializing signing ctx for %s.\n",
   4395  1.1.1.2  christos                     sig_name);
   4396  1.1.1.2  christos                 goto sig_err_break;
   4397  1.1.1.2  christos             }
   4398  1.1.1.2  christos             ERR_set_mark();
   4399  1.1.1.2  christos             if (EVP_PKEY_sign_init(sig_sign_ctx) <= 0
   4400  1.1.1.2  christos                 && (alg == NULL
   4401  1.1.1.2  christos                     || EVP_PKEY_sign_message_init(sig_sign_ctx, alg, NULL) <= 0)) {
   4402  1.1.1.2  christos                 ERR_clear_last_mark();
   4403  1.1.1.2  christos                 BIO_printf(bio_err,
   4404  1.1.1.2  christos                     "Error while initializing signing data structs for %s.\n",
   4405  1.1.1.2  christos                     sig_name);
   4406  1.1.1.2  christos                 goto sig_err_break;
   4407  1.1.1.2  christos             }
   4408  1.1.1.2  christos             ERR_pop_to_mark();
   4409  1.1.1.2  christos             if (use_params == 1 && EVP_PKEY_CTX_set_rsa_padding(sig_sign_ctx, RSA_PKCS1_PADDING) <= 0) {
   4410  1.1.1.2  christos                 BIO_printf(bio_err,
   4411  1.1.1.2  christos                     "Error while initializing padding for %s.\n",
   4412  1.1.1.2  christos                     sig_name);
   4413  1.1.1.2  christos                 goto sig_err_break;
   4414  1.1.1.2  christos             }
   4415  1.1.1.2  christos             if (EVP_PKEY_sign(sig_sign_ctx, NULL, &max_sig_len, md, md_len) <= 0) {
   4416  1.1.1.2  christos                 BIO_printf(bio_err,
   4417  1.1.1.2  christos                     "Error while obtaining signature buffer length for %s.\n",
   4418  1.1.1.2  christos                     sig_name);
   4419  1.1.1.2  christos                 goto sig_err_break;
   4420      1.1  christos             }
   4421      1.1  christos             sig = app_malloc(sig_len = max_sig_len, "signature buffer");
   4422      1.1  christos             if (sig == NULL) {
   4423      1.1  christos                 BIO_printf(bio_err, "MemAlloc error in sign for %s.\n", sig_name);
   4424      1.1  christos                 goto sig_err_break;
   4425      1.1  christos             }
   4426      1.1  christos             if (EVP_PKEY_sign(sig_sign_ctx, sig, &sig_len, md, md_len) <= 0) {
   4427      1.1  christos                 BIO_printf(bio_err, "Signing error for %s.\n", sig_name);
   4428      1.1  christos                 goto sig_err_break;
   4429      1.1  christos             }
   4430      1.1  christos             /* Now prepare verify data structs */
   4431      1.1  christos             memset(md, 0, SHA256_DIGEST_LENGTH);
   4432      1.1  christos             sig_verify_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
   4433  1.1.1.2  christos                 pkey,
   4434  1.1.1.2  christos                 app_get0_propq());
   4435  1.1.1.2  christos             if (sig_verify_ctx == NULL) {
   4436  1.1.1.2  christos                 BIO_printf(bio_err,
   4437  1.1.1.2  christos                     "Error while initializing verify ctx for %s.\n",
   4438  1.1.1.2  christos                     sig_name);
   4439  1.1.1.2  christos                 goto sig_err_break;
   4440  1.1.1.2  christos             }
   4441  1.1.1.2  christos             ERR_set_mark();
   4442  1.1.1.2  christos             if (EVP_PKEY_verify_init(sig_verify_ctx) <= 0
   4443  1.1.1.2  christos                 && (alg == NULL
   4444  1.1.1.2  christos                     || EVP_PKEY_verify_message_init(sig_verify_ctx, alg, NULL) <= 0)) {
   4445  1.1.1.2  christos                 ERR_clear_last_mark();
   4446      1.1  christos                 BIO_printf(bio_err,
   4447  1.1.1.2  christos                     "Error while initializing verify data structs for %s.\n",
   4448  1.1.1.2  christos                     sig_name);
   4449      1.1  christos                 goto sig_err_break;
   4450      1.1  christos             }
   4451  1.1.1.2  christos             ERR_pop_to_mark();
   4452      1.1  christos             if (EVP_PKEY_verify(sig_verify_ctx, sig, sig_len, md, md_len) <= 0) {
   4453      1.1  christos                 BIO_printf(bio_err, "Verify error for %s.\n", sig_name);
   4454      1.1  christos                 goto sig_err_break;
   4455      1.1  christos             }
   4456      1.1  christos             if (EVP_PKEY_verify(sig_verify_ctx, sig, sig_len, md, md_len) <= 0) {
   4457      1.1  christos                 BIO_printf(bio_err, "Verify 2 error for %s.\n", sig_name);
   4458      1.1  christos                 goto sig_err_break;
   4459      1.1  christos             }
   4460      1.1  christos             loopargs[i].sig_gen_ctx[testnum] = sig_gen_ctx;
   4461      1.1  christos             loopargs[i].sig_sign_ctx[testnum] = sig_sign_ctx;
   4462      1.1  christos             loopargs[i].sig_verify_ctx[testnum] = sig_verify_ctx;
   4463      1.1  christos             loopargs[i].sig_max_sig_len[testnum] = max_sig_len;
   4464      1.1  christos             loopargs[i].sig_act_sig_len[testnum] = sig_len;
   4465      1.1  christos             loopargs[i].sig_sig[testnum] = sig;
   4466      1.1  christos             EVP_PKEY_free(pkey);
   4467  1.1.1.2  christos             EVP_SIGNATURE_free(alg);
   4468      1.1  christos             pkey = NULL;
   4469      1.1  christos             continue;
   4470      1.1  christos 
   4471      1.1  christos         sig_err_break:
   4472      1.1  christos             dofail();
   4473      1.1  christos             EVP_PKEY_free(pkey);
   4474  1.1.1.2  christos             EVP_SIGNATURE_free(alg);
   4475      1.1  christos             op_count = 1;
   4476      1.1  christos             sig_checks = 0;
   4477      1.1  christos             break;
   4478      1.1  christos         }
   4479      1.1  christos 
   4480      1.1  christos         if (sig_checks != 0) {
   4481      1.1  christos             kskey_print_message(sig_name, "keygen", seconds.sig);
   4482      1.1  christos             Time_F(START);
   4483      1.1  christos             count = run_benchmark(async_jobs, SIG_keygen_loop, loopargs);
   4484      1.1  christos             d = Time_F(STOP);
   4485      1.1  christos             BIO_printf(bio_err,
   4486  1.1.1.2  christos                 mr ? "+R18:%ld:%s:%.2f\n" : "%ld %s signature keygen ops in %.2fs\n", count,
   4487  1.1.1.2  christos                 sig_name, d);
   4488      1.1  christos             sigs_results[testnum][0] = (double)count / d;
   4489      1.1  christos             op_count = count;
   4490      1.1  christos             kskey_print_message(sig_name, "signs", seconds.sig);
   4491      1.1  christos             Time_F(START);
   4492  1.1.1.2  christos             count = run_benchmark(async_jobs, SIG_sign_loop, loopargs);
   4493      1.1  christos             d = Time_F(STOP);
   4494      1.1  christos             BIO_printf(bio_err,
   4495  1.1.1.2  christos                 mr ? "+R19:%ld:%s:%.2f\n" : "%ld %s signature sign ops in %.2fs\n", count,
   4496  1.1.1.2  christos                 sig_name, d);
   4497      1.1  christos             sigs_results[testnum][1] = (double)count / d;
   4498      1.1  christos             op_count = count;
   4499      1.1  christos 
   4500      1.1  christos             kskey_print_message(sig_name, "verify", seconds.sig);
   4501      1.1  christos             Time_F(START);
   4502  1.1.1.2  christos             count = run_benchmark(async_jobs, SIG_verify_loop, loopargs);
   4503      1.1  christos             d = Time_F(STOP);
   4504      1.1  christos             BIO_printf(bio_err,
   4505  1.1.1.2  christos                 mr ? "+R20:%ld:%s:%.2f\n" : "%ld %s signature verify ops in %.2fs\n", count,
   4506  1.1.1.2  christos                 sig_name, d);
   4507      1.1  christos             sigs_results[testnum][2] = (double)count / d;
   4508      1.1  christos             op_count = count;
   4509      1.1  christos         }
   4510      1.1  christos         if (op_count <= 1)
   4511      1.1  christos             stop_it(sigs_doit, testnum);
   4512      1.1  christos     }
   4513      1.1  christos 
   4514      1.1  christos #ifndef NO_FORK
   4515  1.1.1.2  christos show_res:
   4516      1.1  christos #endif
   4517      1.1  christos     if (!mr) {
   4518      1.1  christos         printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
   4519      1.1  christos         printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
   4520      1.1  christos         printf("options: %s\n", BN_options());
   4521      1.1  christos         printf("%s\n", OpenSSL_version(OPENSSL_CFLAGS));
   4522      1.1  christos         printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
   4523      1.1  christos     }
   4524      1.1  christos 
   4525      1.1  christos     if (pr_header) {
   4526      1.1  christos         if (mr) {
   4527      1.1  christos             printf("+H");
   4528      1.1  christos         } else {
   4529      1.1  christos             printf("The 'numbers' are in 1000s of bytes per second processed.\n");
   4530      1.1  christos             printf("type        ");
   4531      1.1  christos         }
   4532      1.1  christos         for (testnum = 0; testnum < size_num; testnum++)
   4533      1.1  christos             printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
   4534      1.1  christos         printf("\n");
   4535      1.1  christos     }
   4536      1.1  christos 
   4537      1.1  christos     for (k = 0; k < ALGOR_NUM; k++) {
   4538      1.1  christos         const char *alg_name = names[k];
   4539      1.1  christos 
   4540      1.1  christos         if (!doit[k])
   4541      1.1  christos             continue;
   4542      1.1  christos 
   4543      1.1  christos         if (k == D_EVP) {
   4544      1.1  christos             if (evp_cipher == NULL)
   4545      1.1  christos                 alg_name = evp_md_name;
   4546      1.1  christos             else if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
   4547      1.1  christos                 app_bail_out("failed to get name of cipher '%s'\n", evp_cipher);
   4548      1.1  christos         }
   4549      1.1  christos 
   4550      1.1  christos         if (mr)
   4551      1.1  christos             printf("+F:%u:%s", k, alg_name);
   4552      1.1  christos         else
   4553      1.1  christos             printf("%-13s", alg_name);
   4554      1.1  christos         for (testnum = 0; testnum < size_num; testnum++) {
   4555      1.1  christos             if (results[k][testnum] > 10000 && !mr)
   4556      1.1  christos                 printf(" %11.2fk", results[k][testnum] / 1e3);
   4557      1.1  christos             else
   4558      1.1  christos                 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
   4559      1.1  christos         }
   4560      1.1  christos         printf("\n");
   4561      1.1  christos     }
   4562      1.1  christos     testnum = 1;
   4563      1.1  christos     for (k = 0; k < RSA_NUM; k++) {
   4564      1.1  christos         if (!rsa_doit[k])
   4565      1.1  christos             continue;
   4566      1.1  christos         if (testnum && !mr) {
   4567      1.1  christos             printf("%19ssign    verify    encrypt   decrypt   sign/s verify/s  encr./s  decr./s\n", " ");
   4568      1.1  christos             testnum = 0;
   4569      1.1  christos         }
   4570      1.1  christos         if (mr)
   4571      1.1  christos             printf("+F2:%u:%u:%f:%f:%f:%f\n",
   4572  1.1.1.2  christos                 k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1],
   4573  1.1.1.2  christos                 rsa_results[k][2], rsa_results[k][3]);
   4574      1.1  christos         else
   4575      1.1  christos             printf("rsa %5u bits %8.6fs %8.6fs %8.6fs %8.6fs %8.1f %8.1f %8.1f %8.1f\n",
   4576  1.1.1.2  christos                 rsa_keys[k].bits, 1.0 / rsa_results[k][0],
   4577  1.1.1.2  christos                 1.0 / rsa_results[k][1], 1.0 / rsa_results[k][2],
   4578  1.1.1.2  christos                 1.0 / rsa_results[k][3],
   4579  1.1.1.2  christos                 rsa_results[k][0], rsa_results[k][1],
   4580  1.1.1.2  christos                 rsa_results[k][2], rsa_results[k][3]);
   4581      1.1  christos     }
   4582      1.1  christos     testnum = 1;
   4583      1.1  christos #ifndef OPENSSL_NO_DSA
   4584      1.1  christos     for (k = 0; k < DSA_NUM; k++) {
   4585      1.1  christos         if (!dsa_doit[k])
   4586      1.1  christos             continue;
   4587      1.1  christos         if (testnum && !mr) {
   4588      1.1  christos             printf("%18ssign    verify    sign/s verify/s\n", " ");
   4589      1.1  christos             testnum = 0;
   4590      1.1  christos         }
   4591      1.1  christos         if (mr)
   4592      1.1  christos             printf("+F3:%u:%u:%f:%f\n",
   4593  1.1.1.2  christos                 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
   4594      1.1  christos         else
   4595      1.1  christos             printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
   4596  1.1.1.2  christos                 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
   4597  1.1.1.2  christos                 dsa_results[k][0], dsa_results[k][1]);
   4598      1.1  christos     }
   4599      1.1  christos #endif /* OPENSSL_NO_DSA */
   4600      1.1  christos     testnum = 1;
   4601      1.1  christos     for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
   4602      1.1  christos         if (!ecdsa_doit[k])
   4603      1.1  christos             continue;
   4604      1.1  christos         if (testnum && !mr) {
   4605      1.1  christos             printf("%30ssign    verify    sign/s verify/s\n", " ");
   4606      1.1  christos             testnum = 0;
   4607      1.1  christos         }
   4608      1.1  christos 
   4609      1.1  christos         if (mr)
   4610      1.1  christos             printf("+F4:%u:%u:%f:%f\n",
   4611  1.1.1.2  christos                 k, ec_curves[k].bits,
   4612  1.1.1.2  christos                 ecdsa_results[k][0], ecdsa_results[k][1]);
   4613      1.1  christos         else
   4614      1.1  christos             printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
   4615  1.1.1.2  christos                 ec_curves[k].bits, ec_curves[k].name,
   4616  1.1.1.2  christos                 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
   4617  1.1.1.2  christos                 ecdsa_results[k][0], ecdsa_results[k][1]);
   4618      1.1  christos     }
   4619      1.1  christos 
   4620      1.1  christos     testnum = 1;
   4621      1.1  christos     for (k = 0; k < EC_NUM; k++) {
   4622      1.1  christos         if (!ecdh_doit[k])
   4623      1.1  christos             continue;
   4624      1.1  christos         if (testnum && !mr) {
   4625      1.1  christos             printf("%30sop      op/s\n", " ");
   4626      1.1  christos             testnum = 0;
   4627      1.1  christos         }
   4628      1.1  christos         if (mr)
   4629      1.1  christos             printf("+F5:%u:%u:%f:%f\n",
   4630  1.1.1.2  christos                 k, ec_curves[k].bits,
   4631  1.1.1.2  christos                 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
   4632      1.1  christos 
   4633      1.1  christos         else
   4634      1.1  christos             printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
   4635  1.1.1.2  christos                 ec_curves[k].bits, ec_curves[k].name,
   4636  1.1.1.2  christos                 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
   4637      1.1  christos     }
   4638      1.1  christos 
   4639      1.1  christos #ifndef OPENSSL_NO_ECX
   4640      1.1  christos     testnum = 1;
   4641      1.1  christos     for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
   4642      1.1  christos         if (!eddsa_doit[k])
   4643      1.1  christos             continue;
   4644      1.1  christos         if (testnum && !mr) {
   4645      1.1  christos             printf("%30ssign    verify    sign/s verify/s\n", " ");
   4646      1.1  christos             testnum = 0;
   4647      1.1  christos         }
   4648      1.1  christos 
   4649      1.1  christos         if (mr)
   4650      1.1  christos             printf("+F6:%u:%u:%s:%f:%f\n",
   4651  1.1.1.2  christos                 k, ed_curves[k].bits, ed_curves[k].name,
   4652  1.1.1.2  christos                 eddsa_results[k][0], eddsa_results[k][1]);
   4653      1.1  christos         else
   4654      1.1  christos             printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
   4655  1.1.1.2  christos                 ed_curves[k].bits, ed_curves[k].name,
   4656  1.1.1.2  christos                 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
   4657  1.1.1.2  christos                 eddsa_results[k][0], eddsa_results[k][1]);
   4658      1.1  christos     }
   4659      1.1  christos #endif /* OPENSSL_NO_ECX */
   4660      1.1  christos 
   4661      1.1  christos #ifndef OPENSSL_NO_SM2
   4662      1.1  christos     testnum = 1;
   4663      1.1  christos     for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
   4664      1.1  christos         if (!sm2_doit[k])
   4665      1.1  christos             continue;
   4666      1.1  christos         if (testnum && !mr) {
   4667      1.1  christos             printf("%30ssign    verify    sign/s verify/s\n", " ");
   4668      1.1  christos             testnum = 0;
   4669      1.1  christos         }
   4670      1.1  christos 
   4671      1.1  christos         if (mr)
   4672      1.1  christos             printf("+F7:%u:%u:%s:%f:%f\n",
   4673  1.1.1.2  christos                 k, sm2_curves[k].bits, sm2_curves[k].name,
   4674  1.1.1.2  christos                 sm2_results[k][0], sm2_results[k][1]);
   4675      1.1  christos         else
   4676      1.1  christos             printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
   4677  1.1.1.2  christos                 sm2_curves[k].bits, sm2_curves[k].name,
   4678  1.1.1.2  christos                 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
   4679  1.1.1.2  christos                 sm2_results[k][0], sm2_results[k][1]);
   4680      1.1  christos     }
   4681      1.1  christos #endif
   4682      1.1  christos #ifndef OPENSSL_NO_DH
   4683      1.1  christos     testnum = 1;
   4684      1.1  christos     for (k = 0; k < FFDH_NUM; k++) {
   4685      1.1  christos         if (!ffdh_doit[k])
   4686      1.1  christos             continue;
   4687      1.1  christos         if (testnum && !mr) {
   4688      1.1  christos             printf("%23sop     op/s\n", " ");
   4689      1.1  christos             testnum = 0;
   4690      1.1  christos         }
   4691      1.1  christos         if (mr)
   4692      1.1  christos             printf("+F8:%u:%u:%f:%f\n",
   4693  1.1.1.2  christos                 k, ffdh_params[k].bits,
   4694  1.1.1.2  christos                 ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
   4695      1.1  christos 
   4696      1.1  christos         else
   4697      1.1  christos             printf("%4u bits ffdh %8.4fs %8.1f\n",
   4698  1.1.1.2  christos                 ffdh_params[k].bits,
   4699  1.1.1.2  christos                 1.0 / ffdh_results[k][0], ffdh_results[k][0]);
   4700      1.1  christos     }
   4701      1.1  christos #endif /* OPENSSL_NO_DH */
   4702      1.1  christos 
   4703      1.1  christos     testnum = 1;
   4704      1.1  christos     for (k = 0; k < kems_algs_len; k++) {
   4705      1.1  christos         const char *kem_name = kems_algname[k];
   4706      1.1  christos 
   4707      1.1  christos         if (!kems_doit[k] || !do_kems)
   4708      1.1  christos             continue;
   4709      1.1  christos         if (testnum && !mr) {
   4710      1.1  christos             printf("%31skeygen    encaps    decaps keygens/s  encaps/s  decaps/s\n", " ");
   4711      1.1  christos             testnum = 0;
   4712      1.1  christos         }
   4713      1.1  christos         if (mr)
   4714      1.1  christos             printf("+F9:%u:%f:%f:%f\n",
   4715  1.1.1.2  christos                 k, kems_results[k][0], kems_results[k][1],
   4716  1.1.1.2  christos                 kems_results[k][2]);
   4717      1.1  christos         else
   4718      1.1  christos             printf("%27s %8.6fs %8.6fs %8.6fs %9.1f %9.1f %9.1f\n", kem_name,
   4719  1.1.1.2  christos                 1.0 / kems_results[k][0],
   4720  1.1.1.2  christos                 1.0 / kems_results[k][1], 1.0 / kems_results[k][2],
   4721  1.1.1.2  christos                 kems_results[k][0], kems_results[k][1], kems_results[k][2]);
   4722      1.1  christos     }
   4723      1.1  christos     ret = 0;
   4724      1.1  christos 
   4725      1.1  christos     testnum = 1;
   4726      1.1  christos     for (k = 0; k < sigs_algs_len; k++) {
   4727      1.1  christos         const char *sig_name = sigs_algname[k];
   4728      1.1  christos 
   4729      1.1  christos         if (!sigs_doit[k] || !do_sigs)
   4730      1.1  christos             continue;
   4731      1.1  christos         if (testnum && !mr) {
   4732      1.1  christos             printf("%31skeygen     signs    verify keygens/s    sign/s  verify/s\n", " ");
   4733      1.1  christos             testnum = 0;
   4734      1.1  christos         }
   4735      1.1  christos         if (mr)
   4736      1.1  christos             printf("+F10:%u:%f:%f:%f\n",
   4737  1.1.1.2  christos                 k, sigs_results[k][0], sigs_results[k][1],
   4738  1.1.1.2  christos                 sigs_results[k][2]);
   4739      1.1  christos         else
   4740      1.1  christos             printf("%27s %8.6fs %8.6fs %8.6fs %9.1f %9.1f %9.1f\n", sig_name,
   4741  1.1.1.2  christos                 1.0 / sigs_results[k][0], 1.0 / sigs_results[k][1],
   4742  1.1.1.2  christos                 1.0 / sigs_results[k][2], sigs_results[k][0],
   4743  1.1.1.2  christos                 sigs_results[k][1], sigs_results[k][2]);
   4744      1.1  christos     }
   4745      1.1  christos     ret = 0;
   4746      1.1  christos 
   4747  1.1.1.2  christos end:
   4748      1.1  christos     if (ret == 0 && testmode)
   4749      1.1  christos         ret = testmoderesult;
   4750      1.1  christos     ERR_print_errors(bio_err);
   4751      1.1  christos     for (i = 0; i < loopargs_len; i++) {
   4752      1.1  christos         OPENSSL_free(loopargs[i].buf_malloc);
   4753      1.1  christos         OPENSSL_free(loopargs[i].buf2_malloc);
   4754      1.1  christos 
   4755      1.1  christos         BN_free(bn);
   4756      1.1  christos         EVP_PKEY_CTX_free(genctx);
   4757      1.1  christos         for (k = 0; k < RSA_NUM; k++) {
   4758      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].rsa_sign_ctx[k]);
   4759      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].rsa_verify_ctx[k]);
   4760      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].rsa_encrypt_ctx[k]);
   4761      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].rsa_decrypt_ctx[k]);
   4762      1.1  christos         }
   4763      1.1  christos #ifndef OPENSSL_NO_DH
   4764      1.1  christos         OPENSSL_free(loopargs[i].secret_ff_a);
   4765      1.1  christos         OPENSSL_free(loopargs[i].secret_ff_b);
   4766      1.1  christos         for (k = 0; k < FFDH_NUM; k++)
   4767      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
   4768      1.1  christos #endif
   4769      1.1  christos #ifndef OPENSSL_NO_DSA
   4770      1.1  christos         for (k = 0; k < DSA_NUM; k++) {
   4771      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].dsa_sign_ctx[k]);
   4772      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].dsa_verify_ctx[k]);
   4773      1.1  christos         }
   4774      1.1  christos #endif
   4775      1.1  christos         for (k = 0; k < ECDSA_NUM; k++) {
   4776      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].ecdsa_sign_ctx[k]);
   4777      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].ecdsa_verify_ctx[k]);
   4778      1.1  christos         }
   4779      1.1  christos         for (k = 0; k < EC_NUM; k++)
   4780      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
   4781      1.1  christos #ifndef OPENSSL_NO_ECX
   4782      1.1  christos         for (k = 0; k < EdDSA_NUM; k++) {
   4783      1.1  christos             EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
   4784      1.1  christos             EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
   4785      1.1  christos         }
   4786      1.1  christos #endif /* OPENSSL_NO_ECX */
   4787      1.1  christos #ifndef OPENSSL_NO_SM2
   4788      1.1  christos         for (k = 0; k < SM2_NUM; k++) {
   4789      1.1  christos             EVP_PKEY_CTX *pctx = NULL;
   4790      1.1  christos 
   4791      1.1  christos             /* free signing ctx */
   4792      1.1  christos             if (loopargs[i].sm2_ctx[k] != NULL
   4793      1.1  christos                 && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
   4794      1.1  christos                 EVP_PKEY_CTX_free(pctx);
   4795      1.1  christos             EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
   4796      1.1  christos             /* free verification ctx */
   4797      1.1  christos             if (loopargs[i].sm2_vfy_ctx[k] != NULL
   4798      1.1  christos                 && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
   4799      1.1  christos                 EVP_PKEY_CTX_free(pctx);
   4800      1.1  christos             EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
   4801      1.1  christos             /* free pkey */
   4802      1.1  christos             EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
   4803      1.1  christos         }
   4804      1.1  christos #endif
   4805      1.1  christos         for (k = 0; k < kems_algs_len; k++) {
   4806      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].kem_gen_ctx[k]);
   4807      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].kem_encaps_ctx[k]);
   4808      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].kem_decaps_ctx[k]);
   4809      1.1  christos             OPENSSL_free(loopargs[i].kem_out[k]);
   4810      1.1  christos             OPENSSL_free(loopargs[i].kem_send_secret[k]);
   4811      1.1  christos             OPENSSL_free(loopargs[i].kem_rcv_secret[k]);
   4812      1.1  christos         }
   4813      1.1  christos         for (k = 0; k < sigs_algs_len; k++) {
   4814      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].sig_gen_ctx[k]);
   4815      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].sig_sign_ctx[k]);
   4816      1.1  christos             EVP_PKEY_CTX_free(loopargs[i].sig_verify_ctx[k]);
   4817      1.1  christos             OPENSSL_free(loopargs[i].sig_sig[k]);
   4818      1.1  christos         }
   4819      1.1  christos         OPENSSL_free(loopargs[i].secret_a);
   4820      1.1  christos         OPENSSL_free(loopargs[i].secret_b);
   4821      1.1  christos     }
   4822      1.1  christos     OPENSSL_free(evp_hmac_name);
   4823      1.1  christos     OPENSSL_free(evp_cmac_name);
   4824      1.1  christos     for (k = 0; k < kems_algs_len; k++)
   4825      1.1  christos         OPENSSL_free(kems_algname[k]);
   4826      1.1  christos     if (kem_stack != NULL)
   4827      1.1  christos         sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
   4828      1.1  christos     for (k = 0; k < sigs_algs_len; k++)
   4829      1.1  christos         OPENSSL_free(sigs_algname[k]);
   4830      1.1  christos     if (sig_stack != NULL)
   4831      1.1  christos         sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
   4832      1.1  christos 
   4833      1.1  christos     if (async_jobs > 0) {
   4834      1.1  christos         for (i = 0; i < loopargs_len; i++)
   4835      1.1  christos             ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
   4836      1.1  christos     }
   4837      1.1  christos 
   4838      1.1  christos     if (async_init) {
   4839      1.1  christos         ASYNC_cleanup_thread();
   4840      1.1  christos     }
   4841      1.1  christos     OPENSSL_free(loopargs);
   4842      1.1  christos     release_engine(e);
   4843      1.1  christos     EVP_CIPHER_free(evp_cipher);
   4844      1.1  christos     EVP_MAC_free(mac);
   4845      1.1  christos     NCONF_free(conf);
   4846      1.1  christos     return ret;
   4847      1.1  christos }
   4848      1.1  christos 
   4849      1.1  christos static void print_message(const char *s, int length, int tm)
   4850      1.1  christos {
   4851      1.1  christos     BIO_printf(bio_err,
   4852  1.1.1.2  christos         mr ? "+DT:%s:%d:%d\n"
   4853  1.1.1.2  christos            : "Doing %s ops for %ds on %d size blocks: ",
   4854  1.1.1.2  christos         s, tm, length);
   4855      1.1  christos     (void)BIO_flush(bio_err);
   4856      1.1  christos     run = 1;
   4857      1.1  christos     alarm(tm);
   4858      1.1  christos }
   4859      1.1  christos 
   4860      1.1  christos static void pkey_print_message(const char *str, const char *str2, unsigned int bits,
   4861  1.1.1.2  christos     int tm)
   4862      1.1  christos {
   4863      1.1  christos     BIO_printf(bio_err,
   4864  1.1.1.2  christos         mr ? "+DTP:%d:%s:%s:%d\n"
   4865  1.1.1.2  christos            : "Doing %u bits %s %s ops for %ds: ",
   4866  1.1.1.2  christos         bits, str, str2, tm);
   4867      1.1  christos     (void)BIO_flush(bio_err);
   4868      1.1  christos     run = 1;
   4869      1.1  christos     alarm(tm);
   4870      1.1  christos }
   4871      1.1  christos 
   4872      1.1  christos static void kskey_print_message(const char *str, const char *str2, int tm)
   4873      1.1  christos {
   4874      1.1  christos     BIO_printf(bio_err,
   4875  1.1.1.2  christos         mr ? "+DTP:%s:%s:%d\n"
   4876  1.1.1.2  christos            : "Doing %s %s ops for %ds: ",
   4877  1.1.1.2  christos         str, str2, tm);
   4878      1.1  christos     (void)BIO_flush(bio_err);
   4879      1.1  christos     run = 1;
   4880      1.1  christos     alarm(tm);
   4881      1.1  christos }
   4882      1.1  christos 
   4883      1.1  christos static void print_result(int alg, int run_no, int count, double time_used)
   4884      1.1  christos {
   4885      1.1  christos     if (count == -1) {
   4886      1.1  christos         BIO_printf(bio_err, "%s error!\n", names[alg]);
   4887      1.1  christos         dofail();
   4888      1.1  christos         return;
   4889      1.1  christos     }
   4890      1.1  christos     BIO_printf(bio_err,
   4891  1.1.1.2  christos         mr ? "+R:%d:%s:%f\n"
   4892  1.1.1.2  christos            : "%d %s ops in %.2fs\n",
   4893  1.1.1.2  christos         count, names[alg], time_used);
   4894      1.1  christos     results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
   4895      1.1  christos }
   4896      1.1  christos 
   4897      1.1  christos #ifndef NO_FORK
   4898      1.1  christos static char *sstrsep(char **string, const char *delim)
   4899      1.1  christos {
   4900      1.1  christos     char isdelim[256];
   4901      1.1  christos     char *token = *string;
   4902      1.1  christos 
   4903      1.1  christos     memset(isdelim, 0, sizeof(isdelim));
   4904      1.1  christos     isdelim[0] = 1;
   4905      1.1  christos 
   4906      1.1  christos     while (*delim) {
   4907      1.1  christos         isdelim[(unsigned char)(*delim)] = 1;
   4908      1.1  christos         delim++;
   4909      1.1  christos     }
   4910      1.1  christos 
   4911      1.1  christos     while (!isdelim[(unsigned char)(**string)])
   4912      1.1  christos         (*string)++;
   4913      1.1  christos 
   4914      1.1  christos     if (**string) {
   4915      1.1  christos         **string = 0;
   4916      1.1  christos         (*string)++;
   4917      1.1  christos     }
   4918      1.1  christos 
   4919      1.1  christos     return token;
   4920      1.1  christos }
   4921      1.1  christos 
   4922      1.1  christos static int strtoint(const char *str, const int min_val, const int upper_val,
   4923  1.1.1.2  christos     int *res)
   4924      1.1  christos {
   4925      1.1  christos     char *end = NULL;
   4926      1.1  christos     long int val = 0;
   4927      1.1  christos 
   4928      1.1  christos     errno = 0;
   4929      1.1  christos     val = strtol(str, &end, 10);
   4930      1.1  christos     if (errno == 0 && end != str && *end == 0
   4931      1.1  christos         && min_val <= val && val < upper_val) {
   4932      1.1  christos         *res = (int)val;
   4933      1.1  christos         return 1;
   4934      1.1  christos     } else {
   4935      1.1  christos         return 0;
   4936      1.1  christos     }
   4937      1.1  christos }
   4938      1.1  christos 
   4939      1.1  christos static int do_multi(int multi, int size_num)
   4940      1.1  christos {
   4941      1.1  christos     int n;
   4942      1.1  christos     int fd[2];
   4943      1.1  christos     int *fds;
   4944      1.1  christos     int status;
   4945      1.1  christos     static char sep[] = ":";
   4946      1.1  christos 
   4947      1.1  christos     fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
   4948      1.1  christos     for (n = 0; n < multi; ++n) {
   4949      1.1  christos         if (pipe(fd) == -1) {
   4950      1.1  christos             BIO_printf(bio_err, "pipe failure\n");
   4951      1.1  christos             exit(1);
   4952      1.1  christos         }
   4953      1.1  christos         fflush(stdout);
   4954      1.1  christos         (void)BIO_flush(bio_err);
   4955      1.1  christos         if (fork()) {
   4956      1.1  christos             close(fd[1]);
   4957      1.1  christos             fds[n] = fd[0];
   4958      1.1  christos         } else {
   4959      1.1  christos             close(fd[0]);
   4960      1.1  christos             close(1);
   4961      1.1  christos             if (dup(fd[1]) == -1) {
   4962      1.1  christos                 BIO_printf(bio_err, "dup failed\n");
   4963      1.1  christos                 exit(1);
   4964      1.1  christos             }
   4965      1.1  christos             close(fd[1]);
   4966      1.1  christos             mr = 1;
   4967      1.1  christos             usertime = 0;
   4968      1.1  christos             OPENSSL_free(fds);
   4969      1.1  christos             return 0;
   4970      1.1  christos         }
   4971      1.1  christos         printf("Forked child %d\n", n);
   4972      1.1  christos     }
   4973      1.1  christos 
   4974      1.1  christos     /* for now, assume the pipe is long enough to take all the output */
   4975      1.1  christos     for (n = 0; n < multi; ++n) {
   4976      1.1  christos         FILE *f;
   4977      1.1  christos         char buf[1024];
   4978      1.1  christos         char *p;
   4979      1.1  christos         char *tk;
   4980      1.1  christos         int k;
   4981      1.1  christos         double d;
   4982      1.1  christos 
   4983      1.1  christos         if ((f = fdopen(fds[n], "r")) == NULL) {
   4984      1.1  christos             BIO_printf(bio_err, "fdopen failure with 0x%x\n",
   4985  1.1.1.2  christos                 errno);
   4986      1.1  christos             OPENSSL_free(fds);
   4987      1.1  christos             return 1;
   4988      1.1  christos         }
   4989      1.1  christos         while (fgets(buf, sizeof(buf), f)) {
   4990      1.1  christos             p = strchr(buf, '\n');
   4991      1.1  christos             if (p)
   4992      1.1  christos                 *p = '\0';
   4993      1.1  christos             if (buf[0] != '+') {
   4994      1.1  christos                 BIO_printf(bio_err,
   4995  1.1.1.2  christos                     "Don't understand line '%s' from child %d\n", buf,
   4996  1.1.1.2  christos                     n);
   4997      1.1  christos                 continue;
   4998      1.1  christos             }
   4999      1.1  christos             printf("Got: %s from %d\n", buf, n);
   5000      1.1  christos             p = buf;
   5001      1.1  christos             if (CHECK_AND_SKIP_PREFIX(p, "+F:")) {
   5002      1.1  christos                 int alg;
   5003      1.1  christos                 int j;
   5004      1.1  christos 
   5005      1.1  christos                 if (strtoint(sstrsep(&p, sep), 0, ALGOR_NUM, &alg)) {
   5006      1.1  christos                     sstrsep(&p, sep);
   5007      1.1  christos                     for (j = 0; j < size_num; ++j)
   5008      1.1  christos                         results[alg][j] += atof(sstrsep(&p, sep));
   5009      1.1  christos                 }
   5010      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F2:")) {
   5011      1.1  christos                 tk = sstrsep(&p, sep);
   5012      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(rsa_results), &k)) {
   5013      1.1  christos                     sstrsep(&p, sep);
   5014      1.1  christos 
   5015      1.1  christos                     d = atof(sstrsep(&p, sep));
   5016      1.1  christos                     rsa_results[k][0] += d;
   5017      1.1  christos 
   5018      1.1  christos                     d = atof(sstrsep(&p, sep));
   5019      1.1  christos                     rsa_results[k][1] += d;
   5020      1.1  christos 
   5021      1.1  christos                     d = atof(sstrsep(&p, sep));
   5022      1.1  christos                     rsa_results[k][2] += d;
   5023      1.1  christos 
   5024      1.1  christos                     d = atof(sstrsep(&p, sep));
   5025      1.1  christos                     rsa_results[k][3] += d;
   5026      1.1  christos                 }
   5027  1.1.1.2  christos #ifndef OPENSSL_NO_DSA
   5028      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F3:")) {
   5029      1.1  christos                 tk = sstrsep(&p, sep);
   5030      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(dsa_results), &k)) {
   5031      1.1  christos                     sstrsep(&p, sep);
   5032      1.1  christos 
   5033      1.1  christos                     d = atof(sstrsep(&p, sep));
   5034      1.1  christos                     dsa_results[k][0] += d;
   5035      1.1  christos 
   5036      1.1  christos                     d = atof(sstrsep(&p, sep));
   5037      1.1  christos                     dsa_results[k][1] += d;
   5038      1.1  christos                 }
   5039  1.1.1.2  christos #endif /* OPENSSL_NO_DSA */
   5040      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F4:")) {
   5041      1.1  christos                 tk = sstrsep(&p, sep);
   5042      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(ecdsa_results), &k)) {
   5043      1.1  christos                     sstrsep(&p, sep);
   5044      1.1  christos 
   5045      1.1  christos                     d = atof(sstrsep(&p, sep));
   5046      1.1  christos                     ecdsa_results[k][0] += d;
   5047      1.1  christos 
   5048      1.1  christos                     d = atof(sstrsep(&p, sep));
   5049      1.1  christos                     ecdsa_results[k][1] += d;
   5050      1.1  christos                 }
   5051      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F5:")) {
   5052      1.1  christos                 tk = sstrsep(&p, sep);
   5053      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(ecdh_results), &k)) {
   5054      1.1  christos                     sstrsep(&p, sep);
   5055      1.1  christos 
   5056      1.1  christos                     d = atof(sstrsep(&p, sep));
   5057      1.1  christos                     ecdh_results[k][0] += d;
   5058      1.1  christos                 }
   5059  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
   5060      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F6:")) {
   5061      1.1  christos                 tk = sstrsep(&p, sep);
   5062      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(eddsa_results), &k)) {
   5063      1.1  christos                     sstrsep(&p, sep);
   5064      1.1  christos                     sstrsep(&p, sep);
   5065      1.1  christos 
   5066      1.1  christos                     d = atof(sstrsep(&p, sep));
   5067      1.1  christos                     eddsa_results[k][0] += d;
   5068      1.1  christos 
   5069      1.1  christos                     d = atof(sstrsep(&p, sep));
   5070      1.1  christos                     eddsa_results[k][1] += d;
   5071      1.1  christos                 }
   5072  1.1.1.2  christos #endif /* OPENSSL_NO_ECX */
   5073  1.1.1.2  christos #ifndef OPENSSL_NO_SM2
   5074      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F7:")) {
   5075      1.1  christos                 tk = sstrsep(&p, sep);
   5076      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(sm2_results), &k)) {
   5077      1.1  christos                     sstrsep(&p, sep);
   5078      1.1  christos                     sstrsep(&p, sep);
   5079      1.1  christos 
   5080      1.1  christos                     d = atof(sstrsep(&p, sep));
   5081      1.1  christos                     sm2_results[k][0] += d;
   5082      1.1  christos 
   5083      1.1  christos                     d = atof(sstrsep(&p, sep));
   5084      1.1  christos                     sm2_results[k][1] += d;
   5085      1.1  christos                 }
   5086  1.1.1.2  christos #endif /* OPENSSL_NO_SM2 */
   5087  1.1.1.2  christos #ifndef OPENSSL_NO_DH
   5088      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F8:")) {
   5089      1.1  christos                 tk = sstrsep(&p, sep);
   5090      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(ffdh_results), &k)) {
   5091      1.1  christos                     sstrsep(&p, sep);
   5092      1.1  christos 
   5093      1.1  christos                     d = atof(sstrsep(&p, sep));
   5094      1.1  christos                     ffdh_results[k][0] += d;
   5095      1.1  christos                 }
   5096  1.1.1.2  christos #endif /* OPENSSL_NO_DH */
   5097      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F9:")) {
   5098      1.1  christos                 tk = sstrsep(&p, sep);
   5099      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(kems_results), &k)) {
   5100      1.1  christos                     d = atof(sstrsep(&p, sep));
   5101      1.1  christos                     kems_results[k][0] += d;
   5102      1.1  christos 
   5103      1.1  christos                     d = atof(sstrsep(&p, sep));
   5104      1.1  christos                     kems_results[k][1] += d;
   5105      1.1  christos 
   5106      1.1  christos                     d = atof(sstrsep(&p, sep));
   5107      1.1  christos                     kems_results[k][2] += d;
   5108      1.1  christos                 }
   5109      1.1  christos             } else if (CHECK_AND_SKIP_PREFIX(p, "+F10:")) {
   5110      1.1  christos                 tk = sstrsep(&p, sep);
   5111      1.1  christos                 if (strtoint(tk, 0, OSSL_NELEM(sigs_results), &k)) {
   5112      1.1  christos                     d = atof(sstrsep(&p, sep));
   5113      1.1  christos                     sigs_results[k][0] += d;
   5114      1.1  christos 
   5115      1.1  christos                     d = atof(sstrsep(&p, sep));
   5116      1.1  christos                     sigs_results[k][1] += d;
   5117      1.1  christos 
   5118      1.1  christos                     d = atof(sstrsep(&p, sep));
   5119      1.1  christos                     sigs_results[k][2] += d;
   5120      1.1  christos                 }
   5121      1.1  christos             } else if (!HAS_PREFIX(buf, "+H:")) {
   5122      1.1  christos                 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
   5123  1.1.1.2  christos                     n);
   5124      1.1  christos             }
   5125      1.1  christos         }
   5126      1.1  christos 
   5127      1.1  christos         fclose(f);
   5128      1.1  christos     }
   5129      1.1  christos     OPENSSL_free(fds);
   5130      1.1  christos     for (n = 0; n < multi; ++n) {
   5131      1.1  christos         while (wait(&status) == -1)
   5132      1.1  christos             if (errno != EINTR) {
   5133      1.1  christos                 BIO_printf(bio_err, "Waitng for child failed with 0x%x\n",
   5134  1.1.1.2  christos                     errno);
   5135      1.1  christos                 return 1;
   5136      1.1  christos             }
   5137      1.1  christos         if (WIFEXITED(status) && WEXITSTATUS(status)) {
   5138      1.1  christos             BIO_printf(bio_err, "Child exited with %d\n", WEXITSTATUS(status));
   5139      1.1  christos         } else if (WIFSIGNALED(status)) {
   5140      1.1  christos             BIO_printf(bio_err, "Child terminated by signal %d\n",
   5141  1.1.1.2  christos                 WTERMSIG(status));
   5142      1.1  christos         }
   5143      1.1  christos     }
   5144      1.1  christos     return 1;
   5145      1.1  christos }
   5146      1.1  christos #endif
   5147      1.1  christos 
   5148      1.1  christos static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
   5149  1.1.1.2  christos     const openssl_speed_sec_t *seconds)
   5150      1.1  christos {
   5151      1.1  christos     static const int mblengths_list[] = {
   5152      1.1  christos         8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024
   5153      1.1  christos     };
   5154      1.1  christos     const int *mblengths = mblengths_list;
   5155      1.1  christos     int j, count, keylen, num = OSSL_NELEM(mblengths_list), ciph_success = 1;
   5156      1.1  christos     const char *alg_name;
   5157      1.1  christos     unsigned char *inp = NULL, *out = NULL, *key, no_key[32], no_iv[16];
   5158      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5159      1.1  christos     double d = 0.0;
   5160      1.1  christos 
   5161      1.1  christos     if (lengths_single) {
   5162      1.1  christos         mblengths = &lengths_single;
   5163      1.1  christos         num = 1;
   5164      1.1  christos     }
   5165      1.1  christos 
   5166      1.1  christos     inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
   5167      1.1  christos     out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
   5168      1.1  christos     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
   5169      1.1  christos         app_bail_out("failed to allocate cipher context\n");
   5170      1.1  christos     if (!EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv))
   5171      1.1  christos         app_bail_out("failed to initialise cipher context\n");
   5172      1.1  christos 
   5173      1.1  christos     if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) < 0) {
   5174      1.1  christos         BIO_printf(bio_err, "Impossible negative key length: %d\n", keylen);
   5175      1.1  christos         goto err;
   5176      1.1  christos     }
   5177      1.1  christos     key = app_malloc(keylen, "evp_cipher key");
   5178      1.1  christos     if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
   5179      1.1  christos         app_bail_out("failed to generate random cipher key\n");
   5180      1.1  christos     if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
   5181      1.1  christos         app_bail_out("failed to set cipher key\n");
   5182      1.1  christos     OPENSSL_clear_free(key, keylen);
   5183      1.1  christos 
   5184      1.1  christos     if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
   5185  1.1.1.2  christos             sizeof(no_key), no_key)
   5186  1.1.1.2  christos         <= 0)
   5187      1.1  christos         app_bail_out("failed to set AEAD key\n");
   5188      1.1  christos     if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
   5189      1.1  christos         app_bail_out("failed to get cipher name\n");
   5190      1.1  christos 
   5191      1.1  christos     for (j = 0; j < num; j++) {
   5192      1.1  christos         print_message(alg_name, mblengths[j], seconds->sym);
   5193      1.1  christos         Time_F(START);
   5194      1.1  christos         for (count = 0; run && COND(count); count++) {
   5195      1.1  christos             EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
   5196      1.1  christos             size_t len = mblengths[j];
   5197      1.1  christos             int packlen;
   5198      1.1  christos 
   5199  1.1.1.2  christos             memset(aad, 0, 8); /* avoid uninitialized values */
   5200  1.1.1.2  christos             aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
   5201  1.1.1.2  christos             aad[9] = 3; /* version */
   5202      1.1  christos             aad[10] = 2;
   5203  1.1.1.2  christos             aad[11] = 0; /* length */
   5204      1.1  christos             aad[12] = 0;
   5205      1.1  christos             mb_param.out = NULL;
   5206      1.1  christos             mb_param.inp = aad;
   5207      1.1  christos             mb_param.len = len;
   5208      1.1  christos             mb_param.interleave = 8;
   5209      1.1  christos 
   5210      1.1  christos             packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
   5211  1.1.1.2  christos                 sizeof(mb_param), &mb_param);
   5212      1.1  christos 
   5213      1.1  christos             if (packlen > 0) {
   5214      1.1  christos                 mb_param.out = out;
   5215      1.1  christos                 mb_param.inp = inp;
   5216      1.1  christos                 mb_param.len = len;
   5217      1.1  christos                 (void)EVP_CIPHER_CTX_ctrl(ctx,
   5218  1.1.1.2  christos                     EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
   5219  1.1.1.2  christos                     sizeof(mb_param), &mb_param);
   5220      1.1  christos             } else {
   5221      1.1  christos                 int pad;
   5222      1.1  christos 
   5223      1.1  christos                 if (RAND_bytes(inp, 16) <= 0)
   5224      1.1  christos                     app_bail_out("error setting random bytes\n");
   5225      1.1  christos                 len += 16;
   5226      1.1  christos                 aad[11] = (unsigned char)(len >> 8);
   5227      1.1  christos                 aad[12] = (unsigned char)(len);
   5228      1.1  christos                 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
   5229  1.1.1.2  christos                     EVP_AEAD_TLS1_AAD_LEN, aad);
   5230      1.1  christos                 ciph_success = EVP_Cipher(ctx, out, inp, len + pad);
   5231      1.1  christos             }
   5232      1.1  christos         }
   5233      1.1  christos         d = Time_F(STOP);
   5234  1.1.1.2  christos         BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n" : "%d %s ops in %.2fs\n", count, "evp", d);
   5235      1.1  christos         if ((ciph_success <= 0) && (mr == 0))
   5236      1.1  christos             BIO_printf(bio_err, "Error performing cipher op\n");
   5237      1.1  christos         results[D_EVP][j] = ((double)count) / d * mblengths[j];
   5238      1.1  christos     }
   5239      1.1  christos 
   5240      1.1  christos     if (mr) {
   5241      1.1  christos         fprintf(stdout, "+H");
   5242      1.1  christos         for (j = 0; j < num; j++)
   5243      1.1  christos             fprintf(stdout, ":%d", mblengths[j]);
   5244      1.1  christos         fprintf(stdout, "\n");
   5245      1.1  christos         fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
   5246      1.1  christos         for (j = 0; j < num; j++)
   5247      1.1  christos             fprintf(stdout, ":%.2f", results[D_EVP][j]);
   5248      1.1  christos         fprintf(stdout, "\n");
   5249      1.1  christos     } else {
   5250      1.1  christos         fprintf(stdout,
   5251  1.1.1.2  christos             "The 'numbers' are in 1000s of bytes per second processed.\n");
   5252      1.1  christos         fprintf(stdout, "type                    ");
   5253      1.1  christos         for (j = 0; j < num; j++)
   5254      1.1  christos             fprintf(stdout, "%7d bytes", mblengths[j]);
   5255      1.1  christos         fprintf(stdout, "\n");
   5256      1.1  christos         fprintf(stdout, "%-24s", alg_name);
   5257      1.1  christos 
   5258      1.1  christos         for (j = 0; j < num; j++) {
   5259      1.1  christos             if (results[D_EVP][j] > 10000)
   5260      1.1  christos                 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
   5261      1.1  christos             else
   5262      1.1  christos                 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
   5263      1.1  christos         }
   5264      1.1  christos         fprintf(stdout, "\n");
   5265      1.1  christos     }
   5266      1.1  christos 
   5267  1.1.1.2  christos err:
   5268      1.1  christos     OPENSSL_free(inp);
   5269      1.1  christos     OPENSSL_free(out);
   5270      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5271      1.1  christos }
   5272