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