tools/crypto_bench: add support for mbedcrypto
[ffmpeg.git] / tools / crypto_bench.c
1 /*
2  * Copyright (c) 2013 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
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 */
26 #define USE_mbedcrypto       0x08    /* mbed TLS */
27
28 #include <stdlib.h>
29 #include <math.h>
30
31 #include "libavutil/avutil.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/timer.h"
36
37 #ifndef AV_READ_TIME
38 #define AV_READ_TIME(x) 0
39 #endif
40
41 #if HAVE_UNISTD_H
42 #include <unistd.h> /* for getopt */
43 #endif
44 #if !HAVE_GETOPT
45 #include "compat/getopt.c"
46 #endif
47
48 #define MAX_INPUT_SIZE 1048576
49 #define MAX_OUTPUT_SIZE 128
50
51 static const char *enabled_libs;
52 static const char *enabled_algos;
53 static unsigned specified_runs;
54
55 static const uint8_t *hardcoded_key = "FFmpeg is the best program ever.";
56
57 static void fatal_error(const char *tag)
58 {
59     av_log(NULL, AV_LOG_ERROR, "Fatal error: %s\n", tag);
60     exit(1);
61 }
62
63 struct hash_impl {
64     const char *lib;
65     const char *name;
66     void (*run)(uint8_t *output, const uint8_t *input, unsigned size);
67     const char *output;
68 };
69
70 /***************************************************************************
71  * lavu: libavutil
72  ***************************************************************************/
73
74 #include "libavutil/md5.h"
75 #include "libavutil/sha.h"
76 #include "libavutil/sha512.h"
77 #include "libavutil/ripemd.h"
78 #include "libavutil/aes.h"
79 #include "libavutil/blowfish.h"
80 #include "libavutil/camellia.h"
81 #include "libavutil/cast5.h"
82 #include "libavutil/des.h"
83 #include "libavutil/twofish.h"
84 #include "libavutil/rc4.h"
85 #include "libavutil/xtea.h"
86
87 #define IMPL_USE_lavu IMPL_USE
88
89 static void run_lavu_md5(uint8_t *output,
90                          const uint8_t *input, unsigned size)
91 {
92     av_md5_sum(output, input, size);
93 }
94
95 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize)                       \
96 static void run_lavu_ ## suffix(uint8_t *output,                             \
97                                 const uint8_t *input, unsigned size)         \
98 {                                                                            \
99     static struct type *h;                                                   \
100     if (!h && !(h = av_ ## namespace ## _alloc()))                           \
101         fatal_error("out of memory");                                        \
102     av_ ## namespace ## _init(h, hsize);                                     \
103     av_ ## namespace ## _update(h, input, size);                             \
104     av_ ## namespace ## _final(h, output);                                   \
105 }
106
107 DEFINE_LAVU_MD(sha1,      AVSHA,    sha, 160);
108 DEFINE_LAVU_MD(sha256,    AVSHA,    sha, 256);
109 DEFINE_LAVU_MD(sha512,    AVSHA512, sha512, 512);
110 DEFINE_LAVU_MD(ripemd128, AVRIPEMD, ripemd, 128);
111 DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
112
113 static void run_lavu_aes128(uint8_t *output,
114                             const uint8_t *input, unsigned size)
115 {
116     static struct AVAES *aes;
117     if (!aes && !(aes = av_aes_alloc()))
118         fatal_error("out of memory");
119     av_aes_init(aes, hardcoded_key, 128, 0);
120     av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
121 }
122
123 static void run_lavu_blowfish(uint8_t *output,
124                               const uint8_t *input, unsigned size)
125 {
126     static struct AVBlowfish *blowfish;
127     if (!blowfish && !(blowfish = av_blowfish_alloc()))
128         fatal_error("out of memory");
129     av_blowfish_init(blowfish, hardcoded_key, 16);
130     av_blowfish_crypt(blowfish, output, input, size >> 3, NULL, 0);
131 }
132
133 static void run_lavu_camellia(uint8_t *output,
134                               const uint8_t *input, unsigned size)
135 {
136     static struct AVCAMELLIA *camellia;
137     if (!camellia && !(camellia = av_camellia_alloc()))
138         fatal_error("out of memory");
139     av_camellia_init(camellia, hardcoded_key, 128);
140     av_camellia_crypt(camellia, output, input, size >> 4, NULL, 0);
141 }
142
143 static void run_lavu_cast128(uint8_t *output,
144                              const uint8_t *input, unsigned size)
145 {
146     static struct AVCAST5 *cast;
147     if (!cast && !(cast = av_cast5_alloc()))
148         fatal_error("out of memory");
149     av_cast5_init(cast, hardcoded_key, 128);
150     av_cast5_crypt(cast, output, input, size >> 3, 0);
151 }
152
153 static void run_lavu_des(uint8_t *output,
154                               const uint8_t *input, unsigned size)
155 {
156     static struct AVDES *des;
157     if (!des && !(des = av_des_alloc()))
158         fatal_error("out of memory");
159     av_des_init(des, hardcoded_key, 64, 0);
160     av_des_crypt(des, output, input, size >> 3, NULL, 0);
161 }
162
163 static void run_lavu_twofish(uint8_t *output,
164                               const uint8_t *input, unsigned size)
165 {
166     static struct AVTWOFISH *twofish;
167     if (!twofish && !(twofish = av_twofish_alloc()))
168         fatal_error("out of memory");
169     av_twofish_init(twofish, hardcoded_key, 128);
170     av_twofish_crypt(twofish, output, input, size >> 4, NULL, 0);
171 }
172
173 static void run_lavu_rc4(uint8_t *output,
174                               const uint8_t *input, unsigned size)
175 {
176     static struct AVRC4 *rc4;
177     if (!rc4 && !(rc4 = av_rc4_alloc()))
178         fatal_error("out of memory");
179     av_rc4_init(rc4, hardcoded_key, 128, 0);
180     av_rc4_crypt(rc4, output, input, size, NULL, 0);
181 }
182
183 static void run_lavu_xtea(uint8_t *output,
184                               const uint8_t *input, unsigned size)
185 {
186     static struct AVXTEA *xtea;
187     if (!xtea && !(xtea = av_xtea_alloc()))
188         fatal_error("out of memory");
189     av_xtea_init(xtea, hardcoded_key);
190     av_xtea_crypt(xtea, output, input, size >> 3, NULL, 0);
191 }
192
193 /***************************************************************************
194  * crypto: OpenSSL's libcrypto
195  ***************************************************************************/
196
197 #if (USE_EXT_LIBS) & USE_crypto
198
199 #define OPENSSL_DISABLE_OLD_DES_SUPPORT
200 #include <openssl/md5.h>
201 #include <openssl/sha.h>
202 #include <openssl/ripemd.h>
203 #include <openssl/aes.h>
204 #include <openssl/blowfish.h>
205 #include <openssl/camellia.h>
206 #include <openssl/cast.h>
207 #include <openssl/des.h>
208 #include <openssl/rc4.h>
209
210 #define DEFINE_CRYPTO_WRAPPER(suffix, function)                              \
211 static void run_crypto_ ## suffix(uint8_t *output,                           \
212                                   const uint8_t *input, unsigned size)       \
213 {                                                                            \
214     function(input, size, output);                                           \
215 }
216
217 DEFINE_CRYPTO_WRAPPER(md5,       MD5)
218 DEFINE_CRYPTO_WRAPPER(sha1,      SHA1)
219 DEFINE_CRYPTO_WRAPPER(sha256,    SHA256)
220 DEFINE_CRYPTO_WRAPPER(sha512,    SHA512)
221 DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
222
223 static void run_crypto_aes128(uint8_t *output,
224                               const uint8_t *input, unsigned size)
225 {
226     AES_KEY aes;
227     unsigned i;
228
229     AES_set_encrypt_key(hardcoded_key, 128, &aes);
230     size -= 15;
231     for (i = 0; i < size; i += 16)
232         AES_encrypt(input + i, output + i, &aes);
233 }
234
235 static void run_crypto_blowfish(uint8_t *output,
236                                 const uint8_t *input, unsigned size)
237 {
238     BF_KEY blowfish;
239     unsigned i;
240
241     BF_set_key(&blowfish, 16, hardcoded_key);
242     for (i = 0; i < size; i += 8)
243         BF_ecb_encrypt(input + i, output + i, &blowfish, 1);
244 }
245
246 static void run_crypto_camellia(uint8_t *output,
247                                 const uint8_t *input, unsigned size)
248 {
249     CAMELLIA_KEY camellia;
250     unsigned i;
251
252     Camellia_set_key(hardcoded_key, 128, &camellia);
253     size -= 15;
254     for (i = 0; i < size; i += 16)
255         Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
256 }
257
258 static void run_crypto_cast128(uint8_t *output,
259                                const uint8_t *input, unsigned size)
260 {
261     CAST_KEY cast;
262     unsigned i;
263
264     CAST_set_key(&cast, 16, hardcoded_key);
265     for (i = 0; i < size; i += 8)
266         CAST_ecb_encrypt(input + i, output + i, &cast, 1);
267 }
268
269 static void run_crypto_des(uint8_t *output,
270                            const uint8_t *input, unsigned size)
271 {
272     DES_key_schedule des;
273     unsigned i;
274
275     DES_set_key(hardcoded_key, &des);
276     for (i = 0; i < size; i += 8)
277         DES_ecb_encrypt(input + i, output + i, &des, 1);
278 }
279
280 static void run_crypto_rc4(uint8_t *output,
281                                 const uint8_t *input, unsigned size)
282 {
283     RC4_KEY rc4;
284
285     RC4_set_key(&rc4, 16, hardcoded_key);
286     RC4(&rc4, size, input, output);
287 }
288
289 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
290 #else
291 #define IMPL_USE_crypto(...) /* ignore */
292 #endif
293
294 /***************************************************************************
295  * gcrypt: GnuTLS's libgcrypt
296  ***************************************************************************/
297
298 #if (USE_EXT_LIBS) & USE_gcrypt
299
300 #include <gcrypt.h>
301
302 #define DEFINE_GCRYPT_WRAPPER(suffix, algo)                                  \
303 static void run_gcrypt_ ## suffix(uint8_t *output,                           \
304                                   const uint8_t *input, unsigned size)       \
305 {                                                                            \
306     gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size);              \
307 }
308
309 DEFINE_GCRYPT_WRAPPER(md5,       MD5)
310 DEFINE_GCRYPT_WRAPPER(sha1,      SHA1)
311 DEFINE_GCRYPT_WRAPPER(sha256,    SHA256)
312 DEFINE_GCRYPT_WRAPPER(sha512,    SHA512)
313 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
314
315 #define DEFINE_GCRYPT_CYPHER_WRAPPER(suffix, cypher, sz)                            \
316 static void run_gcrypt_ ## suffix(uint8_t *output,                                  \
317                               const uint8_t *input, unsigned size)                  \
318 {                                                                                   \
319     static gcry_cipher_hd_t suffix;                                                 \
320     if (!suffix)                                                                    \
321         gcry_cipher_open(&suffix, GCRY_CIPHER_ ## cypher, GCRY_CIPHER_MODE_ECB, 0); \
322     gcry_cipher_setkey(suffix, hardcoded_key, sz);                                  \
323     gcry_cipher_encrypt(suffix, output, size, input, size);                         \
324 }
325
326 DEFINE_GCRYPT_CYPHER_WRAPPER(aes128,   AES128,      16)
327 DEFINE_GCRYPT_CYPHER_WRAPPER(blowfish, BLOWFISH,    16)
328 DEFINE_GCRYPT_CYPHER_WRAPPER(camellia, CAMELLIA128, 16)
329 DEFINE_GCRYPT_CYPHER_WRAPPER(cast128,  CAST5,       16)
330 DEFINE_GCRYPT_CYPHER_WRAPPER(des,      DES,         8)
331 DEFINE_GCRYPT_CYPHER_WRAPPER(twofish,  TWOFISH128,  16)
332
333 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
334 #else
335 #define IMPL_USE_gcrypt(...) /* ignore */
336 #endif
337
338 /***************************************************************************
339  * mbedcrypto: mbed TLS
340  ***************************************************************************/
341
342 #if (USE_EXT_LIBS) & USE_mbedcrypto
343
344 #include <mbedtls/aes.h>
345 #include <mbedtls/arc4.h>
346 #include <mbedtls/blowfish.h>
347 #include <mbedtls/camellia.h>
348 #include <mbedtls/des.h>
349 #include <mbedtls/md5.h>
350 #include <mbedtls/ripemd160.h>
351 #include <mbedtls/sha1.h>
352 #include <mbedtls/sha256.h>
353 #include <mbedtls/sha512.h>
354 #include <mbedtls/xtea.h>
355
356 #define DEFINE_MBEDCRYPTO_WRAPPER(suffix)                                  \
357 static void run_mbedcrypto_ ## suffix(uint8_t *output,                     \
358                                       const uint8_t *input, unsigned size) \
359 {                                                                          \
360     mbedtls_ ## suffix ## _ret(input, size, output);                       \
361 }
362
363 #define DEFINE_MBEDCRYPTO_WRAPPER_SHA2(suffix)                             \
364 static void run_mbedcrypto_ ## suffix(uint8_t *output,                     \
365                                       const uint8_t *input, unsigned size) \
366 {                                                                          \
367     mbedtls_ ## suffix ## _ret(input, size, output, 0);                    \
368 }
369
370 DEFINE_MBEDCRYPTO_WRAPPER(md5)
371 DEFINE_MBEDCRYPTO_WRAPPER(ripemd160)
372 DEFINE_MBEDCRYPTO_WRAPPER(sha1)
373 DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha256)
374 DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha512)
375
376
377 #define DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(suffix, cypher, algo)                  \
378 static void run_mbedcrypto_ ## suffix(uint8_t *output,                          \
379                                       const uint8_t *input, unsigned size)      \
380 {                                                                               \
381     mbedtls_ ## cypher ## _context cypher;                                      \
382                                                                                 \
383     mbedtls_ ## cypher ## _init(&cypher);                                       \
384     mbedtls_ ## cypher ## _setkey_enc(&cypher, hardcoded_key, 128);             \
385     for (int i = 0; i < size; i += 16)                                          \
386         mbedtls_ ## cypher ## _crypt_ecb(&cypher, MBEDTLS_ ## algo ## _ENCRYPT, \
387                                          input + i, output + i);                \
388     mbedtls_ ## cypher ## _free(&cypher);                                       \
389 }
390
391 DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(aes128, aes, AES)
392 DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(camellia, camellia, CAMELLIA)
393
394 static void run_mbedcrypto_blowfish(uint8_t *output,
395                                     const uint8_t *input, unsigned size)
396 {
397     mbedtls_blowfish_context blowfish;
398
399     mbedtls_blowfish_init(&blowfish);
400     mbedtls_blowfish_setkey(&blowfish, hardcoded_key, 128);
401     for (int i = 0; i < size; i += 8)
402         mbedtls_blowfish_crypt_ecb(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT,
403                                    input + i, output + i);
404     mbedtls_blowfish_free(&blowfish);
405 }
406
407 static void run_mbedcrypto_des(uint8_t *output,
408                                const uint8_t *input, unsigned size)
409 {
410     mbedtls_des_context des;
411
412     mbedtls_des_init(&des);
413     mbedtls_des_setkey_enc(&des, hardcoded_key);
414     for (int i = 0; i < size; i += 8)
415         mbedtls_des_crypt_ecb(&des, input + i, output + i);
416     mbedtls_des_free(&des);
417 }
418
419 static void run_mbedcrypto_rc4(uint8_t *output,
420                                const uint8_t *input, unsigned size)
421 {
422     mbedtls_arc4_context rc4;
423
424     mbedtls_arc4_init(&rc4);
425     mbedtls_arc4_setup(&rc4, hardcoded_key, 16);
426     mbedtls_arc4_crypt(&rc4, size, input, output);
427     mbedtls_arc4_free(&rc4);
428 }
429
430 static void run_mbedcrypto_xtea(uint8_t *output,
431                                 const uint8_t *input, unsigned size)
432 {
433     mbedtls_xtea_context xtea;
434
435     mbedtls_xtea_init(&xtea);
436     mbedtls_xtea_setup(&xtea, hardcoded_key);
437     for (int i = 0; i < size; i += 8)
438         mbedtls_xtea_crypt_ecb(&xtea, MBEDTLS_XTEA_ENCRYPT,
439                                input + i, output + i);
440     mbedtls_xtea_free(&xtea);
441 }
442
443 #define IMPL_USE_mbedcrypto(...) IMPL_USE(__VA_ARGS__)
444 #else
445 #define IMPL_USE_mbedcrypto(...) /* ignore */
446 #endif
447
448 /***************************************************************************
449  * tomcrypt: LibTomCrypt
450  ***************************************************************************/
451
452 #if (USE_EXT_LIBS) & USE_tomcrypt
453
454 #include <tomcrypt.h>
455
456 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo)                     \
457 static void run_tomcrypt_ ## suffix(uint8_t *output,                         \
458                                     const uint8_t *input, unsigned size)     \
459 {                                                                            \
460     hash_state md;                                                           \
461     namespace ## _init(&md);                                                 \
462     namespace ## _process(&md, input, size);                                 \
463     namespace ## _done(&md, output);                                         \
464 }
465
466 DEFINE_TOMCRYPT_WRAPPER(md5,       md5,    MD5)
467 DEFINE_TOMCRYPT_WRAPPER(sha1,      sha1,   SHA1)
468 DEFINE_TOMCRYPT_WRAPPER(sha256,    sha256, SHA256)
469 DEFINE_TOMCRYPT_WRAPPER(sha512,    sha512, SHA512)
470 DEFINE_TOMCRYPT_WRAPPER(ripemd128, rmd128, RIPEMD128)
471 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
472
473 static void run_tomcrypt_aes128(uint8_t *output,
474                                 const uint8_t *input, unsigned size)
475 {
476     symmetric_key aes;
477     unsigned i;
478
479     aes_setup(hardcoded_key, 16, 0, &aes);
480     size -= 15;
481     for (i = 0; i < size; i += 16)
482         aes_ecb_encrypt(input + i, output + i, &aes);
483 }
484
485 static void run_tomcrypt_blowfish(uint8_t *output,
486                                   const uint8_t *input, unsigned size)
487 {
488     symmetric_key blowfish;
489     unsigned i;
490
491     blowfish_setup(hardcoded_key, 16, 0, &blowfish);
492     for (i = 0; i < size; i += 8)
493         blowfish_ecb_encrypt(input + i, output + i, &blowfish);
494 }
495
496 static void run_tomcrypt_camellia(uint8_t *output,
497                                   const uint8_t *input, unsigned size)
498 {
499     symmetric_key camellia;
500     unsigned i;
501
502     camellia_setup(hardcoded_key, 16, 0, &camellia);
503     size -= 15;
504     for (i = 0; i < size; i += 16)
505         camellia_ecb_encrypt(input + i, output + i, &camellia);
506 }
507
508 static void run_tomcrypt_cast128(uint8_t *output,
509                                 const uint8_t *input, unsigned size)
510 {
511     symmetric_key cast;
512     unsigned i;
513
514     cast5_setup(hardcoded_key, 16, 0, &cast);
515     for (i = 0; i < size; i += 8)
516         cast5_ecb_encrypt(input + i, output + i, &cast);
517 }
518
519 static void run_tomcrypt_des(uint8_t *output,
520                              const uint8_t *input, unsigned size)
521 {
522     symmetric_key des;
523     unsigned i;
524
525     des_setup(hardcoded_key, 8, 0, &des);
526     for (i = 0; i < size; i += 8)
527         des_ecb_encrypt(input + i, output + i, &des);
528 }
529
530 static void run_tomcrypt_twofish(uint8_t *output,
531                                 const uint8_t *input, unsigned size)
532 {
533     symmetric_key twofish;
534     unsigned i;
535
536     twofish_setup(hardcoded_key, 16, 0, &twofish);
537     size -= 15;
538     for (i = 0; i < size; i += 16)
539         twofish_ecb_encrypt(input + i, output + i, &twofish);
540 }
541
542 static void run_tomcrypt_xtea(uint8_t *output,
543                               const uint8_t *input, unsigned size)
544 {
545     symmetric_key xtea;
546     unsigned i;
547
548     xtea_setup(hardcoded_key, 16, 0, &xtea);
549     for (i = 0; i < size; i += 8)
550         xtea_ecb_encrypt(input + i, output + i, &xtea);
551 }
552
553
554 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
555 #else
556 #define IMPL_USE_tomcrypt(...) /* ignore */
557 #endif
558
559 /***************************************************************************
560  * Driver code
561  ***************************************************************************/
562
563 static unsigned crc32(const uint8_t *data, unsigned size)
564 {
565     return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
566 }
567
568 static void run_implementation(const uint8_t *input, uint8_t *output,
569                                struct hash_impl *impl, unsigned size)
570 {
571     uint64_t t0, t1;
572     unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
573     unsigned outlen = 0, outcrc = 0;
574     unsigned i, j, val;
575     double mtime, ttime = 0, ttime2 = 0, stime;
576     uint8_t outref[MAX_OUTPUT_SIZE];
577
578     if (enabled_libs  && !av_stristr(enabled_libs,  impl->lib) ||
579         enabled_algos && !av_stristr(enabled_algos, impl->name))
580         return;
581     if (!sscanf(impl->output, "crc:%x", &outcrc)) {
582         outlen = strlen(impl->output) / 2;
583         for (i = 0; i < outlen; i++) {
584             sscanf(impl->output + i * 2, "%02x", &val);
585             outref[i] = val;
586         }
587     }
588     for (i = 0; i < 8; i++) /* heat caches */
589         impl->run(output, input, size);
590     for (i = 0; i < nruns; i++) {
591         memset(output, 0, size); /* avoid leftovers from previous runs */
592         t0 = AV_READ_TIME();
593         impl->run(output, input, size);
594         t1 = AV_READ_TIME();
595         if (outlen ? memcmp(output, outref, outlen) :
596                      crc32(output, size) != outcrc) {
597             fprintf(stderr, "Expected: ");
598             if (outlen)
599                 for (j = 0; j < outlen; j++)
600                     fprintf(stderr, "%02x", output[j]);
601             else
602                 fprintf(stderr, "%08x", crc32(output, size));
603             fprintf(stderr, "\n");
604             fatal_error("output mismatch");
605         }
606         mtime = (double)(t1 - t0) / size;
607         ttime  += mtime;
608         ttime2 += mtime * mtime;
609     }
610
611     ttime  /= nruns;
612     ttime2 /= nruns;
613     stime = sqrt(ttime2 - ttime * ttime);
614     printf("%-10s %-12s size: %7d  runs: %6d  time: %8.3f +- %.3f\n",
615            impl->lib, impl->name, size, nruns, ttime, stime);
616     fflush(stdout);
617 }
618
619 #define IMPL_USE(lib, name, symbol, output) \
620     { #lib, name, run_ ## lib ## _ ## symbol, output },
621 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
622 #define IMPL_ALL(...) \
623     IMPL(lavu,       __VA_ARGS__) \
624     IMPL(crypto,     __VA_ARGS__) \
625     IMPL(gcrypt,     __VA_ARGS__) \
626     IMPL(mbedcrypto, __VA_ARGS__) \
627     IMPL(tomcrypt,   __VA_ARGS__)
628
629 struct hash_impl implementations[] = {
630     IMPL_ALL("MD5",        md5,       "aa26ff5b895356bcffd9292ba9f89e66")
631     IMPL_ALL("SHA-1",      sha1,      "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
632     IMPL_ALL("SHA-256",    sha256,    "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
633     IMPL_ALL("SHA-512",    sha512,    "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
634                                       "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
635     IMPL(lavu,     "RIPEMD-128", ripemd128, "9ab8bfba2ddccc5d99c9d4cdfb844a5f")
636     IMPL(tomcrypt, "RIPEMD-128", ripemd128, "9ab8bfba2ddccc5d99c9d4cdfb844a5f")
637     IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
638     IMPL_ALL("AES-128",    aes128,    "crc:ff6bc888")
639     IMPL_ALL("CAMELLIA",   camellia,  "crc:7abb59a7")
640     IMPL(lavu,     "CAST-128", cast128, "crc:456aa584")
641     IMPL(crypto,   "CAST-128", cast128, "crc:456aa584")
642     IMPL(gcrypt,   "CAST-128", cast128, "crc:456aa584")
643     IMPL(tomcrypt, "CAST-128", cast128, "crc:456aa584")
644     IMPL_ALL("BLOWFISH",   blowfish,  "crc:33e8aa74")
645     IMPL_ALL("DES",        des,       "crc:31291e0b")
646     IMPL(lavu,     "TWOFISH", twofish, "crc:9edbd5c1")
647     IMPL(gcrypt,   "TWOFISH", twofish, "crc:9edbd5c1")
648     IMPL(tomcrypt, "TWOFISH", twofish, "crc:9edbd5c1")
649     IMPL(lavu,     "RC4",     rc4,     "crc:538d37b2")
650     IMPL(crypto,   "RC4",     rc4,     "crc:538d37b2")
651     IMPL(mbedcrypto, "RC4",   rc4,     "crc:538d37b2")
652     IMPL(lavu,     "XTEA",    xtea,    "crc:931fc270")
653     IMPL(mbedcrypto, "XTEA",  xtea,    "crc:931fc270")
654     IMPL(tomcrypt, "XTEA",    xtea,    "crc:931fc270")
655 };
656
657 int main(int argc, char **argv)
658 {
659     uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
660     uint8_t *output = input + MAX_INPUT_SIZE;
661     unsigned i, impl, size;
662     int opt;
663
664     while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
665         switch (opt) {
666         case 'l':
667             enabled_libs = optarg;
668             break;
669         case 'a':
670             enabled_algos = optarg;
671             break;
672         case 'r':
673             specified_runs = strtol(optarg, NULL, 0);
674             break;
675         case 'h':
676         default:
677             fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
678                     argv[0]);
679             if ((USE_EXT_LIBS)) {
680                 char buf[1024];
681                 snprintf(buf, sizeof(buf), "%s%s%s%s",
682                          ((USE_EXT_LIBS) & USE_crypto)   ? "+crypto"   : "",
683                          ((USE_EXT_LIBS) & USE_gcrypt)   ? "+gcrypt"   : "",
684                          ((USE_EXT_LIBS) & USE_mbedcrypto) ? "+mbedcrypto" : "",
685                          ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
686                 fprintf(stderr, "Built with the following external libraries:\n"
687                         "make VERSUS=%s\n", buf + 1);
688             } else {
689                 fprintf(stderr, "Built without external libraries; use\n"
690                         "make VERSUS=crypto+gcrypt+mbedcrypto+tomcrypt tools/crypto_bench\n"
691                         "to enable them.\n");
692             }
693             exit(opt != 'h');
694         }
695     }
696
697     if (!input)
698         fatal_error("out of memory");
699     for (i = 0; i < MAX_INPUT_SIZE; i += 4)
700         AV_WB32(input + i, i);
701
702     size = MAX_INPUT_SIZE;
703     for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
704         run_implementation(input, output, &implementations[impl], size);
705
706     av_free(input);
707
708     return 0;
709 }