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 (defined(OpenBSD) || defined(__FreeBSD_version))
38 #include <sys/param.h>
39 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
40 # define HAVE_CRYPTODEV
42 # if (OpenBSD >= 200110)
43 # define HAVE_SYSLOG_R
47 #ifndef HAVE_CRYPTODEV
50 ENGINE_load_cryptodev(void)
52 /* This is a NOP on platforms without /dev/crypto */
58 #include <sys/types.h>
59 #include <crypto/cryptodev.h>
60 #include <sys/ioctl.h>
70 struct dev_crypto_state
{
71 struct session_op d_sess
;
75 static u_int32_t cryptodev_asymfeat
= 0;
77 static int get_asym_dev_crypto(void);
78 static int open_dev_crypto(void);
79 static int get_dev_crypto(void);
80 static int cryptodev_max_iv(int cipher
);
81 static int cryptodev_key_length_valid(int cipher
, int len
);
82 static int cipher_nid_to_cryptodev(int nid
);
83 static int get_cryptodev_ciphers(const int **cnids
);
84 static int get_cryptodev_digests(const int **cnids
);
85 static int cryptodev_usable_ciphers(const int **nids
);
86 static int cryptodev_usable_digests(const int **nids
);
87 static int cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
88 const unsigned char *in
, unsigned int inl
);
89 static int cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
90 const unsigned char *iv
, int enc
);
91 static int cryptodev_cleanup(EVP_CIPHER_CTX
*ctx
);
92 static int cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
93 const int **nids
, int nid
);
94 static int cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
95 const int **nids
, int nid
);
96 static int bn2crparam(const BIGNUM
*a
, struct crparam
*crp
);
97 static int crparam2bn(struct crparam
*crp
, BIGNUM
*a
);
98 static void zapparams(struct crypt_kop
*kop
);
99 static int cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
,
100 int slen
, BIGNUM
*s
);
102 static int cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
,
103 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
104 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
,
106 static int cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
);
107 static int cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
108 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
109 static int cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
110 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
111 BN_CTX
*ctx
, BN_MONT_CTX
*mont
);
112 static DSA_SIG
*cryptodev_dsa_do_sign(const unsigned char *dgst
,
114 static int cryptodev_dsa_verify(const unsigned char *dgst
, int dgst_len
,
115 DSA_SIG
*sig
, DSA
*dsa
);
116 static int cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
117 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
119 static int cryptodev_dh_compute_key(unsigned char *key
,
120 const BIGNUM
*pub_key
, DH
*dh
);
121 static int cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
,
123 void ENGINE_load_cryptodev(void);
125 static const ENGINE_CMD_DEFN cryptodev_defns
[] = {
135 { CRYPTO_DES_CBC
, NID_des_cbc
, 8, 8, },
136 { CRYPTO_3DES_CBC
, NID_des_ede3_cbc
, 8, 24, },
137 { CRYPTO_AES_CBC
, NID_aes_128_cbc
, 16, 16, },
138 { CRYPTO_BLF_CBC
, NID_bf_cbc
, 8, 16, },
139 { CRYPTO_CAST_CBC
, NID_cast5_cbc
, 8, 16, },
140 { CRYPTO_SKIPJACK_CBC
, NID_undef
, 0, 0, },
141 { 0, NID_undef
, 0, 0, },
148 { CRYPTO_SHA1_HMAC
, NID_hmacWithSHA1
, },
149 { CRYPTO_RIPEMD160_HMAC
, NID_ripemd160
, },
150 { CRYPTO_MD5_KPDK
, NID_undef
, },
151 { CRYPTO_SHA1_KPDK
, NID_undef
, },
152 { CRYPTO_MD5
, NID_md5
, },
153 { CRYPTO_SHA1
, NID_undef
, },
158 * Return a fd if /dev/crypto seems usable, 0 otherwise.
161 open_dev_crypto(void)
166 if ((fd
= open("/dev/crypto", O_RDWR
, 0)) == -1)
169 if (fcntl(fd
, F_SETFD
, 1) == -1) {
183 if ((fd
= open_dev_crypto()) == -1)
185 if (ioctl(fd
, CRIOGET
, &retfd
) == -1)
189 if (fcntl(retfd
, F_SETFD
, 1) == -1) {
196 /* Caching version for asym operations */
198 get_asym_dev_crypto(void)
203 fd
= get_dev_crypto();
208 * XXXX this needs to be set for each alg - and determined from
212 cryptodev_max_iv(int cipher
)
216 for (i
= 0; ciphers
[i
].id
; i
++)
217 if (ciphers
[i
].id
== cipher
)
218 return (ciphers
[i
].ivmax
);
223 * XXXX this needs to be set for each alg - and determined from
224 * a running card. For now, fake it out - but most of these
225 * for real devices should return 1 for the supported key
226 * sizes the device can handle.
229 cryptodev_key_length_valid(int cipher
, int len
)
233 for (i
= 0; ciphers
[i
].id
; i
++)
234 if (ciphers
[i
].id
== cipher
)
235 return (ciphers
[i
].keylen
== len
);
239 /* convert libcrypto nids to cryptodev */
241 cipher_nid_to_cryptodev(int nid
)
245 for (i
= 0; ciphers
[i
].id
; i
++)
246 if (ciphers
[i
].nid
== nid
)
247 return (ciphers
[i
].id
);
252 * Find out what ciphers /dev/crypto will let us have a session for.
253 * XXX note, that some of these openssl doesn't deal with yet!
254 * returning them here is harmless, as long as we return NULL
255 * when asked for a handler in the cryptodev_engine_ciphers routine
258 get_cryptodev_ciphers(const int **cnids
)
260 static int nids
[CRYPTO_ALGORITHM_MAX
];
261 struct session_op sess
;
262 int fd
, i
, count
= 0;
264 if ((fd
= get_dev_crypto()) < 0) {
268 memset(&sess
, 0, sizeof(sess
));
269 sess
.key
= (caddr_t
)"123456781234567812345678";
271 for (i
= 0; ciphers
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
272 if (ciphers
[i
].nid
== NID_undef
)
274 sess
.cipher
= ciphers
[i
].id
;
275 sess
.keylen
= ciphers
[i
].keylen
;
277 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
278 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
279 nids
[count
++] = ciphers
[i
].nid
;
291 * Find out what digests /dev/crypto will let us have a session for.
292 * XXX note, that some of these openssl doesn't deal with yet!
293 * returning them here is harmless, as long as we return NULL
294 * when asked for a handler in the cryptodev_engine_digests routine
297 get_cryptodev_digests(const int **cnids
)
299 static int nids
[CRYPTO_ALGORITHM_MAX
];
300 struct session_op sess
;
301 int fd
, i
, count
= 0;
303 if ((fd
= get_dev_crypto()) < 0) {
307 memset(&sess
, 0, sizeof(sess
));
308 for (i
= 0; digests
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
309 if (digests
[i
].nid
== NID_undef
)
311 sess
.mac
= digests
[i
].id
;
313 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
314 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
315 nids
[count
++] = digests
[i
].nid
;
327 * Find the useable ciphers|digests from dev/crypto - this is the first
328 * thing called by the engine init crud which determines what it
329 * can use for ciphers from this engine. We want to return
330 * only what we can do, anythine else is handled by software.
332 * If we can't initialize the device to do anything useful for
333 * any reason, we want to return a NULL array, and 0 length,
334 * which forces everything to be done is software. By putting
335 * the initalization of the device in here, we ensure we can
336 * use this engine as the default, and if for whatever reason
337 * /dev/crypto won't do what we want it will just be done in
340 * This can (should) be greatly expanded to perhaps take into
341 * account speed of the device, and what we want to do.
342 * (although the disabling of particular alg's could be controlled
343 * by the device driver with sysctl's.) - this is where we
344 * want most of the decisions made about what we actually want
345 * to use from /dev/crypto.
348 cryptodev_usable_ciphers(const int **nids
)
350 return (get_cryptodev_ciphers(nids
));
354 cryptodev_usable_digests(const int **nids
)
357 * XXXX just disable all digests for now, because it sucks.
358 * we need a better way to decide this - i.e. I may not
359 * want digests on slow cards like hifn on fast machines,
360 * but might want them on slow or loaded machines, etc.
361 * will also want them when using crypto cards that don't
362 * suck moose gonads - would be nice to be able to decide something
363 * as reasonable default without having hackery that's card dependent.
364 * of course, the default should probably be just do everything,
365 * with perhaps a sysctl to turn algoritms off (or have them off
366 * by default) on cards that generally suck like the hifn.
373 cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
374 const unsigned char *in
, unsigned int inl
)
376 struct crypt_op cryp
;
377 struct dev_crypto_state
*state
= ctx
->cipher_data
;
378 struct session_op
*sess
= &state
->d_sess
;
380 unsigned char save_iv
[EVP_MAX_IV_LENGTH
];
386 if ((inl
% ctx
->cipher
->block_size
) != 0)
389 memset(&cryp
, 0, sizeof(cryp
));
391 cryp
.ses
= sess
->ses
;
394 cryp
.src
= (caddr_t
) in
;
395 cryp
.dst
= (caddr_t
) out
;
398 cryp
.op
= ctx
->encrypt
? COP_ENCRYPT
: COP_DECRYPT
;
400 if (ctx
->cipher
->iv_len
) {
401 cryp
.iv
= (caddr_t
) ctx
->iv
;
403 iiv
= (void *) in
+ inl
- ctx
->cipher
->iv_len
;
404 memcpy(save_iv
, iiv
, ctx
->cipher
->iv_len
);
409 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) == -1) {
410 /* XXX need better errror handling
411 * this can fail for a number of different reasons.
416 if (ctx
->cipher
->iv_len
) {
418 iiv
= (void *) out
+ inl
- ctx
->cipher
->iv_len
;
421 memcpy(ctx
->iv
, iiv
, ctx
->cipher
->iv_len
);
427 cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
428 const unsigned char *iv
, int enc
)
430 struct dev_crypto_state
*state
= ctx
->cipher_data
;
431 struct session_op
*sess
= &state
->d_sess
;
434 if ((cipher
= cipher_nid_to_cryptodev(ctx
->cipher
->nid
)) == NID_undef
)
437 if (ctx
->cipher
->iv_len
> cryptodev_max_iv(cipher
))
440 if (!cryptodev_key_length_valid(cipher
, ctx
->key_len
))
443 memset(sess
, 0, sizeof(struct session_op
));
445 if ((state
->d_fd
= get_dev_crypto()) < 0)
448 sess
->key
= (unsigned char *)key
;
449 sess
->keylen
= ctx
->key_len
;
450 sess
->cipher
= cipher
;
452 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) == -1) {
461 * free anything we allocated earlier when initting a
462 * session, and close the session.
465 cryptodev_cleanup(EVP_CIPHER_CTX
*ctx
)
468 struct dev_crypto_state
*state
= ctx
->cipher_data
;
469 struct session_op
*sess
= &state
->d_sess
;
474 /* XXX if this ioctl fails, someting's wrong. the invoker
475 * may have called us with a bogus ctx, or we could
476 * have a device that for whatever reason just doesn't
477 * want to play ball - it's not clear what's right
478 * here - should this be an error? should it just
479 * increase a counter, hmm. For right now, we return
480 * 0 - I don't believe that to be "right". we could
481 * call the gorpy openssl lib error handlers that
482 * print messages to users of the library. hmm..
485 if (ioctl(state
->d_fd
, CIOCFSESSION
, &sess
->ses
) == -1) {
497 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
498 * gets called when libcrypto requests a cipher NID.
502 const EVP_CIPHER cryptodev_des_cbc
= {
509 sizeof(struct dev_crypto_state
),
510 EVP_CIPHER_set_asn1_iv
,
511 EVP_CIPHER_get_asn1_iv
,
516 const EVP_CIPHER cryptodev_3des_cbc
= {
523 sizeof(struct dev_crypto_state
),
524 EVP_CIPHER_set_asn1_iv
,
525 EVP_CIPHER_get_asn1_iv
,
529 const EVP_CIPHER cryptodev_bf_cbc
= {
536 sizeof(struct dev_crypto_state
),
537 EVP_CIPHER_set_asn1_iv
,
538 EVP_CIPHER_get_asn1_iv
,
542 const EVP_CIPHER cryptodev_cast_cbc
= {
549 sizeof(struct dev_crypto_state
),
550 EVP_CIPHER_set_asn1_iv
,
551 EVP_CIPHER_get_asn1_iv
,
555 const EVP_CIPHER cryptodev_aes_cbc
= {
562 sizeof(struct dev_crypto_state
),
563 EVP_CIPHER_set_asn1_iv
,
564 EVP_CIPHER_get_asn1_iv
,
569 * Registered by the ENGINE when used to find out how to deal with
570 * a particular NID in the ENGINE. this says what we'll do at the
571 * top level - note, that list is restricted by what we answer with
574 cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
575 const int **nids
, int nid
)
578 return (cryptodev_usable_ciphers(nids
));
581 case NID_des_ede3_cbc
:
582 *cipher
= &cryptodev_3des_cbc
;
585 *cipher
= &cryptodev_des_cbc
;
588 *cipher
= &cryptodev_bf_cbc
;
591 *cipher
= &cryptodev_cast_cbc
;
593 case NID_aes_128_cbc
:
594 *cipher
= &cryptodev_aes_cbc
;
600 return (*cipher
!= NULL
);
604 cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
605 const int **nids
, int nid
)
608 return (cryptodev_usable_digests(nids
));
612 *digest
= NULL
; /* need to make a clean md5 critter */
618 return (*digest
!= NULL
);
622 * Convert a BIGNUM to the representation that /dev/crypto needs.
623 * Upon completion of use, the caller is responsible for freeing
627 bn2crparam(const BIGNUM
*a
, struct crparam
*crp
)
630 ssize_t words
, bytes
, bits
;
636 bits
= BN_num_bits(a
);
637 bytes
= (bits
+ 7) / 8;
644 crp
->crp_nbits
= bits
;
646 for (i
= 0, j
= 0; i
< a
->top
; i
++) {
647 for (k
= 0; k
< BN_BITS2
/ 8; k
++) {
648 if ((j
+ k
) >= bytes
)
650 b
[j
+ k
] = a
->d
[i
] >> (k
* 8);
657 /* Convert a /dev/crypto parameter to a BIGNUM */
659 crparam2bn(struct crparam
*crp
, BIGNUM
*a
)
664 bytes
= (crp
->crp_nbits
+ 7) / 8;
669 if ((pd
= (u_int8_t
*) malloc(bytes
)) == NULL
)
672 for (i
= 0; i
< bytes
; i
++)
673 pd
[i
] = crp
->crp_p
[bytes
- i
- 1];
675 BN_bin2bn(pd
, bytes
, a
);
682 zapparams(struct crypt_kop
*kop
)
686 for (i
= 0; i
<= kop
->crk_iparams
+ kop
->crk_oparams
; i
++) {
687 if (kop
->crk_param
[i
].crp_p
)
688 free(kop
->crk_param
[i
].crp_p
);
689 kop
->crk_param
[i
].crp_p
= NULL
;
690 kop
->crk_param
[i
].crp_nbits
= 0;
695 cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
, int slen
, BIGNUM
*s
)
699 if ((fd
= get_asym_dev_crypto()) < 0)
703 kop
->crk_param
[kop
->crk_iparams
].crp_p
= calloc(rlen
, sizeof(char));
704 kop
->crk_param
[kop
->crk_iparams
].crp_nbits
= rlen
* 8;
708 kop
->crk_param
[kop
->crk_iparams
+1].crp_p
= calloc(slen
, sizeof(char));
709 kop
->crk_param
[kop
->crk_iparams
+1].crp_nbits
= slen
* 8;
713 if (ioctl(fd
, CIOCKEY
, kop
) == 0) {
715 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
], r
);
717 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
+1], s
);
725 cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
726 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
728 struct crypt_kop kop
;
731 /* Currently, we know we can do mod exp iff we can do any
732 * asymmetric operations at all.
734 if (cryptodev_asymfeat
== 0) {
735 ret
= BN_mod_exp(r
, a
, p
, m
, ctx
);
739 memset(&kop
, 0, sizeof kop
);
740 kop
.crk_op
= CRK_MOD_EXP
;
742 /* inputs: a^p % m */
743 if (bn2crparam(a
, &kop
.crk_param
[0]))
745 if (bn2crparam(p
, &kop
.crk_param
[1]))
747 if (bn2crparam(m
, &kop
.crk_param
[2]))
751 if (cryptodev_asym(&kop
, BN_num_bytes(m
), r
, 0, NULL
) == -1) {
752 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
753 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
761 cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
)
767 r
= cryptodev_bn_mod_exp(r0
, I
, rsa
->d
, rsa
->n
, ctx
, NULL
);
773 cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
)
775 struct crypt_kop kop
;
778 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
779 /* XXX 0 means failure?? */
783 memset(&kop
, 0, sizeof kop
);
784 kop
.crk_op
= CRK_MOD_EXP_CRT
;
785 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
786 if (bn2crparam(rsa
->p
, &kop
.crk_param
[0]))
788 if (bn2crparam(rsa
->q
, &kop
.crk_param
[1]))
790 if (bn2crparam(I
, &kop
.crk_param
[2]))
792 if (bn2crparam(rsa
->dmp1
, &kop
.crk_param
[3]))
794 if (bn2crparam(rsa
->dmq1
, &kop
.crk_param
[4]))
796 if (bn2crparam(rsa
->iqmp
, &kop
.crk_param
[5]))
800 if (cryptodev_asym(&kop
, BN_num_bytes(rsa
->n
), r0
, 0, NULL
) == -1) {
801 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
802 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
);
809 static RSA_METHOD cryptodev_rsa
= {
810 "cryptodev RSA method",
811 NULL
, /* rsa_pub_enc */
812 NULL
, /* rsa_pub_dec */
813 NULL
, /* rsa_priv_enc */
814 NULL
, /* rsa_priv_dec */
822 NULL
/* rsa_verify */
826 cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
,
827 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
829 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
833 cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
834 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
835 BN_CTX
*ctx
, BN_MONT_CTX
*mont
)
842 /* v = ( g^u1 * y^u2 mod p ) mod q */
843 /* let t1 = g ^ u1 mod p */
846 if (!dsa
->meth
->bn_mod_exp(dsa
,t1
,dsa
->g
,u1
,dsa
->p
,ctx
,mont
))
849 /* let t2 = y ^ u2 mod p */
850 if (!dsa
->meth
->bn_mod_exp(dsa
,&t2
,dsa
->pub_key
,u2
,dsa
->p
,ctx
,mont
))
852 /* let u1 = t1 * t2 mod p */
853 if (!BN_mod_mul(u1
,t1
,&t2
,dsa
->p
,ctx
))
865 cryptodev_dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
867 struct crypt_kop kop
;
868 BIGNUM
*r
= NULL
, *s
= NULL
;
869 DSA_SIG
*dsaret
= NULL
;
871 if ((r
= BN_new()) == NULL
)
873 if ((s
= BN_new()) == NULL
) {
879 memset(&kop
, 0, sizeof kop
);
880 kop
.crk_op
= CRK_DSA_SIGN
;
882 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
883 kop
.crk_param
[0].crp_p
= (caddr_t
)dgst
;
884 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
885 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
887 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
889 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
891 if (bn2crparam(dsa
->priv_key
, &kop
.crk_param
[4]))
895 if (cryptodev_asym(&kop
, BN_num_bytes(dsa
->q
), r
,
896 BN_num_bytes(dsa
->q
), s
) == 0) {
897 dsaret
= DSA_SIG_new();
901 const DSA_METHOD
*meth
= DSA_OpenSSL();
904 dsaret
= (meth
->dsa_do_sign
)(dgst
, dlen
, dsa
);
907 kop
.crk_param
[0].crp_p
= NULL
;
913 cryptodev_dsa_verify(const unsigned char *dgst
, int dlen
,
914 DSA_SIG
*sig
, DSA
*dsa
)
916 struct crypt_kop kop
;
919 memset(&kop
, 0, sizeof kop
);
920 kop
.crk_op
= CRK_DSA_VERIFY
;
922 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
923 kop
.crk_param
[0].crp_p
= (caddr_t
)dgst
;
924 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
925 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
927 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
929 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
931 if (bn2crparam(dsa
->pub_key
, &kop
.crk_param
[4]))
933 if (bn2crparam(sig
->r
, &kop
.crk_param
[5]))
935 if (bn2crparam(sig
->s
, &kop
.crk_param
[6]))
939 if (cryptodev_asym(&kop
, 0, NULL
, 0, NULL
) == 0) {
940 dsaret
= kop
.crk_status
;
942 const DSA_METHOD
*meth
= DSA_OpenSSL();
944 dsaret
= (meth
->dsa_do_verify
)(dgst
, dlen
, sig
, dsa
);
947 kop
.crk_param
[0].crp_p
= NULL
;
952 static DSA_METHOD cryptodev_dsa
= {
953 "cryptodev DSA method",
955 NULL
, /* dsa_sign_setup */
957 NULL
, /* dsa_mod_exp */
966 cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
967 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
970 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
974 cryptodev_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
976 struct crypt_kop kop
;
980 if ((fd
= get_asym_dev_crypto()) < 0) {
981 const DH_METHOD
*meth
= DH_OpenSSL();
983 return ((meth
->compute_key
)(key
, pub_key
, dh
));
986 keylen
= BN_num_bits(dh
->p
);
988 memset(&kop
, 0, sizeof kop
);
989 kop
.crk_op
= CRK_DH_COMPUTE_KEY
;
991 /* inputs: dh->priv_key pub_key dh->p key */
992 if (bn2crparam(dh
->priv_key
, &kop
.crk_param
[0]))
994 if (bn2crparam(pub_key
, &kop
.crk_param
[1]))
996 if (bn2crparam(dh
->p
, &kop
.crk_param
[2]))
1000 kop
.crk_param
[3].crp_p
= key
;
1001 kop
.crk_param
[3].crp_nbits
= keylen
* 8;
1002 kop
.crk_oparams
= 1;
1004 if (ioctl(fd
, CIOCKEY
, &kop
) == -1) {
1005 const DH_METHOD
*meth
= DH_OpenSSL();
1007 dhret
= (meth
->compute_key
)(key
, pub_key
, dh
);
1010 kop
.crk_param
[3].crp_p
= NULL
;
1015 static DH_METHOD cryptodev_dh
= {
1016 "cryptodev DH method",
1017 NULL
, /* cryptodev_dh_generate_key */
1027 * ctrl right now is just a wrapper that doesn't do much
1028 * but I expect we'll want some options soon.
1031 cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)())
1033 #ifdef HAVE_SYSLOG_R
1034 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1039 #ifdef HAVE_SYSLOG_R
1040 syslog_r(LOG_ERR
, &sd
,
1041 "cryptodev_ctrl: unknown command %d", cmd
);
1043 syslog(LOG_ERR
, "cryptodev_ctrl: unknown command %d", cmd
);
1051 ENGINE_load_cryptodev(void)
1053 ENGINE
*engine
= ENGINE_new();
1058 if ((fd
= get_dev_crypto()) < 0)
1062 * find out what asymmetric crypto algorithms we support
1064 if (ioctl(fd
, CIOCASYMFEAT
, &cryptodev_asymfeat
) == -1) {
1070 if (!ENGINE_set_id(engine
, "cryptodev") ||
1071 !ENGINE_set_name(engine
, "BSD cryptodev engine") ||
1072 !ENGINE_set_ciphers(engine
, cryptodev_engine_ciphers
) ||
1073 !ENGINE_set_digests(engine
, cryptodev_engine_digests
) ||
1074 !ENGINE_set_ctrl_function(engine
, cryptodev_ctrl
) ||
1075 !ENGINE_set_cmd_defns(engine
, cryptodev_defns
)) {
1076 ENGINE_free(engine
);
1080 if (ENGINE_set_RSA(engine
, &cryptodev_rsa
)) {
1081 const RSA_METHOD
*rsa_meth
= RSA_PKCS1_SSLeay();
1083 cryptodev_rsa
.bn_mod_exp
= rsa_meth
->bn_mod_exp
;
1084 cryptodev_rsa
.rsa_mod_exp
= rsa_meth
->rsa_mod_exp
;
1085 cryptodev_rsa
.rsa_pub_enc
= rsa_meth
->rsa_pub_enc
;
1086 cryptodev_rsa
.rsa_pub_dec
= rsa_meth
->rsa_pub_dec
;
1087 cryptodev_rsa
.rsa_priv_enc
= rsa_meth
->rsa_priv_enc
;
1088 cryptodev_rsa
.rsa_priv_dec
= rsa_meth
->rsa_priv_dec
;
1089 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1090 cryptodev_rsa
.bn_mod_exp
= cryptodev_bn_mod_exp
;
1091 if (cryptodev_asymfeat
& CRF_MOD_EXP_CRT
)
1092 cryptodev_rsa
.rsa_mod_exp
=
1093 cryptodev_rsa_mod_exp
;
1095 cryptodev_rsa
.rsa_mod_exp
=
1096 cryptodev_rsa_nocrt_mod_exp
;
1100 if (ENGINE_set_DSA(engine
, &cryptodev_dsa
)) {
1101 const DSA_METHOD
*meth
= DSA_OpenSSL();
1103 memcpy(&cryptodev_dsa
, meth
, sizeof(DSA_METHOD
));
1104 if (cryptodev_asymfeat
& CRF_DSA_SIGN
)
1105 cryptodev_dsa
.dsa_do_sign
= cryptodev_dsa_do_sign
;
1106 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1107 cryptodev_dsa
.bn_mod_exp
= cryptodev_dsa_bn_mod_exp
;
1108 cryptodev_dsa
.dsa_mod_exp
= cryptodev_dsa_dsa_mod_exp
;
1110 if (cryptodev_asymfeat
& CRF_DSA_VERIFY
)
1111 cryptodev_dsa
.dsa_do_verify
= cryptodev_dsa_verify
;
1114 if (ENGINE_set_DH(engine
, &cryptodev_dh
)){
1115 const DH_METHOD
*dh_meth
= DH_OpenSSL();
1117 cryptodev_dh
.generate_key
= dh_meth
->generate_key
;
1118 cryptodev_dh
.compute_key
= dh_meth
->compute_key
;
1119 cryptodev_dh
.bn_mod_exp
= dh_meth
->bn_mod_exp
;
1120 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1121 cryptodev_dh
.bn_mod_exp
= cryptodev_mod_exp_dh
;
1122 if (cryptodev_asymfeat
& CRF_DH_COMPUTE_KEY
)
1123 cryptodev_dh
.compute_key
=
1124 cryptodev_dh_compute_key
;
1129 ENGINE_free(engine
);
1133 #endif /* HAVE_CRYPTODEV */