Added support SHA224
[cryptodev-linux.git] / extras / eng_cryptodev.c
blob7a26f19d1513906e4429be510fdbbce0872b412a
1 /*
2 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3 * Copyright (c) 2002 Theo de Raadt
4 * Copyright (c) 2002 Markus Friedl
5 * Copyright (c) 2012 Nikos Mavrogiannopoulos
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <openssl/objects.h>
31 #include <openssl/engine.h>
32 #include <openssl/evp.h>
33 #include <openssl/bn.h>
35 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
36 (defined(OpenBSD) || defined(__FreeBSD__))
37 #include <sys/param.h>
38 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
39 # define HAVE_CRYPTODEV
40 # endif
41 # if (OpenBSD >= 200110)
42 # define HAVE_SYSLOG_R
43 # endif
44 #endif
46 #ifndef HAVE_CRYPTODEV
48 void
49 ENGINE_load_cryptodev(void)
51 /* This is a NOP on platforms without /dev/crypto */
52 return;
55 #else
57 #include <sys/types.h>
58 #include <crypto/cryptodev.h>
59 #include <crypto/dh/dh.h>
60 #include <crypto/dsa/dsa.h>
61 #include <crypto/err/err.h>
62 #include <crypto/rsa/rsa.h>
63 #include <sys/ioctl.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <unistd.h>
67 #include <fcntl.h>
68 #include <stdarg.h>
69 #include <syslog.h>
70 #include <errno.h>
71 #include <string.h>
73 struct dev_crypto_state {
74 struct session_op d_sess;
75 int d_fd;
77 #ifdef USE_CRYPTODEV_DIGESTS
78 unsigned char digest_res[HASH_MAX_LEN];
79 char *mac_data;
80 int mac_len;
81 #endif
84 static u_int32_t cryptodev_asymfeat = 0;
86 static int get_asym_dev_crypto(void);
87 static int open_dev_crypto(void);
88 static int get_dev_crypto(void);
89 static int get_cryptodev_ciphers(const int **cnids);
90 #ifdef USE_CRYPTODEV_DIGESTS
91 static int get_cryptodev_digests(const int **cnids);
92 #endif
93 static int cryptodev_usable_ciphers(const int **nids);
94 static int cryptodev_usable_digests(const int **nids);
95 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
96 const unsigned char *in, size_t inl);
97 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
98 const unsigned char *iv, int enc);
99 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
100 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
101 const int **nids, int nid);
102 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
103 const int **nids, int nid);
104 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
105 static int crparam2bn(struct crparam *crp, BIGNUM *a);
106 static void zapparams(struct crypt_kop *kop);
107 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
108 int slen, BIGNUM *s);
110 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
111 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
112 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
113 RSA *rsa, BN_CTX *ctx);
114 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
115 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
116 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
117 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
118 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
119 BN_CTX *ctx, BN_MONT_CTX *mont);
120 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
121 int dlen, DSA *dsa);
122 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
123 DSA_SIG *sig, DSA *dsa);
124 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
125 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
126 BN_MONT_CTX *m_ctx);
127 static int cryptodev_dh_compute_key(unsigned char *key,
128 const BIGNUM *pub_key, DH *dh);
129 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
130 void (*f)(void));
131 void ENGINE_load_cryptodev(void);
133 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
134 { 0, NULL, NULL, 0 }
137 static struct {
138 int id;
139 int nid;
140 int ivmax;
141 int keylen;
142 } ciphers[] = {
143 { CRYPTO_ARC4, NID_rc4, 0, 16, },
144 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
145 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
146 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
147 { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
148 { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
149 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
150 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
151 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
152 { 0, NID_undef, 0, 0, },
155 #ifdef USE_CRYPTODEV_DIGESTS
156 static struct {
157 int id;
158 int nid;
159 int digestlen;
160 } digests[] = {
161 #if 0
162 /* HMAC is not supported */
163 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
164 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
165 { CRYPTO_SHA2_256_HMAC, NID_hmacWithSHA256, 32},
166 { CRYPTO_SHA2_384_HMAC, NID_hmacWithSHA384, 48},
167 { CRYPTO_SHA2_512_HMAC, NID_hmacWithSHA512, 64},
168 #endif
169 { CRYPTO_MD5, NID_md5, 16},
170 { CRYPTO_SHA1, NID_sha1, 20},
171 { CRYPTO_SHA2_256, NID_sha256, 32},
172 { CRYPTO_SHA2_384, NID_sha384, 48},
173 { CRYPTO_SHA2_512, NID_sha512, 64},
174 { 0, NID_undef, 0},
176 #endif
179 * Return a fd if /dev/crypto seems usable, 0 otherwise.
181 static int
182 open_dev_crypto(void)
184 static int fd = -1;
186 if (fd == -1) {
187 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
188 return (-1);
189 /* close on exec */
190 if (fcntl(fd, F_SETFD, 1) == -1) {
191 close(fd);
192 fd = -1;
193 return (-1);
196 return (fd);
199 static int
200 get_dev_crypto(void)
202 int fd, retfd;
204 if ((fd = open_dev_crypto()) == -1)
205 return (-1);
206 #ifndef CRIOGET_NOT_NEEDED
207 if (ioctl(fd, CRIOGET, &retfd) == -1)
208 return (-1);
210 /* close on exec */
211 if (fcntl(retfd, F_SETFD, 1) == -1) {
212 close(retfd);
213 return (-1);
215 #else
216 retfd = fd;
217 #endif
218 return (retfd);
221 static void put_dev_crypto(int fd)
223 #ifndef CRIOGET_NOT_NEEDED
224 close(fd);
225 #endif
228 /* Caching version for asym operations */
229 static int
230 get_asym_dev_crypto(void)
232 static int fd = -1;
234 if (fd == -1)
235 fd = get_dev_crypto();
236 return fd;
240 * Find out what ciphers /dev/crypto will let us have a session for.
241 * XXX note, that some of these openssl doesn't deal with yet!
242 * returning them here is harmless, as long as we return NULL
243 * when asked for a handler in the cryptodev_engine_ciphers routine
245 static int
246 get_cryptodev_ciphers(const int **cnids)
248 static int nids[CRYPTO_ALGORITHM_MAX];
249 struct session_op sess;
250 int fd, i, count = 0;
251 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
253 if ((fd = get_dev_crypto()) < 0) {
254 *cnids = NULL;
255 return (0);
257 memset(&sess, 0, sizeof(sess));
258 sess.key = (void*)fake_key;
260 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
261 if (ciphers[i].nid == NID_undef)
262 continue;
263 sess.cipher = ciphers[i].id;
264 sess.keylen = ciphers[i].keylen;
265 sess.mac = 0;
266 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
267 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
268 nids[count++] = ciphers[i].nid;
270 put_dev_crypto(fd);
272 if (count > 0)
273 *cnids = nids;
274 else
275 *cnids = NULL;
276 return (count);
279 #ifdef USE_CRYPTODEV_DIGESTS
281 * Find out what digests /dev/crypto will let us have a session for.
282 * XXX note, that some of these openssl doesn't deal with yet!
283 * returning them here is harmless, as long as we return NULL
284 * when asked for a handler in the cryptodev_engine_digests routine
286 static int
287 get_cryptodev_digests(const int **cnids)
289 static int nids[CRYPTO_ALGORITHM_MAX];
290 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
291 struct session_op sess;
292 int fd, i, count = 0;
294 if ((fd = get_dev_crypto()) < 0) {
295 *cnids = NULL;
296 return (0);
298 memset(&sess, 0, sizeof(sess));
299 sess.mackey = fake_key;
300 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
301 if (digests[i].nid == NID_undef)
302 continue;
303 sess.mac = digests[i].id;
304 sess.mackeylen = 8;
305 sess.cipher = 0;
306 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
307 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
308 nids[count++] = digests[i].nid;
310 put_dev_crypto(fd);
312 if (count > 0)
313 *cnids = nids;
314 else
315 *cnids = NULL;
316 return (count);
318 #endif /* 0 */
321 * Find the useable ciphers|digests from dev/crypto - this is the first
322 * thing called by the engine init crud which determines what it
323 * can use for ciphers from this engine. We want to return
324 * only what we can do, anythine else is handled by software.
326 * If we can't initialize the device to do anything useful for
327 * any reason, we want to return a NULL array, and 0 length,
328 * which forces everything to be done is software. By putting
329 * the initalization of the device in here, we ensure we can
330 * use this engine as the default, and if for whatever reason
331 * /dev/crypto won't do what we want it will just be done in
332 * software
334 * This can (should) be greatly expanded to perhaps take into
335 * account speed of the device, and what we want to do.
336 * (although the disabling of particular alg's could be controlled
337 * by the device driver with sysctl's.) - this is where we
338 * want most of the decisions made about what we actually want
339 * to use from /dev/crypto.
341 static int
342 cryptodev_usable_ciphers(const int **nids)
344 return (get_cryptodev_ciphers(nids));
347 static int
348 cryptodev_usable_digests(const int **nids)
350 #ifdef USE_CRYPTODEV_DIGESTS
351 return (get_cryptodev_digests(nids));
352 #else
354 * XXXX just disable all digests for now, because it sucks.
355 * we need a better way to decide this - i.e. I may not
356 * want digests on slow cards like hifn on fast machines,
357 * but might want them on slow or loaded machines, etc.
358 * will also want them when using crypto cards that don't
359 * suck moose gonads - would be nice to be able to decide something
360 * as reasonable default without having hackery that's card dependent.
361 * of course, the default should probably be just do everything,
362 * with perhaps a sysctl to turn algoritms off (or have them off
363 * by default) on cards that generally suck like the hifn.
365 *nids = NULL;
366 return (0);
367 #endif
370 static int
371 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
372 const unsigned char *in, size_t inl)
374 struct crypt_op cryp;
375 struct dev_crypto_state *state = ctx->cipher_data;
376 struct session_op *sess = &state->d_sess;
377 const void *iiv;
378 unsigned char save_iv[EVP_MAX_IV_LENGTH];
380 if (state->d_fd < 0)
381 return (0);
382 if (!inl)
383 return (1);
384 if ((inl % ctx->cipher->block_size) != 0)
385 return (0);
387 memset(&cryp, 0, sizeof(cryp));
389 cryp.ses = sess->ses;
390 cryp.flags = 0;
391 cryp.len = inl;
392 cryp.src = (void*) in;
393 cryp.dst = (void*) out;
394 cryp.mac = 0;
396 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
398 if (ctx->cipher->iv_len) {
399 cryp.iv = (void*) ctx->iv;
400 if (!ctx->encrypt) {
401 iiv = in + inl - ctx->cipher->iv_len;
402 memcpy(save_iv, iiv, ctx->cipher->iv_len);
404 } else
405 cryp.iv = NULL;
407 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
408 /* XXX need better errror handling
409 * this can fail for a number of different reasons.
411 return (0);
414 if (ctx->cipher->iv_len) {
415 if (ctx->encrypt)
416 iiv = out + inl - ctx->cipher->iv_len;
417 else
418 iiv = save_iv;
419 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
421 return (1);
424 static int
425 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
426 const unsigned char *iv, int enc)
428 struct dev_crypto_state *state = ctx->cipher_data;
429 struct session_op *sess = &state->d_sess;
430 int cipher = -1, i;
432 for (i = 0; ciphers[i].id; i++)
433 if (ctx->cipher->nid == ciphers[i].nid &&
434 ctx->cipher->iv_len <= ciphers[i].ivmax &&
435 ctx->key_len == ciphers[i].keylen) {
436 cipher = ciphers[i].id;
437 break;
440 if (!ciphers[i].id) {
441 state->d_fd = -1;
442 return (0);
445 memset(sess, 0, sizeof(struct session_op));
447 if ((state->d_fd = get_dev_crypto()) < 0)
448 return (0);
450 sess->key = (void*)key;
451 sess->keylen = ctx->key_len;
452 sess->cipher = cipher;
454 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
455 put_dev_crypto(state->d_fd);
456 state->d_fd = -1;
457 return (0);
459 return (1);
463 * free anything we allocated earlier when initting a
464 * session, and close the session.
466 static int
467 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
469 int ret = 0;
470 struct dev_crypto_state *state = ctx->cipher_data;
471 struct session_op *sess = &state->d_sess;
473 if (state->d_fd < 0)
474 return (0);
476 /* XXX if this ioctl fails, someting's wrong. the invoker
477 * may have called us with a bogus ctx, or we could
478 * have a device that for whatever reason just doesn't
479 * want to play ball - it's not clear what's right
480 * here - should this be an error? should it just
481 * increase a counter, hmm. For right now, we return
482 * 0 - I don't believe that to be "right". we could
483 * call the gorpy openssl lib error handlers that
484 * print messages to users of the library. hmm..
487 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
488 ret = 0;
489 } else {
490 ret = 1;
492 put_dev_crypto(state->d_fd);
493 state->d_fd = -1;
495 return (ret);
499 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
500 * gets called when libcrypto requests a cipher NID.
503 /* RC4 */
504 const EVP_CIPHER cryptodev_rc4 = {
505 NID_rc4,
506 1, 16, 0,
507 EVP_CIPH_VARIABLE_LENGTH,
508 cryptodev_init_key,
509 cryptodev_cipher,
510 cryptodev_cleanup,
511 sizeof(struct dev_crypto_state),
512 NULL,
513 NULL,
514 NULL
517 /* DES CBC EVP */
518 const EVP_CIPHER cryptodev_des_cbc = {
519 NID_des_cbc,
520 8, 8, 8,
521 EVP_CIPH_CBC_MODE,
522 cryptodev_init_key,
523 cryptodev_cipher,
524 cryptodev_cleanup,
525 sizeof(struct dev_crypto_state),
526 EVP_CIPHER_set_asn1_iv,
527 EVP_CIPHER_get_asn1_iv,
528 NULL
531 /* 3DES CBC EVP */
532 const EVP_CIPHER cryptodev_3des_cbc = {
533 NID_des_ede3_cbc,
534 8, 24, 8,
535 EVP_CIPH_CBC_MODE,
536 cryptodev_init_key,
537 cryptodev_cipher,
538 cryptodev_cleanup,
539 sizeof(struct dev_crypto_state),
540 EVP_CIPHER_set_asn1_iv,
541 EVP_CIPHER_get_asn1_iv,
542 NULL
545 const EVP_CIPHER cryptodev_bf_cbc = {
546 NID_bf_cbc,
547 8, 16, 8,
548 EVP_CIPH_CBC_MODE,
549 cryptodev_init_key,
550 cryptodev_cipher,
551 cryptodev_cleanup,
552 sizeof(struct dev_crypto_state),
553 EVP_CIPHER_set_asn1_iv,
554 EVP_CIPHER_get_asn1_iv,
555 NULL
558 const EVP_CIPHER cryptodev_cast_cbc = {
559 NID_cast5_cbc,
560 8, 16, 8,
561 EVP_CIPH_CBC_MODE,
562 cryptodev_init_key,
563 cryptodev_cipher,
564 cryptodev_cleanup,
565 sizeof(struct dev_crypto_state),
566 EVP_CIPHER_set_asn1_iv,
567 EVP_CIPHER_get_asn1_iv,
568 NULL
571 const EVP_CIPHER cryptodev_aes_cbc = {
572 NID_aes_128_cbc,
573 16, 16, 16,
574 EVP_CIPH_CBC_MODE,
575 cryptodev_init_key,
576 cryptodev_cipher,
577 cryptodev_cleanup,
578 sizeof(struct dev_crypto_state),
579 EVP_CIPHER_set_asn1_iv,
580 EVP_CIPHER_get_asn1_iv,
581 NULL
584 const EVP_CIPHER cryptodev_aes_192_cbc = {
585 NID_aes_192_cbc,
586 16, 24, 16,
587 EVP_CIPH_CBC_MODE,
588 cryptodev_init_key,
589 cryptodev_cipher,
590 cryptodev_cleanup,
591 sizeof(struct dev_crypto_state),
592 EVP_CIPHER_set_asn1_iv,
593 EVP_CIPHER_get_asn1_iv,
594 NULL
597 const EVP_CIPHER cryptodev_aes_256_cbc = {
598 NID_aes_256_cbc,
599 16, 32, 16,
600 EVP_CIPH_CBC_MODE,
601 cryptodev_init_key,
602 cryptodev_cipher,
603 cryptodev_cleanup,
604 sizeof(struct dev_crypto_state),
605 EVP_CIPHER_set_asn1_iv,
606 EVP_CIPHER_get_asn1_iv,
607 NULL
611 * Registered by the ENGINE when used to find out how to deal with
612 * a particular NID in the ENGINE. this says what we'll do at the
613 * top level - note, that list is restricted by what we answer with
615 static int
616 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
617 const int **nids, int nid)
619 if (!cipher)
620 return (cryptodev_usable_ciphers(nids));
622 switch (nid) {
623 case NID_rc4:
624 *cipher = &cryptodev_rc4;
625 break;
626 case NID_des_ede3_cbc:
627 *cipher = &cryptodev_3des_cbc;
628 break;
629 case NID_des_cbc:
630 *cipher = &cryptodev_des_cbc;
631 break;
632 case NID_bf_cbc:
633 *cipher = &cryptodev_bf_cbc;
634 break;
635 case NID_cast5_cbc:
636 *cipher = &cryptodev_cast_cbc;
637 break;
638 case NID_aes_128_cbc:
639 *cipher = &cryptodev_aes_cbc;
640 break;
641 case NID_aes_192_cbc:
642 *cipher = &cryptodev_aes_192_cbc;
643 break;
644 case NID_aes_256_cbc:
645 *cipher = &cryptodev_aes_256_cbc;
646 break;
647 default:
648 *cipher = NULL;
649 break;
651 return (*cipher != NULL);
655 #ifdef USE_CRYPTODEV_DIGESTS
657 /* convert digest type to cryptodev */
658 static int
659 digest_nid_to_cryptodev(int nid)
661 int i;
663 for (i = 0; digests[i].id; i++)
664 if (digests[i].nid == nid)
665 return (digests[i].id);
666 return (0);
670 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
672 struct dev_crypto_state *state = ctx->md_data;
673 struct session_op *sess = &state->d_sess;
674 int digest;
676 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
677 printf("cryptodev_digest_init: Can't get digest \n");
678 return (0);
680 memset(state, 0, sizeof(struct dev_crypto_state));
682 if ((state->d_fd = get_dev_crypto()) < 0) {
683 printf("cryptodev_digest_init: Can't get Dev \n");
684 return (0);
687 sess->mackey = NULL;
688 sess->mackeylen = 0;
689 sess->mac = digest;
691 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
692 put_dev_crypto(state->d_fd);
693 state->d_fd = -1;
694 printf("cryptodev_digest_init: Open session failed\n");
695 return (0);
698 return (1);
701 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
702 size_t count)
704 struct dev_crypto_state *state = ctx->md_data;
705 struct crypt_op cryp;
706 struct session_op *sess = &state->d_sess;
708 if (!data || state->d_fd < 0) {
709 printf("cryptodev_digest_update: illegal inputs \n");
710 return (0);
713 if (!count) {
714 return (1);
717 #ifndef COP_FLAG_UPDATE
718 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
719 /* if application doesn't support one buffer */
720 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
722 if (!state->mac_data) {
723 printf("cryptodev_digest_update: realloc failed\n");
724 return (0);
727 memcpy(state->mac_data + state->mac_len, data, count);
728 state->mac_len += count;
730 return (1);
733 memset(&cryp, 0, sizeof(cryp));
735 cryp.flags = 0;
736 #else
737 memset(&cryp, 0, sizeof(cryp));
739 cryp.flags = COP_FLAG_UPDATe;
740 #endif
741 cryp.ses = sess->ses;
742 cryp.len = count;
743 cryp.src = (void*) data;
744 cryp.dst = NULL;
745 cryp.mac = (void*) state->digest_res;
747 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
748 printf("cryptodev_digest_update: digest failed\n");
749 return (0);
752 return (1);
756 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
758 struct crypt_op cryp;
759 struct dev_crypto_state *state = ctx->md_data;
760 struct session_op *sess = &state->d_sess;
762 if (!md || state->d_fd < 0) {
763 printf("cryptodev_digest_final: illegal input\n");
764 return(0);
767 #ifndef COP_FLAG_UPDATE
768 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
769 /* if application doesn't support one buffer */
770 memset(&cryp, 0, sizeof(cryp));
771 cryp.ses = sess->ses;
772 cryp.flags = 0;
773 cryp.len = state->mac_len;
774 cryp.src = state->mac_data;
775 cryp.dst = NULL;
776 cryp.mac = (void*)md;
777 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
778 printf("cryptodev_digest_final: digest failed\n");
779 return (0);
782 return 1;
785 memcpy(md, state->digest_res, ctx->digest->md_size);
786 #else
787 memset(&cryp, 0, sizeof(cryp));
788 cryp.ses = sess->ses;
789 cryp.flags = COP_FLAG_FINAL;
790 cryp.len = state->mac_len;
791 cryp.src = state->mac_data;
792 cryp.dst = NULL;
793 cryp.mac = (void*)md;
794 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
795 printf("cryptodev_digest_final: digest failed\n");
796 return (0);
798 #endif
800 return 1;
804 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
806 int ret = 1;
807 struct dev_crypto_state *state = ctx->md_data;
808 struct session_op *sess = &state->d_sess;
810 if (state == NULL)
811 return 0;
813 if (state->d_fd < 0) {
814 printf("cryptodev_digest_cleanup: illegal input\n");
815 return (0);
818 if (state->mac_data) {
819 OPENSSL_free(state->mac_data);
820 state->mac_data = NULL;
821 state->mac_len = 0;
824 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
825 printf("cryptodev_digest_cleanup: failed to close session\n");
826 ret = 0;
827 } else {
828 ret = 1;
830 put_dev_crypto(state->d_fd);
831 state->d_fd = -1;
833 return (ret);
836 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
838 struct dev_crypto_state *fstate = from->md_data;
839 struct dev_crypto_state *dstate = to->md_data;
840 struct session_op *sess;
841 int digest;
843 if (dstate == NULL || fstate == NULL)
844 return 1;
846 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
848 sess = &dstate->d_sess;
850 digest = digest_nid_to_cryptodev(to->digest->type);
852 sess->mackey = NULL;
853 sess->mackeylen = 0;
854 sess->mac = digest;
856 dstate->d_fd = get_dev_crypto();
858 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
859 put_dev_crypto(dstate->d_fd);
860 dstate->d_fd = -1;
861 printf("cryptodev_digest_init: Open session failed\n");
862 return (0);
865 if (fstate->mac_len != 0) {
866 if (fstate->mac_data != NULL)
868 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
869 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
870 dstate->mac_len = fstate->mac_len;
874 return 1;
878 static const EVP_MD cryptodev_sha1 = {
879 NID_sha1,
880 NID_sha1WithRSAEncryption,
881 SHA_DIGEST_LENGTH,
882 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
883 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
884 EVP_MD_FLAG_DIGALGID_ABSENT|
885 #endif
886 EVP_MD_FLAG_ONESHOT,
887 cryptodev_digest_init,
888 cryptodev_digest_update,
889 cryptodev_digest_final,
890 cryptodev_digest_copy,
891 cryptodev_digest_cleanup,
892 EVP_PKEY_RSA_method,
893 SHA_CBLOCK,
894 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
897 static const EVP_MD cryptodev_sha256 = {
898 NID_sha256,
899 NID_sha256WithRSAEncryption,
900 SHA256_DIGEST_LENGTH,
901 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
902 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
903 EVP_MD_FLAG_DIGALGID_ABSENT|
904 #endif
905 EVP_MD_FLAG_ONESHOT,
906 cryptodev_digest_init,
907 cryptodev_digest_update,
908 cryptodev_digest_final,
909 cryptodev_digest_copy,
910 cryptodev_digest_cleanup,
911 EVP_PKEY_RSA_method,
912 SHA256_CBLOCK,
913 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
915 static const EVP_MD cryptodev_sha224 = {
916 NID_sha224,
917 NID_sha224WithRSAEncryption,
918 SHA224_DIGEST_LENGTH,
919 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
920 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
921 EVP_MD_FLAG_DIGALGID_ABSENT|
922 #endif
923 EVP_MD_FLAG_ONESHOT,
924 cryptodev_digest_init,
925 cryptodev_digest_update,
926 cryptodev_digest_final,
927 cryptodev_digest_copy,
928 cryptodev_digest_cleanup,
929 EVP_PKEY_RSA_method,
930 SHA256_CBLOCK,
931 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
934 static const EVP_MD cryptodev_sha384 = {
935 NID_sha384,
936 NID_sha384WithRSAEncryption,
937 SHA384_DIGEST_LENGTH,
938 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
939 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
940 EVP_MD_FLAG_DIGALGID_ABSENT|
941 #endif
942 EVP_MD_FLAG_ONESHOT,
943 cryptodev_digest_init,
944 cryptodev_digest_update,
945 cryptodev_digest_final,
946 cryptodev_digest_copy,
947 cryptodev_digest_cleanup,
948 EVP_PKEY_RSA_method,
949 SHA512_CBLOCK,
950 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
953 static const EVP_MD cryptodev_sha512 = {
954 NID_sha512,
955 NID_sha512WithRSAEncryption,
956 SHA512_DIGEST_LENGTH,
957 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
958 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
959 EVP_MD_FLAG_DIGALGID_ABSENT|
960 #endif
961 EVP_MD_FLAG_ONESHOT,
962 cryptodev_digest_init,
963 cryptodev_digest_update,
964 cryptodev_digest_final,
965 cryptodev_digest_copy,
966 cryptodev_digest_cleanup,
967 EVP_PKEY_RSA_method,
968 SHA512_CBLOCK,
969 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
972 static const EVP_MD cryptodev_md5 = {
973 NID_md5,
974 NID_md5WithRSAEncryption,
975 16 /* MD5_DIGEST_LENGTH */,
976 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
977 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
978 EVP_MD_FLAG_DIGALGID_ABSENT|
979 #endif
980 EVP_MD_FLAG_ONESHOT,
981 cryptodev_digest_init,
982 cryptodev_digest_update,
983 cryptodev_digest_final,
984 cryptodev_digest_copy,
985 cryptodev_digest_cleanup,
986 EVP_PKEY_RSA_method,
987 64 /* MD5_CBLOCK */,
988 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
991 #endif /* USE_CRYPTODEV_DIGESTS */
994 static int
995 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
996 const int **nids, int nid)
998 if (!digest)
999 return (cryptodev_usable_digests(nids));
1001 switch (nid) {
1002 #ifdef USE_CRYPTODEV_DIGESTS
1003 case NID_md5:
1004 *digest = &cryptodev_md5;
1005 break;
1006 case NID_sha1:
1007 *digest = &cryptodev_sha1;
1008 break;
1009 case NID_sha224:
1010 *digest = &cryptodev_sha224;
1011 break;
1012 case NID_sha256:
1013 *digest = &cryptodev_sha256;
1014 break;
1015 case NID_sha384:
1016 *digest = &cryptodev_sha384;
1017 break;
1018 case NID_sha512:
1019 *digest = &cryptodev_sha512;
1020 break;
1021 default:
1022 #endif /* USE_CRYPTODEV_DIGESTS */
1023 *digest = NULL;
1024 break;
1026 return (*digest != NULL);
1030 * Convert a BIGNUM to the representation that /dev/crypto needs.
1031 * Upon completion of use, the caller is responsible for freeing
1032 * crp->crp_p.
1034 static int
1035 bn2crparam(const BIGNUM *a, struct crparam *crp)
1037 int i, j, k;
1038 ssize_t bytes, bits;
1039 u_char *b;
1041 crp->crp_p = NULL;
1042 crp->crp_nbits = 0;
1044 bits = BN_num_bits(a);
1045 bytes = (bits + 7) / 8;
1047 b = malloc(bytes);
1048 if (b == NULL)
1049 return (1);
1050 memset(b, 0, bytes);
1052 crp->crp_p = (void*) b;
1053 crp->crp_nbits = bits;
1055 for (i = 0, j = 0; i < a->top; i++) {
1056 for (k = 0; k < BN_BITS2 / 8; k++) {
1057 if ((j + k) >= bytes)
1058 return (0);
1059 b[j + k] = a->d[i] >> (k * 8);
1061 j += BN_BITS2 / 8;
1063 return (0);
1066 /* Convert a /dev/crypto parameter to a BIGNUM */
1067 static int
1068 crparam2bn(struct crparam *crp, BIGNUM *a)
1070 u_int8_t *pd;
1071 int i, bytes;
1073 bytes = (crp->crp_nbits + 7) / 8;
1075 if (bytes == 0)
1076 return (-1);
1078 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1079 return (-1);
1081 for (i = 0; i < bytes; i++)
1082 pd[i] = crp->crp_p[bytes - i - 1];
1084 BN_bin2bn(pd, bytes, a);
1085 free(pd);
1087 return (0);
1090 static void
1091 zapparams(struct crypt_kop *kop)
1093 int i;
1095 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1096 if (kop->crk_param[i].crp_p)
1097 free(kop->crk_param[i].crp_p);
1098 kop->crk_param[i].crp_p = NULL;
1099 kop->crk_param[i].crp_nbits = 0;
1103 static int
1104 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1106 int fd, ret = -1;
1108 if ((fd = get_asym_dev_crypto()) < 0)
1109 return (ret);
1111 if (r) {
1112 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1113 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1114 kop->crk_oparams++;
1116 if (s) {
1117 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1118 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1119 kop->crk_oparams++;
1122 if (ioctl(fd, CIOCKEY, kop) == 0) {
1123 if (r)
1124 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1125 if (s)
1126 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1127 ret = 0;
1130 return (ret);
1133 static int
1134 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1135 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1137 struct crypt_kop kop;
1138 int ret = 1;
1140 /* Currently, we know we can do mod exp iff we can do any
1141 * asymmetric operations at all.
1143 if (cryptodev_asymfeat == 0) {
1144 ret = BN_mod_exp(r, a, p, m, ctx);
1145 return (ret);
1148 memset(&kop, 0, sizeof kop);
1149 kop.crk_op = CRK_MOD_EXP;
1151 /* inputs: a^p % m */
1152 if (bn2crparam(a, &kop.crk_param[0]))
1153 goto err;
1154 if (bn2crparam(p, &kop.crk_param[1]))
1155 goto err;
1156 if (bn2crparam(m, &kop.crk_param[2]))
1157 goto err;
1158 kop.crk_iparams = 3;
1160 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1161 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1162 printf("OCF asym process failed, Running in software\n");
1163 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1165 } else if (ECANCELED == kop.crk_status) {
1166 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1167 printf("OCF hardware operation cancelled. Running in Software\n");
1168 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1170 /* else cryptodev operation worked ok ==> ret = 1*/
1172 err:
1173 zapparams(&kop);
1174 return (ret);
1177 static int
1178 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1180 int r;
1181 ctx = BN_CTX_new();
1182 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1183 BN_CTX_free(ctx);
1184 return (r);
1187 static int
1188 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1190 struct crypt_kop kop;
1191 int ret = 1;
1193 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1194 /* XXX 0 means failure?? */
1195 return (0);
1198 memset(&kop, 0, sizeof kop);
1199 kop.crk_op = CRK_MOD_EXP_CRT;
1200 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1201 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1202 goto err;
1203 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1204 goto err;
1205 if (bn2crparam(I, &kop.crk_param[2]))
1206 goto err;
1207 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1208 goto err;
1209 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1210 goto err;
1211 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1212 goto err;
1213 kop.crk_iparams = 6;
1215 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1216 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1217 printf("OCF asym process failed, running in Software\n");
1218 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1220 } else if (ECANCELED == kop.crk_status) {
1221 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1222 printf("OCF hardware operation cancelled. Running in Software\n");
1223 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1225 /* else cryptodev operation worked ok ==> ret = 1*/
1227 err:
1228 zapparams(&kop);
1229 return (ret);
1232 static RSA_METHOD cryptodev_rsa = {
1233 "cryptodev RSA method",
1234 NULL, /* rsa_pub_enc */
1235 NULL, /* rsa_pub_dec */
1236 NULL, /* rsa_priv_enc */
1237 NULL, /* rsa_priv_dec */
1238 NULL,
1239 NULL,
1240 NULL, /* init */
1241 NULL, /* finish */
1242 0, /* flags */
1243 NULL, /* app_data */
1244 NULL, /* rsa_sign */
1245 NULL /* rsa_verify */
1248 static int
1249 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1250 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1252 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1255 static int
1256 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1257 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1258 BN_CTX *ctx, BN_MONT_CTX *mont)
1260 BIGNUM t2;
1261 int ret = 0;
1263 BN_init(&t2);
1265 /* v = ( g^u1 * y^u2 mod p ) mod q */
1266 /* let t1 = g ^ u1 mod p */
1267 ret = 0;
1269 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1270 goto err;
1272 /* let t2 = y ^ u2 mod p */
1273 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1274 goto err;
1275 /* let u1 = t1 * t2 mod p */
1276 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1277 goto err;
1279 BN_copy(t1,u1);
1281 ret = 1;
1282 err:
1283 BN_free(&t2);
1284 return(ret);
1287 static DSA_SIG *
1288 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1290 struct crypt_kop kop;
1291 BIGNUM *r = NULL, *s = NULL;
1292 DSA_SIG *dsaret = NULL;
1294 if ((r = BN_new()) == NULL)
1295 goto err;
1296 if ((s = BN_new()) == NULL) {
1297 BN_free(r);
1298 goto err;
1301 memset(&kop, 0, sizeof kop);
1302 kop.crk_op = CRK_DSA_SIGN;
1304 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1305 kop.crk_param[0].crp_p = (void*)dgst;
1306 kop.crk_param[0].crp_nbits = dlen * 8;
1307 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1308 goto err;
1309 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1310 goto err;
1311 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1312 goto err;
1313 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1314 goto err;
1315 kop.crk_iparams = 5;
1317 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1318 BN_num_bytes(dsa->q), s) == 0) {
1319 dsaret = DSA_SIG_new();
1320 dsaret->r = r;
1321 dsaret->s = s;
1322 } else {
1323 const DSA_METHOD *meth = DSA_OpenSSL();
1324 BN_free(r);
1325 BN_free(s);
1326 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1328 err:
1329 kop.crk_param[0].crp_p = NULL;
1330 zapparams(&kop);
1331 return (dsaret);
1334 static int
1335 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1336 DSA_SIG *sig, DSA *dsa)
1338 struct crypt_kop kop;
1339 int dsaret = 1;
1341 memset(&kop, 0, sizeof kop);
1342 kop.crk_op = CRK_DSA_VERIFY;
1344 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1345 kop.crk_param[0].crp_p = (void*)dgst;
1346 kop.crk_param[0].crp_nbits = dlen * 8;
1347 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1348 goto err;
1349 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1350 goto err;
1351 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1352 goto err;
1353 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1354 goto err;
1355 if (bn2crparam(sig->r, &kop.crk_param[5]))
1356 goto err;
1357 if (bn2crparam(sig->s, &kop.crk_param[6]))
1358 goto err;
1359 kop.crk_iparams = 7;
1361 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1362 /*OCF success value is 0, if not zero, change dsaret to fail*/
1363 if(0 != kop.crk_status) dsaret = 0;
1364 } else {
1365 const DSA_METHOD *meth = DSA_OpenSSL();
1367 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1369 err:
1370 kop.crk_param[0].crp_p = NULL;
1371 zapparams(&kop);
1372 return (dsaret);
1375 static DSA_METHOD cryptodev_dsa = {
1376 "cryptodev DSA method",
1377 NULL,
1378 NULL, /* dsa_sign_setup */
1379 NULL,
1380 NULL, /* dsa_mod_exp */
1381 NULL,
1382 NULL, /* init */
1383 NULL, /* finish */
1384 0, /* flags */
1385 NULL /* app_data */
1388 static int
1389 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1390 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1391 BN_MONT_CTX *m_ctx)
1393 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1396 static int
1397 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1399 struct crypt_kop kop;
1400 int dhret = 1;
1401 int fd, keylen;
1403 if ((fd = get_asym_dev_crypto()) < 0) {
1404 const DH_METHOD *meth = DH_OpenSSL();
1406 return ((meth->compute_key)(key, pub_key, dh));
1409 keylen = BN_num_bits(dh->p);
1411 memset(&kop, 0, sizeof kop);
1412 kop.crk_op = CRK_DH_COMPUTE_KEY;
1414 /* inputs: dh->priv_key pub_key dh->p key */
1415 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1416 goto err;
1417 if (bn2crparam(pub_key, &kop.crk_param[1]))
1418 goto err;
1419 if (bn2crparam(dh->p, &kop.crk_param[2]))
1420 goto err;
1421 kop.crk_iparams = 3;
1423 kop.crk_param[3].crp_p = (void*) key;
1424 kop.crk_param[3].crp_nbits = keylen * 8;
1425 kop.crk_oparams = 1;
1427 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1428 const DH_METHOD *meth = DH_OpenSSL();
1430 dhret = (meth->compute_key)(key, pub_key, dh);
1432 err:
1433 kop.crk_param[3].crp_p = NULL;
1434 zapparams(&kop);
1435 return (dhret);
1438 static DH_METHOD cryptodev_dh = {
1439 "cryptodev DH method",
1440 NULL, /* cryptodev_dh_generate_key */
1441 NULL,
1442 NULL,
1443 NULL,
1444 NULL,
1445 0, /* flags */
1446 NULL /* app_data */
1450 * ctrl right now is just a wrapper that doesn't do much
1451 * but I expect we'll want some options soon.
1453 static int
1454 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1456 #ifdef HAVE_SYSLOG_R
1457 struct syslog_data sd = SYSLOG_DATA_INIT;
1458 #endif
1460 switch (cmd) {
1461 default:
1462 #ifdef HAVE_SYSLOG_R
1463 syslog_r(LOG_ERR, &sd,
1464 "cryptodev_ctrl: unknown command %d", cmd);
1465 #else
1466 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1467 #endif
1468 break;
1470 return (1);
1473 void
1474 ENGINE_load_cryptodev(void)
1476 ENGINE *engine = ENGINE_new();
1477 int fd;
1479 if (engine == NULL)
1480 return;
1481 if ((fd = get_dev_crypto()) < 0) {
1482 ENGINE_free(engine);
1483 return;
1487 * find out what asymmetric crypto algorithms we support
1489 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1490 put_dev_crypto(fd);
1491 ENGINE_free(engine);
1492 return;
1494 put_dev_crypto(fd);
1496 if (!ENGINE_set_id(engine, "cryptodev") ||
1497 !ENGINE_set_name(engine, "cryptodev engine") ||
1498 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1499 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1500 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1501 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1502 ENGINE_free(engine);
1503 return;
1506 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1507 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1509 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1510 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1511 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1512 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1513 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1514 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1515 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1516 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1517 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1518 cryptodev_rsa.rsa_mod_exp =
1519 cryptodev_rsa_mod_exp;
1520 else
1521 cryptodev_rsa.rsa_mod_exp =
1522 cryptodev_rsa_nocrt_mod_exp;
1526 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1527 const DSA_METHOD *meth = DSA_OpenSSL();
1529 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1530 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1531 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1532 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1533 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1534 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1536 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1537 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1540 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1541 const DH_METHOD *dh_meth = DH_OpenSSL();
1543 cryptodev_dh.generate_key = dh_meth->generate_key;
1544 cryptodev_dh.compute_key = dh_meth->compute_key;
1545 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1546 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1547 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1548 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1549 cryptodev_dh.compute_key =
1550 cryptodev_dh_compute_key;
1554 ENGINE_add(engine);
1555 ENGINE_free(engine);
1556 ERR_clear_error();
1559 #endif /* HAVE_CRYPTODEV */