Coverage Report

Created: 2026-07-16 06:59

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