1 /* $OpenBSD: speed.c,v 1.28 2022/01/14 09:27:30 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 /* ==================================================================== 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60 * 61 * Portions of the attached software ("Contribution") are developed by 62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 63 * 64 * The Contribution is licensed pursuant to the OpenSSL open source 65 * license provided above. 66 * 67 * The ECDH and ECDSA speed test software is originally written by 68 * Sumit Gupta of Sun Microsystems Laboratories. 69 * 70 */ 71 72 /* most of this code has been pilfered from my libdes speed.c program */ 73 74 #ifndef OPENSSL_NO_SPEED 75 76 #define SECONDS 3 77 #define RSA_SECONDS 10 78 #define DSA_SECONDS 10 79 #define ECDSA_SECONDS 10 80 #define ECDH_SECONDS 10 81 82 #include <math.h> 83 #include <signal.h> 84 #include <stdio.h> 85 #include <stdlib.h> 86 #include <limits.h> 87 #include <string.h> 88 #include <unistd.h> 89 90 #include "apps.h" 91 92 #include <openssl/bn.h> 93 #include <openssl/crypto.h> 94 #include <openssl/err.h> 95 #include <openssl/evp.h> 96 #include <openssl/modes.h> 97 #include <openssl/objects.h> 98 #include <openssl/x509.h> 99 100 #ifndef OPENSSL_NO_AES 101 #include <openssl/aes.h> 102 #endif 103 #ifndef OPENSSL_NO_BF 104 #include <openssl/blowfish.h> 105 #endif 106 #ifndef OPENSSL_NO_CAST 107 #include <openssl/cast.h> 108 #endif 109 #ifndef OPENSSL_NO_CAMELLIA 110 #include <openssl/camellia.h> 111 #endif 112 #ifndef OPENSSL_NO_DES 113 #include <openssl/des.h> 114 #endif 115 #include <openssl/dsa.h> 116 #include <openssl/ecdh.h> 117 #include <openssl/ecdsa.h> 118 #ifndef OPENSSL_NO_HMAC 119 #include <openssl/hmac.h> 120 #endif 121 #ifndef OPENSSL_NO_IDEA 122 #include <openssl/idea.h> 123 #endif 124 #ifndef OPENSSL_NO_MD4 125 #include <openssl/md4.h> 126 #endif 127 #ifndef OPENSSL_NO_MD5 128 #include <openssl/md5.h> 129 #endif 130 #ifndef OPENSSL_NO_RC2 131 #include <openssl/rc2.h> 132 #endif 133 #ifndef OPENSSL_NO_RC4 134 #include <openssl/rc4.h> 135 #endif 136 #include <openssl/rsa.h> 137 #ifndef OPENSSL_NO_RIPEMD 138 #include <openssl/ripemd.h> 139 #endif 140 #ifndef OPENSSL_NO_SHA 141 #include <openssl/sha.h> 142 #endif 143 #ifndef OPENSSL_NO_WHIRLPOOL 144 #include <openssl/whrlpool.h> 145 #endif 146 147 #include "./testdsa.h" 148 #include "./testrsa.h" 149 150 #define BUFSIZE (1024*8+64) 151 int run = 0; 152 153 static int mr = 0; 154 static int usertime = 1; 155 156 static double Time_F(int s); 157 static void print_message(const char *s, long num, int length); 158 static void 159 pkey_print_message(const char *str, const char *str2, 160 long num, int bits, int sec); 161 static void print_result(int alg, int run_no, int count, double time_used); 162 static int do_multi(int multi); 163 164 #define ALGOR_NUM 32 165 #define SIZE_NUM 5 166 #define RSA_NUM 4 167 #define DSA_NUM 3 168 169 #define EC_NUM 16 170 #define MAX_ECDH_SIZE 256 171 172 static const char *names[ALGOR_NUM] = { 173 "md2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", 174 "rc4", "des cbc", "des ede3", "idea cbc", "seed cbc", 175 "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc", 176 "aes-128 cbc", "aes-192 cbc", "aes-256 cbc", 177 "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc", 178 "evp", "sha256", "sha512", "whirlpool", 179 "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash", 180 "aes-128 gcm", "aes-256 gcm", "chacha20 poly1305", 181 }; 182 static double results[ALGOR_NUM][SIZE_NUM]; 183 static int lengths[SIZE_NUM] = {16, 64, 256, 1024, 8 * 1024}; 184 static double rsa_results[RSA_NUM][2]; 185 static double dsa_results[DSA_NUM][2]; 186 static double ecdsa_results[EC_NUM][2]; 187 static double ecdh_results[EC_NUM][1]; 188 189 static void sig_done(int sig); 190 191 static void 192 sig_done(int sig) 193 { 194 signal(SIGALRM, sig_done); 195 run = 0; 196 } 197 198 #define START TM_RESET 199 #define STOP TM_GET 200 201 202 static double 203 Time_F(int s) 204 { 205 if (usertime) 206 return app_timer_user(s); 207 else 208 return app_timer_real(s); 209 } 210 211 212 static const int KDF1_SHA1_len = 20; 213 static void * 214 KDF1_SHA1(const void *in, size_t inlen, void *out, size_t * outlen) 215 { 216 #ifndef OPENSSL_NO_SHA 217 if (*outlen < SHA_DIGEST_LENGTH) 218 return NULL; 219 else 220 *outlen = SHA_DIGEST_LENGTH; 221 return SHA1(in, inlen, out); 222 #else 223 return NULL; 224 #endif /* OPENSSL_NO_SHA */ 225 } 226 227 int 228 speed_main(int argc, char **argv) 229 { 230 unsigned char *buf = NULL, *buf2 = NULL; 231 int mret = 1; 232 long count = 0, save_count = 0; 233 int i, j, k; 234 long rsa_count; 235 unsigned rsa_num; 236 unsigned char md[EVP_MAX_MD_SIZE]; 237 #ifndef OPENSSL_NO_MD4 238 unsigned char md4[MD4_DIGEST_LENGTH]; 239 #endif 240 #ifndef OPENSSL_NO_MD5 241 unsigned char md5[MD5_DIGEST_LENGTH]; 242 unsigned char hmac[MD5_DIGEST_LENGTH]; 243 #endif 244 #ifndef OPENSSL_NO_SHA 245 unsigned char sha[SHA_DIGEST_LENGTH]; 246 #ifndef OPENSSL_NO_SHA256 247 unsigned char sha256[SHA256_DIGEST_LENGTH]; 248 #endif 249 #ifndef OPENSSL_NO_SHA512 250 unsigned char sha512[SHA512_DIGEST_LENGTH]; 251 #endif 252 #endif 253 #ifndef OPENSSL_NO_WHIRLPOOL 254 unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH]; 255 #endif 256 #ifndef OPENSSL_NO_RIPEMD 257 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH]; 258 #endif 259 #ifndef OPENSSL_NO_RC4 260 RC4_KEY rc4_ks; 261 #endif 262 #ifndef OPENSSL_NO_RC2 263 RC2_KEY rc2_ks; 264 #endif 265 #ifndef OPENSSL_NO_IDEA 266 IDEA_KEY_SCHEDULE idea_ks; 267 #endif 268 #ifndef OPENSSL_NO_BF 269 BF_KEY bf_ks; 270 #endif 271 #ifndef OPENSSL_NO_CAST 272 CAST_KEY cast_ks; 273 #endif 274 static const unsigned char key16[16] = 275 {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 276 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12}; 277 #ifndef OPENSSL_NO_AES 278 static const unsigned char key24[24] = 279 {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 280 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 281 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34}; 282 static const unsigned char key32[32] = 283 {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 284 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 285 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 286 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56}; 287 #endif 288 #ifndef OPENSSL_NO_CAMELLIA 289 static const unsigned char ckey24[24] = 290 {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 291 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 292 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34}; 293 static const unsigned char ckey32[32] = 294 {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 295 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 296 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 297 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56}; 298 #endif 299 #ifndef OPENSSL_NO_AES 300 #define MAX_BLOCK_SIZE 128 301 #else 302 #define MAX_BLOCK_SIZE 64 303 #endif 304 unsigned char DES_iv[8]; 305 unsigned char iv[2 * MAX_BLOCK_SIZE / 8]; 306 #ifndef OPENSSL_NO_DES 307 static DES_cblock key = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}; 308 static DES_cblock key2 = {0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12}; 309 static DES_cblock key3 = {0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34}; 310 DES_key_schedule sch; 311 DES_key_schedule sch2; 312 DES_key_schedule sch3; 313 #endif 314 #ifndef OPENSSL_NO_AES 315 AES_KEY aes_ks1, aes_ks2, aes_ks3; 316 #endif 317 #ifndef OPENSSL_NO_CAMELLIA 318 CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3; 319 #endif 320 #define D_MD2 0 321 #define D_MD4 1 322 #define D_MD5 2 323 #define D_HMAC 3 324 #define D_SHA1 4 325 #define D_RMD160 5 326 #define D_RC4 6 327 #define D_CBC_DES 7 328 #define D_EDE3_DES 8 329 #define D_CBC_IDEA 9 330 #define D_CBC_SEED 10 331 #define D_CBC_RC2 11 332 #define D_CBC_RC5 12 333 #define D_CBC_BF 13 334 #define D_CBC_CAST 14 335 #define D_CBC_128_AES 15 336 #define D_CBC_192_AES 16 337 #define D_CBC_256_AES 17 338 #define D_CBC_128_CML 18 339 #define D_CBC_192_CML 19 340 #define D_CBC_256_CML 20 341 #define D_EVP 21 342 #define D_SHA256 22 343 #define D_SHA512 23 344 #define D_WHIRLPOOL 24 345 #define D_IGE_128_AES 25 346 #define D_IGE_192_AES 26 347 #define D_IGE_256_AES 27 348 #define D_GHASH 28 349 #define D_AES_128_GCM 29 350 #define D_AES_256_GCM 30 351 #define D_CHACHA20_POLY1305 31 352 double d = 0.0; 353 long c[ALGOR_NUM][SIZE_NUM]; 354 #define R_DSA_512 0 355 #define R_DSA_1024 1 356 #define R_DSA_2048 2 357 #define R_RSA_512 0 358 #define R_RSA_1024 1 359 #define R_RSA_2048 2 360 #define R_RSA_4096 3 361 362 #define R_EC_P160 0 363 #define R_EC_P192 1 364 #define R_EC_P224 2 365 #define R_EC_P256 3 366 #define R_EC_P384 4 367 #define R_EC_P521 5 368 #define R_EC_K163 6 369 #define R_EC_K233 7 370 #define R_EC_K283 8 371 #define R_EC_K409 9 372 #define R_EC_K571 10 373 #define R_EC_B163 11 374 #define R_EC_B233 12 375 #define R_EC_B283 13 376 #define R_EC_B409 14 377 #define R_EC_B571 15 378 379 RSA *rsa_key[RSA_NUM]; 380 long rsa_c[RSA_NUM][2]; 381 static unsigned int rsa_bits[RSA_NUM] = {512, 1024, 2048, 4096}; 382 static unsigned char *rsa_data[RSA_NUM] = 383 {test512, test1024, test2048, test4096}; 384 static int rsa_data_length[RSA_NUM] = { 385 sizeof(test512), sizeof(test1024), 386 sizeof(test2048), sizeof(test4096)}; 387 DSA *dsa_key[DSA_NUM]; 388 long dsa_c[DSA_NUM][2]; 389 static unsigned int dsa_bits[DSA_NUM] = {512, 1024, 2048}; 390 #ifndef OPENSSL_NO_EC 391 /* 392 * We only test over the following curves as they are representative, 393 * To add tests over more curves, simply add the curve NID and curve 394 * name to the following arrays and increase the EC_NUM value 395 * accordingly. 396 */ 397 static unsigned int test_curves[EC_NUM] = 398 { 399 /* Prime Curves */ 400 NID_secp160r1, 401 NID_X9_62_prime192v1, 402 NID_secp224r1, 403 NID_X9_62_prime256v1, 404 NID_secp384r1, 405 NID_secp521r1, 406 /* Binary Curves */ 407 NID_sect163k1, 408 NID_sect233k1, 409 NID_sect283k1, 410 NID_sect409k1, 411 NID_sect571k1, 412 NID_sect163r2, 413 NID_sect233r1, 414 NID_sect283r1, 415 NID_sect409r1, 416 NID_sect571r1 417 }; 418 static const char *test_curves_names[EC_NUM] = 419 { 420 /* Prime Curves */ 421 "secp160r1", 422 "nistp192", 423 "nistp224", 424 "nistp256", 425 "nistp384", 426 "nistp521", 427 /* Binary Curves */ 428 "nistk163", 429 "nistk233", 430 "nistk283", 431 "nistk409", 432 "nistk571", 433 "nistb163", 434 "nistb233", 435 "nistb283", 436 "nistb409", 437 "nistb571" 438 }; 439 static int test_curves_bits[EC_NUM] = 440 { 441 160, 192, 224, 256, 384, 521, 442 163, 233, 283, 409, 571, 443 163, 233, 283, 409, 571 444 }; 445 446 #endif 447 448 unsigned char ecdsasig[256]; 449 unsigned int ecdsasiglen; 450 EC_KEY *ecdsa[EC_NUM]; 451 long ecdsa_c[EC_NUM][2]; 452 453 EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM]; 454 unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE]; 455 int secret_size_a, secret_size_b; 456 int ecdh_checks = 0; 457 int secret_idx = 0; 458 long ecdh_c[EC_NUM][2]; 459 460 int rsa_doit[RSA_NUM]; 461 int dsa_doit[DSA_NUM]; 462 int ecdsa_doit[EC_NUM]; 463 int ecdh_doit[EC_NUM]; 464 int doit[ALGOR_NUM]; 465 int pr_header = 0; 466 const EVP_CIPHER *evp_cipher = NULL; 467 const EVP_MD *evp_md = NULL; 468 int decrypt = 0; 469 int multi = 0; 470 const char *errstr = NULL; 471 472 if (single_execution) { 473 if (pledge("stdio proc", NULL) == -1) { 474 perror("pledge"); 475 exit(1); 476 } 477 } 478 479 usertime = -1; 480 481 memset(results, 0, sizeof(results)); 482 memset(dsa_key, 0, sizeof(dsa_key)); 483 for (i = 0; i < EC_NUM; i++) 484 ecdsa[i] = NULL; 485 for (i = 0; i < EC_NUM; i++) { 486 ecdh_a[i] = NULL; 487 ecdh_b[i] = NULL; 488 } 489 490 memset(rsa_key, 0, sizeof(rsa_key)); 491 for (i = 0; i < RSA_NUM; i++) 492 rsa_key[i] = NULL; 493 494 if ((buf = malloc(BUFSIZE)) == NULL) { 495 BIO_printf(bio_err, "out of memory\n"); 496 goto end; 497 } 498 if ((buf2 = malloc(BUFSIZE)) == NULL) { 499 BIO_printf(bio_err, "out of memory\n"); 500 goto end; 501 } 502 memset(c, 0, sizeof(c)); 503 memset(DES_iv, 0, sizeof(DES_iv)); 504 memset(iv, 0, sizeof(iv)); 505 506 for (i = 0; i < ALGOR_NUM; i++) 507 doit[i] = 0; 508 for (i = 0; i < RSA_NUM; i++) 509 rsa_doit[i] = 0; 510 for (i = 0; i < DSA_NUM; i++) 511 dsa_doit[i] = 0; 512 for (i = 0; i < EC_NUM; i++) 513 ecdsa_doit[i] = 0; 514 for (i = 0; i < EC_NUM; i++) 515 ecdh_doit[i] = 0; 516 517 518 j = 0; 519 argc--; 520 argv++; 521 while (argc) { 522 if ((argc > 0) && (strcmp(*argv, "-elapsed") == 0)) { 523 usertime = 0; 524 j--; /* Otherwise, -elapsed gets confused with an 525 * algorithm. */ 526 } else if ((argc > 0) && (strcmp(*argv, "-evp") == 0)) { 527 argc--; 528 argv++; 529 if (argc == 0) { 530 BIO_printf(bio_err, "no EVP given\n"); 531 goto end; 532 } 533 evp_cipher = EVP_get_cipherbyname(*argv); 534 if (!evp_cipher) { 535 evp_md = EVP_get_digestbyname(*argv); 536 } 537 if (!evp_cipher && !evp_md) { 538 BIO_printf(bio_err, "%s is an unknown cipher or digest\n", *argv); 539 goto end; 540 } 541 doit[D_EVP] = 1; 542 } else if (argc > 0 && !strcmp(*argv, "-decrypt")) { 543 decrypt = 1; 544 j--; /* Otherwise, -decrypt gets confused with an 545 * algorithm. */ 546 } 547 else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) { 548 argc--; 549 argv++; 550 if (argc == 0) { 551 BIO_printf(bio_err, "no multi count given\n"); 552 goto end; 553 } 554 multi = strtonum(argv[0], 1, INT_MAX, &errstr); 555 if (errstr) { 556 BIO_printf(bio_err, "bad multi count: %s", errstr); 557 goto end; 558 } 559 j--; /* Otherwise, -multi gets confused with an 560 * algorithm. */ 561 } 562 else if (argc > 0 && !strcmp(*argv, "-mr")) { 563 mr = 1; 564 j--; /* Otherwise, -mr gets confused with an 565 * algorithm. */ 566 } else 567 #ifndef OPENSSL_NO_MD4 568 if (strcmp(*argv, "md4") == 0) 569 doit[D_MD4] = 1; 570 else 571 #endif 572 #ifndef OPENSSL_NO_MD5 573 if (strcmp(*argv, "md5") == 0) 574 doit[D_MD5] = 1; 575 else 576 #endif 577 #ifndef OPENSSL_NO_MD5 578 if (strcmp(*argv, "hmac") == 0) 579 doit[D_HMAC] = 1; 580 else 581 #endif 582 #ifndef OPENSSL_NO_SHA 583 if (strcmp(*argv, "sha1") == 0) 584 doit[D_SHA1] = 1; 585 else if (strcmp(*argv, "sha") == 0) 586 doit[D_SHA1] = 1, 587 doit[D_SHA256] = 1, 588 doit[D_SHA512] = 1; 589 else 590 #ifndef OPENSSL_NO_SHA256 591 if (strcmp(*argv, "sha256") == 0) 592 doit[D_SHA256] = 1; 593 else 594 #endif 595 #ifndef OPENSSL_NO_SHA512 596 if (strcmp(*argv, "sha512") == 0) 597 doit[D_SHA512] = 1; 598 else 599 #endif 600 #endif 601 #ifndef OPENSSL_NO_WHIRLPOOL 602 if (strcmp(*argv, "whirlpool") == 0) 603 doit[D_WHIRLPOOL] = 1; 604 else 605 #endif 606 #ifndef OPENSSL_NO_RIPEMD 607 if (strcmp(*argv, "ripemd") == 0) 608 doit[D_RMD160] = 1; 609 else if (strcmp(*argv, "rmd160") == 0) 610 doit[D_RMD160] = 1; 611 else if (strcmp(*argv, "ripemd160") == 0) 612 doit[D_RMD160] = 1; 613 else 614 #endif 615 #ifndef OPENSSL_NO_RC4 616 if (strcmp(*argv, "rc4") == 0) 617 doit[D_RC4] = 1; 618 else 619 #endif 620 #ifndef OPENSSL_NO_DES 621 if (strcmp(*argv, "des-cbc") == 0) 622 doit[D_CBC_DES] = 1; 623 else if (strcmp(*argv, "des-ede3") == 0) 624 doit[D_EDE3_DES] = 1; 625 else 626 #endif 627 #ifndef OPENSSL_NO_AES 628 if (strcmp(*argv, "aes-128-cbc") == 0) 629 doit[D_CBC_128_AES] = 1; 630 else if (strcmp(*argv, "aes-192-cbc") == 0) 631 doit[D_CBC_192_AES] = 1; 632 else if (strcmp(*argv, "aes-256-cbc") == 0) 633 doit[D_CBC_256_AES] = 1; 634 else if (strcmp(*argv, "aes-128-ige") == 0) 635 doit[D_IGE_128_AES] = 1; 636 else if (strcmp(*argv, "aes-192-ige") == 0) 637 doit[D_IGE_192_AES] = 1; 638 else if (strcmp(*argv, "aes-256-ige") == 0) 639 doit[D_IGE_256_AES] = 1; 640 else 641 #endif 642 #ifndef OPENSSL_NO_CAMELLIA 643 if (strcmp(*argv, "camellia-128-cbc") == 0) 644 doit[D_CBC_128_CML] = 1; 645 else if (strcmp(*argv, "camellia-192-cbc") == 0) 646 doit[D_CBC_192_CML] = 1; 647 else if (strcmp(*argv, "camellia-256-cbc") == 0) 648 doit[D_CBC_256_CML] = 1; 649 else 650 #endif 651 #ifndef RSA_NULL 652 if (strcmp(*argv, "openssl") == 0) { 653 RSA_set_default_method(RSA_PKCS1_SSLeay()); 654 j--; 655 } else 656 #endif 657 if (strcmp(*argv, "dsa512") == 0) 658 dsa_doit[R_DSA_512] = 2; 659 else if (strcmp(*argv, "dsa1024") == 0) 660 dsa_doit[R_DSA_1024] = 2; 661 else if (strcmp(*argv, "dsa2048") == 0) 662 dsa_doit[R_DSA_2048] = 2; 663 else if (strcmp(*argv, "rsa512") == 0) 664 rsa_doit[R_RSA_512] = 2; 665 else if (strcmp(*argv, "rsa1024") == 0) 666 rsa_doit[R_RSA_1024] = 2; 667 else if (strcmp(*argv, "rsa2048") == 0) 668 rsa_doit[R_RSA_2048] = 2; 669 else if (strcmp(*argv, "rsa4096") == 0) 670 rsa_doit[R_RSA_4096] = 2; 671 else 672 #ifndef OPENSSL_NO_RC2 673 if (strcmp(*argv, "rc2-cbc") == 0) 674 doit[D_CBC_RC2] = 1; 675 else if (strcmp(*argv, "rc2") == 0) 676 doit[D_CBC_RC2] = 1; 677 else 678 #endif 679 #ifndef OPENSSL_NO_IDEA 680 if (strcmp(*argv, "idea-cbc") == 0) 681 doit[D_CBC_IDEA] = 1; 682 else if (strcmp(*argv, "idea") == 0) 683 doit[D_CBC_IDEA] = 1; 684 else 685 #endif 686 #ifndef OPENSSL_NO_BF 687 if (strcmp(*argv, "bf-cbc") == 0) 688 doit[D_CBC_BF] = 1; 689 else if (strcmp(*argv, "blowfish") == 0) 690 doit[D_CBC_BF] = 1; 691 else if (strcmp(*argv, "bf") == 0) 692 doit[D_CBC_BF] = 1; 693 else 694 #endif 695 #ifndef OPENSSL_NO_CAST 696 if (strcmp(*argv, "cast-cbc") == 0) 697 doit[D_CBC_CAST] = 1; 698 else if (strcmp(*argv, "cast") == 0) 699 doit[D_CBC_CAST] = 1; 700 else if (strcmp(*argv, "cast5") == 0) 701 doit[D_CBC_CAST] = 1; 702 else 703 #endif 704 #ifndef OPENSSL_NO_DES 705 if (strcmp(*argv, "des") == 0) { 706 doit[D_CBC_DES] = 1; 707 doit[D_EDE3_DES] = 1; 708 } else 709 #endif 710 #ifndef OPENSSL_NO_AES 711 if (strcmp(*argv, "aes") == 0) { 712 doit[D_CBC_128_AES] = 1; 713 doit[D_CBC_192_AES] = 1; 714 doit[D_CBC_256_AES] = 1; 715 } else if (strcmp(*argv, "ghash") == 0) 716 doit[D_GHASH] = 1; 717 else if (strcmp(*argv,"aes-128-gcm") == 0) 718 doit[D_AES_128_GCM]=1; 719 else if (strcmp(*argv,"aes-256-gcm") == 0) 720 doit[D_AES_256_GCM]=1; 721 else 722 #endif 723 #ifndef OPENSSL_NO_CAMELLIA 724 if (strcmp(*argv, "camellia") == 0) { 725 doit[D_CBC_128_CML] = 1; 726 doit[D_CBC_192_CML] = 1; 727 doit[D_CBC_256_CML] = 1; 728 } else 729 #endif 730 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) 731 if (strcmp(*argv,"chacha20-poly1305") == 0) 732 doit[D_CHACHA20_POLY1305]=1; 733 else 734 #endif 735 if (strcmp(*argv, "rsa") == 0) { 736 rsa_doit[R_RSA_512] = 1; 737 rsa_doit[R_RSA_1024] = 1; 738 rsa_doit[R_RSA_2048] = 1; 739 rsa_doit[R_RSA_4096] = 1; 740 } else 741 if (strcmp(*argv, "dsa") == 0) { 742 dsa_doit[R_DSA_512] = 1; 743 dsa_doit[R_DSA_1024] = 1; 744 dsa_doit[R_DSA_2048] = 1; 745 } else 746 if (strcmp(*argv, "ecdsap160") == 0) 747 ecdsa_doit[R_EC_P160] = 2; 748 else if (strcmp(*argv, "ecdsap192") == 0) 749 ecdsa_doit[R_EC_P192] = 2; 750 else if (strcmp(*argv, "ecdsap224") == 0) 751 ecdsa_doit[R_EC_P224] = 2; 752 else if (strcmp(*argv, "ecdsap256") == 0) 753 ecdsa_doit[R_EC_P256] = 2; 754 else if (strcmp(*argv, "ecdsap384") == 0) 755 ecdsa_doit[R_EC_P384] = 2; 756 else if (strcmp(*argv, "ecdsap521") == 0) 757 ecdsa_doit[R_EC_P521] = 2; 758 else if (strcmp(*argv, "ecdsak163") == 0) 759 ecdsa_doit[R_EC_K163] = 2; 760 else if (strcmp(*argv, "ecdsak233") == 0) 761 ecdsa_doit[R_EC_K233] = 2; 762 else if (strcmp(*argv, "ecdsak283") == 0) 763 ecdsa_doit[R_EC_K283] = 2; 764 else if (strcmp(*argv, "ecdsak409") == 0) 765 ecdsa_doit[R_EC_K409] = 2; 766 else if (strcmp(*argv, "ecdsak571") == 0) 767 ecdsa_doit[R_EC_K571] = 2; 768 else if (strcmp(*argv, "ecdsab163") == 0) 769 ecdsa_doit[R_EC_B163] = 2; 770 else if (strcmp(*argv, "ecdsab233") == 0) 771 ecdsa_doit[R_EC_B233] = 2; 772 else if (strcmp(*argv, "ecdsab283") == 0) 773 ecdsa_doit[R_EC_B283] = 2; 774 else if (strcmp(*argv, "ecdsab409") == 0) 775 ecdsa_doit[R_EC_B409] = 2; 776 else if (strcmp(*argv, "ecdsab571") == 0) 777 ecdsa_doit[R_EC_B571] = 2; 778 else if (strcmp(*argv, "ecdsa") == 0) { 779 for (i = 0; i < EC_NUM; i++) 780 ecdsa_doit[i] = 1; 781 } else 782 if (strcmp(*argv, "ecdhp160") == 0) 783 ecdh_doit[R_EC_P160] = 2; 784 else if (strcmp(*argv, "ecdhp192") == 0) 785 ecdh_doit[R_EC_P192] = 2; 786 else if (strcmp(*argv, "ecdhp224") == 0) 787 ecdh_doit[R_EC_P224] = 2; 788 else if (strcmp(*argv, "ecdhp256") == 0) 789 ecdh_doit[R_EC_P256] = 2; 790 else if (strcmp(*argv, "ecdhp384") == 0) 791 ecdh_doit[R_EC_P384] = 2; 792 else if (strcmp(*argv, "ecdhp521") == 0) 793 ecdh_doit[R_EC_P521] = 2; 794 else if (strcmp(*argv, "ecdhk163") == 0) 795 ecdh_doit[R_EC_K163] = 2; 796 else if (strcmp(*argv, "ecdhk233") == 0) 797 ecdh_doit[R_EC_K233] = 2; 798 else if (strcmp(*argv, "ecdhk283") == 0) 799 ecdh_doit[R_EC_K283] = 2; 800 else if (strcmp(*argv, "ecdhk409") == 0) 801 ecdh_doit[R_EC_K409] = 2; 802 else if (strcmp(*argv, "ecdhk571") == 0) 803 ecdh_doit[R_EC_K571] = 2; 804 else if (strcmp(*argv, "ecdhb163") == 0) 805 ecdh_doit[R_EC_B163] = 2; 806 else if (strcmp(*argv, "ecdhb233") == 0) 807 ecdh_doit[R_EC_B233] = 2; 808 else if (strcmp(*argv, "ecdhb283") == 0) 809 ecdh_doit[R_EC_B283] = 2; 810 else if (strcmp(*argv, "ecdhb409") == 0) 811 ecdh_doit[R_EC_B409] = 2; 812 else if (strcmp(*argv, "ecdhb571") == 0) 813 ecdh_doit[R_EC_B571] = 2; 814 else if (strcmp(*argv, "ecdh") == 0) { 815 for (i = 0; i < EC_NUM; i++) 816 ecdh_doit[i] = 1; 817 } else 818 { 819 BIO_printf(bio_err, "Error: bad option or value\n"); 820 BIO_printf(bio_err, "\n"); 821 BIO_printf(bio_err, "Available values:\n"); 822 #ifndef OPENSSL_NO_MD4 823 BIO_printf(bio_err, "md4 "); 824 #endif 825 #ifndef OPENSSL_NO_MD5 826 BIO_printf(bio_err, "md5 "); 827 #ifndef OPENSSL_NO_HMAC 828 BIO_printf(bio_err, "hmac "); 829 #endif 830 #endif 831 #ifndef OPENSSL_NO_SHA1 832 BIO_printf(bio_err, "sha1 "); 833 #endif 834 #ifndef OPENSSL_NO_SHA256 835 BIO_printf(bio_err, "sha256 "); 836 #endif 837 #ifndef OPENSSL_NO_SHA512 838 BIO_printf(bio_err, "sha512 "); 839 #endif 840 #ifndef OPENSSL_NO_WHIRLPOOL 841 BIO_printf(bio_err, "whirlpool"); 842 #endif 843 #ifndef OPENSSL_NO_RIPEMD160 844 BIO_printf(bio_err, "rmd160"); 845 #endif 846 #if !defined(OPENSSL_NO_MD2) || \ 847 !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \ 848 !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160) || \ 849 !defined(OPENSSL_NO_WHIRLPOOL) 850 BIO_printf(bio_err, "\n"); 851 #endif 852 853 #ifndef OPENSSL_NO_IDEA 854 BIO_printf(bio_err, "idea-cbc "); 855 #endif 856 #ifndef OPENSSL_NO_RC2 857 BIO_printf(bio_err, "rc2-cbc "); 858 #endif 859 #ifndef OPENSSL_NO_BF 860 BIO_printf(bio_err, "bf-cbc "); 861 #endif 862 #ifndef OPENSSL_NO_DES 863 BIO_printf(bio_err, "des-cbc des-ede3\n"); 864 #endif 865 #ifndef OPENSSL_NO_AES 866 BIO_printf(bio_err, "aes-128-cbc aes-192-cbc aes-256-cbc "); 867 BIO_printf(bio_err, "aes-128-ige aes-192-ige aes-256-ige\n"); 868 BIO_printf(bio_err, "aes-128-gcm aes-256-gcm "); 869 #endif 870 #ifndef OPENSSL_NO_CAMELLIA 871 BIO_printf(bio_err, "\n"); 872 BIO_printf(bio_err, "camellia-128-cbc camellia-192-cbc camellia-256-cbc "); 873 #endif 874 #ifndef OPENSSL_NO_RC4 875 BIO_printf(bio_err, "rc4"); 876 #endif 877 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) 878 BIO_printf(bio_err," chacha20-poly1305"); 879 #endif 880 BIO_printf(bio_err, "\n"); 881 882 BIO_printf(bio_err, "rsa512 rsa1024 rsa2048 rsa4096\n"); 883 884 BIO_printf(bio_err, "dsa512 dsa1024 dsa2048\n"); 885 BIO_printf(bio_err, "ecdsap160 ecdsap192 ecdsap224 ecdsap256 ecdsap384 ecdsap521\n"); 886 BIO_printf(bio_err, "ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\n"); 887 BIO_printf(bio_err, "ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571 ecdsa\n"); 888 BIO_printf(bio_err, "ecdhp160 ecdhp192 ecdhp224 ecdhp256 ecdhp384 ecdhp521\n"); 889 BIO_printf(bio_err, "ecdhk163 ecdhk233 ecdhk283 ecdhk409 ecdhk571\n"); 890 BIO_printf(bio_err, "ecdhb163 ecdhb233 ecdhb283 ecdhb409 ecdhb571 ecdh\n"); 891 892 #ifndef OPENSSL_NO_IDEA 893 BIO_printf(bio_err, "idea "); 894 #endif 895 #ifndef OPENSSL_NO_RC2 896 BIO_printf(bio_err, "rc2 "); 897 #endif 898 #ifndef OPENSSL_NO_DES 899 BIO_printf(bio_err, "des "); 900 #endif 901 #ifndef OPENSSL_NO_AES 902 BIO_printf(bio_err, "aes "); 903 #endif 904 #ifndef OPENSSL_NO_CAMELLIA 905 BIO_printf(bio_err, "camellia "); 906 #endif 907 BIO_printf(bio_err, "rsa "); 908 #ifndef OPENSSL_NO_BF 909 BIO_printf(bio_err, "blowfish"); 910 #endif 911 #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \ 912 !defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \ 913 !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \ 914 !defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA) 915 BIO_printf(bio_err, "\n"); 916 #endif 917 918 BIO_printf(bio_err, "\n"); 919 BIO_printf(bio_err, "Available options:\n"); 920 BIO_printf(bio_err, "-elapsed measure time in real time instead of CPU user time.\n"); 921 BIO_printf(bio_err, "-evp e use EVP e.\n"); 922 BIO_printf(bio_err, "-decrypt time decryption instead of encryption (only EVP).\n"); 923 BIO_printf(bio_err, "-mr produce machine readable output.\n"); 924 BIO_printf(bio_err, "-multi n run n benchmarks in parallel.\n"); 925 goto end; 926 } 927 argc--; 928 argv++; 929 j++; 930 } 931 932 if (multi && do_multi(multi)) 933 goto show_res; 934 935 if (j == 0) { 936 for (i = 0; i < ALGOR_NUM; i++) { 937 if (i != D_EVP) 938 doit[i] = 1; 939 } 940 for (i = 0; i < RSA_NUM; i++) 941 rsa_doit[i] = 1; 942 for (i = 0; i < DSA_NUM; i++) 943 dsa_doit[i] = 1; 944 for (i = 0; i < EC_NUM; i++) 945 ecdsa_doit[i] = 1; 946 for (i = 0; i < EC_NUM; i++) 947 ecdh_doit[i] = 1; 948 } 949 for (i = 0; i < ALGOR_NUM; i++) 950 if (doit[i]) 951 pr_header++; 952 953 if (usertime == 0 && !mr) 954 BIO_printf(bio_err, "You have chosen to measure elapsed time instead of user CPU time.\n"); 955 956 for (i = 0; i < RSA_NUM; i++) { 957 const unsigned char *p; 958 959 p = rsa_data[i]; 960 rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]); 961 if (rsa_key[i] == NULL) { 962 BIO_printf(bio_err, "internal error loading RSA key number %d\n", i); 963 goto end; 964 } 965 } 966 967 dsa_key[0] = get_dsa512(); 968 dsa_key[1] = get_dsa1024(); 969 dsa_key[2] = get_dsa2048(); 970 971 #ifndef OPENSSL_NO_DES 972 DES_set_key_unchecked(&key, &sch); 973 DES_set_key_unchecked(&key2, &sch2); 974 DES_set_key_unchecked(&key3, &sch3); 975 #endif 976 #ifndef OPENSSL_NO_AES 977 AES_set_encrypt_key(key16, 128, &aes_ks1); 978 AES_set_encrypt_key(key24, 192, &aes_ks2); 979 AES_set_encrypt_key(key32, 256, &aes_ks3); 980 #endif 981 #ifndef OPENSSL_NO_CAMELLIA 982 Camellia_set_key(key16, 128, &camellia_ks1); 983 Camellia_set_key(ckey24, 192, &camellia_ks2); 984 Camellia_set_key(ckey32, 256, &camellia_ks3); 985 #endif 986 #ifndef OPENSSL_NO_IDEA 987 idea_set_encrypt_key(key16, &idea_ks); 988 #endif 989 #ifndef OPENSSL_NO_RC4 990 RC4_set_key(&rc4_ks, 16, key16); 991 #endif 992 #ifndef OPENSSL_NO_RC2 993 RC2_set_key(&rc2_ks, 16, key16, 128); 994 #endif 995 #ifndef OPENSSL_NO_BF 996 BF_set_key(&bf_ks, 16, key16); 997 #endif 998 #ifndef OPENSSL_NO_CAST 999 CAST_set_key(&cast_ks, 16, key16); 1000 #endif 1001 memset(rsa_c, 0, sizeof(rsa_c)); 1002 #define COND(c) (run && count<0x7fffffff) 1003 #define COUNT(d) (count) 1004 signal(SIGALRM, sig_done); 1005 1006 #ifndef OPENSSL_NO_MD4 1007 if (doit[D_MD4]) { 1008 for (j = 0; j < SIZE_NUM; j++) { 1009 print_message(names[D_MD4], c[D_MD4][j], lengths[j]); 1010 Time_F(START); 1011 for (count = 0, run = 1; COND(c[D_MD4][j]); count++) 1012 EVP_Digest(&(buf[0]), (unsigned long) lengths[j], &(md4[0]), NULL, EVP_md4(), NULL); 1013 d = Time_F(STOP); 1014 print_result(D_MD4, j, count, d); 1015 } 1016 } 1017 #endif 1018 1019 #ifndef OPENSSL_NO_MD5 1020 if (doit[D_MD5]) { 1021 for (j = 0; j < SIZE_NUM; j++) { 1022 print_message(names[D_MD5], c[D_MD5][j], lengths[j]); 1023 Time_F(START); 1024 for (count = 0, run = 1; COND(c[D_MD5][j]); count++) 1025 EVP_Digest(&(buf[0]), (unsigned long) lengths[j], &(md5[0]), NULL, EVP_get_digestbyname("md5"), NULL); 1026 d = Time_F(STOP); 1027 print_result(D_MD5, j, count, d); 1028 } 1029 } 1030 #endif 1031 1032 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC) 1033 if (doit[D_HMAC]) { 1034 HMAC_CTX *hctx; 1035 1036 if ((hctx = HMAC_CTX_new()) == NULL) { 1037 BIO_printf(bio_err, "Failed to allocate HMAC context.\n"); 1038 goto end; 1039 } 1040 1041 HMAC_Init_ex(hctx, (unsigned char *) "This is a key...", 1042 16, EVP_md5(), NULL); 1043 1044 for (j = 0; j < SIZE_NUM; j++) { 1045 print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]); 1046 Time_F(START); 1047 for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) { 1048 if (!HMAC_Init_ex(hctx, NULL, 0, NULL, NULL)) { 1049 HMAC_CTX_free(hctx); 1050 goto end; 1051 } 1052 if (!HMAC_Update(hctx, buf, lengths[j])) { 1053 HMAC_CTX_free(hctx); 1054 goto end; 1055 } 1056 if (!HMAC_Final(hctx, &(hmac[0]), NULL)) { 1057 HMAC_CTX_free(hctx); 1058 goto end; 1059 } 1060 } 1061 d = Time_F(STOP); 1062 print_result(D_HMAC, j, count, d); 1063 } 1064 HMAC_CTX_free(hctx); 1065 } 1066 #endif 1067 #ifndef OPENSSL_NO_SHA 1068 if (doit[D_SHA1]) { 1069 for (j = 0; j < SIZE_NUM; j++) { 1070 print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]); 1071 Time_F(START); 1072 for (count = 0, run = 1; COND(c[D_SHA1][j]); count++) 1073 EVP_Digest(buf, (unsigned long) lengths[j], &(sha[0]), NULL, EVP_sha1(), NULL); 1074 d = Time_F(STOP); 1075 print_result(D_SHA1, j, count, d); 1076 } 1077 } 1078 #ifndef OPENSSL_NO_SHA256 1079 if (doit[D_SHA256]) { 1080 for (j = 0; j < SIZE_NUM; j++) { 1081 print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]); 1082 Time_F(START); 1083 for (count = 0, run = 1; COND(c[D_SHA256][j]); count++) 1084 SHA256(buf, lengths[j], sha256); 1085 d = Time_F(STOP); 1086 print_result(D_SHA256, j, count, d); 1087 } 1088 } 1089 #endif 1090 1091 #ifndef OPENSSL_NO_SHA512 1092 if (doit[D_SHA512]) { 1093 for (j = 0; j < SIZE_NUM; j++) { 1094 print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]); 1095 Time_F(START); 1096 for (count = 0, run = 1; COND(c[D_SHA512][j]); count++) 1097 SHA512(buf, lengths[j], sha512); 1098 d = Time_F(STOP); 1099 print_result(D_SHA512, j, count, d); 1100 } 1101 } 1102 #endif 1103 #endif 1104 1105 #ifndef OPENSSL_NO_WHIRLPOOL 1106 if (doit[D_WHIRLPOOL]) { 1107 for (j = 0; j < SIZE_NUM; j++) { 1108 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]); 1109 Time_F(START); 1110 for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++) 1111 WHIRLPOOL(buf, lengths[j], whirlpool); 1112 d = Time_F(STOP); 1113 print_result(D_WHIRLPOOL, j, count, d); 1114 } 1115 } 1116 #endif 1117 1118 #ifndef OPENSSL_NO_RIPEMD 1119 if (doit[D_RMD160]) { 1120 for (j = 0; j < SIZE_NUM; j++) { 1121 print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]); 1122 Time_F(START); 1123 for (count = 0, run = 1; COND(c[D_RMD160][j]); count++) 1124 EVP_Digest(buf, (unsigned long) lengths[j], &(rmd160[0]), NULL, EVP_ripemd160(), NULL); 1125 d = Time_F(STOP); 1126 print_result(D_RMD160, j, count, d); 1127 } 1128 } 1129 #endif 1130 #ifndef OPENSSL_NO_RC4 1131 if (doit[D_RC4]) { 1132 for (j = 0; j < SIZE_NUM; j++) { 1133 print_message(names[D_RC4], c[D_RC4][j], lengths[j]); 1134 Time_F(START); 1135 for (count = 0, run = 1; COND(c[D_RC4][j]); count++) 1136 RC4(&rc4_ks, (unsigned int) lengths[j], 1137 buf, buf); 1138 d = Time_F(STOP); 1139 print_result(D_RC4, j, count, d); 1140 } 1141 } 1142 #endif 1143 #ifndef OPENSSL_NO_DES 1144 if (doit[D_CBC_DES]) { 1145 for (j = 0; j < SIZE_NUM; j++) { 1146 print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]); 1147 Time_F(START); 1148 for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++) 1149 DES_ncbc_encrypt(buf, buf, lengths[j], &sch, 1150 &DES_iv, DES_ENCRYPT); 1151 d = Time_F(STOP); 1152 print_result(D_CBC_DES, j, count, d); 1153 } 1154 } 1155 if (doit[D_EDE3_DES]) { 1156 for (j = 0; j < SIZE_NUM; j++) { 1157 print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]); 1158 Time_F(START); 1159 for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++) 1160 DES_ede3_cbc_encrypt(buf, buf, lengths[j], 1161 &sch, &sch2, &sch3, 1162 &DES_iv, DES_ENCRYPT); 1163 d = Time_F(STOP); 1164 print_result(D_EDE3_DES, j, count, d); 1165 } 1166 } 1167 #endif 1168 #ifndef OPENSSL_NO_AES 1169 if (doit[D_CBC_128_AES]) { 1170 for (j = 0; j < SIZE_NUM; j++) { 1171 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j], lengths[j]); 1172 Time_F(START); 1173 for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++) 1174 AES_cbc_encrypt(buf, buf, 1175 (unsigned long) lengths[j], &aes_ks1, 1176 iv, AES_ENCRYPT); 1177 d = Time_F(STOP); 1178 print_result(D_CBC_128_AES, j, count, d); 1179 } 1180 } 1181 if (doit[D_CBC_192_AES]) { 1182 for (j = 0; j < SIZE_NUM; j++) { 1183 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j], lengths[j]); 1184 Time_F(START); 1185 for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++) 1186 AES_cbc_encrypt(buf, buf, 1187 (unsigned long) lengths[j], &aes_ks2, 1188 iv, AES_ENCRYPT); 1189 d = Time_F(STOP); 1190 print_result(D_CBC_192_AES, j, count, d); 1191 } 1192 } 1193 if (doit[D_CBC_256_AES]) { 1194 for (j = 0; j < SIZE_NUM; j++) { 1195 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j], lengths[j]); 1196 Time_F(START); 1197 for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++) 1198 AES_cbc_encrypt(buf, buf, 1199 (unsigned long) lengths[j], &aes_ks3, 1200 iv, AES_ENCRYPT); 1201 d = Time_F(STOP); 1202 print_result(D_CBC_256_AES, j, count, d); 1203 } 1204 } 1205 if (doit[D_IGE_128_AES]) { 1206 for (j = 0; j < SIZE_NUM; j++) { 1207 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j], lengths[j]); 1208 Time_F(START); 1209 for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++) 1210 AES_ige_encrypt(buf, buf2, 1211 (unsigned long) lengths[j], &aes_ks1, 1212 iv, AES_ENCRYPT); 1213 d = Time_F(STOP); 1214 print_result(D_IGE_128_AES, j, count, d); 1215 } 1216 } 1217 if (doit[D_IGE_192_AES]) { 1218 for (j = 0; j < SIZE_NUM; j++) { 1219 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j], lengths[j]); 1220 Time_F(START); 1221 for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++) 1222 AES_ige_encrypt(buf, buf2, 1223 (unsigned long) lengths[j], &aes_ks2, 1224 iv, AES_ENCRYPT); 1225 d = Time_F(STOP); 1226 print_result(D_IGE_192_AES, j, count, d); 1227 } 1228 } 1229 if (doit[D_IGE_256_AES]) { 1230 for (j = 0; j < SIZE_NUM; j++) { 1231 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j], lengths[j]); 1232 Time_F(START); 1233 for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++) 1234 AES_ige_encrypt(buf, buf2, 1235 (unsigned long) lengths[j], &aes_ks3, 1236 iv, AES_ENCRYPT); 1237 d = Time_F(STOP); 1238 print_result(D_IGE_256_AES, j, count, d); 1239 } 1240 } 1241 if (doit[D_GHASH]) { 1242 GCM128_CONTEXT *ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt); 1243 CRYPTO_gcm128_setiv(ctx, (unsigned char *) "0123456789ab", 12); 1244 1245 for (j = 0; j < SIZE_NUM; j++) { 1246 print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]); 1247 Time_F(START); 1248 for (count = 0, run = 1; COND(c[D_GHASH][j]); count++) 1249 CRYPTO_gcm128_aad(ctx, buf, lengths[j]); 1250 d = Time_F(STOP); 1251 print_result(D_GHASH, j, count, d); 1252 } 1253 CRYPTO_gcm128_release(ctx); 1254 } 1255 if (doit[D_AES_128_GCM]) { 1256 const EVP_AEAD *aead = EVP_aead_aes_128_gcm(); 1257 static const unsigned char nonce[32] = {0}; 1258 size_t buf_len, nonce_len; 1259 EVP_AEAD_CTX *ctx; 1260 1261 if ((ctx = EVP_AEAD_CTX_new()) == NULL) { 1262 BIO_printf(bio_err, 1263 "Failed to allocate aead context.\n"); 1264 goto end; 1265 } 1266 1267 EVP_AEAD_CTX_init(ctx, aead, key32, EVP_AEAD_key_length(aead), 1268 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL); 1269 nonce_len = EVP_AEAD_nonce_length(aead); 1270 1271 for (j = 0; j < SIZE_NUM; j++) { 1272 print_message(names[D_AES_128_GCM],c[D_AES_128_GCM][j],lengths[j]); 1273 Time_F(START); 1274 for (count = 0, run = 1; COND(c[D_AES_128_GCM][j]); count++) 1275 EVP_AEAD_CTX_seal(ctx, buf, &buf_len, BUFSIZE, nonce, 1276 nonce_len, buf, lengths[j], NULL, 0); 1277 d=Time_F(STOP); 1278 print_result(D_AES_128_GCM,j,count,d); 1279 } 1280 EVP_AEAD_CTX_free(ctx); 1281 } 1282 1283 if (doit[D_AES_256_GCM]) { 1284 const EVP_AEAD *aead = EVP_aead_aes_256_gcm(); 1285 static const unsigned char nonce[32] = {0}; 1286 size_t buf_len, nonce_len; 1287 EVP_AEAD_CTX *ctx; 1288 1289 if ((ctx = EVP_AEAD_CTX_new()) == NULL) { 1290 BIO_printf(bio_err, 1291 "Failed to allocate aead context.\n"); 1292 goto end; 1293 } 1294 1295 EVP_AEAD_CTX_init(ctx, aead, key32, EVP_AEAD_key_length(aead), 1296 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL); 1297 nonce_len = EVP_AEAD_nonce_length(aead); 1298 1299 for (j = 0; j < SIZE_NUM; j++) { 1300 print_message(names[D_AES_256_GCM],c[D_AES_256_GCM][j],lengths[j]); 1301 Time_F(START); 1302 for (count = 0, run = 1; COND(c[D_AES_256_GCM][j]); count++) 1303 EVP_AEAD_CTX_seal(ctx, buf, &buf_len, BUFSIZE, nonce, 1304 nonce_len, buf, lengths[j], NULL, 0); 1305 d=Time_F(STOP); 1306 print_result(D_AES_256_GCM, j, count, d); 1307 } 1308 EVP_AEAD_CTX_free(ctx); 1309 } 1310 #endif 1311 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) 1312 if (doit[D_CHACHA20_POLY1305]) { 1313 const EVP_AEAD *aead = EVP_aead_chacha20_poly1305(); 1314 static const unsigned char nonce[32] = {0}; 1315 size_t buf_len, nonce_len; 1316 EVP_AEAD_CTX *ctx; 1317 1318 if ((ctx = EVP_AEAD_CTX_new()) == NULL) { 1319 BIO_printf(bio_err, 1320 "Failed to allocate aead context.\n"); 1321 goto end; 1322 } 1323 1324 EVP_AEAD_CTX_init(ctx, aead, key32, EVP_AEAD_key_length(aead), 1325 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL); 1326 nonce_len = EVP_AEAD_nonce_length(aead); 1327 1328 for (j = 0; j < SIZE_NUM; j++) { 1329 print_message(names[D_CHACHA20_POLY1305], 1330 c[D_CHACHA20_POLY1305][j], lengths[j]); 1331 Time_F(START); 1332 for (count = 0, run = 1; COND(c[D_CHACHA20_POLY1305][j]); count++) 1333 EVP_AEAD_CTX_seal(ctx, buf, &buf_len, BUFSIZE, nonce, 1334 nonce_len, buf, lengths[j], NULL, 0); 1335 d=Time_F(STOP); 1336 print_result(D_CHACHA20_POLY1305, j, count, d); 1337 } 1338 EVP_AEAD_CTX_free(ctx); 1339 } 1340 #endif 1341 #ifndef OPENSSL_NO_CAMELLIA 1342 if (doit[D_CBC_128_CML]) { 1343 for (j = 0; j < SIZE_NUM; j++) { 1344 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j], lengths[j]); 1345 Time_F(START); 1346 for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++) 1347 Camellia_cbc_encrypt(buf, buf, 1348 (unsigned long) lengths[j], &camellia_ks1, 1349 iv, CAMELLIA_ENCRYPT); 1350 d = Time_F(STOP); 1351 print_result(D_CBC_128_CML, j, count, d); 1352 } 1353 } 1354 if (doit[D_CBC_192_CML]) { 1355 for (j = 0; j < SIZE_NUM; j++) { 1356 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j], lengths[j]); 1357 Time_F(START); 1358 for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++) 1359 Camellia_cbc_encrypt(buf, buf, 1360 (unsigned long) lengths[j], &camellia_ks2, 1361 iv, CAMELLIA_ENCRYPT); 1362 d = Time_F(STOP); 1363 print_result(D_CBC_192_CML, j, count, d); 1364 } 1365 } 1366 if (doit[D_CBC_256_CML]) { 1367 for (j = 0; j < SIZE_NUM; j++) { 1368 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j], lengths[j]); 1369 Time_F(START); 1370 for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++) 1371 Camellia_cbc_encrypt(buf, buf, 1372 (unsigned long) lengths[j], &camellia_ks3, 1373 iv, CAMELLIA_ENCRYPT); 1374 d = Time_F(STOP); 1375 print_result(D_CBC_256_CML, j, count, d); 1376 } 1377 } 1378 #endif 1379 #ifndef OPENSSL_NO_IDEA 1380 if (doit[D_CBC_IDEA]) { 1381 for (j = 0; j < SIZE_NUM; j++) { 1382 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]); 1383 Time_F(START); 1384 for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++) 1385 idea_cbc_encrypt(buf, buf, 1386 (unsigned long) lengths[j], &idea_ks, 1387 iv, IDEA_ENCRYPT); 1388 d = Time_F(STOP); 1389 print_result(D_CBC_IDEA, j, count, d); 1390 } 1391 } 1392 #endif 1393 #ifndef OPENSSL_NO_RC2 1394 if (doit[D_CBC_RC2]) { 1395 for (j = 0; j < SIZE_NUM; j++) { 1396 print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]); 1397 Time_F(START); 1398 for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++) 1399 RC2_cbc_encrypt(buf, buf, 1400 (unsigned long) lengths[j], &rc2_ks, 1401 iv, RC2_ENCRYPT); 1402 d = Time_F(STOP); 1403 print_result(D_CBC_RC2, j, count, d); 1404 } 1405 } 1406 #endif 1407 #ifndef OPENSSL_NO_BF 1408 if (doit[D_CBC_BF]) { 1409 for (j = 0; j < SIZE_NUM; j++) { 1410 print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]); 1411 Time_F(START); 1412 for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++) 1413 BF_cbc_encrypt(buf, buf, 1414 (unsigned long) lengths[j], &bf_ks, 1415 iv, BF_ENCRYPT); 1416 d = Time_F(STOP); 1417 print_result(D_CBC_BF, j, count, d); 1418 } 1419 } 1420 #endif 1421 #ifndef OPENSSL_NO_CAST 1422 if (doit[D_CBC_CAST]) { 1423 for (j = 0; j < SIZE_NUM; j++) { 1424 print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]); 1425 Time_F(START); 1426 for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++) 1427 CAST_cbc_encrypt(buf, buf, 1428 (unsigned long) lengths[j], &cast_ks, 1429 iv, CAST_ENCRYPT); 1430 d = Time_F(STOP); 1431 print_result(D_CBC_CAST, j, count, d); 1432 } 1433 } 1434 #endif 1435 1436 if (doit[D_EVP]) { 1437 for (j = 0; j < SIZE_NUM; j++) { 1438 if (evp_cipher) { 1439 EVP_CIPHER_CTX *ctx; 1440 int outl; 1441 1442 names[D_EVP] = 1443 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)); 1444 /* 1445 * -O3 -fschedule-insns messes up an 1446 * optimization here! names[D_EVP] somehow 1447 * becomes NULL 1448 */ 1449 print_message(names[D_EVP], save_count, 1450 lengths[j]); 1451 1452 if ((ctx = EVP_CIPHER_CTX_new()) == NULL) { 1453 BIO_printf(bio_err, "Failed to " 1454 "allocate cipher context.\n"); 1455 goto end; 1456 } 1457 if (decrypt) 1458 EVP_DecryptInit_ex(ctx, evp_cipher, NULL, key16, iv); 1459 else 1460 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, key16, iv); 1461 EVP_CIPHER_CTX_set_padding(ctx, 0); 1462 1463 Time_F(START); 1464 if (decrypt) 1465 for (count = 0, run = 1; COND(save_count * 4 * lengths[0] / lengths[j]); count++) 1466 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[j]); 1467 else 1468 for (count = 0, run = 1; COND(save_count * 4 * lengths[0] / lengths[j]); count++) 1469 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[j]); 1470 if (decrypt) 1471 EVP_DecryptFinal_ex(ctx, buf, &outl); 1472 else 1473 EVP_EncryptFinal_ex(ctx, buf, &outl); 1474 d = Time_F(STOP); 1475 EVP_CIPHER_CTX_free(ctx); 1476 } 1477 if (evp_md) { 1478 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md)); 1479 print_message(names[D_EVP], save_count, 1480 lengths[j]); 1481 1482 Time_F(START); 1483 for (count = 0, run = 1; COND(save_count * 4 * lengths[0] / lengths[j]); count++) 1484 EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL); 1485 1486 d = Time_F(STOP); 1487 } 1488 print_result(D_EVP, j, count, d); 1489 } 1490 } 1491 arc4random_buf(buf, 36); 1492 for (j = 0; j < RSA_NUM; j++) { 1493 int ret; 1494 if (!rsa_doit[j]) 1495 continue; 1496 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]); 1497 if (ret == 0) { 1498 BIO_printf(bio_err, "RSA sign failure. No RSA sign will be done.\n"); 1499 ERR_print_errors(bio_err); 1500 rsa_count = 1; 1501 } else { 1502 pkey_print_message("private", "rsa", 1503 rsa_c[j][0], rsa_bits[j], 1504 RSA_SECONDS); 1505 /* RSA_blinding_on(rsa_key[j],NULL); */ 1506 Time_F(START); 1507 for (count = 0, run = 1; COND(rsa_c[j][0]); count++) { 1508 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, 1509 &rsa_num, rsa_key[j]); 1510 if (ret == 0) { 1511 BIO_printf(bio_err, 1512 "RSA sign failure\n"); 1513 ERR_print_errors(bio_err); 1514 count = 1; 1515 break; 1516 } 1517 } 1518 d = Time_F(STOP); 1519 BIO_printf(bio_err, mr ? "+R1:%ld:%d:%.2f\n" 1520 : "%ld %d bit private RSA's in %.2fs\n", 1521 count, rsa_bits[j], d); 1522 rsa_results[j][0] = d / (double) count; 1523 rsa_count = count; 1524 } 1525 1526 ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]); 1527 if (ret <= 0) { 1528 BIO_printf(bio_err, "RSA verify failure. No RSA verify will be done.\n"); 1529 ERR_print_errors(bio_err); 1530 rsa_doit[j] = 0; 1531 } else { 1532 pkey_print_message("public", "rsa", 1533 rsa_c[j][1], rsa_bits[j], 1534 RSA_SECONDS); 1535 Time_F(START); 1536 for (count = 0, run = 1; COND(rsa_c[j][1]); count++) { 1537 ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, 1538 rsa_num, rsa_key[j]); 1539 if (ret <= 0) { 1540 BIO_printf(bio_err, 1541 "RSA verify failure\n"); 1542 ERR_print_errors(bio_err); 1543 count = 1; 1544 break; 1545 } 1546 } 1547 d = Time_F(STOP); 1548 BIO_printf(bio_err, mr ? "+R2:%ld:%d:%.2f\n" 1549 : "%ld %d bit public RSA's in %.2fs\n", 1550 count, rsa_bits[j], d); 1551 rsa_results[j][1] = d / (double) count; 1552 } 1553 1554 if (rsa_count <= 1) { 1555 /* if longer than 10s, don't do any more */ 1556 for (j++; j < RSA_NUM; j++) 1557 rsa_doit[j] = 0; 1558 } 1559 } 1560 1561 arc4random_buf(buf, 20); 1562 for (j = 0; j < DSA_NUM; j++) { 1563 unsigned int kk; 1564 int ret; 1565 1566 if (!dsa_doit[j]) 1567 continue; 1568 /* DSA_generate_key(dsa_key[j]); */ 1569 /* DSA_sign_setup(dsa_key[j],NULL); */ 1570 ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, 1571 &kk, dsa_key[j]); 1572 if (ret == 0) { 1573 BIO_printf(bio_err, "DSA sign failure. No DSA sign will be done.\n"); 1574 ERR_print_errors(bio_err); 1575 rsa_count = 1; 1576 } else { 1577 pkey_print_message("sign", "dsa", 1578 dsa_c[j][0], dsa_bits[j], 1579 DSA_SECONDS); 1580 Time_F(START); 1581 for (count = 0, run = 1; COND(dsa_c[j][0]); count++) { 1582 ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, 1583 &kk, dsa_key[j]); 1584 if (ret == 0) { 1585 BIO_printf(bio_err, 1586 "DSA sign failure\n"); 1587 ERR_print_errors(bio_err); 1588 count = 1; 1589 break; 1590 } 1591 } 1592 d = Time_F(STOP); 1593 BIO_printf(bio_err, mr ? "+R3:%ld:%d:%.2f\n" 1594 : "%ld %d bit DSA signs in %.2fs\n", 1595 count, dsa_bits[j], d); 1596 dsa_results[j][0] = d / (double) count; 1597 rsa_count = count; 1598 } 1599 1600 ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, 1601 kk, dsa_key[j]); 1602 if (ret <= 0) { 1603 BIO_printf(bio_err, "DSA verify failure. No DSA verify will be done.\n"); 1604 ERR_print_errors(bio_err); 1605 dsa_doit[j] = 0; 1606 } else { 1607 pkey_print_message("verify", "dsa", 1608 dsa_c[j][1], dsa_bits[j], 1609 DSA_SECONDS); 1610 Time_F(START); 1611 for (count = 0, run = 1; COND(dsa_c[j][1]); count++) { 1612 ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, 1613 kk, dsa_key[j]); 1614 if (ret <= 0) { 1615 BIO_printf(bio_err, 1616 "DSA verify failure\n"); 1617 ERR_print_errors(bio_err); 1618 count = 1; 1619 break; 1620 } 1621 } 1622 d = Time_F(STOP); 1623 BIO_printf(bio_err, mr ? "+R4:%ld:%d:%.2f\n" 1624 : "%ld %d bit DSA verify in %.2fs\n", 1625 count, dsa_bits[j], d); 1626 dsa_results[j][1] = d / (double) count; 1627 } 1628 1629 if (rsa_count <= 1) { 1630 /* if longer than 10s, don't do any more */ 1631 for (j++; j < DSA_NUM; j++) 1632 dsa_doit[j] = 0; 1633 } 1634 } 1635 1636 for (j = 0; j < EC_NUM; j++) { 1637 int ret; 1638 1639 if (!ecdsa_doit[j]) 1640 continue; /* Ignore Curve */ 1641 ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]); 1642 if (ecdsa[j] == NULL) { 1643 BIO_printf(bio_err, "ECDSA failure.\n"); 1644 ERR_print_errors(bio_err); 1645 rsa_count = 1; 1646 } else { 1647 EC_KEY_precompute_mult(ecdsa[j], NULL); 1648 1649 /* Perform ECDSA signature test */ 1650 EC_KEY_generate_key(ecdsa[j]); 1651 ret = ECDSA_sign(0, buf, 20, ecdsasig, 1652 &ecdsasiglen, ecdsa[j]); 1653 if (ret == 0) { 1654 BIO_printf(bio_err, "ECDSA sign failure. No ECDSA sign will be done.\n"); 1655 ERR_print_errors(bio_err); 1656 rsa_count = 1; 1657 } else { 1658 pkey_print_message("sign", "ecdsa", 1659 ecdsa_c[j][0], 1660 test_curves_bits[j], 1661 ECDSA_SECONDS); 1662 1663 Time_F(START); 1664 for (count = 0, run = 1; COND(ecdsa_c[j][0]); 1665 count++) { 1666 ret = ECDSA_sign(0, buf, 20, 1667 ecdsasig, &ecdsasiglen, 1668 ecdsa[j]); 1669 if (ret == 0) { 1670 BIO_printf(bio_err, "ECDSA sign failure\n"); 1671 ERR_print_errors(bio_err); 1672 count = 1; 1673 break; 1674 } 1675 } 1676 d = Time_F(STOP); 1677 1678 BIO_printf(bio_err, mr ? "+R5:%ld:%d:%.2f\n" : 1679 "%ld %d bit ECDSA signs in %.2fs \n", 1680 count, test_curves_bits[j], d); 1681 ecdsa_results[j][0] = d / (double) count; 1682 rsa_count = count; 1683 } 1684 1685 /* Perform ECDSA verification test */ 1686 ret = ECDSA_verify(0, buf, 20, ecdsasig, 1687 ecdsasiglen, ecdsa[j]); 1688 if (ret != 1) { 1689 BIO_printf(bio_err, "ECDSA verify failure. No ECDSA verify will be done.\n"); 1690 ERR_print_errors(bio_err); 1691 ecdsa_doit[j] = 0; 1692 } else { 1693 pkey_print_message("verify", "ecdsa", 1694 ecdsa_c[j][1], 1695 test_curves_bits[j], 1696 ECDSA_SECONDS); 1697 Time_F(START); 1698 for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) { 1699 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]); 1700 if (ret != 1) { 1701 BIO_printf(bio_err, "ECDSA verify failure\n"); 1702 ERR_print_errors(bio_err); 1703 count = 1; 1704 break; 1705 } 1706 } 1707 d = Time_F(STOP); 1708 BIO_printf(bio_err, mr ? "+R6:%ld:%d:%.2f\n" 1709 : "%ld %d bit ECDSA verify in %.2fs\n", 1710 count, test_curves_bits[j], d); 1711 ecdsa_results[j][1] = d / (double) count; 1712 } 1713 1714 if (rsa_count <= 1) { 1715 /* if longer than 10s, don't do any more */ 1716 for (j++; j < EC_NUM; j++) 1717 ecdsa_doit[j] = 0; 1718 } 1719 } 1720 } 1721 1722 for (j = 0; j < EC_NUM; j++) { 1723 if (!ecdh_doit[j]) 1724 continue; 1725 ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]); 1726 ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]); 1727 if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) { 1728 BIO_printf(bio_err, "ECDH failure.\n"); 1729 ERR_print_errors(bio_err); 1730 rsa_count = 1; 1731 } else { 1732 /* generate two ECDH key pairs */ 1733 if (!EC_KEY_generate_key(ecdh_a[j]) || 1734 !EC_KEY_generate_key(ecdh_b[j])) { 1735 BIO_printf(bio_err, "ECDH key generation failure.\n"); 1736 ERR_print_errors(bio_err); 1737 rsa_count = 1; 1738 } else { 1739 /* 1740 * If field size is not more than 24 octets, 1741 * then use SHA-1 hash of result; otherwise, 1742 * use result (see section 4.8 of 1743 * draft-ietf-tls-ecc-03.txt). 1744 */ 1745 int field_size, outlen; 1746 void *(*kdf) (const void *in, size_t inlen, void *out, size_t * xoutlen); 1747 field_size = EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j])); 1748 if (field_size <= 24 * 8) { 1749 outlen = KDF1_SHA1_len; 1750 kdf = KDF1_SHA1; 1751 } else { 1752 outlen = (field_size + 7) / 8; 1753 kdf = NULL; 1754 } 1755 secret_size_a = ECDH_compute_key(secret_a, outlen, 1756 EC_KEY_get0_public_key(ecdh_b[j]), 1757 ecdh_a[j], kdf); 1758 secret_size_b = ECDH_compute_key(secret_b, outlen, 1759 EC_KEY_get0_public_key(ecdh_a[j]), 1760 ecdh_b[j], kdf); 1761 if (secret_size_a != secret_size_b) 1762 ecdh_checks = 0; 1763 else 1764 ecdh_checks = 1; 1765 1766 for (secret_idx = 0; 1767 (secret_idx < secret_size_a) 1768 && (ecdh_checks == 1); 1769 secret_idx++) { 1770 if (secret_a[secret_idx] != secret_b[secret_idx]) 1771 ecdh_checks = 0; 1772 } 1773 1774 if (ecdh_checks == 0) { 1775 BIO_printf(bio_err, 1776 "ECDH computations don't match.\n"); 1777 ERR_print_errors(bio_err); 1778 rsa_count = 1; 1779 } else { 1780 pkey_print_message("", "ecdh", 1781 ecdh_c[j][0], 1782 test_curves_bits[j], 1783 ECDH_SECONDS); 1784 Time_F(START); 1785 for (count = 0, run = 1; 1786 COND(ecdh_c[j][0]); count++) { 1787 ECDH_compute_key(secret_a, 1788 outlen, 1789 EC_KEY_get0_public_key(ecdh_b[j]), 1790 ecdh_a[j], kdf); 1791 } 1792 d = Time_F(STOP); 1793 BIO_printf(bio_err, mr 1794 ? "+R7:%ld:%d:%.2f\n" 1795 : "%ld %d-bit ECDH ops in %.2fs\n", 1796 count, test_curves_bits[j], d); 1797 ecdh_results[j][0] = d / (double) count; 1798 rsa_count = count; 1799 } 1800 } 1801 } 1802 1803 1804 if (rsa_count <= 1) { 1805 /* if longer than 10s, don't do any more */ 1806 for (j++; j < EC_NUM; j++) 1807 ecdh_doit[j] = 0; 1808 } 1809 } 1810 show_res: 1811 if (!mr) { 1812 fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_VERSION)); 1813 fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_BUILT_ON)); 1814 printf("options:"); 1815 printf("%s ", BN_options()); 1816 #ifndef OPENSSL_NO_RC4 1817 printf("%s ", RC4_options()); 1818 #endif 1819 #ifndef OPENSSL_NO_DES 1820 printf("%s ", DES_options()); 1821 #endif 1822 #ifndef OPENSSL_NO_AES 1823 printf("%s ", AES_options()); 1824 #endif 1825 #ifndef OPENSSL_NO_IDEA 1826 printf("%s ", idea_options()); 1827 #endif 1828 #ifndef OPENSSL_NO_BF 1829 printf("%s ", BF_options()); 1830 #endif 1831 fprintf(stdout, "\n%s\n", SSLeay_version(SSLEAY_CFLAGS)); 1832 } 1833 if (pr_header) { 1834 if (mr) 1835 fprintf(stdout, "+H"); 1836 else { 1837 fprintf(stdout, "The 'numbers' are in 1000s of bytes per second processed.\n"); 1838 fprintf(stdout, "type "); 1839 } 1840 for (j = 0; j < SIZE_NUM; j++) 1841 fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]); 1842 fprintf(stdout, "\n"); 1843 } 1844 for (k = 0; k < ALGOR_NUM; k++) { 1845 if (!doit[k]) 1846 continue; 1847 if (mr) 1848 fprintf(stdout, "+F:%d:%s", k, names[k]); 1849 else 1850 fprintf(stdout, "%-13s", names[k]); 1851 for (j = 0; j < SIZE_NUM; j++) { 1852 if (results[k][j] > 10000 && !mr) 1853 fprintf(stdout, " %11.2fk", results[k][j] / 1e3); 1854 else 1855 fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]); 1856 } 1857 fprintf(stdout, "\n"); 1858 } 1859 j = 1; 1860 for (k = 0; k < RSA_NUM; k++) { 1861 if (!rsa_doit[k]) 1862 continue; 1863 if (j && !mr) { 1864 printf("%18ssign verify sign/s verify/s\n", " "); 1865 j = 0; 1866 } 1867 if (mr) 1868 fprintf(stdout, "+F2:%u:%u:%f:%f\n", 1869 k, rsa_bits[k], rsa_results[k][0], 1870 rsa_results[k][1]); 1871 else 1872 fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n", 1873 rsa_bits[k], rsa_results[k][0], rsa_results[k][1], 1874 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]); 1875 } 1876 j = 1; 1877 for (k = 0; k < DSA_NUM; k++) { 1878 if (!dsa_doit[k]) 1879 continue; 1880 if (j && !mr) { 1881 printf("%18ssign verify sign/s verify/s\n", " "); 1882 j = 0; 1883 } 1884 if (mr) 1885 fprintf(stdout, "+F3:%u:%u:%f:%f\n", 1886 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]); 1887 else 1888 fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n", 1889 dsa_bits[k], dsa_results[k][0], dsa_results[k][1], 1890 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]); 1891 } 1892 j = 1; 1893 for (k = 0; k < EC_NUM; k++) { 1894 if (!ecdsa_doit[k]) 1895 continue; 1896 if (j && !mr) { 1897 printf("%30ssign verify sign/s verify/s\n", " "); 1898 j = 0; 1899 } 1900 if (mr) 1901 fprintf(stdout, "+F4:%u:%u:%f:%f\n", 1902 k, test_curves_bits[k], 1903 ecdsa_results[k][0], ecdsa_results[k][1]); 1904 else 1905 fprintf(stdout, 1906 "%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n", 1907 test_curves_bits[k], 1908 test_curves_names[k], 1909 ecdsa_results[k][0], ecdsa_results[k][1], 1910 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]); 1911 } 1912 1913 1914 j = 1; 1915 for (k = 0; k < EC_NUM; k++) { 1916 if (!ecdh_doit[k]) 1917 continue; 1918 if (j && !mr) { 1919 printf("%30sop op/s\n", " "); 1920 j = 0; 1921 } 1922 if (mr) 1923 fprintf(stdout, "+F5:%u:%u:%f:%f\n", 1924 k, test_curves_bits[k], 1925 ecdh_results[k][0], 1.0 / ecdh_results[k][0]); 1926 1927 else 1928 fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\n", 1929 test_curves_bits[k], 1930 test_curves_names[k], 1931 ecdh_results[k][0], 1.0 / ecdh_results[k][0]); 1932 } 1933 1934 mret = 0; 1935 1936 end: 1937 ERR_print_errors(bio_err); 1938 free(buf); 1939 free(buf2); 1940 for (i = 0; i < RSA_NUM; i++) 1941 if (rsa_key[i] != NULL) 1942 RSA_free(rsa_key[i]); 1943 for (i = 0; i < DSA_NUM; i++) 1944 if (dsa_key[i] != NULL) 1945 DSA_free(dsa_key[i]); 1946 1947 for (i = 0; i < EC_NUM; i++) 1948 if (ecdsa[i] != NULL) 1949 EC_KEY_free(ecdsa[i]); 1950 for (i = 0; i < EC_NUM; i++) { 1951 if (ecdh_a[i] != NULL) 1952 EC_KEY_free(ecdh_a[i]); 1953 if (ecdh_b[i] != NULL) 1954 EC_KEY_free(ecdh_b[i]); 1955 } 1956 1957 1958 return (mret); 1959 } 1960 1961 static void 1962 print_message(const char *s, long num, int length) 1963 { 1964 BIO_printf(bio_err, mr ? "+DT:%s:%d:%d\n" 1965 : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length); 1966 (void) BIO_flush(bio_err); 1967 alarm(SECONDS); 1968 } 1969 1970 static void 1971 pkey_print_message(const char *str, const char *str2, long num, 1972 int bits, int tm) 1973 { 1974 BIO_printf(bio_err, mr ? "+DTP:%d:%s:%s:%d\n" 1975 : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm); 1976 (void) BIO_flush(bio_err); 1977 alarm(tm); 1978 } 1979 1980 static void 1981 print_result(int alg, int run_no, int count, double time_used) 1982 { 1983 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n" 1984 : "%d %s's in %.2fs\n", count, names[alg], time_used); 1985 results[alg][run_no] = ((double) count) / time_used * lengths[run_no]; 1986 } 1987 1988 static char * 1989 sstrsep(char **string, const char *delim) 1990 { 1991 char isdelim[256]; 1992 char *token = *string; 1993 1994 if (**string == 0) 1995 return NULL; 1996 1997 memset(isdelim, 0, sizeof isdelim); 1998 isdelim[0] = 1; 1999 2000 while (*delim) { 2001 isdelim[(unsigned char) (*delim)] = 1; 2002 delim++; 2003 } 2004 2005 while (!isdelim[(unsigned char) (**string)]) { 2006 (*string)++; 2007 } 2008 2009 if (**string) { 2010 **string = 0; 2011 (*string)++; 2012 } 2013 return token; 2014 } 2015 2016 static int 2017 do_multi(int multi) 2018 { 2019 int n; 2020 int fd[2]; 2021 int *fds; 2022 static char sep[] = ":"; 2023 const char *errstr = NULL; 2024 2025 fds = reallocarray(NULL, multi, sizeof *fds); 2026 if (fds == NULL) { 2027 fprintf(stderr, "reallocarray failure\n"); 2028 exit(1); 2029 } 2030 for (n = 0; n < multi; ++n) { 2031 if (pipe(fd) == -1) { 2032 fprintf(stderr, "pipe failure\n"); 2033 exit(1); 2034 } 2035 fflush(stdout); 2036 fflush(stderr); 2037 if (fork()) { 2038 close(fd[1]); 2039 fds[n] = fd[0]; 2040 } else { 2041 close(fd[0]); 2042 close(1); 2043 if (dup(fd[1]) == -1) { 2044 fprintf(stderr, "dup failed\n"); 2045 exit(1); 2046 } 2047 close(fd[1]); 2048 mr = 1; 2049 usertime = 0; 2050 free(fds); 2051 return 0; 2052 } 2053 printf("Forked child %d\n", n); 2054 } 2055 2056 /* for now, assume the pipe is long enough to take all the output */ 2057 for (n = 0; n < multi; ++n) { 2058 FILE *f; 2059 char buf[1024]; 2060 char *p; 2061 2062 f = fdopen(fds[n], "r"); 2063 while (fgets(buf, sizeof buf, f)) { 2064 p = strchr(buf, '\n'); 2065 if (p) 2066 *p = '\0'; 2067 if (buf[0] != '+') { 2068 fprintf(stderr, "Don't understand line '%s' from child %d\n", 2069 buf, n); 2070 continue; 2071 } 2072 printf("Got: %s from %d\n", buf, n); 2073 if (!strncmp(buf, "+F:", 3)) { 2074 int alg; 2075 int j; 2076 2077 p = buf + 3; 2078 alg = strtonum(sstrsep(&p, sep), 2079 0, ALGOR_NUM - 1, &errstr); 2080 sstrsep(&p, sep); 2081 for (j = 0; j < SIZE_NUM; ++j) 2082 results[alg][j] += atof(sstrsep(&p, sep)); 2083 } else if (!strncmp(buf, "+F2:", 4)) { 2084 int k; 2085 double d; 2086 2087 p = buf + 4; 2088 k = strtonum(sstrsep(&p, sep), 2089 0, ALGOR_NUM - 1, &errstr); 2090 sstrsep(&p, sep); 2091 2092 d = atof(sstrsep(&p, sep)); 2093 if (n) 2094 rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d); 2095 else 2096 rsa_results[k][0] = d; 2097 2098 d = atof(sstrsep(&p, sep)); 2099 if (n) 2100 rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d); 2101 else 2102 rsa_results[k][1] = d; 2103 } else if (!strncmp(buf, "+F2:", 4)) { 2104 int k; 2105 double d; 2106 2107 p = buf + 4; 2108 k = strtonum(sstrsep(&p, sep), 2109 0, ALGOR_NUM - 1, &errstr); 2110 sstrsep(&p, sep); 2111 2112 d = atof(sstrsep(&p, sep)); 2113 if (n) 2114 rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d); 2115 else 2116 rsa_results[k][0] = d; 2117 2118 d = atof(sstrsep(&p, sep)); 2119 if (n) 2120 rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d); 2121 else 2122 rsa_results[k][1] = d; 2123 } 2124 else if (!strncmp(buf, "+F3:", 4)) { 2125 int k; 2126 double d; 2127 2128 p = buf + 4; 2129 k = strtonum(sstrsep(&p, sep), 2130 0, ALGOR_NUM - 1, &errstr); 2131 sstrsep(&p, sep); 2132 2133 d = atof(sstrsep(&p, sep)); 2134 if (n) 2135 dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d); 2136 else 2137 dsa_results[k][0] = d; 2138 2139 d = atof(sstrsep(&p, sep)); 2140 if (n) 2141 dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d); 2142 else 2143 dsa_results[k][1] = d; 2144 } 2145 else if (!strncmp(buf, "+F4:", 4)) { 2146 int k; 2147 double d; 2148 2149 p = buf + 4; 2150 k = strtonum(sstrsep(&p, sep), 2151 0, ALGOR_NUM - 1, &errstr); 2152 sstrsep(&p, sep); 2153 2154 d = atof(sstrsep(&p, sep)); 2155 if (n) 2156 ecdsa_results[k][0] = 1 / (1 / ecdsa_results[k][0] + 1 / d); 2157 else 2158 ecdsa_results[k][0] = d; 2159 2160 d = atof(sstrsep(&p, sep)); 2161 if (n) 2162 ecdsa_results[k][1] = 1 / (1 / ecdsa_results[k][1] + 1 / d); 2163 else 2164 ecdsa_results[k][1] = d; 2165 } 2166 2167 else if (!strncmp(buf, "+F5:", 4)) { 2168 int k; 2169 double d; 2170 2171 p = buf + 4; 2172 k = strtonum(sstrsep(&p, sep), 2173 0, ALGOR_NUM - 1, &errstr); 2174 sstrsep(&p, sep); 2175 2176 d = atof(sstrsep(&p, sep)); 2177 if (n) 2178 ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d); 2179 else 2180 ecdh_results[k][0] = d; 2181 2182 } 2183 2184 else if (!strncmp(buf, "+H:", 3)) { 2185 } else 2186 fprintf(stderr, "Unknown type '%s' from child %d\n", buf, n); 2187 } 2188 2189 fclose(f); 2190 } 2191 free(fds); 2192 return 1; 2193 } 2194 #endif 2195