Coverage Report

Created: 2026-09-12 06:55

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