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/camellia.h"
79 #include "libavutil/cast5.h"
80 #include "libavutil/twofish.h"
82 #define IMPL_USE_lavu IMPL_USE
84 static void run_lavu_md5(uint8_t *output,
85 const uint8_t *input, unsigned size)
87 av_md5_sum(output, input, size);
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); \
102 DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
103 DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
104 DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
105 DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
107 static void run_lavu_aes128(uint8_t *output,
108 const uint8_t *input, unsigned size)
110 static struct AVAES *aes;
111 if (!aes && !(aes = av_aes_alloc()))
112 fatal_error("out of memory");
113 av_aes_init(aes, hardcoded_key, 128, 0);
114 av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
117 static void run_lavu_camellia(uint8_t *output,
118 const uint8_t *input, unsigned size)
120 static struct AVCAMELLIA *camellia;
121 if (!camellia && !(camellia = av_camellia_alloc()))
122 fatal_error("out of memory");
123 av_camellia_init(camellia, hardcoded_key, 128);
124 av_camellia_crypt(camellia, output, input, size >> 4, NULL, 0);
127 static void run_lavu_cast128(uint8_t *output,
128 const uint8_t *input, unsigned size)
130 static struct AVCAST5 *cast;
131 if (!cast && !(cast = av_cast5_alloc()))
132 fatal_error("out of memory");
133 av_cast5_init(cast, hardcoded_key, 128);
134 av_cast5_crypt(cast, output, input, size >> 3, 0);
137 static void run_lavu_twofish(uint8_t *output,
138 const uint8_t *input, unsigned size)
140 static struct AVTWOFISH *twofish;
141 if (!twofish && !(twofish = av_twofish_alloc()))
142 fatal_error("out of memory");
143 av_twofish_init(twofish, hardcoded_key, 128);
144 av_twofish_crypt(twofish, output, input, size >> 4, NULL, 0);
146 /***************************************************************************
147 * crypto: OpenSSL's libcrypto
148 ***************************************************************************/
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,
173 const uint8_t *input, unsigned size)
178 AES_set_encrypt_key(hardcoded_key, 128, &aes);
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;
190 Camellia_set_key(hardcoded_key, 128, &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)
202 CAST_set_key(&cast, 16, hardcoded_key);
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(...) /* ignore */
212 /***************************************************************************
213 * gcrypt: GnuTLS's libgcrypt
214 ***************************************************************************/
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);
239 gcry_cipher_setkey(aes, hardcoded_key, 16);
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);
249 gcry_cipher_setkey(camellia, hardcoded_key, 16);
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);
259 gcry_cipher_setkey(cast, hardcoded_key, 16);
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);
269 gcry_cipher_setkey(twofish, hardcoded_key, 16);
270 gcry_cipher_encrypt(twofish, output, size, input, size);
273 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
275 #define IMPL_USE_gcrypt(...) /* ignore */
278 /***************************************************************************
279 * tomcrypt: LibTomCrypt
280 ***************************************************************************/
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)
308 aes_setup(hardcoded_key, 16, 0, &aes);
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;
320 camellia_setup(hardcoded_key, 16, 0, &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)
332 cast5_setup(hardcoded_key, 16, 0, &cast);
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;
343 twofish_setup(hardcoded_key, 16, 0, &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(...) /* ignore */
355 /***************************************************************************
357 ***************************************************************************/
359 static unsigned crc32(const uint8_t *data, unsigned size)
361 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
364 static void run_implementation(const uint8_t *input, uint8_t *output,
365 struct hash_impl *impl, unsigned size)
368 unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
369 unsigned outlen = 0, outcrc = 0;
371 double mtime, ttime = 0, ttime2 = 0, stime;
372 uint8_t outref[MAX_OUTPUT_SIZE];
374 if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
375 enabled_algos && !av_stristr(enabled_algos, impl->name))
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++) /* heat caches */
385 impl->run(output, input, size);
386 for (i = 0; i < nruns; i++) {
387 memset(output, 0, size); /* avoid leftovers from previous runs */
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");
400 fatal_error("output mismatch");
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__)
424 struct hash_impl implementations[] = {
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)
441 uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
442 uint8_t *output = input + MAX_INPUT_SIZE;
443 unsigned i, impl, size;
446 while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
449 enabled_libs = optarg;
452 enabled_algos = optarg;
455 specified_runs = strtol(optarg, NULL, 0);
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" : "",
466 ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
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");
479 fatal_error("out of memory");
480 for (i = 0; i < MAX_INPUT_SIZE; i += 4)
481 AV_WB32(input + i, i);
483 size = MAX_INPUT_SIZE;
484 for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
485 run_implementation(input, output, &implementations[impl], size);