23#define USE_crypto 0x01
24#define USE_gcrypt 0x02
25#define USE_tomcrypt 0x04
26#define USE_mbedcrypto 0x08
39#define AV_READ_TIME(x) 0
49#define MAX_INPUT_SIZE 1048576
50#define MAX_OUTPUT_SIZE 128
56static const uint8_t *
hardcoded_key =
"FFmpeg is the best program ever.";
88#define IMPL_USE_lavu IMPL_USE
91 const uint8_t *input,
unsigned size)
96#define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
97static void run_lavu_ ## suffix(uint8_t *output, \
98 const uint8_t *input, unsigned size) \
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); \
115 const uint8_t *input,
unsigned size)
117 static struct AVAES *aes;
125 const uint8_t *input,
unsigned size)
135 const uint8_t *input,
unsigned size)
145 const uint8_t *input,
unsigned size)
155 const uint8_t *input,
unsigned size)
157 static struct AVDES *des;
165 const uint8_t *input,
unsigned size)
175 const uint8_t *input,
unsigned size)
177 static struct AVRC4 *rc4;
185 const uint8_t *input,
unsigned size)
187 static struct AVXTEA *xtea;
198#if (USE_EXT_LIBS) & USE_crypto
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>
211#define DEFINE_CRYPTO_WRAPPER(suffix, function) \
212static void run_crypto_ ## suffix(uint8_t *output, \
213 const uint8_t *input, unsigned size) \
215 function(input, size, output); \
218DEFINE_CRYPTO_WRAPPER(
md5, MD5)
219DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
220DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
221DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
222DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
224static void run_crypto_aes128(uint8_t *output,
225 const uint8_t *input,
unsigned size)
233 AES_encrypt(input +
i, output +
i, &aes);
236static void run_crypto_blowfish(uint8_t *output,
237 const uint8_t *input,
unsigned size)
244 BF_ecb_encrypt(input +
i, output +
i, &blowfish, 1);
247static void run_crypto_camellia(uint8_t *output,
248 const uint8_t *input,
unsigned size)
250 CAMELLIA_KEY camellia;
256 Camellia_ecb_encrypt(input +
i, output +
i, &camellia, 1);
259static void run_crypto_cast128(uint8_t *output,
260 const uint8_t *input,
unsigned size)
267 CAST_ecb_encrypt(input +
i, output +
i, &cast, 1);
270static void run_crypto_des(uint8_t *output,
271 const uint8_t *input,
unsigned size)
273 DES_key_schedule des;
278 DES_ecb_encrypt(input +
i, output +
i, &des, 1);
281static void run_crypto_rc4(uint8_t *output,
282 const uint8_t *input,
unsigned size)
287 RC4(&rc4,
size, input, output);
290#define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
292#define IMPL_USE_crypto(...)
299#if (USE_EXT_LIBS) & USE_gcrypt
303#define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
304static void run_gcrypt_ ## suffix(uint8_t *output, \
305 const uint8_t *input, unsigned size) \
307 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
310DEFINE_GCRYPT_WRAPPER(
md5, MD5)
311DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
312DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
313DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
314DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
316#define DEFINE_GCRYPT_CYPHER_WRAPPER(suffix, cypher, mode, sz) \
317static void run_gcrypt_ ## suffix(uint8_t *output, \
318 const uint8_t *input, unsigned size) \
320 static gcry_cipher_hd_t 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); \
327DEFINE_GCRYPT_CYPHER_WRAPPER(aes128, AES128, ECB, 16)
328DEFINE_GCRYPT_CYPHER_WRAPPER(blowfish, BLOWFISH, ECB, 16)
329DEFINE_GCRYPT_CYPHER_WRAPPER(camellia, CAMELLIA128, ECB, 16)
330DEFINE_GCRYPT_CYPHER_WRAPPER(cast128, CAST5, ECB, 16)
331DEFINE_GCRYPT_CYPHER_WRAPPER(des, DES, ECB, 8)
332DEFINE_GCRYPT_CYPHER_WRAPPER(twofish, TWOFISH128, ECB, 16)
333DEFINE_GCRYPT_CYPHER_WRAPPER(rc4, ARCFOUR, STREAM, 16)
335#define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
337#define IMPL_USE_gcrypt(...)
344#if (USE_EXT_LIBS) & USE_mbedcrypto
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>
358#define DEFINE_MBEDCRYPTO_WRAPPER(suffix) \
359static void run_mbedcrypto_ ## suffix(uint8_t *output, \
360 const uint8_t *input, unsigned size) \
362 mbedtls_ ## suffix ## _ret(input, size, output); \
365#define DEFINE_MBEDCRYPTO_WRAPPER_SHA2(suffix) \
366static void run_mbedcrypto_ ## suffix(uint8_t *output, \
367 const uint8_t *input, unsigned size) \
369 mbedtls_ ## suffix ## _ret(input, size, output, 0); \
372DEFINE_MBEDCRYPTO_WRAPPER(
md5)
373DEFINE_MBEDCRYPTO_WRAPPER(ripemd160)
374DEFINE_MBEDCRYPTO_WRAPPER(sha1)
375DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha256)
376DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha512)
379#define DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(suffix, cypher, algo) \
380static void run_mbedcrypto_ ## suffix(uint8_t *output, \
381 const uint8_t *input, unsigned size) \
383 mbedtls_ ## cypher ## _context cypher; \
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); \
393DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(aes128, aes, AES)
394DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(camellia, camellia, CAMELLIA)
396static void run_mbedcrypto_blowfish(uint8_t *output,
397 const uint8_t *input,
unsigned size)
399 mbedtls_blowfish_context blowfish;
401 mbedtls_blowfish_init(&blowfish);
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);
409static void run_mbedcrypto_des(uint8_t *output,
410 const uint8_t *input,
unsigned size)
412 mbedtls_des_context des;
414 mbedtls_des_init(&des);
416 for (
int i = 0;
i <
size;
i += 8)
417 mbedtls_des_crypt_ecb(&des, input +
i, output +
i);
418 mbedtls_des_free(&des);
421static void run_mbedcrypto_rc4(uint8_t *output,
422 const uint8_t *input,
unsigned size)
424 mbedtls_arc4_context rc4;
426 mbedtls_arc4_init(&rc4);
428 mbedtls_arc4_crypt(&rc4,
size, input, output);
429 mbedtls_arc4_free(&rc4);
432static void run_mbedcrypto_xtea(uint8_t *output,
433 const uint8_t *input,
unsigned size)
435 mbedtls_xtea_context xtea;
437 mbedtls_xtea_init(&xtea);
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);
445#define IMPL_USE_mbedcrypto(...) IMPL_USE(__VA_ARGS__)
447#define IMPL_USE_mbedcrypto(...)
454#if (USE_EXT_LIBS) & USE_tomcrypt
458#define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
459static void run_tomcrypt_ ## suffix(uint8_t *output, \
460 const uint8_t *input, unsigned size) \
463 namespace ## _init(&md); \
464 namespace ## _process(&md, input, size); \
465 namespace ## _done(&md, output); \
468DEFINE_TOMCRYPT_WRAPPER(
md5,
md5, MD5)
469DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
470DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
471DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
472DEFINE_TOMCRYPT_WRAPPER(ripemd128, rmd128, RIPEMD128)
473DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
475static void run_tomcrypt_aes128(uint8_t *output,
476 const uint8_t *input,
unsigned size)
484 aes_ecb_encrypt(input +
i, output +
i, &aes);
487static void run_tomcrypt_blowfish(uint8_t *output,
488 const uint8_t *input,
unsigned size)
490 symmetric_key blowfish;
495 blowfish_ecb_encrypt(input +
i, output +
i, &blowfish);
498static void run_tomcrypt_camellia(uint8_t *output,
499 const uint8_t *input,
unsigned size)
501 symmetric_key camellia;
507 camellia_ecb_encrypt(input +
i, output +
i, &camellia);
510static void run_tomcrypt_cast128(uint8_t *output,
511 const uint8_t *input,
unsigned size)
518 cast5_ecb_encrypt(input +
i, output +
i, &cast);
521static void run_tomcrypt_des(uint8_t *output,
522 const uint8_t *input,
unsigned size)
529 des_ecb_encrypt(input +
i, output +
i, &des);
532static void run_tomcrypt_rc4(uint8_t *output,
533 const uint8_t *input,
unsigned size)
538 rc4_stream_crypt(&rc4, input,
size, output);
539 rc4_stream_done(&rc4);
542static void run_tomcrypt_twofish(uint8_t *output,
543 const uint8_t *input,
unsigned size)
545 symmetric_key twofish;
551 twofish_ecb_encrypt(input +
i, output +
i, &twofish);
554static void run_tomcrypt_xtea(uint8_t *output,
555 const uint8_t *input,
unsigned size)
562 xtea_ecb_encrypt(input +
i, output +
i, &xtea);
566#define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
568#define IMPL_USE_tomcrypt(...)
585 unsigned outlen = 0, outcrc = 0;
587 double mtime, ttime = 0, ttime2 = 0, stime;
593 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
594 outlen = strlen(impl->
output) / 2;
595 for (
i = 0;
i < outlen;
i++) {
600 for (
i = 0;
i < 8;
i++)
601 impl->
run(output, input,
size);
602 for (
i = 0;
i < nruns;
i++) {
603 memset(output, 0,
size);
605 impl->
run(output, input,
size);
607 if (outlen ? memcmp(output, outref, outlen) :
609 fprintf(stderr,
"Expected: ");
611 for (j = 0; j < outlen; j++)
612 fprintf(stderr,
"%02x", output[j]);
614 fprintf(stderr,
"%08x",
crc32(output,
size));
615 fprintf(stderr,
"\n");
620 ttime2 += mtime * mtime;
625 stime = sqrt(ttime2 - ttime * ttime);
626 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
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__)
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")
671 unsigned i, impl,
size;
674 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
687 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
689 if ((USE_EXT_LIBS)) {
691 snprintf(buf,
sizeof(buf),
"%s%s%s%s",
692 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
693 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
696 fprintf(stderr,
"Built with the following external libraries:\n"
697 "make VERSUS=%s\n", buf + 1);
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");
static double val(void *priv, double ch)
Convenience header that includes libavutil's core.
Public header for libavutil CAMELLIA algorithm.
Public header for libavutil CAST5 algorithm.
#define i(width, name, range_min, range_max)
Public header for CRC hash function implementation.
static void run_lavu_twofish(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_camellia(uint8_t *output, const uint8_t *input, unsigned size)
static const char * enabled_libs
static void run_implementation(const uint8_t *input, uint8_t *output, struct hash_impl *impl, unsigned size)
static unsigned specified_runs
static void fatal_error(const char *tag)
static void run_lavu_xtea(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_blowfish(uint8_t *output, const uint8_t *input, unsigned size)
struct hash_impl implementations[]
static const char * enabled_algos
static void run_lavu_md5(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_cast128(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_des(uint8_t *output, const uint8_t *input, unsigned size)
#define DEFINE_LAVU_MD(suffix, type, namespace, hsize)
static void run_lavu_aes128(uint8_t *output, const uint8_t *input, unsigned size)
static const uint8_t * hardcoded_key
static void run_lavu_rc4(uint8_t *output, const uint8_t *input, unsigned size)
static unsigned crc32(const uint8_t *data, unsigned size)
__device__ int printf(const char *,...)
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, av_unused int decrypt)
static int getopt(int argc, char *argv[], const char *opts)
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
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.
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.
av_cold void av_blowfish_init(AVBlowfish *ctx, const uint8_t *key, int key_len)
Initialize an AVBlowfish context.
AVBlowfish * av_blowfish_alloc(void)
Allocate an AVBlowfish context.
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.
struct AVCAMELLIA * av_camellia_alloc(void)
Allocate an AVCAMELLIA context To free the struct: av_free(ptr)
av_cold int av_camellia_init(AVCAMELLIA *cs, const uint8_t *key, int key_bits)
Initialize an AVCAMELLIA context.
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_cast5_init(AVCAST5 *cs, const uint8_t *key, int key_bits)
Initialize an AVCAST5 context.
struct AVCAST5 * av_cast5_alloc(void)
Allocate an AVCAST5 context To free the struct: av_free(ptr)
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
AVDES * av_des_alloc(void)
Allocate an AVDES context.
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.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_md5_sum(uint8_t *dst, const uint8_t *src, size_t len)
Hash an array of data.
int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt)
Initializes an AVRC4 context.
AVRC4 * av_rc4_alloc(void)
Allocate an AVRC4 context.
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.
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_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.
struct AVTWOFISH * av_twofish_alloc(void)
Allocate an AVTWOFISH context To free the struct: av_free(ptr)
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.
AVXTEA * av_xtea_alloc(void)
Allocate an AVXTEA context.
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.
Public header for MD5 hash function implementation.
Memory handling functions.
Public header for RIPEMD hash function implementation.
Public header for SHA-512 implementation.
Public header for SHA-1 & SHA-256 hash function implementations.
#define FF_ARRAY_ELEMS(a)
void(* run)(uint8_t *output, const uint8_t *input, unsigned size)
static struct AVMD5 * md5
high precision timer, useful to profile code
Public header for libavutil TWOFISH algorithm.
Public header for libavutil XTEA algorithm.