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