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.
15 * 3. Neither the name of the author nor the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <openssl/objects.h>
33 #include <openssl/engine.h>
34 #include <openssl/evp.h>
36 #if (defined(__unix__) || defined(unix)) && !defined(USG)
37 #include <sys/param.h>
38 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
39 # define HAVE_CRYPTODEV
41 # if (OpenBSD >= 200110)
42 # define HAVE_SYSLOG_R
46 #ifndef HAVE_CRYPTODEV
49 ENGINE_load_cryptodev(void)
51 /* This is a NOP on platforms without /dev/crypto */
57 #include <sys/types.h>
58 #include <crypto/cryptodev.h>
59 #include <sys/ioctl.h>
69 struct dev_crypto_state {
70 struct session_op d_sess;
74 static u_int32_t cryptodev_asymfeat = 0;
76 static int get_asym_dev_crypto(void);
77 static int open_dev_crypto(void);
78 static int get_dev_crypto(void);
79 static int cryptodev_max_iv(int cipher);
80 static int cryptodev_key_length_valid(int cipher, int len);
81 static int cipher_nid_to_cryptodev(int nid);
82 static int get_cryptodev_ciphers(const int **cnids);
83 static int get_cryptodev_digests(const int **cnids);
84 static int cryptodev_usable_ciphers(const int **nids);
85 static int cryptodev_usable_digests(const int **nids);
86 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
87 const unsigned char *in, unsigned int inl);
88 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
89 const unsigned char *iv, int enc);
90 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
91 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
92 const int **nids, int nid);
93 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
94 const int **nids, int nid);
95 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
96 static int crparam2bn(struct crparam *crp, BIGNUM *a);
97 static void zapparams(struct crypt_kop *kop);
98 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
101 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
102 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
103 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
105 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
106 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
107 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
108 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
109 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
110 BN_CTX *ctx, BN_MONT_CTX *mont);
111 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
113 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
114 DSA_SIG *sig, DSA *dsa);
115 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
116 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
118 static int cryptodev_dh_compute_key(unsigned char *key,
119 const BIGNUM *pub_key, DH *dh);
120 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
122 void ENGINE_load_cryptodev(void);
124 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
134 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
135 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
136 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
137 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
138 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
139 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
140 { 0, NID_undef, 0, 0, },
147 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, },
148 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, },
149 { CRYPTO_MD5_KPDK, NID_undef, },
150 { CRYPTO_SHA1_KPDK, NID_undef, },
151 { CRYPTO_MD5, NID_md5, },
152 { CRYPTO_SHA1, NID_undef, },
157 * Return a fd if /dev/crypto seems usable, 0 otherwise.
160 open_dev_crypto(void)
165 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
168 if (fcntl(fd, F_SETFD, 1) == -1) {
182 if ((fd = open_dev_crypto()) == -1)
184 if (ioctl(fd, CRIOGET, &retfd) == -1)
188 if (fcntl(retfd, F_SETFD, 1) == -1) {
195 /* Caching version for asym operations */
197 get_asym_dev_crypto(void)
202 fd = get_dev_crypto();
207 * XXXX this needs to be set for each alg - and determined from
211 cryptodev_max_iv(int cipher)
215 for (i = 0; ciphers[i].id; i++)
216 if (ciphers[i].id == cipher)
217 return (ciphers[i].ivmax);
222 * XXXX this needs to be set for each alg - and determined from
223 * a running card. For now, fake it out - but most of these
224 * for real devices should return 1 for the supported key
225 * sizes the device can handle.
228 cryptodev_key_length_valid(int cipher, int len)
232 for (i = 0; ciphers[i].id; i++)
233 if (ciphers[i].id == cipher)
234 return (ciphers[i].keylen == len);
238 /* convert libcrypto nids to cryptodev */
240 cipher_nid_to_cryptodev(int nid)
244 for (i = 0; ciphers[i].id; i++)
245 if (ciphers[i].nid == nid)
246 return (ciphers[i].id);
251 * Find out what ciphers /dev/crypto will let us have a session for.
252 * XXX note, that some of these openssl doesn't deal with yet!
253 * returning them here is harmless, as long as we return NULL
254 * when asked for a handler in the cryptodev_engine_ciphers routine
257 get_cryptodev_ciphers(const int **cnids)
259 static int nids[CRYPTO_ALGORITHM_MAX];
260 struct session_op sess;
261 int fd, i, count = 0;
263 if ((fd = get_dev_crypto()) < 0) {
267 memset(&sess, 0, sizeof(sess));
268 sess.key = (caddr_t)"123456781234567812345678";
270 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
271 if (ciphers[i].nid == NID_undef)
273 sess.cipher = ciphers[i].id;
274 sess.keylen = ciphers[i].keylen;
276 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
277 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
278 nids[count++] = ciphers[i].nid;
290 * Find out what digests /dev/crypto will let us have a session for.
291 * XXX note, that some of these openssl doesn't deal with yet!
292 * returning them here is harmless, as long as we return NULL
293 * when asked for a handler in the cryptodev_engine_digests routine
296 get_cryptodev_digests(const int **cnids)
298 static int nids[CRYPTO_ALGORITHM_MAX];
299 struct session_op sess;
300 int fd, i, count = 0;
302 if ((fd = get_dev_crypto()) < 0) {
306 memset(&sess, 0, sizeof(sess));
307 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
308 if (digests[i].nid == NID_undef)
310 sess.mac = digests[i].id;
312 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
313 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
314 nids[count++] = digests[i].nid;
326 * Find the useable ciphers|digests from dev/crypto - this is the first
327 * thing called by the engine init crud which determines what it
328 * can use for ciphers from this engine. We want to return
329 * only what we can do, anythine else is handled by software.
331 * If we can't initialize the device to do anything useful for
332 * any reason, we want to return a NULL array, and 0 length,
333 * which forces everything to be done is software. By putting
334 * the initalization of the device in here, we ensure we can
335 * use this engine as the default, and if for whatever reason
336 * /dev/crypto won't do what we want it will just be done in
339 * This can (should) be greatly expanded to perhaps take into
340 * account speed of the device, and what we want to do.
341 * (although the disabling of particular alg's could be controlled
342 * by the device driver with sysctl's.) - this is where we
343 * want most of the decisions made about what we actually want
344 * to use from /dev/crypto.
347 cryptodev_usable_ciphers(const int **nids)
349 return (get_cryptodev_ciphers(nids));
353 cryptodev_usable_digests(const int **nids)
356 * XXXX just disable all digests for now, because it sucks.
357 * we need a better way to decide this - i.e. I may not
358 * want digests on slow cards like hifn on fast machines,
359 * but might want them on slow or loaded machines, etc.
360 * will also want them when using crypto cards that don't
361 * suck moose gonads - would be nice to be able to decide something
362 * as reasonable default without having hackery that's card dependent.
363 * of course, the default should probably be just do everything,
364 * with perhaps a sysctl to turn algoritms off (or have them off
365 * by default) on cards that generally suck like the hifn.
372 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
373 const unsigned char *in, unsigned int inl)
375 struct crypt_op cryp;
376 struct dev_crypto_state *state = ctx->cipher_data;
377 struct session_op *sess = &state->d_sess;
379 unsigned char save_iv[EVP_MAX_IV_LENGTH];
385 if ((inl % ctx->cipher->block_size) != 0)
388 memset(&cryp, 0, sizeof(cryp));
390 cryp.ses = sess->ses;
393 cryp.src = (caddr_t) in;
394 cryp.dst = (caddr_t) out;
397 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
399 if (ctx->cipher->iv_len) {
400 cryp.iv = (caddr_t) ctx->iv;
402 iiv = (void *) in + inl - ctx->cipher->iv_len;
403 memcpy(save_iv, iiv, ctx->cipher->iv_len);
408 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
409 /* XXX need better errror handling
410 * this can fail for a number of different reasons.
415 if (ctx->cipher->iv_len) {
417 iiv = (void *) out + inl - ctx->cipher->iv_len;
420 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
426 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
427 const unsigned char *iv, int enc)
429 struct dev_crypto_state *state = ctx->cipher_data;
430 struct session_op *sess = &state->d_sess;
433 if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
436 if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
439 if (!cryptodev_key_length_valid(cipher, ctx->key_len))
442 memset(sess, 0, sizeof(struct session_op));
444 if ((state->d_fd = get_dev_crypto()) < 0)
447 sess->key = (unsigned char *)key;
448 sess->keylen = ctx->key_len;
449 sess->cipher = cipher;
451 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
460 * free anything we allocated earlier when initting a
461 * session, and close the session.
464 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
467 struct dev_crypto_state *state = ctx->cipher_data;
468 struct session_op *sess = &state->d_sess;
473 /* XXX if this ioctl fails, someting's wrong. the invoker
474 * may have called us with a bogus ctx, or we could
475 * have a device that for whatever reason just doesn't
476 * want to play ball - it's not clear what's right
477 * here - should this be an error? should it just
478 * increase a counter, hmm. For right now, we return
479 * 0 - I don't believe that to be "right". we could
480 * call the gorpy openssl lib error handlers that
481 * print messages to users of the library. hmm..
484 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
496 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
497 * gets called when libcrypto requests a cipher NID.
501 const EVP_CIPHER cryptodev_des_cbc = {
508 sizeof(struct dev_crypto_state),
509 EVP_CIPHER_set_asn1_iv,
510 EVP_CIPHER_get_asn1_iv,
515 const EVP_CIPHER cryptodev_3des_cbc = {
522 sizeof(struct dev_crypto_state),
523 EVP_CIPHER_set_asn1_iv,
524 EVP_CIPHER_get_asn1_iv,
528 const EVP_CIPHER cryptodev_bf_cbc = {
535 sizeof(struct dev_crypto_state),
536 EVP_CIPHER_set_asn1_iv,
537 EVP_CIPHER_get_asn1_iv,
541 const EVP_CIPHER cryptodev_cast_cbc = {
548 sizeof(struct dev_crypto_state),
549 EVP_CIPHER_set_asn1_iv,
550 EVP_CIPHER_get_asn1_iv,
554 const EVP_CIPHER cryptodev_aes_cbc = {
561 sizeof(struct dev_crypto_state),
562 EVP_CIPHER_set_asn1_iv,
563 EVP_CIPHER_get_asn1_iv,
568 * Registered by the ENGINE when used to find out how to deal with
569 * a particular NID in the ENGINE. this says what we'll do at the
570 * top level - note, that list is restricted by what we answer with
573 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
574 const int **nids, int nid)
577 return (cryptodev_usable_ciphers(nids));
580 case NID_des_ede3_cbc:
581 *cipher = &cryptodev_3des_cbc;
584 *cipher = &cryptodev_des_cbc;
587 *cipher = &cryptodev_bf_cbc;
590 *cipher = &cryptodev_cast_cbc;
592 case NID_aes_128_cbc:
593 *cipher = &cryptodev_aes_cbc;
599 return (*cipher != NULL);
603 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
604 const int **nids, int nid)
607 return (cryptodev_usable_digests(nids));
611 *digest = NULL; /* need to make a clean md5 critter */
617 return (*digest != NULL);
621 * Convert a BIGNUM to the representation that /dev/crypto needs.
622 * Upon completion of use, the caller is responsible for freeing
626 bn2crparam(const BIGNUM *a, struct crparam *crp)
629 ssize_t words, bytes, bits;
635 bits = BN_num_bits(a);
636 bytes = (bits + 7) / 8;
643 crp->crp_nbits = bits;
645 for (i = 0, j = 0; i < a->top; i++) {
646 for (k = 0; k < BN_BITS2 / 8; k++) {
647 if ((j + k) >= bytes)
649 b[j + k] = a->d[i] >> (k * 8);
656 /* Convert a /dev/crypto parameter to a BIGNUM */
658 crparam2bn(struct crparam *crp, BIGNUM *a)
663 bytes = (crp->crp_nbits + 7) / 8;
668 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
671 for (i = 0; i < bytes; i++)
672 pd[i] = crp->crp_p[bytes - i - 1];
674 BN_bin2bn(pd, bytes, a);
681 zapparams(struct crypt_kop *kop)
685 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
686 if (kop->crk_param[i].crp_p)
687 free(kop->crk_param[i].crp_p);
688 kop->crk_param[i].crp_p = NULL;
689 kop->crk_param[i].crp_nbits = 0;
694 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
698 if ((fd = get_asym_dev_crypto()) < 0)
702 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
703 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
707 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
708 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
712 if (ioctl(fd, CIOCKEY, kop) == 0) {
714 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
716 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
724 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
725 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
727 struct crypt_kop kop;
730 /* Currently, we know we can do mod exp iff we can do any
731 * asymmetric operations at all.
733 if (cryptodev_asymfeat == 0) {
734 ret = BN_mod_exp(r, a, p, m, ctx);
738 memset(&kop, 0, sizeof kop);
739 kop.crk_op = CRK_MOD_EXP;
741 /* inputs: a^p % m */
742 if (bn2crparam(a, &kop.crk_param[0]))
744 if (bn2crparam(p, &kop.crk_param[1]))
746 if (bn2crparam(m, &kop.crk_param[2]))
750 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
751 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
752 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
760 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
766 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
772 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
774 struct crypt_kop kop;
777 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
778 /* XXX 0 means failure?? */
782 memset(&kop, 0, sizeof kop);
783 kop.crk_op = CRK_MOD_EXP_CRT;
784 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
785 if (bn2crparam(rsa->p, &kop.crk_param[0]))
787 if (bn2crparam(rsa->q, &kop.crk_param[1]))
789 if (bn2crparam(I, &kop.crk_param[2]))
791 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
793 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
795 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
799 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
800 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
801 ret = (*meth->rsa_mod_exp)(r0, I, rsa);
808 static RSA_METHOD cryptodev_rsa = {
809 "cryptodev RSA method",
810 NULL, /* rsa_pub_enc */
811 NULL, /* rsa_pub_dec */
812 NULL, /* rsa_priv_enc */
813 NULL, /* rsa_priv_dec */
821 NULL /* rsa_verify */
825 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
826 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
828 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
832 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
833 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
834 BN_CTX *ctx, BN_MONT_CTX *mont)
841 /* v = ( g^u1 * y^u2 mod p ) mod q */
842 /* let t1 = g ^ u1 mod p */
845 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
848 /* let t2 = y ^ u2 mod p */
849 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
851 /* let u1 = t1 * t2 mod p */
852 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
864 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
866 struct crypt_kop kop;
867 BIGNUM *r = NULL, *s = NULL;
868 DSA_SIG *dsaret = NULL;
870 if ((r = BN_new()) == NULL)
872 if ((s = BN_new()) == NULL) {
878 memset(&kop, 0, sizeof kop);
879 kop.crk_op = CRK_DSA_SIGN;
881 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
882 kop.crk_param[0].crp_p = (caddr_t)dgst;
883 kop.crk_param[0].crp_nbits = dlen * 8;
884 if (bn2crparam(dsa->p, &kop.crk_param[1]))
886 if (bn2crparam(dsa->q, &kop.crk_param[2]))
888 if (bn2crparam(dsa->g, &kop.crk_param[3]))
890 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
894 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
895 BN_num_bytes(dsa->q), s) == 0) {
896 dsaret = DSA_SIG_new();
900 const DSA_METHOD *meth = DSA_OpenSSL();
903 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
906 kop.crk_param[0].crp_p = NULL;
912 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
913 DSA_SIG *sig, DSA *dsa)
915 struct crypt_kop kop;
918 memset(&kop, 0, sizeof kop);
919 kop.crk_op = CRK_DSA_VERIFY;
921 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
922 kop.crk_param[0].crp_p = (caddr_t)dgst;
923 kop.crk_param[0].crp_nbits = dlen * 8;
924 if (bn2crparam(dsa->p, &kop.crk_param[1]))
926 if (bn2crparam(dsa->q, &kop.crk_param[2]))
928 if (bn2crparam(dsa->g, &kop.crk_param[3]))
930 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
932 if (bn2crparam(sig->r, &kop.crk_param[5]))
934 if (bn2crparam(sig->s, &kop.crk_param[6]))
938 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
939 dsaret = kop.crk_status;
941 const DSA_METHOD *meth = DSA_OpenSSL();
943 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
946 kop.crk_param[0].crp_p = NULL;
951 static DSA_METHOD cryptodev_dsa = {
952 "cryptodev DSA method",
954 NULL, /* dsa_sign_setup */
956 NULL, /* dsa_mod_exp */
965 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
966 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
969 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
973 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
975 struct crypt_kop kop;
979 if ((fd = get_asym_dev_crypto()) < 0) {
980 const DH_METHOD *meth = DH_OpenSSL();
982 return ((meth->compute_key)(key, pub_key, dh));
985 keylen = BN_num_bits(dh->p);
987 memset(&kop, 0, sizeof kop);
988 kop.crk_op = CRK_DH_COMPUTE_KEY;
990 /* inputs: dh->priv_key pub_key dh->p key */
991 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
993 if (bn2crparam(pub_key, &kop.crk_param[1]))
995 if (bn2crparam(dh->p, &kop.crk_param[2]))
999 kop.crk_param[3].crp_p = key;
1000 kop.crk_param[3].crp_nbits = keylen * 8;
1001 kop.crk_oparams = 1;
1003 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1004 const DH_METHOD *meth = DH_OpenSSL();
1006 dhret = (meth->compute_key)(key, pub_key, dh);
1009 kop.crk_param[3].crp_p = NULL;
1014 static DH_METHOD cryptodev_dh = {
1015 "cryptodev DH method",
1016 NULL, /* cryptodev_dh_generate_key */
1026 * ctrl right now is just a wrapper that doesn't do much
1027 * but I expect we'll want some options soon.
1030 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1032 #ifdef HAVE_SYSLOG_R
1033 struct syslog_data sd = SYSLOG_DATA_INIT;
1038 #ifdef HAVE_SYSLOG_R
1039 syslog_r(LOG_ERR, &sd,
1040 "cryptodev_ctrl: unknown command %d", cmd);
1042 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1050 ENGINE_load_cryptodev(void)
1052 ENGINE *engine = ENGINE_new();
1057 if ((fd = get_dev_crypto()) < 0)
1061 * find out what asymmetric crypto algorithms we support
1063 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
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 */