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