23 #define USE_crypto 0x01
24 #define USE_gcrypt 0x02
25 #define USE_tomcrypt 0x04
37 #define AV_READ_TIME(x) 0
47 #define MAX_INPUT_SIZE 1048576
48 #define MAX_OUTPUT_SIZE 128
86 #define IMPL_USE_lavu IMPL_USE
94 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
95 static void run_lavu_ ## suffix(uint8_t *output, \
96 const uint8_t *input, unsigned size) \
98 static struct type *h; \
99 if (!h && !(h = av_ ## namespace ## _alloc())) \
100 fatal_error("out of memory"); \
101 av_ ## namespace ## _init(h, hsize); \
102 av_ ## namespace ## _update(h, input, size); \
103 av_ ## namespace ## _final(h, output); \
115 static struct AVAES *aes;
155 static struct AVDES *des;
175 static struct AVRC4 *rc4;
185 static struct AVXTEA *xtea;
196 #if (USE_EXT_LIBS) & USE_crypto
198 #define OPENSSL_DISABLE_OLD_DES_SUPPORT
199 #include <openssl/md5.h>
200 #include <openssl/sha.h>
201 #include <openssl/ripemd.h>
202 #include <openssl/aes.h>
203 #include <openssl/blowfish.h>
204 #include <openssl/camellia.h>
205 #include <openssl/cast.h>
206 #include <openssl/des.h>
207 #include <openssl/rc4.h>
209 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
210 static void run_crypto_ ## suffix(uint8_t *output, \
211 const uint8_t *input, unsigned size) \
213 function(input, size, output); \
216 DEFINE_CRYPTO_WRAPPER(
md5,
MD5)
217 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
218 DEFINE_CRYPTO_WRAPPER(sha256,
SHA256)
219 DEFINE_CRYPTO_WRAPPER(sha512,
SHA512)
220 DEFINE_CRYPTO_WRAPPER(ripemd160,
RIPEMD160)
222 static
void run_crypto_aes128(
uint8_t *output,
230 for (i = 0; i <
size; i += 16)
231 AES_encrypt(input + i, output + i, &aes);
234 static void run_crypto_blowfish(
uint8_t *output,
235 const uint8_t *input,
unsigned size)
241 for (i = 0; i <
size; i += 8)
242 BF_ecb_encrypt(input + i, output + i, &blowfish, 1);
245 static void run_crypto_camellia(
uint8_t *output,
246 const uint8_t *input,
unsigned size)
248 CAMELLIA_KEY camellia;
253 for (i = 0; i <
size; i += 16)
254 Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
257 static void run_crypto_cast128(
uint8_t *output,
258 const uint8_t *input,
unsigned size)
264 for (i = 0; i <
size; i += 8)
265 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
268 static void run_crypto_des(
uint8_t *output,
269 const uint8_t *input,
unsigned size)
271 DES_key_schedule des;
275 for (i = 0; i <
size; i += 8)
276 DES_ecb_encrypt(input + i, output + i, &des, 1);
279 static void run_crypto_rc4(
uint8_t *output,
280 const uint8_t *input,
unsigned size)
285 RC4(&rc4, size, input, output);
288 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
290 #define IMPL_USE_crypto(...)
297 #if (USE_EXT_LIBS) & USE_gcrypt
301 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
302 static void run_gcrypt_ ## suffix(uint8_t *output, \
303 const uint8_t *input, unsigned size) \
305 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
308 DEFINE_GCRYPT_WRAPPER(
md5,
MD5)
309 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
310 DEFINE_GCRYPT_WRAPPER(sha256,
SHA256)
311 DEFINE_GCRYPT_WRAPPER(sha512,
SHA512)
312 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
314 #define DEFINE_GCRYPT_CYPHER_WRAPPER(suffix, cypher, sz) \
315 static void run_gcrypt_ ## suffix(uint8_t *output, \
316 const uint8_t *input, unsigned size) \
318 static gcry_cipher_hd_t suffix; \
320 gcry_cipher_open(&suffix, GCRY_CIPHER_ ## cypher, GCRY_CIPHER_MODE_ECB, 0); \
321 gcry_cipher_setkey(suffix, hardcoded_key, sz); \
322 gcry_cipher_encrypt(suffix, output, size, input, size); \
325 DEFINE_GCRYPT_CYPHER_WRAPPER(aes128, AES128, 16)
326 DEFINE_GCRYPT_CYPHER_WRAPPER(blowfish, BLOWFISH, 16)
327 DEFINE_GCRYPT_CYPHER_WRAPPER(camellia, CAMELLIA128, 16)
328 DEFINE_GCRYPT_CYPHER_WRAPPER(cast128, CAST5, 16)
329 DEFINE_GCRYPT_CYPHER_WRAPPER(des, DES, 8)
330 DEFINE_GCRYPT_CYPHER_WRAPPER(twofish, TWOFISH128, 16)
332 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
334 #define IMPL_USE_gcrypt(...)
341 #if (USE_EXT_LIBS) & USE_tomcrypt
343 #include <tomcrypt.h>
345 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
346 static void run_tomcrypt_ ## suffix(uint8_t *output, \
347 const uint8_t *input, unsigned size) \
350 namespace ## _init(&md); \
351 namespace ## _process(&md, input, size); \
352 namespace ## _done(&md, output); \
356 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
357 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256,
SHA256)
358 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512,
SHA512)
359 DEFINE_TOMCRYPT_WRAPPER(ripemd128, rmd128,
RIPEMD128)
360 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160,
RIPEMD160)
362 static
void run_tomcrypt_aes128(
uint8_t *output,
363 const
uint8_t *input,
unsigned size)
370 for (i = 0; i <
size; i += 16)
371 aes_ecb_encrypt(input + i, output + i, &aes);
374 static void run_tomcrypt_blowfish(
uint8_t *output,
375 const uint8_t *input,
unsigned size)
377 symmetric_key blowfish;
381 for (i = 0; i <
size; i += 8)
382 blowfish_ecb_encrypt(input + i, output + i, &blowfish);
385 static void run_tomcrypt_camellia(
uint8_t *output,
386 const uint8_t *input,
unsigned size)
388 symmetric_key camellia;
393 for (i = 0; i <
size; i += 16)
394 camellia_ecb_encrypt(input + i, output + i, &camellia);
397 static void run_tomcrypt_cast128(
uint8_t *output,
398 const uint8_t *input,
unsigned size)
404 for (i = 0; i <
size; i += 8)
405 cast5_ecb_encrypt(input + i, output + i, &cast);
408 static void run_tomcrypt_des(
uint8_t *output,
409 const uint8_t *input,
unsigned size)
415 for (i = 0; i <
size; i += 8)
416 des_ecb_encrypt(input + i, output + i, &des);
419 static void run_tomcrypt_twofish(
uint8_t *output,
420 const uint8_t *input,
unsigned size)
422 symmetric_key twofish;
427 for (i = 0; i <
size; i += 16)
428 twofish_ecb_encrypt(input + i, output + i, &twofish);
431 static void run_tomcrypt_xtea(
uint8_t *output,
432 const uint8_t *input,
unsigned size)
438 for (i = 0; i <
size; i += 8)
439 xtea_ecb_encrypt(input + i, output + i, &xtea);
443 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
445 #define IMPL_USE_tomcrypt(...)
462 unsigned outlen = 0, outcrc = 0;
464 double mtime, ttime = 0, ttime2 = 0, stime;
470 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
471 outlen = strlen(impl->
output) / 2;
472 for (i = 0; i < outlen; i++) {
473 sscanf(impl->
output + i * 2,
"%02x", &val);
477 for (i = 0; i < 8; i++)
478 impl->
run(output, input, size);
479 for (i = 0; i < nruns; i++) {
480 memset(output, 0, size);
482 impl->
run(output, input, size);
484 if (outlen ? memcmp(output, outref, outlen) :
485 crc32(output, size) != outcrc) {
486 fprintf(stderr,
"Expected: ");
488 for (j = 0; j < outlen; j++)
489 fprintf(stderr,
"%02x", output[j]);
491 fprintf(stderr,
"%08x",
crc32(output, size));
492 fprintf(stderr,
"\n");
495 mtime = (double)(t1 - t0) /
size;
497 ttime2 += mtime * mtime;
502 stime = sqrt(ttime2 - ttime * ttime);
503 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
504 impl->
lib, impl->
name, size, nruns, ttime, stime);
508 #define IMPL_USE(lib, name, symbol, output) \
509 { #lib, name, run_ ## lib ## _ ## symbol, output },
510 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
511 #define IMPL_ALL(...) \
512 IMPL(lavu, __VA_ARGS__) \
513 IMPL(crypto, __VA_ARGS__) \
514 IMPL(gcrypt, __VA_ARGS__) \
515 IMPL(tomcrypt, __VA_ARGS__)
518 IMPL_ALL(
"MD5",
md5,
"aa26ff5b895356bcffd9292ba9f89e66")
519 IMPL_ALL(
"SHA-1", sha1,
"1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
520 IMPL_ALL(
"SHA-256", sha256,
"14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
521 IMPL_ALL(
"SHA-512", sha512,
"3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
522 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
523 IMPL(lavu,
"RIPEMD-128", ripemd128,
"9ab8bfba2ddccc5d99c9d4cdfb844a5f")
524 IMPL(tomcrypt,
"RIPEMD-128", ripemd128,
"9ab8bfba2ddccc5d99c9d4cdfb844a5f")
525 IMPL_ALL(
"RIPEMD-160", ripemd160,
"62a5321e4fc8784903bb43ab7752c75f8b25af00")
526 IMPL_ALL(
"AES-128", aes128,
"crc:ff6bc888")
527 IMPL_ALL(
"CAMELLIA", camellia,
"crc:7abb59a7")
528 IMPL_ALL(
"CAST-128", cast128,
"crc:456aa584")
529 IMPL_ALL(
"BLOWFISH", blowfish,
"crc:33e8aa74")
530 IMPL_ALL(
"DES", des,
"crc:31291e0b")
531 IMPL(lavu,
"TWOFISH", twofish,
"crc:9edbd5c1")
532 IMPL(gcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
533 IMPL(tomcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
534 IMPL(lavu,
"RC4", rc4,
"crc:538d37b2")
535 IMPL(crypto,
"RC4", rc4,
"crc:538d37b2")
536 IMPL(lavu,
"XTEA", xtea,
"crc:931fc270")
537 IMPL(tomcrypt,
"XTEA", xtea,
"crc:931fc270")
540 int main(
int argc,
char **argv)
544 unsigned i, impl,
size;
547 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
560 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
562 if ((USE_EXT_LIBS)) {
564 snprintf(buf,
sizeof(buf),
"%s%s%s",
565 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
566 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
568 fprintf(stderr,
"Built with the following external libraries:\n"
569 "make VERSUS=%s\n", buf + 1);
571 fprintf(stderr,
"Built without external libraries; use\n"
572 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
573 "to enable them.\n");
const char const char void * val
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.
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.
ptrdiff_t const GLvoid * data
AVBlowfish * av_blowfish_alloc(void)
Allocate an AVBlowfish 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.
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
static void run_implementation(const uint8_t *input, uint8_t *output, struct hash_impl *impl, unsigned size)
static const uint8_t * hardcoded_key
Public header for RIPEMD hash function implementation.
Public header for libavutil CAST5 algorithm.
static void run_lavu_des(uint8_t *output, const uint8_t *input, 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.
Convenience header that includes libavutil's core.
static void run_lavu_camellia(uint8_t *output, const uint8_t *input, unsigned size)
struct AVCAMELLIA * av_camellia_alloc(void)
Allocate an AVCAMELLIA context To free the struct: av_free(ptr)
av_cold int av_cast5_init(AVCAST5 *cs, const uint8_t *key, int key_bits)
Initialize an AVCAST5 context.
struct hash_impl implementations[]
int main(int argc, char **argv)
Public header for libavutil CAMELLIA algorithm.
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.
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.
Public header for CRC hash function implementation.
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...
void(* run)(uint8_t *output, const uint8_t *input, unsigned size)
void av_md5_sum(uint8_t *dst, const uint8_t *src, const int len)
Hash an array of data.
high precision timer, useful to profile code
static void run_lavu_aes128(uint8_t *output, const uint8_t *input, unsigned size)
Public header for libavutil TWOFISH algorithm.
static const char * enabled_libs
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
av_cold void av_blowfish_init(AVBlowfish *ctx, const uint8_t *key, int key_len)
Initialize an AVBlowfish context.
av_cold int av_camellia_init(AVCAMELLIA *cs, const uint8_t *key, int key_bits)
Initialize an AVCAMELLIA context.
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, av_unused int decrypt)
void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
Initialize an AVXTEA context.
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
static unsigned crc32(const uint8_t *data, unsigned size)
static void run_lavu_md5(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_twofish(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)
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.
Public header for libavutil XTEA algorithm.
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
AVRC4 * av_rc4_alloc(void)
Allocate an AVRC4 context.
#define FF_ARRAY_ELEMS(a)
AVDES * av_des_alloc(void)
Allocate an AVDES context.
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
static const char * enabled_algos
static void run_lavu_cast128(uint8_t *output, const uint8_t *input, unsigned size)
Public header for SHA-1 & SHA-256 hash function implementations.
static void run_lavu_blowfish(uint8_t *output, const uint8_t *input, unsigned size)
static void run_lavu_rc4(uint8_t *output, const uint8_t *input, unsigned size)
static int getopt(int argc, char *argv[], char *opts)
static unsigned specified_runs
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.
static void fatal_error(const char *tag)
Public header for SHA-512 implementation.
int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt)
Initializes an AVRC4 context.
static void run_lavu_xtea(uint8_t *output, const uint8_t *input, unsigned size)
Public header for MD5 hash function implementation.
av_cold int av_twofish_init(AVTWOFISH *cs, const uint8_t *key, int key_bits)
Initialize an AVTWOFISH context.
AVXTEA * av_xtea_alloc(void)
Allocate an AVXTEA context.
#define DEFINE_LAVU_MD(suffix, type, namespace, hsize)