Coverage Report

Created: 2025-10-28 06:56

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