23 #define USE_crypto 0x01
24 #define USE_gcrypt 0x02
25 #define USE_tomcrypt 0x04
37 #define AV_READ_TIME(x) 0
47 #define MAX_INPUT_SIZE 1048576
48 #define MAX_OUTPUT_SIZE 128
82 #define IMPL_USE_lavu IMPL_USE
90 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
91 static void run_lavu_ ## suffix(uint8_t *output, \
92 const uint8_t *input, unsigned size) \
94 static struct type *h; \
95 if (!h && !(h = av_ ## namespace ## _alloc())) \
96 fatal_error("out of memory"); \
97 av_ ## namespace ## _init(h, hsize); \
98 av_ ## namespace ## _update(h, input, size); \
99 av_ ## namespace ## _final(h, output); \
110 static struct AVAES *aes;
150 #if (USE_EXT_LIBS) & USE_crypto
152 #include <openssl/md5.h>
153 #include <openssl/sha.h>
154 #include <openssl/ripemd.h>
155 #include <openssl/aes.h>
156 #include <openssl/camellia.h>
157 #include <openssl/cast.h>
159 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
160 static void run_crypto_ ## suffix(uint8_t *output, \
161 const uint8_t *input, unsigned size) \
163 function(input, size, output); \
166 DEFINE_CRYPTO_WRAPPER(md5,
MD5)
167 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
168 DEFINE_CRYPTO_WRAPPER(sha256,
SHA256)
169 DEFINE_CRYPTO_WRAPPER(sha512,
SHA512)
170 DEFINE_CRYPTO_WRAPPER(ripemd160,
RIPEMD160)
172 static
void run_crypto_aes128(
uint8_t *output,
180 for (i = 0; i <
size; i += 16)
181 AES_encrypt(input + i, output + i, &aes);
184 static void run_crypto_camellia(
uint8_t *output,
185 const uint8_t *input,
unsigned size)
187 CAMELLIA_KEY camellia;
192 for (i = 0; i <
size; i += 16)
193 Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
196 static void run_crypto_cast128(
uint8_t *output,
197 const uint8_t *input,
unsigned size)
203 for (i = 0; i <
size; i += 8)
204 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
207 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
209 #define IMPL_USE_crypto(...)
216 #if (USE_EXT_LIBS) & USE_gcrypt
220 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
221 static void run_gcrypt_ ## suffix(uint8_t *output, \
222 const uint8_t *input, unsigned size) \
224 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
227 DEFINE_GCRYPT_WRAPPER(md5,
MD5)
228 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
229 DEFINE_GCRYPT_WRAPPER(sha256,
SHA256)
230 DEFINE_GCRYPT_WRAPPER(sha512,
SHA512)
231 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
233 static
void run_gcrypt_aes128(
uint8_t *output,
234 const
uint8_t *input,
unsigned size)
236 static gcry_cipher_hd_t aes;
238 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
240 gcry_cipher_encrypt(aes, output, size, input, size);
243 static void run_gcrypt_camellia(
uint8_t *output,
244 const uint8_t *input,
unsigned size)
246 static gcry_cipher_hd_t camellia;
248 gcry_cipher_open(&camellia, GCRY_CIPHER_CAMELLIA128, GCRY_CIPHER_MODE_ECB, 0);
250 gcry_cipher_encrypt(camellia, output, size, input, size);
253 static void run_gcrypt_cast128(
uint8_t *output,
254 const uint8_t *input,
unsigned size)
256 static gcry_cipher_hd_t cast;
258 gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
260 gcry_cipher_encrypt(cast, output, size, input, size);
263 static void run_gcrypt_twofish(
uint8_t *output,
264 const uint8_t *input,
unsigned size)
266 static gcry_cipher_hd_t twofish;
268 gcry_cipher_open(&twofish, GCRY_CIPHER_TWOFISH128, GCRY_CIPHER_MODE_ECB, 0);
270 gcry_cipher_encrypt(twofish, output, size, input, size);
273 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
275 #define IMPL_USE_gcrypt(...)
282 #if (USE_EXT_LIBS) & USE_tomcrypt
284 #include <tomcrypt.h>
286 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
287 static void run_tomcrypt_ ## suffix(uint8_t *output, \
288 const uint8_t *input, unsigned size) \
291 namespace ## _init(&md); \
292 namespace ## _process(&md, input, size); \
293 namespace ## _done(&md, output); \
296 DEFINE_TOMCRYPT_WRAPPER(md5, md5,
MD5)
297 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
298 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256,
SHA256)
299 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512,
SHA512)
300 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160,
RIPEMD160)
302 static
void run_tomcrypt_aes128(
uint8_t *output,
303 const
uint8_t *input,
unsigned size)
310 for (i = 0; i <
size; i += 16)
311 aes_ecb_encrypt(input + i, output + i, &aes);
314 static void run_tomcrypt_camellia(
uint8_t *output,
315 const uint8_t *input,
unsigned size)
317 symmetric_key camellia;
322 for (i = 0; i <
size; i += 16)
323 camellia_ecb_encrypt(input + i, output + i, &camellia);
326 static void run_tomcrypt_cast128(
uint8_t *output,
327 const uint8_t *input,
unsigned size)
333 for (i = 0; i <
size; i += 8)
334 cast5_ecb_encrypt(input + i, output + i, &cast);
337 static void run_tomcrypt_twofish(
uint8_t *output,
338 const uint8_t *input,
unsigned size)
340 symmetric_key twofish;
345 for (i = 0; i <
size; i += 16)
346 twofish_ecb_encrypt(input + i, output + i, &twofish);
350 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
352 #define IMPL_USE_tomcrypt(...)
369 unsigned outlen = 0, outcrc = 0;
371 double mtime, ttime = 0, ttime2 = 0, stime;
377 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
378 outlen = strlen(impl->
output) / 2;
379 for (i = 0; i < outlen; i++) {
380 sscanf(impl->
output + i * 2,
"%02x", &val);
384 for (i = 0; i < 8; i++)
385 impl->
run(output, input, size);
386 for (i = 0; i < nruns; i++) {
387 memset(output, 0, size);
389 impl->
run(output, input, size);
391 if (outlen ? memcmp(output, outref, outlen) :
392 crc32(output, size) != outcrc) {
393 fprintf(stderr,
"Expected: ");
395 for (j = 0; j < outlen; j++)
396 fprintf(stderr,
"%02x", output[j]);
398 fprintf(stderr,
"%08x",
crc32(output, size));
399 fprintf(stderr,
"\n");
402 mtime = (double)(t1 - t0) /
size;
404 ttime2 += mtime * mtime;
409 stime = sqrt(ttime2 - ttime * ttime);
410 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
411 impl->
lib, impl->
name, size, nruns, ttime, stime);
415 #define IMPL_USE(lib, name, symbol, output) \
416 { #lib, name, run_ ## lib ## _ ## symbol, output },
417 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
418 #define IMPL_ALL(...) \
419 IMPL(lavu, __VA_ARGS__) \
420 IMPL(crypto, __VA_ARGS__) \
421 IMPL(gcrypt, __VA_ARGS__) \
422 IMPL(tomcrypt, __VA_ARGS__)
425 IMPL_ALL(
"MD5", md5,
"aa26ff5b895356bcffd9292ba9f89e66")
426 IMPL_ALL(
"SHA-1", sha1,
"1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
427 IMPL_ALL(
"SHA-256", sha256,
"14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
428 IMPL_ALL(
"SHA-512", sha512,
"3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
429 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
430 IMPL_ALL(
"RIPEMD-160", ripemd160,
"62a5321e4fc8784903bb43ab7752c75f8b25af00")
431 IMPL_ALL(
"AES-128", aes128,
"crc:ff6bc888")
432 IMPL_ALL(
"CAMELLIA", camellia,
"crc:7abb59a7")
433 IMPL_ALL(
"CAST-128", cast128,
"crc:456aa584")
434 IMPL(lavu,
"TWOFISH", twofish,
"crc:9edbd5c1")
435 IMPL(gcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
436 IMPL(tomcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
439 int main(
int argc,
char **argv)
443 unsigned i, impl,
size;
446 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
459 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
461 if ((USE_EXT_LIBS)) {
463 snprintf(buf,
sizeof(buf),
"%s%s%s",
464 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
465 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
467 fprintf(stderr,
"Built with the following external libraries:\n"
468 "make VERSUS=%s\n", buf + 1);
470 fprintf(stderr,
"Built without external libraries; use\n"
471 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
472 "to enable them.\n");