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 #ifndef COP_FLAG_UPDATE
718 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
)) {
719 /* if application doesn't support one buffer */
720 state
->mac_data
= OPENSSL_realloc(state
->mac_data
, state
->mac_len
+ count
);
722 if (!state
->mac_data
) {
723 printf("cryptodev_digest_update: realloc failed\n");
727 memcpy(state
->mac_data
+ state
->mac_len
, data
, count
);
728 state
->mac_len
+= count
;
733 memset(&cryp
, 0, sizeof(cryp
));
737 memset(&cryp
, 0, sizeof(cryp
));
739 cryp
.flags
= COP_FLAG_UPDATe
;
741 cryp
.ses
= sess
->ses
;
743 cryp
.src
= (void*) data
;
745 cryp
.mac
= (void*) state
->digest_res
;
747 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
748 printf("cryptodev_digest_update: digest failed\n");
756 static int cryptodev_digest_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
758 struct crypt_op cryp
;
759 struct dev_crypto_state
*state
= ctx
->md_data
;
760 struct session_op
*sess
= &state
->d_sess
;
762 if (!md
|| state
->d_fd
< 0) {
763 printf("cryptodev_digest_final: illegal input\n");
767 #ifndef COP_FLAG_UPDATE
768 if (! (ctx
->flags
& EVP_MD_CTX_FLAG_ONESHOT
) ) {
769 /* if application doesn't support one buffer */
770 memset(&cryp
, 0, sizeof(cryp
));
771 cryp
.ses
= sess
->ses
;
773 cryp
.len
= state
->mac_len
;
774 cryp
.src
= state
->mac_data
;
776 cryp
.mac
= (void*)md
;
777 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
778 printf("cryptodev_digest_final: digest failed\n");
785 memcpy(md
, state
->digest_res
, ctx
->digest
->md_size
);
787 memset(&cryp
, 0, sizeof(cryp
));
788 cryp
.ses
= sess
->ses
;
789 cryp
.flags
= COP_FLAG_FINAL
;
790 cryp
.len
= state
->mac_len
;
791 cryp
.src
= state
->mac_data
;
793 cryp
.mac
= (void*)md
;
794 if (ioctl(state
->d_fd
, CIOCCRYPT
, &cryp
) < 0) {
795 printf("cryptodev_digest_final: digest failed\n");
804 static int cryptodev_digest_cleanup(EVP_MD_CTX
*ctx
)
807 struct dev_crypto_state
*state
= ctx
->md_data
;
808 struct session_op
*sess
= &state
->d_sess
;
813 if (state
->d_fd
< 0) {
814 printf("cryptodev_digest_cleanup: illegal input\n");
818 if (state
->mac_data
) {
819 OPENSSL_free(state
->mac_data
);
820 state
->mac_data
= NULL
;
824 if (ioctl(state
->d_fd
, CIOCFSESSION
, &sess
->ses
) < 0) {
825 printf("cryptodev_digest_cleanup: failed to close session\n");
830 put_dev_crypto(state
->d_fd
);
836 static int cryptodev_digest_copy(EVP_MD_CTX
*to
,const EVP_MD_CTX
*from
)
838 struct dev_crypto_state
*fstate
= from
->md_data
;
839 struct dev_crypto_state
*dstate
= to
->md_data
;
840 struct session_op
*sess
;
843 if (dstate
== NULL
|| fstate
== NULL
)
846 memcpy(dstate
, fstate
, sizeof(struct dev_crypto_state
));
848 sess
= &dstate
->d_sess
;
850 digest
= digest_nid_to_cryptodev(to
->digest
->type
);
856 dstate
->d_fd
= get_dev_crypto();
858 if (ioctl(dstate
->d_fd
, CIOCGSESSION
, sess
) < 0) {
859 put_dev_crypto(dstate
->d_fd
);
861 printf("cryptodev_digest_init: Open session failed\n");
865 if (fstate
->mac_len
!= 0) {
866 if (fstate
->mac_data
!= NULL
)
868 dstate
->mac_data
= OPENSSL_malloc(fstate
->mac_len
);
869 memcpy(dstate
->mac_data
, fstate
->mac_data
, fstate
->mac_len
);
870 dstate
->mac_len
= fstate
->mac_len
;
878 static const EVP_MD cryptodev_sha1
= {
880 NID_sha1WithRSAEncryption
,
882 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
883 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
884 EVP_MD_FLAG_DIGALGID_ABSENT
|
887 cryptodev_digest_init
,
888 cryptodev_digest_update
,
889 cryptodev_digest_final
,
890 cryptodev_digest_copy
,
891 cryptodev_digest_cleanup
,
894 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
897 static const EVP_MD cryptodev_sha256
= {
899 NID_sha256WithRSAEncryption
,
900 SHA256_DIGEST_LENGTH
,
901 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
902 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
903 EVP_MD_FLAG_DIGALGID_ABSENT
|
906 cryptodev_digest_init
,
907 cryptodev_digest_update
,
908 cryptodev_digest_final
,
909 cryptodev_digest_copy
,
910 cryptodev_digest_cleanup
,
913 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
915 static const EVP_MD cryptodev_sha224
= {
917 NID_sha224WithRSAEncryption
,
918 SHA224_DIGEST_LENGTH
,
919 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
920 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
921 EVP_MD_FLAG_DIGALGID_ABSENT
|
924 cryptodev_digest_init
,
925 cryptodev_digest_update
,
926 cryptodev_digest_final
,
927 cryptodev_digest_copy
,
928 cryptodev_digest_cleanup
,
931 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
934 static const EVP_MD cryptodev_sha384
= {
936 NID_sha384WithRSAEncryption
,
937 SHA384_DIGEST_LENGTH
,
938 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
939 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
940 EVP_MD_FLAG_DIGALGID_ABSENT
|
943 cryptodev_digest_init
,
944 cryptodev_digest_update
,
945 cryptodev_digest_final
,
946 cryptodev_digest_copy
,
947 cryptodev_digest_cleanup
,
950 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
953 static const EVP_MD cryptodev_sha512
= {
955 NID_sha512WithRSAEncryption
,
956 SHA512_DIGEST_LENGTH
,
957 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
958 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
959 EVP_MD_FLAG_DIGALGID_ABSENT
|
962 cryptodev_digest_init
,
963 cryptodev_digest_update
,
964 cryptodev_digest_final
,
965 cryptodev_digest_copy
,
966 cryptodev_digest_cleanup
,
969 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
972 static const EVP_MD cryptodev_md5
= {
974 NID_md5WithRSAEncryption
,
975 16 /* MD5_DIGEST_LENGTH */,
976 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
977 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE
|
978 EVP_MD_FLAG_DIGALGID_ABSENT
|
981 cryptodev_digest_init
,
982 cryptodev_digest_update
,
983 cryptodev_digest_final
,
984 cryptodev_digest_copy
,
985 cryptodev_digest_cleanup
,
988 sizeof(EVP_MD
*)+sizeof(struct dev_crypto_state
),
991 #endif /* USE_CRYPTODEV_DIGESTS */
995 cryptodev_engine_digests(ENGINE
*e
, const EVP_MD
**digest
,
996 const int **nids
, int nid
)
999 return (cryptodev_usable_digests(nids
));
1002 #ifdef USE_CRYPTODEV_DIGESTS
1004 *digest
= &cryptodev_md5
;
1007 *digest
= &cryptodev_sha1
;
1010 *digest
= &cryptodev_sha224
;
1013 *digest
= &cryptodev_sha256
;
1016 *digest
= &cryptodev_sha384
;
1019 *digest
= &cryptodev_sha512
;
1022 #endif /* USE_CRYPTODEV_DIGESTS */
1026 return (*digest
!= NULL
);
1030 * Convert a BIGNUM to the representation that /dev/crypto needs.
1031 * Upon completion of use, the caller is responsible for freeing
1035 bn2crparam(const BIGNUM
*a
, struct crparam
*crp
)
1038 ssize_t bytes
, bits
;
1044 bits
= BN_num_bits(a
);
1045 bytes
= (bits
+ 7) / 8;
1050 memset(b
, 0, bytes
);
1052 crp
->crp_p
= (void*) b
;
1053 crp
->crp_nbits
= bits
;
1055 for (i
= 0, j
= 0; i
< a
->top
; i
++) {
1056 for (k
= 0; k
< BN_BITS2
/ 8; k
++) {
1057 if ((j
+ k
) >= bytes
)
1059 b
[j
+ k
] = a
->d
[i
] >> (k
* 8);
1066 /* Convert a /dev/crypto parameter to a BIGNUM */
1068 crparam2bn(struct crparam
*crp
, BIGNUM
*a
)
1073 bytes
= (crp
->crp_nbits
+ 7) / 8;
1078 if ((pd
= (u_int8_t
*) malloc(bytes
)) == NULL
)
1081 for (i
= 0; i
< bytes
; i
++)
1082 pd
[i
] = crp
->crp_p
[bytes
- i
- 1];
1084 BN_bin2bn(pd
, bytes
, a
);
1091 zapparams(struct crypt_kop
*kop
)
1095 for (i
= 0; i
< kop
->crk_iparams
+ kop
->crk_oparams
; i
++) {
1096 if (kop
->crk_param
[i
].crp_p
)
1097 free(kop
->crk_param
[i
].crp_p
);
1098 kop
->crk_param
[i
].crp_p
= NULL
;
1099 kop
->crk_param
[i
].crp_nbits
= 0;
1104 cryptodev_asym(struct crypt_kop
*kop
, int rlen
, BIGNUM
*r
, int slen
, BIGNUM
*s
)
1108 if ((fd
= get_asym_dev_crypto()) < 0)
1112 kop
->crk_param
[kop
->crk_iparams
].crp_p
= calloc(rlen
, sizeof(char));
1113 kop
->crk_param
[kop
->crk_iparams
].crp_nbits
= rlen
* 8;
1117 kop
->crk_param
[kop
->crk_iparams
+1].crp_p
= calloc(slen
, sizeof(char));
1118 kop
->crk_param
[kop
->crk_iparams
+1].crp_nbits
= slen
* 8;
1122 if (ioctl(fd
, CIOCKEY
, kop
) == 0) {
1124 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
], r
);
1126 crparam2bn(&kop
->crk_param
[kop
->crk_iparams
+1], s
);
1134 cryptodev_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
1135 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
1137 struct crypt_kop kop
;
1140 /* Currently, we know we can do mod exp iff we can do any
1141 * asymmetric operations at all.
1143 if (cryptodev_asymfeat
== 0) {
1144 ret
= BN_mod_exp(r
, a
, p
, m
, ctx
);
1148 memset(&kop
, 0, sizeof kop
);
1149 kop
.crk_op
= CRK_MOD_EXP
;
1151 /* inputs: a^p % m */
1152 if (bn2crparam(a
, &kop
.crk_param
[0]))
1154 if (bn2crparam(p
, &kop
.crk_param
[1]))
1156 if (bn2crparam(m
, &kop
.crk_param
[2]))
1158 kop
.crk_iparams
= 3;
1160 if (cryptodev_asym(&kop
, BN_num_bytes(m
), r
, 0, NULL
)) {
1161 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1162 printf("OCF asym process failed, Running in software\n");
1163 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1165 } else if (ECANCELED
== kop
.crk_status
) {
1166 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1167 printf("OCF hardware operation cancelled. Running in Software\n");
1168 ret
= meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, in_mont
);
1170 /* else cryptodev operation worked ok ==> ret = 1*/
1178 cryptodev_rsa_nocrt_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1182 r
= cryptodev_bn_mod_exp(r0
, I
, rsa
->d
, rsa
->n
, ctx
, NULL
);
1188 cryptodev_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
1190 struct crypt_kop kop
;
1193 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
1194 /* XXX 0 means failure?? */
1198 memset(&kop
, 0, sizeof kop
);
1199 kop
.crk_op
= CRK_MOD_EXP_CRT
;
1200 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1201 if (bn2crparam(rsa
->p
, &kop
.crk_param
[0]))
1203 if (bn2crparam(rsa
->q
, &kop
.crk_param
[1]))
1205 if (bn2crparam(I
, &kop
.crk_param
[2]))
1207 if (bn2crparam(rsa
->dmp1
, &kop
.crk_param
[3]))
1209 if (bn2crparam(rsa
->dmq1
, &kop
.crk_param
[4]))
1211 if (bn2crparam(rsa
->iqmp
, &kop
.crk_param
[5]))
1213 kop
.crk_iparams
= 6;
1215 if (cryptodev_asym(&kop
, BN_num_bytes(rsa
->n
), r0
, 0, NULL
)) {
1216 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1217 printf("OCF asym process failed, running in Software\n");
1218 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1220 } else if (ECANCELED
== kop
.crk_status
) {
1221 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
1222 printf("OCF hardware operation cancelled. Running in Software\n");
1223 ret
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
1225 /* else cryptodev operation worked ok ==> ret = 1*/
1232 static RSA_METHOD cryptodev_rsa
= {
1233 "cryptodev RSA method",
1234 NULL
, /* rsa_pub_enc */
1235 NULL
, /* rsa_pub_dec */
1236 NULL
, /* rsa_priv_enc */
1237 NULL
, /* rsa_priv_dec */
1243 NULL
, /* app_data */
1244 NULL
, /* rsa_sign */
1245 NULL
/* rsa_verify */
1249 cryptodev_dsa_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
,
1250 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
1252 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1256 cryptodev_dsa_dsa_mod_exp(DSA
*dsa
, BIGNUM
*t1
, BIGNUM
*g
,
1257 BIGNUM
*u1
, BIGNUM
*pub_key
, BIGNUM
*u2
, BIGNUM
*p
,
1258 BN_CTX
*ctx
, BN_MONT_CTX
*mont
)
1265 /* v = ( g^u1 * y^u2 mod p ) mod q */
1266 /* let t1 = g ^ u1 mod p */
1269 if (!dsa
->meth
->bn_mod_exp(dsa
,t1
,dsa
->g
,u1
,dsa
->p
,ctx
,mont
))
1272 /* let t2 = y ^ u2 mod p */
1273 if (!dsa
->meth
->bn_mod_exp(dsa
,&t2
,dsa
->pub_key
,u2
,dsa
->p
,ctx
,mont
))
1275 /* let u1 = t1 * t2 mod p */
1276 if (!BN_mod_mul(u1
,t1
,&t2
,dsa
->p
,ctx
))
1288 cryptodev_dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
1290 struct crypt_kop kop
;
1291 BIGNUM
*r
= NULL
, *s
= NULL
;
1292 DSA_SIG
*dsaret
= NULL
;
1294 if ((r
= BN_new()) == NULL
)
1296 if ((s
= BN_new()) == NULL
) {
1301 memset(&kop
, 0, sizeof kop
);
1302 kop
.crk_op
= CRK_DSA_SIGN
;
1304 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1305 kop
.crk_param
[0].crp_p
= (void*)dgst
;
1306 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1307 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1309 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1311 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1313 if (bn2crparam(dsa
->priv_key
, &kop
.crk_param
[4]))
1315 kop
.crk_iparams
= 5;
1317 if (cryptodev_asym(&kop
, BN_num_bytes(dsa
->q
), r
,
1318 BN_num_bytes(dsa
->q
), s
) == 0) {
1319 dsaret
= DSA_SIG_new();
1323 const DSA_METHOD
*meth
= DSA_OpenSSL();
1326 dsaret
= (meth
->dsa_do_sign
)(dgst
, dlen
, dsa
);
1329 kop
.crk_param
[0].crp_p
= NULL
;
1335 cryptodev_dsa_verify(const unsigned char *dgst
, int dlen
,
1336 DSA_SIG
*sig
, DSA
*dsa
)
1338 struct crypt_kop kop
;
1341 memset(&kop
, 0, sizeof kop
);
1342 kop
.crk_op
= CRK_DSA_VERIFY
;
1344 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1345 kop
.crk_param
[0].crp_p
= (void*)dgst
;
1346 kop
.crk_param
[0].crp_nbits
= dlen
* 8;
1347 if (bn2crparam(dsa
->p
, &kop
.crk_param
[1]))
1349 if (bn2crparam(dsa
->q
, &kop
.crk_param
[2]))
1351 if (bn2crparam(dsa
->g
, &kop
.crk_param
[3]))
1353 if (bn2crparam(dsa
->pub_key
, &kop
.crk_param
[4]))
1355 if (bn2crparam(sig
->r
, &kop
.crk_param
[5]))
1357 if (bn2crparam(sig
->s
, &kop
.crk_param
[6]))
1359 kop
.crk_iparams
= 7;
1361 if (cryptodev_asym(&kop
, 0, NULL
, 0, NULL
) == 0) {
1362 /*OCF success value is 0, if not zero, change dsaret to fail*/
1363 if(0 != kop
.crk_status
) dsaret
= 0;
1365 const DSA_METHOD
*meth
= DSA_OpenSSL();
1367 dsaret
= (meth
->dsa_do_verify
)(dgst
, dlen
, sig
, dsa
);
1370 kop
.crk_param
[0].crp_p
= NULL
;
1375 static DSA_METHOD cryptodev_dsa
= {
1376 "cryptodev DSA method",
1378 NULL
, /* dsa_sign_setup */
1380 NULL
, /* dsa_mod_exp */
1389 cryptodev_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
1390 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
1393 return (cryptodev_bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
));
1397 cryptodev_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
1399 struct crypt_kop kop
;
1403 if ((fd
= get_asym_dev_crypto()) < 0) {
1404 const DH_METHOD
*meth
= DH_OpenSSL();
1406 return ((meth
->compute_key
)(key
, pub_key
, dh
));
1409 keylen
= BN_num_bits(dh
->p
);
1411 memset(&kop
, 0, sizeof kop
);
1412 kop
.crk_op
= CRK_DH_COMPUTE_KEY
;
1414 /* inputs: dh->priv_key pub_key dh->p key */
1415 if (bn2crparam(dh
->priv_key
, &kop
.crk_param
[0]))
1417 if (bn2crparam(pub_key
, &kop
.crk_param
[1]))
1419 if (bn2crparam(dh
->p
, &kop
.crk_param
[2]))
1421 kop
.crk_iparams
= 3;
1423 kop
.crk_param
[3].crp_p
= (void*) key
;
1424 kop
.crk_param
[3].crp_nbits
= keylen
* 8;
1425 kop
.crk_oparams
= 1;
1427 if (ioctl(fd
, CIOCKEY
, &kop
) == -1) {
1428 const DH_METHOD
*meth
= DH_OpenSSL();
1430 dhret
= (meth
->compute_key
)(key
, pub_key
, dh
);
1433 kop
.crk_param
[3].crp_p
= NULL
;
1438 static DH_METHOD cryptodev_dh
= {
1439 "cryptodev DH method",
1440 NULL
, /* cryptodev_dh_generate_key */
1450 * ctrl right now is just a wrapper that doesn't do much
1451 * but I expect we'll want some options soon.
1454 cryptodev_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void))
1456 #ifdef HAVE_SYSLOG_R
1457 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1462 #ifdef HAVE_SYSLOG_R
1463 syslog_r(LOG_ERR
, &sd
,
1464 "cryptodev_ctrl: unknown command %d", cmd
);
1466 syslog(LOG_ERR
, "cryptodev_ctrl: unknown command %d", cmd
);
1474 ENGINE_load_cryptodev(void)
1476 ENGINE
*engine
= ENGINE_new();
1481 if ((fd
= get_dev_crypto()) < 0) {
1482 ENGINE_free(engine
);
1487 * find out what asymmetric crypto algorithms we support
1489 if (ioctl(fd
, CIOCASYMFEAT
, &cryptodev_asymfeat
) == -1) {
1491 ENGINE_free(engine
);
1496 if (!ENGINE_set_id(engine
, "cryptodev") ||
1497 !ENGINE_set_name(engine
, "cryptodev engine") ||
1498 !ENGINE_set_ciphers(engine
, cryptodev_engine_ciphers
) ||
1499 !ENGINE_set_digests(engine
, cryptodev_engine_digests
) ||
1500 !ENGINE_set_ctrl_function(engine
, cryptodev_ctrl
) ||
1501 !ENGINE_set_cmd_defns(engine
, cryptodev_defns
)) {
1502 ENGINE_free(engine
);
1506 if (ENGINE_set_RSA(engine
, &cryptodev_rsa
)) {
1507 const RSA_METHOD
*rsa_meth
= RSA_PKCS1_SSLeay();
1509 cryptodev_rsa
.bn_mod_exp
= rsa_meth
->bn_mod_exp
;
1510 cryptodev_rsa
.rsa_mod_exp
= rsa_meth
->rsa_mod_exp
;
1511 cryptodev_rsa
.rsa_pub_enc
= rsa_meth
->rsa_pub_enc
;
1512 cryptodev_rsa
.rsa_pub_dec
= rsa_meth
->rsa_pub_dec
;
1513 cryptodev_rsa
.rsa_priv_enc
= rsa_meth
->rsa_priv_enc
;
1514 cryptodev_rsa
.rsa_priv_dec
= rsa_meth
->rsa_priv_dec
;
1515 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1516 cryptodev_rsa
.bn_mod_exp
= cryptodev_bn_mod_exp
;
1517 if (cryptodev_asymfeat
& CRF_MOD_EXP_CRT
)
1518 cryptodev_rsa
.rsa_mod_exp
=
1519 cryptodev_rsa_mod_exp
;
1521 cryptodev_rsa
.rsa_mod_exp
=
1522 cryptodev_rsa_nocrt_mod_exp
;
1526 if (ENGINE_set_DSA(engine
, &cryptodev_dsa
)) {
1527 const DSA_METHOD
*meth
= DSA_OpenSSL();
1529 memcpy(&cryptodev_dsa
, meth
, sizeof(DSA_METHOD
));
1530 if (cryptodev_asymfeat
& CRF_DSA_SIGN
)
1531 cryptodev_dsa
.dsa_do_sign
= cryptodev_dsa_do_sign
;
1532 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1533 cryptodev_dsa
.bn_mod_exp
= cryptodev_dsa_bn_mod_exp
;
1534 cryptodev_dsa
.dsa_mod_exp
= cryptodev_dsa_dsa_mod_exp
;
1536 if (cryptodev_asymfeat
& CRF_DSA_VERIFY
)
1537 cryptodev_dsa
.dsa_do_verify
= cryptodev_dsa_verify
;
1540 if (ENGINE_set_DH(engine
, &cryptodev_dh
)){
1541 const DH_METHOD
*dh_meth
= DH_OpenSSL();
1543 cryptodev_dh
.generate_key
= dh_meth
->generate_key
;
1544 cryptodev_dh
.compute_key
= dh_meth
->compute_key
;
1545 cryptodev_dh
.bn_mod_exp
= dh_meth
->bn_mod_exp
;
1546 if (cryptodev_asymfeat
& CRF_MOD_EXP
) {
1547 cryptodev_dh
.bn_mod_exp
= cryptodev_mod_exp_dh
;
1548 if (cryptodev_asymfeat
& CRF_DH_COMPUTE_KEY
)
1549 cryptodev_dh
.compute_key
=
1550 cryptodev_dh_compute_key
;
1555 ENGINE_free(engine
);
1559 #endif /* HAVE_CRYPTODEV */