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