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 <internal/engine.h>
31 #include <openssl/evp.h>
32 #include <openssl/bn.h>
33 #include <openssl/crypto.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
41 # if (OpenBSD >= 200110)
42 # define HAVE_SYSLOG_R
46 #include <sys/types.h>
48 # include <crypto/cryptodev.h>
49 # include <sys/ioctl.h>
59 #include <openssl/dh.h>
60 #include <openssl/dsa.h>
61 #include <openssl/err.h>
62 #include <openssl/rsa.h>
64 #ifndef HAVE_CRYPTODEV
66 void engine_load_cryptodev_int(void)
68 /* This is a NOP on platforms without /dev/crypto */
74 struct dev_crypto_state {
75 struct session_op d_sess;
77 # ifdef USE_CRYPTODEV_DIGESTS
78 char dummy_mac_key[HASH_MAX_LEN];
79 unsigned char digest_res[HASH_MAX_LEN];
85 static u_int32_t cryptodev_asymfeat = 0;
87 static RSA_METHOD *cryptodev_rsa;
88 #ifndef OPENSSL_NO_DSA
89 static DSA_METHOD *cryptodev_dsa = NULL;
92 static DH_METHOD *cryptodev_dh;
95 static int get_asym_dev_crypto(void);
96 static int open_dev_crypto(void);
97 static int get_dev_crypto(void);
98 static int get_cryptodev_ciphers(const int **cnids);
99 # ifdef USE_CRYPTODEV_DIGESTS
100 static int get_cryptodev_digests(const int **cnids);
102 static int cryptodev_usable_ciphers(const int **nids);
103 static int cryptodev_usable_digests(const int **nids);
104 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
105 const unsigned char *in, size_t inl);
106 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
107 const unsigned char *iv, int enc);
108 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
109 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
110 const int **nids, int nid);
111 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
112 const int **nids, int nid);
113 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
114 static int crparam2bn(struct crparam *crp, BIGNUM *a);
115 static void zapparams(struct crypt_kop *kop);
116 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
117 int slen, BIGNUM *s);
119 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
120 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
122 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
124 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
126 #ifndef OPENSSL_NO_DSA
127 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
128 const BIGNUM *p, const BIGNUM *m,
129 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
130 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
131 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
132 BIGNUM *p, BN_CTX *ctx,
134 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
136 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
137 DSA_SIG *sig, DSA *dsa);
139 #ifndef OPENSSL_NO_DH
140 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
141 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
143 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
146 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
148 void engine_load_cryptodev_int(void);
150 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
161 CRYPTO_ARC4, NID_rc4, 0, 16,
164 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
167 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
170 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
173 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
176 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
178 # ifdef CRYPTO_AES_CTR
180 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
183 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
186 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
190 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
193 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
196 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
203 # ifdef USE_CRYPTODEV_DIGESTS
210 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
213 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
216 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
220 CRYPTO_MD5_KPDK, NID_undef, 0
223 CRYPTO_SHA1_KPDK, NID_undef, 0
226 CRYPTO_MD5, NID_md5, 16
229 CRYPTO_SHA1, NID_sha1, 20
238 * Return a fd if /dev/crypto seems usable, 0 otherwise.
240 static int open_dev_crypto(void)
245 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
248 if (fcntl(fd, F_SETFD, 1) == -1) {
257 static int get_dev_crypto(void)
261 if ((fd = open_dev_crypto()) == -1)
263 # ifndef CRIOGET_NOT_NEEDED
264 if (ioctl(fd, CRIOGET, &retfd) == -1)
268 if (fcntl(retfd, F_SETFD, 1) == -1) {
278 static void put_dev_crypto(int fd)
280 # ifndef CRIOGET_NOT_NEEDED
285 /* Caching version for asym operations */
286 static int get_asym_dev_crypto(void)
291 fd = get_dev_crypto();
296 * Find out what ciphers /dev/crypto will let us have a session for.
297 * XXX note, that some of these openssl doesn't deal with yet!
298 * returning them here is harmless, as long as we return NULL
299 * when asked for a handler in the cryptodev_engine_ciphers routine
301 static int get_cryptodev_ciphers(const int **cnids)
303 static int nids[CRYPTO_ALGORITHM_MAX];
304 struct session_op sess;
305 int fd, i, count = 0;
307 if ((fd = get_dev_crypto()) < 0) {
311 memset(&sess, 0, sizeof(sess));
312 sess.key = (caddr_t) "123456789abcdefghijklmno";
314 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
315 if (ciphers[i].nid == NID_undef)
317 sess.cipher = ciphers[i].id;
318 sess.keylen = ciphers[i].keylen;
320 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
321 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
322 nids[count++] = ciphers[i].nid;
333 # ifdef USE_CRYPTODEV_DIGESTS
335 * Find out what digests /dev/crypto will let us have a session for.
336 * XXX note, that some of these openssl doesn't deal with yet!
337 * returning them here is harmless, as long as we return NULL
338 * when asked for a handler in the cryptodev_engine_digests routine
340 static int get_cryptodev_digests(const int **cnids)
342 static int nids[CRYPTO_ALGORITHM_MAX];
343 struct session_op sess;
344 int fd, i, count = 0;
346 if ((fd = get_dev_crypto()) < 0) {
350 memset(&sess, 0, sizeof(sess));
351 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
352 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
353 if (digests[i].nid == NID_undef)
355 sess.mac = digests[i].id;
356 sess.mackeylen = digests[i].keylen;
358 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
359 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
360 nids[count++] = digests[i].nid;
373 * Find the useable ciphers|digests from dev/crypto - this is the first
374 * thing called by the engine init crud which determines what it
375 * can use for ciphers from this engine. We want to return
376 * only what we can do, anything else is handled by software.
378 * If we can't initialize the device to do anything useful for
379 * any reason, we want to return a NULL array, and 0 length,
380 * which forces everything to be done is software. By putting
381 * the initialization of the device in here, we ensure we can
382 * use this engine as the default, and if for whatever reason
383 * /dev/crypto won't do what we want it will just be done in
386 * This can (should) be greatly expanded to perhaps take into
387 * account speed of the device, and what we want to do.
388 * (although the disabling of particular alg's could be controlled
389 * by the device driver with sysctl's.) - this is where we
390 * want most of the decisions made about what we actually want
391 * to use from /dev/crypto.
393 static int cryptodev_usable_ciphers(const int **nids)
395 return (get_cryptodev_ciphers(nids));
398 static int cryptodev_usable_digests(const int **nids)
400 # ifdef USE_CRYPTODEV_DIGESTS
401 return (get_cryptodev_digests(nids));
404 * XXXX just disable all digests for now, because it sucks.
405 * we need a better way to decide this - i.e. I may not
406 * want digests on slow cards like hifn on fast machines,
407 * but might want them on slow or loaded machines, etc.
408 * will also want them when using crypto cards that don't
409 * suck moose gonads - would be nice to be able to decide something
410 * as reasonable default without having hackery that's card dependent.
411 * of course, the default should probably be just do everything,
412 * with perhaps a sysctl to turn algorithms off (or have them off
413 * by default) on cards that generally suck like the hifn.
421 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
422 const unsigned char *in, size_t inl)
424 struct crypt_op cryp;
425 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
426 struct session_op *sess = &state->d_sess;
428 unsigned char save_iv[EVP_MAX_IV_LENGTH];
434 if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
437 memset(&cryp, 0, sizeof(cryp));
439 cryp.ses = sess->ses;
442 cryp.src = (caddr_t) in;
443 cryp.dst = (caddr_t) out;
446 cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
448 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
449 cryp.iv = (caddr_t) EVP_CIPHER_CTX_iv(ctx);
450 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
451 iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
452 memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
457 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
459 * XXX need better error handling this can fail for a number of
465 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
466 if (EVP_CIPHER_CTX_encrypting(ctx))
467 iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
470 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
471 EVP_CIPHER_CTX_iv_length(ctx));
477 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
478 const unsigned char *iv, int enc)
480 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
481 struct session_op *sess = &state->d_sess;
484 for (i = 0; ciphers[i].id; i++)
485 if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
486 EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
487 EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
488 cipher = ciphers[i].id;
492 if (!ciphers[i].id) {
497 memset(sess, 0, sizeof(*sess));
499 if ((state->d_fd = get_dev_crypto()) < 0)
502 sess->key = (caddr_t) key;
503 sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
504 sess->cipher = cipher;
506 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
507 put_dev_crypto(state->d_fd);
515 * free anything we allocated earlier when initing a
516 * session, and close the session.
518 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
521 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
522 struct session_op *sess = &state->d_sess;
528 * XXX if this ioctl fails, something's wrong. the invoker may have called
529 * us with a bogus ctx, or we could have a device that for whatever
530 * reason just doesn't want to play ball - it's not clear what's right
531 * here - should this be an error? should it just increase a counter,
532 * hmm. For right now, we return 0 - I don't believe that to be "right".
533 * we could call the gorpy openssl lib error handlers that print messages
534 * to users of the library. hmm..
537 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
542 put_dev_crypto(state->d_fd);
549 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
550 * gets called when libcrypto requests a cipher NID.
554 static EVP_CIPHER *rc4_cipher = NULL;
555 static const EVP_CIPHER *cryptodev_rc4(void)
557 if (rc4_cipher == NULL) {
560 if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16)) == NULL
561 || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
562 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
563 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
564 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
565 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
566 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))) {
567 EVP_CIPHER_meth_free(cipher);
576 static EVP_CIPHER *des_cbc_cipher = NULL;
577 static const EVP_CIPHER *cryptodev_des_cbc(void)
579 if (des_cbc_cipher == NULL) {
582 if ((cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8)) == NULL
583 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
584 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
585 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
586 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
587 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
588 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
589 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
590 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
591 EVP_CIPHER_meth_free(cipher);
594 des_cbc_cipher = cipher;
596 return des_cbc_cipher;
600 static EVP_CIPHER *des3_cbc_cipher = NULL;
601 static const EVP_CIPHER *cryptodev_3des_cbc(void)
603 if (des3_cbc_cipher == NULL) {
606 if ((cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24)) == NULL
607 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
608 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
609 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
610 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
611 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
612 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
613 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
614 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
615 EVP_CIPHER_meth_free(cipher);
618 des3_cbc_cipher = cipher;
620 return des3_cbc_cipher;
623 static EVP_CIPHER *bf_cbc_cipher = NULL;
624 static const EVP_CIPHER *cryptodev_bf_cbc(void)
626 if (bf_cbc_cipher == NULL) {
629 if ((cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16)) == NULL
630 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
631 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
632 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
633 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
634 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
635 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
636 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
637 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
638 EVP_CIPHER_meth_free(cipher);
641 bf_cbc_cipher = cipher;
643 return bf_cbc_cipher;
646 static EVP_CIPHER *cast_cbc_cipher = NULL;
647 static const EVP_CIPHER *cryptodev_cast_cbc(void)
649 if (cast_cbc_cipher == NULL) {
652 if ((cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16)) == NULL
653 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
654 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
655 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
656 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
657 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
658 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
659 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
660 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
661 EVP_CIPHER_meth_free(cipher);
664 cast_cbc_cipher = cipher;
666 return cast_cbc_cipher;
669 static EVP_CIPHER *aes_cbc_cipher = NULL;
670 static const EVP_CIPHER *cryptodev_aes_cbc(void)
672 if (aes_cbc_cipher == NULL) {
675 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16)) == NULL
676 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
677 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
678 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
679 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
680 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
681 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
682 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
683 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
684 EVP_CIPHER_meth_free(cipher);
687 aes_cbc_cipher = cipher;
689 return aes_cbc_cipher;
692 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
693 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
695 if (aes_192_cbc_cipher == NULL) {
698 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24)) == NULL
699 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
700 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
701 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
702 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
703 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
704 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
705 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
706 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
707 EVP_CIPHER_meth_free(cipher);
710 aes_192_cbc_cipher = cipher;
712 return aes_192_cbc_cipher;
715 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
716 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
718 if (aes_256_cbc_cipher == NULL) {
721 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32)) == NULL
722 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
723 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
724 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
725 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
726 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
727 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
728 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
729 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
730 EVP_CIPHER_meth_free(cipher);
733 aes_256_cbc_cipher = cipher;
735 return aes_256_cbc_cipher;
738 # ifdef CRYPTO_AES_CTR
739 static EVP_CIPHER *aes_ctr_cipher = NULL;
740 static const EVP_CIPHER *cryptodev_aes_ctr(void)
742 if (aes_ctr_cipher == NULL) {
745 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16)) == NULL
746 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
747 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
748 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
749 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
750 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
751 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
752 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
753 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
754 EVP_CIPHER_meth_free(cipher);
757 aes_ctr_cipher = cipher;
759 return aes_ctr_cipher;
762 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
763 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
765 if (aes_192_ctr_cipher == NULL) {
768 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24)) == NULL
769 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
770 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
771 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
772 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
773 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
774 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
775 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
776 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
777 EVP_CIPHER_meth_free(cipher);
780 aes_192_ctr_cipher = cipher;
782 return aes_192_ctr_cipher;
785 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
786 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
788 if (aes_256_ctr_cipher == NULL) {
791 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32)) == NULL
792 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
793 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
794 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
795 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
796 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
797 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
798 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
799 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
800 EVP_CIPHER_meth_free(cipher);
803 aes_256_ctr_cipher = cipher;
805 return aes_256_ctr_cipher;
809 * Registered by the ENGINE when used to find out how to deal with
810 * a particular NID in the ENGINE. this says what we'll do at the
811 * top level - note, that list is restricted by what we answer with
814 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
815 const int **nids, int nid)
818 return (cryptodev_usable_ciphers(nids));
822 *cipher = cryptodev_rc4();
824 case NID_des_ede3_cbc:
825 *cipher = cryptodev_3des_cbc();
828 *cipher = cryptodev_des_cbc();
831 *cipher = cryptodev_bf_cbc();
834 *cipher = cryptodev_cast_cbc();
836 case NID_aes_128_cbc:
837 *cipher = cryptodev_aes_cbc();
839 case NID_aes_192_cbc:
840 *cipher = cryptodev_aes_192_cbc();
842 case NID_aes_256_cbc:
843 *cipher = cryptodev_aes_256_cbc();
845 # ifdef CRYPTO_AES_CTR
846 case NID_aes_128_ctr:
847 *cipher = cryptodev_aes_ctr();
849 case NID_aes_192_ctr:
850 *cipher = cryptodev_aes_ctr_192();
852 case NID_aes_256_ctr:
853 *cipher = cryptodev_aes_ctr_256();
860 return (*cipher != NULL);
863 # ifdef USE_CRYPTODEV_DIGESTS
865 /* convert digest type to cryptodev */
866 static int digest_nid_to_cryptodev(int nid)
870 for (i = 0; digests[i].id; i++)
871 if (digests[i].nid == nid)
872 return (digests[i].id);
876 static int digest_key_length(int nid)
880 for (i = 0; digests[i].id; i++)
881 if (digests[i].nid == nid)
882 return digests[i].keylen;
886 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
888 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
889 struct session_op *sess = &state->d_sess;
892 if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
893 printf("cryptodev_digest_init: Can't get digest \n");
897 memset(state, 0, sizeof(*state));
899 if ((state->d_fd = get_dev_crypto()) < 0) {
900 printf("cryptodev_digest_init: Can't get Dev \n");
904 sess->mackey = state->dummy_mac_key;
905 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(ctx));
908 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
909 put_dev_crypto(state->d_fd);
911 printf("cryptodev_digest_init: Open session failed\n");
918 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
921 struct crypt_op cryp;
922 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
923 struct session_op *sess = &state->d_sess;
926 if (!data || state->d_fd < 0) {
927 printf("cryptodev_digest_update: illegal inputs \n");
935 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
936 /* if application doesn't support one buffer */
938 OPENSSL_realloc(state->mac_data, state->mac_len + count);
941 printf("cryptodev_digest_update: realloc failed\n");
944 state->mac_data = new_mac_data;
946 memcpy(state->mac_data + state->mac_len, data, count);
947 state->mac_len += count;
952 memset(&cryp, 0, sizeof(cryp));
954 cryp.ses = sess->ses;
957 cryp.src = (caddr_t) data;
959 cryp.mac = (caddr_t) state->digest_res;
960 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
961 printf("cryptodev_digest_update: digest failed\n");
967 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
969 struct crypt_op cryp;
970 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
971 struct session_op *sess = &state->d_sess;
975 if (!md || state->d_fd < 0) {
976 printf("cryptodev_digest_final: illegal input\n");
980 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
981 /* if application doesn't support one buffer */
982 memset(&cryp, 0, sizeof(cryp));
983 cryp.ses = sess->ses;
985 cryp.len = state->mac_len;
986 cryp.src = state->mac_data;
988 cryp.mac = (caddr_t) md;
989 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
990 printf("cryptodev_digest_final: digest failed\n");
997 memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
1002 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
1005 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1006 struct session_op *sess = &state->d_sess;
1011 if (state->d_fd < 0) {
1012 printf("cryptodev_digest_cleanup: illegal input\n");
1016 OPENSSL_free(state->mac_data);
1017 state->mac_data = NULL;
1020 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1021 printf("cryptodev_digest_cleanup: failed to close session\n");
1026 put_dev_crypto(state->d_fd);
1032 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1034 struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1035 struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1036 struct session_op *sess;
1039 if (dstate == NULL || fstate == NULL)
1042 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1044 sess = &dstate->d_sess;
1046 digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1048 sess->mackey = dstate->dummy_mac_key;
1049 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(to));
1052 dstate->d_fd = get_dev_crypto();
1054 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1055 put_dev_crypto(dstate->d_fd);
1057 printf("cryptodev_digest_copy: Open session failed\n");
1061 if (fstate->mac_len != 0) {
1062 if (fstate->mac_data != NULL) {
1063 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1064 if (dstate->mac_data == NULL) {
1065 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1068 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1069 dstate->mac_len = fstate->mac_len;
1076 static EVP_MD *sha1_md = NULL;
1077 static const EVP_MD *cryptodev_sha1(void)
1079 if (sha1_md == NULL) {
1082 if ((md = EVP_MD_meth_new(NID_sha1, NID_undef)) == NULL
1083 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1084 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1085 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1086 || !EVP_MD_meth_set_app_datasize(md,
1087 sizeof(struct dev_crypto_state))
1088 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1089 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1090 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1091 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1092 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1093 EVP_MD_meth_free(md);
1101 static EVP_MD *md5_md = NULL;
1102 static const EVP_MD *cryptodev_md5(void)
1104 if (md5_md == NULL) {
1107 if ((md = EVP_MD_meth_new(NID_md5, NID_undef)) == NULL
1108 || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */)
1109 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1110 || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */)
1111 || !EVP_MD_meth_set_app_datasize(md,
1112 sizeof(struct dev_crypto_state))
1113 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1114 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1115 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1116 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1117 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1118 EVP_MD_meth_free(md);
1126 # endif /* USE_CRYPTODEV_DIGESTS */
1129 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1130 const int **nids, int nid)
1133 return (cryptodev_usable_digests(nids));
1136 # ifdef USE_CRYPTODEV_DIGESTS
1138 *digest = cryptodev_md5();
1141 *digest = cryptodev_sha1();
1144 # endif /* USE_CRYPTODEV_DIGESTS */
1148 return (*digest != NULL);
1151 static int cryptodev_engine_destroy(ENGINE *e)
1153 EVP_CIPHER_meth_free(rc4_cipher);
1155 EVP_CIPHER_meth_free(des_cbc_cipher);
1156 des_cbc_cipher = NULL;
1157 EVP_CIPHER_meth_free(des3_cbc_cipher);
1158 des3_cbc_cipher = NULL;
1159 EVP_CIPHER_meth_free(bf_cbc_cipher);
1160 bf_cbc_cipher = NULL;
1161 EVP_CIPHER_meth_free(cast_cbc_cipher);
1162 cast_cbc_cipher = NULL;
1163 EVP_CIPHER_meth_free(aes_cbc_cipher);
1164 aes_cbc_cipher = NULL;
1165 EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1166 aes_192_cbc_cipher = NULL;
1167 EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1168 aes_256_cbc_cipher = NULL;
1169 # ifdef CRYPTO_AES_CTR
1170 EVP_CIPHER_meth_free(aes_ctr_cipher);
1171 aes_ctr_cipher = NULL;
1172 EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1173 aes_192_ctr_cipher = NULL;
1174 EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1175 aes_256_ctr_cipher = NULL;
1177 # ifdef USE_CRYPTODEV_DIGESTS
1178 EVP_MD_meth_free(sha1_md);
1180 EVP_MD_meth_free(md5_md);
1183 RSA_meth_free(cryptodev_rsa);
1184 cryptodev_rsa = NULL;
1185 #ifndef OPENSSL_NO_DSA
1186 DSA_meth_free(cryptodev_dsa);
1187 cryptodev_dsa = NULL;
1189 #ifndef OPENSSL_NO_DH
1190 DH_meth_free(cryptodev_dh);
1191 cryptodev_dh = NULL;
1197 * Convert a BIGNUM to the representation that /dev/crypto needs.
1198 * Upon completion of use, the caller is responsible for freeing
1201 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1203 ssize_t bytes, bits;
1209 bits = BN_num_bits(a);
1210 bytes = BN_num_bytes(a);
1212 b = OPENSSL_zalloc(bytes);
1216 crp->crp_p = (caddr_t) b;
1217 crp->crp_nbits = bits;
1223 /* Convert a /dev/crypto parameter to a BIGNUM */
1224 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1229 bytes = (crp->crp_nbits + 7) / 8;
1234 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1237 for (i = 0; i < bytes; i++)
1238 pd[i] = crp->crp_p[bytes - i - 1];
1240 BN_bin2bn(pd, bytes, a);
1246 static void zapparams(struct crypt_kop *kop)
1250 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1251 if (kop->crk_param[i].crp_p)
1252 free(kop->crk_param[i].crp_p);
1253 kop->crk_param[i].crp_p = NULL;
1254 kop->crk_param[i].crp_nbits = 0;
1259 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1264 if ((fd = get_asym_dev_crypto()) < 0)
1268 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1269 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1273 kop->crk_param[kop->crk_iparams + 1].crp_p =
1274 calloc(slen, sizeof(char));
1275 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1279 if (ioctl(fd, CIOCKEY, kop) == 0) {
1281 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1283 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1291 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1292 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1294 struct crypt_kop kop;
1298 * Currently, we know we can do mod exp iff we can do any asymmetric
1299 * operations at all.
1301 if (cryptodev_asymfeat == 0) {
1302 ret = BN_mod_exp(r, a, p, m, ctx);
1306 memset(&kop, 0, sizeof(kop));
1307 kop.crk_op = CRK_MOD_EXP;
1309 /* inputs: a^p % m */
1310 if (bn2crparam(a, &kop.crk_param[0]))
1312 if (bn2crparam(p, &kop.crk_param[1]))
1314 if (bn2crparam(m, &kop.crk_param[2]))
1316 kop.crk_iparams = 3;
1318 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1319 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1320 printf("OCF asym process failed, Running in software\n");
1321 ret = RSA_meth_get_bn_mod_exp(meth)(r, a, p, m, ctx, in_mont);
1323 } else if (ECANCELED == kop.crk_status) {
1324 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1325 printf("OCF hardware operation cancelled. Running in Software\n");
1326 ret = RSA_meth_get_bn_mod_exp(meth)(r, a, p, m, ctx, in_mont);
1328 /* else cryptodev operation worked ok ==> ret = 1 */
1336 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1344 RSA_get0_key(rsa, &n, NULL, &d);
1345 r = cryptodev_bn_mod_exp(r0, I, d, n, ctx, NULL);
1351 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1353 struct crypt_kop kop;
1357 BIGNUM *dmp1 = NULL;
1358 BIGNUM *dmq1 = NULL;
1359 BIGNUM *iqmp = NULL;
1362 RSA_get0_factors(rsa, &p, &q);
1363 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
1364 RSA_get0_key(rsa, &n, NULL, NULL);
1366 if (!p || !q || !dmp1 || !dmq1 || !iqmp) {
1367 /* XXX 0 means failure?? */
1371 memset(&kop, 0, sizeof(kop));
1372 kop.crk_op = CRK_MOD_EXP_CRT;
1373 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1374 if (bn2crparam(p, &kop.crk_param[0]))
1376 if (bn2crparam(q, &kop.crk_param[1]))
1378 if (bn2crparam(I, &kop.crk_param[2]))
1380 if (bn2crparam(dmp1, &kop.crk_param[3]))
1382 if (bn2crparam(dmq1, &kop.crk_param[4]))
1384 if (bn2crparam(iqmp, &kop.crk_param[5]))
1386 kop.crk_iparams = 6;
1388 if (cryptodev_asym(&kop, BN_num_bytes(n), r0, 0, NULL)) {
1389 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1390 printf("OCF asym process failed, running in Software\n");
1391 ret = RSA_meth_get_mod_exp(meth)(r0, I, rsa, ctx);
1393 } else if (ECANCELED == kop.crk_status) {
1394 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1395 printf("OCF hardware operation cancelled. Running in Software\n");
1396 ret = RSA_meth_get_mod_exp(meth)(r0, I, rsa, ctx);
1398 /* else cryptodev operation worked ok ==> ret = 1 */
1405 #ifndef OPENSSL_NO_DSA
1407 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1408 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1410 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1414 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1415 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1416 BN_CTX *ctx, BN_MONT_CTX *mont)
1418 BIGNUM *t2, *dsag, *dsap, *dsapub_key;
1420 const DSA_METHOD *meth;
1421 int (*bn_mod_exp)(DSA *, BIGNUM *, BIGNUM *, const BIGNUM *, const BIGNUM *,
1422 BN_CTX *, BN_MONT_CTX *);
1428 /* v = ( g^u1 * y^u2 mod p ) mod q */
1429 /* let t1 = g ^ u1 mod p */
1432 DSA_get0_pqg(dsa, &dsap, NULL, &dsag);
1433 DSA_get0_key(dsa, &dsapub_key, NULL);
1435 meth = DSA_get_method(dsa);
1438 bn_mod_exp = DSA_meth_get_bn_mod_exp(meth);
1439 if (bn_mod_exp == NULL)
1442 if (!bn_mod_exp(dsa, t1, dsag, u1, dsap, ctx, mont))
1445 /* let t2 = y ^ u2 mod p */
1446 if (!bn_mod_exp(dsa, t2, dsapub_key, u2, dsap, ctx, mont))
1448 /* let u1 = t1 * t2 mod p */
1449 if (!BN_mod_mul(u1, t1, t2, dsap, ctx))
1460 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1463 struct crypt_kop kop;
1464 BIGNUM *r = NULL, *s = NULL, *dsap = NULL, *dsaq = NULL, *dsag = NULL;
1465 BIGNUM *priv_key = NULL;
1466 DSA_SIG *dsasig, *dsaret = NULL;
1468 dsasig = DSA_SIG_new();
1471 DSA_SIG_get0(&r, &s, dsasig);
1473 memset(&kop, 0, sizeof(kop));
1474 kop.crk_op = CRK_DSA_SIGN;
1476 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1477 kop.crk_param[0].crp_p = (caddr_t) dgst;
1478 kop.crk_param[0].crp_nbits = dlen * 8;
1479 DSA_get0_pqg(dsa, &dsap, &dsaq, &dsag);
1480 DSA_get0_key(dsa, NULL, &priv_key);
1481 if (bn2crparam(dsap, &kop.crk_param[1]))
1483 if (bn2crparam(dsaq, &kop.crk_param[2]))
1485 if (bn2crparam(dsag, &kop.crk_param[3]))
1487 if (bn2crparam(priv_key, &kop.crk_param[4]))
1489 kop.crk_iparams = 5;
1491 if (cryptodev_asym(&kop, BN_num_bytes(dsaq), r,
1492 BN_num_bytes(dsaq), s) == 0) {
1495 dsaret = DSA_meth_get_sign(DSA_OpenSSL())(dgst, dlen, dsa);
1498 if (dsaret != dsasig)
1499 DSA_SIG_free(dsasig);
1500 kop.crk_param[0].crp_p = NULL;
1506 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1507 DSA_SIG *sig, DSA *dsa)
1509 struct crypt_kop kop;
1511 BIGNUM *pr, *ps, *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL;
1513 memset(&kop, 0, sizeof(kop));
1514 kop.crk_op = CRK_DSA_VERIFY;
1516 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1517 kop.crk_param[0].crp_p = (caddr_t) dgst;
1518 kop.crk_param[0].crp_nbits = dlen * 8;
1519 DSA_get0_pqg(dsa, &p, &q, &g);
1520 if (bn2crparam(p, &kop.crk_param[1]))
1522 if (bn2crparam(q, &kop.crk_param[2]))
1524 if (bn2crparam(g, &kop.crk_param[3]))
1526 DSA_get0_key(dsa, &pub_key, NULL);
1527 if (bn2crparam(pub_key, &kop.crk_param[4]))
1529 DSA_SIG_get0(&pr, &ps, sig);
1530 if (bn2crparam(pr, &kop.crk_param[5]))
1532 if (bn2crparam(ps, &kop.crk_param[6]))
1534 kop.crk_iparams = 7;
1536 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1538 * OCF success value is 0, if not zero, change dsaret to fail
1540 if (0 != kop.crk_status)
1543 dsaret = DSA_meth_get_verify(DSA_OpenSSL())(dgst, dlen, sig, dsa);
1546 kop.crk_param[0].crp_p = NULL;
1552 #ifndef OPENSSL_NO_DH
1554 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1555 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1558 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1562 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1564 struct crypt_kop kop;
1568 BIGNUM *priv_key = NULL;
1570 if ((fd = get_asym_dev_crypto()) < 0) {
1571 const DH_METHOD *meth = DH_OpenSSL();
1573 return DH_meth_get_compute_key(meth)(key, pub_key, dh);
1576 DH_get0_pqg(dh, &p, NULL, NULL);
1577 DH_get0_key(dh, NULL, &priv_key);
1579 keylen = BN_num_bits(p);
1581 memset(&kop, 0, sizeof(kop));
1582 kop.crk_op = CRK_DH_COMPUTE_KEY;
1584 /* inputs: dh->priv_key pub_key dh->p key */
1585 if (bn2crparam(priv_key, &kop.crk_param[0]))
1587 if (bn2crparam(pub_key, &kop.crk_param[1]))
1589 if (bn2crparam(p, &kop.crk_param[2]))
1591 kop.crk_iparams = 3;
1593 kop.crk_param[3].crp_p = (caddr_t) key;
1594 kop.crk_param[3].crp_nbits = keylen * 8;
1595 kop.crk_oparams = 1;
1597 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1598 const DH_METHOD *meth = DH_OpenSSL();
1600 dhret = DH_meth_get_compute_key(meth)(key, pub_key, dh);
1603 kop.crk_param[3].crp_p = NULL;
1608 #endif /* ndef OPENSSL_NO_DH */
1611 * ctrl right now is just a wrapper that doesn't do much
1612 * but I expect we'll want some options soon.
1615 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1617 # ifdef HAVE_SYSLOG_R
1618 struct syslog_data sd = SYSLOG_DATA_INIT;
1623 # ifdef HAVE_SYSLOG_R
1624 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1626 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1633 void engine_load_cryptodev_int(void)
1635 ENGINE *engine = ENGINE_new();
1640 if ((fd = get_dev_crypto()) < 0) {
1641 ENGINE_free(engine);
1646 * find out what asymmetric crypto algorithms we support
1648 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1650 ENGINE_free(engine);
1655 if (!ENGINE_set_id(engine, "cryptodev") ||
1656 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1657 !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1658 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1659 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1660 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1661 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1662 ENGINE_free(engine);
1666 cryptodev_rsa = RSA_meth_dup(RSA_PKCS1_OpenSSL());
1667 if (cryptodev_rsa != NULL) {
1668 RSA_meth_set1_name(cryptodev_rsa, "cryptodev RSA method");
1669 RSA_meth_set_flags(cryptodev_rsa, 0);
1670 if (ENGINE_set_RSA(engine, cryptodev_rsa)) {
1671 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1672 RSA_meth_set_bn_mod_exp(cryptodev_rsa, cryptodev_bn_mod_exp);
1673 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1674 RSA_meth_set_mod_exp(cryptodev_rsa, cryptodev_rsa_mod_exp);
1676 RSA_meth_set_mod_exp(cryptodev_rsa,
1677 cryptodev_rsa_nocrt_mod_exp);
1681 ENGINE_free(engine);
1685 #ifndef OPENSSL_NO_DSA
1686 cryptodev_dsa = DSA_meth_dup(DSA_OpenSSL());
1687 if (cryptodev_dsa != NULL) {
1688 DSA_meth_set1_name(cryptodev_dsa, "cryptodev DSA method");
1689 DSA_meth_set_flags(cryptodev_dsa, 0);
1690 if (ENGINE_set_DSA(engine, cryptodev_dsa)) {
1691 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1692 DSA_meth_set_sign(cryptodev_dsa, cryptodev_dsa_do_sign);
1693 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1694 DSA_meth_set_bn_mod_exp(cryptodev_dsa,
1695 cryptodev_dsa_bn_mod_exp);
1696 DSA_meth_set_mod_exp(cryptodev_dsa, cryptodev_dsa_dsa_mod_exp);
1698 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1699 DSA_meth_set_verify(cryptodev_dsa, cryptodev_dsa_verify);
1702 ENGINE_free(engine);
1707 #ifndef OPENSSL_NO_DH
1708 cryptodev_dh = DH_meth_dup(DH_OpenSSL());
1709 if (cryptodev_dh != NULL) {
1710 DH_meth_set1_name(cryptodev_dh, "cryptodev DH method");
1711 DH_meth_set_flags(cryptodev_dh, 0);
1712 if (ENGINE_set_DH(engine, cryptodev_dh)) {
1713 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1714 DH_meth_set_bn_mod_exp(cryptodev_dh, cryptodev_mod_exp_dh);
1715 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1716 DH_meth_set_compute_key(cryptodev_dh,
1717 cryptodev_dh_compute_key);
1721 ENGINE_free(engine);
1727 ENGINE_free(engine);
1731 #endif /* HAVE_CRYPTODEV */