Coverage Report

Created: 2026-07-16 06:59

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
158
{
44
158
    struct ossl_cipher_generic_get_params_st p;
45
46
158
    if (!ossl_cipher_generic_get_params_decoder(params, &p))
47
0
        return 0;
48
49
158
    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
158
    if (p.aead != NULL
54
158
        && !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
158
    if (p.custiv != NULL
59
158
        && !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
158
    if (p.cts != NULL
64
158
        && !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
158
    if (p.mb != NULL
69
158
        && !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
158
    if (p.rand != NULL
74
158
        && !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
158
    if (p.etm != NULL
79
158
        && !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
158
    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
158
    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
158
    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
158
    return 1;
96
158
}
97
98
const OSSL_PARAM *ossl_cipher_generic_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
99
101
{
100
101
    return cipher_generic_get_ctx_params_list;
101
101
}
102
103
const OSSL_PARAM *ossl_cipher_generic_settable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
104
0
{
105
0
    return cipher_generic_set_ctx_params_list;
106
0
}
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
473
{
144
473
    if (ctx != NULL && ctx->alloced) {
145
0
        OPENSSL_free(ctx->tlsmac);
146
0
        ctx->alloced = 0;
147
0
        ctx->tlsmac = NULL;
148
0
    }
149
473
}
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
0
{
156
0
    ctx->num = 0;
157
0
    ctx->bufsz = 0;
158
0
    ctx->updated = 0;
159
0
    ctx->enc = enc ? 1 : 0;
160
161
0
    if (!ossl_prov_is_running())
162
0
        return 0;
163
164
0
    if (iv != NULL && ctx->mode != EVP_CIPH_ECB_MODE) {
165
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
166
0
            return 0;
167
0
    }
168
0
    if (iv == NULL && ctx->iv_set
169
0
        && (ctx->mode == EVP_CIPH_CBC_MODE
170
0
            || ctx->mode == EVP_CIPH_CFB_MODE
171
0
            || ctx->mode == EVP_CIPH_OFB_MODE))
172
        /* reset IV for these modes to keep compatibility with 1.1.1 */
173
0
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
174
175
0
    if (key != NULL) {
176
0
        if (ctx->variable_keylength == 0) {
177
0
            if (keylen != ctx->keylen) {
178
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
179
0
                return 0;
180
0
            }
181
0
        } else {
182
0
            ctx->keylen = keylen;
183
0
        }
184
0
        if (!ctx->hw->init(ctx, key, ctx->keylen))
185
0
            return 0;
186
0
        ctx->key_set = 1;
187
0
    }
188
0
    return ossl_cipher_generic_set_ctx_params(ctx, params);
189
0
}
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
0
{
195
0
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
196
0
        iv, ivlen, params, 1);
197
0
}
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
0
{
203
0
    return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
204
0
        iv, ivlen, params, 0);
205
0
}
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
0
#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
0
{
236
0
    size_t outlint = 0;
237
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
238
0
    size_t blksz = ctx->blocksize;
239
0
    size_t nextblocks;
240
241
0
    if (!ctx->key_set) {
242
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
243
0
        return 0;
244
0
    }
245
246
0
    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
0
        if (in == NULL
254
0
            || in != out
255
0
            || outsize < inl
256
0
            || !ctx->pad) {
257
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
258
0
            return 0;
259
0
        }
260
261
0
        if (ctx->enc) {
262
0
            unsigned char padval;
263
0
            size_t padnum, loop;
264
265
            /* Add padding */
266
267
0
            padnum = blksz - (inl % blksz);
268
269
0
            if (outsize < inl + padnum) {
270
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
271
0
                return 0;
272
0
            }
273
274
0
            if (padnum > MAX_PADDING) {
275
0
                ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
276
0
                return 0;
277
0
            }
278
0
            padval = (unsigned char)(padnum - 1);
279
            /* we need to add 'padnum' padding bytes of value padval */
280
0
            for (loop = inl; loop < inl + padnum; loop++)
281
0
                out[loop] = padval;
282
0
            inl += padnum;
283
0
        }
284
285
0
        if ((inl % blksz) != 0) {
286
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
287
0
            return 0;
288
0
        }
289
290
        /* Shouldn't normally fail */
291
0
        if (!ctx->hw->cipher(ctx, out, in, inl)) {
292
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
293
0
            return 0;
294
0
        }
295
296
0
        if (ctx->alloced) {
297
0
            OPENSSL_free(ctx->tlsmac);
298
0
            ctx->alloced = 0;
299
0
            ctx->tlsmac = NULL;
300
0
        }
301
302
        /* This only fails if padding is publicly invalid */
303
0
        *outl = inl;
304
0
        if (!ctx->enc
305
0
            && !ossl_cipher_tlsunpadblock(ctx->libctx, ctx->tlsversion,
306
0
                out, outl,
307
0
                blksz, &ctx->tlsmac, &ctx->alloced,
308
0
                ctx->tlsmacsize, 0)) {
309
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
310
0
            return 0;
311
0
        }
312
0
        return 1;
313
0
    }
314
315
0
    if (ctx->bufsz != 0)
316
0
        nextblocks = ossl_cipher_fillblock(ctx->buf, &ctx->bufsz, blksz,
317
0
            &in, &inl);
318
0
    else
319
0
        nextblocks = inl & ~(blksz - 1);
320
321
    /*
322
     * If we're decrypting and we end an update on a block boundary we hold
323
     * the last block back in case this is the last update call and the last
324
     * block is padded.
325
     */
326
0
    if (ctx->bufsz == blksz && (ctx->enc || inl > 0 || !ctx->pad)) {
327
0
        if (outsize < blksz) {
328
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
329
0
            return 0;
330
0
        }
331
0
        if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
332
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
333
0
            return 0;
334
0
        }
335
0
        ctx->bufsz = 0;
336
0
        outlint = blksz;
337
0
        out += blksz;
338
0
    }
339
0
    if (nextblocks > 0) {
340
0
        if (!ctx->enc && ctx->pad && nextblocks == inl) {
341
0
            if (!ossl_assert(inl >= blksz)) {
342
0
                ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
343
0
                return 0;
344
0
            }
345
0
            nextblocks -= blksz;
346
0
        }
347
0
        outlint += nextblocks;
348
0
        if (outsize < outlint) {
349
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
350
0
            return 0;
351
0
        }
352
0
    }
353
0
    if (nextblocks > 0) {
354
0
        if (!ctx->hw->cipher(ctx, out, in, nextblocks)) {
355
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
356
0
            return 0;
357
0
        }
358
0
        in += nextblocks;
359
0
        inl -= nextblocks;
360
0
    }
361
0
    if (inl != 0
362
0
        && !ossl_cipher_trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
363
        /* ERR_raise already called */
364
0
        return 0;
365
0
    }
366
367
0
    *outl = outlint;
368
0
    return inl == 0;
369
0
}
370
371
int ossl_cipher_generic_block_final(void *vctx, unsigned char *out,
372
    size_t *outl, size_t outsize)
373
0
{
374
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
375
0
    size_t blksz = ctx->blocksize;
376
377
0
    if (!ossl_prov_is_running())
378
0
        return 0;
379
380
0
    if (!ctx->key_set) {
381
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
382
0
        return 0;
383
0
    }
384
385
0
    if (ctx->tlsversion > 0) {
386
        /* We never finalize TLS, so this is an error */
387
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
388
0
        return 0;
389
0
    }
390
391
0
    if (ctx->enc) {
392
0
        if (ctx->pad) {
393
0
            ossl_cipher_padblock(ctx->buf, &ctx->bufsz, blksz);
394
0
        } else if (ctx->bufsz == 0) {
395
0
            *outl = 0;
396
0
            return 1;
397
0
        } else if (ctx->bufsz != blksz) {
398
0
            ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
399
0
            return 0;
400
0
        }
401
402
0
        if (outsize < blksz) {
403
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
404
0
            return 0;
405
0
        }
406
0
        if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
407
0
            ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
408
0
            return 0;
409
0
        }
410
0
        ctx->bufsz = 0;
411
0
        *outl = blksz;
412
0
        return 1;
413
0
    }
414
415
    /* Decrypting */
416
0
    if (ctx->bufsz != blksz) {
417
0
        if (ctx->bufsz == 0 && !ctx->pad) {
418
0
            *outl = 0;
419
0
            return 1;
420
0
        }
421
0
        ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
422
0
        return 0;
423
0
    }
424
425
0
    if (!ctx->hw->cipher(ctx, ctx->buf, ctx->buf, blksz)) {
426
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
427
0
        return 0;
428
0
    }
429
430
0
    if (ctx->pad && !ossl_cipher_unpadblock(ctx->buf, &ctx->bufsz, blksz)) {
431
        /* ERR_raise already called */
432
0
        return 0;
433
0
    }
434
435
0
    if (outsize < ctx->bufsz) {
436
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
437
0
        return 0;
438
0
    }
439
0
    memcpy(out, ctx->buf, ctx->bufsz);
440
0
    *outl = ctx->bufsz;
441
0
    ctx->bufsz = 0;
442
0
    return 1;
443
0
}
444
445
int ossl_cipher_generic_stream_update(void *vctx, unsigned char *out,
446
    size_t *outl, size_t outsize,
447
    const unsigned char *in, size_t inl)
448
0
{
449
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
450
451
0
    if (!ctx->key_set) {
452
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
453
0
        return 0;
454
0
    }
455
456
0
    if (inl == 0) {
457
0
        *outl = 0;
458
0
        return 1;
459
0
    }
460
461
0
    if (outsize < inl) {
462
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
463
0
        return 0;
464
0
    }
465
466
0
    if (!ctx->hw->cipher(ctx, out, in, inl)) {
467
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
468
0
        return 0;
469
0
    }
470
471
0
    *outl = inl;
472
0
    if (!ctx->enc && ctx->tlsversion > 0) {
473
        /*
474
         * Remove any TLS padding. Only used by cipher_aes_cbc_hmac_sha1_hw.c and
475
         * cipher_aes_cbc_hmac_sha256_hw.c
476
         */
477
0
        if (ctx->removetlspad) {
478
            /*
479
             * We should have already failed in the cipher() call above if this
480
             * isn't true.
481
             */
482
0
            if (!ossl_assert(*outl >= (size_t)(out[inl - 1] + 1)))
483
0
                return 0;
484
            /* The actual padding length */
485
0
            *outl -= out[inl - 1] + 1;
486
0
        }
487
488
        /* TLS MAC and explicit IV if relevant. We should have already failed
489
         * in the cipher() call above if *outl is too short.
490
         */
491
0
        if (!ossl_assert(*outl >= ctx->removetlsfixed))
492
0
            return 0;
493
0
        *outl -= ctx->removetlsfixed;
494
495
        /* Extract the MAC if there is one */
496
0
        if (ctx->tlsmacsize > 0) {
497
0
            if (*outl < ctx->tlsmacsize)
498
0
                return 0;
499
500
0
            ctx->tlsmac = out + *outl - ctx->tlsmacsize;
501
0
            *outl -= ctx->tlsmacsize;
502
0
        }
503
0
    }
504
505
0
    return 1;
506
0
}
507
int ossl_cipher_generic_stream_final(void *vctx, unsigned char *out,
508
    size_t *outl, size_t outsize)
509
0
{
510
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
511
512
0
    if (!ossl_prov_is_running())
513
0
        return 0;
514
515
0
    if (!ctx->key_set) {
516
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
517
0
        return 0;
518
0
    }
519
520
0
    *outl = 0;
521
0
    return 1;
522
0
}
523
524
int ossl_cipher_generic_cipher(void *vctx, unsigned char *out, size_t *outl,
525
    size_t outsize, const unsigned char *in,
526
    size_t inl)
527
0
{
528
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
529
530
0
    if (!ossl_prov_is_running())
531
0
        return 0;
532
533
0
    if (!ctx->key_set) {
534
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
535
0
        return 0;
536
0
    }
537
538
0
    if (outsize < inl) {
539
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
540
0
        return 0;
541
0
    }
542
543
0
    if (!ctx->hw->cipher(ctx, out, in, inl)) {
544
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
545
0
        return 0;
546
0
    }
547
548
0
    *outl = inl;
549
0
    return 1;
550
0
}
551
552
int ossl_cipher_common_get_ctx_params(PROV_CIPHER_CTX *ctx, const struct ossl_cipher_get_ctx_param_list_st *p)
553
614
{
554
614
    if (p->ivlen != NULL && !OSSL_PARAM_set_size_t(p->ivlen, ctx->ivlen)) {
555
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
556
0
        return 0;
557
0
    }
558
559
614
    if (p->pad != NULL && !OSSL_PARAM_set_uint(p->pad, ctx->pad)) {
560
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
561
0
        return 0;
562
0
    }
563
564
614
    if (p->iv != NULL
565
0
        && !OSSL_PARAM_set_octet_string_or_ptr(p->iv, ctx->oiv, ctx->ivlen)) {
566
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
567
0
        return 0;
568
0
    }
569
570
614
    if (p->updiv != NULL
571
0
        && !OSSL_PARAM_set_octet_string_or_ptr(p->updiv, ctx->iv, ctx->ivlen)) {
572
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
573
0
        return 0;
574
0
    }
575
576
614
    if (p->num != NULL && !OSSL_PARAM_set_uint(p->num, ctx->num)) {
577
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
578
0
        return 0;
579
0
    }
580
581
614
    if (p->keylen != NULL && !OSSL_PARAM_set_size_t(p->keylen, ctx->keylen)) {
582
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
583
0
        return 0;
584
0
    }
585
586
614
    if (p->tlsmac != NULL
587
0
        && !OSSL_PARAM_set_octet_ptr(p->tlsmac, ctx->tlsmac, ctx->tlsmacsize)) {
588
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
589
0
        return 0;
590
0
    }
591
614
    return 1;
592
614
}
593
594
int ossl_cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
595
614
{
596
614
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
597
614
    struct ossl_cipher_get_ctx_param_list_st p;
598
599
614
    if (ctx == NULL || !cipher_generic_get_ctx_params_decoder(params, &p))
600
0
        return 0;
601
614
    return ossl_cipher_common_get_ctx_params(ctx, &p);
602
614
}
603
604
int ossl_cipher_common_set_ctx_params(PROV_CIPHER_CTX *ctx, const struct ossl_cipher_set_ctx_param_list_st *p)
605
0
{
606
0
    if (p->pad != NULL) {
607
0
        unsigned int pad;
608
609
0
        if (!OSSL_PARAM_get_uint(p->pad, &pad)) {
610
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
611
0
            return 0;
612
0
        }
613
0
        ctx->pad = pad ? 1 : 0;
614
0
    }
615
616
0
    if (p->bits != NULL) {
617
0
        unsigned int bits;
618
619
0
        if (!OSSL_PARAM_get_uint(p->bits, &bits)) {
620
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
621
0
            return 0;
622
0
        }
623
0
        ctx->use_bits = bits ? 1 : 0;
624
0
    }
625
626
0
    if (p->tlsvers != NULL) {
627
0
        if (!OSSL_PARAM_get_uint(p->tlsvers, &ctx->tlsversion)) {
628
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
629
0
            return 0;
630
0
        }
631
0
    }
632
633
0
    if (p->tlsmacsize != NULL) {
634
0
        if (!OSSL_PARAM_get_size_t(p->tlsmacsize, &ctx->tlsmacsize)) {
635
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
636
0
            return 0;
637
0
        }
638
0
    }
639
640
0
    if (p->num != NULL) {
641
0
        unsigned int num;
642
643
0
        if (!OSSL_PARAM_get_uint(p->num, &num)) {
644
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
645
0
            return 0;
646
0
        }
647
0
        if (ctx->blocksize > 0 && num >= (unsigned int)ctx->blocksize) {
648
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
649
0
            return 0;
650
0
        }
651
0
        ctx->num = num;
652
0
    }
653
0
    return 1;
654
0
}
655
656
int ossl_cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[])
657
0
{
658
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
659
0
    struct ossl_cipher_set_ctx_param_list_st p;
660
661
0
    if (ossl_param_is_empty(params))
662
0
        return 1;
663
664
0
    if (ctx == NULL || !cipher_generic_set_ctx_params_decoder(params, &p))
665
0
        return 0;
666
0
    return ossl_cipher_common_set_ctx_params(ctx, &p);
667
0
}
668
669
int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
670
    size_t ivlen)
671
141
{
672
141
    if (ivlen != ctx->ivlen
673
141
        || ivlen > sizeof(ctx->iv)) {
674
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
675
0
        return 0;
676
0
    }
677
141
    ctx->iv_set = 1;
678
141
    memcpy(ctx->iv, iv, ivlen);
679
141
    memcpy(ctx->oiv, iv, ivlen);
680
141
    return 1;
681
141
}
682
683
void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
684
    size_t ivbits, unsigned int mode,
685
    uint64_t flags, const PROV_CIPHER_HW *hw,
686
    void *provctx)
687
473
{
688
473
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
689
690
473
    if ((flags & PROV_CIPHER_FLAG_INVERSE_CIPHER) != 0)
691
0
        ctx->inverse_cipher = 1;
692
473
    if ((flags & PROV_CIPHER_FLAG_VARIABLE_LENGTH) != 0)
693
0
        ctx->variable_keylength = 1;
694
695
473
    ctx->pad = 1;
696
473
    ctx->keylen = ((kbits) / 8);
697
473
    ctx->ivlen = ((ivbits) / 8);
698
473
    ctx->hw = hw;
699
473
    ctx->mode = mode;
700
473
    ctx->blocksize = blkbits / 8;
701
473
    if (provctx != NULL)
702
0
        ctx->libctx = PROV_LIBCTX_OF(provctx); /* used for rand */
703
473
}