2002-10-04 20:31:23 +00:00
|
|
|
/* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
|
|
|
|
|
2005-01-07 02:29:27 +00:00
|
|
|
/*-
|
2002-10-04 20:31:23 +00:00
|
|
|
* Copyright (c) 2001 Theo de Raadt
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Effort sponsored in part by the Defense Advanced Research Projects
|
|
|
|
* Agency (DARPA) and Air Force Research Laboratory, Air Force
|
|
|
|
* Materiel Command, USAF, under agreement number F30602-01-2-0537.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 05:57:50 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/random.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
2004-05-30 20:27:19 +00:00
|
|
|
#include <sys/module.h>
|
2002-10-04 20:31:23 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
|
|
|
|
#include <opencrypto/cryptodev.h>
|
|
|
|
#include <opencrypto/xform.h>
|
|
|
|
|
|
|
|
struct csession {
|
|
|
|
TAILQ_ENTRY(csession) next;
|
|
|
|
u_int64_t sid;
|
|
|
|
u_int32_t ses;
|
2003-06-02 23:28:06 +00:00
|
|
|
struct mtx lock; /* for op submission */
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
u_int32_t cipher;
|
|
|
|
struct enc_xform *txform;
|
|
|
|
u_int32_t mac;
|
|
|
|
struct auth_hash *thash;
|
|
|
|
|
|
|
|
caddr_t key;
|
|
|
|
int keylen;
|
|
|
|
u_char tmp_iv[EALG_MAX_BLOCK_LEN];
|
|
|
|
|
|
|
|
caddr_t mackey;
|
|
|
|
int mackeylen;
|
|
|
|
|
2003-11-19 22:42:34 +00:00
|
|
|
struct iovec iovec;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct uio uio;
|
|
|
|
int error;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fcrypt {
|
|
|
|
TAILQ_HEAD(csessionlist, csession) csessions;
|
|
|
|
int sesn;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cryptof_rw(struct file *fp, struct uio *uio,
|
|
|
|
struct ucred *cred, int flags, struct thread *);
|
|
|
|
static int cryptof_ioctl(struct file *, u_long, void *,
|
|
|
|
struct ucred *, struct thread *);
|
|
|
|
static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
|
|
|
|
static int cryptof_kqfilter(struct file *, struct knote *);
|
|
|
|
static int cryptof_stat(struct file *, struct stat *,
|
|
|
|
struct ucred *, struct thread *);
|
|
|
|
static int cryptof_close(struct file *, struct thread *);
|
|
|
|
|
|
|
|
static struct fileops cryptofops = {
|
2003-06-18 18:16:40 +00:00
|
|
|
.fo_read = cryptof_rw,
|
|
|
|
.fo_write = cryptof_rw,
|
|
|
|
.fo_ioctl = cryptof_ioctl,
|
|
|
|
.fo_poll = cryptof_poll,
|
|
|
|
.fo_kqfilter = cryptof_kqfilter,
|
|
|
|
.fo_stat = cryptof_stat,
|
|
|
|
.fo_close = cryptof_close
|
2002-10-04 20:31:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct csession *csefind(struct fcrypt *, u_int);
|
|
|
|
static int csedelete(struct fcrypt *, struct csession *);
|
|
|
|
static struct csession *cseadd(struct fcrypt *, struct csession *);
|
|
|
|
static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
|
|
|
|
u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
|
|
|
|
struct auth_hash *);
|
|
|
|
static int csefree(struct csession *);
|
|
|
|
|
|
|
|
static int cryptodev_op(struct csession *, struct crypt_op *,
|
|
|
|
struct ucred *, struct thread *td);
|
|
|
|
static int cryptodev_key(struct crypt_kop *);
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptof_rw(
|
|
|
|
struct file *fp,
|
|
|
|
struct uio *uio,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
int flags,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_ioctl(
|
|
|
|
struct file *fp,
|
|
|
|
u_long cmd,
|
|
|
|
void *data,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
struct cryptoini cria, crie;
|
2003-01-13 00:33:17 +00:00
|
|
|
struct fcrypt *fcr = fp->f_data;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct csession *cse;
|
|
|
|
struct session_op *sop;
|
|
|
|
struct crypt_op *cop;
|
|
|
|
struct enc_xform *txform = NULL;
|
|
|
|
struct auth_hash *thash = NULL;
|
|
|
|
u_int64_t sid;
|
|
|
|
u_int32_t ses;
|
|
|
|
int error = 0;
|
|
|
|
|
2004-11-17 09:09:55 +00:00
|
|
|
/*
|
|
|
|
* XXX: Not sure Giant is needed, but better safe than sorry
|
|
|
|
*/
|
|
|
|
mtx_lock(&Giant);
|
2002-10-04 20:31:23 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case CIOCGSESSION:
|
|
|
|
sop = (struct session_op *)data;
|
|
|
|
switch (sop->cipher) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CRYPTO_DES_CBC:
|
|
|
|
txform = &enc_xform_des;
|
|
|
|
break;
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
txform = &enc_xform_3des;
|
|
|
|
break;
|
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
txform = &enc_xform_blf;
|
|
|
|
break;
|
|
|
|
case CRYPTO_CAST_CBC:
|
|
|
|
txform = &enc_xform_cast5;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SKIPJACK_CBC:
|
|
|
|
txform = &enc_xform_skipjack;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_CBC:
|
|
|
|
txform = &enc_xform_rijndael128;
|
|
|
|
break;
|
|
|
|
case CRYPTO_NULL_CBC:
|
|
|
|
txform = &enc_xform_null;
|
|
|
|
break;
|
|
|
|
case CRYPTO_ARC4:
|
|
|
|
txform = &enc_xform_arc4;
|
|
|
|
break;
|
|
|
|
default:
|
2004-11-17 09:09:55 +00:00
|
|
|
mtx_unlock(&Giant);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sop->mac) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CRYPTO_MD5_HMAC:
|
2006-05-17 18:24:17 +00:00
|
|
|
thash = &auth_hash_hmac_md5;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
2006-05-17 18:24:17 +00:00
|
|
|
thash = &auth_hash_hmac_sha1;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
2006-05-17 18:24:17 +00:00
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_256;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_384;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_512;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
2006-05-17 18:24:17 +00:00
|
|
|
thash = &auth_hash_hmac_ripemd_160;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
#ifdef notdef
|
|
|
|
case CRYPTO_MD5:
|
|
|
|
thash = &auth_hash_md5;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA1:
|
|
|
|
thash = &auth_hash_sha1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case CRYPTO_NULL_HMAC:
|
|
|
|
thash = &auth_hash_null;
|
|
|
|
break;
|
|
|
|
default:
|
2004-11-17 09:09:55 +00:00
|
|
|
mtx_unlock(&Giant);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero(&crie, sizeof(crie));
|
|
|
|
bzero(&cria, sizeof(cria));
|
|
|
|
|
|
|
|
if (txform) {
|
|
|
|
crie.cri_alg = txform->type;
|
|
|
|
crie.cri_klen = sop->keylen * 8;
|
|
|
|
if (sop->keylen > txform->maxkey ||
|
|
|
|
sop->keylen < txform->minkey) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC(crie.cri_key, u_int8_t *,
|
2003-02-19 05:47:46 +00:00
|
|
|
crie.cri_klen / 8, M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if ((error = copyin(sop->key, crie.cri_key,
|
|
|
|
crie.cri_klen / 8)))
|
|
|
|
goto bail;
|
|
|
|
if (thash)
|
|
|
|
crie.cri_next = &cria;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thash) {
|
|
|
|
cria.cri_alg = thash->type;
|
|
|
|
cria.cri_klen = sop->mackeylen * 8;
|
|
|
|
if (sop->mackeylen != thash->keysize) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cria.cri_klen) {
|
|
|
|
MALLOC(cria.cri_key, u_int8_t *,
|
2003-02-19 05:47:46 +00:00
|
|
|
cria.cri_klen / 8, M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if ((error = copyin(sop->mackey, cria.cri_key,
|
|
|
|
cria.cri_klen / 8)))
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-07 18:37:31 +00:00
|
|
|
error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
|
2006-05-17 18:01:51 +00:00
|
|
|
if (error) {
|
|
|
|
if (crypto_devallowsoft) {
|
|
|
|
error = crypto_newsession(&sid,
|
|
|
|
(txform ? &crie : &cria), 0);
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
goto bail;
|
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
|
|
|
|
cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
|
|
|
|
thash);
|
|
|
|
|
|
|
|
if (cse == NULL) {
|
|
|
|
crypto_freesession(sid);
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
sop->ses = cse->ses;
|
|
|
|
|
|
|
|
bail:
|
|
|
|
if (error) {
|
|
|
|
if (crie.cri_key)
|
|
|
|
FREE(crie.cri_key, M_XDATA);
|
|
|
|
if (cria.cri_key)
|
|
|
|
FREE(cria.cri_key, M_XDATA);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CIOCFSESSION:
|
|
|
|
ses = *(u_int32_t *)data;
|
|
|
|
cse = csefind(fcr, ses);
|
2004-11-17 09:09:55 +00:00
|
|
|
if (cse == NULL) {
|
|
|
|
mtx_unlock(&Giant);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
2004-11-17 09:09:55 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
csedelete(fcr, cse);
|
|
|
|
error = csefree(cse);
|
|
|
|
break;
|
|
|
|
case CIOCCRYPT:
|
|
|
|
cop = (struct crypt_op *)data;
|
|
|
|
cse = csefind(fcr, cop->ses);
|
2004-11-17 09:09:55 +00:00
|
|
|
if (cse == NULL) {
|
|
|
|
mtx_unlock(&Giant);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
2004-11-17 09:09:55 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
error = cryptodev_op(cse, cop, active_cred, td);
|
|
|
|
break;
|
|
|
|
case CIOCKEY:
|
|
|
|
error = cryptodev_key((struct crypt_kop *)data);
|
|
|
|
break;
|
|
|
|
case CIOCASYMFEAT:
|
|
|
|
error = crypto_getfeat((int *)data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
2004-11-17 09:09:55 +00:00
|
|
|
mtx_unlock(&Giant);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cryptodev_cb(void *);
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_op(
|
|
|
|
struct csession *cse,
|
|
|
|
struct crypt_op *cop,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
struct cryptop *crp = NULL;
|
|
|
|
struct cryptodesc *crde = NULL, *crda = NULL;
|
2003-11-19 22:42:34 +00:00
|
|
|
int error;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
if (cop->len > 256*1024-4)
|
|
|
|
return (E2BIG);
|
|
|
|
|
2005-08-18 11:58:03 +00:00
|
|
|
if (cse->txform) {
|
|
|
|
if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2003-11-19 22:42:34 +00:00
|
|
|
cse->uio.uio_iov = &cse->iovec;
|
2002-10-04 20:31:23 +00:00
|
|
|
cse->uio.uio_iovcnt = 1;
|
2003-11-19 22:42:34 +00:00
|
|
|
cse->uio.uio_offset = 0;
|
|
|
|
cse->uio.uio_resid = cop->len;
|
2002-10-04 20:31:23 +00:00
|
|
|
cse->uio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
cse->uio.uio_rw = UIO_WRITE;
|
|
|
|
cse->uio.uio_td = td;
|
|
|
|
cse->uio.uio_iov[0].iov_len = cop->len;
|
2006-05-22 16:24:11 +00:00
|
|
|
if (cse->thash)
|
|
|
|
cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
|
|
|
|
cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
|
|
|
|
M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
|
|
|
|
if (crp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cse->thash) {
|
|
|
|
crda = crp->crp_desc;
|
|
|
|
if (cse->txform)
|
|
|
|
crde = crda->crd_next;
|
|
|
|
} else {
|
|
|
|
if (cse->txform)
|
|
|
|
crde = crp->crp_desc;
|
|
|
|
else {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
if (crda) {
|
|
|
|
crda->crd_skip = 0;
|
|
|
|
crda->crd_len = cop->len;
|
2006-05-22 16:24:11 +00:00
|
|
|
crda->crd_inject = cop->len;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
crda->crd_alg = cse->mac;
|
|
|
|
crda->crd_key = cse->mackey;
|
|
|
|
crda->crd_klen = cse->mackeylen * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crde) {
|
|
|
|
if (cop->op == COP_ENCRYPT)
|
|
|
|
crde->crd_flags |= CRD_F_ENCRYPT;
|
|
|
|
else
|
|
|
|
crde->crd_flags &= ~CRD_F_ENCRYPT;
|
|
|
|
crde->crd_len = cop->len;
|
|
|
|
crde->crd_inject = 0;
|
|
|
|
|
|
|
|
crde->crd_alg = cse->cipher;
|
|
|
|
crde->crd_key = cse->key;
|
|
|
|
crde->crd_klen = cse->keylen * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
crp->crp_ilen = cop->len;
|
2003-02-23 07:25:48 +00:00
|
|
|
crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
|
|
|
|
| (cop->flags & COP_F_BATCH);
|
2002-10-04 20:31:23 +00:00
|
|
|
crp->crp_buf = (caddr_t)&cse->uio;
|
|
|
|
crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
|
|
|
|
crp->crp_sid = cse->sid;
|
|
|
|
crp->crp_opaque = (void *)cse;
|
|
|
|
|
|
|
|
if (cop->iv) {
|
|
|
|
if (crde == NULL) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
|
|
|
|
goto bail;
|
|
|
|
bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
|
|
|
|
crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
|
|
|
|
crde->crd_skip = 0;
|
|
|
|
} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
|
|
|
crde->crd_skip = 0;
|
|
|
|
} else if (crde) {
|
|
|
|
crde->crd_flags |= CRD_F_IV_PRESENT;
|
|
|
|
crde->crd_skip = cse->txform->blocksize;
|
|
|
|
crde->crd_len -= cse->txform->blocksize;
|
|
|
|
}
|
|
|
|
|
2006-05-22 16:24:11 +00:00
|
|
|
if (cop->mac && crda == NULL) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
2003-06-02 23:28:06 +00:00
|
|
|
/*
|
|
|
|
* Let the dispatch run unlocked, then, interlock against the
|
|
|
|
* callback before checking if the operation completed and going
|
|
|
|
* to sleep. This insures drivers don't inherit our lock which
|
|
|
|
* results in a lock order reversal between crypto_dispatch forced
|
|
|
|
* entry and the crypto_done callback into us.
|
|
|
|
*/
|
|
|
|
error = crypto_dispatch(crp);
|
|
|
|
mtx_lock(&cse->lock);
|
|
|
|
if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
|
|
|
|
error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
|
|
|
|
mtx_unlock(&cse->lock);
|
|
|
|
|
|
|
|
if (error != 0)
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
|
|
|
|
|
|
|
if (crp->crp_etype != 0) {
|
|
|
|
error = crp->crp_etype;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cse->error) {
|
|
|
|
error = cse->error;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cop->dst &&
|
|
|
|
(error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
if (cop->mac &&
|
2006-05-22 16:24:11 +00:00
|
|
|
(error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
|
|
|
|
cop->mac, cse->thash->hashsize)))
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
|
|
|
|
|
|
|
bail:
|
|
|
|
if (crp)
|
|
|
|
crypto_freereq(crp);
|
|
|
|
if (cse->uio.uio_iov[0].iov_base)
|
|
|
|
free(cse->uio.uio_iov[0].iov_base, M_XDATA);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_cb(void *op)
|
|
|
|
{
|
|
|
|
struct cryptop *crp = (struct cryptop *) op;
|
|
|
|
struct csession *cse = (struct csession *)crp->crp_opaque;
|
|
|
|
|
|
|
|
cse->error = crp->crp_etype;
|
|
|
|
if (crp->crp_etype == EAGAIN)
|
|
|
|
return crypto_dispatch(crp);
|
2003-06-02 23:28:06 +00:00
|
|
|
mtx_lock(&cse->lock);
|
|
|
|
wakeup_one(crp);
|
|
|
|
mtx_unlock(&cse->lock);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodevkey_cb(void *op)
|
|
|
|
{
|
|
|
|
struct cryptkop *krp = (struct cryptkop *) op;
|
|
|
|
|
|
|
|
wakeup(krp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_key(struct crypt_kop *kop)
|
|
|
|
{
|
|
|
|
struct cryptkop *krp = NULL;
|
|
|
|
int error = EINVAL;
|
|
|
|
int in, out, size, i;
|
|
|
|
|
|
|
|
if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
|
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
in = kop->crk_iparams;
|
|
|
|
out = kop->crk_oparams;
|
|
|
|
switch (kop->crk_op) {
|
|
|
|
case CRK_MOD_EXP:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
return (EINVAL);
|
|
|
|
case CRK_MOD_EXP_CRT:
|
|
|
|
if (in == 6 && out == 1)
|
|
|
|
break;
|
|
|
|
return (EINVAL);
|
|
|
|
case CRK_DSA_SIGN:
|
|
|
|
if (in == 5 && out == 2)
|
|
|
|
break;
|
|
|
|
return (EINVAL);
|
|
|
|
case CRK_DSA_VERIFY:
|
|
|
|
if (in == 7 && out == 0)
|
|
|
|
break;
|
|
|
|
return (EINVAL);
|
|
|
|
case CRK_DH_COMPUTE_KEY:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
return (EINVAL);
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (!krp)
|
|
|
|
return (ENOMEM);
|
|
|
|
bzero(krp, sizeof *krp);
|
|
|
|
krp->krp_op = kop->crk_op;
|
|
|
|
krp->krp_status = kop->crk_status;
|
|
|
|
krp->krp_iparams = kop->crk_iparams;
|
|
|
|
krp->krp_oparams = kop->crk_oparams;
|
|
|
|
krp->krp_status = 0;
|
|
|
|
krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
|
|
|
|
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++)
|
|
|
|
krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
|
|
|
|
for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
|
|
|
|
size = (krp->krp_param[i].crp_nbits + 7) / 8;
|
|
|
|
if (size == 0)
|
|
|
|
continue;
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (i >= krp->krp_iparams)
|
|
|
|
continue;
|
|
|
|
error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = crypto_kdispatch(krp);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
error = tsleep(krp, PSOCK, "crydev", 0);
|
|
|
|
if (error) {
|
|
|
|
/* XXX can this happen? if so, how do we recover? */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (krp->krp_status != 0) {
|
|
|
|
error = krp->krp_status;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
|
|
|
|
size = (krp->krp_param[i].crp_nbits + 7) / 8;
|
|
|
|
if (size == 0)
|
|
|
|
continue;
|
|
|
|
error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (krp) {
|
|
|
|
kop->crk_status = krp->krp_status;
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
|
|
|
if (krp->krp_param[i].crp_p)
|
|
|
|
FREE(krp->krp_param[i].crp_p, M_XDATA);
|
|
|
|
}
|
|
|
|
free(krp, M_XDATA);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_poll(
|
|
|
|
struct file *fp,
|
|
|
|
int events,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_kqfilter(struct file *fp, struct knote *kn)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_stat(
|
|
|
|
struct file *fp,
|
|
|
|
struct stat *sb,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_close(struct file *fp, struct thread *td)
|
|
|
|
{
|
2003-01-13 00:33:17 +00:00
|
|
|
struct fcrypt *fcr = fp->f_data;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct csession *cse;
|
|
|
|
|
|
|
|
while ((cse = TAILQ_FIRST(&fcr->csessions))) {
|
|
|
|
TAILQ_REMOVE(&fcr->csessions, cse, next);
|
|
|
|
(void)csefree(cse);
|
|
|
|
}
|
|
|
|
FREE(fcr, M_XDATA);
|
2003-01-13 00:33:17 +00:00
|
|
|
fp->f_data = NULL;
|
2002-10-04 20:31:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct csession *
|
|
|
|
csefind(struct fcrypt *fcr, u_int ses)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(cse, &fcr->csessions, next)
|
|
|
|
if (cse->ses == ses)
|
|
|
|
return (cse);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
csedelete(struct fcrypt *fcr, struct csession *cse_del)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(cse, &fcr->csessions, next) {
|
|
|
|
if (cse == cse_del) {
|
|
|
|
TAILQ_REMOVE(&fcr->csessions, cse, next);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct csession *
|
|
|
|
cseadd(struct fcrypt *fcr, struct csession *cse)
|
|
|
|
{
|
|
|
|
TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
|
|
|
|
cse->ses = fcr->sesn++;
|
|
|
|
return (cse);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct csession *
|
|
|
|
csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
|
|
|
|
caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
|
|
|
|
struct enc_xform *txform, struct auth_hash *thash)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
2003-06-02 23:28:06 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
/* NB: required when mtx_init is built with INVARIANTS */
|
|
|
|
MALLOC(cse, struct csession *, sizeof(struct csession),
|
|
|
|
M_XDATA, M_NOWAIT | M_ZERO);
|
|
|
|
#else
|
2002-10-04 20:31:23 +00:00
|
|
|
MALLOC(cse, struct csession *, sizeof(struct csession),
|
|
|
|
M_XDATA, M_NOWAIT);
|
2003-06-02 23:28:06 +00:00
|
|
|
#endif
|
2002-10-04 20:31:23 +00:00
|
|
|
if (cse == NULL)
|
|
|
|
return NULL;
|
2003-06-02 23:28:06 +00:00
|
|
|
mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
|
2002-10-04 20:31:23 +00:00
|
|
|
cse->key = key;
|
|
|
|
cse->keylen = keylen/8;
|
|
|
|
cse->mackey = mackey;
|
|
|
|
cse->mackeylen = mackeylen/8;
|
|
|
|
cse->sid = sid;
|
|
|
|
cse->cipher = cipher;
|
|
|
|
cse->mac = mac;
|
|
|
|
cse->txform = txform;
|
|
|
|
cse->thash = thash;
|
|
|
|
cseadd(fcr, cse);
|
|
|
|
return (cse);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
csefree(struct csession *cse)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = crypto_freesession(cse->sid);
|
2003-06-02 23:28:06 +00:00
|
|
|
mtx_destroy(&cse->lock);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (cse->key)
|
|
|
|
FREE(cse->key, M_XDATA);
|
|
|
|
if (cse->mackey)
|
|
|
|
FREE(cse->mackey, M_XDATA);
|
|
|
|
FREE(cse, M_XDATA);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
struct fcrypt *fcr;
|
|
|
|
int fd, error;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CRIOGET:
|
|
|
|
MALLOC(fcr, struct fcrypt *,
|
2003-02-19 05:47:46 +00:00
|
|
|
sizeof(struct fcrypt), M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
TAILQ_INIT(&fcr->csessions);
|
|
|
|
fcr->sesn = 0;
|
|
|
|
|
|
|
|
error = falloc(td, &f, &fd);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
FREE(fcr, M_XDATA);
|
|
|
|
return (error);
|
|
|
|
}
|
2003-10-19 20:41:07 +00:00
|
|
|
/* falloc automatically provides an extra reference to 'f'. */
|
2002-10-04 20:31:23 +00:00
|
|
|
f->f_flag = FREAD | FWRITE;
|
|
|
|
f->f_type = DTYPE_CRYPTO;
|
|
|
|
f->f_ops = &cryptofops;
|
2003-01-13 00:33:17 +00:00
|
|
|
f->f_data = fcr;
|
2002-10-04 20:31:23 +00:00
|
|
|
*(u_int32_t *)data = fd;
|
|
|
|
fdrop(f, td);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cdevsw crypto_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = cryptoopen,
|
|
|
|
.d_read = cryptoread,
|
|
|
|
.d_write = cryptowrite,
|
|
|
|
.d_ioctl = cryptoioctl,
|
|
|
|
.d_name = "crypto",
|
2002-10-04 20:31:23 +00:00
|
|
|
};
|
2004-06-16 09:47:26 +00:00
|
|
|
static struct cdev *crypto_dev;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialization code, both for static and dynamic loading.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cryptodev_modevent(module_t mod, int type, void *unused)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
if (bootverbose)
|
|
|
|
printf("crypto: <crypto device>\n");
|
2002-11-08 23:07:41 +00:00
|
|
|
crypto_dev = make_dev(&crypto_cdevsw, 0,
|
2002-10-04 20:31:23 +00:00
|
|
|
UID_ROOT, GID_WHEEL, 0666,
|
|
|
|
"crypto");
|
|
|
|
return 0;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
/*XXX disallow if active sessions */
|
|
|
|
destroy_dev(crypto_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moduledata_t cryptodev_mod = {
|
|
|
|
"cryptodev",
|
|
|
|
cryptodev_modevent,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
MODULE_VERSION(cryptodev, 1);
|
|
|
|
DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
|
2002-10-16 14:31:34 +00:00
|
|
|
MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
|
2006-02-27 16:56:22 +00:00
|
|
|
MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);
|