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>
33 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
34 (defined(OpenBSD) || defined(__FreeBSD_version))
35 #include <sys/param.h>
36 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
37 # define HAVE_CRYPTODEV
39 # if (OpenBSD >= 200110)
40 # define HAVE_SYSLOG_R
44 #ifndef HAVE_CRYPTODEV
47 ENGINE_load_cryptodev(void)
49 /* This is a NOP on platforms without /dev/crypto */
55 #include <sys/types.h>
56 #include <crypto/cryptodev.h>
57 #include <sys/ioctl.h>
67 struct dev_crypto_state {
68 struct session_op d_sess;
72 static u_int32_t cryptodev_asymfeat = 0;
74 static int get_asym_dev_crypto(void);
75 static int open_dev_crypto(void);
76 static int get_dev_crypto(void);
77 static int cryptodev_max_iv(int cipher);
78 static int cryptodev_key_length_valid(int cipher, int len);
79 static int cipher_nid_to_cryptodev(int nid);
80 static int get_cryptodev_ciphers(const int **cnids);
81 static int get_cryptodev_digests(const int **cnids);
82 static int cryptodev_usable_ciphers(const int **nids);
83 static int cryptodev_usable_digests(const int **nids);
84 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
85 const unsigned char *in, unsigned int inl);
86 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
87 const unsigned char *iv, int enc);
88 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
89 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
90 const int **nids, int nid);
91 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
92 const int **nids, int nid);
93 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
94 static int crparam2bn(struct crparam *crp, BIGNUM *a);
95 static void zapparams(struct crypt_kop *kop);
96 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
99 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
100 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
101 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
103 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
104 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
105 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
106 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
107 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
108 BN_CTX *ctx, BN_MONT_CTX *mont);
109 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
111 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
112 DSA_SIG *sig, DSA *dsa);
113 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
114 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
116 static int cryptodev_dh_compute_key(unsigned char *key,
117 const BIGNUM *pub_key, DH *dh);
118 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
120 void ENGINE_load_cryptodev(void);
122 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
132 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
133 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
134 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
135 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
136 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
137 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
138 { 0, NID_undef, 0, 0, },
145 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, },
146 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, },
147 { CRYPTO_MD5_KPDK, NID_undef, },
148 { CRYPTO_SHA1_KPDK, NID_undef, },
149 { CRYPTO_MD5, NID_md5, },
150 { CRYPTO_SHA1, NID_undef, },
155 * Return a fd if /dev/crypto seems usable, 0 otherwise.
158 open_dev_crypto(void)
163 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
166 if (fcntl(fd, F_SETFD, 1) == -1) {
180 if ((fd = open_dev_crypto()) == -1)
182 if (ioctl(fd, CRIOGET, &retfd) == -1)
186 if (fcntl(retfd, F_SETFD, 1) == -1) {
193 /* Caching version for asym operations */
195 get_asym_dev_crypto(void)
200 fd = get_dev_crypto();
205 * XXXX this needs to be set for each alg - and determined from
209 cryptodev_max_iv(int cipher)
213 for (i = 0; ciphers[i].id; i++)
214 if (ciphers[i].id == cipher)
215 return (ciphers[i].ivmax);
220 * XXXX this needs to be set for each alg - and determined from
221 * a running card. For now, fake it out - but most of these
222 * for real devices should return 1 for the supported key
223 * sizes the device can handle.
226 cryptodev_key_length_valid(int cipher, int len)
230 for (i = 0; ciphers[i].id; i++)
231 if (ciphers[i].id == cipher)
232 return (ciphers[i].keylen == len);
236 /* convert libcrypto nids to cryptodev */
238 cipher_nid_to_cryptodev(int nid)
242 for (i = 0; ciphers[i].id; i++)
243 if (ciphers[i].nid == nid)
244 return (ciphers[i].id);
249 * Find out what ciphers /dev/crypto will let us have a session for.
250 * XXX note, that some of these openssl doesn't deal with yet!
251 * returning them here is harmless, as long as we return NULL
252 * when asked for a handler in the cryptodev_engine_ciphers routine
255 get_cryptodev_ciphers(const int **cnids)
257 static int nids[CRYPTO_ALGORITHM_MAX];
258 struct session_op sess;
259 int fd, i, count = 0;
261 if ((fd = get_dev_crypto()) < 0) {
265 memset(&sess, 0, sizeof(sess));
266 sess.key = (caddr_t)"123456781234567812345678";
268 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
269 if (ciphers[i].nid == NID_undef)
271 sess.cipher = ciphers[i].id;
272 sess.keylen = ciphers[i].keylen;
274 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
275 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
276 nids[count++] = ciphers[i].nid;
288 * Find out what digests /dev/crypto will let us have a session for.
289 * XXX note, that some of these openssl doesn't deal with yet!
290 * returning them here is harmless, as long as we return NULL
291 * when asked for a handler in the cryptodev_engine_digests routine
294 get_cryptodev_digests(const int **cnids)
296 static int nids[CRYPTO_ALGORITHM_MAX];
297 struct session_op sess;
298 int fd, i, count = 0;
300 if ((fd = get_dev_crypto()) < 0) {
304 memset(&sess, 0, sizeof(sess));
305 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
306 if (digests[i].nid == NID_undef)
308 sess.mac = digests[i].id;
310 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
311 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
312 nids[count++] = digests[i].nid;
324 * Find the useable ciphers|digests from dev/crypto - this is the first
325 * thing called by the engine init crud which determines what it
326 * can use for ciphers from this engine. We want to return
327 * only what we can do, anythine else is handled by software.
329 * If we can't initialize the device to do anything useful for
330 * any reason, we want to return a NULL array, and 0 length,
331 * which forces everything to be done is software. By putting
332 * the initalization of the device in here, we ensure we can
333 * use this engine as the default, and if for whatever reason
334 * /dev/crypto won't do what we want it will just be done in
337 * This can (should) be greatly expanded to perhaps take into
338 * account speed of the device, and what we want to do.
339 * (although the disabling of particular alg's could be controlled
340 * by the device driver with sysctl's.) - this is where we
341 * want most of the decisions made about what we actually want
342 * to use from /dev/crypto.
345 cryptodev_usable_ciphers(const int **nids)
347 return (get_cryptodev_ciphers(nids));
351 cryptodev_usable_digests(const int **nids)
354 * XXXX just disable all digests for now, because it sucks.
355 * we need a better way to decide this - i.e. I may not
356 * want digests on slow cards like hifn on fast machines,
357 * but might want them on slow or loaded machines, etc.
358 * will also want them when using crypto cards that don't
359 * suck moose gonads - would be nice to be able to decide something
360 * as reasonable default without having hackery that's card dependent.
361 * of course, the default should probably be just do everything,
362 * with perhaps a sysctl to turn algoritms off (or have them off
363 * by default) on cards that generally suck like the hifn.
370 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
371 const unsigned char *in, unsigned int inl)
373 struct crypt_op cryp;
374 struct dev_crypto_state *state = ctx->cipher_data;
375 struct session_op *sess = &state->d_sess;
377 unsigned char save_iv[EVP_MAX_IV_LENGTH];
383 if ((inl % ctx->cipher->block_size) != 0)
386 memset(&cryp, 0, sizeof(cryp));
388 cryp.ses = sess->ses;
391 cryp.src = (caddr_t) in;
392 cryp.dst = (caddr_t) out;
395 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
397 if (ctx->cipher->iv_len) {
398 cryp.iv = (caddr_t) ctx->iv;
400 iiv = (void *) in + inl - ctx->cipher->iv_len;
401 memcpy(save_iv, iiv, ctx->cipher->iv_len);
406 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
407 /* XXX need better errror handling
408 * this can fail for a number of different reasons.
413 if (ctx->cipher->iv_len) {
415 iiv = (void *) out + inl - ctx->cipher->iv_len;
418 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
424 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
425 const unsigned char *iv, int enc)
427 struct dev_crypto_state *state = ctx->cipher_data;
428 struct session_op *sess = &state->d_sess;
431 if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
434 if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
437 if (!cryptodev_key_length_valid(cipher, ctx->key_len))
440 memset(sess, 0, sizeof(struct session_op));
442 if ((state->d_fd = get_dev_crypto()) < 0)
445 sess->key = (unsigned char *)key;
446 sess->keylen = ctx->key_len;
447 sess->cipher = cipher;
449 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
458 * free anything we allocated earlier when initting a
459 * session, and close the session.
462 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
465 struct dev_crypto_state *state = ctx->cipher_data;
466 struct session_op *sess = &state->d_sess;
471 /* XXX if this ioctl fails, someting's wrong. the invoker
472 * may have called us with a bogus ctx, or we could
473 * have a device that for whatever reason just doesn't
474 * want to play ball - it's not clear what's right
475 * here - should this be an error? should it just
476 * increase a counter, hmm. For right now, we return
477 * 0 - I don't believe that to be "right". we could
478 * call the gorpy openssl lib error handlers that
479 * print messages to users of the library. hmm..
482 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
494 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
495 * gets called when libcrypto requests a cipher NID.
499 const EVP_CIPHER cryptodev_des_cbc = {
506 sizeof(struct dev_crypto_state),
507 EVP_CIPHER_set_asn1_iv,
508 EVP_CIPHER_get_asn1_iv,
513 const EVP_CIPHER cryptodev_3des_cbc = {
520 sizeof(struct dev_crypto_state),
521 EVP_CIPHER_set_asn1_iv,
522 EVP_CIPHER_get_asn1_iv,
526 const EVP_CIPHER cryptodev_bf_cbc = {
533 sizeof(struct dev_crypto_state),
534 EVP_CIPHER_set_asn1_iv,
535 EVP_CIPHER_get_asn1_iv,
539 const EVP_CIPHER cryptodev_cast_cbc = {
546 sizeof(struct dev_crypto_state),
547 EVP_CIPHER_set_asn1_iv,
548 EVP_CIPHER_get_asn1_iv,
552 const EVP_CIPHER cryptodev_aes_cbc = {
559 sizeof(struct dev_crypto_state),
560 EVP_CIPHER_set_asn1_iv,
561 EVP_CIPHER_get_asn1_iv,
566 * Registered by the ENGINE when used to find out how to deal with
567 * a particular NID in the ENGINE. this says what we'll do at the
568 * top level - note, that list is restricted by what we answer with
571 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
572 const int **nids, int nid)
575 return (cryptodev_usable_ciphers(nids));
578 case NID_des_ede3_cbc:
579 *cipher = &cryptodev_3des_cbc;
582 *cipher = &cryptodev_des_cbc;
585 *cipher = &cryptodev_bf_cbc;
588 *cipher = &cryptodev_cast_cbc;
590 case NID_aes_128_cbc:
591 *cipher = &cryptodev_aes_cbc;
597 return (*cipher != NULL);
601 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
602 const int **nids, int nid)
605 return (cryptodev_usable_digests(nids));
609 *digest = NULL; /* need to make a clean md5 critter */
615 return (*digest != NULL);
619 * Convert a BIGNUM to the representation that /dev/crypto needs.
620 * Upon completion of use, the caller is responsible for freeing
624 bn2crparam(const BIGNUM *a, struct crparam *crp)
627 ssize_t words, bytes, bits;
633 bits = BN_num_bits(a);
634 bytes = (bits + 7) / 8;
641 crp->crp_nbits = bits;
643 for (i = 0, j = 0; i < a->top; i++) {
644 for (k = 0; k < BN_BITS2 / 8; k++) {
645 if ((j + k) >= bytes)
647 b[j + k] = a->d[i] >> (k * 8);
654 /* Convert a /dev/crypto parameter to a BIGNUM */
656 crparam2bn(struct crparam *crp, BIGNUM *a)
661 bytes = (crp->crp_nbits + 7) / 8;
666 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
669 for (i = 0; i < bytes; i++)
670 pd[i] = crp->crp_p[bytes - i - 1];
672 BN_bin2bn(pd, bytes, a);
679 zapparams(struct crypt_kop *kop)
683 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
684 if (kop->crk_param[i].crp_p)
685 free(kop->crk_param[i].crp_p);
686 kop->crk_param[i].crp_p = NULL;
687 kop->crk_param[i].crp_nbits = 0;
692 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
696 if ((fd = get_asym_dev_crypto()) < 0)
700 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
701 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
705 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
706 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
710 if (ioctl(fd, CIOCKEY, kop) == 0) {
712 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
714 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
722 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
723 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
725 struct crypt_kop kop;
728 /* Currently, we know we can do mod exp iff we can do any
729 * asymmetric operations at all.
731 if (cryptodev_asymfeat == 0) {
732 ret = BN_mod_exp(r, a, p, m, ctx);
736 memset(&kop, 0, sizeof kop);
737 kop.crk_op = CRK_MOD_EXP;
739 /* inputs: a^p % m */
740 if (bn2crparam(a, &kop.crk_param[0]))
742 if (bn2crparam(p, &kop.crk_param[1]))
744 if (bn2crparam(m, &kop.crk_param[2]))
748 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
749 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
750 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
758 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
764 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
770 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
772 struct crypt_kop kop;
775 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
776 /* XXX 0 means failure?? */
780 memset(&kop, 0, sizeof kop);
781 kop.crk_op = CRK_MOD_EXP_CRT;
782 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
783 if (bn2crparam(rsa->p, &kop.crk_param[0]))
785 if (bn2crparam(rsa->q, &kop.crk_param[1]))
787 if (bn2crparam(I, &kop.crk_param[2]))
789 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
791 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
793 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
797 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
798 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
799 ret = (*meth->rsa_mod_exp)(r0, I, rsa);
806 static RSA_METHOD cryptodev_rsa = {
807 "cryptodev RSA method",
808 NULL, /* rsa_pub_enc */
809 NULL, /* rsa_pub_dec */
810 NULL, /* rsa_priv_enc */
811 NULL, /* rsa_priv_dec */
819 NULL /* rsa_verify */
823 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
824 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
826 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
830 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
831 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
832 BN_CTX *ctx, BN_MONT_CTX *mont)
839 /* v = ( g^u1 * y^u2 mod p ) mod q */
840 /* let t1 = g ^ u1 mod p */
843 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
846 /* let t2 = y ^ u2 mod p */
847 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
849 /* let u1 = t1 * t2 mod p */
850 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
862 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
864 struct crypt_kop kop;
865 BIGNUM *r = NULL, *s = NULL;
866 DSA_SIG *dsaret = NULL;
868 if ((r = BN_new()) == NULL)
870 if ((s = BN_new()) == NULL) {
875 memset(&kop, 0, sizeof kop);
876 kop.crk_op = CRK_DSA_SIGN;
878 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
879 kop.crk_param[0].crp_p = (caddr_t)dgst;
880 kop.crk_param[0].crp_nbits = dlen * 8;
881 if (bn2crparam(dsa->p, &kop.crk_param[1]))
883 if (bn2crparam(dsa->q, &kop.crk_param[2]))
885 if (bn2crparam(dsa->g, &kop.crk_param[3]))
887 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
891 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
892 BN_num_bytes(dsa->q), s) == 0) {
893 dsaret = DSA_SIG_new();
897 const DSA_METHOD *meth = DSA_OpenSSL();
900 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
903 kop.crk_param[0].crp_p = NULL;
909 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
910 DSA_SIG *sig, DSA *dsa)
912 struct crypt_kop kop;
915 memset(&kop, 0, sizeof kop);
916 kop.crk_op = CRK_DSA_VERIFY;
918 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
919 kop.crk_param[0].crp_p = (caddr_t)dgst;
920 kop.crk_param[0].crp_nbits = dlen * 8;
921 if (bn2crparam(dsa->p, &kop.crk_param[1]))
923 if (bn2crparam(dsa->q, &kop.crk_param[2]))
925 if (bn2crparam(dsa->g, &kop.crk_param[3]))
927 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
929 if (bn2crparam(sig->r, &kop.crk_param[5]))
931 if (bn2crparam(sig->s, &kop.crk_param[6]))
935 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
936 dsaret = kop.crk_status;
938 const DSA_METHOD *meth = DSA_OpenSSL();
940 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
943 kop.crk_param[0].crp_p = NULL;
948 static DSA_METHOD cryptodev_dsa = {
949 "cryptodev DSA method",
951 NULL, /* dsa_sign_setup */
953 NULL, /* dsa_mod_exp */
962 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
963 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
966 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
970 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
972 struct crypt_kop kop;
976 if ((fd = get_asym_dev_crypto()) < 0) {
977 const DH_METHOD *meth = DH_OpenSSL();
979 return ((meth->compute_key)(key, pub_key, dh));
982 keylen = BN_num_bits(dh->p);
984 memset(&kop, 0, sizeof kop);
985 kop.crk_op = CRK_DH_COMPUTE_KEY;
987 /* inputs: dh->priv_key pub_key dh->p key */
988 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
990 if (bn2crparam(pub_key, &kop.crk_param[1]))
992 if (bn2crparam(dh->p, &kop.crk_param[2]))
996 kop.crk_param[3].crp_p = key;
997 kop.crk_param[3].crp_nbits = keylen * 8;
1000 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1001 const DH_METHOD *meth = DH_OpenSSL();
1003 dhret = (meth->compute_key)(key, pub_key, dh);
1006 kop.crk_param[3].crp_p = NULL;
1011 static DH_METHOD cryptodev_dh = {
1012 "cryptodev DH method",
1013 NULL, /* cryptodev_dh_generate_key */
1023 * ctrl right now is just a wrapper that doesn't do much
1024 * but I expect we'll want some options soon.
1027 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1029 #ifdef HAVE_SYSLOG_R
1030 struct syslog_data sd = SYSLOG_DATA_INIT;
1035 #ifdef HAVE_SYSLOG_R
1036 syslog_r(LOG_ERR, &sd,
1037 "cryptodev_ctrl: unknown command %d", cmd);
1039 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1047 ENGINE_load_cryptodev(void)
1049 ENGINE *engine = ENGINE_new();
1054 if ((fd = get_dev_crypto()) < 0) {
1055 ENGINE_free(engine);
1060 * find out what asymmetric crypto algorithms we support
1062 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1064 ENGINE_free(engine);
1069 if (!ENGINE_set_id(engine, "cryptodev") ||
1070 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1071 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1072 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1073 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1074 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1075 ENGINE_free(engine);
1079 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1080 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1082 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1083 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1084 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1085 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1086 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1087 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1088 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1089 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1090 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1091 cryptodev_rsa.rsa_mod_exp =
1092 cryptodev_rsa_mod_exp;
1094 cryptodev_rsa.rsa_mod_exp =
1095 cryptodev_rsa_nocrt_mod_exp;
1099 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1100 const DSA_METHOD *meth = DSA_OpenSSL();
1102 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1103 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1104 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1105 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1106 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1107 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1109 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1110 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1113 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1114 const DH_METHOD *dh_meth = DH_OpenSSL();
1116 cryptodev_dh.generate_key = dh_meth->generate_key;
1117 cryptodev_dh.compute_key = dh_meth->compute_key;
1118 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1119 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1120 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1121 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1122 cryptodev_dh.compute_key =
1123 cryptodev_dh_compute_key;
1128 ENGINE_free(engine);
1132 #endif /* HAVE_CRYPTODEV */