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 #ifndef HAVE_CRYPTODEV
48 ENGINE_load_cryptodev(void)
50 /* This is a NOP on platforms without /dev/crypto */
56 #include <sys/types.h>
57 #include <crypto/cryptodev.h>
58 #include <openssl/dh.h>
59 #include <openssl/dsa.h>
60 #include <openssl/err.h>
61 #include <openssl/rsa.h>
62 #include <sys/ioctl.h>
72 struct dev_crypto_state {
73 struct session_op d_sess;
76 #ifdef USE_CRYPTODEV_DIGESTS
77 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 int get_asym_dev_crypto(void);
88 static int open_dev_crypto(void);
89 static int get_dev_crypto(void);
90 static int get_cryptodev_ciphers(const int **cnids);
91 #ifdef USE_CRYPTODEV_DIGESTS
92 static int get_cryptodev_digests(const int **cnids);
94 static int cryptodev_usable_ciphers(const int **nids);
95 static int cryptodev_usable_digests(const int **nids);
96 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
97 const unsigned char *in, size_t inl);
98 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
99 const unsigned char *iv, int enc);
100 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
101 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
102 const int **nids, int nid);
103 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
104 const int **nids, int nid);
105 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
106 static int crparam2bn(struct crparam *crp, BIGNUM *a);
107 static void zapparams(struct crypt_kop *kop);
108 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
109 int slen, BIGNUM *s);
111 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
112 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
113 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
114 RSA *rsa, BN_CTX *ctx);
115 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
116 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
117 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
118 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
119 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
120 BN_CTX *ctx, BN_MONT_CTX *mont);
121 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
123 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
124 DSA_SIG *sig, DSA *dsa);
125 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
126 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
128 static int cryptodev_dh_compute_key(unsigned char *key,
129 const BIGNUM *pub_key, DH *dh);
130 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
132 void ENGINE_load_cryptodev(void);
134 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
144 { CRYPTO_ARC4, NID_rc4, 0, 16, },
145 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
146 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
147 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
148 { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
149 { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
150 #ifdef CRYPTO_AES_CTR
151 { CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16, },
152 { CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24, },
153 { CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32, },
155 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
156 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
157 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
158 { 0, NID_undef, 0, 0, },
161 #ifdef USE_CRYPTODEV_DIGESTS
167 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
168 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
169 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
170 { CRYPTO_MD5_KPDK, NID_undef, 0},
171 { CRYPTO_SHA1_KPDK, NID_undef, 0},
172 { CRYPTO_MD5, NID_md5, 16},
173 { CRYPTO_SHA1, NID_sha1, 20},
179 * Return a fd if /dev/crypto seems usable, 0 otherwise.
182 open_dev_crypto(void)
187 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
190 if (fcntl(fd, F_SETFD, 1) == -1) {
204 if ((fd = open_dev_crypto()) == -1)
206 #ifndef CRIOGET_NOT_NEEDED
207 if (ioctl(fd, CRIOGET, &retfd) == -1)
211 if (fcntl(retfd, F_SETFD, 1) == -1) {
221 static void put_dev_crypto(int fd)
223 #ifndef CRIOGET_NOT_NEEDED
228 /* Caching version for asym operations */
230 get_asym_dev_crypto(void)
235 fd = get_dev_crypto();
240 * Find out what ciphers /dev/crypto will let us have a session for.
241 * XXX note, that some of these openssl doesn't deal with yet!
242 * returning them here is harmless, as long as we return NULL
243 * when asked for a handler in the cryptodev_engine_ciphers routine
246 get_cryptodev_ciphers(const int **cnids)
248 static int nids[CRYPTO_ALGORITHM_MAX];
249 struct session_op sess;
250 int fd, i, count = 0;
252 if ((fd = get_dev_crypto()) < 0) {
256 memset(&sess, 0, sizeof(sess));
257 sess.key = (caddr_t)"123456789abcdefghijklmno";
259 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
260 if (ciphers[i].nid == NID_undef)
262 sess.cipher = ciphers[i].id;
263 sess.keylen = ciphers[i].keylen;
265 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
266 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
267 nids[count++] = ciphers[i].nid;
278 #ifdef USE_CRYPTODEV_DIGESTS
280 * Find out what digests /dev/crypto will let us have a session for.
281 * XXX note, that some of these openssl doesn't deal with yet!
282 * returning them here is harmless, as long as we return NULL
283 * when asked for a handler in the cryptodev_engine_digests routine
286 get_cryptodev_digests(const int **cnids)
288 static int nids[CRYPTO_ALGORITHM_MAX];
289 struct session_op sess;
290 int fd, i, count = 0;
292 if ((fd = get_dev_crypto()) < 0) {
296 memset(&sess, 0, sizeof(sess));
297 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
298 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
299 if (digests[i].nid == NID_undef)
301 sess.mac = digests[i].id;
302 sess.mackeylen = digests[i].keylen;
304 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
305 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
306 nids[count++] = digests[i].nid;
319 * Find the useable ciphers|digests from dev/crypto - this is the first
320 * thing called by the engine init crud which determines what it
321 * can use for ciphers from this engine. We want to return
322 * only what we can do, anythine else is handled by software.
324 * If we can't initialize the device to do anything useful for
325 * any reason, we want to return a NULL array, and 0 length,
326 * which forces everything to be done is software. By putting
327 * the initalization of the device in here, we ensure we can
328 * use this engine as the default, and if for whatever reason
329 * /dev/crypto won't do what we want it will just be done in
332 * This can (should) be greatly expanded to perhaps take into
333 * account speed of the device, and what we want to do.
334 * (although the disabling of particular alg's could be controlled
335 * by the device driver with sysctl's.) - this is where we
336 * want most of the decisions made about what we actually want
337 * to use from /dev/crypto.
340 cryptodev_usable_ciphers(const int **nids)
342 return (get_cryptodev_ciphers(nids));
346 cryptodev_usable_digests(const int **nids)
348 #ifdef USE_CRYPTODEV_DIGESTS
349 return (get_cryptodev_digests(nids));
352 * XXXX just disable all digests for now, because it sucks.
353 * we need a better way to decide this - i.e. I may not
354 * want digests on slow cards like hifn on fast machines,
355 * but might want them on slow or loaded machines, etc.
356 * will also want them when using crypto cards that don't
357 * suck moose gonads - would be nice to be able to decide something
358 * as reasonable default without having hackery that's card dependent.
359 * of course, the default should probably be just do everything,
360 * with perhaps a sysctl to turn algoritms off (or have them off
361 * by default) on cards that generally suck like the hifn.
369 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
370 const unsigned char *in, size_t inl)
372 struct crypt_op cryp;
373 struct dev_crypto_state *state = ctx->cipher_data;
374 struct session_op *sess = &state->d_sess;
376 unsigned char save_iv[EVP_MAX_IV_LENGTH];
382 if ((inl % ctx->cipher->block_size) != 0)
385 memset(&cryp, 0, sizeof(cryp));
387 cryp.ses = sess->ses;
390 cryp.src = (caddr_t) in;
391 cryp.dst = (caddr_t) out;
394 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
396 if (ctx->cipher->iv_len) {
397 cryp.iv = (caddr_t) ctx->iv;
399 iiv = in + inl - ctx->cipher->iv_len;
400 memcpy(save_iv, iiv, ctx->cipher->iv_len);
405 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
406 /* XXX need better errror handling
407 * this can fail for a number of different reasons.
412 if (ctx->cipher->iv_len) {
414 iiv = out + inl - ctx->cipher->iv_len;
417 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
423 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
424 const unsigned char *iv, int enc)
426 struct dev_crypto_state *state = ctx->cipher_data;
427 struct session_op *sess = &state->d_sess;
430 for (i = 0; ciphers[i].id; i++)
431 if (ctx->cipher->nid == ciphers[i].nid &&
432 ctx->cipher->iv_len <= ciphers[i].ivmax &&
433 ctx->key_len == ciphers[i].keylen) {
434 cipher = ciphers[i].id;
438 if (!ciphers[i].id) {
443 memset(sess, 0, sizeof(struct session_op));
445 if ((state->d_fd = get_dev_crypto()) < 0)
448 sess->key = (caddr_t)key;
449 sess->keylen = ctx->key_len;
450 sess->cipher = cipher;
452 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
453 put_dev_crypto(state->d_fd);
461 * free anything we allocated earlier when initting a
462 * session, and close the session.
465 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
468 struct dev_crypto_state *state = ctx->cipher_data;
469 struct session_op *sess = &state->d_sess;
474 /* XXX if this ioctl fails, someting's wrong. the invoker
475 * may have called us with a bogus ctx, or we could
476 * have a device that for whatever reason just doesn't
477 * want to play ball - it's not clear what's right
478 * here - should this be an error? should it just
479 * increase a counter, hmm. For right now, we return
480 * 0 - I don't believe that to be "right". we could
481 * call the gorpy openssl lib error handlers that
482 * print messages to users of the library. hmm..
485 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
490 put_dev_crypto(state->d_fd);
497 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
498 * gets called when libcrypto requests a cipher NID.
502 const EVP_CIPHER cryptodev_rc4 = {
505 EVP_CIPH_VARIABLE_LENGTH,
509 sizeof(struct dev_crypto_state),
516 const EVP_CIPHER cryptodev_des_cbc = {
523 sizeof(struct dev_crypto_state),
524 EVP_CIPHER_set_asn1_iv,
525 EVP_CIPHER_get_asn1_iv,
530 const EVP_CIPHER cryptodev_3des_cbc = {
537 sizeof(struct dev_crypto_state),
538 EVP_CIPHER_set_asn1_iv,
539 EVP_CIPHER_get_asn1_iv,
543 const EVP_CIPHER cryptodev_bf_cbc = {
550 sizeof(struct dev_crypto_state),
551 EVP_CIPHER_set_asn1_iv,
552 EVP_CIPHER_get_asn1_iv,
556 const EVP_CIPHER cryptodev_cast_cbc = {
563 sizeof(struct dev_crypto_state),
564 EVP_CIPHER_set_asn1_iv,
565 EVP_CIPHER_get_asn1_iv,
569 const EVP_CIPHER cryptodev_aes_cbc = {
576 sizeof(struct dev_crypto_state),
577 EVP_CIPHER_set_asn1_iv,
578 EVP_CIPHER_get_asn1_iv,
582 const EVP_CIPHER cryptodev_aes_192_cbc = {
589 sizeof(struct dev_crypto_state),
590 EVP_CIPHER_set_asn1_iv,
591 EVP_CIPHER_get_asn1_iv,
595 const EVP_CIPHER cryptodev_aes_256_cbc = {
602 sizeof(struct dev_crypto_state),
603 EVP_CIPHER_set_asn1_iv,
604 EVP_CIPHER_get_asn1_iv,
607 #ifdef CRYPTO_AES_CTR
608 const EVP_CIPHER cryptodev_aes_ctr = {
615 sizeof(struct dev_crypto_state),
616 EVP_CIPHER_set_asn1_iv,
617 EVP_CIPHER_get_asn1_iv,
621 const EVP_CIPHER cryptodev_aes_ctr_192 = {
628 sizeof(struct dev_crypto_state),
629 EVP_CIPHER_set_asn1_iv,
630 EVP_CIPHER_get_asn1_iv,
634 const EVP_CIPHER cryptodev_aes_ctr_256 = {
641 sizeof(struct dev_crypto_state),
642 EVP_CIPHER_set_asn1_iv,
643 EVP_CIPHER_get_asn1_iv,
648 * Registered by the ENGINE when used to find out how to deal with
649 * a particular NID in the ENGINE. this says what we'll do at the
650 * top level - note, that list is restricted by what we answer with
653 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
654 const int **nids, int nid)
657 return (cryptodev_usable_ciphers(nids));
661 *cipher = &cryptodev_rc4;
663 case NID_des_ede3_cbc:
664 *cipher = &cryptodev_3des_cbc;
667 *cipher = &cryptodev_des_cbc;
670 *cipher = &cryptodev_bf_cbc;
673 *cipher = &cryptodev_cast_cbc;
675 case NID_aes_128_cbc:
676 *cipher = &cryptodev_aes_cbc;
678 case NID_aes_192_cbc:
679 *cipher = &cryptodev_aes_192_cbc;
681 case NID_aes_256_cbc:
682 *cipher = &cryptodev_aes_256_cbc;
684 #ifdef CRYPTO_AES_CTR
685 case NID_aes_128_ctr:
686 *cipher = &cryptodev_aes_ctr;
688 case NID_aes_192_ctr:
689 *cipher = &cryptodev_aes_ctr_192;
691 case NID_aes_256_ctr:
692 *cipher = &cryptodev_aes_ctr_256;
699 return (*cipher != NULL);
703 #ifdef USE_CRYPTODEV_DIGESTS
705 /* convert digest type to cryptodev */
707 digest_nid_to_cryptodev(int nid)
711 for (i = 0; digests[i].id; i++)
712 if (digests[i].nid == nid)
713 return (digests[i].id);
719 digest_key_length(int nid)
723 for (i = 0; digests[i].id; i++)
724 if (digests[i].nid == nid)
725 return digests[i].keylen;
730 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
732 struct dev_crypto_state *state = ctx->md_data;
733 struct session_op *sess = &state->d_sess;
736 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
737 printf("cryptodev_digest_init: Can't get digest \n");
741 memset(state, 0, sizeof(struct dev_crypto_state));
743 if ((state->d_fd = get_dev_crypto()) < 0) {
744 printf("cryptodev_digest_init: Can't get Dev \n");
748 sess->mackey = state->dummy_mac_key;
749 sess->mackeylen = digest_key_length(ctx->digest->type);
752 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
753 put_dev_crypto(state->d_fd);
755 printf("cryptodev_digest_init: Open session failed\n");
762 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
765 struct crypt_op cryp;
766 struct dev_crypto_state *state = ctx->md_data;
767 struct session_op *sess = &state->d_sess;
770 if (!data || state->d_fd < 0) {
771 printf("cryptodev_digest_update: illegal inputs \n");
779 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
780 /* if application doesn't support one buffer */
781 new_mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
784 printf("cryptodev_digest_update: realloc failed\n");
787 state->mac_data = new_mac_data;
789 memcpy(state->mac_data + state->mac_len, data, count);
790 state->mac_len += count;
795 memset(&cryp, 0, sizeof(cryp));
797 cryp.ses = sess->ses;
800 cryp.src = (caddr_t) data;
802 cryp.mac = (caddr_t) state->digest_res;
803 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
804 printf("cryptodev_digest_update: digest failed\n");
811 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
813 struct crypt_op cryp;
814 struct dev_crypto_state *state = ctx->md_data;
815 struct session_op *sess = &state->d_sess;
819 if (!md || state->d_fd < 0) {
820 printf("cryptodev_digest_final: illegal input\n");
824 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
825 /* if application doesn't support one buffer */
826 memset(&cryp, 0, sizeof(cryp));
827 cryp.ses = sess->ses;
829 cryp.len = state->mac_len;
830 cryp.src = state->mac_data;
832 cryp.mac = (caddr_t)md;
833 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
834 printf("cryptodev_digest_final: digest failed\n");
841 memcpy(md, state->digest_res, ctx->digest->md_size);
847 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
850 struct dev_crypto_state *state = ctx->md_data;
851 struct session_op *sess = &state->d_sess;
856 if (state->d_fd < 0) {
857 printf("cryptodev_digest_cleanup: illegal input\n");
861 if (state->mac_data) {
862 OPENSSL_free(state->mac_data);
863 state->mac_data = NULL;
867 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
868 printf("cryptodev_digest_cleanup: failed to close session\n");
873 put_dev_crypto(state->d_fd);
879 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
881 struct dev_crypto_state *fstate = from->md_data;
882 struct dev_crypto_state *dstate = to->md_data;
883 struct session_op *sess;
886 if (dstate == NULL || fstate == NULL)
889 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
891 sess = &dstate->d_sess;
893 digest = digest_nid_to_cryptodev(to->digest->type);
895 sess->mackey = dstate->dummy_mac_key;
896 sess->mackeylen = digest_key_length(to->digest->type);
899 dstate->d_fd = get_dev_crypto();
901 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
902 put_dev_crypto(dstate->d_fd);
904 printf("cryptodev_digest_copy: Open session failed\n");
908 if (fstate->mac_len != 0) {
909 if (fstate->mac_data != NULL)
911 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
912 if (dstate->mac_data == NULL)
914 printf("cryptodev_digest_copy: mac_data allocation failed\n");
917 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
918 dstate->mac_len = fstate->mac_len;
926 const EVP_MD cryptodev_sha1 = {
931 cryptodev_digest_init,
932 cryptodev_digest_update,
933 cryptodev_digest_final,
934 cryptodev_digest_copy,
935 cryptodev_digest_cleanup,
936 EVP_PKEY_NULL_method,
938 sizeof(struct dev_crypto_state),
941 const EVP_MD cryptodev_md5 = {
944 16 /* MD5_DIGEST_LENGTH */,
946 cryptodev_digest_init,
947 cryptodev_digest_update,
948 cryptodev_digest_final,
949 cryptodev_digest_copy,
950 cryptodev_digest_cleanup,
951 EVP_PKEY_NULL_method,
953 sizeof(struct dev_crypto_state),
956 #endif /* USE_CRYPTODEV_DIGESTS */
960 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
961 const int **nids, int nid)
964 return (cryptodev_usable_digests(nids));
967 #ifdef USE_CRYPTODEV_DIGESTS
969 *digest = &cryptodev_md5;
972 *digest = &cryptodev_sha1;
975 #endif /* USE_CRYPTODEV_DIGESTS */
979 return (*digest != NULL);
983 * Convert a BIGNUM to the representation that /dev/crypto needs.
984 * Upon completion of use, the caller is responsible for freeing
988 bn2crparam(const BIGNUM *a, struct crparam *crp)
997 bits = BN_num_bits(a);
998 bytes = (bits + 7) / 8;
1003 memset(b, 0, bytes);
1005 crp->crp_p = (caddr_t) b;
1006 crp->crp_nbits = bits;
1008 for (i = 0, j = 0; i < a->top; i++) {
1009 for (k = 0; k < BN_BITS2 / 8; k++) {
1010 if ((j + k) >= bytes)
1012 b[j + k] = a->d[i] >> (k * 8);
1019 /* Convert a /dev/crypto parameter to a BIGNUM */
1021 crparam2bn(struct crparam *crp, BIGNUM *a)
1026 bytes = (crp->crp_nbits + 7) / 8;
1031 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1034 for (i = 0; i < bytes; i++)
1035 pd[i] = crp->crp_p[bytes - i - 1];
1037 BN_bin2bn(pd, bytes, a);
1044 zapparams(struct crypt_kop *kop)
1048 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1049 if (kop->crk_param[i].crp_p)
1050 free(kop->crk_param[i].crp_p);
1051 kop->crk_param[i].crp_p = NULL;
1052 kop->crk_param[i].crp_nbits = 0;
1057 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1061 if ((fd = get_asym_dev_crypto()) < 0)
1065 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1066 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1070 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1071 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1075 if (ioctl(fd, CIOCKEY, kop) == 0) {
1077 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1079 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1087 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1088 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1090 struct crypt_kop kop;
1093 /* Currently, we know we can do mod exp iff we can do any
1094 * asymmetric operations at all.
1096 if (cryptodev_asymfeat == 0) {
1097 ret = BN_mod_exp(r, a, p, m, ctx);
1101 memset(&kop, 0, sizeof kop);
1102 kop.crk_op = CRK_MOD_EXP;
1104 /* inputs: a^p % m */
1105 if (bn2crparam(a, &kop.crk_param[0]))
1107 if (bn2crparam(p, &kop.crk_param[1]))
1109 if (bn2crparam(m, &kop.crk_param[2]))
1111 kop.crk_iparams = 3;
1113 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1114 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1115 printf("OCF asym process failed, Running in software\n");
1116 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1118 } else if (ECANCELED == kop.crk_status) {
1119 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1120 printf("OCF hardware operation cancelled. Running in Software\n");
1121 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1123 /* else cryptodev operation worked ok ==> ret = 1*/
1131 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1135 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1141 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1143 struct crypt_kop kop;
1146 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1147 /* XXX 0 means failure?? */
1151 memset(&kop, 0, sizeof kop);
1152 kop.crk_op = CRK_MOD_EXP_CRT;
1153 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1154 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1156 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1158 if (bn2crparam(I, &kop.crk_param[2]))
1160 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1162 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1164 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1166 kop.crk_iparams = 6;
1168 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1169 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1170 printf("OCF asym process failed, running in Software\n");
1171 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1173 } else if (ECANCELED == kop.crk_status) {
1174 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1175 printf("OCF hardware operation cancelled. Running in Software\n");
1176 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1178 /* else cryptodev operation worked ok ==> ret = 1*/
1185 static RSA_METHOD cryptodev_rsa = {
1186 "cryptodev RSA method",
1187 NULL, /* rsa_pub_enc */
1188 NULL, /* rsa_pub_dec */
1189 NULL, /* rsa_priv_enc */
1190 NULL, /* rsa_priv_dec */
1196 NULL, /* app_data */
1197 NULL, /* rsa_sign */
1198 NULL /* rsa_verify */
1202 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1203 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1205 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1209 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1210 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1211 BN_CTX *ctx, BN_MONT_CTX *mont)
1218 /* v = ( g^u1 * y^u2 mod p ) mod q */
1219 /* let t1 = g ^ u1 mod p */
1222 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1225 /* let t2 = y ^ u2 mod p */
1226 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1228 /* let u1 = t1 * t2 mod p */
1229 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1241 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1243 struct crypt_kop kop;
1244 BIGNUM *r = NULL, *s = NULL;
1245 DSA_SIG *dsaret = NULL;
1247 if ((r = BN_new()) == NULL)
1249 if ((s = BN_new()) == NULL) {
1254 memset(&kop, 0, sizeof kop);
1255 kop.crk_op = CRK_DSA_SIGN;
1257 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1258 kop.crk_param[0].crp_p = (caddr_t)dgst;
1259 kop.crk_param[0].crp_nbits = dlen * 8;
1260 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1262 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1264 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1266 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1268 kop.crk_iparams = 5;
1270 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1271 BN_num_bytes(dsa->q), s) == 0) {
1272 dsaret = DSA_SIG_new();
1276 const DSA_METHOD *meth = DSA_OpenSSL();
1279 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1282 kop.crk_param[0].crp_p = NULL;
1288 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1289 DSA_SIG *sig, DSA *dsa)
1291 struct crypt_kop kop;
1294 memset(&kop, 0, sizeof kop);
1295 kop.crk_op = CRK_DSA_VERIFY;
1297 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1298 kop.crk_param[0].crp_p = (caddr_t)dgst;
1299 kop.crk_param[0].crp_nbits = dlen * 8;
1300 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1302 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1304 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1306 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1308 if (bn2crparam(sig->r, &kop.crk_param[5]))
1310 if (bn2crparam(sig->s, &kop.crk_param[6]))
1312 kop.crk_iparams = 7;
1314 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1315 /*OCF success value is 0, if not zero, change dsaret to fail*/
1316 if(0 != kop.crk_status) dsaret = 0;
1318 const DSA_METHOD *meth = DSA_OpenSSL();
1320 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1323 kop.crk_param[0].crp_p = NULL;
1328 static DSA_METHOD cryptodev_dsa = {
1329 "cryptodev DSA method",
1331 NULL, /* dsa_sign_setup */
1333 NULL, /* dsa_mod_exp */
1342 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1343 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1346 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1350 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1352 struct crypt_kop kop;
1356 if ((fd = get_asym_dev_crypto()) < 0) {
1357 const DH_METHOD *meth = DH_OpenSSL();
1359 return ((meth->compute_key)(key, pub_key, dh));
1362 keylen = BN_num_bits(dh->p);
1364 memset(&kop, 0, sizeof kop);
1365 kop.crk_op = CRK_DH_COMPUTE_KEY;
1367 /* inputs: dh->priv_key pub_key dh->p key */
1368 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1370 if (bn2crparam(pub_key, &kop.crk_param[1]))
1372 if (bn2crparam(dh->p, &kop.crk_param[2]))
1374 kop.crk_iparams = 3;
1376 kop.crk_param[3].crp_p = (caddr_t) key;
1377 kop.crk_param[3].crp_nbits = keylen * 8;
1378 kop.crk_oparams = 1;
1380 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1381 const DH_METHOD *meth = DH_OpenSSL();
1383 dhret = (meth->compute_key)(key, pub_key, dh);
1386 kop.crk_param[3].crp_p = NULL;
1391 static DH_METHOD cryptodev_dh = {
1392 "cryptodev DH method",
1393 NULL, /* cryptodev_dh_generate_key */
1403 * ctrl right now is just a wrapper that doesn't do much
1404 * but I expect we'll want some options soon.
1407 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1409 #ifdef HAVE_SYSLOG_R
1410 struct syslog_data sd = SYSLOG_DATA_INIT;
1415 #ifdef HAVE_SYSLOG_R
1416 syslog_r(LOG_ERR, &sd,
1417 "cryptodev_ctrl: unknown command %d", cmd);
1419 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1427 ENGINE_load_cryptodev(void)
1429 ENGINE *engine = ENGINE_new();
1434 if ((fd = get_dev_crypto()) < 0) {
1435 ENGINE_free(engine);
1440 * find out what asymmetric crypto algorithms we support
1442 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1444 ENGINE_free(engine);
1449 if (!ENGINE_set_id(engine, "cryptodev") ||
1450 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1451 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1452 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1453 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1454 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1455 ENGINE_free(engine);
1459 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1460 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1462 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1463 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1464 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1465 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1466 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1467 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1468 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1469 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1470 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1471 cryptodev_rsa.rsa_mod_exp =
1472 cryptodev_rsa_mod_exp;
1474 cryptodev_rsa.rsa_mod_exp =
1475 cryptodev_rsa_nocrt_mod_exp;
1479 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1480 const DSA_METHOD *meth = DSA_OpenSSL();
1482 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1483 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1484 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1485 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1486 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1487 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1489 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1490 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1493 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1494 const DH_METHOD *dh_meth = DH_OpenSSL();
1496 cryptodev_dh.generate_key = dh_meth->generate_key;
1497 cryptodev_dh.compute_key = dh_meth->compute_key;
1498 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1499 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1500 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1501 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1502 cryptodev_dh.compute_key =
1503 cryptodev_dh_compute_key;
1508 ENGINE_free(engine);
1512 #endif /* HAVE_CRYPTODEV */