Coverage Report

Created: 2025-11-24 06:26

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