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 <crypto/dh/dh.h>
58 # include <crypto/dsa/dsa.h>
59 # include <crypto/err/err.h>
60 # include <crypto/rsa/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,
164 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
167 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
170 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
177 # ifdef USE_CRYPTODEV_DIGESTS
184 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
187 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
190 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
194 CRYPTO_MD5_KPDK, NID_undef, 0
197 CRYPTO_SHA1_KPDK, NID_undef, 0
200 CRYPTO_MD5, NID_md5, 16
203 CRYPTO_SHA1, NID_sha1, 20
212 * Return a fd if /dev/crypto seems usable, 0 otherwise.
214 static int open_dev_crypto(void)
219 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
222 if (fcntl(fd, F_SETFD, 1) == -1) {
231 static int get_dev_crypto(void)
235 if ((fd = open_dev_crypto()) == -1)
237 # ifndef CRIOGET_NOT_NEEDED
238 if (ioctl(fd, CRIOGET, &retfd) == -1)
242 if (fcntl(retfd, F_SETFD, 1) == -1) {
252 static void put_dev_crypto(int fd)
254 # ifndef CRIOGET_NOT_NEEDED
259 /* Caching version for asym operations */
260 static int get_asym_dev_crypto(void)
265 fd = get_dev_crypto();
270 * Find out what ciphers /dev/crypto will let us have a session for.
271 * XXX note, that some of these openssl doesn't deal with yet!
272 * returning them here is harmless, as long as we return NULL
273 * when asked for a handler in the cryptodev_engine_ciphers routine
275 static int get_cryptodev_ciphers(const int **cnids)
277 static int nids[CRYPTO_ALGORITHM_MAX];
278 struct session_op sess;
279 int fd, i, count = 0;
281 if ((fd = get_dev_crypto()) < 0) {
285 memset(&sess, 0, sizeof(sess));
286 sess.key = (caddr_t) "123456789abcdefghijklmno";
288 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
289 if (ciphers[i].nid == NID_undef)
291 sess.cipher = ciphers[i].id;
292 sess.keylen = ciphers[i].keylen;
294 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
295 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
296 nids[count++] = ciphers[i].nid;
307 # ifdef USE_CRYPTODEV_DIGESTS
309 * Find out what digests /dev/crypto will let us have a session for.
310 * XXX note, that some of these openssl doesn't deal with yet!
311 * returning them here is harmless, as long as we return NULL
312 * when asked for a handler in the cryptodev_engine_digests routine
314 static int get_cryptodev_digests(const int **cnids)
316 static int nids[CRYPTO_ALGORITHM_MAX];
317 struct session_op sess;
318 int fd, i, count = 0;
320 if ((fd = get_dev_crypto()) < 0) {
324 memset(&sess, 0, sizeof(sess));
325 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
326 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
327 if (digests[i].nid == NID_undef)
329 sess.mac = digests[i].id;
330 sess.mackeylen = digests[i].keylen;
332 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
333 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
334 nids[count++] = digests[i].nid;
347 * Find the useable ciphers|digests from dev/crypto - this is the first
348 * thing called by the engine init crud which determines what it
349 * can use for ciphers from this engine. We want to return
350 * only what we can do, anythine else is handled by software.
352 * If we can't initialize the device to do anything useful for
353 * any reason, we want to return a NULL array, and 0 length,
354 * which forces everything to be done is software. By putting
355 * the initalization of the device in here, we ensure we can
356 * use this engine as the default, and if for whatever reason
357 * /dev/crypto won't do what we want it will just be done in
360 * This can (should) be greatly expanded to perhaps take into
361 * account speed of the device, and what we want to do.
362 * (although the disabling of particular alg's could be controlled
363 * by the device driver with sysctl's.) - this is where we
364 * want most of the decisions made about what we actually want
365 * to use from /dev/crypto.
367 static int cryptodev_usable_ciphers(const int **nids)
369 return (get_cryptodev_ciphers(nids));
372 static int cryptodev_usable_digests(const int **nids)
374 # ifdef USE_CRYPTODEV_DIGESTS
375 return (get_cryptodev_digests(nids));
378 * XXXX just disable all digests for now, because it sucks.
379 * we need a better way to decide this - i.e. I may not
380 * want digests on slow cards like hifn on fast machines,
381 * but might want them on slow or loaded machines, etc.
382 * will also want them when using crypto cards that don't
383 * suck moose gonads - would be nice to be able to decide something
384 * as reasonable default without having hackery that's card dependent.
385 * of course, the default should probably be just do everything,
386 * with perhaps a sysctl to turn algoritms off (or have them off
387 * by default) on cards that generally suck like the hifn.
395 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
396 const unsigned char *in, size_t inl)
398 struct crypt_op cryp;
399 struct dev_crypto_state *state = ctx->cipher_data;
400 struct session_op *sess = &state->d_sess;
402 unsigned char save_iv[EVP_MAX_IV_LENGTH];
408 if ((inl % ctx->cipher->block_size) != 0)
411 memset(&cryp, 0, sizeof(cryp));
413 cryp.ses = sess->ses;
416 cryp.src = (caddr_t) in;
417 cryp.dst = (caddr_t) out;
420 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
422 if (ctx->cipher->iv_len) {
423 cryp.iv = (caddr_t) ctx->iv;
425 iiv = in + inl - ctx->cipher->iv_len;
426 memcpy(save_iv, iiv, ctx->cipher->iv_len);
431 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
433 * XXX need better errror handling this can fail for a number of
439 if (ctx->cipher->iv_len) {
441 iiv = out + inl - ctx->cipher->iv_len;
444 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
450 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
451 const unsigned char *iv, int enc)
453 struct dev_crypto_state *state = ctx->cipher_data;
454 struct session_op *sess = &state->d_sess;
457 for (i = 0; ciphers[i].id; i++)
458 if (ctx->cipher->nid == ciphers[i].nid &&
459 ctx->cipher->iv_len <= ciphers[i].ivmax &&
460 ctx->key_len == ciphers[i].keylen) {
461 cipher = ciphers[i].id;
465 if (!ciphers[i].id) {
470 memset(sess, 0, sizeof(struct session_op));
472 if ((state->d_fd = get_dev_crypto()) < 0)
475 sess->key = (caddr_t) key;
476 sess->keylen = ctx->key_len;
477 sess->cipher = cipher;
479 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
480 put_dev_crypto(state->d_fd);
488 * free anything we allocated earlier when initting a
489 * session, and close the session.
491 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
494 struct dev_crypto_state *state = ctx->cipher_data;
495 struct session_op *sess = &state->d_sess;
501 * XXX if this ioctl fails, someting's wrong. the invoker may have called
502 * us with a bogus ctx, or we could have a device that for whatever
503 * reason just doesn't want to play ball - it's not clear what's right
504 * here - should this be an error? should it just increase a counter,
505 * hmm. For right now, we return 0 - I don't believe that to be "right".
506 * we could call the gorpy openssl lib error handlers that print messages
507 * to users of the library. hmm..
510 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
515 put_dev_crypto(state->d_fd);
522 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
523 * gets called when libcrypto requests a cipher NID.
527 const EVP_CIPHER cryptodev_rc4 = {
530 EVP_CIPH_VARIABLE_LENGTH,
534 sizeof(struct dev_crypto_state),
541 const EVP_CIPHER cryptodev_des_cbc = {
548 sizeof(struct dev_crypto_state),
549 EVP_CIPHER_set_asn1_iv,
550 EVP_CIPHER_get_asn1_iv,
555 const EVP_CIPHER cryptodev_3des_cbc = {
562 sizeof(struct dev_crypto_state),
563 EVP_CIPHER_set_asn1_iv,
564 EVP_CIPHER_get_asn1_iv,
568 const EVP_CIPHER cryptodev_bf_cbc = {
575 sizeof(struct dev_crypto_state),
576 EVP_CIPHER_set_asn1_iv,
577 EVP_CIPHER_get_asn1_iv,
581 const EVP_CIPHER cryptodev_cast_cbc = {
588 sizeof(struct dev_crypto_state),
589 EVP_CIPHER_set_asn1_iv,
590 EVP_CIPHER_get_asn1_iv,
594 const EVP_CIPHER cryptodev_aes_cbc = {
601 sizeof(struct dev_crypto_state),
602 EVP_CIPHER_set_asn1_iv,
603 EVP_CIPHER_get_asn1_iv,
607 const EVP_CIPHER cryptodev_aes_192_cbc = {
614 sizeof(struct dev_crypto_state),
615 EVP_CIPHER_set_asn1_iv,
616 EVP_CIPHER_get_asn1_iv,
620 const EVP_CIPHER cryptodev_aes_256_cbc = {
627 sizeof(struct dev_crypto_state),
628 EVP_CIPHER_set_asn1_iv,
629 EVP_CIPHER_get_asn1_iv,
634 * Registered by the ENGINE when used to find out how to deal with
635 * a particular NID in the ENGINE. this says what we'll do at the
636 * top level - note, that list is restricted by what we answer with
639 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
640 const int **nids, int nid)
643 return (cryptodev_usable_ciphers(nids));
647 *cipher = &cryptodev_rc4;
649 case NID_des_ede3_cbc:
650 *cipher = &cryptodev_3des_cbc;
653 *cipher = &cryptodev_des_cbc;
656 *cipher = &cryptodev_bf_cbc;
659 *cipher = &cryptodev_cast_cbc;
661 case NID_aes_128_cbc:
662 *cipher = &cryptodev_aes_cbc;
664 case NID_aes_192_cbc:
665 *cipher = &cryptodev_aes_192_cbc;
667 case NID_aes_256_cbc:
668 *cipher = &cryptodev_aes_256_cbc;
674 return (*cipher != NULL);
677 # ifdef USE_CRYPTODEV_DIGESTS
679 /* convert digest type to cryptodev */
680 static int digest_nid_to_cryptodev(int nid)
684 for (i = 0; digests[i].id; i++)
685 if (digests[i].nid == nid)
686 return (digests[i].id);
690 static int digest_key_length(int nid)
694 for (i = 0; digests[i].id; i++)
695 if (digests[i].nid == nid)
696 return digests[i].keylen;
700 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
702 struct dev_crypto_state *state = ctx->md_data;
703 struct session_op *sess = &state->d_sess;
706 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) {
707 printf("cryptodev_digest_init: Can't get digest \n");
711 memset(state, 0, sizeof(struct dev_crypto_state));
713 if ((state->d_fd = get_dev_crypto()) < 0) {
714 printf("cryptodev_digest_init: Can't get Dev \n");
718 sess->mackey = state->dummy_mac_key;
719 sess->mackeylen = digest_key_length(ctx->digest->type);
722 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
723 put_dev_crypto(state->d_fd);
725 printf("cryptodev_digest_init: Open session failed\n");
732 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
735 struct crypt_op cryp;
736 struct dev_crypto_state *state = ctx->md_data;
737 struct session_op *sess = &state->d_sess;
739 if (!data || state->d_fd < 0) {
740 printf("cryptodev_digest_update: illegal inputs \n");
748 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
749 /* if application doesn't support one buffer */
751 OPENSSL_realloc(state->mac_data, state->mac_len + count);
753 if (!state->mac_data) {
754 printf("cryptodev_digest_update: realloc failed\n");
758 memcpy(state->mac_data + state->mac_len, data, count);
759 state->mac_len += count;
764 memset(&cryp, 0, sizeof(cryp));
766 cryp.ses = sess->ses;
769 cryp.src = (caddr_t) data;
771 cryp.mac = (caddr_t) state->digest_res;
772 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
773 printf("cryptodev_digest_update: digest failed\n");
779 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
781 struct crypt_op cryp;
782 struct dev_crypto_state *state = ctx->md_data;
783 struct session_op *sess = &state->d_sess;
787 if (!md || state->d_fd < 0) {
788 printf("cryptodev_digest_final: illegal input\n");
792 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
793 /* if application doesn't support one buffer */
794 memset(&cryp, 0, sizeof(cryp));
795 cryp.ses = sess->ses;
797 cryp.len = state->mac_len;
798 cryp.src = state->mac_data;
800 cryp.mac = (caddr_t) md;
801 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
802 printf("cryptodev_digest_final: digest failed\n");
809 memcpy(md, state->digest_res, ctx->digest->md_size);
814 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
817 struct dev_crypto_state *state = ctx->md_data;
818 struct session_op *sess = &state->d_sess;
823 if (state->d_fd < 0) {
824 printf("cryptodev_digest_cleanup: illegal input\n");
828 if (state->mac_data) {
829 OPENSSL_free(state->mac_data);
830 state->mac_data = NULL;
834 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
835 printf("cryptodev_digest_cleanup: failed to close session\n");
840 put_dev_crypto(state->d_fd);
846 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
848 struct dev_crypto_state *fstate = from->md_data;
849 struct dev_crypto_state *dstate = to->md_data;
850 struct session_op *sess;
853 if (dstate == NULL || fstate == NULL)
856 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
858 sess = &dstate->d_sess;
860 digest = digest_nid_to_cryptodev(to->digest->type);
862 sess->mackey = dstate->dummy_mac_key;
863 sess->mackeylen = digest_key_length(to->digest->type);
866 dstate->d_fd = get_dev_crypto();
868 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
869 put_dev_crypto(dstate->d_fd);
871 printf("cryptodev_digest_init: Open session failed\n");
875 if (fstate->mac_len != 0) {
876 if (fstate->mac_data != NULL) {
877 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
878 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
879 dstate->mac_len = fstate->mac_len;
886 const EVP_MD cryptodev_sha1 = {
891 cryptodev_digest_init,
892 cryptodev_digest_update,
893 cryptodev_digest_final,
894 cryptodev_digest_copy,
895 cryptodev_digest_cleanup,
896 EVP_PKEY_NULL_method,
898 sizeof(struct dev_crypto_state),
901 const EVP_MD cryptodev_md5 = {
904 16 /* MD5_DIGEST_LENGTH */ ,
906 cryptodev_digest_init,
907 cryptodev_digest_update,
908 cryptodev_digest_final,
909 cryptodev_digest_copy,
910 cryptodev_digest_cleanup,
911 EVP_PKEY_NULL_method,
912 64 /* MD5_CBLOCK */ ,
913 sizeof(struct dev_crypto_state),
916 # endif /* USE_CRYPTODEV_DIGESTS */
919 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
920 const int **nids, int nid)
923 return (cryptodev_usable_digests(nids));
926 # ifdef USE_CRYPTODEV_DIGESTS
928 *digest = &cryptodev_md5;
931 *digest = &cryptodev_sha1;
934 # endif /* USE_CRYPTODEV_DIGESTS */
938 return (*digest != NULL);
942 * Convert a BIGNUM to the representation that /dev/crypto needs.
943 * Upon completion of use, the caller is responsible for freeing
946 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
955 bits = BN_num_bits(a);
956 bytes = (bits + 7) / 8;
963 crp->crp_p = (caddr_t) b;
964 crp->crp_nbits = bits;
966 for (i = 0, j = 0; i < a->top; i++) {
967 for (k = 0; k < BN_BITS2 / 8; k++) {
968 if ((j + k) >= bytes)
970 b[j + k] = a->d[i] >> (k * 8);
977 /* Convert a /dev/crypto parameter to a BIGNUM */
978 static int crparam2bn(struct crparam *crp, BIGNUM *a)
983 bytes = (crp->crp_nbits + 7) / 8;
988 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
991 for (i = 0; i < bytes; i++)
992 pd[i] = crp->crp_p[bytes - i - 1];
994 BN_bin2bn(pd, bytes, a);
1000 static void zapparams(struct crypt_kop *kop)
1004 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1005 if (kop->crk_param[i].crp_p)
1006 free(kop->crk_param[i].crp_p);
1007 kop->crk_param[i].crp_p = NULL;
1008 kop->crk_param[i].crp_nbits = 0;
1013 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1018 if ((fd = get_asym_dev_crypto()) < 0)
1022 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1023 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1027 kop->crk_param[kop->crk_iparams + 1].crp_p =
1028 calloc(slen, sizeof(char));
1029 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1033 if (ioctl(fd, CIOCKEY, kop) == 0) {
1035 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1037 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1045 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1046 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1048 struct crypt_kop kop;
1052 * Currently, we know we can do mod exp iff we can do any asymmetric
1053 * operations at all.
1055 if (cryptodev_asymfeat == 0) {
1056 ret = BN_mod_exp(r, a, p, m, ctx);
1060 memset(&kop, 0, sizeof kop);
1061 kop.crk_op = CRK_MOD_EXP;
1063 /* inputs: a^p % m */
1064 if (bn2crparam(a, &kop.crk_param[0]))
1066 if (bn2crparam(p, &kop.crk_param[1]))
1068 if (bn2crparam(m, &kop.crk_param[2]))
1070 kop.crk_iparams = 3;
1072 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1073 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1074 printf("OCF asym process failed, Running in software\n");
1075 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1077 } else if (ECANCELED == kop.crk_status) {
1078 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1079 printf("OCF hardware operation cancelled. Running in Software\n");
1080 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1082 /* else cryptodev operation worked ok ==> ret = 1 */
1090 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1095 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1101 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1103 struct crypt_kop kop;
1106 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1107 /* XXX 0 means failure?? */
1111 memset(&kop, 0, sizeof kop);
1112 kop.crk_op = CRK_MOD_EXP_CRT;
1113 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1114 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1116 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1118 if (bn2crparam(I, &kop.crk_param[2]))
1120 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1122 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1124 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1126 kop.crk_iparams = 6;
1128 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1129 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1130 printf("OCF asym process failed, running in Software\n");
1131 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1133 } else if (ECANCELED == kop.crk_status) {
1134 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1135 printf("OCF hardware operation cancelled. Running in Software\n");
1136 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1138 /* else cryptodev operation worked ok ==> ret = 1 */
1145 static RSA_METHOD cryptodev_rsa = {
1146 "cryptodev RSA method",
1147 NULL, /* rsa_pub_enc */
1148 NULL, /* rsa_pub_dec */
1149 NULL, /* rsa_priv_enc */
1150 NULL, /* rsa_priv_dec */
1156 NULL, /* app_data */
1157 NULL, /* rsa_sign */
1158 NULL /* rsa_verify */
1162 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1163 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1165 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1169 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1170 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1171 BN_CTX *ctx, BN_MONT_CTX *mont)
1178 /* v = ( g^u1 * y^u2 mod p ) mod q */
1179 /* let t1 = g ^ u1 mod p */
1182 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
1185 /* let t2 = y ^ u2 mod p */
1186 if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
1188 /* let u1 = t1 * t2 mod p */
1189 if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
1200 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1203 struct crypt_kop kop;
1204 BIGNUM *r = NULL, *s = NULL;
1205 DSA_SIG *dsaret = NULL;
1207 if ((r = BN_new()) == NULL)
1209 if ((s = BN_new()) == NULL) {
1214 memset(&kop, 0, sizeof kop);
1215 kop.crk_op = CRK_DSA_SIGN;
1217 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1218 kop.crk_param[0].crp_p = (caddr_t) dgst;
1219 kop.crk_param[0].crp_nbits = dlen * 8;
1220 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1222 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1224 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1226 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1228 kop.crk_iparams = 5;
1230 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1231 BN_num_bytes(dsa->q), s) == 0) {
1232 dsaret = DSA_SIG_new();
1236 const DSA_METHOD *meth = DSA_OpenSSL();
1239 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
1242 kop.crk_param[0].crp_p = NULL;
1248 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1249 DSA_SIG *sig, DSA *dsa)
1251 struct crypt_kop kop;
1254 memset(&kop, 0, sizeof kop);
1255 kop.crk_op = CRK_DSA_VERIFY;
1257 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1258 kop.crk_param[0].crp_p = (caddr_t) dgst;
1259 kop.crk_param[0].crp_nbits = dlen * 8;
1260 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1262 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1264 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1266 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1268 if (bn2crparam(sig->r, &kop.crk_param[5]))
1270 if (bn2crparam(sig->s, &kop.crk_param[6]))
1272 kop.crk_iparams = 7;
1274 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1276 * OCF success value is 0, if not zero, change dsaret to fail
1278 if (0 != kop.crk_status)
1281 const DSA_METHOD *meth = DSA_OpenSSL();
1283 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
1286 kop.crk_param[0].crp_p = NULL;
1291 static DSA_METHOD cryptodev_dsa = {
1292 "cryptodev DSA method",
1294 NULL, /* dsa_sign_setup */
1296 NULL, /* dsa_mod_exp */
1305 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1306 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1309 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1313 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1315 struct crypt_kop kop;
1319 if ((fd = get_asym_dev_crypto()) < 0) {
1320 const DH_METHOD *meth = DH_OpenSSL();
1322 return ((meth->compute_key) (key, pub_key, dh));
1325 keylen = BN_num_bits(dh->p);
1327 memset(&kop, 0, sizeof kop);
1328 kop.crk_op = CRK_DH_COMPUTE_KEY;
1330 /* inputs: dh->priv_key pub_key dh->p key */
1331 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1333 if (bn2crparam(pub_key, &kop.crk_param[1]))
1335 if (bn2crparam(dh->p, &kop.crk_param[2]))
1337 kop.crk_iparams = 3;
1339 kop.crk_param[3].crp_p = (caddr_t) key;
1340 kop.crk_param[3].crp_nbits = keylen * 8;
1341 kop.crk_oparams = 1;
1343 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1344 const DH_METHOD *meth = DH_OpenSSL();
1346 dhret = (meth->compute_key) (key, pub_key, dh);
1349 kop.crk_param[3].crp_p = NULL;
1354 static DH_METHOD cryptodev_dh = {
1355 "cryptodev DH method",
1356 NULL, /* cryptodev_dh_generate_key */
1366 * ctrl right now is just a wrapper that doesn't do much
1367 * but I expect we'll want some options soon.
1370 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1372 # ifdef HAVE_SYSLOG_R
1373 struct syslog_data sd = SYSLOG_DATA_INIT;
1378 # ifdef HAVE_SYSLOG_R
1379 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1381 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1388 void ENGINE_load_cryptodev(void)
1390 ENGINE *engine = ENGINE_new();
1395 if ((fd = get_dev_crypto()) < 0) {
1396 ENGINE_free(engine);
1401 * find out what asymmetric crypto algorithms we support
1403 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1405 ENGINE_free(engine);
1410 if (!ENGINE_set_id(engine, "cryptodev") ||
1411 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1412 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1413 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1414 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1415 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1416 ENGINE_free(engine);
1420 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1421 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1423 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1424 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1425 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1426 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1427 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1428 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1429 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1430 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1431 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1432 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1434 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1438 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1439 const DSA_METHOD *meth = DSA_OpenSSL();
1441 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1442 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1443 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1444 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1445 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1446 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1448 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1449 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1452 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1453 const DH_METHOD *dh_meth = DH_OpenSSL();
1455 cryptodev_dh.generate_key = dh_meth->generate_key;
1456 cryptodev_dh.compute_key = dh_meth->compute_key;
1457 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1458 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1459 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1460 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1461 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1466 ENGINE_free(engine);
1470 #endif /* HAVE_CRYPTODEV */