2
0

aes_icm_nss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
  105. key_len);
  106. /*
  107. * Verify the key_len is valid for one of: AES-128/192/256
  108. */
  109. if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
  110. key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
  111. key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
  112. return srtp_err_status_bad_param;
  113. }
  114. /* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
  115. nss = NSS_InitContext("", "", "", "", NULL,
  116. NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
  117. NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
  118. NSS_INIT_OPTIMIZESPACE);
  119. if (!nss) {
  120. return (srtp_err_status_cipher_fail);
  121. }
  122. /* allocate memory a cipher of type aes_icm */
  123. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  124. if (*c == NULL) {
  125. NSS_ShutdownContext(nss);
  126. return srtp_err_status_alloc_fail;
  127. }
  128. icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
  129. if (icm == NULL) {
  130. NSS_ShutdownContext(nss);
  131. srtp_crypto_free(*c);
  132. *c = NULL;
  133. return srtp_err_status_alloc_fail;
  134. }
  135. icm->key = NULL;
  136. icm->ctx = NULL;
  137. icm->nss = nss;
  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_nss_dealloc(srtp_cipher_t *c)
  166. {
  167. srtp_aes_icm_ctx_t *ctx;
  168. ctx = (srtp_aes_icm_ctx_t *)c->state;
  169. if (ctx) {
  170. /* free any PK11 values that have been created */
  171. if (ctx->key) {
  172. PK11_FreeSymKey(ctx->key);
  173. ctx->key = NULL;
  174. }
  175. if (ctx->ctx) {
  176. PK11_DestroyContext(ctx->ctx, PR_TRUE);
  177. ctx->ctx = NULL;
  178. }
  179. if (ctx->nss) {
  180. NSS_ShutdownContext(ctx->nss);
  181. ctx->nss = NULL;
  182. }
  183. /* zeroize everything */
  184. octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
  185. srtp_crypto_free(ctx);
  186. }
  187. /* free memory */
  188. srtp_crypto_free(c);
  189. return (srtp_err_status_ok);
  190. }
  191. /*
  192. * aes_icm_nss_context_init(...) initializes the aes_icm_context
  193. * using the value in key[].
  194. *
  195. * the key is the secret key
  196. *
  197. * the salt is unpredictable (but not necessarily secret) data which
  198. * randomizes the starting point in the keystream
  199. */
  200. static srtp_err_status_t srtp_aes_icm_nss_context_init(void *cv,
  201. const uint8_t *key)
  202. {
  203. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  204. /*
  205. * set counter and initial values to 'offset' value, being careful not to
  206. * go past the end of the key buffer
  207. */
  208. v128_set_to_zero(&c->counter);
  209. v128_set_to_zero(&c->offset);
  210. memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
  211. memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
  212. /* force last two octets of the offset to zero (for srtp compatibility) */
  213. c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
  214. c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
  215. debug_print(srtp_mod_aes_icm, "key: %s",
  216. srtp_octet_string_hex_string(key, c->key_size));
  217. debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
  218. if (c->key) {
  219. PK11_FreeSymKey(c->key);
  220. c->key = NULL;
  221. }
  222. PK11SlotInfo *slot = PK11_GetBestSlot(CKM_AES_CTR, NULL);
  223. if (!slot) {
  224. return srtp_err_status_bad_param;
  225. }
  226. SECItem keyItem = { siBuffer, (unsigned char *)key, c->key_size };
  227. c->key = PK11_ImportSymKey(slot, CKM_AES_CTR, PK11_OriginUnwrap,
  228. CKA_ENCRYPT, &keyItem, NULL);
  229. PK11_FreeSlot(slot);
  230. if (!c->key) {
  231. return srtp_err_status_cipher_fail;
  232. }
  233. return (srtp_err_status_ok);
  234. }
  235. /*
  236. * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
  237. * the offset
  238. */
  239. static srtp_err_status_t srtp_aes_icm_nss_set_iv(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. /* set up the PK11 context now that we have all the info */
  252. CK_AES_CTR_PARAMS param;
  253. param.ulCounterBits = 16;
  254. memcpy(param.cb, &c->counter, 16);
  255. if (!c->key) {
  256. return srtp_err_status_bad_param;
  257. }
  258. if (c->ctx) {
  259. PK11_DestroyContext(c->ctx, PR_TRUE);
  260. }
  261. SECItem paramItem = { siBuffer, (unsigned char *)&param,
  262. sizeof(CK_AES_CTR_PARAMS) };
  263. c->ctx = PK11_CreateContextBySymKey(CKM_AES_CTR, CKA_ENCRYPT, c->key,
  264. &paramItem);
  265. if (!c->ctx) {
  266. return srtp_err_status_cipher_fail;
  267. }
  268. return srtp_err_status_ok;
  269. }
  270. /*
  271. * This function encrypts a buffer using AES CTR mode
  272. *
  273. * Parameters:
  274. * c Crypto context
  275. * buf data to encrypt
  276. * enc_len length of encrypt buffer
  277. */
  278. static srtp_err_status_t srtp_aes_icm_nss_encrypt(void *cv,
  279. unsigned char *buf,
  280. unsigned int *enc_len)
  281. {
  282. srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
  283. if (!c->ctx) {
  284. return srtp_err_status_bad_param;
  285. }
  286. int rv =
  287. PK11_CipherOp(c->ctx, buf, (int *)enc_len, *enc_len, buf, *enc_len);
  288. srtp_err_status_t status = (srtp_err_status_ok);
  289. if (rv != SECSuccess) {
  290. status = (srtp_err_status_cipher_fail);
  291. }
  292. return status;
  293. }
  294. /*
  295. * Name of this crypto engine
  296. */
  297. static const char srtp_aes_icm_128_nss_description[] =
  298. "AES-128 counter mode using NSS";
  299. static const char srtp_aes_icm_192_nss_description[] =
  300. "AES-192 counter mode using NSS";
  301. static const char srtp_aes_icm_256_nss_description[] =
  302. "AES-256 counter mode using NSS";
  303. /*
  304. * This is the function table for this crypto engine.
  305. * note: the encrypt function is identical to the decrypt function
  306. */
  307. const srtp_cipher_type_t srtp_aes_icm_128 = {
  308. srtp_aes_icm_nss_alloc, /* */
  309. srtp_aes_icm_nss_dealloc, /* */
  310. srtp_aes_icm_nss_context_init, /* */
  311. 0, /* set_aad */
  312. srtp_aes_icm_nss_encrypt, /* */
  313. srtp_aes_icm_nss_encrypt, /* */
  314. srtp_aes_icm_nss_set_iv, /* */
  315. 0, /* get_tag */
  316. srtp_aes_icm_128_nss_description, /* */
  317. &srtp_aes_icm_128_test_case_0, /* */
  318. SRTP_AES_ICM_128 /* */
  319. };
  320. /*
  321. * This is the function table for this crypto engine.
  322. * note: the encrypt function is identical to the decrypt function
  323. */
  324. const srtp_cipher_type_t srtp_aes_icm_192 = {
  325. srtp_aes_icm_nss_alloc, /* */
  326. srtp_aes_icm_nss_dealloc, /* */
  327. srtp_aes_icm_nss_context_init, /* */
  328. 0, /* set_aad */
  329. srtp_aes_icm_nss_encrypt, /* */
  330. srtp_aes_icm_nss_encrypt, /* */
  331. srtp_aes_icm_nss_set_iv, /* */
  332. 0, /* get_tag */
  333. srtp_aes_icm_192_nss_description, /* */
  334. &srtp_aes_icm_192_test_case_0, /* */
  335. SRTP_AES_ICM_192 /* */
  336. };
  337. /*
  338. * This is the function table for this crypto engine.
  339. * note: the encrypt function is identical to the decrypt function
  340. */
  341. const srtp_cipher_type_t srtp_aes_icm_256 = {
  342. srtp_aes_icm_nss_alloc, /* */
  343. srtp_aes_icm_nss_dealloc, /* */
  344. srtp_aes_icm_nss_context_init, /* */
  345. 0, /* set_aad */
  346. srtp_aes_icm_nss_encrypt, /* */
  347. srtp_aes_icm_nss_encrypt, /* */
  348. srtp_aes_icm_nss_set_iv, /* */
  349. 0, /* get_tag */
  350. srtp_aes_icm_256_nss_description, /* */
  351. &srtp_aes_icm_256_test_case_0, /* */
  352. SRTP_AES_ICM_256 /* */
  353. };