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
80 #define IMPL_USE_lavu IMPL_USE
88 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
89 static void run_lavu_ ## suffix(uint8_t *output, \
90 const uint8_t *input, unsigned size) \
92 static struct type *h; \
93 if (!h && !(h = av_ ## namespace ## _alloc())) \
94 fatal_error("out of memory"); \
95 av_ ## namespace ## _init(h, hsize); \
96 av_ ## namespace ## _update(h, input, size); \
97 av_ ## namespace ## _final(h, output); \
108 static struct AVAES *aes;
129 #if (USE_EXT_LIBS) & USE_crypto
131 #include <openssl/md5.h>
132 #include <openssl/sha.h>
133 #include <openssl/ripemd.h>
134 #include <openssl/aes.h>
135 #include <openssl/cast.h>
137 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
138 static void run_crypto_ ## suffix(uint8_t *output, \
139 const uint8_t *input, unsigned size) \
141 function(input, size, output); \
144 DEFINE_CRYPTO_WRAPPER(md5,
MD5)
145 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
146 DEFINE_CRYPTO_WRAPPER(sha256,
SHA256)
147 DEFINE_CRYPTO_WRAPPER(sha512,
SHA512)
148 DEFINE_CRYPTO_WRAPPER(ripemd160,
RIPEMD160)
150 static
void run_crypto_aes128(
uint8_t *output,
158 for (i = 0; i <
size; i += 16)
159 AES_encrypt(input + i, output + i, &aes);
162 static void run_crypto_cast128(
uint8_t *output,
163 const uint8_t *input,
unsigned size)
169 for (i = 0; i <
size; i += 8)
170 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
173 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
175 #define IMPL_USE_crypto(...)
182 #if (USE_EXT_LIBS) & USE_gcrypt
186 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
187 static void run_gcrypt_ ## suffix(uint8_t *output, \
188 const uint8_t *input, unsigned size) \
190 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
193 DEFINE_GCRYPT_WRAPPER(md5,
MD5)
194 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
195 DEFINE_GCRYPT_WRAPPER(sha256,
SHA256)
196 DEFINE_GCRYPT_WRAPPER(sha512,
SHA512)
197 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
199 static
void run_gcrypt_aes128(
uint8_t *output,
200 const
uint8_t *input,
unsigned size)
202 static gcry_cipher_hd_t aes;
204 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
206 gcry_cipher_encrypt(aes, output, size, input, size);
209 static void run_gcrypt_cast128(
uint8_t *output,
210 const uint8_t *input,
unsigned size)
212 static gcry_cipher_hd_t cast;
214 gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
216 gcry_cipher_encrypt(cast, output, size, input, size);
219 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
221 #define IMPL_USE_gcrypt(...)
228 #if (USE_EXT_LIBS) & USE_tomcrypt
230 #include <tomcrypt.h>
232 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
233 static void run_tomcrypt_ ## suffix(uint8_t *output, \
234 const uint8_t *input, unsigned size) \
237 namespace ## _init(&md); \
238 namespace ## _process(&md, input, size); \
239 namespace ## _done(&md, output); \
242 DEFINE_TOMCRYPT_WRAPPER(md5, md5,
MD5)
243 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
244 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256,
SHA256)
245 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512,
SHA512)
246 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160,
RIPEMD160)
248 static
void run_tomcrypt_aes128(
uint8_t *output,
249 const
uint8_t *input,
unsigned size)
256 for (i = 0; i <
size; i += 16)
257 aes_ecb_encrypt(input + i, output + i, &aes);
260 static void run_tomcrypt_cast128(
uint8_t *output,
261 const uint8_t *input,
unsigned size)
267 for (i = 0; i <
size; i += 8)
268 cast5_ecb_encrypt(input + i, output + i, &cast);
271 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
273 #define IMPL_USE_tomcrypt(...)
290 unsigned outlen = 0, outcrc = 0;
292 double mtime, ttime = 0, ttime2 = 0, stime;
298 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
299 outlen = strlen(impl->
output) / 2;
300 for (i = 0; i < outlen; i++) {
301 sscanf(impl->
output + i * 2,
"%02x", &val);
305 for (i = 0; i < 8; i++)
306 impl->
run(output, input, size);
307 for (i = 0; i < nruns; i++) {
308 memset(output, 0, size);
310 impl->
run(output, input, size);
312 if (outlen ? memcmp(output, outref, outlen) :
313 crc32(output, size) != outcrc) {
314 fprintf(stderr,
"Expected: ");
316 for (j = 0; j < outlen; j++)
317 fprintf(stderr,
"%02x", output[j]);
319 fprintf(stderr,
"%08x",
crc32(output, size));
320 fprintf(stderr,
"\n");
323 mtime = (double)(t1 - t0) /
size;
325 ttime2 += mtime * mtime;
330 stime = sqrt(ttime2 - ttime * ttime);
331 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
332 impl->
lib, impl->
name, size, nruns, ttime, stime);
336 #define IMPL_USE(lib, name, symbol, output) \
337 { #lib, name, run_ ## lib ## _ ## symbol, output },
338 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
339 #define IMPL_ALL(...) \
340 IMPL(lavu, __VA_ARGS__) \
341 IMPL(crypto, __VA_ARGS__) \
342 IMPL(gcrypt, __VA_ARGS__) \
343 IMPL(tomcrypt, __VA_ARGS__)
346 IMPL_ALL(
"MD5", md5,
"aa26ff5b895356bcffd9292ba9f89e66")
347 IMPL_ALL(
"SHA-1", sha1,
"1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
348 IMPL_ALL(
"SHA-256", sha256,
"14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
349 IMPL_ALL(
"SHA-512", sha512,
"3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
350 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
351 IMPL_ALL(
"RIPEMD-160", ripemd160,
"62a5321e4fc8784903bb43ab7752c75f8b25af00")
352 IMPL_ALL(
"AES-128", aes128,
"crc:ff6bc888")
353 IMPL_ALL(
"CAST-128", cast128,
"crc:456aa584")
356 int main(
int argc,
char **argv)
360 unsigned i, impl,
size;
363 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
376 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
378 if ((USE_EXT_LIBS)) {
380 snprintf(buf,
sizeof(buf),
"%s%s%s",
381 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
382 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
384 fprintf(stderr,
"Built with the following external libraries:\n"
385 "make VERSUS=%s\n", buf + 1);
387 fprintf(stderr,
"Built without external libraries; use\n"
388 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
389 "to enable them.\n");