]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - crypto/openssl/crypto/evp/evp_enc.c
Merge OpenSSL 0.9.8zc.
[FreeBSD/stable/9.git] / crypto / openssl / 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 <openssl/rand.h>
64 #ifndef OPENSSL_NO_ENGINE
65 #include <openssl/engine.h>
66 #endif
67 #include "../constant_time_locl.h"
68 #include "evp_locl.h"
69
70 #ifdef OPENSSL_FIPS
71         #define M_do_cipher(ctx, out, in, inl) \
72                 EVP_Cipher(ctx,out,in,inl)
73 #else
74         #define M_do_cipher(ctx, out, in, inl) \
75                 ctx->cipher->do_cipher(ctx,out,in,inl)
76 #endif
77
78 const char EVP_version[]="EVP" OPENSSL_VERSION_PTEXT;
79
80 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
81         {
82         EVP_CIPHER_CTX *ctx=OPENSSL_malloc(sizeof *ctx);
83         if (ctx)
84                 EVP_CIPHER_CTX_init(ctx);
85         return ctx;
86         }
87
88 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
89              const unsigned char *key, const unsigned char *iv, int enc)
90         {
91         if (cipher)
92                 EVP_CIPHER_CTX_init(ctx);
93         return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc);
94         }
95
96 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
97              const unsigned char *in, int inl)
98         {
99         if (ctx->encrypt)
100                 return EVP_EncryptUpdate(ctx,out,outl,in,inl);
101         else    return EVP_DecryptUpdate(ctx,out,outl,in,inl);
102         }
103
104 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
105         {
106         if (ctx->encrypt)
107                 return EVP_EncryptFinal_ex(ctx,out,outl);
108         else    return EVP_DecryptFinal_ex(ctx,out,outl);
109         }
110
111 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
112         {
113         if (ctx->encrypt)
114                 return EVP_EncryptFinal(ctx,out,outl);
115         else    return EVP_DecryptFinal(ctx,out,outl);
116         }
117
118 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
119              const unsigned char *key, const unsigned char *iv)
120         {
121         return EVP_CipherInit(ctx, cipher, key, iv, 1);
122         }
123
124 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl,
125                 const unsigned char *key, const unsigned char *iv)
126         {
127         return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
128         }
129
130 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
131              const unsigned char *key, const unsigned char *iv)
132         {
133         return EVP_CipherInit(ctx, cipher, key, iv, 0);
134         }
135
136 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
137              const unsigned char *key, const unsigned char *iv)
138         {
139         return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
140         }
141
142 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
143              const unsigned char *in, int inl)
144         {
145         int i,j,bl;
146
147         if (inl <= 0)
148                 {
149                 *outl = 0;
150                 return inl == 0;
151                 }
152
153         if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0)
154                 {
155                 if(M_do_cipher(ctx,out,in,inl))
156                         {
157                         *outl=inl;
158                         return 1;
159                         }
160                 else
161                         {
162                         *outl=0;
163                         return 0;
164                         }
165                 }
166         i=ctx->buf_len;
167         bl=ctx->cipher->block_size;
168         OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
169         if (i != 0)
170                 {
171                 if (i+inl < bl)
172                         {
173                         memcpy(&(ctx->buf[i]),in,inl);
174                         ctx->buf_len+=inl;
175                         *outl=0;
176                         return 1;
177                         }
178                 else
179                         {
180                         j=bl-i;
181                         memcpy(&(ctx->buf[i]),in,j);
182                         if(!M_do_cipher(ctx,out,ctx->buf,bl)) return 0;
183                         inl-=j;
184                         in+=j;
185                         out+=bl;
186                         *outl=bl;
187                         }
188                 }
189         else
190                 *outl = 0;
191         i=inl&(bl-1);
192         inl-=i;
193         if (inl > 0)
194                 {
195                 if(!M_do_cipher(ctx,out,in,inl)) return 0;
196                 *outl+=inl;
197                 }
198
199         if (i != 0)
200                 memcpy(ctx->buf,&(in[inl]),i);
201         ctx->buf_len=i;
202         return 1;
203         }
204
205 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
206         {
207         int ret;
208         ret = EVP_EncryptFinal_ex(ctx, out, outl);
209         return ret;
210         }
211
212 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
213         {
214         int n,ret;
215         unsigned int i, b, bl;
216
217         b=ctx->cipher->block_size;
218         OPENSSL_assert(b <= sizeof ctx->buf);
219         if (b == 1)
220                 {
221                 *outl=0;
222                 return 1;
223                 }
224         bl=ctx->buf_len;
225         if (ctx->flags & EVP_CIPH_NO_PADDING)
226                 {
227                 if(bl)
228                         {
229                         EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
230                         return 0;
231                         }
232                 *outl = 0;
233                 return 1;
234                 }
235
236         n=b-bl;
237         for (i=bl; i<b; i++)
238                 ctx->buf[i]=n;
239         ret=M_do_cipher(ctx,out,ctx->buf,b);
240
241
242         if(ret)
243                 *outl=b;
244
245         return ret;
246         }
247
248 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
249              const unsigned char *in, int inl)
250         {
251         int fix_len;
252         unsigned int b;
253
254         if (inl <= 0)
255                 {
256                 *outl = 0;
257                 return inl == 0;
258                 }
259
260         if (ctx->flags & EVP_CIPH_NO_PADDING)
261                 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
262
263         b=ctx->cipher->block_size;
264         OPENSSL_assert(b <= sizeof ctx->final);
265
266         if(ctx->final_used)
267                 {
268                 memcpy(out,ctx->final,b);
269                 out+=b;
270                 fix_len = 1;
271                 }
272         else
273                 fix_len = 0;
274
275
276         if(!EVP_EncryptUpdate(ctx,out,outl,in,inl))
277                 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 (b > 1 && !ctx->buf_len)
282                 {
283                 *outl-=b;
284                 ctx->final_used=1;
285                 memcpy(ctx->final,&out[*outl],b);
286                 }
287         else
288                 ctx->final_used = 0;
289
290         if (fix_len)
291                 *outl += b;
292                 
293         return 1;
294         }
295
296 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
297         {
298         int ret;
299         ret = EVP_DecryptFinal_ex(ctx, out, outl);
300         return ret;
301         }
302
303 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
304         {
305         unsigned int i, b;
306         unsigned char pad, padding_good;
307
308         *outl=0;
309         b=(unsigned int)(ctx->cipher->block_size);
310         if (ctx->flags & EVP_CIPH_NO_PADDING)
311                 {
312                 if(ctx->buf_len)
313                         {
314                         EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
315                         return 0;
316                         }
317                 *outl = 0;
318                 return 1;
319                 }
320         if (b > 1)
321                 {
322                 if (ctx->buf_len || !ctx->final_used)
323                         {
324                         EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
325                         return(0);
326                         }
327                 OPENSSL_assert(b <= sizeof ctx->final);
328                 pad=ctx->final[b-1];
329
330                 padding_good = (unsigned char)(~constant_time_is_zero_8(pad));
331                 padding_good &= constant_time_ge_8(b, pad);
332
333                 for (i = 1; i < b; ++i)
334                         {
335                         unsigned char is_pad_index = constant_time_lt_8(i, pad);
336                         unsigned char pad_byte_good = constant_time_eq_8(ctx->final[b-i-1], pad);
337                         padding_good &= constant_time_select_8(is_pad_index, pad_byte_good, 0xff);
338                         }
339
340                 /*
341                  * At least 1 byte is always padding, so we always write b - 1
342                  * bytes to avoid a timing leak. The caller is required to have |b|
343                  * bytes space in |out| by the API contract.
344                  */
345                 for (i = 0; i < b - 1; ++i)
346                         out[i] = ctx->final[i] & padding_good;
347                 /* Safe cast: for a good padding, EVP_MAX_IV_LENGTH >= b >= pad */
348                 *outl = padding_good & ((unsigned char)(b - pad));
349                 return padding_good & 1;
350                 }
351         else
352                 {
353                 *outl = 0;
354                 return 1;
355                 }
356         }
357
358 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
359         {
360         if (ctx)
361                 {
362                 EVP_CIPHER_CTX_cleanup(ctx);
363                 OPENSSL_free(ctx);
364                 }
365         }
366
367 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
368         {
369         if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) 
370                 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
371         if(c->key_len == keylen) return 1;
372         if((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH))
373                 {
374                 c->key_len = keylen;
375                 return 1;
376                 }
377         EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH);
378         return 0;
379         }
380
381 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
382         {
383         if (pad) ctx->flags &= ~EVP_CIPH_NO_PADDING;
384         else ctx->flags |= EVP_CIPH_NO_PADDING;
385         return 1;
386         }
387
388 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
389         {
390         if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
391                 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
392         if (RAND_bytes(key, ctx->key_len) <= 0)
393                 return 0;
394         return 1;
395         }
396
397 #ifndef OPENSSL_NO_ENGINE
398
399 #ifdef OPENSSL_FIPS
400
401 static int do_evp_enc_engine_full(EVP_CIPHER_CTX *ctx, const EVP_CIPHER **pcipher, ENGINE *impl)
402         {
403         if(impl)
404                 {
405                 if (!ENGINE_init(impl))
406                         {
407                         EVPerr(EVP_F_DO_EVP_ENC_ENGINE_FULL, EVP_R_INITIALIZATION_ERROR);
408                         return 0;
409                         }
410                 }
411         else
412                 /* Ask if an ENGINE is reserved for this job */
413                 impl = ENGINE_get_cipher_engine((*pcipher)->nid);
414         if(impl)
415                 {
416                 /* There's an ENGINE for this job ... (apparently) */
417                 const EVP_CIPHER *c = ENGINE_get_cipher(impl, (*pcipher)->nid);
418                 if(!c)
419                         {
420                         /* One positive side-effect of US's export
421                          * control history, is that we should at least
422                          * be able to avoid using US mispellings of
423                          * "initialisation"? */
424                         EVPerr(EVP_F_DO_EVP_ENC_ENGINE_FULL, EVP_R_INITIALIZATION_ERROR);
425                         return 0;
426                         }
427                 /* We'll use the ENGINE's private cipher definition */
428                 *pcipher = c;
429                 /* Store the ENGINE functional reference so we know
430                  * 'cipher' came from an ENGINE and we need to release
431                  * it when done. */
432                 ctx->engine = impl;
433                 }
434         else
435                 ctx->engine = NULL;
436         return 1;
437         }
438
439 void int_EVP_CIPHER_init_engine_callbacks(void)
440         {
441         int_EVP_CIPHER_set_engine_callbacks(
442                 ENGINE_finish, do_evp_enc_engine_full);
443         }
444
445 #endif
446
447 #endif