/src/openssl36/crypto/evp/evp_enc.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-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 | | /* We need to use some engine deprecated APIs */ |
11 | | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | | |
13 | | #include <stdio.h> |
14 | | #include <limits.h> |
15 | | #include <assert.h> |
16 | | #include <openssl/evp.h> |
17 | | #include <openssl/err.h> |
18 | | #include <openssl/rand.h> |
19 | | #ifndef FIPS_MODULE |
20 | | #include <openssl/engine.h> |
21 | | #endif |
22 | | #include <openssl/params.h> |
23 | | #include <openssl/core_names.h> |
24 | | #include "internal/cryptlib.h" |
25 | | #include "internal/provider.h" |
26 | | #include "internal/core.h" |
27 | | #include "internal/safe_math.h" |
28 | | #include "crypto/evp.h" |
29 | | #include "evp_local.h" |
30 | | |
31 | 0 | OSSL_SAFE_MATH_SIGNED(int, int) |
32 | 0 |
|
33 | 0 | int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) |
34 | 907k | { |
35 | 907k | if (ctx == NULL) |
36 | 0 | return 1; |
37 | | |
38 | 907k | if (ctx->cipher == NULL || ctx->cipher->prov == NULL) |
39 | 20.2k | goto legacy; |
40 | | |
41 | 886k | if (ctx->algctx != NULL) { |
42 | 886k | if (ctx->cipher->freectx != NULL) |
43 | 886k | ctx->cipher->freectx(ctx->algctx); |
44 | 886k | ctx->algctx = NULL; |
45 | 886k | } |
46 | 886k | if (ctx->fetched_cipher != NULL) |
47 | 886k | EVP_CIPHER_free(ctx->fetched_cipher); |
48 | 886k | memset(ctx, 0, sizeof(*ctx)); |
49 | 886k | ctx->iv_len = -1; |
50 | | |
51 | 886k | return 1; |
52 | | |
53 | | /* Remove legacy code below when legacy support is removed. */ |
54 | 20.2k | legacy: |
55 | | |
56 | 20.2k | if (ctx->cipher != NULL) { |
57 | 0 | if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx)) |
58 | 0 | return 0; |
59 | | /* Cleanse cipher context data */ |
60 | 0 | if (ctx->cipher_data && ctx->cipher->ctx_size) |
61 | 0 | OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size); |
62 | 0 | } |
63 | 20.2k | OPENSSL_free(ctx->cipher_data); |
64 | 20.2k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
65 | 20.2k | ENGINE_finish(ctx->engine); |
66 | 20.2k | #endif |
67 | 20.2k | memset(ctx, 0, sizeof(*ctx)); |
68 | 20.2k | ctx->iv_len = -1; |
69 | 20.2k | return 1; |
70 | 20.2k | } |
71 | | |
72 | | EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) |
73 | 897k | { |
74 | 897k | EVP_CIPHER_CTX *ctx; |
75 | | |
76 | 897k | ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); |
77 | 897k | if (ctx == NULL) |
78 | 0 | return NULL; |
79 | | |
80 | 897k | ctx->iv_len = -1; |
81 | 897k | return ctx; |
82 | 897k | } |
83 | | |
84 | | void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) |
85 | 1.61M | { |
86 | 1.61M | if (ctx == NULL) |
87 | 715k | return; |
88 | 903k | EVP_CIPHER_CTX_reset(ctx); |
89 | 903k | OPENSSL_free(ctx); |
90 | 903k | } |
91 | | |
92 | | static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx, |
93 | | const EVP_CIPHER *cipher, |
94 | | ENGINE *impl, const unsigned char *key, |
95 | | const unsigned char *iv, int enc, |
96 | | uint8_t is_pipeline, |
97 | | const OSSL_PARAM params[]) |
98 | 2.69M | { |
99 | 2.69M | int n; |
100 | 2.69M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
101 | 2.69M | ENGINE *tmpimpl = NULL; |
102 | 2.69M | #endif |
103 | | |
104 | | /* |
105 | | * enc == 1 means we are encrypting. |
106 | | * enc == 0 means we are decrypting. |
107 | | * enc == -1 means, use the previously initialised value for encrypt/decrypt |
108 | | */ |
109 | 2.69M | if (enc == -1) { |
110 | 119k | enc = ctx->encrypt; |
111 | 2.57M | } else { |
112 | 2.57M | if (enc) |
113 | 2.02M | enc = 1; |
114 | 2.57M | ctx->encrypt = enc; |
115 | 2.57M | } |
116 | | |
117 | 2.69M | if (cipher == NULL && ctx->cipher == NULL) { |
118 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
119 | 0 | return 0; |
120 | 0 | } |
121 | | |
122 | | /* Code below to be removed when legacy support is dropped. */ |
123 | 2.69M | if (is_pipeline) |
124 | 0 | goto nonlegacy; |
125 | | |
126 | 2.69M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
127 | | /* |
128 | | * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so |
129 | | * this context may already have an ENGINE! Try to avoid releasing the |
130 | | * previous handle, re-querying for an ENGINE, and having a |
131 | | * reinitialisation, when it may all be unnecessary. |
132 | | */ |
133 | 2.69M | if (ctx->engine && ctx->cipher |
134 | 0 | && (cipher == NULL || cipher->nid == ctx->cipher->nid)) |
135 | 0 | goto skip_to_init; |
136 | | |
137 | 2.69M | if (cipher != NULL && impl == NULL) { |
138 | | /* Ask if an ENGINE is reserved for this job */ |
139 | 326k | tmpimpl = ENGINE_get_cipher_engine(cipher->nid); |
140 | 326k | } |
141 | 2.69M | #endif |
142 | | |
143 | | /* |
144 | | * If there are engines involved then we should use legacy handling for now. |
145 | | */ |
146 | 2.69M | if (ctx->engine != NULL |
147 | 2.69M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
148 | 2.69M | || tmpimpl != NULL |
149 | 2.69M | #endif |
150 | 2.69M | || impl != NULL |
151 | 2.69M | || (cipher != NULL && cipher->origin == EVP_ORIG_METH) |
152 | 2.69M | || (cipher == NULL && ctx->cipher != NULL |
153 | 2.36M | && ctx->cipher->origin == EVP_ORIG_METH)) { |
154 | 0 | if (ctx->cipher == ctx->fetched_cipher) |
155 | 0 | ctx->cipher = NULL; |
156 | 0 | EVP_CIPHER_free(ctx->fetched_cipher); |
157 | 0 | ctx->fetched_cipher = NULL; |
158 | 0 | goto legacy; |
159 | 0 | } |
160 | | /* |
161 | | * Ensure a context left lying around from last time is cleared |
162 | | * (legacy code) |
163 | | */ |
164 | 2.69M | if (cipher != NULL && ctx->cipher != NULL) { |
165 | 0 | if (ctx->cipher->cleanup != NULL && !ctx->cipher->cleanup(ctx)) |
166 | 0 | return 0; |
167 | 0 | OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size); |
168 | 0 | ctx->cipher_data = NULL; |
169 | 0 | } |
170 | | |
171 | | /* Start of non-legacy code below */ |
172 | 2.69M | nonlegacy: |
173 | | /* Ensure a context left lying around from last time is cleared */ |
174 | 2.69M | if (cipher != NULL && ctx->cipher != NULL) { |
175 | 0 | unsigned long flags = ctx->flags; |
176 | |
|
177 | 0 | EVP_CIPHER_CTX_reset(ctx); |
178 | | /* Restore encrypt and flags */ |
179 | 0 | ctx->encrypt = enc; |
180 | 0 | ctx->flags = flags; |
181 | 0 | } |
182 | | |
183 | 2.69M | if (cipher == NULL) |
184 | 2.36M | cipher = ctx->cipher; |
185 | | |
186 | 2.69M | if (cipher->prov == NULL) { |
187 | | #ifdef FIPS_MODULE |
188 | | /* We only do explicit fetches inside the FIPS module */ |
189 | | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
190 | | return 0; |
191 | | #else |
192 | 0 | EVP_CIPHER *provciph = EVP_CIPHER_fetch(NULL, |
193 | 0 | cipher->nid == NID_undef ? "NULL" |
194 | 0 | : OBJ_nid2sn(cipher->nid), |
195 | 0 | ""); |
196 | |
|
197 | 0 | if (provciph == NULL) |
198 | 0 | return 0; |
199 | 0 | cipher = provciph; |
200 | 0 | EVP_CIPHER_free(ctx->fetched_cipher); |
201 | 0 | ctx->fetched_cipher = provciph; |
202 | 0 | #endif |
203 | 0 | } |
204 | | |
205 | 2.69M | if (!ossl_assert(cipher->prov != NULL)) { |
206 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
207 | 0 | return 0; |
208 | 0 | } |
209 | | |
210 | 2.69M | if (cipher != ctx->fetched_cipher) { |
211 | 326k | if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) { |
212 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
213 | 0 | return 0; |
214 | 0 | } |
215 | 326k | EVP_CIPHER_free(ctx->fetched_cipher); |
216 | | /* Coverity false positive, the reference counting is confusing it */ |
217 | | /* coverity[use_after_free] */ |
218 | 326k | ctx->fetched_cipher = (EVP_CIPHER *)cipher; |
219 | 326k | } |
220 | 2.69M | ctx->cipher = cipher; |
221 | | |
222 | 2.69M | if (is_pipeline && !EVP_CIPHER_can_pipeline(cipher, enc)) { |
223 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PIPELINE_NOT_SUPPORTED); |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | 2.69M | if (ctx->algctx == NULL) { |
228 | 326k | ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov)); |
229 | 326k | if (ctx->algctx == NULL) { |
230 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
231 | 0 | return 0; |
232 | 0 | } |
233 | 326k | } |
234 | | |
235 | 2.69M | if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) { |
236 | | /* |
237 | | * If this ctx was already set up for no padding then we need to tell |
238 | | * the new cipher about it. |
239 | | */ |
240 | 0 | if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | 2.69M | #ifndef FIPS_MODULE |
245 | | /* |
246 | | * Fix for CVE-2023-5363 |
247 | | * Passing in a size as part of the init call takes effect late |
248 | | * so, force such to occur before the initialisation. |
249 | | * |
250 | | * The FIPS provider's internal library context is used in a manner |
251 | | * such that this is not an issue. |
252 | | */ |
253 | 2.69M | if (params != NULL) { |
254 | 0 | OSSL_PARAM param_lens[3] = { OSSL_PARAM_END, OSSL_PARAM_END, |
255 | 0 | OSSL_PARAM_END }; |
256 | 0 | OSSL_PARAM *q = param_lens; |
257 | 0 | const OSSL_PARAM *p; |
258 | |
|
259 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); |
260 | 0 | if (p != NULL) |
261 | 0 | memcpy(q++, p, sizeof(*q)); |
262 | | |
263 | | /* |
264 | | * Note that OSSL_CIPHER_PARAM_AEAD_IVLEN is a synonym for |
265 | | * OSSL_CIPHER_PARAM_IVLEN so both are covered here. |
266 | | */ |
267 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); |
268 | 0 | if (p != NULL) |
269 | 0 | memcpy(q++, p, sizeof(*q)); |
270 | |
|
271 | 0 | if (q != param_lens) { |
272 | 0 | if (!EVP_CIPHER_CTX_set_params(ctx, param_lens)) { |
273 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); |
274 | 0 | return 0; |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | 2.69M | #endif |
279 | | |
280 | 2.69M | if (is_pipeline) |
281 | 0 | return 1; |
282 | | |
283 | 2.69M | if (enc) { |
284 | 2.14M | if (ctx->cipher->einit == NULL) { |
285 | | /* |
286 | | * We still should be able to set the IV using the new API |
287 | | * if the key is not specified and old API is not available |
288 | | */ |
289 | 0 | if (key == NULL && ctx->cipher->einit_skey != NULL) { |
290 | 0 | return ctx->cipher->einit_skey(ctx->algctx, NULL, |
291 | 0 | iv, |
292 | 0 | iv == NULL ? 0 |
293 | 0 | : EVP_CIPHER_CTX_get_iv_length(ctx), |
294 | 0 | params); |
295 | 0 | } |
296 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 2.14M | return ctx->cipher->einit(ctx->algctx, |
301 | 2.14M | key, |
302 | 2.14M | key == NULL ? 0 |
303 | 2.14M | : EVP_CIPHER_CTX_get_key_length(ctx), |
304 | 2.14M | iv, |
305 | 2.14M | iv == NULL ? 0 |
306 | 2.14M | : EVP_CIPHER_CTX_get_iv_length(ctx), |
307 | 2.14M | params); |
308 | 2.14M | } |
309 | | |
310 | 548k | if (ctx->cipher->dinit == NULL) { |
311 | | /* |
312 | | * We still should be able to set the IV using the new API |
313 | | * if the key is not specified and old API is not available |
314 | | */ |
315 | 0 | if (key == NULL && ctx->cipher->dinit_skey != NULL) { |
316 | 0 | return ctx->cipher->dinit_skey(ctx->algctx, NULL, |
317 | 0 | iv, |
318 | 0 | iv == NULL ? 0 |
319 | 0 | : EVP_CIPHER_CTX_get_iv_length(ctx), |
320 | 0 | params); |
321 | 0 | } |
322 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | 548k | return ctx->cipher->dinit(ctx->algctx, |
327 | 548k | key, |
328 | 548k | key == NULL ? 0 |
329 | 548k | : EVP_CIPHER_CTX_get_key_length(ctx), |
330 | 548k | iv, |
331 | 548k | iv == NULL ? 0 |
332 | 548k | : EVP_CIPHER_CTX_get_iv_length(ctx), |
333 | 548k | params); |
334 | | |
335 | | /* Code below to be removed when legacy support is dropped. */ |
336 | 0 | legacy: |
337 | |
|
338 | 0 | if (cipher != NULL) { |
339 | | /* |
340 | | * Ensure a context left lying around from last time is cleared (we |
341 | | * previously attempted to avoid this if the same ENGINE and |
342 | | * EVP_CIPHER could be used). |
343 | | */ |
344 | 0 | if (ctx->cipher) { |
345 | 0 | unsigned long flags = ctx->flags; |
346 | 0 | EVP_CIPHER_CTX_reset(ctx); |
347 | | /* Restore encrypt and flags */ |
348 | 0 | ctx->encrypt = enc; |
349 | 0 | ctx->flags = flags; |
350 | 0 | } |
351 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
352 | 0 | if (impl != NULL) { |
353 | 0 | if (!ENGINE_init(impl)) { |
354 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
355 | 0 | return 0; |
356 | 0 | } |
357 | 0 | } else { |
358 | 0 | impl = tmpimpl; |
359 | 0 | } |
360 | 0 | if (impl != NULL) { |
361 | | /* There's an ENGINE for this job ... (apparently) */ |
362 | 0 | const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid); |
363 | |
|
364 | 0 | if (c == NULL) { |
365 | | /* |
366 | | * One positive side-effect of US's export control history, |
367 | | * is that we should at least be able to avoid using US |
368 | | * misspellings of "initialisation"? |
369 | | */ |
370 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
371 | 0 | return 0; |
372 | 0 | } |
373 | | /* We'll use the ENGINE's private cipher definition */ |
374 | 0 | cipher = c; |
375 | | /* |
376 | | * Store the ENGINE functional reference so we know 'cipher' came |
377 | | * from an ENGINE and we need to release it when done. |
378 | | */ |
379 | 0 | ctx->engine = impl; |
380 | 0 | } else { |
381 | 0 | ctx->engine = NULL; |
382 | 0 | } |
383 | 0 | #endif |
384 | | |
385 | 0 | ctx->cipher = cipher; |
386 | 0 | if (ctx->cipher->ctx_size) { |
387 | 0 | ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size); |
388 | 0 | if (ctx->cipher_data == NULL) { |
389 | 0 | ctx->cipher = NULL; |
390 | 0 | return 0; |
391 | 0 | } |
392 | 0 | } else { |
393 | 0 | ctx->cipher_data = NULL; |
394 | 0 | } |
395 | 0 | ctx->key_len = cipher->key_len; |
396 | | /* Preserve wrap enable flag, zero everything else */ |
397 | 0 | ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW; |
398 | 0 | if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { |
399 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) { |
400 | 0 | ctx->cipher = NULL; |
401 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
402 | 0 | return 0; |
403 | 0 | } |
404 | 0 | } |
405 | 0 | } |
406 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
407 | 0 | skip_to_init: |
408 | 0 | #endif |
409 | 0 | if (ctx->cipher == NULL) |
410 | 0 | return 0; |
411 | | |
412 | | /* we assume block size is a power of 2 in *cryptUpdate */ |
413 | 0 | OPENSSL_assert(ctx->cipher->block_size == 1 |
414 | 0 | || ctx->cipher->block_size == 8 |
415 | 0 | || ctx->cipher->block_size == 16); |
416 | |
|
417 | 0 | if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW) |
418 | 0 | && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) { |
419 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED); |
420 | 0 | return 0; |
421 | 0 | } |
422 | | |
423 | 0 | if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
424 | 0 | & EVP_CIPH_CUSTOM_IV) |
425 | 0 | == 0) { |
426 | 0 | switch (EVP_CIPHER_CTX_get_mode(ctx)) { |
427 | | |
428 | 0 | case EVP_CIPH_STREAM_CIPHER: |
429 | 0 | case EVP_CIPH_ECB_MODE: |
430 | 0 | break; |
431 | | |
432 | 0 | case EVP_CIPH_CFB_MODE: |
433 | 0 | case EVP_CIPH_OFB_MODE: |
434 | |
|
435 | 0 | ctx->num = 0; |
436 | | /* fall-through */ |
437 | |
|
438 | 0 | case EVP_CIPH_CBC_MODE: |
439 | 0 | n = EVP_CIPHER_CTX_get_iv_length(ctx); |
440 | 0 | if (n < 0 || n > (int)sizeof(ctx->iv)) { |
441 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH); |
442 | 0 | return 0; |
443 | 0 | } |
444 | 0 | if (iv != NULL) |
445 | 0 | memcpy(ctx->oiv, iv, n); |
446 | 0 | memcpy(ctx->iv, ctx->oiv, n); |
447 | 0 | break; |
448 | | |
449 | 0 | case EVP_CIPH_CTR_MODE: |
450 | 0 | ctx->num = 0; |
451 | | /* Don't reuse IV for CTR mode */ |
452 | 0 | if (iv != NULL) { |
453 | 0 | n = EVP_CIPHER_CTX_get_iv_length(ctx); |
454 | 0 | if (n <= 0 || n > (int)sizeof(ctx->iv)) { |
455 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH); |
456 | 0 | return 0; |
457 | 0 | } |
458 | 0 | memcpy(ctx->iv, iv, n); |
459 | 0 | } |
460 | 0 | break; |
461 | | |
462 | 0 | default: |
463 | 0 | return 0; |
464 | 0 | } |
465 | 0 | } |
466 | | |
467 | 0 | if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { |
468 | 0 | if (!ctx->cipher->init(ctx, key, iv, enc)) |
469 | 0 | return 0; |
470 | 0 | } |
471 | 0 | ctx->buf_len = 0; |
472 | 0 | ctx->final_used = 0; |
473 | 0 | ctx->block_mask = ctx->cipher->block_size - 1; |
474 | 0 | return 1; |
475 | 0 | } |
476 | | |
477 | | /* |
478 | | * This function is basically evp_cipher_init_internal without ENGINE support. |
479 | | * They should be combined when engines are not supported any longer. |
480 | | */ |
481 | | static int evp_cipher_init_skey_internal(EVP_CIPHER_CTX *ctx, |
482 | | const EVP_CIPHER *cipher, |
483 | | const EVP_SKEY *skey, |
484 | | const unsigned char *iv, size_t iv_len, |
485 | | int enc, const OSSL_PARAM params[]) |
486 | 0 | { |
487 | 0 | int ret; |
488 | | |
489 | | /* |
490 | | * enc == 1 means we are encrypting. |
491 | | * enc == 0 means we are decrypting. |
492 | | * enc == -1 means, use the previously initialised value for encrypt/decrypt |
493 | | */ |
494 | 0 | if (enc == -1) |
495 | 0 | enc = ctx->encrypt; |
496 | 0 | else |
497 | 0 | ctx->encrypt = enc != 0; |
498 | |
|
499 | 0 | if (cipher == NULL && ctx->cipher == NULL) { |
500 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
501 | 0 | return 0; |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * If there are engines involved then we throw an error |
506 | | */ |
507 | 0 | if (ctx->engine != NULL |
508 | 0 | || (cipher != NULL && cipher->origin == EVP_ORIG_METH) |
509 | 0 | || (cipher == NULL && ctx->cipher != NULL |
510 | 0 | && ctx->cipher->origin == EVP_ORIG_METH)) { |
511 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
512 | 0 | return 0; |
513 | 0 | } |
514 | | /* |
515 | | * Ensure a context left lying around from last time is cleared |
516 | | * (legacy code) |
517 | | */ |
518 | 0 | if (cipher != NULL && ctx->cipher != NULL) { |
519 | 0 | if (ctx->cipher->cleanup != NULL && !ctx->cipher->cleanup(ctx)) |
520 | 0 | return 0; |
521 | 0 | OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size); |
522 | 0 | ctx->cipher_data = NULL; |
523 | 0 | } |
524 | | |
525 | | /* Ensure a context left lying around from last time is cleared */ |
526 | 0 | if (cipher != NULL && ctx->cipher != NULL) { |
527 | 0 | unsigned long flags = ctx->flags; |
528 | |
|
529 | 0 | EVP_CIPHER_CTX_reset(ctx); |
530 | | /* Restore encrypt and flags */ |
531 | 0 | ctx->encrypt = enc; |
532 | 0 | ctx->flags = flags; |
533 | 0 | } |
534 | |
|
535 | 0 | if (cipher == NULL) |
536 | 0 | cipher = ctx->cipher; |
537 | |
|
538 | 0 | if (cipher->prov == NULL) { |
539 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
540 | 0 | return 0; |
541 | 0 | } |
542 | | |
543 | 0 | if (cipher != ctx->fetched_cipher) { |
544 | 0 | if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) { |
545 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
546 | 0 | return 0; |
547 | 0 | } |
548 | 0 | EVP_CIPHER_free(ctx->fetched_cipher); |
549 | | /* Coverity false positive, the reference counting is confusing it */ |
550 | | /* coverity[use_after_free] */ |
551 | 0 | ctx->fetched_cipher = (EVP_CIPHER *)cipher; |
552 | 0 | } |
553 | 0 | ctx->cipher = cipher; |
554 | 0 | if (ctx->algctx == NULL) { |
555 | 0 | ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov)); |
556 | 0 | if (ctx->algctx == NULL) { |
557 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
558 | 0 | return 0; |
559 | 0 | } |
560 | 0 | } |
561 | | |
562 | 0 | if (skey != NULL && ctx->cipher->prov != skey->skeymgmt->prov) { |
563 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
564 | 0 | return 0; |
565 | 0 | } |
566 | | |
567 | 0 | if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) { |
568 | | /* |
569 | | * If this ctx was already set up for no padding then we need to tell |
570 | | * the new cipher about it. |
571 | | */ |
572 | 0 | if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) |
573 | 0 | return 0; |
574 | 0 | } |
575 | | |
576 | 0 | if (iv == NULL) |
577 | 0 | iv_len = 0; |
578 | | |
579 | | /* We have a data managed via key management, using the new callbacks */ |
580 | 0 | if (enc) { |
581 | 0 | if (ctx->cipher->einit_skey == NULL) { |
582 | | /* |
583 | | * When skey is NULL, it's a multiple-step init as the current API does. |
584 | | * Otherwise we try to fallback for providers that do not support SKEYs. |
585 | | */ |
586 | 0 | const unsigned char *keydata = NULL; |
587 | 0 | size_t keylen = 0; |
588 | |
|
589 | 0 | if (skey != NULL && !EVP_SKEY_get0_raw_key(skey, &keydata, &keylen)) { |
590 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
591 | 0 | return 0; |
592 | 0 | } |
593 | | |
594 | 0 | ret = ctx->cipher->einit(ctx->algctx, keydata, keylen, |
595 | 0 | iv, iv_len, params); |
596 | 0 | } else { |
597 | 0 | ret = ctx->cipher->einit_skey(ctx->algctx, |
598 | 0 | skey == NULL ? NULL : skey->keydata, |
599 | 0 | iv, iv_len, params); |
600 | 0 | } |
601 | 0 | } else { |
602 | 0 | if (ctx->cipher->dinit_skey == NULL) { |
603 | | /* |
604 | | * When skey is NULL, it's a multiple-step init as the current API does. |
605 | | * Otherwise we try to fallback for providers that do not support SKEYs. |
606 | | */ |
607 | 0 | const unsigned char *keydata = NULL; |
608 | 0 | size_t keylen = 0; |
609 | |
|
610 | 0 | if (skey != NULL && !EVP_SKEY_get0_raw_key(skey, &keydata, &keylen)) { |
611 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
612 | 0 | return 0; |
613 | 0 | } |
614 | | |
615 | 0 | ret = ctx->cipher->dinit(ctx->algctx, keydata, keylen, |
616 | 0 | iv, iv_len, params); |
617 | 0 | } else { |
618 | 0 | ret = ctx->cipher->dinit_skey(ctx->algctx, |
619 | 0 | skey == NULL ? NULL : skey->keydata, |
620 | 0 | iv, iv_len, params); |
621 | 0 | } |
622 | 0 | } |
623 | | |
624 | 0 | return ret; |
625 | 0 | } |
626 | | |
627 | | int EVP_CipherInit_SKEY(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
628 | | EVP_SKEY *skey, const unsigned char *iv, size_t iv_len, |
629 | | int enc, const OSSL_PARAM params[]) |
630 | 0 | { |
631 | 0 | return evp_cipher_init_skey_internal(ctx, cipher, skey, iv, iv_len, enc, params); |
632 | 0 | } |
633 | | |
634 | | int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
635 | | const unsigned char *key, const unsigned char *iv, |
636 | | int enc, const OSSL_PARAM params[]) |
637 | 57.4k | { |
638 | 57.4k | return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, 0, params); |
639 | 57.4k | } |
640 | | |
641 | | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
642 | | const unsigned char *key, const unsigned char *iv, int enc) |
643 | 0 | { |
644 | 0 | if (cipher != NULL) |
645 | 0 | EVP_CIPHER_CTX_reset(ctx); |
646 | 0 | return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, 0, NULL); |
647 | 0 | } |
648 | | |
649 | | int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
650 | | ENGINE *impl, const unsigned char *key, |
651 | | const unsigned char *iv, int enc) |
652 | 5.99M | { |
653 | 5.99M | return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, 0, NULL); |
654 | 5.99M | } |
655 | | |
656 | | int EVP_CipherPipelineEncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
657 | | const unsigned char *key, size_t keylen, |
658 | | size_t numpipes, |
659 | | const unsigned char **iv, size_t ivlen) |
660 | 0 | { |
661 | 0 | if (numpipes > EVP_MAX_PIPES) { |
662 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_TOO_MANY_PIPES); |
663 | 0 | return 0; |
664 | 0 | } |
665 | | |
666 | 0 | ctx->numpipes = numpipes; |
667 | |
|
668 | 0 | if (!evp_cipher_init_internal(ctx, cipher, NULL, NULL, NULL, 1, 1, |
669 | 0 | NULL)) |
670 | 0 | return 0; |
671 | | |
672 | 0 | if (ctx->cipher->p_einit == NULL) { |
673 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
674 | 0 | return 0; |
675 | 0 | } |
676 | | |
677 | 0 | return ctx->cipher->p_einit(ctx->algctx, |
678 | 0 | key, |
679 | 0 | keylen, |
680 | 0 | numpipes, |
681 | 0 | iv, |
682 | 0 | ivlen, |
683 | 0 | NULL); |
684 | 0 | } |
685 | | |
686 | | int EVP_CipherPipelineDecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
687 | | const unsigned char *key, size_t keylen, |
688 | | size_t numpipes, |
689 | | const unsigned char **iv, size_t ivlen) |
690 | 0 | { |
691 | 0 | if (numpipes > EVP_MAX_PIPES) { |
692 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_TOO_MANY_PIPES); |
693 | 0 | return 0; |
694 | 0 | } |
695 | | |
696 | 0 | ctx->numpipes = numpipes; |
697 | |
|
698 | 0 | if (!evp_cipher_init_internal(ctx, cipher, NULL, NULL, NULL, 0, 1, |
699 | 0 | NULL)) |
700 | 0 | return 0; |
701 | | |
702 | 0 | if (ctx->cipher->p_dinit == NULL) { |
703 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
704 | 0 | return 0; |
705 | 0 | } |
706 | | |
707 | 0 | return ctx->cipher->p_dinit(ctx->algctx, |
708 | 0 | key, |
709 | 0 | keylen, |
710 | 0 | numpipes, |
711 | 0 | iv, |
712 | 0 | ivlen, |
713 | 0 | NULL); |
714 | 0 | } |
715 | | |
716 | | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
717 | | const unsigned char *in, int inl) |
718 | 13.5M | { |
719 | 13.5M | if (ctx->encrypt) |
720 | 10.3M | return EVP_EncryptUpdate(ctx, out, outl, in, inl); |
721 | 3.22M | else |
722 | 3.22M | return EVP_DecryptUpdate(ctx, out, outl, in, inl); |
723 | 13.5M | } |
724 | | |
725 | | int EVP_CipherPipelineUpdate(EVP_CIPHER_CTX *ctx, |
726 | | unsigned char **out, size_t *outl, |
727 | | const size_t *outsize, |
728 | | const unsigned char **in, const size_t *inl) |
729 | 0 | { |
730 | 0 | size_t i; |
731 | |
|
732 | 0 | if (ossl_unlikely(outl == NULL || inl == NULL)) { |
733 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
734 | 0 | return 0; |
735 | 0 | } |
736 | | |
737 | 0 | if (ossl_unlikely(ctx->cipher == NULL)) { |
738 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
739 | 0 | return 0; |
740 | 0 | } |
741 | | |
742 | 0 | if (ossl_unlikely(ctx->cipher->prov == NULL)) { |
743 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
744 | 0 | return 0; |
745 | 0 | } |
746 | | |
747 | 0 | if (ossl_unlikely(ctx->cipher->p_cupdate == NULL)) { |
748 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
749 | 0 | return 0; |
750 | 0 | } |
751 | | |
752 | 0 | for (i = 0; i < ctx->numpipes; i++) |
753 | 0 | outl[i] = 0; |
754 | |
|
755 | 0 | return ctx->cipher->p_cupdate(ctx->algctx, ctx->numpipes, |
756 | 0 | out, outl, outsize, |
757 | 0 | in, inl); |
758 | 0 | } |
759 | | |
760 | | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
761 | 3.73M | { |
762 | 3.73M | if (ctx->encrypt) |
763 | 2.42M | return EVP_EncryptFinal_ex(ctx, out, outl); |
764 | 1.31M | else |
765 | 1.31M | return EVP_DecryptFinal_ex(ctx, out, outl); |
766 | 3.73M | } |
767 | | |
768 | | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
769 | 0 | { |
770 | 0 | if (ctx->encrypt) |
771 | 0 | return EVP_EncryptFinal(ctx, out, outl); |
772 | 0 | else |
773 | 0 | return EVP_DecryptFinal(ctx, out, outl); |
774 | 0 | } |
775 | | |
776 | | int EVP_CipherPipelineFinal(EVP_CIPHER_CTX *ctx, |
777 | | unsigned char **out, size_t *outl, |
778 | | const size_t *outsize) |
779 | 0 | { |
780 | 0 | size_t i; |
781 | |
|
782 | 0 | if (ossl_unlikely(outl == NULL)) { |
783 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
784 | 0 | return 0; |
785 | 0 | } |
786 | | |
787 | 0 | if (ossl_unlikely(ctx->cipher == NULL)) { |
788 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
789 | 0 | return 0; |
790 | 0 | } |
791 | | |
792 | 0 | if (ossl_unlikely(ctx->cipher->prov == NULL)) { |
793 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
794 | 0 | return 0; |
795 | 0 | } |
796 | | |
797 | 0 | if (ossl_unlikely(ctx->cipher->p_cfinal == NULL)) { |
798 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
799 | 0 | return 0; |
800 | 0 | } |
801 | | |
802 | 0 | for (i = 0; i < ctx->numpipes; i++) |
803 | 0 | outl[i] = 0; |
804 | |
|
805 | 0 | return ctx->cipher->p_cfinal(ctx->algctx, ctx->numpipes, |
806 | 0 | out, outl, outsize); |
807 | 0 | } |
808 | | |
809 | | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
810 | | const unsigned char *key, const unsigned char *iv) |
811 | 0 | { |
812 | 0 | return EVP_CipherInit(ctx, cipher, key, iv, 1); |
813 | 0 | } |
814 | | |
815 | | int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
816 | | ENGINE *impl, const unsigned char *key, |
817 | | const unsigned char *iv) |
818 | 40.6k | { |
819 | 40.6k | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); |
820 | 40.6k | } |
821 | | |
822 | | int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
823 | | const unsigned char *key, const unsigned char *iv, |
824 | | const OSSL_PARAM params[]) |
825 | 57.4k | { |
826 | 57.4k | return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params); |
827 | 57.4k | } |
828 | | |
829 | | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
830 | | const unsigned char *key, const unsigned char *iv) |
831 | 0 | { |
832 | 0 | return EVP_CipherInit(ctx, cipher, key, iv, 0); |
833 | 0 | } |
834 | | |
835 | | int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
836 | | ENGINE *impl, const unsigned char *key, |
837 | | const unsigned char *iv) |
838 | 1.13k | { |
839 | 1.13k | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); |
840 | 1.13k | } |
841 | | |
842 | | int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
843 | | const unsigned char *key, const unsigned char *iv, |
844 | | const OSSL_PARAM params[]) |
845 | 0 | { |
846 | 0 | return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params); |
847 | 0 | } |
848 | | |
849 | | /* |
850 | | * According to the letter of standard difference between pointers |
851 | | * is specified to be valid only within same object. This makes |
852 | | * it formally challenging to determine if input and output buffers |
853 | | * are not partially overlapping with standard pointer arithmetic. |
854 | | */ |
855 | | #ifdef PTRDIFF_T |
856 | | #undef PTRDIFF_T |
857 | | #endif |
858 | | #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE == 64 |
859 | | /* |
860 | | * Then we have VMS that distinguishes itself by adhering to |
861 | | * sizeof(size_t)==4 even in 64-bit builds, which means that |
862 | | * difference between two pointers might be truncated to 32 bits. |
863 | | * In the context one can even wonder how comparison for |
864 | | * equality is implemented. To be on the safe side we adhere to |
865 | | * PTRDIFF_T even for comparison for equality. |
866 | | */ |
867 | | #define PTRDIFF_T uint64_t |
868 | | #else |
869 | 0 | #define PTRDIFF_T size_t |
870 | | #endif |
871 | | |
872 | | int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len) |
873 | 0 | { |
874 | 0 | PTRDIFF_T diff = (PTRDIFF_T)ptr1 - (PTRDIFF_T)ptr2; |
875 | | /* |
876 | | * Check for partially overlapping buffers. [Binary logical |
877 | | * operations are used instead of boolean to minimize number |
878 | | * of conditional branches.] |
879 | | */ |
880 | 0 | int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | (diff > (0 - (PTRDIFF_T)len))); |
881 | |
|
882 | 0 | return overlapped; |
883 | 0 | } |
884 | | |
885 | | static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx, |
886 | | unsigned char *out, int *outl, |
887 | | const unsigned char *in, int inl) |
888 | 0 | { |
889 | 0 | int i, j, bl, cmpl = inl; |
890 | |
|
891 | 0 | if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) |
892 | 0 | cmpl = safe_div_round_up_int(cmpl, 8, NULL); |
893 | |
|
894 | 0 | bl = ctx->cipher->block_size; |
895 | |
|
896 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
897 | | /* If block size > 1 then the cipher will have to do this check */ |
898 | 0 | if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) { |
899 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
900 | 0 | return 0; |
901 | 0 | } |
902 | | |
903 | 0 | i = ctx->cipher->do_cipher(ctx, out, in, inl); |
904 | 0 | if (i < 0) |
905 | 0 | return 0; |
906 | 0 | else |
907 | 0 | *outl = i; |
908 | 0 | return 1; |
909 | 0 | } |
910 | | |
911 | 0 | if (inl <= 0) { |
912 | 0 | *outl = 0; |
913 | 0 | return inl == 0; |
914 | 0 | } |
915 | 0 | if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) { |
916 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
917 | 0 | return 0; |
918 | 0 | } |
919 | | |
920 | 0 | if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) { |
921 | 0 | if (ctx->cipher->do_cipher(ctx, out, in, inl)) { |
922 | 0 | *outl = inl; |
923 | 0 | return 1; |
924 | 0 | } else { |
925 | 0 | *outl = 0; |
926 | 0 | return 0; |
927 | 0 | } |
928 | 0 | } |
929 | 0 | i = ctx->buf_len; |
930 | 0 | OPENSSL_assert(bl <= (int)sizeof(ctx->buf)); |
931 | 0 | if (i != 0) { |
932 | 0 | if (bl - i > inl) { |
933 | 0 | memcpy(&(ctx->buf[i]), in, inl); |
934 | 0 | ctx->buf_len += inl; |
935 | 0 | *outl = 0; |
936 | 0 | return 1; |
937 | 0 | } else { |
938 | 0 | j = bl - i; |
939 | | |
940 | | /* |
941 | | * Once we've processed the first j bytes from in, the amount of |
942 | | * data left that is a multiple of the block length is: |
943 | | * (inl - j) & ~(bl - 1) |
944 | | * We must ensure that this amount of data, plus the one block that |
945 | | * we process from ctx->buf does not exceed INT_MAX |
946 | | */ |
947 | 0 | if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) { |
948 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW); |
949 | 0 | return 0; |
950 | 0 | } |
951 | 0 | memcpy(&(ctx->buf[i]), in, j); |
952 | 0 | inl -= j; |
953 | 0 | in += j; |
954 | 0 | if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl)) |
955 | 0 | return 0; |
956 | 0 | out += bl; |
957 | 0 | *outl = bl; |
958 | 0 | } |
959 | 0 | } else |
960 | 0 | *outl = 0; |
961 | 0 | i = inl & (bl - 1); |
962 | 0 | inl -= i; |
963 | 0 | if (inl > 0) { |
964 | 0 | if (!ctx->cipher->do_cipher(ctx, out, in, inl)) |
965 | 0 | return 0; |
966 | 0 | *outl += inl; |
967 | 0 | } |
968 | | |
969 | 0 | if (i != 0) |
970 | 0 | memcpy(ctx->buf, &(in[inl]), i); |
971 | 0 | ctx->buf_len = i; |
972 | 0 | return 1; |
973 | 0 | } |
974 | | |
975 | | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
976 | | const unsigned char *in, int inl) |
977 | 7.77M | { |
978 | 7.77M | int ret; |
979 | 7.77M | size_t soutl, inl_ = (size_t)inl; |
980 | 7.77M | int blocksize; |
981 | | |
982 | 7.77M | if (ossl_likely(outl != NULL)) { |
983 | 7.77M | *outl = 0; |
984 | 7.77M | } else { |
985 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
986 | 0 | return 0; |
987 | 0 | } |
988 | | |
989 | | /* Prevent accidental use of decryption context when encrypting */ |
990 | 7.77M | if (ossl_unlikely(!ctx->encrypt)) { |
991 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
992 | 0 | return 0; |
993 | 0 | } |
994 | | |
995 | 7.77M | if (ossl_unlikely(ctx->cipher == NULL)) { |
996 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
997 | 0 | return 0; |
998 | 0 | } |
999 | | |
1000 | 7.77M | if (ossl_unlikely(ctx->cipher->prov == NULL)) |
1001 | 0 | goto legacy; |
1002 | | |
1003 | 7.77M | blocksize = ctx->cipher->block_size; |
1004 | | |
1005 | 7.77M | if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) { |
1006 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
1007 | 0 | return 0; |
1008 | 0 | } |
1009 | | |
1010 | 7.77M | ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl, |
1011 | 7.77M | inl_ + (size_t)(blocksize == 1 ? 0 : blocksize), |
1012 | 7.77M | in, inl_); |
1013 | | |
1014 | 7.77M | if (ossl_likely(ret)) { |
1015 | 7.77M | if (ossl_unlikely(soutl > INT_MAX)) { |
1016 | 3 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
1017 | 3 | return 0; |
1018 | 3 | } |
1019 | 7.77M | *outl = (int)soutl; |
1020 | 7.77M | } |
1021 | | |
1022 | 7.77M | return ret; |
1023 | | |
1024 | | /* Code below to be removed when legacy support is dropped. */ |
1025 | 0 | legacy: |
1026 | |
|
1027 | 0 | return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl); |
1028 | 7.77M | } |
1029 | | |
1030 | | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
1031 | 37 | { |
1032 | 37 | int ret; |
1033 | 37 | ret = EVP_EncryptFinal_ex(ctx, out, outl); |
1034 | 37 | return ret; |
1035 | 37 | } |
1036 | | |
1037 | | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
1038 | 1.76M | { |
1039 | 1.76M | int n, ret; |
1040 | 1.76M | unsigned int i, b, bl; |
1041 | 1.76M | size_t soutl; |
1042 | 1.76M | int blocksize; |
1043 | | |
1044 | 1.76M | if (outl != NULL) { |
1045 | 1.76M | *outl = 0; |
1046 | 1.76M | } else { |
1047 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1048 | 0 | return 0; |
1049 | 0 | } |
1050 | | |
1051 | | /* Prevent accidental use of decryption context when encrypting */ |
1052 | 1.76M | if (!ctx->encrypt) { |
1053 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1054 | 0 | return 0; |
1055 | 0 | } |
1056 | | |
1057 | 1.76M | if (ctx->cipher == NULL) { |
1058 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
1059 | 0 | return 0; |
1060 | 0 | } |
1061 | 1.76M | if (ctx->cipher->prov == NULL) |
1062 | 0 | goto legacy; |
1063 | | |
1064 | 1.76M | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
1065 | | |
1066 | 1.76M | if (blocksize < 1 || ctx->cipher->cfinal == NULL) { |
1067 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
1068 | 0 | return 0; |
1069 | 0 | } |
1070 | | |
1071 | 1.76M | ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl, |
1072 | 1.76M | blocksize == 1 ? 0 : blocksize); |
1073 | | |
1074 | 1.76M | if (ret) { |
1075 | 1.76M | if (soutl > INT_MAX) { |
1076 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
1077 | 0 | return 0; |
1078 | 0 | } |
1079 | 1.76M | *outl = (int)soutl; |
1080 | 1.76M | } |
1081 | | |
1082 | 1.76M | return ret; |
1083 | | |
1084 | | /* Code below to be removed when legacy support is dropped. */ |
1085 | 0 | legacy: |
1086 | |
|
1087 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
1088 | 0 | ret = ctx->cipher->do_cipher(ctx, out, NULL, 0); |
1089 | 0 | if (ret < 0) |
1090 | 0 | return 0; |
1091 | 0 | else |
1092 | 0 | *outl = ret; |
1093 | 0 | return 1; |
1094 | 0 | } |
1095 | | |
1096 | 0 | b = ctx->cipher->block_size; |
1097 | 0 | OPENSSL_assert(b <= sizeof(ctx->buf)); |
1098 | 0 | if (b == 1) { |
1099 | 0 | *outl = 0; |
1100 | 0 | return 1; |
1101 | 0 | } |
1102 | 0 | bl = ctx->buf_len; |
1103 | 0 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
1104 | 0 | if (bl) { |
1105 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
1106 | 0 | return 0; |
1107 | 0 | } |
1108 | 0 | *outl = 0; |
1109 | 0 | return 1; |
1110 | 0 | } |
1111 | | |
1112 | 0 | n = b - bl; |
1113 | 0 | for (i = bl; i < b; i++) |
1114 | 0 | ctx->buf[i] = n; |
1115 | 0 | ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b); |
1116 | |
|
1117 | 0 | if (ret) |
1118 | 0 | *outl = b; |
1119 | |
|
1120 | 0 | return ret; |
1121 | 0 | } |
1122 | | |
1123 | | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
1124 | | const unsigned char *in, int inl) |
1125 | 763k | { |
1126 | 763k | int fix_len, cmpl = inl, ret; |
1127 | 763k | unsigned int b; |
1128 | 763k | size_t soutl, inl_ = (size_t)inl; |
1129 | 763k | int blocksize; |
1130 | | |
1131 | 763k | if (ossl_likely(outl != NULL)) { |
1132 | 763k | *outl = 0; |
1133 | 763k | } else { |
1134 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1135 | 0 | return 0; |
1136 | 0 | } |
1137 | | |
1138 | | /* Prevent accidental use of encryption context when decrypting */ |
1139 | 763k | if (ossl_unlikely(ctx->encrypt)) { |
1140 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1141 | 0 | return 0; |
1142 | 0 | } |
1143 | | |
1144 | 763k | if (ossl_unlikely(ctx->cipher == NULL)) { |
1145 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
1146 | 0 | return 0; |
1147 | 0 | } |
1148 | 763k | if (ossl_unlikely(ctx->cipher->prov == NULL)) |
1149 | 0 | goto legacy; |
1150 | | |
1151 | 763k | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
1152 | | |
1153 | 763k | if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) { |
1154 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
1155 | 0 | return 0; |
1156 | 0 | } |
1157 | 763k | ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl, |
1158 | 763k | inl_ + (size_t)(blocksize == 1 ? 0 : blocksize), |
1159 | 763k | in, inl_); |
1160 | | |
1161 | 763k | if (ossl_likely(ret)) { |
1162 | 748k | if (ossl_unlikely(soutl > INT_MAX)) { |
1163 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
1164 | 0 | return 0; |
1165 | 0 | } |
1166 | 748k | *outl = (int)soutl; |
1167 | 748k | } |
1168 | | |
1169 | 763k | return ret; |
1170 | | |
1171 | | /* Code below to be removed when legacy support is dropped. */ |
1172 | 0 | legacy: |
1173 | |
|
1174 | 0 | b = ctx->cipher->block_size; |
1175 | |
|
1176 | 0 | if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) |
1177 | 0 | cmpl = safe_div_round_up_int(cmpl, 8, NULL); |
1178 | |
|
1179 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
1180 | 0 | if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) { |
1181 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
1182 | 0 | return 0; |
1183 | 0 | } |
1184 | | |
1185 | 0 | fix_len = ctx->cipher->do_cipher(ctx, out, in, inl); |
1186 | 0 | if (fix_len < 0) { |
1187 | 0 | *outl = 0; |
1188 | 0 | return 0; |
1189 | 0 | } else |
1190 | 0 | *outl = fix_len; |
1191 | 0 | return 1; |
1192 | 0 | } |
1193 | | |
1194 | 0 | if (inl <= 0) { |
1195 | 0 | *outl = 0; |
1196 | 0 | return inl == 0; |
1197 | 0 | } |
1198 | | |
1199 | 0 | if (ctx->flags & EVP_CIPH_NO_PADDING) |
1200 | 0 | return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl); |
1201 | | |
1202 | 0 | OPENSSL_assert(b <= sizeof(ctx->final)); |
1203 | |
|
1204 | 0 | if (ctx->final_used) { |
1205 | | /* see comment about PTRDIFF_T comparison above */ |
1206 | 0 | if (((PTRDIFF_T)out == (PTRDIFF_T)in) |
1207 | 0 | || ossl_is_partially_overlapping(out, in, b)) { |
1208 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
1209 | 0 | return 0; |
1210 | 0 | } |
1211 | | /* |
1212 | | * final_used is only ever set if buf_len is 0. Therefore the maximum |
1213 | | * length output we will ever see from evp_EncryptDecryptUpdate is |
1214 | | * the maximum multiple of the block length that is <= inl, or just: |
1215 | | * inl & ~(b - 1) |
1216 | | * Since final_used has been set then the final output length is: |
1217 | | * (inl & ~(b - 1)) + b |
1218 | | * This must never exceed INT_MAX |
1219 | | */ |
1220 | 0 | if ((inl & ~(b - 1)) > INT_MAX - b) { |
1221 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW); |
1222 | 0 | return 0; |
1223 | 0 | } |
1224 | 0 | memcpy(out, ctx->final, b); |
1225 | 0 | out += b; |
1226 | 0 | fix_len = 1; |
1227 | 0 | } else |
1228 | 0 | fix_len = 0; |
1229 | | |
1230 | 0 | if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl)) |
1231 | 0 | return 0; |
1232 | | |
1233 | | /* |
1234 | | * if we have 'decrypted' a multiple of block size, make sure we have a |
1235 | | * copy of this last block |
1236 | | */ |
1237 | 0 | if (b > 1 && !ctx->buf_len) { |
1238 | 0 | *outl -= b; |
1239 | 0 | ctx->final_used = 1; |
1240 | 0 | memcpy(ctx->final, &out[*outl], b); |
1241 | 0 | } else |
1242 | 0 | ctx->final_used = 0; |
1243 | |
|
1244 | 0 | if (fix_len) |
1245 | 0 | *outl += b; |
1246 | |
|
1247 | 0 | return 1; |
1248 | 0 | } |
1249 | | |
1250 | | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
1251 | 1.03k | { |
1252 | 1.03k | int ret; |
1253 | 1.03k | ret = EVP_DecryptFinal_ex(ctx, out, outl); |
1254 | 1.03k | return ret; |
1255 | 1.03k | } |
1256 | | |
1257 | | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
1258 | 708k | { |
1259 | 708k | int i, n; |
1260 | 708k | unsigned int b; |
1261 | 708k | size_t soutl; |
1262 | 708k | int ret; |
1263 | 708k | int blocksize; |
1264 | | |
1265 | 708k | if (outl != NULL) { |
1266 | 708k | *outl = 0; |
1267 | 708k | } else { |
1268 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1269 | 0 | return 0; |
1270 | 0 | } |
1271 | | |
1272 | | /* Prevent accidental use of encryption context when decrypting */ |
1273 | 708k | if (ctx->encrypt) { |
1274 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1275 | 0 | return 0; |
1276 | 0 | } |
1277 | | |
1278 | 708k | if (ctx->cipher == NULL) { |
1279 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
1280 | 0 | return 0; |
1281 | 0 | } |
1282 | | |
1283 | 708k | if (ctx->cipher->prov == NULL) |
1284 | 0 | goto legacy; |
1285 | | |
1286 | 708k | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
1287 | | |
1288 | 708k | if (blocksize < 1 || ctx->cipher->cfinal == NULL) { |
1289 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
1290 | 0 | return 0; |
1291 | 0 | } |
1292 | | |
1293 | 708k | ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl, |
1294 | 708k | blocksize == 1 ? 0 : blocksize); |
1295 | | |
1296 | 708k | if (ret) { |
1297 | 1.37k | if (soutl > INT_MAX) { |
1298 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
1299 | 0 | return 0; |
1300 | 0 | } |
1301 | 1.37k | *outl = (int)soutl; |
1302 | 1.37k | } |
1303 | | |
1304 | 708k | return ret; |
1305 | | |
1306 | | /* Code below to be removed when legacy support is dropped. */ |
1307 | 0 | legacy: |
1308 | |
|
1309 | 0 | *outl = 0; |
1310 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
1311 | 0 | i = ctx->cipher->do_cipher(ctx, out, NULL, 0); |
1312 | 0 | if (i < 0) |
1313 | 0 | return 0; |
1314 | 0 | else |
1315 | 0 | *outl = i; |
1316 | 0 | return 1; |
1317 | 0 | } |
1318 | | |
1319 | 0 | b = ctx->cipher->block_size; |
1320 | 0 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
1321 | 0 | if (ctx->buf_len) { |
1322 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
1323 | 0 | return 0; |
1324 | 0 | } |
1325 | 0 | *outl = 0; |
1326 | 0 | return 1; |
1327 | 0 | } |
1328 | 0 | if (b > 1) { |
1329 | 0 | if (ctx->buf_len || !ctx->final_used) { |
1330 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH); |
1331 | 0 | return 0; |
1332 | 0 | } |
1333 | 0 | OPENSSL_assert(b <= sizeof(ctx->final)); |
1334 | | |
1335 | | /* |
1336 | | * The following assumes that the ciphertext has been authenticated. |
1337 | | * Otherwise it provides a padding oracle. |
1338 | | */ |
1339 | 0 | n = ctx->final[b - 1]; |
1340 | 0 | if (n == 0 || n > (int)b) { |
1341 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT); |
1342 | 0 | return 0; |
1343 | 0 | } |
1344 | 0 | for (i = 0; i < n; i++) { |
1345 | 0 | if (ctx->final[--b] != n) { |
1346 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT); |
1347 | 0 | return 0; |
1348 | 0 | } |
1349 | 0 | } |
1350 | 0 | n = ctx->cipher->block_size - n; |
1351 | 0 | for (i = 0; i < n; i++) |
1352 | 0 | out[i] = ctx->final[i]; |
1353 | 0 | *outl = n; |
1354 | 0 | } |
1355 | 0 | return 1; |
1356 | 0 | } |
1357 | | |
1358 | | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) |
1359 | 222 | { |
1360 | 222 | if (c->cipher->prov != NULL) { |
1361 | 222 | int ok; |
1362 | 222 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
1363 | 222 | size_t len; |
1364 | | |
1365 | 222 | if (EVP_CIPHER_CTX_get_key_length(c) == keylen) |
1366 | 62 | return 1; |
1367 | | |
1368 | | /* Check the cipher actually understands this parameter */ |
1369 | 160 | if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher), |
1370 | 160 | OSSL_CIPHER_PARAM_KEYLEN) |
1371 | 160 | == NULL) { |
1372 | 106 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); |
1373 | 106 | return 0; |
1374 | 106 | } |
1375 | | |
1376 | 54 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len); |
1377 | 54 | if (!OSSL_PARAM_set_int(params, keylen)) |
1378 | 0 | return 0; |
1379 | 54 | ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params); |
1380 | 54 | if (ok <= 0) |
1381 | 54 | return 0; |
1382 | 0 | c->key_len = keylen; |
1383 | 0 | return 1; |
1384 | 54 | } |
1385 | | |
1386 | | /* Code below to be removed when legacy support is dropped. */ |
1387 | | |
1388 | | /* |
1389 | | * Note there have never been any built-in ciphers that define this flag |
1390 | | * since it was first introduced. |
1391 | | */ |
1392 | 0 | if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) |
1393 | 0 | return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL); |
1394 | 0 | if (EVP_CIPHER_CTX_get_key_length(c) == keylen) |
1395 | 0 | return 1; |
1396 | 0 | if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { |
1397 | 0 | c->key_len = keylen; |
1398 | 0 | return 1; |
1399 | 0 | } |
1400 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); |
1401 | 0 | return 0; |
1402 | 0 | } |
1403 | | |
1404 | | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) |
1405 | 1.27k | { |
1406 | 1.27k | int ok; |
1407 | 1.27k | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
1408 | 1.27k | unsigned int pd = pad; |
1409 | | |
1410 | 1.27k | if (pad) |
1411 | 0 | ctx->flags &= ~EVP_CIPH_NO_PADDING; |
1412 | 1.27k | else |
1413 | 1.27k | ctx->flags |= EVP_CIPH_NO_PADDING; |
1414 | | |
1415 | 1.27k | if (ctx->cipher != NULL && ctx->cipher->prov == NULL) |
1416 | 0 | return 1; |
1417 | 1.27k | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd); |
1418 | 1.27k | ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1419 | | |
1420 | 1.27k | return ok != 0; |
1421 | 1.27k | } |
1422 | | |
1423 | | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) |
1424 | 1.81M | { |
1425 | 1.81M | int ret = EVP_CTRL_RET_UNSUPPORTED; |
1426 | 1.81M | int set_params = 1; |
1427 | 1.81M | size_t sz = arg; |
1428 | 1.81M | unsigned int i; |
1429 | 1.81M | OSSL_PARAM params[4] = { |
1430 | 1.81M | OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END |
1431 | 1.81M | }; |
1432 | | |
1433 | 1.81M | if (ctx == NULL || ctx->cipher == NULL) { |
1434 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
1435 | 0 | return 0; |
1436 | 0 | } |
1437 | | |
1438 | 1.81M | if (ctx->cipher->prov == NULL) |
1439 | 0 | goto legacy; |
1440 | | |
1441 | 1.81M | switch (type) { |
1442 | 0 | case EVP_CTRL_SET_KEY_LENGTH: |
1443 | 0 | if (arg < 0) |
1444 | 0 | return 0; |
1445 | 0 | if (ctx->key_len == arg) |
1446 | | /* Skip calling into provider if unchanged. */ |
1447 | 0 | return 1; |
1448 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz); |
1449 | 0 | ctx->key_len = -1; |
1450 | 0 | break; |
1451 | 0 | case EVP_CTRL_RAND_KEY: /* Used by DES */ |
1452 | 0 | set_params = 0; |
1453 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, |
1454 | 0 | ptr, sz); |
1455 | 0 | break; |
1456 | | |
1457 | 0 | case EVP_CTRL_INIT: |
1458 | | /* |
1459 | | * EVP_CTRL_INIT is purely legacy, no provider counterpart. |
1460 | | * As a matter of fact, this should be dead code, but some caller |
1461 | | * might still do a direct control call with this command, so... |
1462 | | * Legacy methods return 1 except for exceptional circumstances, so |
1463 | | * we do the same here to not be disruptive. |
1464 | | */ |
1465 | 0 | return 1; |
1466 | 0 | case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */ |
1467 | 0 | default: |
1468 | 0 | goto end; |
1469 | 3.88k | case EVP_CTRL_AEAD_SET_IVLEN: |
1470 | 3.88k | if (arg < 0) |
1471 | 0 | return 0; |
1472 | 3.88k | if (ctx->iv_len == arg) |
1473 | | /* Skip calling into provider if unchanged. */ |
1474 | 0 | return 1; |
1475 | 3.88k | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz); |
1476 | 3.88k | ctx->iv_len = -1; |
1477 | 3.88k | break; |
1478 | 0 | case EVP_CTRL_CCM_SET_L: |
1479 | 0 | if (arg < 2 || arg > 8) |
1480 | 0 | return 0; |
1481 | 0 | sz = 15 - arg; |
1482 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz); |
1483 | 0 | ctx->iv_len = -1; |
1484 | 0 | break; |
1485 | 1.39k | case EVP_CTRL_AEAD_SET_IV_FIXED: |
1486 | 1.39k | params[0] = OSSL_PARAM_construct_octet_string( |
1487 | 1.39k | OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz); |
1488 | 1.39k | break; |
1489 | 0 | case EVP_CTRL_GCM_IV_GEN: |
1490 | 0 | set_params = 0; |
1491 | 0 | if (arg < 0) |
1492 | 0 | sz = 0; /* special case that uses the iv length */ |
1493 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1494 | 0 | OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz); |
1495 | 0 | break; |
1496 | 0 | case EVP_CTRL_GCM_SET_IV_INV: |
1497 | 0 | if (arg < 0) |
1498 | 0 | return 0; |
1499 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1500 | 0 | OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz); |
1501 | 0 | break; |
1502 | 0 | case EVP_CTRL_GET_RC5_ROUNDS: |
1503 | 0 | set_params = 0; /* Fall thru */ |
1504 | 0 | case EVP_CTRL_SET_RC5_ROUNDS: |
1505 | 0 | if (arg < 0) |
1506 | 0 | return 0; |
1507 | 0 | i = (unsigned int)arg; |
1508 | 0 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i); |
1509 | 0 | break; |
1510 | 0 | case EVP_CTRL_SET_SPEED: |
1511 | 0 | if (arg < 0) |
1512 | 0 | return 0; |
1513 | 0 | i = (unsigned int)arg; |
1514 | 0 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i); |
1515 | 0 | break; |
1516 | 1.16M | case EVP_CTRL_AEAD_GET_TAG: |
1517 | 1.16M | set_params = 0; /* Fall thru */ |
1518 | 1.76M | case EVP_CTRL_AEAD_SET_TAG: |
1519 | 1.76M | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, |
1520 | 1.76M | ptr, sz); |
1521 | 1.76M | break; |
1522 | 42.9k | case EVP_CTRL_AEAD_TLS1_AAD: |
1523 | | /* This one does a set and a get - since it returns a size */ |
1524 | 42.9k | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, |
1525 | 42.9k | ptr, sz); |
1526 | 42.9k | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1527 | 42.9k | if (ret <= 0) |
1528 | 106 | goto end; |
1529 | 42.8k | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz); |
1530 | 42.8k | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1531 | 42.8k | if (ret <= 0) |
1532 | 0 | goto end; |
1533 | 42.8k | if (sz > INT_MAX) |
1534 | 0 | return 0; |
1535 | 42.8k | return (int)sz; |
1536 | 0 | #ifndef OPENSSL_NO_RC2 |
1537 | 0 | case EVP_CTRL_GET_RC2_KEY_BITS: |
1538 | 0 | set_params = 0; /* Fall thru */ |
1539 | 0 | case EVP_CTRL_SET_RC2_KEY_BITS: |
1540 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz); |
1541 | 0 | break; |
1542 | 0 | #endif /* OPENSSL_NO_RC2 */ |
1543 | 0 | #if !defined(OPENSSL_NO_MULTIBLOCK) |
1544 | 0 | case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE: |
1545 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1546 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz); |
1547 | 0 | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1548 | 0 | if (ret <= 0) |
1549 | 0 | return 0; |
1550 | | |
1551 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1552 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz); |
1553 | 0 | params[1] = OSSL_PARAM_construct_end(); |
1554 | 0 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1555 | 0 | if (ret <= 0 || sz > INT_MAX) |
1556 | 0 | return 0; |
1557 | 0 | return (int)sz; |
1558 | 0 | case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: { |
1559 | 0 | EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr; |
1560 | |
|
1561 | 0 | if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM)) |
1562 | 0 | return 0; |
1563 | | |
1564 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1565 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void *)p->inp, p->len); |
1566 | 0 | params[1] = OSSL_PARAM_construct_uint( |
1567 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); |
1568 | 0 | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1569 | 0 | if (ret <= 0) |
1570 | 0 | return ret; |
1571 | | /* Retrieve the return values changed by the set */ |
1572 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1573 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz); |
1574 | 0 | params[1] = OSSL_PARAM_construct_uint( |
1575 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); |
1576 | 0 | params[2] = OSSL_PARAM_construct_end(); |
1577 | 0 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1578 | 0 | if (ret <= 0 || sz > INT_MAX) |
1579 | 0 | return 0; |
1580 | 0 | return (int)sz; |
1581 | 0 | } |
1582 | 0 | case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: { |
1583 | 0 | EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr; |
1584 | |
|
1585 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1586 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len); |
1587 | |
|
1588 | 0 | params[1] = OSSL_PARAM_construct_octet_string( |
1589 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void *)p->inp, |
1590 | 0 | p->len); |
1591 | 0 | params[2] = OSSL_PARAM_construct_uint( |
1592 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); |
1593 | 0 | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1594 | 0 | if (ret <= 0) |
1595 | 0 | return ret; |
1596 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1597 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz); |
1598 | 0 | params[1] = OSSL_PARAM_construct_end(); |
1599 | 0 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1600 | 0 | if (ret <= 0 || sz > INT_MAX) |
1601 | 0 | return 0; |
1602 | 0 | return (int)sz; |
1603 | 0 | } |
1604 | 0 | #endif /* OPENSSL_NO_MULTIBLOCK */ |
1605 | 1.55k | case EVP_CTRL_AEAD_SET_MAC_KEY: |
1606 | 1.55k | if (arg < 0) |
1607 | 0 | return -1; |
1608 | 1.55k | params[0] = OSSL_PARAM_construct_octet_string( |
1609 | 1.55k | OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz); |
1610 | 1.55k | break; |
1611 | 1.81M | } |
1612 | | |
1613 | 1.77M | if (set_params) |
1614 | 616k | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1615 | 1.16M | else |
1616 | 1.16M | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1617 | 1.77M | goto end; |
1618 | | |
1619 | | /* Code below to be removed when legacy support is dropped. */ |
1620 | 0 | legacy: |
1621 | 0 | if (ctx->cipher->ctrl == NULL) { |
1622 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); |
1623 | 0 | return 0; |
1624 | 0 | } |
1625 | | |
1626 | 0 | ret = ctx->cipher->ctrl(ctx, type, arg, ptr); |
1627 | |
|
1628 | 1.77M | end: |
1629 | 1.77M | if (ret == EVP_CTRL_RET_UNSUPPORTED) { |
1630 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
1631 | 0 | return 0; |
1632 | 0 | } |
1633 | 1.77M | return ret; |
1634 | 1.77M | } |
1635 | | |
1636 | | int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[]) |
1637 | 1.68M | { |
1638 | 1.68M | if (cipher != NULL && cipher->get_params != NULL) |
1639 | 1.68M | return cipher->get_params(params); |
1640 | 0 | return 0; |
1641 | 1.68M | } |
1642 | | |
1643 | | int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[]) |
1644 | 27.1k | { |
1645 | 27.1k | int r = 0; |
1646 | 27.1k | const OSSL_PARAM *p; |
1647 | | |
1648 | 27.1k | if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) { |
1649 | 27.0k | r = ctx->cipher->set_ctx_params(ctx->algctx, params); |
1650 | 27.0k | if (r > 0) { |
1651 | 27.0k | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); |
1652 | 27.0k | if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) { |
1653 | 0 | r = 0; |
1654 | 0 | ctx->key_len = -1; |
1655 | 0 | } |
1656 | 27.0k | } |
1657 | 27.0k | if (r > 0) { |
1658 | 27.0k | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); |
1659 | 27.0k | if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) { |
1660 | 0 | r = 0; |
1661 | 0 | ctx->iv_len = -1; |
1662 | 0 | } |
1663 | 27.0k | } |
1664 | 27.0k | } |
1665 | 27.1k | return r; |
1666 | 27.1k | } |
1667 | | |
1668 | | int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[]) |
1669 | 427k | { |
1670 | 427k | if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL) |
1671 | 427k | return ctx->cipher->get_ctx_params(ctx->algctx, params); |
1672 | 0 | return 0; |
1673 | 427k | } |
1674 | | |
1675 | | const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher) |
1676 | 0 | { |
1677 | 0 | if (cipher != NULL && cipher->gettable_params != NULL) |
1678 | 0 | return cipher->gettable_params( |
1679 | 0 | ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher))); |
1680 | 0 | return NULL; |
1681 | 0 | } |
1682 | | |
1683 | | const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher) |
1684 | 338 | { |
1685 | 338 | void *provctx; |
1686 | | |
1687 | 338 | if (cipher != NULL && cipher->settable_ctx_params != NULL) { |
1688 | 338 | provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)); |
1689 | 338 | return cipher->settable_ctx_params(NULL, provctx); |
1690 | 338 | } |
1691 | 0 | return NULL; |
1692 | 338 | } |
1693 | | |
1694 | | const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher) |
1695 | 8.30k | { |
1696 | 8.30k | void *provctx; |
1697 | | |
1698 | 8.30k | if (cipher != NULL && cipher->gettable_ctx_params != NULL) { |
1699 | 8.30k | provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)); |
1700 | 8.30k | return cipher->gettable_ctx_params(NULL, provctx); |
1701 | 8.30k | } |
1702 | 0 | return NULL; |
1703 | 8.30k | } |
1704 | | |
1705 | | const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx) |
1706 | 0 | { |
1707 | 0 | void *alg; |
1708 | |
|
1709 | 0 | if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) { |
1710 | 0 | alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher)); |
1711 | 0 | return cctx->cipher->settable_ctx_params(cctx->algctx, alg); |
1712 | 0 | } |
1713 | 0 | return NULL; |
1714 | 0 | } |
1715 | | |
1716 | | const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx) |
1717 | 0 | { |
1718 | 0 | void *provctx; |
1719 | |
|
1720 | 0 | if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) { |
1721 | 0 | provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher)); |
1722 | 0 | return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx); |
1723 | 0 | } |
1724 | 0 | return NULL; |
1725 | 0 | } |
1726 | | |
1727 | | #ifndef FIPS_MODULE |
1728 | | static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx) |
1729 | 0 | { |
1730 | 0 | const EVP_CIPHER *cipher = ctx->cipher; |
1731 | 0 | const OSSL_PROVIDER *prov; |
1732 | |
|
1733 | 0 | if (cipher == NULL) |
1734 | 0 | return NULL; |
1735 | | |
1736 | 0 | prov = EVP_CIPHER_get0_provider(cipher); |
1737 | 0 | return ossl_provider_libctx(prov); |
1738 | 0 | } |
1739 | | #endif |
1740 | | |
1741 | | int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) |
1742 | 0 | { |
1743 | 0 | if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) |
1744 | 0 | return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); |
1745 | | |
1746 | | #ifdef FIPS_MODULE |
1747 | | return 0; |
1748 | | #else |
1749 | 0 | { |
1750 | 0 | int kl; |
1751 | 0 | OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx); |
1752 | |
|
1753 | 0 | kl = EVP_CIPHER_CTX_get_key_length(ctx); |
1754 | 0 | if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0) |
1755 | 0 | return 0; |
1756 | 0 | return 1; |
1757 | 0 | } |
1758 | 0 | #endif /* FIPS_MODULE */ |
1759 | 0 | } |
1760 | | |
1761 | | EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in) |
1762 | 0 | { |
1763 | 0 | EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new(); |
1764 | |
|
1765 | 0 | if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) { |
1766 | 0 | EVP_CIPHER_CTX_free(out); |
1767 | 0 | out = NULL; |
1768 | 0 | } |
1769 | 0 | return out; |
1770 | 0 | } |
1771 | | |
1772 | | int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) |
1773 | 66 | { |
1774 | 66 | if ((in == NULL) || (in->cipher == NULL)) { |
1775 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED); |
1776 | 0 | return 0; |
1777 | 0 | } |
1778 | | |
1779 | 66 | if (in->cipher->prov == NULL) |
1780 | 0 | goto legacy; |
1781 | | |
1782 | 66 | if (in->cipher->dupctx == NULL) { |
1783 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
1784 | 0 | return 0; |
1785 | 0 | } |
1786 | | |
1787 | 66 | EVP_CIPHER_CTX_reset(out); |
1788 | | |
1789 | 66 | *out = *in; |
1790 | 66 | out->algctx = NULL; |
1791 | | |
1792 | 66 | if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) { |
1793 | 0 | out->fetched_cipher = NULL; |
1794 | 0 | return 0; |
1795 | 0 | } |
1796 | | |
1797 | 66 | out->algctx = in->cipher->dupctx(in->algctx); |
1798 | 66 | if (out->algctx == NULL) { |
1799 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
1800 | 0 | return 0; |
1801 | 0 | } |
1802 | | |
1803 | 66 | return 1; |
1804 | | |
1805 | | /* Code below to be removed when legacy support is dropped. */ |
1806 | 0 | legacy: |
1807 | |
|
1808 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
1809 | | /* Make sure it's safe to copy a cipher context using an ENGINE */ |
1810 | 0 | if (in->engine && !ENGINE_init(in->engine)) { |
1811 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
1812 | 0 | return 0; |
1813 | 0 | } |
1814 | 0 | #endif |
1815 | | |
1816 | 0 | EVP_CIPHER_CTX_reset(out); |
1817 | 0 | memcpy(out, in, sizeof(*out)); |
1818 | |
|
1819 | 0 | if (in->cipher_data && in->cipher->ctx_size) { |
1820 | 0 | out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size); |
1821 | 0 | if (out->cipher_data == NULL) { |
1822 | 0 | out->cipher = NULL; |
1823 | 0 | return 0; |
1824 | 0 | } |
1825 | 0 | memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); |
1826 | 0 | } |
1827 | | |
1828 | 0 | if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) |
1829 | 0 | if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { |
1830 | 0 | out->cipher = NULL; |
1831 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
1832 | 0 | return 0; |
1833 | 0 | } |
1834 | 0 | return 1; |
1835 | 0 | } |
1836 | | |
1837 | | EVP_CIPHER *evp_cipher_new(void) |
1838 | 7.93k | { |
1839 | 7.93k | EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER)); |
1840 | | |
1841 | 7.93k | if (cipher != NULL && !CRYPTO_NEW_REF(&cipher->refcnt, 1)) { |
1842 | 0 | OPENSSL_free(cipher); |
1843 | 0 | return NULL; |
1844 | 0 | } |
1845 | 7.93k | return cipher; |
1846 | 7.93k | } |
1847 | | |
1848 | | /* |
1849 | | * FIPS module note: since internal fetches will be entirely |
1850 | | * provider based, we know that none of its code depends on legacy |
1851 | | * NIDs or any functionality that use them. |
1852 | | */ |
1853 | | #ifndef FIPS_MODULE |
1854 | | /* After removal of legacy support get rid of the need for legacy NIDs */ |
1855 | | static void set_legacy_nid(const char *name, void *vlegacy_nid) |
1856 | 15.5k | { |
1857 | 15.5k | int nid; |
1858 | 15.5k | int *legacy_nid = vlegacy_nid; |
1859 | | /* |
1860 | | * We use lowest level function to get the associated method, because |
1861 | | * higher level functions such as EVP_get_cipherbyname() have changed |
1862 | | * to look at providers too. |
1863 | | */ |
1864 | 15.5k | const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH); |
1865 | | |
1866 | 15.5k | if (*legacy_nid == -1) /* We found a clash already */ |
1867 | 0 | return; |
1868 | 15.5k | if (legacy_method == NULL) |
1869 | 6.95k | return; |
1870 | 8.56k | nid = EVP_CIPHER_get_nid(legacy_method); |
1871 | 8.56k | if (*legacy_nid != NID_undef && *legacy_nid != nid) { |
1872 | 0 | *legacy_nid = -1; |
1873 | 0 | return; |
1874 | 0 | } |
1875 | 8.56k | *legacy_nid = nid; |
1876 | 8.56k | } |
1877 | | #endif |
1878 | | |
1879 | | static void *evp_cipher_from_algorithm(const int name_id, |
1880 | | const OSSL_ALGORITHM *algodef, |
1881 | | OSSL_PROVIDER *prov) |
1882 | 5.59k | { |
1883 | 5.59k | const OSSL_DISPATCH *fns = algodef->implementation; |
1884 | 5.59k | EVP_CIPHER *cipher = NULL; |
1885 | 5.59k | int fnciphcnt = 0, encinit = 0, decinit = 0, fnpipecnt = 0, fnctxcnt = 0; |
1886 | | |
1887 | 5.59k | if ((cipher = evp_cipher_new()) == NULL) { |
1888 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
1889 | 0 | return NULL; |
1890 | 0 | } |
1891 | | |
1892 | 5.59k | #ifndef FIPS_MODULE |
1893 | 5.59k | cipher->nid = NID_undef; |
1894 | 5.59k | if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid) |
1895 | 5.59k | || cipher->nid == -1) { |
1896 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
1897 | 0 | goto err; |
1898 | 0 | } |
1899 | 5.59k | #endif |
1900 | | |
1901 | 5.59k | cipher->name_id = name_id; |
1902 | 5.59k | if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
1903 | 0 | goto err; |
1904 | | |
1905 | 5.59k | cipher->description = algodef->algorithm_description; |
1906 | | |
1907 | 89.1k | for (; fns->function_id != 0; fns++) { |
1908 | 83.5k | switch (fns->function_id) { |
1909 | 5.59k | case OSSL_FUNC_CIPHER_NEWCTX: |
1910 | 5.59k | if (cipher->newctx != NULL) |
1911 | 0 | break; |
1912 | 5.59k | cipher->newctx = OSSL_FUNC_cipher_newctx(fns); |
1913 | 5.59k | fnctxcnt++; |
1914 | 5.59k | break; |
1915 | 5.59k | case OSSL_FUNC_CIPHER_ENCRYPT_INIT: |
1916 | 5.59k | if (cipher->einit != NULL) |
1917 | 0 | break; |
1918 | 5.59k | cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns); |
1919 | 5.59k | encinit = 1; |
1920 | 5.59k | break; |
1921 | 5.59k | case OSSL_FUNC_CIPHER_DECRYPT_INIT: |
1922 | 5.59k | if (cipher->dinit != NULL) |
1923 | 0 | break; |
1924 | 5.59k | cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns); |
1925 | 5.59k | decinit = 1; |
1926 | 5.59k | break; |
1927 | 2.92k | case OSSL_FUNC_CIPHER_ENCRYPT_SKEY_INIT: |
1928 | 2.92k | if (cipher->einit_skey != NULL) |
1929 | 0 | break; |
1930 | 2.92k | cipher->einit_skey = OSSL_FUNC_cipher_encrypt_skey_init(fns); |
1931 | 2.92k | encinit = 1; |
1932 | 2.92k | break; |
1933 | 2.92k | case OSSL_FUNC_CIPHER_DECRYPT_SKEY_INIT: |
1934 | 2.92k | if (cipher->dinit_skey != NULL) |
1935 | 0 | break; |
1936 | 2.92k | cipher->dinit_skey = OSSL_FUNC_cipher_decrypt_skey_init(fns); |
1937 | 2.92k | decinit = 1; |
1938 | 2.92k | break; |
1939 | 5.59k | case OSSL_FUNC_CIPHER_UPDATE: |
1940 | 5.59k | if (cipher->cupdate != NULL) |
1941 | 0 | break; |
1942 | 5.59k | cipher->cupdate = OSSL_FUNC_cipher_update(fns); |
1943 | 5.59k | fnciphcnt++; |
1944 | 5.59k | break; |
1945 | 5.59k | case OSSL_FUNC_CIPHER_FINAL: |
1946 | 5.59k | if (cipher->cfinal != NULL) |
1947 | 0 | break; |
1948 | 5.59k | cipher->cfinal = OSSL_FUNC_cipher_final(fns); |
1949 | 5.59k | fnciphcnt++; |
1950 | 5.59k | break; |
1951 | 5.07k | case OSSL_FUNC_CIPHER_CIPHER: |
1952 | 5.07k | if (cipher->ccipher != NULL) |
1953 | 0 | break; |
1954 | 5.07k | cipher->ccipher = OSSL_FUNC_cipher_cipher(fns); |
1955 | 5.07k | break; |
1956 | 0 | case OSSL_FUNC_CIPHER_PIPELINE_ENCRYPT_INIT: |
1957 | 0 | if (cipher->p_einit != NULL) |
1958 | 0 | break; |
1959 | 0 | cipher->p_einit = OSSL_FUNC_cipher_pipeline_encrypt_init(fns); |
1960 | 0 | fnpipecnt++; |
1961 | 0 | break; |
1962 | 0 | case OSSL_FUNC_CIPHER_PIPELINE_DECRYPT_INIT: |
1963 | 0 | if (cipher->p_dinit != NULL) |
1964 | 0 | break; |
1965 | 0 | cipher->p_dinit = OSSL_FUNC_cipher_pipeline_decrypt_init(fns); |
1966 | 0 | fnpipecnt++; |
1967 | 0 | break; |
1968 | 0 | case OSSL_FUNC_CIPHER_PIPELINE_UPDATE: |
1969 | 0 | if (cipher->p_cupdate != NULL) |
1970 | 0 | break; |
1971 | 0 | cipher->p_cupdate = OSSL_FUNC_cipher_pipeline_update(fns); |
1972 | 0 | fnpipecnt++; |
1973 | 0 | break; |
1974 | 0 | case OSSL_FUNC_CIPHER_PIPELINE_FINAL: |
1975 | 0 | if (cipher->p_cfinal != NULL) |
1976 | 0 | break; |
1977 | 0 | cipher->p_cfinal = OSSL_FUNC_cipher_pipeline_final(fns); |
1978 | 0 | fnpipecnt++; |
1979 | 0 | break; |
1980 | 5.59k | case OSSL_FUNC_CIPHER_FREECTX: |
1981 | 5.59k | if (cipher->freectx != NULL) |
1982 | 0 | break; |
1983 | 5.59k | cipher->freectx = OSSL_FUNC_cipher_freectx(fns); |
1984 | 5.59k | fnctxcnt++; |
1985 | 5.59k | break; |
1986 | 5.54k | case OSSL_FUNC_CIPHER_DUPCTX: |
1987 | 5.54k | if (cipher->dupctx != NULL) |
1988 | 0 | break; |
1989 | 5.54k | cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns); |
1990 | 5.54k | break; |
1991 | 5.59k | case OSSL_FUNC_CIPHER_GET_PARAMS: |
1992 | 5.59k | if (cipher->get_params != NULL) |
1993 | 0 | break; |
1994 | 5.59k | cipher->get_params = OSSL_FUNC_cipher_get_params(fns); |
1995 | 5.59k | break; |
1996 | 5.59k | case OSSL_FUNC_CIPHER_GET_CTX_PARAMS: |
1997 | 5.59k | if (cipher->get_ctx_params != NULL) |
1998 | 0 | break; |
1999 | 5.59k | cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns); |
2000 | 5.59k | break; |
2001 | 5.59k | case OSSL_FUNC_CIPHER_SET_CTX_PARAMS: |
2002 | 5.59k | if (cipher->set_ctx_params != NULL) |
2003 | 0 | break; |
2004 | 5.59k | cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns); |
2005 | 5.59k | break; |
2006 | 5.59k | case OSSL_FUNC_CIPHER_GETTABLE_PARAMS: |
2007 | 5.59k | if (cipher->gettable_params != NULL) |
2008 | 0 | break; |
2009 | 5.59k | cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns); |
2010 | 5.59k | break; |
2011 | 5.59k | case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS: |
2012 | 5.59k | if (cipher->gettable_ctx_params != NULL) |
2013 | 0 | break; |
2014 | 5.59k | cipher->gettable_ctx_params = OSSL_FUNC_cipher_gettable_ctx_params(fns); |
2015 | 5.59k | break; |
2016 | 5.59k | case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS: |
2017 | 5.59k | if (cipher->settable_ctx_params != NULL) |
2018 | 0 | break; |
2019 | 5.59k | cipher->settable_ctx_params = OSSL_FUNC_cipher_settable_ctx_params(fns); |
2020 | 5.59k | break; |
2021 | 83.5k | } |
2022 | 83.5k | } |
2023 | 5.59k | fnciphcnt += encinit + decinit; |
2024 | 5.59k | if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4) |
2025 | 5.59k | || (fnciphcnt == 0 && cipher->ccipher == NULL && fnpipecnt == 0) |
2026 | 5.59k | || (fnpipecnt != 0 && (fnpipecnt < 3 || cipher->p_cupdate == NULL || cipher->p_cfinal == NULL)) |
2027 | 5.59k | || fnctxcnt != 2) { |
2028 | | /* |
2029 | | * In order to be a consistent set of functions we must have at least |
2030 | | * a complete set of "encrypt" functions, or a complete set of "decrypt" |
2031 | | * functions, or a single "cipher" function. In all cases we need both |
2032 | | * the "newctx" and "freectx" functions. |
2033 | | */ |
2034 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
2035 | 0 | goto err; |
2036 | 0 | } |
2037 | 5.59k | if (prov != NULL && !ossl_provider_up_ref(prov)) |
2038 | 0 | goto err; |
2039 | | |
2040 | 5.59k | cipher->prov = prov; |
2041 | | |
2042 | 5.59k | if (!evp_cipher_cache_constants(cipher)) { |
2043 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED); |
2044 | 0 | goto err; |
2045 | 0 | } |
2046 | | |
2047 | 5.59k | return cipher; |
2048 | | |
2049 | 0 | err: |
2050 | 0 | EVP_CIPHER_free(cipher); |
2051 | 0 | return NULL; |
2052 | 5.59k | } |
2053 | | |
2054 | | static int evp_cipher_up_ref(void *cipher) |
2055 | 3.15M | { |
2056 | 3.15M | return EVP_CIPHER_up_ref(cipher); |
2057 | 3.15M | } |
2058 | | |
2059 | | static void evp_cipher_free(void *cipher) |
2060 | 14.4k | { |
2061 | 14.4k | EVP_CIPHER_free(cipher); |
2062 | 14.4k | } |
2063 | | |
2064 | | EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
2065 | | const char *properties) |
2066 | 4.62M | { |
2067 | 4.62M | EVP_CIPHER *cipher = evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties, |
2068 | 4.62M | evp_cipher_from_algorithm, evp_cipher_up_ref, |
2069 | 4.62M | evp_cipher_free); |
2070 | | |
2071 | 4.62M | return cipher; |
2072 | 4.62M | } |
2073 | | |
2074 | | EVP_CIPHER *evp_cipher_fetch_from_prov(OSSL_PROVIDER *prov, |
2075 | | const char *algorithm, |
2076 | | const char *properties) |
2077 | 70 | { |
2078 | 70 | return evp_generic_fetch_from_prov(prov, OSSL_OP_CIPHER, |
2079 | 70 | algorithm, properties, |
2080 | 70 | evp_cipher_from_algorithm, |
2081 | 70 | evp_cipher_up_ref, |
2082 | 70 | evp_cipher_free); |
2083 | 70 | } |
2084 | | |
2085 | | int EVP_CIPHER_can_pipeline(const EVP_CIPHER *cipher, int enc) |
2086 | 0 | { |
2087 | 0 | if (((enc && cipher->p_einit != NULL) || (!enc && cipher->p_dinit != NULL)) |
2088 | 0 | && cipher->p_cupdate != NULL && cipher->p_cfinal != NULL) |
2089 | 0 | return 1; |
2090 | | |
2091 | 0 | return 0; |
2092 | 0 | } |
2093 | | |
2094 | | int EVP_CIPHER_up_ref(EVP_CIPHER *cipher) |
2095 | 4.08M | { |
2096 | 4.08M | int ref = 0; |
2097 | | |
2098 | 4.08M | if (cipher->origin == EVP_ORIG_DYNAMIC) |
2099 | 4.08M | CRYPTO_UP_REF(&cipher->refcnt, &ref); |
2100 | 4.08M | return 1; |
2101 | 4.08M | } |
2102 | | |
2103 | | void evp_cipher_free_int(EVP_CIPHER *cipher) |
2104 | 5.57k | { |
2105 | 5.57k | OPENSSL_free(cipher->type_name); |
2106 | 5.57k | ossl_provider_free(cipher->prov); |
2107 | 5.57k | CRYPTO_FREE_REF(&cipher->refcnt); |
2108 | 5.57k | OPENSSL_free(cipher); |
2109 | 5.57k | } |
2110 | | |
2111 | | void EVP_CIPHER_free(EVP_CIPHER *cipher) |
2112 | 5.56M | { |
2113 | 5.56M | int i; |
2114 | | |
2115 | 5.56M | if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC) |
2116 | 1.47M | return; |
2117 | | |
2118 | 4.09M | CRYPTO_DOWN_REF(&cipher->refcnt, &i); |
2119 | 4.09M | if (i > 0) |
2120 | 4.08M | return; |
2121 | 5.57k | evp_cipher_free_int(cipher); |
2122 | 5.57k | } |
2123 | | |
2124 | | void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx, |
2125 | | void (*fn)(EVP_CIPHER *mac, void *arg), |
2126 | | void *arg) |
2127 | 8 | { |
2128 | 8 | evp_generic_do_all(libctx, OSSL_OP_CIPHER, |
2129 | 8 | (void (*)(void *, void *))fn, arg, |
2130 | 8 | evp_cipher_from_algorithm, evp_cipher_up_ref, |
2131 | 8 | evp_cipher_free); |
2132 | 8 | } |