57 static void *dummy=&dummy;
62 #ifdef OPENSSL_OPENBSD_DEV_CRYPTO
67 #include <sys/ioctl.h>
68 #include <crypto/cryptodev.h>
76 #define MD5_DIGEST_LENGTH 16
80 static int dev_failed;
82 typedef struct session_op session_op;
84 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
86 static void err(
const char *str)
88 fprintf(stderr,
"%s: errno %d\n",str,errno);
91 static int dev_crypto_init(session_op *ses)
99 if ((cryptodev_fd=open(
"/dev/crypto",O_RDWR,0)) < 0)
105 if (
ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
107 err(
"CRIOGET failed");
115 memset(ses,
'\0',
sizeof *ses);
122 if(
ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
123 err(
"CIOCFSESSION failed");
131 const unsigned char *
key,
int klen)
133 if(!dev_crypto_init(CDATA(ctx)))
140 memcpy(CDATA(ctx)->key,key,klen);
142 CDATA(ctx)->cipher=cipher;
143 CDATA(ctx)->keylen=klen;
145 if (
ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
147 err(
"CIOCGSESSION failed");
153 static int dev_crypto_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
154 const unsigned char *in,
unsigned int inl)
156 struct crypt_op cryp;
157 unsigned char lb[MAX_HW_IV];
165 memset(&cryp,
'\0',
sizeof cryp);
166 cryp.ses=CDATA(ctx)->ses;
167 cryp.op=ctx->
encrypt ? COP_ENCRYPT : COP_DECRYPT;
171 cryp.src=(caddr_t)in;
172 cryp.dst=(caddr_t)out;
175 cryp.iv=(caddr_t)ctx->
iv;
180 if(
ioctl(fd, CIOCCRYPT, &cryp) == -1)
191 if(((
unsigned long)in&3) || cinl != inl)
198 if(((
unsigned long)out&3) || cinl != inl)
206 if(
ioctl(fd, CIOCCRYPT, &cryp) == -1)
208 err(
"CIOCCRYPT(2) failed");
209 printf(
"src=%p dst=%p\n",cryp.src,cryp.dst);
216 memcpy(out,cout,inl);
224 err(
"CIOCCRYPT failed");
239 const unsigned char *key,
240 const unsigned char *iv,
int enc)
241 {
return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
243 #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
246 0, dev_crypto_des_ede3_init_key,
253 const
unsigned char *key,
254 const
unsigned char *iv,
int enc)
255 {
return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
262 dev_crypto_rc4_init_key,
272 {
return &r4_cipher; }
282 static int dev_crypto_init_digest(MD_DATA *md_data,
int mac)
284 if(!dev_crypto_init(&md_data->sess))
290 md_data->sess.mac=mac;
292 if (
ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
294 err(
"CIOCGSESSION failed");
300 static int dev_crypto_cleanup_digest(MD_DATA *md_data)
302 if (
ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
304 err(
"CIOCFSESSION failed");
313 static int dev_crypto_md5_init(
EVP_MD_CTX *ctx)
314 {
return dev_crypto_init_digest(ctx->
md_data,CRYPTO_MD5); }
316 static int do_digest(
int ses,
unsigned char *md,
const void *
data,
int len)
318 struct crypt_op cryp;
319 static unsigned char md5zero[16]=
321 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
322 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
328 memcpy(md,md5zero,16);
332 memset(&cryp,
'\0',
sizeof cryp);
336 cryp.src=(caddr_t)data;
337 cryp.dst=(caddr_t)data;
338 cryp.mac=(caddr_t)md;
340 if(
ioctl(fd, CIOCCRYPT, &cryp) == -1)
347 memcpy(dcopy,data,len);
351 if(
ioctl(fd, CIOCCRYPT, &cryp) == -1)
353 err(
"CIOCCRYPT(MAC2) failed");
361 err(
"CIOCCRYPT(MAC) failed");
371 static int dev_crypto_md5_update(
EVP_MD_CTX *ctx,
const void *data,
377 return do_digest(md_data->sess.ses,md_data->md,data,len);
380 memcpy(md_data->data+md_data->len,data,len);
386 static int dev_crypto_md5_final(
EVP_MD_CTX *ctx,
unsigned char *md)
398 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
409 const MD_DATA *from_md=from->
md_data;
416 memcpy(to_md->data,from_md->data,from_md->len);
421 static int dev_crypto_md5_cleanup(
EVP_MD_CTX *ctx)
423 return dev_crypto_cleanup_digest(ctx->
md_data);
426 static const EVP_MD md5_md=
433 dev_crypto_md5_update,
434 dev_crypto_md5_final,
436 dev_crypto_md5_cleanup,
442 const EVP_MD *EVP_dev_crypto_md5(
void)