crypto_bench.c 15 KB

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