1
0

ciphercommon_gcm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Dispatch functions for gcm mode */
  10. #include <openssl/rand.h>
  11. #include <openssl/proverr.h>
  12. #include "prov/ciphercommon.h"
  13. #include "prov/ciphercommon_gcm.h"
  14. #include "prov/providercommon.h"
  15. #include "prov/provider_ctx.h"
  16. #include "internal/param_names.h"
  17. static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
  18. static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
  19. size_t len);
  20. static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
  21. const unsigned char *in, size_t len);
  22. static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
  23. size_t *padlen, const unsigned char *in,
  24. size_t len);
  25. /*
  26. * Called from EVP_CipherInit when there is currently no context via
  27. * the new_ctx() function
  28. */
  29. void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
  30. const PROV_GCM_HW *hw)
  31. {
  32. ctx->pad = 1;
  33. ctx->mode = EVP_CIPH_GCM_MODE;
  34. ctx->taglen = UNINITIALISED_SIZET;
  35. ctx->tls_aad_len = UNINITIALISED_SIZET;
  36. ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
  37. ctx->keylen = keybits / 8;
  38. ctx->hw = hw;
  39. ctx->libctx = PROV_LIBCTX_OF(provctx);
  40. }
  41. /*
  42. * Called by EVP_CipherInit via the _einit and _dinit functions
  43. */
  44. static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
  45. const unsigned char *iv, size_t ivlen,
  46. const OSSL_PARAM params[], int enc)
  47. {
  48. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  49. if (!ossl_prov_is_running())
  50. return 0;
  51. ctx->enc = enc;
  52. if (iv != NULL) {
  53. if (ivlen == 0 || ivlen > sizeof(ctx->iv)) {
  54. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  55. return 0;
  56. }
  57. ctx->ivlen = ivlen;
  58. memcpy(ctx->iv, iv, ivlen);
  59. ctx->iv_state = IV_STATE_BUFFERED;
  60. }
  61. if (key != NULL) {
  62. if (keylen != ctx->keylen) {
  63. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  64. return 0;
  65. }
  66. if (!ctx->hw->setkey(ctx, key, ctx->keylen))
  67. return 0;
  68. ctx->tls_enc_records = 0;
  69. }
  70. return ossl_gcm_set_ctx_params(ctx, params);
  71. }
  72. int ossl_gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
  73. const unsigned char *iv, size_t ivlen,
  74. const OSSL_PARAM params[])
  75. {
  76. return gcm_init(vctx, key, keylen, iv, ivlen, params, 1);
  77. }
  78. int ossl_gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
  79. const unsigned char *iv, size_t ivlen,
  80. const OSSL_PARAM params[])
  81. {
  82. return gcm_init(vctx, key, keylen, iv, ivlen, params, 0);
  83. }
  84. /* increment counter (64-bit int) by 1 */
  85. static void ctr64_inc(unsigned char *counter)
  86. {
  87. int n = 8;
  88. unsigned char c;
  89. do {
  90. --n;
  91. c = counter[n];
  92. ++c;
  93. counter[n] = c;
  94. if (c > 0)
  95. return;
  96. } while (n > 0);
  97. }
  98. static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen)
  99. {
  100. if (!ctx->iv_gen
  101. || !ctx->key_set
  102. || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
  103. return 0;
  104. if (olen == 0 || olen > ctx->ivlen)
  105. olen = ctx->ivlen;
  106. memcpy(out, ctx->iv + ctx->ivlen - olen, olen);
  107. /*
  108. * Invocation field will be at least 8 bytes in size and so no need
  109. * to check wrap around or increment more than last 8 bytes.
  110. */
  111. ctr64_inc(ctx->iv + ctx->ivlen - 8);
  112. ctx->iv_state = IV_STATE_COPIED;
  113. return 1;
  114. }
  115. static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl)
  116. {
  117. if (!ctx->iv_gen
  118. || !ctx->key_set
  119. || ctx->enc)
  120. return 0;
  121. memcpy(ctx->iv + ctx->ivlen - inl, in, inl);
  122. if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
  123. return 0;
  124. ctx->iv_state = IV_STATE_COPIED;
  125. return 1;
  126. }
  127. int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
  128. {
  129. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  130. OSSL_PARAM *p;
  131. size_t sz;
  132. int type;
  133. for (p = params; p->key != NULL; p++) {
  134. type = ossl_param_find_pidx(p->key);
  135. switch (type) {
  136. default:
  137. break;
  138. case PIDX_CIPHER_PARAM_IVLEN:
  139. if (!OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
  140. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  141. return 0;
  142. }
  143. break;
  144. case PIDX_CIPHER_PARAM_KEYLEN:
  145. if (!OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  146. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  147. return 0;
  148. }
  149. break;
  150. case PIDX_CIPHER_PARAM_AEAD_TAGLEN:
  151. {
  152. size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen :
  153. GCM_TAG_MAX_SIZE;
  154. if (!OSSL_PARAM_set_size_t(p, taglen)) {
  155. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  156. return 0;
  157. }
  158. }
  159. break;
  160. case PIDX_CIPHER_PARAM_IV:
  161. if (ctx->iv_state == IV_STATE_UNINITIALISED)
  162. return 0;
  163. if (ctx->ivlen > p->data_size) {
  164. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  165. return 0;
  166. }
  167. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)
  168. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) {
  169. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  170. return 0;
  171. }
  172. break;
  173. case PIDX_CIPHER_PARAM_UPDATED_IV:
  174. if (ctx->iv_state == IV_STATE_UNINITIALISED)
  175. return 0;
  176. if (ctx->ivlen > p->data_size) {
  177. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  178. return 0;
  179. }
  180. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)
  181. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) {
  182. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  183. return 0;
  184. }
  185. break;
  186. case PIDX_CIPHER_PARAM_AEAD_TLS1_AAD_PAD:
  187. if (!OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
  188. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  189. return 0;
  190. }
  191. break;
  192. case PIDX_CIPHER_PARAM_AEAD_TAG:
  193. sz = p->data_size;
  194. if (sz == 0
  195. || sz > EVP_GCM_TLS_TAG_LEN
  196. || !ctx->enc
  197. || ctx->taglen == UNINITIALISED_SIZET) {
  198. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
  199. return 0;
  200. }
  201. if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
  202. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  203. return 0;
  204. }
  205. break;
  206. case PIDX_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN:
  207. if (p->data == NULL
  208. || p->data_type != OSSL_PARAM_OCTET_STRING
  209. || !getivgen(ctx, p->data, p->data_size))
  210. return 0;
  211. break;
  212. case PIDX_CIPHER_PARAM_AEAD_IV_GENERATED:
  213. if (!OSSL_PARAM_set_uint(p, ctx->iv_gen_rand))
  214. return 0;
  215. }
  216. }
  217. return 1;
  218. }
  219. int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  220. {
  221. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  222. const OSSL_PARAM *p;
  223. size_t sz;
  224. void *vp;
  225. int type;
  226. if (ossl_param_is_empty(params))
  227. return 1;
  228. for (p = params; p->key != NULL; p++) {
  229. type = ossl_param_find_pidx(p->key);
  230. switch (type) {
  231. default:
  232. break;
  233. case PIDX_CIPHER_PARAM_AEAD_TAG:
  234. vp = ctx->buf;
  235. if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
  236. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  237. return 0;
  238. }
  239. if (sz == 0 || ctx->enc) {
  240. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
  241. return 0;
  242. }
  243. ctx->taglen = sz;
  244. break;
  245. case PIDX_CIPHER_PARAM_AEAD_IVLEN:
  246. if (!OSSL_PARAM_get_size_t(p, &sz)) {
  247. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  248. return 0;
  249. }
  250. if (sz == 0 || sz > sizeof(ctx->iv)) {
  251. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  252. return 0;
  253. }
  254. if (ctx->ivlen != sz) {
  255. /* If the iv was already set or autogenerated, it is invalid. */
  256. if (ctx->iv_state != IV_STATE_UNINITIALISED)
  257. ctx->iv_state = IV_STATE_FINISHED;
  258. ctx->ivlen = sz;
  259. }
  260. break;
  261. case PIDX_CIPHER_PARAM_AEAD_TLS1_AAD:
  262. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  263. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  264. return 0;
  265. }
  266. sz = gcm_tls_init(ctx, p->data, p->data_size);
  267. if (sz == 0) {
  268. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
  269. return 0;
  270. }
  271. ctx->tls_aad_pad_sz = sz;
  272. break;
  273. case PIDX_CIPHER_PARAM_AEAD_TLS1_IV_FIXED:
  274. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  275. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  276. return 0;
  277. }
  278. if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
  279. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  280. return 0;
  281. }
  282. break;
  283. case PIDX_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV:
  284. if (p->data == NULL
  285. || p->data_type != OSSL_PARAM_OCTET_STRING
  286. || !setivinv(ctx, p->data, p->data_size))
  287. return 0;
  288. break;
  289. }
  290. }
  291. return 1;
  292. }
  293. int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
  294. size_t outsize, const unsigned char *in, size_t inl)
  295. {
  296. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  297. if (inl == 0) {
  298. *outl = 0;
  299. return 1;
  300. }
  301. if (outsize < inl) {
  302. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  303. return 0;
  304. }
  305. if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
  306. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  307. return 0;
  308. }
  309. return 1;
  310. }
  311. int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
  312. size_t outsize)
  313. {
  314. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  315. int i;
  316. if (!ossl_prov_is_running())
  317. return 0;
  318. i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
  319. if (i <= 0)
  320. return 0;
  321. *outl = 0;
  322. return 1;
  323. }
  324. int ossl_gcm_cipher(void *vctx,
  325. unsigned char *out, size_t *outl, size_t outsize,
  326. const unsigned char *in, size_t inl)
  327. {
  328. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  329. if (!ossl_prov_is_running())
  330. return 0;
  331. if (outsize < inl) {
  332. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  333. return 0;
  334. }
  335. if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
  336. return 0;
  337. *outl = inl;
  338. return 1;
  339. }
  340. /*
  341. * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
  342. *
  343. * See also 8.2.2 RBG-based construction.
  344. * Random construction consists of a free field (which can be NULL) and a
  345. * random field which will use a DRBG that can return at least 96 bits of
  346. * entropy strength. (The DRBG must be seeded by the FIPS module).
  347. */
  348. static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
  349. {
  350. int sz = ctx->ivlen - offset;
  351. /* Must be at least 96 bits */
  352. if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
  353. return 0;
  354. /* Use DRBG to generate random iv */
  355. if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0)
  356. return 0;
  357. ctx->iv_state = IV_STATE_BUFFERED;
  358. ctx->iv_gen_rand = 1;
  359. return 1;
  360. }
  361. static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
  362. size_t *padlen, const unsigned char *in,
  363. size_t len)
  364. {
  365. size_t olen = 0;
  366. int rv = 0;
  367. const PROV_GCM_HW *hw = ctx->hw;
  368. if (ctx->tls_aad_len != UNINITIALISED_SIZET)
  369. return gcm_tls_cipher(ctx, out, padlen, in, len);
  370. if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
  371. goto err;
  372. /*
  373. * FIPS requires generation of AES-GCM IV's inside the FIPS module.
  374. * The IV can still be set externally (the security policy will state that
  375. * this is not FIPS compliant). There are some applications
  376. * where setting the IV externally is the only option available.
  377. */
  378. if (ctx->iv_state == IV_STATE_UNINITIALISED) {
  379. if (!ctx->enc || !gcm_iv_generate(ctx, 0))
  380. goto err;
  381. }
  382. if (ctx->iv_state == IV_STATE_BUFFERED) {
  383. if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
  384. goto err;
  385. ctx->iv_state = IV_STATE_COPIED;
  386. }
  387. if (in != NULL) {
  388. /* The input is AAD if out is NULL */
  389. if (out == NULL) {
  390. if (!hw->aadupdate(ctx, in, len))
  391. goto err;
  392. } else {
  393. /* The input is ciphertext OR plaintext */
  394. if (!hw->cipherupdate(ctx, in, len, out))
  395. goto err;
  396. }
  397. } else {
  398. /* The tag must be set before actually decrypting data */
  399. if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
  400. goto err;
  401. if (!hw->cipherfinal(ctx, ctx->buf))
  402. goto err;
  403. ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
  404. goto finish;
  405. }
  406. olen = len;
  407. finish:
  408. rv = 1;
  409. err:
  410. *padlen = olen;
  411. return rv;
  412. }
  413. static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
  414. {
  415. unsigned char *buf;
  416. size_t len;
  417. if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN)
  418. return 0;
  419. /* Save the aad for later use. */
  420. buf = dat->buf;
  421. memcpy(buf, aad, aad_len);
  422. dat->tls_aad_len = aad_len;
  423. len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
  424. /* Correct length for explicit iv. */
  425. if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  426. return 0;
  427. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  428. /* If decrypting correct for tag too. */
  429. if (!dat->enc) {
  430. if (len < EVP_GCM_TLS_TAG_LEN)
  431. return 0;
  432. len -= EVP_GCM_TLS_TAG_LEN;
  433. }
  434. buf[aad_len - 2] = (unsigned char)(len >> 8);
  435. buf[aad_len - 1] = (unsigned char)(len & 0xff);
  436. /* Extra padding: tag appended to record. */
  437. return EVP_GCM_TLS_TAG_LEN;
  438. }
  439. static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
  440. size_t len)
  441. {
  442. /* Special case: -1 length restores whole IV */
  443. if (len == (size_t)-1) {
  444. memcpy(ctx->iv, iv, ctx->ivlen);
  445. ctx->iv_gen = 1;
  446. ctx->iv_state = IV_STATE_BUFFERED;
  447. return 1;
  448. }
  449. /* Fixed field must be at least 4 bytes and invocation field at least 8 */
  450. if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
  451. || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  452. return 0;
  453. if (len > 0)
  454. memcpy(ctx->iv, iv, len);
  455. if (ctx->enc) {
  456. if (RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0)
  457. return 0;
  458. ctx->iv_gen_rand = 1;
  459. }
  460. ctx->iv_gen = 1;
  461. ctx->iv_state = IV_STATE_BUFFERED;
  462. return 1;
  463. }
  464. /*
  465. * Handle TLS GCM packet format. This consists of the last portion of the IV
  466. * followed by the payload and finally the tag. On encrypt generate IV,
  467. * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
  468. * and verify tag.
  469. */
  470. static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
  471. const unsigned char *in, size_t len)
  472. {
  473. int rv = 0;
  474. size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  475. size_t plen = 0;
  476. unsigned char *tag = NULL;
  477. if (!ossl_prov_is_running() || !ctx->key_set)
  478. goto err;
  479. /* Encrypt/decrypt must be performed in place */
  480. if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
  481. goto err;
  482. /*
  483. * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
  484. * Requirements from SP 800-38D". The requirements is for one party to the
  485. * communication to fail after 2^64 - 1 keys. We do this on the encrypting
  486. * side only.
  487. */
  488. if (ctx->enc && ++ctx->tls_enc_records == 0) {
  489. ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS);
  490. goto err;
  491. }
  492. /*
  493. * Set IV from start of buffer or generate IV and write to start of
  494. * buffer.
  495. */
  496. if (ctx->enc) {
  497. if (!getivgen(ctx, out, arg))
  498. goto err;
  499. } else {
  500. if (!setivinv(ctx, out, arg))
  501. goto err;
  502. }
  503. /* Fix buffer and length to point to payload */
  504. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  505. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  506. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  507. tag = ctx->enc ? out + len : (unsigned char *)in + len;
  508. if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
  509. EVP_GCM_TLS_TAG_LEN)) {
  510. if (!ctx->enc)
  511. OPENSSL_cleanse(out, len);
  512. goto err;
  513. }
  514. if (ctx->enc)
  515. plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  516. else
  517. plen = len;
  518. rv = 1;
  519. err:
  520. ctx->iv_state = IV_STATE_FINISHED;
  521. ctx->tls_aad_len = UNINITIALISED_SIZET;
  522. *padlen = plen;
  523. return rv;
  524. }