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 <internal/engine.h>
31 #include <openssl/evp.h>
32 #include <openssl/bn.h>
33 #include <openssl/crypto.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 #include <sys/types.h>
48 # include <crypto/cryptodev.h>
49 # include <sys/ioctl.h>
59 #include <openssl/dh.h>
60 #include <openssl/dsa.h>
61 #include <openssl/err.h>
62 #include <openssl/rsa.h>
64 #ifndef HAVE_CRYPTODEV
66 void engine_load_cryptodev_internal(void)
68 /* This is a NOP on platforms without /dev/crypto */
74 struct dev_crypto_state {
75 struct session_op d_sess;
77 # ifdef USE_CRYPTODEV_DIGESTS
78 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,
114 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
116 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
118 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
119 const BIGNUM *p, const BIGNUM *m,
120 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
121 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
122 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
123 BIGNUM *p, BN_CTX *ctx,
125 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
127 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
128 DSA_SIG *sig, DSA *dsa);
129 #ifndef OPENSSL_NO_DH
130 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
131 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
133 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
136 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
138 void engine_load_cryptodev_internal(void);
140 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
151 CRYPTO_ARC4, NID_rc4, 0, 16,
154 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
157 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
160 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
163 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
166 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
168 # ifdef CRYPTO_AES_CTR
170 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
173 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
176 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
180 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
183 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
186 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
193 # ifdef USE_CRYPTODEV_DIGESTS
200 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
203 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
206 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
210 CRYPTO_MD5_KPDK, NID_undef, 0
213 CRYPTO_SHA1_KPDK, NID_undef, 0
216 CRYPTO_MD5, NID_md5, 16
219 CRYPTO_SHA1, NID_sha1, 20
228 * Return a fd if /dev/crypto seems usable, 0 otherwise.
230 static int open_dev_crypto(void)
235 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
238 if (fcntl(fd, F_SETFD, 1) == -1) {
247 static int get_dev_crypto(void)
251 if ((fd = open_dev_crypto()) == -1)
253 # ifndef CRIOGET_NOT_NEEDED
254 if (ioctl(fd, CRIOGET, &retfd) == -1)
258 if (fcntl(retfd, F_SETFD, 1) == -1) {
268 static void put_dev_crypto(int fd)
270 # ifndef CRIOGET_NOT_NEEDED
275 /* Caching version for asym operations */
276 static int get_asym_dev_crypto(void)
281 fd = get_dev_crypto();
286 * Find out what ciphers /dev/crypto will let us have a session for.
287 * XXX note, that some of these openssl doesn't deal with yet!
288 * returning them here is harmless, as long as we return NULL
289 * when asked for a handler in the cryptodev_engine_ciphers routine
291 static int get_cryptodev_ciphers(const int **cnids)
293 static int nids[CRYPTO_ALGORITHM_MAX];
294 struct session_op sess;
295 int fd, i, count = 0;
297 if ((fd = get_dev_crypto()) < 0) {
301 memset(&sess, 0, sizeof(sess));
302 sess.key = (caddr_t) "123456789abcdefghijklmno";
304 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
305 if (ciphers[i].nid == NID_undef)
307 sess.cipher = ciphers[i].id;
308 sess.keylen = ciphers[i].keylen;
310 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
311 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
312 nids[count++] = ciphers[i].nid;
323 # ifdef USE_CRYPTODEV_DIGESTS
325 * Find out what digests /dev/crypto will let us have a session for.
326 * XXX note, that some of these openssl doesn't deal with yet!
327 * returning them here is harmless, as long as we return NULL
328 * when asked for a handler in the cryptodev_engine_digests routine
330 static int get_cryptodev_digests(const int **cnids)
332 static int nids[CRYPTO_ALGORITHM_MAX];
333 struct session_op sess;
334 int fd, i, count = 0;
336 if ((fd = get_dev_crypto()) < 0) {
340 memset(&sess, 0, sizeof(sess));
341 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
342 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
343 if (digests[i].nid == NID_undef)
345 sess.mac = digests[i].id;
346 sess.mackeylen = digests[i].keylen;
348 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
349 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
350 nids[count++] = digests[i].nid;
363 * Find the useable ciphers|digests from dev/crypto - this is the first
364 * thing called by the engine init crud which determines what it
365 * can use for ciphers from this engine. We want to return
366 * only what we can do, anything else is handled by software.
368 * If we can't initialize the device to do anything useful for
369 * any reason, we want to return a NULL array, and 0 length,
370 * which forces everything to be done is software. By putting
371 * the initialization of the device in here, we ensure we can
372 * use this engine as the default, and if for whatever reason
373 * /dev/crypto won't do what we want it will just be done in
376 * This can (should) be greatly expanded to perhaps take into
377 * account speed of the device, and what we want to do.
378 * (although the disabling of particular alg's could be controlled
379 * by the device driver with sysctl's.) - this is where we
380 * want most of the decisions made about what we actually want
381 * to use from /dev/crypto.
383 static int cryptodev_usable_ciphers(const int **nids)
385 return (get_cryptodev_ciphers(nids));
388 static int cryptodev_usable_digests(const int **nids)
390 # ifdef USE_CRYPTODEV_DIGESTS
391 return (get_cryptodev_digests(nids));
394 * XXXX just disable all digests for now, because it sucks.
395 * we need a better way to decide this - i.e. I may not
396 * want digests on slow cards like hifn on fast machines,
397 * but might want them on slow or loaded machines, etc.
398 * will also want them when using crypto cards that don't
399 * suck moose gonads - would be nice to be able to decide something
400 * as reasonable default without having hackery that's card dependent.
401 * of course, the default should probably be just do everything,
402 * with perhaps a sysctl to turn algorithms off (or have them off
403 * by default) on cards that generally suck like the hifn.
411 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
412 const unsigned char *in, size_t inl)
414 struct crypt_op cryp;
415 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
416 struct session_op *sess = &state->d_sess;
418 unsigned char save_iv[EVP_MAX_IV_LENGTH];
424 if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
427 memset(&cryp, 0, sizeof(cryp));
429 cryp.ses = sess->ses;
432 cryp.src = (caddr_t) in;
433 cryp.dst = (caddr_t) out;
436 cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
438 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
439 cryp.iv = *(caddr_t*) EVP_CIPHER_CTX_iv(ctx);
440 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
441 iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
442 memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
447 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
449 * XXX need better error handling this can fail for a number of
455 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
456 if (EVP_CIPHER_CTX_encrypting(ctx))
457 iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
460 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
461 EVP_CIPHER_CTX_iv_length(ctx));
467 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
468 const unsigned char *iv, int enc)
470 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
471 struct session_op *sess = &state->d_sess;
474 for (i = 0; ciphers[i].id; i++)
475 if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
476 EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
477 EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
478 cipher = ciphers[i].id;
482 if (!ciphers[i].id) {
487 memset(sess, 0, sizeof(*sess));
489 if ((state->d_fd = get_dev_crypto()) < 0)
492 sess->key = (caddr_t) key;
493 sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
494 sess->cipher = cipher;
496 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
497 put_dev_crypto(state->d_fd);
505 * free anything we allocated earlier when initing a
506 * session, and close the session.
508 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
511 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
512 struct session_op *sess = &state->d_sess;
518 * XXX if this ioctl fails, something's wrong. the invoker may have called
519 * us with a bogus ctx, or we could have a device that for whatever
520 * reason just doesn't want to play ball - it's not clear what's right
521 * here - should this be an error? should it just increase a counter,
522 * hmm. For right now, we return 0 - I don't believe that to be "right".
523 * we could call the gorpy openssl lib error handlers that print messages
524 * to users of the library. hmm..
527 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
532 put_dev_crypto(state->d_fd);
539 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
540 * gets called when libcrypto requests a cipher NID.
544 static EVP_CIPHER *rc4_cipher = NULL;
545 static const EVP_CIPHER *cryptodev_rc4(void)
547 if (rc4_cipher == NULL) {
550 if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16)) == NULL
551 || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
552 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
553 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
554 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
555 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
556 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))) {
557 EVP_CIPHER_meth_free(cipher);
566 static EVP_CIPHER *des_cbc_cipher = NULL;
567 static const EVP_CIPHER *cryptodev_des_cbc(void)
569 if (des_cbc_cipher == NULL) {
572 if ((cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8)) == NULL
573 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
574 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
575 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
576 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
577 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
578 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
579 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
580 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
581 EVP_CIPHER_meth_free(cipher);
584 des_cbc_cipher = cipher;
586 return des_cbc_cipher;
590 static EVP_CIPHER *des3_cbc_cipher = NULL;
591 static const EVP_CIPHER *cryptodev_3des_cbc(void)
593 if (des3_cbc_cipher == NULL) {
596 if ((cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24)) == NULL
597 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
598 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
599 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
600 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
601 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
602 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
603 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
604 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
605 EVP_CIPHER_meth_free(cipher);
608 des3_cbc_cipher = cipher;
610 return des3_cbc_cipher;
613 static EVP_CIPHER *bf_cbc_cipher = NULL;
614 static const EVP_CIPHER *cryptodev_bf_cbc(void)
616 if (bf_cbc_cipher == NULL) {
619 if ((cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16)) == NULL
620 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
621 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
622 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
623 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
624 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
625 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
626 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
627 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
628 EVP_CIPHER_meth_free(cipher);
631 bf_cbc_cipher = cipher;
633 return bf_cbc_cipher;
636 static EVP_CIPHER *cast_cbc_cipher = NULL;
637 static const EVP_CIPHER *cryptodev_cast_cbc(void)
639 if (cast_cbc_cipher == NULL) {
642 if ((cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16)) == NULL
643 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
644 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
645 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
646 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
647 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
648 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
649 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
650 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
651 EVP_CIPHER_meth_free(cipher);
654 cast_cbc_cipher = cipher;
656 return cast_cbc_cipher;
659 static EVP_CIPHER *aes_cbc_cipher = NULL;
660 static const EVP_CIPHER *cryptodev_aes_cbc(void)
662 if (aes_cbc_cipher == NULL) {
665 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16)) == NULL
666 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
667 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
668 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
669 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
670 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
671 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
672 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
673 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
674 EVP_CIPHER_meth_free(cipher);
677 aes_cbc_cipher = cipher;
679 return aes_cbc_cipher;
682 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
683 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
685 if (aes_192_cbc_cipher == NULL) {
688 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24)) == NULL
689 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
690 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
691 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
692 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
693 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
694 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
695 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
696 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
697 EVP_CIPHER_meth_free(cipher);
700 aes_192_cbc_cipher = cipher;
702 return aes_192_cbc_cipher;
705 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
706 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
708 if (aes_256_cbc_cipher == NULL) {
711 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32)) == NULL
712 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
713 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
714 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
715 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
716 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
717 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
718 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
719 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
720 EVP_CIPHER_meth_free(cipher);
723 aes_256_cbc_cipher = cipher;
725 return aes_256_cbc_cipher;
728 # ifdef CRYPTO_AES_CTR
729 static EVP_CIPHER *aes_ctr_cipher = NULL;
730 static const EVP_CIPHER *cryptodev_aes_ctr(void)
732 if (aes_ctr_cipher == NULL) {
735 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16)) == NULL
736 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
737 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
738 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
739 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
740 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
741 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
742 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
743 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
744 EVP_CIPHER_meth_free(cipher);
747 aes_ctr_cipher = cipher;
749 return aes_ctr_cipher;
752 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
753 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
755 if (aes_192_ctr_cipher == NULL) {
758 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24)) == NULL
759 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
760 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
761 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
762 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
763 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
764 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
765 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
766 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
767 EVP_CIPHER_meth_free(cipher);
770 aes_192_ctr_cipher = cipher;
772 return aes_192_ctr_cipher;
775 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
776 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
778 if (aes_256_ctr_cipher == NULL) {
781 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32)) == NULL
782 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
783 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
784 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
785 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
786 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
787 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
788 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
789 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
790 EVP_CIPHER_meth_free(cipher);
793 aes_256_ctr_cipher = cipher;
795 return aes_256_ctr_cipher;
799 * Registered by the ENGINE when used to find out how to deal with
800 * a particular NID in the ENGINE. this says what we'll do at the
801 * top level - note, that list is restricted by what we answer with
804 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
805 const int **nids, int nid)
808 return (cryptodev_usable_ciphers(nids));
812 *cipher = cryptodev_rc4();
814 case NID_des_ede3_cbc:
815 *cipher = cryptodev_3des_cbc();
818 *cipher = cryptodev_des_cbc();
821 *cipher = cryptodev_bf_cbc();
824 *cipher = cryptodev_cast_cbc();
826 case NID_aes_128_cbc:
827 *cipher = cryptodev_aes_cbc();
829 case NID_aes_192_cbc:
830 *cipher = cryptodev_aes_192_cbc();
832 case NID_aes_256_cbc:
833 *cipher = cryptodev_aes_256_cbc();
835 # ifdef CRYPTO_AES_CTR
836 case NID_aes_128_ctr:
837 *cipher = cryptodev_aes_ctr();
839 case NID_aes_192_ctr:
840 *cipher = cryptodev_aes_ctr_192();
842 case NID_aes_256_ctr:
843 *cipher = cryptodev_aes_ctr_256();
850 return (*cipher != NULL);
853 # ifdef USE_CRYPTODEV_DIGESTS
855 /* convert digest type to cryptodev */
856 static int digest_nid_to_cryptodev(int nid)
860 for (i = 0; digests[i].id; i++)
861 if (digests[i].nid == nid)
862 return (digests[i].id);
866 static int digest_key_length(int nid)
870 for (i = 0; digests[i].id; i++)
871 if (digests[i].nid == nid)
872 return digests[i].keylen;
876 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
878 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
879 struct session_op *sess = &state->d_sess;
882 if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
883 printf("cryptodev_digest_init: Can't get digest \n");
887 memset(state, 0, sizeof(*state));
889 if ((state->d_fd = get_dev_crypto()) < 0) {
890 printf("cryptodev_digest_init: Can't get Dev \n");
894 sess->mackey = state->dummy_mac_key;
895 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(ctx));
898 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
899 put_dev_crypto(state->d_fd);
901 printf("cryptodev_digest_init: Open session failed\n");
908 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
911 struct crypt_op cryp;
912 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
913 struct session_op *sess = &state->d_sess;
916 if (!data || state->d_fd < 0) {
917 printf("cryptodev_digest_update: illegal inputs \n");
925 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
926 /* if application doesn't support one buffer */
928 OPENSSL_realloc(state->mac_data, state->mac_len + count);
931 printf("cryptodev_digest_update: realloc failed\n");
934 state->mac_data = new_mac_data;
936 memcpy(state->mac_data + state->mac_len, data, count);
937 state->mac_len += count;
942 memset(&cryp, 0, sizeof(cryp));
944 cryp.ses = sess->ses;
947 cryp.src = (caddr_t) data;
949 cryp.mac = (caddr_t) state->digest_res;
950 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
951 printf("cryptodev_digest_update: digest failed\n");
957 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
959 struct crypt_op cryp;
960 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
961 struct session_op *sess = &state->d_sess;
965 if (!md || state->d_fd < 0) {
966 printf("cryptodev_digest_final: illegal input\n");
970 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
971 /* if application doesn't support one buffer */
972 memset(&cryp, 0, sizeof(cryp));
973 cryp.ses = sess->ses;
975 cryp.len = state->mac_len;
976 cryp.src = state->mac_data;
978 cryp.mac = (caddr_t) md;
979 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
980 printf("cryptodev_digest_final: digest failed\n");
987 memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
992 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
995 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
996 struct session_op *sess = &state->d_sess;
1001 if (state->d_fd < 0) {
1002 printf("cryptodev_digest_cleanup: illegal input\n");
1006 OPENSSL_free(state->mac_data);
1007 state->mac_data = NULL;
1010 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1011 printf("cryptodev_digest_cleanup: failed to close session\n");
1016 put_dev_crypto(state->d_fd);
1022 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1024 struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1025 struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1026 struct session_op *sess;
1029 if (dstate == NULL || fstate == NULL)
1032 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1034 sess = &dstate->d_sess;
1036 digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1038 sess->mackey = dstate->dummy_mac_key;
1039 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(to));
1042 dstate->d_fd = get_dev_crypto();
1044 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1045 put_dev_crypto(dstate->d_fd);
1047 printf("cryptodev_digest_copy: Open session failed\n");
1051 if (fstate->mac_len != 0) {
1052 if (fstate->mac_data != NULL) {
1053 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1054 if (dstate->mac_data == NULL) {
1055 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1058 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1059 dstate->mac_len = fstate->mac_len;
1066 static EVP_MD *sha1_md = NULL;
1067 static const EVP_MD *cryptodev_sha1(void)
1069 if (sha1_md == NULL) {
1072 if ((md = EVP_MD_meth_new(NID_sha1, NID_undef)) == NULL
1073 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1074 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1075 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1076 || !EVP_MD_meth_set_app_datasize(md,
1077 sizeof(struct dev_crypto_state))
1078 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1079 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1080 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1081 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1082 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1083 EVP_MD_meth_free(md);
1091 static EVP_MD *md5_md = NULL;
1092 static const EVP_MD *cryptodev_md5(void)
1094 if (md5_md == NULL) {
1097 if ((md = EVP_MD_meth_new(NID_md5, NID_undef)) == NULL
1098 || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */)
1099 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1100 || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */)
1101 || !EVP_MD_meth_set_app_datasize(md,
1102 sizeof(struct dev_crypto_state))
1103 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1104 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1105 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1106 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1107 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1108 EVP_MD_meth_free(md);
1116 # endif /* USE_CRYPTODEV_DIGESTS */
1119 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1120 const int **nids, int nid)
1123 return (cryptodev_usable_digests(nids));
1126 # ifdef USE_CRYPTODEV_DIGESTS
1128 *digest = cryptodev_md5();
1131 *digest = cryptodev_sha1();
1134 # endif /* USE_CRYPTODEV_DIGESTS */
1138 return (*digest != NULL);
1141 static int cryptodev_engine_destroy(ENGINE *e)
1143 EVP_CIPHER_meth_free(rc4_cipher);
1145 EVP_CIPHER_meth_free(des_cbc_cipher);
1146 des_cbc_cipher = NULL;
1147 EVP_CIPHER_meth_free(des3_cbc_cipher);
1148 des3_cbc_cipher = NULL;
1149 EVP_CIPHER_meth_free(bf_cbc_cipher);
1150 bf_cbc_cipher = NULL;
1151 EVP_CIPHER_meth_free(cast_cbc_cipher);
1152 cast_cbc_cipher = NULL;
1153 EVP_CIPHER_meth_free(aes_cbc_cipher);
1154 aes_cbc_cipher = NULL;
1155 EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1156 aes_192_cbc_cipher = NULL;
1157 EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1158 aes_256_cbc_cipher = NULL;
1159 # ifdef CRYPTO_AES_CTR
1160 EVP_CIPHER_meth_free(aes_ctr_cipher);
1161 aes_ctr_cipher = NULL;
1162 EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1163 aes_192_ctr_cipher = NULL;
1164 EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1165 aes_256_ctr_cipher = NULL;
1167 # ifdef USE_CRYPTODEV_DIGESTS
1168 EVP_MD_meth_free(sha1_md);
1170 EVP_MD_meth_free(md5_md);
1177 * Convert a BIGNUM to the representation that /dev/crypto needs.
1178 * Upon completion of use, the caller is responsible for freeing
1181 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1183 ssize_t bytes, bits;
1189 bits = BN_num_bits(a);
1190 bytes = BN_num_bytes(a);
1192 b = OPENSSL_zalloc(bytes);
1196 crp->crp_p = (caddr_t) b;
1197 crp->crp_nbits = bits;
1203 /* Convert a /dev/crypto parameter to a BIGNUM */
1204 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1209 bytes = (crp->crp_nbits + 7) / 8;
1214 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1217 for (i = 0; i < bytes; i++)
1218 pd[i] = crp->crp_p[bytes - i - 1];
1220 BN_bin2bn(pd, bytes, a);
1226 static void zapparams(struct crypt_kop *kop)
1230 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1231 if (kop->crk_param[i].crp_p)
1232 free(kop->crk_param[i].crp_p);
1233 kop->crk_param[i].crp_p = NULL;
1234 kop->crk_param[i].crp_nbits = 0;
1239 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1244 if ((fd = get_asym_dev_crypto()) < 0)
1248 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1249 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1253 kop->crk_param[kop->crk_iparams + 1].crp_p =
1254 calloc(slen, sizeof(char));
1255 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1259 if (ioctl(fd, CIOCKEY, kop) == 0) {
1261 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1263 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1271 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1272 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1274 struct crypt_kop kop;
1278 * Currently, we know we can do mod exp iff we can do any asymmetric
1279 * operations at all.
1281 if (cryptodev_asymfeat == 0) {
1282 ret = BN_mod_exp(r, a, p, m, ctx);
1286 memset(&kop, 0, sizeof(kop));
1287 kop.crk_op = CRK_MOD_EXP;
1289 /* inputs: a^p % m */
1290 if (bn2crparam(a, &kop.crk_param[0]))
1292 if (bn2crparam(p, &kop.crk_param[1]))
1294 if (bn2crparam(m, &kop.crk_param[2]))
1296 kop.crk_iparams = 3;
1298 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1299 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1300 printf("OCF asym process failed, Running in software\n");
1301 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1303 } else if (ECANCELED == kop.crk_status) {
1304 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1305 printf("OCF hardware operation cancelled. Running in Software\n");
1306 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1308 /* else cryptodev operation worked ok ==> ret = 1 */
1316 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1321 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1327 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1329 struct crypt_kop kop;
1332 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1333 /* XXX 0 means failure?? */
1337 memset(&kop, 0, sizeof(kop));
1338 kop.crk_op = CRK_MOD_EXP_CRT;
1339 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1340 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1342 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1344 if (bn2crparam(I, &kop.crk_param[2]))
1346 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1348 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1350 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1352 kop.crk_iparams = 6;
1354 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1355 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1356 printf("OCF asym process failed, running in Software\n");
1357 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1359 } else if (ECANCELED == kop.crk_status) {
1360 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1361 printf("OCF hardware operation cancelled. Running in Software\n");
1362 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
1364 /* else cryptodev operation worked ok ==> ret = 1 */
1371 static RSA_METHOD cryptodev_rsa = {
1372 "cryptodev RSA method",
1373 NULL, /* rsa_pub_enc */
1374 NULL, /* rsa_pub_dec */
1375 NULL, /* rsa_priv_enc */
1376 NULL, /* rsa_priv_dec */
1382 NULL, /* app_data */
1383 NULL, /* rsa_sign */
1384 NULL /* rsa_verify */
1388 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1389 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1391 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1395 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1396 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1397 BN_CTX *ctx, BN_MONT_CTX *mont)
1406 /* v = ( g^u1 * y^u2 mod p ) mod q */
1407 /* let t1 = g ^ u1 mod p */
1410 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
1413 /* let t2 = y ^ u2 mod p */
1414 if (!dsa->meth->bn_mod_exp(dsa, t2, dsa->pub_key, u2, dsa->p, ctx, mont))
1416 /* let u1 = t1 * t2 mod p */
1417 if (!BN_mod_mul(u1, t1, t2, dsa->p, ctx))
1428 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1431 struct crypt_kop kop;
1432 BIGNUM *r = NULL, *s = NULL;
1433 DSA_SIG *dsasig, *dsaret = NULL;
1435 dsasig = DSA_SIG_new();
1438 DSA_SIG_get0(&r, &s, dsasig);
1440 memset(&kop, 0, sizeof(kop));
1441 kop.crk_op = CRK_DSA_SIGN;
1443 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1444 kop.crk_param[0].crp_p = (caddr_t) dgst;
1445 kop.crk_param[0].crp_nbits = dlen * 8;
1446 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1448 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1450 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1452 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1454 kop.crk_iparams = 5;
1456 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1457 BN_num_bytes(dsa->q), s) == 0) {
1460 const DSA_METHOD *meth = DSA_OpenSSL();
1461 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
1464 if (dsaret != dsasig)
1465 DSA_SIG_free(dsasig);
1466 kop.crk_param[0].crp_p = NULL;
1472 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1473 DSA_SIG *sig, DSA *dsa)
1475 struct crypt_kop kop;
1479 memset(&kop, 0, sizeof(kop));
1480 kop.crk_op = CRK_DSA_VERIFY;
1482 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1483 kop.crk_param[0].crp_p = (caddr_t) dgst;
1484 kop.crk_param[0].crp_nbits = dlen * 8;
1485 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1487 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1489 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1491 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1493 DSA_SIG_get0(&pr, &ps, sig);
1494 if (bn2crparam(pr, &kop.crk_param[5]))
1496 if (bn2crparam(ps, &kop.crk_param[6]))
1498 kop.crk_iparams = 7;
1500 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1502 * OCF success value is 0, if not zero, change dsaret to fail
1504 if (0 != kop.crk_status)
1507 const DSA_METHOD *meth = DSA_OpenSSL();
1509 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
1512 kop.crk_param[0].crp_p = NULL;
1517 static DSA_METHOD cryptodev_dsa = {
1518 "cryptodev DSA method",
1520 NULL, /* dsa_sign_setup */
1522 NULL, /* dsa_mod_exp */
1530 #ifndef OPENSSL_NO_DH
1532 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1533 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1536 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1540 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1542 struct crypt_kop kop;
1546 if ((fd = get_asym_dev_crypto()) < 0) {
1547 const DH_METHOD *meth = DH_OpenSSL();
1549 return ((meth->compute_key) (key, pub_key, dh));
1552 keylen = BN_num_bits(dh->p);
1554 memset(&kop, 0, sizeof(kop));
1555 kop.crk_op = CRK_DH_COMPUTE_KEY;
1557 /* inputs: dh->priv_key pub_key dh->p key */
1558 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1560 if (bn2crparam(pub_key, &kop.crk_param[1]))
1562 if (bn2crparam(dh->p, &kop.crk_param[2]))
1564 kop.crk_iparams = 3;
1566 kop.crk_param[3].crp_p = (caddr_t) key;
1567 kop.crk_param[3].crp_nbits = keylen * 8;
1568 kop.crk_oparams = 1;
1570 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1571 const DH_METHOD *meth = DH_OpenSSL();
1573 dhret = (meth->compute_key) (key, pub_key, dh);
1576 kop.crk_param[3].crp_p = NULL;
1581 static DH_METHOD cryptodev_dh = {
1582 "cryptodev DH method",
1583 NULL, /* cryptodev_dh_generate_key */
1592 #endif /* ndef OPENSSL_NO_DH */
1595 * ctrl right now is just a wrapper that doesn't do much
1596 * but I expect we'll want some options soon.
1599 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1601 # ifdef HAVE_SYSLOG_R
1602 struct syslog_data sd = SYSLOG_DATA_INIT;
1607 # ifdef HAVE_SYSLOG_R
1608 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1610 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1617 void engine_load_cryptodev_internal(void)
1619 ENGINE *engine = ENGINE_new();
1624 if ((fd = get_dev_crypto()) < 0) {
1625 ENGINE_free(engine);
1630 * find out what asymmetric crypto algorithms we support
1632 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1634 ENGINE_free(engine);
1639 if (!ENGINE_set_id(engine, "cryptodev") ||
1640 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1641 !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1642 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1643 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1644 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1645 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1646 ENGINE_free(engine);
1650 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1651 const RSA_METHOD *rsa_meth = RSA_PKCS1_OpenSSL();
1653 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1654 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1655 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1656 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1657 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1658 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1659 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1660 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1661 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1662 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
1664 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
1668 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1669 const DSA_METHOD *meth = DSA_OpenSSL();
1671 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1672 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1673 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1674 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1675 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1676 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1678 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1679 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1682 #ifndef OPENSSL_NO_DH
1683 if (ENGINE_set_DH(engine, &cryptodev_dh)) {
1684 const DH_METHOD *dh_meth = DH_OpenSSL();
1686 cryptodev_dh.generate_key = dh_meth->generate_key;
1687 cryptodev_dh.compute_key = dh_meth->compute_key;
1688 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1689 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1690 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1691 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1692 cryptodev_dh.compute_key = cryptodev_dh_compute_key;
1698 ENGINE_free(engine);
1702 #endif /* HAVE_CRYPTODEV */