ciphercommon_gcm.c 16 KB

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