crypto_bench.c 17 KB

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