sl@0: /* crypto/evp/evp_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: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: #include sl@0: #endif sl@0: #include "evp_locl.h" sl@0: sl@0: const char EVP_version[]="EVP" OPENSSL_VERSION_PTEXT; sl@0: sl@0: EXPORT_C void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) sl@0: { sl@0: memset(ctx,0,sizeof(EVP_CIPHER_CTX)); sl@0: /* ctx->cipher=NULL; */ sl@0: } sl@0: sl@0: EXPORT_C EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) sl@0: { sl@0: EVP_CIPHER_CTX *ctx=OPENSSL_malloc(sizeof *ctx); sl@0: if (ctx) sl@0: EVP_CIPHER_CTX_init(ctx); sl@0: return ctx; sl@0: } sl@0: sl@0: EXPORT_C int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, sl@0: const unsigned char *key, const unsigned char *iv, int enc) sl@0: { sl@0: if (cipher) sl@0: EVP_CIPHER_CTX_init(ctx); sl@0: return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc); sl@0: } sl@0: sl@0: EXPORT_C int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, sl@0: const unsigned char *key, const unsigned char *iv, int enc) sl@0: { sl@0: if (enc == -1) sl@0: enc = ctx->encrypt; sl@0: else sl@0: { sl@0: if (enc) sl@0: enc = 1; sl@0: ctx->encrypt = enc; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts sl@0: * so this context may already have an ENGINE! Try to avoid releasing sl@0: * the previous handle, re-querying for an ENGINE, and having a sl@0: * reinitialisation, when it may all be unecessary. */ sl@0: if (ctx->engine && ctx->cipher && (!cipher || sl@0: (cipher && (cipher->nid == ctx->cipher->nid)))) sl@0: goto skip_to_init; sl@0: #endif sl@0: if (cipher) sl@0: { sl@0: /* Ensure a context left lying around from last time is cleared sl@0: * (the previous check attempted to avoid this if the same sl@0: * ENGINE and EVP_CIPHER could be used). */ sl@0: EVP_CIPHER_CTX_cleanup(ctx); sl@0: sl@0: /* Restore encrypt field: it is zeroed by cleanup */ sl@0: ctx->encrypt = enc; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if(impl) sl@0: { sl@0: if (!ENGINE_init(impl)) sl@0: { sl@0: EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); sl@0: return 0; sl@0: } sl@0: } sl@0: else sl@0: /* Ask if an ENGINE is reserved for this job */ sl@0: impl = ENGINE_get_cipher_engine(cipher->nid); sl@0: if(impl) sl@0: { sl@0: /* There's an ENGINE for this job ... (apparently) */ sl@0: const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid); sl@0: if(!c) sl@0: { sl@0: /* One positive side-effect of US's export sl@0: * control history, is that we should at least sl@0: * be able to avoid using US mispellings of sl@0: * "initialisation"? */ sl@0: EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); sl@0: return 0; sl@0: } sl@0: /* We'll use the ENGINE's private cipher definition */ sl@0: cipher = c; sl@0: /* Store the ENGINE functional reference so we know sl@0: * 'cipher' came from an ENGINE and we need to release sl@0: * it when done. */ sl@0: ctx->engine = impl; sl@0: } sl@0: else sl@0: ctx->engine = NULL; sl@0: #endif sl@0: sl@0: ctx->cipher=cipher; sl@0: if (ctx->cipher->ctx_size) sl@0: { sl@0: ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size); sl@0: if (!ctx->cipher_data) sl@0: { sl@0: EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: ctx->cipher_data = NULL; sl@0: } sl@0: ctx->key_len = cipher->key_len; sl@0: ctx->flags = 0; sl@0: if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT) sl@0: { sl@0: if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) sl@0: { sl@0: EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); sl@0: return 0; sl@0: } sl@0: } sl@0: } sl@0: else if(!ctx->cipher) sl@0: { sl@0: EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET); sl@0: return 0; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: skip_to_init: sl@0: #endif sl@0: /* we assume block size is a power of 2 in *cryptUpdate */ sl@0: OPENSSL_assert(ctx->cipher->block_size == 1 sl@0: || ctx->cipher->block_size == 8 sl@0: || ctx->cipher->block_size == 16); sl@0: sl@0: if(!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) { sl@0: switch(EVP_CIPHER_CTX_mode(ctx)) { sl@0: sl@0: case EVP_CIPH_STREAM_CIPHER: sl@0: case EVP_CIPH_ECB_MODE: sl@0: break; sl@0: sl@0: case EVP_CIPH_CFB_MODE: sl@0: case EVP_CIPH_OFB_MODE: sl@0: sl@0: ctx->num = 0; sl@0: sl@0: case EVP_CIPH_CBC_MODE: sl@0: sl@0: OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <= sl@0: (int)sizeof(ctx->iv)); sl@0: if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); sl@0: memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); sl@0: break; sl@0: sl@0: default: sl@0: return 0; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { sl@0: if(!ctx->cipher->init(ctx,key,iv,enc)) return 0; sl@0: } sl@0: ctx->buf_len=0; sl@0: ctx->final_used=0; sl@0: ctx->block_mask=ctx->cipher->block_size-1; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, sl@0: const unsigned char *in, int inl) sl@0: { sl@0: if (ctx->encrypt) sl@0: return EVP_EncryptUpdate(ctx,out,outl,in,inl); sl@0: else return EVP_DecryptUpdate(ctx,out,outl,in,inl); sl@0: } sl@0: sl@0: EXPORT_C int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: if (ctx->encrypt) sl@0: return EVP_EncryptFinal_ex(ctx,out,outl); sl@0: else return EVP_DecryptFinal_ex(ctx,out,outl); sl@0: } sl@0: sl@0: EXPORT_C int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: if (ctx->encrypt) sl@0: return EVP_EncryptFinal(ctx,out,outl); sl@0: else return EVP_DecryptFinal(ctx,out,outl); sl@0: } sl@0: sl@0: EXPORT_C int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, sl@0: const unsigned char *key, const unsigned char *iv) sl@0: { sl@0: return EVP_CipherInit(ctx, cipher, key, iv, 1); sl@0: } sl@0: sl@0: EXPORT_C int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, sl@0: const unsigned char *key, const unsigned char *iv) sl@0: { sl@0: return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, sl@0: const unsigned char *key, const unsigned char *iv) sl@0: { sl@0: return EVP_CipherInit(ctx, cipher, key, iv, 0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, sl@0: const unsigned char *key, const unsigned char *iv) sl@0: { sl@0: return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, sl@0: const unsigned char *in, int inl) sl@0: { sl@0: int i,j,bl; sl@0: sl@0: OPENSSL_assert(inl > 0); sl@0: if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0) sl@0: { sl@0: if(ctx->cipher->do_cipher(ctx,out,in,inl)) sl@0: { sl@0: *outl=inl; sl@0: return 1; sl@0: } sl@0: else sl@0: { sl@0: *outl=0; sl@0: return 0; sl@0: } sl@0: } sl@0: i=ctx->buf_len; sl@0: bl=ctx->cipher->block_size; sl@0: OPENSSL_assert(bl <= (int)sizeof(ctx->buf)); sl@0: if (i != 0) sl@0: { sl@0: if (i+inl < bl) sl@0: { sl@0: memcpy(&(ctx->buf[i]),in,inl); sl@0: ctx->buf_len+=inl; sl@0: *outl=0; sl@0: return 1; sl@0: } sl@0: else sl@0: { sl@0: j=bl-i; sl@0: memcpy(&(ctx->buf[i]),in,j); sl@0: if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0; sl@0: inl-=j; sl@0: in+=j; sl@0: out+=bl; sl@0: *outl=bl; sl@0: } sl@0: } sl@0: else sl@0: *outl = 0; sl@0: i=inl&(bl-1); sl@0: inl-=i; sl@0: if (inl > 0) sl@0: { sl@0: if(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0; sl@0: *outl+=inl; sl@0: } sl@0: sl@0: if (i != 0) sl@0: memcpy(ctx->buf,&(in[inl]),i); sl@0: ctx->buf_len=i; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: int ret; sl@0: ret = EVP_EncryptFinal_ex(ctx, out, outl); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: int n,ret; sl@0: unsigned int i, b, bl; sl@0: sl@0: b=ctx->cipher->block_size; sl@0: OPENSSL_assert(b <= sizeof ctx->buf); sl@0: if (b == 1) sl@0: { sl@0: *outl=0; sl@0: return 1; sl@0: } sl@0: bl=ctx->buf_len; sl@0: if (ctx->flags & EVP_CIPH_NO_PADDING) sl@0: { sl@0: if(bl) sl@0: { sl@0: EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); sl@0: return 0; sl@0: } sl@0: *outl = 0; sl@0: return 1; sl@0: } sl@0: sl@0: n=b-bl; sl@0: for (i=bl; ibuf[i]=n; sl@0: ret=ctx->cipher->do_cipher(ctx,out,ctx->buf,b); sl@0: sl@0: sl@0: if(ret) sl@0: *outl=b; sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, sl@0: const unsigned char *in, int inl) sl@0: { sl@0: int fix_len; sl@0: unsigned int b; sl@0: sl@0: if (inl == 0) sl@0: { sl@0: *outl=0; sl@0: return 1; sl@0: } sl@0: sl@0: if (ctx->flags & EVP_CIPH_NO_PADDING) sl@0: return EVP_EncryptUpdate(ctx, out, outl, in, inl); sl@0: sl@0: b=ctx->cipher->block_size; sl@0: OPENSSL_assert(b <= sizeof ctx->final); sl@0: sl@0: if(ctx->final_used) sl@0: { sl@0: memcpy(out,ctx->final,b); sl@0: out+=b; sl@0: fix_len = 1; sl@0: } sl@0: else sl@0: fix_len = 0; sl@0: sl@0: sl@0: if(!EVP_EncryptUpdate(ctx,out,outl,in,inl)) sl@0: return 0; sl@0: sl@0: /* if we have 'decrypted' a multiple of block size, make sure sl@0: * we have a copy of this last block */ sl@0: if (b > 1 && !ctx->buf_len) sl@0: { sl@0: *outl-=b; sl@0: ctx->final_used=1; sl@0: memcpy(ctx->final,&out[*outl],b); sl@0: } sl@0: else sl@0: ctx->final_used = 0; sl@0: sl@0: if (fix_len) sl@0: *outl += b; sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: int ret; sl@0: ret = EVP_DecryptFinal_ex(ctx, out, outl); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) sl@0: { sl@0: int i,n; sl@0: unsigned int b; sl@0: sl@0: *outl=0; sl@0: b=ctx->cipher->block_size; sl@0: if (ctx->flags & EVP_CIPH_NO_PADDING) sl@0: { sl@0: if(ctx->buf_len) sl@0: { sl@0: EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); sl@0: return 0; sl@0: } sl@0: *outl = 0; sl@0: return 1; sl@0: } sl@0: if (b > 1) sl@0: { sl@0: if (ctx->buf_len || !ctx->final_used) sl@0: { sl@0: EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_WRONG_FINAL_BLOCK_LENGTH); sl@0: return(0); sl@0: } sl@0: OPENSSL_assert(b <= sizeof ctx->final); sl@0: n=ctx->final[b-1]; sl@0: if (n == 0 || n > (int)b) sl@0: { sl@0: EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_BAD_DECRYPT); sl@0: return(0); sl@0: } sl@0: for (i=0; ifinal[--b] != n) sl@0: { sl@0: EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_BAD_DECRYPT); sl@0: return(0); sl@0: } sl@0: } sl@0: n=ctx->cipher->block_size-n; sl@0: for (i=0; ifinal[i]; sl@0: *outl=n; sl@0: } sl@0: else sl@0: *outl=0; sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) sl@0: { sl@0: if (ctx) sl@0: { sl@0: EVP_CIPHER_CTX_cleanup(ctx); sl@0: OPENSSL_free(ctx); sl@0: } sl@0: } sl@0: EXPORT_C int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) sl@0: { sl@0: if (c->cipher != NULL) sl@0: { sl@0: if(c->cipher->cleanup && !c->cipher->cleanup(c)) sl@0: return 0; sl@0: /* Cleanse cipher context data */ sl@0: if (c->cipher_data) sl@0: OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size); sl@0: } sl@0: if (c->cipher_data) sl@0: OPENSSL_free(c->cipher_data); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (c->engine) sl@0: /* The EVP_CIPHER we used belongs to an ENGINE, release the sl@0: * functional reference we held for this reason. */ sl@0: ENGINE_finish(c->engine); sl@0: #endif sl@0: memset(c,0,sizeof(EVP_CIPHER_CTX)); sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) sl@0: { sl@0: if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) sl@0: return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL); sl@0: if(c->key_len == keylen) return 1; sl@0: if((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) sl@0: { sl@0: c->key_len = keylen; sl@0: return 1; sl@0: } sl@0: EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH); sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) sl@0: { sl@0: if (pad) ctx->flags &= ~EVP_CIPH_NO_PADDING; sl@0: else ctx->flags |= EVP_CIPH_NO_PADDING; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) sl@0: { sl@0: int ret; sl@0: if(!ctx->cipher) { sl@0: EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET); sl@0: return 0; sl@0: } sl@0: sl@0: if(!ctx->cipher->ctrl) { sl@0: EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED); sl@0: return 0; sl@0: } sl@0: sl@0: ret = ctx->cipher->ctrl(ctx, type, arg, ptr); sl@0: if(ret == -1) { sl@0: EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); sl@0: return 0; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) sl@0: { sl@0: if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) sl@0: return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); sl@0: if (RAND_bytes(key, ctx->key_len) <= 0) sl@0: return 0; sl@0: return 1; sl@0: } sl@0: