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