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