23 #define USE_crypto 0x01
24 #define USE_gcrypt 0x02
25 #define USE_tomcrypt 0x04
43 #define MAX_INPUT_SIZE 1048576
44 #define MAX_OUTPUT_SIZE 128
75 #define IMPL_USE_lavu IMPL_USE
83 #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
84 static void run_lavu_ ## suffix(uint8_t *output, \
85 const uint8_t *input, unsigned size) \
87 static struct type *h; \
88 if (!h && !(h = av_ ## namespace ## _alloc())) \
89 fatal_error("out of memory"); \
90 av_ ## namespace ## _init(h, hsize); \
91 av_ ## namespace ## _update(h, input, size); \
92 av_ ## namespace ## _final(h, output); \
103 static struct AVAES *aes;
114 #if (USE_EXT_LIBS) & USE_crypto
116 #include <openssl/md5.h>
117 #include <openssl/sha.h>
118 #include <openssl/ripemd.h>
119 #include <openssl/aes.h>
121 #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
122 static void run_crypto_ ## suffix(uint8_t *output, \
123 const uint8_t *input, unsigned size) \
125 function(input, size, output); \
128 DEFINE_CRYPTO_WRAPPER(md5,
MD5)
129 DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
130 DEFINE_CRYPTO_WRAPPER(sha256,
SHA256)
131 DEFINE_CRYPTO_WRAPPER(sha512,
SHA512)
132 DEFINE_CRYPTO_WRAPPER(ripemd160,
RIPEMD160)
134 static
void run_crypto_aes128(
uint8_t *output,
142 for (i = 0; i <
size; i += 16)
143 AES_encrypt(input + i, output + i, &aes);
146 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
148 #define IMPL_USE_crypto(...)
155 #if (USE_EXT_LIBS) & USE_gcrypt
159 #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
160 static void run_gcrypt_ ## suffix(uint8_t *output, \
161 const uint8_t *input, unsigned size) \
163 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
166 DEFINE_GCRYPT_WRAPPER(md5,
MD5)
167 DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
168 DEFINE_GCRYPT_WRAPPER(sha256,
SHA256)
169 DEFINE_GCRYPT_WRAPPER(sha512,
SHA512)
170 DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
172 static
void run_gcrypt_aes128(
uint8_t *output,
173 const
uint8_t *input,
unsigned size)
175 static gcry_cipher_hd_t aes;
177 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
179 gcry_cipher_encrypt(aes, output, size, input, size);
182 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
184 #define IMPL_USE_gcrypt(...)
191 #if (USE_EXT_LIBS) & USE_tomcrypt
193 #include <tomcrypt.h>
195 #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
196 static void run_tomcrypt_ ## suffix(uint8_t *output, \
197 const uint8_t *input, unsigned size) \
200 namespace ## _init(&md); \
201 namespace ## _process(&md, input, size); \
202 namespace ## _done(&md, output); \
205 DEFINE_TOMCRYPT_WRAPPER(md5, md5,
MD5)
206 DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
207 DEFINE_TOMCRYPT_WRAPPER(sha256, sha256,
SHA256)
208 DEFINE_TOMCRYPT_WRAPPER(sha512, sha512,
SHA512)
209 DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160,
RIPEMD160)
211 static
void run_tomcrypt_aes128(
uint8_t *output,
212 const
uint8_t *input,
unsigned size)
219 for (i = 0; i <
size; i += 16)
220 aes_ecb_encrypt(input + i, output + i, &aes);
223 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
225 #define IMPL_USE_tomcrypt(...)
242 unsigned outlen = 0, outcrc = 0;
244 double mtime, ttime = 0, ttime2 = 0, stime;
250 if (!sscanf(impl->
output,
"crc:%x", &outcrc)) {
251 outlen = strlen(impl->
output) / 2;
252 for (i = 0; i < outlen; i++) {
253 sscanf(impl->
output + i * 2,
"%02x", &val);
257 for (i = 0; i < 8; i++)
258 impl->
run(output, input, size);
259 for (i = 0; i < nruns; i++) {
260 memset(output, 0, size);
262 impl->
run(output, input, size);
264 if (outlen ? memcmp(output, outref, outlen) :
265 crc32(output, size) != outcrc) {
266 fprintf(stderr,
"Expected: ");
268 for (j = 0; j < outlen; j++)
269 fprintf(stderr,
"%02x", output[j]);
271 fprintf(stderr,
"%08x",
crc32(output, size));
272 fprintf(stderr,
"\n");
275 mtime = (double)(t1 - t0) /
size;
277 ttime2 += mtime * mtime;
282 stime = sqrt(ttime2 - ttime * ttime);
283 printf(
"%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
284 impl->
lib, impl->
name, size, nruns, ttime, stime);
288 #define IMPL_USE(lib, name, symbol, output) \
289 { #lib, name, run_ ## lib ## _ ## symbol, output },
290 #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
291 #define IMPL_ALL(...) \
292 IMPL(lavu, __VA_ARGS__) \
293 IMPL(crypto, __VA_ARGS__) \
294 IMPL(gcrypt, __VA_ARGS__) \
295 IMPL(tomcrypt, __VA_ARGS__)
298 IMPL_ALL(
"MD5", md5,
"aa26ff5b895356bcffd9292ba9f89e66")
299 IMPL_ALL(
"SHA-1", sha1,
"1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
300 IMPL_ALL(
"SHA-256", sha256,
"14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
301 IMPL_ALL(
"SHA-512", sha512,
"3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
302 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
303 IMPL_ALL(
"RIPEMD-160", ripemd160,
"62a5321e4fc8784903bb43ab7752c75f8b25af00")
304 IMPL_ALL(
"AES-128", aes128,
"crc:ff6bc888")
307 int main(
int argc,
char **argv)
311 unsigned i, impl,
size;
314 while ((opt =
getopt(argc, argv,
"hl:a:r:")) != -1) {
327 fprintf(stderr,
"Usage: %s [-l libs] [-a algos] [-r runs]\n",
329 if ((USE_EXT_LIBS)) {
331 snprintf(buf,
sizeof(buf),
"%s%s%s",
332 ((USE_EXT_LIBS) &
USE_crypto) ?
"+crypto" :
"",
333 ((USE_EXT_LIBS) &
USE_gcrypt) ?
"+gcrypt" :
"",
335 fprintf(stderr,
"Built with the following external libraries:\n"
336 "make VERSUS=%s\n", buf + 1);
338 fprintf(stderr,
"Built without external libraries; use\n"
339 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
340 "to enable them.\n");