2
0

aes_gcm_nss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * aes_gcm_nss.c
  3. *
  4. * AES Galois Counter Mode
  5. *
  6. * Richard L. Barnes
  7. * Cisco Systems, Inc.
  8. *
  9. */
  10. /*
  11. *
  12. * Copyright (c) 2013-2017, Cisco Systems, Inc.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * Neither the name of the Cisco Systems, Inc. nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  36. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  38. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42. * OF THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. */
  45. #ifdef HAVE_CONFIG_H
  46. #include <config.h>
  47. #endif
  48. #include "aes_gcm.h"
  49. #include "alloc.h"
  50. #include "err.h" /* for srtp_debug */
  51. #include "crypto_types.h"
  52. #include "cipher_types.h"
  53. #include "cipher_test_cases.h"
  54. #include <secerr.h>
  55. #include <nspr.h>
  56. srtp_debug_module_t srtp_mod_aes_gcm = {
  57. 0, /* debugging is off by default */
  58. "aes gcm nss" /* printable module name */
  59. };
  60. /*
  61. * For now we only support 8 and 16 octet tags. The spec allows for
  62. * optional 12 byte tag, which may be supported in the future.
  63. */
  64. #define GCM_IV_LEN 12
  65. #define GCM_AUTH_TAG_LEN 16
  66. #define GCM_AUTH_TAG_LEN_8 8
  67. /*
  68. * This function allocates a new instance of this crypto engine.
  69. * The key_len parameter should be one of 28 or 44 for
  70. * AES-128-GCM or AES-256-GCM respectively. Note that the
  71. * key length includes the 14 byte salt value that is used when
  72. * initializing the KDF.
  73. */
  74. static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
  75. int key_len,
  76. int tlen)
  77. {
  78. srtp_aes_gcm_ctx_t *gcm;
  79. NSSInitContext *nss;
  80. debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
  81. key_len);
  82. debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
  83. /*
  84. * Verify the key_len is valid for one of: AES-128/256
  85. */
  86. if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
  87. key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
  88. return (srtp_err_status_bad_param);
  89. }
  90. if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
  91. return (srtp_err_status_bad_param);
  92. }
  93. /* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
  94. nss = NSS_InitContext("", "", "", "", NULL,
  95. NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
  96. NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
  97. NSS_INIT_OPTIMIZESPACE);
  98. if (!nss) {
  99. return (srtp_err_status_cipher_fail);
  100. }
  101. /* allocate memory a cipher of type aes_gcm */
  102. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  103. if (*c == NULL) {
  104. NSS_ShutdownContext(nss);
  105. return (srtp_err_status_alloc_fail);
  106. }
  107. gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
  108. if (gcm == NULL) {
  109. NSS_ShutdownContext(nss);
  110. srtp_crypto_free(*c);
  111. *c = NULL;
  112. return (srtp_err_status_alloc_fail);
  113. }
  114. gcm->nss = nss;
  115. /* set pointers */
  116. (*c)->state = gcm;
  117. /* setup cipher attributes */
  118. switch (key_len) {
  119. case SRTP_AES_GCM_128_KEY_LEN_WSALT:
  120. (*c)->type = &srtp_aes_gcm_128;
  121. (*c)->algorithm = SRTP_AES_GCM_128;
  122. gcm->key_size = SRTP_AES_128_KEY_LEN;
  123. gcm->tag_size = tlen;
  124. gcm->params.ulTagBits = 8 * tlen;
  125. break;
  126. case SRTP_AES_GCM_256_KEY_LEN_WSALT:
  127. (*c)->type = &srtp_aes_gcm_256;
  128. (*c)->algorithm = SRTP_AES_GCM_256;
  129. gcm->key_size = SRTP_AES_256_KEY_LEN;
  130. gcm->tag_size = tlen;
  131. gcm->params.ulTagBits = 8 * tlen;
  132. break;
  133. default:
  134. /* this should never hit, but to be sure... */
  135. return (srtp_err_status_bad_param);
  136. }
  137. /* set key size and tag size*/
  138. (*c)->key_len = key_len;
  139. return (srtp_err_status_ok);
  140. }
  141. /*
  142. * This function deallocates a GCM session
  143. */
  144. static srtp_err_status_t srtp_aes_gcm_nss_dealloc(srtp_cipher_t *c)
  145. {
  146. srtp_aes_gcm_ctx_t *ctx;
  147. ctx = (srtp_aes_gcm_ctx_t *)c->state;
  148. if (ctx) {
  149. /* release NSS resources */
  150. if (ctx->key) {
  151. PK11_FreeSymKey(ctx->key);
  152. }
  153. if (ctx->nss) {
  154. NSS_ShutdownContext(ctx->nss);
  155. ctx->nss = NULL;
  156. }
  157. /* zeroize the key material */
  158. octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
  159. srtp_crypto_free(ctx);
  160. }
  161. /* free memory */
  162. srtp_crypto_free(c);
  163. return (srtp_err_status_ok);
  164. }
  165. /*
  166. * aes_gcm_nss_context_init(...) initializes the aes_gcm_context
  167. * using the value in key[].
  168. *
  169. * the key is the secret key
  170. */
  171. static srtp_err_status_t srtp_aes_gcm_nss_context_init(void *cv,
  172. const uint8_t *key)
  173. {
  174. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  175. c->dir = srtp_direction_any;
  176. debug_print(srtp_mod_aes_gcm, "key: %s",
  177. srtp_octet_string_hex_string(key, c->key_size));
  178. if (c->key) {
  179. PK11_FreeSymKey(c->key);
  180. c->key = NULL;
  181. }
  182. PK11SlotInfo *slot = PK11_GetBestSlot(CKM_AES_GCM, NULL);
  183. if (!slot) {
  184. return (srtp_err_status_cipher_fail);
  185. }
  186. SECItem key_item = { siBuffer, (unsigned char *)key, c->key_size };
  187. c->key = PK11_ImportSymKey(slot, CKM_AES_GCM, PK11_OriginUnwrap,
  188. CKA_ENCRYPT, &key_item, NULL);
  189. PK11_FreeSlot(slot);
  190. if (!c->key) {
  191. return (srtp_err_status_cipher_fail);
  192. }
  193. return (srtp_err_status_ok);
  194. }
  195. /*
  196. * aes_gcm_nss_set_iv(c, iv) sets the counter value to the exor of iv with
  197. * the offset
  198. */
  199. static srtp_err_status_t srtp_aes_gcm_nss_set_iv(
  200. void *cv,
  201. uint8_t *iv,
  202. srtp_cipher_direction_t direction)
  203. {
  204. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  205. if (direction != srtp_direction_encrypt &&
  206. direction != srtp_direction_decrypt) {
  207. return (srtp_err_status_bad_param);
  208. }
  209. c->dir = direction;
  210. debug_print(srtp_mod_aes_gcm, "setting iv: %s",
  211. srtp_octet_string_hex_string(iv, GCM_IV_LEN));
  212. memcpy(c->iv, iv, GCM_IV_LEN);
  213. return (srtp_err_status_ok);
  214. }
  215. /*
  216. * This function processes the AAD
  217. *
  218. * Parameters:
  219. * c Crypto context
  220. * aad Additional data to process for AEAD cipher suites
  221. * aad_len length of aad buffer
  222. */
  223. static srtp_err_status_t srtp_aes_gcm_nss_set_aad(void *cv,
  224. const uint8_t *aad,
  225. uint32_t aad_len)
  226. {
  227. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  228. debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
  229. srtp_octet_string_hex_string(aad, aad_len));
  230. if (aad_len + c->aad_size > MAX_AD_SIZE) {
  231. return srtp_err_status_bad_param;
  232. }
  233. memcpy(c->aad + c->aad_size, aad, aad_len);
  234. c->aad_size += aad_len;
  235. return (srtp_err_status_ok);
  236. }
  237. static srtp_err_status_t srtp_aes_gcm_nss_do_crypto(void *cv,
  238. int encrypt,
  239. unsigned char *buf,
  240. unsigned int *enc_len)
  241. {
  242. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  243. c->params.pIv = c->iv;
  244. c->params.ulIvLen = GCM_IV_LEN;
  245. c->params.pAAD = c->aad;
  246. c->params.ulAADLen = c->aad_size;
  247. // Reset AAD
  248. c->aad_size = 0;
  249. int rv;
  250. SECItem param = { siBuffer, (unsigned char *)&c->params,
  251. sizeof(CK_GCM_PARAMS) };
  252. if (encrypt) {
  253. rv = PK11_Encrypt(c->key, CKM_AES_GCM, &param, buf, enc_len,
  254. *enc_len + 16, buf, *enc_len);
  255. } else {
  256. rv = PK11_Decrypt(c->key, CKM_AES_GCM, &param, buf, enc_len, *enc_len,
  257. buf, *enc_len);
  258. }
  259. srtp_err_status_t status = (srtp_err_status_ok);
  260. if (rv != SECSuccess) {
  261. status = (srtp_err_status_cipher_fail);
  262. }
  263. return status;
  264. }
  265. /*
  266. * This function encrypts a buffer using AES GCM mode
  267. *
  268. * XXX(rlb@ipv.sx): We're required to break off and cache the tag
  269. * here, because the get_tag() method is separate and the tests expect
  270. * encrypt() not to change the size of the plaintext. It might be
  271. * good to update the calling API so that this is cleaner.
  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_gcm_nss_encrypt(void *cv,
  279. unsigned char *buf,
  280. unsigned int *enc_len)
  281. {
  282. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  283. // When we get a non-NULL buffer, we know that the caller is
  284. // prepared to also take the tag. When we get a NULL buffer,
  285. // even though there's no data, we need to give NSS a buffer
  286. // where it can write the tag. We can't just use c->tag because
  287. // memcpy has undefined behavior on overlapping ranges.
  288. unsigned char tagbuf[16];
  289. unsigned char *non_null_buf = buf;
  290. if (!non_null_buf && (*enc_len == 0)) {
  291. non_null_buf = tagbuf;
  292. } else if (!non_null_buf) {
  293. return srtp_err_status_bad_param;
  294. }
  295. srtp_err_status_t status =
  296. srtp_aes_gcm_nss_do_crypto(cv, 1, non_null_buf, enc_len);
  297. if (status != srtp_err_status_ok) {
  298. return status;
  299. }
  300. memcpy(c->tag, non_null_buf + (*enc_len - c->tag_size), c->tag_size);
  301. *enc_len -= c->tag_size;
  302. return srtp_err_status_ok;
  303. }
  304. /*
  305. * This function calculates and returns the GCM tag for a given context.
  306. * This should be called after encrypting the data. The *len value
  307. * is increased by the tag size. The caller must ensure that *buf has
  308. * enough room to accept the appended tag.
  309. *
  310. * Parameters:
  311. * c Crypto context
  312. * buf data to encrypt
  313. * len length of encrypt buffer
  314. */
  315. static srtp_err_status_t srtp_aes_gcm_nss_get_tag(void *cv,
  316. uint8_t *buf,
  317. uint32_t *len)
  318. {
  319. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  320. *len = c->tag_size;
  321. memcpy(buf, c->tag, c->tag_size);
  322. return (srtp_err_status_ok);
  323. }
  324. /*
  325. * This function decrypts a buffer using AES GCM mode
  326. *
  327. * Parameters:
  328. * c Crypto context
  329. * buf data to encrypt
  330. * enc_len length of encrypt buffer
  331. */
  332. static srtp_err_status_t srtp_aes_gcm_nss_decrypt(void *cv,
  333. unsigned char *buf,
  334. unsigned int *enc_len)
  335. {
  336. srtp_err_status_t status = srtp_aes_gcm_nss_do_crypto(cv, 0, buf, enc_len);
  337. if (status != srtp_err_status_ok) {
  338. int err = PR_GetError();
  339. if (err == SEC_ERROR_BAD_DATA) {
  340. status = srtp_err_status_auth_fail;
  341. }
  342. }
  343. return status;
  344. }
  345. /*
  346. * Name of this crypto engine
  347. */
  348. static const char srtp_aes_gcm_128_nss_description[] = "AES-128 GCM using NSS";
  349. static const char srtp_aes_gcm_256_nss_description[] = "AES-256 GCM using NSS";
  350. /*
  351. * This is the vector function table for this crypto engine.
  352. */
  353. /* clang-format off */
  354. const srtp_cipher_type_t srtp_aes_gcm_128 = {
  355. srtp_aes_gcm_nss_alloc,
  356. srtp_aes_gcm_nss_dealloc,
  357. srtp_aes_gcm_nss_context_init,
  358. srtp_aes_gcm_nss_set_aad,
  359. srtp_aes_gcm_nss_encrypt,
  360. srtp_aes_gcm_nss_decrypt,
  361. srtp_aes_gcm_nss_set_iv,
  362. srtp_aes_gcm_nss_get_tag,
  363. srtp_aes_gcm_128_nss_description,
  364. &srtp_aes_gcm_128_test_case_0,
  365. SRTP_AES_GCM_128
  366. };
  367. /* clang-format on */
  368. /*
  369. * This is the vector function table for this crypto engine.
  370. */
  371. /* clang-format off */
  372. const srtp_cipher_type_t srtp_aes_gcm_256 = {
  373. srtp_aes_gcm_nss_alloc,
  374. srtp_aes_gcm_nss_dealloc,
  375. srtp_aes_gcm_nss_context_init,
  376. srtp_aes_gcm_nss_set_aad,
  377. srtp_aes_gcm_nss_encrypt,
  378. srtp_aes_gcm_nss_decrypt,
  379. srtp_aes_gcm_nss_set_iv,
  380. srtp_aes_gcm_nss_get_tag,
  381. srtp_aes_gcm_256_nss_description,
  382. &srtp_aes_gcm_256_test_case_0,
  383. SRTP_AES_GCM_256
  384. };
  385. /* clang-format on */