/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 | 759k | { |
32 | 759k | if (ctx == NULL) |
33 | 0 | return 1; |
34 | | |
35 | 759k | if (ctx->cipher == NULL || ctx->cipher->prov == NULL) |
36 | 18.8k | goto legacy; |
37 | | |
38 | 740k | if (ctx->algctx != NULL) { |
39 | 740k | if (ctx->cipher->freectx != NULL) |
40 | 740k | ctx->cipher->freectx(ctx->algctx); |
41 | 740k | ctx->algctx = NULL; |
42 | 740k | } |
43 | 740k | if (ctx->fetched_cipher != NULL) |
44 | 740k | EVP_CIPHER_free(ctx->fetched_cipher); |
45 | 740k | memset(ctx, 0, sizeof(*ctx)); |
46 | 740k | ctx->iv_len = -1; |
47 | | |
48 | 740k | return 1; |
49 | | |
50 | | /* Remove legacy code below when legacy support is removed. */ |
51 | 18.8k | legacy: |
52 | | |
53 | 18.8k | 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.8k | OPENSSL_free(ctx->cipher_data); |
61 | 18.8k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
62 | 18.8k | ENGINE_finish(ctx->engine); |
63 | 18.8k | #endif |
64 | 18.8k | memset(ctx, 0, sizeof(*ctx)); |
65 | 18.8k | ctx->iv_len = -1; |
66 | 18.8k | return 1; |
67 | 18.8k | } |
68 | | |
69 | | EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) |
70 | 6.28k | { |
71 | 6.28k | return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); |
72 | 6.28k | } |
73 | | |
74 | | void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) |
75 | 1.73M | { |
76 | 1.73M | if (ctx == NULL) |
77 | 715k | return; |
78 | 1.01M | EVP_CIPHER_CTX_reset(ctx); |
79 | 1.01M | OPENSSL_free(ctx); |
80 | 1.01M | } |
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.61M | { |
88 | 3.61M | int n; |
89 | 3.61M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
90 | 3.61M | ENGINE *tmpimpl = NULL; |
91 | 3.61M | #endif |
92 | | |
93 | 3.61M | 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.61M | if (enc == -1) { |
101 | 208k | enc = ctx->encrypt; |
102 | 3.41M | } else { |
103 | 3.41M | if (enc) |
104 | 2.65M | enc = 1; |
105 | 3.41M | ctx->encrypt = enc; |
106 | 3.41M | } |
107 | | |
108 | 3.61M | 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.61M | #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.61M | if (ctx->engine && ctx->cipher |
123 | 0 | && (cipher == NULL || cipher->nid == ctx->cipher->nid)) |
124 | 0 | goto skip_to_init; |
125 | | |
126 | 3.61M | if (cipher != NULL && impl == NULL) { |
127 | | /* Ask if an ENGINE is reserved for this job */ |
128 | 396k | tmpimpl = ENGINE_get_cipher_engine(cipher->nid); |
129 | 396k | } |
130 | 3.61M | #endif |
131 | | |
132 | | /* |
133 | | * If there are engines involved then we should use legacy handling for now. |
134 | | */ |
135 | 3.61M | if (ctx->engine != NULL |
136 | 3.61M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
137 | 3.61M | || tmpimpl != NULL |
138 | 3.61M | #endif |
139 | 3.61M | || impl != NULL |
140 | 3.61M | || (cipher != NULL && cipher->origin == EVP_ORIG_METH) |
141 | 3.61M | || (cipher == NULL && ctx->cipher != NULL |
142 | 3.22M | && 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.61M | 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.61M | 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.61M | if (cipher == NULL) |
173 | 3.22M | cipher = ctx->cipher; |
174 | | |
175 | 3.61M | 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.61M | 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.61M | if (cipher != ctx->fetched_cipher) { |
200 | 396k | 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 | 396k | EVP_CIPHER_free(ctx->fetched_cipher); |
205 | 396k | ctx->fetched_cipher = (EVP_CIPHER *)cipher; |
206 | 396k | } |
207 | 3.61M | ctx->cipher = cipher; |
208 | 3.61M | if (ctx->algctx == NULL) { |
209 | 396k | ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov)); |
210 | 396k | if (ctx->algctx == NULL) { |
211 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
212 | 0 | return 0; |
213 | 0 | } |
214 | 396k | } |
215 | | |
216 | 3.61M | 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.61M | #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.61M | 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.61M | #endif |
260 | | |
261 | 3.61M | if (enc) { |
262 | 2.86M | if (ctx->cipher->einit == NULL) { |
263 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
264 | 0 | return 0; |
265 | 0 | } |
266 | | |
267 | 2.86M | return ctx->cipher->einit(ctx->algctx, |
268 | 2.86M | key, |
269 | 2.86M | key == NULL ? 0 |
270 | 2.86M | : EVP_CIPHER_CTX_get_key_length(ctx), |
271 | 2.86M | iv, |
272 | 2.86M | iv == NULL ? 0 |
273 | 2.86M | : EVP_CIPHER_CTX_get_iv_length(ctx), |
274 | 2.86M | params); |
275 | 2.86M | } |
276 | | |
277 | 757k | if (ctx->cipher->dinit == NULL) { |
278 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
279 | 0 | return 0; |
280 | 0 | } |
281 | | |
282 | 757k | return ctx->cipher->dinit(ctx->algctx, |
283 | 757k | key, |
284 | 757k | key == NULL ? 0 |
285 | 757k | : EVP_CIPHER_CTX_get_key_length(ctx), |
286 | 757k | iv, |
287 | 757k | iv == NULL ? 0 |
288 | 757k | : EVP_CIPHER_CTX_get_iv_length(ctx), |
289 | 757k | 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 | 58.7k | { |
438 | 58.7k | return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params); |
439 | 58.7k | } |
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.30M | { |
453 | 6.30M | return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL); |
454 | 6.30M | } |
455 | | |
456 | | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
457 | | const unsigned char *in, int inl) |
458 | 13.7M | { |
459 | 13.7M | if (ctx->encrypt) |
460 | 10.4M | return EVP_EncryptUpdate(ctx, out, outl, in, inl); |
461 | 3.30M | else |
462 | 3.30M | return EVP_DecryptUpdate(ctx, out, outl, in, inl); |
463 | 13.7M | } |
464 | | |
465 | | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
466 | 3.81M | { |
467 | 3.81M | if (ctx->encrypt) |
468 | 2.49M | return EVP_EncryptFinal_ex(ctx, out, outl); |
469 | 1.32M | else |
470 | 1.32M | return EVP_DecryptFinal_ex(ctx, out, outl); |
471 | 3.81M | } |
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.3k | { |
491 | 42.3k | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); |
492 | 42.3k | } |
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 | 58.7k | { |
498 | 58.7k | return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params); |
499 | 58.7k | } |
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.08k | { |
511 | 1.08k | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); |
512 | 1.08k | } |
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 | 26.5k | { |
650 | 26.5k | int ret; |
651 | 26.5k | size_t soutl, inl_ = (size_t)inl; |
652 | 26.5k | int blocksize; |
653 | | |
654 | 26.5k | if (outl != NULL) { |
655 | 26.5k | *outl = 0; |
656 | 26.5k | } else { |
657 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
658 | 0 | return 0; |
659 | 0 | } |
660 | | |
661 | | /* Prevent accidental use of decryption context when encrypting */ |
662 | 26.5k | if (!ctx->encrypt) { |
663 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
664 | 0 | return 0; |
665 | 0 | } |
666 | | |
667 | 26.5k | if (ctx->cipher == NULL) { |
668 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
669 | 0 | return 0; |
670 | 0 | } |
671 | | |
672 | 26.5k | if (ctx->cipher->prov == NULL) |
673 | 0 | goto legacy; |
674 | | |
675 | 26.5k | blocksize = ctx->cipher->block_size; |
676 | | |
677 | 26.5k | if (ctx->cipher->cupdate == NULL || blocksize < 1) { |
678 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
679 | 0 | return 0; |
680 | 0 | } |
681 | | |
682 | 26.5k | ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl, |
683 | 26.5k | inl_ + (size_t)(blocksize == 1 ? 0 : blocksize), |
684 | 26.5k | in, inl_); |
685 | | |
686 | 26.5k | if (ret) { |
687 | 26.5k | if (soutl > INT_MAX) { |
688 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
689 | 0 | return 0; |
690 | 0 | } |
691 | 26.5k | *outl = soutl; |
692 | 26.5k | } |
693 | | |
694 | 26.5k | return ret; |
695 | | |
696 | | /* Code below to be removed when legacy support is dropped. */ |
697 | 0 | legacy: |
698 | |
|
699 | 0 | return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl); |
700 | 26.5k | } |
701 | | |
702 | | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
703 | 57 | { |
704 | 57 | int ret; |
705 | 57 | ret = EVP_EncryptFinal_ex(ctx, out, outl); |
706 | 57 | return ret; |
707 | 57 | } |
708 | | |
709 | | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
710 | 1.90M | { |
711 | 1.90M | int n, ret; |
712 | 1.90M | unsigned int i, b, bl; |
713 | 1.90M | size_t soutl; |
714 | 1.90M | int blocksize; |
715 | | |
716 | 1.90M | if (outl != NULL) { |
717 | 1.90M | *outl = 0; |
718 | 1.90M | } else { |
719 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
720 | 0 | return 0; |
721 | 0 | } |
722 | | |
723 | | /* Prevent accidental use of decryption context when encrypting */ |
724 | 1.90M | if (!ctx->encrypt) { |
725 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
726 | 0 | return 0; |
727 | 0 | } |
728 | | |
729 | 1.90M | if (ctx->cipher == NULL) { |
730 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
731 | 0 | return 0; |
732 | 0 | } |
733 | 1.90M | if (ctx->cipher->prov == NULL) |
734 | 0 | goto legacy; |
735 | | |
736 | 1.90M | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
737 | | |
738 | 1.90M | if (blocksize < 1 || ctx->cipher->cfinal == NULL) { |
739 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
740 | 0 | return 0; |
741 | 0 | } |
742 | | |
743 | 1.90M | ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl, |
744 | 1.90M | blocksize == 1 ? 0 : blocksize); |
745 | | |
746 | 1.90M | if (ret) { |
747 | 1.90M | if (soutl > INT_MAX) { |
748 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
749 | 0 | return 0; |
750 | 0 | } |
751 | 1.90M | *outl = soutl; |
752 | 1.90M | } |
753 | | |
754 | 1.90M | return ret; |
755 | | |
756 | | /* Code below to be removed when legacy support is dropped. */ |
757 | 0 | legacy: |
758 | |
|
759 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
760 | 0 | ret = ctx->cipher->do_cipher(ctx, out, NULL, 0); |
761 | 0 | if (ret < 0) |
762 | 0 | return 0; |
763 | 0 | else |
764 | 0 | *outl = ret; |
765 | 0 | return 1; |
766 | 0 | } |
767 | | |
768 | 0 | b = ctx->cipher->block_size; |
769 | 0 | OPENSSL_assert(b <= sizeof(ctx->buf)); |
770 | 0 | if (b == 1) { |
771 | 0 | *outl = 0; |
772 | 0 | return 1; |
773 | 0 | } |
774 | 0 | bl = ctx->buf_len; |
775 | 0 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
776 | 0 | if (bl) { |
777 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
778 | 0 | return 0; |
779 | 0 | } |
780 | 0 | *outl = 0; |
781 | 0 | return 1; |
782 | 0 | } |
783 | | |
784 | 0 | n = b - bl; |
785 | 0 | for (i = bl; i < b; i++) |
786 | 0 | ctx->buf[i] = n; |
787 | 0 | ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b); |
788 | |
|
789 | 0 | if (ret) |
790 | 0 | *outl = b; |
791 | |
|
792 | 0 | return ret; |
793 | 0 | } |
794 | | |
795 | | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
796 | | const unsigned char *in, int inl) |
797 | 49.5k | { |
798 | 49.5k | int fix_len, cmpl = inl, ret; |
799 | 49.5k | unsigned int b; |
800 | 49.5k | size_t soutl, inl_ = (size_t)inl; |
801 | 49.5k | int blocksize; |
802 | | |
803 | 49.5k | if (outl != NULL) { |
804 | 49.5k | *outl = 0; |
805 | 49.5k | } else { |
806 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
807 | 0 | return 0; |
808 | 0 | } |
809 | | |
810 | | /* Prevent accidental use of encryption context when decrypting */ |
811 | 49.5k | if (ctx->encrypt) { |
812 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
813 | 0 | return 0; |
814 | 0 | } |
815 | | |
816 | 49.5k | if (ctx->cipher == NULL) { |
817 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
818 | 0 | return 0; |
819 | 0 | } |
820 | 49.5k | if (ctx->cipher->prov == NULL) |
821 | 0 | goto legacy; |
822 | | |
823 | 49.5k | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
824 | | |
825 | 49.5k | if (ctx->cipher->cupdate == NULL || blocksize < 1) { |
826 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
827 | 0 | return 0; |
828 | 0 | } |
829 | 49.5k | ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl, |
830 | 49.5k | inl_ + (size_t)(blocksize == 1 ? 0 : blocksize), |
831 | 49.5k | in, inl_); |
832 | | |
833 | 49.5k | if (ret) { |
834 | 49.2k | if (soutl > INT_MAX) { |
835 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
836 | 0 | return 0; |
837 | 0 | } |
838 | 49.2k | *outl = soutl; |
839 | 49.2k | } |
840 | | |
841 | 49.5k | return ret; |
842 | | |
843 | | /* Code below to be removed when legacy support is dropped. */ |
844 | 0 | legacy: |
845 | |
|
846 | 0 | b = ctx->cipher->block_size; |
847 | |
|
848 | 0 | if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) |
849 | 0 | cmpl = (cmpl + 7) / 8; |
850 | |
|
851 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
852 | 0 | if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) { |
853 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
854 | 0 | return 0; |
855 | 0 | } |
856 | | |
857 | 0 | fix_len = ctx->cipher->do_cipher(ctx, out, in, inl); |
858 | 0 | if (fix_len < 0) { |
859 | 0 | *outl = 0; |
860 | 0 | return 0; |
861 | 0 | } else |
862 | 0 | *outl = fix_len; |
863 | 0 | return 1; |
864 | 0 | } |
865 | | |
866 | 0 | if (inl <= 0) { |
867 | 0 | *outl = 0; |
868 | 0 | return inl == 0; |
869 | 0 | } |
870 | | |
871 | 0 | if (ctx->flags & EVP_CIPH_NO_PADDING) |
872 | 0 | return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl); |
873 | | |
874 | 0 | OPENSSL_assert(b <= sizeof(ctx->final)); |
875 | |
|
876 | 0 | if (ctx->final_used) { |
877 | | /* see comment about PTRDIFF_T comparison above */ |
878 | 0 | if (((PTRDIFF_T)out == (PTRDIFF_T)in) |
879 | 0 | || ossl_is_partially_overlapping(out, in, b)) { |
880 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
881 | 0 | return 0; |
882 | 0 | } |
883 | | /* |
884 | | * final_used is only ever set if buf_len is 0. Therefore the maximum |
885 | | * length output we will ever see from evp_EncryptDecryptUpdate is |
886 | | * the maximum multiple of the block length that is <= inl, or just: |
887 | | * inl & ~(b - 1) |
888 | | * Since final_used has been set then the final output length is: |
889 | | * (inl & ~(b - 1)) + b |
890 | | * This must never exceed INT_MAX |
891 | | */ |
892 | 0 | if ((inl & ~(b - 1)) > INT_MAX - b) { |
893 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW); |
894 | 0 | return 0; |
895 | 0 | } |
896 | 0 | memcpy(out, ctx->final, b); |
897 | 0 | out += b; |
898 | 0 | fix_len = 1; |
899 | 0 | } else |
900 | 0 | fix_len = 0; |
901 | | |
902 | 0 | if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl)) |
903 | 0 | return 0; |
904 | | |
905 | | /* |
906 | | * if we have 'decrypted' a multiple of block size, make sure we have a |
907 | | * copy of this last block |
908 | | */ |
909 | 0 | if (b > 1 && !ctx->buf_len) { |
910 | 0 | *outl -= b; |
911 | 0 | ctx->final_used = 1; |
912 | 0 | memcpy(ctx->final, &out[*outl], b); |
913 | 0 | } else |
914 | 0 | ctx->final_used = 0; |
915 | |
|
916 | 0 | if (fix_len) |
917 | 0 | *outl += b; |
918 | |
|
919 | 0 | return 1; |
920 | 0 | } |
921 | | |
922 | | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
923 | 958 | { |
924 | 958 | int ret; |
925 | 958 | ret = EVP_DecryptFinal_ex(ctx, out, outl); |
926 | 958 | return ret; |
927 | 958 | } |
928 | | |
929 | | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
930 | 342k | { |
931 | 342k | int i, n; |
932 | 342k | unsigned int b; |
933 | 342k | size_t soutl; |
934 | 342k | int ret; |
935 | 342k | int blocksize; |
936 | | |
937 | 342k | if (outl != NULL) { |
938 | 342k | *outl = 0; |
939 | 342k | } else { |
940 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
941 | 0 | return 0; |
942 | 0 | } |
943 | | |
944 | | /* Prevent accidental use of encryption context when decrypting */ |
945 | 342k | if (ctx->encrypt) { |
946 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
947 | 0 | return 0; |
948 | 0 | } |
949 | | |
950 | 342k | if (ctx->cipher == NULL) { |
951 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
952 | 0 | return 0; |
953 | 0 | } |
954 | | |
955 | 342k | if (ctx->cipher->prov == NULL) |
956 | 0 | goto legacy; |
957 | | |
958 | 342k | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
959 | | |
960 | 342k | if (blocksize < 1 || ctx->cipher->cfinal == NULL) { |
961 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
962 | 0 | return 0; |
963 | 0 | } |
964 | | |
965 | 342k | ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl, |
966 | 342k | blocksize == 1 ? 0 : blocksize); |
967 | | |
968 | 342k | if (ret) { |
969 | 1.31k | if (soutl > INT_MAX) { |
970 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
971 | 0 | return 0; |
972 | 0 | } |
973 | 1.31k | *outl = soutl; |
974 | 1.31k | } |
975 | | |
976 | 342k | return ret; |
977 | | |
978 | | /* Code below to be removed when legacy support is dropped. */ |
979 | 0 | legacy: |
980 | |
|
981 | 0 | *outl = 0; |
982 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
983 | 0 | i = ctx->cipher->do_cipher(ctx, out, NULL, 0); |
984 | 0 | if (i < 0) |
985 | 0 | return 0; |
986 | 0 | else |
987 | 0 | *outl = i; |
988 | 0 | return 1; |
989 | 0 | } |
990 | | |
991 | 0 | b = ctx->cipher->block_size; |
992 | 0 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
993 | 0 | if (ctx->buf_len) { |
994 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
995 | 0 | return 0; |
996 | 0 | } |
997 | 0 | *outl = 0; |
998 | 0 | return 1; |
999 | 0 | } |
1000 | 0 | if (b > 1) { |
1001 | 0 | if (ctx->buf_len || !ctx->final_used) { |
1002 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH); |
1003 | 0 | return 0; |
1004 | 0 | } |
1005 | 0 | OPENSSL_assert(b <= sizeof(ctx->final)); |
1006 | | |
1007 | | /* |
1008 | | * The following assumes that the ciphertext has been authenticated. |
1009 | | * Otherwise it provides a padding oracle. |
1010 | | */ |
1011 | 0 | n = ctx->final[b - 1]; |
1012 | 0 | if (n == 0 || n > (int)b) { |
1013 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT); |
1014 | 0 | return 0; |
1015 | 0 | } |
1016 | 0 | for (i = 0; i < n; i++) { |
1017 | 0 | if (ctx->final[--b] != n) { |
1018 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT); |
1019 | 0 | return 0; |
1020 | 0 | } |
1021 | 0 | } |
1022 | 0 | n = ctx->cipher->block_size - n; |
1023 | 0 | for (i = 0; i < n; i++) |
1024 | 0 | out[i] = ctx->final[i]; |
1025 | 0 | *outl = n; |
1026 | 0 | } else |
1027 | 0 | *outl = 0; |
1028 | 0 | return 1; |
1029 | 0 | } |
1030 | | |
1031 | | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) |
1032 | | { |
1033 | | if (c->cipher->prov != NULL) { |
1034 | | int ok; |
1035 | | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
1036 | | size_t len = keylen; |
1037 | | |
1038 | | if (EVP_CIPHER_CTX_get_key_length(c) == keylen) |
1039 | | return 1; |
1040 | | |
1041 | | /* Check the cipher actually understands this parameter */ |
1042 | | if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher), |
1043 | | OSSL_CIPHER_PARAM_KEYLEN) |
1044 | | == NULL) { |
1045 | | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); |
1046 | | return 0; |
1047 | | } |
1048 | | |
1049 | | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len); |
1050 | | ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params); |
1051 | | |
1052 | | return ok > 0 ? 1 : 0; |
1053 | | } |
1054 | | |
1055 | | /* Code below to be removed when legacy support is dropped. */ |
1056 | | |
1057 | | /* |
1058 | | * Note there have never been any built-in ciphers that define this flag |
1059 | | * since it was first introduced. |
1060 | | */ |
1061 | | if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) |
1062 | | return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL); |
1063 | | if (EVP_CIPHER_CTX_get_key_length(c) == keylen) |
1064 | | return 1; |
1065 | | if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { |
1066 | | c->key_len = keylen; |
1067 | | return 1; |
1068 | | } |
1069 | | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); |
1070 | | return 0; |
1071 | | } |
1072 | | |
1073 | | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) |
1074 | 1.14k | { |
1075 | 1.14k | int ok; |
1076 | 1.14k | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
1077 | 1.14k | unsigned int pd = pad; |
1078 | | |
1079 | 1.14k | if (pad) |
1080 | 0 | ctx->flags &= ~EVP_CIPH_NO_PADDING; |
1081 | 1.14k | else |
1082 | 1.14k | ctx->flags |= EVP_CIPH_NO_PADDING; |
1083 | | |
1084 | 1.14k | if (ctx->cipher != NULL && ctx->cipher->prov == NULL) |
1085 | 0 | return 1; |
1086 | 1.14k | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd); |
1087 | 1.14k | ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1088 | | |
1089 | 1.14k | return ok != 0; |
1090 | 1.14k | } |
1091 | | |
1092 | | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) |
1093 | 30.8k | { |
1094 | 30.8k | int ret = EVP_CTRL_RET_UNSUPPORTED; |
1095 | 30.8k | int set_params = 1; |
1096 | 30.8k | size_t sz = arg; |
1097 | 30.8k | unsigned int i; |
1098 | 30.8k | OSSL_PARAM params[4] = { |
1099 | 30.8k | OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END |
1100 | 30.8k | }; |
1101 | | |
1102 | 30.8k | if (ctx == NULL || ctx->cipher == NULL) { |
1103 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); |
1104 | 0 | return 0; |
1105 | 0 | } |
1106 | | |
1107 | 30.8k | if (ctx->cipher->prov == NULL) |
1108 | 0 | goto legacy; |
1109 | | |
1110 | 30.8k | switch (type) { |
1111 | 0 | case EVP_CTRL_SET_KEY_LENGTH: |
1112 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz); |
1113 | 0 | break; |
1114 | 0 | case EVP_CTRL_RAND_KEY: /* Used by DES */ |
1115 | 0 | set_params = 0; |
1116 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, |
1117 | 0 | ptr, sz); |
1118 | 0 | break; |
1119 | | |
1120 | 0 | case EVP_CTRL_INIT: |
1121 | | /* |
1122 | | * EVP_CTRL_INIT is purely legacy, no provider counterpart. |
1123 | | * As a matter of fact, this should be dead code, but some caller |
1124 | | * might still do a direct control call with this command, so... |
1125 | | * Legacy methods return 1 except for exceptional circumstances, so |
1126 | | * we do the same here to not be disruptive. |
1127 | | */ |
1128 | 0 | return 1; |
1129 | 0 | case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */ |
1130 | 0 | default: |
1131 | 0 | goto end; |
1132 | 1.70k | case EVP_CTRL_AEAD_SET_IVLEN: |
1133 | 1.70k | if (arg < 0) |
1134 | 0 | return 0; |
1135 | 1.70k | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz); |
1136 | 1.70k | ctx->iv_len = -1; |
1137 | 1.70k | break; |
1138 | 0 | case EVP_CTRL_CCM_SET_L: |
1139 | 0 | if (arg < 2 || arg > 8) |
1140 | 0 | return 0; |
1141 | 0 | sz = 15 - arg; |
1142 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz); |
1143 | 0 | ctx->iv_len = -1; |
1144 | 0 | break; |
1145 | 335 | case EVP_CTRL_AEAD_SET_IV_FIXED: |
1146 | 335 | params[0] = OSSL_PARAM_construct_octet_string( |
1147 | 335 | OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz); |
1148 | 335 | break; |
1149 | 0 | case EVP_CTRL_GCM_IV_GEN: |
1150 | 0 | set_params = 0; |
1151 | 0 | if (arg < 0) |
1152 | 0 | sz = 0; /* special case that uses the iv length */ |
1153 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1154 | 0 | OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz); |
1155 | 0 | break; |
1156 | 0 | case EVP_CTRL_GCM_SET_IV_INV: |
1157 | 0 | if (arg < 0) |
1158 | 0 | return 0; |
1159 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1160 | 0 | OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz); |
1161 | 0 | break; |
1162 | 0 | case EVP_CTRL_GET_RC5_ROUNDS: |
1163 | 0 | set_params = 0; /* Fall thru */ |
1164 | 0 | case EVP_CTRL_SET_RC5_ROUNDS: |
1165 | 0 | if (arg < 0) |
1166 | 0 | return 0; |
1167 | 0 | i = (unsigned int)arg; |
1168 | 0 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i); |
1169 | 0 | break; |
1170 | 0 | case EVP_CTRL_SET_SPEED: |
1171 | 0 | if (arg < 0) |
1172 | 0 | return 0; |
1173 | 0 | i = (unsigned int)arg; |
1174 | 0 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i); |
1175 | 0 | break; |
1176 | 2.33k | case EVP_CTRL_AEAD_GET_TAG: |
1177 | 2.33k | set_params = 0; /* Fall thru */ |
1178 | 26.7k | case EVP_CTRL_AEAD_SET_TAG: |
1179 | 26.7k | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, |
1180 | 26.7k | ptr, sz); |
1181 | 26.7k | break; |
1182 | 1.26k | case EVP_CTRL_AEAD_TLS1_AAD: |
1183 | | /* This one does a set and a get - since it returns a size */ |
1184 | 1.26k | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, |
1185 | 1.26k | ptr, sz); |
1186 | 1.26k | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1187 | 1.26k | if (ret <= 0) |
1188 | 17 | goto end; |
1189 | 1.24k | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz); |
1190 | 1.24k | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1191 | 1.24k | if (ret <= 0) |
1192 | 0 | goto end; |
1193 | 1.24k | return sz; |
1194 | 0 | #ifndef OPENSSL_NO_RC2 |
1195 | 0 | case EVP_CTRL_GET_RC2_KEY_BITS: |
1196 | 0 | set_params = 0; /* Fall thru */ |
1197 | 0 | case EVP_CTRL_SET_RC2_KEY_BITS: |
1198 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz); |
1199 | 0 | break; |
1200 | 0 | #endif /* OPENSSL_NO_RC2 */ |
1201 | 0 | #if !defined(OPENSSL_NO_MULTIBLOCK) |
1202 | 0 | case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE: |
1203 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1204 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz); |
1205 | 0 | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1206 | 0 | if (ret <= 0) |
1207 | 0 | return 0; |
1208 | | |
1209 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1210 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz); |
1211 | 0 | params[1] = OSSL_PARAM_construct_end(); |
1212 | 0 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1213 | 0 | if (ret <= 0) |
1214 | 0 | return 0; |
1215 | 0 | return sz; |
1216 | 0 | case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: { |
1217 | 0 | EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr; |
1218 | |
|
1219 | 0 | if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM)) |
1220 | 0 | return 0; |
1221 | | |
1222 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1223 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void *)p->inp, p->len); |
1224 | 0 | params[1] = OSSL_PARAM_construct_uint( |
1225 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); |
1226 | 0 | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1227 | 0 | if (ret <= 0) |
1228 | 0 | return ret; |
1229 | | /* Retrieve the return values changed by the set */ |
1230 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1231 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz); |
1232 | 0 | params[1] = OSSL_PARAM_construct_uint( |
1233 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); |
1234 | 0 | params[2] = OSSL_PARAM_construct_end(); |
1235 | 0 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1236 | 0 | if (ret <= 0) |
1237 | 0 | return 0; |
1238 | 0 | return sz; |
1239 | 0 | } |
1240 | 0 | case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: { |
1241 | 0 | EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr; |
1242 | |
|
1243 | 0 | params[0] = OSSL_PARAM_construct_octet_string( |
1244 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len); |
1245 | |
|
1246 | 0 | params[1] = OSSL_PARAM_construct_octet_string( |
1247 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void *)p->inp, |
1248 | 0 | p->len); |
1249 | 0 | params[2] = OSSL_PARAM_construct_uint( |
1250 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); |
1251 | 0 | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1252 | 0 | if (ret <= 0) |
1253 | 0 | return ret; |
1254 | 0 | params[0] = OSSL_PARAM_construct_size_t( |
1255 | 0 | OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz); |
1256 | 0 | params[1] = OSSL_PARAM_construct_end(); |
1257 | 0 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1258 | 0 | if (ret <= 0) |
1259 | 0 | return 0; |
1260 | 0 | return sz; |
1261 | 0 | } |
1262 | 0 | #endif /* OPENSSL_NO_MULTIBLOCK */ |
1263 | 748 | case EVP_CTRL_AEAD_SET_MAC_KEY: |
1264 | 748 | if (arg < 0) |
1265 | 0 | return -1; |
1266 | 748 | params[0] = OSSL_PARAM_construct_octet_string( |
1267 | 748 | OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz); |
1268 | 748 | break; |
1269 | 30.8k | } |
1270 | | |
1271 | 29.5k | if (set_params) |
1272 | 27.2k | ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
1273 | 2.33k | else |
1274 | 2.33k | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
1275 | 29.5k | goto end; |
1276 | | |
1277 | | /* Code below to be removed when legacy support is dropped. */ |
1278 | 0 | legacy: |
1279 | 0 | if (ctx->cipher->ctrl == NULL) { |
1280 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); |
1281 | 0 | return 0; |
1282 | 0 | } |
1283 | | |
1284 | 0 | ret = ctx->cipher->ctrl(ctx, type, arg, ptr); |
1285 | |
|
1286 | 29.5k | end: |
1287 | 29.5k | if (ret == EVP_CTRL_RET_UNSUPPORTED) { |
1288 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
1289 | 0 | return 0; |
1290 | 0 | } |
1291 | 29.5k | return ret; |
1292 | 29.5k | } |
1293 | | |
1294 | | int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[]) |
1295 | 1.69M | { |
1296 | 1.69M | if (cipher != NULL && cipher->get_params != NULL) |
1297 | 1.69M | return cipher->get_params(params); |
1298 | 0 | return 0; |
1299 | 1.69M | } |
1300 | | |
1301 | | int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[]) |
1302 | 2.02k | { |
1303 | 2.02k | if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) { |
1304 | 2.02k | ctx->iv_len = -1; |
1305 | 2.02k | return ctx->cipher->set_ctx_params(ctx->algctx, params); |
1306 | 2.02k | } |
1307 | 0 | return 0; |
1308 | 2.02k | } |
1309 | | |
1310 | | int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[]) |
1311 | 484k | { |
1312 | 484k | if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL) |
1313 | 484k | return ctx->cipher->get_ctx_params(ctx->algctx, params); |
1314 | 0 | return 0; |
1315 | 484k | } |
1316 | | |
1317 | | const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher) |
1318 | 0 | { |
1319 | 0 | if (cipher != NULL && cipher->gettable_params != NULL) |
1320 | 0 | return cipher->gettable_params( |
1321 | 0 | ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher))); |
1322 | 0 | return NULL; |
1323 | 0 | } |
1324 | | |
1325 | | const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher) |
1326 | 364 | { |
1327 | 364 | void *provctx; |
1328 | | |
1329 | 364 | if (cipher != NULL && cipher->settable_ctx_params != NULL) { |
1330 | 364 | provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)); |
1331 | 364 | return cipher->settable_ctx_params(NULL, provctx); |
1332 | 364 | } |
1333 | 0 | return NULL; |
1334 | 364 | } |
1335 | | |
1336 | | const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher) |
1337 | 8.69k | { |
1338 | 8.69k | void *provctx; |
1339 | | |
1340 | 8.69k | if (cipher != NULL && cipher->gettable_ctx_params != NULL) { |
1341 | 8.69k | provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)); |
1342 | 8.69k | return cipher->gettable_ctx_params(NULL, provctx); |
1343 | 8.69k | } |
1344 | 0 | return NULL; |
1345 | 8.69k | } |
1346 | | |
1347 | | const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx) |
1348 | 0 | { |
1349 | 0 | void *alg; |
1350 | |
|
1351 | 0 | if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) { |
1352 | 0 | alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher)); |
1353 | 0 | return cctx->cipher->settable_ctx_params(cctx->algctx, alg); |
1354 | 0 | } |
1355 | 0 | return NULL; |
1356 | 0 | } |
1357 | | |
1358 | | const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx) |
1359 | 0 | { |
1360 | 0 | void *provctx; |
1361 | |
|
1362 | 0 | if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) { |
1363 | 0 | provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher)); |
1364 | 0 | return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx); |
1365 | 0 | } |
1366 | 0 | return NULL; |
1367 | 0 | } |
1368 | | |
1369 | | #ifndef FIPS_MODULE |
1370 | | static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx) |
1371 | 0 | { |
1372 | 0 | const EVP_CIPHER *cipher = ctx->cipher; |
1373 | 0 | const OSSL_PROVIDER *prov; |
1374 | |
|
1375 | 0 | if (cipher == NULL) |
1376 | 0 | return NULL; |
1377 | | |
1378 | 0 | prov = EVP_CIPHER_get0_provider(cipher); |
1379 | 0 | return ossl_provider_libctx(prov); |
1380 | 0 | } |
1381 | | #endif |
1382 | | |
1383 | | int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) |
1384 | 0 | { |
1385 | 0 | if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) |
1386 | 0 | return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); |
1387 | | |
1388 | | #ifdef FIPS_MODULE |
1389 | | return 0; |
1390 | | #else |
1391 | 0 | { |
1392 | 0 | int kl; |
1393 | 0 | OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx); |
1394 | |
|
1395 | 0 | kl = EVP_CIPHER_CTX_get_key_length(ctx); |
1396 | 0 | if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0) |
1397 | 0 | return 0; |
1398 | 0 | return 1; |
1399 | 0 | } |
1400 | 0 | #endif /* FIPS_MODULE */ |
1401 | 0 | } |
1402 | | |
1403 | | int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) |
1404 | 252 | { |
1405 | 252 | if ((in == NULL) || (in->cipher == NULL)) { |
1406 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED); |
1407 | 0 | return 0; |
1408 | 0 | } |
1409 | | |
1410 | 252 | if (in->cipher->prov == NULL) |
1411 | 0 | goto legacy; |
1412 | | |
1413 | 252 | if (in->cipher->dupctx == NULL) { |
1414 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
1415 | 0 | return 0; |
1416 | 0 | } |
1417 | | |
1418 | 252 | EVP_CIPHER_CTX_reset(out); |
1419 | | |
1420 | 252 | *out = *in; |
1421 | 252 | out->algctx = NULL; |
1422 | | |
1423 | 252 | if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) { |
1424 | 0 | out->fetched_cipher = NULL; |
1425 | 0 | return 0; |
1426 | 0 | } |
1427 | | |
1428 | 252 | out->algctx = in->cipher->dupctx(in->algctx); |
1429 | 252 | if (out->algctx == NULL) { |
1430 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
1431 | 0 | return 0; |
1432 | 0 | } |
1433 | | |
1434 | 252 | return 1; |
1435 | | |
1436 | | /* Code below to be removed when legacy support is dropped. */ |
1437 | 0 | legacy: |
1438 | |
|
1439 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
1440 | | /* Make sure it's safe to copy a cipher context using an ENGINE */ |
1441 | 0 | if (in->engine && !ENGINE_init(in->engine)) { |
1442 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
1443 | 0 | return 0; |
1444 | 0 | } |
1445 | 0 | #endif |
1446 | | |
1447 | 0 | EVP_CIPHER_CTX_reset(out); |
1448 | 0 | memcpy(out, in, sizeof(*out)); |
1449 | |
|
1450 | 0 | if (in->cipher_data && in->cipher->ctx_size) { |
1451 | 0 | out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size); |
1452 | 0 | if (out->cipher_data == NULL) { |
1453 | 0 | out->cipher = NULL; |
1454 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
1455 | 0 | return 0; |
1456 | 0 | } |
1457 | 0 | memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); |
1458 | 0 | } |
1459 | | |
1460 | 0 | if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) |
1461 | 0 | if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { |
1462 | 0 | out->cipher = NULL; |
1463 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
1464 | 0 | return 0; |
1465 | 0 | } |
1466 | 0 | return 1; |
1467 | 0 | } |
1468 | | |
1469 | | EVP_CIPHER *evp_cipher_new(void) |
1470 | 372 | { |
1471 | 372 | EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER)); |
1472 | | |
1473 | 372 | if (cipher != NULL) { |
1474 | 372 | cipher->lock = CRYPTO_THREAD_lock_new(); |
1475 | 372 | if (cipher->lock == NULL) { |
1476 | 0 | OPENSSL_free(cipher); |
1477 | 0 | return NULL; |
1478 | 0 | } |
1479 | 372 | cipher->refcnt = 1; |
1480 | 372 | } |
1481 | 372 | return cipher; |
1482 | 372 | } |
1483 | | |
1484 | | /* |
1485 | | * FIPS module note: since internal fetches will be entirely |
1486 | | * provider based, we know that none of its code depends on legacy |
1487 | | * NIDs or any functionality that use them. |
1488 | | */ |
1489 | | #ifndef FIPS_MODULE |
1490 | | /* After removal of legacy support get rid of the need for legacy NIDs */ |
1491 | | static void set_legacy_nid(const char *name, void *vlegacy_nid) |
1492 | 16.2k | { |
1493 | 16.2k | int nid; |
1494 | 16.2k | int *legacy_nid = vlegacy_nid; |
1495 | | /* |
1496 | | * We use lowest level function to get the associated method, because |
1497 | | * higher level functions such as EVP_get_cipherbyname() have changed |
1498 | | * to look at providers too. |
1499 | | */ |
1500 | 16.2k | const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH); |
1501 | | |
1502 | 16.2k | if (*legacy_nid == -1) /* We found a clash already */ |
1503 | 0 | return; |
1504 | 16.2k | if (legacy_method == NULL) |
1505 | 7.28k | return; |
1506 | 8.96k | nid = EVP_CIPHER_get_nid(legacy_method); |
1507 | 8.96k | if (*legacy_nid != NID_undef && *legacy_nid != nid) { |
1508 | 0 | *legacy_nid = -1; |
1509 | 0 | return; |
1510 | 0 | } |
1511 | 8.96k | *legacy_nid = nid; |
1512 | 8.96k | } |
1513 | | #endif |
1514 | | |
1515 | | static void *evp_cipher_from_algorithm(const int name_id, |
1516 | | const OSSL_ALGORITHM *algodef, |
1517 | | OSSL_PROVIDER *prov) |
1518 | 2.84k | { |
1519 | 2.84k | const OSSL_DISPATCH *fns = algodef->implementation; |
1520 | 2.84k | EVP_CIPHER *cipher = NULL; |
1521 | 2.84k | int fnciphcnt = 0, fnctxcnt = 0; |
1522 | | |
1523 | 2.84k | if ((cipher = evp_cipher_new()) == NULL) { |
1524 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
1525 | 0 | return NULL; |
1526 | 0 | } |
1527 | | |
1528 | 2.84k | #ifndef FIPS_MODULE |
1529 | 2.84k | cipher->nid = NID_undef; |
1530 | 2.84k | if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid) |
1531 | 2.84k | || cipher->nid == -1) { |
1532 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
1533 | 0 | EVP_CIPHER_free(cipher); |
1534 | 0 | return NULL; |
1535 | 0 | } |
1536 | 2.84k | #endif |
1537 | | |
1538 | 2.84k | cipher->name_id = name_id; |
1539 | 2.84k | if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) { |
1540 | 0 | EVP_CIPHER_free(cipher); |
1541 | 0 | return NULL; |
1542 | 0 | } |
1543 | 2.84k | cipher->description = algodef->algorithm_description; |
1544 | | |
1545 | 42.3k | for (; fns->function_id != 0; fns++) { |
1546 | 39.5k | switch (fns->function_id) { |
1547 | 2.84k | case OSSL_FUNC_CIPHER_NEWCTX: |
1548 | 2.84k | if (cipher->newctx != NULL) |
1549 | 0 | break; |
1550 | 2.84k | cipher->newctx = OSSL_FUNC_cipher_newctx(fns); |
1551 | 2.84k | fnctxcnt++; |
1552 | 2.84k | break; |
1553 | 2.84k | case OSSL_FUNC_CIPHER_ENCRYPT_INIT: |
1554 | 2.84k | if (cipher->einit != NULL) |
1555 | 0 | break; |
1556 | 2.84k | cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns); |
1557 | 2.84k | fnciphcnt++; |
1558 | 2.84k | break; |
1559 | 2.84k | case OSSL_FUNC_CIPHER_DECRYPT_INIT: |
1560 | 2.84k | if (cipher->dinit != NULL) |
1561 | 0 | break; |
1562 | 2.84k | cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns); |
1563 | 2.84k | fnciphcnt++; |
1564 | 2.84k | break; |
1565 | 2.84k | case OSSL_FUNC_CIPHER_UPDATE: |
1566 | 2.84k | if (cipher->cupdate != NULL) |
1567 | 0 | break; |
1568 | 2.84k | cipher->cupdate = OSSL_FUNC_cipher_update(fns); |
1569 | 2.84k | fnciphcnt++; |
1570 | 2.84k | break; |
1571 | 2.84k | case OSSL_FUNC_CIPHER_FINAL: |
1572 | 2.84k | if (cipher->cfinal != NULL) |
1573 | 0 | break; |
1574 | 2.84k | cipher->cfinal = OSSL_FUNC_cipher_final(fns); |
1575 | 2.84k | fnciphcnt++; |
1576 | 2.84k | break; |
1577 | 2.57k | case OSSL_FUNC_CIPHER_CIPHER: |
1578 | 2.57k | if (cipher->ccipher != NULL) |
1579 | 0 | break; |
1580 | 2.57k | cipher->ccipher = OSSL_FUNC_cipher_cipher(fns); |
1581 | 2.57k | break; |
1582 | 2.84k | case OSSL_FUNC_CIPHER_FREECTX: |
1583 | 2.84k | if (cipher->freectx != NULL) |
1584 | 0 | break; |
1585 | 2.84k | cipher->freectx = OSSL_FUNC_cipher_freectx(fns); |
1586 | 2.84k | fnctxcnt++; |
1587 | 2.84k | break; |
1588 | 2.82k | case OSSL_FUNC_CIPHER_DUPCTX: |
1589 | 2.82k | if (cipher->dupctx != NULL) |
1590 | 0 | break; |
1591 | 2.82k | cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns); |
1592 | 2.82k | break; |
1593 | 2.84k | case OSSL_FUNC_CIPHER_GET_PARAMS: |
1594 | 2.84k | if (cipher->get_params != NULL) |
1595 | 0 | break; |
1596 | 2.84k | cipher->get_params = OSSL_FUNC_cipher_get_params(fns); |
1597 | 2.84k | break; |
1598 | 2.84k | case OSSL_FUNC_CIPHER_GET_CTX_PARAMS: |
1599 | 2.84k | if (cipher->get_ctx_params != NULL) |
1600 | 0 | break; |
1601 | 2.84k | cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns); |
1602 | 2.84k | break; |
1603 | 2.84k | case OSSL_FUNC_CIPHER_SET_CTX_PARAMS: |
1604 | 2.84k | if (cipher->set_ctx_params != NULL) |
1605 | 0 | break; |
1606 | 2.84k | cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns); |
1607 | 2.84k | break; |
1608 | 2.84k | case OSSL_FUNC_CIPHER_GETTABLE_PARAMS: |
1609 | 2.84k | if (cipher->gettable_params != NULL) |
1610 | 0 | break; |
1611 | 2.84k | cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns); |
1612 | 2.84k | break; |
1613 | 2.84k | case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS: |
1614 | 2.84k | if (cipher->gettable_ctx_params != NULL) |
1615 | 0 | break; |
1616 | 2.84k | cipher->gettable_ctx_params = OSSL_FUNC_cipher_gettable_ctx_params(fns); |
1617 | 2.84k | break; |
1618 | 2.84k | case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS: |
1619 | 2.84k | if (cipher->settable_ctx_params != NULL) |
1620 | 0 | break; |
1621 | 2.84k | cipher->settable_ctx_params = OSSL_FUNC_cipher_settable_ctx_params(fns); |
1622 | 2.84k | break; |
1623 | 39.5k | } |
1624 | 39.5k | } |
1625 | 2.84k | if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4) |
1626 | 2.84k | || (fnciphcnt == 0 && cipher->ccipher == NULL) |
1627 | 2.84k | || fnctxcnt != 2) { |
1628 | | /* |
1629 | | * In order to be a consistent set of functions we must have at least |
1630 | | * a complete set of "encrypt" functions, or a complete set of "decrypt" |
1631 | | * functions, or a single "cipher" function. In all cases we need both |
1632 | | * the "newctx" and "freectx" functions. |
1633 | | */ |
1634 | 0 | EVP_CIPHER_free(cipher); |
1635 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
1636 | 0 | return NULL; |
1637 | 0 | } |
1638 | 2.84k | cipher->prov = prov; |
1639 | 2.84k | if (prov != NULL) |
1640 | 2.84k | ossl_provider_up_ref(prov); |
1641 | | |
1642 | 2.84k | if (!evp_cipher_cache_constants(cipher)) { |
1643 | 0 | EVP_CIPHER_free(cipher); |
1644 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED); |
1645 | 0 | cipher = NULL; |
1646 | 0 | } |
1647 | | |
1648 | 2.84k | return cipher; |
1649 | 2.84k | } |
1650 | | |
1651 | | static int evp_cipher_up_ref(void *cipher) |
1652 | 3.27M | { |
1653 | 3.27M | return EVP_CIPHER_up_ref(cipher); |
1654 | 3.27M | } |
1655 | | |
1656 | | static void evp_cipher_free(void *cipher) |
1657 | 15.1k | { |
1658 | 15.1k | EVP_CIPHER_free(cipher); |
1659 | 15.1k | } |
1660 | | |
1661 | | EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
1662 | | const char *properties) |
1663 | 4.74M | { |
1664 | 4.74M | EVP_CIPHER *cipher = evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties, |
1665 | 4.74M | evp_cipher_from_algorithm, evp_cipher_up_ref, |
1666 | 4.74M | evp_cipher_free); |
1667 | | |
1668 | 4.74M | return cipher; |
1669 | 4.74M | } |
1670 | | |
1671 | | int EVP_CIPHER_up_ref(EVP_CIPHER *cipher) |
1672 | 4.32M | { |
1673 | 4.32M | int ref = 0; |
1674 | | |
1675 | 4.32M | if (cipher->origin == EVP_ORIG_DYNAMIC) |
1676 | 4.32M | CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock); |
1677 | 4.32M | return 1; |
1678 | 4.32M | } |
1679 | | |
1680 | | void evp_cipher_free_int(EVP_CIPHER *cipher) |
1681 | 5.83k | { |
1682 | 5.83k | OPENSSL_free(cipher->type_name); |
1683 | 5.83k | ossl_provider_free(cipher->prov); |
1684 | 5.83k | CRYPTO_THREAD_lock_free(cipher->lock); |
1685 | 5.83k | OPENSSL_free(cipher); |
1686 | 5.83k | } |
1687 | | |
1688 | | void EVP_CIPHER_free(EVP_CIPHER *cipher) |
1689 | 5.91M | { |
1690 | 5.91M | int i; |
1691 | | |
1692 | 5.91M | if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC) |
1693 | 1.58M | return; |
1694 | | |
1695 | 4.32M | CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock); |
1696 | 4.32M | if (i > 0) |
1697 | 4.32M | return; |
1698 | 5.83k | evp_cipher_free_int(cipher); |
1699 | 5.83k | } |
1700 | | |
1701 | | void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx, |
1702 | | void (*fn)(EVP_CIPHER *mac, void *arg), |
1703 | | void *arg) |
1704 | 8 | { |
1705 | 8 | evp_generic_do_all(libctx, OSSL_OP_CIPHER, |
1706 | 8 | (void (*)(void *, void *))fn, arg, |
1707 | 8 | evp_cipher_from_algorithm, evp_cipher_up_ref, |
1708 | 8 | evp_cipher_free); |
1709 | 8 | } |