Coverage Report

Created: 2026-11-16 07:12

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