nettle-benchmark.c 5.42 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
#include "aes.h"
Niels Möller's avatar
Niels Möller committed
40
41
42
43
44
45
46
#include "arcfour.h"
#include "blowfish.h"
#include "cast128.h"
#include "des.h"
#include "serpent.h"
#include "twofish.h"

47
48
49
#include "nettle-meta.h"
#include "nettle-internal.h"

50
51
#include "cbc.h"

Niels Möller's avatar
Niels Möller committed
52

53
/* Encrypt 1K at a time, for one second */
Niels Möller's avatar
Niels Möller committed
54
#define BENCH_BLOCK 1024
55
#define BENCH_INTERVAL CLOCKS_PER_SEC
56

Niels Möller's avatar
Niels Möller committed
57
58
59
/* Total MB:s, for MB/s figures. */
#define BENCH_TOTAL 10.0

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

struct bench_cipher_info
{
  void *ctx;
87
  nettle_crypt_func crypt;
88
89
90
91
92
93
94
  uint8_t *data;
};

static void
bench_cipher(void *arg)
{
  struct bench_cipher_info *info = arg;
95
  info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
96
97
98
99
100
}

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

static void
bench_cbc_encrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
113
114
115
  cbc_encrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
116
117
118
119
120
121
}

static void
bench_cbc_decrypt(void *arg)
{
  struct bench_cbc_info *info = arg;
122
123
124
  cbc_decrypt(info->ctx, info->crypt,
	      info->block_size, info->iv,
	      BENCH_BLOCK, info->data, info->data);
125
126
127
128
129
130
131
}

/* 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
132
  for (i = j = 0; i<BENCH_BLOCK;  i++)
133
134
135
136
137
138
139
140
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
141
142
init_key(unsigned length,
         uint8_t *key)
143
{
Niels Möller's avatar
Niels Möller committed
144
145
146
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
147
148
}

Niels Möller's avatar
Niels Möller committed
149
150
static void
display(const char *name, const char *mode,
151
	double speed)
Niels Möller's avatar
Niels Möller committed
152
{
153
  printf("%13s (%s): %.3fMB/s\n",
Niels Möller's avatar
Niels Möller committed
154
	 name, mode,
155
	 1 / (speed * 1048576.0 / BENCH_BLOCK));
Niels Möller's avatar
Niels Möller committed
156
157
}

Niels Möller's avatar
Niels Möller committed
158
static void
159
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
160
161
162
163
164
165
166
167
168
{
  void *ctx = alloca(cipher->context_size);
  uint8_t *key = alloca(cipher->key_size);

  uint8_t data[BENCH_BLOCK];

  printf("\n");
  
  init_data(data);
169
170

  {
Niels Möller's avatar
Niels Möller committed
171
172
173
174
175
    /* 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;
176
    
Niels Möller's avatar
Niels Möller committed
177
    init_key(cipher->key_size, key);
178
    cipher->set_encrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
179
180
181

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

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

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

Niels Möller's avatar
Niels Möller committed
214
215
	display(cipher->name, "CBC encrypt",
		time_function(bench_cbc_encrypt, &info));
Niels Möller's avatar
Niels Möller committed
216
      }
217

Niels Möller's avatar
Niels Möller committed
218
      {
Niels Möller's avatar
Niels Möller committed
219
220
221
222
223
224
        struct bench_cbc_info info;
	info.ctx = ctx;
	info.crypt = cipher->decrypt;
	info.data = data;
	info.block_size = cipher->block_size;
	info.iv = iv;
225
    
Niels Möller's avatar
Niels Möller committed
226
        memset(iv, 0, sizeof(iv));
227

228
        cipher->set_decrypt_key(ctx, cipher->key_size, key);
229

Niels Möller's avatar
Niels Möller committed
230
231
	display(cipher->name, "CBC decrypt",
		time_function(bench_cbc_decrypt, &info));
Niels Möller's avatar
Niels Möller committed
232
233
234
235
      }
    }
}

236
237
238
239
240
#if HAVE_LIBCRYPTO
# define OPENSSL(x) x,
#else
# define OPENSSL(x)
#endif
Niels Möller's avatar
Niels Möller committed
241
242

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

260
  for (i = 0; ciphers[i]; i++)
261
    time_cipher(ciphers[i]);
Niels Möller's avatar
Niels Möller committed
262
  
263
264
  return 0;
}