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