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 #include <openssl/evp.h>
51 #include <openssl/objects.h>
52 #include <openssl/rsa.h>
55 /* check flag after OpenSSL headers to ensure make depend works */
56 #ifdef OPENSSL_OPENBSD_DEV_CRYPTO
61 #include <sys/ioctl.h>
62 #include <crypto/cryptodev.h>
66 /* longest key supported in hardware */
70 #define MD5_DIGEST_LENGTH 16
74 static int dev_failed;
76 typedef struct session_op session_op;
78 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
80 static void err(const char *str)
82 fprintf(stderr,"%s: errno %d\n",str,errno);
85 static int dev_crypto_init(session_op *ses)
93 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
99 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
101 err("CRIOGET failed");
109 memset(ses,'\0',sizeof *ses);
114 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
116 if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
117 err("CIOCFSESSION failed");
119 OPENSSL_free(CDATA(ctx)->key);
124 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
125 const unsigned char *key,int klen)
127 if(!dev_crypto_init(CDATA(ctx)))
130 CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
132 assert(ctx->cipher->iv_len <= MAX_HW_IV);
134 memcpy(CDATA(ctx)->key,key,klen);
136 CDATA(ctx)->cipher=cipher;
137 CDATA(ctx)->keylen=klen;
139 if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
141 err("CIOCGSESSION failed");
147 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
148 const unsigned char *in,unsigned int inl)
150 struct crypt_op cryp;
151 unsigned char lb[MAX_HW_IV];
159 memset(&cryp,'\0',sizeof cryp);
160 cryp.ses=CDATA(ctx)->ses;
161 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
164 assert((inl&(ctx->cipher->block_size-1)) == 0);
165 cryp.src=(caddr_t)in;
166 cryp.dst=(caddr_t)out;
168 if(ctx->cipher->iv_len)
169 cryp.iv=(caddr_t)ctx->iv;
172 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
174 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
176 if(errno == EINVAL) /* buffers are misaligned */
182 /* NB: this can only make cinl != inl with stream ciphers */
185 if(((unsigned long)in&3) || cinl != inl)
187 cin=OPENSSL_malloc(cinl);
192 if(((unsigned long)out&3) || cinl != inl)
194 cout=OPENSSL_malloc(cinl);
200 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
202 err("CIOCCRYPT(2) failed");
203 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
210 memcpy(out,cout,inl);
218 err("CIOCCRYPT failed");
225 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
227 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
232 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
233 const unsigned char *key,
234 const unsigned char *iv, int enc)
235 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
237 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
239 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
240 0, dev_crypto_des_ede3_init_key,
242 EVP_CIPHER_set_asn1_iv,
243 EVP_CIPHER_get_asn1_iv,
246 static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
247 const unsigned char *key,
248 const unsigned char *iv, int enc)
249 { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
251 static const EVP_CIPHER r4_cipher=
254 1,16,0, /* FIXME: key should be up to 256 bytes */
255 EVP_CIPH_VARIABLE_LENGTH,
256 dev_crypto_rc4_init_key,
265 const EVP_CIPHER *EVP_dev_crypto_rc4(void)
266 { return &r4_cipher; }
273 unsigned char md[EVP_MAX_MD_SIZE];
276 static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
278 if(!dev_crypto_init(&md_data->sess))
284 md_data->sess.mac=mac;
286 if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
288 err("CIOCGSESSION failed");
294 static int dev_crypto_cleanup_digest(MD_DATA *md_data)
296 if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
298 err("CIOCFSESSION failed");
305 /* FIXME: if device can do chained MACs, then don't accumulate */
306 /* FIXME: move accumulation to the framework */
307 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
308 { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
310 static int do_digest(int ses,unsigned char *md,const void *data,int len)
312 struct crypt_op cryp;
313 static unsigned char md5zero[16]=
315 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
316 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
319 /* some cards can't do zero length */
322 memcpy(md,md5zero,16);
326 memset(&cryp,'\0',sizeof cryp);
328 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
330 cryp.src=(caddr_t)data;
331 cryp.dst=(caddr_t)data; // FIXME!!!
332 cryp.mac=(caddr_t)md;
334 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
336 if(errno == EINVAL) /* buffer is misaligned */
340 dcopy=OPENSSL_malloc(len);
341 memcpy(dcopy,data,len);
343 cryp.dst=cryp.src; // FIXME!!!
345 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
347 err("CIOCCRYPT(MAC2) failed");
355 err("CIOCCRYPT(MAC) failed");
365 static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
368 MD_DATA *md_data=ctx->md_data;
370 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
371 return do_digest(md_data->sess.ses,md_data->md,data,len);
373 md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
374 memcpy(md_data->data+md_data->len,data,len);
380 static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
383 MD_DATA *md_data=ctx->md_data;
385 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
387 memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
392 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
393 OPENSSL_free(md_data->data);
401 static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
403 const MD_DATA *from_md=from->md_data;
404 MD_DATA *to_md=to->md_data;
406 // How do we copy sessions?
407 assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
409 to_md->data=OPENSSL_malloc(from_md->len);
410 memcpy(to_md->data,from_md->data,from_md->len);
415 static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
417 return dev_crypto_cleanup_digest(ctx->md_data);
420 static const EVP_MD md5_md=
423 NID_md5WithRSAEncryption,
425 EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
427 dev_crypto_md5_update,
428 dev_crypto_md5_final,
430 dev_crypto_md5_cleanup,
436 const EVP_MD *EVP_dev_crypto_md5(void)