Coverage Report

Created: 2026-10-16 07:31

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