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