load cryptodev if HAVE_CRYPTODEV is set too
[openssl.git] / crypto / engine / eng_cryptodev.c
1 /*
2  * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3  * Copyright (c) 2002 Theo de Raadt
4  * Copyright (c) 2002 Markus Friedl
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    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 the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28
29 #include <openssl/objects.h>
30 #include <openssl/engine.h>
31 #include <openssl/evp.h>
32 #include <openssl/bn.h>
33
34 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
35         (defined(OpenBSD) || defined(__FreeBSD__))
36 #include <sys/param.h>
37 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
38 #  define HAVE_CRYPTODEV
39 # endif
40 # if (OpenBSD >= 200110)
41 #  define HAVE_SYSLOG_R
42 # endif
43 #endif
44
45 #ifndef HAVE_CRYPTODEV
46
47 void
48 ENGINE_load_cryptodev(void)
49 {
50         /* This is a NOP on platforms without /dev/crypto */
51         return;
52 }
53
54 #else 
55  
56 #include <sys/types.h>
57 #include <crypto/cryptodev.h>
58 #include <crypto/dh/dh.h>
59 #include <crypto/dsa/dsa.h>
60 #include <crypto/err/err.h>
61 #include <crypto/rsa/rsa.h>
62 #include <sys/ioctl.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <unistd.h>
66 #include <fcntl.h>
67 #include <stdarg.h>
68 #include <syslog.h>
69 #include <errno.h>
70 #include <string.h>
71
72 struct dev_crypto_state {
73         struct session_op d_sess;
74         int d_fd;
75
76 #ifdef USE_CRYPTODEV_DIGESTS
77         char dummy_mac_key[HASH_MAX_LEN];
78
79         unsigned char digest_res[HASH_MAX_LEN];
80         char *mac_data;
81         int mac_len;
82
83         int copy;
84 #endif
85 };
86
87 static u_int32_t cryptodev_asymfeat = 0;
88
89 static int get_asym_dev_crypto(void);
90 static int open_dev_crypto(void);
91 static int get_dev_crypto(void);
92 static int get_cryptodev_ciphers(const int **cnids);
93 /*static int get_cryptodev_digests(const int **cnids);*/
94 static int cryptodev_usable_ciphers(const int **nids);
95 static int cryptodev_usable_digests(const int **nids);
96 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
97     const unsigned char *in, size_t inl);
98 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
99     const unsigned char *iv, int enc);
100 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
101 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
102     const int **nids, int nid);
103 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
104     const int **nids, int nid);
105 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
106 static int crparam2bn(struct crparam *crp, BIGNUM *a);
107 static void zapparams(struct crypt_kop *kop);
108 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
109     int slen, BIGNUM *s);
110
111 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
112     const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
113 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
114     RSA *rsa, BN_CTX *ctx);
115 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
116 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
117     const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
118 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
119     BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
120     BN_CTX *ctx, BN_MONT_CTX *mont);
121 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
122     int dlen, DSA *dsa);
123 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
124     DSA_SIG *sig, DSA *dsa);
125 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
126     const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
127     BN_MONT_CTX *m_ctx);
128 static int cryptodev_dh_compute_key(unsigned char *key,
129     const BIGNUM *pub_key, DH *dh);
130 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
131     void (*f)());
132 void ENGINE_load_cryptodev(void);
133
134 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
135         { 0, NULL, NULL, 0 }
136 };
137
138 static struct {
139         int     id;
140         int     nid;
141         int     ivmax;
142         int     keylen;
143 } ciphers[] = {
144         { CRYPTO_ARC4,                  NID_rc4,                0,      16, },
145         { CRYPTO_DES_CBC,               NID_des_cbc,            8,       8, },
146         { CRYPTO_3DES_CBC,              NID_des_ede3_cbc,       8,      24, },
147         { CRYPTO_AES_CBC,               NID_aes_128_cbc,        16,     16, },
148         { CRYPTO_AES_CBC,               NID_aes_192_cbc,        16,     24, },
149         { CRYPTO_AES_CBC,               NID_aes_256_cbc,        16,     32, },
150         { CRYPTO_BLF_CBC,               NID_bf_cbc,             8,      16, },
151         { CRYPTO_CAST_CBC,              NID_cast5_cbc,          8,      16, },
152         { CRYPTO_SKIPJACK_CBC,          NID_undef,              0,       0, },
153         { 0,                            NID_undef,              0,       0, },
154 };
155
156 #if 0
157 static struct {
158         int     id;
159         int     nid;
160         int     keylen;
161 } digests[] = {
162         { CRYPTO_MD5_HMAC,              NID_hmacWithMD5,        16},
163         { CRYPTO_SHA1_HMAC,             NID_hmacWithSHA1,       20},
164         { CRYPTO_RIPEMD160_HMAC,        NID_ripemd160,          16/*?*/},
165         { CRYPTO_MD5_KPDK,              NID_undef,              0},
166         { CRYPTO_SHA1_KPDK,             NID_undef,              0},
167         { CRYPTO_MD5,                   NID_md5,                16},
168         { CRYPTO_SHA1,                  NID_sha1,               20},
169         { 0,                            NID_undef,              0},
170 };
171 #endif
172
173 /*
174  * Return a fd if /dev/crypto seems usable, 0 otherwise.
175  */
176 static int
177 open_dev_crypto(void)
178 {
179         static int fd = -1;
180
181         if (fd == -1) {
182                 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
183                         return (-1);
184                 /* close on exec */
185                 if (fcntl(fd, F_SETFD, 1) == -1) {
186                         close(fd);
187                         fd = -1;
188                         return (-1);
189                 }
190         }
191         return (fd);
192 }
193
194 static int
195 get_dev_crypto(void)
196 {
197         int fd, retfd;
198
199         if ((fd = open_dev_crypto()) == -1)
200                 return (-1);
201         if (ioctl(fd, CRIOGET, &retfd) == -1)
202                 return (-1);
203
204         /* close on exec */
205         if (fcntl(retfd, F_SETFD, 1) == -1) {
206                 close(retfd);
207                 return (-1);
208         }
209         return (retfd);
210 }
211
212 /* Caching version for asym operations */
213 static int
214 get_asym_dev_crypto(void)
215 {
216         static int fd = -1;
217
218         if (fd == -1)
219                 fd = get_dev_crypto();
220         return fd;
221 }
222
223 /*
224  * Find out what ciphers /dev/crypto will let us have a session for.
225  * XXX note, that some of these openssl doesn't deal with yet!
226  * returning them here is harmless, as long as we return NULL
227  * when asked for a handler in the cryptodev_engine_ciphers routine
228  */
229 static int
230 get_cryptodev_ciphers(const int **cnids)
231 {
232         static int nids[CRYPTO_ALGORITHM_MAX];
233         struct session_op sess;
234         int fd, i, count = 0;
235
236         if ((fd = get_dev_crypto()) < 0) {
237                 *cnids = NULL;
238                 return (0);
239         }
240         memset(&sess, 0, sizeof(sess));
241         sess.key = (caddr_t)"123456789abcdefghijklmno";
242
243         for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
244                 if (ciphers[i].nid == NID_undef)
245                         continue;
246                 sess.cipher = ciphers[i].id;
247                 sess.keylen = ciphers[i].keylen;
248                 sess.mac = 0;
249                 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
250                     ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
251                         nids[count++] = ciphers[i].nid;
252         }
253         close(fd);
254
255         if (count > 0)
256                 *cnids = nids;
257         else
258                 *cnids = NULL;
259         return (count);
260 }
261
262 /*
263  * Find out what digests /dev/crypto will let us have a session for.
264  * XXX note, that some of these openssl doesn't deal with yet!
265  * returning them here is harmless, as long as we return NULL
266  * when asked for a handler in the cryptodev_engine_digests routine
267  */
268 #if 0  /* not (yet?) used */
269 static int
270 get_cryptodev_digests(const int **cnids)
271 {
272         static int nids[CRYPTO_ALGORITHM_MAX];
273         struct session_op sess;
274         int fd, i, count = 0;
275
276         if ((fd = get_dev_crypto()) < 0) {
277                 *cnids = NULL;
278                 return (0);
279         }
280         memset(&sess, 0, sizeof(sess));
281         sess.mackey = (caddr_t)"123456789abcdefghijklmno";
282         for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
283                 if (digests[i].nid == NID_undef)
284                         continue;
285                 sess.mac = digests[i].id;
286                 sess.mackeylen = digests[i].keylen;
287                 sess.cipher = 0;
288                 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
289                     ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
290                         nids[count++] = digests[i].nid;
291         }
292         close(fd);
293
294         if (count > 0)
295                 *cnids = nids;
296         else
297                 *cnids = NULL;
298         return (count);
299 }
300 #endif  /* 0 */
301
302 /*
303  * Find the useable ciphers|digests from dev/crypto - this is the first
304  * thing called by the engine init crud which determines what it
305  * can use for ciphers from this engine. We want to return
306  * only what we can do, anythine else is handled by software.
307  *
308  * If we can't initialize the device to do anything useful for
309  * any reason, we want to return a NULL array, and 0 length,
310  * which forces everything to be done is software. By putting
311  * the initalization of the device in here, we ensure we can
312  * use this engine as the default, and if for whatever reason
313  * /dev/crypto won't do what we want it will just be done in
314  * software
315  *
316  * This can (should) be greatly expanded to perhaps take into
317  * account speed of the device, and what we want to do.
318  * (although the disabling of particular alg's could be controlled
319  * by the device driver with sysctl's.) - this is where we
320  * want most of the decisions made about what we actually want
321  * to use from /dev/crypto.
322  */
323 static int
324 cryptodev_usable_ciphers(const int **nids)
325 {
326         return (get_cryptodev_ciphers(nids));
327 }
328
329 static int
330 cryptodev_usable_digests(const int **nids)
331 {
332 #ifdef USE_CRYPTODEV_DIGESTS
333         return (get_cryptodev_digests(nids));
334 #else
335         /*
336          * XXXX just disable all digests for now, because it sucks.
337          * we need a better way to decide this - i.e. I may not
338          * want digests on slow cards like hifn on fast machines,
339          * but might want them on slow or loaded machines, etc.
340          * will also want them when using crypto cards that don't
341          * suck moose gonads - would be nice to be able to decide something
342          * as reasonable default without having hackery that's card dependent.
343          * of course, the default should probably be just do everything,
344          * with perhaps a sysctl to turn algoritms off (or have them off
345          * by default) on cards that generally suck like the hifn.
346          */
347         *nids = NULL;
348         return (0);
349 #endif
350 }
351
352 static int
353 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
354     const unsigned char *in, size_t inl)
355 {
356         struct crypt_op cryp;
357         struct dev_crypto_state *state = ctx->cipher_data;
358         struct session_op *sess = &state->d_sess;
359         const void *iiv;
360         unsigned char save_iv[EVP_MAX_IV_LENGTH];
361
362         if (state->d_fd < 0)
363                 return (0);
364         if (!inl)
365                 return (1);
366         if ((inl % ctx->cipher->block_size) != 0)
367                 return (0);
368
369         memset(&cryp, 0, sizeof(cryp));
370
371         cryp.ses = sess->ses;
372         cryp.flags = 0;
373         cryp.len = inl;
374         cryp.src = (caddr_t) in;
375         cryp.dst = (caddr_t) out;
376         cryp.mac = 0;
377
378         cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
379
380         if (ctx->cipher->iv_len) {
381                 cryp.iv = (caddr_t) ctx->iv;
382                 if (!ctx->encrypt) {
383                         iiv = in + inl - ctx->cipher->iv_len;
384                         memcpy(save_iv, iiv, ctx->cipher->iv_len);
385                 }
386         } else
387                 cryp.iv = NULL;
388
389         if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
390                 /* XXX need better errror handling
391                  * this can fail for a number of different reasons.
392                  */
393                 return (0);
394         }
395
396         if (ctx->cipher->iv_len) {
397                 if (ctx->encrypt)
398                         iiv = out + inl - ctx->cipher->iv_len;
399                 else
400                         iiv = save_iv;
401                 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
402         }
403         return (1);
404 }
405
406 static int
407 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
408     const unsigned char *iv, int enc)
409 {
410         struct dev_crypto_state *state = ctx->cipher_data;
411         struct session_op *sess = &state->d_sess;
412         int cipher = -1, i;
413
414         for (i = 0; ciphers[i].id; i++)
415                 if (ctx->cipher->nid == ciphers[i].nid &&
416                     ctx->cipher->iv_len <= ciphers[i].ivmax &&
417                     ctx->key_len == ciphers[i].keylen) {
418                         cipher = ciphers[i].id;
419                         break;
420                 }
421
422         if (!ciphers[i].id) {
423                 state->d_fd = -1;
424                 return (0);
425         }
426
427         memset(sess, 0, sizeof(struct session_op));
428
429         if ((state->d_fd = get_dev_crypto()) < 0)
430                 return (0);
431
432         sess->key = (caddr_t)key;
433         sess->keylen = ctx->key_len;
434         sess->cipher = cipher;
435
436         if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
437                 close(state->d_fd);
438                 state->d_fd = -1;
439                 return (0);
440         }
441         return (1);
442 }
443
444 /*
445  * free anything we allocated earlier when initting a
446  * session, and close the session.
447  */
448 static int
449 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
450 {
451         int ret = 0;
452         struct dev_crypto_state *state = ctx->cipher_data;
453         struct session_op *sess = &state->d_sess;
454
455         if (state->d_fd < 0)
456                 return (0);
457
458         /* XXX if this ioctl fails, someting's wrong. the invoker
459          * may have called us with a bogus ctx, or we could
460          * have a device that for whatever reason just doesn't
461          * want to play ball - it's not clear what's right
462          * here - should this be an error? should it just
463          * increase a counter, hmm. For right now, we return
464          * 0 - I don't believe that to be "right". we could
465          * call the gorpy openssl lib error handlers that
466          * print messages to users of the library. hmm..
467          */
468
469         if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
470                 ret = 0;
471         } else {
472                 ret = 1;
473         }
474         close(state->d_fd);
475         state->d_fd = -1;
476
477         return (ret);
478 }
479
480 /*
481  * libcrypto EVP stuff - this is how we get wired to EVP so the engine
482  * gets called when libcrypto requests a cipher NID.
483  */
484
485 /* RC4 */
486 const EVP_CIPHER cryptodev_rc4 = {
487         NID_rc4,
488         1, 16, 0,
489         EVP_CIPH_VARIABLE_LENGTH,
490         cryptodev_init_key,
491         cryptodev_cipher,
492         cryptodev_cleanup,
493         sizeof(struct dev_crypto_state),
494         NULL,
495         NULL,
496         NULL
497 };
498
499 /* DES CBC EVP */
500 const EVP_CIPHER cryptodev_des_cbc = {
501         NID_des_cbc,
502         8, 8, 8,
503         EVP_CIPH_CBC_MODE,
504         cryptodev_init_key,
505         cryptodev_cipher,
506         cryptodev_cleanup,
507         sizeof(struct dev_crypto_state),
508         EVP_CIPHER_set_asn1_iv,
509         EVP_CIPHER_get_asn1_iv,
510         NULL
511 };
512
513 /* 3DES CBC EVP */
514 const EVP_CIPHER cryptodev_3des_cbc = {
515         NID_des_ede3_cbc,
516         8, 24, 8,
517         EVP_CIPH_CBC_MODE,
518         cryptodev_init_key,
519         cryptodev_cipher,
520         cryptodev_cleanup,
521         sizeof(struct dev_crypto_state),
522         EVP_CIPHER_set_asn1_iv,
523         EVP_CIPHER_get_asn1_iv,
524         NULL
525 };
526
527 const EVP_CIPHER cryptodev_bf_cbc = {
528         NID_bf_cbc,
529         8, 16, 8,
530         EVP_CIPH_CBC_MODE,
531         cryptodev_init_key,
532         cryptodev_cipher,
533         cryptodev_cleanup,
534         sizeof(struct dev_crypto_state),
535         EVP_CIPHER_set_asn1_iv,
536         EVP_CIPHER_get_asn1_iv,
537         NULL
538 };
539
540 const EVP_CIPHER cryptodev_cast_cbc = {
541         NID_cast5_cbc,
542         8, 16, 8,
543         EVP_CIPH_CBC_MODE,
544         cryptodev_init_key,
545         cryptodev_cipher,
546         cryptodev_cleanup,
547         sizeof(struct dev_crypto_state),
548         EVP_CIPHER_set_asn1_iv,
549         EVP_CIPHER_get_asn1_iv,
550         NULL
551 };
552
553 const EVP_CIPHER cryptodev_aes_cbc = {
554         NID_aes_128_cbc,
555         16, 16, 16,
556         EVP_CIPH_CBC_MODE,
557         cryptodev_init_key,
558         cryptodev_cipher,
559         cryptodev_cleanup,
560         sizeof(struct dev_crypto_state),
561         EVP_CIPHER_set_asn1_iv,
562         EVP_CIPHER_get_asn1_iv,
563         NULL
564 };
565
566 const EVP_CIPHER cryptodev_aes_192_cbc = {
567         NID_aes_192_cbc,
568         16, 24, 16,
569         EVP_CIPH_CBC_MODE,
570         cryptodev_init_key,
571         cryptodev_cipher,
572         cryptodev_cleanup,
573         sizeof(struct dev_crypto_state),
574         EVP_CIPHER_set_asn1_iv,
575         EVP_CIPHER_get_asn1_iv,
576         NULL
577 };
578
579 const EVP_CIPHER cryptodev_aes_256_cbc = {
580         NID_aes_256_cbc,
581         16, 32, 16,
582         EVP_CIPH_CBC_MODE,
583         cryptodev_init_key,
584         cryptodev_cipher,
585         cryptodev_cleanup,
586         sizeof(struct dev_crypto_state),
587         EVP_CIPHER_set_asn1_iv,
588         EVP_CIPHER_get_asn1_iv,
589         NULL
590 };
591
592 /*
593  * Registered by the ENGINE when used to find out how to deal with
594  * a particular NID in the ENGINE. this says what we'll do at the
595  * top level - note, that list is restricted by what we answer with
596  */
597 static int
598 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
599     const int **nids, int nid)
600 {
601         if (!cipher)
602                 return (cryptodev_usable_ciphers(nids));
603
604         switch (nid) {
605         case NID_rc4:
606                 *cipher = &cryptodev_rc4;
607                 break;
608         case NID_des_ede3_cbc:
609                 *cipher = &cryptodev_3des_cbc;
610                 break;
611         case NID_des_cbc:
612                 *cipher = &cryptodev_des_cbc;
613                 break;
614         case NID_bf_cbc:
615                 *cipher = &cryptodev_bf_cbc;
616                 break;
617         case NID_cast5_cbc:
618                 *cipher = &cryptodev_cast_cbc;
619                 break;
620         case NID_aes_128_cbc:
621                 *cipher = &cryptodev_aes_cbc;
622                 break;
623         case NID_aes_192_cbc:
624                 *cipher = &cryptodev_aes_192_cbc;
625                 break;
626         case NID_aes_256_cbc:
627                 *cipher = &cryptodev_aes_256_cbc;
628                 break;
629         default:
630                 *cipher = NULL;
631                 break;
632         }
633         return (*cipher != NULL);
634 }
635
636
637 #ifdef USE_CRYPTODEV_DIGESTS
638
639 /* convert digest type to cryptodev */
640 static int
641 digest_nid_to_cryptodev(int nid)
642 {
643         int i;
644
645         for (i = 0; digests[i].id; i++)
646                 if (digests[i].nid == nid)
647                         return (digests[i].id);
648         return (0);
649 }
650
651
652 static int
653 digest_key_length(int nid)
654 {
655         int i;
656
657         for (i = 0; digests[i].id; i++)
658                 if (digests[i].nid == nid)
659                         return digests[i].keylen;
660         return (0);
661 }
662
663
664 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
665 {
666         struct dev_crypto_state *state = ctx->md_data;
667         struct session_op *sess = &state->d_sess;
668         int digest;
669
670         if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
671                 printf("cryptodev_digest_init: Can't get digest \n");
672                 return (0);
673         }
674
675         memset(state, 0, sizeof(struct dev_crypto_state));
676
677         if ((state->d_fd = get_dev_crypto()) < 0) {
678                 printf("cryptodev_digest_init: Can't get Dev \n");
679                 return (0);
680         }
681
682         sess->mackey = state->dummy_mac_key;
683         sess->mackeylen = digest_key_length(ctx->digest->type);
684         sess->mac = digest;
685
686         if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
687                 close(state->d_fd);
688                 state->d_fd = -1;
689                 printf("cryptodev_digest_init: Open session failed\n");
690                 return (0);
691         }
692
693         return (1);
694 }
695
696 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
697                 size_t count)
698 {
699         struct crypt_op cryp;
700         struct dev_crypto_state *state = ctx->md_data;
701         struct session_op *sess = &state->d_sess;
702
703         if (!data || state->d_fd < 0) {
704                 printf("cryptodev_digest_update: illegal inputs \n");
705                 return (0);
706         }
707
708         if (!count) {
709                 return (0);
710         }
711
712         if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
713                 /* if application doesn't support one buffer */
714                 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
715
716                 if (!state->mac_data) {
717                         printf("cryptodev_digest_update: realloc failed\n");
718                         return (0);
719                 }
720
721                 memcpy(state->mac_data + state->mac_len, data, count);
722                 state->mac_len += count;
723         
724                 return (1);
725         }
726
727         memset(&cryp, 0, sizeof(cryp));
728
729         cryp.ses = sess->ses;
730         cryp.flags = 0;
731         cryp.len = count;
732         cryp.src = (caddr_t) data;
733         cryp.dst = NULL;
734         cryp.mac = (caddr_t) state->digest_res;
735         if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
736                 printf("cryptodev_digest_update: digest failed\n");
737                 return (0);
738         }
739         return (1);
740 }
741
742
743 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
744 {
745         struct crypt_op cryp;
746         struct dev_crypto_state *state = ctx->md_data;
747         struct session_op *sess = &state->d_sess;
748
749         int ret = 1;
750
751         if (!md || state->d_fd < 0) {
752                 printf("cryptodev_digest_final: illegal input\n");
753                 return(0);
754         }
755
756         if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
757                 /* if application doesn't support one buffer */
758                 memset(&cryp, 0, sizeof(cryp));
759
760                 cryp.ses = sess->ses;
761                 cryp.flags = 0;
762                 cryp.len = state->mac_len;
763                 cryp.src = state->mac_data;
764                 cryp.dst = NULL;
765                 cryp.mac = (caddr_t)md;
766
767                 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
768                         printf("cryptodev_digest_final: digest failed\n");
769                         return (0);
770                 }
771
772                 return 1;
773         }
774
775         memcpy(md, state->digest_res, ctx->digest->md_size);
776
777         return (ret);
778 }
779
780
781 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
782 {
783         int ret = 1;
784         struct dev_crypto_state *state = ctx->md_data;
785         struct session_op *sess = &state->d_sess;
786
787         if (state->d_fd < 0) {
788                 printf("cryptodev_digest_cleanup: illegal input\n");
789                 return (0);
790         }
791
792         if (state->mac_data) {
793                 OPENSSL_free(state->mac_data);
794                 state->mac_data = NULL;
795                 state->mac_len = 0;
796         }
797
798         if (state->copy)
799                 return 1;
800
801         if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
802                 printf("cryptodev_digest_cleanup: failed to close session\n");
803                 ret = 0;
804         } else {
805                 ret = 1;
806         }
807         close(state->d_fd);     
808         state->d_fd = -1;
809
810         return (ret);
811 }
812
813 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
814 {
815         struct dev_crypto_state *fstate = from->md_data;
816         struct dev_crypto_state *dstate = to->md_data;
817
818         memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
819
820         if (fstate->mac_len != 0) {
821                 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
822                 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
823         }
824
825         dstate->copy = 1;
826
827         return 1;
828 }
829
830
831 const EVP_MD cryptodev_sha1 = {
832         NID_sha1,
833         NID_undef, 
834         SHA_DIGEST_LENGTH, 
835         EVP_MD_FLAG_ONESHOT,
836         cryptodev_digest_init,
837         cryptodev_digest_update,
838         cryptodev_digest_final,
839         cryptodev_digest_copy,
840         cryptodev_digest_cleanup,
841         EVP_PKEY_NULL_method,
842         SHA_CBLOCK,
843         sizeof(struct dev_crypto_state),
844 };
845
846 const EVP_MD cryptodev_md5 = {
847         NID_md5,
848         NID_undef, 
849         16 /* MD5_DIGEST_LENGTH */, 
850         EVP_MD_FLAG_ONESHOT,
851         cryptodev_digest_init,
852         cryptodev_digest_update,
853         cryptodev_digest_final,
854         cryptodev_digest_copy,
855         cryptodev_digest_cleanup,
856         EVP_PKEY_NULL_method,
857         64 /* MD5_CBLOCK */,
858         sizeof(struct dev_crypto_state),
859 };
860
861 #endif /* USE_CRYPTODEV_DIGESTS */
862
863
864 static int
865 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
866     const int **nids, int nid)
867 {
868         if (!digest)
869                 return (cryptodev_usable_digests(nids));
870
871         switch (nid) {
872 #ifdef USE_CRYPTODEV_DIGESTS
873         case NID_md5:
874                 *digest = &cryptodev_md5; 
875                 break;
876         case NID_sha1:
877                 *digest = &cryptodev_sha1;
878                 break;
879         default:
880 #endif /* USE_CRYPTODEV_DIGESTS */
881                 *digest = NULL;
882                 break;
883         }
884         return (*digest != NULL);
885 }
886
887 /*
888  * Convert a BIGNUM to the representation that /dev/crypto needs.
889  * Upon completion of use, the caller is responsible for freeing
890  * crp->crp_p.
891  */
892 static int
893 bn2crparam(const BIGNUM *a, struct crparam *crp)
894 {
895         int i, j, k;
896         ssize_t bytes, bits;
897         u_char *b;
898
899         crp->crp_p = NULL;
900         crp->crp_nbits = 0;
901
902         bits = BN_num_bits(a);
903         bytes = (bits + 7) / 8;
904
905         b = malloc(bytes);
906         if (b == NULL)
907                 return (1);
908         memset(b, 0, bytes);
909
910         crp->crp_p = (caddr_t) b;
911         crp->crp_nbits = bits;
912
913         for (i = 0, j = 0; i < a->top; i++) {
914                 for (k = 0; k < BN_BITS2 / 8; k++) {
915                         if ((j + k) >= bytes)
916                                 return (0);
917                         b[j + k] = a->d[i] >> (k * 8);
918                 }
919                 j += BN_BITS2 / 8;
920         }
921         return (0);
922 }
923
924 /* Convert a /dev/crypto parameter to a BIGNUM */
925 static int
926 crparam2bn(struct crparam *crp, BIGNUM *a)
927 {
928         u_int8_t *pd;
929         int i, bytes;
930
931         bytes = (crp->crp_nbits + 7) / 8;
932
933         if (bytes == 0)
934                 return (-1);
935
936         if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
937                 return (-1);
938
939         for (i = 0; i < bytes; i++)
940                 pd[i] = crp->crp_p[bytes - i - 1];
941
942         BN_bin2bn(pd, bytes, a);
943         free(pd);
944
945         return (0);
946 }
947
948 static void
949 zapparams(struct crypt_kop *kop)
950 {
951         int i;
952
953         for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
954                 if (kop->crk_param[i].crp_p)
955                         free(kop->crk_param[i].crp_p);
956                 kop->crk_param[i].crp_p = NULL;
957                 kop->crk_param[i].crp_nbits = 0;
958         }
959 }
960
961 static int
962 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
963 {
964         int fd, ret = -1;
965
966         if ((fd = get_asym_dev_crypto()) < 0)
967                 return (ret);
968
969         if (r) {
970                 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
971                 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
972                 kop->crk_oparams++;
973         }
974         if (s) {
975                 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
976                 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
977                 kop->crk_oparams++;
978         }
979
980         if (ioctl(fd, CIOCKEY, kop) == 0) {
981                 if (r)
982                         crparam2bn(&kop->crk_param[kop->crk_iparams], r);
983                 if (s)
984                         crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
985                 ret = 0;
986         }
987
988         return (ret);
989 }
990
991 static int
992 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
993     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
994 {
995         struct crypt_kop kop;
996         int ret = 1;
997
998         /* Currently, we know we can do mod exp iff we can do any
999          * asymmetric operations at all.
1000          */
1001         if (cryptodev_asymfeat == 0) {
1002                 ret = BN_mod_exp(r, a, p, m, ctx);
1003                 return (ret);
1004         }
1005
1006         memset(&kop, 0, sizeof kop);
1007         kop.crk_op = CRK_MOD_EXP;
1008
1009         /* inputs: a^p % m */
1010         if (bn2crparam(a, &kop.crk_param[0]))
1011                 goto err;
1012         if (bn2crparam(p, &kop.crk_param[1]))
1013                 goto err;
1014         if (bn2crparam(m, &kop.crk_param[2]))
1015                 goto err;
1016         kop.crk_iparams = 3;
1017
1018         if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
1019                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1020                 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1021         }
1022 err:
1023         zapparams(&kop);
1024         return (ret);
1025 }
1026
1027 static int
1028 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1029 {
1030         int r;
1031         ctx = BN_CTX_new();
1032         r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1033         BN_CTX_free(ctx);
1034         return (r);
1035 }
1036
1037 static int
1038 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1039 {
1040         struct crypt_kop kop;
1041         int ret = 1;
1042
1043         if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1044                 /* XXX 0 means failure?? */
1045                 return (0);
1046         }
1047
1048         memset(&kop, 0, sizeof kop);
1049         kop.crk_op = CRK_MOD_EXP_CRT;
1050         /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1051         if (bn2crparam(rsa->p, &kop.crk_param[0]))
1052                 goto err;
1053         if (bn2crparam(rsa->q, &kop.crk_param[1]))
1054                 goto err;
1055         if (bn2crparam(I, &kop.crk_param[2]))
1056                 goto err;
1057         if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1058                 goto err;
1059         if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1060                 goto err;
1061         if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1062                 goto err;
1063         kop.crk_iparams = 6;
1064
1065         if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
1066                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1067                 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1068         }
1069 err:
1070         zapparams(&kop);
1071         return (ret);
1072 }
1073
1074 static RSA_METHOD cryptodev_rsa = {
1075         "cryptodev RSA method",
1076         NULL,                           /* rsa_pub_enc */
1077         NULL,                           /* rsa_pub_dec */
1078         NULL,                           /* rsa_priv_enc */
1079         NULL,                           /* rsa_priv_dec */
1080         NULL,
1081         NULL,
1082         NULL,                           /* init */
1083         NULL,                           /* finish */
1084         0,                              /* flags */
1085         NULL,                           /* app_data */
1086         NULL,                           /* rsa_sign */
1087         NULL                            /* rsa_verify */
1088 };
1089
1090 static int
1091 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1092     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1093 {
1094         return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1095 }
1096
1097 static int
1098 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1099     BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1100     BN_CTX *ctx, BN_MONT_CTX *mont)
1101 {
1102         BIGNUM t2;
1103         int ret = 0;
1104
1105         BN_init(&t2);
1106
1107         /* v = ( g^u1 * y^u2 mod p ) mod q */
1108         /* let t1 = g ^ u1 mod p */
1109         ret = 0;
1110
1111         if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1112                 goto err;
1113
1114         /* let t2 = y ^ u2 mod p */
1115         if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1116                 goto err;
1117         /* let u1 = t1 * t2 mod p */
1118         if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1119                 goto err;
1120
1121         BN_copy(t1,u1);
1122
1123         ret = 1;
1124 err:
1125         BN_free(&t2);
1126         return(ret);
1127 }
1128
1129 static DSA_SIG *
1130 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1131 {
1132         struct crypt_kop kop;
1133         BIGNUM *r = NULL, *s = NULL;
1134         DSA_SIG *dsaret = NULL;
1135
1136         if ((r = BN_new()) == NULL)
1137                 goto err;
1138         if ((s = BN_new()) == NULL) {
1139                 BN_free(r);
1140                 goto err;
1141         }
1142
1143         memset(&kop, 0, sizeof kop);
1144         kop.crk_op = CRK_DSA_SIGN;
1145
1146         /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1147         kop.crk_param[0].crp_p = (caddr_t)dgst;
1148         kop.crk_param[0].crp_nbits = dlen * 8;
1149         if (bn2crparam(dsa->p, &kop.crk_param[1]))
1150                 goto err;
1151         if (bn2crparam(dsa->q, &kop.crk_param[2]))
1152                 goto err;
1153         if (bn2crparam(dsa->g, &kop.crk_param[3]))
1154                 goto err;
1155         if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1156                 goto err;
1157         kop.crk_iparams = 5;
1158
1159         if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1160             BN_num_bytes(dsa->q), s) == 0) {
1161                 dsaret = DSA_SIG_new();
1162                 dsaret->r = r;
1163                 dsaret->s = s;
1164         } else {
1165                 const DSA_METHOD *meth = DSA_OpenSSL();
1166                 BN_free(r);
1167                 BN_free(s);
1168                 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1169         }
1170 err:
1171         kop.crk_param[0].crp_p = NULL;
1172         zapparams(&kop);
1173         return (dsaret);
1174 }
1175
1176 static int
1177 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1178     DSA_SIG *sig, DSA *dsa)
1179 {
1180         struct crypt_kop kop;
1181         int dsaret = 1;
1182
1183         memset(&kop, 0, sizeof kop);
1184         kop.crk_op = CRK_DSA_VERIFY;
1185
1186         /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1187         kop.crk_param[0].crp_p = (caddr_t)dgst;
1188         kop.crk_param[0].crp_nbits = dlen * 8;
1189         if (bn2crparam(dsa->p, &kop.crk_param[1]))
1190                 goto err;
1191         if (bn2crparam(dsa->q, &kop.crk_param[2]))
1192                 goto err;
1193         if (bn2crparam(dsa->g, &kop.crk_param[3]))
1194                 goto err;
1195         if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1196                 goto err;
1197         if (bn2crparam(sig->r, &kop.crk_param[5]))
1198                 goto err;
1199         if (bn2crparam(sig->s, &kop.crk_param[6]))
1200                 goto err;
1201         kop.crk_iparams = 7;
1202
1203         if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1204                 dsaret = kop.crk_status;
1205         } else {
1206                 const DSA_METHOD *meth = DSA_OpenSSL();
1207
1208                 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1209         }
1210 err:
1211         kop.crk_param[0].crp_p = NULL;
1212         zapparams(&kop);
1213         return (dsaret);
1214 }
1215
1216 static DSA_METHOD cryptodev_dsa = {
1217         "cryptodev DSA method",
1218         NULL,
1219         NULL,                           /* dsa_sign_setup */
1220         NULL,
1221         NULL,                           /* dsa_mod_exp */
1222         NULL,
1223         NULL,                           /* init */
1224         NULL,                           /* finish */
1225         0,      /* flags */
1226         NULL    /* app_data */
1227 };
1228
1229 static int
1230 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1231     const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1232     BN_MONT_CTX *m_ctx)
1233 {
1234         return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1235 }
1236
1237 static int
1238 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1239 {
1240         struct crypt_kop kop;
1241         int dhret = 1;
1242         int fd, keylen;
1243
1244         if ((fd = get_asym_dev_crypto()) < 0) {
1245                 const DH_METHOD *meth = DH_OpenSSL();
1246
1247                 return ((meth->compute_key)(key, pub_key, dh));
1248         }
1249
1250         keylen = BN_num_bits(dh->p);
1251
1252         memset(&kop, 0, sizeof kop);
1253         kop.crk_op = CRK_DH_COMPUTE_KEY;
1254
1255         /* inputs: dh->priv_key pub_key dh->p key */
1256         if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1257                 goto err;
1258         if (bn2crparam(pub_key, &kop.crk_param[1]))
1259                 goto err;
1260         if (bn2crparam(dh->p, &kop.crk_param[2]))
1261                 goto err;
1262         kop.crk_iparams = 3;
1263
1264         kop.crk_param[3].crp_p = (caddr_t) key;
1265         kop.crk_param[3].crp_nbits = keylen * 8;
1266         kop.crk_oparams = 1;
1267
1268         if (ioctl(fd, CIOCKEY, &kop) == -1) {
1269                 const DH_METHOD *meth = DH_OpenSSL();
1270
1271                 dhret = (meth->compute_key)(key, pub_key, dh);
1272         }
1273 err:
1274         kop.crk_param[3].crp_p = NULL;
1275         zapparams(&kop);
1276         return (dhret);
1277 }
1278
1279 static DH_METHOD cryptodev_dh = {
1280         "cryptodev DH method",
1281         NULL,                           /* cryptodev_dh_generate_key */
1282         NULL,
1283         NULL,
1284         NULL,
1285         NULL,
1286         0,      /* flags */
1287         NULL    /* app_data */
1288 };
1289
1290 /*
1291  * ctrl right now is just a wrapper that doesn't do much
1292  * but I expect we'll want some options soon.
1293  */
1294 static int
1295 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1296 {
1297 #ifdef HAVE_SYSLOG_R
1298         struct syslog_data sd = SYSLOG_DATA_INIT;
1299 #endif
1300
1301         switch (cmd) {
1302         default:
1303 #ifdef HAVE_SYSLOG_R
1304                 syslog_r(LOG_ERR, &sd,
1305                     "cryptodev_ctrl: unknown command %d", cmd);
1306 #else
1307                 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1308 #endif
1309                 break;
1310         }
1311         return (1);
1312 }
1313
1314 void
1315 ENGINE_load_cryptodev(void)
1316 {
1317         ENGINE *engine = ENGINE_new();
1318         int fd;
1319
1320         if (engine == NULL)
1321                 return;
1322         if ((fd = get_dev_crypto()) < 0) {
1323                 ENGINE_free(engine);
1324                 return;
1325         }
1326
1327         /*
1328          * find out what asymmetric crypto algorithms we support
1329          */
1330         if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1331                 close(fd);
1332                 ENGINE_free(engine);
1333                 return;
1334         }
1335         close(fd);
1336
1337         if (!ENGINE_set_id(engine, "cryptodev") ||
1338             !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1339             !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1340             !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1341             !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1342             !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1343                 ENGINE_free(engine);
1344                 return;
1345         }
1346
1347         if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1348                 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1349
1350                 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1351                 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1352                 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1353                 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1354                 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1355                 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1356                 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1357                         cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1358                         if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1359                                 cryptodev_rsa.rsa_mod_exp =
1360                                     cryptodev_rsa_mod_exp;
1361                         else
1362                                 cryptodev_rsa.rsa_mod_exp =
1363                                     cryptodev_rsa_nocrt_mod_exp;
1364                 }
1365         }
1366
1367         if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1368                 const DSA_METHOD *meth = DSA_OpenSSL();
1369
1370                 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1371                 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1372                         cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1373                 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1374                         cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1375                         cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1376                 }
1377                 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1378                         cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1379         }
1380
1381         if (ENGINE_set_DH(engine, &cryptodev_dh)){
1382                 const DH_METHOD *dh_meth = DH_OpenSSL();
1383
1384                 cryptodev_dh.generate_key = dh_meth->generate_key;
1385                 cryptodev_dh.compute_key = dh_meth->compute_key;
1386                 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1387                 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1388                         cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1389                         if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1390                                 cryptodev_dh.compute_key =
1391                                     cryptodev_dh_compute_key;
1392                 }
1393         }
1394
1395         ENGINE_add(engine);
1396         ENGINE_free(engine);
1397         ERR_clear_error();
1398 }
1399
1400 #endif /* HAVE_CRYPTODEV */