Coverage Report

Created: 2026-05-24 07:14

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-2025 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
918k
{
44
918k
    struct ossl_cipher_generic_get_params_st p;
45
46
918k
    if (!ossl_cipher_generic_get_params_decoder(params, &p))
47
0
        return 0;
48
49
918k
    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
918k
    if (p.aead != NULL
54
3.77k
        && !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
918k
    if (p.custiv != NULL
59
3.77k
        && !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
918k
    if (p.cts != NULL
64
3.77k
        && !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
918k
    if (p.mb != NULL
69
3.77k
        && !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
918k
    if (p.rand != NULL
74
3.77k
        && !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
918k
    if (p.etm != NULL
79
3.77k
        && !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
918k
    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
918k
    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
918k
    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
918k
    return 1;
96
918k
}
97
98
const OSSL_PARAM *ossl_cipher_generic_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
99
5.47k
{
100
5.47k
    return cipher_generic_get_ctx_params_list;
101
5.47k
}
102
103
const OSSL_PARAM *ossl_cipher_generic_settable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
104
254
{
105
254
    return cipher_generic_set_ctx_params_list;
106
254
}
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
271k
{
144
271k
    if (ctx != NULL && ctx->alloced) {
145
4.29k
        OPENSSL_free(ctx->tlsmac);
146
4.29k
        ctx->alloced = 0;
147
4.29k
        ctx->tlsmac = NULL;
148
4.29k
    }
149
271k
}
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
5.23M
{
156
5.23M
    ctx->num = 0;
157
5.23M
    ctx->bufsz = 0;
158
5.23M
    ctx->updated = 0;
159
5.23M
    ctx->enc = enc ? 1 : 0;
160
161
5.23M
    if (!ossl_prov_is_running())
162
0
        return 0;
163
164
5.23M
    if (iv != NULL && ctx->mode != EVP_CIPH_ECB_MODE) {
165
739k
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
166
0
            return 0;
167
739k
    }
168
5.23M
    if (iv == NULL && ctx->iv_set
169
210k
        && (ctx->mode == EVP_CIPH_CBC_MODE
170
210k
            || ctx->mode == EVP_CIPH_CFB_MODE
171
210k
            || ctx->mode == EVP_CIPH_OFB_MODE))
172
        /* reset IV for these modes to keep compatibility with 1.1.1 */
173
90
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
174
175
5.23M
    if (key != NULL) {
176
695k
        if (ctx->variable_keylength == 0) {
177
695k
            if (keylen != ctx->keylen) {
178
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
179
0
                return 0;
180
0
            }
181
695k
        } else {
182
0
            ctx->keylen = keylen;
183
0
        }
184
695k
        if (!ctx->hw->init(ctx, key, ctx->keylen))
185
0
            return 0;
186
695k
        ctx->key_set = 1;
187
695k
    }
188
5.23M
    return ossl_cipher_generic_set_ctx_params(ctx, params);
189
5.23M
}
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
5.07M
{
195
5.07M
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
196
5.07M
        iv, ivlen, params, 1);
197
5.07M
}
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
164k
{
203
164k
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
204
164k
        iv, ivlen, params, 0);
205
164k
}
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
7.57k
#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
10.2M
{
236
10.2M
    size_t outlint = 0;
237
10.2M
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
238
10.2M
    size_t blksz = ctx->blocksize;
239
10.2M
    size_t nextblocks;
240
241
10.2M
    if (!ctx->key_set) {
242
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
243
0
        return 0;
244
0
    }
245
246
10.2M
    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
265k
        if (in == NULL
254
265k
            || in != out
255
265k
            || outsize < inl
256
265k
            || !ctx->pad) {
257
5
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
258
5
            return 0;
259
5
        }
260
261
265k
        if (ctx->enc) {
262
7.57k
            unsigned char padval;
263
7.57k
            size_t padnum, loop;
264
265
            /* Add padding */
266
267
7.57k
            padnum = blksz - (inl % blksz);
268
269
7.57k
            if (outsize < inl + padnum) {
270
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
271
0
                return 0;
272
0
            }
273
274
7.57k
            if (padnum > MAX_PADDING) {
275
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
276
0
                return 0;
277
0
            }
278
7.57k
            padval = (unsigned char)(padnum - 1);
279
7.57k
            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
7.57k
            } else {
284
                /* we need to add 'padnum' padding bytes of value padval */
285
65.6k
                for (loop = inl; loop < inl + padnum; loop++)
286
58.0k
                    out[loop] = padval;
287
7.57k
            }
288
7.57k
            inl += padnum;
289
7.57k
        }
290
291
265k
        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
265k
        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
265k
        if (ctx->alloced) {
303
206k
            OPENSSL_free(ctx->tlsmac);
304
206k
            ctx->alloced = 0;
305
206k
            ctx->tlsmac = NULL;
306
206k
        }
307
308
        /* This only fails if padding is publicly invalid */
309
265k
        *outl = inl;
310
265k
        if (!ctx->enc
311
257k
            && !ossl_cipher_tlsunpadblock(ctx->libctx, ctx->tlsversion,
312
257k
                out, outl,
313
257k
                blksz, &ctx->tlsmac, &ctx->alloced,
314
257k
                ctx->tlsmacsize, 0)) {
315
46.7k
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
316
46.7k
            return 0;
317
46.7k
        }
318
218k
        return 1;
319
265k
    }
320
321
10.0M
    if (ctx->bufsz != 0)
322
0
        nextblocks = ossl_cipher_fillblock(ctx->buf, &ctx->bufsz, blksz,
323
0
            &in, &inl);
324
10.0M
    else
325
10.0M
        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
10.0M
    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
10.0M
    if (nextblocks > 0) {
346
10.0M
        if (!ctx->enc && ctx->pad && nextblocks == inl) {
347
1.16k
            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
1.16k
            nextblocks -= blksz;
352
1.16k
        }
353
10.0M
        outlint += nextblocks;
354
10.0M
        if (outsize < outlint) {
355
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
356
0
            return 0;
357
0
        }
358
10.0M
    }
359
10.0M
    if (nextblocks > 0) {
360
10.0M
        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
10.0M
        in += nextblocks;
365
10.0M
        inl -= nextblocks;
366
10.0M
    }
367
10.0M
    if (inl != 0
368
1.32k
        && !ossl_cipher_trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
369
        /* ERR_raise already called */
370
0
        return 0;
371
0
    }
372
373
10.0M
    *outl = outlint;
374
10.0M
    return inl == 0;
375
10.0M
}
376
377
int ossl_cipher_generic_block_final(void *vctx, unsigned char *out,
378
    size_t *outl, size_t outsize)
379
1.38k
{
380
1.38k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
381
1.38k
    size_t blksz = ctx->blocksize;
382
383
1.38k
    if (!ossl_prov_is_running())
384
0
        return 0;
385
386
1.38k
    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.38k
    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.38k
    if (ctx->enc) {
398
189
        if (ctx->pad) {
399
107
            ossl_cipher_padblock(ctx->buf, &ctx->bufsz, blksz);
400
107
        } else if (ctx->bufsz == 0) {
401
56
            *outl = 0;
402
56
            return 1;
403
56
        } else if (ctx->bufsz != blksz) {
404
26
            ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
405
26
            return 0;
406
26
        }
407
408
107
        if (outsize < blksz) {
409
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
410
0
            return 0;
411
0
        }
412
107
        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
107
        ctx->bufsz = 0;
417
107
        *outl = blksz;
418
107
        return 1;
419
107
    }
420
421
    /* Decrypting */
422
1.19k
    if (ctx->bufsz != blksz) {
423
33
        if (ctx->bufsz == 0 && !ctx->pad) {
424
0
            *outl = 0;
425
0
            return 1;
426
0
        }
427
33
        ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
428
33
        return 0;
429
33
    }
430
431
1.16k
    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
1.16k
    if (ctx->pad && !ossl_cipher_unpadblock(ctx->buf, &ctx->bufsz, blksz)) {
437
        /* ERR_raise already called */
438
94
        return 0;
439
94
    }
440
441
1.06k
    if (outsize < ctx->bufsz) {
442
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
443
0
        return 0;
444
0
    }
445
1.06k
    memcpy(out, ctx->buf, ctx->bufsz);
446
1.06k
    *outl = ctx->bufsz;
447
1.06k
    ctx->bufsz = 0;
448
1.06k
    return 1;
449
1.06k
}
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
344k
{
455
344k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
456
457
344k
    if (!ctx->key_set) {
458
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
459
0
        return 0;
460
0
    }
461
462
344k
    if (inl == 0) {
463
0
        *outl = 0;
464
0
        return 1;
465
0
    }
466
467
344k
    if (outsize < inl) {
468
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
469
0
        return 0;
470
0
    }
471
472
344k
    if (!ctx->hw->cipher(ctx, out, in, inl)) {
473
501
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
474
501
        return 0;
475
501
    }
476
477
343k
    *outl = inl;
478
343k
    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
343k
    return 1;
512
343k
}
513
int ossl_cipher_generic_stream_final(void *vctx, unsigned char *out,
514
    size_t *outl, size_t outsize)
515
657
{
516
657
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
517
518
657
    if (!ossl_prov_is_running())
519
0
        return 0;
520
521
657
    if (!ctx->key_set) {
522
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
523
0
        return 0;
524
0
    }
525
526
657
    *outl = 0;
527
657
    return 1;
528
657
}
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.0k
{
534
17.0k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
535
536
17.0k
    if (!ossl_prov_is_running())
537
0
        return 0;
538
539
17.0k
    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.0k
    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.0k
    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.0k
    *outl = inl;
555
17.0k
    return 1;
556
17.0k
}
557
558
int ossl_cipher_common_get_ctx_params(PROV_CIPHER_CTX *ctx, const struct ossl_cipher_get_ctx_param_list_st *p)
559
167k
{
560
167k
    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
167k
    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
167k
    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
167k
    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
167k
    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
167k
    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
167k
    if (p->tlsmac != NULL
593
68.4k
        && !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
167k
    return 1;
598
167k
}
599
600
int ossl_cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
601
167k
{
602
167k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
603
167k
    struct ossl_cipher_get_ctx_param_list_st p;
604
605
167k
    if (ctx == NULL || !cipher_generic_get_ctx_params_decoder(params, &p))
606
0
        return 0;
607
167k
    return ossl_cipher_common_get_ctx_params(ctx, &p);
608
167k
}
609
610
int ossl_cipher_common_set_ctx_params(PROV_CIPHER_CTX *ctx, const struct ossl_cipher_set_ctx_param_list_st *p)
611
3.95k
{
612
3.95k
    if (p->pad != NULL) {
613
492
        unsigned int pad;
614
615
492
        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
492
        ctx->pad = pad ? 1 : 0;
620
492
    }
621
622
3.95k
    if (p->bits != NULL) {
623
102
        unsigned int bits;
624
625
102
        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
102
        ctx->use_bits = bits ? 1 : 0;
630
102
    }
631
632
3.95k
    if (p->tlsvers != NULL) {
633
3.56k
        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
3.56k
    }
638
639
3.95k
    if (p->tlsmacsize != NULL) {
640
3.56k
        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
3.56k
    }
645
646
3.95k
    if (p->num != NULL) {
647
121
        unsigned int num;
648
649
121
        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
121
        if (ctx->blocksize > 0 && num >= (unsigned int)ctx->blocksize) {
654
14
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
655
14
            return 0;
656
14
        }
657
107
        ctx->num = num;
658
107
    }
659
3.94k
    return 1;
660
3.95k
}
661
662
int ossl_cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[])
663
2.03M
{
664
2.03M
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
665
2.03M
    struct ossl_cipher_set_ctx_param_list_st p;
666
667
2.03M
    if (ossl_param_is_empty(params))
668
2.03M
        return 1;
669
670
3.95k
    if (ctx == NULL || !cipher_generic_set_ctx_params_decoder(params, &p))
671
0
        return 0;
672
3.95k
    return ossl_cipher_common_set_ctx_params(ctx, &p);
673
3.95k
}
674
675
int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
676
    size_t ivlen)
677
744k
{
678
744k
    if (ivlen != ctx->ivlen
679
744k
        || ivlen > sizeof(ctx->iv)) {
680
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
681
0
        return 0;
682
0
    }
683
744k
    ctx->iv_set = 1;
684
744k
    memcpy(ctx->iv, iv, ivlen);
685
744k
    memcpy(ctx->oiv, iv, ivlen);
686
744k
    return 1;
687
744k
}
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
280k
{
694
280k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
695
696
280k
    if ((flags & PROV_CIPHER_FLAG_INVERSE_CIPHER) != 0)
697
13
        ctx->inverse_cipher = 1;
698
280k
    if ((flags & PROV_CIPHER_FLAG_VARIABLE_LENGTH) != 0)
699
0
        ctx->variable_keylength = 1;
700
701
280k
    ctx->pad = 1;
702
280k
    ctx->keylen = ((kbits) / 8);
703
280k
    ctx->ivlen = ((ivbits) / 8);
704
280k
    ctx->hw = hw;
705
280k
    ctx->mode = mode;
706
280k
    ctx->blocksize = blkbits / 8;
707
280k
    if (provctx != NULL)
708
257k
        ctx->libctx = PROV_LIBCTX_OF(provctx); /* used for rand */
709
280k
}