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