cipher_driver.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * cipher_driver.c
  3. *
  4. * A driver for the generic cipher type
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017 Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47. #include <stdio.h> /* for printf() */
  48. #include "getopt_s.h"
  49. #include "cipher.h"
  50. #include "cipher_priv.h"
  51. #include "datatypes.h"
  52. #define PRINT_DEBUG 0
  53. void cipher_driver_test_throughput(srtp_cipher_t *c);
  54. srtp_err_status_t cipher_driver_self_test(srtp_cipher_type_t *ct);
  55. /*
  56. * cipher_driver_test_buffering(ct) tests the cipher's output
  57. * buffering for correctness by checking the consistency of succesive
  58. * calls
  59. */
  60. srtp_err_status_t cipher_driver_test_buffering(srtp_cipher_t *c);
  61. /*
  62. * functions for testing cipher cache thrash
  63. */
  64. srtp_err_status_t cipher_driver_test_array_throughput(srtp_cipher_type_t *ct,
  65. int klen,
  66. int num_cipher);
  67. void cipher_array_test_throughput(srtp_cipher_t *ca[], int num_cipher);
  68. uint64_t cipher_array_bits_per_second(srtp_cipher_t *cipher_array[],
  69. int num_cipher,
  70. unsigned octets_in_buffer,
  71. int num_trials);
  72. srtp_err_status_t cipher_array_delete(srtp_cipher_t *cipher_array[],
  73. int num_cipher);
  74. srtp_err_status_t cipher_array_alloc_init(srtp_cipher_t ***cipher_array,
  75. int num_ciphers,
  76. srtp_cipher_type_t *ctype,
  77. int klen);
  78. void usage(char *prog_name)
  79. {
  80. printf("usage: %s [ -t | -v | -a ]\n", prog_name);
  81. exit(255);
  82. }
  83. void check_status(srtp_err_status_t s)
  84. {
  85. if (s) {
  86. printf("error (code %d)\n", s);
  87. exit(s);
  88. }
  89. return;
  90. }
  91. /*
  92. * null_cipher and srtp_aes_icm are the cipher meta-objects
  93. * defined in the files in crypto/cipher subdirectory. these are
  94. * declared external so that we can use these cipher types here
  95. */
  96. extern srtp_cipher_type_t srtp_null_cipher;
  97. extern srtp_cipher_type_t srtp_aes_icm_128;
  98. extern srtp_cipher_type_t srtp_aes_icm_256;
  99. #ifdef GCM
  100. extern srtp_cipher_type_t srtp_aes_icm_192;
  101. extern srtp_cipher_type_t srtp_aes_gcm_128;
  102. extern srtp_cipher_type_t srtp_aes_gcm_256;
  103. #endif
  104. int main(int argc, char *argv[])
  105. {
  106. srtp_cipher_t *c = NULL;
  107. srtp_err_status_t status;
  108. /* clang-format off */
  109. unsigned char test_key[48] = {
  110. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  111. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  112. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  113. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  114. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  115. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  116. };
  117. /* clang-format on */
  118. int q;
  119. unsigned do_timing_test = 0;
  120. unsigned do_validation = 0;
  121. unsigned do_array_timing_test = 0;
  122. /* process input arguments */
  123. while (1) {
  124. q = getopt_s(argc, argv, "tva");
  125. if (q == -1)
  126. break;
  127. switch (q) {
  128. case 't':
  129. do_timing_test = 1;
  130. break;
  131. case 'v':
  132. do_validation = 1;
  133. break;
  134. case 'a':
  135. do_array_timing_test = 1;
  136. break;
  137. default:
  138. usage(argv[0]);
  139. }
  140. }
  141. printf("cipher test driver\n"
  142. "David A. McGrew\n"
  143. "Cisco Systems, Inc.\n");
  144. if (!do_validation && !do_timing_test && !do_array_timing_test)
  145. usage(argv[0]);
  146. /* arry timing (cache thrash) test */
  147. if (do_array_timing_test) {
  148. int max_num_cipher = 1 << 16; /* number of ciphers in cipher_array */
  149. int num_cipher;
  150. for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8)
  151. cipher_driver_test_array_throughput(&srtp_null_cipher, 0,
  152. num_cipher);
  153. for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8)
  154. cipher_driver_test_array_throughput(
  155. &srtp_aes_icm_128, SRTP_AES_ICM_128_KEY_LEN_WSALT, num_cipher);
  156. for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8)
  157. cipher_driver_test_array_throughput(
  158. &srtp_aes_icm_256, SRTP_AES_ICM_256_KEY_LEN_WSALT, num_cipher);
  159. #ifdef GCM
  160. for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8)
  161. cipher_driver_test_array_throughput(
  162. &srtp_aes_icm_192, SRTP_AES_ICM_192_KEY_LEN_WSALT, num_cipher);
  163. for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8) {
  164. cipher_driver_test_array_throughput(
  165. &srtp_aes_gcm_128, SRTP_AES_GCM_128_KEY_LEN_WSALT, num_cipher);
  166. }
  167. for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8) {
  168. cipher_driver_test_array_throughput(
  169. &srtp_aes_gcm_256, SRTP_AES_GCM_256_KEY_LEN_WSALT, num_cipher);
  170. }
  171. #endif
  172. }
  173. if (do_validation) {
  174. cipher_driver_self_test(&srtp_null_cipher);
  175. cipher_driver_self_test(&srtp_aes_icm_128);
  176. cipher_driver_self_test(&srtp_aes_icm_256);
  177. #ifdef GCM
  178. cipher_driver_self_test(&srtp_aes_icm_192);
  179. cipher_driver_self_test(&srtp_aes_gcm_128);
  180. cipher_driver_self_test(&srtp_aes_gcm_256);
  181. #endif
  182. }
  183. /* do timing and/or buffer_test on srtp_null_cipher */
  184. status = srtp_cipher_type_alloc(&srtp_null_cipher, &c, 0, 0);
  185. check_status(status);
  186. status = srtp_cipher_init(c, NULL);
  187. check_status(status);
  188. if (do_timing_test)
  189. cipher_driver_test_throughput(c);
  190. if (do_validation) {
  191. status = cipher_driver_test_buffering(c);
  192. check_status(status);
  193. }
  194. status = srtp_cipher_dealloc(c);
  195. check_status(status);
  196. /* run the throughput test on the aes_icm cipher (128-bit key) */
  197. status = srtp_cipher_type_alloc(&srtp_aes_icm_128, &c,
  198. SRTP_AES_ICM_128_KEY_LEN_WSALT, 0);
  199. if (status) {
  200. fprintf(stderr, "error: can't allocate cipher\n");
  201. exit(status);
  202. }
  203. status = srtp_cipher_init(c, test_key);
  204. check_status(status);
  205. if (do_timing_test)
  206. cipher_driver_test_throughput(c);
  207. if (do_validation) {
  208. status = cipher_driver_test_buffering(c);
  209. check_status(status);
  210. }
  211. status = srtp_cipher_dealloc(c);
  212. check_status(status);
  213. /* repeat the tests with 256-bit keys */
  214. status = srtp_cipher_type_alloc(&srtp_aes_icm_256, &c,
  215. SRTP_AES_ICM_256_KEY_LEN_WSALT, 0);
  216. if (status) {
  217. fprintf(stderr, "error: can't allocate cipher\n");
  218. exit(status);
  219. }
  220. status = srtp_cipher_init(c, test_key);
  221. check_status(status);
  222. if (do_timing_test)
  223. cipher_driver_test_throughput(c);
  224. if (do_validation) {
  225. status = cipher_driver_test_buffering(c);
  226. check_status(status);
  227. }
  228. status = srtp_cipher_dealloc(c);
  229. check_status(status);
  230. #ifdef GCM
  231. /* run the throughput test on the aes_gcm_128 cipher */
  232. status = srtp_cipher_type_alloc(&srtp_aes_gcm_128, &c,
  233. SRTP_AES_GCM_128_KEY_LEN_WSALT, 8);
  234. if (status) {
  235. fprintf(stderr, "error: can't allocate GCM 128 cipher\n");
  236. exit(status);
  237. }
  238. status = srtp_cipher_init(c, test_key);
  239. check_status(status);
  240. if (do_timing_test) {
  241. cipher_driver_test_throughput(c);
  242. }
  243. // GCM ciphers don't do buffering; they're "one shot"
  244. status = srtp_cipher_dealloc(c);
  245. check_status(status);
  246. /* run the throughput test on the aes_gcm_256 cipher */
  247. status = srtp_cipher_type_alloc(&srtp_aes_gcm_256, &c,
  248. SRTP_AES_GCM_256_KEY_LEN_WSALT, 16);
  249. if (status) {
  250. fprintf(stderr, "error: can't allocate GCM 256 cipher\n");
  251. exit(status);
  252. }
  253. status = srtp_cipher_init(c, test_key);
  254. check_status(status);
  255. if (do_timing_test) {
  256. cipher_driver_test_throughput(c);
  257. }
  258. // GCM ciphers don't do buffering; they're "one shot"
  259. status = srtp_cipher_dealloc(c);
  260. check_status(status);
  261. #endif
  262. return 0;
  263. }
  264. void cipher_driver_test_throughput(srtp_cipher_t *c)
  265. {
  266. int i;
  267. int min_enc_len = 32;
  268. int max_enc_len = 2048; /* should be a power of two */
  269. int num_trials = 1000000;
  270. printf("timing %s throughput, key length %d:\n", c->type->description,
  271. c->key_len);
  272. fflush(stdout);
  273. for (i = min_enc_len; i <= max_enc_len; i = i * 2)
  274. printf("msg len: %d\tgigabits per second: %f\n", i,
  275. srtp_cipher_bits_per_second(c, i, num_trials) / 1e9);
  276. }
  277. srtp_err_status_t cipher_driver_self_test(srtp_cipher_type_t *ct)
  278. {
  279. srtp_err_status_t status;
  280. printf("running cipher self-test for %s...", ct->description);
  281. status = srtp_cipher_type_self_test(ct);
  282. if (status) {
  283. printf("failed with error code %d\n", status);
  284. exit(status);
  285. }
  286. printf("passed\n");
  287. return srtp_err_status_ok;
  288. }
  289. /*
  290. * cipher_driver_test_buffering(ct) tests the cipher's output
  291. * buffering for correctness by checking the consistency of succesive
  292. * calls
  293. */
  294. #define INITIAL_BUFLEN 1024
  295. srtp_err_status_t cipher_driver_test_buffering(srtp_cipher_t *c)
  296. {
  297. int i, j, num_trials = 1000;
  298. unsigned len, buflen = INITIAL_BUFLEN;
  299. uint8_t buffer0[INITIAL_BUFLEN], buffer1[INITIAL_BUFLEN], *current, *end;
  300. uint8_t idx[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34 };
  302. srtp_err_status_t status;
  303. printf("testing output buffering for cipher %s...", c->type->description);
  304. for (i = 0; i < num_trials; i++) {
  305. /* set buffers to zero */
  306. for (j = 0; j < (int)buflen; j++) {
  307. buffer0[j] = buffer1[j] = 0;
  308. }
  309. /* initialize cipher */
  310. status = srtp_cipher_set_iv(c, (uint8_t *)idx, srtp_direction_encrypt);
  311. if (status)
  312. return status;
  313. /* generate 'reference' value by encrypting all at once */
  314. status = srtp_cipher_encrypt(c, buffer0, &buflen);
  315. if (status)
  316. return status;
  317. /* re-initialize cipher */
  318. status = srtp_cipher_set_iv(c, (uint8_t *)idx, srtp_direction_encrypt);
  319. if (status)
  320. return status;
  321. /* now loop over short lengths until buffer1 is encrypted */
  322. current = buffer1;
  323. end = buffer1 + buflen;
  324. while (current < end) {
  325. /* choose a short length */
  326. len = srtp_cipher_rand_u32_for_tests() & 0x01f;
  327. /* make sure that len doesn't cause us to overreach the buffer */
  328. if (current + len > end)
  329. len = end - current;
  330. status = srtp_cipher_encrypt(c, current, &len);
  331. if (status)
  332. return status;
  333. /* advance pointer into buffer1 to reflect encryption */
  334. current += len;
  335. /* if buffer1 is all encrypted, break out of loop */
  336. if (current == end)
  337. break;
  338. }
  339. /* compare buffers */
  340. for (j = 0; j < (int)buflen; j++) {
  341. if (buffer0[j] != buffer1[j]) {
  342. #if PRINT_DEBUG
  343. printf("test case %d failed at byte %d\n", i, j);
  344. printf("computed: %s\n",
  345. octet_string_hex_string(buffer1, buflen));
  346. printf("expected: %s\n",
  347. octet_string_hex_string(buffer0, buflen));
  348. #endif
  349. return srtp_err_status_algo_fail;
  350. }
  351. }
  352. }
  353. printf("passed\n");
  354. return srtp_err_status_ok;
  355. }
  356. /*
  357. * The function cipher_test_throughput_array() tests the effect of CPU
  358. * cache thrash on cipher throughput.
  359. *
  360. * cipher_array_alloc_init(ctype, array, num_ciphers) creates an array
  361. * of srtp_cipher_t of type ctype
  362. */
  363. srtp_err_status_t cipher_array_alloc_init(srtp_cipher_t ***ca,
  364. int num_ciphers,
  365. srtp_cipher_type_t *ctype,
  366. int klen)
  367. {
  368. int i, j;
  369. srtp_err_status_t status;
  370. uint8_t *key;
  371. srtp_cipher_t **cipher_array;
  372. /* pad klen allocation, to handle aes_icm reading 16 bytes for the
  373. 14-byte salt */
  374. int klen_pad = ((klen + 15) >> 4) << 4;
  375. /* allocate array of pointers to ciphers */
  376. cipher_array = (srtp_cipher_t **)srtp_crypto_alloc(sizeof(srtp_cipher_t *) *
  377. num_ciphers);
  378. if (cipher_array == NULL)
  379. return srtp_err_status_alloc_fail;
  380. /* set ca to location of cipher_array */
  381. *ca = cipher_array;
  382. /* allocate key */
  383. key = srtp_crypto_alloc(klen_pad);
  384. if (key == NULL) {
  385. srtp_crypto_free(cipher_array);
  386. return srtp_err_status_alloc_fail;
  387. }
  388. /* allocate and initialize an array of ciphers */
  389. for (i = 0; i < num_ciphers; i++) {
  390. /* allocate cipher */
  391. status = srtp_cipher_type_alloc(ctype, cipher_array, klen, 16);
  392. if (status)
  393. return status;
  394. /* generate random key and initialize cipher */
  395. srtp_cipher_rand_for_tests(key, klen);
  396. for (j = klen; j < klen_pad; j++)
  397. key[j] = 0;
  398. status = srtp_cipher_init(*cipher_array, key);
  399. if (status)
  400. return status;
  401. /* printf("%dth cipher is at %p\n", i, *cipher_array); */
  402. /* printf("%dth cipher description: %s\n", i, */
  403. /* (*cipher_array)->type->description); */
  404. /* advance cipher array pointer */
  405. cipher_array++;
  406. }
  407. srtp_crypto_free(key);
  408. return srtp_err_status_ok;
  409. }
  410. srtp_err_status_t cipher_array_delete(srtp_cipher_t *cipher_array[],
  411. int num_cipher)
  412. {
  413. int i;
  414. for (i = 0; i < num_cipher; i++) {
  415. srtp_cipher_dealloc(cipher_array[i]);
  416. }
  417. srtp_crypto_free(cipher_array);
  418. return srtp_err_status_ok;
  419. }
  420. /*
  421. * cipher_array_bits_per_second(c, l, t) computes (an estimate of) the
  422. * number of bits that a cipher implementation can encrypt in a second
  423. * when distinct keys are used to encrypt distinct messages
  424. *
  425. * c is a cipher (which MUST be allocated an initialized already), l
  426. * is the length in octets of the test data to be encrypted, and t is
  427. * the number of trials
  428. *
  429. * if an error is encountered, the value 0 is returned
  430. */
  431. uint64_t cipher_array_bits_per_second(srtp_cipher_t *cipher_array[],
  432. int num_cipher,
  433. unsigned octets_in_buffer,
  434. int num_trials)
  435. {
  436. int i;
  437. v128_t nonce;
  438. clock_t timer;
  439. unsigned char *enc_buf;
  440. int cipher_index = srtp_cipher_rand_u32_for_tests() % num_cipher;
  441. /* Over-alloc, for NIST CBC padding */
  442. enc_buf = srtp_crypto_alloc(octets_in_buffer + 17);
  443. if (enc_buf == NULL)
  444. return 0; /* indicate bad parameters by returning null */
  445. /* time repeated trials */
  446. v128_set_to_zero(&nonce);
  447. timer = clock();
  448. for (i = 0; i < num_trials; i++, nonce.v32[3] = i) {
  449. /* length parameter to srtp_cipher_encrypt is in/out -- out is total,
  450. * padded
  451. * length -- so reset it each time. */
  452. unsigned octets_to_encrypt = octets_in_buffer;
  453. /* encrypt buffer with cipher */
  454. srtp_cipher_set_iv(cipher_array[cipher_index], (uint8_t *)&nonce,
  455. srtp_direction_encrypt);
  456. srtp_cipher_encrypt(cipher_array[cipher_index], enc_buf,
  457. &octets_to_encrypt);
  458. /* choose a cipher at random from the array*/
  459. cipher_index = (*((uint32_t *)enc_buf)) % num_cipher;
  460. }
  461. timer = clock() - timer;
  462. srtp_crypto_free(enc_buf);
  463. if (timer == 0) {
  464. /* Too fast! */
  465. return 0;
  466. }
  467. return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer;
  468. }
  469. void cipher_array_test_throughput(srtp_cipher_t *ca[], int num_cipher)
  470. {
  471. int i;
  472. int min_enc_len = 16;
  473. int max_enc_len = 2048; /* should be a power of two */
  474. int num_trials = 1000000;
  475. printf("timing %s throughput with key length %d, array size %d:\n",
  476. (ca[0])->type->description, (ca[0])->key_len, num_cipher);
  477. fflush(stdout);
  478. for (i = min_enc_len; i <= max_enc_len; i = i * 4)
  479. printf("msg len: %d\tgigabits per second: %f\n", i,
  480. cipher_array_bits_per_second(ca, num_cipher, i, num_trials) /
  481. 1e9);
  482. }
  483. srtp_err_status_t cipher_driver_test_array_throughput(srtp_cipher_type_t *ct,
  484. int klen,
  485. int num_cipher)
  486. {
  487. srtp_cipher_t **ca = NULL;
  488. srtp_err_status_t status;
  489. status = cipher_array_alloc_init(&ca, num_cipher, ct, klen);
  490. if (status) {
  491. printf("error: cipher_array_alloc_init() failed with error code %d\n",
  492. status);
  493. return status;
  494. }
  495. cipher_array_test_throughput(ca, num_cipher);
  496. cipher_array_delete(ca, num_cipher);
  497. return srtp_err_status_ok;
  498. }