2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
12 * Copyright (c) 2002 Theo de Raadt
13 * Copyright (c) 2002 Markus Friedl
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
26 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <openssl/objects.h>
39 #include <internal/engine.h>
40 #include <openssl/evp.h>
41 #include <openssl/bn.h>
42 #include <openssl/crypto.h>
44 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
45 (defined(OpenBSD) || defined(__FreeBSD__))
46 # include <sys/param.h>
47 # if (defined(OpenBSD) && (OpenBSD >= 200112)) || \
48 (defined(__FreeBSD_version) && \
49 ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || \
50 __FreeBSD_version >= 500041))
51 # define HAVE_CRYPTODEV
53 # if defined(OpenBSD) && (OpenBSD >= 200110)
54 # define HAVE_SYSLOG_R
58 #include <sys/types.h>
60 # include <crypto/cryptodev.h>
61 # include <sys/ioctl.h>
71 #include <openssl/dh.h>
72 #include <openssl/dsa.h>
73 #include <openssl/err.h>
74 #include <openssl/rsa.h>
76 #ifndef HAVE_CRYPTODEV
78 void engine_load_cryptodev_int(void)
80 /* This is a NOP on platforms without /dev/crypto */
86 struct dev_crypto_state {
87 struct session_op d_sess;
89 # ifdef USE_CRYPTODEV_DIGESTS
90 char dummy_mac_key[HASH_MAX_LEN];
91 unsigned char digest_res[HASH_MAX_LEN];
97 static u_int32_t cryptodev_asymfeat = 0;
99 static RSA_METHOD *cryptodev_rsa;
100 #ifndef OPENSSL_NO_DSA
101 static DSA_METHOD *cryptodev_dsa = NULL;
103 #ifndef OPENSSL_NO_DH
104 static DH_METHOD *cryptodev_dh;
107 static int get_asym_dev_crypto(void);
108 static int open_dev_crypto(void);
109 static int get_dev_crypto(void);
110 static int get_cryptodev_ciphers(const int **cnids);
111 # ifdef USE_CRYPTODEV_DIGESTS
112 static int get_cryptodev_digests(const int **cnids);
114 static int cryptodev_usable_ciphers(const int **nids);
115 static int cryptodev_usable_digests(const int **nids);
116 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
117 const unsigned char *in, size_t inl);
118 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
119 const unsigned char *iv, int enc);
120 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
121 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
122 const int **nids, int nid);
123 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
124 const int **nids, int nid);
125 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
126 static int crparam2bn(struct crparam *crp, BIGNUM *a);
127 static void zapparams(struct crypt_kop *kop);
128 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
129 int slen, BIGNUM *s);
131 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
132 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
134 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
136 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
138 #ifndef OPENSSL_NO_DSA
139 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a,
140 const BIGNUM *p, const BIGNUM *m,
141 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
142 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
143 const BIGNUM *u1, const BIGNUM *pub_key,
144 const BIGNUM *u2, const BIGNUM *p,
145 BN_CTX *ctx, BN_MONT_CTX *mont);
146 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
148 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
149 DSA_SIG *sig, DSA *dsa);
151 #ifndef OPENSSL_NO_DH
152 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
153 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
155 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
158 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
160 void engine_load_cryptodev_int(void);
162 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
173 CRYPTO_ARC4, NID_rc4, 0, 16,
176 CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
179 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
182 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
185 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
188 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
190 # ifdef CRYPTO_AES_CTR
192 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
195 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
198 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
202 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
205 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
208 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
215 # ifdef USE_CRYPTODEV_DIGESTS
222 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
225 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
228 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
232 CRYPTO_MD5_KPDK, NID_undef, 0
235 CRYPTO_SHA1_KPDK, NID_undef, 0
238 CRYPTO_MD5, NID_md5, 16
241 CRYPTO_SHA1, NID_sha1, 20
250 * Return a fd if /dev/crypto seems usable, 0 otherwise.
252 static int open_dev_crypto(void)
257 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
260 if (fcntl(fd, F_SETFD, 1) == -1) {
269 static int get_dev_crypto(void)
273 if ((fd = open_dev_crypto()) == -1)
275 # ifndef CRIOGET_NOT_NEEDED
276 if (ioctl(fd, CRIOGET, &retfd) == -1)
280 if (fcntl(retfd, F_SETFD, 1) == -1) {
290 static void put_dev_crypto(int fd)
292 # ifndef CRIOGET_NOT_NEEDED
297 /* Caching version for asym operations */
298 static int get_asym_dev_crypto(void)
303 fd = get_dev_crypto();
308 * Find out what ciphers /dev/crypto will let us have a session for.
309 * XXX note, that some of these openssl doesn't deal with yet!
310 * returning them here is harmless, as long as we return NULL
311 * when asked for a handler in the cryptodev_engine_ciphers routine
313 static int get_cryptodev_ciphers(const int **cnids)
315 static int nids[CRYPTO_ALGORITHM_MAX];
316 struct session_op sess;
317 int fd, i, count = 0;
319 if ((fd = get_dev_crypto()) < 0) {
323 memset(&sess, 0, sizeof(sess));
324 sess.key = (caddr_t) "123456789abcdefghijklmno";
326 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
327 if (ciphers[i].nid == NID_undef)
329 sess.cipher = ciphers[i].id;
330 sess.keylen = ciphers[i].keylen;
332 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
333 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
334 nids[count++] = ciphers[i].nid;
345 # ifdef USE_CRYPTODEV_DIGESTS
347 * Find out what digests /dev/crypto will let us have a session for.
348 * XXX note, that some of these openssl doesn't deal with yet!
349 * returning them here is harmless, as long as we return NULL
350 * when asked for a handler in the cryptodev_engine_digests routine
352 static int get_cryptodev_digests(const int **cnids)
354 static int nids[CRYPTO_ALGORITHM_MAX];
355 struct session_op sess;
356 int fd, i, count = 0;
358 if ((fd = get_dev_crypto()) < 0) {
362 memset(&sess, 0, sizeof(sess));
363 sess.mackey = (caddr_t) "123456789abcdefghijklmno";
364 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
365 if (digests[i].nid == NID_undef)
367 sess.mac = digests[i].id;
368 sess.mackeylen = digests[i].keylen;
370 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
371 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
372 nids[count++] = digests[i].nid;
385 * Find the useable ciphers|digests from dev/crypto - this is the first
386 * thing called by the engine init crud which determines what it
387 * can use for ciphers from this engine. We want to return
388 * only what we can do, anything else is handled by software.
390 * If we can't initialize the device to do anything useful for
391 * any reason, we want to return a NULL array, and 0 length,
392 * which forces everything to be done is software. By putting
393 * the initialization of the device in here, we ensure we can
394 * use this engine as the default, and if for whatever reason
395 * /dev/crypto won't do what we want it will just be done in
398 * This can (should) be greatly expanded to perhaps take into
399 * account speed of the device, and what we want to do.
400 * (although the disabling of particular alg's could be controlled
401 * by the device driver with sysctl's.) - this is where we
402 * want most of the decisions made about what we actually want
403 * to use from /dev/crypto.
405 static int cryptodev_usable_ciphers(const int **nids)
407 return (get_cryptodev_ciphers(nids));
410 static int cryptodev_usable_digests(const int **nids)
412 # ifdef USE_CRYPTODEV_DIGESTS
413 return (get_cryptodev_digests(nids));
416 * XXXX just disable all digests for now, because it sucks.
417 * we need a better way to decide this - i.e. I may not
418 * want digests on slow cards like hifn on fast machines,
419 * but might want them on slow or loaded machines, etc.
420 * will also want them when using crypto cards that don't
421 * suck moose gonads - would be nice to be able to decide something
422 * as reasonable default without having hackery that's card dependent.
423 * of course, the default should probably be just do everything,
424 * with perhaps a sysctl to turn algorithms off (or have them off
425 * by default) on cards that generally suck like the hifn.
433 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
434 const unsigned char *in, size_t inl)
436 struct crypt_op cryp;
437 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
438 struct session_op *sess = &state->d_sess;
440 unsigned char save_iv[EVP_MAX_IV_LENGTH];
446 if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
449 memset(&cryp, 0, sizeof(cryp));
451 cryp.ses = sess->ses;
454 cryp.src = (caddr_t) in;
455 cryp.dst = (caddr_t) out;
458 cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
460 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
461 cryp.iv = (caddr_t) EVP_CIPHER_CTX_iv(ctx);
462 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
463 iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
464 memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
469 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
471 * XXX need better error handling this can fail for a number of
477 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
478 if (EVP_CIPHER_CTX_encrypting(ctx))
479 iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
482 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
483 EVP_CIPHER_CTX_iv_length(ctx));
489 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
490 const unsigned char *iv, int enc)
492 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
493 struct session_op *sess = &state->d_sess;
496 for (i = 0; ciphers[i].id; i++)
497 if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
498 EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
499 EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
500 cipher = ciphers[i].id;
504 if (!ciphers[i].id) {
509 memset(sess, 0, sizeof(*sess));
511 if ((state->d_fd = get_dev_crypto()) < 0)
514 sess->key = (caddr_t) key;
515 sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
516 sess->cipher = cipher;
518 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
519 put_dev_crypto(state->d_fd);
527 * free anything we allocated earlier when initing a
528 * session, and close the session.
530 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
533 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
534 struct session_op *sess = &state->d_sess;
540 * XXX if this ioctl fails, something's wrong. the invoker may have called
541 * us with a bogus ctx, or we could have a device that for whatever
542 * reason just doesn't want to play ball - it's not clear what's right
543 * here - should this be an error? should it just increase a counter,
544 * hmm. For right now, we return 0 - I don't believe that to be "right".
545 * we could call the gorpy openssl lib error handlers that print messages
546 * to users of the library. hmm..
549 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
554 put_dev_crypto(state->d_fd);
561 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
562 * gets called when libcrypto requests a cipher NID.
566 static EVP_CIPHER *rc4_cipher = NULL;
567 static const EVP_CIPHER *cryptodev_rc4(void)
569 if (rc4_cipher == NULL) {
572 if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16)) == NULL
573 || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
574 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
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_free(cipher);
588 static EVP_CIPHER *des_cbc_cipher = NULL;
589 static const EVP_CIPHER *cryptodev_des_cbc(void)
591 if (des_cbc_cipher == NULL) {
594 if ((cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8)) == NULL
595 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
596 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
597 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
598 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
599 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
600 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
601 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
602 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
603 EVP_CIPHER_meth_free(cipher);
606 des_cbc_cipher = cipher;
608 return des_cbc_cipher;
612 static EVP_CIPHER *des3_cbc_cipher = NULL;
613 static const EVP_CIPHER *cryptodev_3des_cbc(void)
615 if (des3_cbc_cipher == NULL) {
618 if ((cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24)) == NULL
619 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
620 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
621 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
622 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
623 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
624 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
625 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
626 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
627 EVP_CIPHER_meth_free(cipher);
630 des3_cbc_cipher = cipher;
632 return des3_cbc_cipher;
635 static EVP_CIPHER *bf_cbc_cipher = NULL;
636 static const EVP_CIPHER *cryptodev_bf_cbc(void)
638 if (bf_cbc_cipher == NULL) {
641 if ((cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16)) == NULL
642 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
643 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
644 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
645 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
646 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
647 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
648 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
649 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
650 EVP_CIPHER_meth_free(cipher);
653 bf_cbc_cipher = cipher;
655 return bf_cbc_cipher;
658 static EVP_CIPHER *cast_cbc_cipher = NULL;
659 static const EVP_CIPHER *cryptodev_cast_cbc(void)
661 if (cast_cbc_cipher == NULL) {
664 if ((cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16)) == NULL
665 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
666 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
667 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
668 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
669 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
670 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
671 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
672 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
673 EVP_CIPHER_meth_free(cipher);
676 cast_cbc_cipher = cipher;
678 return cast_cbc_cipher;
681 static EVP_CIPHER *aes_cbc_cipher = NULL;
682 static const EVP_CIPHER *cryptodev_aes_cbc(void)
684 if (aes_cbc_cipher == NULL) {
687 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16)) == NULL
688 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
689 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
690 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
691 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
692 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
693 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
694 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
695 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
696 EVP_CIPHER_meth_free(cipher);
699 aes_cbc_cipher = cipher;
701 return aes_cbc_cipher;
704 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
705 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
707 if (aes_192_cbc_cipher == NULL) {
710 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24)) == NULL
711 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
712 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
713 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
714 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
715 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
716 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
717 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
718 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
719 EVP_CIPHER_meth_free(cipher);
722 aes_192_cbc_cipher = cipher;
724 return aes_192_cbc_cipher;
727 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
728 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
730 if (aes_256_cbc_cipher == NULL) {
733 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32)) == NULL
734 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
735 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE)
736 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
737 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
738 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
739 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
740 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
741 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
742 EVP_CIPHER_meth_free(cipher);
745 aes_256_cbc_cipher = cipher;
747 return aes_256_cbc_cipher;
750 # ifdef CRYPTO_AES_CTR
751 static EVP_CIPHER *aes_ctr_cipher = NULL;
752 static const EVP_CIPHER *cryptodev_aes_ctr(void)
754 if (aes_ctr_cipher == NULL) {
757 if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16)) == NULL
758 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
759 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
760 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
761 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
762 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
763 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
764 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
765 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
766 EVP_CIPHER_meth_free(cipher);
769 aes_ctr_cipher = cipher;
771 return aes_ctr_cipher;
774 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
775 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
777 if (aes_192_ctr_cipher == NULL) {
780 if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24)) == NULL
781 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
782 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
783 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
784 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
785 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
786 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
787 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
788 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
789 EVP_CIPHER_meth_free(cipher);
792 aes_192_ctr_cipher = cipher;
794 return aes_192_ctr_cipher;
797 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
798 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
800 if (aes_256_ctr_cipher == NULL) {
803 if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32)) == NULL
804 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
805 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE)
806 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
807 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
808 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
809 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
810 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
811 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
812 EVP_CIPHER_meth_free(cipher);
815 aes_256_ctr_cipher = cipher;
817 return aes_256_ctr_cipher;
821 * Registered by the ENGINE when used to find out how to deal with
822 * a particular NID in the ENGINE. this says what we'll do at the
823 * top level - note, that list is restricted by what we answer with
826 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
827 const int **nids, int nid)
830 return (cryptodev_usable_ciphers(nids));
834 *cipher = cryptodev_rc4();
836 case NID_des_ede3_cbc:
837 *cipher = cryptodev_3des_cbc();
840 *cipher = cryptodev_des_cbc();
843 *cipher = cryptodev_bf_cbc();
846 *cipher = cryptodev_cast_cbc();
848 case NID_aes_128_cbc:
849 *cipher = cryptodev_aes_cbc();
851 case NID_aes_192_cbc:
852 *cipher = cryptodev_aes_192_cbc();
854 case NID_aes_256_cbc:
855 *cipher = cryptodev_aes_256_cbc();
857 # ifdef CRYPTO_AES_CTR
858 case NID_aes_128_ctr:
859 *cipher = cryptodev_aes_ctr();
861 case NID_aes_192_ctr:
862 *cipher = cryptodev_aes_192_ctr();
864 case NID_aes_256_ctr:
865 *cipher = cryptodev_aes_256_ctr();
872 return (*cipher != NULL);
875 # ifdef USE_CRYPTODEV_DIGESTS
877 /* convert digest type to cryptodev */
878 static int digest_nid_to_cryptodev(int nid)
882 for (i = 0; digests[i].id; i++)
883 if (digests[i].nid == nid)
884 return (digests[i].id);
888 static int digest_key_length(int nid)
892 for (i = 0; digests[i].id; i++)
893 if (digests[i].nid == nid)
894 return digests[i].keylen;
898 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
900 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
901 struct session_op *sess = &state->d_sess;
904 if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
905 printf("cryptodev_digest_init: Can't get digest \n");
909 memset(state, 0, sizeof(*state));
911 if ((state->d_fd = get_dev_crypto()) < 0) {
912 printf("cryptodev_digest_init: Can't get Dev \n");
916 sess->mackey = state->dummy_mac_key;
917 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(ctx));
920 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
921 put_dev_crypto(state->d_fd);
923 printf("cryptodev_digest_init: Open session failed\n");
930 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
933 struct crypt_op cryp;
934 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
935 struct session_op *sess = &state->d_sess;
938 if (!data || state->d_fd < 0) {
939 printf("cryptodev_digest_update: illegal inputs \n");
947 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
948 /* if application doesn't support one buffer */
950 OPENSSL_realloc(state->mac_data, state->mac_len + count);
953 printf("cryptodev_digest_update: realloc failed\n");
956 state->mac_data = new_mac_data;
958 memcpy(state->mac_data + state->mac_len, data, count);
959 state->mac_len += count;
964 memset(&cryp, 0, sizeof(cryp));
966 cryp.ses = sess->ses;
969 cryp.src = (caddr_t) data;
971 cryp.mac = (caddr_t) state->digest_res;
972 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
973 printf("cryptodev_digest_update: digest failed\n");
979 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
981 struct crypt_op cryp;
982 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
983 struct session_op *sess = &state->d_sess;
987 if (!md || state->d_fd < 0) {
988 printf("cryptodev_digest_final: illegal input\n");
992 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
993 /* if application doesn't support one buffer */
994 memset(&cryp, 0, sizeof(cryp));
995 cryp.ses = sess->ses;
997 cryp.len = state->mac_len;
998 cryp.src = state->mac_data;
1000 cryp.mac = (caddr_t) md;
1001 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1002 printf("cryptodev_digest_final: digest failed\n");
1009 memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
1014 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
1017 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1018 struct session_op *sess = &state->d_sess;
1023 if (state->d_fd < 0) {
1024 printf("cryptodev_digest_cleanup: illegal input\n");
1028 OPENSSL_free(state->mac_data);
1029 state->mac_data = NULL;
1032 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1033 printf("cryptodev_digest_cleanup: failed to close session\n");
1038 put_dev_crypto(state->d_fd);
1044 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1046 struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1047 struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1048 struct session_op *sess;
1051 if (dstate == NULL || fstate == NULL)
1054 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1056 sess = &dstate->d_sess;
1058 digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1060 sess->mackey = dstate->dummy_mac_key;
1061 sess->mackeylen = digest_key_length(EVP_MD_CTX_type(to));
1064 dstate->d_fd = get_dev_crypto();
1066 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1067 put_dev_crypto(dstate->d_fd);
1069 printf("cryptodev_digest_copy: Open session failed\n");
1073 if (fstate->mac_len != 0) {
1074 if (fstate->mac_data != NULL) {
1075 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1076 if (dstate->mac_data == NULL) {
1077 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1080 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1081 dstate->mac_len = fstate->mac_len;
1088 static EVP_MD *sha1_md = NULL;
1089 static const EVP_MD *cryptodev_sha1(void)
1091 if (sha1_md == NULL) {
1094 if ((md = EVP_MD_meth_new(NID_sha1, NID_undef)) == NULL
1095 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1096 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1097 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1098 || !EVP_MD_meth_set_app_datasize(md,
1099 sizeof(struct dev_crypto_state))
1100 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1101 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1102 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1103 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1104 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1105 EVP_MD_meth_free(md);
1113 static EVP_MD *md5_md = NULL;
1114 static const EVP_MD *cryptodev_md5(void)
1116 if (md5_md == NULL) {
1119 if ((md = EVP_MD_meth_new(NID_md5, NID_undef)) == NULL
1120 || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */)
1121 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1122 || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */)
1123 || !EVP_MD_meth_set_app_datasize(md,
1124 sizeof(struct dev_crypto_state))
1125 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1126 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1127 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1128 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1129 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1130 EVP_MD_meth_free(md);
1138 # endif /* USE_CRYPTODEV_DIGESTS */
1141 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1142 const int **nids, int nid)
1145 return (cryptodev_usable_digests(nids));
1148 # ifdef USE_CRYPTODEV_DIGESTS
1150 *digest = cryptodev_md5();
1153 *digest = cryptodev_sha1();
1156 # endif /* USE_CRYPTODEV_DIGESTS */
1160 return (*digest != NULL);
1163 static int cryptodev_engine_destroy(ENGINE *e)
1165 EVP_CIPHER_meth_free(rc4_cipher);
1167 EVP_CIPHER_meth_free(des_cbc_cipher);
1168 des_cbc_cipher = NULL;
1169 EVP_CIPHER_meth_free(des3_cbc_cipher);
1170 des3_cbc_cipher = NULL;
1171 EVP_CIPHER_meth_free(bf_cbc_cipher);
1172 bf_cbc_cipher = NULL;
1173 EVP_CIPHER_meth_free(cast_cbc_cipher);
1174 cast_cbc_cipher = NULL;
1175 EVP_CIPHER_meth_free(aes_cbc_cipher);
1176 aes_cbc_cipher = NULL;
1177 EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1178 aes_192_cbc_cipher = NULL;
1179 EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1180 aes_256_cbc_cipher = NULL;
1181 # ifdef CRYPTO_AES_CTR
1182 EVP_CIPHER_meth_free(aes_ctr_cipher);
1183 aes_ctr_cipher = NULL;
1184 EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1185 aes_192_ctr_cipher = NULL;
1186 EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1187 aes_256_ctr_cipher = NULL;
1189 # ifdef USE_CRYPTODEV_DIGESTS
1190 EVP_MD_meth_free(sha1_md);
1192 EVP_MD_meth_free(md5_md);
1195 RSA_meth_free(cryptodev_rsa);
1196 cryptodev_rsa = NULL;
1197 #ifndef OPENSSL_NO_DSA
1198 DSA_meth_free(cryptodev_dsa);
1199 cryptodev_dsa = NULL;
1201 #ifndef OPENSSL_NO_DH
1202 DH_meth_free(cryptodev_dh);
1203 cryptodev_dh = NULL;
1209 * Convert a BIGNUM to the representation that /dev/crypto needs.
1210 * Upon completion of use, the caller is responsible for freeing
1213 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1215 ssize_t bytes, bits;
1221 bits = BN_num_bits(a);
1222 bytes = BN_num_bytes(a);
1224 b = OPENSSL_zalloc(bytes);
1228 crp->crp_p = (caddr_t) b;
1229 crp->crp_nbits = bits;
1235 /* Convert a /dev/crypto parameter to a BIGNUM */
1236 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1241 bytes = (crp->crp_nbits + 7) / 8;
1246 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1249 for (i = 0; i < bytes; i++)
1250 pd[i] = crp->crp_p[bytes - i - 1];
1252 BN_bin2bn(pd, bytes, a);
1258 static void zapparams(struct crypt_kop *kop)
1262 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1263 OPENSSL_free(kop->crk_param[i].crp_p);
1264 kop->crk_param[i].crp_p = NULL;
1265 kop->crk_param[i].crp_nbits = 0;
1270 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1275 if ((fd = get_asym_dev_crypto()) < 0)
1279 kop->crk_param[kop->crk_iparams].crp_p = OPENSSL_zalloc(rlen);
1280 if (kop->crk_param[kop->crk_iparams].crp_p == NULL)
1282 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1286 kop->crk_param[kop->crk_iparams + 1].crp_p =
1287 OPENSSL_zalloc(slen);
1288 /* No need to free the kop->crk_iparams parameter if it was allocated,
1289 * callers of this routine have to free allocated parameters through
1290 * zapparams both in case of success and failure
1292 if (kop->crk_param[kop->crk_iparams+1].crp_p == NULL)
1294 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1298 if (ioctl(fd, CIOCKEY, kop) == 0) {
1300 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1302 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1310 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1311 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1313 struct crypt_kop kop;
1317 * Currently, we know we can do mod exp iff we can do any asymmetric
1318 * operations at all.
1320 if (cryptodev_asymfeat == 0) {
1321 ret = BN_mod_exp(r, a, p, m, ctx);
1325 memset(&kop, 0, sizeof(kop));
1326 kop.crk_op = CRK_MOD_EXP;
1328 /* inputs: a^p % m */
1329 if (bn2crparam(a, &kop.crk_param[0]))
1331 if (bn2crparam(p, &kop.crk_param[1]))
1333 if (bn2crparam(m, &kop.crk_param[2]))
1335 kop.crk_iparams = 3;
1337 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1338 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1339 printf("OCF asym process failed, Running in software\n");
1340 ret = RSA_meth_get_bn_mod_exp(meth)(r, a, p, m, ctx, in_mont);
1342 } else if (ECANCELED == kop.crk_status) {
1343 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1344 printf("OCF hardware operation cancelled. Running in Software\n");
1345 ret = RSA_meth_get_bn_mod_exp(meth)(r, a, p, m, ctx, in_mont);
1347 /* else cryptodev operation worked ok ==> ret = 1 */
1355 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1359 const BIGNUM *n = NULL;
1360 const BIGNUM *d = NULL;
1363 RSA_get0_key(rsa, &n, NULL, &d);
1364 r = cryptodev_bn_mod_exp(r0, I, d, n, ctx, NULL);
1370 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1372 struct crypt_kop kop;
1374 const BIGNUM *p = NULL;
1375 const BIGNUM *q = NULL;
1376 const BIGNUM *dmp1 = NULL;
1377 const BIGNUM *dmq1 = NULL;
1378 const BIGNUM *iqmp = NULL;
1379 const BIGNUM *n = NULL;
1381 RSA_get0_factors(rsa, &p, &q);
1382 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
1383 RSA_get0_key(rsa, &n, NULL, NULL);
1385 if (!p || !q || !dmp1 || !dmq1 || !iqmp) {
1386 /* XXX 0 means failure?? */
1390 memset(&kop, 0, sizeof(kop));
1391 kop.crk_op = CRK_MOD_EXP_CRT;
1392 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1393 if (bn2crparam(p, &kop.crk_param[0]))
1395 if (bn2crparam(q, &kop.crk_param[1]))
1397 if (bn2crparam(I, &kop.crk_param[2]))
1399 if (bn2crparam(dmp1, &kop.crk_param[3]))
1401 if (bn2crparam(dmq1, &kop.crk_param[4]))
1403 if (bn2crparam(iqmp, &kop.crk_param[5]))
1405 kop.crk_iparams = 6;
1407 if (cryptodev_asym(&kop, BN_num_bytes(n), r0, 0, NULL)) {
1408 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1409 printf("OCF asym process failed, running in Software\n");
1410 ret = RSA_meth_get_mod_exp(meth)(r0, I, rsa, ctx);
1412 } else if (ECANCELED == kop.crk_status) {
1413 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1414 printf("OCF hardware operation cancelled. Running in Software\n");
1415 ret = RSA_meth_get_mod_exp(meth)(r0, I, rsa, ctx);
1417 /* else cryptodev operation worked ok ==> ret = 1 */
1424 #ifndef OPENSSL_NO_DSA
1426 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1427 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1429 return cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx);
1433 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
1434 const BIGNUM *u1, const BIGNUM *pub_key,
1435 const BIGNUM *u2, const BIGNUM *p, BN_CTX *ctx,
1438 const BIGNUM *dsag, *dsap, *dsapub_key;
1441 const DSA_METHOD *meth;
1442 int (*bn_mod_exp)(DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
1443 BN_CTX *, BN_MONT_CTX *);
1449 /* v = ( g^u1 * y^u2 mod p ) mod q */
1450 /* let t1 = g ^ u1 mod p */
1453 DSA_get0_pqg(dsa, &dsap, NULL, &dsag);
1454 DSA_get0_key(dsa, &dsapub_key, NULL);
1456 meth = DSA_get_method(dsa);
1459 bn_mod_exp = DSA_meth_get_bn_mod_exp(meth);
1460 if (bn_mod_exp == NULL)
1463 if (!bn_mod_exp(dsa, t1, dsag, u1, dsap, ctx, mont))
1466 /* let t2 = y ^ u2 mod p */
1467 if (!bn_mod_exp(dsa, t2, dsapub_key, u2, dsap, ctx, mont))
1469 /* let t1 = t1 * t2 mod p */
1470 if (!BN_mod_mul(t1, t1, t2, dsap, ctx))
1479 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1482 struct crypt_kop kop;
1484 const BIGNUM *dsap = NULL, *dsaq = NULL, *dsag = NULL;
1485 const BIGNUM *priv_key = NULL;
1486 DSA_SIG *dsasig, *dsaret = NULL;
1488 dsasig = DSA_SIG_new();
1492 memset(&kop, 0, sizeof(kop));
1493 kop.crk_op = CRK_DSA_SIGN;
1495 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1496 kop.crk_param[0].crp_p = (caddr_t) dgst;
1497 kop.crk_param[0].crp_nbits = dlen * 8;
1498 DSA_get0_pqg(dsa, &dsap, &dsaq, &dsag);
1499 DSA_get0_key(dsa, NULL, &priv_key);
1500 if (bn2crparam(dsap, &kop.crk_param[1]))
1502 if (bn2crparam(dsaq, &kop.crk_param[2]))
1504 if (bn2crparam(dsag, &kop.crk_param[3]))
1506 if (bn2crparam(priv_key, &kop.crk_param[4]))
1508 kop.crk_iparams = 5;
1516 if (cryptodev_asym(&kop, BN_num_bytes(dsaq), r,
1517 BN_num_bytes(dsaq), s) == 0) {
1518 DSA_SIG_set0(dsasig, r, s);
1521 dsaret = DSA_meth_get_sign(DSA_OpenSSL())(dgst, dlen, dsa);
1524 if (dsaret != dsasig)
1525 DSA_SIG_free(dsasig);
1526 kop.crk_param[0].crp_p = NULL;
1532 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1533 DSA_SIG *sig, DSA *dsa)
1535 struct crypt_kop kop;
1537 const BIGNUM *pr, *ps, *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL;
1539 memset(&kop, 0, sizeof(kop));
1540 kop.crk_op = CRK_DSA_VERIFY;
1542 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1543 kop.crk_param[0].crp_p = (caddr_t) dgst;
1544 kop.crk_param[0].crp_nbits = dlen * 8;
1545 DSA_get0_pqg(dsa, &p, &q, &g);
1546 if (bn2crparam(p, &kop.crk_param[1]))
1548 if (bn2crparam(q, &kop.crk_param[2]))
1550 if (bn2crparam(g, &kop.crk_param[3]))
1552 DSA_get0_key(dsa, &pub_key, NULL);
1553 if (bn2crparam(pub_key, &kop.crk_param[4]))
1555 DSA_SIG_get0(sig, &pr, &ps);
1556 if (bn2crparam(pr, &kop.crk_param[5]))
1558 if (bn2crparam(ps, &kop.crk_param[6]))
1560 kop.crk_iparams = 7;
1562 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1564 * OCF success value is 0, if not zero, change dsaret to fail
1566 if (0 != kop.crk_status)
1569 dsaret = DSA_meth_get_verify(DSA_OpenSSL())(dgst, dlen, sig, dsa);
1572 kop.crk_param[0].crp_p = NULL;
1578 #ifndef OPENSSL_NO_DH
1580 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1581 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1584 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1588 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1590 struct crypt_kop kop;
1593 const BIGNUM *p = NULL;
1594 const BIGNUM *priv_key = NULL;
1596 if ((fd = get_asym_dev_crypto()) < 0) {
1597 const DH_METHOD *meth = DH_OpenSSL();
1599 return DH_meth_get_compute_key(meth)(key, pub_key, dh);
1602 DH_get0_pqg(dh, &p, NULL, NULL);
1603 DH_get0_key(dh, NULL, &priv_key);
1605 keylen = BN_num_bits(p);
1607 memset(&kop, 0, sizeof(kop));
1608 kop.crk_op = CRK_DH_COMPUTE_KEY;
1610 /* inputs: dh->priv_key pub_key dh->p key */
1611 if (bn2crparam(priv_key, &kop.crk_param[0]))
1613 if (bn2crparam(pub_key, &kop.crk_param[1]))
1615 if (bn2crparam(p, &kop.crk_param[2]))
1617 kop.crk_iparams = 3;
1619 kop.crk_param[3].crp_p = (caddr_t) key;
1620 kop.crk_param[3].crp_nbits = keylen * 8;
1621 kop.crk_oparams = 1;
1623 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1624 const DH_METHOD *meth = DH_OpenSSL();
1626 dhret = DH_meth_get_compute_key(meth)(key, pub_key, dh);
1629 kop.crk_param[3].crp_p = NULL;
1634 #endif /* ndef OPENSSL_NO_DH */
1637 * ctrl right now is just a wrapper that doesn't do much
1638 * but I expect we'll want some options soon.
1641 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1643 # ifdef HAVE_SYSLOG_R
1644 struct syslog_data sd = SYSLOG_DATA_INIT;
1649 # ifdef HAVE_SYSLOG_R
1650 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1652 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1659 void engine_load_cryptodev_int(void)
1661 ENGINE *engine = ENGINE_new();
1666 if ((fd = get_dev_crypto()) < 0) {
1667 ENGINE_free(engine);
1672 * find out what asymmetric crypto algorithms we support
1674 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1676 ENGINE_free(engine);
1681 if (!ENGINE_set_id(engine, "cryptodev") ||
1682 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1683 !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1684 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1685 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1686 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1687 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1688 ENGINE_free(engine);
1692 cryptodev_rsa = RSA_meth_dup(RSA_PKCS1_OpenSSL());
1693 if (cryptodev_rsa != NULL) {
1694 RSA_meth_set1_name(cryptodev_rsa, "cryptodev RSA method");
1695 RSA_meth_set_flags(cryptodev_rsa, 0);
1696 if (ENGINE_set_RSA(engine, cryptodev_rsa)) {
1697 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1698 RSA_meth_set_bn_mod_exp(cryptodev_rsa, cryptodev_bn_mod_exp);
1699 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1700 RSA_meth_set_mod_exp(cryptodev_rsa, cryptodev_rsa_mod_exp);
1702 RSA_meth_set_mod_exp(cryptodev_rsa,
1703 cryptodev_rsa_nocrt_mod_exp);
1707 ENGINE_free(engine);
1711 #ifndef OPENSSL_NO_DSA
1712 cryptodev_dsa = DSA_meth_dup(DSA_OpenSSL());
1713 if (cryptodev_dsa != NULL) {
1714 DSA_meth_set1_name(cryptodev_dsa, "cryptodev DSA method");
1715 DSA_meth_set_flags(cryptodev_dsa, 0);
1716 if (ENGINE_set_DSA(engine, cryptodev_dsa)) {
1717 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1718 DSA_meth_set_sign(cryptodev_dsa, cryptodev_dsa_do_sign);
1719 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1720 DSA_meth_set_bn_mod_exp(cryptodev_dsa,
1721 cryptodev_dsa_bn_mod_exp);
1722 DSA_meth_set_mod_exp(cryptodev_dsa, cryptodev_dsa_dsa_mod_exp);
1724 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1725 DSA_meth_set_verify(cryptodev_dsa, cryptodev_dsa_verify);
1728 ENGINE_free(engine);
1733 #ifndef OPENSSL_NO_DH
1734 cryptodev_dh = DH_meth_dup(DH_OpenSSL());
1735 if (cryptodev_dh != NULL) {
1736 DH_meth_set1_name(cryptodev_dh, "cryptodev DH method");
1737 DH_meth_set_flags(cryptodev_dh, 0);
1738 if (ENGINE_set_DH(engine, cryptodev_dh)) {
1739 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1740 DH_meth_set_bn_mod_exp(cryptodev_dh, cryptodev_mod_exp_dh);
1741 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1742 DH_meth_set_compute_key(cryptodev_dh,
1743 cryptodev_dh_compute_key);
1747 ENGINE_free(engine);
1753 ENGINE_free(engine);
1757 #endif /* HAVE_CRYPTODEV */