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 void ENGINE_load_openbsd_dev_crypto(void)
60 /* This is a NOP unless OPENSSL_OPENBSD_DEV_CRYPTO is defined */
64 #else /* OPENSSL_OPENBSD_DEV_CRYPTO */
71 #include <sys/ioctl.h>
72 #include <openssl/engine.h>
73 #include <openssl/evp.h>
75 /* Maybe this is needed? ... */
76 #include "../evp/evp_locl.h"
78 #include <crypto/cryptodev.h>
80 /****************************************************/
81 /* Declare the normal generic ENGINE stuff here ... */
83 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
84 const int **nids, int nid);
85 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
86 const int **nids, int nid);
88 static const char dev_crypto_id[] = "openbsd_dev_crypto";
89 static const char dev_crypto_name[] = "OpenBSD /dev/crypto";
91 static ENGINE *engine_openbsd_dev_crypto(void)
93 ENGINE *engine=ENGINE_new();
95 if(!ENGINE_set_id(engine, dev_crypto_id) ||
96 !ENGINE_set_name(engine, dev_crypto_name) ||
97 !ENGINE_set_ciphers(engine, dev_crypto_ciphers) ||
98 !ENGINE_set_digests(engine, dev_crypto_digests))
107 void ENGINE_load_openbsd_dev_crypto(void)
109 /* Copied from eng_[openssl|dyn].c */
110 ENGINE *toadd = engine_openbsd_dev_crypto();
117 /******************************************************************************/
118 /* Clip in the stuff from crypto/evp/openbsd_hw.c here. NB: What has changed? */
119 /* I've removed the exposed EVP_*** functions, they're accessed through the */
120 /* "dev_crypto_[ciphers|digests]" handlers. I've also moved the EVP_CIPHER */
121 /* and EVP_MD structures to the bottom where they are close to the handlers */
122 /* that expose them. What should be done? The global data (file-descriptors, */
123 /* etc) should be put into ENGINE's ex_data support, and per-context data */
124 /* (also file-descriptors perhaps) should be put into the contexts. Also code */
125 /* formatting, fprintf statements, and OpenSSL-style error handling should be */
126 /* added (dynamically, like the other ENGINEs). Also, "dynamic" support */
127 /* be added to this ENGINE once it's up and running so that it could be built */
128 /* as a shared-library. What else? device initialisation should take place */
129 /* inside an ENGINE 'init()' handler (and likewise 'finish()'). ciphers and */
130 /* digests won't be used by the framework unless the ENGINE has been */
131 /* successfully initialised (that's one of the things you get for free) so */
132 /* initialisation, including returning failure if device setup fails, can be */
133 /* handled quite cleanly. This could presumably handle the opening (and then */
134 /* closing inside 'finish()') of the 'cryptodev_fd' file-descriptor). */
136 /* longest key supported in hardware */
137 #define MAX_HW_KEY 24
140 #define MD5_DIGEST_LENGTH 16
141 #define MD5_CBLOCK 64
144 static int dev_failed;
146 typedef struct session_op session_op;
148 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
150 static void err(const char *str)
152 fprintf(stderr,"%s: errno %d\n",str,errno);
155 static int dev_crypto_init(session_op *ses)
163 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
169 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
171 err("CRIOGET failed");
179 memset(ses,'\0',sizeof *ses);
184 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
186 fprintf(stderr,"cleanup %d\n",CDATA(ctx)->ses);
187 if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
188 err("CIOCFSESSION failed");
190 OPENSSL_free(CDATA(ctx)->key);
195 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
196 const unsigned char *key,int klen)
198 if(!dev_crypto_init(CDATA(ctx)))
201 CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
203 assert(ctx->cipher->iv_len <= MAX_HW_IV);
205 memcpy(CDATA(ctx)->key,key,klen);
207 CDATA(ctx)->cipher=cipher;
208 CDATA(ctx)->keylen=klen;
210 if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
212 err("CIOCGSESSION failed");
218 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
219 const unsigned char *in,unsigned int inl)
221 struct crypt_op cryp;
222 unsigned char lb[MAX_HW_IV];
230 memset(&cryp,'\0',sizeof cryp);
231 cryp.ses=CDATA(ctx)->ses;
232 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
235 assert((inl&(ctx->cipher->block_size-1)) == 0);
236 cryp.src=(caddr_t)in;
237 cryp.dst=(caddr_t)out;
239 if(ctx->cipher->iv_len)
240 cryp.iv=(caddr_t)ctx->iv;
243 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
245 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
247 if(errno == EINVAL) /* buffers are misaligned */
253 /* NB: this can only make cinl != inl with stream ciphers */
256 if(((unsigned long)in&3) || cinl != inl)
258 cin=OPENSSL_malloc(cinl);
263 if(((unsigned long)out&3) || cinl != inl)
265 cout=OPENSSL_malloc(cinl);
271 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
273 err("CIOCCRYPT(2) failed");
274 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
281 memcpy(out,cout,inl);
289 err("CIOCCRYPT failed");
296 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
298 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
303 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
304 const unsigned char *key,
305 const unsigned char *iv, int enc)
306 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
308 static int dev_crypto_rc4_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_ARC4,key,16); }
318 unsigned char md[EVP_MAX_MD_SIZE];
321 static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
323 if(!dev_crypto_init(&md_data->sess))
329 md_data->sess.mac=mac;
331 if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
333 err("CIOCGSESSION failed");
336 fprintf(stderr,"opened %d\n",md_data->sess.ses);
340 static int dev_crypto_cleanup_digest(MD_DATA *md_data)
342 fprintf(stderr,"cleanup %d\n",md_data->sess.ses);
343 if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
345 err("CIOCFSESSION failed");
352 /* FIXME: if device can do chained MACs, then don't accumulate */
353 /* FIXME: move accumulation to the framework */
354 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
355 { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
357 static int do_digest(int ses,unsigned char *md,const void *data,int len)
359 struct crypt_op cryp;
360 static unsigned char md5zero[16]=
362 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
363 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
366 /* some cards can't do zero length */
369 memcpy(md,md5zero,16);
373 memset(&cryp,'\0',sizeof cryp);
375 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
377 cryp.src=(caddr_t)data;
378 cryp.dst=(caddr_t)data; // FIXME!!!
379 cryp.mac=(caddr_t)md;
381 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
383 if(errno == EINVAL) /* buffer is misaligned */
387 dcopy=OPENSSL_malloc(len);
388 memcpy(dcopy,data,len);
390 cryp.dst=cryp.src; // FIXME!!!
392 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
394 err("CIOCCRYPT(MAC2) failed");
402 err("CIOCCRYPT(MAC) failed");
412 static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
415 MD_DATA *md_data=ctx->md_data;
417 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
418 return do_digest(md_data->sess.ses,md_data->md,data,len);
420 md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
421 memcpy(md_data->data+md_data->len,data,len);
427 static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
430 MD_DATA *md_data=ctx->md_data;
432 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
434 memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
439 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
440 OPENSSL_free(md_data->data);
448 static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
450 const MD_DATA *from_md=from->md_data;
451 MD_DATA *to_md=to->md_data;
453 // How do we copy sessions?
454 assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
456 to_md->data=OPENSSL_malloc(from_md->len);
457 memcpy(to_md->data,from_md->data,from_md->len);
462 static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
464 return dev_crypto_cleanup_digest(ctx->md_data);
467 /**************************************************************************/
468 /* Here are the moved declarations of the EVP_CIPHER and EVP_MD */
469 /* implementations. They're down here to be within easy editor-distance */
470 /* of the digests and ciphers handler functions. */
472 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
474 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
475 0, dev_crypto_des_ede3_init_key,
477 EVP_CIPHER_set_asn1_iv,
478 EVP_CIPHER_get_asn1_iv,
481 static const EVP_CIPHER r4_cipher=
484 1,16,0, /* FIXME: key should be up to 256 bytes */
485 EVP_CIPH_VARIABLE_LENGTH,
486 dev_crypto_rc4_init_key,
495 static const EVP_MD md5_md=
498 NID_md5WithRSAEncryption,
500 EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
502 dev_crypto_md5_update,
503 dev_crypto_md5_final,
505 dev_crypto_md5_cleanup,
511 /****************************************************************/
512 /* Implement the dev_crypto_[ciphers|digests] handlers here ... */
514 static int cipher_nids[] = {NID_des_ede3_cbc, NID_rc4};
515 static int cipher_nids_num = 2;
516 static int digest_nids[] = {NID_md5};
517 static int digest_nids_num = 1;
519 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
520 const int **nids, int nid)
524 /* We are returning a list of supported nids */
526 return cipher_nids_num;
528 /* We are being asked for a specific cipher */
530 *cipher = &r4_cipher;
531 else if(nid == NID_des_ede3_cbc)
532 *cipher = &dev_crypto_des_ede3_cbc;
541 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
542 const int **nids, int nid)
546 /* We are returning a list of supported nids */
548 return digest_nids_num;
550 /* We are being asked for a specific digest */
561 #endif /* OPENSSL_OPENBSD_DEV_CRYPTO */