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