ciphercommon_gcm.c 17 KB

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