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>
33 #include <openssl/dsa.h>
34 #include <openssl/rsa.h>
35 #include <openssl/dh.h>
36 #include <openssl/err.h>
38 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
39 (defined(OpenBSD) || defined(__FreeBSD__))
40 # include <sys/param.h>
41 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
42 # define HAVE_CRYPTODEV
44 # if (OpenBSD >= 200110)
45 # define HAVE_SYSLOG_R
49 #ifndef HAVE_CRYPTODEV
51 void ENGINE_load_cryptodev(void)
53 /* This is a NOP on platforms without /dev/crypto */
59 # include <sys/types.h>
60 # include <crypto/cryptodev.h>
61 # include <sys/ioctl.h>
71 struct dev_crypto_state {
72 struct session_op d_sess;
76 static u_int32_t cryptodev_asymfeat = 0;
78 static int get_asym_dev_crypto(void);
79 static int open_dev_crypto(void);
80 static int get_dev_crypto(void);
81 static int cryptodev_max_iv(int cipher);
82 static int cryptodev_key_length_valid(int cipher, int len);
83 static int cipher_nid_to_cryptodev(int nid);
84 static int get_cryptodev_ciphers(const int **cnids);
86 * static int get_cryptodev_digests(const int **cnids);
88 static int cryptodev_usable_ciphers(const int **nids);
89 static int cryptodev_usable_digests(const int **nids);
90 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
91 const unsigned char *in, unsigned int inl);
92 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
93 const unsigned char *iv, int enc);
94 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
95 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
96 const int **nids, int nid);
97 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
98 const int **nids, int nid);
99 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
100 static int crparam2bn(struct crparam *crp, BIGNUM *a);
101 static void zapparams(struct crypt_kop *kop);
102 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
103 int slen, BIGNUM *s);
105 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
106 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
108 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
110 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
112 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
113 const BIGNUM *p, const BIGNUM *m,
114 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
115 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
116 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
117 BIGNUM *p, BN_CTX *ctx,
119 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
121 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
122 DSA_SIG *sig, DSA *dsa);
123 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
124 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
126 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
128 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
129 void ENGINE_load_cryptodev(void);
131 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
142 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
145 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
148 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
151 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
154 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
157 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
170 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1,
173 CRYPTO_RIPEMD160_HMAC, NID_ripemd160,
176 CRYPTO_MD5_KPDK, NID_undef,
179 CRYPTO_SHA1_KPDK, NID_undef,
185 CRYPTO_SHA1, NID_undef,
194 * Return a fd if /dev/crypto seems usable, 0 otherwise.
196 static int open_dev_crypto(void)
201 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
204 if (fcntl(fd, F_SETFD, 1) == -1) {
213 static int get_dev_crypto(void)
217 if ((fd = open_dev_crypto()) == -1)
219 if (ioctl(fd, CRIOGET, &retfd) == -1)
223 if (fcntl(retfd, F_SETFD, 1) == -1) {
230 /* Caching version for asym operations */
231 static int get_asym_dev_crypto(void)
236 fd = get_dev_crypto();
241 * XXXX this needs to be set for each alg - and determined from
244 static int cryptodev_max_iv(int cipher)
248 for (i = 0; ciphers[i].id; i++)
249 if (ciphers[i].id == cipher)
250 return (ciphers[i].ivmax);
255 * XXXX this needs to be set for each alg - and determined from
256 * a running card. For now, fake it out - but most of these
257 * for real devices should return 1 for the supported key
258 * sizes the device can handle.
260 static int cryptodev_key_length_valid(int cipher, int len)
264 for (i = 0; ciphers[i].id; i++)
265 if (ciphers[i].id == cipher)
266 return (ciphers[i].keylen == len);
270 /* convert libcrypto nids to cryptodev */
271 static int cipher_nid_to_cryptodev(int nid)
275 for (i = 0; ciphers[i].id; i++)
276 if (ciphers[i].nid == nid)
277 return (ciphers[i].id);
282 * Find out what ciphers /dev/crypto will let us have a session for.
283 * XXX note, that some of these openssl doesn't deal with yet!
284 * returning them here is harmless, as long as we return NULL
285 * when asked for a handler in the cryptodev_engine_ciphers routine
287 static int get_cryptodev_ciphers(const int **cnids)
289 static int nids[CRYPTO_ALGORITHM_MAX];
290 struct session_op sess;
291 int fd, i, count = 0;
293 if ((fd = get_dev_crypto()) < 0) {
297 memset(&sess, 0, sizeof(sess));
298 sess.key = (caddr_t) "123456781234567812345678";
300 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
301 if (ciphers[i].nid == NID_undef)
303 sess.cipher = ciphers[i].id;
304 sess.keylen = ciphers[i].keylen;
306 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
307 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
308 nids[count++] = ciphers[i].nid;
321 * Find out what digests /dev/crypto will let us have a session for.
322 * XXX note, that some of these openssl doesn't deal with yet!
323 * returning them here is harmless, as long as we return NULL
324 * when asked for a handler in the cryptodev_engine_digests routine
326 static int get_cryptodev_digests(const int **cnids)
328 static int nids[CRYPTO_ALGORITHM_MAX];
329 struct session_op sess;
330 int fd, i, count = 0;
332 if ((fd = get_dev_crypto()) < 0) {
336 memset(&sess, 0, sizeof(sess));
337 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
338 if (digests[i].nid == NID_undef)
340 sess.mac = digests[i].id;
342 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
343 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
344 nids[count++] = digests[i].nid;
358 * Find the useable ciphers|digests from dev/crypto - this is the first
359 * thing called by the engine init crud which determines what it
360 * can use for ciphers from this engine. We want to return
361 * only what we can do, anythine else is handled by software.
363 * If we can't initialize the device to do anything useful for
364 * any reason, we want to return a NULL array, and 0 length,
365 * which forces everything to be done is software. By putting
366 * the initalization of the device in here, we ensure we can
367 * use this engine as the default, and if for whatever reason
368 * /dev/crypto won't do what we want it will just be done in
371 * This can (should) be greatly expanded to perhaps take into
372 * account speed of the device, and what we want to do.
373 * (although the disabling of particular alg's could be controlled
374 * by the device driver with sysctl's.) - this is where we
375 * want most of the decisions made about what we actually want
376 * to use from /dev/crypto.
378 static int cryptodev_usable_ciphers(const int **nids)
380 return (get_cryptodev_ciphers(nids));
383 static int cryptodev_usable_digests(const int **nids)
386 * XXXX just disable all digests for now, because it sucks.
387 * we need a better way to decide this - i.e. I may not
388 * want digests on slow cards like hifn on fast machines,
389 * but might want them on slow or loaded machines, etc.
390 * will also want them when using crypto cards that don't
391 * suck moose gonads - would be nice to be able to decide something
392 * as reasonable default without having hackery that's card dependent.
393 * of course, the default should probably be just do everything,
394 * with perhaps a sysctl to turn algoritms off (or have them off
395 * by default) on cards that generally suck like the hifn.
402 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
403 const unsigned char *in, unsigned int inl)
405 struct crypt_op cryp;
406 struct dev_crypto_state *state = ctx->cipher_data;
407 struct session_op *sess = &state->d_sess;
409 unsigned char save_iv[EVP_MAX_IV_LENGTH];
415 if ((inl % ctx->cipher->block_size) != 0)
418 memset(&cryp, 0, sizeof(cryp));
420 cryp.ses = sess->ses;
423 cryp.src = (caddr_t) in;
424 cryp.dst = (caddr_t) out;
427 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
429 if (ctx->cipher->iv_len) {
430 cryp.iv = (caddr_t) ctx->iv;
432 iiv = in + inl - ctx->cipher->iv_len;
433 memcpy(save_iv, iiv, ctx->cipher->iv_len);
438 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
440 * XXX need better errror handling this can fail for a number of
446 if (ctx->cipher->iv_len) {
448 iiv = out + inl - ctx->cipher->iv_len;
451 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
457 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
458 const unsigned char *iv, int enc)
460 struct dev_crypto_state *state = ctx->cipher_data;
461 struct session_op *sess = &state->d_sess;
464 if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
467 if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
470 if (!cryptodev_key_length_valid(cipher, ctx->key_len))
473 memset(sess, 0, sizeof(struct session_op));
475 if ((state->d_fd = get_dev_crypto()) < 0)
478 sess->key = (char *)key;
479 sess->keylen = ctx->key_len;
480 sess->cipher = cipher;
482 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
491 * free anything we allocated earlier when initting a
492 * session, and close the session.
494 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
497 struct dev_crypto_state *state = ctx->cipher_data;
498 struct session_op *sess = &state->d_sess;
504 * XXX if this ioctl fails, someting's wrong. the invoker may have called
505 * us with a bogus ctx, or we could have a device that for whatever
506 * reason just doesn't want to play ball - it's not clear what's right
507 * here - should this be an error? should it just increase a counter,
508 * hmm. For right now, we return 0 - I don't believe that to be "right".
509 * we could call the gorpy openssl lib error handlers that print messages
510 * to users of the library. hmm..
513 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
525 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
526 * gets called when libcrypto requests a cipher NID.
530 const EVP_CIPHER cryptodev_des_cbc = {
537 sizeof(struct dev_crypto_state),
538 EVP_CIPHER_set_asn1_iv,
539 EVP_CIPHER_get_asn1_iv,
544 const EVP_CIPHER cryptodev_3des_cbc = {
551 sizeof(struct dev_crypto_state),
552 EVP_CIPHER_set_asn1_iv,
553 EVP_CIPHER_get_asn1_iv,
557 const EVP_CIPHER cryptodev_bf_cbc = {
564 sizeof(struct dev_crypto_state),
565 EVP_CIPHER_set_asn1_iv,
566 EVP_CIPHER_get_asn1_iv,
570 const EVP_CIPHER cryptodev_cast_cbc = {
577 sizeof(struct dev_crypto_state),
578 EVP_CIPHER_set_asn1_iv,
579 EVP_CIPHER_get_asn1_iv,
583 const EVP_CIPHER cryptodev_aes_cbc = {
590 sizeof(struct dev_crypto_state),
591 EVP_CIPHER_set_asn1_iv,
592 EVP_CIPHER_get_asn1_iv,
597 * Registered by the ENGINE when used to find out how to deal with
598 * a particular NID in the ENGINE. this says what we'll do at the
599 * top level - note, that list is restricted by what we answer with
602 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
603 const int **nids, int nid)
606 return (cryptodev_usable_ciphers(nids));
609 case NID_des_ede3_cbc:
610 *cipher = &cryptodev_3des_cbc;
613 *cipher = &cryptodev_des_cbc;
616 *cipher = &cryptodev_bf_cbc;
619 *cipher = &cryptodev_cast_cbc;
621 case NID_aes_128_cbc:
622 *cipher = &cryptodev_aes_cbc;
628 return (*cipher != NULL);
632 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
633 const int **nids, int nid)
636 return (cryptodev_usable_digests(nids));
640 *digest = NULL; /* need to make a clean md5 critter */
646 return (*digest != NULL);
650 * Convert a BIGNUM to the representation that /dev/crypto needs.
651 * Upon completion of use, the caller is responsible for freeing
654 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
663 bits = BN_num_bits(a);
664 bytes = (bits + 7) / 8;
670 crp->crp_p = (char *)b;
671 crp->crp_nbits = bits;
673 for (i = 0, j = 0; i < a->top; i++) {
674 for (k = 0; k < BN_BITS2 / 8; k++) {
675 if ((j + k) >= bytes)
677 b[j + k] = a->d[i] >> (k * 8);
684 /* Convert a /dev/crypto parameter to a BIGNUM */
685 static int crparam2bn(struct crparam *crp, BIGNUM *a)
690 bytes = (crp->crp_nbits + 7) / 8;
695 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
698 for (i = 0; i < bytes; i++)
699 pd[i] = crp->crp_p[bytes - i - 1];
701 BN_bin2bn(pd, bytes, a);
707 static void zapparams(struct crypt_kop *kop)
711 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
712 if (kop->crk_param[i].crp_p)
713 free(kop->crk_param[i].crp_p);
714 kop->crk_param[i].crp_p = NULL;
715 kop->crk_param[i].crp_nbits = 0;
720 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
725 if ((fd = get_asym_dev_crypto()) < 0)
729 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
730 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
734 kop->crk_param[kop->crk_iparams + 1].crp_p =
735 calloc(slen, sizeof(char));
736 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
740 if (ioctl(fd, CIOCKEY, kop) == 0) {
742 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
744 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
752 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
753 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
755 struct crypt_kop kop;
759 * Currently, we know we can do mod exp iff we can do any asymmetric
762 if (cryptodev_asymfeat == 0) {
763 ret = BN_mod_exp(r, a, p, m, ctx);
767 memset(&kop, 0, sizeof kop);
768 kop.crk_op = CRK_MOD_EXP;
770 /* inputs: a^p % m */
771 if (bn2crparam(a, &kop.crk_param[0]))
773 if (bn2crparam(p, &kop.crk_param[1]))
775 if (bn2crparam(m, &kop.crk_param[2]))
779 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
780 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
781 printf("OCF asym process failed, Running in software\n");
782 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
784 } else if (ECANCELED == kop.crk_status) {
785 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
786 printf("OCF hardware operation cancelled. Running in Software\n");
787 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
789 /* else cryptodev operation worked ok ==> ret = 1 */
797 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
802 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
807 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
809 struct crypt_kop kop;
812 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
813 /* XXX 0 means failure?? */
817 memset(&kop, 0, sizeof kop);
818 kop.crk_op = CRK_MOD_EXP_CRT;
819 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
820 if (bn2crparam(rsa->p, &kop.crk_param[0]))
822 if (bn2crparam(rsa->q, &kop.crk_param[1]))
824 if (bn2crparam(I, &kop.crk_param[2]))
826 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
828 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
830 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
834 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
835 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
836 printf("OCF asym process failed, running in Software\n");
837 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
839 } else if (ECANCELED == kop.crk_status) {
840 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
841 printf("OCF hardware operation cancelled. Running in Software\n");
842 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
844 /* else cryptodev operation worked ok ==> ret = 1 */
851 static RSA_METHOD cryptodev_rsa = {
852 "cryptodev RSA method",
853 NULL, /* rsa_pub_enc */
854 NULL, /* rsa_pub_dec */
855 NULL, /* rsa_priv_enc */
856 NULL, /* rsa_priv_dec */
864 NULL /* rsa_verify */
868 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
869 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
871 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
875 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
876 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
877 BN_CTX *ctx, BN_MONT_CTX *mont)
884 /* v = ( g^u1 * y^u2 mod p ) mod q */
885 /* let t1 = g ^ u1 mod p */
888 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
891 /* let t2 = y ^ u2 mod p */
892 if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
894 /* let u1 = t1 * t2 mod p */
895 if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
906 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
909 struct crypt_kop kop;
910 BIGNUM *r = NULL, *s = NULL;
911 DSA_SIG *dsaret = NULL;
913 if ((r = BN_new()) == NULL)
915 if ((s = BN_new()) == NULL) {
920 memset(&kop, 0, sizeof kop);
921 kop.crk_op = CRK_DSA_SIGN;
923 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
924 kop.crk_param[0].crp_p = (caddr_t) dgst;
925 kop.crk_param[0].crp_nbits = dlen * 8;
926 if (bn2crparam(dsa->p, &kop.crk_param[1]))
928 if (bn2crparam(dsa->q, &kop.crk_param[2]))
930 if (bn2crparam(dsa->g, &kop.crk_param[3]))
932 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
936 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
937 BN_num_bytes(dsa->q), s) == 0) {
938 dsaret = DSA_SIG_new();
942 const DSA_METHOD *meth = DSA_OpenSSL();
945 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
948 kop.crk_param[0].crp_p = NULL;
954 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
955 DSA_SIG *sig, DSA *dsa)
957 struct crypt_kop kop;
960 memset(&kop, 0, sizeof kop);
961 kop.crk_op = CRK_DSA_VERIFY;
963 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
964 kop.crk_param[0].crp_p = (caddr_t) dgst;
965 kop.crk_param[0].crp_nbits = dlen * 8;
966 if (bn2crparam(dsa->p, &kop.crk_param[1]))
968 if (bn2crparam(dsa->q, &kop.crk_param[2]))
970 if (bn2crparam(dsa->g, &kop.crk_param[3]))
972 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
974 if (bn2crparam(sig->r, &kop.crk_param[5]))
976 if (bn2crparam(sig->s, &kop.crk_param[6]))
980 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
982 * OCF success value is 0, if not zero, change dsaret to fail
984 if (0 != kop.crk_status)
987 const DSA_METHOD *meth = DSA_OpenSSL();
989 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
992 kop.crk_param[0].crp_p = NULL;
997 static DSA_METHOD cryptodev_dsa = {
998 "cryptodev DSA method",
1000 NULL, /* dsa_sign_setup */
1002 NULL, /* dsa_mod_exp */
1011 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1012 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1015 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1019 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1021 struct crypt_kop kop;
1025 if ((fd = get_asym_dev_crypto()) < 0) {
1026 const DH_METHOD *meth = DH_OpenSSL();
1028 return ((meth->compute_key) (key, pub_key, dh));
1031 keylen = BN_num_bits(dh->p);
1033 memset(&kop, 0, sizeof kop);
1034 kop.crk_op = CRK_DH_COMPUTE_KEY;
1036 /* inputs: dh->priv_key pub_key dh->p key */
1037 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1039 if (bn2crparam(pub_key, &kop.crk_param[1]))
1041 if (bn2crparam(dh->p, &kop.crk_param[2]))
1043 kop.crk_iparams = 3;
1045 kop.crk_param[3].crp_p = (char *)key;
1046 kop.crk_param[3].crp_nbits = keylen * 8;
1047 kop.crk_oparams = 1;
1049 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1050 const DH_METHOD *meth = DH_OpenSSL();
1052 dhret = (meth->compute_key) (key, pub_key, dh);
1055 kop.crk_param[3].crp_p = NULL;
1060 static DH_METHOD cryptodev_dh = {
1061 "cryptodev DH method",
1062 NULL, /* cryptodev_dh_generate_key */
1072 * ctrl right now is just a wrapper that doesn't do much
1073 * but I expect we'll want some options soon.
1075 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
1077 # ifdef HAVE_SYSLOG_R
1078 struct syslog_data sd = SYSLOG_DATA_INIT;
1083 # ifdef HAVE_SYSLOG_R
1084 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1086 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1093 void ENGINE_load_cryptodev(void)
1095 ENGINE *engine = ENGINE_new();
1100 if ((fd = get_dev_crypto()) < 0) {
1101 ENGINE_free(engine);
1106 * find out what asymmetric crypto algorithms we support
1108 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1110 ENGINE_free(engine);
1115 if (!ENGINE_set_id(engine, "cryptodev") ||
1116 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1117 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1118 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1119 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1120 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1121 ENGINE_free(engine);
1125 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1126 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1128 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1129 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1130 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1131 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1132 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1133 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1134 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1135 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1136 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1137 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1139 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1143 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1144 const DSA_METHOD *meth = DSA_OpenSSL();
1146 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1147 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1148 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1149 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1150 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1151 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1153 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1154 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1157 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1158 const DH_METHOD *dh_meth = DH_OpenSSL();
1160 cryptodev_dh.generate_key = dh_meth->generate_key;
1161 cryptodev_dh.compute_key = dh_meth->compute_key;
1162 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1163 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1164 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1165 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1166 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1171 ENGINE_free(engine);
1175 #endif /* HAVE_CRYPTODEV */