This commit was generated by cvs2svn to track changes on a CVS vendor
[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 "evp.h"
62
63 char *EVP_version="EVP part of SSLeay 0.9.0b 29-Jun-1998";
64
65 void EVP_CIPHER_CTX_init(ctx)
66 EVP_CIPHER_CTX *ctx;
67         {
68         memset(ctx,0,sizeof(EVP_CIPHER_CTX));
69         /* ctx->cipher=NULL; */
70         }
71
72 void EVP_CipherInit(ctx,data,key,iv,enc)
73 EVP_CIPHER_CTX *ctx;
74 EVP_CIPHER *data;
75 unsigned char *key;
76 unsigned char *iv;
77 int enc;
78         {
79         if (enc)
80                 EVP_EncryptInit(ctx,data,key,iv);
81         else    
82                 EVP_DecryptInit(ctx,data,key,iv);
83         }
84
85 void EVP_CipherUpdate(ctx,out,outl,in,inl)
86 EVP_CIPHER_CTX *ctx;
87 unsigned char *out;
88 int *outl;
89 unsigned char *in;
90 int inl;
91         {
92         if (ctx->encrypt)
93                 EVP_EncryptUpdate(ctx,out,outl,in,inl);
94         else    EVP_DecryptUpdate(ctx,out,outl,in,inl);
95         }
96
97 int EVP_CipherFinal(ctx,out,outl)
98 EVP_CIPHER_CTX *ctx;
99 unsigned char *out;
100 int *outl;
101         {
102         if (ctx->encrypt)
103                 {
104                 EVP_EncryptFinal(ctx,out,outl);
105                 return(1);
106                 }
107         else    return(EVP_DecryptFinal(ctx,out,outl));
108         }
109
110 void EVP_EncryptInit(ctx,cipher,key,iv)
111 EVP_CIPHER_CTX *ctx;
112 EVP_CIPHER *cipher;
113 unsigned char *key;
114 unsigned char *iv;
115         {
116         if (cipher != NULL)
117                 ctx->cipher=cipher;
118         ctx->cipher->init(ctx,key,iv,1);
119         ctx->encrypt=1;
120         ctx->buf_len=0;
121         }
122
123 void EVP_DecryptInit(ctx,cipher,key,iv)
124 EVP_CIPHER_CTX *ctx;
125 EVP_CIPHER *cipher;
126 unsigned char *key;
127 unsigned char *iv;
128         {
129         if (cipher != NULL)
130                 ctx->cipher=cipher;
131         ctx->cipher->init(ctx,key,iv,0);
132         ctx->encrypt=0;
133         ctx->buf_len=0;
134         }
135
136
137 void EVP_EncryptUpdate(ctx,out,outl,in,inl)
138 EVP_CIPHER_CTX *ctx;
139 unsigned char *out;
140 int *outl;
141 unsigned char *in;
142 int inl;
143         {
144         int i,j,bl;
145
146         i=ctx->buf_len;
147         bl=ctx->cipher->block_size;
148         *outl=0;
149         if ((inl == 0) && (i != bl)) return;
150         if (i != 0)
151                 {
152                 if (i+inl < bl)
153                         {
154                         memcpy(&(ctx->buf[i]),in,inl);
155                         ctx->buf_len+=inl;
156                         return;
157                         }
158                 else
159                         {
160                         j=bl-i;
161                         if (j != 0) memcpy(&(ctx->buf[i]),in,j);
162                         ctx->cipher->do_cipher(ctx,out,ctx->buf,bl);
163                         inl-=j;
164                         in+=j;
165                         out+=bl;
166                         *outl+=bl;
167                         }
168                 }
169         i=inl%bl; /* how much is left */
170         inl-=i;
171         if (inl > 0)
172                 {
173                 ctx->cipher->do_cipher(ctx,out,in,inl);
174                 *outl+=inl;
175                 }
176
177         if (i != 0)
178                 memcpy(ctx->buf,&(in[inl]),i);
179         ctx->buf_len=i;
180         }
181
182 void EVP_EncryptFinal(ctx,out,outl)
183 EVP_CIPHER_CTX *ctx;
184 unsigned char *out;
185 int *outl;
186         {
187         int i,n,b,bl;
188
189         b=ctx->cipher->block_size;
190         if (b == 1)
191                 {
192                 *outl=0;
193                 return;
194                 }
195         bl=ctx->buf_len;
196         n=b-bl;
197         for (i=bl; i<b; i++)
198                 ctx->buf[i]=n;
199         ctx->cipher->do_cipher(ctx,out,ctx->buf,b);
200         *outl=b;
201         }
202
203 void EVP_DecryptUpdate(ctx,out,outl,in,inl)
204 EVP_CIPHER_CTX *ctx;
205 unsigned char *out;
206 int *outl;
207 unsigned char *in;
208 int inl;
209         {
210         int b,bl,n;
211         int keep_last=0;
212
213         *outl=0;
214         if (inl == 0) return;
215
216         b=ctx->cipher->block_size;
217         if (b > 1)
218                 {
219                 /* Is the input a multiple of the block size? */
220                 bl=ctx->buf_len;
221                 n=inl+bl;
222                 if (n%b == 0)
223                         {
224                         if (inl < b) /* must be 'just one' buff */
225                                 {
226                                 memcpy(&(ctx->buf[bl]),in,inl);
227                                 ctx->buf_len=b;
228                                 *outl=0;
229                                 return;
230                                 }
231                         keep_last=1;
232                         inl-=b; /* don't do the last block */
233                         }
234                 }
235         EVP_EncryptUpdate(ctx,out,outl,in,inl);
236
237         /* if we have 'decrypted' a multiple of block size, make sure
238          * we have a copy of this last block */
239         if (keep_last)
240                 {
241                 memcpy(&(ctx->buf[0]),&(in[inl]),b);
242 #ifdef DEBUG
243                 if (ctx->buf_len != 0)
244                         {
245                         abort();
246                         }
247 #endif
248                 ctx->buf_len=b;
249                 }
250         }
251
252 int EVP_DecryptFinal(ctx,out,outl)
253 EVP_CIPHER_CTX *ctx;
254 unsigned char *out;
255 int *outl;
256         {
257         int i,b;
258         int n;
259
260         *outl=0;
261         b=ctx->cipher->block_size;
262         if (b > 1)
263                 {
264                 if (ctx->buf_len != b)
265                         {
266                         EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
267                         return(0);
268                         }
269                 EVP_EncryptUpdate(ctx,ctx->buf,&n,ctx->buf,0);
270                 if (n != b)
271                         return(0);
272                 n=ctx->buf[b-1];
273                 if (n > b)
274                         {
275                         EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
276                         return(0);
277                         }
278                 for (i=0; i<n; i++)
279                         {
280                         if (ctx->buf[--b] != n)
281                                 {
282                                 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
283                                 return(0);
284                                 }
285                         }
286                 n=ctx->cipher->block_size-n;
287                 for (i=0; i<n; i++)
288                         out[i]=ctx->buf[i];
289                 *outl=n;
290                 }
291         else
292                 *outl=0;
293         return(1);
294         }
295
296 void EVP_CIPHER_CTX_cleanup(c)
297 EVP_CIPHER_CTX *c;
298         {
299         if ((c->cipher != NULL) && (c->cipher->cleanup != NULL))
300                 c->cipher->cleanup(c);
301         memset(c,0,sizeof(EVP_CIPHER_CTX));
302         }
303