2
0

cipher.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * cipher.c
  3. *
  4. * cipher meta-functions
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. *
  9. */
  10. /*
  11. *
  12. * Copyright (c) 2001-2006,2013 Cisco Systems, Inc.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * Neither the name of the Cisco Systems, Inc. nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  36. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  38. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42. * OF THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. */
  45. #include "cipher.h"
  46. #include "crypto_types.h"
  47. #include "rand_source.h" /* used in invertibiltiy tests */
  48. #include "alloc.h" /* for crypto_alloc(), crypto_free() */
  49. debug_module_t mod_cipher = {
  50. 0, /* debugging is off by default */
  51. "cipher" /* printable module name */
  52. };
  53. err_status_t
  54. cipher_output(cipher_t *c, uint8_t *buffer, int num_octets_to_output) {
  55. /* zeroize the buffer */
  56. octet_string_set_to_zero(buffer, num_octets_to_output);
  57. /* exor keystream into buffer */
  58. return cipher_encrypt(c, buffer, (unsigned int *) &num_octets_to_output);
  59. }
  60. /* some bookkeeping functions */
  61. int
  62. cipher_get_key_length(const cipher_t *c) {
  63. return c->key_len;
  64. }
  65. /*
  66. * cipher_type_test(ct, test_data) tests a cipher of type ct against
  67. * test cases provided in a list test_data of values of key, salt, iv,
  68. * plaintext, and ciphertext that is known to be good
  69. */
  70. #define SELF_TEST_BUF_OCTETS 128
  71. #define NUM_RAND_TESTS 128
  72. #define MAX_KEY_LEN 64
  73. err_status_t
  74. cipher_type_test(const cipher_type_t *ct, const cipher_test_case_t *test_data) {
  75. const cipher_test_case_t *test_case = test_data;
  76. cipher_t *c;
  77. err_status_t status;
  78. uint8_t buffer[SELF_TEST_BUF_OCTETS];
  79. uint8_t buffer2[SELF_TEST_BUF_OCTETS];
  80. int tag_len;
  81. unsigned int len;
  82. int i, j, case_num = 0;
  83. debug_print(mod_cipher, "running self-test for cipher %s",
  84. ct->description);
  85. /*
  86. * check to make sure that we have at least one test case, and
  87. * return an error if we don't - we need to be paranoid here
  88. */
  89. if (test_case == NULL)
  90. return err_status_cant_check;
  91. /*
  92. * loop over all test cases, perform known-answer tests of both the
  93. * encryption and decryption functions
  94. */
  95. while (test_case != NULL) {
  96. /* allocate cipher */
  97. status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->tag_length_octets);
  98. if (status)
  99. return status;
  100. /*
  101. * test the encrypt function
  102. */
  103. debug_print(mod_cipher, "testing encryption", NULL);
  104. /* initialize cipher */
  105. status = cipher_init(c, test_case->key);
  106. if (status) {
  107. cipher_dealloc(c);
  108. return status;
  109. }
  110. /* copy plaintext into test buffer */
  111. if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) {
  112. cipher_dealloc(c);
  113. return err_status_bad_param;
  114. }
  115. for (i=0; i < test_case->plaintext_length_octets; i++)
  116. buffer[i] = test_case->plaintext[i];
  117. debug_print(mod_cipher, "plaintext: %s",
  118. octet_string_hex_string(buffer,
  119. test_case->plaintext_length_octets));
  120. /* set the initialization vector */
  121. status = cipher_set_iv(c, test_case->idx, direction_encrypt);
  122. if (status) {
  123. cipher_dealloc(c);
  124. return status;
  125. }
  126. if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) {
  127. debug_print(mod_cipher, "IV: %s",
  128. octet_string_hex_string(test_case->idx, 12));
  129. /*
  130. * Set the AAD
  131. */
  132. status = cipher_set_aad(c, test_case->aad,
  133. test_case->aad_length_octets);
  134. if (status) {
  135. cipher_dealloc(c);
  136. return status;
  137. }
  138. debug_print(mod_cipher, "AAD: %s",
  139. octet_string_hex_string(test_case->aad,
  140. test_case->aad_length_octets));
  141. }
  142. /* encrypt */
  143. len = test_case->plaintext_length_octets;
  144. status = cipher_encrypt(c, buffer, &len);
  145. if (status) {
  146. cipher_dealloc(c);
  147. return status;
  148. }
  149. if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) {
  150. /*
  151. * Get the GCM tag
  152. */
  153. status = cipher_get_tag(c, buffer + len, &tag_len);
  154. if (status) {
  155. cipher_dealloc(c);
  156. return status;
  157. }
  158. len += tag_len;
  159. }
  160. debug_print(mod_cipher, "ciphertext: %s",
  161. octet_string_hex_string(buffer,
  162. test_case->ciphertext_length_octets));
  163. /* compare the resulting ciphertext with that in the test case */
  164. if (len != (unsigned int)test_case->ciphertext_length_octets)
  165. return err_status_algo_fail;
  166. status = err_status_ok;
  167. for (i=0; i < test_case->ciphertext_length_octets; i++)
  168. if (buffer[i] != test_case->ciphertext[i]) {
  169. status = err_status_algo_fail;
  170. debug_print(mod_cipher, "test case %d failed", case_num);
  171. debug_print(mod_cipher, "(failure at byte %d)", i);
  172. break;
  173. }
  174. if (status) {
  175. debug_print(mod_cipher, "c computed: %s",
  176. octet_string_hex_string(buffer,
  177. 2*test_case->plaintext_length_octets));
  178. debug_print(mod_cipher, "c expected: %s",
  179. octet_string_hex_string(test_case->ciphertext,
  180. 2*test_case->plaintext_length_octets));
  181. cipher_dealloc(c);
  182. return err_status_algo_fail;
  183. }
  184. /*
  185. * test the decrypt function
  186. */
  187. debug_print(mod_cipher, "testing decryption", NULL);
  188. /* re-initialize cipher for decryption */
  189. status = cipher_init(c, test_case->key);
  190. if (status) {
  191. cipher_dealloc(c);
  192. return status;
  193. }
  194. /* copy ciphertext into test buffer */
  195. if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) {
  196. cipher_dealloc(c);
  197. return err_status_bad_param;
  198. }
  199. for (i=0; i < test_case->ciphertext_length_octets; i++)
  200. buffer[i] = test_case->ciphertext[i];
  201. debug_print(mod_cipher, "ciphertext: %s",
  202. octet_string_hex_string(buffer,
  203. test_case->plaintext_length_octets));
  204. /* set the initialization vector */
  205. status = cipher_set_iv(c, test_case->idx, direction_decrypt);
  206. if (status) {
  207. cipher_dealloc(c);
  208. return status;
  209. }
  210. if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) {
  211. /*
  212. * Set the AAD
  213. */
  214. status = cipher_set_aad(c, test_case->aad,
  215. test_case->aad_length_octets);
  216. if (status) {
  217. cipher_dealloc(c);
  218. return status;
  219. }
  220. debug_print(mod_cipher, "AAD: %s",
  221. octet_string_hex_string(test_case->aad,
  222. test_case->aad_length_octets));
  223. }
  224. /* decrypt */
  225. len = test_case->ciphertext_length_octets;
  226. status = cipher_decrypt(c, buffer, &len);
  227. if (status) {
  228. cipher_dealloc(c);
  229. return status;
  230. }
  231. debug_print(mod_cipher, "plaintext: %s",
  232. octet_string_hex_string(buffer,
  233. test_case->plaintext_length_octets));
  234. /* compare the resulting plaintext with that in the test case */
  235. if (len != (unsigned int)test_case->plaintext_length_octets)
  236. return err_status_algo_fail;
  237. status = err_status_ok;
  238. for (i=0; i < test_case->plaintext_length_octets; i++)
  239. if (buffer[i] != test_case->plaintext[i]) {
  240. status = err_status_algo_fail;
  241. debug_print(mod_cipher, "test case %d failed", case_num);
  242. debug_print(mod_cipher, "(failure at byte %d)", i);
  243. }
  244. if (status) {
  245. debug_print(mod_cipher, "p computed: %s",
  246. octet_string_hex_string(buffer,
  247. 2*test_case->plaintext_length_octets));
  248. debug_print(mod_cipher, "p expected: %s",
  249. octet_string_hex_string(test_case->plaintext,
  250. 2*test_case->plaintext_length_octets));
  251. cipher_dealloc(c);
  252. return err_status_algo_fail;
  253. }
  254. /* deallocate the cipher */
  255. status = cipher_dealloc(c);
  256. if (status)
  257. return status;
  258. /*
  259. * the cipher passed the test case, so move on to the next test
  260. * case in the list; if NULL, we'l proceed to the next test
  261. */
  262. test_case = test_case->next_test_case;
  263. ++case_num;
  264. }
  265. /* now run some random invertibility tests */
  266. /* allocate cipher, using paramaters from the first test case */
  267. test_case = test_data;
  268. status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->tag_length_octets);
  269. if (status)
  270. return status;
  271. rand_source_init();
  272. for (j=0; j < NUM_RAND_TESTS; j++) {
  273. unsigned length;
  274. int plaintext_len;
  275. uint8_t key[MAX_KEY_LEN];
  276. uint8_t iv[MAX_KEY_LEN];
  277. /* choose a length at random (leaving room for IV and padding) */
  278. length = rand() % (SELF_TEST_BUF_OCTETS - 64);
  279. debug_print(mod_cipher, "random plaintext length %d\n", length);
  280. status = rand_source_get_octet_string(buffer, length);
  281. if (status) return status;
  282. debug_print(mod_cipher, "plaintext: %s",
  283. octet_string_hex_string(buffer, length));
  284. /* copy plaintext into second buffer */
  285. for (i=0; (unsigned int)i < length; i++)
  286. buffer2[i] = buffer[i];
  287. /* choose a key at random */
  288. if (test_case->key_length_octets > MAX_KEY_LEN)
  289. return err_status_cant_check;
  290. status = rand_source_get_octet_string(key, test_case->key_length_octets);
  291. if (status) return status;
  292. /* chose a random initialization vector */
  293. status = rand_source_get_octet_string(iv, MAX_KEY_LEN);
  294. if (status) return status;
  295. /* initialize cipher */
  296. status = cipher_init(c, key);
  297. if (status) {
  298. cipher_dealloc(c);
  299. return status;
  300. }
  301. /* set initialization vector */
  302. status = cipher_set_iv(c, test_case->idx, direction_encrypt);
  303. if (status) {
  304. cipher_dealloc(c);
  305. return status;
  306. }
  307. if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) {
  308. /*
  309. * Set the AAD
  310. */
  311. status = cipher_set_aad(c, test_case->aad,
  312. test_case->aad_length_octets);
  313. if (status) {
  314. cipher_dealloc(c);
  315. return status;
  316. }
  317. debug_print(mod_cipher, "AAD: %s",
  318. octet_string_hex_string(test_case->aad,
  319. test_case->aad_length_octets));
  320. }
  321. /* encrypt buffer with cipher */
  322. plaintext_len = length;
  323. status = cipher_encrypt(c, buffer, &length);
  324. if (status) {
  325. cipher_dealloc(c);
  326. return status;
  327. }
  328. if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) {
  329. /*
  330. * Get the GCM tag
  331. */
  332. status = cipher_get_tag(c, buffer + length, &tag_len);
  333. if (status) {
  334. cipher_dealloc(c);
  335. return status;
  336. }
  337. length += tag_len;
  338. }
  339. debug_print(mod_cipher, "ciphertext: %s",
  340. octet_string_hex_string(buffer, length));
  341. /*
  342. * re-initialize cipher for decryption, re-set the iv, then
  343. * decrypt the ciphertext
  344. */
  345. status = cipher_init(c, key);
  346. if (status) {
  347. cipher_dealloc(c);
  348. return status;
  349. }
  350. status = cipher_set_iv(c, test_case->idx, direction_decrypt);
  351. if (status) {
  352. cipher_dealloc(c);
  353. return status;
  354. }
  355. if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) {
  356. /*
  357. * Set the AAD
  358. */
  359. status = cipher_set_aad(c, test_case->aad,
  360. test_case->aad_length_octets);
  361. if (status) {
  362. cipher_dealloc(c);
  363. return status;
  364. }
  365. debug_print(mod_cipher, "AAD: %s",
  366. octet_string_hex_string(test_case->aad,
  367. test_case->aad_length_octets));
  368. }
  369. status = cipher_decrypt(c, buffer, &length);
  370. if (status) {
  371. cipher_dealloc(c);
  372. return status;
  373. }
  374. debug_print(mod_cipher, "plaintext[2]: %s",
  375. octet_string_hex_string(buffer, length));
  376. /* compare the resulting plaintext with the original one */
  377. if (length != (unsigned int)plaintext_len) {
  378. return err_status_algo_fail;
  379. }
  380. status = err_status_ok;
  381. for (i=0; i < plaintext_len; i++)
  382. if (buffer[i] != buffer2[i]) {
  383. status = err_status_algo_fail;
  384. debug_print(mod_cipher, "random test case %d failed", case_num);
  385. debug_print(mod_cipher, "(failure at byte %d)", i);
  386. }
  387. if (status) {
  388. cipher_dealloc(c);
  389. return err_status_algo_fail;
  390. }
  391. }
  392. status = cipher_dealloc(c);
  393. if (status)
  394. return status;
  395. return err_status_ok;
  396. }
  397. /*
  398. * cipher_type_self_test(ct) performs cipher_type_test on ct's internal
  399. * list of test data.
  400. */
  401. err_status_t
  402. cipher_type_self_test(const cipher_type_t *ct) {
  403. return cipher_type_test(ct, ct->test_data);
  404. }
  405. /*
  406. * cipher_bits_per_second(c, l, t) computes (an estimate of) the
  407. * number of bits that a cipher implementation can encrypt in a second
  408. *
  409. * c is a cipher (which MUST be allocated and initialized already), l
  410. * is the length in octets of the test data to be encrypted, and t is
  411. * the number of trials
  412. *
  413. * if an error is encountered, the value 0 is returned
  414. */
  415. uint64_t
  416. cipher_bits_per_second(cipher_t *c, int octets_in_buffer, int num_trials) {
  417. int i;
  418. v128_t nonce;
  419. clock_t timer;
  420. unsigned char *enc_buf;
  421. unsigned int len = octets_in_buffer;
  422. enc_buf = (unsigned char*) crypto_alloc(octets_in_buffer);
  423. if (enc_buf == NULL)
  424. return 0; /* indicate bad parameters by returning null */
  425. /* time repeated trials */
  426. v128_set_to_zero(&nonce);
  427. timer = clock();
  428. for(i=0; i < num_trials; i++, nonce.v32[3] = i) {
  429. cipher_set_iv(c, &nonce, direction_encrypt);
  430. cipher_encrypt(c, enc_buf, &len);
  431. }
  432. timer = clock() - timer;
  433. crypto_free(enc_buf);
  434. if (timer == 0) {
  435. /* Too fast! */
  436. return 0;
  437. }
  438. return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer;
  439. }