You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
12KB

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