crypto_bench.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #define IMPL_USE_lavu IMPL_USE
  67. static void run_lavu_md5(uint8_t *output,
  68. const uint8_t *input, unsigned size)
  69. {
  70. av_md5_sum(output, input, size);
  71. }
  72. #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
  73. static void run_lavu_ ## suffix(uint8_t *output, \
  74. const uint8_t *input, unsigned size) \
  75. { \
  76. static struct type *h; \
  77. if (!h && !(h = av_ ## namespace ## _alloc())) \
  78. fatal_error("out of memory"); \
  79. av_ ## namespace ## _init(h, hsize); \
  80. av_ ## namespace ## _update(h, input, size); \
  81. av_ ## namespace ## _final(h, output); \
  82. }
  83. DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
  84. DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
  85. DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
  86. DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
  87. static void run_lavu_aes128(uint8_t *output,
  88. const uint8_t *input, unsigned size)
  89. {
  90. static struct AVAES *aes;
  91. if (!aes && !(aes = av_aes_alloc()))
  92. fatal_error("out of memory");
  93. av_aes_init(aes, hardcoded_key, 128, 0);
  94. av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
  95. }
  96. /***************************************************************************
  97. * crypto: OpenSSL's libcrypto
  98. ***************************************************************************/
  99. #if (USE_EXT_LIBS) & USE_crypto
  100. #include <openssl/md5.h>
  101. #include <openssl/sha.h>
  102. #include <openssl/ripemd.h>
  103. #include <openssl/aes.h>
  104. #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
  105. static void run_crypto_ ## suffix(uint8_t *output, \
  106. const uint8_t *input, unsigned size) \
  107. { \
  108. function(input, size, output); \
  109. }
  110. DEFINE_CRYPTO_WRAPPER(md5, MD5)
  111. DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
  112. DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
  113. DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
  114. DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
  115. static void run_crypto_aes128(uint8_t *output,
  116. const uint8_t *input, unsigned size)
  117. {
  118. AES_KEY aes;
  119. unsigned i;
  120. AES_set_encrypt_key(hardcoded_key, 128, &aes);
  121. size -= 15;
  122. for (i = 0; i < size; i += 16)
  123. AES_encrypt(input + i, output + i, &aes);
  124. }
  125. #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
  126. #else
  127. #define IMPL_USE_crypto(...) /* ignore */
  128. #endif
  129. /***************************************************************************
  130. * gcrypt: GnuTLS's libgcrypt
  131. ***************************************************************************/
  132. #if (USE_EXT_LIBS) & USE_gcrypt
  133. #include <gcrypt.h>
  134. #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
  135. static void run_gcrypt_ ## suffix(uint8_t *output, \
  136. const uint8_t *input, unsigned size) \
  137. { \
  138. gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
  139. }
  140. DEFINE_GCRYPT_WRAPPER(md5, MD5)
  141. DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
  142. DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
  143. DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
  144. DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
  145. static void run_gcrypt_aes128(uint8_t *output,
  146. const uint8_t *input, unsigned size)
  147. {
  148. static gcry_cipher_hd_t aes;
  149. if (aes == NULL)
  150. gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
  151. gcry_cipher_setkey(aes, hardcoded_key, 16);
  152. gcry_cipher_encrypt(aes, output, size, input, size);
  153. }
  154. #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
  155. #else
  156. #define IMPL_USE_gcrypt(...) /* ignore */
  157. #endif
  158. /***************************************************************************
  159. * tomcrypt: LibTomCrypt
  160. ***************************************************************************/
  161. #if (USE_EXT_LIBS) & USE_tomcrypt
  162. #include <tomcrypt.h>
  163. #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
  164. static void run_tomcrypt_ ## suffix(uint8_t *output, \
  165. const uint8_t *input, unsigned size) \
  166. { \
  167. hash_state md; \
  168. namespace ## _init(&md); \
  169. namespace ## _process(&md, input, size); \
  170. namespace ## _done(&md, output); \
  171. }
  172. DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
  173. DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
  174. DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
  175. DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
  176. DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
  177. static void run_tomcrypt_aes128(uint8_t *output,
  178. const uint8_t *input, unsigned size)
  179. {
  180. symmetric_key aes;
  181. unsigned i;
  182. aes_setup(hardcoded_key, 16, 0, &aes);
  183. size -= 15;
  184. for (i = 0; i < size; i += 16)
  185. aes_ecb_encrypt(input + i, output + i, &aes);
  186. }
  187. #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
  188. #else
  189. #define IMPL_USE_tomcrypt(...) /* ignore */
  190. #endif
  191. /***************************************************************************
  192. * Driver code
  193. ***************************************************************************/
  194. static unsigned crc32(const uint8_t *data, unsigned size)
  195. {
  196. return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
  197. }
  198. static void run_implementation(const uint8_t *input, uint8_t *output,
  199. struct hash_impl *impl, unsigned size)
  200. {
  201. uint64_t t0, t1;
  202. unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
  203. unsigned outlen = 0, outcrc = 0;
  204. unsigned i, j, val;
  205. double mtime, ttime = 0, ttime2 = 0, stime;
  206. uint8_t outref[MAX_OUTPUT_SIZE];
  207. if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
  208. enabled_algos && !av_stristr(enabled_algos, impl->name))
  209. return;
  210. if (!sscanf(impl->output, "crc:%x", &outcrc)) {
  211. outlen = strlen(impl->output) / 2;
  212. for (i = 0; i < outlen; i++) {
  213. sscanf(impl->output + i * 2, "%02x", &val);
  214. outref[i] = val;
  215. }
  216. }
  217. for (i = 0; i < 8; i++) /* heat caches */
  218. impl->run(output, input, size);
  219. for (i = 0; i < nruns; i++) {
  220. memset(output, 0, size); /* avoid leftovers from previous runs */
  221. t0 = AV_READ_TIME();
  222. impl->run(output, input, size);
  223. t1 = AV_READ_TIME();
  224. if (outlen ? memcmp(output, outref, outlen) :
  225. crc32(output, size) != outcrc) {
  226. fprintf(stderr, "Expected: ");
  227. if (outlen)
  228. for (j = 0; j < outlen; j++)
  229. fprintf(stderr, "%02x", output[j]);
  230. else
  231. fprintf(stderr, "%08x", crc32(output, size));
  232. fprintf(stderr, "\n");
  233. fatal_error("output mismatch");
  234. }
  235. mtime = (double)(t1 - t0) / size;
  236. ttime += mtime;
  237. ttime2 += mtime * mtime;
  238. }
  239. ttime /= nruns;
  240. ttime2 /= nruns;
  241. stime = sqrt(ttime2 - ttime * ttime);
  242. printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
  243. impl->lib, impl->name, size, nruns, ttime, stime);
  244. fflush(stdout);
  245. }
  246. #define IMPL_USE(lib, name, symbol, output) \
  247. { #lib, name, run_ ## lib ## _ ## symbol, output },
  248. #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
  249. #define IMPL_ALL(...) \
  250. IMPL(lavu, __VA_ARGS__) \
  251. IMPL(crypto, __VA_ARGS__) \
  252. IMPL(gcrypt, __VA_ARGS__) \
  253. IMPL(tomcrypt, __VA_ARGS__)
  254. struct hash_impl implementations[] = {
  255. IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
  256. IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
  257. IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
  258. IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
  259. "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
  260. IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
  261. IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
  262. };
  263. int main(int argc, char **argv)
  264. {
  265. uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
  266. uint8_t *output = input + MAX_INPUT_SIZE;
  267. unsigned i, impl, size;
  268. int opt;
  269. while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
  270. switch (opt) {
  271. case 'l':
  272. enabled_libs = optarg;
  273. break;
  274. case 'a':
  275. enabled_algos = optarg;
  276. break;
  277. case 'r':
  278. specified_runs = strtol(optarg, NULL, 0);
  279. break;
  280. case 'h':
  281. default:
  282. fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
  283. argv[0]);
  284. if ((USE_EXT_LIBS)) {
  285. char buf[1024];
  286. snprintf(buf, sizeof(buf), "%s%s%s",
  287. ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
  288. ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
  289. ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
  290. fprintf(stderr, "Built with the following external libraries:\n"
  291. "make VERSUS=%s\n", buf + 1);
  292. } else {
  293. fprintf(stderr, "Built without external libraries; use\n"
  294. "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
  295. "to enable them.\n");
  296. }
  297. exit(opt != 'h');
  298. }
  299. }
  300. if (!input)
  301. fatal_error("out of memory");
  302. for (i = 0; i < MAX_INPUT_SIZE; i += 4)
  303. AV_WB32(input + i, i);
  304. size = MAX_INPUT_SIZE;
  305. for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
  306. run_implementation(input, output, &implementations[impl], size);
  307. av_free(input);
  308. return 0;
  309. }