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"
65 #include <openssl/conf.h>
67 #ifndef OPENSSL_OPENBSD_DEV_CRYPTO
69 void ENGINE_load_openbsd_dev_crypto(void)
71 /* This is a NOP unless OPENSSL_OPENBSD_DEV_CRYPTO is defined */
75 #else /* OPENSSL_OPENBSD_DEV_CRYPTO */
82 #include <sys/ioctl.h>
84 #include <crypto/cryptodev.h>
86 /****************************************************/
87 /* Declare the normal generic ENGINE stuff here ... */
89 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
90 const int **nids, int nid);
91 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
92 const int **nids, int nid);
94 static const char dev_crypto_id[] = "openbsd_dev_crypto";
95 static const char dev_crypto_name[] = "OpenBSD /dev/crypto";
97 static long allow_misaligned;
99 #define DEV_CRYPTO_CMD_ALLOW_MISALIGNED ENGINE_CMD_BASE
100 static const ENGINE_CMD_DEFN dev_crypto_cmd_defns[]=
102 { DEV_CRYPTO_CMD_ALLOW_MISALIGNED,
104 "Permit misaligned data to be used",
105 ENGINE_CMD_FLAG_NUMERIC },
109 static int dev_crypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
113 case DEV_CRYPTO_CMD_ALLOW_MISALIGNED:
115 printf("allow misaligned=%ld\n",allow_misaligned);
122 static ENGINE *engine_openbsd_dev_crypto(void)
124 ENGINE *engine=ENGINE_new();
126 if(!ENGINE_set_id(engine, dev_crypto_id) ||
127 !ENGINE_set_name(engine, dev_crypto_name) ||
128 !ENGINE_set_ciphers(engine, dev_crypto_ciphers) ||
129 !ENGINE_set_digests(engine, dev_crypto_digests) ||
130 !ENGINE_set_ctrl_function(engine, dev_crypto_ctrl) ||
131 !ENGINE_set_cmd_defns(engine, dev_crypto_cmd_defns))
140 void ENGINE_load_openbsd_dev_crypto(void)
142 /* Copied from eng_[openssl|dyn].c */
143 ENGINE *toadd = engine_openbsd_dev_crypto();
150 /******************************************************************************/
151 /* Clip in the stuff from crypto/evp/openbsd_hw.c here. NB: What has changed? */
152 /* I've removed the exposed EVP_*** functions, they're accessed through the */
153 /* "dev_crypto_[ciphers|digests]" handlers. I've also moved the EVP_CIPHER */
154 /* and EVP_MD structures to the bottom where they are close to the handlers */
155 /* that expose them. What should be done? The global data (file-descriptors, */
156 /* etc) should be put into ENGINE's ex_data support, and per-context data */
157 /* (also file-descriptors perhaps) should be put into the contexts. Also code */
158 /* formatting, fprintf statements, and OpenSSL-style error handling should be */
159 /* added (dynamically, like the other ENGINEs). Also, "dynamic" support */
160 /* be added to this ENGINE once it's up and running so that it could be built */
161 /* as a shared-library. What else? device initialisation should take place */
162 /* inside an ENGINE 'init()' handler (and likewise 'finish()'). ciphers and */
163 /* digests won't be used by the framework unless the ENGINE has been */
164 /* successfully initialised (that's one of the things you get for free) so */
165 /* initialisation, including returning failure if device setup fails, can be */
166 /* handled quite cleanly. This could presumably handle the opening (and then */
167 /* closing inside 'finish()') of the 'cryptodev_fd' file-descriptor). */
169 /* longest key supported in hardware */
170 #define MAX_HW_KEY 24
173 #define MD5_DIGEST_LENGTH 16
174 #define MD5_CBLOCK 64
177 static int dev_failed;
179 typedef struct session_op session_op;
181 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
183 static void err(const char *str)
185 fprintf(stderr,"%s: errno %d\n",str,errno);
188 static int dev_crypto_init(session_op *ses)
196 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
202 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
204 err("CRIOGET failed");
212 memset(ses,'\0',sizeof *ses);
217 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
219 fprintf(stderr,"cleanup %d\n",CDATA(ctx)->ses);
220 if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
221 err("CIOCFSESSION failed");
223 OPENSSL_free(CDATA(ctx)->key);
228 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
229 const unsigned char *key,int klen)
231 if(!dev_crypto_init(CDATA(ctx)))
234 CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
236 assert(ctx->cipher->iv_len <= MAX_HW_IV);
238 memcpy(CDATA(ctx)->key,key,klen);
240 CDATA(ctx)->cipher=cipher;
241 CDATA(ctx)->keylen=klen;
243 if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
245 err("CIOCGSESSION failed");
251 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
252 const unsigned char *in,unsigned int inl)
254 struct crypt_op cryp;
255 unsigned char lb[MAX_HW_IV];
263 memset(&cryp,'\0',sizeof cryp);
264 cryp.ses=CDATA(ctx)->ses;
265 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
268 assert((inl&(ctx->cipher->block_size-1)) == 0);
269 cryp.src=(caddr_t)in;
270 cryp.dst=(caddr_t)out;
272 if(ctx->cipher->iv_len)
273 cryp.iv=(caddr_t)ctx->iv;
276 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
278 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
280 if(errno == EINVAL) /* buffers are misaligned */
286 /* NB: this can only make cinl != inl with stream ciphers */
289 if(((unsigned long)in&3) || cinl != inl)
291 cin=OPENSSL_malloc(cinl);
296 if(((unsigned long)out&3) || cinl != inl)
298 cout=OPENSSL_malloc(cinl);
304 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
306 err("CIOCCRYPT(2) failed");
307 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
314 memcpy(out,cout,inl);
322 err("CIOCCRYPT failed");
329 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
331 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
336 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
337 const unsigned char *key,
338 const unsigned char *iv, int enc)
339 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
341 static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
342 const unsigned char *key,
343 const unsigned char *iv, int enc)
344 { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
351 unsigned char md[EVP_MAX_MD_SIZE];
354 static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
356 if(!dev_crypto_init(&md_data->sess))
362 md_data->sess.mac=mac;
364 if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
366 err("CIOCGSESSION failed");
369 fprintf(stderr,"opened %d\n",md_data->sess.ses);
373 static int dev_crypto_cleanup_digest(MD_DATA *md_data)
375 fprintf(stderr,"cleanup %d\n",md_data->sess.ses);
376 if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
378 err("CIOCFSESSION failed");
385 /* FIXME: if device can do chained MACs, then don't accumulate */
386 /* FIXME: move accumulation to the framework */
387 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
388 { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
390 static int do_digest(int ses,unsigned char *md,const void *data,int len)
392 struct crypt_op cryp;
393 static unsigned char md5zero[16]=
395 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
396 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
399 /* some cards can't do zero length */
402 memcpy(md,md5zero,16);
406 memset(&cryp,'\0',sizeof cryp);
408 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
410 cryp.src=(caddr_t)data;
411 cryp.dst=(caddr_t)data; // FIXME!!!
412 cryp.mac=(caddr_t)md;
414 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
416 if(errno == EINVAL && allow_misaligned) /* buffer is misaligned */
420 dcopy=OPENSSL_malloc(len);
421 memcpy(dcopy,data,len);
423 cryp.dst=cryp.src; // FIXME!!!
425 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
427 err("CIOCCRYPT(MAC2) failed");
435 err("CIOCCRYPT(MAC) failed");
445 static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
448 MD_DATA *md_data=ctx->md_data;
450 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
451 return do_digest(md_data->sess.ses,md_data->md,data,len);
453 md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
454 memcpy(md_data->data+md_data->len,data,len);
460 static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
463 MD_DATA *md_data=ctx->md_data;
465 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
467 memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
472 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
473 OPENSSL_free(md_data->data);
481 static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
483 const MD_DATA *from_md=from->md_data;
484 MD_DATA *to_md=to->md_data;
486 // How do we copy sessions?
487 assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
489 to_md->data=OPENSSL_malloc(from_md->len);
490 memcpy(to_md->data,from_md->data,from_md->len);
495 static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
497 return dev_crypto_cleanup_digest(ctx->md_data);
500 /**************************************************************************/
501 /* Here are the moved declarations of the EVP_CIPHER and EVP_MD */
502 /* implementations. They're down here to be within easy editor-distance */
503 /* of the digests and ciphers handler functions. */
505 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
507 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
508 0, dev_crypto_des_ede3_init_key,
510 EVP_CIPHER_set_asn1_iv,
511 EVP_CIPHER_get_asn1_iv,
514 static const EVP_CIPHER r4_cipher=
517 1,16,0, /* FIXME: key should be up to 256 bytes */
518 EVP_CIPH_VARIABLE_LENGTH,
519 dev_crypto_rc4_init_key,
528 static const EVP_MD md5_md=
531 NID_md5WithRSAEncryption,
533 EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
535 dev_crypto_md5_update,
536 dev_crypto_md5_final,
538 dev_crypto_md5_cleanup,
544 /****************************************************************/
545 /* Implement the dev_crypto_[ciphers|digests] handlers here ... */
547 static int cipher_nids[] = {NID_des_ede3_cbc, NID_rc4};
548 static int cipher_nids_num = 2;
549 static int digest_nids[] = {NID_md5};
550 static int digest_nids_num = 1;
552 static int dev_crypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
553 const int **nids, int nid)
557 /* We are returning a list of supported nids */
559 return cipher_nids_num;
561 /* We are being asked for a specific cipher */
563 *cipher = &r4_cipher;
564 else if(nid == NID_des_ede3_cbc)
565 *cipher = &dev_crypto_des_ede3_cbc;
574 static int dev_crypto_digests(ENGINE *e, const EVP_MD **digest,
575 const int **nids, int nid)
579 /* We are returning a list of supported nids */
581 return digest_nids_num;
583 /* We are being asked for a specific digest */
594 #endif /* OPENSSL_OPENBSD_DEV_CRYPTO */