]>
Commit | Line | Data |
---|---|---|
62867571 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
62867571 RS |
4 | * Licensed under the OpenSSL license (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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
c3a73daf | 11 | #include <assert.h> |
b39fc560 | 12 | #include "internal/cryptlib.h" |
ec577822 | 13 | #include <openssl/evp.h> |
7f060601 | 14 | #include <openssl/err.h> |
3a87a9b9 | 15 | #include <openssl/rand.h> |
3c27208f | 16 | #include <openssl/engine.h> |
135727ab | 17 | #include "internal/evp_int.h" |
d91f4568 | 18 | #include "internal/rand.h" |
57ae2e24 | 19 | #include "evp_locl.h" |
d02b48c6 | 20 | |
8baf9968 | 21 | int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c) |
0f113f3e | 22 | { |
8baf9968 RL |
23 | if (c == NULL) |
24 | return 1; | |
25 | if (c->cipher != NULL) { | |
26 | if (c->cipher->cleanup && !c->cipher->cleanup(c)) | |
27 | return 0; | |
28 | /* Cleanse cipher context data */ | |
29 | if (c->cipher_data && c->cipher->ctx_size) | |
30 | OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size); | |
31 | } | |
32 | OPENSSL_free(c->cipher_data); | |
33 | #ifndef OPENSSL_NO_ENGINE | |
7c96dbcd | 34 | ENGINE_finish(c->engine); |
8baf9968 RL |
35 | #endif |
36 | memset(c, 0, sizeof(*c)); | |
37 | return 1; | |
0f113f3e | 38 | } |
d02b48c6 | 39 | |
b40228a6 | 40 | EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) |
0f113f3e | 41 | { |
8baf9968 RL |
42 | return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); |
43 | } | |
44 | ||
45 | void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) | |
46 | { | |
47 | EVP_CIPHER_CTX_reset(ctx); | |
48 | OPENSSL_free(ctx); | |
0f113f3e | 49 | } |
581f1c84 | 50 | |
360370d9 | 51 | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
0f113f3e MC |
52 | const unsigned char *key, const unsigned char *iv, int enc) |
53 | { | |
ffd23209 KR |
54 | if (cipher != NULL) |
55 | EVP_CIPHER_CTX_reset(ctx); | |
0f113f3e MC |
56 | return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc); |
57 | } | |
58 | ||
59 | int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | |
60 | ENGINE *impl, const unsigned char *key, | |
61 | const unsigned char *iv, int enc) | |
62 | { | |
63 | if (enc == -1) | |
64 | enc = ctx->encrypt; | |
65 | else { | |
66 | if (enc) | |
67 | enc = 1; | |
68 | ctx->encrypt = enc; | |
69 | } | |
0b13e9f0 | 70 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
71 | /* |
72 | * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so | |
73 | * this context may already have an ENGINE! Try to avoid releasing the | |
74 | * previous handle, re-querying for an ENGINE, and having a | |
0d4fb843 | 75 | * reinitialisation, when it may all be unnecessary. |
0f113f3e | 76 | */ |
f6b94279 | 77 | if (ctx->engine && ctx->cipher |
a7f9e0a4 | 78 | && (cipher == NULL || cipher->nid == ctx->cipher->nid)) |
0f113f3e | 79 | goto skip_to_init; |
0b13e9f0 | 80 | #endif |
0f113f3e MC |
81 | if (cipher) { |
82 | /* | |
83 | * Ensure a context left lying around from last time is cleared (the | |
84 | * previous check attempted to avoid this if the same ENGINE and | |
85 | * EVP_CIPHER could be used). | |
86 | */ | |
87 | if (ctx->cipher) { | |
88 | unsigned long flags = ctx->flags; | |
c0ca39bd | 89 | EVP_CIPHER_CTX_reset(ctx); |
0f113f3e MC |
90 | /* Restore encrypt and flags */ |
91 | ctx->encrypt = enc; | |
92 | ctx->flags = flags; | |
93 | } | |
0b13e9f0 | 94 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
95 | if (impl) { |
96 | if (!ENGINE_init(impl)) { | |
97 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
98 | return 0; | |
99 | } | |
100 | } else | |
101 | /* Ask if an ENGINE is reserved for this job */ | |
102 | impl = ENGINE_get_cipher_engine(cipher->nid); | |
103 | if (impl) { | |
104 | /* There's an ENGINE for this job ... (apparently) */ | |
105 | const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid); | |
106 | if (!c) { | |
107 | /* | |
108 | * One positive side-effect of US's export control history, | |
109 | * is that we should at least be able to avoid using US | |
0d4fb843 | 110 | * misspellings of "initialisation"? |
0f113f3e MC |
111 | */ |
112 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
113 | return 0; | |
114 | } | |
115 | /* We'll use the ENGINE's private cipher definition */ | |
116 | cipher = c; | |
117 | /* | |
118 | * Store the ENGINE functional reference so we know 'cipher' came | |
119 | * from an ENGINE and we need to release it when done. | |
120 | */ | |
121 | ctx->engine = impl; | |
122 | } else | |
123 | ctx->engine = NULL; | |
0b13e9f0 | 124 | #endif |
544a2aea | 125 | |
0f113f3e MC |
126 | ctx->cipher = cipher; |
127 | if (ctx->cipher->ctx_size) { | |
b51bce94 | 128 | ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size); |
90945fa3 | 129 | if (ctx->cipher_data == NULL) { |
273a0218 | 130 | ctx->cipher = NULL; |
0f113f3e MC |
131 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE); |
132 | return 0; | |
133 | } | |
134 | } else { | |
135 | ctx->cipher_data = NULL; | |
136 | } | |
137 | ctx->key_len = cipher->key_len; | |
138 | /* Preserve wrap enable flag, zero everything else */ | |
139 | ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW; | |
140 | if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { | |
141 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { | |
273a0218 | 142 | ctx->cipher = NULL; |
0f113f3e MC |
143 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); |
144 | return 0; | |
145 | } | |
146 | } | |
147 | } else if (!ctx->cipher) { | |
148 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET); | |
149 | return 0; | |
150 | } | |
0b13e9f0 | 151 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 152 | skip_to_init: |
0b13e9f0 | 153 | #endif |
0f113f3e MC |
154 | /* we assume block size is a power of 2 in *cryptUpdate */ |
155 | OPENSSL_assert(ctx->cipher->block_size == 1 | |
156 | || ctx->cipher->block_size == 8 | |
157 | || ctx->cipher->block_size == 16); | |
158 | ||
159 | if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW) | |
160 | && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) { | |
161 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED); | |
162 | return 0; | |
163 | } | |
164 | ||
480d3323 | 165 | if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) { |
0f113f3e MC |
166 | switch (EVP_CIPHER_CTX_mode(ctx)) { |
167 | ||
168 | case EVP_CIPH_STREAM_CIPHER: | |
169 | case EVP_CIPH_ECB_MODE: | |
170 | break; | |
171 | ||
172 | case EVP_CIPH_CFB_MODE: | |
173 | case EVP_CIPH_OFB_MODE: | |
174 | ||
175 | ctx->num = 0; | |
176 | /* fall-through */ | |
177 | ||
178 | case EVP_CIPH_CBC_MODE: | |
179 | ||
180 | OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <= | |
181 | (int)sizeof(ctx->iv)); | |
182 | if (iv) | |
183 | memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); | |
184 | memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); | |
185 | break; | |
186 | ||
187 | case EVP_CIPH_CTR_MODE: | |
188 | ctx->num = 0; | |
189 | /* Don't reuse IV for CTR mode */ | |
190 | if (iv) | |
191 | memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx)); | |
192 | break; | |
193 | ||
194 | default: | |
195 | return 0; | |
0f113f3e MC |
196 | } |
197 | } | |
198 | ||
199 | if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { | |
200 | if (!ctx->cipher->init(ctx, key, iv, enc)) | |
201 | return 0; | |
202 | } | |
203 | ctx->buf_len = 0; | |
204 | ctx->final_used = 0; | |
205 | ctx->block_mask = ctx->cipher->block_size - 1; | |
206 | return 1; | |
207 | } | |
d02b48c6 | 208 | |
be06a934 | 209 | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
0f113f3e MC |
210 | const unsigned char *in, int inl) |
211 | { | |
212 | if (ctx->encrypt) | |
213 | return EVP_EncryptUpdate(ctx, out, outl, in, inl); | |
214 | else | |
215 | return EVP_DecryptUpdate(ctx, out, outl, in, inl); | |
216 | } | |
d02b48c6 | 217 | |
581f1c84 | 218 | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
0f113f3e MC |
219 | { |
220 | if (ctx->encrypt) | |
221 | return EVP_EncryptFinal_ex(ctx, out, outl); | |
222 | else | |
223 | return EVP_DecryptFinal_ex(ctx, out, outl); | |
224 | } | |
581f1c84 | 225 | |
6b691a5c | 226 | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
0f113f3e MC |
227 | { |
228 | if (ctx->encrypt) | |
229 | return EVP_EncryptFinal(ctx, out, outl); | |
230 | else | |
231 | return EVP_DecryptFinal(ctx, out, outl); | |
232 | } | |
d02b48c6 | 233 | |
be06a934 | 234 | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
0f113f3e MC |
235 | const unsigned char *key, const unsigned char *iv) |
236 | { | |
237 | return EVP_CipherInit(ctx, cipher, key, iv, 1); | |
238 | } | |
18eda732 | 239 | |
0f113f3e MC |
240 | int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
241 | ENGINE *impl, const unsigned char *key, | |
242 | const unsigned char *iv) | |
243 | { | |
244 | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); | |
245 | } | |
d02b48c6 | 246 | |
be06a934 | 247 | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
0f113f3e MC |
248 | const unsigned char *key, const unsigned char *iv) |
249 | { | |
250 | return EVP_CipherInit(ctx, cipher, key, iv, 0); | |
251 | } | |
18eda732 | 252 | |
0f113f3e MC |
253 | int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
254 | ENGINE *impl, const unsigned char *key, | |
255 | const unsigned char *iv) | |
256 | { | |
257 | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); | |
258 | } | |
d02b48c6 | 259 | |
c3a73daf AP |
260 | /* |
261 | * According to the letter of standard difference between pointers | |
262 | * is specified to be valid only within same object. This makes | |
263 | * it formally challenging to determine if input and output buffers | |
264 | * are not partially overlapping with standard pointer arithmetic. | |
265 | */ | |
266 | #ifdef PTRDIFF_T | |
267 | # undef PTRDIFF_T | |
268 | #endif | |
269 | #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64 | |
270 | /* | |
271 | * Then we have VMS that distinguishes itself by adhering to | |
5fc77684 AP |
272 | * sizeof(size_t)==4 even in 64-bit builds, which means that |
273 | * difference between two pointers might be truncated to 32 bits. | |
274 | * In the context one can even wonder how comparison for | |
275 | * equality is implemented. To be on the safe side we adhere to | |
276 | * PTRDIFF_T even for comparison for equality. | |
c3a73daf AP |
277 | */ |
278 | # define PTRDIFF_T uint64_t | |
279 | #else | |
280 | # define PTRDIFF_T size_t | |
281 | #endif | |
282 | ||
7141ba31 | 283 | int is_partially_overlapping(const void *ptr1, const void *ptr2, int len) |
c3a73daf AP |
284 | { |
285 | PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2; | |
286 | /* | |
287 | * Check for partially overlapping buffers. [Binary logical | |
288 | * operations are used instead of boolean to minimize number | |
289 | * of conditional branches.] | |
290 | */ | |
83151b73 AP |
291 | int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | |
292 | (diff > (0 - (PTRDIFF_T)len))); | |
b153f092 | 293 | |
83151b73 | 294 | return overlapped; |
c3a73daf AP |
295 | } |
296 | ||
be06a934 | 297 | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
0f113f3e MC |
298 | const unsigned char *in, int inl) |
299 | { | |
64846096 LP |
300 | int i, j, bl, cmpl = inl; |
301 | ||
302 | if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) | |
303 | cmpl = (cmpl + 7) / 8; | |
0f113f3e | 304 | |
7141ba31 MC |
305 | bl = ctx->cipher->block_size; |
306 | ||
0f113f3e | 307 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
7141ba31 | 308 | /* If block size > 1 then the cipher will have to do this check */ |
64846096 | 309 | if (bl == 1 && is_partially_overlapping(out, in, cmpl)) { |
83151b73 | 310 | EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING); |
5fc77684 | 311 | return 0; |
83151b73 | 312 | } |
5fc77684 | 313 | |
0f113f3e MC |
314 | i = ctx->cipher->do_cipher(ctx, out, in, inl); |
315 | if (i < 0) | |
316 | return 0; | |
317 | else | |
318 | *outl = i; | |
319 | return 1; | |
320 | } | |
321 | ||
322 | if (inl <= 0) { | |
323 | *outl = 0; | |
324 | return inl == 0; | |
325 | } | |
64846096 | 326 | if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) { |
83151b73 | 327 | EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING); |
5fc77684 | 328 | return 0; |
83151b73 | 329 | } |
0f113f3e MC |
330 | |
331 | if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) { | |
332 | if (ctx->cipher->do_cipher(ctx, out, in, inl)) { | |
333 | *outl = inl; | |
334 | return 1; | |
335 | } else { | |
336 | *outl = 0; | |
337 | return 0; | |
338 | } | |
339 | } | |
340 | i = ctx->buf_len; | |
0f113f3e MC |
341 | OPENSSL_assert(bl <= (int)sizeof(ctx->buf)); |
342 | if (i != 0) { | |
3f358213 | 343 | if (bl - i > inl) { |
0f113f3e MC |
344 | memcpy(&(ctx->buf[i]), in, inl); |
345 | ctx->buf_len += inl; | |
346 | *outl = 0; | |
347 | return 1; | |
348 | } else { | |
349 | j = bl - i; | |
350 | memcpy(&(ctx->buf[i]), in, j); | |
0f113f3e MC |
351 | inl -= j; |
352 | in += j; | |
5fc77684 AP |
353 | if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl)) |
354 | return 0; | |
0f113f3e MC |
355 | out += bl; |
356 | *outl = bl; | |
357 | } | |
358 | } else | |
359 | *outl = 0; | |
360 | i = inl & (bl - 1); | |
361 | inl -= i; | |
362 | if (inl > 0) { | |
363 | if (!ctx->cipher->do_cipher(ctx, out, in, inl)) | |
364 | return 0; | |
365 | *outl += inl; | |
366 | } | |
367 | ||
368 | if (i != 0) | |
369 | memcpy(ctx->buf, &(in[inl]), i); | |
370 | ctx->buf_len = i; | |
371 | return 1; | |
372 | } | |
d02b48c6 | 373 | |
be06a934 | 374 | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
0f113f3e MC |
375 | { |
376 | int ret; | |
377 | ret = EVP_EncryptFinal_ex(ctx, out, outl); | |
378 | return ret; | |
379 | } | |
581f1c84 DSH |
380 | |
381 | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |
0f113f3e MC |
382 | { |
383 | int n, ret; | |
384 | unsigned int i, b, bl; | |
385 | ||
386 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { | |
387 | ret = ctx->cipher->do_cipher(ctx, out, NULL, 0); | |
388 | if (ret < 0) | |
389 | return 0; | |
390 | else | |
391 | *outl = ret; | |
392 | return 1; | |
393 | } | |
394 | ||
395 | b = ctx->cipher->block_size; | |
cbe29648 | 396 | OPENSSL_assert(b <= sizeof(ctx->buf)); |
0f113f3e MC |
397 | if (b == 1) { |
398 | *outl = 0; | |
399 | return 1; | |
400 | } | |
401 | bl = ctx->buf_len; | |
402 | if (ctx->flags & EVP_CIPH_NO_PADDING) { | |
403 | if (bl) { | |
404 | EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, | |
405 | EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); | |
406 | return 0; | |
407 | } | |
408 | *outl = 0; | |
409 | return 1; | |
410 | } | |
411 | ||
412 | n = b - bl; | |
413 | for (i = bl; i < b; i++) | |
414 | ctx->buf[i] = n; | |
415 | ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b); | |
416 | ||
417 | if (ret) | |
418 | *outl = b; | |
419 | ||
420 | return ret; | |
421 | } | |
d02b48c6 | 422 | |
be06a934 | 423 | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
0f113f3e MC |
424 | const unsigned char *in, int inl) |
425 | { | |
64846096 | 426 | int fix_len, cmpl = inl; |
0f113f3e MC |
427 | unsigned int b; |
428 | ||
7141ba31 MC |
429 | b = ctx->cipher->block_size; |
430 | ||
64846096 LP |
431 | if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) |
432 | cmpl = (cmpl + 7) / 8; | |
433 | ||
0f113f3e | 434 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
64846096 | 435 | if (b == 1 && is_partially_overlapping(out, in, cmpl)) { |
83151b73 | 436 | EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING); |
5fc77684 | 437 | return 0; |
83151b73 | 438 | } |
5fc77684 | 439 | |
0f113f3e MC |
440 | fix_len = ctx->cipher->do_cipher(ctx, out, in, inl); |
441 | if (fix_len < 0) { | |
442 | *outl = 0; | |
443 | return 0; | |
444 | } else | |
445 | *outl = fix_len; | |
446 | return 1; | |
447 | } | |
448 | ||
449 | if (inl <= 0) { | |
450 | *outl = 0; | |
451 | return inl == 0; | |
452 | } | |
453 | ||
454 | if (ctx->flags & EVP_CIPH_NO_PADDING) | |
455 | return EVP_EncryptUpdate(ctx, out, outl, in, inl); | |
456 | ||
cbe29648 | 457 | OPENSSL_assert(b <= sizeof(ctx->final)); |
0f113f3e MC |
458 | |
459 | if (ctx->final_used) { | |
5fc77684 AP |
460 | /* see comment about PTRDIFF_T comparison above */ |
461 | if (((PTRDIFF_T)out == (PTRDIFF_T)in) | |
83151b73 AP |
462 | || is_partially_overlapping(out, in, b)) { |
463 | EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING); | |
5fc77684 | 464 | return 0; |
83151b73 | 465 | } |
0f113f3e MC |
466 | memcpy(out, ctx->final, b); |
467 | out += b; | |
468 | fix_len = 1; | |
469 | } else | |
470 | fix_len = 0; | |
471 | ||
472 | if (!EVP_EncryptUpdate(ctx, out, outl, in, inl)) | |
473 | return 0; | |
474 | ||
475 | /* | |
476 | * if we have 'decrypted' a multiple of block size, make sure we have a | |
477 | * copy of this last block | |
478 | */ | |
479 | if (b > 1 && !ctx->buf_len) { | |
480 | *outl -= b; | |
481 | ctx->final_used = 1; | |
482 | memcpy(ctx->final, &out[*outl], b); | |
483 | } else | |
484 | ctx->final_used = 0; | |
485 | ||
486 | if (fix_len) | |
487 | *outl += b; | |
488 | ||
489 | return 1; | |
490 | } | |
d02b48c6 | 491 | |
6b691a5c | 492 | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
0f113f3e MC |
493 | { |
494 | int ret; | |
495 | ret = EVP_DecryptFinal_ex(ctx, out, outl); | |
496 | return ret; | |
497 | } | |
581f1c84 DSH |
498 | |
499 | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |
0f113f3e MC |
500 | { |
501 | int i, n; | |
502 | unsigned int b; | |
503 | *outl = 0; | |
504 | ||
505 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { | |
506 | i = ctx->cipher->do_cipher(ctx, out, NULL, 0); | |
507 | if (i < 0) | |
508 | return 0; | |
509 | else | |
510 | *outl = i; | |
511 | return 1; | |
512 | } | |
513 | ||
514 | b = ctx->cipher->block_size; | |
515 | if (ctx->flags & EVP_CIPH_NO_PADDING) { | |
516 | if (ctx->buf_len) { | |
517 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, | |
518 | EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); | |
519 | return 0; | |
520 | } | |
521 | *outl = 0; | |
522 | return 1; | |
523 | } | |
524 | if (b > 1) { | |
525 | if (ctx->buf_len || !ctx->final_used) { | |
526 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH); | |
26a7d938 | 527 | return 0; |
0f113f3e | 528 | } |
cbe29648 | 529 | OPENSSL_assert(b <= sizeof(ctx->final)); |
0f113f3e MC |
530 | |
531 | /* | |
532 | * The following assumes that the ciphertext has been authenticated. | |
533 | * Otherwise it provides a padding oracle. | |
534 | */ | |
535 | n = ctx->final[b - 1]; | |
536 | if (n == 0 || n > (int)b) { | |
537 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT); | |
26a7d938 | 538 | return 0; |
0f113f3e MC |
539 | } |
540 | for (i = 0; i < n; i++) { | |
541 | if (ctx->final[--b] != n) { | |
542 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT); | |
26a7d938 | 543 | return 0; |
0f113f3e MC |
544 | } |
545 | } | |
546 | n = ctx->cipher->block_size - n; | |
547 | for (i = 0; i < n; i++) | |
548 | out[i] = ctx->final[i]; | |
549 | *outl = n; | |
550 | } else | |
551 | *outl = 0; | |
208fb891 | 552 | return 1; |
0f113f3e | 553 | } |
d02b48c6 | 554 | |
6343829a | 555 | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) |
0f113f3e MC |
556 | { |
557 | if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) | |
558 | return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL); | |
559 | if (c->key_len == keylen) | |
560 | return 1; | |
561 | if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { | |
562 | c->key_len = keylen; | |
563 | return 1; | |
564 | } | |
565 | EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH); | |
566 | return 0; | |
567 | } | |
49528751 | 568 | |
f2e5ca84 | 569 | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) |
0f113f3e MC |
570 | { |
571 | if (pad) | |
572 | ctx->flags &= ~EVP_CIPH_NO_PADDING; | |
573 | else | |
574 | ctx->flags |= EVP_CIPH_NO_PADDING; | |
575 | return 1; | |
576 | } | |
f2e5ca84 | 577 | |
49528751 DSH |
578 | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) |
579 | { | |
0f113f3e | 580 | int ret; |
d91f4568 KR |
581 | |
582 | if (type == EVP_CTRL_GET_DRBG) { | |
583 | *(RAND_DRBG **)ptr = ctx->drbg; | |
584 | return 1; | |
585 | } | |
586 | if (type == EVP_CTRL_SET_DRBG) { | |
587 | ctx->drbg = ptr; | |
588 | return 1; | |
589 | } | |
0f113f3e MC |
590 | if (!ctx->cipher) { |
591 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | if (!ctx->cipher->ctrl) { | |
596 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED); | |
597 | return 0; | |
598 | } | |
599 | ||
600 | ret = ctx->cipher->ctrl(ctx, type, arg, ptr); | |
601 | if (ret == -1) { | |
602 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, | |
603 | EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); | |
604 | return 0; | |
605 | } | |
606 | return ret; | |
49528751 | 607 | } |
216659eb DSH |
608 | |
609 | int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) | |
0f113f3e MC |
610 | { |
611 | if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) | |
612 | return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); | |
d91f4568 KR |
613 | if (ctx->drbg) { |
614 | if (RAND_DRBG_bytes(ctx->drbg, key, ctx->key_len) == 0) | |
615 | return 0; | |
616 | } else if (RAND_bytes(key, ctx->key_len) <= 0) { | |
0f113f3e | 617 | return 0; |
d91f4568 | 618 | } |
0f113f3e MC |
619 | return 1; |
620 | } | |
216659eb | 621 | |
c2bf7208 | 622 | int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) |
0f113f3e MC |
623 | { |
624 | if ((in == NULL) || (in->cipher == NULL)) { | |
625 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED); | |
626 | return 0; | |
627 | } | |
c2bf7208 | 628 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
629 | /* Make sure it's safe to copy a cipher context using an ENGINE */ |
630 | if (in->engine && !ENGINE_init(in->engine)) { | |
631 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB); | |
632 | return 0; | |
633 | } | |
c2bf7208 DSH |
634 | #endif |
635 | ||
c0ca39bd | 636 | EVP_CIPHER_CTX_reset(out); |
b4faea50 | 637 | memcpy(out, in, sizeof(*out)); |
0f113f3e MC |
638 | |
639 | if (in->cipher_data && in->cipher->ctx_size) { | |
640 | out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size); | |
90945fa3 | 641 | if (out->cipher_data == NULL) { |
273a0218 | 642 | out->cipher = NULL; |
0f113f3e MC |
643 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE); |
644 | return 0; | |
645 | } | |
646 | memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); | |
647 | } | |
648 | ||
649 | if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) | |
273a0218 BE |
650 | if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { |
651 | out->cipher = NULL; | |
652 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR); | |
653 | return 0; | |
654 | } | |
0f113f3e MC |
655 | return 1; |
656 | } |