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