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