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 <crypto/dh/dh.h>
59 #include <crypto/dsa/dsa.h>
60 #include <crypto/err/err.h>
61 #include <crypto/rsa/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_BLF_CBC, NID_bf_cbc, 8, 16, },
151 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
152 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
153 { 0, NID_undef, 0, 0, },
156 #ifdef USE_CRYPTODEV_DIGESTS
162 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
163 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
164 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
165 { CRYPTO_MD5_KPDK, NID_undef, 0},
166 { CRYPTO_SHA1_KPDK, NID_undef, 0},
167 { CRYPTO_MD5, NID_md5, 16},
168 { CRYPTO_SHA1, NID_sha1, 20},
174 * Return a fd if /dev/crypto seems usable, 0 otherwise.
177 open_dev_crypto(void)
182 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
185 if (fcntl(fd, F_SETFD, 1) == -1) {
199 if ((fd = open_dev_crypto()) == -1)
201 #ifndef CRIOGET_NOT_NEEDED
202 if (ioctl(fd, CRIOGET, &retfd) == -1)
206 if (fcntl(retfd, F_SETFD, 1) == -1) {
216 static void put_dev_crypto(int fd)
218 #ifndef CRIOGET_NOT_NEEDED
223 /* Caching version for asym operations */
225 get_asym_dev_crypto(void)
230 fd = get_dev_crypto();
235 * Find out what ciphers /dev/crypto will let us have a session for.
236 * XXX note, that some of these openssl doesn't deal with yet!
237 * returning them here is harmless, as long as we return NULL
238 * when asked for a handler in the cryptodev_engine_ciphers routine
241 get_cryptodev_ciphers(const int **cnids)
243 static int nids[CRYPTO_ALGORITHM_MAX];
244 struct session_op sess;
245 int fd, i, count = 0;
247 if ((fd = get_dev_crypto()) < 0) {
251 memset(&sess, 0, sizeof(sess));
252 sess.key = (caddr_t)"123456789abcdefghijklmno";
254 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
255 if (ciphers[i].nid == NID_undef)
257 sess.cipher = ciphers[i].id;
258 sess.keylen = ciphers[i].keylen;
260 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
261 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
262 nids[count++] = ciphers[i].nid;
273 #ifdef USE_CRYPTODEV_DIGESTS
275 * Find out what digests /dev/crypto will let us have a session for.
276 * XXX note, that some of these openssl doesn't deal with yet!
277 * returning them here is harmless, as long as we return NULL
278 * when asked for a handler in the cryptodev_engine_digests routine
281 get_cryptodev_digests(const int **cnids)
283 static int nids[CRYPTO_ALGORITHM_MAX];
284 struct session_op sess;
285 int fd, i, count = 0;
287 if ((fd = get_dev_crypto()) < 0) {
291 memset(&sess, 0, sizeof(sess));
292 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
293 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
294 if (digests[i].nid == NID_undef)
296 sess.mac = digests[i].id;
297 sess.mackeylen = digests[i].keylen;
299 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
300 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
301 nids[count++] = digests[i].nid;
314 * Find the useable ciphers|digests from dev/crypto - this is the first
315 * thing called by the engine init crud which determines what it
316 * can use for ciphers from this engine. We want to return
317 * only what we can do, anythine else is handled by software.
319 * If we can't initialize the device to do anything useful for
320 * any reason, we want to return a NULL array, and 0 length,
321 * which forces everything to be done is software. By putting
322 * the initalization of the device in here, we ensure we can
323 * use this engine as the default, and if for whatever reason
324 * /dev/crypto won't do what we want it will just be done in
327 * This can (should) be greatly expanded to perhaps take into
328 * account speed of the device, and what we want to do.
329 * (although the disabling of particular alg's could be controlled
330 * by the device driver with sysctl's.) - this is where we
331 * want most of the decisions made about what we actually want
332 * to use from /dev/crypto.
335 cryptodev_usable_ciphers(const int **nids)
337 return (get_cryptodev_ciphers(nids));
341 cryptodev_usable_digests(const int **nids)
343 #ifdef USE_CRYPTODEV_DIGESTS
344 return (get_cryptodev_digests(nids));
347 * XXXX just disable all digests for now, because it sucks.
348 * we need a better way to decide this - i.e. I may not
349 * want digests on slow cards like hifn on fast machines,
350 * but might want them on slow or loaded machines, etc.
351 * will also want them when using crypto cards that don't
352 * suck moose gonads - would be nice to be able to decide something
353 * as reasonable default without having hackery that's card dependent.
354 * of course, the default should probably be just do everything,
355 * with perhaps a sysctl to turn algoritms off (or have them off
356 * by default) on cards that generally suck like the hifn.
364 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
365 const unsigned char *in, size_t inl)
367 struct crypt_op cryp;
368 struct dev_crypto_state *state = ctx->cipher_data;
369 struct session_op *sess = &state->d_sess;
371 unsigned char save_iv[EVP_MAX_IV_LENGTH];
377 if ((inl % ctx->cipher->block_size) != 0)
380 memset(&cryp, 0, sizeof(cryp));
382 cryp.ses = sess->ses;
385 cryp.src = (caddr_t) in;
386 cryp.dst = (caddr_t) out;
389 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
391 if (ctx->cipher->iv_len) {
392 cryp.iv = (caddr_t) ctx->iv;
394 iiv = in + inl - ctx->cipher->iv_len;
395 memcpy(save_iv, iiv, ctx->cipher->iv_len);
400 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
401 /* XXX need better errror handling
402 * this can fail for a number of different reasons.
407 if (ctx->cipher->iv_len) {
409 iiv = out + inl - ctx->cipher->iv_len;
412 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
418 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
419 const unsigned char *iv, int enc)
421 struct dev_crypto_state *state = ctx->cipher_data;
422 struct session_op *sess = &state->d_sess;
425 for (i = 0; ciphers[i].id; i++)
426 if (ctx->cipher->nid == ciphers[i].nid &&
427 ctx->cipher->iv_len <= ciphers[i].ivmax &&
428 ctx->key_len == ciphers[i].keylen) {
429 cipher = ciphers[i].id;
433 if (!ciphers[i].id) {
438 memset(sess, 0, sizeof(struct session_op));
440 if ((state->d_fd = get_dev_crypto()) < 0)
443 sess->key = (caddr_t)key;
444 sess->keylen = ctx->key_len;
445 sess->cipher = cipher;
447 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
448 put_dev_crypto(state->d_fd);
456 * free anything we allocated earlier when initting a
457 * session, and close the session.
460 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
463 struct dev_crypto_state *state = ctx->cipher_data;
464 struct session_op *sess = &state->d_sess;
469 /* XXX if this ioctl fails, someting's wrong. the invoker
470 * may have called us with a bogus ctx, or we could
471 * have a device that for whatever reason just doesn't
472 * want to play ball - it's not clear what's right
473 * here - should this be an error? should it just
474 * increase a counter, hmm. For right now, we return
475 * 0 - I don't believe that to be "right". we could
476 * call the gorpy openssl lib error handlers that
477 * print messages to users of the library. hmm..
480 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
485 put_dev_crypto(state->d_fd);
492 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
493 * gets called when libcrypto requests a cipher NID.
497 const EVP_CIPHER cryptodev_rc4 = {
500 EVP_CIPH_VARIABLE_LENGTH,
504 sizeof(struct dev_crypto_state),
511 const EVP_CIPHER cryptodev_des_cbc = {
518 sizeof(struct dev_crypto_state),
519 EVP_CIPHER_set_asn1_iv,
520 EVP_CIPHER_get_asn1_iv,
525 const EVP_CIPHER cryptodev_3des_cbc = {
532 sizeof(struct dev_crypto_state),
533 EVP_CIPHER_set_asn1_iv,
534 EVP_CIPHER_get_asn1_iv,
538 const EVP_CIPHER cryptodev_bf_cbc = {
545 sizeof(struct dev_crypto_state),
546 EVP_CIPHER_set_asn1_iv,
547 EVP_CIPHER_get_asn1_iv,
551 const EVP_CIPHER cryptodev_cast_cbc = {
558 sizeof(struct dev_crypto_state),
559 EVP_CIPHER_set_asn1_iv,
560 EVP_CIPHER_get_asn1_iv,
564 const EVP_CIPHER cryptodev_aes_cbc = {
571 sizeof(struct dev_crypto_state),
572 EVP_CIPHER_set_asn1_iv,
573 EVP_CIPHER_get_asn1_iv,
577 const EVP_CIPHER cryptodev_aes_192_cbc = {
584 sizeof(struct dev_crypto_state),
585 EVP_CIPHER_set_asn1_iv,
586 EVP_CIPHER_get_asn1_iv,
590 const EVP_CIPHER cryptodev_aes_256_cbc = {
597 sizeof(struct dev_crypto_state),
598 EVP_CIPHER_set_asn1_iv,
599 EVP_CIPHER_get_asn1_iv,
604 * Registered by the ENGINE when used to find out how to deal with
605 * a particular NID in the ENGINE. this says what we'll do at the
606 * top level - note, that list is restricted by what we answer with
609 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
610 const int **nids, int nid)
613 return (cryptodev_usable_ciphers(nids));
617 *cipher = &cryptodev_rc4;
619 case NID_des_ede3_cbc:
620 *cipher = &cryptodev_3des_cbc;
623 *cipher = &cryptodev_des_cbc;
626 *cipher = &cryptodev_bf_cbc;
629 *cipher = &cryptodev_cast_cbc;
631 case NID_aes_128_cbc:
632 *cipher = &cryptodev_aes_cbc;
634 case NID_aes_192_cbc:
635 *cipher = &cryptodev_aes_192_cbc;
637 case NID_aes_256_cbc:
638 *cipher = &cryptodev_aes_256_cbc;
644 return (*cipher != NULL);
648 #ifdef USE_CRYPTODEV_DIGESTS
650 /* convert digest type to cryptodev */
652 digest_nid_to_cryptodev(int nid)
656 for (i = 0; digests[i].id; i++)
657 if (digests[i].nid == nid)
658 return (digests[i].id);
664 digest_key_length(int nid)
668 for (i = 0; digests[i].id; i++)
669 if (digests[i].nid == nid)
670 return digests[i].keylen;
675 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
677 struct dev_crypto_state *state = ctx->md_data;
678 struct session_op *sess = &state->d_sess;
681 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
682 printf("cryptodev_digest_init: Can't get digest \n");
686 memset(state, 0, sizeof(struct dev_crypto_state));
688 if ((state->d_fd = get_dev_crypto()) < 0) {
689 printf("cryptodev_digest_init: Can't get Dev \n");
693 sess->mackey = state->dummy_mac_key;
694 sess->mackeylen = digest_key_length(ctx->digest->type);
697 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
698 put_dev_crypto(state->d_fd);
700 printf("cryptodev_digest_init: Open session failed\n");
707 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
710 struct crypt_op cryp;
711 struct dev_crypto_state *state = ctx->md_data;
712 struct session_op *sess = &state->d_sess;
714 if (!data || state->d_fd < 0) {
715 printf("cryptodev_digest_update: illegal inputs \n");
723 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
724 /* if application doesn't support one buffer */
725 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
727 if (!state->mac_data) {
728 printf("cryptodev_digest_update: realloc failed\n");
732 memcpy(state->mac_data + state->mac_len, data, count);
733 state->mac_len += count;
738 memset(&cryp, 0, sizeof(cryp));
740 cryp.ses = sess->ses;
743 cryp.src = (caddr_t) data;
745 cryp.mac = (caddr_t) state->digest_res;
746 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
747 printf("cryptodev_digest_update: digest failed\n");
754 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
756 struct crypt_op cryp;
757 struct dev_crypto_state *state = ctx->md_data;
758 struct session_op *sess = &state->d_sess;
762 if (!md || state->d_fd < 0) {
763 printf("cryptodev_digest_final: illegal input\n");
767 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
768 /* if application doesn't support one buffer */
769 memset(&cryp, 0, sizeof(cryp));
770 cryp.ses = sess->ses;
772 cryp.len = state->mac_len;
773 cryp.src = state->mac_data;
775 cryp.mac = (caddr_t)md;
776 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
777 printf("cryptodev_digest_final: digest failed\n");
784 memcpy(md, state->digest_res, ctx->digest->md_size);
790 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
793 struct dev_crypto_state *state = ctx->md_data;
794 struct session_op *sess = &state->d_sess;
799 if (state->d_fd < 0) {
800 printf("cryptodev_digest_cleanup: illegal input\n");
804 if (state->mac_data) {
805 OPENSSL_free(state->mac_data);
806 state->mac_data = NULL;
810 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
811 printf("cryptodev_digest_cleanup: failed to close session\n");
816 put_dev_crypto(state->d_fd);
822 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
824 struct dev_crypto_state *fstate = from->md_data;
825 struct dev_crypto_state *dstate = to->md_data;
826 struct session_op *sess;
829 if (dstate == NULL || fstate == NULL)
832 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
834 sess = &dstate->d_sess;
836 digest = digest_nid_to_cryptodev(to->digest->type);
838 sess->mackey = dstate->dummy_mac_key;
839 sess->mackeylen = digest_key_length(to->digest->type);
842 dstate->d_fd = get_dev_crypto();
844 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
845 put_dev_crypto(dstate->d_fd);
847 printf("cryptodev_digest_init: Open session failed\n");
851 if (fstate->mac_len != 0) {
852 if (fstate->mac_data != NULL)
854 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
855 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
856 dstate->mac_len = fstate->mac_len;
864 const EVP_MD cryptodev_sha1 = {
869 cryptodev_digest_init,
870 cryptodev_digest_update,
871 cryptodev_digest_final,
872 cryptodev_digest_copy,
873 cryptodev_digest_cleanup,
874 EVP_PKEY_NULL_method,
876 sizeof(struct dev_crypto_state),
879 const EVP_MD cryptodev_md5 = {
882 16 /* MD5_DIGEST_LENGTH */,
884 cryptodev_digest_init,
885 cryptodev_digest_update,
886 cryptodev_digest_final,
887 cryptodev_digest_copy,
888 cryptodev_digest_cleanup,
889 EVP_PKEY_NULL_method,
891 sizeof(struct dev_crypto_state),
894 #endif /* USE_CRYPTODEV_DIGESTS */
898 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
899 const int **nids, int nid)
902 return (cryptodev_usable_digests(nids));
905 #ifdef USE_CRYPTODEV_DIGESTS
907 *digest = &cryptodev_md5;
910 *digest = &cryptodev_sha1;
913 #endif /* USE_CRYPTODEV_DIGESTS */
917 return (*digest != NULL);
921 * Convert a BIGNUM to the representation that /dev/crypto needs.
922 * Upon completion of use, the caller is responsible for freeing
926 bn2crparam(const BIGNUM *a, struct crparam *crp)
935 bits = BN_num_bits(a);
936 bytes = (bits + 7) / 8;
943 crp->crp_p = (caddr_t) b;
944 crp->crp_nbits = bits;
946 for (i = 0, j = 0; i < a->top; i++) {
947 for (k = 0; k < BN_BITS2 / 8; k++) {
948 if ((j + k) >= bytes)
950 b[j + k] = a->d[i] >> (k * 8);
957 /* Convert a /dev/crypto parameter to a BIGNUM */
959 crparam2bn(struct crparam *crp, BIGNUM *a)
964 bytes = (crp->crp_nbits + 7) / 8;
969 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
972 for (i = 0; i < bytes; i++)
973 pd[i] = crp->crp_p[bytes - i - 1];
975 BN_bin2bn(pd, bytes, a);
982 zapparams(struct crypt_kop *kop)
986 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
987 if (kop->crk_param[i].crp_p)
988 free(kop->crk_param[i].crp_p);
989 kop->crk_param[i].crp_p = NULL;
990 kop->crk_param[i].crp_nbits = 0;
995 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
999 if ((fd = get_asym_dev_crypto()) < 0)
1003 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1004 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1008 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1009 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1013 if (ioctl(fd, CIOCKEY, kop) == 0) {
1015 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1017 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1025 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1026 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1028 struct crypt_kop kop;
1031 /* Currently, we know we can do mod exp iff we can do any
1032 * asymmetric operations at all.
1034 if (cryptodev_asymfeat == 0) {
1035 ret = BN_mod_exp(r, a, p, m, ctx);
1039 memset(&kop, 0, sizeof kop);
1040 kop.crk_op = CRK_MOD_EXP;
1042 /* inputs: a^p % m */
1043 if (bn2crparam(a, &kop.crk_param[0]))
1045 if (bn2crparam(p, &kop.crk_param[1]))
1047 if (bn2crparam(m, &kop.crk_param[2]))
1049 kop.crk_iparams = 3;
1051 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1052 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1053 printf("OCF asym process failed, Running in software\n");
1054 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1056 } else if (ECANCELED == kop.crk_status) {
1057 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1058 printf("OCF hardware operation cancelled. Running in Software\n");
1059 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1061 /* else cryptodev operation worked ok ==> ret = 1*/
1069 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1073 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1079 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1081 struct crypt_kop kop;
1084 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1085 /* XXX 0 means failure?? */
1089 memset(&kop, 0, sizeof kop);
1090 kop.crk_op = CRK_MOD_EXP_CRT;
1091 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1092 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1094 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1096 if (bn2crparam(I, &kop.crk_param[2]))
1098 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1100 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1102 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1104 kop.crk_iparams = 6;
1106 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1107 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1108 printf("OCF asym process failed, running in Software\n");
1109 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1111 } else if (ECANCELED == kop.crk_status) {
1112 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1113 printf("OCF hardware operation cancelled. Running in Software\n");
1114 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1116 /* else cryptodev operation worked ok ==> ret = 1*/
1123 static RSA_METHOD cryptodev_rsa = {
1124 "cryptodev RSA method",
1125 NULL, /* rsa_pub_enc */
1126 NULL, /* rsa_pub_dec */
1127 NULL, /* rsa_priv_enc */
1128 NULL, /* rsa_priv_dec */
1134 NULL, /* app_data */
1135 NULL, /* rsa_sign */
1136 NULL /* rsa_verify */
1140 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1141 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1143 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1147 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1148 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1149 BN_CTX *ctx, BN_MONT_CTX *mont)
1156 /* v = ( g^u1 * y^u2 mod p ) mod q */
1157 /* let t1 = g ^ u1 mod p */
1160 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1163 /* let t2 = y ^ u2 mod p */
1164 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1166 /* let u1 = t1 * t2 mod p */
1167 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1179 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1181 struct crypt_kop kop;
1182 BIGNUM *r = NULL, *s = NULL;
1183 DSA_SIG *dsaret = NULL;
1185 if ((r = BN_new()) == NULL)
1187 if ((s = BN_new()) == NULL) {
1192 memset(&kop, 0, sizeof kop);
1193 kop.crk_op = CRK_DSA_SIGN;
1195 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1196 kop.crk_param[0].crp_p = (caddr_t)dgst;
1197 kop.crk_param[0].crp_nbits = dlen * 8;
1198 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1200 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1202 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1204 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1206 kop.crk_iparams = 5;
1208 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1209 BN_num_bytes(dsa->q), s) == 0) {
1210 dsaret = DSA_SIG_new();
1214 const DSA_METHOD *meth = DSA_OpenSSL();
1217 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1220 kop.crk_param[0].crp_p = NULL;
1226 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1227 DSA_SIG *sig, DSA *dsa)
1229 struct crypt_kop kop;
1232 memset(&kop, 0, sizeof kop);
1233 kop.crk_op = CRK_DSA_VERIFY;
1235 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1236 kop.crk_param[0].crp_p = (caddr_t)dgst;
1237 kop.crk_param[0].crp_nbits = dlen * 8;
1238 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1240 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1242 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1244 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1246 if (bn2crparam(sig->r, &kop.crk_param[5]))
1248 if (bn2crparam(sig->s, &kop.crk_param[6]))
1250 kop.crk_iparams = 7;
1252 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1253 /*OCF success value is 0, if not zero, change dsaret to fail*/
1254 if(0 != kop.crk_status) dsaret = 0;
1256 const DSA_METHOD *meth = DSA_OpenSSL();
1258 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1261 kop.crk_param[0].crp_p = NULL;
1266 static DSA_METHOD cryptodev_dsa = {
1267 "cryptodev DSA method",
1269 NULL, /* dsa_sign_setup */
1271 NULL, /* dsa_mod_exp */
1280 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1281 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1284 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1288 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1290 struct crypt_kop kop;
1294 if ((fd = get_asym_dev_crypto()) < 0) {
1295 const DH_METHOD *meth = DH_OpenSSL();
1297 return ((meth->compute_key)(key, pub_key, dh));
1300 keylen = BN_num_bits(dh->p);
1302 memset(&kop, 0, sizeof kop);
1303 kop.crk_op = CRK_DH_COMPUTE_KEY;
1305 /* inputs: dh->priv_key pub_key dh->p key */
1306 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1308 if (bn2crparam(pub_key, &kop.crk_param[1]))
1310 if (bn2crparam(dh->p, &kop.crk_param[2]))
1312 kop.crk_iparams = 3;
1314 kop.crk_param[3].crp_p = (caddr_t) key;
1315 kop.crk_param[3].crp_nbits = keylen * 8;
1316 kop.crk_oparams = 1;
1318 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1319 const DH_METHOD *meth = DH_OpenSSL();
1321 dhret = (meth->compute_key)(key, pub_key, dh);
1324 kop.crk_param[3].crp_p = NULL;
1329 static DH_METHOD cryptodev_dh = {
1330 "cryptodev DH method",
1331 NULL, /* cryptodev_dh_generate_key */
1341 * ctrl right now is just a wrapper that doesn't do much
1342 * but I expect we'll want some options soon.
1345 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1347 #ifdef HAVE_SYSLOG_R
1348 struct syslog_data sd = SYSLOG_DATA_INIT;
1353 #ifdef HAVE_SYSLOG_R
1354 syslog_r(LOG_ERR, &sd,
1355 "cryptodev_ctrl: unknown command %d", cmd);
1357 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1365 ENGINE_load_cryptodev(void)
1367 ENGINE *engine = ENGINE_new();
1372 if ((fd = get_dev_crypto()) < 0) {
1373 ENGINE_free(engine);
1378 * find out what asymmetric crypto algorithms we support
1380 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1382 ENGINE_free(engine);
1387 if (!ENGINE_set_id(engine, "cryptodev") ||
1388 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1389 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1390 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1391 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1392 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1393 ENGINE_free(engine);
1397 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1398 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1400 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1401 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1402 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1403 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1404 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1405 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1406 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1407 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1408 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1409 cryptodev_rsa.rsa_mod_exp =
1410 cryptodev_rsa_mod_exp;
1412 cryptodev_rsa.rsa_mod_exp =
1413 cryptodev_rsa_nocrt_mod_exp;
1417 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1418 const DSA_METHOD *meth = DSA_OpenSSL();
1420 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1421 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1422 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1423 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1424 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1425 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1427 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1428 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1431 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1432 const DH_METHOD *dh_meth = DH_OpenSSL();
1434 cryptodev_dh.generate_key = dh_meth->generate_key;
1435 cryptodev_dh.compute_key = dh_meth->compute_key;
1436 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1437 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1438 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1439 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1440 cryptodev_dh.compute_key =
1441 cryptodev_dh_compute_key;
1446 ENGINE_free(engine);
1450 #endif /* HAVE_CRYPTODEV */