1 |
|
|
/* $OpenBSD: evp_enc.c,v 1.36 2017/01/29 17:49:23 beck Exp $ */ |
2 |
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 |
|
|
* All rights reserved. |
4 |
|
|
* |
5 |
|
|
* This package is an SSL implementation written |
6 |
|
|
* by Eric Young (eay@cryptsoft.com). |
7 |
|
|
* The implementation was written so as to conform with Netscapes SSL. |
8 |
|
|
* |
9 |
|
|
* This library is free for commercial and non-commercial use as long as |
10 |
|
|
* the following conditions are aheared to. The following conditions |
11 |
|
|
* apply to all code found in this distribution, be it the RC4, RSA, |
12 |
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 |
|
|
* included with this distribution is covered by the same copyright terms |
14 |
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 |
|
|
* |
16 |
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in |
17 |
|
|
* the code are not to be removed. |
18 |
|
|
* If this package is used in a product, Eric Young should be given attribution |
19 |
|
|
* as the author of the parts of the library used. |
20 |
|
|
* This can be in the form of a textual message at program startup or |
21 |
|
|
* in documentation (online or textual) provided with the package. |
22 |
|
|
* |
23 |
|
|
* Redistribution and use in source and binary forms, with or without |
24 |
|
|
* modification, are permitted provided that the following conditions |
25 |
|
|
* are met: |
26 |
|
|
* 1. Redistributions of source code must retain the copyright |
27 |
|
|
* notice, this list of conditions and the following disclaimer. |
28 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
29 |
|
|
* notice, this list of conditions and the following disclaimer in the |
30 |
|
|
* documentation and/or other materials provided with the distribution. |
31 |
|
|
* 3. All advertising materials mentioning features or use of this software |
32 |
|
|
* must display the following acknowledgement: |
33 |
|
|
* "This product includes cryptographic software written by |
34 |
|
|
* Eric Young (eay@cryptsoft.com)" |
35 |
|
|
* The word 'cryptographic' can be left out if the rouines from the library |
36 |
|
|
* being used are not cryptographic related :-). |
37 |
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from |
38 |
|
|
* the apps directory (application code) you must include an acknowledgement: |
39 |
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 |
|
|
* |
41 |
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 |
|
|
* SUCH DAMAGE. |
52 |
|
|
* |
53 |
|
|
* The licence and distribution terms for any publically available version or |
54 |
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be |
55 |
|
|
* copied and put under another distribution licence |
56 |
|
|
* [including the GNU Public Licence.] |
57 |
|
|
*/ |
58 |
|
|
|
59 |
|
|
#include <stdio.h> |
60 |
|
|
#include <stdlib.h> |
61 |
|
|
#include <string.h> |
62 |
|
|
|
63 |
|
|
#include <sys/types.h> |
64 |
|
|
|
65 |
|
|
#include <openssl/opensslconf.h> |
66 |
|
|
|
67 |
|
|
#include <openssl/err.h> |
68 |
|
|
#include <openssl/evp.h> |
69 |
|
|
|
70 |
|
|
#ifndef OPENSSL_NO_ENGINE |
71 |
|
|
#include <openssl/engine.h> |
72 |
|
|
#endif |
73 |
|
|
|
74 |
|
|
#include "evp_locl.h" |
75 |
|
|
|
76 |
|
|
#define M_do_cipher(ctx, out, in, inl) ctx->cipher->do_cipher(ctx, out, in, inl) |
77 |
|
|
|
78 |
|
|
void |
79 |
|
|
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) |
80 |
|
|
{ |
81 |
|
3122 |
memset(ctx, 0, sizeof(EVP_CIPHER_CTX)); |
82 |
|
1561 |
} |
83 |
|
|
|
84 |
|
|
EVP_CIPHER_CTX * |
85 |
|
|
EVP_CIPHER_CTX_new(void) |
86 |
|
|
{ |
87 |
|
984 |
return calloc(1, sizeof(EVP_CIPHER_CTX)); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
int |
91 |
|
|
EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
92 |
|
|
const unsigned char *key, const unsigned char *iv, int enc) |
93 |
|
|
{ |
94 |
|
|
if (cipher) |
95 |
|
|
EVP_CIPHER_CTX_init(ctx); |
96 |
|
|
return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
int |
100 |
|
|
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, |
101 |
|
|
const unsigned char *key, const unsigned char *iv, int enc) |
102 |
|
|
{ |
103 |
✗✓ |
5814 |
if (enc == -1) |
104 |
|
|
enc = ctx->encrypt; |
105 |
|
|
else { |
106 |
✓✓ |
2907 |
if (enc) |
107 |
|
1625 |
enc = 1; |
108 |
|
2907 |
ctx->encrypt = enc; |
109 |
|
|
} |
110 |
|
|
#ifndef OPENSSL_NO_ENGINE |
111 |
|
|
/* Whether it's nice or not, "Inits" can be used on "Final"'d contexts |
112 |
|
|
* so this context may already have an ENGINE! Try to avoid releasing |
113 |
|
|
* the previous handle, re-querying for an ENGINE, and having a |
114 |
|
|
* reinitialisation, when it may all be unecessary. */ |
115 |
✗✓✗✗ ✗✗ |
2907 |
if (ctx->engine && ctx->cipher && |
116 |
|
|
(!cipher || (cipher && (cipher->nid == ctx->cipher->nid)))) |
117 |
|
|
goto skip_to_init; |
118 |
|
|
#endif |
119 |
✓✓ |
2907 |
if (cipher) { |
120 |
|
|
/* Ensure a context left lying around from last time is cleared |
121 |
|
|
* (the previous check attempted to avoid this if the same |
122 |
|
|
* ENGINE and EVP_CIPHER could be used). */ |
123 |
✓✓ |
2173 |
if (ctx->cipher) { |
124 |
|
198 |
unsigned long flags = ctx->flags; |
125 |
|
198 |
EVP_CIPHER_CTX_cleanup(ctx); |
126 |
|
|
/* Restore encrypt and flags */ |
127 |
|
198 |
ctx->encrypt = enc; |
128 |
|
198 |
ctx->flags = flags; |
129 |
|
198 |
} |
130 |
|
|
#ifndef OPENSSL_NO_ENGINE |
131 |
✗✓ |
2173 |
if (impl) { |
132 |
|
|
if (!ENGINE_init(impl)) { |
133 |
|
|
EVPerror(EVP_R_INITIALIZATION_ERROR); |
134 |
|
|
return 0; |
135 |
|
|
} |
136 |
|
|
} else |
137 |
|
|
/* Ask if an ENGINE is reserved for this job */ |
138 |
|
2173 |
impl = ENGINE_get_cipher_engine(cipher->nid); |
139 |
✗✓ |
2173 |
if (impl) { |
140 |
|
|
/* There's an ENGINE for this job ... (apparently) */ |
141 |
|
|
const EVP_CIPHER *c = |
142 |
|
|
ENGINE_get_cipher(impl, cipher->nid); |
143 |
|
|
if (!c) { |
144 |
|
|
EVPerror(EVP_R_INITIALIZATION_ERROR); |
145 |
|
|
return 0; |
146 |
|
|
} |
147 |
|
|
/* We'll use the ENGINE's private cipher definition */ |
148 |
|
|
cipher = c; |
149 |
|
|
/* Store the ENGINE functional reference so we know |
150 |
|
|
* 'cipher' came from an ENGINE and we need to release |
151 |
|
|
* it when done. */ |
152 |
|
|
ctx->engine = impl; |
153 |
|
|
} else |
154 |
|
2173 |
ctx->engine = NULL; |
155 |
|
|
#endif |
156 |
|
|
|
157 |
|
2173 |
ctx->cipher = cipher; |
158 |
✓✓ |
2173 |
if (ctx->cipher->ctx_size) { |
159 |
|
2137 |
ctx->cipher_data = malloc(ctx->cipher->ctx_size); |
160 |
✗✓ |
2137 |
if (!ctx->cipher_data) { |
161 |
|
|
EVPerror(ERR_R_MALLOC_FAILURE); |
162 |
|
|
return 0; |
163 |
|
|
} |
164 |
|
|
} else { |
165 |
|
36 |
ctx->cipher_data = NULL; |
166 |
|
|
} |
167 |
|
2173 |
ctx->key_len = cipher->key_len; |
168 |
|
2173 |
ctx->flags = 0; |
169 |
✓✓ |
2173 |
if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { |
170 |
✗✓ |
112 |
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { |
171 |
|
|
EVPerror(EVP_R_INITIALIZATION_ERROR); |
172 |
|
|
return 0; |
173 |
|
|
} |
174 |
|
|
} |
175 |
✗✓ |
734 |
} else if (!ctx->cipher) { |
176 |
|
|
EVPerror(EVP_R_NO_CIPHER_SET); |
177 |
|
|
return 0; |
178 |
|
|
} |
179 |
|
|
#ifndef OPENSSL_NO_ENGINE |
180 |
|
|
skip_to_init: |
181 |
|
|
#endif |
182 |
|
|
/* we assume block size is a power of 2 in *cryptUpdate */ |
183 |
✓✓✗✓
|
4261 |
if (ctx->cipher->block_size != 1 && |
184 |
✓✓ |
1904 |
ctx->cipher->block_size != 8 && |
185 |
|
1354 |
ctx->cipher->block_size != 16) { |
186 |
|
|
EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
187 |
|
|
return 0; |
188 |
|
|
} |
189 |
|
|
|
190 |
✓✗ |
2907 |
if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) { |
191 |
✗✓✓✓ ✗✗✓ |
6004 |
switch (EVP_CIPHER_CTX_mode(ctx)) { |
192 |
|
|
|
193 |
|
|
case EVP_CIPH_STREAM_CIPHER: |
194 |
|
|
case EVP_CIPH_ECB_MODE: |
195 |
|
|
break; |
196 |
|
|
|
197 |
|
|
case EVP_CIPH_CFB_MODE: |
198 |
|
|
case EVP_CIPH_OFB_MODE: |
199 |
|
|
|
200 |
|
824 |
ctx->num = 0; |
201 |
|
|
/* fall-through */ |
202 |
|
|
|
203 |
|
|
case EVP_CIPH_CBC_MODE: |
204 |
|
|
|
205 |
✗✓ |
2246 |
if ((size_t)EVP_CIPHER_CTX_iv_length(ctx) > |
206 |
|
|
sizeof(ctx->iv)) { |
207 |
|
|
EVPerror(EVP_R_IV_TOO_LARGE); |
208 |
|
|
return 0; |
209 |
|
|
} |
210 |
✓✓ |
2246 |
if (iv) |
211 |
|
3334 |
memcpy(ctx->oiv, iv, |
212 |
|
1667 |
EVP_CIPHER_CTX_iv_length(ctx)); |
213 |
|
4492 |
memcpy(ctx->iv, ctx->oiv, |
214 |
|
2246 |
EVP_CIPHER_CTX_iv_length(ctx)); |
215 |
|
2246 |
break; |
216 |
|
|
|
217 |
|
|
case EVP_CIPH_CTR_MODE: |
218 |
|
27 |
ctx->num = 0; |
219 |
|
|
/* Don't reuse IV for CTR mode */ |
220 |
✓✗ |
27 |
if (iv) |
221 |
|
54 |
memcpy(ctx->iv, iv, |
222 |
|
27 |
EVP_CIPHER_CTX_iv_length(ctx)); |
223 |
|
|
break; |
224 |
|
|
|
225 |
|
|
default: |
226 |
|
|
return 0; |
227 |
|
|
break; |
228 |
|
|
} |
229 |
|
|
} |
230 |
|
|
|
231 |
✓✓✗✓
|
3657 |
if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { |
232 |
✗✓ |
2157 |
if (!ctx->cipher->init(ctx, key, iv, enc)) |
233 |
|
|
return 0; |
234 |
|
|
} |
235 |
|
2907 |
ctx->buf_len = 0; |
236 |
|
2907 |
ctx->final_used = 0; |
237 |
|
2907 |
ctx->block_mask = ctx->cipher->block_size - 1; |
238 |
|
2907 |
return 1; |
239 |
|
2907 |
} |
240 |
|
|
|
241 |
|
|
int |
242 |
|
|
EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
243 |
|
|
const unsigned char *in, int inl) |
244 |
|
|
{ |
245 |
✓✓ |
6024 |
if (ctx->encrypt) |
246 |
|
1695 |
return EVP_EncryptUpdate(ctx, out, outl, in, inl); |
247 |
|
|
else |
248 |
|
1317 |
return EVP_DecryptUpdate(ctx, out, outl, in, inl); |
249 |
|
3012 |
} |
250 |
|
|
|
251 |
|
|
int |
252 |
|
|
EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
253 |
|
|
{ |
254 |
✓✓ |
1320 |
if (ctx->encrypt) |
255 |
|
323 |
return EVP_EncryptFinal_ex(ctx, out, outl); |
256 |
|
|
else |
257 |
|
337 |
return EVP_DecryptFinal_ex(ctx, out, outl); |
258 |
|
660 |
} |
259 |
|
|
|
260 |
|
|
__warn_references(EVP_CipherFinal, |
261 |
|
|
"warning: EVP_CipherFinal is often misused, please use EVP_CipherFinal_ex and EVP_CIPHER_CTX_cleanup"); |
262 |
|
|
|
263 |
|
|
int |
264 |
|
|
EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
265 |
|
|
{ |
266 |
|
|
int ret; |
267 |
|
|
if (ctx->encrypt) |
268 |
|
|
ret = EVP_EncryptFinal_ex(ctx, out, outl); |
269 |
|
|
else |
270 |
|
|
ret = EVP_DecryptFinal_ex(ctx, out, outl); |
271 |
|
|
return ret; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
int |
275 |
|
|
EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
276 |
|
|
const unsigned char *key, const unsigned char *iv) |
277 |
|
|
{ |
278 |
|
|
return EVP_CipherInit(ctx, cipher, key, iv, 1); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
int |
282 |
|
|
EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, |
283 |
|
|
const unsigned char *key, const unsigned char *iv) |
284 |
|
|
{ |
285 |
|
1470 |
return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
int |
289 |
|
|
EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
290 |
|
|
const unsigned char *key, const unsigned char *iv) |
291 |
|
|
{ |
292 |
|
|
return EVP_CipherInit(ctx, cipher, key, iv, 0); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
int |
296 |
|
|
EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, |
297 |
|
|
const unsigned char *key, const unsigned char *iv) |
298 |
|
|
{ |
299 |
|
696 |
return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
int |
303 |
|
|
EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
304 |
|
|
const unsigned char *in, int inl) |
305 |
|
|
{ |
306 |
|
|
int i, j, bl; |
307 |
|
|
|
308 |
✗✓ |
7982 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
309 |
|
|
i = M_do_cipher(ctx, out, in, inl); |
310 |
|
|
if (i < 0) |
311 |
|
|
return 0; |
312 |
|
|
else |
313 |
|
|
*outl = i; |
314 |
|
|
return 1; |
315 |
|
|
} |
316 |
|
|
|
317 |
✗✓ |
3991 |
if (inl <= 0) { |
318 |
|
|
*outl = 0; |
319 |
|
|
return inl == 0; |
320 |
|
|
} |
321 |
|
|
|
322 |
✓✓✓✓
|
6926 |
if (ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0) { |
323 |
✓✗ |
2488 |
if (M_do_cipher(ctx, out, in, inl)) { |
324 |
|
2488 |
*outl = inl; |
325 |
|
2488 |
return 1; |
326 |
|
|
} else { |
327 |
|
|
*outl = 0; |
328 |
|
|
return 0; |
329 |
|
|
} |
330 |
|
|
} |
331 |
|
1503 |
i = ctx->buf_len; |
332 |
|
1503 |
bl = ctx->cipher->block_size; |
333 |
✗✓ |
1503 |
if ((size_t)bl > sizeof(ctx->buf)) { |
334 |
|
|
EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
335 |
|
|
*outl = 0; |
336 |
|
|
return 0; |
337 |
|
|
} |
338 |
✓✓ |
1503 |
if (i != 0) { |
339 |
✗✓ |
1056 |
if (bl - i > inl) { |
340 |
|
|
memcpy(&(ctx->buf[i]), in, inl); |
341 |
|
|
ctx->buf_len += inl; |
342 |
|
|
*outl = 0; |
343 |
|
|
return 1; |
344 |
|
|
} else { |
345 |
|
|
j = bl - i; |
346 |
|
1056 |
memcpy(&(ctx->buf[i]), in, j); |
347 |
✗✓ |
1056 |
if (!M_do_cipher(ctx, out, ctx->buf, bl)) |
348 |
|
|
return 0; |
349 |
|
1056 |
inl -= j; |
350 |
|
1056 |
in += j; |
351 |
|
1056 |
out += bl; |
352 |
|
1056 |
*outl = bl; |
353 |
|
|
} |
354 |
|
1056 |
} else |
355 |
|
447 |
*outl = 0; |
356 |
|
1503 |
i = inl&(bl - 1); |
357 |
|
1503 |
inl -= i; |
358 |
✓✗ |
1503 |
if (inl > 0) { |
359 |
✗✓ |
1503 |
if (!M_do_cipher(ctx, out, in, inl)) |
360 |
|
|
return 0; |
361 |
|
1503 |
*outl += inl; |
362 |
|
1503 |
} |
363 |
|
|
|
364 |
✓✓ |
1503 |
if (i != 0) |
365 |
|
1415 |
memcpy(ctx->buf, &(in[inl]), i); |
366 |
|
1503 |
ctx->buf_len = i; |
367 |
|
1503 |
return 1; |
368 |
|
3991 |
} |
369 |
|
|
|
370 |
|
|
__warn_references(EVP_EncryptFinal, |
371 |
|
|
"warning: EVP_EncryptFinal is often misused, please use EVP_EncryptFinal_ex and EVP_CIPHER_CTX_cleanup"); |
372 |
|
|
|
373 |
|
|
int |
374 |
|
|
EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
375 |
|
|
{ |
376 |
|
|
int ret; |
377 |
|
|
|
378 |
|
|
ret = EVP_EncryptFinal_ex(ctx, out, outl); |
379 |
|
|
return ret; |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
int |
383 |
|
|
EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
384 |
|
|
{ |
385 |
|
|
int n, ret; |
386 |
|
|
unsigned int i, b, bl; |
387 |
|
|
|
388 |
✗✓ |
1884 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
389 |
|
|
ret = M_do_cipher(ctx, out, NULL, 0); |
390 |
|
|
if (ret < 0) |
391 |
|
|
return 0; |
392 |
|
|
else |
393 |
|
|
*outl = ret; |
394 |
|
|
return 1; |
395 |
|
|
} |
396 |
|
|
|
397 |
|
942 |
b = ctx->cipher->block_size; |
398 |
✗✓ |
942 |
if (b > sizeof ctx->buf) { |
399 |
|
|
EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
400 |
|
|
return 0; |
401 |
|
|
} |
402 |
✓✓ |
942 |
if (b == 1) { |
403 |
|
379 |
*outl = 0; |
404 |
|
379 |
return 1; |
405 |
|
|
} |
406 |
|
563 |
bl = ctx->buf_len; |
407 |
✓✓ |
563 |
if (ctx->flags & EVP_CIPH_NO_PADDING) { |
408 |
✗✓ |
198 |
if (bl) { |
409 |
|
|
EVPerror(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
410 |
|
|
return 0; |
411 |
|
|
} |
412 |
|
198 |
*outl = 0; |
413 |
|
198 |
return 1; |
414 |
|
|
} |
415 |
|
|
|
416 |
|
365 |
n = b - bl; |
417 |
✓✓ |
5874 |
for (i = bl; i < b; i++) |
418 |
|
2572 |
ctx->buf[i] = n; |
419 |
|
365 |
ret = M_do_cipher(ctx, out, ctx->buf, b); |
420 |
|
|
|
421 |
|
|
|
422 |
✓✗ |
365 |
if (ret) |
423 |
|
365 |
*outl = b; |
424 |
|
|
|
425 |
|
365 |
return ret; |
426 |
|
942 |
} |
427 |
|
|
|
428 |
|
|
int |
429 |
|
|
EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
430 |
|
|
const unsigned char *in, int inl) |
431 |
|
|
{ |
432 |
|
|
int fix_len; |
433 |
|
|
unsigned int b; |
434 |
|
|
|
435 |
✗✓ |
3330 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
436 |
|
|
fix_len = M_do_cipher(ctx, out, in, inl); |
437 |
|
|
if (fix_len < 0) { |
438 |
|
|
*outl = 0; |
439 |
|
|
return 0; |
440 |
|
|
} else |
441 |
|
|
*outl = fix_len; |
442 |
|
|
return 1; |
443 |
|
|
} |
444 |
|
|
|
445 |
✗✓ |
1665 |
if (inl <= 0) { |
446 |
|
|
*outl = 0; |
447 |
|
|
return inl == 0; |
448 |
|
|
} |
449 |
|
|
|
450 |
✓✓ |
1665 |
if (ctx->flags & EVP_CIPH_NO_PADDING) |
451 |
|
342 |
return EVP_EncryptUpdate(ctx, out, outl, in, inl); |
452 |
|
|
|
453 |
|
1323 |
b = ctx->cipher->block_size; |
454 |
✗✓ |
1323 |
if (b > sizeof ctx->final) { |
455 |
|
|
EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
456 |
|
|
return 0; |
457 |
|
|
} |
458 |
|
|
|
459 |
✗✓ |
1323 |
if (ctx->final_used) { |
460 |
|
|
memcpy(out, ctx->final, b); |
461 |
|
|
out += b; |
462 |
|
|
fix_len = 1; |
463 |
|
|
} else |
464 |
|
|
fix_len = 0; |
465 |
|
|
|
466 |
|
|
|
467 |
✗✓ |
1323 |
if (!EVP_EncryptUpdate(ctx, out, outl, in, inl)) |
468 |
|
|
return 0; |
469 |
|
|
|
470 |
|
|
/* if we have 'decrypted' a multiple of block size, make sure |
471 |
|
|
* we have a copy of this last block */ |
472 |
✓✓✓✓
|
1964 |
if (b > 1 && !ctx->buf_len) { |
473 |
|
201 |
*outl -= b; |
474 |
|
201 |
ctx->final_used = 1; |
475 |
|
201 |
memcpy(ctx->final, &out[*outl], b); |
476 |
|
201 |
} else |
477 |
|
1122 |
ctx->final_used = 0; |
478 |
|
|
|
479 |
✗✓ |
1323 |
if (fix_len) |
480 |
|
|
*outl += b; |
481 |
|
|
|
482 |
|
1323 |
return 1; |
483 |
|
1665 |
} |
484 |
|
|
|
485 |
|
|
__warn_references(EVP_DecryptFinal, |
486 |
|
|
"warning: EVP_DecryptFinal is often misused, please use EVP_DecryptFinal_ex and EVP_CIPHER_CTX_cleanup"); |
487 |
|
|
|
488 |
|
|
int |
489 |
|
|
EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
490 |
|
|
{ |
491 |
|
|
int ret; |
492 |
|
|
|
493 |
|
|
ret = EVP_DecryptFinal_ex(ctx, out, outl); |
494 |
|
|
return ret; |
495 |
|
|
} |
496 |
|
|
|
497 |
|
|
int |
498 |
|
|
EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
499 |
|
|
{ |
500 |
|
|
int i, n; |
501 |
|
|
unsigned int b; |
502 |
|
1370 |
*outl = 0; |
503 |
|
|
|
504 |
✗✓ |
685 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
505 |
|
|
i = M_do_cipher(ctx, out, NULL, 0); |
506 |
|
|
if (i < 0) |
507 |
|
|
return 0; |
508 |
|
|
else |
509 |
|
|
*outl = i; |
510 |
|
|
return 1; |
511 |
|
|
} |
512 |
|
|
|
513 |
|
685 |
b = ctx->cipher->block_size; |
514 |
✓✓ |
685 |
if (ctx->flags & EVP_CIPH_NO_PADDING) { |
515 |
✗✓ |
342 |
if (ctx->buf_len) { |
516 |
|
|
EVPerror(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
517 |
|
|
return 0; |
518 |
|
|
} |
519 |
|
342 |
*outl = 0; |
520 |
|
342 |
return 1; |
521 |
|
|
} |
522 |
✓✓ |
343 |
if (b > 1) { |
523 |
✓✗✗✓
|
402 |
if (ctx->buf_len || !ctx->final_used) { |
524 |
|
|
EVPerror(EVP_R_WRONG_FINAL_BLOCK_LENGTH); |
525 |
|
|
return (0); |
526 |
|
|
} |
527 |
✗✓ |
201 |
if (b > sizeof ctx->final) { |
528 |
|
|
EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
529 |
|
|
return 0; |
530 |
|
|
} |
531 |
|
201 |
n = ctx->final[b - 1]; |
532 |
✓✗✗✓
|
402 |
if (n == 0 || n > (int)b) { |
533 |
|
|
EVPerror(EVP_R_BAD_DECRYPT); |
534 |
|
|
return (0); |
535 |
|
|
} |
536 |
✓✓ |
2862 |
for (i = 0; i < n; i++) { |
537 |
✗✓ |
1230 |
if (ctx->final[--b] != n) { |
538 |
|
|
EVPerror(EVP_R_BAD_DECRYPT); |
539 |
|
|
return (0); |
540 |
|
|
} |
541 |
|
|
} |
542 |
|
201 |
n = ctx->cipher->block_size - n; |
543 |
✓✓ |
2550 |
for (i = 0; i < n; i++) |
544 |
|
1074 |
out[i] = ctx->final[i]; |
545 |
|
201 |
*outl = n; |
546 |
|
201 |
} else |
547 |
|
142 |
*outl = 0; |
548 |
|
343 |
return (1); |
549 |
|
685 |
} |
550 |
|
|
|
551 |
|
|
void |
552 |
|
|
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) |
553 |
|
|
{ |
554 |
✓✓ |
143372 |
if (ctx) { |
555 |
|
492 |
EVP_CIPHER_CTX_cleanup(ctx); |
556 |
|
492 |
free(ctx); |
557 |
|
492 |
} |
558 |
|
71686 |
} |
559 |
|
|
|
560 |
|
|
int |
561 |
|
|
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) |
562 |
|
|
{ |
563 |
✓✓ |
4658 |
if (c->cipher != NULL) { |
564 |
✗✓✗✗
|
2251 |
if (c->cipher->cleanup && !c->cipher->cleanup(c)) |
565 |
|
|
return 0; |
566 |
|
|
/* Cleanse cipher context data */ |
567 |
✓✓ |
2251 |
if (c->cipher_data) |
568 |
|
2215 |
explicit_bzero(c->cipher_data, c->cipher->ctx_size); |
569 |
|
|
} |
570 |
|
2329 |
free(c->cipher_data); |
571 |
|
|
#ifndef OPENSSL_NO_ENGINE |
572 |
✗✓ |
2329 |
if (c->engine) |
573 |
|
|
/* The EVP_CIPHER we used belongs to an ENGINE, release the |
574 |
|
|
* functional reference we held for this reason. */ |
575 |
|
|
ENGINE_finish(c->engine); |
576 |
|
|
#endif |
577 |
|
2329 |
explicit_bzero(c, sizeof(EVP_CIPHER_CTX)); |
578 |
|
2329 |
return 1; |
579 |
|
2329 |
} |
580 |
|
|
|
581 |
|
|
int |
582 |
|
|
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) |
583 |
|
|
{ |
584 |
✗✓ |
52 |
if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) |
585 |
|
|
return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, |
586 |
|
|
keylen, NULL); |
587 |
✓✗ |
26 |
if (c->key_len == keylen) |
588 |
|
26 |
return 1; |
589 |
|
|
if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { |
590 |
|
|
c->key_len = keylen; |
591 |
|
|
return 1; |
592 |
|
|
} |
593 |
|
|
EVPerror(EVP_R_INVALID_KEY_LENGTH); |
594 |
|
|
return 0; |
595 |
|
26 |
} |
596 |
|
|
|
597 |
|
|
int |
598 |
|
|
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) |
599 |
|
|
{ |
600 |
|
2259 |
if (pad) |
601 |
|
753 |
ctx->flags &= ~EVP_CIPH_NO_PADDING; |
602 |
|
|
else |
603 |
|
753 |
ctx->flags |= EVP_CIPH_NO_PADDING; |
604 |
|
753 |
return 1; |
605 |
|
|
} |
606 |
|
|
|
607 |
|
|
int |
608 |
|
|
EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) |
609 |
|
|
{ |
610 |
|
|
int ret; |
611 |
|
|
|
612 |
✗✓ |
2272 |
if (!ctx->cipher) { |
613 |
|
|
EVPerror(EVP_R_NO_CIPHER_SET); |
614 |
|
|
return 0; |
615 |
|
|
} |
616 |
|
|
|
617 |
✓✓ |
1136 |
if (!ctx->cipher->ctrl) { |
618 |
|
14 |
EVPerror(EVP_R_CTRL_NOT_IMPLEMENTED); |
619 |
|
14 |
return 0; |
620 |
|
|
} |
621 |
|
|
|
622 |
|
1122 |
ret = ctx->cipher->ctrl(ctx, type, arg, ptr); |
623 |
✓✓ |
1122 |
if (ret == -1) { |
624 |
|
2 |
EVPerror(EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
625 |
|
2 |
return 0; |
626 |
|
|
} |
627 |
|
1120 |
return ret; |
628 |
|
1136 |
} |
629 |
|
|
|
630 |
|
|
int |
631 |
|
|
EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) |
632 |
|
|
{ |
633 |
✗✓ |
12 |
if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) |
634 |
|
|
return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); |
635 |
|
6 |
arc4random_buf(key, ctx->key_len); |
636 |
|
6 |
return 1; |
637 |
|
6 |
} |
638 |
|
|
|
639 |
|
|
int |
640 |
|
|
EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) |
641 |
|
|
{ |
642 |
✓✗✗✓
|
234 |
if ((in == NULL) || (in->cipher == NULL)) { |
643 |
|
|
EVPerror(EVP_R_INPUT_NOT_INITIALIZED); |
644 |
|
|
return 0; |
645 |
|
|
} |
646 |
|
|
#ifndef OPENSSL_NO_ENGINE |
647 |
|
|
/* Make sure it's safe to copy a cipher context using an ENGINE */ |
648 |
✗✓✗✗
|
78 |
if (in->engine && !ENGINE_init(in->engine)) { |
649 |
|
|
EVPerror(ERR_R_ENGINE_LIB); |
650 |
|
|
return 0; |
651 |
|
|
} |
652 |
|
|
#endif |
653 |
|
|
|
654 |
|
78 |
EVP_CIPHER_CTX_cleanup(out); |
655 |
|
78 |
memcpy(out, in, sizeof *out); |
656 |
|
|
|
657 |
✓✗✓✗
|
156 |
if (in->cipher_data && in->cipher->ctx_size) { |
658 |
|
78 |
out->cipher_data = malloc(in->cipher->ctx_size); |
659 |
✗✓ |
78 |
if (!out->cipher_data) { |
660 |
|
|
EVPerror(ERR_R_MALLOC_FAILURE); |
661 |
|
|
return 0; |
662 |
|
|
} |
663 |
|
78 |
memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); |
664 |
|
78 |
} |
665 |
|
|
|
666 |
✗✓ |
78 |
if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) |
667 |
|
|
return in->cipher->ctrl((EVP_CIPHER_CTX *)in, |
668 |
|
|
EVP_CTRL_COPY, 0, out); |
669 |
|
78 |
return 1; |
670 |
|
78 |
} |