Coverage Report

Created: 2023-06-08 06:40

/src/openssl/providers/implementations/ciphers/ciphercommon_gcm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2021 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
0
{
36
0
    ctx->pad = 1;
37
0
    ctx->mode = EVP_CIPH_GCM_MODE;
38
0
    ctx->taglen = UNINITIALISED_SIZET;
39
0
    ctx->tls_aad_len = UNINITIALISED_SIZET;
40
0
    ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
41
0
    ctx->keylen = keybits / 8;
42
0
    ctx->hw = hw;
43
0
    ctx->libctx = PROV_LIBCTX_OF(provctx);
44
0
}
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
0
{
53
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
54
55
0
    if (!ossl_prov_is_running())
56
0
        return 0;
57
58
0
    ctx->enc = enc;
59
60
0
    if (iv != NULL) {
61
0
        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
0
        ctx->ivlen = ivlen;
66
0
        memcpy(ctx->iv, iv, ivlen);
67
0
        ctx->iv_state = IV_STATE_BUFFERED;
68
0
    }
69
70
0
    if (key != NULL) {
71
0
        if (keylen != ctx->keylen) {
72
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
73
0
            return 0;
74
0
        }
75
0
        if (!ctx->hw->setkey(ctx, key, ctx->keylen))
76
0
            return 0;
77
0
        ctx->tls_enc_records = 0;
78
0
    }
79
0
    return ossl_gcm_set_ctx_params(ctx, params);
80
0
}
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
0
{
86
0
    return gcm_init(vctx, key, keylen, iv, ivlen, params, 1);
87
0
}
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
0
{
93
0
    return gcm_init(vctx, key, keylen, iv, ivlen, params, 0);
94
0
}
95
96
/* increment counter (64-bit int) by 1 */
97
static void ctr64_inc(unsigned char *counter)
98
0
{
99
0
    int n = 8;
100
0
    unsigned char c;
101
102
0
    do {
103
0
        --n;
104
0
        c = counter[n];
105
0
        ++c;
106
0
        counter[n] = c;
107
0
        if (c > 0)
108
0
            return;
109
0
    } while (n > 0);
110
0
}
111
112
static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen)
113
0
{
114
0
    if (!ctx->iv_gen
115
0
        || !ctx->key_set
116
0
        || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
117
0
        return 0;
118
0
    if (olen == 0 || olen > ctx->ivlen)
119
0
        olen = ctx->ivlen;
120
0
    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
0
    ctr64_inc(ctx->iv + ctx->ivlen - 8);
126
0
    ctx->iv_state = IV_STATE_COPIED;
127
0
    return 1;
128
0
}
129
130
static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl)
131
0
{
132
0
    if (!ctx->iv_gen
133
0
        || !ctx->key_set
134
0
        || ctx->enc)
135
0
        return 0;
136
137
0
    memcpy(ctx->iv + ctx->ivlen - inl, in, inl);
138
0
    if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
139
0
        return 0;
140
0
    ctx->iv_state = IV_STATE_COPIED;
141
0
    return 1;
142
0
}
143
144
int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
145
0
{
146
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
147
0
    OSSL_PARAM *p;
148
0
    size_t sz;
149
0
    int type;
150
151
0
    for (p = params; p->key != NULL; p++) {
152
0
        type = ossl_param_find_pidx(p->key);
153
0
        switch (type) {
154
0
        default:
155
0
            break;
156
157
0
        case PIDX_CIPHER_PARAM_IVLEN:
158
0
            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
0
            break;
163
164
0
        case PIDX_CIPHER_PARAM_KEYLEN:
165
0
            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
0
            break;
170
171
0
        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
0
        case PIDX_CIPHER_PARAM_AEAD_TLS1_AAD_PAD:
212
0
            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
0
            break;
217
218
0
        case PIDX_CIPHER_PARAM_AEAD_TAG:
219
0
            sz = p->data_size;
220
0
            if (sz == 0
221
0
                || sz > EVP_GCM_TLS_TAG_LEN
222
0
                || !ctx->enc
223
0
                || ctx->taglen == UNINITIALISED_SIZET) {
224
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
225
0
                return 0;
226
0
            }
227
0
            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
0
            break;
232
233
0
        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
0
        }
240
0
    }
241
0
    return 1;
242
0
}
243
244
int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
245
0
{
246
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
247
0
    const OSSL_PARAM *p;
248
0
    size_t sz;
249
0
    void *vp;
250
0
    int type;
251
252
0
    if (params == NULL)
253
0
        return 1;
254
255
0
    for (p = params; p->key != NULL; p++) {
256
0
        type = ossl_param_find_pidx(p->key);
257
0
        switch (type) {
258
0
        default:
259
0
            break;
260
261
0
        case PIDX_CIPHER_PARAM_AEAD_TAG:
262
0
            vp = ctx->buf;
263
0
            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
0
            if (sz == 0 || ctx->enc) {
268
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
269
0
                return 0;
270
0
            }
271
0
            ctx->taglen = sz;
272
0
            break;
273
274
0
        case PIDX_CIPHER_PARAM_AEAD_IVLEN:
275
0
            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
0
            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
0
            ctx->ivlen = sz;
284
0
            break;
285
286
0
        case PIDX_CIPHER_PARAM_AEAD_TLS1_AAD:
287
0
            if (p->data_type != OSSL_PARAM_OCTET_STRING) {
288
0
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
289
0
                return 0;
290
0
            }
291
0
            sz = gcm_tls_init(ctx, p->data, p->data_size);
292
0
            if (sz == 0) {
293
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
294
0
                return 0;
295
0
            }
296
0
            ctx->tls_aad_pad_sz = sz;
297
0
            break;
298
299
0
        case PIDX_CIPHER_PARAM_AEAD_TLS1_IV_FIXED:
300
0
            if (p->data_type != OSSL_PARAM_OCTET_STRING) {
301
0
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
302
0
                return 0;
303
0
            }
304
0
            if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
305
0
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
306
0
                return 0;
307
0
            }
308
0
            break;
309
310
0
        case PIDX_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV:
311
0
            if (p->data == NULL
312
0
                || p->data_type != OSSL_PARAM_OCTET_STRING
313
0
                || !setivinv(ctx, p->data, p->data_size))
314
0
                return 0;
315
0
            break;
316
0
        }
317
0
    }
318
319
0
    return 1;
320
0
}
321
322
int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
323
                           size_t outsize, const unsigned char *in, size_t inl)
324
0
{
325
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
326
327
0
    if (inl == 0) {
328
0
        *outl = 0;
329
0
        return 1;
330
0
    }
331
332
0
    if (outsize < inl) {
333
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
334
0
        return 0;
335
0
    }
336
337
0
    if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
338
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
339
0
        return 0;
340
0
    }
341
0
    return 1;
342
0
}
343
344
int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
345
                          size_t outsize)
346
0
{
347
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
348
0
    int i;
349
350
0
    if (!ossl_prov_is_running())
351
0
        return 0;
352
353
0
    i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
354
0
    if (i <= 0)
355
0
        return 0;
356
357
0
    *outl = 0;
358
0
    return 1;
359
0
}
360
361
int ossl_gcm_cipher(void *vctx,
362
                    unsigned char *out, size_t *outl, size_t outsize,
363
                    const unsigned char *in, size_t inl)
364
0
{
365
0
    PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
366
367
0
    if (!ossl_prov_is_running())
368
0
        return 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
378
0
    *outl = inl;
379
0
    return 1;
380
0
}
381
382
/*
383
 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
384
 *
385
 * See also 8.2.2 RBG-based construction.
386
 * Random construction consists of a free field (which can be NULL) and a
387
 * random field which will use a DRBG that can return at least 96 bits of
388
 * entropy strength. (The DRBG must be seeded by the FIPS module).
389
 */
390
static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
391
0
{
392
0
    int sz = ctx->ivlen - offset;
393
394
    /* Must be at least 96 bits */
395
0
    if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
396
0
        return 0;
397
398
    /* Use DRBG to generate random iv */
399
0
    if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0)
400
0
        return 0;
401
0
    ctx->iv_state = IV_STATE_BUFFERED;
402
0
    ctx->iv_gen_rand = 1;
403
0
    return 1;
404
0
}
405
406
static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
407
                               size_t *padlen, const unsigned char *in,
408
                               size_t len)
409
0
{
410
0
    size_t olen = 0;
411
0
    int rv = 0;
412
0
    const PROV_GCM_HW *hw = ctx->hw;
413
414
0
    if (ctx->tls_aad_len != UNINITIALISED_SIZET)
415
0
        return gcm_tls_cipher(ctx, out, padlen, in, len);
416
417
0
    if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
418
0
        goto err;
419
420
    /*
421
     * FIPS requires generation of AES-GCM IV's inside the FIPS module.
422
     * The IV can still be set externally (the security policy will state that
423
     * this is not FIPS compliant). There are some applications
424
     * where setting the IV externally is the only option available.
425
     */
426
0
    if (ctx->iv_state == IV_STATE_UNINITIALISED) {
427
0
        if (!ctx->enc || !gcm_iv_generate(ctx, 0))
428
0
            goto err;
429
0
    }
430
431
0
    if (ctx->iv_state == IV_STATE_BUFFERED) {
432
0
        if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
433
0
            goto err;
434
0
        ctx->iv_state = IV_STATE_COPIED;
435
0
    }
436
437
0
    if (in != NULL) {
438
        /*  The input is AAD if out is NULL */
439
0
        if (out == NULL) {
440
0
            if (!hw->aadupdate(ctx, in, len))
441
0
                goto err;
442
0
        } else {
443
            /* The input is ciphertext OR plaintext */
444
0
            if (!hw->cipherupdate(ctx, in, len, out))
445
0
                goto err;
446
0
        }
447
0
    } else {
448
        /* The tag must be set before actually decrypting data */
449
0
        if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
450
0
            goto err;
451
0
        if (!hw->cipherfinal(ctx, ctx->buf))
452
0
            goto err;
453
0
        ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
454
0
        goto finish;
455
0
    }
456
0
    olen = len;
457
0
finish:
458
0
    rv = 1;
459
0
err:
460
0
    *padlen = olen;
461
0
    return rv;
462
0
}
463
464
static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
465
0
{
466
0
    unsigned char *buf;
467
0
    size_t len;
468
469
0
    if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN)
470
0
       return 0;
471
472
    /* Save the aad for later use. */
473
0
    buf = dat->buf;
474
0
    memcpy(buf, aad, aad_len);
475
0
    dat->tls_aad_len = aad_len;
476
477
0
    len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
478
    /* Correct length for explicit iv. */
479
0
    if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
480
0
        return 0;
481
0
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
482
483
    /* If decrypting correct for tag too. */
484
0
    if (!dat->enc) {
485
0
        if (len < EVP_GCM_TLS_TAG_LEN)
486
0
            return 0;
487
0
        len -= EVP_GCM_TLS_TAG_LEN;
488
0
    }
489
0
    buf[aad_len - 2] = (unsigned char)(len >> 8);
490
0
    buf[aad_len - 1] = (unsigned char)(len & 0xff);
491
    /* Extra padding: tag appended to record. */
492
0
    return EVP_GCM_TLS_TAG_LEN;
493
0
}
494
495
static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
496
                                size_t len)
497
0
{
498
    /* Special case: -1 length restores whole IV */
499
0
    if (len == (size_t)-1) {
500
0
        memcpy(ctx->iv, iv, ctx->ivlen);
501
0
        ctx->iv_gen = 1;
502
0
        ctx->iv_state = IV_STATE_BUFFERED;
503
0
        return 1;
504
0
    }
505
    /* Fixed field must be at least 4 bytes and invocation field at least 8 */
506
0
    if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
507
0
        || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
508
0
            return 0;
509
0
    if (len > 0)
510
0
        memcpy(ctx->iv, iv, len);
511
0
    if (ctx->enc
512
0
        && RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0)
513
0
            return 0;
514
0
    ctx->iv_gen = 1;
515
0
    ctx->iv_state = IV_STATE_BUFFERED;
516
0
    return 1;
517
0
}
518
519
/*
520
 * Handle TLS GCM packet format. This consists of the last portion of the IV
521
 * followed by the payload and finally the tag. On encrypt generate IV,
522
 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
523
 * and verify tag.
524
 */
525
static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
526
                          const unsigned char *in, size_t len)
527
0
{
528
0
    int rv = 0;
529
0
    size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
530
0
    size_t plen = 0;
531
0
    unsigned char *tag = NULL;
532
533
0
    if (!ossl_prov_is_running() || !ctx->key_set)
534
0
        goto err;
535
536
    /* Encrypt/decrypt must be performed in place */
537
0
    if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
538
0
        goto err;
539
540
    /*
541
     * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
542
     * Requirements from SP 800-38D".  The requirements is for one party to the
543
     * communication to fail after 2^64 - 1 keys.  We do this on the encrypting
544
     * side only.
545
     */
546
0
    if (ctx->enc && ++ctx->tls_enc_records == 0) {
547
0
        ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS);
548
0
        goto err;
549
0
    }
550
551
    /*
552
     * Set IV from start of buffer or generate IV and write to start of
553
     * buffer.
554
     */
555
0
    if (ctx->enc) {
556
0
        if (!getivgen(ctx, out, arg))
557
0
            goto err;
558
0
    } else {
559
0
        if (!setivinv(ctx, out, arg))
560
0
            goto err;
561
0
    }
562
563
    /* Fix buffer and length to point to payload */
564
0
    in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
565
0
    out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
566
0
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
567
568
0
    tag = ctx->enc ? out + len : (unsigned char *)in + len;
569
0
    if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
570
0
                          EVP_GCM_TLS_TAG_LEN)) {
571
0
        if (!ctx->enc)
572
0
            OPENSSL_cleanse(out, len);
573
0
        goto err;
574
0
    }
575
0
    if (ctx->enc)
576
0
        plen =  len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
577
0
    else
578
0
        plen = len;
579
580
0
    rv = 1;
581
0
err:
582
0
    ctx->iv_state = IV_STATE_FINISHED;
583
0
    ctx->tls_aad_len = UNINITIALISED_SIZET;
584
0
    *padlen = plen;
585
0
    return rv;
586
0
}