nettle-benchmark.c 5.45 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 29 30
#if HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

31
#include "aes.h"
Niels Möller's avatar
Niels Möller committed
32 33 34 35 36 37 38
#include "arcfour.h"
#include "blowfish.h"
#include "cast128.h"
#include "des.h"
#include "serpent.h"
#include "twofish.h"

39 40 41
#include "nettle-meta.h"
#include "nettle-internal.h"

42 43
#include "cbc.h"

Niels Möller's avatar
Niels Möller committed
44
#include <assert.h>
45 46 47 48 49 50 51
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>

Niels Möller's avatar
Niels Möller committed
52 53 54 55 56 57
#if __GNUC__
# define UNUSED __attribute__ ((__unused__))
#else
# define UNUSED
#endif

Niels Möller's avatar
Niels Möller committed
58
/* Encrypt 10MB, 1K at a time. */
Niels Möller's avatar
Niels Möller committed
59 60
#define BENCH_BLOCK 1024
#define BENCH_COUNT 10240
61

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

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static double
time_function(void (*f)(void *arg), void *arg)
{
  clock_t before;
  clock_t after;

  before = clock();
  
  f(arg);
  
  after = clock();

  return ((double)(after - before)) / CLOCKS_PER_SEC;
}

struct bench_cipher_info
{
  void *ctx;
83
  nettle_crypt_func crypt;
84 85 86 87 88 89 90 91 92
  uint8_t *data;
};

static void
bench_cipher(void *arg)
{
  struct bench_cipher_info *info = arg;
  unsigned i;
  
Niels Möller's avatar
Niels Möller committed
93 94
  for (i = 0; i<BENCH_COUNT; i++)
    info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
95 96 97 98 99
}

struct bench_cbc_info
{
  void *ctx;
100 101
  nettle_crypt_func crypt;
 
102
  uint8_t *data;
103
  
104 105 106 107 108 109 110 111 112 113
  unsigned block_size;
  uint8_t *iv;
};

static void
bench_cbc_encrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
  unsigned i;

Niels Möller's avatar
Niels Möller committed
114
  for (i = 0; i<BENCH_COUNT; i++)
115 116
    cbc_encrypt(info->ctx, info->crypt,
		info->block_size, info->iv,
Niels Möller's avatar
Niels Möller committed
117
		BENCH_BLOCK, info->data, info->data);
118 119 120 121 122 123 124 125
}

static void
bench_cbc_decrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
  unsigned i;

Niels Möller's avatar
Niels Möller committed
126
  for (i = 0; i<BENCH_COUNT; i++)
127 128
    cbc_decrypt(info->ctx, info->crypt,
		info->block_size, info->iv,
Niels Möller's avatar
Niels Möller committed
129
		BENCH_BLOCK, info->data, info->data);
130 131 132 133 134 135 136
}

/* 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
137
  for (i = j = 0; i<BENCH_BLOCK;  i++)
138 139 140 141 142 143 144 145
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
146 147
init_key(unsigned length,
         uint8_t *key)
148
{
Niels Möller's avatar
Niels Möller committed
149 150 151
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
152 153
}

Niels Möller's avatar
Niels Möller committed
154 155 156 157 158 159 160 161 162
static void
display(const char *name, const char *mode,
	double elapsed)
{
  printf("%13s (%s): %.2fs, %.3fMB/s\n",
	 name, mode,
	 elapsed, BENCH_TOTAL / elapsed);
}

Niels Möller's avatar
Niels Möller committed
163
static void
164
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
165 166 167 168 169 170 171 172 173
{
  void *ctx = alloca(cipher->context_size);
  uint8_t *key = alloca(cipher->key_size);

  uint8_t data[BENCH_BLOCK];

  printf("\n");
  
  init_data(data);
174 175

  {
Niels Möller's avatar
Niels Möller committed
176 177 178 179 180
    /* 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;
181
    
Niels Möller's avatar
Niels Möller committed
182
    init_key(cipher->key_size, key);
183
    cipher->set_encrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
184 185 186

    display(cipher->name, "ECB encrypt",
	    time_function(bench_cipher, &info));
187
  }
Niels Möller's avatar
Niels Möller committed
188
  
189
  {
Niels Möller's avatar
Niels Möller committed
190 191 192 193
    struct bench_cipher_info info;
    info.ctx = ctx;
    info.crypt = cipher->decrypt;
    info.data = data;
194
    
Niels Möller's avatar
Niels Möller committed
195
    init_key(cipher->key_size, key);
196
    cipher->set_decrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
197 198 199

    display(cipher->name, "ECB decrypt",
	    time_function(bench_cipher, &info));
200 201
  }

Niels Möller's avatar
Niels Möller committed
202 203 204 205 206 207
  if (cipher->block_size)
    {
      uint8_t *iv = alloca(cipher->block_size);
      
      /* Do CBC mode */
      {
Niels Möller's avatar
Niels Möller committed
208 209 210 211 212 213
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->encrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
214
    
Niels Möller's avatar
Niels Möller committed
215
        memset(iv, 0, sizeof(iv));
216
    
217
        cipher->set_encrypt_key(ctx, cipher->key_size, key);
218

Niels Möller's avatar
Niels Möller committed
219 220
	display(cipher->name, "CBC encrypt",
		time_function(bench_cbc_encrypt, &info));
Niels Möller's avatar
Niels Möller committed
221
      }
222

Niels Möller's avatar
Niels Möller committed
223
      {
Niels Möller's avatar
Niels Möller committed
224 225 226 227 228 229
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->decrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
230
    
Niels Möller's avatar
Niels Möller committed
231
        memset(iv, 0, sizeof(iv));
232

233
        cipher->set_decrypt_key(ctx, cipher->key_size, key);
234

Niels Möller's avatar
Niels Möller committed
235 236
	display(cipher->name, "CBC decrypt",
		time_function(bench_cbc_decrypt, &info));
Niels Möller's avatar
Niels Möller committed
237 238 239 240
      }
    }
}

241 242 243 244 245
#if HAVE_LIBCRYPTO
# define OPENSSL(x) x,
#else
# define OPENSSL(x)
#endif
Niels Möller's avatar
Niels Möller committed
246 247

int
Niels Möller's avatar
Niels Möller committed
248
main(int argc UNUSED, char **argv UNUSED)
Niels Möller's avatar
Niels Möller committed
249 250
{
  unsigned i;
251
  const struct nettle_cipher *ciphers[] =
Niels Möller's avatar
Niels Möller committed
252
    {
253 254 255
      &nettle_aes128, &nettle_aes192, &nettle_aes256,
      &nettle_arcfour128,
      &nettle_blowfish128,
256 257 258 259
      OPENSSL(&nettle_openssl_blowfish128)
      &nettle_cast128, OPENSSL(&nettle_openssl_cast128)
      &nettle_des, OPENSSL(&nettle_openssl_des)
      &nettle_des3,
260 261
      &nettle_serpent256,
      &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
262
      NULL
Niels Möller's avatar
Niels Möller committed
263
    };
264

265
  for (i = 0; ciphers[i]; i++)
266
    time_cipher(ciphers[i]);
Niels Möller's avatar
Niels Möller committed
267
  
268 269
  return 0;
}