nettle-benchmark.c 22.9 KB
Newer Older
1
/* nettle-benchmark.c
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   
   Tests the performance of the various algorithms.

   Copyright (C) 2001, 2010, 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle 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
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
33

34
35
#if HAVE_CONFIG_H
# include "config.h"
36
37
38
39
#endif

#include <assert.h>
#include <errno.h>
40
#include <math.h>
Niels Möller's avatar
Niels Möller committed
41
#include <stdarg.h>
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
47

48
49
#include "timing.h"

50
#include "aes.h"
Niels Möller's avatar
Niels Möller committed
51
52
53
#include "arcfour.h"
#include "blowfish.h"
#include "cast128.h"
Niels Möller's avatar
Niels Möller committed
54
#include "cbc.h"
Niels Möller's avatar
Niels Möller committed
55
#include "ctr.h"
Niels Möller's avatar
Niels Möller committed
56
#include "des.h"
57
#include "eax.h"
Niels Möller's avatar
Niels Möller committed
58
#include "gcm.h"
59
#include "memxor.h"
60
#include "salsa20.h"
61
#include "salsa20-internal.h"
Niels Möller's avatar
Niels Möller committed
62
#include "serpent.h"
63
64
#include "sha1.h"
#include "sha2.h"
Niels Möller's avatar
Niels Möller committed
65
#include "sha3.h"
Niels Möller's avatar
Niels Möller committed
66
#include "twofish.h"
Niels Möller's avatar
Niels Möller committed
67
#include "umac.h"
68
#include "cmac.h"
69
#include "poly1305.h"
70
#include "hmac.h"
Niels Möller's avatar
Niels Möller committed
71

72
73
74
#include "nettle-meta.h"
#include "nettle-internal.h"

Niels Möller's avatar
Niels Möller committed
75
#include "getopt.h"
76

77
static double frequency = 0.0;
Niels Möller's avatar
Niels Möller committed
78

79
/* Process BENCH_BLOCK bytes at a time, for BENCH_INTERVAL seconds. */
80
#define BENCH_BLOCK 10240
81
#define BENCH_INTERVAL 0.1
Niels Möller's avatar
Niels Möller committed
82

83
84
/* FIXME: Proper configure test for rdtsc? */
#ifndef WITH_CYCLE_COUNTER
85
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
86
87
88
89
90
91
92
#  define WITH_CYCLE_COUNTER 1
# else
#  define WITH_CYCLE_COUNTER 0
# endif
#endif

#if WITH_CYCLE_COUNTER
93
# if defined(__i386__)
94
#define GET_CYCLE_COUNTER(hi, lo)		\
95
96
97
98
99
100
101
102
  __asm__ volatile ("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")
103
104
# elif defined(__x86_64__)
#define GET_CYCLE_COUNTER(hi, lo)		\
105
106
107
108
109
110
111
112
  __asm__ volatile ("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")
113
# endif
114
115
116
#define BENCH_ITERATIONS 10
#endif

117
static void NORETURN PRINTF_STYLE(1,2)
Niels Möller's avatar
Niels Möller committed
118
119
120
121
122
123
124
125
126
127
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  exit(EXIT_FAILURE);
}

128
129
static double overhead = 0.0; 

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

137
  for (ncalls = 10 ;;)
138
    {
139
      unsigned i;
140
141

      time_start();
142
143
      for (i = 0; i < ncalls; i++)
	f(arg);
144
      elapsed = time_end();
145
146
147
148
149
150
      if (elapsed > BENCH_INTERVAL)
	break;
      else if (elapsed < BENCH_INTERVAL / 10)
	ncalls *= 10;
      else
	ncalls *= 2;
151
    }
152
153
154
155
156
157
158
  return elapsed / ncalls - overhead;
}

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

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

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

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

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

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

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

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

struct bench_cbc_info
{
  void *ctx;
213
  nettle_cipher_func *crypt;
214
 
215
216
217
  const uint8_t *src;
  uint8_t *dst;

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
  cbc_encrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
228
	      BENCH_BLOCK, info->dst, info->src);
229
230
231
232
233
234
}

static void
bench_cbc_decrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
235
236
  cbc_decrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
237
	      BENCH_BLOCK, info->dst, info->src);
238
239
}

Niels Möller's avatar
Niels Möller committed
240
241
242
243
244
245
static void
bench_ctr(void *arg)
{
  struct bench_cbc_info *info = arg;
  ctr_crypt(info->ctx, info->crypt,
	    info->block_size, info->iv,
246
	    BENCH_BLOCK, info->dst, info->src);
Niels Möller's avatar
Niels Möller committed
247
248
}

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
struct bench_aead_info
{
  void *ctx;
  nettle_crypt_func *crypt;
  nettle_hash_update_func *update;
  uint8_t *data;
};

static void
bench_aead_crypt(void *arg)
{
  const struct bench_aead_info *info = arg;
  info->crypt (info->ctx, BENCH_BLOCK, info->data, info->data);
}

static void
bench_aead_update(void *arg)
{
  const struct bench_aead_info *info = arg;
  info->update (info->ctx, BENCH_BLOCK, info->data);
}

271
272
273
274
275
/* 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
276
  for (i = j = 0; i<BENCH_BLOCK;  i++)
277
278
279
280
281
282
283
284
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
285
286
init_key(unsigned length,
         uint8_t *key)
287
{
Niels Möller's avatar
Niels Möller committed
288
289
290
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
291
292
}

293
294
295
296
297
298
299
300
301
static void
init_nonce(unsigned length,
	   uint8_t *nonce)
{
  unsigned i;
  for (i = 0; i<length; i++)
    nonce[i] = 3*i;
}

302
303
304
static void
header(void)
{
305
  printf("%18s %12s Mbyte/s%s\n",
306
	 "Algorithm", "mode", 
Niels Möller's avatar
Niels Möller committed
307
	 frequency > 0.0 ? " cycles/byte cycles/block" : "");  
308
309
}

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

326
327
328
329
330
static void *
xalloc(size_t size)
{
  void *p = malloc(size);
  if (!p)
331
    die("Virtual memory exhausted.\n");
332
333
334
335

  return p;
}

336
337
338
339
340
341
342
343
344
345
346
347
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");  
}



348
349
350
351
static void
time_memxor(void)
{
  struct bench_memxor_info info;
352
353
354
  unsigned long src[BENCH_BLOCK / sizeof(long) + 2];
  unsigned long other[BENCH_BLOCK / sizeof(long) + 2];
  unsigned long dst[BENCH_BLOCK / sizeof(long) + 1];
355
356
357
358

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

359
360
  display ("memxor", "aligned", sizeof(unsigned long),
	   time_function(bench_memxor, &info));
361
  info.src = (const char *) src + 1;
362
  display ("memxor", "unaligned", sizeof(unsigned long),
363
364
365
366
367
368
369
	   time_function(bench_memxor, &info));

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

370
  info.other = (const char *) other + 1;
371
372
  display ("memxor3", "unaligned01", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));
373
  info.src = (const char *) src + 1;
374
375
  display ("memxor3", "unaligned11", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));
376
  info.other = (const char *) other + 2;
377
378
  display ("memxor3", "unaligned12", sizeof(unsigned long),
	   time_function(bench_memxor3, &info));  
379
380
}

381
382
383
384
385
static void
time_hash(const struct nettle_hash *hash)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
386

387
  info.ctx = xalloc(hash->context_size); 
388
389
390
391
392
393
  info.update = hash->update;
  info.data = data;

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

Niels Möller's avatar
Niels Möller committed
394
  display(hash->name, "update", hash->block_size,
395
	  time_function(bench_hash, &info));
396
397

  free(info.ctx);
398
399
}

Niels Möller's avatar
Niels Möller committed
400
401
402
403
404
405
406
407
408
static void
time_umac(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
  struct umac32_ctx ctx32;
  struct umac64_ctx ctx64;
  struct umac96_ctx ctx96;
  struct umac128_ctx ctx128;
409

Niels Möller's avatar
Niels Möller committed
410
411
412
413
414
415
416
  uint8_t key[16];

  umac32_set_key (&ctx32, key);
  info.ctx = &ctx32;
  info.update = (nettle_hash_update_func *) umac32_update;
  info.data = data;

417
  display("umac32", "update", UMAC_BLOCK_SIZE,
Niels Möller's avatar
Niels Möller committed
418
419
420
421
422
423
424
	  time_function(bench_hash, &info));

  umac64_set_key (&ctx64, key);
  info.ctx = &ctx64;
  info.update = (nettle_hash_update_func *) umac64_update;
  info.data = data;

425
  display("umac64", "update", UMAC_BLOCK_SIZE,
Niels Möller's avatar
Niels Möller committed
426
427
428
429
430
431
432
	  time_function(bench_hash, &info));

  umac96_set_key (&ctx96, key);
  info.ctx = &ctx96;
  info.update = (nettle_hash_update_func *) umac96_update;
  info.data = data;

433
  display("umac96", "update", UMAC_BLOCK_SIZE,
Niels Möller's avatar
Niels Möller committed
434
435
436
437
438
439
440
	  time_function(bench_hash, &info));

  umac128_set_key (&ctx128, key);
  info.ctx = &ctx128;
  info.update = (nettle_hash_update_func *) umac128_update;
  info.data = data;

441
  display("umac128", "update", UMAC_BLOCK_SIZE,
Niels Möller's avatar
Niels Möller committed
442
443
444
	  time_function(bench_hash, &info));
}

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
static void
time_cmac(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
  struct cmac_aes128_ctx ctx;

  uint8_t key[16];

  cmac_aes128_set_key (&ctx, key);
  info.ctx = &ctx;
  info.update = (nettle_hash_update_func *) cmac_aes128_update;
  info.data = data;

  display("cmac-aes128", "update", AES_BLOCK_SIZE,
	  time_function(bench_hash, &info));
}

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
static void
time_poly1305_aes(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
  struct poly1305_aes_ctx ctx;
  uint8_t key[32];

  poly1305_aes_set_key (&ctx, key);
  info.ctx = &ctx;
  info.update = (nettle_hash_update_func *) poly1305_aes_update;
  info.data = data;

  display("poly1305-aes", "update", 1024,
	  time_function(bench_hash, &info));
}

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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
struct bench_hmac_info
{
  void *ctx;
  nettle_hash_update_func *update;
  nettle_hash_digest_func *digest;
  size_t length;
  size_t digest_length;
  const uint8_t *data;
};

static void
bench_hmac(void *arg)
{
  struct bench_hmac_info *info = arg;
  uint8_t digest[NETTLE_MAX_HASH_DIGEST_SIZE];
  size_t pos, length;

  length = info->length;
  for (pos = 0; pos < BENCH_BLOCK; pos += length)
    {
      size_t single = pos + length < BENCH_BLOCK ?
			length :
			BENCH_BLOCK - pos;
      info->update(info->ctx, single, info->data + pos);
      info->digest(info->ctx, info->digest_length, digest);
    }
}

static const struct
{
  size_t length;
  const char *msg;
} hmac_tests[] = {
  { 64, "64 bytes" },
  { 256, "256 bytes" },
  { 1024, "1024 bytes" },
  { 4096, "4096 bytes" },
  { BENCH_BLOCK, "single msg" },
  { 0, NULL },
};

static void
time_hmac_md5(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hmac_info info;
  struct hmac_md5_ctx md5_ctx;
  unsigned int pos;

  init_data(data);
  info.data = data;

  hmac_md5_set_key(&md5_ctx, MD5_BLOCK_SIZE, data);
  info.ctx = &md5_ctx;
  info.update = (nettle_hash_update_func *) hmac_md5_update;
  info.digest = (nettle_hash_digest_func *) hmac_md5_digest;
  info.digest_length = MD5_DIGEST_SIZE;

  for (pos = 0; hmac_tests[pos].length != 0; pos++)
    {
      info.length = hmac_tests[pos].length;
      display("hmac-md5", hmac_tests[pos].msg, MD5_BLOCK_SIZE,
	      time_function(bench_hmac, &info));
    }
}

static void
time_hmac_sha1(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hmac_info info;
  struct hmac_sha1_ctx sha1_ctx;
  unsigned int pos;

  init_data(data);
  info.data = data;

  hmac_sha1_set_key(&sha1_ctx, SHA1_BLOCK_SIZE, data);
  info.ctx = &sha1_ctx;
  info.update = (nettle_hash_update_func *) hmac_sha1_update;
  info.digest = (nettle_hash_digest_func *) hmac_sha1_digest;
  info.digest_length = SHA1_DIGEST_SIZE;

  for (pos = 0; hmac_tests[pos].length != 0; pos++)
    {
      info.length = hmac_tests[pos].length;
      display("hmac-sha1", hmac_tests[pos].msg, SHA1_BLOCK_SIZE,
	      time_function(bench_hmac, &info));
    }
}

static void
time_hmac_sha256(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hmac_info info;
  struct hmac_sha256_ctx sha256_ctx;
  unsigned int pos;

  init_data(data);
  info.data = data;

  hmac_sha256_set_key(&sha256_ctx, SHA256_BLOCK_SIZE, data);
  info.ctx = &sha256_ctx;
  info.update = (nettle_hash_update_func *) hmac_sha256_update;
  info.digest = (nettle_hash_digest_func *) hmac_sha256_digest;
  info.digest_length = SHA256_DIGEST_SIZE;

  for (pos = 0; hmac_tests[pos].length != 0; pos++)
    {
      info.length = hmac_tests[pos].length;
      display("hmac-sha256", hmac_tests[pos].msg, SHA256_BLOCK_SIZE,
	      time_function(bench_hmac, &info));
    }
}

static void
time_hmac_sha512(void)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hmac_info info;
  struct hmac_sha512_ctx sha512_ctx;
  unsigned int pos;

  init_data(data);
  info.data = data;

  hmac_sha512_set_key(&sha512_ctx, SHA512_BLOCK_SIZE, data);
  info.ctx = &sha512_ctx;
  info.update = (nettle_hash_update_func *) hmac_sha512_update;
  info.digest = (nettle_hash_digest_func *) hmac_sha512_digest;
  info.digest_length = SHA512_DIGEST_SIZE;

  for (pos = 0; hmac_tests[pos].length != 0; pos++)
    {
      info.length = hmac_tests[pos].length;
      display("hmac-sha512", hmac_tests[pos].msg, SHA512_BLOCK_SIZE,
	      time_function(bench_hmac, &info));
    }
}

621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
638
static void
639
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
640
{
641
642
  void *ctx = xalloc(cipher->context_size);
  uint8_t *key = xalloc(cipher->key_size);
Niels Möller's avatar
Niels Möller committed
643

644
  static uint8_t src_data[BENCH_BLOCK];
645
  static uint8_t data[BENCH_BLOCK];
Niels Möller's avatar
Niels Möller committed
646
647
648
649

  printf("\n");
  
  init_data(data);
650
  init_data(src_data);
651
652

  {
Niels Möller's avatar
Niels Möller committed
653
654
655
656
657
    /* 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;
658
    
Niels Möller's avatar
Niels Möller committed
659
    init_key(cipher->key_size, key);
660
    cipher->set_encrypt_key(ctx, key);
Niels Möller's avatar
Niels Möller committed
661

Niels Möller's avatar
Niels Möller committed
662
    display(cipher->name, "ECB encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
663
	    time_function(bench_cipher, &info));
664
  }
Niels Möller's avatar
Niels Möller committed
665
  
666
  {
Niels Möller's avatar
Niels Möller committed
667
668
669
670
    struct bench_cipher_info info;
    info.ctx = ctx;
    info.crypt = cipher->decrypt;
    info.data = data;
671
    
Niels Möller's avatar
Niels Möller committed
672
    init_key(cipher->key_size, key);
673
    cipher->set_decrypt_key(ctx, key);
Niels Möller's avatar
Niels Möller committed
674

Niels Möller's avatar
Niels Möller committed
675
    display(cipher->name, "ECB decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
676
	    time_function(bench_cipher, &info));
677
678
  }

679
  if (block_cipher_p(cipher))
Niels Möller's avatar
Niels Möller committed
680
    {
681
      uint8_t *iv = xalloc(cipher->block_size);
Niels Möller's avatar
Niels Möller committed
682
683
684
      
      /* Do CBC mode */
      {
Niels Möller's avatar
Niels Möller committed
685
686
687
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
688
689
	info.src = src_data;
	info.dst = data;
Niels Möller's avatar
Niels Möller committed
690
691
	info.block_size = cipher->block_size;
	info.iv = iv;
692
    
693
        memset(iv, 0, cipher->block_size);
694
    
695
        cipher->set_encrypt_key(ctx, key);
696

Niels Möller's avatar
Niels Möller committed
697
	display(cipher->name, "CBC encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
698
		time_function(bench_cbc_encrypt, &info));
Niels Möller's avatar
Niels Möller committed
699
      }
700

Niels Möller's avatar
Niels Möller committed
701
      {
Niels Möller's avatar
Niels Möller committed
702
703
704
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->decrypt;
705
706
	info.src = src_data;
	info.dst = data;
Niels Möller's avatar
Niels Möller committed
707
708
	info.block_size = cipher->block_size;
	info.iv = iv;
709
    
710
        memset(iv, 0, cipher->block_size);
711

712
        cipher->set_decrypt_key(ctx, key);
713

Niels Möller's avatar
Niels Möller committed
714
	display(cipher->name, "CBC decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
715
		time_function(bench_cbc_decrypt, &info));
716
717
718
719
720
721

	memset(iv, 0, cipher->block_size);
	info.src = data;

	display(cipher->name, "  (in-place)", cipher->block_size,
		time_function(bench_cbc_decrypt, &info));
Niels Möller's avatar
Niels Möller committed
722
      }
Niels Möller's avatar
Niels Möller committed
723
724
725
726
727
728

      /* Do CTR mode */
      {
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
729
730
	info.src = src_data;
	info.dst = data;
Niels Möller's avatar
Niels Möller committed
731
732
733
	info.block_size = cipher->block_size;
	info.iv = iv;
    
734
        memset(iv, 0, cipher->block_size);
Niels Möller's avatar
Niels Möller committed
735
    
736
        cipher->set_encrypt_key(ctx, key);
Niels Möller's avatar
Niels Möller committed
737
738
739

	display(cipher->name, "CTR", cipher->block_size,
		time_function(bench_ctr, &info));	
740
741
742
743
744
745

	memset(iv, 0, cipher->block_size);
	info.src = data;

	display(cipher->name, "  (in-place)", cipher->block_size,
		time_function(bench_ctr, &info));
Niels Möller's avatar
Niels Möller committed
746
747
      }
      
748
      free(iv);
Niels Möller's avatar
Niels Möller committed
749
    }
750
751
  free(ctx);
  free(key);
Niels Möller's avatar
Niels Möller committed
752
753
}

754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
static void
time_aead(const struct nettle_aead *aead)
{
  void *ctx = xalloc(aead->context_size);
  uint8_t *key = xalloc(aead->key_size);
  uint8_t *nonce = xalloc(aead->nonce_size);
  static uint8_t data[BENCH_BLOCK];

  printf("\n");
  
  init_data(data);
  if (aead->set_nonce)
    init_nonce (aead->nonce_size, nonce);

  {
    /* Decent initializers are a GNU extension, so don't use it here. */
    struct bench_aead_info info;
    info.ctx = ctx;
    info.crypt = aead->encrypt;
    info.data = data;
    
    init_key(aead->key_size, key);
    aead->set_encrypt_key(ctx, key);
    if (aead->set_nonce)
      aead->set_nonce (ctx, nonce);

    display(aead->name, "encrypt", aead->block_size,
	    time_function(bench_aead_crypt, &info));
  }
  
  {
    struct bench_aead_info info;
    info.ctx = ctx;
    info.crypt = aead->decrypt;
    info.data = data;
    
    init_key(aead->key_size, key);
    aead->set_decrypt_key(ctx, key);
    if (aead->set_nonce)
      aead->set_nonce (ctx, nonce);

    display(aead->name, "decrypt", aead->block_size,
	    time_function(bench_aead_crypt, &info));
  }

  if (aead->update)
    {
      struct bench_aead_info info;
      info.ctx = ctx;
      info.update = aead->update;
      info.data = data;

      aead->set_encrypt_key(ctx, key);

      if (aead->set_nonce)
	aead->set_nonce (ctx, nonce);
    
      display(aead->name, "update", aead->block_size,
	      time_function(bench_aead_update, &info));
    }
  free(ctx);
  free(key);
  free(nonce);
}

Niels Möller's avatar
Niels Möller committed
819
820
/* Try to get accurate cycle times for assembler functions. */
#if WITH_CYCLE_COUNTER
821
822
823
824
825
826
827
828
829
830
831
832
833
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#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)

856
857
858
859
static void
bench_sha1_compress(void)
{
  uint32_t state[_SHA1_DIGEST_LENGTH];
860
  uint8_t data[SHA1_BLOCK_SIZE];
Niels Möller's avatar
Niels Möller committed
861
  double t;
862

863
  TIME_CYCLES (t, nettle_sha1_compress(state, data));
864

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

868
869
870
871
static void
bench_salsa20_core(void)
{
  uint32_t state[_SALSA20_INPUT_LENGTH];
Niels Möller's avatar
Niels Möller committed
872
  double t;
873

Niels Möller's avatar
Niels Möller committed
874
875
876
  TIME_CYCLES (t, _nettle_salsa20_core(state, state, 20));
  printf("salsa20_core: %.2f cycles\n", t);  
}
877

Niels Möller's avatar
Niels Möller committed
878
879
880
881
882
static void
bench_sha3_permute(void)
{
  struct sha3_state state;
  double t;
883

Niels Möller's avatar
Niels Möller committed
884
885
  TIME_CYCLES (t, sha3_permute (&state));
  printf("sha3_permute: %.2f cycles (%.2f / round)\n", t, t / 24.0);
886
887
888
889
}
#else
#define bench_sha1_compress()
#define bench_salsa20_core()
Niels Möller's avatar
Niels Möller committed
890
#define bench_sha3_permute()
891
892
#endif

Niels Möller's avatar
Niels Möller committed
893
#if WITH_OPENSSL
894
895
896
897
# define OPENSSL(x) x,
#else
# define OPENSSL(x)
#endif
Niels Möller's avatar
Niels Möller committed
898
899

int
900
main(int argc, char **argv)
Niels Möller's avatar
Niels Möller committed
901
902
{
  unsigned i;
903
  int c;
904
  const char *alg;
905

906
907
908
909
#if WITH_OPENSSL
  nettle_openssl_init();
#endif

910
911
912
  const struct nettle_hash *hashes[] =
    {
      &nettle_md2, &nettle_md4, &nettle_md5,
913
      OPENSSL(&nettle_openssl_md5)
914
      &nettle_sha1, OPENSSL(&nettle_openssl_sha1)
Niels Möller's avatar
Niels Möller committed
915
916
      &nettle_sha224, &nettle_sha256,
      &nettle_sha384, &nettle_sha512,
917
      &nettle_sha512_224, &nettle_sha512_256,
918
919
      &nettle_sha3_224, &nettle_sha3_256,
      &nettle_sha3_384, &nettle_sha3_512,
920
      &nettle_ripemd160, &nettle_gosthash94,
921
922
923
      NULL
    };

924
  const struct nettle_cipher *ciphers[] =
Niels Möller's avatar
Niels Möller committed
925
    {
926
      &nettle_aes128, &nettle_aes192, &nettle_aes256,
Niels Möller's avatar
Niels Möller committed
927
928
929
930
      OPENSSL(&nettle_openssl_aes128)
      OPENSSL(&nettle_openssl_aes192)
      OPENSSL(&nettle_openssl_aes256)
      &nettle_blowfish128, OPENSSL(&nettle_openssl_blowfish128)
Niels Möller's avatar
Niels Möller committed
931
      &nettle_camellia128, &nettle_camellia192, &nettle_camellia256,
932
933
934
      &nettle_cast128, OPENSSL(&nettle_openssl_cast128)
      &nettle_des, OPENSSL(&nettle_openssl_des)
      &nettle_des3,
935
936
      &nettle_serpent256,
      &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
937
938
939
940
941
      NULL
    };

  const struct nettle_aead *aeads[] =
    {
942
943
944
945
      /* Stream ciphers */
      &nettle_arcfour128, OPENSSL(&nettle_openssl_arcfour128)
      &nettle_salsa20, &nettle_salsa20r12, &nettle_chacha,
      /* Proper AEAD algorithme. */
946
947
948
      &nettle_gcm_aes128,
      &nettle_gcm_aes192,
      &nettle_gcm_aes256,
949
950
951
      OPENSSL(&nettle_openssl_gcm_aes128)
      OPENSSL(&nettle_openssl_gcm_aes192)
      OPENSSL(&nettle_openssl_gcm_aes256)
952
953
954
955
      &nettle_gcm_camellia128,
      &nettle_gcm_camellia256,
      &nettle_eax_aes128,
      &nettle_chacha_poly1305,
956
      NULL
Niels Möller's avatar
Niels Möller committed
957
    };
958

959
960
961
962
963
964
965
966
967
968
  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)
969
970
971
972
973
974
    switch (c)
      {
      case 'f':
	frequency = atof(optarg);
	if (frequency > 0.0)
	  break;
Niels Möller's avatar
Niels Möller committed
975
	/* Fall through */
976

977
      case OPT_HELP:
978
	printf("Usage: nettle-benchmark [-f clock frequency] [alg...]\n");
979
980
981
	return EXIT_SUCCESS;

      case '?':
982
983
984
985
986
987
	return EXIT_FAILURE;

      default:
	abort();
    }

988
  time_init();
989
  bench_sha1_compress();
990
  bench_salsa20_core();
Niels Möller's avatar
Niels Möller committed
991
992
  bench_sha3_permute();
  printf("\n");
993
994
  time_overhead();

995
996
  header();

997
  do
998
    {
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
      alg = argv[optind];

      if (!alg || strstr ("memxor", alg))
	{
	  time_memxor();
	  printf("\n");
	}

      for (i = 0; hashes[i]; i++)
	if (!alg || strstr(hashes[i]->name, alg))
	  time_hash(hashes[i]);

      if (!alg || strstr ("umac", alg))
	time_umac();
Niels Möller's avatar
Niels Möller committed
1013

1014
1015
1016
      if (!alg || strstr ("cmac", alg))
	time_cmac();

1017
1018
      if (!alg || strstr ("poly1305-aes", alg))
	time_poly1305_aes();
Niels Möller's avatar
Niels Möller committed
1019

1020
1021
1022
      for (i = 0; ciphers[i]; i++)
	if (!alg || strstr(ciphers[i]->name, alg))
	  time_cipher(ciphers[i]);
1023

1024
1025
1026
      for (i = 0; aeads[i]; i++)
	if (!alg || strstr(aeads[i]->name, alg))
	  time_aead(aeads[i]);
1027

1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
      if (!alg || strstr ("hmac-md5", alg))
	time_hmac_md5();

      if (!alg || strstr ("hmac-sha1", alg))
	time_hmac_sha1();

      if (!alg || strstr ("hmac-sha256", alg))
	time_hmac_sha256();

      if (!alg || strstr ("hmac-sha512", alg))
	time_hmac_sha512();

1040
1041
      optind++;
    } while (alg && argv[optind]);
Niels Möller's avatar
Niels Möller committed
1042

1043
1044
  return 0;
}