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.
30 #include <openssl/objects.h>
31 #include <openssl/engine.h>
32 #include <openssl/evp.h>
33 #include <openssl/bn.h>
35 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
36 (defined(OpenBSD) || defined(__FreeBSD__))
37 # include <sys/param.h>
38 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
39 # define HAVE_CRYPTODEV
41 # if (OpenBSD >= 200110)
42 # define HAVE_SYSLOG_R
46 #ifndef HAVE_CRYPTODEV
48 void 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;
75 # ifdef USE_CRYPTODEV_DIGESTS
76 char dummy_mac_key[HASH_MAX_LEN];
77 unsigned char digest_res[HASH_MAX_LEN];
83 static u_int32_t cryptodev_asymfeat = 0;
85 static int get_asym_dev_crypto(void);
86 static int open_dev_crypto(void);
87 static int get_dev_crypto(void);
88 static int get_cryptodev_ciphers(const int **cnids);
89 # ifdef USE_CRYPTODEV_DIGESTS
90 static int get_cryptodev_digests(const int **cnids);
92 static int cryptodev_usable_ciphers(const int **nids);
93 static int cryptodev_usable_digests(const int **nids);
94 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
95 const unsigned char *in, size_t inl);
96 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
97 const unsigned char *iv, int enc);
98 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
99 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
100 const int **nids, int nid);
101 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
102 const int **nids, int nid);
103 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
104 static int crparam2bn(struct crparam *crp, BIGNUM *a);
105 static void zapparams(struct crypt_kop *kop);
106 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
107 int slen, BIGNUM *s);
109 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
110 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
112 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
114 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
116 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
117 const BIGNUM *p, const BIGNUM *m,
118 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
119 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
120 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
121 BIGNUM *p, BN_CTX *ctx,
123 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
125 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
126 DSA_SIG *sig, DSA *dsa);
127 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
128 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
130 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
132 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
134 void ENGINE_load_cryptodev(void);
136 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
147 CRYPTO_ARC4, NID_rc4, 0, 16,
150 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
153 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
156 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
159 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
162 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
164 # ifdef CRYPTO_AES_CTR
166 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
169 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
172 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
176 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
179 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
182 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
189 # ifdef USE_CRYPTODEV_DIGESTS
196 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
199 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
202 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
206 CRYPTO_MD5_KPDK, NID_undef, 0
209 CRYPTO_SHA1_KPDK, NID_undef, 0
212 CRYPTO_MD5, NID_md5, 16
215 CRYPTO_SHA1, NID_sha1, 20
224 * Return a fd if /dev/crypto seems usable, 0 otherwise.
226 static int open_dev_crypto(void)
231 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
234 if (fcntl(fd, F_SETFD, 1) == -1) {
243 static int get_dev_crypto(void)
247 if ((fd = open_dev_crypto()) == -1)
249 # ifndef CRIOGET_NOT_NEEDED
250 if (ioctl(fd, CRIOGET, &retfd) == -1)
254 if (fcntl(retfd, F_SETFD, 1) == -1) {
264 static void put_dev_crypto(int fd)
266 # ifndef CRIOGET_NOT_NEEDED
271 /* Caching version for asym operations */
272 static int get_asym_dev_crypto(void)
277 fd = get_dev_crypto();
282 * Find out what ciphers /dev/crypto will let us have a session for.
283 * XXX note, that some of these openssl doesn't deal with yet!
284 * returning them here is harmless, as long as we return NULL
285 * when asked for a handler in the cryptodev_engine_ciphers routine
287 static int get_cryptodev_ciphers(const int **cnids)
289 static int nids[CRYPTO_ALGORITHM_MAX];
290 struct session_op sess;
291 int fd, i, count = 0;
293 if ((fd = get_dev_crypto()) < 0) {
297 memset(&sess, 0, sizeof(sess));
298 sess.key = (caddr_t) "123456789abcdefghijklmno";
300 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
301 if (ciphers[i].nid == NID_undef)
303 sess.cipher = ciphers[i].id;
304 sess.keylen = ciphers[i].keylen;
306 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
307 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
308 nids[count++] = ciphers[i].nid;
319 # ifdef USE_CRYPTODEV_DIGESTS
321 * Find out what digests /dev/crypto will let us have a session for.
322 * XXX note, that some of these openssl doesn't deal with yet!
323 * returning them here is harmless, as long as we return NULL
324 * when asked for a handler in the cryptodev_engine_digests routine
326 static int get_cryptodev_digests(const int **cnids)
328 static int nids[CRYPTO_ALGORITHM_MAX];
329 struct session_op sess;
330 int fd, i, count = 0;
332 if ((fd = get_dev_crypto()) < 0) {
336 memset(&sess, 0, sizeof(sess));
337 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
338 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
339 if (digests[i].nid == NID_undef)
341 sess.mac = digests[i].id;
342 sess.mackeylen = digests[i].keylen;
344 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
345 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
346 nids[count++] = digests[i].nid;
359 * Find the useable ciphers|digests from dev/crypto - this is the first
360 * thing called by the engine init crud which determines what it
361 * can use for ciphers from this engine. We want to return
362 * only what we can do, anythine else is handled by software.
364 * If we can't initialize the device to do anything useful for
365 * any reason, we want to return a NULL array, and 0 length,
366 * which forces everything to be done is software. By putting
367 * the initalization of the device in here, we ensure we can
368 * use this engine as the default, and if for whatever reason
369 * /dev/crypto won't do what we want it will just be done in
372 * This can (should) be greatly expanded to perhaps take into
373 * account speed of the device, and what we want to do.
374 * (although the disabling of particular alg's could be controlled
375 * by the device driver with sysctl's.) - this is where we
376 * want most of the decisions made about what we actually want
377 * to use from /dev/crypto.
379 static int cryptodev_usable_ciphers(const int **nids)
381 return (get_cryptodev_ciphers(nids));
384 static int cryptodev_usable_digests(const int **nids)
386 # ifdef USE_CRYPTODEV_DIGESTS
387 return (get_cryptodev_digests(nids));
390 * XXXX just disable all digests for now, because it sucks.
391 * we need a better way to decide this - i.e. I may not
392 * want digests on slow cards like hifn on fast machines,
393 * but might want them on slow or loaded machines, etc.
394 * will also want them when using crypto cards that don't
395 * suck moose gonads - would be nice to be able to decide something
396 * as reasonable default without having hackery that's card dependent.
397 * of course, the default should probably be just do everything,
398 * with perhaps a sysctl to turn algoritms off (or have them off
399 * by default) on cards that generally suck like the hifn.
407 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
408 const unsigned char *in, size_t inl)
410 struct crypt_op cryp;
411 struct dev_crypto_state *state = ctx->cipher_data;
412 struct session_op *sess = &state->d_sess;
414 unsigned char save_iv[EVP_MAX_IV_LENGTH];
420 if ((inl % ctx->cipher->block_size) != 0)
423 memset(&cryp, 0, sizeof(cryp));
425 cryp.ses = sess->ses;
428 cryp.src = (caddr_t) in;
429 cryp.dst = (caddr_t) out;
432 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
434 if (ctx->cipher->iv_len) {
435 cryp.iv = (caddr_t) ctx->iv;
437 iiv = in + inl - ctx->cipher->iv_len;
438 memcpy(save_iv, iiv, ctx->cipher->iv_len);
443 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
445 * XXX need better errror handling this can fail for a number of
451 if (ctx->cipher->iv_len) {
453 iiv = out + inl - ctx->cipher->iv_len;
456 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
462 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
463 const unsigned char *iv, int enc)
465 struct dev_crypto_state *state = ctx->cipher_data;
466 struct session_op *sess = &state->d_sess;
469 for (i = 0; ciphers[i].id; i++)
470 if (ctx->cipher->nid == ciphers[i].nid &&
471 ctx->cipher->iv_len <= ciphers[i].ivmax &&
472 ctx->key_len == ciphers[i].keylen) {
473 cipher = ciphers[i].id;
477 if (!ciphers[i].id) {
482 memset(sess, 0, sizeof(struct session_op));
484 if ((state->d_fd = get_dev_crypto()) < 0)
487 sess->key = (caddr_t) key;
488 sess->keylen = ctx->key_len;
489 sess->cipher = cipher;
491 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
492 put_dev_crypto(state->d_fd);
500 * free anything we allocated earlier when initting a
501 * session, and close the session.
503 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
506 struct dev_crypto_state *state = ctx->cipher_data;
507 struct session_op *sess = &state->d_sess;
513 * XXX if this ioctl fails, someting's wrong. the invoker may have called
514 * us with a bogus ctx, or we could have a device that for whatever
515 * reason just doesn't want to play ball - it's not clear what's right
516 * here - should this be an error? should it just increase a counter,
517 * hmm. For right now, we return 0 - I don't believe that to be "right".
518 * we could call the gorpy openssl lib error handlers that print messages
519 * to users of the library. hmm..
522 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
527 put_dev_crypto(state->d_fd);
534 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
535 * gets called when libcrypto requests a cipher NID.
539 const EVP_CIPHER cryptodev_rc4 = {
542 EVP_CIPH_VARIABLE_LENGTH,
546 sizeof(struct dev_crypto_state),
553 const EVP_CIPHER cryptodev_des_cbc = {
560 sizeof(struct dev_crypto_state),
561 EVP_CIPHER_set_asn1_iv,
562 EVP_CIPHER_get_asn1_iv,
567 const EVP_CIPHER cryptodev_3des_cbc = {
574 sizeof(struct dev_crypto_state),
575 EVP_CIPHER_set_asn1_iv,
576 EVP_CIPHER_get_asn1_iv,
580 const EVP_CIPHER cryptodev_bf_cbc = {
587 sizeof(struct dev_crypto_state),
588 EVP_CIPHER_set_asn1_iv,
589 EVP_CIPHER_get_asn1_iv,
593 const EVP_CIPHER cryptodev_cast_cbc = {
600 sizeof(struct dev_crypto_state),
601 EVP_CIPHER_set_asn1_iv,
602 EVP_CIPHER_get_asn1_iv,
606 const EVP_CIPHER cryptodev_aes_cbc = {
613 sizeof(struct dev_crypto_state),
614 EVP_CIPHER_set_asn1_iv,
615 EVP_CIPHER_get_asn1_iv,
619 const EVP_CIPHER cryptodev_aes_192_cbc = {
626 sizeof(struct dev_crypto_state),
627 EVP_CIPHER_set_asn1_iv,
628 EVP_CIPHER_get_asn1_iv,
632 const EVP_CIPHER cryptodev_aes_256_cbc = {
639 sizeof(struct dev_crypto_state),
640 EVP_CIPHER_set_asn1_iv,
641 EVP_CIPHER_get_asn1_iv,
645 # ifdef CRYPTO_AES_CTR
646 const EVP_CIPHER cryptodev_aes_ctr = {
653 sizeof(struct dev_crypto_state),
654 EVP_CIPHER_set_asn1_iv,
655 EVP_CIPHER_get_asn1_iv,
659 const EVP_CIPHER cryptodev_aes_ctr_192 = {
666 sizeof(struct dev_crypto_state),
667 EVP_CIPHER_set_asn1_iv,
668 EVP_CIPHER_get_asn1_iv,
672 const EVP_CIPHER cryptodev_aes_ctr_256 = {
679 sizeof(struct dev_crypto_state),
680 EVP_CIPHER_set_asn1_iv,
681 EVP_CIPHER_get_asn1_iv,
686 * Registered by the ENGINE when used to find out how to deal with
687 * a particular NID in the ENGINE. this says what we'll do at the
688 * top level - note, that list is restricted by what we answer with
691 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
692 const int **nids, int nid)
695 return (cryptodev_usable_ciphers(nids));
699 *cipher = &cryptodev_rc4;
701 case NID_des_ede3_cbc:
702 *cipher = &cryptodev_3des_cbc;
705 *cipher = &cryptodev_des_cbc;
708 *cipher = &cryptodev_bf_cbc;
711 *cipher = &cryptodev_cast_cbc;
713 case NID_aes_128_cbc:
714 *cipher = &cryptodev_aes_cbc;
716 case NID_aes_192_cbc:
717 *cipher = &cryptodev_aes_192_cbc;
719 case NID_aes_256_cbc:
720 *cipher = &cryptodev_aes_256_cbc;
722 # ifdef CRYPTO_AES_CTR
723 case NID_aes_128_ctr:
724 *cipher = &cryptodev_aes_ctr;
726 case NID_aes_192_ctr:
727 *cipher = &cryptodev_aes_ctr_192;
729 case NID_aes_256_ctr:
730 *cipher = &cryptodev_aes_ctr_256;
737 return (*cipher != NULL);
740 # ifdef USE_CRYPTODEV_DIGESTS
742 /* convert digest type to cryptodev */
743 static int digest_nid_to_cryptodev(int nid)
747 for (i = 0; digests[i].id; i++)
748 if (digests[i].nid == nid)
749 return (digests[i].id);
753 static int digest_key_length(int nid)
757 for (i = 0; digests[i].id; i++)
758 if (digests[i].nid == nid)
759 return digests[i].keylen;
763 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
765 struct dev_crypto_state *state = ctx->md_data;
766 struct session_op *sess = &state->d_sess;
769 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) {
770 printf("cryptodev_digest_init: Can't get digest \n");
774 memset(state, 0, sizeof(struct dev_crypto_state));
776 if ((state->d_fd = get_dev_crypto()) < 0) {
777 printf("cryptodev_digest_init: Can't get Dev \n");
781 sess->mackey = state->dummy_mac_key;
782 sess->mackeylen = digest_key_length(ctx->digest->type);
785 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
786 put_dev_crypto(state->d_fd);
788 printf("cryptodev_digest_init: Open session failed\n");
795 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
798 struct crypt_op cryp;
799 struct dev_crypto_state *state = ctx->md_data;
800 struct session_op *sess = &state->d_sess;
802 if (!data || state->d_fd < 0) {
803 printf("cryptodev_digest_update: illegal inputs \n");
811 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
812 /* if application doesn't support one buffer */
814 OPENSSL_realloc(state->mac_data, state->mac_len + count);
816 if (mac_data == NULL) {
817 printf("cryptodev_digest_update: realloc failed\n");
821 state->mac_data = mac_data;
822 memcpy(state->mac_data + state->mac_len, data, count);
823 state->mac_len += count;
828 memset(&cryp, 0, sizeof(cryp));
830 cryp.ses = sess->ses;
833 cryp.src = (caddr_t) data;
835 cryp.mac = (caddr_t) state->digest_res;
836 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
837 printf("cryptodev_digest_update: digest failed\n");
843 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
845 struct crypt_op cryp;
846 struct dev_crypto_state *state = ctx->md_data;
847 struct session_op *sess = &state->d_sess;
851 if (!md || state->d_fd < 0) {
852 printf("cryptodev_digest_final: illegal input\n");
856 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
857 /* if application doesn't support one buffer */
858 memset(&cryp, 0, sizeof(cryp));
859 cryp.ses = sess->ses;
861 cryp.len = state->mac_len;
862 cryp.src = state->mac_data;
864 cryp.mac = (caddr_t) md;
865 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
866 printf("cryptodev_digest_final: digest failed\n");
873 memcpy(md, state->digest_res, ctx->digest->md_size);
878 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
881 struct dev_crypto_state *state = ctx->md_data;
882 struct session_op *sess = &state->d_sess;
887 if (state->d_fd < 0) {
888 printf("cryptodev_digest_cleanup: illegal input\n");
892 if (state->mac_data) {
893 OPENSSL_free(state->mac_data);
894 state->mac_data = NULL;
898 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
899 printf("cryptodev_digest_cleanup: failed to close session\n");
904 put_dev_crypto(state->d_fd);
910 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
912 struct dev_crypto_state *fstate = from->md_data;
913 struct dev_crypto_state *dstate = to->md_data;
914 struct session_op *sess;
917 if (dstate == NULL || fstate == NULL)
920 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
922 sess = &dstate->d_sess;
924 digest = digest_nid_to_cryptodev(to->digest->type);
926 sess->mackey = dstate->dummy_mac_key;
927 sess->mackeylen = digest_key_length(to->digest->type);
930 dstate->d_fd = get_dev_crypto();
932 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
933 put_dev_crypto(dstate->d_fd);
935 printf("cryptodev_digest_init: Open session failed\n");
939 dstate->mac_len = fstate->mac_len;
940 if (fstate->mac_len != 0) {
941 if (fstate->mac_data != NULL) {
942 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
943 if (dstate->mac_data == NULL) {
944 printf("cryptodev_digest_init: malloc failed\n");
947 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
954 const EVP_MD cryptodev_sha1 = {
959 cryptodev_digest_init,
960 cryptodev_digest_update,
961 cryptodev_digest_final,
962 cryptodev_digest_copy,
963 cryptodev_digest_cleanup,
964 EVP_PKEY_NULL_method,
966 sizeof(struct dev_crypto_state),
969 const EVP_MD cryptodev_md5 = {
972 16 /* MD5_DIGEST_LENGTH */ ,
974 cryptodev_digest_init,
975 cryptodev_digest_update,
976 cryptodev_digest_final,
977 cryptodev_digest_copy,
978 cryptodev_digest_cleanup,
979 EVP_PKEY_NULL_method,
980 64 /* MD5_CBLOCK */ ,
981 sizeof(struct dev_crypto_state),
984 # endif /* USE_CRYPTODEV_DIGESTS */
987 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
988 const int **nids, int nid)
991 return (cryptodev_usable_digests(nids));
994 # ifdef USE_CRYPTODEV_DIGESTS
996 *digest = &cryptodev_md5;
999 *digest = &cryptodev_sha1;
1002 # endif /* USE_CRYPTODEV_DIGESTS */
1006 return (*digest != NULL);
1010 * Convert a BIGNUM to the representation that /dev/crypto needs.
1011 * Upon completion of use, the caller is responsible for freeing
1014 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1017 ssize_t bytes, bits;
1023 bits = BN_num_bits(a);
1024 bytes = (bits + 7) / 8;
1029 memset(b, 0, bytes);
1031 crp->crp_p = (caddr_t) b;
1032 crp->crp_nbits = bits;
1034 for (i = 0, j = 0; i < a->top; i++) {
1035 for (k = 0; k < BN_BITS2 / 8; k++) {
1036 if ((j + k) >= bytes)
1038 b[j + k] = a->d[i] >> (k * 8);
1045 /* Convert a /dev/crypto parameter to a BIGNUM */
1046 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1051 bytes = (crp->crp_nbits + 7) / 8;
1056 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1059 for (i = 0; i < bytes; i++)
1060 pd[i] = crp->crp_p[bytes - i - 1];
1062 BN_bin2bn(pd, bytes, a);
1068 static void zapparams(struct crypt_kop *kop)
1072 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1073 OPENSSL_free(kop->crk_param[i].crp_p);
1074 kop->crk_param[i].crp_p = NULL;
1075 kop->crk_param[i].crp_nbits = 0;
1080 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1085 if ((fd = get_asym_dev_crypto()) < 0)
1089 kop->crk_param[kop->crk_iparams].crp_p = OPENSSL_malloc(rlen);
1090 if (kop->crk_param[kop->crk_iparams].crp_p == NULL)
1092 memset(kop->crk_param[kop->crk_iparams].crp_p, 0, (size_t)rlen);
1093 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1097 kop->crk_param[kop->crk_iparams + 1].crp_p = OPENSSL_malloc(slen);
1098 /* No need to free the kop->crk_iparams parameter if it was allocated,
1099 * callers of this routine have to free allocated parameters through
1100 * zapparams both in case of success and failure
1102 if (kop->crk_param[kop->crk_iparams+1].crp_p == NULL)
1104 memset(kop->crk_param[kop->crk_iparams + 1].crp_p, 0, (size_t)slen);
1105 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1109 if (ioctl(fd, CIOCKEY, kop) == 0) {
1111 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1113 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1121 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1122 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1124 struct crypt_kop kop;
1128 * Currently, we know we can do mod exp iff we can do any asymmetric
1129 * operations at all.
1131 if (cryptodev_asymfeat == 0) {
1132 ret = BN_mod_exp(r, a, p, m, ctx);
1136 memset(&kop, 0, sizeof kop);
1137 kop.crk_op = CRK_MOD_EXP;
1139 /* inputs: a^p % m */
1140 if (bn2crparam(a, &kop.crk_param[0]))
1142 if (bn2crparam(p, &kop.crk_param[1]))
1144 if (bn2crparam(m, &kop.crk_param[2]))
1146 kop.crk_iparams = 3;
1148 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1149 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1150 printf("OCF asym process failed, Running in software\n");
1151 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1153 } else if (ECANCELED == kop.crk_status) {
1154 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1155 printf("OCF hardware operation cancelled. Running in Software\n");
1156 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1158 /* else cryptodev operation worked ok ==> ret = 1 */
1166 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1171 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1177 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1179 struct crypt_kop kop;
1182 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1183 /* XXX 0 means failure?? */
1187 memset(&kop, 0, sizeof kop);
1188 kop.crk_op = CRK_MOD_EXP_CRT;
1189 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1190 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1192 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1194 if (bn2crparam(I, &kop.crk_param[2]))
1196 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1198 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1200 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1202 kop.crk_iparams = 6;
1204 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1205 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1206 printf("OCF asym process failed, running in Software\n");
1207 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1209 } else if (ECANCELED == kop.crk_status) {
1210 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1211 printf("OCF hardware operation cancelled. Running in Software\n");
1212 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1214 /* else cryptodev operation worked ok ==> ret = 1 */
1221 static RSA_METHOD cryptodev_rsa = {
1222 "cryptodev RSA method",
1223 NULL, /* rsa_pub_enc */
1224 NULL, /* rsa_pub_dec */
1225 NULL, /* rsa_priv_enc */
1226 NULL, /* rsa_priv_dec */
1232 NULL, /* app_data */
1233 NULL, /* rsa_sign */
1234 NULL /* rsa_verify */
1238 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1239 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1241 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1245 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1246 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1247 BN_CTX *ctx, BN_MONT_CTX *mont)
1254 /* v = ( g^u1 * y^u2 mod p ) mod q */
1255 /* let t1 = g ^ u1 mod p */
1258 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
1261 /* let t2 = y ^ u2 mod p */
1262 if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
1264 /* let u1 = t1 * t2 mod p */
1265 if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
1276 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1279 struct crypt_kop kop;
1280 BIGNUM *r = NULL, *s = NULL;
1281 DSA_SIG *dsaret = NULL;
1283 if ((r = BN_new()) == NULL)
1285 if ((s = BN_new()) == NULL) {
1290 memset(&kop, 0, sizeof kop);
1291 kop.crk_op = CRK_DSA_SIGN;
1293 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1294 kop.crk_param[0].crp_p = (caddr_t) dgst;
1295 kop.crk_param[0].crp_nbits = dlen * 8;
1296 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1298 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1300 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1302 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1304 kop.crk_iparams = 5;
1306 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1307 BN_num_bytes(dsa->q), s) == 0) {
1308 dsaret = DSA_SIG_new();
1315 const DSA_METHOD *meth = DSA_OpenSSL();
1316 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
1321 kop.crk_param[0].crp_p = NULL;
1327 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1328 DSA_SIG *sig, DSA *dsa)
1330 struct crypt_kop kop;
1333 memset(&kop, 0, sizeof kop);
1334 kop.crk_op = CRK_DSA_VERIFY;
1336 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1337 kop.crk_param[0].crp_p = (caddr_t) dgst;
1338 kop.crk_param[0].crp_nbits = dlen * 8;
1339 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1341 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1343 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1345 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1347 if (bn2crparam(sig->r, &kop.crk_param[5]))
1349 if (bn2crparam(sig->s, &kop.crk_param[6]))
1351 kop.crk_iparams = 7;
1353 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1355 * OCF success value is 0, if not zero, change dsaret to fail
1357 if (0 != kop.crk_status)
1360 const DSA_METHOD *meth = DSA_OpenSSL();
1362 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
1365 kop.crk_param[0].crp_p = NULL;
1370 static DSA_METHOD cryptodev_dsa = {
1371 "cryptodev DSA method",
1373 NULL, /* dsa_sign_setup */
1375 NULL, /* dsa_mod_exp */
1384 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1385 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1388 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1392 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1394 struct crypt_kop kop;
1398 if ((fd = get_asym_dev_crypto()) < 0) {
1399 const DH_METHOD *meth = DH_OpenSSL();
1401 return ((meth->compute_key) (key, pub_key, dh));
1404 keylen = BN_num_bits(dh->p);
1406 memset(&kop, 0, sizeof kop);
1407 kop.crk_op = CRK_DH_COMPUTE_KEY;
1409 /* inputs: dh->priv_key pub_key dh->p key */
1410 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1412 if (bn2crparam(pub_key, &kop.crk_param[1]))
1414 if (bn2crparam(dh->p, &kop.crk_param[2]))
1416 kop.crk_iparams = 3;
1418 kop.crk_param[3].crp_p = (caddr_t) key;
1419 kop.crk_param[3].crp_nbits = keylen * 8;
1420 kop.crk_oparams = 1;
1422 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1423 const DH_METHOD *meth = DH_OpenSSL();
1425 dhret = (meth->compute_key) (key, pub_key, dh);
1428 kop.crk_param[3].crp_p = NULL;
1433 static DH_METHOD cryptodev_dh = {
1434 "cryptodev DH method",
1435 NULL, /* cryptodev_dh_generate_key */
1445 * ctrl right now is just a wrapper that doesn't do much
1446 * but I expect we'll want some options soon.
1449 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1451 # ifdef HAVE_SYSLOG_R
1452 struct syslog_data sd = SYSLOG_DATA_INIT;
1457 # ifdef HAVE_SYSLOG_R
1458 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1460 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1467 void ENGINE_load_cryptodev(void)
1469 ENGINE *engine = ENGINE_new();
1474 if ((fd = get_dev_crypto()) < 0) {
1475 ENGINE_free(engine);
1480 * find out what asymmetric crypto algorithms we support
1482 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1484 ENGINE_free(engine);
1489 if (!ENGINE_set_id(engine, "cryptodev") ||
1490 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1491 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1492 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1493 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1494 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1495 ENGINE_free(engine);
1499 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1500 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1502 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1503 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1504 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1505 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1506 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1507 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1508 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1509 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1510 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1511 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1513 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1517 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1518 const DSA_METHOD *meth = DSA_OpenSSL();
1520 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1521 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1522 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1523 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1524 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1525 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1527 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1528 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1531 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1532 const DH_METHOD *dh_meth = DH_OpenSSL();
1534 cryptodev_dh.generate_key = dh_meth->generate_key;
1535 cryptodev_dh.compute_key = dh_meth->compute_key;
1536 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1537 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1538 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1539 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1540 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1545 ENGINE_free(engine);
1549 #endif /* HAVE_CRYPTODEV */