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 = ctx->cipher_data;
417 struct session_op *sess = &state->d_sess;
419 unsigned char save_iv[EVP_MAX_IV_LENGTH];
425 if ((inl % ctx->cipher->block_size) != 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 = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
439 if (ctx->cipher->iv_len) {
440 cryp.iv = (caddr_t) ctx->iv;
442 iiv = in + inl - ctx->cipher->iv_len;
443 memcpy(save_iv, iiv, ctx->cipher->iv_len);
448 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
450 * XXX need better errror handling this can fail for a number of
456 if (ctx->cipher->iv_len) {
458 iiv = out + inl - ctx->cipher->iv_len;
461 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
467 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
468 const unsigned char *iv, int enc)
470 struct dev_crypto_state *state = ctx->cipher_data;
471 struct session_op *sess = &state->d_sess;
474 for (i = 0; ciphers[i].id; i++)
475 if (ctx->cipher->nid == ciphers[i].nid &&
476 ctx->cipher->iv_len <= ciphers[i].ivmax &&
477 ctx->key_len == ciphers[i].keylen) {
478 cipher = ciphers[i].id;
482 if (!ciphers[i].id) {
487 memset(sess, 0, sizeof(*sess));
489 if ((state->d_fd = get_dev_crypto()) < 0)
492 sess->key = (caddr_t) key;
493 sess->keylen = ctx->key_len;
494 sess->cipher = cipher;
496 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
497 put_dev_crypto(state->d_fd);
505 * free anything we allocated earlier when initting a
506 * session, and close the session.
508 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
511 struct dev_crypto_state *state = ctx->cipher_data;
512 struct session_op *sess = &state->d_sess;
518 * XXX if this ioctl fails, someting's wrong. the invoker may have called
519 * us with a bogus ctx, or we could have a device that for whatever
520 * reason just doesn't want to play ball - it's not clear what's right
521 * here - should this be an error? should it just increase a counter,
522 * hmm. For right now, we return 0 - I don't believe that to be "right".
523 * we could call the gorpy openssl lib error handlers that print messages
524 * to users of the library. hmm..
527 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
532 put_dev_crypto(state->d_fd);
539 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
540 * gets called when libcrypto requests a cipher NID.
544 static const EVP_CIPHER cryptodev_rc4 = {
547 EVP_CIPH_VARIABLE_LENGTH,
551 sizeof(struct dev_crypto_state),
558 static const EVP_CIPHER cryptodev_des_cbc = {
565 sizeof(struct dev_crypto_state),
566 EVP_CIPHER_set_asn1_iv,
567 EVP_CIPHER_get_asn1_iv,
572 static const EVP_CIPHER cryptodev_3des_cbc = {
579 sizeof(struct dev_crypto_state),
580 EVP_CIPHER_set_asn1_iv,
581 EVP_CIPHER_get_asn1_iv,
585 static const EVP_CIPHER cryptodev_bf_cbc = {
592 sizeof(struct dev_crypto_state),
593 EVP_CIPHER_set_asn1_iv,
594 EVP_CIPHER_get_asn1_iv,
598 static const EVP_CIPHER cryptodev_cast_cbc = {
605 sizeof(struct dev_crypto_state),
606 EVP_CIPHER_set_asn1_iv,
607 EVP_CIPHER_get_asn1_iv,
611 static const EVP_CIPHER cryptodev_aes_cbc = {
618 sizeof(struct dev_crypto_state),
619 EVP_CIPHER_set_asn1_iv,
620 EVP_CIPHER_get_asn1_iv,
624 static const EVP_CIPHER cryptodev_aes_192_cbc = {
631 sizeof(struct dev_crypto_state),
632 EVP_CIPHER_set_asn1_iv,
633 EVP_CIPHER_get_asn1_iv,
637 static const EVP_CIPHER cryptodev_aes_256_cbc = {
644 sizeof(struct dev_crypto_state),
645 EVP_CIPHER_set_asn1_iv,
646 EVP_CIPHER_get_asn1_iv,
650 # ifdef CRYPTO_AES_CTR
651 const EVP_CIPHER cryptodev_aes_ctr = {
658 sizeof(struct dev_crypto_state),
659 EVP_CIPHER_set_asn1_iv,
660 EVP_CIPHER_get_asn1_iv,
664 const EVP_CIPHER cryptodev_aes_ctr_192 = {
671 sizeof(struct dev_crypto_state),
672 EVP_CIPHER_set_asn1_iv,
673 EVP_CIPHER_get_asn1_iv,
677 const EVP_CIPHER cryptodev_aes_ctr_256 = {
684 sizeof(struct dev_crypto_state),
685 EVP_CIPHER_set_asn1_iv,
686 EVP_CIPHER_get_asn1_iv,
691 * Registered by the ENGINE when used to find out how to deal with
692 * a particular NID in the ENGINE. this says what we'll do at the
693 * top level - note, that list is restricted by what we answer with
696 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
697 const int **nids, int nid)
700 return (cryptodev_usable_ciphers(nids));
704 *cipher = &cryptodev_rc4;
706 case NID_des_ede3_cbc:
707 *cipher = &cryptodev_3des_cbc;
710 *cipher = &cryptodev_des_cbc;
713 *cipher = &cryptodev_bf_cbc;
716 *cipher = &cryptodev_cast_cbc;
718 case NID_aes_128_cbc:
719 *cipher = &cryptodev_aes_cbc;
721 case NID_aes_192_cbc:
722 *cipher = &cryptodev_aes_192_cbc;
724 case NID_aes_256_cbc:
725 *cipher = &cryptodev_aes_256_cbc;
727 # ifdef CRYPTO_AES_CTR
728 case NID_aes_128_ctr:
729 *cipher = &cryptodev_aes_ctr;
731 case NID_aes_192_ctr:
732 *cipher = &cryptodev_aes_ctr_192;
734 case NID_aes_256_ctr:
735 *cipher = &cryptodev_aes_ctr_256;
742 return (*cipher != NULL);
745 # ifdef USE_CRYPTODEV_DIGESTS
747 /* convert digest type to cryptodev */
748 static int digest_nid_to_cryptodev(int nid)
752 for (i = 0; digests[i].id; i++)
753 if (digests[i].nid == nid)
754 return (digests[i].id);
758 static int digest_key_length(int nid)
762 for (i = 0; digests[i].id; i++)
763 if (digests[i].nid == nid)
764 return digests[i].keylen;
768 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
770 struct dev_crypto_state *state = ctx->md_data;
771 struct session_op *sess = &state->d_sess;
774 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) {
775 printf("cryptodev_digest_init: Can't get digest \n");
779 memset(state, 0, sizeof(*state));
781 if ((state->d_fd = get_dev_crypto()) < 0) {
782 printf("cryptodev_digest_init: Can't get Dev \n");
786 sess->mackey = state->dummy_mac_key;
787 sess->mackeylen = digest_key_length(ctx->digest->type);
790 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
791 put_dev_crypto(state->d_fd);
793 printf("cryptodev_digest_init: Open session failed\n");
800 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
803 struct crypt_op cryp;
804 struct dev_crypto_state *state = ctx->md_data;
805 struct session_op *sess = &state->d_sess;
808 if (!data || state->d_fd < 0) {
809 printf("cryptodev_digest_update: illegal inputs \n");
817 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
818 /* if application doesn't support one buffer */
820 OPENSSL_realloc(state->mac_data, state->mac_len + count);
823 printf("cryptodev_digest_update: realloc failed\n");
826 state->mac_data = new_mac_data;
828 memcpy(state->mac_data + state->mac_len, data, count);
829 state->mac_len += count;
834 memset(&cryp, 0, sizeof(cryp));
836 cryp.ses = sess->ses;
839 cryp.src = (caddr_t) data;
841 cryp.mac = (caddr_t) state->digest_res;
842 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
843 printf("cryptodev_digest_update: digest failed\n");
849 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
851 struct crypt_op cryp;
852 struct dev_crypto_state *state = ctx->md_data;
853 struct session_op *sess = &state->d_sess;
857 if (!md || state->d_fd < 0) {
858 printf("cryptodev_digest_final: illegal input\n");
862 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
863 /* if application doesn't support one buffer */
864 memset(&cryp, 0, sizeof(cryp));
865 cryp.ses = sess->ses;
867 cryp.len = state->mac_len;
868 cryp.src = state->mac_data;
870 cryp.mac = (caddr_t) md;
871 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
872 printf("cryptodev_digest_final: digest failed\n");
879 memcpy(md, state->digest_res, ctx->digest->md_size);
884 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
887 struct dev_crypto_state *state = ctx->md_data;
888 struct session_op *sess = &state->d_sess;
893 if (state->d_fd < 0) {
894 printf("cryptodev_digest_cleanup: illegal input\n");
898 OPENSSL_free(state->mac_data);
899 state->mac_data = NULL;
902 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
903 printf("cryptodev_digest_cleanup: failed to close session\n");
908 put_dev_crypto(state->d_fd);
914 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
916 struct dev_crypto_state *fstate = from->md_data;
917 struct dev_crypto_state *dstate = to->md_data;
918 struct session_op *sess;
921 if (dstate == NULL || fstate == NULL)
924 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
926 sess = &dstate->d_sess;
928 digest = digest_nid_to_cryptodev(to->digest->type);
930 sess->mackey = dstate->dummy_mac_key;
931 sess->mackeylen = digest_key_length(to->digest->type);
934 dstate->d_fd = get_dev_crypto();
936 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
937 put_dev_crypto(dstate->d_fd);
939 printf("cryptodev_digest_copy: Open session failed\n");
943 if (fstate->mac_len != 0) {
944 if (fstate->mac_data != NULL) {
945 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
946 if (dstate->mac_data == NULL) {
947 printf("cryptodev_digest_copy: mac_data allocation failed\n");
950 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
951 dstate->mac_len = fstate->mac_len;
958 const EVP_MD cryptodev_sha1 = {
963 cryptodev_digest_init,
964 cryptodev_digest_update,
965 cryptodev_digest_final,
966 cryptodev_digest_copy,
967 cryptodev_digest_cleanup,
968 EVP_PKEY_NULL_method,
970 sizeof(struct dev_crypto_state),
973 const EVP_MD cryptodev_md5 = {
976 16 /* MD5_DIGEST_LENGTH */ ,
978 cryptodev_digest_init,
979 cryptodev_digest_update,
980 cryptodev_digest_final,
981 cryptodev_digest_copy,
982 cryptodev_digest_cleanup,
983 EVP_PKEY_NULL_method,
984 64 /* MD5_CBLOCK */ ,
985 sizeof(struct dev_crypto_state),
988 # endif /* USE_CRYPTODEV_DIGESTS */
991 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
992 const int **nids, int nid)
995 return (cryptodev_usable_digests(nids));
998 # ifdef USE_CRYPTODEV_DIGESTS
1000 *digest = &cryptodev_md5;
1003 *digest = &cryptodev_sha1;
1006 # endif /* USE_CRYPTODEV_DIGESTS */
1010 return (*digest != NULL);
1014 * Convert a BIGNUM to the representation that /dev/crypto needs.
1015 * Upon completion of use, the caller is responsible for freeing
1018 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1020 ssize_t bytes, bits;
1026 bits = BN_num_bits(a);
1027 bytes = BN_num_bytes(a);
1029 b = OPENSSL_zalloc(bytes);
1033 crp->crp_p = (caddr_t) b;
1034 crp->crp_nbits = bits;
1040 /* Convert a /dev/crypto parameter to a BIGNUM */
1041 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1046 bytes = (crp->crp_nbits + 7) / 8;
1051 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1054 for (i = 0; i < bytes; i++)
1055 pd[i] = crp->crp_p[bytes - i - 1];
1057 BN_bin2bn(pd, bytes, a);
1063 static void zapparams(struct crypt_kop *kop)
1067 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1068 if (kop->crk_param[i].crp_p)
1069 free(kop->crk_param[i].crp_p);
1070 kop->crk_param[i].crp_p = NULL;
1071 kop->crk_param[i].crp_nbits = 0;
1076 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1081 if ((fd = get_asym_dev_crypto()) < 0)
1085 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1086 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1090 kop->crk_param[kop->crk_iparams + 1].crp_p =
1091 calloc(slen, sizeof(char));
1092 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1096 if (ioctl(fd, CIOCKEY, kop) == 0) {
1098 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1100 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1108 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1109 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1111 struct crypt_kop kop;
1115 * Currently, we know we can do mod exp iff we can do any asymmetric
1116 * operations at all.
1118 if (cryptodev_asymfeat == 0) {
1119 ret = BN_mod_exp(r, a, p, m, ctx);
1123 memset(&kop, 0, sizeof(kop));
1124 kop.crk_op = CRK_MOD_EXP;
1126 /* inputs: a^p % m */
1127 if (bn2crparam(a, &kop.crk_param[0]))
1129 if (bn2crparam(p, &kop.crk_param[1]))
1131 if (bn2crparam(m, &kop.crk_param[2]))
1133 kop.crk_iparams = 3;
1135 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1136 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1137 printf("OCF asym process failed, Running in software\n");
1138 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1140 } else if (ECANCELED == kop.crk_status) {
1141 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1142 printf("OCF hardware operation cancelled. Running in Software\n");
1143 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1145 /* else cryptodev operation worked ok ==> ret = 1 */
1153 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1158 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1164 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1166 struct crypt_kop kop;
1169 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1170 /* XXX 0 means failure?? */
1174 memset(&kop, 0, sizeof(kop));
1175 kop.crk_op = CRK_MOD_EXP_CRT;
1176 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1177 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1179 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1181 if (bn2crparam(I, &kop.crk_param[2]))
1183 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1185 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1187 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1189 kop.crk_iparams = 6;
1191 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1192 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1193 printf("OCF asym process failed, running in Software\n");
1194 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1196 } else if (ECANCELED == kop.crk_status) {
1197 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1198 printf("OCF hardware operation cancelled. Running in Software\n");
1199 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1201 /* else cryptodev operation worked ok ==> ret = 1 */
1208 static RSA_METHOD cryptodev_rsa = {
1209 "cryptodev RSA method",
1210 NULL, /* rsa_pub_enc */
1211 NULL, /* rsa_pub_dec */
1212 NULL, /* rsa_priv_enc */
1213 NULL, /* rsa_priv_dec */
1219 NULL, /* app_data */
1220 NULL, /* rsa_sign */
1221 NULL /* rsa_verify */
1225 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1226 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1228 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1232 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1233 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1234 BN_CTX *ctx, BN_MONT_CTX *mont)
1243 /* v = ( g^u1 * y^u2 mod p ) mod q */
1244 /* let t1 = g ^ u1 mod p */
1247 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
1250 /* let t2 = y ^ u2 mod p */
1251 if (!dsa->meth->bn_mod_exp(dsa, t2, dsa->pub_key, u2, dsa->p, ctx, mont))
1253 /* let u1 = t1 * t2 mod p */
1254 if (!BN_mod_mul(u1, t1, t2, dsa->p, ctx))
1265 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1268 struct crypt_kop kop;
1269 BIGNUM *r = NULL, *s = NULL;
1270 DSA_SIG *dsaret = NULL;
1272 if ((r = BN_new()) == NULL)
1274 if ((s = BN_new()) == NULL) {
1279 memset(&kop, 0, sizeof(kop));
1280 kop.crk_op = CRK_DSA_SIGN;
1282 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1283 kop.crk_param[0].crp_p = (caddr_t) dgst;
1284 kop.crk_param[0].crp_nbits = dlen * 8;
1285 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1287 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1289 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1291 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1293 kop.crk_iparams = 5;
1295 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1296 BN_num_bytes(dsa->q), s) == 0) {
1297 dsaret = DSA_SIG_new();
1303 const DSA_METHOD *meth = DSA_OpenSSL();
1306 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
1309 kop.crk_param[0].crp_p = NULL;
1315 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1316 DSA_SIG *sig, DSA *dsa)
1318 struct crypt_kop kop;
1321 memset(&kop, 0, sizeof(kop));
1322 kop.crk_op = CRK_DSA_VERIFY;
1324 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1325 kop.crk_param[0].crp_p = (caddr_t) dgst;
1326 kop.crk_param[0].crp_nbits = dlen * 8;
1327 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1329 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1331 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1333 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1335 if (bn2crparam(sig->r, &kop.crk_param[5]))
1337 if (bn2crparam(sig->s, &kop.crk_param[6]))
1339 kop.crk_iparams = 7;
1341 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1343 * OCF success value is 0, if not zero, change dsaret to fail
1345 if (0 != kop.crk_status)
1348 const DSA_METHOD *meth = DSA_OpenSSL();
1350 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
1353 kop.crk_param[0].crp_p = NULL;
1358 static DSA_METHOD cryptodev_dsa = {
1359 "cryptodev DSA method",
1361 NULL, /* dsa_sign_setup */
1363 NULL, /* dsa_mod_exp */
1371 #ifndef OPENSSL_NO_DH
1373 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1374 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1377 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1381 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1383 struct crypt_kop kop;
1387 if ((fd = get_asym_dev_crypto()) < 0) {
1388 const DH_METHOD *meth = DH_OpenSSL();
1390 return ((meth->compute_key) (key, pub_key, dh));
1393 keylen = BN_num_bits(dh->p);
1395 memset(&kop, 0, sizeof(kop));
1396 kop.crk_op = CRK_DH_COMPUTE_KEY;
1398 /* inputs: dh->priv_key pub_key dh->p key */
1399 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1401 if (bn2crparam(pub_key, &kop.crk_param[1]))
1403 if (bn2crparam(dh->p, &kop.crk_param[2]))
1405 kop.crk_iparams = 3;
1407 kop.crk_param[3].crp_p = (caddr_t) key;
1408 kop.crk_param[3].crp_nbits = keylen * 8;
1409 kop.crk_oparams = 1;
1411 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1412 const DH_METHOD *meth = DH_OpenSSL();
1414 dhret = (meth->compute_key) (key, pub_key, dh);
1417 kop.crk_param[3].crp_p = NULL;
1422 static DH_METHOD cryptodev_dh = {
1423 "cryptodev DH method",
1424 NULL, /* cryptodev_dh_generate_key */
1433 #endif /* ndef OPENSSL_NO_DH */
1436 * ctrl right now is just a wrapper that doesn't do much
1437 * but I expect we'll want some options soon.
1440 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1442 # ifdef HAVE_SYSLOG_R
1443 struct syslog_data sd = SYSLOG_DATA_INIT;
1448 # ifdef HAVE_SYSLOG_R
1449 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1451 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1458 void ENGINE_load_cryptodev(void)
1460 ENGINE *engine = ENGINE_new();
1465 if ((fd = get_dev_crypto()) < 0) {
1466 ENGINE_free(engine);
1471 * find out what asymmetric crypto algorithms we support
1473 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1475 ENGINE_free(engine);
1480 if (!ENGINE_set_id(engine, "cryptodev") ||
1481 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1482 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1483 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1484 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1485 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1486 ENGINE_free(engine);
1490 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1491 const RSA_METHOD *rsa_meth = RSA_PKCS1_OpenSSL();
1493 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1494 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1495 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1496 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1497 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1498 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1499 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1500 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1501 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1502 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1504 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1508 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1509 const DSA_METHOD *meth = DSA_OpenSSL();
1511 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1512 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1513 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1514 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1515 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1516 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1518 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1519 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1522 #ifndef OPENSSL_NO_DH
1523 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1524 const DH_METHOD *dh_meth = DH_OpenSSL();
1526 cryptodev_dh.generate_key = dh_meth->generate_key;
1527 cryptodev_dh.compute_key = dh_meth->compute_key;
1528 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1529 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1530 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1531 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1532 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1538 ENGINE_free(engine);
1542 #endif /* HAVE_CRYPTODEV */