Coverage Report

Created: 2026-09-13 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/ciphercommon.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
/*
11
 * Generic dispatch table functions for ciphers.
12
 */
13
14
/* For SSL3_VERSION */
15
#include <string.h>
16
#include <openssl/prov_ssl.h>
17
#include <openssl/proverr.h>
18
#include "ciphercommon_local.h"
19
#include "prov/provider_ctx.h"
20
#include "prov/providercommon.h"
21
#include "internal/skey.h"
22
#include "internal/e_os.h"
23
#include "crypto/types.h"
24
25
#define ossl_cipher_generic_get_params_st ossl_cipher_get_param_list_st
26
#define cipher_generic_get_ctx_params_st ossl_cipher_get_ctx_param_list_st
27
#define cipher_generic_set_ctx_params_st ossl_cipher_set_ctx_param_list_st
28
#define cipher_var_keylen_set_ctx_params_st ossl_cipher_set_ctx_param_list_st
29
30
#include "providers/implementations/ciphers/ciphercommon.inc"
31
32
/*-
33
 * Generic cipher functions for OSSL_PARAM gettables and settables
34
 */
35
36
const OSSL_PARAM *ossl_cipher_generic_gettable_params(ossl_unused void *provctx)
37
0
{
38
0
    return ossl_cipher_generic_get_params_list;
39
0
}
40
41
int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
42
    uint64_t flags,
43
    size_t kbits, size_t blkbits, size_t ivbits)
44
147
{
45
147
    struct ossl_cipher_get_param_list_st p;
46
47
147
    if (!ossl_cipher_generic_get_params_decoder(params, &p))
48
0
        return 0;
49
50
147
    return ossl_cipher_common_get_params(&p, md, flags, kbits, blkbits,
51
147
        ivbits);
52
147
}
53
54
int ossl_cipher_common_get_params(const struct ossl_cipher_get_param_list_st *p,
55
    unsigned int md, uint64_t flags, size_t kbits, size_t blkbits,
56
    size_t ivbits)
57
158
{
58
158
    if (p->mode != NULL && !OSSL_PARAM_set_uint(p->mode, md)) {
59
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
60
0
        return 0;
61
0
    }
62
158
    if (p->aead != NULL
63
147
        && !OSSL_PARAM_set_int(p->aead, (flags & PROV_CIPHER_FLAG_AEAD) != 0)) {
64
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
65
0
        return 0;
66
0
    }
67
158
    if (p->custiv != NULL
68
158
        && !OSSL_PARAM_set_int(p->custiv,
69
158
            (flags & PROV_CIPHER_FLAG_CUSTOM_IV) != 0)) {
70
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
71
0
        return 0;
72
0
    }
73
158
    if (p->cts != NULL
74
147
        && !OSSL_PARAM_set_int(p->cts, (flags & PROV_CIPHER_FLAG_CTS) != 0)) {
75
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
76
0
        return 0;
77
0
    }
78
158
    if (p->mb != NULL
79
147
        && !OSSL_PARAM_set_int(p->mb,
80
147
            (flags & PROV_CIPHER_FLAG_TLS1_MULTIBLOCK) != 0)) {
81
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
82
0
        return 0;
83
0
    }
84
158
    if (p->rand != NULL
85
158
        && !OSSL_PARAM_set_int(p->rand,
86
158
            (flags & PROV_CIPHER_FLAG_RAND_KEY) != 0)) {
87
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
88
0
        return 0;
89
0
    }
90
158
    if (p->etm != NULL
91
147
        && !OSSL_PARAM_set_int(p->etm,
92
147
            (flags & EVP_CIPH_FLAG_ENC_THEN_MAC) != 0)) {
93
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
94
0
        return 0;
95
0
    }
96
158
    if (p->keylen != NULL
97
158
        && !OSSL_PARAM_set_size_t(p->keylen, kbits / 8)) {
98
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
99
0
        return 0;
100
0
    }
101
158
    if (p->bsize != NULL
102
158
        && !OSSL_PARAM_set_size_t(p->bsize, blkbits / 8)) {
103
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
104
0
        return 0;
105
0
    }
106
158
    if (p->ivlen != NULL
107
158
        && !OSSL_PARAM_set_size_t(p->ivlen, ivbits / 8)) {
108
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
109
0
        return 0;
110
0
    }
111
158
    return 1;
112
158
}
113
114
const OSSL_PARAM *ossl_cipher_generic_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
115
101
{
116
101
    return cipher_generic_get_ctx_params_list;
117
101
}
118
119
const OSSL_PARAM *ossl_cipher_generic_settable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
120
0
{
121
0
    return cipher_generic_set_ctx_params_list;
122
0
}
123
124
/*
125
 * Variable key length cipher functions for OSSL_PARAM settables
126
 */
127
128
const OSSL_PARAM *ossl_cipher_var_keylen_settable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
129
0
{
130
0
    return cipher_var_keylen_set_ctx_params_list;
131
0
}
132
133
int ossl_cipher_var_keylen_set_ctx_params(void *vctx, const OSSL_PARAM params[])
134
0
{
135
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
136
0
    struct ossl_cipher_set_ctx_param_list_st p;
137
138
0
    if (ctx == NULL || !cipher_var_keylen_set_ctx_params_decoder(params, &p))
139
0
        return 0;
140
0
    return ossl_cipher_common_set_ctx_params(ctx, &p);
141
0
}
142
143
void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx)
144
465
{
145
465
    if (ctx != NULL && ctx->alloced) {
146
0
        OPENSSL_free(ctx->tlsmac);
147
0
        ctx->alloced = 0;
148
0
        ctx->tlsmac = NULL;
149
0
    }
150
465
}
151
152
/*
153
 * Deep-copy the tlsmac buffer after a shallow dupctx copy.
154
 * Must be called after OPENSSL_memdup or *dctx = *sctx to avoid
155
 * double-free when both contexts are freed.
156
 * Returns 1 on success, 0 on allocation failure.
157
 */
158
int ossl_cipher_generic_dupctx_tlsmac(PROV_CIPHER_CTX *dst,
159
    const PROV_CIPHER_CTX *src)
160
0
{
161
0
    if (src->tlsmac != NULL && src->alloced) {
162
0
        dst->tlsmac = OPENSSL_memdup(src->tlsmac, src->tlsmacsize);
163
0
        if (dst->tlsmac == NULL) {
164
0
            dst->alloced = 0;
165
0
            return 0;
166
0
        }
167
0
    }
168
0
    return 1;
169
0
}
170
171
static int cipher_generic_init_internal(PROV_CIPHER_CTX *ctx,
172
    const unsigned char *key, size_t keylen,
173
    const unsigned char *iv, size_t ivlen,
174
    const OSSL_PARAM params[], int enc)
175
0
{
176
0
    ctx->num = 0;
177
0
    ctx->bufsz = 0;
178
0
    ctx->updated = 0;
179
0
    ctx->enc = enc ? 1 : 0;
180
181
0
    if (!ossl_prov_is_running())
182
0
        return 0;
183
184
0
    if (iv != NULL && ctx->mode != EVP_CIPH_ECB_MODE) {
185
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
186
0
            return 0;
187
0
    }
188
0
    if (iv == NULL && ctx->iv_set
189
0
        && (ctx->mode == EVP_CIPH_CBC_MODE
190
0
            || ctx->mode == EVP_CIPH_CFB_MODE
191
0
            || ctx->mode == EVP_CIPH_OFB_MODE))
192
        /* reset IV for these modes to keep compatibility with 1.1.1 */
193
0
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
194
195
0
    if (key != NULL) {
196
0
        if (ctx->variable_keylength == 0) {
197
0
            if (keylen != ctx->keylen) {
198
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
199
0
                return 0;
200
0
            }
201
0
        } else {
202
0
            ctx->keylen = keylen;
203
0
        }
204
0
        if (!ctx->hw->init(ctx, key, ctx->keylen))
205
0
            return 0;
206
0
        ctx->key_set = 1;
207
0
    }
208
0
    return ossl_cipher_generic_set_ctx_params(ctx, params);
209
0
}
210
211
int ossl_cipher_generic_einit(void *vctx, const unsigned char *key,
212
    size_t keylen, const unsigned char *iv,
213
    size_t ivlen, const OSSL_PARAM params[])
214
0
{
215
0
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
216
0
        iv, ivlen, params, 1);
217
0
}
218
219
int ossl_cipher_generic_dinit(void *vctx, const unsigned char *key,
220
    size_t keylen, const unsigned char *iv,
221
    size_t ivlen, const OSSL_PARAM params[])
222
0
{
223
0
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
224
0
        iv, ivlen, params, 0);
225
0
}
226
227
int ossl_cipher_generic_skey_einit(void *vctx, void *skeydata,
228
    const unsigned char *iv, size_t ivlen,
229
    const OSSL_PARAM params[])
230
0
{
231
0
    PROV_SKEY *key = skeydata;
232
233
0
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx,
234
0
        key->data, key->length,
235
0
        iv, ivlen, params, 1);
236
0
}
237
238
int ossl_cipher_generic_skey_dinit(void *vctx, void *skeydata,
239
    const unsigned char *iv, size_t ivlen,
240
    const OSSL_PARAM params[])
241
0
{
242
0
    PROV_SKEY *key = skeydata;
243
244
0
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx,
245
0
        key->data, key->length,
246
0
        iv, ivlen, params, 0);
247
0
}
248
249
/* Max padding including padding length byte */
250
0
#define MAX_PADDING 256
251
252
int ossl_cipher_generic_block_update(void *vctx, unsigned char *out,
253
    size_t *outl, size_t outsize,
254
    const unsigned char *in, size_t inl)
255
0
{
256
0
    size_t outlint = 0;
257
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
258
0
    size_t blksz = ctx->blocksize;
259
0
    size_t nextblocks;
260
261
0
    if (!ctx->key_set) {
262
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
263
0
        return 0;
264
0
    }
265
266
0
    if (ctx->tlsversion > 0) {
267
        /*
268
         * Each update call corresponds to a TLS record and is individually
269
         * padded
270
         */
271
272
        /* Sanity check inputs */
273
0
        if (in == NULL
274
0
            || in != out
275
0
            || outsize < inl
276
0
            || !ctx->pad) {
277
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
278
0
            return 0;
279
0
        }
280
281
0
        if (ctx->enc) {
282
0
            unsigned char padval;
283
0
            size_t padnum, loop;
284
285
            /* Add padding */
286
287
0
            padnum = blksz - (inl % blksz);
288
289
0
            if (outsize < inl + padnum) {
290
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
291
0
                return 0;
292
0
            }
293
294
0
            if (padnum > MAX_PADDING) {
295
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
296
0
                return 0;
297
0
            }
298
0
            padval = (unsigned char)(padnum - 1);
299
            /* we need to add 'padnum' padding bytes of value padval */
300
0
            for (loop = inl; loop < inl + padnum; loop++)
301
0
                out[loop] = padval;
302
0
            inl += padnum;
303
0
        }
304
305
0
        if ((inl % blksz) != 0) {
306
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
307
0
            return 0;
308
0
        }
309
310
        /* Shouldn't normally fail */
311
0
        if (!ctx->hw->cipher(ctx, out, in, inl)) {
312
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
313
0
            return 0;
314
0
        }
315
316
0
        if (ctx->alloced) {
317
0
            OPENSSL_free(ctx->tlsmac);
318
0
            ctx->alloced = 0;
319
0
            ctx->tlsmac = NULL;
320
0
        }
321
322
        /* This only fails if padding is publicly invalid */
323
0
        *outl = inl;
324
0
        if (!ctx->enc
325
0
            && !ossl_cipher_tlsunpadblock(ctx->libctx, ctx->tlsversion,
326
0
                out, outl,
327
0
                blksz, &ctx->tlsmac, &ctx->alloced,
328
0
                ctx->tlsmacsize, 0)) {
329
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
330
0
            return 0;
331
0
        }
332
0
        return 1;
333
0
    }
334
335
0
    if (ctx->bufsz != 0)
336
0
        nextblocks = ossl_cipher_fillblock(ctx->buf, &ctx->bufsz, blksz,
337
0
            &in, &inl);
338
0
    else
339
0
        nextblocks = inl & ~(blksz - 1);
340
341
    /*
342
     * If we're decrypting and we end an update on a block boundary we hold
343
     * the last block back in case this is the last update call and the last
344
     * block is padded.
345
     */
346
0
    if (ctx->bufsz == blksz && (ctx->enc || inl > 0 || !ctx->pad)) {
347
0
        if (outsize < blksz) {
348
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
349
0
            return 0;
350
0
        }
351
0
        if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
352
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
353
0
            return 0;
354
0
        }
355
0
        ctx->bufsz = 0;
356
0
        outlint = blksz;
357
0
        out += blksz;
358
0
    }
359
0
    if (nextblocks > 0) {
360
0
        if (!ctx->enc && ctx->pad && nextblocks == inl) {
361
0
            if (!ossl_assert(inl >= blksz)) {
362
0
                ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
363
0
                return 0;
364
0
            }
365
0
            nextblocks -= blksz;
366
0
        }
367
0
        outlint += nextblocks;
368
0
        if (outsize < outlint) {
369
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
370
0
            return 0;
371
0
        }
372
0
    }
373
0
    if (nextblocks > 0) {
374
0
        if (!ctx->hw->cipher(ctx, out, in, nextblocks)) {
375
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
376
0
            return 0;
377
0
        }
378
0
        in += nextblocks;
379
0
        inl -= nextblocks;
380
0
    }
381
0
    if (inl != 0
382
0
        && !ossl_cipher_trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
383
        /* ERR_raise already called */
384
0
        return 0;
385
0
    }
386
387
0
    *outl = outlint;
388
0
    return inl == 0;
389
0
}
390
391
int ossl_cipher_generic_block_final(void *vctx, unsigned char *out,
392
    size_t *outl, size_t outsize)
393
0
{
394
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
395
0
    size_t blksz = ctx->blocksize;
396
397
0
    if (!ossl_prov_is_running())
398
0
        return 0;
399
400
0
    if (!ctx->key_set) {
401
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
402
0
        return 0;
403
0
    }
404
405
0
    if (ctx->tlsversion > 0) {
406
        /* We never finalize TLS, so this is an error */
407
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
408
0
        return 0;
409
0
    }
410
411
0
    if (ctx->enc) {
412
0
        if (ctx->pad) {
413
0
            ossl_cipher_padblock(ctx->buf, &ctx->bufsz, blksz);
414
0
        } else if (ctx->bufsz == 0) {
415
0
            *outl = 0;
416
0
            return 1;
417
0
        } else if (ctx->bufsz != blksz) {
418
0
            ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
419
0
            return 0;
420
0
        }
421
422
0
        if (outsize < blksz) {
423
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
424
0
            return 0;
425
0
        }
426
0
        if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
427
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
428
0
            return 0;
429
0
        }
430
0
        ctx->bufsz = 0;
431
0
        *outl = blksz;
432
0
        return 1;
433
0
    }
434
435
    /* Decrypting */
436
0
    if (ctx->bufsz != blksz) {
437
0
        if (ctx->bufsz == 0 && !ctx->pad) {
438
0
            *outl = 0;
439
0
            return 1;
440
0
        }
441
0
        ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
442
0
        return 0;
443
0
    }
444
445
0
    if (!ctx->hw->cipher(ctx, ctx->buf, ctx->buf, blksz)) {
446
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
447
0
        return 0;
448
0
    }
449
450
0
    if (ctx->pad && !ossl_cipher_unpadblock(ctx->buf, &ctx->bufsz, blksz)) {
451
        /* ERR_raise already called */
452
0
        return 0;
453
0
    }
454
455
0
    if (outsize < ctx->bufsz) {
456
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
457
0
        return 0;
458
0
    }
459
0
    memcpy(out, ctx->buf, ctx->bufsz);
460
0
    *outl = ctx->bufsz;
461
0
    ctx->bufsz = 0;
462
0
    return 1;
463
0
}
464
465
int ossl_cipher_generic_stream_update(void *vctx, unsigned char *out,
466
    size_t *outl, size_t outsize,
467
    const unsigned char *in, size_t inl)
468
0
{
469
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
470
471
0
    if (!ctx->key_set) {
472
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
473
0
        return 0;
474
0
    }
475
476
0
    if (inl == 0) {
477
0
        *outl = 0;
478
0
        return 1;
479
0
    }
480
481
0
    if (outsize < inl) {
482
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
483
0
        return 0;
484
0
    }
485
486
0
    if (!ctx->hw->cipher(ctx, out, in, inl)) {
487
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
488
0
        return 0;
489
0
    }
490
491
0
    *outl = inl;
492
0
    if (!ctx->enc && ctx->tlsversion > 0) {
493
        /*
494
         * Remove any TLS padding. Only used by cipher_aes_cbc_hmac_sha1_hw.c and
495
         * cipher_aes_cbc_hmac_sha256_hw.c
496
         */
497
0
        if (ctx->removetlspad) {
498
            /*
499
             * We should have already failed in the cipher() call above if this
500
             * isn't true.
501
             */
502
0
            if (!ossl_assert(*outl >= (size_t)(out[inl - 1] + 1)))
503
0
                return 0;
504
            /* The actual padding length */
505
0
            *outl -= out[inl - 1] + 1;
506
0
        }
507
508
        /* TLS MAC and explicit IV if relevant. We should have already failed
509
         * in the cipher() call above if *outl is too short.
510
         */
511
0
        if (!ossl_assert(*outl >= ctx->removetlsfixed))
512
0
            return 0;
513
0
        *outl -= ctx->removetlsfixed;
514
515
        /* Extract the MAC if there is one */
516
0
        if (ctx->tlsmacsize > 0) {
517
0
            if (*outl < ctx->tlsmacsize)
518
0
                return 0;
519
520
0
            ctx->tlsmac = out + *outl - ctx->tlsmacsize;
521
0
            *outl -= ctx->tlsmacsize;
522
0
        }
523
0
    }
524
525
0
    return 1;
526
0
}
527
int ossl_cipher_generic_stream_final(void *vctx, unsigned char *out,
528
    size_t *outl, size_t outsize)
529
0
{
530
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
531
532
0
    if (!ossl_prov_is_running())
533
0
        return 0;
534
535
0
    if (!ctx->key_set) {
536
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
537
0
        return 0;
538
0
    }
539
540
0
    *outl = 0;
541
0
    return 1;
542
0
}
543
544
int ossl_cipher_generic_cipher(void *vctx, unsigned char *out, size_t *outl,
545
    size_t outsize, const unsigned char *in,
546
    size_t inl)
547
0
{
548
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
549
550
0
    if (!ossl_prov_is_running())
551
0
        return 0;
552
553
0
    if (!ctx->key_set) {
554
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
555
0
        return 0;
556
0
    }
557
558
0
    if (outsize < inl) {
559
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
560
0
        return 0;
561
0
    }
562
563
0
    if (!ctx->hw->cipher(ctx, out, in, inl)) {
564
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
565
0
        return 0;
566
0
    }
567
568
0
    *outl = inl;
569
0
    return 1;
570
0
}
571
572
int ossl_cipher_common_get_ctx_params(PROV_CIPHER_CTX *ctx, const struct ossl_cipher_get_ctx_param_list_st *p)
573
601
{
574
601
    if (p->ivlen != NULL && !OSSL_PARAM_set_size_t(p->ivlen, ctx->ivlen)) {
575
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
576
0
        return 0;
577
0
    }
578
579
601
    if (p->pad != NULL && !OSSL_PARAM_set_uint(p->pad, ctx->pad)) {
580
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
581
0
        return 0;
582
0
    }
583
584
601
    if (p->iv != NULL
585
0
        && !OSSL_PARAM_set_octet_string_or_ptr(p->iv, ctx->oiv, ctx->ivlen)) {
586
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
587
0
        return 0;
588
0
    }
589
590
601
    if (p->updiv != NULL
591
0
        && !OSSL_PARAM_set_octet_string_or_ptr(p->updiv, ctx->iv, ctx->ivlen)) {
592
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
593
0
        return 0;
594
0
    }
595
596
601
    if (p->num != NULL && !OSSL_PARAM_set_uint(p->num, ctx->num)) {
597
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
598
0
        return 0;
599
0
    }
600
601
601
    if (p->keylen != NULL && !OSSL_PARAM_set_size_t(p->keylen, ctx->keylen)) {
602
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
603
0
        return 0;
604
0
    }
605
606
601
    if (p->tlsmac != NULL
607
0
        && !OSSL_PARAM_set_octet_ptr(p->tlsmac, ctx->tlsmac, ctx->tlsmacsize)) {
608
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
609
0
        return 0;
610
0
    }
611
601
    return 1;
612
601
}
613
614
int ossl_cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
615
601
{
616
601
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
617
601
    struct ossl_cipher_get_ctx_param_list_st p;
618
619
601
    if (ctx == NULL || !cipher_generic_get_ctx_params_decoder(params, &p))
620
0
        return 0;
621
601
    return ossl_cipher_common_get_ctx_params(ctx, &p);
622
601
}
623
624
int ossl_cipher_common_set_ctx_params(PROV_CIPHER_CTX *ctx, const struct ossl_cipher_set_ctx_param_list_st *p)
625
0
{
626
0
    if (p->pad != NULL) {
627
0
        unsigned int pad;
628
629
0
        if (!OSSL_PARAM_get_uint(p->pad, &pad)) {
630
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
631
0
            return 0;
632
0
        }
633
0
        ctx->pad = pad ? 1 : 0;
634
0
    }
635
636
0
    if (p->bits != NULL) {
637
0
        unsigned int bits;
638
639
0
        if (!OSSL_PARAM_get_uint(p->bits, &bits)) {
640
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
641
0
            return 0;
642
0
        }
643
0
        ctx->use_bits = bits ? 1 : 0;
644
0
    }
645
646
0
    if (p->tlsvers != NULL) {
647
0
        if (!OSSL_PARAM_get_uint(p->tlsvers, &ctx->tlsversion)) {
648
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
649
0
            return 0;
650
0
        }
651
0
    }
652
653
0
    if (p->tlsmacsize != NULL) {
654
0
        if (!OSSL_PARAM_get_size_t(p->tlsmacsize, &ctx->tlsmacsize)) {
655
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
656
0
            return 0;
657
0
        }
658
0
    }
659
660
0
    if (p->num != NULL) {
661
0
        unsigned int num;
662
663
0
        if (!OSSL_PARAM_get_uint(p->num, &num)) {
664
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
665
0
            return 0;
666
0
        }
667
0
        if (ctx->blocksize > 0 && num >= (unsigned int)ctx->blocksize) {
668
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
669
0
            return 0;
670
0
        }
671
0
        ctx->num = num;
672
0
    }
673
674
0
    if (p->keylen != NULL) {
675
0
        size_t keylen;
676
677
0
        if (!OSSL_PARAM_get_size_t(p->keylen, &keylen)) {
678
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
679
0
            return 0;
680
0
        }
681
0
        if (ctx->keylen != keylen) {
682
0
            ctx->keylen = keylen;
683
0
            ctx->key_set = 0;
684
0
        }
685
0
    }
686
0
    return 1;
687
0
}
688
689
int ossl_cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[])
690
0
{
691
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
692
0
    struct ossl_cipher_set_ctx_param_list_st p;
693
694
0
    if (ossl_param_is_empty(params))
695
0
        return 1;
696
697
0
    if (ctx == NULL || !cipher_generic_set_ctx_params_decoder(params, &p))
698
0
        return 0;
699
0
    return ossl_cipher_common_set_ctx_params(ctx, &p);
700
0
}
701
702
int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
703
    size_t ivlen)
704
136
{
705
136
    if (ivlen != ctx->ivlen
706
136
        || ivlen > sizeof(ctx->iv)) {
707
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
708
0
        return 0;
709
0
    }
710
136
    ctx->iv_set = 1;
711
136
    memcpy(ctx->iv, iv, ivlen);
712
136
    memcpy(ctx->oiv, iv, ivlen);
713
136
    return 1;
714
136
}
715
716
void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
717
    size_t ivbits, unsigned int mode,
718
    uint64_t flags, const PROV_CIPHER_HW *hw,
719
    void *provctx)
720
465
{
721
465
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
722
723
465
    if ((flags & PROV_CIPHER_FLAG_INVERSE_CIPHER) != 0)
724
0
        ctx->inverse_cipher = 1;
725
465
    if ((flags & PROV_CIPHER_FLAG_VARIABLE_LENGTH) != 0)
726
0
        ctx->variable_keylength = 1;
727
728
465
    ctx->pad = 1;
729
465
    ctx->keylen = ((kbits) / 8);
730
465
    ctx->ivlen = ((ivbits) / 8);
731
465
    ctx->hw = hw;
732
465
    ctx->mode = mode;
733
465
    ctx->blocksize = blkbits / 8;
734
465
    if (provctx != NULL)
735
0
        ctx->libctx = PROV_LIBCTX_OF(provctx); /* used for rand */
736
465
}