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 { CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16, },
151 { CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24, },
152 { CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32, },
153 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
154 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
155 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
156 { 0, NID_undef, 0, 0, },
159 #ifdef USE_CRYPTODEV_DIGESTS
165 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
166 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
167 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
168 { CRYPTO_MD5_KPDK, NID_undef, 0},
169 { CRYPTO_SHA1_KPDK, NID_undef, 0},
170 { CRYPTO_MD5, NID_md5, 16},
171 { CRYPTO_SHA1, NID_sha1, 20},
177 * Return a fd if /dev/crypto seems usable, 0 otherwise.
180 open_dev_crypto(void)
185 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
188 if (fcntl(fd, F_SETFD, 1) == -1) {
202 if ((fd = open_dev_crypto()) == -1)
204 #ifndef CRIOGET_NOT_NEEDED
205 if (ioctl(fd, CRIOGET, &retfd) == -1)
209 if (fcntl(retfd, F_SETFD, 1) == -1) {
219 static void put_dev_crypto(int fd)
221 #ifndef CRIOGET_NOT_NEEDED
226 /* Caching version for asym operations */
228 get_asym_dev_crypto(void)
233 fd = get_dev_crypto();
238 * Find out what ciphers /dev/crypto will let us have a session for.
239 * XXX note, that some of these openssl doesn't deal with yet!
240 * returning them here is harmless, as long as we return NULL
241 * when asked for a handler in the cryptodev_engine_ciphers routine
244 get_cryptodev_ciphers(const int **cnids)
246 static int nids[CRYPTO_ALGORITHM_MAX];
247 struct session_op sess;
248 int fd, i, count = 0;
250 if ((fd = get_dev_crypto()) < 0) {
254 memset(&sess, 0, sizeof(sess));
255 sess.key = (caddr_t)"123456789abcdefghijklmno";
257 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
258 if (ciphers[i].nid == NID_undef)
260 sess.cipher = ciphers[i].id;
261 sess.keylen = ciphers[i].keylen;
263 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
264 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
265 nids[count++] = ciphers[i].nid;
276 #ifdef USE_CRYPTODEV_DIGESTS
278 * Find out what digests /dev/crypto will let us have a session for.
279 * XXX note, that some of these openssl doesn't deal with yet!
280 * returning them here is harmless, as long as we return NULL
281 * when asked for a handler in the cryptodev_engine_digests routine
284 get_cryptodev_digests(const int **cnids)
286 static int nids[CRYPTO_ALGORITHM_MAX];
287 struct session_op sess;
288 int fd, i, count = 0;
290 if ((fd = get_dev_crypto()) < 0) {
294 memset(&sess, 0, sizeof(sess));
295 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
296 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
297 if (digests[i].nid == NID_undef)
299 sess.mac = digests[i].id;
300 sess.mackeylen = digests[i].keylen;
302 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
303 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
304 nids[count++] = digests[i].nid;
317 * Find the useable ciphers|digests from dev/crypto - this is the first
318 * thing called by the engine init crud which determines what it
319 * can use for ciphers from this engine. We want to return
320 * only what we can do, anythine else is handled by software.
322 * If we can't initialize the device to do anything useful for
323 * any reason, we want to return a NULL array, and 0 length,
324 * which forces everything to be done is software. By putting
325 * the initalization of the device in here, we ensure we can
326 * use this engine as the default, and if for whatever reason
327 * /dev/crypto won't do what we want it will just be done in
330 * This can (should) be greatly expanded to perhaps take into
331 * account speed of the device, and what we want to do.
332 * (although the disabling of particular alg's could be controlled
333 * by the device driver with sysctl's.) - this is where we
334 * want most of the decisions made about what we actually want
335 * to use from /dev/crypto.
338 cryptodev_usable_ciphers(const int **nids)
340 return (get_cryptodev_ciphers(nids));
344 cryptodev_usable_digests(const int **nids)
346 #ifdef USE_CRYPTODEV_DIGESTS
347 return (get_cryptodev_digests(nids));
350 * XXXX just disable all digests for now, because it sucks.
351 * we need a better way to decide this - i.e. I may not
352 * want digests on slow cards like hifn on fast machines,
353 * but might want them on slow or loaded machines, etc.
354 * will also want them when using crypto cards that don't
355 * suck moose gonads - would be nice to be able to decide something
356 * as reasonable default without having hackery that's card dependent.
357 * of course, the default should probably be just do everything,
358 * with perhaps a sysctl to turn algoritms off (or have them off
359 * by default) on cards that generally suck like the hifn.
367 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
368 const unsigned char *in, size_t inl)
370 struct crypt_op cryp;
371 struct dev_crypto_state *state = ctx->cipher_data;
372 struct session_op *sess = &state->d_sess;
374 unsigned char save_iv[EVP_MAX_IV_LENGTH];
380 if ((inl % ctx->cipher->block_size) != 0)
383 memset(&cryp, 0, sizeof(cryp));
385 cryp.ses = sess->ses;
388 cryp.src = (caddr_t) in;
389 cryp.dst = (caddr_t) out;
392 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
394 if (ctx->cipher->iv_len) {
395 cryp.iv = (caddr_t) ctx->iv;
397 iiv = in + inl - ctx->cipher->iv_len;
398 memcpy(save_iv, iiv, ctx->cipher->iv_len);
403 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
404 /* XXX need better errror handling
405 * this can fail for a number of different reasons.
410 if (ctx->cipher->iv_len) {
412 iiv = out + inl - ctx->cipher->iv_len;
415 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
421 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
422 const unsigned char *iv, int enc)
424 struct dev_crypto_state *state = ctx->cipher_data;
425 struct session_op *sess = &state->d_sess;
428 for (i = 0; ciphers[i].id; i++)
429 if (ctx->cipher->nid == ciphers[i].nid &&
430 ctx->cipher->iv_len <= ciphers[i].ivmax &&
431 ctx->key_len == ciphers[i].keylen) {
432 cipher = ciphers[i].id;
436 if (!ciphers[i].id) {
441 memset(sess, 0, sizeof(struct session_op));
443 if ((state->d_fd = get_dev_crypto()) < 0)
446 sess->key = (caddr_t)key;
447 sess->keylen = ctx->key_len;
448 sess->cipher = cipher;
450 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
451 put_dev_crypto(state->d_fd);
459 * free anything we allocated earlier when initting a
460 * session, and close the session.
463 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
466 struct dev_crypto_state *state = ctx->cipher_data;
467 struct session_op *sess = &state->d_sess;
472 /* XXX if this ioctl fails, someting's wrong. the invoker
473 * may have called us with a bogus ctx, or we could
474 * have a device that for whatever reason just doesn't
475 * want to play ball - it's not clear what's right
476 * here - should this be an error? should it just
477 * increase a counter, hmm. For right now, we return
478 * 0 - I don't believe that to be "right". we could
479 * call the gorpy openssl lib error handlers that
480 * print messages to users of the library. hmm..
483 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
488 put_dev_crypto(state->d_fd);
495 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
496 * gets called when libcrypto requests a cipher NID.
500 const EVP_CIPHER cryptodev_rc4 = {
503 EVP_CIPH_VARIABLE_LENGTH,
507 sizeof(struct dev_crypto_state),
514 const EVP_CIPHER cryptodev_des_cbc = {
521 sizeof(struct dev_crypto_state),
522 EVP_CIPHER_set_asn1_iv,
523 EVP_CIPHER_get_asn1_iv,
528 const EVP_CIPHER cryptodev_3des_cbc = {
535 sizeof(struct dev_crypto_state),
536 EVP_CIPHER_set_asn1_iv,
537 EVP_CIPHER_get_asn1_iv,
541 const EVP_CIPHER cryptodev_bf_cbc = {
548 sizeof(struct dev_crypto_state),
549 EVP_CIPHER_set_asn1_iv,
550 EVP_CIPHER_get_asn1_iv,
554 const EVP_CIPHER cryptodev_cast_cbc = {
561 sizeof(struct dev_crypto_state),
562 EVP_CIPHER_set_asn1_iv,
563 EVP_CIPHER_get_asn1_iv,
567 const EVP_CIPHER cryptodev_aes_cbc = {
574 sizeof(struct dev_crypto_state),
575 EVP_CIPHER_set_asn1_iv,
576 EVP_CIPHER_get_asn1_iv,
580 const EVP_CIPHER cryptodev_aes_192_cbc = {
587 sizeof(struct dev_crypto_state),
588 EVP_CIPHER_set_asn1_iv,
589 EVP_CIPHER_get_asn1_iv,
593 const EVP_CIPHER cryptodev_aes_256_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_ctr = {
613 sizeof(struct dev_crypto_state),
614 EVP_CIPHER_set_asn1_iv,
615 EVP_CIPHER_get_asn1_iv,
619 const EVP_CIPHER cryptodev_aes_ctr_192 = {
626 sizeof(struct dev_crypto_state),
627 EVP_CIPHER_set_asn1_iv,
628 EVP_CIPHER_get_asn1_iv,
632 const EVP_CIPHER cryptodev_aes_ctr_256 = {
639 sizeof(struct dev_crypto_state),
640 EVP_CIPHER_set_asn1_iv,
641 EVP_CIPHER_get_asn1_iv,
646 * Registered by the ENGINE when used to find out how to deal with
647 * a particular NID in the ENGINE. this says what we'll do at the
648 * top level - note, that list is restricted by what we answer with
651 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
652 const int **nids, int nid)
655 return (cryptodev_usable_ciphers(nids));
659 *cipher = &cryptodev_rc4;
661 case NID_des_ede3_cbc:
662 *cipher = &cryptodev_3des_cbc;
665 *cipher = &cryptodev_des_cbc;
668 *cipher = &cryptodev_bf_cbc;
671 *cipher = &cryptodev_cast_cbc;
673 case NID_aes_128_cbc:
674 *cipher = &cryptodev_aes_cbc;
676 case NID_aes_192_cbc:
677 *cipher = &cryptodev_aes_192_cbc;
679 case NID_aes_256_cbc:
680 *cipher = &cryptodev_aes_256_cbc;
682 case NID_aes_128_ctr:
683 *cipher = &cryptodev_aes_ctr;
685 case NID_aes_192_ctr:
686 *cipher = &cryptodev_aes_ctr_192;
688 case NID_aes_256_ctr:
689 *cipher = &cryptodev_aes_ctr_256;
695 return (*cipher != NULL);
699 #ifdef USE_CRYPTODEV_DIGESTS
701 /* convert digest type to cryptodev */
703 digest_nid_to_cryptodev(int nid)
707 for (i = 0; digests[i].id; i++)
708 if (digests[i].nid == nid)
709 return (digests[i].id);
715 digest_key_length(int nid)
719 for (i = 0; digests[i].id; i++)
720 if (digests[i].nid == nid)
721 return digests[i].keylen;
726 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
728 struct dev_crypto_state *state = ctx->md_data;
729 struct session_op *sess = &state->d_sess;
732 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
733 printf("cryptodev_digest_init: Can't get digest \n");
737 memset(state, 0, sizeof(struct dev_crypto_state));
739 if ((state->d_fd = get_dev_crypto()) < 0) {
740 printf("cryptodev_digest_init: Can't get Dev \n");
744 sess->mackey = state->dummy_mac_key;
745 sess->mackeylen = digest_key_length(ctx->digest->type);
748 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
749 put_dev_crypto(state->d_fd);
751 printf("cryptodev_digest_init: Open session failed\n");
758 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
761 struct crypt_op cryp;
762 struct dev_crypto_state *state = ctx->md_data;
763 struct session_op *sess = &state->d_sess;
765 if (!data || state->d_fd < 0) {
766 printf("cryptodev_digest_update: illegal inputs \n");
774 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
775 /* if application doesn't support one buffer */
776 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
778 if (!state->mac_data) {
779 printf("cryptodev_digest_update: realloc failed\n");
783 memcpy(state->mac_data + state->mac_len, data, count);
784 state->mac_len += count;
789 memset(&cryp, 0, sizeof(cryp));
791 cryp.ses = sess->ses;
794 cryp.src = (caddr_t) data;
796 cryp.mac = (caddr_t) state->digest_res;
797 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
798 printf("cryptodev_digest_update: digest failed\n");
805 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
807 struct crypt_op cryp;
808 struct dev_crypto_state *state = ctx->md_data;
809 struct session_op *sess = &state->d_sess;
813 if (!md || state->d_fd < 0) {
814 printf("cryptodev_digest_final: illegal input\n");
818 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
819 /* if application doesn't support one buffer */
820 memset(&cryp, 0, sizeof(cryp));
821 cryp.ses = sess->ses;
823 cryp.len = state->mac_len;
824 cryp.src = state->mac_data;
826 cryp.mac = (caddr_t)md;
827 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
828 printf("cryptodev_digest_final: digest failed\n");
835 memcpy(md, state->digest_res, ctx->digest->md_size);
841 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
844 struct dev_crypto_state *state = ctx->md_data;
845 struct session_op *sess = &state->d_sess;
850 if (state->d_fd < 0) {
851 printf("cryptodev_digest_cleanup: illegal input\n");
855 if (state->mac_data) {
856 OPENSSL_free(state->mac_data);
857 state->mac_data = NULL;
861 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
862 printf("cryptodev_digest_cleanup: failed to close session\n");
867 put_dev_crypto(state->d_fd);
873 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
875 struct dev_crypto_state *fstate = from->md_data;
876 struct dev_crypto_state *dstate = to->md_data;
877 struct session_op *sess;
880 if (dstate == NULL || fstate == NULL)
883 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
885 sess = &dstate->d_sess;
887 digest = digest_nid_to_cryptodev(to->digest->type);
889 sess->mackey = dstate->dummy_mac_key;
890 sess->mackeylen = digest_key_length(to->digest->type);
893 dstate->d_fd = get_dev_crypto();
895 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
896 put_dev_crypto(dstate->d_fd);
898 printf("cryptodev_digest_init: Open session failed\n");
902 if (fstate->mac_len != 0) {
903 if (fstate->mac_data != NULL)
905 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
906 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
907 dstate->mac_len = fstate->mac_len;
915 const EVP_MD cryptodev_sha1 = {
920 cryptodev_digest_init,
921 cryptodev_digest_update,
922 cryptodev_digest_final,
923 cryptodev_digest_copy,
924 cryptodev_digest_cleanup,
925 EVP_PKEY_NULL_method,
927 sizeof(struct dev_crypto_state),
930 const EVP_MD cryptodev_md5 = {
933 16 /* MD5_DIGEST_LENGTH */,
935 cryptodev_digest_init,
936 cryptodev_digest_update,
937 cryptodev_digest_final,
938 cryptodev_digest_copy,
939 cryptodev_digest_cleanup,
940 EVP_PKEY_NULL_method,
942 sizeof(struct dev_crypto_state),
945 #endif /* USE_CRYPTODEV_DIGESTS */
949 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
950 const int **nids, int nid)
953 return (cryptodev_usable_digests(nids));
956 #ifdef USE_CRYPTODEV_DIGESTS
958 *digest = &cryptodev_md5;
961 *digest = &cryptodev_sha1;
964 #endif /* USE_CRYPTODEV_DIGESTS */
968 return (*digest != NULL);
972 * Convert a BIGNUM to the representation that /dev/crypto needs.
973 * Upon completion of use, the caller is responsible for freeing
977 bn2crparam(const BIGNUM *a, struct crparam *crp)
986 bits = BN_num_bits(a);
987 bytes = (bits + 7) / 8;
994 crp->crp_p = (caddr_t) b;
995 crp->crp_nbits = bits;
997 for (i = 0, j = 0; i < a->top; i++) {
998 for (k = 0; k < BN_BITS2 / 8; k++) {
999 if ((j + k) >= bytes)
1001 b[j + k] = a->d[i] >> (k * 8);
1008 /* Convert a /dev/crypto parameter to a BIGNUM */
1010 crparam2bn(struct crparam *crp, BIGNUM *a)
1015 bytes = (crp->crp_nbits + 7) / 8;
1020 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1023 for (i = 0; i < bytes; i++)
1024 pd[i] = crp->crp_p[bytes - i - 1];
1026 BN_bin2bn(pd, bytes, a);
1033 zapparams(struct crypt_kop *kop)
1037 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1038 if (kop->crk_param[i].crp_p)
1039 free(kop->crk_param[i].crp_p);
1040 kop->crk_param[i].crp_p = NULL;
1041 kop->crk_param[i].crp_nbits = 0;
1046 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1050 if ((fd = get_asym_dev_crypto()) < 0)
1054 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1055 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1059 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1060 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1064 if (ioctl(fd, CIOCKEY, kop) == 0) {
1066 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1068 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1076 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1077 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1079 struct crypt_kop kop;
1082 /* Currently, we know we can do mod exp iff we can do any
1083 * asymmetric operations at all.
1085 if (cryptodev_asymfeat == 0) {
1086 ret = BN_mod_exp(r, a, p, m, ctx);
1090 memset(&kop, 0, sizeof kop);
1091 kop.crk_op = CRK_MOD_EXP;
1093 /* inputs: a^p % m */
1094 if (bn2crparam(a, &kop.crk_param[0]))
1096 if (bn2crparam(p, &kop.crk_param[1]))
1098 if (bn2crparam(m, &kop.crk_param[2]))
1100 kop.crk_iparams = 3;
1102 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1103 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1104 printf("OCF asym process failed, Running in software\n");
1105 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1107 } else if (ECANCELED == kop.crk_status) {
1108 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1109 printf("OCF hardware operation cancelled. Running in Software\n");
1110 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1112 /* else cryptodev operation worked ok ==> ret = 1*/
1120 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1124 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1130 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1132 struct crypt_kop kop;
1135 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1136 /* XXX 0 means failure?? */
1140 memset(&kop, 0, sizeof kop);
1141 kop.crk_op = CRK_MOD_EXP_CRT;
1142 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1143 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1145 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1147 if (bn2crparam(I, &kop.crk_param[2]))
1149 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1151 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1153 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1155 kop.crk_iparams = 6;
1157 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1158 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1159 printf("OCF asym process failed, running in Software\n");
1160 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1162 } else if (ECANCELED == kop.crk_status) {
1163 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1164 printf("OCF hardware operation cancelled. Running in Software\n");
1165 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1167 /* else cryptodev operation worked ok ==> ret = 1*/
1174 static RSA_METHOD cryptodev_rsa = {
1175 "cryptodev RSA method",
1176 NULL, /* rsa_pub_enc */
1177 NULL, /* rsa_pub_dec */
1178 NULL, /* rsa_priv_enc */
1179 NULL, /* rsa_priv_dec */
1185 NULL, /* app_data */
1186 NULL, /* rsa_sign */
1187 NULL /* rsa_verify */
1191 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1192 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1194 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1198 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1199 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1200 BN_CTX *ctx, BN_MONT_CTX *mont)
1207 /* v = ( g^u1 * y^u2 mod p ) mod q */
1208 /* let t1 = g ^ u1 mod p */
1211 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1214 /* let t2 = y ^ u2 mod p */
1215 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1217 /* let u1 = t1 * t2 mod p */
1218 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1230 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1232 struct crypt_kop kop;
1233 BIGNUM *r = NULL, *s = NULL;
1234 DSA_SIG *dsaret = NULL;
1236 if ((r = BN_new()) == NULL)
1238 if ((s = BN_new()) == NULL) {
1243 memset(&kop, 0, sizeof kop);
1244 kop.crk_op = CRK_DSA_SIGN;
1246 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1247 kop.crk_param[0].crp_p = (caddr_t)dgst;
1248 kop.crk_param[0].crp_nbits = dlen * 8;
1249 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1251 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1253 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1255 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1257 kop.crk_iparams = 5;
1259 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1260 BN_num_bytes(dsa->q), s) == 0) {
1261 dsaret = DSA_SIG_new();
1265 const DSA_METHOD *meth = DSA_OpenSSL();
1268 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1271 kop.crk_param[0].crp_p = NULL;
1277 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1278 DSA_SIG *sig, DSA *dsa)
1280 struct crypt_kop kop;
1283 memset(&kop, 0, sizeof kop);
1284 kop.crk_op = CRK_DSA_VERIFY;
1286 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1287 kop.crk_param[0].crp_p = (caddr_t)dgst;
1288 kop.crk_param[0].crp_nbits = dlen * 8;
1289 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1291 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1293 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1295 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1297 if (bn2crparam(sig->r, &kop.crk_param[5]))
1299 if (bn2crparam(sig->s, &kop.crk_param[6]))
1301 kop.crk_iparams = 7;
1303 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1304 /*OCF success value is 0, if not zero, change dsaret to fail*/
1305 if(0 != kop.crk_status) dsaret = 0;
1307 const DSA_METHOD *meth = DSA_OpenSSL();
1309 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1312 kop.crk_param[0].crp_p = NULL;
1317 static DSA_METHOD cryptodev_dsa = {
1318 "cryptodev DSA method",
1320 NULL, /* dsa_sign_setup */
1322 NULL, /* dsa_mod_exp */
1331 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1332 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1335 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1339 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1341 struct crypt_kop kop;
1345 if ((fd = get_asym_dev_crypto()) < 0) {
1346 const DH_METHOD *meth = DH_OpenSSL();
1348 return ((meth->compute_key)(key, pub_key, dh));
1351 keylen = BN_num_bits(dh->p);
1353 memset(&kop, 0, sizeof kop);
1354 kop.crk_op = CRK_DH_COMPUTE_KEY;
1356 /* inputs: dh->priv_key pub_key dh->p key */
1357 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1359 if (bn2crparam(pub_key, &kop.crk_param[1]))
1361 if (bn2crparam(dh->p, &kop.crk_param[2]))
1363 kop.crk_iparams = 3;
1365 kop.crk_param[3].crp_p = (caddr_t) key;
1366 kop.crk_param[3].crp_nbits = keylen * 8;
1367 kop.crk_oparams = 1;
1369 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1370 const DH_METHOD *meth = DH_OpenSSL();
1372 dhret = (meth->compute_key)(key, pub_key, dh);
1375 kop.crk_param[3].crp_p = NULL;
1380 static DH_METHOD cryptodev_dh = {
1381 "cryptodev DH method",
1382 NULL, /* cryptodev_dh_generate_key */
1392 * ctrl right now is just a wrapper that doesn't do much
1393 * but I expect we'll want some options soon.
1396 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1398 #ifdef HAVE_SYSLOG_R
1399 struct syslog_data sd = SYSLOG_DATA_INIT;
1404 #ifdef HAVE_SYSLOG_R
1405 syslog_r(LOG_ERR, &sd,
1406 "cryptodev_ctrl: unknown command %d", cmd);
1408 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1416 ENGINE_load_cryptodev(void)
1418 ENGINE *engine = ENGINE_new();
1423 if ((fd = get_dev_crypto()) < 0) {
1424 ENGINE_free(engine);
1429 * find out what asymmetric crypto algorithms we support
1431 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1433 ENGINE_free(engine);
1438 if (!ENGINE_set_id(engine, "cryptodev") ||
1439 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1440 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1441 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1442 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1443 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1444 ENGINE_free(engine);
1448 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1449 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1451 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1452 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1453 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1454 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1455 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1456 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1457 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1458 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1459 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1460 cryptodev_rsa.rsa_mod_exp =
1461 cryptodev_rsa_mod_exp;
1463 cryptodev_rsa.rsa_mod_exp =
1464 cryptodev_rsa_nocrt_mod_exp;
1468 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1469 const DSA_METHOD *meth = DSA_OpenSSL();
1471 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1472 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1473 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1474 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1475 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1476 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1478 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1479 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1482 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1483 const DH_METHOD *dh_meth = DH_OpenSSL();
1485 cryptodev_dh.generate_key = dh_meth->generate_key;
1486 cryptodev_dh.compute_key = dh_meth->compute_key;
1487 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1488 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1489 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1490 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1491 cryptodev_dh.compute_key =
1492 cryptodev_dh_compute_key;
1497 ENGINE_free(engine);
1501 #endif /* HAVE_CRYPTODEV */