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