2
0

aes_icm_mbedtls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
  117. key_len);
  118. /*
  119. * Verify the key_len is valid for one of: AES-128/192/256
  120. */
  121. if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
  122. key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
  123. key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  124. return srtp_err_status_bad_param;
  125. }
  126. /* allocate memory a cipher of type aes_icm */
  127. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  128. if (*c == NULL) {
  129. return srtp_err_status_alloc_fail;
  130. }
  131. icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
  132. if (icm == NULL) {
  133. srtp_crypto_free(*c);
  134. *c = NULL;
  135. return srtp_err_status_alloc_fail;
  136. }
  137. icm->ctx =
  138. (mbedtls_aes_context *)srtp_crypto_alloc(sizeof(mbedtls_aes_context));
  139. if (icm->ctx == NULL) {
  140. srtp_crypto_free(icm);
  141. srtp_crypto_free(*c);
  142. *c = NULL;
  143. return srtp_err_status_alloc_fail;
  144. }
  145. mbedtls_aes_init(icm->ctx);
  146. /* set pointers */
  147. (*c)->state = icm;
  148. /* setup cipher parameters */
  149. switch (key_len) {
  150. case SRTP_AES_ICM_128_KEY_LEN_WSALT:
  151. (*c)->algorithm = SRTP_AES_ICM_128;
  152. (*c)->type = &srtp_aes_icm_128;
  153. icm->key_size = SRTP_AES_128_KEY_LEN;
  154. break;
  155. case SRTP_AES_ICM_192_KEY_LEN_WSALT:
  156. (*c)->algorithm = SRTP_AES_ICM_192;
  157. (*c)->type = &srtp_aes_icm_192;
  158. icm->key_size = SRTP_AES_192_KEY_LEN;
  159. break;
  160. case SRTP_AES_ICM_256_KEY_LEN_WSALT:
  161. (*c)->algorithm = SRTP_AES_ICM_256;
  162. (*c)->type = &srtp_aes_icm_256;
  163. icm->key_size = SRTP_AES_256_KEY_LEN;
  164. break;
  165. }
  166. /* set key size */
  167. (*c)->key_len = key_len;
  168. return srtp_err_status_ok;
  169. }
  170. /*
  171. * This function deallocates an instance of this engine
  172. */
  173. static srtp_err_status_t srtp_aes_icm_mbedtls_dealloc(srtp_cipher_t *c)
  174. {
  175. srtp_aes_icm_ctx_t *ctx;
  176. if (c == NULL) {
  177. return srtp_err_status_bad_param;
  178. }
  179. /*
  180. * Free the aes context
  181. */
  182. ctx = (srtp_aes_icm_ctx_t *)c->state;
  183. if (ctx != NULL) {
  184. mbedtls_aes_free(ctx->ctx);
  185. srtp_crypto_free(ctx->ctx);
  186. /* zeroize the key material */
  187. octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
  188. srtp_crypto_free(ctx);
  189. }
  190. /* free memory */
  191. srtp_crypto_free(c);
  192. return srtp_err_status_ok;
  193. }
  194. static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
  195. const uint8_t *key)
  196. {
  197. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  198. uint32_t key_size_in_bits = (c->key_size << 3);
  199. int errcode = 0;
  200. /*
  201. * set counter and initial values to 'offset' value, being careful not to
  202. * go past the end of the key buffer
  203. */
  204. v128_set_to_zero(&c->counter);
  205. v128_set_to_zero(&c->offset);
  206. memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
  207. memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
  208. /* force last two octets of the offset to zero (for srtp compatibility) */
  209. c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
  210. c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
  211. debug_print(srtp_mod_aes_icm, "key: %s",
  212. srtp_octet_string_hex_string(key, c->key_size));
  213. debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
  214. switch (c->key_size) {
  215. case SRTP_AES_256_KEY_LEN:
  216. case SRTP_AES_192_KEY_LEN:
  217. case SRTP_AES_128_KEY_LEN:
  218. break;
  219. default:
  220. return srtp_err_status_bad_param;
  221. break;
  222. }
  223. errcode = mbedtls_aes_setkey_enc(c->ctx, key, key_size_in_bits);
  224. if (errcode != 0) {
  225. debug_print(srtp_mod_aes_icm, "errCode: %d", errcode);
  226. }
  227. return srtp_err_status_ok;
  228. }
  229. /*
  230. * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
  231. * the offset
  232. */
  233. static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
  234. void *cv,
  235. uint8_t *iv,
  236. srtp_cipher_direction_t dir)
  237. {
  238. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  239. v128_t nonce;
  240. c->nc_off = 0;
  241. /* set nonce (for alignment) */
  242. v128_copy_octet_string(&nonce, iv);
  243. debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
  244. v128_xor(&c->counter, &c->offset, &nonce);
  245. debug_print(srtp_mod_aes_icm, "set_counter: %s",
  246. v128_hex_string(&c->counter));
  247. return srtp_err_status_ok;
  248. }
  249. /*
  250. * This function encrypts a buffer using AES CTR mode
  251. *
  252. * Parameters:
  253. * c Crypto context
  254. * buf data to encrypt
  255. * enc_len length of encrypt buffer
  256. */
  257. static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
  258. unsigned char *buf,
  259. unsigned int *enc_len)
  260. {
  261. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  262. int errCode = 0;
  263. debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
  264. errCode =
  265. mbedtls_aes_crypt_ctr(c->ctx, *enc_len, &(c->nc_off), c->counter.v8,
  266. c->stream_block.v8, buf, buf);
  267. if (errCode != 0) {
  268. debug_print(srtp_mod_aes_icm, "encrypt error: %d", errCode);
  269. return srtp_err_status_cipher_fail;
  270. }
  271. return srtp_err_status_ok;
  272. }
  273. /*
  274. * Name of this crypto engine
  275. */
  276. static const char srtp_aes_icm_128_mbedtls_description[] =
  277. "AES-128 counter mode using mbedtls";
  278. static const char srtp_aes_icm_192_mbedtls_description[] =
  279. "AES-192 counter mode using mbedtls";
  280. static const char srtp_aes_icm_256_mbedtls_description[] =
  281. "AES-256 counter mode using mbedtls";
  282. /*
  283. * This is the function table for this crypto engine.
  284. * note: the encrypt function is identical to the decrypt function
  285. */
  286. const srtp_cipher_type_t srtp_aes_icm_128 = {
  287. srtp_aes_icm_mbedtls_alloc, /* */
  288. srtp_aes_icm_mbedtls_dealloc, /* */
  289. srtp_aes_icm_mbedtls_context_init, /* */
  290. 0, /* set_aad */
  291. srtp_aes_icm_mbedtls_encrypt, /* */
  292. srtp_aes_icm_mbedtls_encrypt, /* */
  293. srtp_aes_icm_mbedtls_set_iv, /* */
  294. 0, /* get_tag */
  295. srtp_aes_icm_128_mbedtls_description, /* */
  296. &srtp_aes_icm_128_test_case_0, /* */
  297. SRTP_AES_ICM_128 /* */
  298. };
  299. /*
  300. * This is the function table for this crypto engine.
  301. * note: the encrypt function is identical to the decrypt function
  302. */
  303. const srtp_cipher_type_t srtp_aes_icm_192 = {
  304. srtp_aes_icm_mbedtls_alloc, /* */
  305. srtp_aes_icm_mbedtls_dealloc, /* */
  306. srtp_aes_icm_mbedtls_context_init, /* */
  307. 0, /* set_aad */
  308. srtp_aes_icm_mbedtls_encrypt, /* */
  309. srtp_aes_icm_mbedtls_encrypt, /* */
  310. srtp_aes_icm_mbedtls_set_iv, /* */
  311. 0, /* get_tag */
  312. srtp_aes_icm_192_mbedtls_description, /* */
  313. &srtp_aes_icm_192_test_case_0, /* */
  314. SRTP_AES_ICM_192 /* */
  315. };
  316. /*
  317. * This is the function table for this crypto engine.
  318. * note: the encrypt function is identical to the decrypt function
  319. */
  320. const srtp_cipher_type_t srtp_aes_icm_256 = {
  321. srtp_aes_icm_mbedtls_alloc, /* */
  322. srtp_aes_icm_mbedtls_dealloc, /* */
  323. srtp_aes_icm_mbedtls_context_init, /* */
  324. 0, /* set_aad */
  325. srtp_aes_icm_mbedtls_encrypt, /* */
  326. srtp_aes_icm_mbedtls_encrypt, /* */
  327. srtp_aes_icm_mbedtls_set_iv, /* */
  328. 0, /* get_tag */
  329. srtp_aes_icm_256_mbedtls_description, /* */
  330. &srtp_aes_icm_256_test_case_0, /* */
  331. SRTP_AES_ICM_256 /* */
  332. };