sl@0: /* Written by Ben Laurie, 2001 */ sl@0: /* sl@0: * Copyright (c) 2001 The OpenSSL Project. All rights reserved. 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: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "evp_locl.h" sl@0: sl@0: /* This stuff should now all be supported through sl@0: * crypto/engine/hw_openbsd_dev_crypto.c unless I botched it up */ sl@0: static void *dummy=&dummy; sl@0: sl@0: #if 0 sl@0: sl@0: /* check flag after OpenSSL headers to ensure make depend works */ sl@0: #ifdef OPENSSL_OPENBSD_DEV_CRYPTO sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* longest key supported in hardware */ sl@0: #define MAX_HW_KEY 24 sl@0: #define MAX_HW_IV 8 sl@0: sl@0: #define MD5_DIGEST_LENGTH 16 sl@0: #define MD5_CBLOCK 64 sl@0: sl@0: static int fd; sl@0: static int dev_failed; sl@0: sl@0: typedef struct session_op session_op; sl@0: sl@0: #define CDATA(ctx) EVP_C_DATA(session_op,ctx) sl@0: sl@0: static void err(const char *str) sl@0: { sl@0: fprintf(stderr,"%s: errno %d\n",str,errno); sl@0: } sl@0: sl@0: static int dev_crypto_init(session_op *ses) sl@0: { sl@0: if(dev_failed) sl@0: return 0; sl@0: if(!fd) sl@0: { sl@0: int cryptodev_fd; sl@0: sl@0: if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0) sl@0: { sl@0: err("/dev/crypto"); sl@0: dev_failed=1; sl@0: return 0; sl@0: } sl@0: if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1) sl@0: { sl@0: err("CRIOGET failed"); sl@0: close(cryptodev_fd); sl@0: dev_failed=1; sl@0: return 0; sl@0: } sl@0: close(cryptodev_fd); sl@0: } sl@0: assert(ses); sl@0: memset(ses,'\0',sizeof *ses); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx) sl@0: { sl@0: if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1) sl@0: err("CIOCFSESSION failed"); sl@0: sl@0: OPENSSL_free(CDATA(ctx)->key); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher, sl@0: const unsigned char *key,int klen) sl@0: { sl@0: if(!dev_crypto_init(CDATA(ctx))) sl@0: return 0; sl@0: sl@0: CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY); sl@0: sl@0: assert(ctx->cipher->iv_len <= MAX_HW_IV); sl@0: sl@0: memcpy(CDATA(ctx)->key,key,klen); sl@0: sl@0: CDATA(ctx)->cipher=cipher; sl@0: CDATA(ctx)->keylen=klen; sl@0: sl@0: if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1) sl@0: { sl@0: err("CIOCGSESSION failed"); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, sl@0: const unsigned char *in,unsigned int inl) sl@0: { sl@0: struct crypt_op cryp; sl@0: unsigned char lb[MAX_HW_IV]; sl@0: sl@0: if(!inl) sl@0: return 1; sl@0: sl@0: assert(CDATA(ctx)); sl@0: assert(!dev_failed); sl@0: sl@0: memset(&cryp,'\0',sizeof cryp); sl@0: cryp.ses=CDATA(ctx)->ses; sl@0: cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT; sl@0: cryp.flags=0; sl@0: cryp.len=inl; sl@0: assert((inl&(ctx->cipher->block_size-1)) == 0); sl@0: cryp.src=(caddr_t)in; sl@0: cryp.dst=(caddr_t)out; sl@0: cryp.mac=0; sl@0: if(ctx->cipher->iv_len) sl@0: cryp.iv=(caddr_t)ctx->iv; sl@0: sl@0: if(!ctx->encrypt) sl@0: memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len); sl@0: sl@0: if(ioctl(fd, CIOCCRYPT, &cryp) == -1) sl@0: { sl@0: if(errno == EINVAL) /* buffers are misaligned */ sl@0: { sl@0: unsigned int cinl=0; sl@0: char *cin=NULL; sl@0: char *cout=NULL; sl@0: sl@0: /* NB: this can only make cinl != inl with stream ciphers */ sl@0: cinl=(inl+3)/4*4; sl@0: sl@0: if(((unsigned long)in&3) || cinl != inl) sl@0: { sl@0: cin=OPENSSL_malloc(cinl); sl@0: memcpy(cin,in,inl); sl@0: cryp.src=cin; sl@0: } sl@0: sl@0: if(((unsigned long)out&3) || cinl != inl) sl@0: { sl@0: cout=OPENSSL_malloc(cinl); sl@0: cryp.dst=cout; sl@0: } sl@0: sl@0: cryp.len=cinl; sl@0: sl@0: if(ioctl(fd, CIOCCRYPT, &cryp) == -1) sl@0: { sl@0: err("CIOCCRYPT(2) failed"); sl@0: printf("src=%p dst=%p\n",cryp.src,cryp.dst); sl@0: abort(); sl@0: return 0; sl@0: } sl@0: sl@0: if(cout) sl@0: { sl@0: memcpy(out,cout,inl); sl@0: OPENSSL_free(cout); sl@0: } sl@0: if(cin) sl@0: OPENSSL_free(cin); sl@0: } sl@0: else sl@0: { sl@0: err("CIOCCRYPT failed"); sl@0: abort(); sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: if(ctx->encrypt) sl@0: memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len); sl@0: else sl@0: memcpy(ctx->iv,lb,ctx->cipher->iv_len); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx, sl@0: const unsigned char *key, sl@0: const unsigned char *iv, int enc) sl@0: { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); } sl@0: sl@0: #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher sl@0: sl@0: BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8, sl@0: 0, dev_crypto_des_ede3_init_key, sl@0: dev_crypto_cleanup, sl@0: EVP_CIPHER_set_asn1_iv, sl@0: EVP_CIPHER_get_asn1_iv, sl@0: NULL) sl@0: sl@0: static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx, sl@0: const unsigned char *key, sl@0: const unsigned char *iv, int enc) sl@0: { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); } sl@0: sl@0: static const EVP_CIPHER r4_cipher= sl@0: { sl@0: NID_rc4, sl@0: 1,16,0, /* FIXME: key should be up to 256 bytes */ sl@0: EVP_CIPH_VARIABLE_LENGTH, sl@0: dev_crypto_rc4_init_key, sl@0: dev_crypto_cipher, sl@0: dev_crypto_cleanup, sl@0: sizeof(session_op), sl@0: NULL, sl@0: NULL, sl@0: NULL sl@0: }; sl@0: sl@0: const EVP_CIPHER *EVP_dev_crypto_rc4(void) sl@0: { return &r4_cipher; } sl@0: sl@0: typedef struct sl@0: { sl@0: session_op sess; sl@0: char *data; sl@0: int len; sl@0: unsigned char md[EVP_MAX_MD_SIZE]; sl@0: } MD_DATA; sl@0: sl@0: static int dev_crypto_init_digest(MD_DATA *md_data,int mac) sl@0: { sl@0: if(!dev_crypto_init(&md_data->sess)) sl@0: return 0; sl@0: sl@0: md_data->len=0; sl@0: md_data->data=NULL; sl@0: sl@0: md_data->sess.mac=mac; sl@0: sl@0: if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1) sl@0: { sl@0: err("CIOCGSESSION failed"); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_cleanup_digest(MD_DATA *md_data) sl@0: { sl@0: if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1) sl@0: { sl@0: err("CIOCFSESSION failed"); sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: /* FIXME: if device can do chained MACs, then don't accumulate */ sl@0: /* FIXME: move accumulation to the framework */ sl@0: static int dev_crypto_md5_init(EVP_MD_CTX *ctx) sl@0: { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); } sl@0: sl@0: static int do_digest(int ses,unsigned char *md,const void *data,int len) sl@0: { sl@0: struct crypt_op cryp; sl@0: static unsigned char md5zero[16]= sl@0: { sl@0: 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04, sl@0: 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e sl@0: }; sl@0: sl@0: /* some cards can't do zero length */ sl@0: if(!len) sl@0: { sl@0: memcpy(md,md5zero,16); sl@0: return 1; sl@0: } sl@0: sl@0: memset(&cryp,'\0',sizeof cryp); sl@0: cryp.ses=ses; sl@0: cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */ sl@0: cryp.len=len; sl@0: cryp.src=(caddr_t)data; sl@0: cryp.dst=(caddr_t)data; // FIXME!!! sl@0: cryp.mac=(caddr_t)md; sl@0: sl@0: if(ioctl(fd, CIOCCRYPT, &cryp) == -1) sl@0: { sl@0: if(errno == EINVAL) /* buffer is misaligned */ sl@0: { sl@0: char *dcopy; sl@0: sl@0: dcopy=OPENSSL_malloc(len); sl@0: memcpy(dcopy,data,len); sl@0: cryp.src=dcopy; sl@0: cryp.dst=cryp.src; // FIXME!!! sl@0: sl@0: if(ioctl(fd, CIOCCRYPT, &cryp) == -1) sl@0: { sl@0: err("CIOCCRYPT(MAC2) failed"); sl@0: abort(); sl@0: return 0; sl@0: } sl@0: OPENSSL_free(dcopy); sl@0: } sl@0: else sl@0: { sl@0: err("CIOCCRYPT(MAC) failed"); sl@0: abort(); sl@0: return 0; sl@0: } sl@0: } sl@0: // printf("done\n"); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data, sl@0: unsigned long len) sl@0: { sl@0: MD_DATA *md_data=ctx->md_data; sl@0: sl@0: if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT) sl@0: return do_digest(md_data->sess.ses,md_data->md,data,len); sl@0: sl@0: md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len); sl@0: memcpy(md_data->data+md_data->len,data,len); sl@0: md_data->len+=len; sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md) sl@0: { sl@0: int ret; sl@0: MD_DATA *md_data=ctx->md_data; sl@0: sl@0: if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT) sl@0: { sl@0: memcpy(md,md_data->md,MD5_DIGEST_LENGTH); sl@0: ret=1; sl@0: } sl@0: else sl@0: { sl@0: ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len); sl@0: OPENSSL_free(md_data->data); sl@0: md_data->data=NULL; sl@0: md_data->len=0; sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from) sl@0: { sl@0: const MD_DATA *from_md=from->md_data; sl@0: MD_DATA *to_md=to->md_data; sl@0: sl@0: // How do we copy sessions? sl@0: assert(from->digest->flags&EVP_MD_FLAG_ONESHOT); sl@0: sl@0: to_md->data=OPENSSL_malloc(from_md->len); sl@0: memcpy(to_md->data,from_md->data,from_md->len); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx) sl@0: { sl@0: return dev_crypto_cleanup_digest(ctx->md_data); sl@0: } sl@0: sl@0: static const EVP_MD md5_md= sl@0: { sl@0: NID_md5, sl@0: NID_md5WithRSAEncryption, sl@0: MD5_DIGEST_LENGTH, sl@0: EVP_MD_FLAG_ONESHOT, // XXX: set according to device info... sl@0: dev_crypto_md5_init, sl@0: dev_crypto_md5_update, sl@0: dev_crypto_md5_final, sl@0: dev_crypto_md5_copy, sl@0: dev_crypto_md5_cleanup, sl@0: EVP_PKEY_RSA_method, sl@0: MD5_CBLOCK, sl@0: sizeof(MD_DATA), sl@0: }; sl@0: sl@0: const EVP_MD *EVP_dev_crypto_md5(void) sl@0: { return &md5_md; } sl@0: sl@0: #endif sl@0: #endif