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