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