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>
34 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
35 (defined(OpenBSD) || defined(__FreeBSD__))
36 #include <sys/param.h>
37 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
38 # define HAVE_CRYPTODEV
40 # if (OpenBSD >= 200110)
41 # define HAVE_SYSLOG_R
45 #ifndef HAVE_CRYPTODEV
48 ENGINE_load_cryptodev(void)
50 /* This is a NOP on platforms without /dev/crypto */
56 #include <sys/types.h>
57 #include <crypto/cryptodev.h>
58 #include <sys/ioctl.h>
68 struct dev_crypto_state
{
69 struct session_op d_sess
;
72 #ifdef USE_CRYPTODEV_DIGESTS
73 char dummy_mac_key
[HASH_MAX_LEN
];
75 unsigned char digest_res
[HASH_MAX_LEN
];
83 static u_int32_t cryptodev_asymfeat
= 0;
85 static int get_asym_dev_crypto(void);
86 static int open_dev_crypto(void);
87 static int get_dev_crypto(void);
88 static int get_cryptodev_ciphers(const int **cnids
);
89 /*static int get_cryptodev_digests(const int **cnids);*/
90 static int cryptodev_usable_ciphers(const int **nids
);
91 static int cryptodev_usable_digests(const int **nids
);
92 static int cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
93 const unsigned char *in
, size_t inl
);
94 static int cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
95 const unsigned char *iv
, int enc
);
96 static int cryptodev_cleanup(EVP_CIPHER_CTX
*ctx
);
97 static int cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
98 const int **nids
, int nid
);
99 static int cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
100 const int **nids
, int nid
);
101 static int bn2crparam(const BIGNUM
*a
, struct crparam
*crp
);
102 static int crparam2bn(struct crparam
*crp
, BIGNUM
*a
);
103 static void zapparams(struct crypt_kop
*kop
);
104 static int cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
,
105 int slen
, BIGNUM
*s
);
107 static int cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
,
108 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
109 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
,
110 RSA
*rsa
, BN_CTX
*ctx
);
111 static int cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
);
112 static int cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
113 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
114 static int cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
115 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
116 BN_CTX
*ctx
, BN_MONT_CTX
*mont
);
117 static DSA_SIG
*cryptodev_dsa_do_sign(const unsigned char *dgst
,
119 static int cryptodev_dsa_verify(const unsigned char *dgst
, int dgst_len
,
120 DSA_SIG
*sig
, DSA
*dsa
);
121 static int cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
122 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
124 static int cryptodev_dh_compute_key(unsigned char *key
,
125 const BIGNUM
*pub_key
, DH
*dh
);
126 static int cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
,
128 void ENGINE_load_cryptodev(void);
130 static const ENGINE_CMD_DEFN cryptodev_defns
[] = {
140 { CRYPTO_ARC4
, NID_rc4
, 0, 16, },
141 { CRYPTO_DES_CBC
, NID_des_cbc
, 8, 8, },
142 { CRYPTO_3DES_CBC
, NID_des_ede3_cbc
, 8, 24, },
143 { CRYPTO_AES_CBC
, NID_aes_128_cbc
, 16, 16, },
144 { CRYPTO_AES_CBC
, NID_aes_192_cbc
, 16, 24, },
145 { CRYPTO_AES_CBC
, NID_aes_256_cbc
, 16, 32, },
146 { CRYPTO_BLF_CBC
, NID_bf_cbc
, 8, 16, },
147 { CRYPTO_CAST_CBC
, NID_cast5_cbc
, 8, 16, },
148 { CRYPTO_SKIPJACK_CBC
, NID_undef
, 0, 0, },
149 { 0, NID_undef
, 0, 0, },
152 #if 0 /* not (yet?) used */
158 { CRYPTO_MD5_HMAC
, NID_hmacWithMD5
, 16},
159 { CRYPTO_SHA1_HMAC
, NID_hmacWithSHA1
, 20},
160 { CRYPTO_RIPEMD160_HMAC
, NID_ripemd160
, 16/*?*/},
161 { CRYPTO_MD5_KPDK
, NID_undef
, 0},
162 { CRYPTO_SHA1_KPDK
, NID_undef
, 0},
163 { CRYPTO_MD5
, NID_md5
, 16},
164 { CRYPTO_SHA1
, NID_sha1
, 20},
170 * Return a fd if /dev/crypto seems usable, 0 otherwise.
173 open_dev_crypto(void)
178 if ((fd
= open("/dev/crypto", O_RDWR
, 0)) == -1)
181 if (fcntl(fd
, F_SETFD
, 1) == -1) {
195 if ((fd
= open_dev_crypto()) == -1)
197 if (ioctl(fd
, CRIOGET
, &retfd
) == -1)
201 if (fcntl(retfd
, F_SETFD
, 1) == -1) {
208 /* Caching version for asym operations */
210 get_asym_dev_crypto(void)
215 fd
= get_dev_crypto();
220 * Find out what ciphers /dev/crypto will let us have a session for.
221 * XXX note, that some of these openssl doesn't deal with yet!
222 * returning them here is harmless, as long as we return NULL
223 * when asked for a handler in the cryptodev_engine_ciphers routine
226 get_cryptodev_ciphers(const int **cnids
)
228 static int nids
[CRYPTO_ALGORITHM_MAX
];
229 struct session_op sess
;
230 int fd
, i
, count
= 0;
232 if ((fd
= get_dev_crypto()) < 0) {
236 memset(&sess
, 0, sizeof(sess
));
237 sess
.key
= (caddr_t
)"123456789abcdefghijklmno";
239 for (i
= 0; ciphers
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
240 if (ciphers
[i
].nid
== NID_undef
)
242 sess
.cipher
= ciphers
[i
].id
;
243 sess
.keylen
= ciphers
[i
].keylen
;
245 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
246 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
247 nids
[count
++] = ciphers
[i
].nid
;
259 * Find out what digests /dev/crypto will let us have a session for.
260 * XXX note, that some of these openssl doesn't deal with yet!
261 * returning them here is harmless, as long as we return NULL
262 * when asked for a handler in the cryptodev_engine_digests routine
264 #if 0 /* not (yet?) used */
266 get_cryptodev_digests(const int **cnids
)
268 static int nids
[CRYPTO_ALGORITHM_MAX
];
269 struct session_op sess
;
270 int fd
, i
, count
= 0;
272 if ((fd
= get_dev_crypto()) < 0) {
276 memset(&sess
, 0, sizeof(sess
));
277 sess
.mackey
= (caddr_t
)"123456789abcdefghijklmno";
278 for (i
= 0; digests
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
279 if (digests
[i
].nid
== NID_undef
)
281 sess
.mac
= digests
[i
].id
;
282 sess
.mackeylen
= digests
[i
].keylen
;
284 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
285 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
286 nids
[count
++] = digests
[i
].nid
;
299 * Find the useable ciphers|digests from dev/crypto - this is the first
300 * thing called by the engine init crud which determines what it
301 * can use for ciphers from this engine. We want to return
302 * only what we can do, anythine else is handled by software.
304 * If we can't initialize the device to do anything useful for
305 * any reason, we want to return a NULL array, and 0 length,
306 * which forces everything to be done is software. By putting
307 * the initalization of the device in here, we ensure we can
308 * use this engine as the default, and if for whatever reason
309 * /dev/crypto won't do what we want it will just be done in
312 * This can (should) be greatly expanded to perhaps take into
313 * account speed of the device, and what we want to do.
314 * (although the disabling of particular alg's could be controlled
315 * by the device driver with sysctl's.) - this is where we
316 * want most of the decisions made about what we actually want
317 * to use from /dev/crypto.
320 cryptodev_usable_ciphers(const int **nids
)
322 return (get_cryptodev_ciphers(nids
));
326 cryptodev_usable_digests(const int **nids
)
328 #ifdef USE_CRYPTODEV_DIGESTS
329 return (get_cryptodev_digests(nids
));
332 * XXXX just disable all digests for now, because it sucks.
333 * we need a better way to decide this - i.e. I may not
334 * want digests on slow cards like hifn on fast machines,
335 * but might want them on slow or loaded machines, etc.
336 * will also want them when using crypto cards that don't
337 * suck moose gonads - would be nice to be able to decide something
338 * as reasonable default without having hackery that's card dependent.
339 * of course, the default should probably be just do everything,
340 * with perhaps a sysctl to turn algoritms off (or have them off
341 * by default) on cards that generally suck like the hifn.
349 cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
350 const unsigned char *in
, size_t inl
)
352 struct crypt_op cryp
;
353 struct dev_crypto_state
*state
= ctx
->cipher_data
;
354 struct session_op
*sess
= &state
->d_sess
;
356 unsigned char save_iv
[EVP_MAX_IV_LENGTH
];
362 if ((inl
% ctx
->cipher
->block_size
) != 0)
365 memset(&cryp
, 0, sizeof(cryp
));
367 cryp
.ses
= sess
->ses
;
370 cryp
.src
= (caddr_t
) in
;
371 cryp
.dst
= (caddr_t
) out
;
374 cryp
.op
= ctx
->encrypt
? COP_ENCRYPT
: COP_DECRYPT
;
376 if (ctx
->cipher
->iv_len
) {
377 cryp
.iv
= (caddr_t
) ctx
->iv
;
379 iiv
= in
+ inl
- ctx
->cipher
->iv_len
;
380 memcpy(save_iv
, iiv
, ctx
->cipher
->iv_len
);
385 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) == -1) {
386 /* XXX need better errror handling
387 * this can fail for a number of different reasons.
392 if (ctx
->cipher
->iv_len
) {
394 iiv
= out
+ inl
- ctx
->cipher
->iv_len
;
397 memcpy(ctx
->iv
, iiv
, ctx
->cipher
->iv_len
);
403 cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
404 const unsigned char *iv
, int enc
)
406 struct dev_crypto_state
*state
= ctx
->cipher_data
;
407 struct session_op
*sess
= &state
->d_sess
;
410 for (i
= 0; ciphers
[i
].id
; i
++)
411 if (ctx
->cipher
->nid
== ciphers
[i
].nid
&&
412 ctx
->cipher
->iv_len
<= ciphers
[i
].ivmax
&&
413 ctx
->key_len
== ciphers
[i
].keylen
) {
414 cipher
= ciphers
[i
].id
;
418 if (!ciphers
[i
].id
) {
423 memset(sess
, 0, sizeof(struct session_op
));
425 if ((state
->d_fd
= get_dev_crypto()) < 0)
428 sess
->key
= (caddr_t
)key
;
429 sess
->keylen
= ctx
->key_len
;
430 sess
->cipher
= cipher
;
432 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) == -1) {
441 * free anything we allocated earlier when initting a
442 * session, and close the session.
445 cryptodev_cleanup(EVP_CIPHER_CTX
*ctx
)
448 struct dev_crypto_state
*state
= ctx
->cipher_data
;
449 struct session_op
*sess
= &state
->d_sess
;
454 /* XXX if this ioctl fails, someting's wrong. the invoker
455 * may have called us with a bogus ctx, or we could
456 * have a device that for whatever reason just doesn't
457 * want to play ball - it's not clear what's right
458 * here - should this be an error? should it just
459 * increase a counter, hmm. For right now, we return
460 * 0 - I don't believe that to be "right". we could
461 * call the gorpy openssl lib error handlers that
462 * print messages to users of the library. hmm..
465 if (ioctl(state
->d_fd
, CIOCFSESSION
, &sess
->ses
) == -1) {
477 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
478 * gets called when libcrypto requests a cipher NID.
482 const EVP_CIPHER cryptodev_rc4
= {
485 EVP_CIPH_VARIABLE_LENGTH
,
489 sizeof(struct dev_crypto_state
),
496 const EVP_CIPHER cryptodev_des_cbc
= {
503 sizeof(struct dev_crypto_state
),
504 EVP_CIPHER_set_asn1_iv
,
505 EVP_CIPHER_get_asn1_iv
,
510 const EVP_CIPHER cryptodev_3des_cbc
= {
517 sizeof(struct dev_crypto_state
),
518 EVP_CIPHER_set_asn1_iv
,
519 EVP_CIPHER_get_asn1_iv
,
523 const EVP_CIPHER cryptodev_bf_cbc
= {
530 sizeof(struct dev_crypto_state
),
531 EVP_CIPHER_set_asn1_iv
,
532 EVP_CIPHER_get_asn1_iv
,
536 const EVP_CIPHER cryptodev_cast_cbc
= {
543 sizeof(struct dev_crypto_state
),
544 EVP_CIPHER_set_asn1_iv
,
545 EVP_CIPHER_get_asn1_iv
,
549 const EVP_CIPHER cryptodev_aes_cbc
= {
556 sizeof(struct dev_crypto_state
),
557 EVP_CIPHER_set_asn1_iv
,
558 EVP_CIPHER_get_asn1_iv
,
562 const EVP_CIPHER cryptodev_aes_192_cbc
= {
569 sizeof(struct dev_crypto_state
),
570 EVP_CIPHER_set_asn1_iv
,
571 EVP_CIPHER_get_asn1_iv
,
575 const EVP_CIPHER cryptodev_aes_256_cbc
= {
582 sizeof(struct dev_crypto_state
),
583 EVP_CIPHER_set_asn1_iv
,
584 EVP_CIPHER_get_asn1_iv
,
589 * Registered by the ENGINE when used to find out how to deal with
590 * a particular NID in the ENGINE. this says what we'll do at the
591 * top level - note, that list is restricted by what we answer with
594 cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
595 const int **nids
, int nid
)
598 return (cryptodev_usable_ciphers(nids
));
602 *cipher
= &cryptodev_rc4
;
604 case NID_des_ede3_cbc
:
605 *cipher
= &cryptodev_3des_cbc
;
608 *cipher
= &cryptodev_des_cbc
;
611 *cipher
= &cryptodev_bf_cbc
;
614 *cipher
= &cryptodev_cast_cbc
;
616 case NID_aes_128_cbc
:
617 *cipher
= &cryptodev_aes_cbc
;
619 case NID_aes_192_cbc
:
620 *cipher
= &cryptodev_aes_192_cbc
;
622 case NID_aes_256_cbc
:
623 *cipher
= &cryptodev_aes_256_cbc
;
629 return (*cipher
!= NULL
);
633 #ifdef USE_CRYPTODEV_DIGESTS
635 /* convert digest type to cryptodev */
637 digest_nid_to_cryptodev(int nid
)
641 for (i
= 0; digests
[i
].id
; i
++)
642 if (digests
[i
].nid
== nid
)
643 return (digests
[i
].id
);
649 digest_key_length(int nid
)
653 for (i
= 0; digests
[i
].id
; i
++)
654 if (digests
[i
].nid
== nid
)
655 return digests
[i
].keylen
;
660 static int cryptodev_digest_init(EVP_MD_CTX
*ctx
)
662 struct dev_crypto_state
*state
= ctx
->md_data
;
663 struct session_op
*sess
= &state
->d_sess
;
666 if ((digest
= digest_nid_to_cryptodev(ctx
->digest
->type
)) == NID_undef
){
667 printf("cryptodev_digest_init: Can't get digest \n");
671 memset(state
, 0, sizeof(struct dev_crypto_state
));
673 if ((state
->d_fd
= get_dev_crypto()) < 0) {
674 printf("cryptodev_digest_init: Can't get Dev \n");
678 sess
->mackey
= state
->dummy_mac_key
;
679 sess
->mackeylen
= digest_key_length(ctx
->digest
->type
);
682 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) < 0) {
685 printf("cryptodev_digest_init: Open session failed\n");
692 static int cryptodev_digest_update(EVP_MD_CTX
*ctx
, const void *data
,
695 struct crypt_op cryp
;
696 struct dev_crypto_state
*state
= ctx
->md_data
;
697 struct session_op
*sess
= &state
->d_sess
;
699 if (!data
|| state
->d_fd
< 0) {
700 printf("cryptodev_digest_update: illegal inputs \n");
708 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
)) {
709 /* if application doesn't support one buffer */
710 state
->mac_data
= OPENSSL_realloc(state
->mac_data
, state
->mac_len
+ count
);
712 if (!state
->mac_data
) {
713 printf("cryptodev_digest_update: realloc failed\n");
717 memcpy(state
->mac_data
+ state
->mac_len
, data
, count
);
718 state
->mac_len
+= count
;
723 memset(&cryp
, 0, sizeof(cryp
));
725 cryp
.ses
= sess
->ses
;
728 cryp
.src
= (caddr_t
) data
;
730 cryp
.mac
= (caddr_t
) state
->digest_res
;
731 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
732 printf("cryptodev_digest_update: digest failed\n");
739 static int cryptodev_digest_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
741 struct crypt_op cryp
;
742 struct dev_crypto_state
*state
= ctx
->md_data
;
743 struct session_op
*sess
= &state
->d_sess
;
747 if (!md
|| state
->d_fd
< 0) {
748 printf("cryptodev_digest_final: illegal input\n");
752 if (! (ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
) ) {
753 /* if application doesn't support one buffer */
754 memset(&cryp
, 0, sizeof(cryp
));
756 cryp
.ses
= sess
->ses
;
758 cryp
.len
= state
->mac_len
;
759 cryp
.src
= state
->mac_data
;
761 cryp
.mac
= (caddr_t
)md
;
763 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
764 printf("cryptodev_digest_final: digest failed\n");
771 memcpy(md
, state
->digest_res
, ctx
->digest
->md_size
);
777 static int cryptodev_digest_cleanup(EVP_MD_CTX
*ctx
)
780 struct dev_crypto_state
*state
= ctx
->md_data
;
781 struct session_op
*sess
= &state
->d_sess
;
783 if (state
->d_fd
< 0) {
784 printf("cryptodev_digest_cleanup: illegal input\n");
788 if (state
->mac_data
) {
789 OPENSSL_free(state
->mac_data
);
790 state
->mac_data
= NULL
;
797 if (ioctl(state
->d_fd
, CIOCFSESSION
, &sess
->ses
) < 0) {
798 printf("cryptodev_digest_cleanup: failed to close session\n");
809 static int cryptodev_digest_copy(EVP_MD_CTX
*to
,const EVP_MD_CTX
*from
)
811 struct dev_crypto_state
*fstate
= from
->md_data
;
812 struct dev_crypto_state
*dstate
= to
->md_data
;
814 memcpy(dstate
, fstate
, sizeof(struct dev_crypto_state
));
816 if (fstate
->mac_len
!= 0) {
817 dstate
->mac_data
= OPENSSL_malloc(fstate
->mac_len
);
818 memcpy(dstate
->mac_data
, fstate
->mac_data
, fstate
->mac_len
);
827 const EVP_MD cryptodev_sha1
= {
832 cryptodev_digest_init
,
833 cryptodev_digest_update
,
834 cryptodev_digest_final
,
835 cryptodev_digest_copy
,
836 cryptodev_digest_cleanup
,
837 EVP_PKEY_NULL_method
,
839 sizeof(struct dev_crypto_state
),
842 const EVP_MD cryptodev_md5
= {
845 16 /* MD5_DIGEST_LENGTH */,
847 cryptodev_digest_init
,
848 cryptodev_digest_update
,
849 cryptodev_digest_final
,
850 cryptodev_digest_copy
,
851 cryptodev_digest_cleanup
,
852 EVP_PKEY_NULL_method
,
854 sizeof(struct dev_crypto_state
),
857 #endif /* USE_CRYPTODEV_DIGESTS */
861 cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
862 const int **nids
, int nid
)
865 return (cryptodev_usable_digests(nids
));
868 #ifdef USE_CRYPTODEV_DIGESTS
870 *digest
= &cryptodev_md5
;
873 *digest
= &cryptodev_sha1
;
876 #endif /* USE_CRYPTODEV_DIGESTS */
880 return (*digest
!= NULL
);
884 * Convert a BIGNUM to the representation that /dev/crypto needs.
885 * Upon completion of use, the caller is responsible for freeing
889 bn2crparam(const BIGNUM
*a
, struct crparam
*crp
)
898 bits
= BN_num_bits(a
);
899 bytes
= (bits
+ 7) / 8;
906 crp
->crp_p
= (caddr_t
) b
;
907 crp
->crp_nbits
= bits
;
909 for (i
= 0, j
= 0; i
< a
->top
; i
++) {
910 for (k
= 0; k
< BN_BITS2
/ 8; k
++) {
911 if ((j
+ k
) >= bytes
)
913 b
[j
+ k
] = a
->d
[i
] >> (k
* 8);
920 /* Convert a /dev/crypto parameter to a BIGNUM */
922 crparam2bn(struct crparam
*crp
, BIGNUM
*a
)
927 bytes
= (crp
->crp_nbits
+ 7) / 8;
932 if ((pd
= (u_int8_t
*) malloc(bytes
)) == NULL
)
935 for (i
= 0; i
< bytes
; i
++)
936 pd
[i
] = crp
->crp_p
[bytes
- i
- 1];
938 BN_bin2bn(pd
, bytes
, a
);
945 zapparams(struct crypt_kop
*kop
)
949 for (i
= 0; i
< kop
->crk_iparams
+ kop
->crk_oparams
; i
++) {
950 if (kop
->crk_param
[i
].crp_p
)
951 free(kop
->crk_param
[i
].crp_p
);
952 kop
->crk_param
[i
].crp_p
= NULL
;
953 kop
->crk_param
[i
].crp_nbits
= 0;
958 cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
, int slen
, BIGNUM
*s
)
962 if ((fd
= get_asym_dev_crypto()) < 0)
966 kop
->crk_param
[kop
->crk_iparams
].crp_p
= calloc(rlen
, sizeof(char));
967 kop
->crk_param
[kop
->crk_iparams
].crp_nbits
= rlen
* 8;
971 kop
->crk_param
[kop
->crk_iparams
+1].crp_p
= calloc(slen
, sizeof(char));
972 kop
->crk_param
[kop
->crk_iparams
+1].crp_nbits
= slen
* 8;
976 if (ioctl(fd
, CIOCKEY
, kop
) == 0) {
978 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
], r
);
980 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
+1], s
);
988 cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
989 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
991 struct crypt_kop kop
;
994 /* Currently, we know we can do mod exp iff we can do any
995 * asymmetric operations at all.
997 if (cryptodev_asymfeat
== 0) {
998 ret
= BN_mod_exp(r
, a
, p
, m
, ctx
);
1002 memset(&kop
, 0, sizeof kop
);
1003 kop
.crk_op
= CRK_MOD_EXP
;
1005 /* inputs: a^p % m */
1006 if (bn2crparam(a
, &kop
.crk_param
[0]))
1008 if (bn2crparam(p
, &kop
.crk_param
[1]))
1010 if (bn2crparam(m
, &kop
.crk_param
[2]))
1012 kop
.crk_iparams
= 3;
1014 if (cryptodev_asym(&kop
, BN_num_bytes(m
), r
, 0, NULL
) == -1) {
1015 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1016 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1024 cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1028 r
= cryptodev_bn_mod_exp(r0
, I
, rsa
->d
, rsa
->n
, ctx
, NULL
);
1034 cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1036 struct crypt_kop kop
;
1039 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
1040 /* XXX 0 means failure?? */
1044 memset(&kop
, 0, sizeof kop
);
1045 kop
.crk_op
= CRK_MOD_EXP_CRT
;
1046 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1047 if (bn2crparam(rsa
->p
, &kop
.crk_param
[0]))
1049 if (bn2crparam(rsa
->q
, &kop
.crk_param
[1]))
1051 if (bn2crparam(I
, &kop
.crk_param
[2]))
1053 if (bn2crparam(rsa
->dmp1
, &kop
.crk_param
[3]))
1055 if (bn2crparam(rsa
->dmq1
, &kop
.crk_param
[4]))
1057 if (bn2crparam(rsa
->iqmp
, &kop
.crk_param
[5]))
1059 kop
.crk_iparams
= 6;
1061 if (cryptodev_asym(&kop
, BN_num_bytes(rsa
->n
), r0
, 0, NULL
) == -1) {
1062 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1063 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1070 static RSA_METHOD cryptodev_rsa
= {
1071 "cryptodev RSA method",
1072 NULL
, /* rsa_pub_enc */
1073 NULL
, /* rsa_pub_dec */
1074 NULL
, /* rsa_priv_enc */
1075 NULL
, /* rsa_priv_dec */
1081 NULL
, /* app_data */
1082 NULL
, /* rsa_sign */
1083 NULL
/* rsa_verify */
1087 cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
,
1088 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
1090 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1094 cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
1095 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
1096 BN_CTX
*ctx
, BN_MONT_CTX
*mont
)
1103 /* v = ( g^u1 * y^u2 mod p ) mod q */
1104 /* let t1 = g ^ u1 mod p */
1107 if (!dsa
->meth
->bn_mod_exp(dsa
,t1
,dsa
->g
,u1
,dsa
->p
,ctx
,mont
))
1110 /* let t2 = y ^ u2 mod p */
1111 if (!dsa
->meth
->bn_mod_exp(dsa
,&t2
,dsa
->pub_key
,u2
,dsa
->p
,ctx
,mont
))
1113 /* let u1 = t1 * t2 mod p */
1114 if (!BN_mod_mul(u1
,t1
,&t2
,dsa
->p
,ctx
))
1126 cryptodev_dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
1128 struct crypt_kop kop
;
1129 BIGNUM
*r
= NULL
, *s
= NULL
;
1130 DSA_SIG
*dsaret
= NULL
;
1132 if ((r
= BN_new()) == NULL
)
1134 if ((s
= BN_new()) == NULL
) {
1139 memset(&kop
, 0, sizeof kop
);
1140 kop
.crk_op
= CRK_DSA_SIGN
;
1142 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1143 kop
.crk_param
[0].crp_p
= (caddr_t
)dgst
;
1144 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1145 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1147 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1149 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1151 if (bn2crparam(dsa
->priv_key
, &kop
.crk_param
[4]))
1153 kop
.crk_iparams
= 5;
1155 if (cryptodev_asym(&kop
, BN_num_bytes(dsa
->q
), r
,
1156 BN_num_bytes(dsa
->q
), s
) == 0) {
1157 dsaret
= DSA_SIG_new();
1161 const DSA_METHOD
*meth
= DSA_OpenSSL();
1164 dsaret
= (meth
->dsa_do_sign
)(dgst
, dlen
, dsa
);
1167 kop
.crk_param
[0].crp_p
= NULL
;
1173 cryptodev_dsa_verify(const unsigned char *dgst
, int dlen
,
1174 DSA_SIG
*sig
, DSA
*dsa
)
1176 struct crypt_kop kop
;
1179 memset(&kop
, 0, sizeof kop
);
1180 kop
.crk_op
= CRK_DSA_VERIFY
;
1182 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1183 kop
.crk_param
[0].crp_p
= (caddr_t
)dgst
;
1184 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1185 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1187 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1189 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1191 if (bn2crparam(dsa
->pub_key
, &kop
.crk_param
[4]))
1193 if (bn2crparam(sig
->r
, &kop
.crk_param
[5]))
1195 if (bn2crparam(sig
->s
, &kop
.crk_param
[6]))
1197 kop
.crk_iparams
= 7;
1199 if (cryptodev_asym(&kop
, 0, NULL
, 0, NULL
) == 0) {
1200 dsaret
= kop
.crk_status
;
1202 const DSA_METHOD
*meth
= DSA_OpenSSL();
1204 dsaret
= (meth
->dsa_do_verify
)(dgst
, dlen
, sig
, dsa
);
1207 kop
.crk_param
[0].crp_p
= NULL
;
1212 static DSA_METHOD cryptodev_dsa
= {
1213 "cryptodev DSA method",
1215 NULL
, /* dsa_sign_setup */
1217 NULL
, /* dsa_mod_exp */
1226 cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
1227 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
1230 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1234 cryptodev_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
1236 struct crypt_kop kop
;
1240 if ((fd
= get_asym_dev_crypto()) < 0) {
1241 const DH_METHOD
*meth
= DH_OpenSSL();
1243 return ((meth
->compute_key
)(key
, pub_key
, dh
));
1246 keylen
= BN_num_bits(dh
->p
);
1248 memset(&kop
, 0, sizeof kop
);
1249 kop
.crk_op
= CRK_DH_COMPUTE_KEY
;
1251 /* inputs: dh->priv_key pub_key dh->p key */
1252 if (bn2crparam(dh
->priv_key
, &kop
.crk_param
[0]))
1254 if (bn2crparam(pub_key
, &kop
.crk_param
[1]))
1256 if (bn2crparam(dh
->p
, &kop
.crk_param
[2]))
1258 kop
.crk_iparams
= 3;
1260 kop
.crk_param
[3].crp_p
= (caddr_t
) key
;
1261 kop
.crk_param
[3].crp_nbits
= keylen
* 8;
1262 kop
.crk_oparams
= 1;
1264 if (ioctl(fd
, CIOCKEY
, &kop
) == -1) {
1265 const DH_METHOD
*meth
= DH_OpenSSL();
1267 dhret
= (meth
->compute_key
)(key
, pub_key
, dh
);
1270 kop
.crk_param
[3].crp_p
= NULL
;
1275 static DH_METHOD cryptodev_dh
= {
1276 "cryptodev DH method",
1277 NULL
, /* cryptodev_dh_generate_key */
1287 * ctrl right now is just a wrapper that doesn't do much
1288 * but I expect we'll want some options soon.
1291 cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void))
1293 #ifdef HAVE_SYSLOG_R
1294 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1299 #ifdef HAVE_SYSLOG_R
1300 syslog_r(LOG_ERR
, &sd
,
1301 "cryptodev_ctrl: unknown command %d", cmd
);
1303 syslog(LOG_ERR
, "cryptodev_ctrl: unknown command %d", cmd
);
1311 ENGINE_load_cryptodev(void)
1313 ENGINE
*engine
= ENGINE_new();
1318 if ((fd
= get_dev_crypto()) < 0) {
1319 ENGINE_free(engine
);
1324 * find out what asymmetric crypto algorithms we support
1326 if (ioctl(fd
, CIOCASYMFEAT
, &cryptodev_asymfeat
) == -1) {
1328 ENGINE_free(engine
);
1333 if (!ENGINE_set_id(engine
, "cryptodev") ||
1334 !ENGINE_set_name(engine
, "BSD cryptodev engine") ||
1335 !ENGINE_set_ciphers(engine
, cryptodev_engine_ciphers
) ||
1336 !ENGINE_set_digests(engine
, cryptodev_engine_digests
) ||
1337 !ENGINE_set_ctrl_function(engine
, cryptodev_ctrl
) ||
1338 !ENGINE_set_cmd_defns(engine
, cryptodev_defns
)) {
1339 ENGINE_free(engine
);
1343 if (ENGINE_set_RSA(engine
, &cryptodev_rsa
)) {
1344 const RSA_METHOD
*rsa_meth
= RSA_PKCS1_SSLeay();
1346 cryptodev_rsa
.bn_mod_exp
= rsa_meth
->bn_mod_exp
;
1347 cryptodev_rsa
.rsa_mod_exp
= rsa_meth
->rsa_mod_exp
;
1348 cryptodev_rsa
.rsa_pub_enc
= rsa_meth
->rsa_pub_enc
;
1349 cryptodev_rsa
.rsa_pub_dec
= rsa_meth
->rsa_pub_dec
;
1350 cryptodev_rsa
.rsa_priv_enc
= rsa_meth
->rsa_priv_enc
;
1351 cryptodev_rsa
.rsa_priv_dec
= rsa_meth
->rsa_priv_dec
;
1352 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1353 cryptodev_rsa
.bn_mod_exp
= cryptodev_bn_mod_exp
;
1354 if (cryptodev_asymfeat
& CRF_MOD_EXP_CRT
)
1355 cryptodev_rsa
.rsa_mod_exp
=
1356 cryptodev_rsa_mod_exp
;
1358 cryptodev_rsa
.rsa_mod_exp
=
1359 cryptodev_rsa_nocrt_mod_exp
;
1363 if (ENGINE_set_DSA(engine
, &cryptodev_dsa
)) {
1364 const DSA_METHOD
*meth
= DSA_OpenSSL();
1366 memcpy(&cryptodev_dsa
, meth
, sizeof(DSA_METHOD
));
1367 if (cryptodev_asymfeat
& CRF_DSA_SIGN
)
1368 cryptodev_dsa
.dsa_do_sign
= cryptodev_dsa_do_sign
;
1369 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1370 cryptodev_dsa
.bn_mod_exp
= cryptodev_dsa_bn_mod_exp
;
1371 cryptodev_dsa
.dsa_mod_exp
= cryptodev_dsa_dsa_mod_exp
;
1373 if (cryptodev_asymfeat
& CRF_DSA_VERIFY
)
1374 cryptodev_dsa
.dsa_do_verify
= cryptodev_dsa_verify
;
1377 if (ENGINE_set_DH(engine
, &cryptodev_dh
)){
1378 const DH_METHOD
*dh_meth
= DH_OpenSSL();
1380 cryptodev_dh
.generate_key
= dh_meth
->generate_key
;
1381 cryptodev_dh
.compute_key
= dh_meth
->compute_key
;
1382 cryptodev_dh
.bn_mod_exp
= dh_meth
->bn_mod_exp
;
1383 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1384 cryptodev_dh
.bn_mod_exp
= cryptodev_mod_exp_dh
;
1385 if (cryptodev_asymfeat
& CRF_DH_COMPUTE_KEY
)
1386 cryptodev_dh
.compute_key
=
1387 cryptodev_dh_compute_key
;
1392 ENGINE_free(engine
);
1396 #endif /* HAVE_CRYPTODEV */