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_version))
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 <sys/ioctl.h>
68 struct dev_crypto_state {
69 struct session_op d_sess;
72 #ifdef USE_CRYPTODEV_DIGESTS
73 char dummy_mac_key[HASH_MAX_LEN];
75 unsigned char digest_res[HASH_MAX_LEN];
83 static u_int32_t cryptodev_asymfeat = 0;
85 static int get_asym_dev_crypto(void);
86 static int open_dev_crypto(void);
87 static int get_dev_crypto(void);
88 static int get_cryptodev_ciphers(const int **cnids);
89 static int get_cryptodev_digests(const int **cnids);
90 static int cryptodev_usable_ciphers(const int **nids);
91 static int cryptodev_usable_digests(const int **nids);
92 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
93 const unsigned char *in, size_t inl);
94 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
95 const unsigned char *iv, int enc);
96 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
97 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
98 const int **nids, int nid);
99 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
100 const int **nids, int nid);
101 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
102 static int crparam2bn(struct crparam *crp, BIGNUM *a);
103 static void zapparams(struct crypt_kop *kop);
104 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
105 int slen, BIGNUM *s);
107 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
108 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
109 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
110 RSA *rsa, BN_CTX *ctx);
111 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
112 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
113 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
114 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
115 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
116 BN_CTX *ctx, BN_MONT_CTX *mont);
117 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
119 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
120 DSA_SIG *sig, DSA *dsa);
121 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
122 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
124 static int cryptodev_dh_compute_key(unsigned char *key,
125 const BIGNUM *pub_key, DH *dh);
126 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
128 void ENGINE_load_cryptodev(void);
130 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
140 { CRYPTO_ARC4, NID_rc4, 0, 16, },
141 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
142 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
143 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
144 { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
145 { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
146 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
147 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
148 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
149 { 0, NID_undef, 0, 0, },
157 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
158 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
159 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
160 { CRYPTO_MD5_KPDK, NID_undef, 0},
161 { CRYPTO_SHA1_KPDK, NID_undef, 0},
162 { CRYPTO_MD5, NID_md5, 16},
163 { CRYPTO_SHA1, NID_sha1, 20},
168 * Return a fd if /dev/crypto seems usable, 0 otherwise.
171 open_dev_crypto(void)
176 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
179 if (fcntl(fd, F_SETFD, 1) == -1) {
193 if ((fd = open_dev_crypto()) == -1)
195 if (ioctl(fd, CRIOGET, &retfd) == -1)
199 if (fcntl(retfd, F_SETFD, 1) == -1) {
206 /* Caching version for asym operations */
208 get_asym_dev_crypto(void)
213 fd = get_dev_crypto();
218 * Find out what ciphers /dev/crypto will let us have a session for.
219 * XXX note, that some of these openssl doesn't deal with yet!
220 * returning them here is harmless, as long as we return NULL
221 * when asked for a handler in the cryptodev_engine_ciphers routine
224 get_cryptodev_ciphers(const int **cnids)
226 static int nids[CRYPTO_ALGORITHM_MAX];
227 struct session_op sess;
228 int fd, i, count = 0;
230 if ((fd = get_dev_crypto()) < 0) {
234 memset(&sess, 0, sizeof(sess));
235 sess.key = (caddr_t)"123456789abcdefghijklmno";
237 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
238 if (ciphers[i].nid == NID_undef)
240 sess.cipher = ciphers[i].id;
241 sess.keylen = ciphers[i].keylen;
243 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
244 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
245 nids[count++] = ciphers[i].nid;
257 * Find out what digests /dev/crypto will let us have a session for.
258 * XXX note, that some of these openssl doesn't deal with yet!
259 * returning them here is harmless, as long as we return NULL
260 * when asked for a handler in the cryptodev_engine_digests routine
263 get_cryptodev_digests(const int **cnids)
265 static int nids[CRYPTO_ALGORITHM_MAX];
266 struct session_op sess;
267 int fd, i, count = 0;
269 if ((fd = get_dev_crypto()) < 0) {
273 memset(&sess, 0, sizeof(sess));
274 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
275 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
276 if (digests[i].nid == NID_undef)
278 sess.mac = digests[i].id;
279 sess.mackeylen = digests[i].keylen;
281 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
282 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
283 nids[count++] = digests[i].nid;
295 * Find the useable ciphers|digests from dev/crypto - this is the first
296 * thing called by the engine init crud which determines what it
297 * can use for ciphers from this engine. We want to return
298 * only what we can do, anythine else is handled by software.
300 * If we can't initialize the device to do anything useful for
301 * any reason, we want to return a NULL array, and 0 length,
302 * which forces everything to be done is software. By putting
303 * the initalization of the device in here, we ensure we can
304 * use this engine as the default, and if for whatever reason
305 * /dev/crypto won't do what we want it will just be done in
308 * This can (should) be greatly expanded to perhaps take into
309 * account speed of the device, and what we want to do.
310 * (although the disabling of particular alg's could be controlled
311 * by the device driver with sysctl's.) - this is where we
312 * want most of the decisions made about what we actually want
313 * to use from /dev/crypto.
316 cryptodev_usable_ciphers(const int **nids)
318 return (get_cryptodev_ciphers(nids));
322 cryptodev_usable_digests(const int **nids)
324 #ifdef USE_CRYPTODEV_DIGESTS
325 return (get_cryptodev_digests(nids));
328 * XXXX just disable all digests for now, because it sucks.
329 * we need a better way to decide this - i.e. I may not
330 * want digests on slow cards like hifn on fast machines,
331 * but might want them on slow or loaded machines, etc.
332 * will also want them when using crypto cards that don't
333 * suck moose gonads - would be nice to be able to decide something
334 * as reasonable default without having hackery that's card dependent.
335 * of course, the default should probably be just do everything,
336 * with perhaps a sysctl to turn algoritms off (or have them off
337 * by default) on cards that generally suck like the hifn.
345 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
346 const unsigned char *in, size_t inl)
348 struct crypt_op cryp;
349 struct dev_crypto_state *state = ctx->cipher_data;
350 struct session_op *sess = &state->d_sess;
352 unsigned char save_iv[EVP_MAX_IV_LENGTH];
358 if ((inl % ctx->cipher->block_size) != 0)
361 memset(&cryp, 0, sizeof(cryp));
363 cryp.ses = sess->ses;
366 cryp.src = (caddr_t) in;
367 cryp.dst = (caddr_t) out;
370 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
372 if (ctx->cipher->iv_len) {
373 cryp.iv = (caddr_t) ctx->iv;
375 iiv = (void *) in + inl - ctx->cipher->iv_len;
376 memcpy(save_iv, iiv, ctx->cipher->iv_len);
381 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
382 /* XXX need better errror handling
383 * this can fail for a number of different reasons.
388 if (ctx->cipher->iv_len) {
390 iiv = (void *) out + inl - ctx->cipher->iv_len;
393 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
399 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
400 const unsigned char *iv, int enc)
402 struct dev_crypto_state *state = ctx->cipher_data;
403 struct session_op *sess = &state->d_sess;
406 for (i = 0; ciphers[i].id; i++)
407 if (ctx->cipher->nid == ciphers[i].nid &&
408 ctx->cipher->iv_len <= ciphers[i].ivmax &&
409 ctx->key_len == ciphers[i].keylen) {
410 cipher = ciphers[i].id;
414 if (!ciphers[i].id) {
419 memset(sess, 0, sizeof(struct session_op));
421 if ((state->d_fd = get_dev_crypto()) < 0)
424 sess->key = (caddr_t)key;
425 sess->keylen = ctx->key_len;
426 sess->cipher = cipher;
428 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
437 * free anything we allocated earlier when initting a
438 * session, and close the session.
441 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
444 struct dev_crypto_state *state = ctx->cipher_data;
445 struct session_op *sess = &state->d_sess;
450 /* XXX if this ioctl fails, someting's wrong. the invoker
451 * may have called us with a bogus ctx, or we could
452 * have a device that for whatever reason just doesn't
453 * want to play ball - it's not clear what's right
454 * here - should this be an error? should it just
455 * increase a counter, hmm. For right now, we return
456 * 0 - I don't believe that to be "right". we could
457 * call the gorpy openssl lib error handlers that
458 * print messages to users of the library. hmm..
461 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
473 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
474 * gets called when libcrypto requests a cipher NID.
478 const EVP_CIPHER cryptodev_rc4 = {
481 EVP_CIPH_VARIABLE_LENGTH,
485 sizeof(struct dev_crypto_state),
492 const EVP_CIPHER cryptodev_des_cbc = {
499 sizeof(struct dev_crypto_state),
500 EVP_CIPHER_set_asn1_iv,
501 EVP_CIPHER_get_asn1_iv,
506 const EVP_CIPHER cryptodev_3des_cbc = {
513 sizeof(struct dev_crypto_state),
514 EVP_CIPHER_set_asn1_iv,
515 EVP_CIPHER_get_asn1_iv,
519 const EVP_CIPHER cryptodev_bf_cbc = {
526 sizeof(struct dev_crypto_state),
527 EVP_CIPHER_set_asn1_iv,
528 EVP_CIPHER_get_asn1_iv,
532 const EVP_CIPHER cryptodev_cast_cbc = {
539 sizeof(struct dev_crypto_state),
540 EVP_CIPHER_set_asn1_iv,
541 EVP_CIPHER_get_asn1_iv,
545 const EVP_CIPHER cryptodev_aes_cbc = {
552 sizeof(struct dev_crypto_state),
553 EVP_CIPHER_set_asn1_iv,
554 EVP_CIPHER_get_asn1_iv,
558 const EVP_CIPHER cryptodev_aes_192_cbc = {
565 sizeof(struct dev_crypto_state),
566 EVP_CIPHER_set_asn1_iv,
567 EVP_CIPHER_get_asn1_iv,
571 const EVP_CIPHER cryptodev_aes_256_cbc = {
578 sizeof(struct dev_crypto_state),
579 EVP_CIPHER_set_asn1_iv,
580 EVP_CIPHER_get_asn1_iv,
585 * Registered by the ENGINE when used to find out how to deal with
586 * a particular NID in the ENGINE. this says what we'll do at the
587 * top level - note, that list is restricted by what we answer with
590 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
591 const int **nids, int nid)
594 return (cryptodev_usable_ciphers(nids));
598 *cipher = &cryptodev_rc4;
600 case NID_des_ede3_cbc:
601 *cipher = &cryptodev_3des_cbc;
604 *cipher = &cryptodev_des_cbc;
607 *cipher = &cryptodev_bf_cbc;
610 *cipher = &cryptodev_cast_cbc;
612 case NID_aes_128_cbc:
613 *cipher = &cryptodev_aes_cbc;
615 case NID_aes_192_cbc:
616 *cipher = &cryptodev_aes_192_cbc;
618 case NID_aes_256_cbc:
619 *cipher = &cryptodev_aes_256_cbc;
625 return (*cipher != NULL);
629 #ifdef USE_CRYPTODEV_DIGESTS
631 /* convert digest type to cryptodev */
633 digest_nid_to_cryptodev(int nid)
637 for (i = 0; digests[i].id; i++)
638 if (digests[i].nid == nid)
639 return (digests[i].id);
645 digest_key_length(int nid)
649 for (i = 0; digests[i].id; i++)
650 if (digests[i].nid == nid)
651 return digests[i].keylen;
656 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
658 struct dev_crypto_state *state = ctx->md_data;
659 struct session_op *sess = &state->d_sess;
662 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
663 printf("cryptodev_digest_init: Can't get digest \n");
667 memset(state, 0, sizeof(struct dev_crypto_state));
669 if ((state->d_fd = get_dev_crypto()) < 0) {
670 printf("cryptodev_digest_init: Can't get Dev \n");
674 sess->mackey = state->dummy_mac_key;
675 sess->mackeylen = digest_key_length(ctx->digest->type);
678 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
681 printf("cryptodev_digest_init: Open session failed\n");
688 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
691 struct crypt_op cryp;
692 struct dev_crypto_state *state = ctx->md_data;
693 struct session_op *sess = &state->d_sess;
695 if (!data || state->d_fd < 0) {
696 printf("cryptodev_digest_update: illegal inputs \n");
704 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
705 /* if application doesn't support one buffer */
706 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
708 if (!state->mac_data) {
709 printf("cryptodev_digest_update: realloc failed\n");
713 memcpy(state->mac_data + state->mac_len, data, count);
714 state->mac_len += count;
719 memset(&cryp, 0, sizeof(cryp));
721 cryp.ses = sess->ses;
724 cryp.src = (caddr_t) data;
726 cryp.mac = (caddr_t) state->digest_res;
727 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
728 printf("cryptodev_digest_update: digest failed\n");
735 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
737 struct crypt_op cryp;
738 struct dev_crypto_state *state = ctx->md_data;
739 struct session_op *sess = &state->d_sess;
743 if (!md || state->d_fd < 0) {
744 printf("cryptodev_digest_final: illegal input\n");
748 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
749 /* if application doesn't support one buffer */
750 memset(&cryp, 0, sizeof(cryp));
752 cryp.ses = sess->ses;
754 cryp.len = state->mac_len;
755 cryp.src = state->mac_data;
757 cryp.mac = (caddr_t)md;
759 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
760 printf("cryptodev_digest_final: digest failed\n");
767 memcpy(md, state->digest_res, ctx->digest->md_size);
773 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
776 struct dev_crypto_state *state = ctx->md_data;
777 struct session_op *sess = &state->d_sess;
779 if (state->d_fd < 0) {
780 printf("cryptodev_digest_cleanup: illegal input\n");
784 if (state->mac_data) {
785 OPENSSL_free(state->mac_data);
786 state->mac_data = NULL;
793 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
794 printf("cryptodev_digest_cleanup: failed to close session\n");
805 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
807 struct dev_crypto_state *fstate = from->md_data;
808 struct dev_crypto_state *dstate = to->md_data;
810 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
812 if (fstate->mac_len != 0) {
813 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
814 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
823 const EVP_MD cryptodev_sha1 = {
828 cryptodev_digest_init,
829 cryptodev_digest_update,
830 cryptodev_digest_final,
831 cryptodev_digest_copy,
832 cryptodev_digest_cleanup,
833 EVP_PKEY_NULL_method,
835 sizeof(struct dev_crypto_state),
838 const EVP_MD cryptodev_md5 = {
841 16 /* MD5_DIGEST_LENGTH */,
843 cryptodev_digest_init,
844 cryptodev_digest_update,
845 cryptodev_digest_final,
846 cryptodev_digest_copy,
847 cryptodev_digest_cleanup,
848 EVP_PKEY_NULL_method,
850 sizeof(struct dev_crypto_state),
853 #endif /* USE_CRYPTODEV_DIGESTS */
857 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
858 const int **nids, int nid)
861 return (cryptodev_usable_digests(nids));
864 #ifdef USE_CRYPTODEV_DIGESTS
866 *digest = &cryptodev_md5;
869 *digest = &cryptodev_sha1;
872 #endif /* USE_CRYPTODEV_DIGESTS */
876 return (*digest != NULL);
880 * Convert a BIGNUM to the representation that /dev/crypto needs.
881 * Upon completion of use, the caller is responsible for freeing
885 bn2crparam(const BIGNUM *a, struct crparam *crp)
894 bits = BN_num_bits(a);
895 bytes = (bits + 7) / 8;
902 crp->crp_p = (caddr_t) b;
903 crp->crp_nbits = bits;
905 for (i = 0, j = 0; i < a->top; i++) {
906 for (k = 0; k < BN_BITS2 / 8; k++) {
907 if ((j + k) >= bytes)
909 b[j + k] = a->d[i] >> (k * 8);
916 /* Convert a /dev/crypto parameter to a BIGNUM */
918 crparam2bn(struct crparam *crp, BIGNUM *a)
923 bytes = (crp->crp_nbits + 7) / 8;
928 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
931 for (i = 0; i < bytes; i++)
932 pd[i] = crp->crp_p[bytes - i - 1];
934 BN_bin2bn(pd, bytes, a);
941 zapparams(struct crypt_kop *kop)
945 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
946 if (kop->crk_param[i].crp_p)
947 free(kop->crk_param[i].crp_p);
948 kop->crk_param[i].crp_p = NULL;
949 kop->crk_param[i].crp_nbits = 0;
954 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
958 if ((fd = get_asym_dev_crypto()) < 0)
962 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
963 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
967 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
968 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
972 if (ioctl(fd, CIOCKEY, kop) == 0) {
974 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
976 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
984 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
985 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
987 struct crypt_kop kop;
990 /* Currently, we know we can do mod exp iff we can do any
991 * asymmetric operations at all.
993 if (cryptodev_asymfeat == 0) {
994 ret = BN_mod_exp(r, a, p, m, ctx);
998 memset(&kop, 0, sizeof kop);
999 kop.crk_op = CRK_MOD_EXP;
1001 /* inputs: a^p % m */
1002 if (bn2crparam(a, &kop.crk_param[0]))
1004 if (bn2crparam(p, &kop.crk_param[1]))
1006 if (bn2crparam(m, &kop.crk_param[2]))
1008 kop.crk_iparams = 3;
1010 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
1011 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1012 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1020 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1024 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1030 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1032 struct crypt_kop kop;
1035 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1036 /* XXX 0 means failure?? */
1040 memset(&kop, 0, sizeof kop);
1041 kop.crk_op = CRK_MOD_EXP_CRT;
1042 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1043 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1045 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1047 if (bn2crparam(I, &kop.crk_param[2]))
1049 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1051 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1053 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1055 kop.crk_iparams = 6;
1057 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
1058 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1059 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1066 static RSA_METHOD cryptodev_rsa = {
1067 "cryptodev RSA method",
1068 NULL, /* rsa_pub_enc */
1069 NULL, /* rsa_pub_dec */
1070 NULL, /* rsa_priv_enc */
1071 NULL, /* rsa_priv_dec */
1077 NULL, /* app_data */
1078 NULL, /* rsa_sign */
1079 NULL /* rsa_verify */
1083 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1084 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1086 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1090 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1091 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1092 BN_CTX *ctx, BN_MONT_CTX *mont)
1099 /* v = ( g^u1 * y^u2 mod p ) mod q */
1100 /* let t1 = g ^ u1 mod p */
1103 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1106 /* let t2 = y ^ u2 mod p */
1107 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1109 /* let u1 = t1 * t2 mod p */
1110 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1122 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1124 struct crypt_kop kop;
1125 BIGNUM *r = NULL, *s = NULL;
1126 DSA_SIG *dsaret = NULL;
1128 if ((r = BN_new()) == NULL)
1130 if ((s = BN_new()) == NULL) {
1135 memset(&kop, 0, sizeof kop);
1136 kop.crk_op = CRK_DSA_SIGN;
1138 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1139 kop.crk_param[0].crp_p = (caddr_t)dgst;
1140 kop.crk_param[0].crp_nbits = dlen * 8;
1141 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1143 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1145 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1147 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1149 kop.crk_iparams = 5;
1151 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1152 BN_num_bytes(dsa->q), s) == 0) {
1153 dsaret = DSA_SIG_new();
1157 const DSA_METHOD *meth = DSA_OpenSSL();
1160 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1163 kop.crk_param[0].crp_p = NULL;
1169 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1170 DSA_SIG *sig, DSA *dsa)
1172 struct crypt_kop kop;
1175 memset(&kop, 0, sizeof kop);
1176 kop.crk_op = CRK_DSA_VERIFY;
1178 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1179 kop.crk_param[0].crp_p = (caddr_t)dgst;
1180 kop.crk_param[0].crp_nbits = dlen * 8;
1181 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1183 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1185 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1187 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1189 if (bn2crparam(sig->r, &kop.crk_param[5]))
1191 if (bn2crparam(sig->s, &kop.crk_param[6]))
1193 kop.crk_iparams = 7;
1195 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1196 dsaret = kop.crk_status;
1198 const DSA_METHOD *meth = DSA_OpenSSL();
1200 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1203 kop.crk_param[0].crp_p = NULL;
1208 static DSA_METHOD cryptodev_dsa = {
1209 "cryptodev DSA method",
1211 NULL, /* dsa_sign_setup */
1213 NULL, /* dsa_mod_exp */
1222 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1223 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1226 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1230 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1232 struct crypt_kop kop;
1236 if ((fd = get_asym_dev_crypto()) < 0) {
1237 const DH_METHOD *meth = DH_OpenSSL();
1239 return ((meth->compute_key)(key, pub_key, dh));
1242 keylen = BN_num_bits(dh->p);
1244 memset(&kop, 0, sizeof kop);
1245 kop.crk_op = CRK_DH_COMPUTE_KEY;
1247 /* inputs: dh->priv_key pub_key dh->p key */
1248 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1250 if (bn2crparam(pub_key, &kop.crk_param[1]))
1252 if (bn2crparam(dh->p, &kop.crk_param[2]))
1254 kop.crk_iparams = 3;
1256 kop.crk_param[3].crp_p = (caddr_t) key;
1257 kop.crk_param[3].crp_nbits = keylen * 8;
1258 kop.crk_oparams = 1;
1260 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1261 const DH_METHOD *meth = DH_OpenSSL();
1263 dhret = (meth->compute_key)(key, pub_key, dh);
1266 kop.crk_param[3].crp_p = NULL;
1271 static DH_METHOD cryptodev_dh = {
1272 "cryptodev DH method",
1273 NULL, /* cryptodev_dh_generate_key */
1283 * ctrl right now is just a wrapper that doesn't do much
1284 * but I expect we'll want some options soon.
1287 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1289 #ifdef HAVE_SYSLOG_R
1290 struct syslog_data sd = SYSLOG_DATA_INIT;
1295 #ifdef HAVE_SYSLOG_R
1296 syslog_r(LOG_ERR, &sd,
1297 "cryptodev_ctrl: unknown command %d", cmd);
1299 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1307 ENGINE_load_cryptodev(void)
1309 ENGINE *engine = ENGINE_new();
1314 if ((fd = get_dev_crypto()) < 0) {
1315 ENGINE_free(engine);
1320 * find out what asymmetric crypto algorithms we support
1322 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1324 ENGINE_free(engine);
1329 if (!ENGINE_set_id(engine, "cryptodev") ||
1330 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1331 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1332 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1333 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1334 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1335 ENGINE_free(engine);
1339 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1340 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1342 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1343 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1344 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1345 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1346 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1347 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1348 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1349 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1350 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1351 cryptodev_rsa.rsa_mod_exp =
1352 cryptodev_rsa_mod_exp;
1354 cryptodev_rsa.rsa_mod_exp =
1355 cryptodev_rsa_nocrt_mod_exp;
1359 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1360 const DSA_METHOD *meth = DSA_OpenSSL();
1362 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1363 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1364 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1365 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1366 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1367 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1369 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1370 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1373 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1374 const DH_METHOD *dh_meth = DH_OpenSSL();
1376 cryptodev_dh.generate_key = dh_meth->generate_key;
1377 cryptodev_dh.compute_key = dh_meth->compute_key;
1378 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1379 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1380 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1381 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1382 cryptodev_dh.compute_key =
1383 cryptodev_dh_compute_key;
1388 ENGINE_free(engine);
1392 #endif /* HAVE_CRYPTODEV */