2 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3 * Copyright (c) 2002 Theo de Raadt
4 * Copyright (c) 2002 Markus Friedl
5 * Copyright (c) 2012 Nikos Mavrogiannopoulos
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <openssl/objects.h>
31 #include <openssl/engine.h>
32 #include <openssl/evp.h>
33 #include <openssl/bn.h>
35 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
36 (defined(OpenBSD) || defined(__FreeBSD__))
37 #include <sys/param.h>
38 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
39 # define HAVE_CRYPTODEV
41 # if (OpenBSD >= 200110)
42 # define HAVE_SYSLOG_R
46 #ifndef HAVE_CRYPTODEV
49 ENGINE_load_cryptodev(void)
51 /* This is a NOP on platforms without /dev/crypto */
57 #include <sys/types.h>
58 #include <crypto/cryptodev.h>
59 #include <crypto/dh/dh.h>
60 #include <crypto/dsa/dsa.h>
61 #include <crypto/err/err.h>
62 #include <crypto/rsa/rsa.h>
63 #include <sys/ioctl.h>
73 struct dev_crypto_state
{
74 struct session_op d_sess
;
77 #ifdef USE_CRYPTODEV_DIGESTS
78 unsigned char digest_res
[HASH_MAX_LEN
];
84 static u_int32_t cryptodev_asymfeat
= 0;
86 static int get_asym_dev_crypto(void);
87 static int open_dev_crypto(void);
88 static int get_dev_crypto(void);
89 static int get_cryptodev_ciphers(const int **cnids
);
90 #ifdef USE_CRYPTODEV_DIGESTS
91 static int get_cryptodev_digests(const int **cnids
);
93 static int cryptodev_usable_ciphers(const int **nids
);
94 static int cryptodev_usable_digests(const int **nids
);
95 static int cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
96 const unsigned char *in
, size_t inl
);
97 static int cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
98 const unsigned char *iv
, int enc
);
99 static int cryptodev_cleanup(EVP_CIPHER_CTX
*ctx
);
100 static int cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
101 const int **nids
, int nid
);
102 static int cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
103 const int **nids
, int nid
);
104 static int bn2crparam(const BIGNUM
*a
, struct crparam
*crp
);
105 static int crparam2bn(struct crparam
*crp
, BIGNUM
*a
);
106 static void zapparams(struct crypt_kop
*kop
);
107 static int cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
,
108 int slen
, BIGNUM
*s
);
110 static int cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
,
111 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
112 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
,
113 RSA
*rsa
, BN_CTX
*ctx
);
114 static int cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
);
115 static int cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
116 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
117 static int cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
118 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
119 BN_CTX
*ctx
, BN_MONT_CTX
*mont
);
120 static DSA_SIG
*cryptodev_dsa_do_sign(const unsigned char *dgst
,
122 static int cryptodev_dsa_verify(const unsigned char *dgst
, int dgst_len
,
123 DSA_SIG
*sig
, DSA
*dsa
);
124 static int cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
125 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
127 static int cryptodev_dh_compute_key(unsigned char *key
,
128 const BIGNUM
*pub_key
, DH
*dh
);
129 static int cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
,
131 void ENGINE_load_cryptodev(void);
133 static const ENGINE_CMD_DEFN cryptodev_defns
[] = {
143 { CRYPTO_ARC4
, NID_rc4
, 0, 16, },
144 { CRYPTO_DES_CBC
, NID_des_cbc
, 8, 8, },
145 { CRYPTO_3DES_CBC
, NID_des_ede3_cbc
, 8, 24, },
146 { CRYPTO_AES_CBC
, NID_aes_128_cbc
, 16, 16, },
147 { CRYPTO_AES_CBC
, NID_aes_192_cbc
, 16, 24, },
148 { CRYPTO_AES_CBC
, NID_aes_256_cbc
, 16, 32, },
149 { CRYPTO_BLF_CBC
, NID_bf_cbc
, 8, 16, },
150 { CRYPTO_CAST_CBC
, NID_cast5_cbc
, 8, 16, },
151 { CRYPTO_SKIPJACK_CBC
, NID_undef
, 0, 0, },
152 { 0, NID_undef
, 0, 0, },
155 #ifdef USE_CRYPTODEV_DIGESTS
162 /* HMAC is not supported */
163 { CRYPTO_MD5_HMAC
, NID_hmacWithMD5
, 16},
164 { CRYPTO_SHA1_HMAC
, NID_hmacWithSHA1
, 20},
165 { CRYPTO_SHA2_256_HMAC
, NID_hmacWithSHA256
, 32},
166 { CRYPTO_SHA2_384_HMAC
, NID_hmacWithSHA384
, 48},
167 { CRYPTO_SHA2_512_HMAC
, NID_hmacWithSHA512
, 64},
169 { CRYPTO_MD5
, NID_md5
, 16},
170 { CRYPTO_SHA1
, NID_sha1
, 20},
171 { CRYPTO_SHA2_256
, NID_sha256
, 32},
172 { CRYPTO_SHA2_384
, NID_sha384
, 48},
173 { CRYPTO_SHA2_512
, NID_sha512
, 64},
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;
251 unsigned char fake_key
[CRYPTO_CIPHER_MAX_KEY_LEN
];
253 if ((fd
= get_dev_crypto()) < 0) {
257 memset(&sess
, 0, sizeof(sess
));
258 sess
.key
= (void*)fake_key
;
260 for (i
= 0; ciphers
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
261 if (ciphers
[i
].nid
== NID_undef
)
263 sess
.cipher
= ciphers
[i
].id
;
264 sess
.keylen
= ciphers
[i
].keylen
;
266 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
267 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
268 nids
[count
++] = ciphers
[i
].nid
;
279 #ifdef USE_CRYPTODEV_DIGESTS
281 * Find out what digests /dev/crypto will let us have a session for.
282 * XXX note, that some of these openssl doesn't deal with yet!
283 * returning them here is harmless, as long as we return NULL
284 * when asked for a handler in the cryptodev_engine_digests routine
287 get_cryptodev_digests(const int **cnids
)
289 static int nids
[CRYPTO_ALGORITHM_MAX
];
290 unsigned char fake_key
[CRYPTO_CIPHER_MAX_KEY_LEN
];
291 struct session_op sess
;
292 int fd
, i
, count
= 0;
294 if ((fd
= get_dev_crypto()) < 0) {
298 memset(&sess
, 0, sizeof(sess
));
299 sess
.mackey
= fake_key
;
300 for (i
= 0; digests
[i
].id
&& count
< CRYPTO_ALGORITHM_MAX
; i
++) {
301 if (digests
[i
].nid
== NID_undef
)
303 sess
.mac
= digests
[i
].id
;
306 if (ioctl(fd
, CIOCGSESSION
, &sess
) != -1 &&
307 ioctl(fd
, CIOCFSESSION
, &sess
.ses
) != -1)
308 nids
[count
++] = digests
[i
].nid
;
321 * Find the useable ciphers|digests from dev/crypto - this is the first
322 * thing called by the engine init crud which determines what it
323 * can use for ciphers from this engine. We want to return
324 * only what we can do, anythine else is handled by software.
326 * If we can't initialize the device to do anything useful for
327 * any reason, we want to return a NULL array, and 0 length,
328 * which forces everything to be done is software. By putting
329 * the initalization of the device in here, we ensure we can
330 * use this engine as the default, and if for whatever reason
331 * /dev/crypto won't do what we want it will just be done in
334 * This can (should) be greatly expanded to perhaps take into
335 * account speed of the device, and what we want to do.
336 * (although the disabling of particular alg's could be controlled
337 * by the device driver with sysctl's.) - this is where we
338 * want most of the decisions made about what we actually want
339 * to use from /dev/crypto.
342 cryptodev_usable_ciphers(const int **nids
)
344 return (get_cryptodev_ciphers(nids
));
348 cryptodev_usable_digests(const int **nids
)
350 #ifdef USE_CRYPTODEV_DIGESTS
351 return (get_cryptodev_digests(nids
));
354 * XXXX just disable all digests for now, because it sucks.
355 * we need a better way to decide this - i.e. I may not
356 * want digests on slow cards like hifn on fast machines,
357 * but might want them on slow or loaded machines, etc.
358 * will also want them when using crypto cards that don't
359 * suck moose gonads - would be nice to be able to decide something
360 * as reasonable default without having hackery that's card dependent.
361 * of course, the default should probably be just do everything,
362 * with perhaps a sysctl to turn algoritms off (or have them off
363 * by default) on cards that generally suck like the hifn.
371 cryptodev_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
372 const unsigned char *in
, size_t inl
)
374 struct crypt_op cryp
;
375 struct dev_crypto_state
*state
= ctx
->cipher_data
;
376 struct session_op
*sess
= &state
->d_sess
;
378 unsigned char save_iv
[EVP_MAX_IV_LENGTH
];
384 if ((inl
% ctx
->cipher
->block_size
) != 0)
387 memset(&cryp
, 0, sizeof(cryp
));
389 cryp
.ses
= sess
->ses
;
392 cryp
.src
= (void*) in
;
393 cryp
.dst
= (void*) out
;
396 cryp
.op
= ctx
->encrypt
? COP_ENCRYPT
: COP_DECRYPT
;
398 if (ctx
->cipher
->iv_len
) {
399 cryp
.iv
= (void*) ctx
->iv
;
401 iiv
= in
+ inl
- ctx
->cipher
->iv_len
;
402 memcpy(save_iv
, iiv
, ctx
->cipher
->iv_len
);
407 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) == -1) {
408 /* XXX need better errror handling
409 * this can fail for a number of different reasons.
414 if (ctx
->cipher
->iv_len
) {
416 iiv
= out
+ inl
- ctx
->cipher
->iv_len
;
419 memcpy(ctx
->iv
, iiv
, ctx
->cipher
->iv_len
);
425 cryptodev_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
426 const unsigned char *iv
, int enc
)
428 struct dev_crypto_state
*state
= ctx
->cipher_data
;
429 struct session_op
*sess
= &state
->d_sess
;
432 for (i
= 0; ciphers
[i
].id
; i
++)
433 if (ctx
->cipher
->nid
== ciphers
[i
].nid
&&
434 ctx
->cipher
->iv_len
<= ciphers
[i
].ivmax
&&
435 ctx
->key_len
== ciphers
[i
].keylen
) {
436 cipher
= ciphers
[i
].id
;
440 if (!ciphers
[i
].id
) {
445 memset(sess
, 0, sizeof(struct session_op
));
447 if ((state
->d_fd
= get_dev_crypto()) < 0)
450 sess
->key
= (void*)key
;
451 sess
->keylen
= ctx
->key_len
;
452 sess
->cipher
= cipher
;
454 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) == -1) {
455 put_dev_crypto(state
->d_fd
);
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) {
492 put_dev_crypto(state
->d_fd
);
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_rc4
= {
507 EVP_CIPH_VARIABLE_LENGTH
,
511 sizeof(struct dev_crypto_state
),
518 const EVP_CIPHER cryptodev_des_cbc
= {
525 sizeof(struct dev_crypto_state
),
526 EVP_CIPHER_set_asn1_iv
,
527 EVP_CIPHER_get_asn1_iv
,
532 const EVP_CIPHER cryptodev_3des_cbc
= {
539 sizeof(struct dev_crypto_state
),
540 EVP_CIPHER_set_asn1_iv
,
541 EVP_CIPHER_get_asn1_iv
,
545 const EVP_CIPHER cryptodev_bf_cbc
= {
552 sizeof(struct dev_crypto_state
),
553 EVP_CIPHER_set_asn1_iv
,
554 EVP_CIPHER_get_asn1_iv
,
558 const EVP_CIPHER cryptodev_cast_cbc
= {
565 sizeof(struct dev_crypto_state
),
566 EVP_CIPHER_set_asn1_iv
,
567 EVP_CIPHER_get_asn1_iv
,
571 const EVP_CIPHER cryptodev_aes_cbc
= {
578 sizeof(struct dev_crypto_state
),
579 EVP_CIPHER_set_asn1_iv
,
580 EVP_CIPHER_get_asn1_iv
,
584 const EVP_CIPHER cryptodev_aes_192_cbc
= {
591 sizeof(struct dev_crypto_state
),
592 EVP_CIPHER_set_asn1_iv
,
593 EVP_CIPHER_get_asn1_iv
,
597 const EVP_CIPHER cryptodev_aes_256_cbc
= {
604 sizeof(struct dev_crypto_state
),
605 EVP_CIPHER_set_asn1_iv
,
606 EVP_CIPHER_get_asn1_iv
,
611 * Registered by the ENGINE when used to find out how to deal with
612 * a particular NID in the ENGINE. this says what we'll do at the
613 * top level - note, that list is restricted by what we answer with
616 cryptodev_engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
617 const int **nids
, int nid
)
620 return (cryptodev_usable_ciphers(nids
));
624 *cipher
= &cryptodev_rc4
;
626 case NID_des_ede3_cbc
:
627 *cipher
= &cryptodev_3des_cbc
;
630 *cipher
= &cryptodev_des_cbc
;
633 *cipher
= &cryptodev_bf_cbc
;
636 *cipher
= &cryptodev_cast_cbc
;
638 case NID_aes_128_cbc
:
639 *cipher
= &cryptodev_aes_cbc
;
641 case NID_aes_192_cbc
:
642 *cipher
= &cryptodev_aes_192_cbc
;
644 case NID_aes_256_cbc
:
645 *cipher
= &cryptodev_aes_256_cbc
;
651 return (*cipher
!= NULL
);
655 #ifdef USE_CRYPTODEV_DIGESTS
657 /* convert digest type to cryptodev */
659 digest_nid_to_cryptodev(int nid
)
663 for (i
= 0; digests
[i
].id
; i
++)
664 if (digests
[i
].nid
== nid
)
665 return (digests
[i
].id
);
670 static int cryptodev_digest_init(EVP_MD_CTX
*ctx
)
672 struct dev_crypto_state
*state
= ctx
->md_data
;
673 struct session_op
*sess
= &state
->d_sess
;
676 if ((digest
= digest_nid_to_cryptodev(ctx
->digest
->type
)) == NID_undef
){
677 printf("cryptodev_digest_init: Can't get digest \n");
680 memset(state
, 0, sizeof(struct dev_crypto_state
));
682 if ((state
->d_fd
= get_dev_crypto()) < 0) {
683 printf("cryptodev_digest_init: Can't get Dev \n");
691 if (ioctl(state
->d_fd
, CIOCGSESSION
, sess
) < 0) {
692 put_dev_crypto(state
->d_fd
);
694 printf("cryptodev_digest_init: Open session failed\n");
701 static int cryptodev_digest_update(EVP_MD_CTX
*ctx
, const void *data
,
704 struct dev_crypto_state
*state
= ctx
->md_data
;
705 struct crypt_op cryp
;
706 struct session_op
*sess
= &state
->d_sess
;
708 if (!data
|| state
->d_fd
< 0) {
709 printf("cryptodev_digest_update: illegal inputs \n");
717 /* If an application wants to compute the hash in one big chunk (one-shot mode)
718 * then tack the data onto the end of the buffer (and grow as necessary).
720 if ( ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
) {
721 state
->mac_data
= OPENSSL_realloc(state
->mac_data
, state
->mac_len
+ count
);
723 if (!state
->mac_data
) {
724 printf("cryptodev_digest_update: realloc failed\n");
728 memcpy(state
->mac_data
+ state
->mac_len
, data
, count
);
729 state
->mac_len
+= count
;
734 memset(&cryp
, 0, sizeof(cryp
));
736 cryp
.ses
= sess
->ses
;
737 #ifdef COP_FLAG_UPDATE
738 cryp
.flags
= COP_FLAG_UPDATE
;
741 cryp
.src
= (void*) data
;
743 cryp
.mac
= (void*) state
->digest_res
;
744 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
745 printf("cryptodev_digest_update: digest failed\n");
752 static int cryptodev_digest_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
754 struct crypt_op cryp
;
755 struct dev_crypto_state
*state
= ctx
->md_data
;
756 struct session_op
*sess
= &state
->d_sess
;
758 if (!md
|| state
->d_fd
< 0) {
759 printf("cryptodev_digest_final: illegal input\n");
763 memset(&cryp
, 0, sizeof(cryp
));
764 cryp
.ses
= sess
->ses
;
765 #ifdef COP_FLAG_FINAL
766 cryp
.flags
= COP_FLAG_FINAL
;
768 cryp
.len
= state
->mac_len
;
769 cryp
.src
= state
->mac_data
;
771 cryp
.mac
= (void*)md
;
772 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
773 printf("cryptodev_digest_final: digest failed\n");
781 static int cryptodev_digest_cleanup(EVP_MD_CTX
*ctx
)
784 struct dev_crypto_state
*state
= ctx
->md_data
;
785 struct session_op
*sess
= &state
->d_sess
;
790 if (state
->d_fd
< 0) {
791 printf("cryptodev_digest_cleanup: illegal input\n");
795 if (state
->mac_data
) {
796 OPENSSL_free(state
->mac_data
);
797 state
->mac_data
= NULL
;
801 if (ioctl(state
->d_fd
, CIOCFSESSION
, &sess
->ses
) < 0) {
802 printf("cryptodev_digest_cleanup: failed to close session\n");
807 put_dev_crypto(state
->d_fd
);
813 static int cryptodev_digest_copy(EVP_MD_CTX
*to
,const EVP_MD_CTX
*from
)
815 struct dev_crypto_state
*fstate
= from
->md_data
;
816 struct dev_crypto_state
*dstate
= to
->md_data
;
817 struct session_op
*sess
;
820 if (dstate
== NULL
|| fstate
== NULL
)
823 memcpy(dstate
, fstate
, sizeof(struct dev_crypto_state
));
825 sess
= &dstate
->d_sess
;
827 digest
= digest_nid_to_cryptodev(to
->digest
->type
);
833 dstate
->d_fd
= get_dev_crypto();
835 if (ioctl(dstate
->d_fd
, CIOCGSESSION
, sess
) < 0) {
836 put_dev_crypto(dstate
->d_fd
);
838 printf("cryptodev_digest_init: Open session failed\n");
842 if (fstate
->mac_len
!= 0) {
843 if (fstate
->mac_data
!= NULL
)
845 dstate
->mac_data
= OPENSSL_malloc(fstate
->mac_len
);
846 memcpy(dstate
->mac_data
, fstate
->mac_data
, fstate
->mac_len
);
847 dstate
->mac_len
= fstate
->mac_len
;
855 static const EVP_MD cryptodev_sha1
= {
857 NID_sha1WithRSAEncryption
,
859 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
860 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
861 EVP_MD_FLAG_DIGALGID_ABSENT
|
864 cryptodev_digest_init
,
865 cryptodev_digest_update
,
866 cryptodev_digest_final
,
867 cryptodev_digest_copy
,
868 cryptodev_digest_cleanup
,
871 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
874 static const EVP_MD cryptodev_sha256
= {
876 NID_sha256WithRSAEncryption
,
877 SHA256_DIGEST_LENGTH
,
878 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
879 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
880 EVP_MD_FLAG_DIGALGID_ABSENT
|
883 cryptodev_digest_init
,
884 cryptodev_digest_update
,
885 cryptodev_digest_final
,
886 cryptodev_digest_copy
,
887 cryptodev_digest_cleanup
,
890 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
893 static const EVP_MD cryptodev_sha384
= {
895 NID_sha384WithRSAEncryption
,
896 SHA384_DIGEST_LENGTH
,
897 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
898 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
899 EVP_MD_FLAG_DIGALGID_ABSENT
|
902 cryptodev_digest_init
,
903 cryptodev_digest_update
,
904 cryptodev_digest_final
,
905 cryptodev_digest_copy
,
906 cryptodev_digest_cleanup
,
909 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
912 static const EVP_MD cryptodev_sha512
= {
914 NID_sha512WithRSAEncryption
,
915 SHA512_DIGEST_LENGTH
,
916 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
917 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
918 EVP_MD_FLAG_DIGALGID_ABSENT
|
921 cryptodev_digest_init
,
922 cryptodev_digest_update
,
923 cryptodev_digest_final
,
924 cryptodev_digest_copy
,
925 cryptodev_digest_cleanup
,
928 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
931 static const EVP_MD cryptodev_md5
= {
933 NID_md5WithRSAEncryption
,
934 16 /* MD5_DIGEST_LENGTH */,
935 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
936 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
937 EVP_MD_FLAG_DIGALGID_ABSENT
|
940 cryptodev_digest_init
,
941 cryptodev_digest_update
,
942 cryptodev_digest_final
,
943 cryptodev_digest_copy
,
944 cryptodev_digest_cleanup
,
947 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
950 #endif /* USE_CRYPTODEV_DIGESTS */
954 cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
955 const int **nids
, int nid
)
958 return (cryptodev_usable_digests(nids
));
961 #ifdef USE_CRYPTODEV_DIGESTS
963 *digest
= &cryptodev_md5
;
966 *digest
= &cryptodev_sha1
;
969 *digest
= &cryptodev_sha256
;
972 *digest
= &cryptodev_sha384
;
975 *digest
= &cryptodev_sha512
;
978 #endif /* USE_CRYPTODEV_DIGESTS */
982 return (*digest
!= NULL
);
986 * Convert a BIGNUM to the representation that /dev/crypto needs.
987 * Upon completion of use, the caller is responsible for freeing
991 bn2crparam(const BIGNUM
*a
, struct crparam
*crp
)
1000 bits
= BN_num_bits(a
);
1001 bytes
= (bits
+ 7) / 8;
1006 memset(b
, 0, bytes
);
1008 crp
->crp_p
= (void*) b
;
1009 crp
->crp_nbits
= bits
;
1011 for (i
= 0, j
= 0; i
< a
->top
; i
++) {
1012 for (k
= 0; k
< BN_BITS2
/ 8; k
++) {
1013 if ((j
+ k
) >= bytes
)
1015 b
[j
+ k
] = a
->d
[i
] >> (k
* 8);
1022 /* Convert a /dev/crypto parameter to a BIGNUM */
1024 crparam2bn(struct crparam
*crp
, BIGNUM
*a
)
1029 bytes
= (crp
->crp_nbits
+ 7) / 8;
1034 if ((pd
= (u_int8_t
*) malloc(bytes
)) == NULL
)
1037 for (i
= 0; i
< bytes
; i
++)
1038 pd
[i
] = crp
->crp_p
[bytes
- i
- 1];
1040 BN_bin2bn(pd
, bytes
, a
);
1047 zapparams(struct crypt_kop
*kop
)
1051 for (i
= 0; i
< kop
->crk_iparams
+ kop
->crk_oparams
; i
++) {
1052 if (kop
->crk_param
[i
].crp_p
)
1053 free(kop
->crk_param
[i
].crp_p
);
1054 kop
->crk_param
[i
].crp_p
= NULL
;
1055 kop
->crk_param
[i
].crp_nbits
= 0;
1060 cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
, int slen
, BIGNUM
*s
)
1064 if ((fd
= get_asym_dev_crypto()) < 0)
1068 kop
->crk_param
[kop
->crk_iparams
].crp_p
= calloc(rlen
, sizeof(char));
1069 kop
->crk_param
[kop
->crk_iparams
].crp_nbits
= rlen
* 8;
1073 kop
->crk_param
[kop
->crk_iparams
+1].crp_p
= calloc(slen
, sizeof(char));
1074 kop
->crk_param
[kop
->crk_iparams
+1].crp_nbits
= slen
* 8;
1078 if (ioctl(fd
, CIOCKEY
, kop
) == 0) {
1080 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
], r
);
1082 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
+1], s
);
1090 cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
1091 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
1093 struct crypt_kop kop
;
1096 /* Currently, we know we can do mod exp iff we can do any
1097 * asymmetric operations at all.
1099 if (cryptodev_asymfeat
== 0) {
1100 ret
= BN_mod_exp(r
, a
, p
, m
, ctx
);
1104 memset(&kop
, 0, sizeof kop
);
1105 kop
.crk_op
= CRK_MOD_EXP
;
1107 /* inputs: a^p % m */
1108 if (bn2crparam(a
, &kop
.crk_param
[0]))
1110 if (bn2crparam(p
, &kop
.crk_param
[1]))
1112 if (bn2crparam(m
, &kop
.crk_param
[2]))
1114 kop
.crk_iparams
= 3;
1116 if (cryptodev_asym(&kop
, BN_num_bytes(m
), r
, 0, NULL
)) {
1117 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1118 printf("OCF asym process failed, Running in software\n");
1119 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1121 } else if (ECANCELED
== kop
.crk_status
) {
1122 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1123 printf("OCF hardware operation cancelled. Running in Software\n");
1124 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1126 /* else cryptodev operation worked ok ==> ret = 1*/
1134 cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1138 r
= cryptodev_bn_mod_exp(r0
, I
, rsa
->d
, rsa
->n
, ctx
, NULL
);
1144 cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1146 struct crypt_kop kop
;
1149 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
1150 /* XXX 0 means failure?? */
1154 memset(&kop
, 0, sizeof kop
);
1155 kop
.crk_op
= CRK_MOD_EXP_CRT
;
1156 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1157 if (bn2crparam(rsa
->p
, &kop
.crk_param
[0]))
1159 if (bn2crparam(rsa
->q
, &kop
.crk_param
[1]))
1161 if (bn2crparam(I
, &kop
.crk_param
[2]))
1163 if (bn2crparam(rsa
->dmp1
, &kop
.crk_param
[3]))
1165 if (bn2crparam(rsa
->dmq1
, &kop
.crk_param
[4]))
1167 if (bn2crparam(rsa
->iqmp
, &kop
.crk_param
[5]))
1169 kop
.crk_iparams
= 6;
1171 if (cryptodev_asym(&kop
, BN_num_bytes(rsa
->n
), r0
, 0, NULL
)) {
1172 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1173 printf("OCF asym process failed, running in Software\n");
1174 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1176 } else if (ECANCELED
== kop
.crk_status
) {
1177 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1178 printf("OCF hardware operation cancelled. Running in Software\n");
1179 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1181 /* else cryptodev operation worked ok ==> ret = 1*/
1188 static RSA_METHOD cryptodev_rsa
= {
1189 "cryptodev RSA method",
1190 NULL
, /* rsa_pub_enc */
1191 NULL
, /* rsa_pub_dec */
1192 NULL
, /* rsa_priv_enc */
1193 NULL
, /* rsa_priv_dec */
1199 NULL
, /* app_data */
1200 NULL
, /* rsa_sign */
1201 NULL
/* rsa_verify */
1205 cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
,
1206 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
1208 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1212 cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
1213 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
1214 BN_CTX
*ctx
, BN_MONT_CTX
*mont
)
1221 /* v = ( g^u1 * y^u2 mod p ) mod q */
1222 /* let t1 = g ^ u1 mod p */
1225 if (!dsa
->meth
->bn_mod_exp(dsa
,t1
,dsa
->g
,u1
,dsa
->p
,ctx
,mont
))
1228 /* let t2 = y ^ u2 mod p */
1229 if (!dsa
->meth
->bn_mod_exp(dsa
,&t2
,dsa
->pub_key
,u2
,dsa
->p
,ctx
,mont
))
1231 /* let u1 = t1 * t2 mod p */
1232 if (!BN_mod_mul(u1
,t1
,&t2
,dsa
->p
,ctx
))
1244 cryptodev_dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
1246 struct crypt_kop kop
;
1247 BIGNUM
*r
= NULL
, *s
= NULL
;
1248 DSA_SIG
*dsaret
= NULL
;
1250 if ((r
= BN_new()) == NULL
)
1252 if ((s
= BN_new()) == NULL
) {
1257 memset(&kop
, 0, sizeof kop
);
1258 kop
.crk_op
= CRK_DSA_SIGN
;
1260 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1261 kop
.crk_param
[0].crp_p
= (void*)dgst
;
1262 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1263 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1265 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1267 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1269 if (bn2crparam(dsa
->priv_key
, &kop
.crk_param
[4]))
1271 kop
.crk_iparams
= 5;
1273 if (cryptodev_asym(&kop
, BN_num_bytes(dsa
->q
), r
,
1274 BN_num_bytes(dsa
->q
), s
) == 0) {
1275 dsaret
= DSA_SIG_new();
1279 const DSA_METHOD
*meth
= DSA_OpenSSL();
1282 dsaret
= (meth
->dsa_do_sign
)(dgst
, dlen
, dsa
);
1285 kop
.crk_param
[0].crp_p
= NULL
;
1291 cryptodev_dsa_verify(const unsigned char *dgst
, int dlen
,
1292 DSA_SIG
*sig
, DSA
*dsa
)
1294 struct crypt_kop kop
;
1297 memset(&kop
, 0, sizeof kop
);
1298 kop
.crk_op
= CRK_DSA_VERIFY
;
1300 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1301 kop
.crk_param
[0].crp_p
= (void*)dgst
;
1302 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1303 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1305 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1307 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1309 if (bn2crparam(dsa
->pub_key
, &kop
.crk_param
[4]))
1311 if (bn2crparam(sig
->r
, &kop
.crk_param
[5]))
1313 if (bn2crparam(sig
->s
, &kop
.crk_param
[6]))
1315 kop
.crk_iparams
= 7;
1317 if (cryptodev_asym(&kop
, 0, NULL
, 0, NULL
) == 0) {
1318 /*OCF success value is 0, if not zero, change dsaret to fail*/
1319 if(0 != kop
.crk_status
) dsaret
= 0;
1321 const DSA_METHOD
*meth
= DSA_OpenSSL();
1323 dsaret
= (meth
->dsa_do_verify
)(dgst
, dlen
, sig
, dsa
);
1326 kop
.crk_param
[0].crp_p
= NULL
;
1331 static DSA_METHOD cryptodev_dsa
= {
1332 "cryptodev DSA method",
1334 NULL
, /* dsa_sign_setup */
1336 NULL
, /* dsa_mod_exp */
1345 cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
1346 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
1349 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1353 cryptodev_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
1355 struct crypt_kop kop
;
1359 if ((fd
= get_asym_dev_crypto()) < 0) {
1360 const DH_METHOD
*meth
= DH_OpenSSL();
1362 return ((meth
->compute_key
)(key
, pub_key
, dh
));
1365 keylen
= BN_num_bits(dh
->p
);
1367 memset(&kop
, 0, sizeof kop
);
1368 kop
.crk_op
= CRK_DH_COMPUTE_KEY
;
1370 /* inputs: dh->priv_key pub_key dh->p key */
1371 if (bn2crparam(dh
->priv_key
, &kop
.crk_param
[0]))
1373 if (bn2crparam(pub_key
, &kop
.crk_param
[1]))
1375 if (bn2crparam(dh
->p
, &kop
.crk_param
[2]))
1377 kop
.crk_iparams
= 3;
1379 kop
.crk_param
[3].crp_p
= (void*) key
;
1380 kop
.crk_param
[3].crp_nbits
= keylen
* 8;
1381 kop
.crk_oparams
= 1;
1383 if (ioctl(fd
, CIOCKEY
, &kop
) == -1) {
1384 const DH_METHOD
*meth
= DH_OpenSSL();
1386 dhret
= (meth
->compute_key
)(key
, pub_key
, dh
);
1389 kop
.crk_param
[3].crp_p
= NULL
;
1394 static DH_METHOD cryptodev_dh
= {
1395 "cryptodev DH method",
1396 NULL
, /* cryptodev_dh_generate_key */
1406 * ctrl right now is just a wrapper that doesn't do much
1407 * but I expect we'll want some options soon.
1410 cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void))
1412 #ifdef HAVE_SYSLOG_R
1413 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1418 #ifdef HAVE_SYSLOG_R
1419 syslog_r(LOG_ERR
, &sd
,
1420 "cryptodev_ctrl: unknown command %d", cmd
);
1422 syslog(LOG_ERR
, "cryptodev_ctrl: unknown command %d", cmd
);
1430 ENGINE_load_cryptodev(void)
1432 ENGINE
*engine
= ENGINE_new();
1437 if ((fd
= get_dev_crypto()) < 0) {
1438 ENGINE_free(engine
);
1443 * find out what asymmetric crypto algorithms we support
1445 if (ioctl(fd
, CIOCASYMFEAT
, &cryptodev_asymfeat
) == -1) {
1447 ENGINE_free(engine
);
1452 if (!ENGINE_set_id(engine
, "cryptodev") ||
1453 !ENGINE_set_name(engine
, "cryptodev engine") ||
1454 !ENGINE_set_ciphers(engine
, cryptodev_engine_ciphers
) ||
1455 !ENGINE_set_digests(engine
, cryptodev_engine_digests
) ||
1456 !ENGINE_set_ctrl_function(engine
, cryptodev_ctrl
) ||
1457 !ENGINE_set_cmd_defns(engine
, cryptodev_defns
)) {
1458 ENGINE_free(engine
);
1462 if (ENGINE_set_RSA(engine
, &cryptodev_rsa
)) {
1463 const RSA_METHOD
*rsa_meth
= RSA_PKCS1_SSLeay();
1465 cryptodev_rsa
.bn_mod_exp
= rsa_meth
->bn_mod_exp
;
1466 cryptodev_rsa
.rsa_mod_exp
= rsa_meth
->rsa_mod_exp
;
1467 cryptodev_rsa
.rsa_pub_enc
= rsa_meth
->rsa_pub_enc
;
1468 cryptodev_rsa
.rsa_pub_dec
= rsa_meth
->rsa_pub_dec
;
1469 cryptodev_rsa
.rsa_priv_enc
= rsa_meth
->rsa_priv_enc
;
1470 cryptodev_rsa
.rsa_priv_dec
= rsa_meth
->rsa_priv_dec
;
1471 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1472 cryptodev_rsa
.bn_mod_exp
= cryptodev_bn_mod_exp
;
1473 if (cryptodev_asymfeat
& CRF_MOD_EXP_CRT
)
1474 cryptodev_rsa
.rsa_mod_exp
=
1475 cryptodev_rsa_mod_exp
;
1477 cryptodev_rsa
.rsa_mod_exp
=
1478 cryptodev_rsa_nocrt_mod_exp
;
1482 if (ENGINE_set_DSA(engine
, &cryptodev_dsa
)) {
1483 const DSA_METHOD
*meth
= DSA_OpenSSL();
1485 memcpy(&cryptodev_dsa
, meth
, sizeof(DSA_METHOD
));
1486 if (cryptodev_asymfeat
& CRF_DSA_SIGN
)
1487 cryptodev_dsa
.dsa_do_sign
= cryptodev_dsa_do_sign
;
1488 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1489 cryptodev_dsa
.bn_mod_exp
= cryptodev_dsa_bn_mod_exp
;
1490 cryptodev_dsa
.dsa_mod_exp
= cryptodev_dsa_dsa_mod_exp
;
1492 if (cryptodev_asymfeat
& CRF_DSA_VERIFY
)
1493 cryptodev_dsa
.dsa_do_verify
= cryptodev_dsa_verify
;
1496 if (ENGINE_set_DH(engine
, &cryptodev_dh
)){
1497 const DH_METHOD
*dh_meth
= DH_OpenSSL();
1499 cryptodev_dh
.generate_key
= dh_meth
->generate_key
;
1500 cryptodev_dh
.compute_key
= dh_meth
->compute_key
;
1501 cryptodev_dh
.bn_mod_exp
= dh_meth
->bn_mod_exp
;
1502 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1503 cryptodev_dh
.bn_mod_exp
= cryptodev_mod_exp_dh
;
1504 if (cryptodev_asymfeat
& CRF_DH_COMPUTE_KEY
)
1505 cryptodev_dh
.compute_key
=
1506 cryptodev_dh_compute_key
;
1511 ENGINE_free(engine
);
1515 #endif /* HAVE_CRYPTODEV */