Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/providers/implementations/ciphers/ciphercommon.c.in

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