2 * Copyright (c) 2013 Nicolas George
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /* Optional external libraries; can be enabled using:
22 * make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench */
23 #define USE_crypto 0x01 /* OpenSSL's libcrypto */
24 #define USE_gcrypt 0x02 /* GnuTLS's libgcrypt */
25 #define USE_tomcrypt 0x04 /* LibTomCrypt */
30 #include "libavutil/avutil.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/crc.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/timer.h"
37 #define AV_READ_TIME(x) 0
41 #include <unistd.h> /* for getopt */
44 #include "compat/getopt.c"
47 #define MAX_INPUT_SIZE 1048576
48 #define MAX_OUTPUT_SIZE 128
50 static const char *enabled_libs;
51 static const char *enabled_algos;
52 static unsigned specified_runs;
54 static const uint8_t *hardcoded_key = "FFmpeg is the best program ever.";
56 static void fatal_error(const char *tag)
58 av_log(NULL, AV_LOG_ERROR, "Fatal error: %s\n", tag);
65 void (*run)(uint8_t *output, const uint8_t *input, unsigned size);
69 /***************************************************************************
71 ***************************************************************************/
73 #include "libavutil/md5.h"
74 #include "libavutil/sha.h"
75 #include "libavutil/sha512.h"
76 #include "libavutil/ripemd.h"
77 #include "libavutil/aes.h"
78 #include "libavutil/blowfish.h"
79 #include "libavutil/camellia.h"
80 #include "libavutil/cast5.h"
81 #include "libavutil/twofish.h"
82 #include "libavutil/rc4.h"
84 #define IMPL_USE_lavu IMPL_USE
86 static void run_lavu_md5(uint8_t *output,
87 const uint8_t *input, unsigned size)
89 av_md5_sum(output, input, size);
92 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
93 static void run_lavu_ ## suffix(uint8_t *output, \
94 const uint8_t *input, unsigned size) \
96 static struct type *h; \
97 if (!h && !(h = av_ ## namespace ## _alloc())) \
98 fatal_error("out of memory"); \
99 av_ ## namespace ## _init(h, hsize); \
100 av_ ## namespace ## _update(h, input, size); \
101 av_ ## namespace ## _final(h, output); \
104 DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
105 DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
106 DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
107 DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
109 static void run_lavu_aes128(uint8_t *output,
110 const uint8_t *input, unsigned size)
112 static struct AVAES *aes;
113 if (!aes && !(aes = av_aes_alloc()))
114 fatal_error("out of memory");
115 av_aes_init(aes, hardcoded_key, 128, 0);
116 av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
119 static void run_lavu_blowfish(uint8_t *output,
120 const uint8_t *input, unsigned size)
122 static struct AVBlowfish *blowfish;
123 if (!blowfish && !(blowfish = av_blowfish_alloc()))
124 fatal_error("out of memory");
125 av_blowfish_init(blowfish, hardcoded_key, 16);
126 av_blowfish_crypt(blowfish, output, input, size >> 3, NULL, 0);
129 static void run_lavu_camellia(uint8_t *output,
130 const uint8_t *input, unsigned size)
132 static struct AVCAMELLIA *camellia;
133 if (!camellia && !(camellia = av_camellia_alloc()))
134 fatal_error("out of memory");
135 av_camellia_init(camellia, hardcoded_key, 128);
136 av_camellia_crypt(camellia, output, input, size >> 4, NULL, 0);
139 static void run_lavu_cast128(uint8_t *output,
140 const uint8_t *input, unsigned size)
142 static struct AVCAST5 *cast;
143 if (!cast && !(cast = av_cast5_alloc()))
144 fatal_error("out of memory");
145 av_cast5_init(cast, hardcoded_key, 128);
146 av_cast5_crypt(cast, output, input, size >> 3, 0);
149 static void run_lavu_twofish(uint8_t *output,
150 const uint8_t *input, unsigned size)
152 static struct AVTWOFISH *twofish;
153 if (!twofish && !(twofish = av_twofish_alloc()))
154 fatal_error("out of memory");
155 av_twofish_init(twofish, hardcoded_key, 128);
156 av_twofish_crypt(twofish, output, input, size >> 4, NULL, 0);
159 static void run_lavu_rc4(uint8_t *output,
160 const uint8_t *input, unsigned size)
162 static struct AVRC4 *rc4;
163 if (!rc4 && !(rc4 = av_rc4_alloc()))
164 fatal_error("out of memory");
165 av_rc4_init(rc4, hardcoded_key, 128, 0);
166 av_rc4_crypt(rc4, output, input, size, NULL, 0);
169 /***************************************************************************
170 * crypto: OpenSSL's libcrypto
171 ***************************************************************************/
173 #if (USE_EXT_LIBS) & USE_crypto
175 #include <openssl/md5.h>
176 #include <openssl/sha.h>
177 #include <openssl/ripemd.h>
178 #include <openssl/aes.h>
179 #include <openssl/blowfish.h>
180 #include <openssl/camellia.h>
181 #include <openssl/cast.h>
182 #include <openssl/rc4.h>
184 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
185 static void run_crypto_ ## suffix(uint8_t *output, \
186 const uint8_t *input, unsigned size) \
188 function(input, size, output); \
191 DEFINE_CRYPTO_WRAPPER(md5, MD5)
192 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
193 DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
194 DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
195 DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
197 static void run_crypto_aes128(uint8_t *output,
198 const uint8_t *input, unsigned size)
203 AES_set_encrypt_key(hardcoded_key, 128, &aes);
205 for (i = 0; i < size; i += 16)
206 AES_encrypt(input + i, output + i, &aes);
209 static void run_crypto_blowfish(uint8_t *output,
210 const uint8_t *input, unsigned size)
215 BF_set_key(&blowfish, 16, hardcoded_key);
216 for (i = 0; i < size; i += 8)
217 BF_ecb_encrypt(input + i, output + i, &blowfish, 1);
220 static void run_crypto_camellia(uint8_t *output,
221 const uint8_t *input, unsigned size)
223 CAMELLIA_KEY camellia;
226 Camellia_set_key(hardcoded_key, 128, &camellia);
228 for (i = 0; i < size; i += 16)
229 Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
232 static void run_crypto_cast128(uint8_t *output,
233 const uint8_t *input, unsigned size)
238 CAST_set_key(&cast, 16, hardcoded_key);
239 for (i = 0; i < size; i += 8)
240 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
243 static void run_crypto_rc4(uint8_t *output,
244 const uint8_t *input, unsigned size)
248 RC4_set_key(&rc4, 16, hardcoded_key);
249 RC4(&rc4, size, input, output);
252 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
254 #define IMPL_USE_crypto(...) /* ignore */
257 /***************************************************************************
258 * gcrypt: GnuTLS's libgcrypt
259 ***************************************************************************/
261 #if (USE_EXT_LIBS) & USE_gcrypt
265 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
266 static void run_gcrypt_ ## suffix(uint8_t *output, \
267 const uint8_t *input, unsigned size) \
269 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
272 DEFINE_GCRYPT_WRAPPER(md5, MD5)
273 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
274 DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
275 DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
276 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
278 static void run_gcrypt_aes128(uint8_t *output,
279 const uint8_t *input, unsigned size)
281 static gcry_cipher_hd_t aes;
283 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
284 gcry_cipher_setkey(aes, hardcoded_key, 16);
285 gcry_cipher_encrypt(aes, output, size, input, size);
288 static void run_gcrypt_blowfish(uint8_t *output,
289 const uint8_t *input, unsigned size)
291 static gcry_cipher_hd_t blowfish;
293 gcry_cipher_open(&blowfish, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 0);
294 gcry_cipher_setkey(blowfish, hardcoded_key, 16);
295 gcry_cipher_encrypt(blowfish, output, size, input, size);
298 static void run_gcrypt_camellia(uint8_t *output,
299 const uint8_t *input, unsigned size)
301 static gcry_cipher_hd_t camellia;
303 gcry_cipher_open(&camellia, GCRY_CIPHER_CAMELLIA128, GCRY_CIPHER_MODE_ECB, 0);
304 gcry_cipher_setkey(camellia, hardcoded_key, 16);
305 gcry_cipher_encrypt(camellia, output, size, input, size);
308 static void run_gcrypt_cast128(uint8_t *output,
309 const uint8_t *input, unsigned size)
311 static gcry_cipher_hd_t cast;
313 gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
314 gcry_cipher_setkey(cast, hardcoded_key, 16);
315 gcry_cipher_encrypt(cast, output, size, input, size);
318 static void run_gcrypt_twofish(uint8_t *output,
319 const uint8_t *input, unsigned size)
321 static gcry_cipher_hd_t twofish;
323 gcry_cipher_open(&twofish, GCRY_CIPHER_TWOFISH128, GCRY_CIPHER_MODE_ECB, 0);
324 gcry_cipher_setkey(twofish, hardcoded_key, 16);
325 gcry_cipher_encrypt(twofish, output, size, input, size);
328 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
330 #define IMPL_USE_gcrypt(...) /* ignore */
333 /***************************************************************************
334 * tomcrypt: LibTomCrypt
335 ***************************************************************************/
337 #if (USE_EXT_LIBS) & USE_tomcrypt
339 #include <tomcrypt.h>
341 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
342 static void run_tomcrypt_ ## suffix(uint8_t *output, \
343 const uint8_t *input, unsigned size) \
346 namespace ## _init(&md); \
347 namespace ## _process(&md, input, size); \
348 namespace ## _done(&md, output); \
351 DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
352 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
353 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
354 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
355 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
357 static void run_tomcrypt_aes128(uint8_t *output,
358 const uint8_t *input, unsigned size)
363 aes_setup(hardcoded_key, 16, 0, &aes);
365 for (i = 0; i < size; i += 16)
366 aes_ecb_encrypt(input + i, output + i, &aes);
369 static void run_tomcrypt_blowfish(uint8_t *output,
370 const uint8_t *input, unsigned size)
372 symmetric_key blowfish;
375 blowfish_setup(hardcoded_key, 16, 0, &blowfish);
376 for (i = 0; i < size; i += 8)
377 blowfish_ecb_encrypt(input + i, output + i, &blowfish);
380 static void run_tomcrypt_camellia(uint8_t *output,
381 const uint8_t *input, unsigned size)
383 symmetric_key camellia;
386 camellia_setup(hardcoded_key, 16, 0, &camellia);
388 for (i = 0; i < size; i += 16)
389 camellia_ecb_encrypt(input + i, output + i, &camellia);
392 static void run_tomcrypt_cast128(uint8_t *output,
393 const uint8_t *input, unsigned size)
398 cast5_setup(hardcoded_key, 16, 0, &cast);
399 for (i = 0; i < size; i += 8)
400 cast5_ecb_encrypt(input + i, output + i, &cast);
403 static void run_tomcrypt_twofish(uint8_t *output,
404 const uint8_t *input, unsigned size)
406 symmetric_key twofish;
409 twofish_setup(hardcoded_key, 16, 0, &twofish);
411 for (i = 0; i < size; i += 16)
412 twofish_ecb_encrypt(input + i, output + i, &twofish);
416 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
418 #define IMPL_USE_tomcrypt(...) /* ignore */
421 /***************************************************************************
423 ***************************************************************************/
425 static unsigned crc32(const uint8_t *data, unsigned size)
427 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
430 static void run_implementation(const uint8_t *input, uint8_t *output,
431 struct hash_impl *impl, unsigned size)
434 unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
435 unsigned outlen = 0, outcrc = 0;
437 double mtime, ttime = 0, ttime2 = 0, stime;
438 uint8_t outref[MAX_OUTPUT_SIZE];
440 if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
441 enabled_algos && !av_stristr(enabled_algos, impl->name))
443 if (!sscanf(impl->output, "crc:%x", &outcrc)) {
444 outlen = strlen(impl->output) / 2;
445 for (i = 0; i < outlen; i++) {
446 sscanf(impl->output + i * 2, "%02x", &val);
450 for (i = 0; i < 8; i++) /* heat caches */
451 impl->run(output, input, size);
452 for (i = 0; i < nruns; i++) {
453 memset(output, 0, size); /* avoid leftovers from previous runs */
455 impl->run(output, input, size);
457 if (outlen ? memcmp(output, outref, outlen) :
458 crc32(output, size) != outcrc) {
459 fprintf(stderr, "Expected: ");
461 for (j = 0; j < outlen; j++)
462 fprintf(stderr, "%02x", output[j]);
464 fprintf(stderr, "%08x", crc32(output, size));
465 fprintf(stderr, "\n");
466 fatal_error("output mismatch");
468 mtime = (double)(t1 - t0) / size;
470 ttime2 += mtime * mtime;
475 stime = sqrt(ttime2 - ttime * ttime);
476 printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
477 impl->lib, impl->name, size, nruns, ttime, stime);
481 #define IMPL_USE(lib, name, symbol, output) \
482 { #lib, name, run_ ## lib ## _ ## symbol, output },
483 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
484 #define IMPL_ALL(...) \
485 IMPL(lavu, __VA_ARGS__) \
486 IMPL(crypto, __VA_ARGS__) \
487 IMPL(gcrypt, __VA_ARGS__) \
488 IMPL(tomcrypt, __VA_ARGS__)
490 struct hash_impl implementations[] = {
491 IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
492 IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
493 IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
494 IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
495 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
496 IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
497 IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
498 IMPL_ALL("CAMELLIA", camellia, "crc:7abb59a7")
499 IMPL_ALL("CAST-128", cast128, "crc:456aa584")
500 IMPL_ALL("BLOWFISH", blowfish, "crc:33e8aa74")
501 IMPL(lavu, "TWOFISH", twofish, "crc:9edbd5c1")
502 IMPL(gcrypt, "TWOFISH", twofish, "crc:9edbd5c1")
503 IMPL(tomcrypt, "TWOFISH", twofish, "crc:9edbd5c1")
504 IMPL(lavu, "RC4", rc4, "crc:538d37b2")
505 IMPL(crypto, "RC4", rc4, "crc:538d37b2")
508 int main(int argc, char **argv)
510 uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
511 uint8_t *output = input + MAX_INPUT_SIZE;
512 unsigned i, impl, size;
515 while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
518 enabled_libs = optarg;
521 enabled_algos = optarg;
524 specified_runs = strtol(optarg, NULL, 0);
528 fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
530 if ((USE_EXT_LIBS)) {
532 snprintf(buf, sizeof(buf), "%s%s%s",
533 ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
534 ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
535 ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
536 fprintf(stderr, "Built with the following external libraries:\n"
537 "make VERSUS=%s\n", buf + 1);
539 fprintf(stderr, "Built without external libraries; use\n"
540 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
541 "to enable them.\n");
548 fatal_error("out of memory");
549 for (i = 0; i < MAX_INPUT_SIZE; i += 4)
550 AV_WB32(input + i, i);
552 size = MAX_INPUT_SIZE;
553 for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
554 run_implementation(input, output, &implementations[impl], size);