aes_icm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include "cipher_test_cases.h"
  52. srtp_debug_module_t srtp_mod_aes_icm = {
  53. 0, /* debugging is off by default */
  54. "aes icm" /* printable module name */
  55. };
  56. /*
  57. * integer counter mode works as follows:
  58. *
  59. * 16 bits
  60. * <----->
  61. * +------+------+------+------+------+------+------+------+
  62. * | nonce | pakcet index | ctr |---+
  63. * +------+------+------+------+------+------+------+------+ |
  64. * |
  65. * +------+------+------+------+------+------+------+------+ v
  66. * | salt |000000|->(+)
  67. * +------+------+------+------+------+------+------+------+ |
  68. * |
  69. * +---------+
  70. * | encrypt |
  71. * +---------+
  72. * |
  73. * +------+------+------+------+------+------+------+------+ |
  74. * | keystream block |<--+
  75. * +------+------+------+------+------+------+------+------+
  76. *
  77. * All fields are big-endian
  78. *
  79. * ctr is the block counter, which increments from zero for
  80. * each packet (16 bits wide)
  81. *
  82. * packet index is distinct for each packet (48 bits wide)
  83. *
  84. * nonce can be distinct across many uses of the same key, or
  85. * can be a fixed value per key, or can be per-packet randomness
  86. * (64 bits)
  87. *
  88. */
  89. static srtp_err_status_t srtp_aes_icm_alloc(srtp_cipher_t **c,
  90. int key_len,
  91. int tlen)
  92. {
  93. srtp_aes_icm_ctx_t *icm;
  94. (void)tlen;
  95. debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
  96. key_len);
  97. /*
  98. * The check for key_len = 30/46 does not apply. Our usage
  99. * of aes functions with key_len = values other than 30
  100. * has not broken anything. Don't know what would be the
  101. * effect of skipping this check for srtp in general.
  102. */
  103. if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
  104. key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  105. return srtp_err_status_bad_param;
  106. }
  107. /* allocate memory a cipher of type aes_icm */
  108. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  109. if (*c == NULL) {
  110. return srtp_err_status_alloc_fail;
  111. }
  112. icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
  113. if (icm == NULL) {
  114. srtp_crypto_free(*c);
  115. *c = NULL;
  116. return srtp_err_status_alloc_fail;
  117. }
  118. /* set pointers */
  119. (*c)->state = icm;
  120. switch (key_len) {
  121. case SRTP_AES_ICM_256_KEY_LEN_WSALT:
  122. (*c)->algorithm = SRTP_AES_ICM_256;
  123. (*c)->type = &srtp_aes_icm_256;
  124. break;
  125. default:
  126. (*c)->algorithm = SRTP_AES_ICM_128;
  127. (*c)->type = &srtp_aes_icm_128;
  128. break;
  129. }
  130. /* set key size */
  131. icm->key_size = key_len;
  132. (*c)->key_len = key_len;
  133. return srtp_err_status_ok;
  134. }
  135. static srtp_err_status_t srtp_aes_icm_dealloc(srtp_cipher_t *c)
  136. {
  137. srtp_aes_icm_ctx_t *ctx;
  138. if (c == NULL) {
  139. return srtp_err_status_bad_param;
  140. }
  141. ctx = (srtp_aes_icm_ctx_t *)c->state;
  142. if (ctx) {
  143. /* zeroize the key material */
  144. octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
  145. srtp_crypto_free(ctx);
  146. }
  147. /* free the cipher context */
  148. srtp_crypto_free(c);
  149. return srtp_err_status_ok;
  150. }
  151. /*
  152. * aes_icm_context_init(...) initializes the aes_icm_context
  153. * using the value in key[].
  154. *
  155. * the key is the secret key
  156. *
  157. * the salt is unpredictable (but not necessarily secret) data which
  158. * randomizes the starting point in the keystream
  159. */
  160. static srtp_err_status_t srtp_aes_icm_context_init(void *cv, const uint8_t *key)
  161. {
  162. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  163. srtp_err_status_t status;
  164. int base_key_len, copy_len;
  165. if (c->key_size == SRTP_AES_ICM_128_KEY_LEN_WSALT ||
  166. c->key_size == SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  167. base_key_len = c->key_size - SRTP_SALT_LEN;
  168. } else {
  169. return srtp_err_status_bad_param;
  170. }
  171. /*
  172. * set counter and initial values to 'offset' value, being careful not to
  173. * go past the end of the key buffer
  174. */
  175. v128_set_to_zero(&c->counter);
  176. v128_set_to_zero(&c->offset);
  177. copy_len = c->key_size - base_key_len;
  178. /* force last two octets of the offset to be left zero (for srtp
  179. * compatibility) */
  180. if (copy_len > SRTP_SALT_LEN) {
  181. copy_len = SRTP_SALT_LEN;
  182. }
  183. memcpy(&c->counter, key + base_key_len, copy_len);
  184. memcpy(&c->offset, key + base_key_len, copy_len);
  185. debug_print(srtp_mod_aes_icm, "key: %s",
  186. srtp_octet_string_hex_string(key, base_key_len));
  187. debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
  188. /* expand key */
  189. status =
  190. srtp_aes_expand_encryption_key(key, base_key_len, &c->expanded_key);
  191. if (status) {
  192. v128_set_to_zero(&c->counter);
  193. v128_set_to_zero(&c->offset);
  194. return status;
  195. }
  196. /* indicate that the keystream_buffer is empty */
  197. c->bytes_in_buffer = 0;
  198. return srtp_err_status_ok;
  199. }
  200. /*
  201. * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
  202. * the offset
  203. */
  204. static srtp_err_status_t srtp_aes_icm_set_iv(void *cv,
  205. uint8_t *iv,
  206. srtp_cipher_direction_t direction)
  207. {
  208. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  209. v128_t nonce;
  210. (void)direction;
  211. /* set nonce (for alignment) */
  212. v128_copy_octet_string(&nonce, iv);
  213. debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
  214. v128_xor(&c->counter, &c->offset, &nonce);
  215. debug_print(srtp_mod_aes_icm, "set_counter: %s",
  216. v128_hex_string(&c->counter));
  217. /* indicate that the keystream_buffer is empty */
  218. c->bytes_in_buffer = 0;
  219. return srtp_err_status_ok;
  220. }
  221. /*
  222. * aes_icm_advance(...) refills the keystream_buffer and
  223. * advances the block index of the sicm_context forward by one
  224. *
  225. * this is an internal, hopefully inlined function
  226. */
  227. static void srtp_aes_icm_advance(srtp_aes_icm_ctx_t *c)
  228. {
  229. /* fill buffer with new keystream */
  230. v128_copy(&c->keystream_buffer, &c->counter);
  231. srtp_aes_encrypt(&c->keystream_buffer, &c->expanded_key);
  232. c->bytes_in_buffer = sizeof(v128_t);
  233. debug_print(srtp_mod_aes_icm, "counter: %s",
  234. v128_hex_string(&c->counter));
  235. debug_print(srtp_mod_aes_icm, "ciphertext: %s",
  236. v128_hex_string(&c->keystream_buffer));
  237. /* clock counter forward */
  238. if (!++(c->counter.v8[15])) {
  239. ++(c->counter.v8[14]);
  240. }
  241. }
  242. /*
  243. * icm_encrypt deals with the following cases:
  244. *
  245. * bytes_to_encr < bytes_in_buffer
  246. * - add keystream into data
  247. *
  248. * bytes_to_encr > bytes_in_buffer
  249. * - add keystream into data until keystream_buffer is depleted
  250. * - loop over blocks, filling keystream_buffer and then
  251. * adding keystream into data
  252. * - fill buffer then add in remaining (< 16) bytes of keystream
  253. */
  254. static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
  255. unsigned char *buf,
  256. unsigned int *enc_len)
  257. {
  258. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  259. unsigned int bytes_to_encr = *enc_len;
  260. unsigned int i;
  261. uint32_t *b;
  262. /* check that there's enough segment left*/
  263. unsigned int bytes_of_new_keystream = bytes_to_encr - c->bytes_in_buffer;
  264. unsigned int blocks_of_new_keystream = (bytes_of_new_keystream + 15) >> 4;
  265. if ((blocks_of_new_keystream + htons(c->counter.v16[7])) > 0xffff) {
  266. return srtp_err_status_terminus;
  267. }
  268. debug_print(srtp_mod_aes_icm, "block index: %d", htons(c->counter.v16[7]));
  269. if (bytes_to_encr <= (unsigned int)c->bytes_in_buffer) {
  270. /* deal with odd case of small bytes_to_encr */
  271. for (i = (sizeof(v128_t) - c->bytes_in_buffer);
  272. i < (sizeof(v128_t) - c->bytes_in_buffer + bytes_to_encr); i++) {
  273. *buf++ ^= c->keystream_buffer.v8[i];
  274. }
  275. c->bytes_in_buffer -= bytes_to_encr;
  276. /* return now to avoid the main loop */
  277. return srtp_err_status_ok;
  278. } else {
  279. /* encrypt bytes until the remaining data is 16-byte aligned */
  280. for (i = (sizeof(v128_t) - c->bytes_in_buffer); i < sizeof(v128_t);
  281. i++) {
  282. *buf++ ^= c->keystream_buffer.v8[i];
  283. }
  284. bytes_to_encr -= c->bytes_in_buffer;
  285. c->bytes_in_buffer = 0;
  286. }
  287. /* now loop over entire 16-byte blocks of keystream */
  288. for (i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
  289. /* fill buffer with new keystream */
  290. srtp_aes_icm_advance(c);
  291. /*
  292. * add keystream into the data buffer (this would be a lot faster
  293. * if we could assume 32-bit alignment!)
  294. */
  295. #if ALIGN_32
  296. b = (uint32_t *)buf;
  297. *b++ ^= c->keystream_buffer.v32[0];
  298. *b++ ^= c->keystream_buffer.v32[1];
  299. *b++ ^= c->keystream_buffer.v32[2];
  300. *b++ ^= c->keystream_buffer.v32[3];
  301. buf = (uint8_t *)b;
  302. #else
  303. if ((((uintptr_t)buf) & 0x03) != 0) {
  304. *buf++ ^= c->keystream_buffer.v8[0];
  305. *buf++ ^= c->keystream_buffer.v8[1];
  306. *buf++ ^= c->keystream_buffer.v8[2];
  307. *buf++ ^= c->keystream_buffer.v8[3];
  308. *buf++ ^= c->keystream_buffer.v8[4];
  309. *buf++ ^= c->keystream_buffer.v8[5];
  310. *buf++ ^= c->keystream_buffer.v8[6];
  311. *buf++ ^= c->keystream_buffer.v8[7];
  312. *buf++ ^= c->keystream_buffer.v8[8];
  313. *buf++ ^= c->keystream_buffer.v8[9];
  314. *buf++ ^= c->keystream_buffer.v8[10];
  315. *buf++ ^= c->keystream_buffer.v8[11];
  316. *buf++ ^= c->keystream_buffer.v8[12];
  317. *buf++ ^= c->keystream_buffer.v8[13];
  318. *buf++ ^= c->keystream_buffer.v8[14];
  319. *buf++ ^= c->keystream_buffer.v8[15];
  320. } else {
  321. b = (uint32_t *)buf;
  322. *b++ ^= c->keystream_buffer.v32[0];
  323. *b++ ^= c->keystream_buffer.v32[1];
  324. *b++ ^= c->keystream_buffer.v32[2];
  325. *b++ ^= c->keystream_buffer.v32[3];
  326. buf = (uint8_t *)b;
  327. }
  328. #endif /* #if ALIGN_32 */
  329. }
  330. /* if there is a tail end of the data, process it */
  331. if ((bytes_to_encr & 0xf) != 0) {
  332. /* fill buffer with new keystream */
  333. srtp_aes_icm_advance(c);
  334. for (i = 0; i < (bytes_to_encr & 0xf); i++) {
  335. *buf++ ^= c->keystream_buffer.v8[i];
  336. }
  337. /* reset the keystream buffer size to right value */
  338. c->bytes_in_buffer = sizeof(v128_t) - i;
  339. } else {
  340. /* no tail, so just reset the keystream buffer size to zero */
  341. c->bytes_in_buffer = 0;
  342. }
  343. return srtp_err_status_ok;
  344. }
  345. static const char srtp_aes_icm_128_description[] =
  346. "AES-128 integer counter mode";
  347. static const char srtp_aes_icm_256_description[] =
  348. "AES-256 integer counter mode";
  349. /*
  350. * note: the encrypt function is identical to the decrypt function
  351. */
  352. const srtp_cipher_type_t srtp_aes_icm_128 = {
  353. srtp_aes_icm_alloc, /* */
  354. srtp_aes_icm_dealloc, /* */
  355. srtp_aes_icm_context_init, /* */
  356. 0, /* set_aad */
  357. srtp_aes_icm_encrypt, /* */
  358. srtp_aes_icm_encrypt, /* */
  359. srtp_aes_icm_set_iv, /* */
  360. 0, /* get_tag */
  361. srtp_aes_icm_128_description, /* */
  362. &srtp_aes_icm_128_test_case_0, /* */
  363. SRTP_AES_ICM_128 /* */
  364. };
  365. const srtp_cipher_type_t srtp_aes_icm_256 = {
  366. srtp_aes_icm_alloc, /* */
  367. srtp_aes_icm_dealloc, /* */
  368. srtp_aes_icm_context_init, /* */
  369. 0, /* set_aad */
  370. srtp_aes_icm_encrypt, /* */
  371. srtp_aes_icm_encrypt, /* */
  372. srtp_aes_icm_set_iv, /* */
  373. 0, /* get_tag */
  374. srtp_aes_icm_256_description, /* */
  375. &srtp_aes_icm_256_test_case_0, /* */
  376. SRTP_AES_ICM_256 /* */
  377. };