nettle-benchmark.c 15.9 KB
Newer Older
1 2 3 4 5 6 7 8
/* nettle-benchmark.c
 *
 * Tries the performance of the various algorithms.
 *
 */
 
/* nettle, low-level cryptographics library
 *
Niels Möller's avatar
Niels Möller committed
9
 * Copyright (C) 2001, 2010 Niels Möller
10 11 12 13 14 15 16 17 18 19 20 21 22
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
23 24
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
25 26
 */

27 28
#if HAVE_CONFIG_H
# include "config.h"
29 30 31 32
#endif

#include <assert.h>
#include <errno.h>
33
#include <math.h>
Niels Möller's avatar
Niels Möller committed
34
#include <stdarg.h>
35 36 37 38 39
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
40

41
#include "aes.h"
Niels Möller's avatar
Niels Möller committed
42 43 44
#include "arcfour.h"
#include "blowfish.h"
#include "cast128.h"
Niels Möller's avatar
Niels Möller committed
45
#include "cbc.h"
Niels Möller's avatar
Niels Möller committed
46
#include "ctr.h"
Niels Möller's avatar
Niels Möller committed
47
#include "des.h"
Niels Möller's avatar
Niels Möller committed
48
#include "gcm.h"
49
#include "memxor.h"
50
#include "salsa20.h"
Niels Möller's avatar
Niels Möller committed
51
#include "serpent.h"
52
#include "sha.h"
Niels Möller's avatar
Niels Möller committed
53
#include "sha3.h"
Niels Möller's avatar
Niels Möller committed
54 55
#include "twofish.h"

56 57 58
#include "nettle-meta.h"
#include "nettle-internal.h"

Niels Möller's avatar
Niels Möller committed
59
#include "getopt.h"
60

61
static double frequency = 0.0;
Niels Möller's avatar
Niels Möller committed
62

63
/* Process BENCH_BLOCK bytes at a time, for BENCH_INTERVAL seconds. */
64
#define BENCH_BLOCK 10240
65
#define BENCH_INTERVAL 0.1
Niels Möller's avatar
Niels Möller committed
66

67 68
/* FIXME: Proper configure test for rdtsc? */
#ifndef WITH_CYCLE_COUNTER
69
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
70 71 72 73 74 75 76
#  define WITH_CYCLE_COUNTER 1
# else
#  define WITH_CYCLE_COUNTER 0
# endif
#endif

#if WITH_CYCLE_COUNTER
77
# if defined(__i386__)
78 79 80 81 82 83 84 85 86
#define GET_CYCLE_COUNTER(hi, lo)		\
  __asm__("xorl %%eax,%%eax\n"			\
	  "movl %%ebx, %%edi\n"			\
	  "cpuid\n"				\
	  "rdtsc\n"				\
	  "movl %%edi, %%ebx\n"			\
	  : "=a" (lo), "=d" (hi)		\
	  : /* No inputs. */			\
	  : "%edi", "%ecx", "cc")
87 88 89 90 91 92 93 94 95 96 97
# elif defined(__x86_64__)
#define GET_CYCLE_COUNTER(hi, lo)		\
  __asm__("xorl %%eax,%%eax\n"			\
	  "mov %%rbx, %%r10\n"			\
	  "cpuid\n"				\
	  "rdtsc\n"				\
	  "mov %%r10, %%rbx\n"			\
	  : "=a" (lo), "=d" (hi)		\
	  : /* No inputs. */			\
	  : "%r10", "%rcx", "cc")
# endif
98 99 100
#define BENCH_ITERATIONS 10
#endif

101
static void NORETURN PRINTF_STYLE(1,2)
Niels Möller's avatar
Niels Möller committed
102 103 104 105 106 107 108 109 110 111
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  exit(EXIT_FAILURE);
}

112 113
static double overhead = 0.0; 

114
#if HAVE_CLOCK_GETTIME && defined CLOCK_PROCESS_CPUTIME_ID
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#define TRY_CLOCK_GETTIME 1
struct timespec cgt_start;

static int
cgt_works_p(void)
{
  struct timespec now;
  return clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now) == 0;
}

static void
cgt_time_start(void)
{
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cgt_start) < 0)
    die("clock_gettime failed: %s\n", strerror(errno));
}

static double
cgt_time_end(void)
{
    struct timespec end;
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0)
      die("clock_gettime failed: %s\n", strerror(errno));

    return end.tv_sec - cgt_start.tv_sec
      + 1e-9 * (end.tv_nsec - cgt_start.tv_nsec);
}

static void (*time_start)(void);
static double (*time_end)(void);

146
#else /* !HAVE_CLOCK_GETTIME */
147 148 149
#define TRY_CLOCK_GETTIME 0
#define time_start clock_time_start
#define time_end clock_time_end
150 151
#endif /* !HAVE_CLOCK_GETTIME */

152 153 154 155 156 157 158 159 160 161 162 163 164 165
static clock_t clock_start;

static void
clock_time_start(void)
{
  clock_start = clock();
}

static double
clock_time_end(void)
{
  return (double) (clock() - (clock_start)) / CLOCKS_PER_SEC;
}

166
/* Returns second per function call */
167 168 169
static double
time_function(void (*f)(void *arg), void *arg)
{
170
  unsigned ncalls;
171 172
  double elapsed;

173
  for (ncalls = 10 ;;)
174
    {
175
      unsigned i;
176 177

      time_start();
178 179
      for (i = 0; i < ncalls; i++)
	f(arg);
180
      elapsed = time_end();
181 182 183 184 185 186
      if (elapsed > BENCH_INTERVAL)
	break;
      else if (elapsed < BENCH_INTERVAL / 10)
	ncalls *= 10;
      else
	ncalls *= 2;
187
    }
188 189 190 191 192 193 194
  return elapsed / ncalls - overhead;
}

static void
bench_nothing(void *arg UNUSED)
{
  return;
195 196
}

197 198 199 200
struct bench_memxor_info
{
  uint8_t *dst;
  const uint8_t *src;
201
  const uint8_t *other;  
202 203 204 205 206 207 208 209 210
};

static void
bench_memxor(void *arg)
{
  struct bench_memxor_info *info = arg;
  memxor (info->dst, info->src, BENCH_BLOCK);
}

211 212 213 214 215 216 217
static void
bench_memxor3(void *arg)
{
  struct bench_memxor_info *info = arg;
  memxor3 (info->dst, info->src, info->other, BENCH_BLOCK);
}

218 219 220
struct bench_hash_info
{
  void *ctx;
221
  nettle_hash_update_func *update;
222 223 224 225 226 227 228 229 230 231
  const uint8_t *data;
};

static void
bench_hash(void *arg)
{
  struct bench_hash_info *info = arg;
  info->update(info->ctx, BENCH_BLOCK, info->data);
}

232 233 234
struct bench_cipher_info
{
  void *ctx;
235
  nettle_crypt_func *crypt;
236 237 238 239 240 241 242
  uint8_t *data;
};

static void
bench_cipher(void *arg)
{
  struct bench_cipher_info *info = arg;
243
  info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
244 245 246 247 248
}

struct bench_cbc_info
{
  void *ctx;
249
  nettle_crypt_func *crypt;
250
 
251
  uint8_t *data;
252
  
253 254 255 256 257 258 259 260
  unsigned block_size;
  uint8_t *iv;
};

static void
bench_cbc_encrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
261 262 263
  cbc_encrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
264 265 266 267 268 269
}

static void
bench_cbc_decrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
270 271 272
  cbc_decrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
273 274
}

Niels Möller's avatar
Niels Möller committed
275 276 277 278 279 280 281 282 283
static void
bench_ctr(void *arg)
{
  struct bench_cbc_info *info = arg;
  ctr_crypt(info->ctx, info->crypt,
	    info->block_size, info->iv,
	    BENCH_BLOCK, info->data, info->data);
}

284 285 286 287 288
/* Set data[i] = floor(sqrt(i)) */
static void
init_data(uint8_t *data)
{
  unsigned i,j;
Niels Möller's avatar
Niels Möller committed
289
  for (i = j = 0; i<BENCH_BLOCK;  i++)
290 291 292 293 294 295 296 297
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
298 299
init_key(unsigned length,
         uint8_t *key)
300
{
Niels Möller's avatar
Niels Möller committed
301 302 303
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
304 305
}

306 307 308 309 310
static void
header(void)
{
  printf("%18s %11s Mbyte/s%s\n",
	 "Algorithm", "mode", 
Niels Möller's avatar
Niels Möller committed
311
	 frequency > 0.0 ? " cycles/byte cycles/block" : "");  
312 313
}

Niels Möller's avatar
Niels Möller committed
314
static void
Niels Möller's avatar
Niels Möller committed
315
display(const char *name, const char *mode, unsigned block_size,
316
	double time)
Niels Möller's avatar
Niels Möller committed
317
{
318
  printf("%18s %11s %7.2f",
Niels Möller's avatar
Niels Möller committed
319
	 name, mode,
320
	 BENCH_BLOCK / (time * 1048576.0));
321
  if (frequency > 0.0)
Niels Möller's avatar
Niels Möller committed
322
    {
323
      printf(" %11.2f", time * frequency / BENCH_BLOCK);
Niels Möller's avatar
Niels Möller committed
324
      if (block_size > 0)
325
	printf(" %12.2f", time * frequency * block_size / BENCH_BLOCK);
Niels Möller's avatar
Niels Möller committed
326
    }
327
  printf("\n");
Niels Möller's avatar
Niels Möller committed
328 329
}

330 331 332 333 334
static void *
xalloc(size_t size)
{
  void *p = malloc(size);
  if (!p)
335
    die("Virtual memory exhausted.\n");
336 337 338 339

  return p;
}

340 341 342 343 344 345 346 347 348 349 350 351
static void
time_overhead(void)
{
  overhead = time_function(bench_nothing, NULL);
  printf("benchmark call overhead: %7f us", overhead * 1e6);
  if (frequency > 0.0)
    printf("%7.2f cycles\n", overhead * frequency);
  printf("\n");  
}



352 353 354 355
static void
time_memxor(void)
{
  struct bench_memxor_info info;
356
  uint8_t src[BENCH_BLOCK + sizeof(long)];
357
  uint8_t other[BENCH_BLOCK + sizeof(long)];
358
  uint8_t dst[BENCH_BLOCK];
359 360 361 362

  info.src = src;
  info.dst = dst;

363 364
  display ("memxor", "aligned", sizeof(unsigned long),
	   time_function(bench_memxor, &info));
365
  info.src = src + 1;
366
  display ("memxor", "unaligned", sizeof(unsigned long),
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	   time_function(bench_memxor, &info));

  info.src = src;
  info.other = other;
  display ("memxor3", "aligned", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));

  info.other = other + 1;
  display ("memxor3", "unaligned01", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));
  info.src = src + 1;
  display ("memxor3", "unaligned11", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));
  info.other = other + 2;
  display ("memxor3", "unaligned12", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));  
383 384
}

385 386 387 388 389
static void
time_hash(const struct nettle_hash *hash)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
390

391
  info.ctx = xalloc(hash->context_size); 
392 393 394 395 396 397
  info.update = hash->update;
  info.data = data;

  init_data(data);
  hash->init(info.ctx);

Niels Möller's avatar
Niels Möller committed
398
  display(hash->name, "update", hash->block_size,
399
	  time_function(bench_hash, &info));
400 401

  free(info.ctx);
402 403
}

Niels Möller's avatar
Niels Möller committed
404
static void
405
time_gcm(void)
Niels Möller's avatar
Niels Möller committed
406 407
{
  static uint8_t data[BENCH_BLOCK];
408 409
  struct bench_hash_info hinfo;
  struct bench_cipher_info cinfo;
410
  struct gcm_aes_ctx ctx;
411

Niels Möller's avatar
Niels Möller committed
412 413 414
  uint8_t key[16];
  uint8_t iv[GCM_IV_SIZE];

415 416
  gcm_aes_set_key(&ctx, sizeof(key), key);
  gcm_aes_set_iv(&ctx, sizeof(iv), iv);
Niels Möller's avatar
Niels Möller committed
417

418 419 420 421 422 423 424 425 426 427
  hinfo.ctx = &ctx;
  hinfo.update = (nettle_hash_update_func *) gcm_aes_update;
  hinfo.data = data;
  
  display("gcm-aes", "update", GCM_BLOCK_SIZE,
	  time_function(bench_hash, &hinfo));
  
  cinfo.ctx = &ctx;
  cinfo.crypt = (nettle_crypt_func *) gcm_aes_encrypt;
  cinfo.data = data;
Niels Möller's avatar
Niels Möller committed
428

429 430 431 432 433 434 435
  display("gcm-aes", "encrypt", GCM_BLOCK_SIZE,
	  time_function(bench_cipher, &cinfo));

  cinfo.crypt = (nettle_crypt_func *) gcm_aes_decrypt;

  display("gcm-aes", "decrypt", GCM_BLOCK_SIZE,
	  time_function(bench_cipher, &cinfo));
Niels Möller's avatar
Niels Möller committed
436 437
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
static int
prefix_p(const char *prefix, const char *s)
{
  size_t i;
  for (i = 0; prefix[i]; i++)
    if (prefix[i] != s[i])
      return 0;
  return 1;
}

static int
block_cipher_p(const struct nettle_cipher *cipher)
{
  /* Don't use nettle cbc and ctr for openssl ciphers. */
  return cipher->block_size > 0 && !prefix_p("openssl", cipher->name);
}

Niels Möller's avatar
Niels Möller committed
455
static void
456
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
457
{
458 459
  void *ctx = xalloc(cipher->context_size);
  uint8_t *key = xalloc(cipher->key_size);
Niels Möller's avatar
Niels Möller committed
460

461
  static uint8_t data[BENCH_BLOCK];
Niels Möller's avatar
Niels Möller committed
462 463 464 465

  printf("\n");
  
  init_data(data);
466 467

  {
Niels Möller's avatar
Niels Möller committed
468 469 470 471 472
    /* Decent initializers are a GNU extension, so don't use it here. */
    struct bench_cipher_info info;
    info.ctx = ctx;
    info.crypt = cipher->encrypt;
    info.data = data;
473
    
Niels Möller's avatar
Niels Möller committed
474
    init_key(cipher->key_size, key);
475
    cipher->set_encrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
476

Niels Möller's avatar
Niels Möller committed
477
    display(cipher->name, "ECB encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
478
	    time_function(bench_cipher, &info));
479
  }
Niels Möller's avatar
Niels Möller committed
480
  
481
  {
Niels Möller's avatar
Niels Möller committed
482 483 484 485
    struct bench_cipher_info info;
    info.ctx = ctx;
    info.crypt = cipher->decrypt;
    info.data = data;
486
    
Niels Möller's avatar
Niels Möller committed
487
    init_key(cipher->key_size, key);
488
    cipher->set_decrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
489

Niels Möller's avatar
Niels Möller committed
490
    display(cipher->name, "ECB decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
491
	    time_function(bench_cipher, &info));
492 493
  }

494
  if (block_cipher_p(cipher))
Niels Möller's avatar
Niels Möller committed
495
    {
496
      uint8_t *iv = xalloc(cipher->block_size);
Niels Möller's avatar
Niels Möller committed
497 498 499
      
      /* Do CBC mode */
      {
Niels Möller's avatar
Niels Möller committed
500 501 502 503 504 505
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
506
    
Niels Möller's avatar
Niels Möller committed
507
        memset(iv, 0, sizeof(iv));
508
    
509
        cipher->set_encrypt_key(ctx, cipher->key_size, key);
510

Niels Möller's avatar
Niels Möller committed
511
	display(cipher->name, "CBC encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
512
		time_function(bench_cbc_encrypt, &info));
Niels Möller's avatar
Niels Möller committed
513
      }
514

Niels Möller's avatar
Niels Möller committed
515
      {
Niels Möller's avatar
Niels Möller committed
516 517 518 519 520 521
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->decrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
522
    
Niels Möller's avatar
Niels Möller committed
523
        memset(iv, 0, sizeof(iv));
524

525
        cipher->set_decrypt_key(ctx, cipher->key_size, key);
526

Niels Möller's avatar
Niels Möller committed
527
	display(cipher->name, "CBC decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
528
		time_function(bench_cbc_decrypt, &info));
Niels Möller's avatar
Niels Möller committed
529
      }
Niels Möller's avatar
Niels Möller committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

      /* Do CTR mode */
      {
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
    
        memset(iv, 0, sizeof(iv));
    
        cipher->set_encrypt_key(ctx, cipher->key_size, key);

	display(cipher->name, "CTR", cipher->block_size,
		time_function(bench_ctr, &info));	
      }
      
548
      free(iv);
Niels Möller's avatar
Niels Möller committed
549
    }
550 551
  free(ctx);
  free(key);
Niels Möller's avatar
Niels Möller committed
552 553
}

Niels Möller's avatar
Niels Möller committed
554 555
/* Try to get accurate cycle times for assembler functions. */
#if WITH_CYCLE_COUNTER
556 557 558 559 560 561 562 563 564 565 566 567 568
static int
compare_double(const void *ap, const void *bp)
{
  double a = *(const double *) ap;
  double b = *(const double *) bp;
  if (a < b)
    return -1;
  else if (a > b)
    return 1;
  else
    return 0;
}

Niels Möller's avatar
Niels Möller committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
#define TIME_CYCLES(t, code) do {				\
  double tc_count[5];						\
  uint32_t tc_start_lo, tc_start_hi, tc_end_lo, tc_end_hi;	\
  unsigned tc_i, tc_j;						\
  for (tc_j = 0; tc_j < 5; tc_j++)				\
    {								\
      tc_i = 0;							\
      GET_CYCLE_COUNTER(tc_start_hi, tc_start_lo);		\
      for (; tc_i < BENCH_ITERATIONS; tc_i++)			\
	{ code; }						\
								\
      GET_CYCLE_COUNTER(tc_end_hi, tc_end_lo);			\
								\
      tc_end_hi -= (tc_start_hi + (tc_start_lo > tc_end_lo));	\
      tc_end_lo -= tc_start_lo;					\
								\
      tc_count[tc_j] = ldexp(tc_end_hi, 32) + tc_end_lo;	\
    }								\
  qsort(tc_count, 5, sizeof(double), compare_double);		\
  (t) = tc_count[2] / BENCH_ITERATIONS;				\
} while (0)

591 592 593 594
static void
bench_sha1_compress(void)
{
  uint32_t state[_SHA1_DIGEST_LENGTH];
Niels Möller's avatar
Niels Möller committed
595 596
  uint8_t data[SHA1_DATA_SIZE];
  double t;
597

Niels Möller's avatar
Niels Möller committed
598
  TIME_CYCLES (t, _nettle_sha1_compress(state, data));
599

Niels Möller's avatar
Niels Möller committed
600
  printf("sha1_compress: %.2f cycles\n", t);  
601 602
}

603 604 605 606
static void
bench_salsa20_core(void)
{
  uint32_t state[_SALSA20_INPUT_LENGTH];
Niels Möller's avatar
Niels Möller committed
607
  double t;
608

Niels Möller's avatar
Niels Möller committed
609 610 611
  TIME_CYCLES (t, _nettle_salsa20_core(state, state, 20));
  printf("salsa20_core: %.2f cycles\n", t);  
}
612

Niels Möller's avatar
Niels Möller committed
613 614 615 616 617
static void
bench_sha3_permute(void)
{
  struct sha3_state state;
  double t;
618

Niels Möller's avatar
Niels Möller committed
619 620
  TIME_CYCLES (t, sha3_permute (&state));
  printf("sha3_permute: %.2f cycles (%.2f / round)\n", t, t / 24.0);
621 622 623 624
}
#else
#define bench_sha1_compress()
#define bench_salsa20_core()
Niels Möller's avatar
Niels Möller committed
625
#define bench_sha3_permute()
626 627
#endif

Niels Möller's avatar
Niels Möller committed
628
#if WITH_OPENSSL
629 630 631 632
# define OPENSSL(x) x,
#else
# define OPENSSL(x)
#endif
Niels Möller's avatar
Niels Möller committed
633 634

int
635
main(int argc, char **argv)
Niels Möller's avatar
Niels Möller committed
636 637
{
  unsigned i;
638
  int c;
639
  const char *alg;
640 641 642 643

  const struct nettle_hash *hashes[] =
    {
      &nettle_md2, &nettle_md4, &nettle_md5,
644
      OPENSSL(&nettle_openssl_md5)
645
      &nettle_sha1, OPENSSL(&nettle_openssl_sha1)
Niels Möller's avatar
Niels Möller committed
646 647
      &nettle_sha224, &nettle_sha256,
      &nettle_sha384, &nettle_sha512,
Niels Möller's avatar
Niels Möller committed
648
      &nettle_sha3_256,
649
      &nettle_ripemd160, &nettle_gosthash94,
650 651 652
      NULL
    };

653
  const struct nettle_cipher *ciphers[] =
Niels Möller's avatar
Niels Möller committed
654
    {
655
      &nettle_aes128, &nettle_aes192, &nettle_aes256,
Niels Möller's avatar
Niels Möller committed
656 657 658 659 660
      OPENSSL(&nettle_openssl_aes128)
      OPENSSL(&nettle_openssl_aes192)
      OPENSSL(&nettle_openssl_aes256)
      &nettle_arcfour128, OPENSSL(&nettle_openssl_arcfour128)
      &nettle_blowfish128, OPENSSL(&nettle_openssl_blowfish128)
Niels Möller's avatar
Niels Möller committed
661
      &nettle_camellia128, &nettle_camellia192, &nettle_camellia256,
662 663 664
      &nettle_cast128, OPENSSL(&nettle_openssl_cast128)
      &nettle_des, OPENSSL(&nettle_openssl_des)
      &nettle_des3,
665 666
      &nettle_serpent256,
      &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
667
      &nettle_salsa20,
668
      NULL
Niels Möller's avatar
Niels Möller committed
669
    };
670

671 672 673 674 675 676 677 678 679 680
  enum { OPT_HELP = 300 };
  static const struct option options[] =
    {
      /* Name, args, flag, val */
      { "help", no_argument, NULL, OPT_HELP },
      { "clock-frequency", required_argument, NULL, 'f' },
      { NULL, 0, NULL, 0 }
    };
  
  while ( (c = getopt_long(argc, argv, "f:", options, NULL)) != -1)
681 682 683 684 685 686 687
    switch (c)
      {
      case 'f':
	frequency = atof(optarg);
	if (frequency > 0.0)
	  break;

688 689 690 691 692
      case OPT_HELP:
	printf("Usage: nettle-benchmark [-f clock frequency] [alg]\n");
	return EXIT_SUCCESS;

      case '?':
693 694 695 696 697 698
	return EXIT_FAILURE;

      default:
	abort();
    }

699
  alg = argv[optind];
700

701 702 703 704 705 706 707 708 709 710 711 712 713 714
  /* Choose timing function */
#if TRY_CLOCK_GETTIME
  if (cgt_works_p())
    {
      time_start = cgt_time_start;
      time_end = cgt_time_end;
    }
  else
    {
      fprintf(stderr, "clock_gettime not working, falling back to clock\n");
      time_start = clock_time_start;
      time_end = clock_time_end;
    }
#endif
715
  bench_sha1_compress();
716
  bench_salsa20_core();
Niels Möller's avatar
Niels Möller committed
717 718
  bench_sha3_permute();
  printf("\n");
719 720
  time_overhead();

721 722
  header();

723 724 725 726 727
  if (!alg || strstr ("memxor", alg))
    {
      time_memxor();
      printf("\n");
    }
728
  
729
  for (i = 0; hashes[i]; i++)
730 731
    if (!alg || strstr(hashes[i]->name, alg))
      time_hash(hashes[i]);
Niels Möller's avatar
Niels Möller committed
732

733
  for (i = 0; ciphers[i]; i++)
734 735 736
    if (!alg || strstr(ciphers[i]->name, alg))
      time_cipher(ciphers[i]);

737 738 739 740 741 742
  if (!alg || strstr ("gcm", alg))
    {
      printf("\n");
      time_gcm();
    }

743 744
  return 0;
}