Coverage Report

Created: 2026-10-28 06:28

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