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