nettle-benchmark.c 4.81 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
27
/* 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.
 */

#include "aes.h"
Niels Möller's avatar
Niels Möller committed
28
29
30
31
32
33
34
#include "arcfour.h"
#include "blowfish.h"
#include "cast128.h"
#include "des.h"
#include "serpent.h"
#include "twofish.h"

35
36
37
#include "nettle-meta.h"
#include "nettle-internal.h"

38
39
#include "cbc.h"

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

#include <time.h>

Niels Möller's avatar
Niels Möller committed
48
49
50
/* Encrypt 100MB, 1K at a time. */
#define BENCH_BLOCK 1024
#define BENCH_COUNT 10240
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

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;
70
  nettle_crypt_func crypt;
71
72
73
74
75
76
77
78
79
  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
80
81
  for (i = 0; i<BENCH_COUNT; i++)
    info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
82
83
84
85
86
}

struct bench_cbc_info
{
  void *ctx;
87
88
  nettle_crypt_func crypt;
 
89
  uint8_t *data;
90
  
91
92
93
94
95
96
97
98
99
100
  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
101
  for (i = 0; i<BENCH_COUNT; i++)
102
103
    cbc_encrypt(info->ctx, info->crypt,
		info->block_size, info->iv,
Niels Möller's avatar
Niels Möller committed
104
		BENCH_BLOCK, info->data, info->data);
105
106
107
108
109
110
111
112
}

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

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

/* 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
124
  for (i = j = 0; i<BENCH_BLOCK;  i++)
125
126
127
128
129
130
131
132
    {
      if (j*j < i)
	j++;
      data[i] = j;
    }
}

static void
Niels Möller's avatar
Niels Möller committed
133
134
init_key(unsigned length,
         uint8_t *key)
135
{
Niels Möller's avatar
Niels Möller committed
136
137
138
  unsigned i;
  for (i = 0; i<length; i++)
    key[i] = i;
139
140
}

Niels Möller's avatar
Niels Möller committed
141
static void
142
time_cipher(const struct nettle_cipher *cipher)
Niels Möller's avatar
Niels Möller committed
143
144
145
146
147
148
149
150
151
{
  void *ctx = alloca(cipher->context_size);
  uint8_t *key = alloca(cipher->key_size);

  uint8_t data[BENCH_BLOCK];

  printf("\n");
  
  init_data(data);
152
153
154

  {
    struct bench_cipher_info info
Niels Möller's avatar
Niels Möller committed
155
      = { ctx, cipher->encrypt, data };
156
    
Niels Möller's avatar
Niels Möller committed
157
    init_key(cipher->key_size, key);
158
    cipher->set_encrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
159
160
161
    
    printf("%13s (ECB encrypt): %f\n", cipher->name,
           time_function(bench_cipher, &info));
162
  }
Niels Möller's avatar
Niels Möller committed
163
  
164
165
  {
    struct bench_cipher_info info
Niels Möller's avatar
Niels Möller committed
166
      = { ctx, cipher->decrypt, data };
167
    
Niels Möller's avatar
Niels Möller committed
168
    init_key(cipher->key_size, key);
169
    cipher->set_decrypt_key(ctx, cipher->key_size, key);
Niels Möller's avatar
Niels Möller committed
170
171
172
    
    printf("%13s (ECB decrypt): %f\n", cipher->name,
           time_function(bench_cipher, &info));
173
174
  }

Niels Möller's avatar
Niels Möller committed
175
176
177
178
179
180
181
182
  if (cipher->block_size)
    {
      uint8_t *iv = alloca(cipher->block_size);
      
      /* Do CBC mode */
      {
        struct bench_cbc_info info
          = { ctx, cipher->encrypt, data, cipher->block_size, iv };
183
    
Niels Möller's avatar
Niels Möller committed
184
        memset(iv, 0, sizeof(iv));
185
    
186
        cipher->set_encrypt_key(ctx, cipher->key_size, key);
187

Niels Möller's avatar
Niels Möller committed
188
189
190
191
        printf("%13s (CBC encrypt): %f\n", cipher->name,
               time_function(bench_cbc_encrypt,
                             &info));
      }
192

Niels Möller's avatar
Niels Möller committed
193
194
195
      {
        struct bench_cbc_info info
          = { ctx, cipher->decrypt, data, cipher->block_size, iv };
196
    
Niels Möller's avatar
Niels Möller committed
197
        memset(iv, 0, sizeof(iv));
198

199
        cipher->set_decrypt_key(ctx, cipher->key_size, key);
200

Niels Möller's avatar
Niels Möller committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        printf("%13s (CBC decrypt): %f\n", cipher->name,
               time_function(bench_cbc_decrypt,
                             &info));
      }
    }
}


#define NCIPHERS 12

int
main(int argc, char **argv)
{
  unsigned i;
215
  const struct nettle_cipher *ciphers[NCIPHERS] =
Niels Möller's avatar
Niels Möller committed
216
    {
217
218
219
220
221
222
223
      &nettle_aes128, &nettle_aes192, &nettle_aes256,
      &nettle_arcfour128,
      &nettle_blowfish128,
      &nettle_cast128,
      &nettle_des, &nettle_des3,
      &nettle_serpent256,
      &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
Niels Möller's avatar
Niels Möller committed
224
    };
225

Niels Möller's avatar
Niels Möller committed
226
  for (i = 0; i<NCIPHERS; i++)
227
    time_cipher(ciphers[i]);
Niels Möller's avatar
Niels Möller committed
228
  
229
230
  return 0;
}