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.c1354
1 files changed, 1354 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..d3b186c132
--- /dev/null
+++ b/src/lib/libcrypto/engine/hw_cryptodev.c
@@ -0,0 +1,1354 @@
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(out)) {
697 bcopy(spare, out, inl);
698 free(spare);
699 }
700
701 if (ivp == ivs)
702 bcopy(ivp, iv, ctx->cipher->iv_len);
703
704 if (ctx->cipher->iv_len) {
705 if (ctx->encrypt)
706 iiv = (void *) out + inl - ctx->cipher->iv_len;
707 else
708 iiv = save_iv;
709 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
710 }
711 return (1);
712}
713
714static int
715xcrypt_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
716 const unsigned char *iv, int enc)
717{
718 AES_KEY *k = ctx->cipher_data;
719#ifndef AES_ASM
720 int i;
721#endif
722
723 bzero(k, sizeof *k);
724 if (enc)
725 AES_set_encrypt_key(key, ctx->key_len * 8, k);
726 else
727 AES_set_decrypt_key(key, ctx->key_len * 8, k);
728
729#ifndef AES_ASM
730 /*
731 * XXX Damn OpenSSL byte swaps the expanded key!!
732 *
733 * XXX But only if we're using the C implementation of AES
734 */
735 for (i = 0; i < 4 * (AES_MAXNR + 1); i++)
736 k->rd_key[i] = htonl(k->rd_key[i]);
737#endif
738
739 return (1);
740}
741
742static int
743xcrypt_cleanup(EVP_CIPHER_CTX *ctx)
744{
745 bzero(ctx->cipher_data, ctx->cipher->ctx_size);
746 return (1);
747}
748
749static int
750check_viac3aes(void)
751{
752 int mib[2] = { CTL_MACHDEP, CPU_XCRYPT }, value;
753 size_t size = sizeof(value);
754
755 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size,
756 NULL, 0) < 0)
757 return (0);
758 if (value == 0)
759 return (0);
760
761 if (value & C3_HAS_AES) {
762 cryptodev_aes_128_cbc.init = xcrypt_init_key;
763 cryptodev_aes_128_cbc.do_cipher = xcrypt_cipher;
764 cryptodev_aes_128_cbc.cleanup = xcrypt_cleanup;
765 cryptodev_aes_128_cbc.ctx_size = sizeof(AES_KEY);
766
767 cryptodev_aes_192_cbc.init = xcrypt_init_key;
768 cryptodev_aes_192_cbc.do_cipher = xcrypt_cipher;
769 cryptodev_aes_192_cbc.cleanup = xcrypt_cleanup;
770 cryptodev_aes_192_cbc.ctx_size = sizeof(AES_KEY);
771
772 cryptodev_aes_256_cbc.init = xcrypt_init_key;
773 cryptodev_aes_256_cbc.do_cipher = xcrypt_cipher;
774 cryptodev_aes_256_cbc.cleanup = xcrypt_cleanup;
775 cryptodev_aes_256_cbc.ctx_size = sizeof(AES_KEY);
776 }
777 return (value);
778}
779#endif /* __i386__ */
780
781/*
782 * Registered by the ENGINE when used to find out how to deal with
783 * a particular NID in the ENGINE. this says what we'll do at the
784 * top level - note, that list is restricted by what we answer with
785 */
786static int
787cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
788 const int **nids, int nid)
789{
790 if (!cipher)
791 return (cryptodev_usable_ciphers(nids));
792
793 switch (nid) {
794 case NID_des_ede3_cbc:
795 *cipher = &cryptodev_3des_cbc;
796 break;
797 case NID_des_cbc:
798 *cipher = &cryptodev_des_cbc;
799 break;
800 case NID_bf_cbc:
801 *cipher = &cryptodev_bf_cbc;
802 break;
803 case NID_cast5_cbc:
804 *cipher = &cryptodev_cast_cbc;
805 break;
806 case NID_aes_128_cbc:
807 *cipher = &cryptodev_aes_128_cbc;
808 break;
809 case NID_aes_192_cbc:
810 *cipher = &cryptodev_aes_192_cbc;
811 break;
812 case NID_aes_256_cbc:
813 *cipher = &cryptodev_aes_256_cbc;
814 break;
815 default:
816 *cipher = NULL;
817 break;
818 }
819 return (*cipher != NULL);
820}
821
822static int
823cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
824 const int **nids, int nid)
825{
826 if (!digest)
827 return (cryptodev_usable_digests(nids));
828
829 switch (nid) {
830 case NID_md5:
831 *digest = NULL; /* need to make a clean md5 critter */
832 break;
833 default:
834 *digest = NULL;
835 break;
836 }
837 return (*digest != NULL);
838}
839
840/*
841 * Convert a BIGNUM to the representation that /dev/crypto needs.
842 * Upon completion of use, the caller is responsible for freeing
843 * crp->crp_p.
844 */
845static int
846bn2crparam(const BIGNUM *a, struct crparam *crp)
847{
848 int i, j, k;
849 ssize_t bytes, bits;
850 u_char *b;
851
852 crp->crp_p = NULL;
853 crp->crp_nbits = 0;
854
855 bits = BN_num_bits(a);
856 bytes = (bits + 7) / 8;
857
858 b = malloc(bytes);
859 if (b == NULL)
860 return (1);
861
862 crp->crp_p = b;
863 crp->crp_nbits = bits;
864
865 for (i = 0, j = 0; i < a->top; i++) {
866 for (k = 0; k < BN_BITS2 / 8; k++) {
867 if ((j + k) >= bytes)
868 return (0);
869 b[j + k] = a->d[i] >> (k * 8);
870 }
871 j += BN_BITS2 / 8;
872 }
873 return (0);
874}
875
876/* Convert a /dev/crypto parameter to a BIGNUM */
877static int
878crparam2bn(struct crparam *crp, BIGNUM *a)
879{
880 u_int8_t *pd;
881 int i, bytes;
882
883 bytes = (crp->crp_nbits + 7) / 8;
884
885 if (bytes == 0)
886 return (-1);
887
888 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
889 return (-1);
890
891 for (i = 0; i < bytes; i++)
892 pd[i] = crp->crp_p[bytes - i - 1];
893
894 BN_bin2bn(pd, bytes, a);
895 free(pd);
896
897 return (0);
898}
899
900static void
901zapparams(struct crypt_kop *kop)
902{
903 int i;
904
905 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
906 if (kop->crk_param[i].crp_p)
907 free(kop->crk_param[i].crp_p);
908 kop->crk_param[i].crp_p = NULL;
909 kop->crk_param[i].crp_nbits = 0;
910 }
911}
912
913static int
914cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
915{
916 int fd, ret = -1;
917
918 if ((fd = get_asym_dev_crypto()) < 0)
919 return (ret);
920
921 if (r) {
922 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
923 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
924 kop->crk_oparams++;
925 }
926 if (s) {
927 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
928 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
929 kop->crk_oparams++;
930 }
931
932 if (ioctl(fd, CIOCKEY, kop) == 0) {
933 if (r)
934 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
935 if (s)
936 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
937 ret = 0;
938 }
939
940 return (ret);
941}
942
943static int
944cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
945 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
946{
947 struct crypt_kop kop;
948 int ret = 1;
949
950 /* Currently, we know we can do mod exp iff we can do any
951 * asymmetric operations at all.
952 */
953 if (cryptodev_asymfeat == 0) {
954 ret = BN_mod_exp(r, a, p, m, ctx);
955 return (ret);
956 }
957
958 memset(&kop, 0, sizeof kop);
959 kop.crk_op = CRK_MOD_EXP;
960
961 /* inputs: a^p % m */
962 if (bn2crparam(a, &kop.crk_param[0]))
963 goto err;
964 if (bn2crparam(p, &kop.crk_param[1]))
965 goto err;
966 if (bn2crparam(m, &kop.crk_param[2]))
967 goto err;
968 kop.crk_iparams = 3;
969
970 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
971 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
972 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
973 }
974err:
975 zapparams(&kop);
976 return (ret);
977}
978
979static int
980cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
981{
982 int r;
983 BN_CTX *ctx;
984
985 ctx = BN_CTX_new();
986 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
987 BN_CTX_free(ctx);
988 return (r);
989}
990
991static int
992cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
993{
994 struct crypt_kop kop;
995 int ret = 1;
996
997 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
998 /* XXX 0 means failure?? */
999 return (0);
1000 }
1001
1002 memset(&kop, 0, sizeof kop);
1003 kop.crk_op = CRK_MOD_EXP_CRT;
1004 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1005 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1006 goto err;
1007 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1008 goto err;
1009 if (bn2crparam(I, &kop.crk_param[2]))
1010 goto err;
1011 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1012 goto err;
1013 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1014 goto err;
1015 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1016 goto err;
1017 kop.crk_iparams = 6;
1018
1019 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
1020 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1021 ret = (*meth->rsa_mod_exp)(r0, I, rsa);
1022 }
1023err:
1024 zapparams(&kop);
1025 return (ret);
1026}
1027
1028static RSA_METHOD cryptodev_rsa = {
1029 "cryptodev RSA method",
1030 NULL, /* rsa_pub_enc */
1031 NULL, /* rsa_pub_dec */
1032 NULL, /* rsa_priv_enc */
1033 NULL, /* rsa_priv_dec */
1034 NULL,
1035 NULL,
1036 NULL, /* init */
1037 NULL, /* finish */
1038 0, /* flags */
1039 NULL, /* app_data */
1040 NULL, /* rsa_sign */
1041 NULL /* rsa_verify */
1042};
1043
1044static int
1045cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1046 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1047{
1048 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1049}
1050
1051static int
1052cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1053 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1054 BN_CTX *ctx, BN_MONT_CTX *mont)
1055{
1056 BIGNUM t2;
1057 int ret = 0;
1058
1059 BN_init(&t2);
1060
1061 /* v = ( g^u1 * y^u2 mod p ) mod q */
1062 /* let t1 = g ^ u1 mod p */
1063 ret = 0;
1064
1065 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1066 goto err;
1067
1068 /* let t2 = y ^ u2 mod p */
1069 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1070 goto err;
1071 /* let u1 = t1 * t2 mod p */
1072 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1073 goto err;
1074
1075 BN_copy(t1,u1);
1076
1077 ret = 1;
1078err:
1079 BN_free(&t2);
1080 return(ret);
1081}
1082
1083static DSA_SIG *
1084cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1085{
1086 struct crypt_kop kop;
1087 BIGNUM *r = NULL, *s = NULL;
1088 DSA_SIG *dsaret = NULL;
1089
1090 if ((r = BN_new()) == NULL)
1091 goto err;
1092 if ((s = BN_new()) == NULL) {
1093 BN_free(r);
1094 goto err;
1095 }
1096
1097 memset(&kop, 0, sizeof kop);
1098 kop.crk_op = CRK_DSA_SIGN;
1099
1100 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1101 kop.crk_param[0].crp_p = (caddr_t)dgst;
1102 kop.crk_param[0].crp_nbits = dlen * 8;
1103 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1104 goto err;
1105 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1106 goto err;
1107 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1108 goto err;
1109 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1110 goto err;
1111 kop.crk_iparams = 5;
1112
1113 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1114 BN_num_bytes(dsa->q), s) == 0) {
1115 dsaret = DSA_SIG_new();
1116 dsaret->r = r;
1117 dsaret->s = s;
1118 } else {
1119 const DSA_METHOD *meth = DSA_OpenSSL();
1120 BN_free(r);
1121 BN_free(s);
1122 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1123 }
1124err:
1125 kop.crk_param[0].crp_p = NULL;
1126 zapparams(&kop);
1127 return (dsaret);
1128}
1129
1130static int
1131cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1132 DSA_SIG *sig, DSA *dsa)
1133{
1134 struct crypt_kop kop;
1135 int dsaret = 1;
1136
1137 memset(&kop, 0, sizeof kop);
1138 kop.crk_op = CRK_DSA_VERIFY;
1139
1140 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1141 kop.crk_param[0].crp_p = (caddr_t)dgst;
1142 kop.crk_param[0].crp_nbits = dlen * 8;
1143 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1144 goto err;
1145 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1146 goto err;
1147 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1148 goto err;
1149 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1150 goto err;
1151 if (bn2crparam(sig->r, &kop.crk_param[5]))
1152 goto err;
1153 if (bn2crparam(sig->s, &kop.crk_param[6]))
1154 goto err;
1155 kop.crk_iparams = 7;
1156
1157 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1158 dsaret = kop.crk_status;
1159 } else {
1160 const DSA_METHOD *meth = DSA_OpenSSL();
1161
1162 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1163 }
1164err:
1165 kop.crk_param[0].crp_p = NULL;
1166 zapparams(&kop);
1167 return (dsaret);
1168}
1169
1170static DSA_METHOD cryptodev_dsa = {
1171 "cryptodev DSA method",
1172 NULL,
1173 NULL, /* dsa_sign_setup */
1174 NULL,
1175 NULL, /* dsa_mod_exp */
1176 NULL,
1177 NULL, /* init */
1178 NULL, /* finish */
1179 0, /* flags */
1180 NULL /* app_data */
1181};
1182
1183static int
1184cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1185 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1186 BN_MONT_CTX *m_ctx)
1187{
1188 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1189}
1190
1191static int
1192cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1193{
1194 struct crypt_kop kop;
1195 int dhret = 1;
1196 int fd, keylen;
1197
1198 if ((fd = get_asym_dev_crypto()) < 0) {
1199 const DH_METHOD *meth = DH_OpenSSL();
1200
1201 return ((meth->compute_key)(key, pub_key, dh));
1202 }
1203
1204 keylen = BN_num_bits(dh->p);
1205
1206 memset(&kop, 0, sizeof kop);
1207 kop.crk_op = CRK_DH_COMPUTE_KEY;
1208
1209 /* inputs: dh->priv_key pub_key dh->p key */
1210 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1211 goto err;
1212 if (bn2crparam(pub_key, &kop.crk_param[1]))
1213 goto err;
1214 if (bn2crparam(dh->p, &kop.crk_param[2]))
1215 goto err;
1216 kop.crk_iparams = 3;
1217
1218 kop.crk_param[3].crp_p = key;
1219 kop.crk_param[3].crp_nbits = keylen * 8;
1220 kop.crk_oparams = 1;
1221
1222 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1223 const DH_METHOD *meth = DH_OpenSSL();
1224
1225 dhret = (meth->compute_key)(key, pub_key, dh);
1226 }
1227err:
1228 kop.crk_param[3].crp_p = NULL;
1229 zapparams(&kop);
1230 return (dhret);
1231}
1232
1233static DH_METHOD cryptodev_dh = {
1234 "cryptodev DH method",
1235 NULL, /* cryptodev_dh_generate_key */
1236 NULL,
1237 NULL,
1238 NULL,
1239 NULL,
1240 0, /* flags */
1241 NULL /* app_data */
1242};
1243
1244/*
1245 * ctrl right now is just a wrapper that doesn't do much
1246 * but I expect we'll want some options soon.
1247 */
1248static int
1249cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1250{
1251#ifdef HAVE_SYSLOG_R
1252 struct syslog_data sd = SYSLOG_DATA_INIT;
1253#endif
1254
1255 switch (cmd) {
1256 default:
1257#ifdef HAVE_SYSLOG_R
1258 syslog_r(LOG_ERR, &sd,
1259 "cryptodev_ctrl: unknown command %d", cmd);
1260#else
1261 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1262#endif
1263 break;
1264 }
1265 return (1);
1266}
1267
1268void
1269ENGINE_load_cryptodev(void)
1270{
1271 ENGINE *engine = ENGINE_new();
1272 int fd;
1273
1274 if (engine == NULL)
1275 return;
1276 if ((fd = get_dev_crypto()) < 0) {
1277 ENGINE_free(engine);
1278 return;
1279 }
1280
1281 /*
1282 * find out what asymmetric crypto algorithms we support
1283 */
1284 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1285 close(fd);
1286 ENGINE_free(engine);
1287 return;
1288 }
1289 close(fd);
1290
1291 if (!ENGINE_set_id(engine, "cryptodev") ||
1292 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1293 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1294 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1295 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1296 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1297 ENGINE_free(engine);
1298 return;
1299 }
1300
1301 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1302 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1303
1304 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1305 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1306 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1307 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1308 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1309 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1310 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1311 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1312 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1313 cryptodev_rsa.rsa_mod_exp =
1314 cryptodev_rsa_mod_exp;
1315 else
1316 cryptodev_rsa.rsa_mod_exp =
1317 cryptodev_rsa_nocrt_mod_exp;
1318 }
1319 }
1320
1321 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1322 const DSA_METHOD *meth = DSA_OpenSSL();
1323
1324 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1325 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1326 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1327 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1328 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1329 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1330 }
1331 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1332 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1333 }
1334
1335 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1336 const DH_METHOD *dh_meth = DH_OpenSSL();
1337
1338 cryptodev_dh.generate_key = dh_meth->generate_key;
1339 cryptodev_dh.compute_key = dh_meth->compute_key;
1340 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1341 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1342 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1343 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1344 cryptodev_dh.compute_key =
1345 cryptodev_dh_compute_key;
1346 }
1347 }
1348
1349 ENGINE_add(engine);
1350 ENGINE_free(engine);
1351 ERR_clear_error();
1352}
1353
1354#endif /* HAVE_CRYPTODEV */