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