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