crypto_bench.c 25 KB

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