nettle-benchmark.c 13.3 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 Mller
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *  
 * 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
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

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 "des.h"
Niels Möller's avatar
Niels Möller committed
47
#include "gcm.h"
48
#include "memxor.h"
Niels Möller's avatar
Niels Möller committed
49
#include "serpent.h"
50
#include "sha.h"
Niels Möller's avatar
Niels Möller committed
51 52
#include "twofish.h"

53 54 55
#include "nettle-meta.h"
#include "nettle-internal.h"

Niels Möller's avatar
Niels Möller committed
56
#include "getopt.h"
57

58
static double frequency = 0.0;
Niels Möller's avatar
Niels Möller committed
59

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

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

#if WITH_CYCLE_COUNTER
74
# if defined(__i386__)
75 76 77 78 79 80 81 82 83
#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")
84 85 86 87 88 89 90 91 92 93 94
# 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
95 96 97
#define BENCH_ITERATIONS 10
#endif

Niels Möller's avatar
Niels Möller committed
98 99 100 101 102 103 104 105 106 107 108
static void
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  exit(EXIT_FAILURE);
}

109 110
static double overhead = 0.0; 

111 112
#if HAVE_CLOCK_GETTIME && defined CLOCK_PROCESS_CPUTIME_ID
#define TIME_TYPE struct timespec
Niels Möller's avatar
Niels Möller committed
113 114 115 116 117 118 119 120 121 122
#define TIME_START(start) do {					\
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(start)) < 0)	\
      die("clock_gettime failed: %s\n", strerror(errno));	\
  }  while (0)
#define TIME_END(elapsed, start) do {					\
    struct timespec _time_end_after;					\
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &_time_end_after) < 0)	\
      die("clock_gettime failed: %s\n", strerror(errno));		\
    (elapsed) = _time_end_after.tv_sec - (start).tv_sec			\
      + 1e-9 * (_time_end_after.tv_nsec - (start).tv_nsec);		\
123 124 125 126 127 128 129 130
  } while (0)
#else /* !HAVE_CLOCK_GETTIME */
#define TIME_TYPE clock_t
#define TIME_START(start) ((start) = clock)
#define TIME_END(elapsed, start) \
  ((elapsed) = (double) (clock() - (start)) / CLOCKS_PER_SEC)
#endif /* !HAVE_CLOCK_GETTIME */

131
/* Returns second per function call */
132 133 134
static double
time_function(void (*f)(void *arg), void *arg)
{
135
  unsigned ncalls;
136 137
  double elapsed;

138
  for (ncalls = 10 ;;)
139
    {
140 141 142 143 144 145 146 147 148 149 150 151
      TIME_TYPE start;
      unsigned i;
      TIME_START(start);
      for (i = 0; i < ncalls; i++)
	f(arg);
      TIME_END(elapsed, start);
      if (elapsed > BENCH_INTERVAL)
	break;
      else if (elapsed < BENCH_INTERVAL / 10)
	ncalls *= 10;
      else
	ncalls *= 2;
152
    }
153 154 155 156 157 158 159
  return elapsed / ncalls - overhead;
}

static void
bench_nothing(void *arg UNUSED)
{
  return;
160 161
}

162 163 164 165
struct bench_memxor_info
{
  uint8_t *dst;
  const uint8_t *src;
166
  const uint8_t *other;  
167 168 169 170 171 172 173 174 175
};

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

176 177 178 179 180 181 182
static void
bench_memxor3(void *arg)
{
  struct bench_memxor_info *info = arg;
  memxor3 (info->dst, info->src, info->other, BENCH_BLOCK);
}

183 184 185
struct bench_hash_info
{
  void *ctx;
186
  nettle_hash_update_func *update;
187 188 189 190 191 192 193 194 195 196
  const uint8_t *data;
};

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

197 198 199
struct bench_cipher_info
{
  void *ctx;
200
  nettle_crypt_func *crypt;
201 202 203 204 205 206 207
  uint8_t *data;
};

static void
bench_cipher(void *arg)
{
  struct bench_cipher_info *info = arg;
208
  info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
209 210 211 212 213
}

struct bench_cbc_info
{
  void *ctx;
214
  nettle_crypt_func *crypt;
215
 
216
  uint8_t *data;
217
  
218 219 220 221 222 223 224 225
  unsigned block_size;
  uint8_t *iv;
};

static void
bench_cbc_encrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
226 227 228
  cbc_encrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
229 230 231 232 233 234
}

static void
bench_cbc_decrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
235 236 237
  cbc_decrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
238 239 240 241 242 243 244
}

/* 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
245
  for (i = j = 0; i<BENCH_BLOCK;  i++)
246 247 248 249 250 251 252 253
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
254 255
init_key(unsigned length,
         uint8_t *key)
256
{
Niels Möller's avatar
Niels Möller committed
257 258 259
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
260 261
}

262 263 264 265 266
static void
header(void)
{
  printf("%18s %11s Mbyte/s%s\n",
	 "Algorithm", "mode", 
Niels Möller's avatar
Niels Möller committed
267
	 frequency > 0.0 ? " cycles/byte cycles/block" : "");  
268 269
}

Niels Möller's avatar
Niels Möller committed
270
static void
Niels Möller's avatar
Niels Möller committed
271
display(const char *name, const char *mode, unsigned block_size,
272
	double time)
Niels Möller's avatar
Niels Möller committed
273
{
274
  printf("%18s %11s %7.2f",
Niels Möller's avatar
Niels Möller committed
275
	 name, mode,
276
	 BENCH_BLOCK / (time * 1048576.0));
277
  if (frequency > 0.0)
Niels Möller's avatar
Niels Möller committed
278
    {
279
      printf(" %11.2f", time * frequency / BENCH_BLOCK);
Niels Möller's avatar
Niels Möller committed
280
      if (block_size > 0)
281
	printf(" %12.2f", time * frequency * block_size / BENCH_BLOCK);
Niels Möller's avatar
Niels Möller committed
282
    }
283
  printf("\n");
Niels Möller's avatar
Niels Möller committed
284 285
}

286 287 288 289 290 291 292 293 294 295 296 297 298
static void *
xalloc(size_t size)
{
  void *p = malloc(size);
  if (!p)
    {
      fprintf(stderr, "Virtual memory exhausted.\n");
      abort();
    }

  return p;
}

299 300 301 302 303 304 305 306 307 308 309 310
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");  
}



311 312 313 314
static void
time_memxor(void)
{
  struct bench_memxor_info info;
315
  uint8_t src[BENCH_BLOCK + sizeof(long)];
316
  uint8_t other[BENCH_BLOCK + sizeof(long)];
317
  uint8_t dst[BENCH_BLOCK];
318 319 320 321

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

322 323
  display ("memxor", "aligned", sizeof(unsigned long),
	   time_function(bench_memxor, &info));
324
  info.src = src + 1;
325
  display ("memxor", "unaligned", sizeof(unsigned long),
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	   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));  
342 343
}

344 345 346 347 348
static void
time_hash(const struct nettle_hash *hash)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
349

350
  info.ctx = xalloc(hash->context_size); 
351 352 353 354 355 356
  info.update = hash->update;
  info.data = data;

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

Niels Möller's avatar
Niels Möller committed
357
  display(hash->name, "update", hash->block_size,
358
	  time_function(bench_hash, &info));
359 360

  free(info.ctx);
361 362
}

Niels Möller's avatar
Niels Möller committed
363 364 365 366 367
static void
time_gmac(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
368
  struct gcm_aes_ctx ctx;
Niels Möller's avatar
Niels Möller committed
369 370 371
  uint8_t key[16];
  uint8_t iv[GCM_IV_SIZE];

372 373
  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
374

375 376
  info.ctx = &ctx;
  info.update = (nettle_hash_update_func *) gcm_aes_auth;
Niels Möller's avatar
Niels Möller committed
377 378 379 380 381 382
  info.data = data;

  display("gmac", "auth", GCM_BLOCK_SIZE,
	  time_function(bench_hash, &info));
}

Niels Möller's avatar
Niels Möller committed
383
static void
384
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
385
{
386 387
  void *ctx = xalloc(cipher->context_size);
  uint8_t *key = xalloc(cipher->key_size);
Niels Möller's avatar
Niels Möller committed
388

389
  static uint8_t data[BENCH_BLOCK];
Niels Möller's avatar
Niels Möller committed
390 391 392 393

  printf("\n");
  
  init_data(data);
394 395

  {
Niels Möller's avatar
Niels Möller committed
396 397 398 399 400
    /* 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;
401
    
Niels Möller's avatar
Niels Möller committed
402
    init_key(cipher->key_size, key);
403
    cipher->set_encrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
404

Niels Möller's avatar
Niels Möller committed
405
    display(cipher->name, "ECB encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
406
	    time_function(bench_cipher, &info));
407
  }
Niels Möller's avatar
Niels Möller committed
408
  
409
  {
Niels Möller's avatar
Niels Möller committed
410 411 412 413
    struct bench_cipher_info info;
    info.ctx = ctx;
    info.crypt = cipher->decrypt;
    info.data = data;
414
    
Niels Möller's avatar
Niels Möller committed
415
    init_key(cipher->key_size, key);
416
    cipher->set_decrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
417

Niels Möller's avatar
Niels Möller committed
418
    display(cipher->name, "ECB decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
419
	    time_function(bench_cipher, &info));
420 421
  }

Niels Möller's avatar
Niels Möller committed
422 423
  /* Don't use nettle cbc to benchmark openssl ciphers */
  if (cipher->block_size && cipher->name[0] != 'o')
Niels Möller's avatar
Niels Möller committed
424
    {
425
      uint8_t *iv = xalloc(cipher->block_size);
Niels Möller's avatar
Niels Möller committed
426 427 428
      
      /* Do CBC mode */
      {
Niels Möller's avatar
Niels Möller committed
429 430 431 432 433 434
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
435
    
Niels Möller's avatar
Niels Möller committed
436
        memset(iv, 0, sizeof(iv));
437
    
438
        cipher->set_encrypt_key(ctx, cipher->key_size, key);
439

Niels Möller's avatar
Niels Möller committed
440
	display(cipher->name, "CBC encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
441
		time_function(bench_cbc_encrypt, &info));
Niels Möller's avatar
Niels Möller committed
442
      }
443

Niels Möller's avatar
Niels Möller committed
444
      {
Niels Möller's avatar
Niels Möller committed
445 446 447 448 449 450
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->decrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
451
    
Niels Möller's avatar
Niels Möller committed
452
        memset(iv, 0, sizeof(iv));
453

454
        cipher->set_decrypt_key(ctx, cipher->key_size, key);
455

Niels Möller's avatar
Niels Möller committed
456
	display(cipher->name, "CBC decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
457
		time_function(bench_cbc_decrypt, &info));
Niels Möller's avatar
Niels Möller committed
458
      }
459
      free(iv);
Niels Möller's avatar
Niels Möller committed
460
    }
461 462
  free(ctx);
  free(key);
Niels Möller's avatar
Niels Möller committed
463 464
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
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;
}

/* Try to get accurate cycle times for assembler functions. */
static void
bench_sha1_compress(void)
{
#if WITH_CYCLE_COUNTER
  uint32_t state[_SHA1_DIGEST_LENGTH];
  uint8_t data[BENCH_ITERATIONS * SHA1_DATA_SIZE];
  uint32_t start_lo, start_hi, end_lo, end_hi;

  double count[5];
  
  uint8_t *p;
  unsigned i, j;

  for (j = 0; j < 5; j++)
    {
      i = 0;
      p = data;
      GET_CYCLE_COUNTER(start_hi, start_lo);
      for (; i < BENCH_ITERATIONS; i++, p += SHA1_DATA_SIZE)
	_nettle_sha1_compress(state, p);

      GET_CYCLE_COUNTER(end_hi, end_lo);

      end_hi -= (start_hi + (start_lo > end_lo));
      end_lo -= start_lo;

      count[j] = ldexp(end_hi, 32) + end_lo;
    }

  qsort(count, 5, sizeof(double), compare_double);
  printf("sha1_compress: %.2f cycles\n\n", count[2] / BENCH_ITERATIONS);  
#endif
}

Niels Möller's avatar
Niels Möller committed
513
#if WITH_OPENSSL
514 515 516 517
# define OPENSSL(x) x,
#else
# define OPENSSL(x)
#endif
Niels Möller's avatar
Niels Möller committed
518 519

int
520
main(int argc, char **argv)
Niels Möller's avatar
Niels Möller committed
521 522
{
  unsigned i;
523
  int c;
524
  const char *alg;
525 526 527 528

  const struct nettle_hash *hashes[] =
    {
      &nettle_md2, &nettle_md4, &nettle_md5,
529
      OPENSSL(&nettle_openssl_md5)
530
      &nettle_sha1, OPENSSL(&nettle_openssl_sha1)
Niels Möller's avatar
Niels Möller committed
531 532
      &nettle_sha224, &nettle_sha256,
      &nettle_sha384, &nettle_sha512,
533 534 535
      NULL
    };

536
  const struct nettle_cipher *ciphers[] =
Niels Möller's avatar
Niels Möller committed
537
    {
538
      &nettle_aes128, &nettle_aes192, &nettle_aes256,
Niels Möller's avatar
Niels Möller committed
539 540 541 542 543
      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
544
      &nettle_camellia128, &nettle_camellia192, &nettle_camellia256,
545 546 547
      &nettle_cast128, OPENSSL(&nettle_openssl_cast128)
      &nettle_des, OPENSSL(&nettle_openssl_des)
      &nettle_des3,
548 549
      &nettle_serpent256,
      &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
550
      NULL
Niels Möller's avatar
Niels Möller committed
551
    };
552

553 554 555 556 557 558 559 560 561 562
  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)
563 564 565 566 567 568 569
    switch (c)
      {
      case 'f':
	frequency = atof(optarg);
	if (frequency > 0.0)
	  break;

570 571 572 573 574
      case OPT_HELP:
	printf("Usage: nettle-benchmark [-f clock frequency] [alg]\n");
	return EXIT_SUCCESS;

      case '?':
575 576 577 578 579 580
	return EXIT_FAILURE;

      default:
	abort();
    }

581
  alg = argv[optind];
582

583 584
  bench_sha1_compress();

585 586
  time_overhead();

587 588
  header();

589 590 591 592 593
  if (!alg || strstr ("memxor", alg))
    {
      time_memxor();
      printf("\n");
    }
594
  
595 596 597 598 599
  for (i = 0; hashes[i]; i++)
    {
      if (!alg || strstr(hashes[i]->name, alg))
	time_hash(hashes[i]);
    }
Niels Möller's avatar
Niels Möller committed
600 601 602 603 604 605 606

  if (!alg || strstr ("gmac", alg))
    {
      time_gmac();
      printf("\n");
    }

607
  for (i = 0; ciphers[i]; i++)
608 609 610
    if (!alg || strstr(ciphers[i]->name, alg))
      time_cipher(ciphers[i]);

611 612
  return 0;
}