2 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3 * Copyright (c) 2002 Theo de Raadt
4 * Copyright (c) 2002 Markus Friedl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <openssl/objects.h>
30 #include <openssl/engine.h>
31 #include <openssl/evp.h>
32 #include <openssl/bn.h>
34 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
35 (defined(OpenBSD) || defined(__FreeBSD__))
36 # include <sys/param.h>
37 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
38 # define HAVE_CRYPTODEV
40 # if (OpenBSD >= 200110)
41 # define HAVE_SYSLOG_R
45 #include <sys/types.h>
47 # include <crypto/cryptodev.h>
48 # include <sys/ioctl.h>
59 # include <openssl/dh.h>
61 #include <openssl/dsa.h>
62 #include <openssl/err.h>
63 #include <openssl/rsa.h>
65 #ifndef HAVE_CRYPTODEV
67 void ENGINE_load_cryptodev(void)
69 /* This is a NOP on platforms without /dev/crypto */
75 struct dev_crypto_state {
76 struct session_op d_sess;
78 # ifdef USE_CRYPTODEV_DIGESTS
79 char dummy_mac_key[HASH_MAX_LEN];
80 unsigned char digest_res[HASH_MAX_LEN];
86 static u_int32_t cryptodev_asymfeat = 0;
88 static int get_asym_dev_crypto(void);
89 static int open_dev_crypto(void);
90 static int get_dev_crypto(void);
91 static int get_cryptodev_ciphers(const int **cnids);
92 # ifdef USE_CRYPTODEV_DIGESTS
93 static int get_cryptodev_digests(const int **cnids);
95 static int cryptodev_usable_ciphers(const int **nids);
96 static int cryptodev_usable_digests(const int **nids);
97 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
98 const unsigned char *in, size_t inl);
99 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
100 const unsigned char *iv, int enc);
101 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
102 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
103 const int **nids, int nid);
104 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
105 const int **nids, int nid);
106 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
107 static int crparam2bn(struct crparam *crp, BIGNUM *a);
108 static void zapparams(struct crypt_kop *kop);
109 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
110 int slen, BIGNUM *s);
112 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
113 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
115 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
117 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
119 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
120 const BIGNUM *p, const BIGNUM *m,
121 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
122 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
123 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
124 BIGNUM *p, BN_CTX *ctx,
126 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
128 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
129 DSA_SIG *sig, DSA *dsa);
130 #ifndef OPENSSL_NO_DH
131 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
132 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
134 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
137 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
139 void ENGINE_load_cryptodev(void);
141 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
152 CRYPTO_ARC4, NID_rc4, 0, 16,
155 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
158 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
161 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
164 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
167 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
169 # ifdef CRYPTO_AES_CTR
171 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
174 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
177 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
181 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
184 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
187 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
194 # ifdef USE_CRYPTODEV_DIGESTS
201 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
204 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
207 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
211 CRYPTO_MD5_KPDK, NID_undef, 0
214 CRYPTO_SHA1_KPDK, NID_undef, 0
217 CRYPTO_MD5, NID_md5, 16
220 CRYPTO_SHA1, NID_sha1, 20
229 * Return a fd if /dev/crypto seems usable, 0 otherwise.
231 static int open_dev_crypto(void)
236 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
239 if (fcntl(fd, F_SETFD, 1) == -1) {
248 static int get_dev_crypto(void)
252 if ((fd = open_dev_crypto()) == -1)
254 # ifndef CRIOGET_NOT_NEEDED
255 if (ioctl(fd, CRIOGET, &retfd) == -1)
259 if (fcntl(retfd, F_SETFD, 1) == -1) {
269 static void put_dev_crypto(int fd)
271 # ifndef CRIOGET_NOT_NEEDED
276 /* Caching version for asym operations */
277 static int get_asym_dev_crypto(void)
282 fd = get_dev_crypto();
287 * Find out what ciphers /dev/crypto will let us have a session for.
288 * XXX note, that some of these openssl doesn't deal with yet!
289 * returning them here is harmless, as long as we return NULL
290 * when asked for a handler in the cryptodev_engine_ciphers routine
292 static int get_cryptodev_ciphers(const int **cnids)
294 static int nids[CRYPTO_ALGORITHM_MAX];
295 struct session_op sess;
296 int fd, i, count = 0;
298 if ((fd = get_dev_crypto()) < 0) {
302 memset(&sess, 0, sizeof(sess));
303 sess.key = (caddr_t) "123456789abcdefghijklmno";
305 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
306 if (ciphers[i].nid == NID_undef)
308 sess.cipher = ciphers[i].id;
309 sess.keylen = ciphers[i].keylen;
311 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
312 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
313 nids[count++] = ciphers[i].nid;
324 # ifdef USE_CRYPTODEV_DIGESTS
326 * Find out what digests /dev/crypto will let us have a session for.
327 * XXX note, that some of these openssl doesn't deal with yet!
328 * returning them here is harmless, as long as we return NULL
329 * when asked for a handler in the cryptodev_engine_digests routine
331 static int get_cryptodev_digests(const int **cnids)
333 static int nids[CRYPTO_ALGORITHM_MAX];
334 struct session_op sess;
335 int fd, i, count = 0;
337 if ((fd = get_dev_crypto()) < 0) {
341 memset(&sess, 0, sizeof(sess));
342 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
343 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
344 if (digests[i].nid == NID_undef)
346 sess.mac = digests[i].id;
347 sess.mackeylen = digests[i].keylen;
349 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
350 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
351 nids[count++] = digests[i].nid;
364 * Find the useable ciphers|digests from dev/crypto - this is the first
365 * thing called by the engine init crud which determines what it
366 * can use for ciphers from this engine. We want to return
367 * only what we can do, anythine else is handled by software.
369 * If we can't initialize the device to do anything useful for
370 * any reason, we want to return a NULL array, and 0 length,
371 * which forces everything to be done is software. By putting
372 * the initalization of the device in here, we ensure we can
373 * use this engine as the default, and if for whatever reason
374 * /dev/crypto won't do what we want it will just be done in
377 * This can (should) be greatly expanded to perhaps take into
378 * account speed of the device, and what we want to do.
379 * (although the disabling of particular alg's could be controlled
380 * by the device driver with sysctl's.) - this is where we
381 * want most of the decisions made about what we actually want
382 * to use from /dev/crypto.
384 static int cryptodev_usable_ciphers(const int **nids)
386 return (get_cryptodev_ciphers(nids));
389 static int cryptodev_usable_digests(const int **nids)
391 # ifdef USE_CRYPTODEV_DIGESTS
392 return (get_cryptodev_digests(nids));
395 * XXXX just disable all digests for now, because it sucks.
396 * we need a better way to decide this - i.e. I may not
397 * want digests on slow cards like hifn on fast machines,
398 * but might want them on slow or loaded machines, etc.
399 * will also want them when using crypto cards that don't
400 * suck moose gonads - would be nice to be able to decide something
401 * as reasonable default without having hackery that's card dependent.
402 * of course, the default should probably be just do everything,
403 * with perhaps a sysctl to turn algoritms off (or have them off
404 * by default) on cards that generally suck like the hifn.
412 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
413 const unsigned char *in, size_t inl)
415 struct crypt_op cryp;
416 struct dev_crypto_state *state = EVP_CIPHER_CTX_cipher_data(ctx);
417 struct session_op *sess = &state->d_sess;
419 unsigned char save_iv[EVP_MAX_IV_LENGTH];
425 if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
428 memset(&cryp, 0, sizeof(cryp));
430 cryp.ses = sess->ses;
433 cryp.src = (caddr_t) in;
434 cryp.dst = (caddr_t) out;
437 cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
439 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
440 cryp.iv = *(caddr_t*) EVP_CIPHER_CTX_iv(ctx);
441 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
442 iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
443 memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
448 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
450 * XXX need better errror handling this can fail for a number of
456 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
457 if (EVP_CIPHER_CTX_encrypting(ctx))
458 iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
461 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
462 EVP_CIPHER_CTX_iv_length(ctx));
468 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
469 const unsigned char *iv, int enc)
471 struct dev_crypto_state *state = EVP_CIPHER_CTX_cipher_data(ctx);
472 struct session_op *sess = &state->d_sess;
475 for (i = 0; ciphers[i].id; i++)
476 if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
477 EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
478 EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
479 cipher = ciphers[i].id;
483 if (!ciphers[i].id) {
488 memset(sess, 0, sizeof(*sess));
490 if ((state->d_fd = get_dev_crypto()) < 0)
493 sess->key = (caddr_t) key;
494 sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
495 sess->cipher = cipher;
497 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
498 put_dev_crypto(state->d_fd);
506 * free anything we allocated earlier when initting a
507 * session, and close the session.
509 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
512 struct dev_crypto_state *state = EVP_CIPHER_CTX_cipher_data(ctx);
513 struct session_op *sess = &state->d_sess;
519 * XXX if this ioctl fails, someting's wrong. the invoker may have called
520 * us with a bogus ctx, or we could have a device that for whatever
521 * reason just doesn't want to play ball - it's not clear what's right
522 * here - should this be an error? should it just increase a counter,
523 * hmm. For right now, we return 0 - I don't believe that to be "right".
524 * we could call the gorpy openssl lib error handlers that print messages
525 * to users of the library. hmm..
528 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
533 put_dev_crypto(state->d_fd);
540 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
541 * gets called when libcrypto requests a cipher NID.
545 static EVP_CIPHER *rc4_cipher = NULL;
546 static const EVP_CIPHER *cryptodev_rc4(void)
548 if (rc4_cipher == NULL) {
551 if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16)) == NULL
552 || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
553 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
554 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
555 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
556 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
557 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))) {
558 EVP_CIPHER_meth_free(cipher);
567 static EVP_CIPHER *des_cbc_cipher = NULL;
568 static const EVP_CIPHER *cryptodev_des_cbc(void)
570 if (des_cbc_cipher == NULL) {
573 if ((cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8)) == NULL
574 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
575 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
576 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
577 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
578 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
579 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
580 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
581 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
582 EVP_CIPHER_meth_free(cipher);
585 des_cbc_cipher = cipher;
587 return des_cbc_cipher;
591 static EVP_CIPHER *des3_cbc_cipher = NULL;
592 static const EVP_CIPHER *cryptodev_3des_cbc(void)
594 if (des3_cbc_cipher == NULL) {
597 if ((cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24)) == NULL
598 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
599 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
600 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
601 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
602 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
603 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
604 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
605 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
606 EVP_CIPHER_meth_free(cipher);
609 des3_cbc_cipher = cipher;
611 return des3_cbc_cipher;
614 static EVP_CIPHER *bf_cbc_cipher = NULL;
615 static const EVP_CIPHER *cryptodev_bf_cbc(void)
617 if (bf_cbc_cipher == NULL) {
620 if ((cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16)) == NULL
621 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
622 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
623 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
624 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
625 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
626 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
627 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
628 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
629 EVP_CIPHER_meth_free(cipher);
632 bf_cbc_cipher = cipher;
634 return bf_cbc_cipher;
637 static EVP_CIPHER *cast_cbc_cipher = NULL;
638 static const EVP_CIPHER *cryptodev_cast_cbc(void)
640 if (cast_cbc_cipher == NULL) {
643 if ((cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16)) == NULL
644 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
645 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
646 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
647 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
648 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
649 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
650 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
651 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
652 EVP_CIPHER_meth_free(cipher);
655 cast_cbc_cipher = cipher;
657 return cast_cbc_cipher;
660 static EVP_CIPHER *aes_cbc_cipher = NULL;
661 static const EVP_CIPHER *cryptodev_aes_cbc(void)
663 if (aes_cbc_cipher == NULL) {
666 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16)) == NULL
667 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
668 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
669 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
670 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
671 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
672 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
673 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
674 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
675 EVP_CIPHER_meth_free(cipher);
678 aes_cbc_cipher = cipher;
680 return aes_cbc_cipher;
683 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
684 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
686 if (aes_192_cbc_cipher == NULL) {
689 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24)) == NULL
690 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
691 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
692 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
693 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
694 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
695 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
696 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
697 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
698 EVP_CIPHER_meth_free(cipher);
701 aes_192_cbc_cipher = cipher;
703 return aes_192_cbc_cipher;
706 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
707 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
709 if (aes_256_cbc_cipher == NULL) {
712 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32)) == NULL
713 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
714 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
715 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
716 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
717 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
718 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
719 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
720 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
721 EVP_CIPHER_meth_free(cipher);
724 aes_256_cbc_cipher = cipher;
726 return aes_256_cbc_cipher;
729 # ifdef CRYPTO_AES_CTR
730 static EVP_CIPHER *aes_ctr_cipher = NULL;
731 static const EVP_CIPHER *cryptodev_aes_ctr(void)
733 if (aes_ctr_cipher == NULL) {
736 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16)) == NULL
737 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
738 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
739 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
740 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
741 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
742 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
743 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
744 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
745 EVP_CIPHER_meth_free(cipher);
748 aes_ctr_cipher = cipher;
750 return aes_ctr_cipher;
753 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
754 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
756 if (aes_192_ctr_cipher == NULL) {
759 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24)) == NULL
760 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
761 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
762 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
763 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
764 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
765 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
766 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
767 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
768 EVP_CIPHER_meth_free(cipher);
771 aes_192_ctr_cipher = cipher;
773 return aes_192_ctr_cipher;
776 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
777 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
779 if (aes_256_ctr_cipher == NULL) {
782 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32)) == NULL
783 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
784 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
785 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
786 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
787 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
788 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
789 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
790 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
791 EVP_CIPHER_meth_free(cipher);
794 aes_256_ctr_cipher = cipher;
796 return aes_256_ctr_cipher;
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
805 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
806 const int **nids, int nid)
809 return (cryptodev_usable_ciphers(nids));
813 *cipher = cryptodev_rc4();
815 case NID_des_ede3_cbc:
816 *cipher = cryptodev_3des_cbc();
819 *cipher = cryptodev_des_cbc();
822 *cipher = cryptodev_bf_cbc();
825 *cipher = cryptodev_cast_cbc();
827 case NID_aes_128_cbc:
828 *cipher = cryptodev_aes_cbc();
830 case NID_aes_192_cbc:
831 *cipher = cryptodev_aes_192_cbc();
833 case NID_aes_256_cbc:
834 *cipher = cryptodev_aes_256_cbc();
836 # ifdef CRYPTO_AES_CTR
837 case NID_aes_128_ctr:
838 *cipher = cryptodev_aes_ctr();
840 case NID_aes_192_ctr:
841 *cipher = cryptodev_aes_ctr_192();
843 case NID_aes_256_ctr:
844 *cipher = cryptodev_aes_ctr_256();
851 return (*cipher != NULL);
854 # ifdef USE_CRYPTODEV_DIGESTS
856 /* convert digest type to cryptodev */
857 static int digest_nid_to_cryptodev(int nid)
861 for (i = 0; digests[i].id; i++)
862 if (digests[i].nid == nid)
863 return (digests[i].id);
867 static int digest_key_length(int nid)
871 for (i = 0; digests[i].id; i++)
872 if (digests[i].nid == nid)
873 return digests[i].keylen;
877 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
879 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
880 struct session_op *sess = &state->d_sess;
883 if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
884 printf("cryptodev_digest_init: Can't get digest \n");
888 memset(state, 0, sizeof(*state));
890 if ((state->d_fd = get_dev_crypto()) < 0) {
891 printf("cryptodev_digest_init: Can't get Dev \n");
895 sess->mackey = state->dummy_mac_key;
896 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(ctx));
899 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
900 put_dev_crypto(state->d_fd);
902 printf("cryptodev_digest_init: Open session failed\n");
909 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
912 struct crypt_op cryp;
913 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
914 struct session_op *sess = &state->d_sess;
917 if (!data || state->d_fd < 0) {
918 printf("cryptodev_digest_update: illegal inputs \n");
926 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
927 /* if application doesn't support one buffer */
929 OPENSSL_realloc(state->mac_data, state->mac_len + count);
932 printf("cryptodev_digest_update: realloc failed\n");
935 state->mac_data = new_mac_data;
937 memcpy(state->mac_data + state->mac_len, data, count);
938 state->mac_len += count;
943 memset(&cryp, 0, sizeof(cryp));
945 cryp.ses = sess->ses;
948 cryp.src = (caddr_t) data;
950 cryp.mac = (caddr_t) state->digest_res;
951 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
952 printf("cryptodev_digest_update: digest failed\n");
958 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
960 struct crypt_op cryp;
961 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
962 struct session_op *sess = &state->d_sess;
966 if (!md || state->d_fd < 0) {
967 printf("cryptodev_digest_final: illegal input\n");
971 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
972 /* if application doesn't support one buffer */
973 memset(&cryp, 0, sizeof(cryp));
974 cryp.ses = sess->ses;
976 cryp.len = state->mac_len;
977 cryp.src = state->mac_data;
979 cryp.mac = (caddr_t) md;
980 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
981 printf("cryptodev_digest_final: digest failed\n");
988 memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
993 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
996 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
997 struct session_op *sess = &state->d_sess;
1002 if (state->d_fd < 0) {
1003 printf("cryptodev_digest_cleanup: illegal input\n");
1007 OPENSSL_free(state->mac_data);
1008 state->mac_data = NULL;
1011 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1012 printf("cryptodev_digest_cleanup: failed to close session\n");
1017 put_dev_crypto(state->d_fd);
1023 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1025 struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1026 struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1027 struct session_op *sess;
1030 if (dstate == NULL || fstate == NULL)
1033 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1035 sess = &dstate->d_sess;
1037 digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1039 sess->mackey = dstate->dummy_mac_key;
1040 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(to));
1043 dstate->d_fd = get_dev_crypto();
1045 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1046 put_dev_crypto(dstate->d_fd);
1048 printf("cryptodev_digest_copy: Open session failed\n");
1052 if (fstate->mac_len != 0) {
1053 if (fstate->mac_data != NULL) {
1054 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1055 if (dstate->mac_data == NULL) {
1056 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1059 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1060 dstate->mac_len = fstate->mac_len;
1067 static EVP_MD *sha1_md = NULL;
1068 static const EVP_MD *cryptodev_sha1(void)
1070 if (sha1_md == NULL) {
1073 if ((md = EVP_MD_meth_new(NID_sha1, NID_undef)) == NULL
1074 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1075 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1076 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1077 || !EVP_MD_meth_set_app_datasize(md,
1078 sizeof(struct dev_crypto_state))
1079 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1080 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1081 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1082 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1083 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1084 EVP_MD_meth_free(md);
1092 static EVP_MD *md5_md = NULL;
1093 static const EVP_MD *cryptodev_md5(void)
1095 if (md5_md == NULL) {
1098 if ((md = EVP_MD_meth_new(NID_md5, NID_undef)) == NULL
1099 || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */)
1100 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1101 || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */)
1102 || !EVP_MD_meth_set_app_datasize(md,
1103 sizeof(struct dev_crypto_state))
1104 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1105 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1106 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1107 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1108 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1109 EVP_MD_meth_free(md);
1117 # endif /* USE_CRYPTODEV_DIGESTS */
1120 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1121 const int **nids, int nid)
1124 return (cryptodev_usable_digests(nids));
1127 # ifdef USE_CRYPTODEV_DIGESTS
1129 *digest = cryptodev_md5();
1132 *digest = cryptodev_sha1();
1135 # endif /* USE_CRYPTODEV_DIGESTS */
1139 return (*digest != NULL);
1142 static int cryptodev_engine_destroy(ENGINE *e)
1144 EVP_CIPHER_meth_free(rc4_cipher);
1146 EVP_CIPHER_meth_free(des_cbc_cipher);
1147 des_cbc_cipher = NULL;
1148 EVP_CIPHER_meth_free(des3_cbc_cipher);
1149 des3_cbc_cipher = NULL;
1150 EVP_CIPHER_meth_free(bf_cbc_cipher);
1151 bf_cbc_cipher = NULL;
1152 EVP_CIPHER_meth_free(cast_cbc_cipher);
1153 cast_cbc_cipher = NULL;
1154 EVP_CIPHER_meth_free(aes_cbc_cipher);
1155 aes_cbc_cipher = NULL;
1156 EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1157 aes_192_cbc_cipher = NULL;
1158 EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1159 aes_256_cbc_cipher = NULL;
1160 # ifdef CRYPTO_AES_CTR
1161 EVP_CIPHER_meth_free(aes_ctr_cipher);
1162 aes_ctr_cipher = NULL;
1163 EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1164 aes_192_ctr_cipher = NULL;
1165 EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1166 aes_256_ctr_cipher = NULL;
1168 # ifdef USE_CRYPTODEV_DIGESTS
1169 EVP_MD_meth_free(sha1_md);
1171 EVP_MD_meth_free(md5_md);
1178 * Convert a BIGNUM to the representation that /dev/crypto needs.
1179 * Upon completion of use, the caller is responsible for freeing
1182 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1184 ssize_t bytes, bits;
1190 bits = BN_num_bits(a);
1191 bytes = BN_num_bytes(a);
1193 b = OPENSSL_zalloc(bytes);
1197 crp->crp_p = (caddr_t) b;
1198 crp->crp_nbits = bits;
1204 /* Convert a /dev/crypto parameter to a BIGNUM */
1205 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1210 bytes = (crp->crp_nbits + 7) / 8;
1215 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1218 for (i = 0; i < bytes; i++)
1219 pd[i] = crp->crp_p[bytes - i - 1];
1221 BN_bin2bn(pd, bytes, a);
1227 static void zapparams(struct crypt_kop *kop)
1231 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1232 if (kop->crk_param[i].crp_p)
1233 free(kop->crk_param[i].crp_p);
1234 kop->crk_param[i].crp_p = NULL;
1235 kop->crk_param[i].crp_nbits = 0;
1240 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1245 if ((fd = get_asym_dev_crypto()) < 0)
1249 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1250 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1254 kop->crk_param[kop->crk_iparams + 1].crp_p =
1255 calloc(slen, sizeof(char));
1256 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1260 if (ioctl(fd, CIOCKEY, kop) == 0) {
1262 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1264 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1272 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1273 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1275 struct crypt_kop kop;
1279 * Currently, we know we can do mod exp iff we can do any asymmetric
1280 * operations at all.
1282 if (cryptodev_asymfeat == 0) {
1283 ret = BN_mod_exp(r, a, p, m, ctx);
1287 memset(&kop, 0, sizeof(kop));
1288 kop.crk_op = CRK_MOD_EXP;
1290 /* inputs: a^p % m */
1291 if (bn2crparam(a, &kop.crk_param[0]))
1293 if (bn2crparam(p, &kop.crk_param[1]))
1295 if (bn2crparam(m, &kop.crk_param[2]))
1297 kop.crk_iparams = 3;
1299 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1300 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1301 printf("OCF asym process failed, Running in software\n");
1302 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1304 } else if (ECANCELED == kop.crk_status) {
1305 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1306 printf("OCF hardware operation cancelled. Running in Software\n");
1307 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1309 /* else cryptodev operation worked ok ==> ret = 1 */
1317 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1322 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1328 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1330 struct crypt_kop kop;
1333 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1334 /* XXX 0 means failure?? */
1338 memset(&kop, 0, sizeof(kop));
1339 kop.crk_op = CRK_MOD_EXP_CRT;
1340 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1341 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1343 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1345 if (bn2crparam(I, &kop.crk_param[2]))
1347 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1349 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1351 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1353 kop.crk_iparams = 6;
1355 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1356 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1357 printf("OCF asym process failed, running in Software\n");
1358 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1360 } else if (ECANCELED == kop.crk_status) {
1361 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1362 printf("OCF hardware operation cancelled. Running in Software\n");
1363 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1365 /* else cryptodev operation worked ok ==> ret = 1 */
1372 static RSA_METHOD cryptodev_rsa = {
1373 "cryptodev RSA method",
1374 NULL, /* rsa_pub_enc */
1375 NULL, /* rsa_pub_dec */
1376 NULL, /* rsa_priv_enc */
1377 NULL, /* rsa_priv_dec */
1383 NULL, /* app_data */
1384 NULL, /* rsa_sign */
1385 NULL /* rsa_verify */
1389 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1390 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1392 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1396 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1397 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1398 BN_CTX *ctx, BN_MONT_CTX *mont)
1407 /* v = ( g^u1 * y^u2 mod p ) mod q */
1408 /* let t1 = g ^ u1 mod p */
1411 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
1414 /* let t2 = y ^ u2 mod p */
1415 if (!dsa->meth->bn_mod_exp(dsa, t2, dsa->pub_key, u2, dsa->p, ctx, mont))
1417 /* let u1 = t1 * t2 mod p */
1418 if (!BN_mod_mul(u1, t1, t2, dsa->p, ctx))
1429 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1432 struct crypt_kop kop;
1433 BIGNUM *r = NULL, *s = NULL;
1434 DSA_SIG *dsaret = NULL;
1436 if ((r = BN_new()) == NULL)
1438 if ((s = BN_new()) == NULL) {
1443 memset(&kop, 0, sizeof(kop));
1444 kop.crk_op = CRK_DSA_SIGN;
1446 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1447 kop.crk_param[0].crp_p = (caddr_t) dgst;
1448 kop.crk_param[0].crp_nbits = dlen * 8;
1449 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1451 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1453 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1455 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1457 kop.crk_iparams = 5;
1459 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1460 BN_num_bytes(dsa->q), s) == 0) {
1461 dsaret = DSA_SIG_new();
1467 const DSA_METHOD *meth = DSA_OpenSSL();
1470 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
1473 kop.crk_param[0].crp_p = NULL;
1479 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1480 DSA_SIG *sig, DSA *dsa)
1482 struct crypt_kop kop;
1485 memset(&kop, 0, sizeof(kop));
1486 kop.crk_op = CRK_DSA_VERIFY;
1488 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1489 kop.crk_param[0].crp_p = (caddr_t) dgst;
1490 kop.crk_param[0].crp_nbits = dlen * 8;
1491 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1493 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1495 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1497 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1499 if (bn2crparam(sig->r, &kop.crk_param[5]))
1501 if (bn2crparam(sig->s, &kop.crk_param[6]))
1503 kop.crk_iparams = 7;
1505 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1507 * OCF success value is 0, if not zero, change dsaret to fail
1509 if (0 != kop.crk_status)
1512 const DSA_METHOD *meth = DSA_OpenSSL();
1514 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
1517 kop.crk_param[0].crp_p = NULL;
1522 static DSA_METHOD cryptodev_dsa = {
1523 "cryptodev DSA method",
1525 NULL, /* dsa_sign_setup */
1527 NULL, /* dsa_mod_exp */
1535 #ifndef OPENSSL_NO_DH
1537 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1538 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1541 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1545 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1547 struct crypt_kop kop;
1551 if ((fd = get_asym_dev_crypto()) < 0) {
1552 const DH_METHOD *meth = DH_OpenSSL();
1554 return ((meth->compute_key) (key, pub_key, dh));
1557 keylen = BN_num_bits(dh->p);
1559 memset(&kop, 0, sizeof(kop));
1560 kop.crk_op = CRK_DH_COMPUTE_KEY;
1562 /* inputs: dh->priv_key pub_key dh->p key */
1563 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1565 if (bn2crparam(pub_key, &kop.crk_param[1]))
1567 if (bn2crparam(dh->p, &kop.crk_param[2]))
1569 kop.crk_iparams = 3;
1571 kop.crk_param[3].crp_p = (caddr_t) key;
1572 kop.crk_param[3].crp_nbits = keylen * 8;
1573 kop.crk_oparams = 1;
1575 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1576 const DH_METHOD *meth = DH_OpenSSL();
1578 dhret = (meth->compute_key) (key, pub_key, dh);
1581 kop.crk_param[3].crp_p = NULL;
1586 static DH_METHOD cryptodev_dh = {
1587 "cryptodev DH method",
1588 NULL, /* cryptodev_dh_generate_key */
1597 #endif /* ndef OPENSSL_NO_DH */
1600 * ctrl right now is just a wrapper that doesn't do much
1601 * but I expect we'll want some options soon.
1604 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1606 # ifdef HAVE_SYSLOG_R
1607 struct syslog_data sd = SYSLOG_DATA_INIT;
1612 # ifdef HAVE_SYSLOG_R
1613 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1615 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1622 void ENGINE_load_cryptodev(void)
1624 ENGINE *engine = ENGINE_new();
1629 if ((fd = get_dev_crypto()) < 0) {
1630 ENGINE_free(engine);
1635 * find out what asymmetric crypto algorithms we support
1637 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1639 ENGINE_free(engine);
1644 if (!ENGINE_set_id(engine, "cryptodev") ||
1645 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1646 !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1647 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1648 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1649 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1650 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1651 ENGINE_free(engine);
1655 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1656 const RSA_METHOD *rsa_meth = RSA_PKCS1_OpenSSL();
1658 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1659 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1660 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1661 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1662 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1663 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1664 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1665 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1666 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1667 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1669 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1673 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1674 const DSA_METHOD *meth = DSA_OpenSSL();
1676 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1677 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1678 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1679 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1680 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1681 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1683 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1684 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1687 #ifndef OPENSSL_NO_DH
1688 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1689 const DH_METHOD *dh_meth = DH_OpenSSL();
1691 cryptodev_dh.generate_key = dh_meth->generate_key;
1692 cryptodev_dh.compute_key = dh_meth->compute_key;
1693 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1694 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1695 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1696 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1697 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1703 ENGINE_free(engine);
1707 #endif /* HAVE_CRYPTODEV */