3 * Tries the performance of the various algorithms.
7 /* nettle, low-level cryptographics library
9 * Copyright (C) 2001, 2010 Niels Möller
11 * The nettle library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or (at your
14 * option) any later version.
16 * The nettle library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 * License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with the nettle library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
50 #include "nettle-meta.h"
51 #include "nettle-internal.h"
55 static double frequency = 0.0;
57 /* Process BENCH_BLOCK bytes at a time, for BENCH_INTERVAL clocks. */
58 #define BENCH_BLOCK 10240
59 #define BENCH_INTERVAL (CLOCKS_PER_SEC / 4)
61 /* Total MB:s, for MB/s figures. */
62 #define BENCH_TOTAL 10.0
64 /* FIXME: Proper configure test for rdtsc? */
65 #ifndef WITH_CYCLE_COUNTER
66 # if defined(__GNUC__) && defined(__i386__)
67 # define WITH_CYCLE_COUNTER 1
69 # define WITH_CYCLE_COUNTER 0
73 #if WITH_CYCLE_COUNTER
74 #define GET_CYCLE_COUNTER(hi, lo) \
75 __asm__("xorl %%eax,%%eax\n" \
76 "movl %%ebx, %%edi\n" \
79 "movl %%edi, %%ebx\n" \
80 : "=a" (lo), "=d" (hi) \
82 : "%edi", "%ecx", "cc")
83 #define BENCH_ITERATIONS 10
86 /* Returns second per function call */
88 time_function(void (*f)(void *arg), void *arg)
96 done = before + BENCH_INTERVAL;
105 while (after < done);
107 return ((double)(after - before)) / CLOCKS_PER_SEC / ncalls;
110 struct bench_hash_info
113 nettle_hash_update_func *update;
118 bench_hash(void *arg)
120 struct bench_hash_info *info = arg;
121 info->update(info->ctx, BENCH_BLOCK, info->data);
124 struct bench_cipher_info
127 nettle_crypt_func *crypt;
132 bench_cipher(void *arg)
134 struct bench_cipher_info *info = arg;
135 info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
138 struct bench_cbc_info
141 nettle_crypt_func *crypt;
150 bench_cbc_encrypt(void *arg)
152 struct bench_cbc_info *info = arg;
153 cbc_encrypt(info->ctx, info->crypt,
154 info->block_size, info->iv,
155 BENCH_BLOCK, info->data, info->data);
159 bench_cbc_decrypt(void *arg)
161 struct bench_cbc_info *info = arg;
162 cbc_decrypt(info->ctx, info->crypt,
163 info->block_size, info->iv,
164 BENCH_BLOCK, info->data, info->data);
167 /* Set data[i] = floor(sqrt(i)) */
169 init_data(uint8_t *data)
172 for (i = j = 0; i<BENCH_BLOCK; i++)
181 init_key(unsigned length,
185 for (i = 0; i<length; i++)
192 printf("%18s %11s Mbyte/s%s\n",
194 frequency > 0.0 ? " cycles/byte cycles/block" : "");
198 display(const char *name, const char *mode, unsigned block_size,
201 printf("%18s %11s %7.2f",
203 BENCH_BLOCK / (time * 1048576.0));
206 printf(" %11.2f", time * frequency / BENCH_BLOCK);
208 printf(" %12.2f", time * frequency * block_size / BENCH_BLOCK);
216 void *p = malloc(size);
219 fprintf(stderr, "Virtual memory exhausted.\n");
227 time_hash(const struct nettle_hash *hash)
229 static uint8_t data[BENCH_BLOCK];
230 struct bench_hash_info info;
231 info.ctx = xalloc(hash->context_size);
232 info.update = hash->update;
236 hash->init(info.ctx);
238 display(hash->name, "update", hash->block_size,
239 time_function(bench_hash, &info));
245 time_cipher(const struct nettle_cipher *cipher)
247 void *ctx = xalloc(cipher->context_size);
248 uint8_t *key = xalloc(cipher->key_size);
250 static uint8_t data[BENCH_BLOCK];
257 /* Decent initializers are a GNU extension, so don't use it here. */
258 struct bench_cipher_info info;
260 info.crypt = cipher->encrypt;
263 init_key(cipher->key_size, key);
264 cipher->set_encrypt_key(ctx, cipher->key_size, key);
266 display(cipher->name, "ECB encrypt", cipher->block_size,
267 time_function(bench_cipher, &info));
271 struct bench_cipher_info info;
273 info.crypt = cipher->decrypt;
276 init_key(cipher->key_size, key);
277 cipher->set_decrypt_key(ctx, cipher->key_size, key);
279 display(cipher->name, "ECB decrypt", cipher->block_size,
280 time_function(bench_cipher, &info));
283 /* Don't use nettle cbc to benchmark openssl ciphers */
284 if (cipher->block_size && cipher->name[0] != 'o')
286 uint8_t *iv = xalloc(cipher->block_size);
290 struct bench_cbc_info info;
292 info.crypt = cipher->encrypt;
294 info.block_size = cipher->block_size;
297 memset(iv, 0, sizeof(iv));
299 cipher->set_encrypt_key(ctx, cipher->key_size, key);
301 display(cipher->name, "CBC encrypt", cipher->block_size,
302 time_function(bench_cbc_encrypt, &info));
306 struct bench_cbc_info info;
308 info.crypt = cipher->decrypt;
310 info.block_size = cipher->block_size;
313 memset(iv, 0, sizeof(iv));
315 cipher->set_decrypt_key(ctx, cipher->key_size, key);
317 display(cipher->name, "CBC decrypt", cipher->block_size,
318 time_function(bench_cbc_decrypt, &info));
327 compare_double(const void *ap, const void *bp)
329 double a = *(const double *) ap;
330 double b = *(const double *) bp;
339 /* Try to get accurate cycle times for assembler functions. */
341 bench_sha1_compress(void)
343 #if WITH_CYCLE_COUNTER
344 uint32_t state[_SHA1_DIGEST_LENGTH];
345 uint8_t data[BENCH_ITERATIONS * SHA1_DATA_SIZE];
346 uint32_t start_lo, start_hi, end_lo, end_hi;
353 for (j = 0; j < 5; j++)
357 GET_CYCLE_COUNTER(start_hi, start_lo);
358 for (; i < BENCH_ITERATIONS; i++, p += SHA1_DATA_SIZE)
359 _nettle_sha1_compress(state, p);
361 GET_CYCLE_COUNTER(end_hi, end_lo);
363 end_hi -= (start_hi + (start_lo > end_lo));
366 count[j] = ldexp(end_hi, 32) + end_lo;
369 qsort(count, 5, sizeof(double), compare_double);
370 printf("sha1_compress: %.2f cycles\n\n", count[2] / BENCH_ITERATIONS);
375 # define OPENSSL(x) x,
381 main(int argc, char **argv)
386 const struct nettle_hash *hashes[] =
388 &nettle_md2, &nettle_md4, &nettle_md5,
389 OPENSSL(&nettle_openssl_md5)
390 &nettle_sha1, OPENSSL(&nettle_openssl_sha1)
391 &nettle_sha224, &nettle_sha256,
392 &nettle_sha384, &nettle_sha512,
396 const struct nettle_cipher *ciphers[] =
398 &nettle_aes128, &nettle_aes192, &nettle_aes256,
399 OPENSSL(&nettle_openssl_aes128)
400 OPENSSL(&nettle_openssl_aes192)
401 OPENSSL(&nettle_openssl_aes256)
402 &nettle_arcfour128, OPENSSL(&nettle_openssl_arcfour128)
403 &nettle_blowfish128, OPENSSL(&nettle_openssl_blowfish128)
404 &nettle_camellia128, &nettle_camellia192, &nettle_camellia256,
405 &nettle_cast128, OPENSSL(&nettle_openssl_cast128)
406 &nettle_des, OPENSSL(&nettle_openssl_des)
409 &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
413 while ( (c = getopt(argc, argv, "f:")) != -1)
417 frequency = atof(optarg);
422 fprintf(stderr, "Usage: nettle-benchmark [-f clock frequency]\n");
429 bench_sha1_compress();
433 for (i = 0; hashes[i]; i++)
434 time_hash(hashes[i]);
436 for (i = 0; ciphers[i]; i++)
437 time_cipher(ciphers[i]);