Coverage Report

Created: 2025-10-28 06:56

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