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