Go to the documentation of this file.
23 #define USE_crypto 0x01
24 #define USE_gcrypt 0x02
25 #define USE_tomcrypt 0x04
26 #define USE_mbedcrypto 0x08
38 #define AV_READ_TIME(x) 0
48 #define MAX_INPUT_SIZE 1048576
49 #define MAX_OUTPUT_SIZE 128
55 static const uint8_t *
hardcoded_key =
"FFmpeg is the best program ever.";
87 #define IMPL_USE_lavu IMPL_USE
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) \
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); \
116 static struct AVAES *aes;
156 static struct AVDES *des;
176 static struct AVRC4 *rc4;
186 static struct AVXTEA *xtea;
197 #if (USE_EXT_LIBS) & USE_crypto
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>
210 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
211 static void run_crypto_ ## suffix(uint8_t *output, \
212 const uint8_t *input, unsigned size) \
214 function(input, size, output); \
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)
223 static void run_crypto_aes128(uint8_t *
output,
235 static void run_crypto_blowfish(uint8_t *
output,
246 static void run_crypto_camellia(uint8_t *
output,
249 CAMELLIA_KEY camellia;
258 static void run_crypto_cast128(uint8_t *
output,
269 static void run_crypto_des(uint8_t *
output,
272 DES_key_schedule des;
280 static void run_crypto_rc4(uint8_t *
output,
289 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
291 #define IMPL_USE_crypto(...)
298 #if (USE_EXT_LIBS) & USE_gcrypt
302 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
303 static void run_gcrypt_ ## suffix(uint8_t *output, \
304 const uint8_t *input, unsigned size) \
306 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
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)
315 #define DEFINE_GCRYPT_CYPHER_WRAPPER(suffix, cypher, mode, sz) \
316 static void run_gcrypt_ ## suffix(uint8_t *output, \
317 const uint8_t *input, unsigned size) \
319 static gcry_cipher_hd_t suffix; \
321 gcry_cipher_open(&suffix, GCRY_CIPHER_ ## cypher, GCRY_CIPHER_MODE_ ## mode, 0); \
322 gcry_cipher_setkey(suffix, hardcoded_key, sz); \
323 gcry_cipher_encrypt(suffix, output, size, input, size); \
326 DEFINE_GCRYPT_CYPHER_WRAPPER(aes128, AES128, ECB, 16)
327 DEFINE_GCRYPT_CYPHER_WRAPPER(blowfish, BLOWFISH, ECB, 16)
328 DEFINE_GCRYPT_CYPHER_WRAPPER(camellia, CAMELLIA128, ECB, 16)
329 DEFINE_GCRYPT_CYPHER_WRAPPER(cast128, CAST5, ECB, 16)
330 DEFINE_GCRYPT_CYPHER_WRAPPER(des, DES, ECB, 8)
331 DEFINE_GCRYPT_CYPHER_WRAPPER(twofish, TWOFISH128, ECB, 16)
332 DEFINE_GCRYPT_CYPHER_WRAPPER(rc4, ARCFOUR, STREAM, 16)
334 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
336 #define IMPL_USE_gcrypt(...)
343 #if (USE_EXT_LIBS) & USE_mbedcrypto
345 #include <mbedtls/aes.h>
346 #include <mbedtls/arc4.h>
347 #include <mbedtls/blowfish.h>
348 #include <mbedtls/camellia.h>
349 #include <mbedtls/des.h>
350 #include <mbedtls/md5.h>
351 #include <mbedtls/ripemd160.h>
352 #include <mbedtls/sha1.h>
353 #include <mbedtls/sha256.h>
354 #include <mbedtls/sha512.h>
355 #include <mbedtls/xtea.h>
357 #define DEFINE_MBEDCRYPTO_WRAPPER(suffix) \
358 static void run_mbedcrypto_ ## suffix(uint8_t *output, \
359 const uint8_t *input, unsigned size) \
361 mbedtls_ ## suffix ## _ret(input, size, output); \
364 #define DEFINE_MBEDCRYPTO_WRAPPER_SHA2(suffix) \
365 static void run_mbedcrypto_ ## suffix(uint8_t *output, \
366 const uint8_t *input, unsigned size) \
368 mbedtls_ ## suffix ## _ret(input, size, output, 0); \
371 DEFINE_MBEDCRYPTO_WRAPPER(
md5)
372 DEFINE_MBEDCRYPTO_WRAPPER(ripemd160)
373 DEFINE_MBEDCRYPTO_WRAPPER(sha1)
374 DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha256)
375 DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha512)
378 #define DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(suffix, cypher, algo) \
379 static void run_mbedcrypto_ ## suffix(uint8_t *output, \
380 const uint8_t *input, unsigned size) \
382 mbedtls_ ## cypher ## _context cypher; \
384 mbedtls_ ## cypher ## _init(&cypher); \
385 mbedtls_ ## cypher ## _setkey_enc(&cypher, hardcoded_key, 128); \
386 for (int i = 0; i < size; i += 16) \
387 mbedtls_ ## cypher ## _crypt_ecb(&cypher, MBEDTLS_ ## algo ## _ENCRYPT, \
388 input + i, output + i); \
389 mbedtls_ ## cypher ## _free(&cypher); \
392 DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(aes128, aes, AES)
393 DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(camellia, camellia, CAMELLIA)
395 static void run_mbedcrypto_blowfish(uint8_t *
output,
398 mbedtls_blowfish_context blowfish;
400 mbedtls_blowfish_init(&blowfish);
402 for (
int i = 0;
i <
size;
i += 8)
403 mbedtls_blowfish_crypt_ecb(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT,
405 mbedtls_blowfish_free(&blowfish);
408 static void run_mbedcrypto_des(uint8_t *
output,
411 mbedtls_des_context des;
413 mbedtls_des_init(&des);
415 for (
int i = 0;
i <
size;
i += 8)
417 mbedtls_des_free(&des);
420 static void run_mbedcrypto_rc4(uint8_t *
output,
423 mbedtls_arc4_context rc4;
425 mbedtls_arc4_init(&rc4);
428 mbedtls_arc4_free(&rc4);
431 static void run_mbedcrypto_xtea(uint8_t *
output,
434 mbedtls_xtea_context xtea;
436 mbedtls_xtea_init(&xtea);
438 for (
int i = 0;
i <
size;
i += 8)
439 mbedtls_xtea_crypt_ecb(&xtea, MBEDTLS_XTEA_ENCRYPT,
441 mbedtls_xtea_free(&xtea);
444 #define IMPL_USE_mbedcrypto(...) IMPL_USE(__VA_ARGS__)
446 #define IMPL_USE_mbedcrypto(...)
453 #if (USE_EXT_LIBS) & USE_tomcrypt
455 #include <tomcrypt.h>
457 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
458 static void run_tomcrypt_ ## suffix(uint8_t *output, \
459 const uint8_t *input, unsigned size) \
462 namespace ## _init(&md); \
463 namespace ## _process(&md, input, size); \
464 namespace ## _done(&md, output); \
468 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
469 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256,
SHA256)
470 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512,
SHA512)
471 DEFINE_TOMCRYPT_WRAPPER(ripemd128, rmd128,
RIPEMD128)
472 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160,
RIPEMD160)
474 static void run_tomcrypt_aes128(uint8_t *
output,
486 static void run_tomcrypt_blowfish(uint8_t *
output,
489 symmetric_key blowfish;
497 static void run_tomcrypt_camellia(uint8_t *
output,
500 symmetric_key camellia;
509 static void run_tomcrypt_cast128(uint8_t *
output,
520 static void run_tomcrypt_des(uint8_t *
output,
531 static void run_tomcrypt_rc4(uint8_t *
output,
538 rc4_stream_done(&rc4);
541 static void run_tomcrypt_twofish(uint8_t *
output,
544 symmetric_key twofish;
553 static void run_tomcrypt_xtea(uint8_t *
output,
565 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
567 #define IMPL_USE_tomcrypt(...)
584 unsigned outlen = 0, outcrc = 0;
586 double mtime, ttime = 0, ttime2 = 0, stime;
592 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
593 outlen = strlen(impl->
output) / 2;
594 for (
i = 0;
i < outlen;
i++) {
599 for (
i = 0;
i < 8;
i++)
601 for (
i = 0;
i < nruns;
i++) {
606 if (outlen ? memcmp(
output, outref, outlen) :
608 fprintf(stderr,
"Expected: ");
610 for (j = 0; j < outlen; j++)
611 fprintf(stderr,
"%02x",
output[j]);
614 fprintf(stderr,
"\n");
619 ttime2 += mtime * mtime;
624 stime = sqrt(ttime2 - ttime * ttime);
625 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
630 #define IMPL_USE(lib, name, symbol, output) \
631 { #lib, name, run_ ## lib ## _ ## symbol, output },
632 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
633 #define IMPL_ALL(...) \
634 IMPL(lavu, __VA_ARGS__) \
635 IMPL(crypto, __VA_ARGS__) \
636 IMPL(gcrypt, __VA_ARGS__) \
637 IMPL(mbedcrypto, __VA_ARGS__) \
638 IMPL(tomcrypt, __VA_ARGS__)
641 IMPL_ALL(
"MD5",
md5,
"aa26ff5b895356bcffd9292ba9f89e66")
642 IMPL_ALL(
"SHA-1", sha1,
"1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
643 IMPL_ALL(
"SHA-256", sha256,
"14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
644 IMPL_ALL(
"SHA-512", sha512,
"3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
645 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
646 IMPL(lavu,
"RIPEMD-128", ripemd128,
"9ab8bfba2ddccc5d99c9d4cdfb844a5f")
647 IMPL(tomcrypt,
"RIPEMD-128", ripemd128,
"9ab8bfba2ddccc5d99c9d4cdfb844a5f")
648 IMPL_ALL(
"RIPEMD-160", ripemd160,
"62a5321e4fc8784903bb43ab7752c75f8b25af00")
649 IMPL_ALL(
"AES-128", aes128,
"crc:ff6bc888")
650 IMPL_ALL(
"CAMELLIA", camellia,
"crc:7abb59a7")
651 IMPL(lavu,
"CAST-128", cast128,
"crc:456aa584")
652 IMPL(crypto,
"CAST-128", cast128,
"crc:456aa584")
653 IMPL(gcrypt,
"CAST-128", cast128,
"crc:456aa584")
654 IMPL(tomcrypt,
"CAST-128", cast128,
"crc:456aa584")
655 IMPL_ALL(
"BLOWFISH", blowfish,
"crc:33e8aa74")
656 IMPL_ALL(
"DES", des,
"crc:31291e0b")
657 IMPL(lavu,
"TWOFISH", twofish,
"crc:9edbd5c1")
658 IMPL(gcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
659 IMPL(tomcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
660 IMPL_ALL(
"RC4", rc4,
"crc:538d37b2")
661 IMPL(lavu,
"XTEA", xtea,
"crc:931fc270")
662 IMPL(mbedcrypto,
"XTEA", xtea,
"crc:931fc270")
663 IMPL(tomcrypt,
"XTEA", xtea,
"crc:931fc270")
666 int main(
int argc,
char **argv)
670 unsigned i, impl,
size;
673 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
686 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
688 if ((USE_EXT_LIBS)) {
690 snprintf(buf,
sizeof(buf),
"%s%s%s%s",
691 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
692 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
695 fprintf(stderr,
"Built with the following external libraries:\n"
696 "make VERSUS=%s\n", buf + 1);
698 fprintf(stderr,
"Built without external libraries; use\n"
699 "make VERSUS=crypto+gcrypt+mbedcrypto+tomcrypt tools/crypto_bench\n"
700 "to enable them.\n");
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, av_unused int decrypt)
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a cast
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
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.
struct AVCAMELLIA * av_camellia_alloc(void)
Allocate an AVCAMELLIA context To free the struct: av_free(ptr)
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
struct AVCAST5 * av_cast5_alloc(void)
Allocate an AVCAST5 context To free the struct: av_free(ptr)
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.
#define DEFINE_LAVU_MD(suffix, type, namespace, hsize)
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.
av_cold int av_twofish_init(AVTWOFISH *cs, const uint8_t *key, int key_bits)
Initialize an AVTWOFISH context.
void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
Initialize an AVXTEA context.
Public header for libavutil CAST5 algorithm.
static void run_lavu_des(uint8_t *output, const uint8_t *input, unsigned size)
static double val(void *priv, double ch)
static void run_lavu_aes128(uint8_t *output, const uint8_t *input, unsigned size)
struct hash_impl implementations[]
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.
static void run_lavu_camellia(uint8_t *output, const uint8_t *input, unsigned size)
struct AVTWOFISH * av_twofish_alloc(void)
Allocate an AVTWOFISH context To free the struct: av_free(ptr)
static const char * enabled_libs
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
static int getopt(int argc, char *argv[], char *opts)
AVBlowfish * av_blowfish_alloc(void)
Allocate an AVBlowfish context.
av_cold int av_camellia_init(AVCAMELLIA *cs, const uint8_t *key, int key_bits)
Initialize an AVCAMELLIA context.
int main(int argc, char **argv)
static unsigned crc32(const uint8_t *data, unsigned size)
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.
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
static void run_lavu_cast128(uint8_t *output, const uint8_t *input, unsigned size)
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.
static void run_lavu_twofish(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_md5(uint8_t *output, const uint8_t *input, unsigned size)
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.
Public header for libavutil TWOFISH algorithm.
static const char * enabled_algos
void av_md5_sum(uint8_t *dst, const uint8_t *src, size_t len)
Hash an array of data.
static void run_lavu_rc4(uint8_t *output, const uint8_t *input, unsigned size)
printf("static const uint8_t my_array[100] = {\n")
static void run_lavu_blowfish(uint8_t *output, const uint8_t *input, unsigned size)
AVRC4 * av_rc4_alloc(void)
Allocate an AVRC4 context.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
static void fatal_error(const char *tag)
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
AVDES * av_des_alloc(void)
Allocate an AVDES context.
void(* run)(uint8_t *output, const uint8_t *input, unsigned size)
#define i(width, name, range_min, range_max)
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.
AVXTEA * av_xtea_alloc(void)
Allocate an AVXTEA context.
int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt)
Initializes an AVRC4 context.
av_cold int av_cast5_init(AVCAST5 *cs, const uint8_t *key, int key_bits)
Initialize an AVCAST5 context.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
static unsigned specified_runs
Public header for libavutil XTEA algorithm.
static void run_lavu_xtea(uint8_t *output, const uint8_t *input, unsigned size)
static void run_implementation(const uint8_t *input, uint8_t *output, struct hash_impl *impl, unsigned size)
av_cold void av_blowfish_init(AVBlowfish *ctx, const uint8_t *key, int key_len)
Initialize an AVBlowfish context.
static const uint8_t * hardcoded_key
Public header for libavutil CAMELLIA algorithm.