aes_icm_mbedtls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * aes_icm_mbedtls.c
  3. *
  4. * AES Integer Counter Mode
  5. *
  6. * YongCheng Yang
  7. */
  8. /*
  9. *
  10. * Copyright (c) 2013-2017, Cisco Systems, Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * Neither the name of the Cisco Systems, Inc. nor the names of its
  26. * contributors may be used to endorse or promote products derived
  27. * from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  34. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  40. * OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. */
  43. #ifdef HAVE_CONFIG_H
  44. #include <config.h>
  45. #endif
  46. #include <mbedtls/aes.h>
  47. #include "aes_icm_ext.h"
  48. #include "crypto_types.h"
  49. #include "err.h" /* for srtp_debug */
  50. #include "alloc.h"
  51. #include "cipher_types.h"
  52. #include "cipher_test_cases.h"
  53. srtp_debug_module_t srtp_mod_aes_icm = {
  54. 0, /* debugging is off by default */
  55. "aes icm mbedtls" /* printable module name */
  56. };
  57. /*
  58. * integer counter mode works as follows:
  59. *
  60. * https://tools.ietf.org/html/rfc3711#section-4.1.1
  61. *
  62. * E(k, IV) || E(k, IV + 1 mod 2^128) || E(k, IV + 2 mod 2^128) ...
  63. * IV = (k_s * 2^16) XOR (SSRC * 2^64) XOR (i * 2^16)
  64. *
  65. * IV SHALL be defined by the SSRC, the SRTP packet index i,
  66. * and the SRTP session salting key k_s.
  67. *
  68. * SSRC: 32bits.
  69. * Sequence number: 16bits.
  70. * nonce is 64bits. .
  71. * packet index = ROC || SEQ. (ROC: Rollover counter)
  72. *
  73. * 16 bits
  74. * <----->
  75. * +------+------+------+------+------+------+------+------+
  76. * | nonce | packet index | ctr |---+
  77. * +------+------+------+------+------+------+------+------+ |
  78. * |
  79. * +------+------+------+------+------+------+------+------+ v
  80. * | salt |000000|->(+)
  81. * +------+------+------+------+------+------+------+------+ |
  82. * |
  83. * +---------+
  84. * | encrypt |
  85. * +---------+
  86. * |
  87. * +------+------+------+------+------+------+------+------+ |
  88. * | keystream block |<--+
  89. * +------+------+------+------+------+------+------+------+
  90. *
  91. * All fields are big-endian
  92. *
  93. * ctr is the block counter, which increments from zero for
  94. * each packet (16 bits wide)
  95. *
  96. * packet index is distinct for each packet (48 bits wide)
  97. *
  98. * nonce can be distinct across many uses of the same key, or
  99. * can be a fixed value per key, or can be per-packet randomness
  100. * (64 bits)
  101. *
  102. */
  103. /*
  104. * This function allocates a new instance of this crypto engine.
  105. * The key_len parameter should be one of 30, 38, or 46 for
  106. * AES-128, AES-192, and AES-256 respectively. Note, this key_len
  107. * value is inflated, as it also accounts for the 112 bit salt
  108. * value. The tlen argument is for the AEAD tag length, which
  109. * isn't used in counter mode.
  110. */
  111. static srtp_err_status_t srtp_aes_icm_mbedtls_alloc(srtp_cipher_t **c,
  112. int key_len,
  113. int tlen)
  114. {
  115. srtp_aes_icm_ctx_t *icm;
  116. (void)tlen;
  117. debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
  118. key_len);
  119. /*
  120. * Verify the key_len is valid for one of: AES-128/192/256
  121. */
  122. if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
  123. key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
  124. key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  125. return srtp_err_status_bad_param;
  126. }
  127. /* allocate memory a cipher of type aes_icm */
  128. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  129. if (*c == NULL) {
  130. return srtp_err_status_alloc_fail;
  131. }
  132. icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
  133. if (icm == NULL) {
  134. srtp_crypto_free(*c);
  135. *c = NULL;
  136. return srtp_err_status_alloc_fail;
  137. }
  138. icm->ctx =
  139. (mbedtls_aes_context *)srtp_crypto_alloc(sizeof(mbedtls_aes_context));
  140. if (icm->ctx == NULL) {
  141. srtp_crypto_free(icm);
  142. srtp_crypto_free(*c);
  143. *c = NULL;
  144. return srtp_err_status_alloc_fail;
  145. }
  146. mbedtls_aes_init(icm->ctx);
  147. /* set pointers */
  148. (*c)->state = icm;
  149. /* setup cipher parameters */
  150. switch (key_len) {
  151. case SRTP_AES_ICM_128_KEY_LEN_WSALT:
  152. (*c)->algorithm = SRTP_AES_ICM_128;
  153. (*c)->type = &srtp_aes_icm_128;
  154. icm->key_size = SRTP_AES_128_KEY_LEN;
  155. break;
  156. case SRTP_AES_ICM_192_KEY_LEN_WSALT:
  157. (*c)->algorithm = SRTP_AES_ICM_192;
  158. (*c)->type = &srtp_aes_icm_192;
  159. icm->key_size = SRTP_AES_192_KEY_LEN;
  160. break;
  161. case SRTP_AES_ICM_256_KEY_LEN_WSALT:
  162. (*c)->algorithm = SRTP_AES_ICM_256;
  163. (*c)->type = &srtp_aes_icm_256;
  164. icm->key_size = SRTP_AES_256_KEY_LEN;
  165. break;
  166. }
  167. /* set key size */
  168. (*c)->key_len = key_len;
  169. return srtp_err_status_ok;
  170. }
  171. /*
  172. * This function deallocates an instance of this engine
  173. */
  174. static srtp_err_status_t srtp_aes_icm_mbedtls_dealloc(srtp_cipher_t *c)
  175. {
  176. srtp_aes_icm_ctx_t *ctx;
  177. if (c == NULL) {
  178. return srtp_err_status_bad_param;
  179. }
  180. /*
  181. * Free the aes context
  182. */
  183. ctx = (srtp_aes_icm_ctx_t *)c->state;
  184. if (ctx != NULL) {
  185. mbedtls_aes_free(ctx->ctx);
  186. srtp_crypto_free(ctx->ctx);
  187. /* zeroize the key material */
  188. octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
  189. srtp_crypto_free(ctx);
  190. }
  191. /* free memory */
  192. srtp_crypto_free(c);
  193. return srtp_err_status_ok;
  194. }
  195. static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
  196. const uint8_t *key)
  197. {
  198. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  199. uint32_t key_size_in_bits = (c->key_size << 3);
  200. int errcode = 0;
  201. /*
  202. * set counter and initial values to 'offset' value, being careful not to
  203. * go past the end of the key buffer
  204. */
  205. v128_set_to_zero(&c->counter);
  206. v128_set_to_zero(&c->offset);
  207. memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
  208. memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
  209. /* force last two octets of the offset to zero (for srtp compatibility) */
  210. c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
  211. c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
  212. debug_print(srtp_mod_aes_icm, "key: %s",
  213. srtp_octet_string_hex_string(key, c->key_size));
  214. debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
  215. switch (c->key_size) {
  216. case SRTP_AES_256_KEY_LEN:
  217. case SRTP_AES_192_KEY_LEN:
  218. case SRTP_AES_128_KEY_LEN:
  219. break;
  220. default:
  221. return srtp_err_status_bad_param;
  222. break;
  223. }
  224. errcode = mbedtls_aes_setkey_enc(c->ctx, key, key_size_in_bits);
  225. if (errcode != 0) {
  226. debug_print(srtp_mod_aes_icm, "errCode: %d", errcode);
  227. }
  228. return srtp_err_status_ok;
  229. }
  230. /*
  231. * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
  232. * the offset
  233. */
  234. static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
  235. void *cv,
  236. uint8_t *iv,
  237. srtp_cipher_direction_t dir)
  238. {
  239. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  240. v128_t nonce;
  241. (void)dir;
  242. c->nc_off = 0;
  243. /* set nonce (for alignment) */
  244. v128_copy_octet_string(&nonce, iv);
  245. debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
  246. v128_xor(&c->counter, &c->offset, &nonce);
  247. debug_print(srtp_mod_aes_icm, "set_counter: %s",
  248. v128_hex_string(&c->counter));
  249. return srtp_err_status_ok;
  250. }
  251. /*
  252. * This function encrypts a buffer using AES CTR mode
  253. *
  254. * Parameters:
  255. * c Crypto context
  256. * buf data to encrypt
  257. * enc_len length of encrypt buffer
  258. */
  259. static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
  260. unsigned char *buf,
  261. unsigned int *enc_len)
  262. {
  263. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  264. int errCode = 0;
  265. debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
  266. errCode =
  267. mbedtls_aes_crypt_ctr(c->ctx, *enc_len, &(c->nc_off), c->counter.v8,
  268. c->stream_block.v8, buf, buf);
  269. if (errCode != 0) {
  270. debug_print(srtp_mod_aes_icm, "encrypt error: %d", errCode);
  271. return srtp_err_status_cipher_fail;
  272. }
  273. return srtp_err_status_ok;
  274. }
  275. /*
  276. * Name of this crypto engine
  277. */
  278. static const char srtp_aes_icm_128_mbedtls_description[] =
  279. "AES-128 counter mode using mbedtls";
  280. static const char srtp_aes_icm_192_mbedtls_description[] =
  281. "AES-192 counter mode using mbedtls";
  282. static const char srtp_aes_icm_256_mbedtls_description[] =
  283. "AES-256 counter mode using mbedtls";
  284. /*
  285. * This is the function table for this crypto engine.
  286. * note: the encrypt function is identical to the decrypt function
  287. */
  288. const srtp_cipher_type_t srtp_aes_icm_128 = {
  289. srtp_aes_icm_mbedtls_alloc, /* */
  290. srtp_aes_icm_mbedtls_dealloc, /* */
  291. srtp_aes_icm_mbedtls_context_init, /* */
  292. 0, /* set_aad */
  293. srtp_aes_icm_mbedtls_encrypt, /* */
  294. srtp_aes_icm_mbedtls_encrypt, /* */
  295. srtp_aes_icm_mbedtls_set_iv, /* */
  296. 0, /* get_tag */
  297. srtp_aes_icm_128_mbedtls_description, /* */
  298. &srtp_aes_icm_128_test_case_0, /* */
  299. SRTP_AES_ICM_128 /* */
  300. };
  301. /*
  302. * This is the function table for this crypto engine.
  303. * note: the encrypt function is identical to the decrypt function
  304. */
  305. const srtp_cipher_type_t srtp_aes_icm_192 = {
  306. srtp_aes_icm_mbedtls_alloc, /* */
  307. srtp_aes_icm_mbedtls_dealloc, /* */
  308. srtp_aes_icm_mbedtls_context_init, /* */
  309. 0, /* set_aad */
  310. srtp_aes_icm_mbedtls_encrypt, /* */
  311. srtp_aes_icm_mbedtls_encrypt, /* */
  312. srtp_aes_icm_mbedtls_set_iv, /* */
  313. 0, /* get_tag */
  314. srtp_aes_icm_192_mbedtls_description, /* */
  315. &srtp_aes_icm_192_test_case_0, /* */
  316. SRTP_AES_ICM_192 /* */
  317. };
  318. /*
  319. * This is the function table for this crypto engine.
  320. * note: the encrypt function is identical to the decrypt function
  321. */
  322. const srtp_cipher_type_t srtp_aes_icm_256 = {
  323. srtp_aes_icm_mbedtls_alloc, /* */
  324. srtp_aes_icm_mbedtls_dealloc, /* */
  325. srtp_aes_icm_mbedtls_context_init, /* */
  326. 0, /* set_aad */
  327. srtp_aes_icm_mbedtls_encrypt, /* */
  328. srtp_aes_icm_mbedtls_encrypt, /* */
  329. srtp_aes_icm_mbedtls_set_iv, /* */
  330. 0, /* get_tag */
  331. srtp_aes_icm_256_mbedtls_description, /* */
  332. &srtp_aes_icm_256_test_case_0, /* */
  333. SRTP_AES_ICM_256 /* */
  334. };