Coverage Report

Created: 2024-07-27 06:39

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