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