]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_enc.c
Handle the common case first (where input size is a multiple of block size).
[thirdparty/openssl.git] / crypto / evp / evp_enc.c
1 /* crypto/evp/evp_enc.c */
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 "cryptlib.h"
61 #include <openssl/evp.h>
62 #include <openssl/err.h>
63 #include "evp_locl.h"
64
65 #include <assert.h>
66
67 const char *EVP_version="EVP" OPENSSL_VERSION_PTEXT;
68
69 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
70 {
71 memset(ctx,0,sizeof(EVP_CIPHER_CTX));
72 /* ctx->cipher=NULL; */
73 }
74
75 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
76 unsigned char *key, unsigned char *iv, int enc)
77 {
78 if(enc && (enc != -1)) enc = 1;
79 if (cipher) {
80 ctx->cipher=cipher;
81 ctx->key_len = cipher->key_len;
82 ctx->flags = 0;
83 if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
84 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
85 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_INITIALIZATION_ERROR);
86 return 0;
87 }
88 }
89 } else if(!ctx->cipher) {
90 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_NO_CIPHER_SET);
91 return 0;
92 }
93
94 /* we assume block size is a power of 2 in *cryptUpdate */
95 assert(ctx->cipher->block_size == 1
96 || ctx->cipher->block_size == 8
97 || ctx->cipher->block_size == 16);
98
99 if(!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
100 switch(EVP_CIPHER_CTX_mode(ctx)) {
101
102 case EVP_CIPH_STREAM_CIPHER:
103 case EVP_CIPH_ECB_MODE:
104 break;
105
106 case EVP_CIPH_CFB_MODE:
107 case EVP_CIPH_OFB_MODE:
108
109 ctx->num = 0;
110
111 case EVP_CIPH_CBC_MODE:
112
113 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
114 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
115 break;
116
117 default:
118 return 0;
119 break;
120 }
121 }
122
123 if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
124 if(!ctx->cipher->init(ctx,key,iv,enc)) return 0;
125 }
126 if(enc != -1) ctx->encrypt=enc;
127 ctx->buf_len=0;
128 return 1;
129 }
130
131 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
132 unsigned char *in, int inl)
133 {
134 if (ctx->encrypt)
135 return EVP_EncryptUpdate(ctx,out,outl,in,inl);
136 else return EVP_DecryptUpdate(ctx,out,outl,in,inl);
137 }
138
139 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
140 {
141 if (ctx->encrypt)
142 return EVP_EncryptFinal(ctx,out,outl);
143 else return(EVP_DecryptFinal(ctx,out,outl));
144 }
145
146 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
147 unsigned char *key, unsigned char *iv)
148 {
149 return EVP_CipherInit(ctx, cipher, key, iv, 1);
150 }
151
152 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
153 unsigned char *key, unsigned char *iv)
154 {
155 return EVP_CipherInit(ctx, cipher, key, iv, 0);
156 }
157
158 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
159 unsigned char *in, int inl)
160 {
161 int i,j,bl;
162
163 i=ctx->buf_len;
164 bl=ctx->cipher->block_size;
165 if ((inl == 0) && (i != bl))
166 {
167 *outl=0;
168 return 1;
169 }
170 if(i == 0 && (inl&(bl-1)) == 0)
171 {
172 if(ctx->cipher->do_cipher(ctx,out,in,inl))
173 {
174 *outl=inl;
175 return 1;
176 }
177 else
178 {
179 *outl=0;
180 return 0;
181 }
182 }
183 *outl=0;
184 if (i != 0)
185 {
186 if (i+inl < bl)
187 {
188 memcpy(&(ctx->buf[i]),in,inl);
189 ctx->buf_len+=inl;
190 return 1;
191 }
192 else
193 {
194 j=bl-i;
195 if (j != 0) memcpy(&(ctx->buf[i]),in,j);
196 if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0;
197 inl-=j;
198 in+=j;
199 out+=bl;
200 *outl+=bl;
201 }
202 }
203 i=inl&(bl-1);
204 inl-=i;
205 if (inl > 0)
206 {
207 if(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0;
208 *outl+=inl;
209 }
210
211 if (i != 0)
212 memcpy(ctx->buf,&(in[inl]),i);
213 ctx->buf_len=i;
214 return 1;
215 }
216
217 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
218 {
219 int i,n,b,bl;
220
221 b=ctx->cipher->block_size;
222 if (b == 1)
223 {
224 *outl=0;
225 return 1;
226 }
227 bl=ctx->buf_len;
228 if (ctx->flags & EVP_CIPH_NO_PADDING)
229 {
230 if(bl)
231 {
232 EVPerr(EVP_F_EVP_ENCRYPTFINAL,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
233 return 0;
234 }
235 *outl = 0;
236 return 1;
237 }
238 n=b-bl;
239 for (i=bl; i<b; i++)
240 ctx->buf[i]=n;
241 if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,b)) return 0;
242 *outl=b;
243 return 1;
244 }
245
246 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
247 unsigned char *in, int inl)
248 {
249 int b,bl,n;
250 int keep_last=0;
251
252 *outl=0;
253 if (inl == 0) return 1;
254
255 if (ctx->flags & EVP_CIPH_NO_PADDING)
256 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
257
258 b=ctx->cipher->block_size;
259 if (b > 1)
260 {
261 /* Is the input a multiple of the block size? */
262 bl=ctx->buf_len;
263 n=inl+bl;
264 if (n%b == 0)
265 {
266 if (inl < b) /* must be 'just one' buff */
267 {
268 memcpy(&(ctx->buf[bl]),in,inl);
269 ctx->buf_len=b;
270 *outl=0;
271 return 1;
272 }
273 keep_last=1;
274 inl-=b; /* don't do the last block */
275 }
276 }
277 if(!EVP_EncryptUpdate(ctx,out,outl,in,inl)) return 0;
278
279 /* if we have 'decrypted' a multiple of block size, make sure
280 * we have a copy of this last block */
281 if (keep_last)
282 {
283 memcpy(&(ctx->buf[0]),&(in[inl]),b);
284 #ifdef DEBUG
285 if (ctx->buf_len != 0)
286 {
287 abort();
288 }
289 #endif
290 ctx->buf_len=b;
291 }
292 return 1;
293 }
294
295 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
296 {
297 int i,b;
298 int n;
299
300 *outl=0;
301 b=ctx->cipher->block_size;
302 if (ctx->flags & EVP_CIPH_NO_PADDING)
303 {
304 if(ctx->buf_len)
305 {
306 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
307 return 0;
308 }
309 *outl = 0;
310 return 1;
311 }
312 if (b > 1)
313 {
314 if (ctx->buf_len != b)
315 {
316 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
317 return(0);
318 }
319 if(!EVP_EncryptUpdate(ctx,ctx->buf,&n,ctx->buf,0)) return 0;
320 if (n != b)
321 return(0);
322 n=ctx->buf[b-1];
323 if (n > b)
324 {
325 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
326 return(0);
327 }
328 for (i=0; i<n; i++)
329 {
330 if (ctx->buf[--b] != n)
331 {
332 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
333 return(0);
334 }
335 }
336 n=ctx->cipher->block_size-n;
337 for (i=0; i<n; i++)
338 out[i]=ctx->buf[i];
339 *outl=n;
340 }
341 else
342 *outl=0;
343 return(1);
344 }
345
346 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
347 {
348 if ((c->cipher != NULL) && (c->cipher->cleanup != NULL))
349 {
350 if(!c->cipher->cleanup(c)) return 0;
351 }
352 memset(c,0,sizeof(EVP_CIPHER_CTX));
353 return 1;
354 }
355
356 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
357 {
358 if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
359 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
360 if(c->key_len == keylen) return 1;
361 if((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH))
362 {
363 c->key_len = keylen;
364 return 1;
365 }
366 EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH);
367 return 0;
368 }
369
370 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
371 {
372 if (pad) ctx->flags &= ~EVP_CIPH_NO_PADDING;
373 else ctx->flags |= EVP_CIPH_NO_PADDING;
374 return 1;
375 }
376
377 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
378 {
379 int ret;
380 if(!ctx->cipher) {
381 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
382 return 0;
383 }
384
385 if(!ctx->cipher->ctrl) {
386 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
387 return 0;
388 }
389
390 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
391 if(ret == -1) {
392 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
393 return 0;
394 }
395 return ret;
396 }