10Sstevel@tonic-gate /* crypto/evp/evp_enc.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/evp.h>
620Sstevel@tonic-gate #include <openssl/err.h>
63*2139Sjp161948 #include <openssl/rand.h>
640Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
650Sstevel@tonic-gate #include <openssl/engine.h>
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate #include "evp_locl.h"
680Sstevel@tonic-gate
690Sstevel@tonic-gate const char *EVP_version="EVP" OPENSSL_VERSION_PTEXT;
700Sstevel@tonic-gate
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX * ctx)710Sstevel@tonic-gate void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate memset(ctx,0,sizeof(EVP_CIPHER_CTX));
740Sstevel@tonic-gate /* ctx->cipher=NULL; */
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate
EVP_CipherInit(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv,int enc)780Sstevel@tonic-gate int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
790Sstevel@tonic-gate const unsigned char *key, const unsigned char *iv, int enc)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate if (cipher)
820Sstevel@tonic-gate EVP_CIPHER_CTX_init(ctx);
830Sstevel@tonic-gate return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
EVP_CipherInit_ex(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,ENGINE * impl,const unsigned char * key,const unsigned char * iv,int enc)860Sstevel@tonic-gate int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
870Sstevel@tonic-gate const unsigned char *key, const unsigned char *iv, int enc)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate if (enc == -1)
900Sstevel@tonic-gate enc = ctx->encrypt;
910Sstevel@tonic-gate else
920Sstevel@tonic-gate {
930Sstevel@tonic-gate if (enc)
940Sstevel@tonic-gate enc = 1;
950Sstevel@tonic-gate ctx->encrypt = enc;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
980Sstevel@tonic-gate /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
990Sstevel@tonic-gate * so this context may already have an ENGINE! Try to avoid releasing
1000Sstevel@tonic-gate * the previous handle, re-querying for an ENGINE, and having a
1010Sstevel@tonic-gate * reinitialisation, when it may all be unecessary. */
1020Sstevel@tonic-gate if (ctx->engine && ctx->cipher && (!cipher ||
1030Sstevel@tonic-gate (cipher && (cipher->nid == ctx->cipher->nid))))
1040Sstevel@tonic-gate goto skip_to_init;
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate if (cipher)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate /* Ensure a context left lying around from last time is cleared
1090Sstevel@tonic-gate * (the previous check attempted to avoid this if the same
1100Sstevel@tonic-gate * ENGINE and EVP_CIPHER could be used). */
1110Sstevel@tonic-gate EVP_CIPHER_CTX_cleanup(ctx);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /* Restore encrypt field: it is zeroed by cleanup */
1140Sstevel@tonic-gate ctx->encrypt = enc;
1150Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1160Sstevel@tonic-gate if(impl)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate if (!ENGINE_init(impl))
1190Sstevel@tonic-gate {
120*2139Sjp161948 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
1210Sstevel@tonic-gate return 0;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate else
1250Sstevel@tonic-gate /* Ask if an ENGINE is reserved for this job */
1260Sstevel@tonic-gate impl = ENGINE_get_cipher_engine(cipher->nid);
1270Sstevel@tonic-gate if(impl)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate /* There's an ENGINE for this job ... (apparently) */
1300Sstevel@tonic-gate const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
1310Sstevel@tonic-gate if(!c)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate /* One positive side-effect of US's export
1340Sstevel@tonic-gate * control history, is that we should at least
1350Sstevel@tonic-gate * be able to avoid using US mispellings of
1360Sstevel@tonic-gate * "initialisation"? */
137*2139Sjp161948 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
1380Sstevel@tonic-gate return 0;
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate /* We'll use the ENGINE's private cipher definition */
1410Sstevel@tonic-gate cipher = c;
1420Sstevel@tonic-gate /* Store the ENGINE functional reference so we know
1430Sstevel@tonic-gate * 'cipher' came from an ENGINE and we need to release
1440Sstevel@tonic-gate * it when done. */
1450Sstevel@tonic-gate ctx->engine = impl;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate else
1480Sstevel@tonic-gate ctx->engine = NULL;
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate ctx->cipher=cipher;
1520Sstevel@tonic-gate if (ctx->cipher->ctx_size)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
1550Sstevel@tonic-gate if (!ctx->cipher_data)
1560Sstevel@tonic-gate {
157*2139Sjp161948 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
1580Sstevel@tonic-gate return 0;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate ctx->cipher_data = NULL;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate ctx->key_len = cipher->key_len;
1660Sstevel@tonic-gate ctx->flags = 0;
1670Sstevel@tonic-gate if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL))
1700Sstevel@tonic-gate {
171*2139Sjp161948 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
1720Sstevel@tonic-gate return 0;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate else if(!ctx->cipher)
1770Sstevel@tonic-gate {
178*2139Sjp161948 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
1790Sstevel@tonic-gate return 0;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1820Sstevel@tonic-gate skip_to_init:
1830Sstevel@tonic-gate #endif
1840Sstevel@tonic-gate /* we assume block size is a power of 2 in *cryptUpdate */
1850Sstevel@tonic-gate OPENSSL_assert(ctx->cipher->block_size == 1
1860Sstevel@tonic-gate || ctx->cipher->block_size == 8
1870Sstevel@tonic-gate || ctx->cipher->block_size == 16);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if(!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
1900Sstevel@tonic-gate switch(EVP_CIPHER_CTX_mode(ctx)) {
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate case EVP_CIPH_STREAM_CIPHER:
1930Sstevel@tonic-gate case EVP_CIPH_ECB_MODE:
1940Sstevel@tonic-gate break;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate case EVP_CIPH_CFB_MODE:
1970Sstevel@tonic-gate case EVP_CIPH_OFB_MODE:
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate ctx->num = 0;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate case EVP_CIPH_CBC_MODE:
2020Sstevel@tonic-gate
203*2139Sjp161948 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
204*2139Sjp161948 (int)sizeof(ctx->iv));
2050Sstevel@tonic-gate if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
2060Sstevel@tonic-gate memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
2070Sstevel@tonic-gate break;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate default:
2100Sstevel@tonic-gate return 0;
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
2160Sstevel@tonic-gate if(!ctx->cipher->init(ctx,key,iv,enc)) return 0;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate ctx->buf_len=0;
2190Sstevel@tonic-gate ctx->final_used=0;
2200Sstevel@tonic-gate ctx->block_mask=ctx->cipher->block_size-1;
2210Sstevel@tonic-gate return 1;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
EVP_CipherUpdate(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl,const unsigned char * in,int inl)2240Sstevel@tonic-gate int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
2250Sstevel@tonic-gate const unsigned char *in, int inl)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate if (ctx->encrypt)
2280Sstevel@tonic-gate return EVP_EncryptUpdate(ctx,out,outl,in,inl);
2290Sstevel@tonic-gate else return EVP_DecryptUpdate(ctx,out,outl,in,inl);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
EVP_CipherFinal_ex(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)2320Sstevel@tonic-gate int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate if (ctx->encrypt)
2350Sstevel@tonic-gate return EVP_EncryptFinal_ex(ctx,out,outl);
2360Sstevel@tonic-gate else return EVP_DecryptFinal_ex(ctx,out,outl);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
EVP_CipherFinal(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)2390Sstevel@tonic-gate int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate if (ctx->encrypt)
2420Sstevel@tonic-gate return EVP_EncryptFinal(ctx,out,outl);
2430Sstevel@tonic-gate else return EVP_DecryptFinal(ctx,out,outl);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
EVP_EncryptInit(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv)2460Sstevel@tonic-gate int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
2470Sstevel@tonic-gate const unsigned char *key, const unsigned char *iv)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate return EVP_CipherInit(ctx, cipher, key, iv, 1);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
EVP_EncryptInit_ex(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,ENGINE * impl,const unsigned char * key,const unsigned char * iv)2520Sstevel@tonic-gate int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl,
2530Sstevel@tonic-gate const unsigned char *key, const unsigned char *iv)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
EVP_DecryptInit(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv)2580Sstevel@tonic-gate int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
2590Sstevel@tonic-gate const unsigned char *key, const unsigned char *iv)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate return EVP_CipherInit(ctx, cipher, key, iv, 0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
EVP_DecryptInit_ex(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,ENGINE * impl,const unsigned char * key,const unsigned char * iv)2640Sstevel@tonic-gate int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
2650Sstevel@tonic-gate const unsigned char *key, const unsigned char *iv)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
EVP_EncryptUpdate(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl,const unsigned char * in,int inl)2700Sstevel@tonic-gate int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
2710Sstevel@tonic-gate const unsigned char *in, int inl)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate int i,j,bl;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate OPENSSL_assert(inl > 0);
2760Sstevel@tonic-gate if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate if(ctx->cipher->do_cipher(ctx,out,in,inl))
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate *outl=inl;
2810Sstevel@tonic-gate return 1;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate else
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate *outl=0;
2860Sstevel@tonic-gate return 0;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate i=ctx->buf_len;
2900Sstevel@tonic-gate bl=ctx->cipher->block_size;
291*2139Sjp161948 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
2920Sstevel@tonic-gate if (i != 0)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate if (i+inl < bl)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate memcpy(&(ctx->buf[i]),in,inl);
2970Sstevel@tonic-gate ctx->buf_len+=inl;
2980Sstevel@tonic-gate *outl=0;
2990Sstevel@tonic-gate return 1;
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate else
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate j=bl-i;
3040Sstevel@tonic-gate memcpy(&(ctx->buf[i]),in,j);
3050Sstevel@tonic-gate if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0;
3060Sstevel@tonic-gate inl-=j;
3070Sstevel@tonic-gate in+=j;
3080Sstevel@tonic-gate out+=bl;
3090Sstevel@tonic-gate *outl=bl;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate else
3130Sstevel@tonic-gate *outl = 0;
3140Sstevel@tonic-gate i=inl&(bl-1);
3150Sstevel@tonic-gate inl-=i;
3160Sstevel@tonic-gate if (inl > 0)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate if(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0;
3190Sstevel@tonic-gate *outl+=inl;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate if (i != 0)
3230Sstevel@tonic-gate memcpy(ctx->buf,&(in[inl]),i);
3240Sstevel@tonic-gate ctx->buf_len=i;
3250Sstevel@tonic-gate return 1;
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
EVP_EncryptFinal(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)3280Sstevel@tonic-gate int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate int ret;
3310Sstevel@tonic-gate ret = EVP_EncryptFinal_ex(ctx, out, outl);
3320Sstevel@tonic-gate return ret;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
EVP_EncryptFinal_ex(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)3350Sstevel@tonic-gate int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
3360Sstevel@tonic-gate {
337*2139Sjp161948 int n,ret;
338*2139Sjp161948 unsigned int i, b, bl;
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate b=ctx->cipher->block_size;
3410Sstevel@tonic-gate OPENSSL_assert(b <= sizeof ctx->buf);
3420Sstevel@tonic-gate if (b == 1)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate *outl=0;
3450Sstevel@tonic-gate return 1;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate bl=ctx->buf_len;
3480Sstevel@tonic-gate if (ctx->flags & EVP_CIPH_NO_PADDING)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate if(bl)
3510Sstevel@tonic-gate {
352*2139Sjp161948 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
3530Sstevel@tonic-gate return 0;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate *outl = 0;
3560Sstevel@tonic-gate return 1;
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate n=b-bl;
3600Sstevel@tonic-gate for (i=bl; i<b; i++)
3610Sstevel@tonic-gate ctx->buf[i]=n;
3620Sstevel@tonic-gate ret=ctx->cipher->do_cipher(ctx,out,ctx->buf,b);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate if(ret)
3660Sstevel@tonic-gate *outl=b;
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate return ret;
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate
EVP_DecryptUpdate(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl,const unsigned char * in,int inl)3710Sstevel@tonic-gate int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
3720Sstevel@tonic-gate const unsigned char *in, int inl)
3730Sstevel@tonic-gate {
374*2139Sjp161948 int fix_len;
375*2139Sjp161948 unsigned int b;
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate if (inl == 0)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate *outl=0;
3800Sstevel@tonic-gate return 1;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate if (ctx->flags & EVP_CIPH_NO_PADDING)
3840Sstevel@tonic-gate return EVP_EncryptUpdate(ctx, out, outl, in, inl);
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate b=ctx->cipher->block_size;
3870Sstevel@tonic-gate OPENSSL_assert(b <= sizeof ctx->final);
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate if(ctx->final_used)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate memcpy(out,ctx->final,b);
3920Sstevel@tonic-gate out+=b;
3930Sstevel@tonic-gate fix_len = 1;
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate else
3960Sstevel@tonic-gate fix_len = 0;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate if(!EVP_EncryptUpdate(ctx,out,outl,in,inl))
4000Sstevel@tonic-gate return 0;
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate /* if we have 'decrypted' a multiple of block size, make sure
4030Sstevel@tonic-gate * we have a copy of this last block */
4040Sstevel@tonic-gate if (b > 1 && !ctx->buf_len)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate *outl-=b;
4070Sstevel@tonic-gate ctx->final_used=1;
4080Sstevel@tonic-gate memcpy(ctx->final,&out[*outl],b);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate else
4110Sstevel@tonic-gate ctx->final_used = 0;
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate if (fix_len)
4140Sstevel@tonic-gate *outl += b;
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate return 1;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
EVP_DecryptFinal(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)4190Sstevel@tonic-gate int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate int ret;
4220Sstevel@tonic-gate ret = EVP_DecryptFinal_ex(ctx, out, outl);
4230Sstevel@tonic-gate return ret;
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate
EVP_DecryptFinal_ex(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)4260Sstevel@tonic-gate int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
4270Sstevel@tonic-gate {
428*2139Sjp161948 int i,n;
429*2139Sjp161948 unsigned int b;
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate *outl=0;
4320Sstevel@tonic-gate b=ctx->cipher->block_size;
4330Sstevel@tonic-gate if (ctx->flags & EVP_CIPH_NO_PADDING)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate if(ctx->buf_len)
4360Sstevel@tonic-gate {
437*2139Sjp161948 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
4380Sstevel@tonic-gate return 0;
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate *outl = 0;
4410Sstevel@tonic-gate return 1;
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate if (b > 1)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate if (ctx->buf_len || !ctx->final_used)
4460Sstevel@tonic-gate {
447*2139Sjp161948 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
4480Sstevel@tonic-gate return(0);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate OPENSSL_assert(b <= sizeof ctx->final);
4510Sstevel@tonic-gate n=ctx->final[b-1];
452*2139Sjp161948 if (n == 0 || n > (int)b)
4530Sstevel@tonic-gate {
454*2139Sjp161948 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_BAD_DECRYPT);
4550Sstevel@tonic-gate return(0);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate for (i=0; i<n; i++)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate if (ctx->final[--b] != n)
4600Sstevel@tonic-gate {
461*2139Sjp161948 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_BAD_DECRYPT);
4620Sstevel@tonic-gate return(0);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate n=ctx->cipher->block_size-n;
4660Sstevel@tonic-gate for (i=0; i<n; i++)
4670Sstevel@tonic-gate out[i]=ctx->final[i];
4680Sstevel@tonic-gate *outl=n;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate else
4710Sstevel@tonic-gate *outl=0;
4720Sstevel@tonic-gate return(1);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX * c)4750Sstevel@tonic-gate int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate if (c->cipher != NULL)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate if(c->cipher->cleanup && !c->cipher->cleanup(c))
4800Sstevel@tonic-gate return 0;
4810Sstevel@tonic-gate /* Cleanse cipher context data */
4820Sstevel@tonic-gate if (c->cipher_data)
4830Sstevel@tonic-gate OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate if (c->cipher_data)
4860Sstevel@tonic-gate OPENSSL_free(c->cipher_data);
4870Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
4880Sstevel@tonic-gate if (c->engine)
4890Sstevel@tonic-gate /* The EVP_CIPHER we used belongs to an ENGINE, release the
4900Sstevel@tonic-gate * functional reference we held for this reason. */
4910Sstevel@tonic-gate ENGINE_finish(c->engine);
4920Sstevel@tonic-gate #endif
4930Sstevel@tonic-gate memset(c,0,sizeof(EVP_CIPHER_CTX));
4940Sstevel@tonic-gate return 1;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX * c,int keylen)4970Sstevel@tonic-gate int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
5000Sstevel@tonic-gate return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
5010Sstevel@tonic-gate if(c->key_len == keylen) return 1;
5020Sstevel@tonic-gate if((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH))
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate c->key_len = keylen;
5050Sstevel@tonic-gate return 1;
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH);
5080Sstevel@tonic-gate return 0;
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX * ctx,int pad)5110Sstevel@tonic-gate int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate if (pad) ctx->flags &= ~EVP_CIPH_NO_PADDING;
5140Sstevel@tonic-gate else ctx->flags |= EVP_CIPH_NO_PADDING;
5150Sstevel@tonic-gate return 1;
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX * ctx,int type,int arg,void * ptr)5180Sstevel@tonic-gate int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate int ret;
5210Sstevel@tonic-gate if(!ctx->cipher) {
5220Sstevel@tonic-gate EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
5230Sstevel@tonic-gate return 0;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate if(!ctx->cipher->ctrl) {
5270Sstevel@tonic-gate EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
5280Sstevel@tonic-gate return 0;
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
5320Sstevel@tonic-gate if(ret == -1) {
5330Sstevel@tonic-gate EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
5340Sstevel@tonic-gate return 0;
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate return ret;
5370Sstevel@tonic-gate }
538*2139Sjp161948
EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX * ctx,unsigned char * key)539*2139Sjp161948 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
540*2139Sjp161948 {
541*2139Sjp161948 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
542*2139Sjp161948 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
543*2139Sjp161948 if (RAND_bytes(key, ctx->key_len) <= 0)
544*2139Sjp161948 return 0;
545*2139Sjp161948 return 1;
546*2139Sjp161948 }
547*2139Sjp161948
548