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];
87 static u_int32_t cryptodev_asymfeat = 0;
89 static int get_asym_dev_crypto(void);
90 static int open_dev_crypto(void);
91 static int get_dev_crypto(void);
92 static int get_cryptodev_ciphers(const int **cnids);
93 #ifdef USE_CRYPTODEV_DIGESTS
94 static int get_cryptodev_digests(const int **cnids);
96 static int cryptodev_usable_ciphers(const int **nids);
97 static int cryptodev_usable_digests(const int **nids);
98 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
99 const unsigned char *in, size_t inl);
100 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
101 const unsigned char *iv, int enc);
102 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
103 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
104 const int **nids, int nid);
105 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
106 const int **nids, int nid);
107 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
108 static int crparam2bn(struct crparam *crp, BIGNUM *a);
109 static void zapparams(struct crypt_kop *kop);
110 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
111 int slen, BIGNUM *s);
113 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
114 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
115 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
116 RSA *rsa, BN_CTX *ctx);
117 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
118 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
119 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
120 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
121 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
122 BN_CTX *ctx, BN_MONT_CTX *mont);
123 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
125 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
126 DSA_SIG *sig, DSA *dsa);
127 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
128 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
130 static int cryptodev_dh_compute_key(unsigned char *key,
131 const BIGNUM *pub_key, DH *dh);
132 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
134 void ENGINE_load_cryptodev(void);
136 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
146 { CRYPTO_ARC4, NID_rc4, 0, 16, },
147 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
148 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
149 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
150 { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
151 { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
152 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
153 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
154 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
155 { 0, NID_undef, 0, 0, },
158 #ifdef USE_CRYPTODEV_DIGESTS
164 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
165 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
166 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
167 { CRYPTO_MD5_KPDK, NID_undef, 0},
168 { CRYPTO_SHA1_KPDK, NID_undef, 0},
169 { CRYPTO_MD5, NID_md5, 16},
170 { CRYPTO_SHA1, NID_sha1, 20},
176 * Return a fd if /dev/crypto seems usable, 0 otherwise.
179 open_dev_crypto(void)
184 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
187 if (fcntl(fd, F_SETFD, 1) == -1) {
201 if ((fd = open_dev_crypto()) == -1)
203 if (ioctl(fd, CRIOGET, &retfd) == -1)
207 if (fcntl(retfd, F_SETFD, 1) == -1) {
214 /* Caching version for asym operations */
216 get_asym_dev_crypto(void)
221 fd = get_dev_crypto();
226 * Find out what ciphers /dev/crypto will let us have a session for.
227 * XXX note, that some of these openssl doesn't deal with yet!
228 * returning them here is harmless, as long as we return NULL
229 * when asked for a handler in the cryptodev_engine_ciphers routine
232 get_cryptodev_ciphers(const int **cnids)
234 static int nids[CRYPTO_ALGORITHM_MAX];
235 struct session_op sess;
236 int fd, i, count = 0;
238 if ((fd = get_dev_crypto()) < 0) {
242 memset(&sess, 0, sizeof(sess));
243 sess.key = (caddr_t)"123456789abcdefghijklmno";
245 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
246 if (ciphers[i].nid == NID_undef)
248 sess.cipher = ciphers[i].id;
249 sess.keylen = ciphers[i].keylen;
251 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
252 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
253 nids[count++] = ciphers[i].nid;
264 #ifdef USE_CRYPTODEV_DIGESTS
266 * Find out what digests /dev/crypto will let us have a session for.
267 * XXX note, that some of these openssl doesn't deal with yet!
268 * returning them here is harmless, as long as we return NULL
269 * when asked for a handler in the cryptodev_engine_digests routine
272 get_cryptodev_digests(const int **cnids)
274 static int nids[CRYPTO_ALGORITHM_MAX];
275 struct session_op sess;
276 int fd, i, count = 0;
278 if ((fd = get_dev_crypto()) < 0) {
282 memset(&sess, 0, sizeof(sess));
283 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
284 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
285 if (digests[i].nid == NID_undef)
287 sess.mac = digests[i].id;
288 sess.mackeylen = digests[i].keylen;
290 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
291 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
292 nids[count++] = digests[i].nid;
305 * Find the useable ciphers|digests from dev/crypto - this is the first
306 * thing called by the engine init crud which determines what it
307 * can use for ciphers from this engine. We want to return
308 * only what we can do, anythine else is handled by software.
310 * If we can't initialize the device to do anything useful for
311 * any reason, we want to return a NULL array, and 0 length,
312 * which forces everything to be done is software. By putting
313 * the initalization of the device in here, we ensure we can
314 * use this engine as the default, and if for whatever reason
315 * /dev/crypto won't do what we want it will just be done in
318 * This can (should) be greatly expanded to perhaps take into
319 * account speed of the device, and what we want to do.
320 * (although the disabling of particular alg's could be controlled
321 * by the device driver with sysctl's.) - this is where we
322 * want most of the decisions made about what we actually want
323 * to use from /dev/crypto.
326 cryptodev_usable_ciphers(const int **nids)
328 return (get_cryptodev_ciphers(nids));
332 cryptodev_usable_digests(const int **nids)
334 #ifdef USE_CRYPTODEV_DIGESTS
335 return (get_cryptodev_digests(nids));
338 * XXXX just disable all digests for now, because it sucks.
339 * we need a better way to decide this - i.e. I may not
340 * want digests on slow cards like hifn on fast machines,
341 * but might want them on slow or loaded machines, etc.
342 * will also want them when using crypto cards that don't
343 * suck moose gonads - would be nice to be able to decide something
344 * as reasonable default without having hackery that's card dependent.
345 * of course, the default should probably be just do everything,
346 * with perhaps a sysctl to turn algoritms off (or have them off
347 * by default) on cards that generally suck like the hifn.
355 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
356 const unsigned char *in, size_t inl)
358 struct crypt_op cryp;
359 struct dev_crypto_state *state = ctx->cipher_data;
360 struct session_op *sess = &state->d_sess;
362 unsigned char save_iv[EVP_MAX_IV_LENGTH];
368 if ((inl % ctx->cipher->block_size) != 0)
371 memset(&cryp, 0, sizeof(cryp));
373 cryp.ses = sess->ses;
376 cryp.src = (caddr_t) in;
377 cryp.dst = (caddr_t) out;
380 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
382 if (ctx->cipher->iv_len) {
383 cryp.iv = (caddr_t) ctx->iv;
385 iiv = in + inl - ctx->cipher->iv_len;
386 memcpy(save_iv, iiv, ctx->cipher->iv_len);
391 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
392 /* XXX need better errror handling
393 * this can fail for a number of different reasons.
398 if (ctx->cipher->iv_len) {
400 iiv = out + inl - ctx->cipher->iv_len;
403 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
409 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
410 const unsigned char *iv, int enc)
412 struct dev_crypto_state *state = ctx->cipher_data;
413 struct session_op *sess = &state->d_sess;
416 for (i = 0; ciphers[i].id; i++)
417 if (ctx->cipher->nid == ciphers[i].nid &&
418 ctx->cipher->iv_len <= ciphers[i].ivmax &&
419 ctx->key_len == ciphers[i].keylen) {
420 cipher = ciphers[i].id;
424 if (!ciphers[i].id) {
429 memset(sess, 0, sizeof(struct session_op));
431 if ((state->d_fd = get_dev_crypto()) < 0)
434 sess->key = (caddr_t)key;
435 sess->keylen = ctx->key_len;
436 sess->cipher = cipher;
438 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
447 * free anything we allocated earlier when initting a
448 * session, and close the session.
451 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
454 struct dev_crypto_state *state = ctx->cipher_data;
455 struct session_op *sess = &state->d_sess;
460 /* XXX if this ioctl fails, someting's wrong. the invoker
461 * may have called us with a bogus ctx, or we could
462 * have a device that for whatever reason just doesn't
463 * want to play ball - it's not clear what's right
464 * here - should this be an error? should it just
465 * increase a counter, hmm. For right now, we return
466 * 0 - I don't believe that to be "right". we could
467 * call the gorpy openssl lib error handlers that
468 * print messages to users of the library. hmm..
471 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
483 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
484 * gets called when libcrypto requests a cipher NID.
488 const EVP_CIPHER cryptodev_rc4 = {
491 EVP_CIPH_VARIABLE_LENGTH,
495 sizeof(struct dev_crypto_state),
502 const EVP_CIPHER cryptodev_des_cbc = {
509 sizeof(struct dev_crypto_state),
510 EVP_CIPHER_set_asn1_iv,
511 EVP_CIPHER_get_asn1_iv,
516 const EVP_CIPHER cryptodev_3des_cbc = {
523 sizeof(struct dev_crypto_state),
524 EVP_CIPHER_set_asn1_iv,
525 EVP_CIPHER_get_asn1_iv,
529 const EVP_CIPHER cryptodev_bf_cbc = {
536 sizeof(struct dev_crypto_state),
537 EVP_CIPHER_set_asn1_iv,
538 EVP_CIPHER_get_asn1_iv,
542 const EVP_CIPHER cryptodev_cast_cbc = {
549 sizeof(struct dev_crypto_state),
550 EVP_CIPHER_set_asn1_iv,
551 EVP_CIPHER_get_asn1_iv,
555 const EVP_CIPHER cryptodev_aes_cbc = {
562 sizeof(struct dev_crypto_state),
563 EVP_CIPHER_set_asn1_iv,
564 EVP_CIPHER_get_asn1_iv,
568 const EVP_CIPHER cryptodev_aes_192_cbc = {
575 sizeof(struct dev_crypto_state),
576 EVP_CIPHER_set_asn1_iv,
577 EVP_CIPHER_get_asn1_iv,
581 const EVP_CIPHER cryptodev_aes_256_cbc = {
588 sizeof(struct dev_crypto_state),
589 EVP_CIPHER_set_asn1_iv,
590 EVP_CIPHER_get_asn1_iv,
595 * Registered by the ENGINE when used to find out how to deal with
596 * a particular NID in the ENGINE. this says what we'll do at the
597 * top level - note, that list is restricted by what we answer with
600 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
601 const int **nids, int nid)
604 return (cryptodev_usable_ciphers(nids));
608 *cipher = &cryptodev_rc4;
610 case NID_des_ede3_cbc:
611 *cipher = &cryptodev_3des_cbc;
614 *cipher = &cryptodev_des_cbc;
617 *cipher = &cryptodev_bf_cbc;
620 *cipher = &cryptodev_cast_cbc;
622 case NID_aes_128_cbc:
623 *cipher = &cryptodev_aes_cbc;
625 case NID_aes_192_cbc:
626 *cipher = &cryptodev_aes_192_cbc;
628 case NID_aes_256_cbc:
629 *cipher = &cryptodev_aes_256_cbc;
635 return (*cipher != NULL);
639 #ifdef USE_CRYPTODEV_DIGESTS
641 /* convert digest type to cryptodev */
643 digest_nid_to_cryptodev(int nid)
647 for (i = 0; digests[i].id; i++)
648 if (digests[i].nid == nid)
649 return (digests[i].id);
655 digest_key_length(int nid)
659 for (i = 0; digests[i].id; i++)
660 if (digests[i].nid == nid)
661 return digests[i].keylen;
666 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
668 struct dev_crypto_state *state = ctx->md_data;
669 struct session_op *sess = &state->d_sess;
672 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
673 printf("cryptodev_digest_init: Can't get digest \n");
677 memset(state, 0, sizeof(struct dev_crypto_state));
679 if ((state->d_fd = get_dev_crypto()) < 0) {
680 printf("cryptodev_digest_init: Can't get Dev \n");
684 sess->mackey = state->dummy_mac_key;
685 sess->mackeylen = digest_key_length(ctx->digest->type);
688 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
691 printf("cryptodev_digest_init: Open session failed\n");
698 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
701 struct crypt_op cryp;
702 struct dev_crypto_state *state = ctx->md_data;
703 struct session_op *sess = &state->d_sess;
705 if (!data || state->d_fd < 0) {
706 printf("cryptodev_digest_update: illegal inputs \n");
714 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
715 /* if application doesn't support one buffer */
716 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
718 if (!state->mac_data) {
719 printf("cryptodev_digest_update: realloc failed\n");
723 memcpy(state->mac_data + state->mac_len, data, count);
724 state->mac_len += count;
729 memset(&cryp, 0, sizeof(cryp));
731 cryp.ses = sess->ses;
734 cryp.src = (caddr_t) data;
736 cryp.mac = (caddr_t) state->digest_res;
737 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
738 printf("cryptodev_digest_update: digest failed\n");
745 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
747 struct crypt_op cryp;
748 struct dev_crypto_state *state = ctx->md_data;
749 struct session_op *sess = &state->d_sess;
753 if (!md || state->d_fd < 0) {
754 printf("cryptodev_digest_final: illegal input\n");
758 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
759 /* if application doesn't support one buffer */
760 memset(&cryp, 0, sizeof(cryp));
762 cryp.ses = sess->ses;
764 cryp.len = state->mac_len;
765 cryp.src = state->mac_data;
767 cryp.mac = (caddr_t)md;
769 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
770 printf("cryptodev_digest_final: digest failed\n");
777 memcpy(md, state->digest_res, ctx->digest->md_size);
783 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
786 struct dev_crypto_state *state = ctx->md_data;
787 struct session_op *sess = &state->d_sess;
789 if (state->d_fd < 0) {
790 printf("cryptodev_digest_cleanup: illegal input\n");
794 if (state->mac_data) {
795 OPENSSL_free(state->mac_data);
796 state->mac_data = NULL;
803 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
804 printf("cryptodev_digest_cleanup: failed to close session\n");
815 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
817 struct dev_crypto_state *fstate = from->md_data;
818 struct dev_crypto_state *dstate = to->md_data;
820 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
822 if (fstate->mac_len != 0) {
823 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
824 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
833 const EVP_MD cryptodev_sha1 = {
838 cryptodev_digest_init,
839 cryptodev_digest_update,
840 cryptodev_digest_final,
841 cryptodev_digest_copy,
842 cryptodev_digest_cleanup,
843 EVP_PKEY_NULL_method,
845 sizeof(struct dev_crypto_state),
848 const EVP_MD cryptodev_md5 = {
851 16 /* MD5_DIGEST_LENGTH */,
853 cryptodev_digest_init,
854 cryptodev_digest_update,
855 cryptodev_digest_final,
856 cryptodev_digest_copy,
857 cryptodev_digest_cleanup,
858 EVP_PKEY_NULL_method,
860 sizeof(struct dev_crypto_state),
863 #endif /* USE_CRYPTODEV_DIGESTS */
867 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
868 const int **nids, int nid)
871 return (cryptodev_usable_digests(nids));
874 #ifdef USE_CRYPTODEV_DIGESTS
876 *digest = &cryptodev_md5;
879 *digest = &cryptodev_sha1;
882 #endif /* USE_CRYPTODEV_DIGESTS */
886 return (*digest != NULL);
890 * Convert a BIGNUM to the representation that /dev/crypto needs.
891 * Upon completion of use, the caller is responsible for freeing
895 bn2crparam(const BIGNUM *a, struct crparam *crp)
904 bits = BN_num_bits(a);
905 bytes = (bits + 7) / 8;
912 crp->crp_p = (caddr_t) b;
913 crp->crp_nbits = bits;
915 for (i = 0, j = 0; i < a->top; i++) {
916 for (k = 0; k < BN_BITS2 / 8; k++) {
917 if ((j + k) >= bytes)
919 b[j + k] = a->d[i] >> (k * 8);
926 /* Convert a /dev/crypto parameter to a BIGNUM */
928 crparam2bn(struct crparam *crp, BIGNUM *a)
933 bytes = (crp->crp_nbits + 7) / 8;
938 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
941 for (i = 0; i < bytes; i++)
942 pd[i] = crp->crp_p[bytes - i - 1];
944 BN_bin2bn(pd, bytes, a);
951 zapparams(struct crypt_kop *kop)
955 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
956 if (kop->crk_param[i].crp_p)
957 free(kop->crk_param[i].crp_p);
958 kop->crk_param[i].crp_p = NULL;
959 kop->crk_param[i].crp_nbits = 0;
964 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
968 if ((fd = get_asym_dev_crypto()) < 0)
972 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
973 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
977 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
978 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
982 if (ioctl(fd, CIOCKEY, kop) == 0) {
984 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
986 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
994 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
995 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
997 struct crypt_kop kop;
1000 /* Currently, we know we can do mod exp iff we can do any
1001 * asymmetric operations at all.
1003 if (cryptodev_asymfeat == 0) {
1004 ret = BN_mod_exp(r, a, p, m, ctx);
1008 memset(&kop, 0, sizeof kop);
1009 kop.crk_op = CRK_MOD_EXP;
1011 /* inputs: a^p % m */
1012 if (bn2crparam(a, &kop.crk_param[0]))
1014 if (bn2crparam(p, &kop.crk_param[1]))
1016 if (bn2crparam(m, &kop.crk_param[2]))
1018 kop.crk_iparams = 3;
1020 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1021 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1022 printf("OCF asym process failed, Running in software\n");
1023 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1025 } else if (ECANCELED == kop.crk_status) {
1026 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1027 printf("OCF hardware operation cancelled. Running in Software\n");
1028 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1030 /* else cryptodev operation worked ok ==> ret = 1*/
1038 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1042 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1048 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1050 struct crypt_kop kop;
1053 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1054 /* XXX 0 means failure?? */
1058 memset(&kop, 0, sizeof kop);
1059 kop.crk_op = CRK_MOD_EXP_CRT;
1060 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1061 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1063 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1065 if (bn2crparam(I, &kop.crk_param[2]))
1067 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1069 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1071 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1073 kop.crk_iparams = 6;
1075 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1076 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1077 printf("OCF asym process failed, running in Software\n");
1078 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1080 } else if (ECANCELED == kop.crk_status) {
1081 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1082 printf("OCF hardware operation cancelled. Running in Software\n");
1083 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1085 /* else cryptodev operation worked ok ==> ret = 1*/
1092 static RSA_METHOD cryptodev_rsa = {
1093 "cryptodev RSA method",
1094 NULL, /* rsa_pub_enc */
1095 NULL, /* rsa_pub_dec */
1096 NULL, /* rsa_priv_enc */
1097 NULL, /* rsa_priv_dec */
1103 NULL, /* app_data */
1104 NULL, /* rsa_sign */
1105 NULL /* rsa_verify */
1109 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1110 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1112 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1116 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1117 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1118 BN_CTX *ctx, BN_MONT_CTX *mont)
1125 /* v = ( g^u1 * y^u2 mod p ) mod q */
1126 /* let t1 = g ^ u1 mod p */
1129 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1132 /* let t2 = y ^ u2 mod p */
1133 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1135 /* let u1 = t1 * t2 mod p */
1136 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1148 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1150 struct crypt_kop kop;
1151 BIGNUM *r = NULL, *s = NULL;
1152 DSA_SIG *dsaret = NULL;
1154 if ((r = BN_new()) == NULL)
1156 if ((s = BN_new()) == NULL) {
1161 memset(&kop, 0, sizeof kop);
1162 kop.crk_op = CRK_DSA_SIGN;
1164 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1165 kop.crk_param[0].crp_p = (caddr_t)dgst;
1166 kop.crk_param[0].crp_nbits = dlen * 8;
1167 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1169 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1171 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1173 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1175 kop.crk_iparams = 5;
1177 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1178 BN_num_bytes(dsa->q), s) == 0) {
1179 dsaret = DSA_SIG_new();
1183 const DSA_METHOD *meth = DSA_OpenSSL();
1186 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1189 kop.crk_param[0].crp_p = NULL;
1195 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1196 DSA_SIG *sig, DSA *dsa)
1198 struct crypt_kop kop;
1201 memset(&kop, 0, sizeof kop);
1202 kop.crk_op = CRK_DSA_VERIFY;
1204 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1205 kop.crk_param[0].crp_p = (caddr_t)dgst;
1206 kop.crk_param[0].crp_nbits = dlen * 8;
1207 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1209 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1211 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1213 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1215 if (bn2crparam(sig->r, &kop.crk_param[5]))
1217 if (bn2crparam(sig->s, &kop.crk_param[6]))
1219 kop.crk_iparams = 7;
1221 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1222 /*OCF success value is 0, if not zero, change dsaret to fail*/
1223 if(0 != kop.crk_status) dsaret = 0;
1225 const DSA_METHOD *meth = DSA_OpenSSL();
1227 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1230 kop.crk_param[0].crp_p = NULL;
1235 static DSA_METHOD cryptodev_dsa = {
1236 "cryptodev DSA method",
1238 NULL, /* dsa_sign_setup */
1240 NULL, /* dsa_mod_exp */
1249 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1250 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1253 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1257 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1259 struct crypt_kop kop;
1263 if ((fd = get_asym_dev_crypto()) < 0) {
1264 const DH_METHOD *meth = DH_OpenSSL();
1266 return ((meth->compute_key)(key, pub_key, dh));
1269 keylen = BN_num_bits(dh->p);
1271 memset(&kop, 0, sizeof kop);
1272 kop.crk_op = CRK_DH_COMPUTE_KEY;
1274 /* inputs: dh->priv_key pub_key dh->p key */
1275 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1277 if (bn2crparam(pub_key, &kop.crk_param[1]))
1279 if (bn2crparam(dh->p, &kop.crk_param[2]))
1281 kop.crk_iparams = 3;
1283 kop.crk_param[3].crp_p = (caddr_t) key;
1284 kop.crk_param[3].crp_nbits = keylen * 8;
1285 kop.crk_oparams = 1;
1287 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1288 const DH_METHOD *meth = DH_OpenSSL();
1290 dhret = (meth->compute_key)(key, pub_key, dh);
1293 kop.crk_param[3].crp_p = NULL;
1298 static DH_METHOD cryptodev_dh = {
1299 "cryptodev DH method",
1300 NULL, /* cryptodev_dh_generate_key */
1310 * ctrl right now is just a wrapper that doesn't do much
1311 * but I expect we'll want some options soon.
1314 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1316 #ifdef HAVE_SYSLOG_R
1317 struct syslog_data sd = SYSLOG_DATA_INIT;
1322 #ifdef HAVE_SYSLOG_R
1323 syslog_r(LOG_ERR, &sd,
1324 "cryptodev_ctrl: unknown command %d", cmd);
1326 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1334 ENGINE_load_cryptodev(void)
1336 ENGINE *engine = ENGINE_new();
1341 if ((fd = get_dev_crypto()) < 0) {
1342 ENGINE_free(engine);
1347 * find out what asymmetric crypto algorithms we support
1349 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1351 ENGINE_free(engine);
1356 if (!ENGINE_set_id(engine, "cryptodev") ||
1357 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1358 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1359 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1360 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1361 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1362 ENGINE_free(engine);
1366 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1367 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1369 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1370 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1371 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1372 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1373 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1374 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1375 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1376 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1377 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1378 cryptodev_rsa.rsa_mod_exp =
1379 cryptodev_rsa_mod_exp;
1381 cryptodev_rsa.rsa_mod_exp =
1382 cryptodev_rsa_nocrt_mod_exp;
1386 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1387 const DSA_METHOD *meth = DSA_OpenSSL();
1389 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1390 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1391 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1392 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1393 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1394 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1396 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1397 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1400 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1401 const DH_METHOD *dh_meth = DH_OpenSSL();
1403 cryptodev_dh.generate_key = dh_meth->generate_key;
1404 cryptodev_dh.compute_key = dh_meth->compute_key;
1405 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1406 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1407 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1408 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1409 cryptodev_dh.compute_key =
1410 cryptodev_dh_compute_key;
1415 ENGINE_free(engine);
1419 #endif /* HAVE_CRYPTODEV */