Coverage Report

Created: 2026-08-18 07:24

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