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