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"
81 #define IMPL_USE_lavu IMPL_USE
83 static void run_lavu_md5(uint8_t *output,
84 const uint8_t *input, unsigned size)
86 av_md5_sum(output, input, size);
89 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
90 static void run_lavu_ ## suffix(uint8_t *output, \
91 const uint8_t *input, unsigned size) \
93 static struct type *h; \
94 if (!h && !(h = av_ ## namespace ## _alloc())) \
95 fatal_error("out of memory"); \
96 av_ ## namespace ## _init(h, hsize); \
97 av_ ## namespace ## _update(h, input, size); \
98 av_ ## namespace ## _final(h, output); \
101 DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
102 DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
103 DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
104 DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
106 static void run_lavu_aes128(uint8_t *output,
107 const uint8_t *input, unsigned size)
109 static struct AVAES *aes;
110 if (!aes && !(aes = av_aes_alloc()))
111 fatal_error("out of memory");
112 av_aes_init(aes, hardcoded_key, 128, 0);
113 av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
116 static void run_lavu_camellia(uint8_t *output,
117 const uint8_t *input, unsigned size)
119 static struct AVCAMELLIA *camellia;
120 if (!camellia && !(camellia = av_camellia_alloc()))
121 fatal_error("out of memory");
122 av_camellia_init(camellia, hardcoded_key, 128);
123 av_camellia_crypt(camellia, output, input, size >> 4, NULL, 0);
126 static void run_lavu_cast128(uint8_t *output,
127 const uint8_t *input, unsigned size)
129 static struct AVCAST5 *cast;
130 if (!cast && !(cast = av_cast5_alloc()))
131 fatal_error("out of memory");
132 av_cast5_init(cast, hardcoded_key, 128);
133 av_cast5_crypt(cast, output, input, size >> 3, 0);
136 /***************************************************************************
137 * crypto: OpenSSL's libcrypto
138 ***************************************************************************/
140 #if (USE_EXT_LIBS) & USE_crypto
142 #include <openssl/md5.h>
143 #include <openssl/sha.h>
144 #include <openssl/ripemd.h>
145 #include <openssl/aes.h>
146 #include <openssl/camellia.h>
147 #include <openssl/cast.h>
149 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
150 static void run_crypto_ ## suffix(uint8_t *output, \
151 const uint8_t *input, unsigned size) \
153 function(input, size, output); \
156 DEFINE_CRYPTO_WRAPPER(md5, MD5)
157 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
158 DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
159 DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
160 DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
162 static void run_crypto_aes128(uint8_t *output,
163 const uint8_t *input, unsigned size)
168 AES_set_encrypt_key(hardcoded_key, 128, &aes);
170 for (i = 0; i < size; i += 16)
171 AES_encrypt(input + i, output + i, &aes);
174 static void run_crypto_camellia(uint8_t *output,
175 const uint8_t *input, unsigned size)
177 CAMELLIA_KEY camellia;
180 Camellia_set_key(hardcoded_key, 128, &camellia);
182 for (i = 0; i < size; i += 16)
183 Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
186 static void run_crypto_cast128(uint8_t *output,
187 const uint8_t *input, unsigned size)
192 CAST_set_key(&cast, 16, hardcoded_key);
193 for (i = 0; i < size; i += 8)
194 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
197 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
199 #define IMPL_USE_crypto(...) /* ignore */
202 /***************************************************************************
203 * gcrypt: GnuTLS's libgcrypt
204 ***************************************************************************/
206 #if (USE_EXT_LIBS) & USE_gcrypt
210 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
211 static void run_gcrypt_ ## suffix(uint8_t *output, \
212 const uint8_t *input, unsigned size) \
214 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
217 DEFINE_GCRYPT_WRAPPER(md5, MD5)
218 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
219 DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
220 DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
221 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
223 static void run_gcrypt_aes128(uint8_t *output,
224 const uint8_t *input, unsigned size)
226 static gcry_cipher_hd_t aes;
228 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
229 gcry_cipher_setkey(aes, hardcoded_key, 16);
230 gcry_cipher_encrypt(aes, output, size, input, size);
233 static void run_gcrypt_camellia(uint8_t *output,
234 const uint8_t *input, unsigned size)
236 static gcry_cipher_hd_t camellia;
238 gcry_cipher_open(&camellia, GCRY_CIPHER_CAMELLIA128, GCRY_CIPHER_MODE_ECB, 0);
239 gcry_cipher_setkey(camellia, hardcoded_key, 16);
240 gcry_cipher_encrypt(camellia, output, size, input, size);
243 static void run_gcrypt_cast128(uint8_t *output,
244 const uint8_t *input, unsigned size)
246 static gcry_cipher_hd_t cast;
248 gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
249 gcry_cipher_setkey(cast, hardcoded_key, 16);
250 gcry_cipher_encrypt(cast, output, size, input, size);
253 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
255 #define IMPL_USE_gcrypt(...) /* ignore */
258 /***************************************************************************
259 * tomcrypt: LibTomCrypt
260 ***************************************************************************/
262 #if (USE_EXT_LIBS) & USE_tomcrypt
264 #include <tomcrypt.h>
266 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
267 static void run_tomcrypt_ ## suffix(uint8_t *output, \
268 const uint8_t *input, unsigned size) \
271 namespace ## _init(&md); \
272 namespace ## _process(&md, input, size); \
273 namespace ## _done(&md, output); \
276 DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
277 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
278 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
279 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
280 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
282 static void run_tomcrypt_aes128(uint8_t *output,
283 const uint8_t *input, unsigned size)
288 aes_setup(hardcoded_key, 16, 0, &aes);
290 for (i = 0; i < size; i += 16)
291 aes_ecb_encrypt(input + i, output + i, &aes);
294 static void run_tomcrypt_camellia(uint8_t *output,
295 const uint8_t *input, unsigned size)
297 symmetric_key camellia;
300 camellia_setup(hardcoded_key, 16, 0, &camellia);
302 for (i = 0; i < size; i += 16)
303 camellia_ecb_encrypt(input + i, output + i, &camellia);
306 static void run_tomcrypt_cast128(uint8_t *output,
307 const uint8_t *input, unsigned size)
312 cast5_setup(hardcoded_key, 16, 0, &cast);
313 for (i = 0; i < size; i += 8)
314 cast5_ecb_encrypt(input + i, output + i, &cast);
317 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
319 #define IMPL_USE_tomcrypt(...) /* ignore */
322 /***************************************************************************
324 ***************************************************************************/
326 static unsigned crc32(const uint8_t *data, unsigned size)
328 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
331 static void run_implementation(const uint8_t *input, uint8_t *output,
332 struct hash_impl *impl, unsigned size)
335 unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
336 unsigned outlen = 0, outcrc = 0;
338 double mtime, ttime = 0, ttime2 = 0, stime;
339 uint8_t outref[MAX_OUTPUT_SIZE];
341 if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
342 enabled_algos && !av_stristr(enabled_algos, impl->name))
344 if (!sscanf(impl->output, "crc:%x", &outcrc)) {
345 outlen = strlen(impl->output) / 2;
346 for (i = 0; i < outlen; i++) {
347 sscanf(impl->output + i * 2, "%02x", &val);
351 for (i = 0; i < 8; i++) /* heat caches */
352 impl->run(output, input, size);
353 for (i = 0; i < nruns; i++) {
354 memset(output, 0, size); /* avoid leftovers from previous runs */
356 impl->run(output, input, size);
358 if (outlen ? memcmp(output, outref, outlen) :
359 crc32(output, size) != outcrc) {
360 fprintf(stderr, "Expected: ");
362 for (j = 0; j < outlen; j++)
363 fprintf(stderr, "%02x", output[j]);
365 fprintf(stderr, "%08x", crc32(output, size));
366 fprintf(stderr, "\n");
367 fatal_error("output mismatch");
369 mtime = (double)(t1 - t0) / size;
371 ttime2 += mtime * mtime;
376 stime = sqrt(ttime2 - ttime * ttime);
377 printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
378 impl->lib, impl->name, size, nruns, ttime, stime);
382 #define IMPL_USE(lib, name, symbol, output) \
383 { #lib, name, run_ ## lib ## _ ## symbol, output },
384 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
385 #define IMPL_ALL(...) \
386 IMPL(lavu, __VA_ARGS__) \
387 IMPL(crypto, __VA_ARGS__) \
388 IMPL(gcrypt, __VA_ARGS__) \
389 IMPL(tomcrypt, __VA_ARGS__)
391 struct hash_impl implementations[] = {
392 IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
393 IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
394 IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
395 IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
396 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
397 IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
398 IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
399 IMPL_ALL("CAMELLIA", camellia, "crc:7abb59a7")
400 IMPL_ALL("CAST-128", cast128, "crc:456aa584")
403 int main(int argc, char **argv)
405 uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
406 uint8_t *output = input + MAX_INPUT_SIZE;
407 unsigned i, impl, size;
410 while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
413 enabled_libs = optarg;
416 enabled_algos = optarg;
419 specified_runs = strtol(optarg, NULL, 0);
423 fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
425 if ((USE_EXT_LIBS)) {
427 snprintf(buf, sizeof(buf), "%s%s%s",
428 ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
429 ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
430 ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
431 fprintf(stderr, "Built with the following external libraries:\n"
432 "make VERSUS=%s\n", buf + 1);
434 fprintf(stderr, "Built without external libraries; use\n"
435 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
436 "to enable them.\n");
443 fatal_error("out of memory");
444 for (i = 0; i < MAX_INPUT_SIZE; i += 4)
445 AV_WB32(input + i, i);
447 size = MAX_INPUT_SIZE;
448 for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
449 run_implementation(input, output, &implementations[impl], size);