2 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 #ifdef OPENSSL_OPENBSD_DEV_CRYPTO
54 #include <sys/ioctl.h>
55 #include <crypto/cryptodev.h>
57 #include <openssl/evp.h>
58 #include <openssl/objects.h>
62 /* longest key supported in hardware */
66 static int dev_failed;
68 typedef struct session_op session_op;
70 #define data(ctx) EVP_C_DATA(session_op,ctx)
72 static void err(const char *str)
74 fprintf(stderr,"%s: errno %d\n",str,errno);
77 static int dev_crypto_init(EVP_CIPHER_CTX *ctx)
85 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
91 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
93 err("CRIOGET failed");
101 memset(data(ctx),'\0',sizeof *data(ctx));
102 data(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
107 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
109 fprintf(stderr,"clean up session %d\n",data(ctx)->ses);
110 if(ioctl(fd,CIOCFSESSION,&data(ctx)->ses) == -1)
111 err("CIOCFSESSION failed");
113 OPENSSL_free(data(ctx)->key);
118 /* FIXME: there should be some non-fatal way to report we fell back to s/w? */
119 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
120 const unsigned char *key,
121 const unsigned char *iv, int enc)
123 if(!dev_crypto_init(ctx))
125 /* fall back to using software... */
126 ctx->cipher=EVP_des_ede3_cbc();
127 return ctx->cipher->init(ctx,key,iv,enc);
129 memcpy(data(ctx)->key,key,24);
131 data(ctx)->cipher=CRYPTO_3DES_CBC;
133 data(ctx)->keylen=24;
135 if (ioctl(fd,CIOCGSESSION,data(ctx)) == -1)
137 err("CIOCGSESSION failed");
138 /* fall back to using software... */
139 dev_crypto_cleanup(ctx);
140 ctx->cipher=EVP_des_ede3_cbc();
141 return ctx->cipher->init(ctx,key,iv,enc);
143 fprintf(stderr,"created session %d\n",data(ctx)->ses);
147 static int dev_crypto_des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx,
149 const unsigned char *in,
152 struct crypt_op cryp;
158 memset(&cryp,'\0',sizeof cryp);
159 cryp.ses=data(ctx)->ses;
160 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
163 cryp.len=((inl+7)/8)*8;
166 assert((inl&7) == 0);
167 cryp.src=(caddr_t)in;
168 cryp.dst=(caddr_t)out;
170 cryp.iv=(caddr_t)ctx->iv;
173 memcpy(lb,&in[cryp.len-8],8);
175 if (ioctl(fd, CIOCCRYPT, &cryp) == -1)
177 err("CIOCCRYPT failed");
183 memcpy(ctx->iv,&out[cryp.len-8],8);
185 memcpy(ctx->iv,lb,8);
190 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
191 0, dev_crypto_des_ede3_init_key,
193 EVP_CIPHER_set_asn1_iv,
194 EVP_CIPHER_get_asn1_iv,
197 static void *dummy=&dummy;