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 #include <openssl/engine.h>
57 #include <openssl/evp.h>
59 /* Maybe this is needed? ... */
63 #include "../evp/evp_locl.h"
66 #ifndef OPENSSL_OPENBSD_DEV_CRYPTO
68 void ENGINE_load_openbsd_dev_crypto(void)
70 /* This is a NOP unless OPENSSL_OPENBSD_DEV_CRYPTO is defined */
74 #else /* OPENSSL_OPENBSD_DEV_CRYPTO */
81 #include <sys/ioctl.h>
83 #include <crypto/cryptodev.h>
85 /****************************************************/
86 /* Declare the normal generic ENGINE stuff here ... */
88 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
89 const int **nids, int nid);
90 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
91 const int **nids, int nid);
93 static const char dev_crypto_id[] = "openbsd_dev_crypto";
94 static const char dev_crypto_name[] = "OpenBSD /dev/crypto";
96 static ENGINE *engine_openbsd_dev_crypto(void)
98 ENGINE *engine=ENGINE_new();
100 if(!ENGINE_set_id(engine, dev_crypto_id) ||
101 !ENGINE_set_name(engine, dev_crypto_name) ||
102 !ENGINE_set_ciphers(engine, dev_crypto_ciphers) ||
103 !ENGINE_set_digests(engine, dev_crypto_digests))
112 void ENGINE_load_openbsd_dev_crypto(void)
114 /* Copied from eng_[openssl|dyn].c */
115 ENGINE *toadd = engine_openbsd_dev_crypto();
122 /******************************************************************************/
123 /* Clip in the stuff from crypto/evp/openbsd_hw.c here. NB: What has changed? */
124 /* I've removed the exposed EVP_*** functions, they're accessed through the */
125 /* "dev_crypto_[ciphers|digests]" handlers. I've also moved the EVP_CIPHER */
126 /* and EVP_MD structures to the bottom where they are close to the handlers */
127 /* that expose them. What should be done? The global data (file-descriptors, */
128 /* etc) should be put into ENGINE's ex_data support, and per-context data */
129 /* (also file-descriptors perhaps) should be put into the contexts. Also code */
130 /* formatting, fprintf statements, and OpenSSL-style error handling should be */
131 /* added (dynamically, like the other ENGINEs). Also, "dynamic" support */
132 /* be added to this ENGINE once it's up and running so that it could be built */
133 /* as a shared-library. What else? device initialisation should take place */
134 /* inside an ENGINE 'init()' handler (and likewise 'finish()'). ciphers and */
135 /* digests won't be used by the framework unless the ENGINE has been */
136 /* successfully initialised (that's one of the things you get for free) so */
137 /* initialisation, including returning failure if device setup fails, can be */
138 /* handled quite cleanly. This could presumably handle the opening (and then */
139 /* closing inside 'finish()') of the 'cryptodev_fd' file-descriptor). */
141 /* longest key supported in hardware */
142 #define MAX_HW_KEY 24
145 #define MD5_DIGEST_LENGTH 16
146 #define MD5_CBLOCK 64
149 static int dev_failed;
151 typedef struct session_op session_op;
153 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
155 static void err(const char *str)
157 fprintf(stderr,"%s: errno %d\n",str,errno);
160 static int dev_crypto_init(session_op *ses)
168 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
174 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
176 err("CRIOGET failed");
184 memset(ses,'\0',sizeof *ses);
189 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
191 fprintf(stderr,"cleanup %d\n",CDATA(ctx)->ses);
192 if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
193 err("CIOCFSESSION failed");
195 OPENSSL_free(CDATA(ctx)->key);
200 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
201 const unsigned char *key,int klen)
203 if(!dev_crypto_init(CDATA(ctx)))
206 CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
208 assert(ctx->cipher->iv_len <= MAX_HW_IV);
210 memcpy(CDATA(ctx)->key,key,klen);
212 CDATA(ctx)->cipher=cipher;
213 CDATA(ctx)->keylen=klen;
215 if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
217 err("CIOCGSESSION failed");
223 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
224 const unsigned char *in,unsigned int inl)
226 struct crypt_op cryp;
227 unsigned char lb[MAX_HW_IV];
235 memset(&cryp,'\0',sizeof cryp);
236 cryp.ses=CDATA(ctx)->ses;
237 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
240 assert((inl&(ctx->cipher->block_size-1)) == 0);
241 cryp.src=(caddr_t)in;
242 cryp.dst=(caddr_t)out;
244 if(ctx->cipher->iv_len)
245 cryp.iv=(caddr_t)ctx->iv;
248 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
250 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
252 if(errno == EINVAL) /* buffers are misaligned */
258 /* NB: this can only make cinl != inl with stream ciphers */
261 if(((unsigned long)in&3) || cinl != inl)
263 cin=OPENSSL_malloc(cinl);
268 if(((unsigned long)out&3) || cinl != inl)
270 cout=OPENSSL_malloc(cinl);
276 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
278 err("CIOCCRYPT(2) failed");
279 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
286 memcpy(out,cout,inl);
294 err("CIOCCRYPT failed");
301 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
303 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
308 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
309 const unsigned char *key,
310 const unsigned char *iv, int enc)
311 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
313 static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
314 const unsigned char *key,
315 const unsigned char *iv, int enc)
316 { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
323 unsigned char md[EVP_MAX_MD_SIZE];
326 static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
328 if(!dev_crypto_init(&md_data->sess))
334 md_data->sess.mac=mac;
336 if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
338 err("CIOCGSESSION failed");
341 fprintf(stderr,"opened %d\n",md_data->sess.ses);
345 static int dev_crypto_cleanup_digest(MD_DATA *md_data)
347 fprintf(stderr,"cleanup %d\n",md_data->sess.ses);
348 if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
350 err("CIOCFSESSION failed");
357 /* FIXME: if device can do chained MACs, then don't accumulate */
358 /* FIXME: move accumulation to the framework */
359 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
360 { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
362 static int do_digest(int ses,unsigned char *md,const void *data,int len)
364 struct crypt_op cryp;
365 static unsigned char md5zero[16]=
367 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
368 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
371 /* some cards can't do zero length */
374 memcpy(md,md5zero,16);
378 memset(&cryp,'\0',sizeof cryp);
380 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
382 cryp.src=(caddr_t)data;
383 cryp.dst=(caddr_t)data; // FIXME!!!
384 cryp.mac=(caddr_t)md;
386 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
388 if(errno == EINVAL) /* buffer is misaligned */
392 dcopy=OPENSSL_malloc(len);
393 memcpy(dcopy,data,len);
395 cryp.dst=cryp.src; // FIXME!!!
397 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
399 err("CIOCCRYPT(MAC2) failed");
407 err("CIOCCRYPT(MAC) failed");
417 static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
420 MD_DATA *md_data=ctx->md_data;
422 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
423 return do_digest(md_data->sess.ses,md_data->md,data,len);
425 md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
426 memcpy(md_data->data+md_data->len,data,len);
432 static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
435 MD_DATA *md_data=ctx->md_data;
437 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
439 memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
444 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
445 OPENSSL_free(md_data->data);
453 static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
455 const MD_DATA *from_md=from->md_data;
456 MD_DATA *to_md=to->md_data;
458 // How do we copy sessions?
459 assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
461 to_md->data=OPENSSL_malloc(from_md->len);
462 memcpy(to_md->data,from_md->data,from_md->len);
467 static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
469 return dev_crypto_cleanup_digest(ctx->md_data);
472 /**************************************************************************/
473 /* Here are the moved declarations of the EVP_CIPHER and EVP_MD */
474 /* implementations. They're down here to be within easy editor-distance */
475 /* of the digests and ciphers handler functions. */
477 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
479 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
480 0, dev_crypto_des_ede3_init_key,
482 EVP_CIPHER_set_asn1_iv,
483 EVP_CIPHER_get_asn1_iv,
486 static const EVP_CIPHER r4_cipher=
489 1,16,0, /* FIXME: key should be up to 256 bytes */
490 EVP_CIPH_VARIABLE_LENGTH,
491 dev_crypto_rc4_init_key,
500 static const EVP_MD md5_md=
503 NID_md5WithRSAEncryption,
505 EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
507 dev_crypto_md5_update,
508 dev_crypto_md5_final,
510 dev_crypto_md5_cleanup,
516 /****************************************************************/
517 /* Implement the dev_crypto_[ciphers|digests] handlers here ... */
519 static int cipher_nids[] = {NID_des_ede3_cbc, NID_rc4};
520 static int cipher_nids_num = 2;
521 static int digest_nids[] = {NID_md5};
522 static int digest_nids_num = 1;
524 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
525 const int **nids, int nid)
529 /* We are returning a list of supported nids */
531 return cipher_nids_num;
533 /* We are being asked for a specific cipher */
535 *cipher = &r4_cipher;
536 else if(nid == NID_des_ede3_cbc)
537 *cipher = &dev_crypto_des_ede3_cbc;
546 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
547 const int **nids, int nid)
551 /* We are returning a list of supported nids */
553 return digest_nids_num;
555 /* We are being asked for a specific digest */
566 #endif /* OPENSSL_OPENBSD_DEV_CRYPTO */