aes_gcm_nss.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 <secerr.h>
  54. #include <nspr.h>
  55. srtp_debug_module_t srtp_mod_aes_gcm = {
  56. 0, /* debugging is off by default */
  57. "aes gcm nss" /* printable module name */
  58. };
  59. /*
  60. * For now we only support 8 and 16 octet tags. The spec allows for
  61. * optional 12 byte tag, which may be supported in the future.
  62. */
  63. #define GCM_IV_LEN 12
  64. #define GCM_AUTH_TAG_LEN 16
  65. #define GCM_AUTH_TAG_LEN_8 8
  66. /*
  67. * This function allocates a new instance of this crypto engine.
  68. * The key_len parameter should be one of 28 or 44 for
  69. * AES-128-GCM or AES-256-GCM respectively. Note that the
  70. * key length includes the 14 byte salt value that is used when
  71. * initializing the KDF.
  72. */
  73. static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
  74. int key_len,
  75. int tlen)
  76. {
  77. srtp_aes_gcm_ctx_t *gcm;
  78. NSSInitContext *nss;
  79. debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
  80. key_len);
  81. debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
  82. /*
  83. * Verify the key_len is valid for one of: AES-128/256
  84. */
  85. if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
  86. key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
  87. return (srtp_err_status_bad_param);
  88. }
  89. if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
  90. return (srtp_err_status_bad_param);
  91. }
  92. /* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
  93. nss = NSS_InitContext("", "", "", "", NULL,
  94. NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
  95. NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
  96. NSS_INIT_OPTIMIZESPACE);
  97. if (!nss) {
  98. return (srtp_err_status_cipher_fail);
  99. }
  100. /* allocate memory a cipher of type aes_gcm */
  101. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  102. if (*c == NULL) {
  103. NSS_ShutdownContext(nss);
  104. return (srtp_err_status_alloc_fail);
  105. }
  106. gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
  107. if (gcm == NULL) {
  108. NSS_ShutdownContext(nss);
  109. srtp_crypto_free(*c);
  110. *c = NULL;
  111. return (srtp_err_status_alloc_fail);
  112. }
  113. gcm->nss = nss;
  114. /* set pointers */
  115. (*c)->state = gcm;
  116. /* setup cipher attributes */
  117. switch (key_len) {
  118. case SRTP_AES_GCM_128_KEY_LEN_WSALT:
  119. (*c)->type = &srtp_aes_gcm_128;
  120. (*c)->algorithm = SRTP_AES_GCM_128;
  121. gcm->key_size = SRTP_AES_128_KEY_LEN;
  122. gcm->tag_size = tlen;
  123. gcm->params.ulTagBits = 8 * tlen;
  124. break;
  125. case SRTP_AES_GCM_256_KEY_LEN_WSALT:
  126. (*c)->type = &srtp_aes_gcm_256;
  127. (*c)->algorithm = SRTP_AES_GCM_256;
  128. gcm->key_size = SRTP_AES_256_KEY_LEN;
  129. gcm->tag_size = tlen;
  130. gcm->params.ulTagBits = 8 * tlen;
  131. break;
  132. default:
  133. /* this should never hit, but to be sure... */
  134. return (srtp_err_status_bad_param);
  135. }
  136. /* set key size and tag size*/
  137. (*c)->key_len = key_len;
  138. return (srtp_err_status_ok);
  139. }
  140. /*
  141. * This function deallocates a GCM session
  142. */
  143. static srtp_err_status_t srtp_aes_gcm_nss_dealloc(srtp_cipher_t *c)
  144. {
  145. srtp_aes_gcm_ctx_t *ctx;
  146. ctx = (srtp_aes_gcm_ctx_t *)c->state;
  147. if (ctx) {
  148. /* release NSS resources */
  149. if (ctx->key) {
  150. PK11_FreeSymKey(ctx->key);
  151. }
  152. if (ctx->nss) {
  153. NSS_ShutdownContext(ctx->nss);
  154. ctx->nss = NULL;
  155. }
  156. /* zeroize the key material */
  157. octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
  158. srtp_crypto_free(ctx);
  159. }
  160. /* free memory */
  161. srtp_crypto_free(c);
  162. return (srtp_err_status_ok);
  163. }
  164. /*
  165. * aes_gcm_nss_context_init(...) initializes the aes_gcm_context
  166. * using the value in key[].
  167. *
  168. * the key is the secret key
  169. */
  170. static srtp_err_status_t srtp_aes_gcm_nss_context_init(void *cv,
  171. const uint8_t *key)
  172. {
  173. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  174. c->dir = srtp_direction_any;
  175. debug_print(srtp_mod_aes_gcm, "key: %s",
  176. srtp_octet_string_hex_string(key, c->key_size));
  177. if (c->key) {
  178. PK11_FreeSymKey(c->key);
  179. c->key = NULL;
  180. }
  181. PK11SlotInfo *slot = PK11_GetBestSlot(CKM_AES_GCM, NULL);
  182. if (!slot) {
  183. return (srtp_err_status_cipher_fail);
  184. }
  185. SECItem key_item = { siBuffer, (unsigned char *)key, c->key_size };
  186. c->key = PK11_ImportSymKey(slot, CKM_AES_GCM, PK11_OriginUnwrap,
  187. CKA_ENCRYPT, &key_item, NULL);
  188. PK11_FreeSlot(slot);
  189. if (!c->key) {
  190. return (srtp_err_status_cipher_fail);
  191. }
  192. return (srtp_err_status_ok);
  193. }
  194. /*
  195. * aes_gcm_nss_set_iv(c, iv) sets the counter value to the exor of iv with
  196. * the offset
  197. */
  198. static srtp_err_status_t srtp_aes_gcm_nss_set_iv(
  199. void *cv,
  200. uint8_t *iv,
  201. srtp_cipher_direction_t direction)
  202. {
  203. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  204. if (direction != srtp_direction_encrypt &&
  205. direction != srtp_direction_decrypt) {
  206. return (srtp_err_status_bad_param);
  207. }
  208. c->dir = direction;
  209. debug_print(srtp_mod_aes_gcm, "setting iv: %s",
  210. srtp_octet_string_hex_string(iv, GCM_IV_LEN));
  211. memcpy(c->iv, iv, GCM_IV_LEN);
  212. return (srtp_err_status_ok);
  213. }
  214. /*
  215. * This function processes the AAD
  216. *
  217. * Parameters:
  218. * c Crypto context
  219. * aad Additional data to process for AEAD cipher suites
  220. * aad_len length of aad buffer
  221. */
  222. static srtp_err_status_t srtp_aes_gcm_nss_set_aad(void *cv,
  223. const uint8_t *aad,
  224. uint32_t aad_len)
  225. {
  226. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  227. debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
  228. srtp_octet_string_hex_string(aad, aad_len));
  229. if (aad_len + c->aad_size > MAX_AD_SIZE) {
  230. return srtp_err_status_bad_param;
  231. }
  232. memcpy(c->aad + c->aad_size, aad, aad_len);
  233. c->aad_size += aad_len;
  234. return (srtp_err_status_ok);
  235. }
  236. static srtp_err_status_t srtp_aes_gcm_nss_do_crypto(void *cv,
  237. int encrypt,
  238. unsigned char *buf,
  239. unsigned int *enc_len)
  240. {
  241. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  242. c->params.pIv = c->iv;
  243. c->params.ulIvLen = GCM_IV_LEN;
  244. c->params.pAAD = c->aad;
  245. c->params.ulAADLen = c->aad_size;
  246. // Reset AAD
  247. c->aad_size = 0;
  248. int rv;
  249. SECItem param = { siBuffer, (unsigned char *)&c->params,
  250. sizeof(CK_GCM_PARAMS) };
  251. if (encrypt) {
  252. rv = PK11_Encrypt(c->key, CKM_AES_GCM, &param, buf, enc_len,
  253. *enc_len + 16, buf, *enc_len);
  254. } else {
  255. rv = PK11_Decrypt(c->key, CKM_AES_GCM, &param, buf, enc_len, *enc_len,
  256. buf, *enc_len);
  257. }
  258. srtp_err_status_t status = (srtp_err_status_ok);
  259. if (rv != SECSuccess) {
  260. status = (srtp_err_status_cipher_fail);
  261. }
  262. return status;
  263. }
  264. /*
  265. * This function encrypts a buffer using AES GCM mode
  266. *
  267. * XXX(rlb@ipv.sx): We're required to break off and cache the tag
  268. * here, because the get_tag() method is separate and the tests expect
  269. * encrypt() not to change the size of the plaintext. It might be
  270. * good to update the calling API so that this is cleaner.
  271. *
  272. * Parameters:
  273. * c Crypto context
  274. * buf data to encrypt
  275. * enc_len length of encrypt buffer
  276. */
  277. static srtp_err_status_t srtp_aes_gcm_nss_encrypt(void *cv,
  278. unsigned char *buf,
  279. unsigned int *enc_len)
  280. {
  281. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  282. // When we get a non-NULL buffer, we know that the caller is
  283. // prepared to also take the tag. When we get a NULL buffer,
  284. // even though there's no data, we need to give NSS a buffer
  285. // where it can write the tag. We can't just use c->tag because
  286. // memcpy has undefined behavior on overlapping ranges.
  287. unsigned char tagbuf[16];
  288. unsigned char *non_null_buf = buf;
  289. if (!non_null_buf && (*enc_len == 0)) {
  290. non_null_buf = tagbuf;
  291. } else if (!non_null_buf) {
  292. return srtp_err_status_bad_param;
  293. }
  294. srtp_err_status_t status =
  295. srtp_aes_gcm_nss_do_crypto(cv, 1, non_null_buf, enc_len);
  296. if (status != srtp_err_status_ok) {
  297. return status;
  298. }
  299. memcpy(c->tag, non_null_buf + (*enc_len - c->tag_size), c->tag_size);
  300. *enc_len -= c->tag_size;
  301. return srtp_err_status_ok;
  302. }
  303. /*
  304. * This function calculates and returns the GCM tag for a given context.
  305. * This should be called after encrypting the data. The *len value
  306. * is increased by the tag size. The caller must ensure that *buf has
  307. * enough room to accept the appended tag.
  308. *
  309. * Parameters:
  310. * c Crypto context
  311. * buf data to encrypt
  312. * len length of encrypt buffer
  313. */
  314. static srtp_err_status_t srtp_aes_gcm_nss_get_tag(void *cv,
  315. uint8_t *buf,
  316. uint32_t *len)
  317. {
  318. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  319. *len = c->tag_size;
  320. memcpy(buf, c->tag, c->tag_size);
  321. return (srtp_err_status_ok);
  322. }
  323. /*
  324. * This function decrypts a buffer using AES GCM mode
  325. *
  326. * Parameters:
  327. * c Crypto context
  328. * buf data to encrypt
  329. * enc_len length of encrypt buffer
  330. */
  331. static srtp_err_status_t srtp_aes_gcm_nss_decrypt(void *cv,
  332. unsigned char *buf,
  333. unsigned int *enc_len)
  334. {
  335. srtp_err_status_t status = srtp_aes_gcm_nss_do_crypto(cv, 0, buf, enc_len);
  336. if (status != srtp_err_status_ok) {
  337. int err = PR_GetError();
  338. if (err == SEC_ERROR_BAD_DATA) {
  339. status = srtp_err_status_auth_fail;
  340. }
  341. }
  342. return status;
  343. }
  344. /*
  345. * Name of this crypto engine
  346. */
  347. static const char srtp_aes_gcm_128_nss_description[] = "AES-128 GCM using NSS";
  348. static const char srtp_aes_gcm_256_nss_description[] = "AES-256 GCM using NSS";
  349. /*
  350. * KAT values for AES self-test. These
  351. * values we're derived from independent test code
  352. * using OpenSSL.
  353. */
  354. /* clang-format off */
  355. static const uint8_t srtp_aes_gcm_test_case_0_key[SRTP_AES_GCM_128_KEY_LEN_WSALT] = {
  356. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  357. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  358. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  359. 0x09, 0x0a, 0x0b, 0x0c,
  360. };
  361. /* clang-format on */
  362. /* clang-format off */
  363. static uint8_t srtp_aes_gcm_test_case_0_iv[12] = {
  364. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  365. 0xde, 0xca, 0xf8, 0x88
  366. };
  367. /* clang-format on */
  368. /* clang-format off */
  369. static const uint8_t srtp_aes_gcm_test_case_0_plaintext[60] = {
  370. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  371. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  372. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  373. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  374. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  375. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  376. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  377. 0xba, 0x63, 0x7b, 0x39
  378. };
  379. /* clang-format off */
  380. static const uint8_t srtp_aes_gcm_test_case_0_aad[20] = {
  381. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  382. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  383. 0xab, 0xad, 0xda, 0xd2
  384. };
  385. /* clang-format on */
  386. /* clang-format off */
  387. static const uint8_t srtp_aes_gcm_test_case_0_ciphertext[76] = {
  388. 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  389. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  390. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  391. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  392. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  393. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  394. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  395. 0x3d, 0x58, 0xe0, 0x91,
  396. /* the last 16 bytes are the tag */
  397. 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
  398. 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47,
  399. };
  400. /* clang-format on */
  401. static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_0a = {
  402. SRTP_AES_GCM_128_KEY_LEN_WSALT, /* octets in key */
  403. srtp_aes_gcm_test_case_0_key, /* key */
  404. srtp_aes_gcm_test_case_0_iv, /* packet index */
  405. 60, /* octets in plaintext */
  406. srtp_aes_gcm_test_case_0_plaintext, /* plaintext */
  407. 68, /* octets in ciphertext */
  408. srtp_aes_gcm_test_case_0_ciphertext, /* ciphertext + tag */
  409. 20, /* octets in AAD */
  410. srtp_aes_gcm_test_case_0_aad, /* AAD */
  411. GCM_AUTH_TAG_LEN_8, /* */
  412. NULL /* pointer to next testcase */
  413. };
  414. static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_0 = {
  415. SRTP_AES_GCM_128_KEY_LEN_WSALT, /* octets in key */
  416. srtp_aes_gcm_test_case_0_key, /* key */
  417. srtp_aes_gcm_test_case_0_iv, /* packet index */
  418. 60, /* octets in plaintext */
  419. srtp_aes_gcm_test_case_0_plaintext, /* plaintext */
  420. 76, /* octets in ciphertext */
  421. srtp_aes_gcm_test_case_0_ciphertext, /* ciphertext + tag */
  422. 20, /* octets in AAD */
  423. srtp_aes_gcm_test_case_0_aad, /* AAD */
  424. GCM_AUTH_TAG_LEN, /* */
  425. &srtp_aes_gcm_test_case_0a /* pointer to next testcase */
  426. };
  427. /* clang-format off */
  428. static const uint8_t srtp_aes_gcm_test_case_1_key[SRTP_AES_GCM_256_KEY_LEN_WSALT] = {
  429. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  430. 0xa5, 0x59, 0x09, 0xc5, 0x54, 0x66, 0x93, 0x1c,
  431. 0xaf, 0xf5, 0x26, 0x9a, 0x21, 0xd5, 0x14, 0xb2,
  432. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  433. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  434. 0x09, 0x0a, 0x0b, 0x0c,
  435. };
  436. /* clang-format on */
  437. /* clang-format off */
  438. static uint8_t srtp_aes_gcm_test_case_1_iv[12] = {
  439. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  440. 0xde, 0xca, 0xf8, 0x88
  441. };
  442. /* clang-format on */
  443. /* clang-format off */
  444. static const uint8_t srtp_aes_gcm_test_case_1_plaintext[60] = {
  445. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  446. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  447. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  448. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  449. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  450. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  451. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  452. 0xba, 0x63, 0x7b, 0x39
  453. };
  454. /* clang-format on */
  455. /* clang-format off */
  456. static const uint8_t srtp_aes_gcm_test_case_1_aad[20] = {
  457. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  458. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  459. 0xab, 0xad, 0xda, 0xd2
  460. };
  461. /* clang-format on */
  462. /* clang-format off */
  463. static const uint8_t srtp_aes_gcm_test_case_1_ciphertext[76] = {
  464. 0x0b, 0x11, 0xcf, 0xaf, 0x68, 0x4d, 0xae, 0x46,
  465. 0xc7, 0x90, 0xb8, 0x8e, 0xb7, 0x6a, 0x76, 0x2a,
  466. 0x94, 0x82, 0xca, 0xab, 0x3e, 0x39, 0xd7, 0x86,
  467. 0x1b, 0xc7, 0x93, 0xed, 0x75, 0x7f, 0x23, 0x5a,
  468. 0xda, 0xfd, 0xd3, 0xe2, 0x0e, 0x80, 0x87, 0xa9,
  469. 0x6d, 0xd7, 0xe2, 0x6a, 0x7d, 0x5f, 0xb4, 0x80,
  470. 0xef, 0xef, 0xc5, 0x29, 0x12, 0xd1, 0xaa, 0x10,
  471. 0x09, 0xc9, 0x86, 0xc1,
  472. /* the last 16 bytes are the tag */
  473. 0x45, 0xbc, 0x03, 0xe6, 0xe1, 0xac, 0x0a, 0x9f,
  474. 0x81, 0xcb, 0x8e, 0x5b, 0x46, 0x65, 0x63, 0x1d,
  475. };
  476. /* clang-format on */
  477. static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1a = {
  478. SRTP_AES_GCM_256_KEY_LEN_WSALT, /* octets in key */
  479. srtp_aes_gcm_test_case_1_key, /* key */
  480. srtp_aes_gcm_test_case_1_iv, /* packet index */
  481. 60, /* octets in plaintext */
  482. srtp_aes_gcm_test_case_1_plaintext, /* plaintext */
  483. 68, /* octets in ciphertext */
  484. srtp_aes_gcm_test_case_1_ciphertext, /* ciphertext + tag */
  485. 20, /* octets in AAD */
  486. srtp_aes_gcm_test_case_1_aad, /* AAD */
  487. GCM_AUTH_TAG_LEN_8, /* */
  488. NULL /* pointer to next testcase */
  489. };
  490. static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1 = {
  491. SRTP_AES_GCM_256_KEY_LEN_WSALT, /* octets in key */
  492. srtp_aes_gcm_test_case_1_key, /* key */
  493. srtp_aes_gcm_test_case_1_iv, /* packet index */
  494. 60, /* octets in plaintext */
  495. srtp_aes_gcm_test_case_1_plaintext, /* plaintext */
  496. 76, /* octets in ciphertext */
  497. srtp_aes_gcm_test_case_1_ciphertext, /* ciphertext + tag */
  498. 20, /* octets in AAD */
  499. srtp_aes_gcm_test_case_1_aad, /* AAD */
  500. GCM_AUTH_TAG_LEN, /* */
  501. &srtp_aes_gcm_test_case_1a /* pointer to next testcase */
  502. };
  503. /*
  504. * This is the vector function table for this crypto engine.
  505. */
  506. /* clang-format off */
  507. const srtp_cipher_type_t srtp_aes_gcm_128 = {
  508. srtp_aes_gcm_nss_alloc,
  509. srtp_aes_gcm_nss_dealloc,
  510. srtp_aes_gcm_nss_context_init,
  511. srtp_aes_gcm_nss_set_aad,
  512. srtp_aes_gcm_nss_encrypt,
  513. srtp_aes_gcm_nss_decrypt,
  514. srtp_aes_gcm_nss_set_iv,
  515. srtp_aes_gcm_nss_get_tag,
  516. srtp_aes_gcm_128_nss_description,
  517. &srtp_aes_gcm_test_case_0,
  518. SRTP_AES_GCM_128
  519. };
  520. /* clang-format on */
  521. /*
  522. * This is the vector function table for this crypto engine.
  523. */
  524. /* clang-format off */
  525. const srtp_cipher_type_t srtp_aes_gcm_256 = {
  526. srtp_aes_gcm_nss_alloc,
  527. srtp_aes_gcm_nss_dealloc,
  528. srtp_aes_gcm_nss_context_init,
  529. srtp_aes_gcm_nss_set_aad,
  530. srtp_aes_gcm_nss_encrypt,
  531. srtp_aes_gcm_nss_decrypt,
  532. srtp_aes_gcm_nss_set_iv,
  533. srtp_aes_gcm_nss_get_tag,
  534. srtp_aes_gcm_256_nss_description,
  535. &srtp_aes_gcm_test_case_1,
  536. SRTP_AES_GCM_256
  537. };
  538. /* clang-format on */