Coverage Report

Created: 2026-10-28 07:07

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
7
{
159
7
    return ossl_cipher_gcm_get_ctx_params_list;
160
7
}
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 (in == NULL) {
363
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
364
0
        return 0;
365
0
    }
366
367
0
    if (outsize < inl) {
368
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
369
0
        return 0;
370
0
    }
371
372
0
    if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
373
0
        return 0;
374
0
    return 1;
375
0
}
376
377
int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
378
    size_t outsize)
379
0
{
380
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
381
0
    int i;
382
383
0
    if (!ossl_prov_is_running())
384
0
        return 0;
385
386
0
    i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
387
0
    if (i <= 0)
388
0
        return 0;
389
390
0
    *outl = 0;
391
0
    return 1;
392
0
}
393
394
int ossl_gcm_cipher(void *vctx,
395
    unsigned char *out, size_t *outl, size_t outsize,
396
    const unsigned char *in, size_t inl)
397
0
{
398
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
399
400
0
    if (!ossl_prov_is_running())
401
0
        return 0;
402
403
0
    if (outsize < inl) {
404
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
405
0
        return 0;
406
0
    }
407
408
0
    if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
409
0
        return 0;
410
411
0
    *outl = inl;
412
0
    return 1;
413
0
}
414
415
/*
416
 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
417
 *
418
 * See also 8.2.2 RBG-based construction.
419
 * Random construction consists of a free field (which can be NULL) and a
420
 * random field which will use a DRBG that can return at least 96 bits of
421
 * entropy strength. (The DRBG must be seeded by the FIPS module).
422
 */
423
static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
424
0
{
425
0
    int sz = (int)(ctx->ivlen - offset);
426
427
    /* Must be at least 96 bits */
428
0
    if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
429
0
        return 0;
430
431
    /* Use DRBG to generate random iv */
432
0
    if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0)
433
0
        return 0;
434
0
    ctx->iv_state = IV_STATE_BUFFERED;
435
0
    ctx->iv_gen_rand = 1;
436
0
    return 1;
437
0
}
438
439
/*
440
 * If we try to grab the iv before the update - it wont be generated yet,
441
 * so we generate it here for this case.
442
 */
443
static int on_preupdate_generate_iv(PROV_GCM_CTX *ctx)
444
0
{
445
0
    if (ctx->iv_state == IV_STATE_UNINITIALISED) {
446
0
        if (ctx->tls_aad_len != UNINITIALISED_SIZET)
447
0
            return 0;
448
0
        if (!ctx->enc || !gcm_iv_generate(ctx, 0))
449
0
            return 0;
450
0
    }
451
0
    return 1;
452
0
}
453
454
/* returns 1 on success, 0 on failure with a reason on the error queue */
455
static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
456
    size_t *padlen, const unsigned char *in,
457
    size_t len)
458
0
{
459
0
    size_t olen = 0;
460
0
    int rv = 0;
461
0
    const PROV_GCM_HW *hw = ctx->hw;
462
463
0
    if (ctx->tls_aad_len != UNINITIALISED_SIZET)
464
0
        return gcm_tls_cipher(ctx, out, padlen, in, len);
465
466
0
    if (ctx->key_set == 0 || ctx->iv_state == IV_STATE_FINISHED) {
467
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
468
0
        goto err;
469
0
    }
470
471
    /*
472
     * FIPS requires generation of AES-GCM IV's inside the FIPS module.
473
     * The IV can still be set externally (the security policy will state that
474
     * this is not FIPS compliant). There are some applications
475
     * where setting the IV externally is the only option available.
476
     */
477
0
    if (ctx->iv_state == IV_STATE_UNINITIALISED) {
478
0
        if (ctx->enc == 0 || gcm_iv_generate(ctx, 0) == 0) {
479
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
480
0
            goto err;
481
0
        }
482
0
    }
483
484
0
    if (ctx->iv_state == IV_STATE_BUFFERED) {
485
0
        if (hw->setiv(ctx, ctx->iv, ctx->ivlen) == 0) {
486
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
487
0
            goto err;
488
0
        }
489
0
        ctx->iv_state = IV_STATE_COPIED;
490
0
    }
491
492
0
    if (in != NULL) {
493
        /*  The input is AAD if out is NULL */
494
0
        if (out == NULL) {
495
0
            int rv_aad = hw->aadupdate(ctx, in, len);
496
497
0
            if (rv_aad == -2) { /* AAD after payload */
498
0
                ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER);
499
0
                goto err;
500
0
            }
501
0
            if (rv_aad <= 0) { /* AAD length overflow or other failure */
502
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
503
0
                goto err;
504
0
            }
505
0
        } else {
506
            /* The input is ciphertext OR plaintext */
507
0
            if (hw->cipherupdate(ctx, in, len, out) == 0) {
508
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
509
0
                goto err;
510
0
            }
511
0
        }
512
0
    } else {
513
        /* The tag must be set before actually decrypting data */
514
0
        if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET) {
515
0
            ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
516
0
            goto err;
517
0
        }
518
0
        if (hw->cipherfinal(ctx, ctx->buf) == 0) {
519
0
            if (ctx->enc == 0)
520
0
                ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT);
521
0
            else
522
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
523
0
            goto err;
524
0
        }
525
0
        ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
526
0
        goto finish;
527
0
    }
528
0
    olen = len;
529
0
finish:
530
0
    rv = 1;
531
0
err:
532
0
    *padlen = olen;
533
0
    return rv;
534
0
}
535
536
static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
537
0
{
538
0
    unsigned char *buf;
539
0
    size_t len;
540
541
0
    if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN)
542
0
        return 0;
543
544
    /* Save the aad for later use. */
545
0
    buf = dat->buf;
546
0
    memcpy(buf, aad, aad_len);
547
0
    dat->tls_aad_len = aad_len;
548
549
0
    len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
550
    /* Correct length for explicit iv. */
551
0
    if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
552
0
        return 0;
553
0
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
554
555
    /* If decrypting correct for tag too. */
556
0
    if (!dat->enc) {
557
0
        if (len < EVP_GCM_TLS_TAG_LEN)
558
0
            return 0;
559
0
        len -= EVP_GCM_TLS_TAG_LEN;
560
0
    }
561
0
    buf[aad_len - 2] = (unsigned char)(len >> 8);
562
0
    buf[aad_len - 1] = (unsigned char)(len & 0xff);
563
    /* Extra padding: tag appended to record. */
564
0
    return EVP_GCM_TLS_TAG_LEN;
565
0
}
566
567
static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
568
    size_t len)
569
0
{
570
    /* Special case: -1 length restores whole IV */
571
0
    if (len == (size_t)-1) {
572
0
        memcpy(ctx->iv, iv, ctx->ivlen);
573
0
        ctx->iv_gen_rand = 0;
574
0
        ctx->iv_gen = 1;
575
0
        ctx->iv_state = IV_STATE_BUFFERED;
576
0
        return 1;
577
0
    }
578
    /* Fixed field must be at least 4 bytes and invocation field at least 8 */
579
0
    if (len < EVP_GCM_TLS_FIXED_IV_LEN
580
0
        || len > ctx->ivlen
581
0
        || (ctx->ivlen - len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
582
0
        return 0;
583
0
    if (len > 0)
584
0
        memcpy(ctx->iv, iv, len);
585
0
    if (ctx->enc) {
586
0
        if (RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0)
587
0
            return 0;
588
0
        ctx->iv_gen_rand = 1;
589
0
    }
590
0
    ctx->iv_gen = 1;
591
0
    ctx->iv_state = IV_STATE_BUFFERED;
592
0
    return 1;
593
0
}
594
595
/*
596
 * Handle TLS GCM packet format. This consists of the last portion of the IV
597
 * followed by the payload and finally the tag. On encrypt generate IV,
598
 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
599
 * and verify tag.
600
 */
601
static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
602
    const unsigned char *in, size_t len)
603
0
{
604
0
    int rv = 0;
605
0
    size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
606
0
    size_t plen = 0;
607
0
    unsigned char *tag = NULL;
608
609
0
    if (ossl_prov_is_running() == 0 || ctx->key_set == 0) {
610
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
611
0
        goto err;
612
0
    }
613
614
    /* Encrypt/decrypt must be performed in place */
615
0
    if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) {
616
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
617
0
        goto err;
618
0
    }
619
620
    /*
621
     * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
622
     * Requirements from SP 800-38D".  The requirements is for one party to the
623
     * communication to fail after 2^64 - 1 keys.  We do this on the encrypting
624
     * side only.
625
     */
626
0
    if (ctx->enc && ++ctx->tls_enc_records == 0) {
627
0
        ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS);
628
0
        goto err;
629
0
    }
630
631
    /*
632
     * Set IV from start of buffer or generate IV and write to start of
633
     * buffer.
634
     */
635
0
    if (ctx->enc) {
636
0
        if (getivgen(ctx, out, arg) == 0) {
637
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
638
0
            goto err;
639
0
        }
640
0
    } else {
641
0
        if (setivinv(ctx, out, arg) == 0) {
642
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
643
0
            goto err;
644
0
        }
645
0
    }
646
647
    /* Fix buffer and length to point to payload */
648
0
    in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
649
0
    out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
650
0
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
651
652
0
    tag = ctx->enc ? out + len : (unsigned char *)in + len;
653
0
    if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
654
0
            EVP_GCM_TLS_TAG_LEN)) {
655
0
        if (!ctx->enc)
656
0
            OPENSSL_cleanse(out, len);
657
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
658
0
        goto err;
659
0
    }
660
0
    if (ctx->enc)
661
0
        plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
662
0
    else
663
0
        plen = len;
664
665
0
    rv = 1;
666
0
err:
667
0
    ctx->iv_state = IV_STATE_FINISHED;
668
0
    ctx->tls_aad_len = UNINITIALISED_SIZET;
669
0
    *padlen = plen;
670
0
    return rv;
671
0
}