crypto_bench.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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+mbedcrypto 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. #define USE_mbedcrypto 0x08 /* mbed TLS */
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include "libavutil/avutil.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/crc.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/timer.h"
  33. #ifndef AV_READ_TIME
  34. #define AV_READ_TIME(x) 0
  35. #endif
  36. #if HAVE_UNISTD_H
  37. #include <unistd.h> /* for getopt */
  38. #endif
  39. #if !HAVE_GETOPT
  40. #include "compat/getopt.c"
  41. #endif
  42. #define MAX_INPUT_SIZE 1048576
  43. #define MAX_OUTPUT_SIZE 128
  44. static const char *enabled_libs;
  45. static const char *enabled_algos;
  46. static unsigned specified_runs;
  47. static const uint8_t *hardcoded_key = "FFmpeg is the best program ever.";
  48. static void fatal_error(const char *tag)
  49. {
  50. av_log(NULL, AV_LOG_ERROR, "Fatal error: %s\n", tag);
  51. exit(1);
  52. }
  53. struct hash_impl {
  54. const char *lib;
  55. const char *name;
  56. void (*run)(uint8_t *output, const uint8_t *input, unsigned size);
  57. const char *output;
  58. };
  59. /***************************************************************************
  60. * lavu: libavutil
  61. ***************************************************************************/
  62. #include "libavutil/md5.h"
  63. #include "libavutil/sha.h"
  64. #include "libavutil/sha512.h"
  65. #include "libavutil/ripemd.h"
  66. #include "libavutil/aes.h"
  67. #include "libavutil/blowfish.h"
  68. #include "libavutil/camellia.h"
  69. #include "libavutil/cast5.h"
  70. #include "libavutil/des.h"
  71. #include "libavutil/twofish.h"
  72. #include "libavutil/rc4.h"
  73. #include "libavutil/xtea.h"
  74. #define IMPL_USE_lavu IMPL_USE
  75. static void run_lavu_md5(uint8_t *output,
  76. const uint8_t *input, unsigned size)
  77. {
  78. av_md5_sum(output, input, size);
  79. }
  80. #define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
  81. static void run_lavu_ ## suffix(uint8_t *output, \
  82. const uint8_t *input, unsigned size) \
  83. { \
  84. static struct type *h; \
  85. if (!h && !(h = av_ ## namespace ## _alloc())) \
  86. fatal_error("out of memory"); \
  87. av_ ## namespace ## _init(h, hsize); \
  88. av_ ## namespace ## _update(h, input, size); \
  89. av_ ## namespace ## _final(h, output); \
  90. }
  91. DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
  92. DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
  93. DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
  94. DEFINE_LAVU_MD(ripemd128, AVRIPEMD, ripemd, 128);
  95. DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
  96. static void run_lavu_aes128(uint8_t *output,
  97. const uint8_t *input, unsigned size)
  98. {
  99. static struct AVAES *aes;
  100. if (!aes && !(aes = av_aes_alloc()))
  101. fatal_error("out of memory");
  102. av_aes_init(aes, hardcoded_key, 128, 0);
  103. av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
  104. }
  105. static void run_lavu_blowfish(uint8_t *output,
  106. const uint8_t *input, unsigned size)
  107. {
  108. static struct AVBlowfish *blowfish;
  109. if (!blowfish && !(blowfish = av_blowfish_alloc()))
  110. fatal_error("out of memory");
  111. av_blowfish_init(blowfish, hardcoded_key, 16);
  112. av_blowfish_crypt(blowfish, output, input, size >> 3, NULL, 0);
  113. }
  114. static void run_lavu_camellia(uint8_t *output,
  115. const uint8_t *input, unsigned size)
  116. {
  117. static struct AVCAMELLIA *camellia;
  118. if (!camellia && !(camellia = av_camellia_alloc()))
  119. fatal_error("out of memory");
  120. av_camellia_init(camellia, hardcoded_key, 128);
  121. av_camellia_crypt(camellia, output, input, size >> 4, NULL, 0);
  122. }
  123. static void run_lavu_cast128(uint8_t *output,
  124. const uint8_t *input, unsigned size)
  125. {
  126. static struct AVCAST5 *cast;
  127. if (!cast && !(cast = av_cast5_alloc()))
  128. fatal_error("out of memory");
  129. av_cast5_init(cast, hardcoded_key, 128);
  130. av_cast5_crypt(cast, output, input, size >> 3, 0);
  131. }
  132. static void run_lavu_des(uint8_t *output,
  133. const uint8_t *input, unsigned size)
  134. {
  135. static struct AVDES *des;
  136. if (!des && !(des = av_des_alloc()))
  137. fatal_error("out of memory");
  138. av_des_init(des, hardcoded_key, 64, 0);
  139. av_des_crypt(des, output, input, size >> 3, NULL, 0);
  140. }
  141. static void run_lavu_twofish(uint8_t *output,
  142. const uint8_t *input, unsigned size)
  143. {
  144. static struct AVTWOFISH *twofish;
  145. if (!twofish && !(twofish = av_twofish_alloc()))
  146. fatal_error("out of memory");
  147. av_twofish_init(twofish, hardcoded_key, 128);
  148. av_twofish_crypt(twofish, output, input, size >> 4, NULL, 0);
  149. }
  150. static void run_lavu_rc4(uint8_t *output,
  151. const uint8_t *input, unsigned size)
  152. {
  153. static struct AVRC4 *rc4;
  154. if (!rc4 && !(rc4 = av_rc4_alloc()))
  155. fatal_error("out of memory");
  156. av_rc4_init(rc4, hardcoded_key, 128, 0);
  157. av_rc4_crypt(rc4, output, input, size, NULL, 0);
  158. }
  159. static void run_lavu_xtea(uint8_t *output,
  160. const uint8_t *input, unsigned size)
  161. {
  162. static struct AVXTEA *xtea;
  163. if (!xtea && !(xtea = av_xtea_alloc()))
  164. fatal_error("out of memory");
  165. av_xtea_init(xtea, hardcoded_key);
  166. av_xtea_crypt(xtea, output, input, size >> 3, NULL, 0);
  167. }
  168. /***************************************************************************
  169. * crypto: OpenSSL's libcrypto
  170. ***************************************************************************/
  171. #if (USE_EXT_LIBS) & USE_crypto
  172. #define OPENSSL_DISABLE_OLD_DES_SUPPORT
  173. #include <openssl/md5.h>
  174. #include <openssl/sha.h>
  175. #include <openssl/ripemd.h>
  176. #include <openssl/aes.h>
  177. #include <openssl/blowfish.h>
  178. #include <openssl/camellia.h>
  179. #include <openssl/cast.h>
  180. #include <openssl/des.h>
  181. #include <openssl/rc4.h>
  182. #define DEFINE_CRYPTO_WRAPPER(suffix, function) \
  183. static void run_crypto_ ## suffix(uint8_t *output, \
  184. const uint8_t *input, unsigned size) \
  185. { \
  186. function(input, size, output); \
  187. }
  188. DEFINE_CRYPTO_WRAPPER(md5, MD5)
  189. DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
  190. DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
  191. DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
  192. DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
  193. static void run_crypto_aes128(uint8_t *output,
  194. const uint8_t *input, unsigned size)
  195. {
  196. AES_KEY aes;
  197. unsigned i;
  198. AES_set_encrypt_key(hardcoded_key, 128, &aes);
  199. size -= 15;
  200. for (i = 0; i < size; i += 16)
  201. AES_encrypt(input + i, output + i, &aes);
  202. }
  203. static void run_crypto_blowfish(uint8_t *output,
  204. const uint8_t *input, unsigned size)
  205. {
  206. BF_KEY blowfish;
  207. unsigned i;
  208. BF_set_key(&blowfish, 16, hardcoded_key);
  209. for (i = 0; i < size; i += 8)
  210. BF_ecb_encrypt(input + i, output + i, &blowfish, 1);
  211. }
  212. static void run_crypto_camellia(uint8_t *output,
  213. const uint8_t *input, unsigned size)
  214. {
  215. CAMELLIA_KEY camellia;
  216. unsigned i;
  217. Camellia_set_key(hardcoded_key, 128, &camellia);
  218. size -= 15;
  219. for (i = 0; i < size; i += 16)
  220. Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
  221. }
  222. static void run_crypto_cast128(uint8_t *output,
  223. const uint8_t *input, unsigned size)
  224. {
  225. CAST_KEY cast;
  226. unsigned i;
  227. CAST_set_key(&cast, 16, hardcoded_key);
  228. for (i = 0; i < size; i += 8)
  229. CAST_ecb_encrypt(input + i, output + i, &cast, 1);
  230. }
  231. static void run_crypto_des(uint8_t *output,
  232. const uint8_t *input, unsigned size)
  233. {
  234. DES_key_schedule des;
  235. unsigned i;
  236. DES_set_key(hardcoded_key, &des);
  237. for (i = 0; i < size; i += 8)
  238. DES_ecb_encrypt(input + i, output + i, &des, 1);
  239. }
  240. static void run_crypto_rc4(uint8_t *output,
  241. const uint8_t *input, unsigned size)
  242. {
  243. RC4_KEY rc4;
  244. RC4_set_key(&rc4, 16, hardcoded_key);
  245. RC4(&rc4, size, input, output);
  246. }
  247. #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
  248. #else
  249. #define IMPL_USE_crypto(...) /* ignore */
  250. #endif
  251. /***************************************************************************
  252. * gcrypt: GnuTLS's libgcrypt
  253. ***************************************************************************/
  254. #if (USE_EXT_LIBS) & USE_gcrypt
  255. #include <gcrypt.h>
  256. #define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
  257. static void run_gcrypt_ ## suffix(uint8_t *output, \
  258. const uint8_t *input, unsigned size) \
  259. { \
  260. gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
  261. }
  262. DEFINE_GCRYPT_WRAPPER(md5, MD5)
  263. DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
  264. DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
  265. DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
  266. DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
  267. #define DEFINE_GCRYPT_CYPHER_WRAPPER(suffix, cypher, mode, sz) \
  268. static void run_gcrypt_ ## suffix(uint8_t *output, \
  269. const uint8_t *input, unsigned size) \
  270. { \
  271. static gcry_cipher_hd_t suffix; \
  272. if (!suffix) \
  273. gcry_cipher_open(&suffix, GCRY_CIPHER_ ## cypher, GCRY_CIPHER_MODE_ ## mode, 0); \
  274. gcry_cipher_setkey(suffix, hardcoded_key, sz); \
  275. gcry_cipher_encrypt(suffix, output, size, input, size); \
  276. }
  277. DEFINE_GCRYPT_CYPHER_WRAPPER(aes128, AES128, ECB, 16)
  278. DEFINE_GCRYPT_CYPHER_WRAPPER(blowfish, BLOWFISH, ECB, 16)
  279. DEFINE_GCRYPT_CYPHER_WRAPPER(camellia, CAMELLIA128, ECB, 16)
  280. DEFINE_GCRYPT_CYPHER_WRAPPER(cast128, CAST5, ECB, 16)
  281. DEFINE_GCRYPT_CYPHER_WRAPPER(des, DES, ECB, 8)
  282. DEFINE_GCRYPT_CYPHER_WRAPPER(twofish, TWOFISH128, ECB, 16)
  283. DEFINE_GCRYPT_CYPHER_WRAPPER(rc4, ARCFOUR, STREAM, 16)
  284. #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
  285. #else
  286. #define IMPL_USE_gcrypt(...) /* ignore */
  287. #endif
  288. /***************************************************************************
  289. * mbedcrypto: mbed TLS
  290. ***************************************************************************/
  291. #if (USE_EXT_LIBS) & USE_mbedcrypto
  292. #include <mbedtls/aes.h>
  293. #include <mbedtls/arc4.h>
  294. #include <mbedtls/blowfish.h>
  295. #include <mbedtls/camellia.h>
  296. #include <mbedtls/des.h>
  297. #include <mbedtls/md5.h>
  298. #include <mbedtls/ripemd160.h>
  299. #include <mbedtls/sha1.h>
  300. #include <mbedtls/sha256.h>
  301. #include <mbedtls/sha512.h>
  302. #include <mbedtls/xtea.h>
  303. #define DEFINE_MBEDCRYPTO_WRAPPER(suffix) \
  304. static void run_mbedcrypto_ ## suffix(uint8_t *output, \
  305. const uint8_t *input, unsigned size) \
  306. { \
  307. mbedtls_ ## suffix ## _ret(input, size, output); \
  308. }
  309. #define DEFINE_MBEDCRYPTO_WRAPPER_SHA2(suffix) \
  310. static void run_mbedcrypto_ ## suffix(uint8_t *output, \
  311. const uint8_t *input, unsigned size) \
  312. { \
  313. mbedtls_ ## suffix ## _ret(input, size, output, 0); \
  314. }
  315. DEFINE_MBEDCRYPTO_WRAPPER(md5)
  316. DEFINE_MBEDCRYPTO_WRAPPER(ripemd160)
  317. DEFINE_MBEDCRYPTO_WRAPPER(sha1)
  318. DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha256)
  319. DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha512)
  320. #define DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(suffix, cypher, algo) \
  321. static void run_mbedcrypto_ ## suffix(uint8_t *output, \
  322. const uint8_t *input, unsigned size) \
  323. { \
  324. mbedtls_ ## cypher ## _context cypher; \
  325. \
  326. mbedtls_ ## cypher ## _init(&cypher); \
  327. mbedtls_ ## cypher ## _setkey_enc(&cypher, hardcoded_key, 128); \
  328. for (int i = 0; i < size; i += 16) \
  329. mbedtls_ ## cypher ## _crypt_ecb(&cypher, MBEDTLS_ ## algo ## _ENCRYPT, \
  330. input + i, output + i); \
  331. mbedtls_ ## cypher ## _free(&cypher); \
  332. }
  333. DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(aes128, aes, AES)
  334. DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(camellia, camellia, CAMELLIA)
  335. static void run_mbedcrypto_blowfish(uint8_t *output,
  336. const uint8_t *input, unsigned size)
  337. {
  338. mbedtls_blowfish_context blowfish;
  339. mbedtls_blowfish_init(&blowfish);
  340. mbedtls_blowfish_setkey(&blowfish, hardcoded_key, 128);
  341. for (int i = 0; i < size; i += 8)
  342. mbedtls_blowfish_crypt_ecb(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT,
  343. input + i, output + i);
  344. mbedtls_blowfish_free(&blowfish);
  345. }
  346. static void run_mbedcrypto_des(uint8_t *output,
  347. const uint8_t *input, unsigned size)
  348. {
  349. mbedtls_des_context des;
  350. mbedtls_des_init(&des);
  351. mbedtls_des_setkey_enc(&des, hardcoded_key);
  352. for (int i = 0; i < size; i += 8)
  353. mbedtls_des_crypt_ecb(&des, input + i, output + i);
  354. mbedtls_des_free(&des);
  355. }
  356. static void run_mbedcrypto_rc4(uint8_t *output,
  357. const uint8_t *input, unsigned size)
  358. {
  359. mbedtls_arc4_context rc4;
  360. mbedtls_arc4_init(&rc4);
  361. mbedtls_arc4_setup(&rc4, hardcoded_key, 16);
  362. mbedtls_arc4_crypt(&rc4, size, input, output);
  363. mbedtls_arc4_free(&rc4);
  364. }
  365. static void run_mbedcrypto_xtea(uint8_t *output,
  366. const uint8_t *input, unsigned size)
  367. {
  368. mbedtls_xtea_context xtea;
  369. mbedtls_xtea_init(&xtea);
  370. mbedtls_xtea_setup(&xtea, hardcoded_key);
  371. for (int i = 0; i < size; i += 8)
  372. mbedtls_xtea_crypt_ecb(&xtea, MBEDTLS_XTEA_ENCRYPT,
  373. input + i, output + i);
  374. mbedtls_xtea_free(&xtea);
  375. }
  376. #define IMPL_USE_mbedcrypto(...) IMPL_USE(__VA_ARGS__)
  377. #else
  378. #define IMPL_USE_mbedcrypto(...) /* ignore */
  379. #endif
  380. /***************************************************************************
  381. * tomcrypt: LibTomCrypt
  382. ***************************************************************************/
  383. #if (USE_EXT_LIBS) & USE_tomcrypt
  384. #include <tomcrypt.h>
  385. #define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
  386. static void run_tomcrypt_ ## suffix(uint8_t *output, \
  387. const uint8_t *input, unsigned size) \
  388. { \
  389. hash_state md; \
  390. namespace ## _init(&md); \
  391. namespace ## _process(&md, input, size); \
  392. namespace ## _done(&md, output); \
  393. }
  394. DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
  395. DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
  396. DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
  397. DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
  398. DEFINE_TOMCRYPT_WRAPPER(ripemd128, rmd128, RIPEMD128)
  399. DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
  400. static void run_tomcrypt_aes128(uint8_t *output,
  401. const uint8_t *input, unsigned size)
  402. {
  403. symmetric_key aes;
  404. unsigned i;
  405. aes_setup(hardcoded_key, 16, 0, &aes);
  406. size -= 15;
  407. for (i = 0; i < size; i += 16)
  408. aes_ecb_encrypt(input + i, output + i, &aes);
  409. }
  410. static void run_tomcrypt_blowfish(uint8_t *output,
  411. const uint8_t *input, unsigned size)
  412. {
  413. symmetric_key blowfish;
  414. unsigned i;
  415. blowfish_setup(hardcoded_key, 16, 0, &blowfish);
  416. for (i = 0; i < size; i += 8)
  417. blowfish_ecb_encrypt(input + i, output + i, &blowfish);
  418. }
  419. static void run_tomcrypt_camellia(uint8_t *output,
  420. const uint8_t *input, unsigned size)
  421. {
  422. symmetric_key camellia;
  423. unsigned i;
  424. camellia_setup(hardcoded_key, 16, 0, &camellia);
  425. size -= 15;
  426. for (i = 0; i < size; i += 16)
  427. camellia_ecb_encrypt(input + i, output + i, &camellia);
  428. }
  429. static void run_tomcrypt_cast128(uint8_t *output,
  430. const uint8_t *input, unsigned size)
  431. {
  432. symmetric_key cast;
  433. unsigned i;
  434. cast5_setup(hardcoded_key, 16, 0, &cast);
  435. for (i = 0; i < size; i += 8)
  436. cast5_ecb_encrypt(input + i, output + i, &cast);
  437. }
  438. static void run_tomcrypt_des(uint8_t *output,
  439. const uint8_t *input, unsigned size)
  440. {
  441. symmetric_key des;
  442. unsigned i;
  443. des_setup(hardcoded_key, 8, 0, &des);
  444. for (i = 0; i < size; i += 8)
  445. des_ecb_encrypt(input + i, output + i, &des);
  446. }
  447. static void run_tomcrypt_rc4(uint8_t *output,
  448. const uint8_t *input, unsigned size)
  449. {
  450. rc4_state rc4;
  451. rc4_stream_setup(&rc4, hardcoded_key, 16);
  452. rc4_stream_crypt(&rc4, input, size, output);
  453. rc4_stream_done(&rc4);
  454. }
  455. static void run_tomcrypt_twofish(uint8_t *output,
  456. const uint8_t *input, unsigned size)
  457. {
  458. symmetric_key twofish;
  459. unsigned i;
  460. twofish_setup(hardcoded_key, 16, 0, &twofish);
  461. size -= 15;
  462. for (i = 0; i < size; i += 16)
  463. twofish_ecb_encrypt(input + i, output + i, &twofish);
  464. }
  465. static void run_tomcrypt_xtea(uint8_t *output,
  466. const uint8_t *input, unsigned size)
  467. {
  468. symmetric_key xtea;
  469. unsigned i;
  470. xtea_setup(hardcoded_key, 16, 0, &xtea);
  471. for (i = 0; i < size; i += 8)
  472. xtea_ecb_encrypt(input + i, output + i, &xtea);
  473. }
  474. #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
  475. #else
  476. #define IMPL_USE_tomcrypt(...) /* ignore */
  477. #endif
  478. /***************************************************************************
  479. * Driver code
  480. ***************************************************************************/
  481. static unsigned crc32(const uint8_t *data, unsigned size)
  482. {
  483. return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
  484. }
  485. static void run_implementation(const uint8_t *input, uint8_t *output,
  486. struct hash_impl *impl, unsigned size)
  487. {
  488. uint64_t t0, t1;
  489. unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
  490. unsigned outlen = 0, outcrc = 0;
  491. unsigned i, j, val;
  492. double mtime, ttime = 0, ttime2 = 0, stime;
  493. uint8_t outref[MAX_OUTPUT_SIZE];
  494. if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
  495. enabled_algos && !av_stristr(enabled_algos, impl->name))
  496. return;
  497. if (!sscanf(impl->output, "crc:%x", &outcrc)) {
  498. outlen = strlen(impl->output) / 2;
  499. for (i = 0; i < outlen; i++) {
  500. sscanf(impl->output + i * 2, "%02x", &val);
  501. outref[i] = val;
  502. }
  503. }
  504. for (i = 0; i < 8; i++) /* heat caches */
  505. impl->run(output, input, size);
  506. for (i = 0; i < nruns; i++) {
  507. memset(output, 0, size); /* avoid leftovers from previous runs */
  508. t0 = AV_READ_TIME();
  509. impl->run(output, input, size);
  510. t1 = AV_READ_TIME();
  511. if (outlen ? memcmp(output, outref, outlen) :
  512. crc32(output, size) != outcrc) {
  513. fprintf(stderr, "Expected: ");
  514. if (outlen)
  515. for (j = 0; j < outlen; j++)
  516. fprintf(stderr, "%02x", output[j]);
  517. else
  518. fprintf(stderr, "%08x", crc32(output, size));
  519. fprintf(stderr, "\n");
  520. fatal_error("output mismatch");
  521. }
  522. mtime = (double)(t1 - t0) / size;
  523. ttime += mtime;
  524. ttime2 += mtime * mtime;
  525. }
  526. ttime /= nruns;
  527. ttime2 /= nruns;
  528. stime = sqrt(ttime2 - ttime * ttime);
  529. printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
  530. impl->lib, impl->name, size, nruns, ttime, stime);
  531. fflush(stdout);
  532. }
  533. #define IMPL_USE(lib, name, symbol, output) \
  534. { #lib, name, run_ ## lib ## _ ## symbol, output },
  535. #define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
  536. #define IMPL_ALL(...) \
  537. IMPL(lavu, __VA_ARGS__) \
  538. IMPL(crypto, __VA_ARGS__) \
  539. IMPL(gcrypt, __VA_ARGS__) \
  540. IMPL(mbedcrypto, __VA_ARGS__) \
  541. IMPL(tomcrypt, __VA_ARGS__)
  542. struct hash_impl implementations[] = {
  543. IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
  544. IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
  545. IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
  546. IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
  547. "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
  548. IMPL(lavu, "RIPEMD-128", ripemd128, "9ab8bfba2ddccc5d99c9d4cdfb844a5f")
  549. IMPL(tomcrypt, "RIPEMD-128", ripemd128, "9ab8bfba2ddccc5d99c9d4cdfb844a5f")
  550. IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
  551. IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
  552. IMPL_ALL("CAMELLIA", camellia, "crc:7abb59a7")
  553. IMPL(lavu, "CAST-128", cast128, "crc:456aa584")
  554. IMPL(crypto, "CAST-128", cast128, "crc:456aa584")
  555. IMPL(gcrypt, "CAST-128", cast128, "crc:456aa584")
  556. IMPL(tomcrypt, "CAST-128", cast128, "crc:456aa584")
  557. IMPL_ALL("BLOWFISH", blowfish, "crc:33e8aa74")
  558. IMPL_ALL("DES", des, "crc:31291e0b")
  559. IMPL(lavu, "TWOFISH", twofish, "crc:9edbd5c1")
  560. IMPL(gcrypt, "TWOFISH", twofish, "crc:9edbd5c1")
  561. IMPL(tomcrypt, "TWOFISH", twofish, "crc:9edbd5c1")
  562. IMPL_ALL("RC4", rc4, "crc:538d37b2")
  563. IMPL(lavu, "XTEA", xtea, "crc:931fc270")
  564. IMPL(mbedcrypto, "XTEA", xtea, "crc:931fc270")
  565. IMPL(tomcrypt, "XTEA", xtea, "crc:931fc270")
  566. };
  567. int main(int argc, char **argv)
  568. {
  569. uint8_t *input;
  570. uint8_t *output;
  571. unsigned i, impl, size;
  572. int opt;
  573. while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
  574. switch (opt) {
  575. case 'l':
  576. enabled_libs = optarg;
  577. break;
  578. case 'a':
  579. enabled_algos = optarg;
  580. break;
  581. case 'r':
  582. specified_runs = strtol(optarg, NULL, 0);
  583. break;
  584. case 'h':
  585. default:
  586. fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
  587. argv[0]);
  588. if ((USE_EXT_LIBS)) {
  589. char buf[1024];
  590. snprintf(buf, sizeof(buf), "%s%s%s%s",
  591. ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
  592. ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
  593. ((USE_EXT_LIBS) & USE_mbedcrypto) ? "+mbedcrypto" : "",
  594. ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
  595. fprintf(stderr, "Built with the following external libraries:\n"
  596. "make VERSUS=%s\n", buf + 1);
  597. } else {
  598. fprintf(stderr, "Built without external libraries; use\n"
  599. "make VERSUS=crypto+gcrypt+mbedcrypto+tomcrypt tools/crypto_bench\n"
  600. "to enable them.\n");
  601. }
  602. exit(opt != 'h');
  603. }
  604. }
  605. input = av_malloc(MAX_INPUT_SIZE * 2);
  606. if (!input)
  607. fatal_error("out of memory");
  608. for (i = 0; i < MAX_INPUT_SIZE; i += 4)
  609. AV_WB32(input + i, i);
  610. output = input + MAX_INPUT_SIZE;
  611. size = MAX_INPUT_SIZE;
  612. for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
  613. run_implementation(input, output, &implementations[impl], size);
  614. av_free(input);
  615. return 0;
  616. }