aes_icm.c 14 KB

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