nettle-benchmark.c 7.57 KB
Newer Older
1 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
/* nettle-benchmark.c
 *
 * Tries the performance of the various algorithms.
 *
 */
 
/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2001 Niels Mller
 *  
 * 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 33 34 35 36 37
#endif

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
38

39
/* For getopt */
Niels Möller's avatar
Niels Möller committed
40 41 42
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
43

44
#include "aes.h"
Niels Möller's avatar
Niels Möller committed
45 46 47 48 49 50 51
#include "arcfour.h"
#include "blowfish.h"
#include "cast128.h"
#include "des.h"
#include "serpent.h"
#include "twofish.h"

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

55 56
#include "cbc.h"

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

59 60 61
/* Process BENCH_BLOCK bytes at a time, for BENCH_INTERVAL clocks. */
#define BENCH_BLOCK 10240
#define BENCH_INTERVAL (CLOCKS_PER_SEC / 4)
62

Niels Möller's avatar
Niels Möller committed
63 64 65
/* Total MB:s, for MB/s figures. */
#define BENCH_TOTAL 10.0

66
/* Returns second per function call */
67 68 69 70 71
static double
time_function(void (*f)(void *arg), void *arg)
{
  clock_t before;
  clock_t after;
72 73 74
  clock_t done;
  unsigned ncalls;
  
75
  before = clock();
76 77
  done = before + BENCH_INTERVAL;
  ncalls = 0;
78
  
79 80 81 82 83 84 85
  do 
    {
      f(arg);
      after = clock();
      ncalls++;
    }
  while (after < done);
86
  
87
  return ((double)(after - before)) / CLOCKS_PER_SEC / ncalls;
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103
struct bench_hash_info
{
  void *ctx;
  nettle_hash_update_func update;
  const uint8_t *data;
};

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

104 105 106
struct bench_cipher_info
{
  void *ctx;
107
  nettle_crypt_func crypt;
108 109 110 111 112 113 114
  uint8_t *data;
};

static void
bench_cipher(void *arg)
{
  struct bench_cipher_info *info = arg;
115
  info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
116 117 118 119 120
}

struct bench_cbc_info
{
  void *ctx;
121 122
  nettle_crypt_func crypt;
 
123
  uint8_t *data;
124
  
125 126 127 128 129 130 131 132
  unsigned block_size;
  uint8_t *iv;
};

static void
bench_cbc_encrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
133 134 135
  cbc_encrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
136 137 138 139 140 141
}

static void
bench_cbc_decrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
142 143 144
  cbc_decrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
145 146 147 148 149 150 151
}

/* 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
152
  for (i = j = 0; i<BENCH_BLOCK;  i++)
153 154 155 156 157 158 159 160
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
161 162
init_key(unsigned length,
         uint8_t *key)
163
{
Niels Möller's avatar
Niels Möller committed
164 165 166
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
167 168
}

169 170 171 172 173
static void
header(void)
{
  printf("%18s %11s Mbyte/s%s\n",
	 "Algorithm", "mode", 
Niels Möller's avatar
Niels Möller committed
174
	 frequency > 0.0 ? " cycles/byte cycles/block" : "");  
175 176
}

Niels Möller's avatar
Niels Möller committed
177
static void
Niels Möller's avatar
Niels Möller committed
178
display(const char *name, const char *mode, unsigned block_size,
179
	double time)
Niels Möller's avatar
Niels Möller committed
180
{
181
  printf("%18s %11s %7.2f",
Niels Möller's avatar
Niels Möller committed
182
	 name, mode,
183
	 BENCH_BLOCK / (time * 1048576.0));
184
  if (frequency > 0.0)
Niels Möller's avatar
Niels Möller committed
185
    {
186
      printf(" %11.2f", time * frequency / BENCH_BLOCK);
Niels Möller's avatar
Niels Möller committed
187
      if (block_size > 0)
188
	printf(" %12.2f", time * frequency * block_size / BENCH_BLOCK);
Niels Möller's avatar
Niels Möller committed
189
    }
190
  printf("\n");
Niels Möller's avatar
Niels Möller committed
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205
static void *
xalloc(size_t size)
{
  void *p = malloc(size);
  if (!p)
    {
      fprintf(stderr, "Virtual memory exhausted.\n");
      abort();
    }

  return p;
}

206 207 208 209 210
static void
time_hash(const struct nettle_hash *hash)
{
  static uint8_t data[BENCH_BLOCK];
  struct bench_hash_info info;
211
  info.ctx = xalloc(hash->context_size); 
212 213 214 215 216 217
  info.update = hash->update;
  info.data = data;

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

Niels Möller's avatar
Niels Möller committed
218
  display(hash->name, "update", hash->block_size,
219
	  time_function(bench_hash, &info));
220 221

  free(info.ctx);
222 223
}

Niels Möller's avatar
Niels Möller committed
224
static void
225
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
226
{
227 228
  void *ctx = xalloc(cipher->context_size);
  uint8_t *key = xalloc(cipher->key_size);
Niels Möller's avatar
Niels Möller committed
229

230
  static uint8_t data[BENCH_BLOCK];
Niels Möller's avatar
Niels Möller committed
231 232 233 234

  printf("\n");
  
  init_data(data);
235 236

  {
Niels Möller's avatar
Niels Möller committed
237 238 239 240 241
    /* 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;
242
    
Niels Möller's avatar
Niels Möller committed
243
    init_key(cipher->key_size, key);
244
    cipher->set_encrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
245

Niels Möller's avatar
Niels Möller committed
246
    display(cipher->name, "ECB encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
247
	    time_function(bench_cipher, &info));
248
  }
Niels Möller's avatar
Niels Möller committed
249
  
250
  {
Niels Möller's avatar
Niels Möller committed
251 252 253 254
    struct bench_cipher_info info;
    info.ctx = ctx;
    info.crypt = cipher->decrypt;
    info.data = data;
255
    
Niels Möller's avatar
Niels Möller committed
256
    init_key(cipher->key_size, key);
257
    cipher->set_decrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
258

Niels Möller's avatar
Niels Möller committed
259
    display(cipher->name, "ECB decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
260
	    time_function(bench_cipher, &info));
261 262
  }

Niels Möller's avatar
Niels Möller committed
263 264
  /* 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
265
    {
266
      uint8_t *iv = xalloc(cipher->block_size);
Niels Möller's avatar
Niels Möller committed
267 268 269
      
      /* Do CBC mode */
      {
Niels Möller's avatar
Niels Möller committed
270 271 272 273 274 275
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
276
    
Niels Möller's avatar
Niels Möller committed
277
        memset(iv, 0, sizeof(iv));
278
    
279
        cipher->set_encrypt_key(ctx, cipher->key_size, key);
280

Niels Möller's avatar
Niels Möller committed
281
	display(cipher->name, "CBC encrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
282
		time_function(bench_cbc_encrypt, &info));
Niels Möller's avatar
Niels Möller committed
283
      }
284

Niels Möller's avatar
Niels Möller committed
285
      {
Niels Möller's avatar
Niels Möller committed
286 287 288 289 290 291
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->decrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
292
    
Niels Möller's avatar
Niels Möller committed
293
        memset(iv, 0, sizeof(iv));
294

295
        cipher->set_decrypt_key(ctx, cipher->key_size, key);
296

Niels Möller's avatar
Niels Möller committed
297
	display(cipher->name, "CBC decrypt", cipher->block_size,
Niels Möller's avatar
Niels Möller committed
298
		time_function(bench_cbc_decrypt, &info));
Niels Möller's avatar
Niels Möller committed
299
      }
300
      free(iv);
Niels Möller's avatar
Niels Möller committed
301
    }
302 303
  free(ctx);
  free(key);
Niels Möller's avatar
Niels Möller committed
304 305
}

Niels Möller's avatar
Niels Möller committed
306
#if WITH_OPENSSL
307 308 309 310
# define OPENSSL(x) x,
#else
# define OPENSSL(x)
#endif
Niels Möller's avatar
Niels Möller committed
311 312

int
Niels Möller's avatar
Niels Möller committed
313
main(int argc UNUSED, char **argv UNUSED)
Niels Möller's avatar
Niels Möller committed
314 315
{
  unsigned i;
316
  int c;
317 318 319 320

  const struct nettle_hash *hashes[] =
    {
      &nettle_md2, &nettle_md4, &nettle_md5,
321
      OPENSSL(&nettle_openssl_md5)
322 323
      &nettle_sha1, OPENSSL(&nettle_openssl_sha1)
      &nettle_sha256,
324 325 326
      NULL
    };

327
  const struct nettle_cipher *ciphers[] =
Niels Möller's avatar
Niels Möller committed
328
    {
329
      &nettle_aes128, &nettle_aes192, &nettle_aes256,
Niels Möller's avatar
Niels Möller committed
330 331 332 333 334
      OPENSSL(&nettle_openssl_aes128)
      OPENSSL(&nettle_openssl_aes192)
      OPENSSL(&nettle_openssl_aes256)
      &nettle_arcfour128, OPENSSL(&nettle_openssl_arcfour128)
      &nettle_blowfish128, OPENSSL(&nettle_openssl_blowfish128)
335 336 337
      &nettle_cast128, OPENSSL(&nettle_openssl_cast128)
      &nettle_des, OPENSSL(&nettle_openssl_des)
      &nettle_des3,
338 339
      &nettle_serpent256,
      &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
340
      NULL
Niels Möller's avatar
Niels Möller committed
341
    };
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  while ( (c = getopt(argc, argv, "f:")) != -1)
    switch (c)
      {
      case 'f':
	frequency = atof(optarg);
	if (frequency > 0.0)
	  break;

      case ':': case '?':
	fprintf(stderr, "Usage: nettle-benchmark [-f clock frequency]\n");
	return EXIT_FAILURE;

      default:
	abort();
    }

  header();

361 362 363
  for (i = 0; hashes[i]; i++)
    time_hash(hashes[i]);
  
364
  for (i = 0; ciphers[i]; i++)
365
    time_cipher(ciphers[i]);
Niels Möller's avatar
Niels Möller committed
366
  
367 368
  return 0;
}