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>
39 ENGINE_load_cryptodev(void)
41 /* This is a NOP unless __OpenBSD__ is defined */
45 #else /* __OpenBSD__ */
47 #include <sys/types.h>
48 #include <sys/param.h>
53 ENGINE_load_cryptodev(void)
55 /* This is a NOP unless we have release 3.0 (released december 2001) */
59 #else /* OpenBSD 3.0 or above */
61 #include <crypto/cryptodev.h>
62 #include <sys/ioctl.h>
72 struct dev_crypto_state {
73 struct session_op d_sess;
77 static u_int32_t cryptodev_asymfeat = 0;
79 static int get_asym_dev_crypto(void);
80 static int open_dev_crypto(void);
81 static int get_dev_crypto(void);
82 static int cryptodev_max_iv(int cipher);
83 static int cryptodev_key_length_valid(int cipher, int len);
84 static int cipher_nid_to_cryptodev(int nid);
85 static int get_cryptodev_ciphers(const int **cnids);
86 static int get_cryptodev_digests(const int **cnids);
87 static int cryptodev_usable_ciphers(const int **nids);
88 static int cryptodev_usable_digests(const int **nids);
89 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
90 const unsigned char *in, unsigned int inl);
91 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
92 const unsigned char *iv, int enc);
93 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
94 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
95 const int **nids, int nid);
96 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
97 const int **nids, int nid);
98 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
99 static int crparam2bn(struct crparam *crp, BIGNUM *a);
100 static void zapparams(struct crypt_kop *kop);
101 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
102 int slen, BIGNUM *s);
104 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
105 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
106 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
108 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
109 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
110 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
111 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
112 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
113 BN_CTX *ctx, BN_MONT_CTX *mont);
114 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
116 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
117 DSA_SIG *sig, DSA *dsa);
118 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
119 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
121 static int cryptodev_dh_compute_key(unsigned char *key,
122 const BIGNUM *pub_key, DH *dh);
123 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
125 void ENGINE_load_cryptodev(void);
127 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
137 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
138 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
139 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
140 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
141 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
142 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
143 { 0, NID_undef, 0, 0, },
150 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, },
151 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, },
152 { CRYPTO_MD5_KPDK, NID_undef, },
153 { CRYPTO_SHA1_KPDK, NID_undef, },
154 { CRYPTO_MD5, NID_md5, },
155 { CRYPTO_SHA1, NID_undef, },
160 * Return a fd if /dev/crypto seems usable, 0 otherwise.
163 open_dev_crypto(void)
168 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
171 if (fcntl(fd, F_SETFD, 1) == -1) {
185 if ((fd = open_dev_crypto()) == -1)
187 if (ioctl(fd, CRIOGET, &retfd) == -1)
191 if (fcntl(retfd, F_SETFD, 1) == -1) {
198 /* Caching version for asym operations */
200 get_asym_dev_crypto(void)
205 fd = get_dev_crypto();
210 * XXXX this needs to be set for each alg - and determined from
214 cryptodev_max_iv(int cipher)
218 for (i = 0; ciphers[i].id; i++)
219 if (ciphers[i].id == cipher)
220 return (ciphers[i].ivmax);
225 * XXXX this needs to be set for each alg - and determined from
226 * a running card. For now, fake it out - but most of these
227 * for real devices should return 1 for the supported key
228 * sizes the device can handle.
231 cryptodev_key_length_valid(int cipher, int len)
235 for (i = 0; ciphers[i].id; i++)
236 if (ciphers[i].id == cipher)
237 return (ciphers[i].keylen == len);
241 /* convert libcrypto nids to cryptodev */
243 cipher_nid_to_cryptodev(int nid)
247 for (i = 0; ciphers[i].id; i++)
248 if (ciphers[i].nid == nid)
249 return (ciphers[i].id);
254 * Find out what ciphers /dev/crypto will let us have a session for.
255 * XXX note, that some of these openssl doesn't deal with yet!
256 * returning them here is harmless, as long as we return NULL
257 * when asked for a handler in the cryptodev_engine_ciphers routine
260 get_cryptodev_ciphers(const int **cnids)
262 static int nids[CRYPTO_ALGORITHM_MAX];
263 struct session_op sess;
264 int fd, i, count = 0;
266 if ((fd = get_dev_crypto()) < 0) {
270 memset(&sess, 0, sizeof(sess));
271 sess.key = (caddr_t)"123456781234567812345678";
273 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
274 if (ciphers[i].nid == NID_undef)
276 sess.cipher = ciphers[i].id;
277 sess.keylen = ciphers[i].keylen;
279 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
280 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
281 nids[count++] = ciphers[i].nid;
293 * Find out what digests /dev/crypto will let us have a session for.
294 * XXX note, that some of these openssl doesn't deal with yet!
295 * returning them here is harmless, as long as we return NULL
296 * when asked for a handler in the cryptodev_engine_digests routine
299 get_cryptodev_digests(const int **cnids)
301 static int nids[CRYPTO_ALGORITHM_MAX];
302 struct session_op sess;
303 int fd, i, count = 0;
305 if ((fd = get_dev_crypto()) < 0) {
309 memset(&sess, 0, sizeof(sess));
310 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
311 if (digests[i].nid == NID_undef)
313 sess.mac = digests[i].id;
315 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
316 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
317 nids[count++] = digests[i].nid;
329 * Find the useable ciphers|digests from dev/crypto - this is the first
330 * thing called by the engine init crud which determines what it
331 * can use for ciphers from this engine. We want to return
332 * only what we can do, anythine else is handled by software.
334 * If we can't initialize the device to do anything useful for
335 * any reason, we want to return a NULL array, and 0 length,
336 * which forces everything to be done is software. By putting
337 * the initalization of the device in here, we ensure we can
338 * use this engine as the default, and if for whatever reason
339 * /dev/crypto won't do what we want it will just be done in
342 * This can (should) be greatly expanded to perhaps take into
343 * account speed of the device, and what we want to do.
344 * (although the disabling of particular alg's could be controlled
345 * by the device driver with sysctl's.) - this is where we
346 * want most of the decisions made about what we actually want
347 * to use from /dev/crypto.
350 cryptodev_usable_ciphers(const int **nids)
352 return (get_cryptodev_ciphers(nids));
356 cryptodev_usable_digests(const int **nids)
359 * XXXX just disable all digests for now, because it sucks.
360 * we need a better way to decide this - i.e. I may not
361 * want digests on slow cards like hifn on fast machines,
362 * but might want them on slow or loaded machines, etc.
363 * will also want them when using crypto cards that don't
364 * suck moose gonads - would be nice to be able to decide something
365 * as reasonable default without having hackery that's card dependent.
366 * of course, the default should probably be just do everything,
367 * with perhaps a sysctl to turn algoritms off (or have them off
368 * by default) on cards that generally suck like the hifn.
375 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
376 const unsigned char *in, unsigned int inl)
378 struct crypt_op cryp;
379 struct dev_crypto_state *state = ctx->cipher_data;
380 struct session_op *sess = &state->d_sess;
382 unsigned char save_iv[EVP_MAX_IV_LENGTH];
388 if ((inl % ctx->cipher->block_size) != 0)
391 memset(&cryp, 0, sizeof(cryp));
393 cryp.ses = sess->ses;
396 cryp.src = (caddr_t) in;
397 cryp.dst = (caddr_t) out;
400 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
402 if (ctx->cipher->iv_len) {
403 cryp.iv = (caddr_t) ctx->iv;
405 iiv = (void *) in + inl - ctx->cipher->iv_len;
406 memcpy(save_iv, iiv, ctx->cipher->iv_len);
411 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
412 /* XXX need better errror handling
413 * this can fail for a number of different reasons.
418 if (ctx->cipher->iv_len) {
420 iiv = (void *) out + inl - ctx->cipher->iv_len;
423 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
429 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
430 const unsigned char *iv, int enc)
432 struct dev_crypto_state *state = ctx->cipher_data;
433 struct session_op *sess = &state->d_sess;
436 if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
439 if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
442 if (!cryptodev_key_length_valid(cipher, ctx->key_len))
445 memset(sess, 0, sizeof(struct session_op));
447 if ((state->d_fd = get_dev_crypto()) < 0)
450 sess->key = (unsigned char *)key;
451 sess->keylen = ctx->key_len;
452 sess->cipher = cipher;
454 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
463 * free anything we allocated earlier when initting a
464 * session, and close the session.
467 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
470 struct dev_crypto_state *state = ctx->cipher_data;
471 struct session_op *sess = &state->d_sess;
476 /* XXX if this ioctl fails, someting's wrong. the invoker
477 * may have called us with a bogus ctx, or we could
478 * have a device that for whatever reason just doesn't
479 * want to play ball - it's not clear what's right
480 * here - should this be an error? should it just
481 * increase a counter, hmm. For right now, we return
482 * 0 - I don't believe that to be "right". we could
483 * call the gorpy openssl lib error handlers that
484 * print messages to users of the library. hmm..
487 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
499 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
500 * gets called when libcrypto requests a cipher NID.
504 const EVP_CIPHER cryptodev_des_cbc = {
511 sizeof(struct dev_crypto_state),
512 EVP_CIPHER_set_asn1_iv,
513 EVP_CIPHER_get_asn1_iv,
518 const EVP_CIPHER cryptodev_3des_cbc = {
525 sizeof(struct dev_crypto_state),
526 EVP_CIPHER_set_asn1_iv,
527 EVP_CIPHER_get_asn1_iv,
531 const EVP_CIPHER cryptodev_bf_cbc = {
538 sizeof(struct dev_crypto_state),
539 EVP_CIPHER_set_asn1_iv,
540 EVP_CIPHER_get_asn1_iv,
544 const EVP_CIPHER cryptodev_cast_cbc = {
551 sizeof(struct dev_crypto_state),
552 EVP_CIPHER_set_asn1_iv,
553 EVP_CIPHER_get_asn1_iv,
557 const EVP_CIPHER cryptodev_aes_cbc = {
564 sizeof(struct dev_crypto_state),
565 EVP_CIPHER_set_asn1_iv,
566 EVP_CIPHER_get_asn1_iv,
571 * Registered by the ENGINE when used to find out how to deal with
572 * a particular NID in the ENGINE. this says what we'll do at the
573 * top level - note, that list is restricted by what we answer with
576 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
577 const int **nids, int nid)
580 return (cryptodev_usable_ciphers(nids));
583 case NID_des_ede3_cbc:
584 *cipher = &cryptodev_3des_cbc;
587 *cipher = &cryptodev_des_cbc;
590 *cipher = &cryptodev_bf_cbc;
593 *cipher = &cryptodev_cast_cbc;
595 case NID_aes_128_cbc:
596 *cipher = &cryptodev_aes_cbc;
602 return (*cipher != NULL);
606 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
607 const int **nids, int nid)
610 return (cryptodev_usable_digests(nids));
614 *digest = NULL; /* need to make a clean md5 critter */
620 return (*digest != NULL);
624 * Convert a BIGNUM to the representation that /dev/crypto needs.
625 * Upon completion of use, the caller is responsible for freeing
629 bn2crparam(const BIGNUM *a, struct crparam *crp)
632 ssize_t words, bytes, bits;
638 bits = BN_num_bits(a);
639 bytes = (bits + 7) / 8;
646 crp->crp_nbits = bits;
648 for (i = 0, j = 0; i < a->top; i++) {
649 for (k = 0; k < BN_BITS2 / 8; k++) {
650 if ((j + k) >= bytes)
652 b[j + k] = a->d[i] >> (k * 8);
659 /* Convert a /dev/crypto parameter to a BIGNUM */
661 crparam2bn(struct crparam *crp, BIGNUM *a)
666 bytes = (crp->crp_nbits + 7) / 8;
671 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
674 for (i = 0; i < bytes; i++)
675 pd[i] = crp->crp_p[bytes - i - 1];
677 BN_bin2bn(pd, bytes, a);
684 zapparams(struct crypt_kop *kop)
688 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
689 if (kop->crk_param[i].crp_p)
690 free(kop->crk_param[i].crp_p);
691 kop->crk_param[i].crp_p = NULL;
692 kop->crk_param[i].crp_nbits = 0;
697 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
701 if ((fd = get_asym_dev_crypto()) < 0)
705 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
706 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
710 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
711 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
715 if (ioctl(fd, CIOCKEY, kop) == 0) {
717 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
719 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
727 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
728 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
730 struct crypt_kop kop;
733 /* Currently, we know we can do mod exp iff we can do any
734 * asymmetric operations at all.
736 if (cryptodev_asymfeat == 0) {
737 ret = BN_mod_exp(r, a, p, m, ctx);
741 memset(&kop, 0, sizeof kop);
742 kop.crk_op = CRK_MOD_EXP;
744 /* inputs: a^p % m */
745 if (bn2crparam(a, &kop.crk_param[0]))
747 if (bn2crparam(p, &kop.crk_param[1]))
749 if (bn2crparam(m, &kop.crk_param[2]))
753 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
754 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
755 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
763 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
769 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
775 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
777 struct crypt_kop kop;
780 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
781 /* XXX 0 means failure?? */
785 memset(&kop, 0, sizeof kop);
786 kop.crk_op = CRK_MOD_EXP_CRT;
787 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
788 if (bn2crparam(rsa->p, &kop.crk_param[0]))
790 if (bn2crparam(rsa->q, &kop.crk_param[1]))
792 if (bn2crparam(I, &kop.crk_param[2]))
794 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
796 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
798 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
802 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
803 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
804 ret = (*meth->rsa_mod_exp)(r0, I, rsa);
811 static RSA_METHOD cryptodev_rsa = {
812 "cryptodev RSA method",
813 NULL, /* rsa_pub_enc */
814 NULL, /* rsa_pub_dec */
815 NULL, /* rsa_priv_enc */
816 NULL, /* rsa_priv_dec */
824 NULL /* rsa_verify */
828 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
829 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
831 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
835 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
836 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
837 BN_CTX *ctx, BN_MONT_CTX *mont)
844 /* v = ( g^u1 * y^u2 mod p ) mod q */
845 /* let t1 = g ^ u1 mod p */
848 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
851 /* let t2 = y ^ u2 mod p */
852 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
854 /* let u1 = t1 * t2 mod p */
855 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
867 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
869 struct crypt_kop kop;
870 BIGNUM *r = NULL, *s = NULL;
871 DSA_SIG *dsaret = NULL;
873 if ((r = BN_new()) == NULL)
875 if ((s = BN_new()) == NULL) {
881 memset(&kop, 0, sizeof kop);
882 kop.crk_op = CRK_DSA_SIGN;
884 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
885 kop.crk_param[0].crp_p = (caddr_t)dgst;
886 kop.crk_param[0].crp_nbits = dlen * 8;
887 if (bn2crparam(dsa->p, &kop.crk_param[1]))
889 if (bn2crparam(dsa->q, &kop.crk_param[2]))
891 if (bn2crparam(dsa->g, &kop.crk_param[3]))
893 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
897 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
898 BN_num_bytes(dsa->q), s) == 0) {
899 dsaret = DSA_SIG_new();
903 const DSA_METHOD *meth = DSA_OpenSSL();
906 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
909 kop.crk_param[0].crp_p = NULL;
915 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
916 DSA_SIG *sig, DSA *dsa)
918 struct crypt_kop kop;
921 memset(&kop, 0, sizeof kop);
922 kop.crk_op = CRK_DSA_VERIFY;
924 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
925 kop.crk_param[0].crp_p = (caddr_t)dgst;
926 kop.crk_param[0].crp_nbits = dlen * 8;
927 if (bn2crparam(dsa->p, &kop.crk_param[1]))
929 if (bn2crparam(dsa->q, &kop.crk_param[2]))
931 if (bn2crparam(dsa->g, &kop.crk_param[3]))
933 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
935 if (bn2crparam(sig->r, &kop.crk_param[5]))
937 if (bn2crparam(sig->s, &kop.crk_param[6]))
941 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
942 dsaret = kop.crk_status;
944 const DSA_METHOD *meth = DSA_OpenSSL();
946 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
949 kop.crk_param[0].crp_p = NULL;
954 static DSA_METHOD cryptodev_dsa = {
955 "cryptodev DSA method",
957 NULL, /* dsa_sign_setup */
959 NULL, /* dsa_mod_exp */
968 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
969 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
972 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
976 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
978 struct crypt_kop kop;
982 if ((fd = get_asym_dev_crypto()) < 0) {
983 const DH_METHOD *meth = DH_OpenSSL();
985 return ((meth->compute_key)(key, pub_key, dh));
988 keylen = BN_num_bits(dh->p);
990 memset(&kop, 0, sizeof kop);
991 kop.crk_op = CRK_DH_COMPUTE_KEY;
993 /* inputs: dh->priv_key pub_key dh->p key */
994 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
996 if (bn2crparam(pub_key, &kop.crk_param[1]))
998 if (bn2crparam(dh->p, &kop.crk_param[2]))
1000 kop.crk_iparams = 3;
1002 kop.crk_param[3].crp_p = key;
1003 kop.crk_param[3].crp_nbits = keylen * 8;
1004 kop.crk_oparams = 1;
1006 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1007 const DH_METHOD *meth = DH_OpenSSL();
1009 dhret = (meth->compute_key)(key, pub_key, dh);
1012 kop.crk_param[3].crp_p = NULL;
1017 static DH_METHOD cryptodev_dh = {
1018 "cryptodev DH method",
1019 NULL, /* cryptodev_dh_generate_key */
1029 * ctrl right now is just a wrapper that doesn't do much
1030 * but I expect we'll want some options soon.
1033 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1035 struct syslog_data sd = SYSLOG_DATA_INIT;
1039 syslog_r(LOG_ERR, &sd,
1040 "cryptodev_ctrl: unknown command %d", cmd);
1047 ENGINE_load_cryptodev(void)
1049 ENGINE *engine = ENGINE_new();
1054 if ((fd = get_dev_crypto()) < 0)
1058 * find out what asymmetric crypto algorithms we support
1060 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1066 if (!ENGINE_set_id(engine, "cryptodev") ||
1067 !ENGINE_set_name(engine, "OpenBSD cryptodev engine") ||
1068 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1069 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1070 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1071 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1072 ENGINE_free(engine);
1076 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1077 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1079 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1080 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1081 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1082 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1083 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1084 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1085 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1086 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1087 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1088 cryptodev_rsa.rsa_mod_exp =
1089 cryptodev_rsa_mod_exp;
1091 cryptodev_rsa.rsa_mod_exp =
1092 cryptodev_rsa_nocrt_mod_exp;
1096 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1097 const DSA_METHOD *meth = DSA_OpenSSL();
1099 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1100 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1101 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1102 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1103 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1104 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1106 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1107 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1110 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1111 const DH_METHOD *dh_meth = DH_OpenSSL();
1113 cryptodev_dh.generate_key = dh_meth->generate_key;
1114 cryptodev_dh.compute_key = dh_meth->compute_key;
1115 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1116 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1117 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1118 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1119 cryptodev_dh.compute_key =
1120 cryptodev_dh_compute_key;
1125 ENGINE_free(engine);
1129 #endif /* OpenBSD 3.0 or above */
1130 #endif /* __OpenBSD__ */