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 <openssl/dh.h>
59 #include <openssl/dsa.h>
60 #include <openssl/err.h>
61 #include <openssl/rsa.h>
62 #include <sys/ioctl.h>
72 struct dev_crypto_state
{
73 struct session_op d_sess
;
76 #ifdef USE_CRYPTODEV_DIGESTS
77 char dummy_mac_key
[HASH_MAX_LEN
];
79 unsigned char digest_res
[HASH_MAX_LEN
];
85 static u_int32_t cryptodev_asymfeat
= 0;
87 static int get_asym_dev_crypto(void);
88 static int open_dev_crypto(void);
89 static int get_dev_crypto(void);
90 static int get_cryptodev_ciphers(const int **cnids
);
91 #ifdef USE_CRYPTODEV_DIGESTS
92 static int get_cryptodev_digests(const int **cnids
);
94 static int cryptodev_usable_ciphers(const int **nids
);
95 static int cryptodev_usable_digests(const int **nids
);
96 static int cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
97 const unsigned char *in
, size_t inl
);
98 static int cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
99 const unsigned char *iv
, int enc
);
100 static int cryptodev_cleanup(EVP_CIPHER_CTX
*ctx
);
101 static int cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
102 const int **nids
, int nid
);
103 static int cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
104 const int **nids
, int nid
);
105 static int bn2crparam(const BIGNUM
*a
, struct crparam
*crp
);
106 static int crparam2bn(struct crparam
*crp
, BIGNUM
*a
);
107 static void zapparams(struct crypt_kop
*kop
);
108 static int cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
,
109 int slen
, BIGNUM
*s
);
111 static int cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
,
112 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
113 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
,
114 RSA
*rsa
, BN_CTX
*ctx
);
115 static int cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
);
116 static int cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
117 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
118 static int cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
119 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
120 BN_CTX
*ctx
, BN_MONT_CTX
*mont
);
121 static DSA_SIG
*cryptodev_dsa_do_sign(const unsigned char *dgst
,
123 static int cryptodev_dsa_verify(const unsigned char *dgst
, int dgst_len
,
124 DSA_SIG
*sig
, DSA
*dsa
);
125 static int cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
126 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
128 static int cryptodev_dh_compute_key(unsigned char *key
,
129 const BIGNUM
*pub_key
, DH
*dh
);
130 static int cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
,
132 void ENGINE_load_cryptodev(void);
134 static const ENGINE_CMD_DEFN cryptodev_defns
[] = {
144 { CRYPTO_ARC4
, NID_rc4
, 0, 16, },
145 { CRYPTO_DES_CBC
, NID_des_cbc
, 8, 8, },
146 { CRYPTO_3DES_CBC
, NID_des_ede3_cbc
, 8, 24, },
147 { CRYPTO_AES_CBC
, NID_aes_128_cbc
, 16, 16, },
148 { CRYPTO_AES_CBC
, NID_aes_192_cbc
, 16, 24, },
149 { CRYPTO_AES_CBC
, NID_aes_256_cbc
, 16, 32, },
150 #ifdef CRYPTO_AES_CTR
151 { CRYPTO_AES_CTR
, NID_aes_128_ctr
, 14, 16, },
152 { CRYPTO_AES_CTR
, NID_aes_192_ctr
, 14, 24, },
153 { CRYPTO_AES_CTR
, NID_aes_256_ctr
, 14, 32, },
155 { CRYPTO_BLF_CBC
, NID_bf_cbc
, 8, 16, },
156 { CRYPTO_CAST_CBC
, NID_cast5_cbc
, 8, 16, },
157 { CRYPTO_SKIPJACK_CBC
, NID_undef
, 0, 0, },
158 { 0, NID_undef
, 0, 0, },
161 #ifdef USE_CRYPTODEV_DIGESTS
167 { CRYPTO_MD5_HMAC
, NID_hmacWithMD5
, 16},
168 { CRYPTO_SHA1_HMAC
, NID_hmacWithSHA1
, 20},
169 { CRYPTO_RIPEMD160_HMAC
, NID_ripemd160
, 16/*?*/},
170 { CRYPTO_MD5_KPDK
, NID_undef
, 0},
171 { CRYPTO_SHA1_KPDK
, NID_undef
, 0},
172 { CRYPTO_MD5
, NID_md5
, 16},
173 { CRYPTO_SHA1
, NID_sha1
, 20},
179 * Return a fd if /dev/crypto seems usable, 0 otherwise.
182 open_dev_crypto(void)
187 if ((fd
= open("/dev/crypto", O_RDWR
, 0)) == -1)
190 if (fcntl(fd
, F_SETFD
, 1) == -1) {
204 if ((fd
= open_dev_crypto()) == -1)
206 #ifndef CRIOGET_NOT_NEEDED
207 if (ioctl(fd
, CRIOGET
, &retfd
) == -1)
211 if (fcntl(retfd
, F_SETFD
, 1) == -1) {
221 static void put_dev_crypto(int fd
)
223 #ifndef CRIOGET_NOT_NEEDED
228 /* Caching version for asym operations */
230 get_asym_dev_crypto(void)
235 fd
= get_dev_crypto();
240 * Find out what ciphers /dev/crypto will let us have a session for.
241 * XXX note, that some of these openssl doesn't deal with yet!
242 * returning them here is harmless, as long as we return NULL
243 * when asked for a handler in the cryptodev_engine_ciphers routine
246 get_cryptodev_ciphers(const int **cnids
)
248 static int nids
[CRYPTO_ALGORITHM_MAX
];
249 struct session_op sess
;
250 int fd
, i
, count
= 0;
252 if ((fd
= get_dev_crypto()) < 0) {
256 memset(&sess
, 0, sizeof(sess
));
257 sess
.key
= (caddr_t
)"123456789abcdefghijklmno";
259 for (i
= 0; ciphers
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
260 if (ciphers
[i
].nid
== NID_undef
)
262 sess
.cipher
= ciphers
[i
].id
;
263 sess
.keylen
= ciphers
[i
].keylen
;
265 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
266 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
267 nids
[count
++] = ciphers
[i
].nid
;
278 #ifdef USE_CRYPTODEV_DIGESTS
280 * Find out what digests /dev/crypto will let us have a session for.
281 * XXX note, that some of these openssl doesn't deal with yet!
282 * returning them here is harmless, as long as we return NULL
283 * when asked for a handler in the cryptodev_engine_digests routine
286 get_cryptodev_digests(const int **cnids
)
288 static int nids
[CRYPTO_ALGORITHM_MAX
];
289 struct session_op sess
;
290 int fd
, i
, count
= 0;
292 if ((fd
= get_dev_crypto()) < 0) {
296 memset(&sess
, 0, sizeof(sess
));
297 sess
.mackey
= (caddr_t
)"123456789abcdefghijklmno";
298 for (i
= 0; digests
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
299 if (digests
[i
].nid
== NID_undef
)
301 sess
.mac
= digests
[i
].id
;
302 sess
.mackeylen
= digests
[i
].keylen
;
304 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
305 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
306 nids
[count
++] = digests
[i
].nid
;
319 * Find the useable ciphers|digests from dev/crypto - this is the first
320 * thing called by the engine init crud which determines what it
321 * can use for ciphers from this engine. We want to return
322 * only what we can do, anythine else is handled by software.
324 * If we can't initialize the device to do anything useful for
325 * any reason, we want to return a NULL array, and 0 length,
326 * which forces everything to be done is software. By putting
327 * the initalization of the device in here, we ensure we can
328 * use this engine as the default, and if for whatever reason
329 * /dev/crypto won't do what we want it will just be done in
332 * This can (should) be greatly expanded to perhaps take into
333 * account speed of the device, and what we want to do.
334 * (although the disabling of particular alg's could be controlled
335 * by the device driver with sysctl's.) - this is where we
336 * want most of the decisions made about what we actually want
337 * to use from /dev/crypto.
340 cryptodev_usable_ciphers(const int **nids
)
342 return (get_cryptodev_ciphers(nids
));
346 cryptodev_usable_digests(const int **nids
)
348 #ifdef USE_CRYPTODEV_DIGESTS
349 return (get_cryptodev_digests(nids
));
352 * XXXX just disable all digests for now, because it sucks.
353 * we need a better way to decide this - i.e. I may not
354 * want digests on slow cards like hifn on fast machines,
355 * but might want them on slow or loaded machines, etc.
356 * will also want them when using crypto cards that don't
357 * suck moose gonads - would be nice to be able to decide something
358 * as reasonable default without having hackery that's card dependent.
359 * of course, the default should probably be just do everything,
360 * with perhaps a sysctl to turn algoritms off (or have them off
361 * by default) on cards that generally suck like the hifn.
369 cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
370 const unsigned char *in
, size_t inl
)
372 struct crypt_op cryp
;
373 struct dev_crypto_state
*state
= ctx
->cipher_data
;
374 struct session_op
*sess
= &state
->d_sess
;
376 unsigned char save_iv
[EVP_MAX_IV_LENGTH
];
382 if ((inl
% ctx
->cipher
->block_size
) != 0)
385 memset(&cryp
, 0, sizeof(cryp
));
387 cryp
.ses
= sess
->ses
;
390 cryp
.src
= (caddr_t
) in
;
391 cryp
.dst
= (caddr_t
) out
;
394 cryp
.op
= ctx
->encrypt
? COP_ENCRYPT
: COP_DECRYPT
;
396 if (ctx
->cipher
->iv_len
) {
397 cryp
.iv
= (caddr_t
) ctx
->iv
;
399 iiv
= in
+ inl
- ctx
->cipher
->iv_len
;
400 memcpy(save_iv
, iiv
, ctx
->cipher
->iv_len
);
405 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) == -1) {
406 /* XXX need better errror handling
407 * this can fail for a number of different reasons.
412 if (ctx
->cipher
->iv_len
) {
414 iiv
= out
+ inl
- ctx
->cipher
->iv_len
;
417 memcpy(ctx
->iv
, iiv
, ctx
->cipher
->iv_len
);
423 cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
424 const unsigned char *iv
, int enc
)
426 struct dev_crypto_state
*state
= ctx
->cipher_data
;
427 struct session_op
*sess
= &state
->d_sess
;
430 for (i
= 0; ciphers
[i
].id
; i
++)
431 if (ctx
->cipher
->nid
== ciphers
[i
].nid
&&
432 ctx
->cipher
->iv_len
<= ciphers
[i
].ivmax
&&
433 ctx
->key_len
== ciphers
[i
].keylen
) {
434 cipher
= ciphers
[i
].id
;
438 if (!ciphers
[i
].id
) {
443 memset(sess
, 0, sizeof(struct session_op
));
445 if ((state
->d_fd
= get_dev_crypto()) < 0)
448 sess
->key
= (caddr_t
)key
;
449 sess
->keylen
= ctx
->key_len
;
450 sess
->cipher
= cipher
;
452 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) == -1) {
453 put_dev_crypto(state
->d_fd
);
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) {
490 put_dev_crypto(state
->d_fd
);
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_rc4
= {
505 EVP_CIPH_VARIABLE_LENGTH
,
509 sizeof(struct dev_crypto_state
),
516 const EVP_CIPHER cryptodev_des_cbc
= {
523 sizeof(struct dev_crypto_state
),
524 EVP_CIPHER_set_asn1_iv
,
525 EVP_CIPHER_get_asn1_iv
,
530 const EVP_CIPHER cryptodev_3des_cbc
= {
537 sizeof(struct dev_crypto_state
),
538 EVP_CIPHER_set_asn1_iv
,
539 EVP_CIPHER_get_asn1_iv
,
543 const EVP_CIPHER cryptodev_bf_cbc
= {
550 sizeof(struct dev_crypto_state
),
551 EVP_CIPHER_set_asn1_iv
,
552 EVP_CIPHER_get_asn1_iv
,
556 const EVP_CIPHER cryptodev_cast_cbc
= {
563 sizeof(struct dev_crypto_state
),
564 EVP_CIPHER_set_asn1_iv
,
565 EVP_CIPHER_get_asn1_iv
,
569 const EVP_CIPHER cryptodev_aes_cbc
= {
576 sizeof(struct dev_crypto_state
),
577 EVP_CIPHER_set_asn1_iv
,
578 EVP_CIPHER_get_asn1_iv
,
582 const EVP_CIPHER cryptodev_aes_192_cbc
= {
589 sizeof(struct dev_crypto_state
),
590 EVP_CIPHER_set_asn1_iv
,
591 EVP_CIPHER_get_asn1_iv
,
595 const EVP_CIPHER cryptodev_aes_256_cbc
= {
602 sizeof(struct dev_crypto_state
),
603 EVP_CIPHER_set_asn1_iv
,
604 EVP_CIPHER_get_asn1_iv
,
607 #ifdef CRYPTO_AES_CTR
608 const EVP_CIPHER cryptodev_aes_ctr
= {
615 sizeof(struct dev_crypto_state
),
616 EVP_CIPHER_set_asn1_iv
,
617 EVP_CIPHER_get_asn1_iv
,
621 const EVP_CIPHER cryptodev_aes_ctr_192
= {
628 sizeof(struct dev_crypto_state
),
629 EVP_CIPHER_set_asn1_iv
,
630 EVP_CIPHER_get_asn1_iv
,
634 const EVP_CIPHER cryptodev_aes_ctr_256
= {
641 sizeof(struct dev_crypto_state
),
642 EVP_CIPHER_set_asn1_iv
,
643 EVP_CIPHER_get_asn1_iv
,
648 * Registered by the ENGINE when used to find out how to deal with
649 * a particular NID in the ENGINE. this says what we'll do at the
650 * top level - note, that list is restricted by what we answer with
653 cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
654 const int **nids
, int nid
)
657 return (cryptodev_usable_ciphers(nids
));
661 *cipher
= &cryptodev_rc4
;
663 case NID_des_ede3_cbc
:
664 *cipher
= &cryptodev_3des_cbc
;
667 *cipher
= &cryptodev_des_cbc
;
670 *cipher
= &cryptodev_bf_cbc
;
673 *cipher
= &cryptodev_cast_cbc
;
675 case NID_aes_128_cbc
:
676 *cipher
= &cryptodev_aes_cbc
;
678 case NID_aes_192_cbc
:
679 *cipher
= &cryptodev_aes_192_cbc
;
681 case NID_aes_256_cbc
:
682 *cipher
= &cryptodev_aes_256_cbc
;
684 #ifdef CRYPTO_AES_CTR
685 case NID_aes_128_ctr
:
686 *cipher
= &cryptodev_aes_ctr
;
688 case NID_aes_192_ctr
:
689 *cipher
= &cryptodev_aes_ctr_192
;
691 case NID_aes_256_ctr
:
692 *cipher
= &cryptodev_aes_ctr_256
;
699 return (*cipher
!= NULL
);
703 #ifdef USE_CRYPTODEV_DIGESTS
705 /* convert digest type to cryptodev */
707 digest_nid_to_cryptodev(int nid
)
711 for (i
= 0; digests
[i
].id
; i
++)
712 if (digests
[i
].nid
== nid
)
713 return (digests
[i
].id
);
719 digest_key_length(int nid
)
723 for (i
= 0; digests
[i
].id
; i
++)
724 if (digests
[i
].nid
== nid
)
725 return digests
[i
].keylen
;
730 static int cryptodev_digest_init(EVP_MD_CTX
*ctx
)
732 struct dev_crypto_state
*state
= ctx
->md_data
;
733 struct session_op
*sess
= &state
->d_sess
;
736 if ((digest
= digest_nid_to_cryptodev(ctx
->digest
->type
)) == NID_undef
){
737 printf("cryptodev_digest_init: Can't get digest \n");
741 memset(state
, 0, sizeof(struct dev_crypto_state
));
743 if ((state
->d_fd
= get_dev_crypto()) < 0) {
744 printf("cryptodev_digest_init: Can't get Dev \n");
748 sess
->mackey
= state
->dummy_mac_key
;
749 sess
->mackeylen
= digest_key_length(ctx
->digest
->type
);
752 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) < 0) {
753 put_dev_crypto(state
->d_fd
);
755 printf("cryptodev_digest_init: Open session failed\n");
762 static int cryptodev_digest_update(EVP_MD_CTX
*ctx
, const void *data
,
765 struct crypt_op cryp
;
766 struct dev_crypto_state
*state
= ctx
->md_data
;
767 struct session_op
*sess
= &state
->d_sess
;
770 if (!data
|| state
->d_fd
< 0) {
771 printf("cryptodev_digest_update: illegal inputs \n");
779 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
)) {
780 /* if application doesn't support one buffer */
781 new_mac_data
= OPENSSL_realloc(state
->mac_data
, state
->mac_len
+ count
);
784 printf("cryptodev_digest_update: realloc failed\n");
787 state
->mac_data
= new_mac_data
;
789 memcpy(state
->mac_data
+ state
->mac_len
, data
, count
);
790 state
->mac_len
+= count
;
795 memset(&cryp
, 0, sizeof(cryp
));
797 cryp
.ses
= sess
->ses
;
800 cryp
.src
= (caddr_t
) data
;
802 cryp
.mac
= (caddr_t
) state
->digest_res
;
803 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
804 printf("cryptodev_digest_update: digest failed\n");
811 static int cryptodev_digest_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
813 struct crypt_op cryp
;
814 struct dev_crypto_state
*state
= ctx
->md_data
;
815 struct session_op
*sess
= &state
->d_sess
;
819 if (!md
|| state
->d_fd
< 0) {
820 printf("cryptodev_digest_final: illegal input\n");
824 if (! (ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
) ) {
825 /* if application doesn't support one buffer */
826 memset(&cryp
, 0, sizeof(cryp
));
827 cryp
.ses
= sess
->ses
;
829 cryp
.len
= state
->mac_len
;
830 cryp
.src
= state
->mac_data
;
832 cryp
.mac
= (caddr_t
)md
;
833 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
834 printf("cryptodev_digest_final: digest failed\n");
841 memcpy(md
, state
->digest_res
, ctx
->digest
->md_size
);
847 static int cryptodev_digest_cleanup(EVP_MD_CTX
*ctx
)
850 struct dev_crypto_state
*state
= ctx
->md_data
;
851 struct session_op
*sess
= &state
->d_sess
;
856 if (state
->d_fd
< 0) {
857 printf("cryptodev_digest_cleanup: illegal input\n");
861 if (state
->mac_data
) {
862 OPENSSL_free(state
->mac_data
);
863 state
->mac_data
= NULL
;
867 if (ioctl(state
->d_fd
, CIOCFSESSION
, &sess
->ses
) < 0) {
868 printf("cryptodev_digest_cleanup: failed to close session\n");
873 put_dev_crypto(state
->d_fd
);
879 static int cryptodev_digest_copy(EVP_MD_CTX
*to
,const EVP_MD_CTX
*from
)
881 struct dev_crypto_state
*fstate
= from
->md_data
;
882 struct dev_crypto_state
*dstate
= to
->md_data
;
883 struct session_op
*sess
;
886 if (dstate
== NULL
|| fstate
== NULL
)
889 memcpy(dstate
, fstate
, sizeof(struct dev_crypto_state
));
891 sess
= &dstate
->d_sess
;
893 digest
= digest_nid_to_cryptodev(to
->digest
->type
);
895 sess
->mackey
= dstate
->dummy_mac_key
;
896 sess
->mackeylen
= digest_key_length(to
->digest
->type
);
899 dstate
->d_fd
= get_dev_crypto();
901 if (ioctl(dstate
->d_fd
, CIOCGSESSION
, sess
) < 0) {
902 put_dev_crypto(dstate
->d_fd
);
904 printf("cryptodev_digest_init: Open session failed\n");
908 if (fstate
->mac_len
!= 0) {
909 if (fstate
->mac_data
!= NULL
)
911 dstate
->mac_data
= OPENSSL_malloc(fstate
->mac_len
);
912 memcpy(dstate
->mac_data
, fstate
->mac_data
, fstate
->mac_len
);
913 dstate
->mac_len
= fstate
->mac_len
;
921 const EVP_MD cryptodev_sha1
= {
926 cryptodev_digest_init
,
927 cryptodev_digest_update
,
928 cryptodev_digest_final
,
929 cryptodev_digest_copy
,
930 cryptodev_digest_cleanup
,
931 EVP_PKEY_NULL_method
,
933 sizeof(struct dev_crypto_state
),
936 const EVP_MD cryptodev_md5
= {
939 16 /* MD5_DIGEST_LENGTH */,
941 cryptodev_digest_init
,
942 cryptodev_digest_update
,
943 cryptodev_digest_final
,
944 cryptodev_digest_copy
,
945 cryptodev_digest_cleanup
,
946 EVP_PKEY_NULL_method
,
948 sizeof(struct dev_crypto_state
),
951 #endif /* USE_CRYPTODEV_DIGESTS */
955 cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
956 const int **nids
, int nid
)
959 return (cryptodev_usable_digests(nids
));
962 #ifdef USE_CRYPTODEV_DIGESTS
964 *digest
= &cryptodev_md5
;
967 *digest
= &cryptodev_sha1
;
970 #endif /* USE_CRYPTODEV_DIGESTS */
974 return (*digest
!= NULL
);
978 * Convert a BIGNUM to the representation that /dev/crypto needs.
979 * Upon completion of use, the caller is responsible for freeing
983 bn2crparam(const BIGNUM
*a
, struct crparam
*crp
)
992 bits
= BN_num_bits(a
);
993 bytes
= (bits
+ 7) / 8;
1000 crp
->crp_p
= (caddr_t
) b
;
1001 crp
->crp_nbits
= bits
;
1003 for (i
= 0, j
= 0; i
< a
->top
; i
++) {
1004 for (k
= 0; k
< BN_BITS2
/ 8; k
++) {
1005 if ((j
+ k
) >= bytes
)
1007 b
[j
+ k
] = a
->d
[i
] >> (k
* 8);
1014 /* Convert a /dev/crypto parameter to a BIGNUM */
1016 crparam2bn(struct crparam
*crp
, BIGNUM
*a
)
1021 bytes
= (crp
->crp_nbits
+ 7) / 8;
1026 if ((pd
= (u_int8_t
*) malloc(bytes
)) == NULL
)
1029 for (i
= 0; i
< bytes
; i
++)
1030 pd
[i
] = crp
->crp_p
[bytes
- i
- 1];
1032 BN_bin2bn(pd
, bytes
, a
);
1039 zapparams(struct crypt_kop
*kop
)
1043 for (i
= 0; i
< kop
->crk_iparams
+ kop
->crk_oparams
; i
++) {
1044 if (kop
->crk_param
[i
].crp_p
)
1045 free(kop
->crk_param
[i
].crp_p
);
1046 kop
->crk_param
[i
].crp_p
= NULL
;
1047 kop
->crk_param
[i
].crp_nbits
= 0;
1052 cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
, int slen
, BIGNUM
*s
)
1056 if ((fd
= get_asym_dev_crypto()) < 0)
1060 kop
->crk_param
[kop
->crk_iparams
].crp_p
= calloc(rlen
, sizeof(char));
1061 kop
->crk_param
[kop
->crk_iparams
].crp_nbits
= rlen
* 8;
1065 kop
->crk_param
[kop
->crk_iparams
+1].crp_p
= calloc(slen
, sizeof(char));
1066 kop
->crk_param
[kop
->crk_iparams
+1].crp_nbits
= slen
* 8;
1070 if (ioctl(fd
, CIOCKEY
, kop
) == 0) {
1072 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
], r
);
1074 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
+1], s
);
1082 cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
1083 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
1085 struct crypt_kop kop
;
1088 /* Currently, we know we can do mod exp iff we can do any
1089 * asymmetric operations at all.
1091 if (cryptodev_asymfeat
== 0) {
1092 ret
= BN_mod_exp(r
, a
, p
, m
, ctx
);
1096 memset(&kop
, 0, sizeof kop
);
1097 kop
.crk_op
= CRK_MOD_EXP
;
1099 /* inputs: a^p % m */
1100 if (bn2crparam(a
, &kop
.crk_param
[0]))
1102 if (bn2crparam(p
, &kop
.crk_param
[1]))
1104 if (bn2crparam(m
, &kop
.crk_param
[2]))
1106 kop
.crk_iparams
= 3;
1108 if (cryptodev_asym(&kop
, BN_num_bytes(m
), r
, 0, NULL
)) {
1109 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1110 printf("OCF asym process failed, Running in software\n");
1111 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1113 } else if (ECANCELED
== kop
.crk_status
) {
1114 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1115 printf("OCF hardware operation cancelled. Running in Software\n");
1116 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1118 /* else cryptodev operation worked ok ==> ret = 1*/
1126 cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1130 r
= cryptodev_bn_mod_exp(r0
, I
, rsa
->d
, rsa
->n
, ctx
, NULL
);
1136 cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1138 struct crypt_kop kop
;
1141 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
1142 /* XXX 0 means failure?? */
1146 memset(&kop
, 0, sizeof kop
);
1147 kop
.crk_op
= CRK_MOD_EXP_CRT
;
1148 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1149 if (bn2crparam(rsa
->p
, &kop
.crk_param
[0]))
1151 if (bn2crparam(rsa
->q
, &kop
.crk_param
[1]))
1153 if (bn2crparam(I
, &kop
.crk_param
[2]))
1155 if (bn2crparam(rsa
->dmp1
, &kop
.crk_param
[3]))
1157 if (bn2crparam(rsa
->dmq1
, &kop
.crk_param
[4]))
1159 if (bn2crparam(rsa
->iqmp
, &kop
.crk_param
[5]))
1161 kop
.crk_iparams
= 6;
1163 if (cryptodev_asym(&kop
, BN_num_bytes(rsa
->n
), r0
, 0, NULL
)) {
1164 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1165 printf("OCF asym process failed, running in Software\n");
1166 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1168 } else if (ECANCELED
== kop
.crk_status
) {
1169 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1170 printf("OCF hardware operation cancelled. Running in Software\n");
1171 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1173 /* else cryptodev operation worked ok ==> ret = 1*/
1180 static RSA_METHOD cryptodev_rsa
= {
1181 "cryptodev RSA method",
1182 NULL
, /* rsa_pub_enc */
1183 NULL
, /* rsa_pub_dec */
1184 NULL
, /* rsa_priv_enc */
1185 NULL
, /* rsa_priv_dec */
1191 NULL
, /* app_data */
1192 NULL
, /* rsa_sign */
1193 NULL
/* rsa_verify */
1197 cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
,
1198 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
1200 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1204 cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
1205 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
1206 BN_CTX
*ctx
, BN_MONT_CTX
*mont
)
1213 /* v = ( g^u1 * y^u2 mod p ) mod q */
1214 /* let t1 = g ^ u1 mod p */
1217 if (!dsa
->meth
->bn_mod_exp(dsa
,t1
,dsa
->g
,u1
,dsa
->p
,ctx
,mont
))
1220 /* let t2 = y ^ u2 mod p */
1221 if (!dsa
->meth
->bn_mod_exp(dsa
,&t2
,dsa
->pub_key
,u2
,dsa
->p
,ctx
,mont
))
1223 /* let u1 = t1 * t2 mod p */
1224 if (!BN_mod_mul(u1
,t1
,&t2
,dsa
->p
,ctx
))
1236 cryptodev_dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
1238 struct crypt_kop kop
;
1239 BIGNUM
*r
= NULL
, *s
= NULL
;
1240 DSA_SIG
*dsaret
= NULL
;
1242 if ((r
= BN_new()) == NULL
)
1244 if ((s
= BN_new()) == NULL
) {
1249 memset(&kop
, 0, sizeof kop
);
1250 kop
.crk_op
= CRK_DSA_SIGN
;
1252 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1253 kop
.crk_param
[0].crp_p
= (caddr_t
)dgst
;
1254 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1255 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1257 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1259 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1261 if (bn2crparam(dsa
->priv_key
, &kop
.crk_param
[4]))
1263 kop
.crk_iparams
= 5;
1265 if (cryptodev_asym(&kop
, BN_num_bytes(dsa
->q
), r
,
1266 BN_num_bytes(dsa
->q
), s
) == 0) {
1267 dsaret
= DSA_SIG_new();
1271 const DSA_METHOD
*meth
= DSA_OpenSSL();
1274 dsaret
= (meth
->dsa_do_sign
)(dgst
, dlen
, dsa
);
1277 kop
.crk_param
[0].crp_p
= NULL
;
1283 cryptodev_dsa_verify(const unsigned char *dgst
, int dlen
,
1284 DSA_SIG
*sig
, DSA
*dsa
)
1286 struct crypt_kop kop
;
1289 memset(&kop
, 0, sizeof kop
);
1290 kop
.crk_op
= CRK_DSA_VERIFY
;
1292 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1293 kop
.crk_param
[0].crp_p
= (caddr_t
)dgst
;
1294 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1295 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1297 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1299 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1301 if (bn2crparam(dsa
->pub_key
, &kop
.crk_param
[4]))
1303 if (bn2crparam(sig
->r
, &kop
.crk_param
[5]))
1305 if (bn2crparam(sig
->s
, &kop
.crk_param
[6]))
1307 kop
.crk_iparams
= 7;
1309 if (cryptodev_asym(&kop
, 0, NULL
, 0, NULL
) == 0) {
1310 /*OCF success value is 0, if not zero, change dsaret to fail*/
1311 if(0 != kop
.crk_status
) dsaret
= 0;
1313 const DSA_METHOD
*meth
= DSA_OpenSSL();
1315 dsaret
= (meth
->dsa_do_verify
)(dgst
, dlen
, sig
, dsa
);
1318 kop
.crk_param
[0].crp_p
= NULL
;
1323 static DSA_METHOD cryptodev_dsa
= {
1324 "cryptodev DSA method",
1326 NULL
, /* dsa_sign_setup */
1328 NULL
, /* dsa_mod_exp */
1337 cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
1338 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
1341 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1345 cryptodev_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
1347 struct crypt_kop kop
;
1351 if ((fd
= get_asym_dev_crypto()) < 0) {
1352 const DH_METHOD
*meth
= DH_OpenSSL();
1354 return ((meth
->compute_key
)(key
, pub_key
, dh
));
1357 keylen
= BN_num_bits(dh
->p
);
1359 memset(&kop
, 0, sizeof kop
);
1360 kop
.crk_op
= CRK_DH_COMPUTE_KEY
;
1362 /* inputs: dh->priv_key pub_key dh->p key */
1363 if (bn2crparam(dh
->priv_key
, &kop
.crk_param
[0]))
1365 if (bn2crparam(pub_key
, &kop
.crk_param
[1]))
1367 if (bn2crparam(dh
->p
, &kop
.crk_param
[2]))
1369 kop
.crk_iparams
= 3;
1371 kop
.crk_param
[3].crp_p
= (caddr_t
) key
;
1372 kop
.crk_param
[3].crp_nbits
= keylen
* 8;
1373 kop
.crk_oparams
= 1;
1375 if (ioctl(fd
, CIOCKEY
, &kop
) == -1) {
1376 const DH_METHOD
*meth
= DH_OpenSSL();
1378 dhret
= (meth
->compute_key
)(key
, pub_key
, dh
);
1381 kop
.crk_param
[3].crp_p
= NULL
;
1386 static DH_METHOD cryptodev_dh
= {
1387 "cryptodev DH method",
1388 NULL
, /* cryptodev_dh_generate_key */
1398 * ctrl right now is just a wrapper that doesn't do much
1399 * but I expect we'll want some options soon.
1402 cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void))
1404 #ifdef HAVE_SYSLOG_R
1405 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1410 #ifdef HAVE_SYSLOG_R
1411 syslog_r(LOG_ERR
, &sd
,
1412 "cryptodev_ctrl: unknown command %d", cmd
);
1414 syslog(LOG_ERR
, "cryptodev_ctrl: unknown command %d", cmd
);
1422 ENGINE_load_cryptodev(void)
1424 ENGINE
*engine
= ENGINE_new();
1429 if ((fd
= get_dev_crypto()) < 0) {
1430 ENGINE_free(engine
);
1435 * find out what asymmetric crypto algorithms we support
1437 if (ioctl(fd
, CIOCASYMFEAT
, &cryptodev_asymfeat
) == -1) {
1439 ENGINE_free(engine
);
1444 if (!ENGINE_set_id(engine
, "cryptodev") ||
1445 !ENGINE_set_name(engine
, "BSD cryptodev engine") ||
1446 !ENGINE_set_ciphers(engine
, cryptodev_engine_ciphers
) ||
1447 !ENGINE_set_digests(engine
, cryptodev_engine_digests
) ||
1448 !ENGINE_set_ctrl_function(engine
, cryptodev_ctrl
) ||
1449 !ENGINE_set_cmd_defns(engine
, cryptodev_defns
)) {
1450 ENGINE_free(engine
);
1454 if (ENGINE_set_RSA(engine
, &cryptodev_rsa
)) {
1455 const RSA_METHOD
*rsa_meth
= RSA_PKCS1_SSLeay();
1457 cryptodev_rsa
.bn_mod_exp
= rsa_meth
->bn_mod_exp
;
1458 cryptodev_rsa
.rsa_mod_exp
= rsa_meth
->rsa_mod_exp
;
1459 cryptodev_rsa
.rsa_pub_enc
= rsa_meth
->rsa_pub_enc
;
1460 cryptodev_rsa
.rsa_pub_dec
= rsa_meth
->rsa_pub_dec
;
1461 cryptodev_rsa
.rsa_priv_enc
= rsa_meth
->rsa_priv_enc
;
1462 cryptodev_rsa
.rsa_priv_dec
= rsa_meth
->rsa_priv_dec
;
1463 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1464 cryptodev_rsa
.bn_mod_exp
= cryptodev_bn_mod_exp
;
1465 if (cryptodev_asymfeat
& CRF_MOD_EXP_CRT
)
1466 cryptodev_rsa
.rsa_mod_exp
=
1467 cryptodev_rsa_mod_exp
;
1469 cryptodev_rsa
.rsa_mod_exp
=
1470 cryptodev_rsa_nocrt_mod_exp
;
1474 if (ENGINE_set_DSA(engine
, &cryptodev_dsa
)) {
1475 const DSA_METHOD
*meth
= DSA_OpenSSL();
1477 memcpy(&cryptodev_dsa
, meth
, sizeof(DSA_METHOD
));
1478 if (cryptodev_asymfeat
& CRF_DSA_SIGN
)
1479 cryptodev_dsa
.dsa_do_sign
= cryptodev_dsa_do_sign
;
1480 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1481 cryptodev_dsa
.bn_mod_exp
= cryptodev_dsa_bn_mod_exp
;
1482 cryptodev_dsa
.dsa_mod_exp
= cryptodev_dsa_dsa_mod_exp
;
1484 if (cryptodev_asymfeat
& CRF_DSA_VERIFY
)
1485 cryptodev_dsa
.dsa_do_verify
= cryptodev_dsa_verify
;
1488 if (ENGINE_set_DH(engine
, &cryptodev_dh
)){
1489 const DH_METHOD
*dh_meth
= DH_OpenSSL();
1491 cryptodev_dh
.generate_key
= dh_meth
->generate_key
;
1492 cryptodev_dh
.compute_key
= dh_meth
->compute_key
;
1493 cryptodev_dh
.bn_mod_exp
= dh_meth
->bn_mod_exp
;
1494 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1495 cryptodev_dh
.bn_mod_exp
= cryptodev_mod_exp_dh
;
1496 if (cryptodev_asymfeat
& CRF_DH_COMPUTE_KEY
)
1497 cryptodev_dh
.compute_key
=
1498 cryptodev_dh_compute_key
;
1503 ENGINE_free(engine
);
1507 #endif /* HAVE_CRYPTODEV */