Coverage Report

Created: 2025-12-31 06:58

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