1 /* Written by Ben Laurie <ben@algroup.co.uk> August 2001 */
2 /* ====================================================================
3 * Copyright (c) 1999-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 * licensing@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.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
56 #ifndef OPENSSL_OPENBSD_DEV_CRYPTO
58 static void *dummy=&dummy;
60 #else /* OPENSSL_OPENBSD_DEV_CRYPTO */
65 #include <sys/ioctl.h>
66 #include <crypto/cryptodev.h>
67 #include <openssl/engine.h>
68 #include <openssl/evp.h>
70 /* Maybe this is needed? ... */
71 #include "../evp/evp_locl.h"
73 /****************************************************/
74 /* Declare the normal generic ENGINE stuff here ... */
76 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
77 const int **nids, int nid);
78 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
79 const int **nids, int nid);
81 static const char *dev_crypto_id = "openbsd_dev_crypto";
82 static const char *dev_crypto_name = "OpenBSD /dev/crypto";
84 static ENGINE *engine_openbsd_dev_crypto(void)
86 ENGINE *engine=ENGINE_new();
88 if(!ENGINE_set_id(engine, dev_crypto_id) ||
89 !ENGINE_set_name(engine, dev_crypto_name) ||
90 !ENGINE_set_ciphers(engine, dev_crypto_ciphers) ||
91 !ENGINE_set_digests(engine, dev_crypto_digests))
100 void ENGINE_load_openbsd_dev_crypto(void)
102 /* Copied from eng_[openssl|dyn].c */
103 ENGINE *toadd = engine_openbsd_dev_crypto();
110 /******************************************************************************/
111 /* Clip in the stuff from crypto/evp/openbsd_hw.c here. NB: What has changed? */
112 /* I've removed the exposed EVP_*** functions, they're accessed through the */
113 /* "dev_crypto_[ciphers|digests]" handlers. I've also moved the EVP_CIPHER */
114 /* and EVP_MD structures to the bottom where they are close to the handlers */
115 /* that expose them. What should be done? The global data (file-descriptors, */
116 /* etc) should be put into ENGINE's ex_data support, and per-context data */
117 /* (also file-descriptors perhaps) should be put into the contexts. Also code */
118 /* formatting, fprintf statements, and OpenSSL-style error handling should be */
119 /* added (dynamically, like the other ENGINEs). Also, "dynamic" support */
120 /* be added to this ENGINE once it's up and running so that it could be built */
121 /* as a shared-library. What else? device initialisation should take place */
122 /* inside an ENGINE 'init()' handler (and likewise 'finish()'). ciphers and */
123 /* digests won't be used by the framework unless the ENGINE has been */
124 /* successfully initialised (that's one of the things you get for free) so */
125 /* initialisation, including returning failure if device setup fails, can be */
126 /* handled quite cleanly. This could presumably handle the opening (and then */
127 /* closing inside 'finish()') of the 'cryptodev_fd' file-descriptor). */
129 /* longest key supported in hardware */
130 #define MAX_HW_KEY 24
133 #define MD5_DIGEST_LENGTH 16
134 #define MD5_CBLOCK 64
137 static int dev_failed;
139 typedef struct session_op session_op;
141 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
143 static void err(const char *str)
145 fprintf(stderr,"%s: errno %d\n",str,errno);
148 static int dev_crypto_init(session_op *ses)
156 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
162 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
164 err("CRIOGET failed");
172 memset(ses,'\0',sizeof *ses);
177 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
179 if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
180 err("CIOCFSESSION failed");
182 OPENSSL_free(CDATA(ctx)->key);
187 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
188 const unsigned char *key,int klen)
190 if(!dev_crypto_init(CDATA(ctx)))
193 CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
195 assert(ctx->cipher->iv_len <= MAX_HW_IV);
197 memcpy(CDATA(ctx)->key,key,klen);
199 CDATA(ctx)->cipher=cipher;
200 CDATA(ctx)->keylen=klen;
202 if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
204 err("CIOCGSESSION failed");
210 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
211 const unsigned char *in,unsigned int inl)
213 struct crypt_op cryp;
214 unsigned char lb[MAX_HW_IV];
222 memset(&cryp,'\0',sizeof cryp);
223 cryp.ses=CDATA(ctx)->ses;
224 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
227 assert((inl&(ctx->cipher->block_size-1)) == 0);
228 cryp.src=(caddr_t)in;
229 cryp.dst=(caddr_t)out;
231 if(ctx->cipher->iv_len)
232 cryp.iv=(caddr_t)ctx->iv;
235 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
237 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
239 if(errno == EINVAL) /* buffers are misaligned */
245 /* NB: this can only make cinl != inl with stream ciphers */
248 if(((unsigned long)in&3) || cinl != inl)
250 cin=OPENSSL_malloc(cinl);
255 if(((unsigned long)out&3) || cinl != inl)
257 cout=OPENSSL_malloc(cinl);
263 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
265 err("CIOCCRYPT(2) failed");
266 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
273 memcpy(out,cout,inl);
281 err("CIOCCRYPT failed");
288 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
290 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
295 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
296 const unsigned char *key,
297 const unsigned char *iv, int enc)
298 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
300 static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
301 const unsigned char *key,
302 const unsigned char *iv, int enc)
303 { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
310 unsigned char md[EVP_MAX_MD_SIZE];
313 static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
315 if(!dev_crypto_init(&md_data->sess))
321 md_data->sess.mac=mac;
323 if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
325 err("CIOCGSESSION failed");
331 static int dev_crypto_cleanup_digest(MD_DATA *md_data)
333 if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
335 err("CIOCFSESSION failed");
342 /* FIXME: if device can do chained MACs, then don't accumulate */
343 /* FIXME: move accumulation to the framework */
344 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
345 { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
347 static int do_digest(int ses,unsigned char *md,const void *data,int len)
349 struct crypt_op cryp;
350 static unsigned char md5zero[16]=
352 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
353 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
356 /* some cards can't do zero length */
359 memcpy(md,md5zero,16);
363 memset(&cryp,'\0',sizeof cryp);
365 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
367 cryp.src=(caddr_t)data;
368 cryp.dst=(caddr_t)data; // FIXME!!!
369 cryp.mac=(caddr_t)md;
371 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
373 if(errno == EINVAL) /* buffer is misaligned */
377 dcopy=OPENSSL_malloc(len);
378 memcpy(dcopy,data,len);
380 cryp.dst=cryp.src; // FIXME!!!
382 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
384 err("CIOCCRYPT(MAC2) failed");
392 err("CIOCCRYPT(MAC) failed");
402 static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
405 MD_DATA *md_data=ctx->md_data;
407 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
408 return do_digest(md_data->sess.ses,md_data->md,data,len);
410 md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
411 memcpy(md_data->data+md_data->len,data,len);
417 static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
420 MD_DATA *md_data=ctx->md_data;
422 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
424 memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
429 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
430 OPENSSL_free(md_data->data);
438 static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
440 const MD_DATA *from_md=from->md_data;
441 MD_DATA *to_md=to->md_data;
443 // How do we copy sessions?
444 assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
446 to_md->data=OPENSSL_malloc(from_md->len);
447 memcpy(to_md->data,from_md->data,from_md->len);
452 static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
454 return dev_crypto_cleanup_digest(ctx->md_data);
457 /**************************************************************************/
458 /* Here are the moved declarations of the EVP_CIPHER and EVP_MD */
459 /* implementations. They're down here to be within easy editor-distance */
460 /* of the digests and ciphers handler functions. */
462 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
464 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
465 0, dev_crypto_des_ede3_init_key,
467 EVP_CIPHER_set_asn1_iv,
468 EVP_CIPHER_get_asn1_iv,
471 static const EVP_CIPHER r4_cipher=
474 1,16,0, /* FIXME: key should be up to 256 bytes */
475 EVP_CIPH_VARIABLE_LENGTH,
476 dev_crypto_rc4_init_key,
485 static const EVP_MD md5_md=
488 NID_md5WithRSAEncryption,
490 EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
492 dev_crypto_md5_update,
493 dev_crypto_md5_final,
495 dev_crypto_md5_cleanup,
501 /****************************************************************/
502 /* Implement the dev_crypto_[ciphers|digests] handlers here ... */
504 static int cipher_nids[] = {NID_des_ede3_cbc, NID_rc4};
505 static int cipher_nids_num = 2;
506 static int digest_nids[] = {NID_md5};
507 static int digest_nids_num = 1;
509 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
510 const int **nids, int nid)
514 /* We are returning a list of supported nids */
516 return cipher_nids_num;
518 /* We are being asked for a specific cipher */
520 *cipher = &r4_cipher;
521 else if(nid == NID_des_ede3_cbc)
522 *cipher = &dev_crypto_des_ede3_cbc;
531 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
532 const int **nids, int nid)
536 /* We are returning a list of supported nids */
538 return digest_nids_num;
540 /* We are being asked for a specific digest */
551 #endif /* OPENSSL_OPENBSD_DEV_CRYPTO */