34 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
35 (defined(OpenBSD) || defined(__FreeBSD__))
36 #include <sys/param.h>
37 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
38 # define HAVE_CRYPTODEV
40 # if (OpenBSD >= 200110)
41 # define HAVE_SYSLOG_R
45 #ifndef HAVE_CRYPTODEV
56 #include <sys/types.h>
57 #include <crypto/cryptodev.h>
62 #include <sys/ioctl.h>
72 struct dev_crypto_state {
73 struct session_op d_sess;
76 #ifdef USE_CRYPTODEV_DIGESTS
77 char dummy_mac_key[HASH_MAX_LEN];
79 unsigned char digest_res[HASH_MAX_LEN];
85 static u_int32_t cryptodev_asymfeat = 0;
87 static int get_asym_dev_crypto(
void);
88 static int open_dev_crypto(
void);
89 static int get_dev_crypto(
void);
90 static int get_cryptodev_ciphers(
const int **cnids);
91 #ifdef USE_CRYPTODEV_DIGESTS
92 static int get_cryptodev_digests(
const int **cnids);
94 static int cryptodev_usable_ciphers(
const int **nids);
95 static int cryptodev_usable_digests(
const int **nids);
96 static int cryptodev_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
97 const unsigned char *in,
size_t inl);
99 const unsigned char *iv,
int enc);
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,
113 static int cryptodev_rsa_nocrt_mod_exp(
BIGNUM *r0,
const BIGNUM *
I,
121 static DSA_SIG *cryptodev_dsa_do_sign(
const unsigned char *dgst,
123 static int cryptodev_dsa_verify(
const unsigned char *dgst,
int dgst_len,
125 static int cryptodev_mod_exp_dh(
const DH *dh,
BIGNUM *r,
const BIGNUM *
a,
128 static int cryptodev_dh_compute_key(
unsigned char *
key,
130 static int cryptodev_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
144 { CRYPTO_ARC4,
NID_rc4, 0, 16, },
152 { CRYPTO_SKIPJACK_CBC,
NID_undef, 0, 0, },
156 #ifdef USE_CRYPTODEV_DIGESTS
177 open_dev_crypto(
void)
182 if ((fd = open(
"/dev/crypto", O_RDWR, 0)) == -1)
185 if (fcntl(fd, F_SETFD, 1) == -1) {
199 if ((fd = open_dev_crypto()) == -1)
201 #ifndef CRIOGET_NOT_NEEDED
202 if (
ioctl(fd, CRIOGET, &retfd) == -1)
206 if (fcntl(retfd, F_SETFD, 1) == -1) {
216 static void put_dev_crypto(
int fd)
218 #ifndef CRIOGET_NOT_NEEDED
225 get_asym_dev_crypto(
void)
230 fd = get_dev_crypto();
241 get_cryptodev_ciphers(
const int **cnids)
243 static int nids[CRYPTO_ALGORITHM_MAX];
244 struct session_op sess;
245 int fd, i, count = 0;
247 if ((fd = get_dev_crypto()) < 0) {
251 memset(&sess, 0,
sizeof(sess));
252 sess.key = (caddr_t)
"123456789abcdefghijklmno";
254 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
257 sess.cipher = ciphers[i].id;
258 sess.keylen = ciphers[i].keylen;
260 if (
ioctl(fd, CIOCGSESSION, &sess) != -1 &&
261 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
262 nids[count++] = ciphers[i].
nid;
273 #ifdef USE_CRYPTODEV_DIGESTS
281 get_cryptodev_digests(
const int **cnids)
283 static int nids[CRYPTO_ALGORITHM_MAX];
284 struct session_op sess;
285 int fd, i, count = 0;
287 if ((fd = get_dev_crypto()) < 0) {
291 memset(&sess, 0,
sizeof(sess));
292 sess.mackey = (caddr_t)
"123456789abcdefghijklmno";
293 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
296 sess.mac = digests[i].id;
297 sess.mackeylen = digests[i].keylen;
299 if (
ioctl(fd, CIOCGSESSION, &sess) != -1 &&
300 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
301 nids[count++] = digests[i].
nid;
335 cryptodev_usable_ciphers(
const int **nids)
337 return (get_cryptodev_ciphers(nids));
341 cryptodev_usable_digests(
const int **nids)
343 #ifdef USE_CRYPTODEV_DIGESTS
344 return (get_cryptodev_digests(nids));
365 const unsigned char *in,
size_t inl)
367 struct crypt_op cryp;
369 struct session_op *sess = &state->d_sess;
380 memset(&cryp, 0,
sizeof(cryp));
382 cryp.ses = sess->ses;
385 cryp.src = (caddr_t) in;
386 cryp.dst = (caddr_t) out;
389 cryp.op = ctx->
encrypt ? COP_ENCRYPT : COP_DECRYPT;
392 cryp.iv = (caddr_t) ctx->
iv;
400 if (
ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
419 const unsigned char *iv,
int enc)
422 struct session_op *sess = &state->d_sess;
425 for (i = 0; ciphers[i].id; i++)
426 if (ctx->
cipher->
nid == ciphers[i].nid &&
428 ctx->
key_len == ciphers[i].keylen) {
429 cipher = ciphers[i].id;
433 if (!ciphers[i].
id) {
438 memset(sess, 0,
sizeof(
struct session_op));
440 if ((state->d_fd = get_dev_crypto()) < 0)
443 sess->key = (caddr_t)key;
445 sess->cipher = cipher;
447 if (
ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
448 put_dev_crypto(state->d_fd);
464 struct session_op *sess = &state->d_sess;
480 if (
ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
485 put_dev_crypto(state->d_fd);
504 sizeof(
struct dev_crypto_state),
518 sizeof(
struct dev_crypto_state),
532 sizeof(
struct dev_crypto_state),
545 sizeof(
struct dev_crypto_state),
558 sizeof(
struct dev_crypto_state),
571 sizeof(
struct dev_crypto_state),
584 sizeof(
struct dev_crypto_state),
597 sizeof(
struct dev_crypto_state),
610 const int **nids,
int nid)
613 return (cryptodev_usable_ciphers(nids));
617 *cipher = &cryptodev_rc4;
620 *cipher = &cryptodev_3des_cbc;
623 *cipher = &cryptodev_des_cbc;
626 *cipher = &cryptodev_bf_cbc;
629 *cipher = &cryptodev_cast_cbc;
632 *cipher = &cryptodev_aes_cbc;
635 *cipher = &cryptodev_aes_192_cbc;
638 *cipher = &cryptodev_aes_256_cbc;
644 return (*cipher != NULL);
648 #ifdef USE_CRYPTODEV_DIGESTS
652 digest_nid_to_cryptodev(
int nid)
656 for (i = 0; digests[i].id; i++)
657 if (digests[i].nid == nid)
658 return (digests[i].
id);
664 digest_key_length(
int nid)
668 for (i = 0; digests[i].id; i++)
669 if (digests[i].nid == nid)
670 return digests[i].keylen;
675 static int cryptodev_digest_init(
EVP_MD_CTX *ctx)
677 struct dev_crypto_state *state = ctx->
md_data;
678 struct session_op *sess = &state->d_sess;
682 printf(
"cryptodev_digest_init: Can't get digest \n");
686 memset(state, 0,
sizeof(
struct dev_crypto_state));
688 if ((state->d_fd = get_dev_crypto()) < 0) {
689 printf(
"cryptodev_digest_init: Can't get Dev \n");
693 sess->mackey = state->dummy_mac_key;
694 sess->mackeylen = digest_key_length(ctx->
digest->
type);
697 if (
ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
698 put_dev_crypto(state->d_fd);
700 printf(
"cryptodev_digest_init: Open session failed\n");
707 static int cryptodev_digest_update(
EVP_MD_CTX *ctx,
const void *
data,
710 struct crypt_op cryp;
711 struct dev_crypto_state *state = ctx->
md_data;
712 struct session_op *sess = &state->d_sess;
714 if (!data || state->d_fd < 0) {
715 printf(
"cryptodev_digest_update: illegal inputs \n");
725 state->mac_data =
OPENSSL_realloc(state->mac_data, state->mac_len + count);
727 if (!state->mac_data) {
728 printf(
"cryptodev_digest_update: realloc failed\n");
732 memcpy(state->mac_data + state->mac_len, data, count);
733 state->mac_len += count;
738 memset(&cryp, 0,
sizeof(cryp));
740 cryp.ses = sess->ses;
743 cryp.src = (caddr_t) data;
745 cryp.mac = (caddr_t) state->digest_res;
746 if (
ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
747 printf(
"cryptodev_digest_update: digest failed\n");
754 static int cryptodev_digest_final(
EVP_MD_CTX *ctx,
unsigned char *md)
756 struct crypt_op cryp;
757 struct dev_crypto_state *state = ctx->
md_data;
758 struct session_op *sess = &state->d_sess;
762 if (!md || state->d_fd < 0) {
763 printf(
"cryptodev_digest_final: illegal input\n");
769 memset(&cryp, 0,
sizeof(cryp));
770 cryp.ses = sess->ses;
772 cryp.len = state->mac_len;
773 cryp.src = state->mac_data;
775 cryp.mac = (caddr_t)md;
776 if (
ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
777 printf(
"cryptodev_digest_final: digest failed\n");
790 static int cryptodev_digest_cleanup(
EVP_MD_CTX *ctx)
793 struct dev_crypto_state *state = ctx->
md_data;
794 struct session_op *sess = &state->d_sess;
799 if (state->d_fd < 0) {
800 printf(
"cryptodev_digest_cleanup: illegal input\n");
804 if (state->mac_data) {
806 state->mac_data = NULL;
810 if (
ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
811 printf(
"cryptodev_digest_cleanup: failed to close session\n");
816 put_dev_crypto(state->d_fd);
824 struct dev_crypto_state *fstate = from->
md_data;
825 struct dev_crypto_state *dstate = to->
md_data;
826 struct session_op *sess;
829 if (dstate == NULL || fstate == NULL)
832 memcpy(dstate, fstate,
sizeof(
struct dev_crypto_state));
834 sess = &dstate->d_sess;
836 digest = digest_nid_to_cryptodev(to->
digest->
type);
838 sess->mackey = dstate->dummy_mac_key;
839 sess->mackeylen = digest_key_length(to->
digest->
type);
842 dstate->d_fd = get_dev_crypto();
844 if (
ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
845 put_dev_crypto(dstate->d_fd);
847 printf(
"cryptodev_digest_init: Open session failed\n");
851 if (fstate->mac_len != 0) {
852 if (fstate->mac_data != NULL)
855 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
856 dstate->mac_len = fstate->mac_len;
864 const EVP_MD cryptodev_sha1 = {
869 cryptodev_digest_init,
870 cryptodev_digest_update,
871 cryptodev_digest_final,
872 cryptodev_digest_copy,
873 cryptodev_digest_cleanup,
876 sizeof(
struct dev_crypto_state),
879 const EVP_MD cryptodev_md5 = {
884 cryptodev_digest_init,
885 cryptodev_digest_update,
886 cryptodev_digest_final,
887 cryptodev_digest_copy,
888 cryptodev_digest_cleanup,
891 sizeof(
struct dev_crypto_state),
898 cryptodev_engine_digests(
ENGINE *e,
const EVP_MD **digest,
899 const int **nids,
int nid)
902 return (cryptodev_usable_digests(nids));
905 #ifdef USE_CRYPTODEV_DIGESTS
907 *digest = &cryptodev_md5;
910 *digest = &cryptodev_sha1;
917 return (*digest != NULL);
926 bn2crparam(
const BIGNUM *
a,
struct crparam *crp)
936 bytes = (bits + 7) / 8;
943 crp->crp_p = (caddr_t) b;
944 crp->crp_nbits =
bits;
946 for (i = 0, j = 0; i < a->
top; i++) {
947 for (k = 0; k < BN_BITS2 / 8; k++) {
948 if ((j + k) >=
bytes)
950 b[j + k] = a->
d[i] >> (k * 8);
959 crparam2bn(
struct crparam *crp,
BIGNUM *a)
964 bytes = (crp->crp_nbits + 7) / 8;
969 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
972 for (i = 0; i <
bytes; i++)
973 pd[i] = crp->crp_p[bytes - i - 1];
982 zapparams(
struct crypt_kop *kop)
986 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
987 if (kop->crk_param[i].crp_p)
988 free(kop->crk_param[i].crp_p);
989 kop->crk_param[i].crp_p = NULL;
990 kop->crk_param[i].crp_nbits = 0;
995 cryptodev_asym(
struct crypt_kop *kop,
int rlen,
BIGNUM *r,
int slen,
BIGNUM *s)
999 if ((fd = get_asym_dev_crypto()) < 0)
1003 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen,
sizeof(
char));
1004 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1008 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen,
sizeof(
char));
1009 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1013 if (
ioctl(fd, CIOCKEY, kop) == 0) {
1015 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1017 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1028 struct crypt_kop kop;
1034 if (cryptodev_asymfeat == 0) {
1039 memset(&kop, 0,
sizeof kop);
1040 kop.crk_op = CRK_MOD_EXP;
1043 if (bn2crparam(a, &kop.crk_param[0]))
1045 if (bn2crparam(p, &kop.crk_param[1]))
1047 if (bn2crparam(m, &kop.crk_param[2]))
1049 kop.crk_iparams = 3;
1051 if (cryptodev_asym(&kop,
BN_num_bytes(m), r, 0, NULL)) {
1053 printf(
"OCF asym process failed, Running in software\n");
1054 ret = meth->
bn_mod_exp(r, a, p, m, ctx, in_mont);
1056 }
else if (ECANCELED == kop.crk_status) {
1058 printf(
"OCF hardware operation cancelled. Running in Software\n");
1059 ret = meth->
bn_mod_exp(r, a, p, m, ctx, in_mont);
1073 r = cryptodev_bn_mod_exp(r0, I, rsa->
d, rsa->
n, ctx, NULL);
1081 struct crypt_kop kop;
1084 if (!rsa->
p || !rsa->
q || !rsa->
dmp1 || !rsa->
dmq1 || !rsa->
iqmp) {
1089 memset(&kop, 0,
sizeof kop);
1090 kop.crk_op = CRK_MOD_EXP_CRT;
1092 if (bn2crparam(rsa->
p, &kop.crk_param[0]))
1094 if (bn2crparam(rsa->
q, &kop.crk_param[1]))
1096 if (bn2crparam(I, &kop.crk_param[2]))
1098 if (bn2crparam(rsa->
dmp1, &kop.crk_param[3]))
1100 if (bn2crparam(rsa->
dmq1, &kop.crk_param[4]))
1102 if (bn2crparam(rsa->
iqmp, &kop.crk_param[5]))
1104 kop.crk_iparams = 6;
1106 if (cryptodev_asym(&kop,
BN_num_bytes(rsa->
n), r0, 0, NULL)) {
1108 printf(
"OCF asym process failed, running in Software\n");
1111 }
else if (ECANCELED == kop.crk_status) {
1113 printf(
"OCF hardware operation cancelled. Running in Software\n");
1124 "cryptodev RSA method",
1143 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1179 cryptodev_dsa_do_sign(
const unsigned char *dgst,
int dlen,
DSA *dsa)
1181 struct crypt_kop kop;
1182 BIGNUM *r = NULL, *s = NULL;
1185 if ((r =
BN_new()) == NULL)
1187 if ((s =
BN_new()) == NULL) {
1192 memset(&kop, 0,
sizeof kop);
1193 kop.crk_op = CRK_DSA_SIGN;
1196 kop.crk_param[0].crp_p = (caddr_t)dgst;
1197 kop.crk_param[0].crp_nbits = dlen * 8;
1198 if (bn2crparam(dsa->
p, &kop.crk_param[1]))
1200 if (bn2crparam(dsa->
q, &kop.crk_param[2]))
1202 if (bn2crparam(dsa->
g, &kop.crk_param[3]))
1204 if (bn2crparam(dsa->
priv_key, &kop.crk_param[4]))
1206 kop.crk_iparams = 5;
1220 kop.crk_param[0].crp_p = NULL;
1226 cryptodev_dsa_verify(
const unsigned char *dgst,
int dlen,
1229 struct crypt_kop kop;
1232 memset(&kop, 0,
sizeof kop);
1233 kop.crk_op = CRK_DSA_VERIFY;
1236 kop.crk_param[0].crp_p = (caddr_t)dgst;
1237 kop.crk_param[0].crp_nbits = dlen * 8;
1238 if (bn2crparam(dsa->
p, &kop.crk_param[1]))
1240 if (bn2crparam(dsa->
q, &kop.crk_param[2]))
1242 if (bn2crparam(dsa->
g, &kop.crk_param[3]))
1244 if (bn2crparam(dsa->
pub_key, &kop.crk_param[4]))
1246 if (bn2crparam(sig->
r, &kop.crk_param[5]))
1248 if (bn2crparam(sig->
s, &kop.crk_param[6]))
1250 kop.crk_iparams = 7;
1252 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1254 if(0 != kop.crk_status) dsaret = 0;
1261 kop.crk_param[0].crp_p = NULL;
1267 "cryptodev DSA method",
1284 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1288 cryptodev_dh_compute_key(
unsigned char *key,
const BIGNUM *pub_key,
DH *dh)
1290 struct crypt_kop kop;
1294 if ((fd = get_asym_dev_crypto()) < 0) {
1302 memset(&kop, 0,
sizeof kop);
1303 kop.crk_op = CRK_DH_COMPUTE_KEY;
1306 if (bn2crparam(dh->
priv_key, &kop.crk_param[0]))
1308 if (bn2crparam(pub_key, &kop.crk_param[1]))
1310 if (bn2crparam(dh->
p, &kop.crk_param[2]))
1312 kop.crk_iparams = 3;
1314 kop.crk_param[3].crp_p = (caddr_t) key;
1315 kop.crk_param[3].crp_nbits = keylen * 8;
1316 kop.crk_oparams = 1;
1318 if (
ioctl(fd, CIOCKEY, &kop) == -1) {
1324 kop.crk_param[3].crp_p = NULL;
1330 "cryptodev DH method",
1345 cryptodev_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
1347 #ifdef HAVE_SYSLOG_R
1348 struct syslog_data sd = SYSLOG_DATA_INIT;
1353 #ifdef HAVE_SYSLOG_R
1354 syslog_r(LOG_ERR, &sd,
1355 "cryptodev_ctrl: unknown command %d", cmd);
1357 syslog(LOG_ERR,
"cryptodev_ctrl: unknown command %d", cmd);
1372 if ((fd = get_dev_crypto()) < 0) {
1380 if (
ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1406 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1407 cryptodev_rsa.
bn_mod_exp = cryptodev_bn_mod_exp;
1408 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1410 cryptodev_rsa_mod_exp;
1413 cryptodev_rsa_nocrt_mod_exp;
1420 memcpy(&cryptodev_dsa, meth,
sizeof(
DSA_METHOD));
1421 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1422 cryptodev_dsa.
dsa_do_sign = cryptodev_dsa_do_sign;
1423 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1424 cryptodev_dsa.
bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1425 cryptodev_dsa.
dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1427 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1437 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1438 cryptodev_dh.
bn_mod_exp = cryptodev_mod_exp_dh;
1439 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1441 cryptodev_dh_compute_key;