aes_icm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * aes_icm.c
  3. *
  4. * AES Integer Counter Mode
  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. #define ALIGN_32 0
  48. #include "aes_icm.h"
  49. #include "alloc.h"
  50. #include "cipher_types.h"
  51. srtp_debug_module_t srtp_mod_aes_icm = {
  52. 0, /* debugging is off by default */
  53. "aes icm" /* printable module name */
  54. };
  55. /*
  56. * integer counter mode works as follows:
  57. *
  58. * 16 bits
  59. * <----->
  60. * +------+------+------+------+------+------+------+------+
  61. * | nonce | pakcet index | ctr |---+
  62. * +------+------+------+------+------+------+------+------+ |
  63. * |
  64. * +------+------+------+------+------+------+------+------+ v
  65. * | salt |000000|->(+)
  66. * +------+------+------+------+------+------+------+------+ |
  67. * |
  68. * +---------+
  69. * | encrypt |
  70. * +---------+
  71. * |
  72. * +------+------+------+------+------+------+------+------+ |
  73. * | keystream block |<--+
  74. * +------+------+------+------+------+------+------+------+
  75. *
  76. * All fields are big-endian
  77. *
  78. * ctr is the block counter, which increments from zero for
  79. * each packet (16 bits wide)
  80. *
  81. * packet index is distinct for each packet (48 bits wide)
  82. *
  83. * nonce can be distinct across many uses of the same key, or
  84. * can be a fixed value per key, or can be per-packet randomness
  85. * (64 bits)
  86. *
  87. */
  88. static srtp_err_status_t srtp_aes_icm_alloc(srtp_cipher_t **c,
  89. int key_len,
  90. int tlen)
  91. {
  92. srtp_aes_icm_ctx_t *icm;
  93. debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
  94. key_len);
  95. /*
  96. * The check for key_len = 30/46 does not apply. Our usage
  97. * of aes functions with key_len = values other than 30
  98. * has not broken anything. Don't know what would be the
  99. * effect of skipping this check for srtp in general.
  100. */
  101. if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
  102. key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  103. return srtp_err_status_bad_param;
  104. }
  105. /* allocate memory a cipher of type aes_icm */
  106. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  107. if (*c == NULL) {
  108. return srtp_err_status_alloc_fail;
  109. }
  110. icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
  111. if (icm == NULL) {
  112. srtp_crypto_free(*c);
  113. *c = NULL;
  114. return srtp_err_status_alloc_fail;
  115. }
  116. /* set pointers */
  117. (*c)->state = icm;
  118. switch (key_len) {
  119. case SRTP_AES_ICM_256_KEY_LEN_WSALT:
  120. (*c)->algorithm = SRTP_AES_ICM_256;
  121. (*c)->type = &srtp_aes_icm_256;
  122. break;
  123. default:
  124. (*c)->algorithm = SRTP_AES_ICM_128;
  125. (*c)->type = &srtp_aes_icm_128;
  126. break;
  127. }
  128. /* set key size */
  129. icm->key_size = key_len;
  130. (*c)->key_len = key_len;
  131. return srtp_err_status_ok;
  132. }
  133. static srtp_err_status_t srtp_aes_icm_dealloc(srtp_cipher_t *c)
  134. {
  135. srtp_aes_icm_ctx_t *ctx;
  136. if (c == NULL) {
  137. return srtp_err_status_bad_param;
  138. }
  139. ctx = (srtp_aes_icm_ctx_t *)c->state;
  140. if (ctx) {
  141. /* zeroize the key material */
  142. octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
  143. srtp_crypto_free(ctx);
  144. }
  145. /* free the cipher context */
  146. srtp_crypto_free(c);
  147. return srtp_err_status_ok;
  148. }
  149. /*
  150. * aes_icm_context_init(...) initializes the aes_icm_context
  151. * using the value in key[].
  152. *
  153. * the key is the secret key
  154. *
  155. * the salt is unpredictable (but not necessarily secret) data which
  156. * randomizes the starting point in the keystream
  157. */
  158. static srtp_err_status_t srtp_aes_icm_context_init(void *cv, const uint8_t *key)
  159. {
  160. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  161. srtp_err_status_t status;
  162. int base_key_len, copy_len;
  163. if (c->key_size == SRTP_AES_ICM_128_KEY_LEN_WSALT ||
  164. c->key_size == SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  165. base_key_len = c->key_size - SRTP_SALT_LEN;
  166. } else {
  167. return srtp_err_status_bad_param;
  168. }
  169. /*
  170. * set counter and initial values to 'offset' value, being careful not to
  171. * go past the end of the key buffer
  172. */
  173. v128_set_to_zero(&c->counter);
  174. v128_set_to_zero(&c->offset);
  175. copy_len = c->key_size - base_key_len;
  176. /* force last two octets of the offset to be left zero (for srtp
  177. * compatibility) */
  178. if (copy_len > SRTP_SALT_LEN) {
  179. copy_len = SRTP_SALT_LEN;
  180. }
  181. memcpy(&c->counter, key + base_key_len, copy_len);
  182. memcpy(&c->offset, key + base_key_len, copy_len);
  183. debug_print(srtp_mod_aes_icm, "key: %s",
  184. srtp_octet_string_hex_string(key, base_key_len));
  185. debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
  186. /* expand key */
  187. status =
  188. srtp_aes_expand_encryption_key(key, base_key_len, &c->expanded_key);
  189. if (status) {
  190. v128_set_to_zero(&c->counter);
  191. v128_set_to_zero(&c->offset);
  192. return status;
  193. }
  194. /* indicate that the keystream_buffer is empty */
  195. c->bytes_in_buffer = 0;
  196. return srtp_err_status_ok;
  197. }
  198. /*
  199. * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
  200. * the offset
  201. */
  202. static srtp_err_status_t srtp_aes_icm_set_iv(void *cv,
  203. uint8_t *iv,
  204. srtp_cipher_direction_t direction)
  205. {
  206. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  207. v128_t nonce;
  208. /* set nonce (for alignment) */
  209. v128_copy_octet_string(&nonce, iv);
  210. debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
  211. v128_xor(&c->counter, &c->offset, &nonce);
  212. debug_print(srtp_mod_aes_icm, "set_counter: %s",
  213. v128_hex_string(&c->counter));
  214. /* indicate that the keystream_buffer is empty */
  215. c->bytes_in_buffer = 0;
  216. return srtp_err_status_ok;
  217. }
  218. /*
  219. * aes_icm_advance(...) refills the keystream_buffer and
  220. * advances the block index of the sicm_context forward by one
  221. *
  222. * this is an internal, hopefully inlined function
  223. */
  224. static void srtp_aes_icm_advance(srtp_aes_icm_ctx_t *c)
  225. {
  226. /* fill buffer with new keystream */
  227. v128_copy(&c->keystream_buffer, &c->counter);
  228. srtp_aes_encrypt(&c->keystream_buffer, &c->expanded_key);
  229. c->bytes_in_buffer = sizeof(v128_t);
  230. debug_print(srtp_mod_aes_icm, "counter: %s",
  231. v128_hex_string(&c->counter));
  232. debug_print(srtp_mod_aes_icm, "ciphertext: %s",
  233. v128_hex_string(&c->keystream_buffer));
  234. /* clock counter forward */
  235. if (!++(c->counter.v8[15])) {
  236. ++(c->counter.v8[14]);
  237. }
  238. }
  239. /*
  240. * icm_encrypt deals with the following cases:
  241. *
  242. * bytes_to_encr < bytes_in_buffer
  243. * - add keystream into data
  244. *
  245. * bytes_to_encr > bytes_in_buffer
  246. * - add keystream into data until keystream_buffer is depleted
  247. * - loop over blocks, filling keystream_buffer and then
  248. * adding keystream into data
  249. * - fill buffer then add in remaining (< 16) bytes of keystream
  250. */
  251. static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
  252. unsigned char *buf,
  253. unsigned int *enc_len)
  254. {
  255. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  256. unsigned int bytes_to_encr = *enc_len;
  257. unsigned int i;
  258. uint32_t *b;
  259. /* check that there's enough segment left*/
  260. if ((bytes_to_encr + htons(c->counter.v16[7])) > 0xffff) {
  261. return srtp_err_status_terminus;
  262. }
  263. debug_print(srtp_mod_aes_icm, "block index: %d", htons(c->counter.v16[7]));
  264. if (bytes_to_encr <= (unsigned int)c->bytes_in_buffer) {
  265. /* deal with odd case of small bytes_to_encr */
  266. for (i = (sizeof(v128_t) - c->bytes_in_buffer);
  267. i < (sizeof(v128_t) - c->bytes_in_buffer + bytes_to_encr); i++) {
  268. *buf++ ^= c->keystream_buffer.v8[i];
  269. }
  270. c->bytes_in_buffer -= bytes_to_encr;
  271. /* return now to avoid the main loop */
  272. return srtp_err_status_ok;
  273. } else {
  274. /* encrypt bytes until the remaining data is 16-byte aligned */
  275. for (i = (sizeof(v128_t) - c->bytes_in_buffer); i < sizeof(v128_t);
  276. i++) {
  277. *buf++ ^= c->keystream_buffer.v8[i];
  278. }
  279. bytes_to_encr -= c->bytes_in_buffer;
  280. c->bytes_in_buffer = 0;
  281. }
  282. /* now loop over entire 16-byte blocks of keystream */
  283. for (i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
  284. /* fill buffer with new keystream */
  285. srtp_aes_icm_advance(c);
  286. /*
  287. * add keystream into the data buffer (this would be a lot faster
  288. * if we could assume 32-bit alignment!)
  289. */
  290. #if ALIGN_32
  291. b = (uint32_t *)buf;
  292. *b++ ^= c->keystream_buffer.v32[0];
  293. *b++ ^= c->keystream_buffer.v32[1];
  294. *b++ ^= c->keystream_buffer.v32[2];
  295. *b++ ^= c->keystream_buffer.v32[3];
  296. buf = (uint8_t *)b;
  297. #else
  298. if ((((uintptr_t)buf) & 0x03) != 0) {
  299. *buf++ ^= c->keystream_buffer.v8[0];
  300. *buf++ ^= c->keystream_buffer.v8[1];
  301. *buf++ ^= c->keystream_buffer.v8[2];
  302. *buf++ ^= c->keystream_buffer.v8[3];
  303. *buf++ ^= c->keystream_buffer.v8[4];
  304. *buf++ ^= c->keystream_buffer.v8[5];
  305. *buf++ ^= c->keystream_buffer.v8[6];
  306. *buf++ ^= c->keystream_buffer.v8[7];
  307. *buf++ ^= c->keystream_buffer.v8[8];
  308. *buf++ ^= c->keystream_buffer.v8[9];
  309. *buf++ ^= c->keystream_buffer.v8[10];
  310. *buf++ ^= c->keystream_buffer.v8[11];
  311. *buf++ ^= c->keystream_buffer.v8[12];
  312. *buf++ ^= c->keystream_buffer.v8[13];
  313. *buf++ ^= c->keystream_buffer.v8[14];
  314. *buf++ ^= c->keystream_buffer.v8[15];
  315. } else {
  316. b = (uint32_t *)buf;
  317. *b++ ^= c->keystream_buffer.v32[0];
  318. *b++ ^= c->keystream_buffer.v32[1];
  319. *b++ ^= c->keystream_buffer.v32[2];
  320. *b++ ^= c->keystream_buffer.v32[3];
  321. buf = (uint8_t *)b;
  322. }
  323. #endif /* #if ALIGN_32 */
  324. }
  325. /* if there is a tail end of the data, process it */
  326. if ((bytes_to_encr & 0xf) != 0) {
  327. /* fill buffer with new keystream */
  328. srtp_aes_icm_advance(c);
  329. for (i = 0; i < (bytes_to_encr & 0xf); i++) {
  330. *buf++ ^= c->keystream_buffer.v8[i];
  331. }
  332. /* reset the keystream buffer size to right value */
  333. c->bytes_in_buffer = sizeof(v128_t) - i;
  334. } else {
  335. /* no tail, so just reset the keystream buffer size to zero */
  336. c->bytes_in_buffer = 0;
  337. }
  338. return srtp_err_status_ok;
  339. }
  340. static const char srtp_aes_icm_128_description[] =
  341. "AES-128 integer counter mode";
  342. static const char srtp_aes_icm_256_description[] =
  343. "AES-256 integer counter mode";
  344. /* clang-format off */
  345. static const uint8_t srtp_aes_icm_128_test_case_0_key[SRTP_AES_ICM_128_KEY_LEN_WSALT] = {
  346. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  347. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
  348. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  349. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
  350. };
  351. /* clang-format on */
  352. /* clang-format off */
  353. static uint8_t srtp_aes_icm_128_test_case_0_nonce[16] = {
  354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  355. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  356. };
  357. /* clang-format on */
  358. /* clang-format off */
  359. static const uint8_t srtp_aes_icm_128_test_case_0_plaintext[32] = {
  360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  364. };
  365. /* clang-format on */
  366. /* clang-format off */
  367. static const uint8_t srtp_aes_icm_128_test_case_0_ciphertext[32] = {
  368. 0xe0, 0x3e, 0xad, 0x09, 0x35, 0xc9, 0x5e, 0x80,
  369. 0xe1, 0x66, 0xb1, 0x6d, 0xd9, 0x2b, 0x4e, 0xb4,
  370. 0xd2, 0x35, 0x13, 0x16, 0x2b, 0x02, 0xd0, 0xf7,
  371. 0x2a, 0x43, 0xa2, 0xfe, 0x4a, 0x5f, 0x97, 0xab
  372. };
  373. /* clang-format on */
  374. static const srtp_cipher_test_case_t srtp_aes_icm_128_test_case_0 = {
  375. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* octets in key */
  376. srtp_aes_icm_128_test_case_0_key, /* key */
  377. srtp_aes_icm_128_test_case_0_nonce, /* packet index */
  378. 32, /* octets in plaintext */
  379. srtp_aes_icm_128_test_case_0_plaintext, /* plaintext */
  380. 32, /* octets in ciphertext */
  381. srtp_aes_icm_128_test_case_0_ciphertext, /* ciphertext */
  382. 0, /* */
  383. NULL, /* */
  384. 0, /* */
  385. NULL /* pointer to next testcase */
  386. };
  387. /* clang-format off */
  388. static const uint8_t srtp_aes_icm_256_test_case_0_key[SRTP_AES_ICM_256_KEY_LEN_WSALT] = {
  389. 0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70,
  390. 0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92,
  391. 0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82,
  392. 0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98,
  393. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  394. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
  395. };
  396. /* clang-format on */
  397. /* clang-format off */
  398. static uint8_t srtp_aes_icm_256_test_case_0_nonce[16] = {
  399. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  400. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  401. };
  402. /* clang-format on */
  403. /* clang-format off */
  404. static const uint8_t srtp_aes_icm_256_test_case_0_plaintext[32] = {
  405. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  406. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  407. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  408. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  409. };
  410. /* clang-format on */
  411. /* clang-format off */
  412. static const uint8_t srtp_aes_icm_256_test_case_0_ciphertext[32] = {
  413. 0x92, 0xbd, 0xd2, 0x8a, 0x93, 0xc3, 0xf5, 0x25,
  414. 0x11, 0xc6, 0x77, 0xd0, 0x8b, 0x55, 0x15, 0xa4,
  415. 0x9d, 0xa7, 0x1b, 0x23, 0x78, 0xa8, 0x54, 0xf6,
  416. 0x70, 0x50, 0x75, 0x6d, 0xed, 0x16, 0x5b, 0xac
  417. };
  418. /* clang-format on */
  419. static const srtp_cipher_test_case_t srtp_aes_icm_256_test_case_0 = {
  420. SRTP_AES_ICM_256_KEY_LEN_WSALT, /* octets in key */
  421. srtp_aes_icm_256_test_case_0_key, /* key */
  422. srtp_aes_icm_256_test_case_0_nonce, /* packet index */
  423. 32, /* octets in plaintext */
  424. srtp_aes_icm_256_test_case_0_plaintext, /* plaintext */
  425. 32, /* octets in ciphertext */
  426. srtp_aes_icm_256_test_case_0_ciphertext, /* ciphertext */
  427. 0, /* */
  428. NULL, /* */
  429. 0, /* */
  430. NULL, /* pointer to next testcase */
  431. };
  432. /*
  433. * note: the encrypt function is identical to the decrypt function
  434. */
  435. const srtp_cipher_type_t srtp_aes_icm_128 = {
  436. srtp_aes_icm_alloc, /* */
  437. srtp_aes_icm_dealloc, /* */
  438. srtp_aes_icm_context_init, /* */
  439. 0, /* set_aad */
  440. srtp_aes_icm_encrypt, /* */
  441. srtp_aes_icm_encrypt, /* */
  442. srtp_aes_icm_set_iv, /* */
  443. 0, /* get_tag */
  444. srtp_aes_icm_128_description, /* */
  445. &srtp_aes_icm_128_test_case_0, /* */
  446. SRTP_AES_ICM_128 /* */
  447. };
  448. const srtp_cipher_type_t srtp_aes_icm_256 = {
  449. srtp_aes_icm_alloc, /* */
  450. srtp_aes_icm_dealloc, /* */
  451. srtp_aes_icm_context_init, /* */
  452. 0, /* set_aad */
  453. srtp_aes_icm_encrypt, /* */
  454. srtp_aes_icm_encrypt, /* */
  455. srtp_aes_icm_set_iv, /* */
  456. 0, /* get_tag */
  457. srtp_aes_icm_256_description, /* */
  458. &srtp_aes_icm_256_test_case_0, /* */
  459. SRTP_AES_ICM_256 /* */
  460. };