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 /*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, },
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 if (ioctl(fd, CRIOGET, &retfd) == -1)
205 if (fcntl(retfd, F_SETFD, 1) == -1) {
212 /* Caching version for asym operations */
214 get_asym_dev_crypto(void)
219 fd = get_dev_crypto();
224 * Find out what ciphers /dev/crypto will let us have a session for.
225 * XXX note, that some of these openssl doesn't deal with yet!
226 * returning them here is harmless, as long as we return NULL
227 * when asked for a handler in the cryptodev_engine_ciphers routine
230 get_cryptodev_ciphers(const int **cnids)
232 static int nids[CRYPTO_ALGORITHM_MAX];
233 struct session_op sess;
234 int fd, i, count = 0;
236 if ((fd = get_dev_crypto()) < 0) {
240 memset(&sess, 0, sizeof(sess));
241 sess.key = (caddr_t)"123456789abcdefghijklmno";
243 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
244 if (ciphers[i].nid == NID_undef)
246 sess.cipher = ciphers[i].id;
247 sess.keylen = ciphers[i].keylen;
249 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
250 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
251 nids[count++] = ciphers[i].nid;
263 * Find out what digests /dev/crypto will let us have a session for.
264 * XXX note, that some of these openssl doesn't deal with yet!
265 * returning them here is harmless, as long as we return NULL
266 * when asked for a handler in the cryptodev_engine_digests routine
268 #if 0 /* not (yet?) used */
270 get_cryptodev_digests(const int **cnids)
272 static int nids[CRYPTO_ALGORITHM_MAX];
273 struct session_op sess;
274 int fd, i, count = 0;
276 if ((fd = get_dev_crypto()) < 0) {
280 memset(&sess, 0, sizeof(sess));
281 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
282 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
283 if (digests[i].nid == NID_undef)
285 sess.mac = digests[i].id;
286 sess.mackeylen = digests[i].keylen;
288 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
289 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
290 nids[count++] = digests[i].nid;
303 * Find the useable ciphers|digests from dev/crypto - this is the first
304 * thing called by the engine init crud which determines what it
305 * can use for ciphers from this engine. We want to return
306 * only what we can do, anythine else is handled by software.
308 * If we can't initialize the device to do anything useful for
309 * any reason, we want to return a NULL array, and 0 length,
310 * which forces everything to be done is software. By putting
311 * the initalization of the device in here, we ensure we can
312 * use this engine as the default, and if for whatever reason
313 * /dev/crypto won't do what we want it will just be done in
316 * This can (should) be greatly expanded to perhaps take into
317 * account speed of the device, and what we want to do.
318 * (although the disabling of particular alg's could be controlled
319 * by the device driver with sysctl's.) - this is where we
320 * want most of the decisions made about what we actually want
321 * to use from /dev/crypto.
324 cryptodev_usable_ciphers(const int **nids)
326 return (get_cryptodev_ciphers(nids));
330 cryptodev_usable_digests(const int **nids)
332 #ifdef USE_CRYPTODEV_DIGESTS
333 return (get_cryptodev_digests(nids));
336 * XXXX just disable all digests for now, because it sucks.
337 * we need a better way to decide this - i.e. I may not
338 * want digests on slow cards like hifn on fast machines,
339 * but might want them on slow or loaded machines, etc.
340 * will also want them when using crypto cards that don't
341 * suck moose gonads - would be nice to be able to decide something
342 * as reasonable default without having hackery that's card dependent.
343 * of course, the default should probably be just do everything,
344 * with perhaps a sysctl to turn algoritms off (or have them off
345 * by default) on cards that generally suck like the hifn.
353 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
354 const unsigned char *in, size_t inl)
356 struct crypt_op cryp;
357 struct dev_crypto_state *state = ctx->cipher_data;
358 struct session_op *sess = &state->d_sess;
360 unsigned char save_iv[EVP_MAX_IV_LENGTH];
366 if ((inl % ctx->cipher->block_size) != 0)
369 memset(&cryp, 0, sizeof(cryp));
371 cryp.ses = sess->ses;
374 cryp.src = (caddr_t) in;
375 cryp.dst = (caddr_t) out;
378 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
380 if (ctx->cipher->iv_len) {
381 cryp.iv = (caddr_t) ctx->iv;
383 iiv = in + inl - ctx->cipher->iv_len;
384 memcpy(save_iv, iiv, ctx->cipher->iv_len);
389 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
390 /* XXX need better errror handling
391 * this can fail for a number of different reasons.
396 if (ctx->cipher->iv_len) {
398 iiv = out + inl - ctx->cipher->iv_len;
401 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
407 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
408 const unsigned char *iv, int enc)
410 struct dev_crypto_state *state = ctx->cipher_data;
411 struct session_op *sess = &state->d_sess;
414 for (i = 0; ciphers[i].id; i++)
415 if (ctx->cipher->nid == ciphers[i].nid &&
416 ctx->cipher->iv_len <= ciphers[i].ivmax &&
417 ctx->key_len == ciphers[i].keylen) {
418 cipher = ciphers[i].id;
422 if (!ciphers[i].id) {
427 memset(sess, 0, sizeof(struct session_op));
429 if ((state->d_fd = get_dev_crypto()) < 0)
432 sess->key = (caddr_t)key;
433 sess->keylen = ctx->key_len;
434 sess->cipher = cipher;
436 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
445 * free anything we allocated earlier when initting a
446 * session, and close the session.
449 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
452 struct dev_crypto_state *state = ctx->cipher_data;
453 struct session_op *sess = &state->d_sess;
458 /* XXX if this ioctl fails, someting's wrong. the invoker
459 * may have called us with a bogus ctx, or we could
460 * have a device that for whatever reason just doesn't
461 * want to play ball - it's not clear what's right
462 * here - should this be an error? should it just
463 * increase a counter, hmm. For right now, we return
464 * 0 - I don't believe that to be "right". we could
465 * call the gorpy openssl lib error handlers that
466 * print messages to users of the library. hmm..
469 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
481 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
482 * gets called when libcrypto requests a cipher NID.
486 const EVP_CIPHER cryptodev_rc4 = {
489 EVP_CIPH_VARIABLE_LENGTH,
493 sizeof(struct dev_crypto_state),
500 const EVP_CIPHER cryptodev_des_cbc = {
507 sizeof(struct dev_crypto_state),
508 EVP_CIPHER_set_asn1_iv,
509 EVP_CIPHER_get_asn1_iv,
514 const EVP_CIPHER cryptodev_3des_cbc = {
521 sizeof(struct dev_crypto_state),
522 EVP_CIPHER_set_asn1_iv,
523 EVP_CIPHER_get_asn1_iv,
527 const EVP_CIPHER cryptodev_bf_cbc = {
534 sizeof(struct dev_crypto_state),
535 EVP_CIPHER_set_asn1_iv,
536 EVP_CIPHER_get_asn1_iv,
540 const EVP_CIPHER cryptodev_cast_cbc = {
547 sizeof(struct dev_crypto_state),
548 EVP_CIPHER_set_asn1_iv,
549 EVP_CIPHER_get_asn1_iv,
553 const EVP_CIPHER cryptodev_aes_cbc = {
560 sizeof(struct dev_crypto_state),
561 EVP_CIPHER_set_asn1_iv,
562 EVP_CIPHER_get_asn1_iv,
566 const EVP_CIPHER cryptodev_aes_192_cbc = {
573 sizeof(struct dev_crypto_state),
574 EVP_CIPHER_set_asn1_iv,
575 EVP_CIPHER_get_asn1_iv,
579 const EVP_CIPHER cryptodev_aes_256_cbc = {
586 sizeof(struct dev_crypto_state),
587 EVP_CIPHER_set_asn1_iv,
588 EVP_CIPHER_get_asn1_iv,
593 * Registered by the ENGINE when used to find out how to deal with
594 * a particular NID in the ENGINE. this says what we'll do at the
595 * top level - note, that list is restricted by what we answer with
598 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
599 const int **nids, int nid)
602 return (cryptodev_usable_ciphers(nids));
606 *cipher = &cryptodev_rc4;
608 case NID_des_ede3_cbc:
609 *cipher = &cryptodev_3des_cbc;
612 *cipher = &cryptodev_des_cbc;
615 *cipher = &cryptodev_bf_cbc;
618 *cipher = &cryptodev_cast_cbc;
620 case NID_aes_128_cbc:
621 *cipher = &cryptodev_aes_cbc;
623 case NID_aes_192_cbc:
624 *cipher = &cryptodev_aes_192_cbc;
626 case NID_aes_256_cbc:
627 *cipher = &cryptodev_aes_256_cbc;
633 return (*cipher != NULL);
637 #ifdef USE_CRYPTODEV_DIGESTS
639 /* convert digest type to cryptodev */
641 digest_nid_to_cryptodev(int nid)
645 for (i = 0; digests[i].id; i++)
646 if (digests[i].nid == nid)
647 return (digests[i].id);
653 digest_key_length(int nid)
657 for (i = 0; digests[i].id; i++)
658 if (digests[i].nid == nid)
659 return digests[i].keylen;
664 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
666 struct dev_crypto_state *state = ctx->md_data;
667 struct session_op *sess = &state->d_sess;
670 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
671 printf("cryptodev_digest_init: Can't get digest \n");
675 memset(state, 0, sizeof(struct dev_crypto_state));
677 if ((state->d_fd = get_dev_crypto()) < 0) {
678 printf("cryptodev_digest_init: Can't get Dev \n");
682 sess->mackey = state->dummy_mac_key;
683 sess->mackeylen = digest_key_length(ctx->digest->type);
686 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
689 printf("cryptodev_digest_init: Open session failed\n");
696 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
699 struct crypt_op cryp;
700 struct dev_crypto_state *state = ctx->md_data;
701 struct session_op *sess = &state->d_sess;
703 if (!data || state->d_fd < 0) {
704 printf("cryptodev_digest_update: illegal inputs \n");
712 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
713 /* if application doesn't support one buffer */
714 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
716 if (!state->mac_data) {
717 printf("cryptodev_digest_update: realloc failed\n");
721 memcpy(state->mac_data + state->mac_len, data, count);
722 state->mac_len += count;
727 memset(&cryp, 0, sizeof(cryp));
729 cryp.ses = sess->ses;
732 cryp.src = (caddr_t) data;
734 cryp.mac = (caddr_t) state->digest_res;
735 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
736 printf("cryptodev_digest_update: digest failed\n");
743 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
745 struct crypt_op cryp;
746 struct dev_crypto_state *state = ctx->md_data;
747 struct session_op *sess = &state->d_sess;
751 if (!md || state->d_fd < 0) {
752 printf("cryptodev_digest_final: illegal input\n");
756 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
757 /* if application doesn't support one buffer */
758 memset(&cryp, 0, sizeof(cryp));
760 cryp.ses = sess->ses;
762 cryp.len = state->mac_len;
763 cryp.src = state->mac_data;
765 cryp.mac = (caddr_t)md;
767 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
768 printf("cryptodev_digest_final: digest failed\n");
775 memcpy(md, state->digest_res, ctx->digest->md_size);
781 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
784 struct dev_crypto_state *state = ctx->md_data;
785 struct session_op *sess = &state->d_sess;
787 if (state->d_fd < 0) {
788 printf("cryptodev_digest_cleanup: illegal input\n");
792 if (state->mac_data) {
793 OPENSSL_free(state->mac_data);
794 state->mac_data = NULL;
801 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
802 printf("cryptodev_digest_cleanup: failed to close session\n");
813 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
815 struct dev_crypto_state *fstate = from->md_data;
816 struct dev_crypto_state *dstate = to->md_data;
818 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
820 if (fstate->mac_len != 0) {
821 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
822 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
831 const EVP_MD cryptodev_sha1 = {
836 cryptodev_digest_init,
837 cryptodev_digest_update,
838 cryptodev_digest_final,
839 cryptodev_digest_copy,
840 cryptodev_digest_cleanup,
841 EVP_PKEY_NULL_method,
843 sizeof(struct dev_crypto_state),
846 const EVP_MD cryptodev_md5 = {
849 16 /* MD5_DIGEST_LENGTH */,
851 cryptodev_digest_init,
852 cryptodev_digest_update,
853 cryptodev_digest_final,
854 cryptodev_digest_copy,
855 cryptodev_digest_cleanup,
856 EVP_PKEY_NULL_method,
858 sizeof(struct dev_crypto_state),
861 #endif /* USE_CRYPTODEV_DIGESTS */
865 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
866 const int **nids, int nid)
869 return (cryptodev_usable_digests(nids));
872 #ifdef USE_CRYPTODEV_DIGESTS
874 *digest = &cryptodev_md5;
877 *digest = &cryptodev_sha1;
880 #endif /* USE_CRYPTODEV_DIGESTS */
884 return (*digest != NULL);
888 * Convert a BIGNUM to the representation that /dev/crypto needs.
889 * Upon completion of use, the caller is responsible for freeing
893 bn2crparam(const BIGNUM *a, struct crparam *crp)
902 bits = BN_num_bits(a);
903 bytes = (bits + 7) / 8;
910 crp->crp_p = (caddr_t) b;
911 crp->crp_nbits = bits;
913 for (i = 0, j = 0; i < a->top; i++) {
914 for (k = 0; k < BN_BITS2 / 8; k++) {
915 if ((j + k) >= bytes)
917 b[j + k] = a->d[i] >> (k * 8);
924 /* Convert a /dev/crypto parameter to a BIGNUM */
926 crparam2bn(struct crparam *crp, BIGNUM *a)
931 bytes = (crp->crp_nbits + 7) / 8;
936 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
939 for (i = 0; i < bytes; i++)
940 pd[i] = crp->crp_p[bytes - i - 1];
942 BN_bin2bn(pd, bytes, a);
949 zapparams(struct crypt_kop *kop)
953 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
954 if (kop->crk_param[i].crp_p)
955 free(kop->crk_param[i].crp_p);
956 kop->crk_param[i].crp_p = NULL;
957 kop->crk_param[i].crp_nbits = 0;
962 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
966 if ((fd = get_asym_dev_crypto()) < 0)
970 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
971 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
975 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
976 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
980 if (ioctl(fd, CIOCKEY, kop) == 0) {
982 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
984 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
992 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
993 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
995 struct crypt_kop kop;
998 /* Currently, we know we can do mod exp iff we can do any
999 * asymmetric operations at all.
1001 if (cryptodev_asymfeat == 0) {
1002 ret = BN_mod_exp(r, a, p, m, ctx);
1006 memset(&kop, 0, sizeof kop);
1007 kop.crk_op = CRK_MOD_EXP;
1009 /* inputs: a^p % m */
1010 if (bn2crparam(a, &kop.crk_param[0]))
1012 if (bn2crparam(p, &kop.crk_param[1]))
1014 if (bn2crparam(m, &kop.crk_param[2]))
1016 kop.crk_iparams = 3;
1018 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
1019 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1020 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1028 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1032 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1038 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1040 struct crypt_kop kop;
1043 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1044 /* XXX 0 means failure?? */
1048 memset(&kop, 0, sizeof kop);
1049 kop.crk_op = CRK_MOD_EXP_CRT;
1050 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1051 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1053 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1055 if (bn2crparam(I, &kop.crk_param[2]))
1057 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1059 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1061 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1063 kop.crk_iparams = 6;
1065 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
1066 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1067 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1074 static RSA_METHOD cryptodev_rsa = {
1075 "cryptodev RSA method",
1076 NULL, /* rsa_pub_enc */
1077 NULL, /* rsa_pub_dec */
1078 NULL, /* rsa_priv_enc */
1079 NULL, /* rsa_priv_dec */
1085 NULL, /* app_data */
1086 NULL, /* rsa_sign */
1087 NULL /* rsa_verify */
1091 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1092 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1094 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1098 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1099 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1100 BN_CTX *ctx, BN_MONT_CTX *mont)
1107 /* v = ( g^u1 * y^u2 mod p ) mod q */
1108 /* let t1 = g ^ u1 mod p */
1111 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1114 /* let t2 = y ^ u2 mod p */
1115 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1117 /* let u1 = t1 * t2 mod p */
1118 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1130 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1132 struct crypt_kop kop;
1133 BIGNUM *r = NULL, *s = NULL;
1134 DSA_SIG *dsaret = NULL;
1136 if ((r = BN_new()) == NULL)
1138 if ((s = BN_new()) == NULL) {
1143 memset(&kop, 0, sizeof kop);
1144 kop.crk_op = CRK_DSA_SIGN;
1146 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1147 kop.crk_param[0].crp_p = (caddr_t)dgst;
1148 kop.crk_param[0].crp_nbits = dlen * 8;
1149 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1151 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1153 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1155 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1157 kop.crk_iparams = 5;
1159 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1160 BN_num_bytes(dsa->q), s) == 0) {
1161 dsaret = DSA_SIG_new();
1165 const DSA_METHOD *meth = DSA_OpenSSL();
1168 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1171 kop.crk_param[0].crp_p = NULL;
1177 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1178 DSA_SIG *sig, DSA *dsa)
1180 struct crypt_kop kop;
1183 memset(&kop, 0, sizeof kop);
1184 kop.crk_op = CRK_DSA_VERIFY;
1186 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1187 kop.crk_param[0].crp_p = (caddr_t)dgst;
1188 kop.crk_param[0].crp_nbits = dlen * 8;
1189 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1191 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1193 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1195 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1197 if (bn2crparam(sig->r, &kop.crk_param[5]))
1199 if (bn2crparam(sig->s, &kop.crk_param[6]))
1201 kop.crk_iparams = 7;
1203 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1204 dsaret = kop.crk_status;
1206 const DSA_METHOD *meth = DSA_OpenSSL();
1208 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1211 kop.crk_param[0].crp_p = NULL;
1216 static DSA_METHOD cryptodev_dsa = {
1217 "cryptodev DSA method",
1219 NULL, /* dsa_sign_setup */
1221 NULL, /* dsa_mod_exp */
1230 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1231 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1234 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1238 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1240 struct crypt_kop kop;
1244 if ((fd = get_asym_dev_crypto()) < 0) {
1245 const DH_METHOD *meth = DH_OpenSSL();
1247 return ((meth->compute_key)(key, pub_key, dh));
1250 keylen = BN_num_bits(dh->p);
1252 memset(&kop, 0, sizeof kop);
1253 kop.crk_op = CRK_DH_COMPUTE_KEY;
1255 /* inputs: dh->priv_key pub_key dh->p key */
1256 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1258 if (bn2crparam(pub_key, &kop.crk_param[1]))
1260 if (bn2crparam(dh->p, &kop.crk_param[2]))
1262 kop.crk_iparams = 3;
1264 kop.crk_param[3].crp_p = (caddr_t) key;
1265 kop.crk_param[3].crp_nbits = keylen * 8;
1266 kop.crk_oparams = 1;
1268 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1269 const DH_METHOD *meth = DH_OpenSSL();
1271 dhret = (meth->compute_key)(key, pub_key, dh);
1274 kop.crk_param[3].crp_p = NULL;
1279 static DH_METHOD cryptodev_dh = {
1280 "cryptodev DH method",
1281 NULL, /* cryptodev_dh_generate_key */
1291 * ctrl right now is just a wrapper that doesn't do much
1292 * but I expect we'll want some options soon.
1295 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1297 #ifdef HAVE_SYSLOG_R
1298 struct syslog_data sd = SYSLOG_DATA_INIT;
1303 #ifdef HAVE_SYSLOG_R
1304 syslog_r(LOG_ERR, &sd,
1305 "cryptodev_ctrl: unknown command %d", cmd);
1307 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1315 ENGINE_load_cryptodev(void)
1317 ENGINE *engine = ENGINE_new();
1322 if ((fd = get_dev_crypto()) < 0) {
1323 ENGINE_free(engine);
1328 * find out what asymmetric crypto algorithms we support
1330 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1332 ENGINE_free(engine);
1337 if (!ENGINE_set_id(engine, "cryptodev") ||
1338 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1339 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1340 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1341 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1342 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1343 ENGINE_free(engine);
1347 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1348 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1350 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1351 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1352 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1353 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1354 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1355 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1356 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1357 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1358 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1359 cryptodev_rsa.rsa_mod_exp =
1360 cryptodev_rsa_mod_exp;
1362 cryptodev_rsa.rsa_mod_exp =
1363 cryptodev_rsa_nocrt_mod_exp;
1367 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1368 const DSA_METHOD *meth = DSA_OpenSSL();
1370 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1371 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1372 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1373 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1374 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1375 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1377 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1378 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1381 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1382 const DH_METHOD *dh_meth = DH_OpenSSL();
1384 cryptodev_dh.generate_key = dh_meth->generate_key;
1385 cryptodev_dh.compute_key = dh_meth->compute_key;
1386 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1387 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1388 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1389 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1390 cryptodev_dh.compute_key =
1391 cryptodev_dh_compute_key;
1396 ENGINE_free(engine);
1400 #endif /* HAVE_CRYPTODEV */