aes_icm_ossl.c 12 KB

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