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.

584 lines
20KB

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