2
0

aes_icm_ossl.c 12 KB

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