1 /* Written by Ben Laurie, 2001 */
3 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 #ifdef OPENSSL_OPENBSD_DEV_CRYPTO
55 #include <sys/ioctl.h>
56 #include <crypto/cryptodev.h>
58 #include <openssl/evp.h>
59 #include <openssl/objects.h>
60 #include <openssl/rsa.h>
64 /* longest key supported in hardware */
68 #define MD5_DIGEST_LENGTH 16
72 static int dev_failed;
74 typedef struct session_op session_op;
76 #define data(ctx) EVP_C_DATA(session_op,ctx)
78 static void err(const char *str)
80 fprintf(stderr,"%s: errno %d\n",str,errno);
83 static int dev_crypto_init(session_op *ses)
91 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
97 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
99 err("CRIOGET failed");
107 memset(ses,'\0',sizeof *ses);
112 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
114 printf("Cleanup %d\n",data(ctx)->ses);
115 if(ioctl(fd,CIOCFSESSION,&data(ctx)->ses) == -1)
116 err("CIOCFSESSION failed");
118 OPENSSL_free(data(ctx)->key);
123 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
124 const unsigned char *key,int klen)
126 if(!dev_crypto_init(data(ctx)))
129 data(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
131 assert(ctx->cipher->iv_len <= MAX_HW_IV);
133 memcpy(data(ctx)->key,key,klen);
135 data(ctx)->cipher=cipher;
136 data(ctx)->keylen=klen;
138 if (ioctl(fd,CIOCGSESSION,data(ctx)) == -1)
140 err("CIOCGSESSION failed");
143 printf("Init %d\n",data(ctx)->ses);
147 static int dev_crypto_init_digest(session_op *ses,int mac)
149 if(!dev_crypto_init(ses))
154 if (ioctl(fd,CIOCGSESSION,ses) == -1)
156 err("CIOCGSESSION failed");
159 printf("Init MAC %d\n",ses->ses);
163 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
164 const unsigned char *in,unsigned int inl)
166 struct crypt_op cryp;
167 unsigned char lb[MAX_HW_IV];
175 memset(&cryp,'\0',sizeof cryp);
176 cryp.ses=data(ctx)->ses;
177 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
180 assert((inl&(ctx->cipher->block_size-1)) == 0);
181 cryp.src=(caddr_t)in;
182 cryp.dst=(caddr_t)out;
184 if(ctx->cipher->iv_len)
185 cryp.iv=(caddr_t)ctx->iv;
188 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
190 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
192 if(errno == EINVAL) /* buffers are misaligned */
198 /* NB: this can only make cinl != inl with stream ciphers */
201 if(((unsigned long)in&3) || cinl != inl)
203 cin=OPENSSL_malloc(cinl);
208 if(((unsigned long)out&3) || cinl != inl)
210 cout=OPENSSL_malloc(cinl);
216 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
218 err("CIOCCRYPT(2) failed");
219 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
226 memcpy(out,cout,inl);
234 err("CIOCCRYPT failed");
241 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
243 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
248 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
249 const unsigned char *key,
250 const unsigned char *iv, int enc)
251 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
253 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
255 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
256 0, dev_crypto_des_ede3_init_key,
258 EVP_CIPHER_set_asn1_iv,
259 EVP_CIPHER_get_asn1_iv,
262 static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
263 const unsigned char *key,
264 const unsigned char *iv, int enc)
265 { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
267 static const EVP_CIPHER r4_cipher=
270 1,16,0, /* FIXME: key should be up to 256 bytes */
271 EVP_CIPH_VARIABLE_LENGTH,
272 dev_crypto_rc4_init_key,
281 const EVP_CIPHER *EVP_dev_crypto_rc4(void)
282 { return &r4_cipher; }
284 static int dev_crypto_md5_init(void *md_data)
285 { return dev_crypto_init_digest(md_data,CRYPTO_MD5); }
287 static int dev_crypto_md5_update(void *md_data,const void *data,
290 struct crypt_op cryp;
291 session_op *ses=md_data;
292 char buf[MD5_DIGEST_LENGTH];
295 memset(&cryp,'\0',sizeof cryp);
298 cryp.src=(caddr_t)data;
301 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
303 err("CIOCCRYPT(MAC) failed");
307 printf("update done\n");
311 static int dev_crypto_md5_final(unsigned char *md,void *md_data)
313 struct crypt_op cryp;
314 session_op *ses=md_data;
317 memset(&cryp,'\0',sizeof cryp);
320 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
321 cryp.src=(caddr_t)md;
322 cryp.dst=(caddr_t)md;
324 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
326 err("CIOCCRYPT(MAC,final) failed");
331 printf("final done\n");
335 static const EVP_MD md5_md=
338 NID_md5WithRSAEncryption,
341 dev_crypto_md5_update,
342 dev_crypto_md5_final,
348 const EVP_MD *EVP_dev_crypto_md5(void)
352 static void *dummy=&dummy;