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
47 void ENGINE_load_cryptodev(void)
49 /* This is a NOP on platforms without /dev/crypto */
55 # include <sys/types.h>
56 # include <crypto/cryptodev.h>
57 # include <openssl/dh.h>
58 # include <openssl/dsa.h>
59 # include <openssl/err.h>
60 # include <openssl/rsa.h>
61 # include <sys/ioctl.h>
71 struct dev_crypto_state {
72 struct session_op d_sess;
74 # ifdef USE_CRYPTODEV_DIGESTS
75 char dummy_mac_key[HASH_MAX_LEN];
76 unsigned char digest_res[HASH_MAX_LEN];
82 static u_int32_t cryptodev_asymfeat = 0;
84 static int get_asym_dev_crypto(void);
85 static int open_dev_crypto(void);
86 static int get_dev_crypto(void);
87 static int get_cryptodev_ciphers(const int **cnids);
88 # ifdef USE_CRYPTODEV_DIGESTS
89 static int get_cryptodev_digests(const int **cnids);
91 static int cryptodev_usable_ciphers(const int **nids);
92 static int cryptodev_usable_digests(const int **nids);
93 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
94 const unsigned char *in, size_t inl);
95 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
96 const unsigned char *iv, int enc);
97 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
98 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
99 const int **nids, int nid);
100 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
101 const int **nids, int nid);
102 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
103 static int crparam2bn(struct crparam *crp, BIGNUM *a);
104 static void zapparams(struct crypt_kop *kop);
105 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
106 int slen, BIGNUM *s);
108 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
109 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
111 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
113 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
115 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
116 const BIGNUM *p, const BIGNUM *m,
117 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,
120 BIGNUM *p, BN_CTX *ctx,
122 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
124 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
125 DSA_SIG *sig, DSA *dsa);
126 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
127 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
129 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
131 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
133 void ENGINE_load_cryptodev(void);
135 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
146 CRYPTO_ARC4, NID_rc4, 0, 16,
149 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
152 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
155 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
158 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
161 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
163 # ifdef CRYPTO_AES_CTR
165 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
168 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
171 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
175 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
178 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
181 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
188 # ifdef USE_CRYPTODEV_DIGESTS
195 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
198 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
201 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
205 CRYPTO_MD5_KPDK, NID_undef, 0
208 CRYPTO_SHA1_KPDK, NID_undef, 0
211 CRYPTO_MD5, NID_md5, 16
214 CRYPTO_SHA1, NID_sha1, 20
223 * Return a fd if /dev/crypto seems usable, 0 otherwise.
225 static int open_dev_crypto(void)
230 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
233 if (fcntl(fd, F_SETFD, 1) == -1) {
242 static int get_dev_crypto(void)
246 if ((fd = open_dev_crypto()) == -1)
248 # ifndef CRIOGET_NOT_NEEDED
249 if (ioctl(fd, CRIOGET, &retfd) == -1)
253 if (fcntl(retfd, F_SETFD, 1) == -1) {
263 static void put_dev_crypto(int fd)
265 # ifndef CRIOGET_NOT_NEEDED
270 /* Caching version for asym operations */
271 static int get_asym_dev_crypto(void)
276 fd = get_dev_crypto();
281 * Find out what ciphers /dev/crypto will let us have a session for.
282 * XXX note, that some of these openssl doesn't deal with yet!
283 * returning them here is harmless, as long as we return NULL
284 * when asked for a handler in the cryptodev_engine_ciphers routine
286 static int get_cryptodev_ciphers(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.key = (caddr_t) "123456789abcdefghijklmno";
299 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
300 if (ciphers[i].nid == NID_undef)
302 sess.cipher = ciphers[i].id;
303 sess.keylen = ciphers[i].keylen;
305 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
306 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
307 nids[count++] = ciphers[i].nid;
318 # ifdef USE_CRYPTODEV_DIGESTS
320 * Find out what digests /dev/crypto will let us have a session for.
321 * XXX note, that some of these openssl doesn't deal with yet!
322 * returning them here is harmless, as long as we return NULL
323 * when asked for a handler in the cryptodev_engine_digests routine
325 static int get_cryptodev_digests(const int **cnids)
327 static int nids[CRYPTO_ALGORITHM_MAX];
328 struct session_op sess;
329 int fd, i, count = 0;
331 if ((fd = get_dev_crypto()) < 0) {
335 memset(&sess, 0, sizeof(sess));
336 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
337 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
338 if (digests[i].nid == NID_undef)
340 sess.mac = digests[i].id;
341 sess.mackeylen = digests[i].keylen;
343 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
344 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
345 nids[count++] = digests[i].nid;
358 * Find the useable ciphers|digests from dev/crypto - this is the first
359 * thing called by the engine init crud which determines what it
360 * can use for ciphers from this engine. We want to return
361 * only what we can do, anythine else is handled by software.
363 * If we can't initialize the device to do anything useful for
364 * any reason, we want to return a NULL array, and 0 length,
365 * which forces everything to be done is software. By putting
366 * the initalization of the device in here, we ensure we can
367 * use this engine as the default, and if for whatever reason
368 * /dev/crypto won't do what we want it will just be done in
371 * This can (should) be greatly expanded to perhaps take into
372 * account speed of the device, and what we want to do.
373 * (although the disabling of particular alg's could be controlled
374 * by the device driver with sysctl's.) - this is where we
375 * want most of the decisions made about what we actually want
376 * to use from /dev/crypto.
378 static int cryptodev_usable_ciphers(const int **nids)
380 return (get_cryptodev_ciphers(nids));
383 static int cryptodev_usable_digests(const int **nids)
385 # ifdef USE_CRYPTODEV_DIGESTS
386 return (get_cryptodev_digests(nids));
389 * XXXX just disable all digests for now, because it sucks.
390 * we need a better way to decide this - i.e. I may not
391 * want digests on slow cards like hifn on fast machines,
392 * but might want them on slow or loaded machines, etc.
393 * will also want them when using crypto cards that don't
394 * suck moose gonads - would be nice to be able to decide something
395 * as reasonable default without having hackery that's card dependent.
396 * of course, the default should probably be just do everything,
397 * with perhaps a sysctl to turn algoritms off (or have them off
398 * by default) on cards that generally suck like the hifn.
406 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
407 const unsigned char *in, size_t inl)
409 struct crypt_op cryp;
410 struct dev_crypto_state *state = ctx->cipher_data;
411 struct session_op *sess = &state->d_sess;
413 unsigned char save_iv[EVP_MAX_IV_LENGTH];
419 if ((inl % ctx->cipher->block_size) != 0)
422 memset(&cryp, 0, sizeof(cryp));
424 cryp.ses = sess->ses;
427 cryp.src = (caddr_t) in;
428 cryp.dst = (caddr_t) out;
431 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
433 if (ctx->cipher->iv_len) {
434 cryp.iv = (caddr_t) ctx->iv;
436 iiv = in + inl - ctx->cipher->iv_len;
437 memcpy(save_iv, iiv, ctx->cipher->iv_len);
442 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
444 * XXX need better errror handling this can fail for a number of
450 if (ctx->cipher->iv_len) {
452 iiv = out + inl - ctx->cipher->iv_len;
455 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
461 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
462 const unsigned char *iv, int enc)
464 struct dev_crypto_state *state = ctx->cipher_data;
465 struct session_op *sess = &state->d_sess;
468 for (i = 0; ciphers[i].id; i++)
469 if (ctx->cipher->nid == ciphers[i].nid &&
470 ctx->cipher->iv_len <= ciphers[i].ivmax &&
471 ctx->key_len == ciphers[i].keylen) {
472 cipher = ciphers[i].id;
476 if (!ciphers[i].id) {
481 memset(sess, 0, sizeof(struct session_op));
483 if ((state->d_fd = get_dev_crypto()) < 0)
486 sess->key = (caddr_t) key;
487 sess->keylen = ctx->key_len;
488 sess->cipher = cipher;
490 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
491 put_dev_crypto(state->d_fd);
499 * free anything we allocated earlier when initting a
500 * session, and close the session.
502 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
505 struct dev_crypto_state *state = ctx->cipher_data;
506 struct session_op *sess = &state->d_sess;
512 * XXX if this ioctl fails, someting's wrong. the invoker may have called
513 * us with a bogus ctx, or we could have a device that for whatever
514 * reason just doesn't want to play ball - it's not clear what's right
515 * here - should this be an error? should it just increase a counter,
516 * hmm. For right now, we return 0 - I don't believe that to be "right".
517 * we could call the gorpy openssl lib error handlers that print messages
518 * to users of the library. hmm..
521 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
526 put_dev_crypto(state->d_fd);
533 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
534 * gets called when libcrypto requests a cipher NID.
538 const EVP_CIPHER cryptodev_rc4 = {
541 EVP_CIPH_VARIABLE_LENGTH,
545 sizeof(struct dev_crypto_state),
552 const EVP_CIPHER cryptodev_des_cbc = {
559 sizeof(struct dev_crypto_state),
560 EVP_CIPHER_set_asn1_iv,
561 EVP_CIPHER_get_asn1_iv,
566 const EVP_CIPHER cryptodev_3des_cbc = {
573 sizeof(struct dev_crypto_state),
574 EVP_CIPHER_set_asn1_iv,
575 EVP_CIPHER_get_asn1_iv,
579 const EVP_CIPHER cryptodev_bf_cbc = {
586 sizeof(struct dev_crypto_state),
587 EVP_CIPHER_set_asn1_iv,
588 EVP_CIPHER_get_asn1_iv,
592 const EVP_CIPHER cryptodev_cast_cbc = {
599 sizeof(struct dev_crypto_state),
600 EVP_CIPHER_set_asn1_iv,
601 EVP_CIPHER_get_asn1_iv,
605 const EVP_CIPHER cryptodev_aes_cbc = {
612 sizeof(struct dev_crypto_state),
613 EVP_CIPHER_set_asn1_iv,
614 EVP_CIPHER_get_asn1_iv,
618 const EVP_CIPHER cryptodev_aes_192_cbc = {
625 sizeof(struct dev_crypto_state),
626 EVP_CIPHER_set_asn1_iv,
627 EVP_CIPHER_get_asn1_iv,
631 const EVP_CIPHER cryptodev_aes_256_cbc = {
638 sizeof(struct dev_crypto_state),
639 EVP_CIPHER_set_asn1_iv,
640 EVP_CIPHER_get_asn1_iv,
644 # ifdef CRYPTO_AES_CTR
645 const EVP_CIPHER cryptodev_aes_ctr = {
652 sizeof(struct dev_crypto_state),
653 EVP_CIPHER_set_asn1_iv,
654 EVP_CIPHER_get_asn1_iv,
658 const EVP_CIPHER cryptodev_aes_ctr_192 = {
665 sizeof(struct dev_crypto_state),
666 EVP_CIPHER_set_asn1_iv,
667 EVP_CIPHER_get_asn1_iv,
671 const EVP_CIPHER cryptodev_aes_ctr_256 = {
678 sizeof(struct dev_crypto_state),
679 EVP_CIPHER_set_asn1_iv,
680 EVP_CIPHER_get_asn1_iv,
685 * Registered by the ENGINE when used to find out how to deal with
686 * a particular NID in the ENGINE. this says what we'll do at the
687 * top level - note, that list is restricted by what we answer with
690 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
691 const int **nids, int nid)
694 return (cryptodev_usable_ciphers(nids));
698 *cipher = &cryptodev_rc4;
700 case NID_des_ede3_cbc:
701 *cipher = &cryptodev_3des_cbc;
704 *cipher = &cryptodev_des_cbc;
707 *cipher = &cryptodev_bf_cbc;
710 *cipher = &cryptodev_cast_cbc;
712 case NID_aes_128_cbc:
713 *cipher = &cryptodev_aes_cbc;
715 case NID_aes_192_cbc:
716 *cipher = &cryptodev_aes_192_cbc;
718 case NID_aes_256_cbc:
719 *cipher = &cryptodev_aes_256_cbc;
721 # ifdef CRYPTO_AES_CTR
722 case NID_aes_128_ctr:
723 *cipher = &cryptodev_aes_ctr;
725 case NID_aes_192_ctr:
726 *cipher = &cryptodev_aes_ctr_192;
728 case NID_aes_256_ctr:
729 *cipher = &cryptodev_aes_ctr_256;
736 return (*cipher != NULL);
739 # ifdef USE_CRYPTODEV_DIGESTS
741 /* convert digest type to cryptodev */
742 static int digest_nid_to_cryptodev(int nid)
746 for (i = 0; digests[i].id; i++)
747 if (digests[i].nid == nid)
748 return (digests[i].id);
752 static int digest_key_length(int nid)
756 for (i = 0; digests[i].id; i++)
757 if (digests[i].nid == nid)
758 return digests[i].keylen;
762 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
764 struct dev_crypto_state *state = ctx->md_data;
765 struct session_op *sess = &state->d_sess;
768 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) {
769 printf("cryptodev_digest_init: Can't get digest \n");
773 memset(state, 0, sizeof(struct dev_crypto_state));
775 if ((state->d_fd = get_dev_crypto()) < 0) {
776 printf("cryptodev_digest_init: Can't get Dev \n");
780 sess->mackey = state->dummy_mac_key;
781 sess->mackeylen = digest_key_length(ctx->digest->type);
784 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
785 put_dev_crypto(state->d_fd);
787 printf("cryptodev_digest_init: Open session failed\n");
794 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
797 struct crypt_op cryp;
798 struct dev_crypto_state *state = ctx->md_data;
799 struct session_op *sess = &state->d_sess;
801 if (!data || state->d_fd < 0) {
802 printf("cryptodev_digest_update: illegal inputs \n");
810 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
811 /* if application doesn't support one buffer */
813 OPENSSL_realloc(state->mac_data, state->mac_len + count);
815 if (!state->mac_data) {
816 printf("cryptodev_digest_update: realloc failed\n");
820 memcpy(state->mac_data + state->mac_len, data, count);
821 state->mac_len += count;
826 memset(&cryp, 0, sizeof(cryp));
828 cryp.ses = sess->ses;
831 cryp.src = (caddr_t) data;
833 cryp.mac = (caddr_t) state->digest_res;
834 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
835 printf("cryptodev_digest_update: digest failed\n");
841 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
843 struct crypt_op cryp;
844 struct dev_crypto_state *state = ctx->md_data;
845 struct session_op *sess = &state->d_sess;
849 if (!md || state->d_fd < 0) {
850 printf("cryptodev_digest_final: illegal input\n");
854 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
855 /* if application doesn't support one buffer */
856 memset(&cryp, 0, sizeof(cryp));
857 cryp.ses = sess->ses;
859 cryp.len = state->mac_len;
860 cryp.src = state->mac_data;
862 cryp.mac = (caddr_t) md;
863 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
864 printf("cryptodev_digest_final: digest failed\n");
871 memcpy(md, state->digest_res, ctx->digest->md_size);
876 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
879 struct dev_crypto_state *state = ctx->md_data;
880 struct session_op *sess = &state->d_sess;
885 if (state->d_fd < 0) {
886 printf("cryptodev_digest_cleanup: illegal input\n");
890 if (state->mac_data) {
891 OPENSSL_free(state->mac_data);
892 state->mac_data = NULL;
896 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
897 printf("cryptodev_digest_cleanup: failed to close session\n");
902 put_dev_crypto(state->d_fd);
908 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
910 struct dev_crypto_state *fstate = from->md_data;
911 struct dev_crypto_state *dstate = to->md_data;
912 struct session_op *sess;
915 if (dstate == NULL || fstate == NULL)
918 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
920 sess = &dstate->d_sess;
922 digest = digest_nid_to_cryptodev(to->digest->type);
924 sess->mackey = dstate->dummy_mac_key;
925 sess->mackeylen = digest_key_length(to->digest->type);
928 dstate->d_fd = get_dev_crypto();
930 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
931 put_dev_crypto(dstate->d_fd);
933 printf("cryptodev_digest_init: Open session failed\n");
937 if (fstate->mac_len != 0) {
938 if (fstate->mac_data != NULL) {
939 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
940 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
941 dstate->mac_len = fstate->mac_len;
948 const EVP_MD cryptodev_sha1 = {
953 cryptodev_digest_init,
954 cryptodev_digest_update,
955 cryptodev_digest_final,
956 cryptodev_digest_copy,
957 cryptodev_digest_cleanup,
958 EVP_PKEY_NULL_method,
960 sizeof(struct dev_crypto_state),
963 const EVP_MD cryptodev_md5 = {
966 16 /* MD5_DIGEST_LENGTH */ ,
968 cryptodev_digest_init,
969 cryptodev_digest_update,
970 cryptodev_digest_final,
971 cryptodev_digest_copy,
972 cryptodev_digest_cleanup,
973 EVP_PKEY_NULL_method,
974 64 /* MD5_CBLOCK */ ,
975 sizeof(struct dev_crypto_state),
978 # endif /* USE_CRYPTODEV_DIGESTS */
981 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
982 const int **nids, int nid)
985 return (cryptodev_usable_digests(nids));
988 # ifdef USE_CRYPTODEV_DIGESTS
990 *digest = &cryptodev_md5;
993 *digest = &cryptodev_sha1;
996 # endif /* USE_CRYPTODEV_DIGESTS */
1000 return (*digest != NULL);
1004 * Convert a BIGNUM to the representation that /dev/crypto needs.
1005 * Upon completion of use, the caller is responsible for freeing
1008 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1011 ssize_t bytes, bits;
1017 bits = BN_num_bits(a);
1018 bytes = (bits + 7) / 8;
1023 memset(b, 0, bytes);
1025 crp->crp_p = (caddr_t) b;
1026 crp->crp_nbits = bits;
1028 for (i = 0, j = 0; i < a->top; i++) {
1029 for (k = 0; k < BN_BITS2 / 8; k++) {
1030 if ((j + k) >= bytes)
1032 b[j + k] = a->d[i] >> (k * 8);
1039 /* Convert a /dev/crypto parameter to a BIGNUM */
1040 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1045 bytes = (crp->crp_nbits + 7) / 8;
1050 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1053 for (i = 0; i < bytes; i++)
1054 pd[i] = crp->crp_p[bytes - i - 1];
1056 BN_bin2bn(pd, bytes, a);
1062 static void zapparams(struct crypt_kop *kop)
1066 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1067 if (kop->crk_param[i].crp_p)
1068 free(kop->crk_param[i].crp_p);
1069 kop->crk_param[i].crp_p = NULL;
1070 kop->crk_param[i].crp_nbits = 0;
1075 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1080 if ((fd = get_asym_dev_crypto()) < 0)
1084 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1085 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1089 kop->crk_param[kop->crk_iparams + 1].crp_p =
1090 calloc(slen, sizeof(char));
1091 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1095 if (ioctl(fd, CIOCKEY, kop) == 0) {
1097 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1099 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1107 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1108 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1110 struct crypt_kop kop;
1114 * Currently, we know we can do mod exp iff we can do any asymmetric
1115 * operations at all.
1117 if (cryptodev_asymfeat == 0) {
1118 ret = BN_mod_exp(r, a, p, m, ctx);
1122 memset(&kop, 0, sizeof kop);
1123 kop.crk_op = CRK_MOD_EXP;
1125 /* inputs: a^p % m */
1126 if (bn2crparam(a, &kop.crk_param[0]))
1128 if (bn2crparam(p, &kop.crk_param[1]))
1130 if (bn2crparam(m, &kop.crk_param[2]))
1132 kop.crk_iparams = 3;
1134 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1135 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1136 printf("OCF asym process failed, Running in software\n");
1137 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1139 } else if (ECANCELED == kop.crk_status) {
1140 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1141 printf("OCF hardware operation cancelled. Running in Software\n");
1142 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1144 /* else cryptodev operation worked ok ==> ret = 1 */
1152 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1157 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1163 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1165 struct crypt_kop kop;
1168 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1169 /* XXX 0 means failure?? */
1173 memset(&kop, 0, sizeof kop);
1174 kop.crk_op = CRK_MOD_EXP_CRT;
1175 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1176 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1178 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1180 if (bn2crparam(I, &kop.crk_param[2]))
1182 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1184 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1186 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1188 kop.crk_iparams = 6;
1190 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1191 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1192 printf("OCF asym process failed, running in Software\n");
1193 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1195 } else if (ECANCELED == kop.crk_status) {
1196 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1197 printf("OCF hardware operation cancelled. Running in Software\n");
1198 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1200 /* else cryptodev operation worked ok ==> ret = 1 */
1207 static RSA_METHOD cryptodev_rsa = {
1208 "cryptodev RSA method",
1209 NULL, /* rsa_pub_enc */
1210 NULL, /* rsa_pub_dec */
1211 NULL, /* rsa_priv_enc */
1212 NULL, /* rsa_priv_dec */
1218 NULL, /* app_data */
1219 NULL, /* rsa_sign */
1220 NULL /* rsa_verify */
1224 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1225 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1227 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1231 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1232 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1233 BN_CTX *ctx, BN_MONT_CTX *mont)
1240 /* v = ( g^u1 * y^u2 mod p ) mod q */
1241 /* let t1 = g ^ u1 mod p */
1244 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
1247 /* let t2 = y ^ u2 mod p */
1248 if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
1250 /* let u1 = t1 * t2 mod p */
1251 if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
1262 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1265 struct crypt_kop kop;
1266 BIGNUM *r = NULL, *s = NULL;
1267 DSA_SIG *dsaret = NULL;
1269 if ((r = BN_new()) == NULL)
1271 if ((s = BN_new()) == NULL) {
1276 memset(&kop, 0, sizeof kop);
1277 kop.crk_op = CRK_DSA_SIGN;
1279 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1280 kop.crk_param[0].crp_p = (caddr_t) dgst;
1281 kop.crk_param[0].crp_nbits = dlen * 8;
1282 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1284 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1286 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1288 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1290 kop.crk_iparams = 5;
1292 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1293 BN_num_bytes(dsa->q), s) == 0) {
1294 dsaret = DSA_SIG_new();
1298 const DSA_METHOD *meth = DSA_OpenSSL();
1301 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
1304 kop.crk_param[0].crp_p = NULL;
1310 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1311 DSA_SIG *sig, DSA *dsa)
1313 struct crypt_kop kop;
1316 memset(&kop, 0, sizeof kop);
1317 kop.crk_op = CRK_DSA_VERIFY;
1319 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1320 kop.crk_param[0].crp_p = (caddr_t) dgst;
1321 kop.crk_param[0].crp_nbits = dlen * 8;
1322 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1324 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1326 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1328 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1330 if (bn2crparam(sig->r, &kop.crk_param[5]))
1332 if (bn2crparam(sig->s, &kop.crk_param[6]))
1334 kop.crk_iparams = 7;
1336 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1338 * OCF success value is 0, if not zero, change dsaret to fail
1340 if (0 != kop.crk_status)
1343 const DSA_METHOD *meth = DSA_OpenSSL();
1345 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
1348 kop.crk_param[0].crp_p = NULL;
1353 static DSA_METHOD cryptodev_dsa = {
1354 "cryptodev DSA method",
1356 NULL, /* dsa_sign_setup */
1358 NULL, /* dsa_mod_exp */
1367 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1368 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1371 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1375 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1377 struct crypt_kop kop;
1381 if ((fd = get_asym_dev_crypto()) < 0) {
1382 const DH_METHOD *meth = DH_OpenSSL();
1384 return ((meth->compute_key) (key, pub_key, dh));
1387 keylen = BN_num_bits(dh->p);
1389 memset(&kop, 0, sizeof kop);
1390 kop.crk_op = CRK_DH_COMPUTE_KEY;
1392 /* inputs: dh->priv_key pub_key dh->p key */
1393 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1395 if (bn2crparam(pub_key, &kop.crk_param[1]))
1397 if (bn2crparam(dh->p, &kop.crk_param[2]))
1399 kop.crk_iparams = 3;
1401 kop.crk_param[3].crp_p = (caddr_t) key;
1402 kop.crk_param[3].crp_nbits = keylen * 8;
1403 kop.crk_oparams = 1;
1405 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1406 const DH_METHOD *meth = DH_OpenSSL();
1408 dhret = (meth->compute_key) (key, pub_key, dh);
1411 kop.crk_param[3].crp_p = NULL;
1416 static DH_METHOD cryptodev_dh = {
1417 "cryptodev DH method",
1418 NULL, /* cryptodev_dh_generate_key */
1428 * ctrl right now is just a wrapper that doesn't do much
1429 * but I expect we'll want some options soon.
1432 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1434 # ifdef HAVE_SYSLOG_R
1435 struct syslog_data sd = SYSLOG_DATA_INIT;
1440 # ifdef HAVE_SYSLOG_R
1441 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1443 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1450 void ENGINE_load_cryptodev(void)
1452 ENGINE *engine = ENGINE_new();
1457 if ((fd = get_dev_crypto()) < 0) {
1458 ENGINE_free(engine);
1463 * find out what asymmetric crypto algorithms we support
1465 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1467 ENGINE_free(engine);
1472 if (!ENGINE_set_id(engine, "cryptodev") ||
1473 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1474 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1475 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1476 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1477 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1478 ENGINE_free(engine);
1482 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1483 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1485 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1486 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1487 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1488 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1489 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1490 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1491 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1492 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1493 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1494 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1496 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1500 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1501 const DSA_METHOD *meth = DSA_OpenSSL();
1503 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1504 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1505 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1506 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1507 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1508 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1510 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1511 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1514 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1515 const DH_METHOD *dh_meth = DH_OpenSSL();
1517 cryptodev_dh.generate_key = dh_meth->generate_key;
1518 cryptodev_dh.compute_key = dh_meth->compute_key;
1519 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1520 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1521 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1522 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1523 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1528 ENGINE_free(engine);
1532 #endif /* HAVE_CRYPTODEV */