crypto_bench.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2013 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /* Optional external libraries; can be enabled using:
  21. * make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench */
  22. #define USE_crypto 0x01 /* OpenSSL's libcrypto */
  23. #define USE_gcrypt 0x02 /* GnuTLS's libgcrypt */
  24. #define USE_tomcrypt 0x04 /* LibTomCrypt */
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include "libavutil/avutil.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/crc.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/timer.h"
  32. #ifndef AV_READ_TIME
  33. #define AV_READ_TIME(x) 0
  34. #endif
  35. #if HAVE_UNISTD_H
  36. #include <unistd.h> /* for getopt */
  37. #endif
  38. #if !HAVE_GETOPT
  39. #include "compat/getopt.c"
  40. #endif
  41. #define MAX_INPUT_SIZE 1048576
  42. #define MAX_OUTPUT_SIZE 128
  43. static const char *enabled_libs;
  44. static const char *enabled_algos;
  45. static unsigned specified_runs;
  46. static const uint8_t *hardcoded_key = "FFmpeg is the best program ever.";
  47. static void fatal_error(const char *tag)
  48. {
  49. av_log(NULL, AV_LOG_ERROR, "Fatal error: %s\n", tag);
  50. exit(1);
  51. }
  52. struct hash_impl {
  53. const char *lib;
  54. const char *name;
  55. void (*run)(uint8_t *output, const uint8_t *input, unsigned size);
  56. const char *output;
  57. };
  58. /***************************************************************************
  59. * lavu: libavutil
  60. ***************************************************************************/
  61. #include "libavutil/md5.h"
  62. #include "libavutil/sha.h"
  63. #include "libavutil/sha512.h"
  64. #include "libavutil/ripemd.h"
  65. #include "libavutil/aes.h"
  66. #include "libavutil/cast5.h"
  67. #define IMPL_USE_lavu IMPL_USE
  68. static void run_lavu_md5(uint8_t *output,
  69. const uint8_t *input, unsigned size)
  70. {
  71. av_md5_sum(output, input, size);
  72. }
  73. #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
  74. static void run_lavu_ ## suffix(uint8_t *output, \
  75. const uint8_t *input, unsigned size) \
  76. { \
  77. static struct type *h; \
  78. if (!h && !(h = av_ ## namespace ## _alloc())) \
  79. fatal_error("out of memory"); \
  80. av_ ## namespace ## _init(h, hsize); \
  81. av_ ## namespace ## _update(h, input, size); \
  82. av_ ## namespace ## _final(h, output); \
  83. }
  84. DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
  85. DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
  86. DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
  87. DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
  88. static void run_lavu_aes128(uint8_t *output,
  89. const uint8_t *input, unsigned size)
  90. {
  91. static struct AVAES *aes;
  92. if (!aes && !(aes = av_aes_alloc()))
  93. fatal_error("out of memory");
  94. av_aes_init(aes, hardcoded_key, 128, 0);
  95. av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
  96. }
  97. static void run_lavu_cast128(uint8_t *output,
  98. const uint8_t *input, unsigned size)
  99. {
  100. static struct AVCAST5 *cast;
  101. if (!cast && !(cast = av_cast5_alloc()))
  102. fatal_error("out of memory");
  103. av_cast5_init(cast, hardcoded_key, 128);
  104. av_cast5_crypt(cast, output, input, size >> 3, 0);
  105. }
  106. /***************************************************************************
  107. * crypto: OpenSSL's libcrypto
  108. ***************************************************************************/
  109. #if (USE_EXT_LIBS) & USE_crypto
  110. #include <openssl/md5.h>
  111. #include <openssl/sha.h>
  112. #include <openssl/ripemd.h>
  113. #include <openssl/aes.h>
  114. #include <openssl/cast.h>
  115. #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
  116. static void run_crypto_ ## suffix(uint8_t *output, \
  117. const uint8_t *input, unsigned size) \
  118. { \
  119. function(input, size, output); \
  120. }
  121. DEFINE_CRYPTO_WRAPPER(md5, MD5)
  122. DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
  123. DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
  124. DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
  125. DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
  126. static void run_crypto_aes128(uint8_t *output,
  127. const uint8_t *input, unsigned size)
  128. {
  129. AES_KEY aes;
  130. unsigned i;
  131. AES_set_encrypt_key(hardcoded_key, 128, &aes);
  132. size -= 15;
  133. for (i = 0; i < size; i += 16)
  134. AES_encrypt(input + i, output + i, &aes);
  135. }
  136. static void run_crypto_cast128(uint8_t *output,
  137. const uint8_t *input, unsigned size)
  138. {
  139. CAST_KEY cast;
  140. unsigned i;
  141. CAST_set_key(&cast, 16, hardcoded_key);
  142. for (i = 0; i < size; i += 8)
  143. CAST_ecb_encrypt(input + i, output + i, &cast, 1);
  144. }
  145. #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
  146. #else
  147. #define IMPL_USE_crypto(...) /* ignore */
  148. #endif
  149. /***************************************************************************
  150. * gcrypt: GnuTLS's libgcrypt
  151. ***************************************************************************/
  152. #if (USE_EXT_LIBS) & USE_gcrypt
  153. #include <gcrypt.h>
  154. #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
  155. static void run_gcrypt_ ## suffix(uint8_t *output, \
  156. const uint8_t *input, unsigned size) \
  157. { \
  158. gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
  159. }
  160. DEFINE_GCRYPT_WRAPPER(md5, MD5)
  161. DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
  162. DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
  163. DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
  164. DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
  165. static void run_gcrypt_aes128(uint8_t *output,
  166. const uint8_t *input, unsigned size)
  167. {
  168. static gcry_cipher_hd_t aes;
  169. if (!aes)
  170. gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
  171. gcry_cipher_setkey(aes, hardcoded_key, 16);
  172. gcry_cipher_encrypt(aes, output, size, input, size);
  173. }
  174. static void run_gcrypt_cast128(uint8_t *output,
  175. const uint8_t *input, unsigned size)
  176. {
  177. static gcry_cipher_hd_t cast;
  178. if (!cast)
  179. gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
  180. gcry_cipher_setkey(cast, hardcoded_key, 16);
  181. gcry_cipher_encrypt(cast, output, size, input, size);
  182. }
  183. #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
  184. #else
  185. #define IMPL_USE_gcrypt(...) /* ignore */
  186. #endif
  187. /***************************************************************************
  188. * tomcrypt: LibTomCrypt
  189. ***************************************************************************/
  190. #if (USE_EXT_LIBS) & USE_tomcrypt
  191. #include <tomcrypt.h>
  192. #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
  193. static void run_tomcrypt_ ## suffix(uint8_t *output, \
  194. const uint8_t *input, unsigned size) \
  195. { \
  196. hash_state md; \
  197. namespace ## _init(&md); \
  198. namespace ## _process(&md, input, size); \
  199. namespace ## _done(&md, output); \
  200. }
  201. DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
  202. DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
  203. DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
  204. DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
  205. DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
  206. static void run_tomcrypt_aes128(uint8_t *output,
  207. const uint8_t *input, unsigned size)
  208. {
  209. symmetric_key aes;
  210. unsigned i;
  211. aes_setup(hardcoded_key, 16, 0, &aes);
  212. size -= 15;
  213. for (i = 0; i < size; i += 16)
  214. aes_ecb_encrypt(input + i, output + i, &aes);
  215. }
  216. static void run_tomcrypt_cast128(uint8_t *output,
  217. const uint8_t *input, unsigned size)
  218. {
  219. symmetric_key cast;
  220. unsigned i;
  221. cast5_setup(hardcoded_key, 16, 0, &cast);
  222. for (i = 0; i < size; i += 8)
  223. cast5_ecb_encrypt(input + i, output + i, &cast);
  224. }
  225. #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
  226. #else
  227. #define IMPL_USE_tomcrypt(...) /* ignore */
  228. #endif
  229. /***************************************************************************
  230. * Driver code
  231. ***************************************************************************/
  232. static unsigned crc32(const uint8_t *data, unsigned size)
  233. {
  234. return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
  235. }
  236. static void run_implementation(const uint8_t *input, uint8_t *output,
  237. struct hash_impl *impl, unsigned size)
  238. {
  239. uint64_t t0, t1;
  240. unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
  241. unsigned outlen = 0, outcrc = 0;
  242. unsigned i, j, val;
  243. double mtime, ttime = 0, ttime2 = 0, stime;
  244. uint8_t outref[MAX_OUTPUT_SIZE];
  245. if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
  246. enabled_algos && !av_stristr(enabled_algos, impl->name))
  247. return;
  248. if (!sscanf(impl->output, "crc:%x", &outcrc)) {
  249. outlen = strlen(impl->output) / 2;
  250. for (i = 0; i < outlen; i++) {
  251. sscanf(impl->output + i * 2, "%02x", &val);
  252. outref[i] = val;
  253. }
  254. }
  255. for (i = 0; i < 8; i++) /* heat caches */
  256. impl->run(output, input, size);
  257. for (i = 0; i < nruns; i++) {
  258. memset(output, 0, size); /* avoid leftovers from previous runs */
  259. t0 = AV_READ_TIME();
  260. impl->run(output, input, size);
  261. t1 = AV_READ_TIME();
  262. if (outlen ? memcmp(output, outref, outlen) :
  263. crc32(output, size) != outcrc) {
  264. fprintf(stderr, "Expected: ");
  265. if (outlen)
  266. for (j = 0; j < outlen; j++)
  267. fprintf(stderr, "%02x", output[j]);
  268. else
  269. fprintf(stderr, "%08x", crc32(output, size));
  270. fprintf(stderr, "\n");
  271. fatal_error("output mismatch");
  272. }
  273. mtime = (double)(t1 - t0) / size;
  274. ttime += mtime;
  275. ttime2 += mtime * mtime;
  276. }
  277. ttime /= nruns;
  278. ttime2 /= nruns;
  279. stime = sqrt(ttime2 - ttime * ttime);
  280. printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
  281. impl->lib, impl->name, size, nruns, ttime, stime);
  282. fflush(stdout);
  283. }
  284. #define IMPL_USE(lib, name, symbol, output) \
  285. { #lib, name, run_ ## lib ## _ ## symbol, output },
  286. #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
  287. #define IMPL_ALL(...) \
  288. IMPL(lavu, __VA_ARGS__) \
  289. IMPL(crypto, __VA_ARGS__) \
  290. IMPL(gcrypt, __VA_ARGS__) \
  291. IMPL(tomcrypt, __VA_ARGS__)
  292. struct hash_impl implementations[] = {
  293. IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
  294. IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
  295. IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
  296. IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
  297. "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
  298. IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
  299. IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
  300. IMPL_ALL("CAST-128", cast128, "crc:456aa584")
  301. };
  302. int main(int argc, char **argv)
  303. {
  304. uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
  305. uint8_t *output = input + MAX_INPUT_SIZE;
  306. unsigned i, impl, size;
  307. int opt;
  308. while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
  309. switch (opt) {
  310. case 'l':
  311. enabled_libs = optarg;
  312. break;
  313. case 'a':
  314. enabled_algos = optarg;
  315. break;
  316. case 'r':
  317. specified_runs = strtol(optarg, NULL, 0);
  318. break;
  319. case 'h':
  320. default:
  321. fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
  322. argv[0]);
  323. if ((USE_EXT_LIBS)) {
  324. char buf[1024];
  325. snprintf(buf, sizeof(buf), "%s%s%s",
  326. ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
  327. ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
  328. ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
  329. fprintf(stderr, "Built with the following external libraries:\n"
  330. "make VERSUS=%s\n", buf + 1);
  331. } else {
  332. fprintf(stderr, "Built without external libraries; use\n"
  333. "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
  334. "to enable them.\n");
  335. }
  336. exit(opt != 'h');
  337. }
  338. }
  339. if (!input)
  340. fatal_error("out of memory");
  341. for (i = 0; i < MAX_INPUT_SIZE; i += 4)
  342. AV_WB32(input + i, i);
  343. size = MAX_INPUT_SIZE;
  344. for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
  345. run_implementation(input, output, &implementations[impl], size);
  346. av_free(input);
  347. return 0;
  348. }