aes_icm_nss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * aes_icm_nss.c
  3. *
  4. * AES Integer Counter Mode
  5. *
  6. * Richard L. Barnes
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2013-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. #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 nss" /* printable module name */
  56. };
  57. /*
  58. * integer counter mode works as follows:
  59. *
  60. * 16 bits
  61. * <----->
  62. * +------+------+------+------+------+------+------+------+
  63. * | nonce | packet index | ctr |---+
  64. * +------+------+------+------+------+------+------+------+ |
  65. * |
  66. * +------+------+------+------+------+------+------+------+ v
  67. * | salt |000000|->(+)
  68. * +------+------+------+------+------+------+------+------+ |
  69. * |
  70. * +---------+
  71. * | encrypt |
  72. * +---------+
  73. * |
  74. * +------+------+------+------+------+------+------+------+ |
  75. * | keystream block |<--+
  76. * +------+------+------+------+------+------+------+------+
  77. *
  78. * All fields are big-endian
  79. *
  80. * ctr is the block counter, which increments from zero for
  81. * each packet (16 bits wide)
  82. *
  83. * packet index is distinct for each packet (48 bits wide)
  84. *
  85. * nonce can be distinct across many uses of the same key, or
  86. * can be a fixed value per key, or can be per-packet randomness
  87. * (64 bits)
  88. *
  89. */
  90. /*
  91. * This function allocates a new instance of this crypto engine.
  92. * The key_len parameter should be one of 30, 38, or 46 for
  93. * AES-128, AES-192, and AES-256 respectively. Note, this key_len
  94. * value is inflated, as it also accounts for the 112 bit salt
  95. * value. The tlen argument is for the AEAD tag length, which
  96. * isn't used in counter mode.
  97. */
  98. static srtp_err_status_t srtp_aes_icm_nss_alloc(srtp_cipher_t **c,
  99. int key_len,
  100. int tlen)
  101. {
  102. srtp_aes_icm_ctx_t *icm;
  103. NSSInitContext *nss;
  104. (void)tlen;
  105. debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
  106. key_len);
  107. /*
  108. * Verify the key_len is valid for one of: AES-128/192/256
  109. */
  110. if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
  111. key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
  112. key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  113. return srtp_err_status_bad_param;
  114. }
  115. /* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
  116. nss = NSS_InitContext("", "", "", "", NULL,
  117. NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
  118. NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
  119. NSS_INIT_OPTIMIZESPACE);
  120. if (!nss) {
  121. return (srtp_err_status_cipher_fail);
  122. }
  123. /* allocate memory a cipher of type aes_icm */
  124. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  125. if (*c == NULL) {
  126. NSS_ShutdownContext(nss);
  127. return srtp_err_status_alloc_fail;
  128. }
  129. icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
  130. if (icm == NULL) {
  131. NSS_ShutdownContext(nss);
  132. srtp_crypto_free(*c);
  133. *c = NULL;
  134. return srtp_err_status_alloc_fail;
  135. }
  136. icm->key = NULL;
  137. icm->ctx = NULL;
  138. icm->nss = nss;
  139. /* set pointers */
  140. (*c)->state = icm;
  141. /* setup cipher parameters */
  142. switch (key_len) {
  143. case SRTP_AES_ICM_128_KEY_LEN_WSALT:
  144. (*c)->algorithm = SRTP_AES_ICM_128;
  145. (*c)->type = &srtp_aes_icm_128;
  146. icm->key_size = SRTP_AES_128_KEY_LEN;
  147. break;
  148. case SRTP_AES_ICM_192_KEY_LEN_WSALT:
  149. (*c)->algorithm = SRTP_AES_ICM_192;
  150. (*c)->type = &srtp_aes_icm_192;
  151. icm->key_size = SRTP_AES_192_KEY_LEN;
  152. break;
  153. case SRTP_AES_ICM_256_KEY_LEN_WSALT:
  154. (*c)->algorithm = SRTP_AES_ICM_256;
  155. (*c)->type = &srtp_aes_icm_256;
  156. icm->key_size = SRTP_AES_256_KEY_LEN;
  157. break;
  158. }
  159. /* set key size */
  160. (*c)->key_len = key_len;
  161. return srtp_err_status_ok;
  162. }
  163. /*
  164. * This function deallocates an instance of this engine
  165. */
  166. static srtp_err_status_t srtp_aes_icm_nss_dealloc(srtp_cipher_t *c)
  167. {
  168. srtp_aes_icm_ctx_t *ctx;
  169. ctx = (srtp_aes_icm_ctx_t *)c->state;
  170. if (ctx) {
  171. /* free any PK11 values that have been created */
  172. if (ctx->key) {
  173. PK11_FreeSymKey(ctx->key);
  174. ctx->key = NULL;
  175. }
  176. if (ctx->ctx) {
  177. PK11_DestroyContext(ctx->ctx, PR_TRUE);
  178. ctx->ctx = NULL;
  179. }
  180. if (ctx->nss) {
  181. NSS_ShutdownContext(ctx->nss);
  182. ctx->nss = NULL;
  183. }
  184. /* zeroize everything */
  185. octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
  186. srtp_crypto_free(ctx);
  187. }
  188. /* free memory */
  189. srtp_crypto_free(c);
  190. return (srtp_err_status_ok);
  191. }
  192. /*
  193. * aes_icm_nss_context_init(...) initializes the aes_icm_context
  194. * using the value in key[].
  195. *
  196. * the key is the secret key
  197. *
  198. * the salt is unpredictable (but not necessarily secret) data which
  199. * randomizes the starting point in the keystream
  200. */
  201. static srtp_err_status_t srtp_aes_icm_nss_context_init(void *cv,
  202. const uint8_t *key)
  203. {
  204. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  205. /*
  206. * set counter and initial values to 'offset' value, being careful not to
  207. * go past the end of the key buffer
  208. */
  209. v128_set_to_zero(&c->counter);
  210. v128_set_to_zero(&c->offset);
  211. memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
  212. memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
  213. /* force last two octets of the offset to zero (for srtp compatibility) */
  214. c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
  215. c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
  216. debug_print(srtp_mod_aes_icm, "key: %s",
  217. srtp_octet_string_hex_string(key, c->key_size));
  218. debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
  219. if (c->key) {
  220. PK11_FreeSymKey(c->key);
  221. c->key = NULL;
  222. }
  223. PK11SlotInfo *slot = PK11_GetBestSlot(CKM_AES_CTR, NULL);
  224. if (!slot) {
  225. return srtp_err_status_bad_param;
  226. }
  227. SECItem keyItem = { siBuffer, (unsigned char *)key, c->key_size };
  228. c->key = PK11_ImportSymKey(slot, CKM_AES_CTR, PK11_OriginUnwrap,
  229. CKA_ENCRYPT, &keyItem, NULL);
  230. PK11_FreeSlot(slot);
  231. if (!c->key) {
  232. return srtp_err_status_cipher_fail;
  233. }
  234. return (srtp_err_status_ok);
  235. }
  236. /*
  237. * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
  238. * the offset
  239. */
  240. static srtp_err_status_t srtp_aes_icm_nss_set_iv(void *cv,
  241. uint8_t *iv,
  242. srtp_cipher_direction_t dir)
  243. {
  244. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  245. v128_t nonce;
  246. (void)dir;
  247. /* set nonce (for alignment) */
  248. v128_copy_octet_string(&nonce, iv);
  249. debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
  250. v128_xor(&c->counter, &c->offset, &nonce);
  251. debug_print(srtp_mod_aes_icm, "set_counter: %s",
  252. v128_hex_string(&c->counter));
  253. /* set up the PK11 context now that we have all the info */
  254. CK_AES_CTR_PARAMS param;
  255. param.ulCounterBits = 16;
  256. memcpy(param.cb, &c->counter, 16);
  257. if (!c->key) {
  258. return srtp_err_status_bad_param;
  259. }
  260. if (c->ctx) {
  261. PK11_DestroyContext(c->ctx, PR_TRUE);
  262. }
  263. SECItem paramItem = { siBuffer, (unsigned char *)&param,
  264. sizeof(CK_AES_CTR_PARAMS) };
  265. c->ctx = PK11_CreateContextBySymKey(CKM_AES_CTR, CKA_ENCRYPT, c->key,
  266. &paramItem);
  267. if (!c->ctx) {
  268. return srtp_err_status_cipher_fail;
  269. }
  270. return srtp_err_status_ok;
  271. }
  272. /*
  273. * This function encrypts a buffer using AES CTR mode
  274. *
  275. * Parameters:
  276. * c Crypto context
  277. * buf data to encrypt
  278. * enc_len length of encrypt buffer
  279. */
  280. static srtp_err_status_t srtp_aes_icm_nss_encrypt(void *cv,
  281. unsigned char *buf,
  282. unsigned int *enc_len)
  283. {
  284. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  285. if (!c->ctx) {
  286. return srtp_err_status_bad_param;
  287. }
  288. int rv =
  289. PK11_CipherOp(c->ctx, buf, (int *)enc_len, *enc_len, buf, *enc_len);
  290. srtp_err_status_t status = (srtp_err_status_ok);
  291. if (rv != SECSuccess) {
  292. status = (srtp_err_status_cipher_fail);
  293. }
  294. return status;
  295. }
  296. /*
  297. * Name of this crypto engine
  298. */
  299. static const char srtp_aes_icm_128_nss_description[] =
  300. "AES-128 counter mode using NSS";
  301. static const char srtp_aes_icm_192_nss_description[] =
  302. "AES-192 counter mode using NSS";
  303. static const char srtp_aes_icm_256_nss_description[] =
  304. "AES-256 counter mode using NSS";
  305. /*
  306. * This is the function table for this crypto engine.
  307. * note: the encrypt function is identical to the decrypt function
  308. */
  309. const srtp_cipher_type_t srtp_aes_icm_128 = {
  310. srtp_aes_icm_nss_alloc, /* */
  311. srtp_aes_icm_nss_dealloc, /* */
  312. srtp_aes_icm_nss_context_init, /* */
  313. 0, /* set_aad */
  314. srtp_aes_icm_nss_encrypt, /* */
  315. srtp_aes_icm_nss_encrypt, /* */
  316. srtp_aes_icm_nss_set_iv, /* */
  317. 0, /* get_tag */
  318. srtp_aes_icm_128_nss_description, /* */
  319. &srtp_aes_icm_128_test_case_0, /* */
  320. SRTP_AES_ICM_128 /* */
  321. };
  322. /*
  323. * This is the function table for this crypto engine.
  324. * note: the encrypt function is identical to the decrypt function
  325. */
  326. const srtp_cipher_type_t srtp_aes_icm_192 = {
  327. srtp_aes_icm_nss_alloc, /* */
  328. srtp_aes_icm_nss_dealloc, /* */
  329. srtp_aes_icm_nss_context_init, /* */
  330. 0, /* set_aad */
  331. srtp_aes_icm_nss_encrypt, /* */
  332. srtp_aes_icm_nss_encrypt, /* */
  333. srtp_aes_icm_nss_set_iv, /* */
  334. 0, /* get_tag */
  335. srtp_aes_icm_192_nss_description, /* */
  336. &srtp_aes_icm_192_test_case_0, /* */
  337. SRTP_AES_ICM_192 /* */
  338. };
  339. /*
  340. * This is the function table for this crypto engine.
  341. * note: the encrypt function is identical to the decrypt function
  342. */
  343. const srtp_cipher_type_t srtp_aes_icm_256 = {
  344. srtp_aes_icm_nss_alloc, /* */
  345. srtp_aes_icm_nss_dealloc, /* */
  346. srtp_aes_icm_nss_context_init, /* */
  347. 0, /* set_aad */
  348. srtp_aes_icm_nss_encrypt, /* */
  349. srtp_aes_icm_nss_encrypt, /* */
  350. srtp_aes_icm_nss_set_iv, /* */
  351. 0, /* get_tag */
  352. srtp_aes_icm_256_nss_description, /* */
  353. &srtp_aes_icm_256_test_case_0, /* */
  354. SRTP_AES_ICM_256 /* */
  355. };