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
85 #define IMPL_USE_lavu IMPL_USE
93 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
94 static void run_lavu_ ## suffix(uint8_t *output, \
95 const uint8_t *input, unsigned size) \
97 static struct type *h; \
98 if (!h && !(h = av_ ## namespace ## _alloc())) \
99 fatal_error("out of memory"); \
100 av_ ## namespace ## _init(h, hsize); \
101 av_ ## namespace ## _update(h, input, size); \
102 av_ ## namespace ## _final(h, output); \
114 static struct AVAES *aes;
164 static struct AVRC4 *rc4;
174 static struct AVXTEA *xtea;
185 #if (USE_EXT_LIBS) & USE_crypto
187 #include <openssl/md5.h>
188 #include <openssl/sha.h>
189 #include <openssl/ripemd.h>
190 #include <openssl/aes.h>
191 #include <openssl/blowfish.h>
192 #include <openssl/camellia.h>
193 #include <openssl/cast.h>
194 #include <openssl/rc4.h>
196 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
197 static void run_crypto_ ## suffix(uint8_t *output, \
198 const uint8_t *input, unsigned size) \
200 function(input, size, output); \
203 DEFINE_CRYPTO_WRAPPER(
md5,
MD5)
204 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
205 DEFINE_CRYPTO_WRAPPER(sha256,
SHA256)
206 DEFINE_CRYPTO_WRAPPER(sha512,
SHA512)
207 DEFINE_CRYPTO_WRAPPER(ripemd160,
RIPEMD160)
209 static
void run_crypto_aes128(
uint8_t *output,
217 for (i = 0; i <
size; i += 16)
218 AES_encrypt(input + i, output + i, &aes);
221 static void run_crypto_blowfish(
uint8_t *output,
222 const uint8_t *input,
unsigned size)
228 for (i = 0; i <
size; i += 8)
229 BF_ecb_encrypt(input + i, output + i, &blowfish, 1);
232 static void run_crypto_camellia(
uint8_t *output,
233 const uint8_t *input,
unsigned size)
235 CAMELLIA_KEY camellia;
240 for (i = 0; i <
size; i += 16)
241 Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
244 static void run_crypto_cast128(
uint8_t *output,
245 const uint8_t *input,
unsigned size)
251 for (i = 0; i <
size; i += 8)
252 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
255 static void run_crypto_rc4(
uint8_t *output,
256 const uint8_t *input,
unsigned size)
261 RC4(&rc4, size, input, output);
264 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
266 #define IMPL_USE_crypto(...)
273 #if (USE_EXT_LIBS) & USE_gcrypt
277 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
278 static void run_gcrypt_ ## suffix(uint8_t *output, \
279 const uint8_t *input, unsigned size) \
281 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
284 DEFINE_GCRYPT_WRAPPER(
md5,
MD5)
285 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
286 DEFINE_GCRYPT_WRAPPER(sha256,
SHA256)
287 DEFINE_GCRYPT_WRAPPER(sha512,
SHA512)
288 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
290 static
void run_gcrypt_aes128(
uint8_t *output,
291 const
uint8_t *input,
unsigned size)
293 static gcry_cipher_hd_t aes;
295 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
297 gcry_cipher_encrypt(aes, output, size, input, size);
300 static void run_gcrypt_blowfish(
uint8_t *output,
301 const uint8_t *input,
unsigned size)
303 static gcry_cipher_hd_t blowfish;
305 gcry_cipher_open(&blowfish, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 0);
307 gcry_cipher_encrypt(blowfish, output, size, input, size);
310 static void run_gcrypt_camellia(
uint8_t *output,
311 const uint8_t *input,
unsigned size)
313 static gcry_cipher_hd_t camellia;
315 gcry_cipher_open(&camellia, GCRY_CIPHER_CAMELLIA128, GCRY_CIPHER_MODE_ECB, 0);
317 gcry_cipher_encrypt(camellia, output, size, input, size);
320 static void run_gcrypt_cast128(
uint8_t *output,
321 const uint8_t *input,
unsigned size)
323 static gcry_cipher_hd_t cast;
325 gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
327 gcry_cipher_encrypt(cast, output, size, input, size);
330 static void run_gcrypt_twofish(
uint8_t *output,
331 const uint8_t *input,
unsigned size)
333 static gcry_cipher_hd_t twofish;
335 gcry_cipher_open(&twofish, GCRY_CIPHER_TWOFISH128, GCRY_CIPHER_MODE_ECB, 0);
337 gcry_cipher_encrypt(twofish, output, size, input, size);
340 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
342 #define IMPL_USE_gcrypt(...)
349 #if (USE_EXT_LIBS) & USE_tomcrypt
351 #include <tomcrypt.h>
353 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
354 static void run_tomcrypt_ ## suffix(uint8_t *output, \
355 const uint8_t *input, unsigned size) \
358 namespace ## _init(&md); \
359 namespace ## _process(&md, input, size); \
360 namespace ## _done(&md, output); \
364 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
365 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256,
SHA256)
366 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512,
SHA512)
367 DEFINE_TOMCRYPT_WRAPPER(ripemd128, rmd128,
RIPEMD128)
368 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160,
RIPEMD160)
370 static
void run_tomcrypt_aes128(
uint8_t *output,
371 const
uint8_t *input,
unsigned size)
378 for (i = 0; i <
size; i += 16)
379 aes_ecb_encrypt(input + i, output + i, &aes);
382 static void run_tomcrypt_blowfish(
uint8_t *output,
383 const uint8_t *input,
unsigned size)
385 symmetric_key blowfish;
389 for (i = 0; i <
size; i += 8)
390 blowfish_ecb_encrypt(input + i, output + i, &blowfish);
393 static void run_tomcrypt_camellia(
uint8_t *output,
394 const uint8_t *input,
unsigned size)
396 symmetric_key camellia;
401 for (i = 0; i <
size; i += 16)
402 camellia_ecb_encrypt(input + i, output + i, &camellia);
405 static void run_tomcrypt_cast128(
uint8_t *output,
406 const uint8_t *input,
unsigned size)
412 for (i = 0; i <
size; i += 8)
413 cast5_ecb_encrypt(input + i, output + i, &cast);
416 static void run_tomcrypt_twofish(
uint8_t *output,
417 const uint8_t *input,
unsigned size)
419 symmetric_key twofish;
424 for (i = 0; i <
size; i += 16)
425 twofish_ecb_encrypt(input + i, output + i, &twofish);
428 static void run_tomcrypt_xtea(
uint8_t *output,
429 const uint8_t *input,
unsigned size)
435 for (i = 0; i <
size; i += 8)
436 xtea_ecb_encrypt(input + i, output + i, &xtea);
440 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
442 #define IMPL_USE_tomcrypt(...)
459 unsigned outlen = 0, outcrc = 0;
461 double mtime, ttime = 0, ttime2 = 0, stime;
467 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
468 outlen = strlen(impl->
output) / 2;
469 for (i = 0; i < outlen; i++) {
470 sscanf(impl->
output + i * 2,
"%02x", &val);
474 for (i = 0; i < 8; i++)
475 impl->
run(output, input, size);
476 for (i = 0; i < nruns; i++) {
477 memset(output, 0, size);
479 impl->
run(output, input, size);
481 if (outlen ? memcmp(output, outref, outlen) :
482 crc32(output, size) != outcrc) {
483 fprintf(stderr,
"Expected: ");
485 for (j = 0; j < outlen; j++)
486 fprintf(stderr,
"%02x", output[j]);
488 fprintf(stderr,
"%08x",
crc32(output, size));
489 fprintf(stderr,
"\n");
492 mtime = (double)(t1 - t0) /
size;
494 ttime2 += mtime * mtime;
499 stime = sqrt(ttime2 - ttime * ttime);
500 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
501 impl->
lib, impl->
name, size, nruns, ttime, stime);
505 #define IMPL_USE(lib, name, symbol, output) \
506 { #lib, name, run_ ## lib ## _ ## symbol, output },
507 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
508 #define IMPL_ALL(...) \
509 IMPL(lavu, __VA_ARGS__) \
510 IMPL(crypto, __VA_ARGS__) \
511 IMPL(gcrypt, __VA_ARGS__) \
512 IMPL(tomcrypt, __VA_ARGS__)
515 IMPL_ALL(
"MD5",
md5,
"aa26ff5b895356bcffd9292ba9f89e66")
516 IMPL_ALL(
"SHA-1", sha1,
"1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
517 IMPL_ALL(
"SHA-256", sha256,
"14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
518 IMPL_ALL(
"SHA-512", sha512,
"3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
519 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
520 IMPL(lavu,
"RIPEMD-128", ripemd128,
"9ab8bfba2ddccc5d99c9d4cdfb844a5f")
521 IMPL(tomcrypt,
"RIPEMD-128", ripemd128,
"9ab8bfba2ddccc5d99c9d4cdfb844a5f")
522 IMPL_ALL(
"RIPEMD-160", ripemd160,
"62a5321e4fc8784903bb43ab7752c75f8b25af00")
523 IMPL_ALL(
"AES-128", aes128,
"crc:ff6bc888")
524 IMPL_ALL(
"CAMELLIA", camellia,
"crc:7abb59a7")
525 IMPL_ALL(
"CAST-128", cast128,
"crc:456aa584")
526 IMPL_ALL(
"BLOWFISH", blowfish,
"crc:33e8aa74")
527 IMPL(lavu,
"TWOFISH", twofish,
"crc:9edbd5c1")
528 IMPL(gcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
529 IMPL(tomcrypt,
"TWOFISH", twofish,
"crc:9edbd5c1")
530 IMPL(lavu,
"RC4", rc4,
"crc:538d37b2")
531 IMPL(crypto,
"RC4", rc4,
"crc:538d37b2")
532 IMPL(lavu,
"XTEA", xtea,
"crc:931fc270")
533 IMPL(tomcrypt,
"XTEA", xtea,
"crc:931fc270")
536 int main(
int argc,
char **argv)
540 unsigned i, impl,
size;
543 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
556 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
558 if ((USE_EXT_LIBS)) {
560 snprintf(buf,
sizeof(buf),
"%s%s%s",
561 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
562 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
564 fprintf(stderr,
"Built with the following external libraries:\n"
565 "make VERSUS=%s\n", buf + 1);
567 fprintf(stderr,
"Built without external libraries; use\n"
568 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
569 "to enable them.\n");
const char const char void * val
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 libavutil CAST5 algorithm.
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.
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.
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.
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)
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)
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)
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)
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)