sl@0: /* crypto/evp/bio_enc.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: static int enc_write(BIO *h, const char *buf, int num); sl@0: static int enc_read(BIO *h, char *buf, int size); sl@0: /*static int enc_puts(BIO *h, const char *str); */ sl@0: /*static int enc_gets(BIO *h, char *str, int size); */ sl@0: static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2); sl@0: static int enc_new(BIO *h); sl@0: static int enc_free(BIO *data); sl@0: static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps); sl@0: #define ENC_BLOCK_SIZE (1024*4) sl@0: #define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2) sl@0: sl@0: typedef struct enc_struct sl@0: { sl@0: int buf_len; sl@0: int buf_off; sl@0: int cont; /* <= 0 when finished */ sl@0: int finished; sl@0: int ok; /* bad decrypt */ sl@0: EVP_CIPHER_CTX cipher; sl@0: /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate sl@0: * can return up to a block more data than is presented to it sl@0: */ sl@0: char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2]; sl@0: } BIO_ENC_CTX; sl@0: sl@0: #ifndef EMULATOR sl@0: static BIO_METHOD methods_enc= sl@0: { sl@0: BIO_TYPE_CIPHER,"cipher", sl@0: enc_write, sl@0: enc_read, sl@0: NULL, /* enc_puts, */ sl@0: NULL, /* enc_gets, */ sl@0: enc_ctrl, sl@0: enc_new, sl@0: enc_free, sl@0: enc_callback_ctrl, sl@0: }; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(methods_enc,bio_enc,BIO_METHOD) sl@0: #define methods_enc (*GET_WSD_VAR_NAME(methods_enc,bio_enc, s)()) sl@0: const BIO_METHOD temp_s_methods_enc= sl@0: { sl@0: BIO_TYPE_CIPHER,"cipher", sl@0: enc_write, sl@0: enc_read, sl@0: NULL, /* enc_puts, */ sl@0: NULL, /* enc_gets, */ sl@0: enc_ctrl, sl@0: enc_new, sl@0: enc_free, sl@0: enc_callback_ctrl, sl@0: }; sl@0: sl@0: #endif sl@0: sl@0: EXPORT_C BIO_METHOD *BIO_f_cipher(void) sl@0: { sl@0: return(&methods_enc); sl@0: } sl@0: sl@0: static int enc_new(BIO *bi) sl@0: { sl@0: BIO_ENC_CTX *ctx; sl@0: sl@0: ctx=(BIO_ENC_CTX *)OPENSSL_malloc(sizeof(BIO_ENC_CTX)); sl@0: if (ctx == NULL) return(0); sl@0: EVP_CIPHER_CTX_init(&ctx->cipher); sl@0: sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: ctx->cont=1; sl@0: ctx->finished=0; sl@0: ctx->ok=1; sl@0: sl@0: bi->init=0; sl@0: bi->ptr=(char *)ctx; sl@0: bi->flags=0; sl@0: return(1); sl@0: } sl@0: sl@0: static int enc_free(BIO *a) sl@0: { sl@0: BIO_ENC_CTX *b; sl@0: sl@0: if (a == NULL) return(0); sl@0: b=(BIO_ENC_CTX *)a->ptr; sl@0: EVP_CIPHER_CTX_cleanup(&(b->cipher)); sl@0: OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX)); sl@0: OPENSSL_free(a->ptr); sl@0: a->ptr=NULL; sl@0: a->init=0; sl@0: a->flags=0; sl@0: return(1); sl@0: } sl@0: sl@0: static int enc_read(BIO *b, char *out, int outl) sl@0: { sl@0: int ret=0,i; sl@0: BIO_ENC_CTX *ctx; sl@0: sl@0: if (out == NULL) return(0); sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: sl@0: if ((ctx == NULL) || (b->next_bio == NULL)) return(0); sl@0: sl@0: /* First check if there are bytes decoded/encoded */ sl@0: if (ctx->buf_len > 0) sl@0: { sl@0: i=ctx->buf_len-ctx->buf_off; sl@0: if (i > outl) i=outl; sl@0: memcpy(out,&(ctx->buf[ctx->buf_off]),i); sl@0: ret=i; sl@0: out+=i; sl@0: outl-=i; sl@0: ctx->buf_off+=i; sl@0: if (ctx->buf_len == ctx->buf_off) sl@0: { sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: } sl@0: } sl@0: sl@0: /* At this point, we have room of outl bytes and an empty sl@0: * buffer, so we should read in some more. */ sl@0: sl@0: while (outl > 0) sl@0: { sl@0: if (ctx->cont <= 0) break; sl@0: sl@0: /* read in at IV offset, read the EVP_Cipher sl@0: * documentation about why */ sl@0: i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE); sl@0: sl@0: if (i <= 0) sl@0: { sl@0: /* Should be continue next time we are called? */ sl@0: if (!BIO_should_retry(b->next_bio)) sl@0: { sl@0: ctx->cont=i; sl@0: i=EVP_CipherFinal_ex(&(ctx->cipher), sl@0: (unsigned char *)ctx->buf, sl@0: &(ctx->buf_len)); sl@0: ctx->ok=i; sl@0: ctx->buf_off=0; sl@0: } sl@0: else sl@0: { sl@0: ret=(ret == 0)?i:ret; sl@0: break; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: EVP_CipherUpdate(&(ctx->cipher), sl@0: (unsigned char *)ctx->buf,&ctx->buf_len, sl@0: (unsigned char *)&(ctx->buf[BUF_OFFSET]),i); sl@0: ctx->cont=1; sl@0: /* Note: it is possible for EVP_CipherUpdate to sl@0: * decrypt zero bytes because this is or looks like sl@0: * the final block: if this happens we should retry sl@0: * and either read more data or decrypt the final sl@0: * block sl@0: */ sl@0: if(ctx->buf_len == 0) continue; sl@0: } sl@0: sl@0: if (ctx->buf_len <= outl) sl@0: i=ctx->buf_len; sl@0: else sl@0: i=outl; sl@0: if (i <= 0) break; sl@0: memcpy(out,ctx->buf,i); sl@0: ret+=i; sl@0: ctx->buf_off=i; sl@0: outl-=i; sl@0: out+=i; sl@0: } sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: BIO_copy_next_retry(b); sl@0: return((ret == 0)?ctx->cont:ret); sl@0: } sl@0: sl@0: static int enc_write(BIO *b, const char *in, int inl) sl@0: { sl@0: int ret=0,n,i; sl@0: BIO_ENC_CTX *ctx; sl@0: sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: ret=inl; sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: n=ctx->buf_len-ctx->buf_off; sl@0: while (n > 0) sl@0: { sl@0: i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return(i); sl@0: } sl@0: ctx->buf_off+=i; sl@0: n-=i; sl@0: } sl@0: /* at this point all pending data has been written */ sl@0: sl@0: if ((in == NULL) || (inl <= 0)) return(0); sl@0: sl@0: ctx->buf_off=0; sl@0: while (inl > 0) sl@0: { sl@0: n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl; sl@0: EVP_CipherUpdate(&(ctx->cipher), sl@0: (unsigned char *)ctx->buf,&ctx->buf_len, sl@0: (unsigned char *)in,n); sl@0: inl-=n; sl@0: in+=n; sl@0: sl@0: ctx->buf_off=0; sl@0: n=ctx->buf_len; sl@0: while (n > 0) sl@0: { sl@0: i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return (ret == inl) ? i : ret - inl; sl@0: } sl@0: n-=i; sl@0: ctx->buf_off+=i; sl@0: } sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: } sl@0: BIO_copy_next_retry(b); sl@0: return(ret); sl@0: } sl@0: sl@0: static long enc_ctrl(BIO *b, int cmd, long num, void *ptr) sl@0: { sl@0: BIO *dbio; sl@0: BIO_ENC_CTX *ctx,*dctx; sl@0: long ret=1; sl@0: int i; sl@0: EVP_CIPHER_CTX **c_ctx; sl@0: sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case BIO_CTRL_RESET: sl@0: ctx->ok=1; sl@0: ctx->finished=0; sl@0: EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, sl@0: ctx->cipher.encrypt); sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_EOF: /* More to read */ sl@0: if (ctx->cont <= 0) sl@0: ret=1; sl@0: else sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_WPENDING: sl@0: ret=ctx->buf_len-ctx->buf_off; sl@0: if (ret <= 0) sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_PENDING: /* More to read in buffer */ sl@0: ret=ctx->buf_len-ctx->buf_off; sl@0: if (ret <= 0) sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_FLUSH: sl@0: /* do a final write */ sl@0: again: sl@0: while (ctx->buf_len != ctx->buf_off) sl@0: { sl@0: i=enc_write(b,NULL,0); sl@0: if (i < 0) sl@0: return i; sl@0: } sl@0: sl@0: if (!ctx->finished) sl@0: { sl@0: ctx->finished=1; sl@0: ctx->buf_off=0; sl@0: ret=EVP_CipherFinal_ex(&(ctx->cipher), sl@0: (unsigned char *)ctx->buf, sl@0: &(ctx->buf_len)); sl@0: ctx->ok=(int)ret; sl@0: if (ret <= 0) break; sl@0: sl@0: /* push out the bytes */ sl@0: goto again; sl@0: } sl@0: sl@0: /* Finally flush the underlying BIO */ sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_C_GET_CIPHER_STATUS: sl@0: ret=(long)ctx->ok; sl@0: break; sl@0: case BIO_C_DO_STATE_MACHINE: sl@0: BIO_clear_retry_flags(b); sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: BIO_copy_next_retry(b); sl@0: break; sl@0: case BIO_C_GET_CIPHER_CTX: sl@0: c_ctx=(EVP_CIPHER_CTX **)ptr; sl@0: (*c_ctx)= &(ctx->cipher); sl@0: b->init=1; sl@0: break; sl@0: case BIO_CTRL_DUP: sl@0: dbio=(BIO *)ptr; sl@0: dctx=(BIO_ENC_CTX *)dbio->ptr; sl@0: memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher)); sl@0: dbio->init=1; sl@0: break; sl@0: default: sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) sl@0: { sl@0: long ret=1; sl@0: sl@0: if (b->next_bio == NULL) return(0); sl@0: switch (cmd) sl@0: { sl@0: default: sl@0: ret=BIO_callback_ctrl(b->next_bio,cmd,fp); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: /* sl@0: void BIO_set_cipher_ctx(b,c) sl@0: BIO *b; sl@0: EVP_CIPHER_ctx *c; sl@0: { sl@0: if (b == NULL) return; sl@0: sl@0: if ((b->callback != NULL) && sl@0: (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) sl@0: return; sl@0: sl@0: b->init=1; sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); sl@0: sl@0: if (b->callback != NULL) sl@0: b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); sl@0: } sl@0: */ sl@0: sl@0: EXPORT_C void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, sl@0: const unsigned char *i, int e) sl@0: { sl@0: BIO_ENC_CTX *ctx; sl@0: sl@0: if (b == NULL) return; sl@0: sl@0: if ((b->callback != NULL) && sl@0: (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0)) sl@0: return; sl@0: sl@0: b->init=1; sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e); sl@0: sl@0: if (b->callback != NULL) sl@0: b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L); sl@0: } sl@0: