Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.60
      1 /*	$NetBSD: cryptodev.c,v 1.60 2011/05/23 13:53:59 drochner Exp $ */
      2 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
      3 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
      4 
      5 /*-
      6  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Coyote Point Systems, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 2001 Theo de Raadt
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  *
     41  * 1. Redistributions of source code must retain the above copyright
     42  *   notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *   notice, this list of conditions and the following disclaimer in the
     45  *   documentation and/or other materials provided with the distribution.
     46  * 3. The name of the author may not be used to endorse or promote products
     47  *   derived from this software without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59  *
     60  * Effort sponsored in part by the Defense Advanced Research Projects
     61  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     62  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     63  *
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.60 2011/05/23 13:53:59 drochner Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kmem.h>
     72 #include <sys/malloc.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/pool.h>
     75 #include <sys/sysctl.h>
     76 #include <sys/file.h>
     77 #include <sys/filedesc.h>
     78 #include <sys/errno.h>
     79 #include <sys/md5.h>
     80 #include <sys/sha1.h>
     81 #include <sys/conf.h>
     82 #include <sys/device.h>
     83 #include <sys/kauth.h>
     84 #include <sys/select.h>
     85 #include <sys/poll.h>
     86 #include <sys/atomic.h>
     87 #include <sys/stat.h>
     88 
     89 #include "opt_ocf.h"
     90 #include "opt_compat_netbsd.h"
     91 #include <opencrypto/cryptodev.h>
     92 #include <opencrypto/cryptodev_internal.h>
     93 #include <opencrypto/xform.h>
     94 
     95 struct csession {
     96 	TAILQ_ENTRY(csession) next;
     97 	u_int64_t	sid;
     98 	u_int32_t	ses;
     99 
    100 	u_int32_t	cipher;		/* note: shares name space in crd_alg */
    101 	const struct enc_xform *txform;
    102 	u_int32_t	mac;		/* note: shares name space in crd_alg */
    103 	const struct auth_hash *thash;
    104 	u_int32_t	comp_alg;	/* note: shares name space in crd_alg */
    105 	const struct comp_algo *tcomp;
    106 
    107 	void *		key;
    108 	int		keylen;
    109 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
    110 
    111 	void *		mackey;
    112 	int		mackeylen;
    113 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
    114 
    115 	struct iovec	iovec[1];	/* user requests never have more */
    116 	struct uio	uio;
    117 	int		error;
    118 };
    119 
    120 struct fcrypt {
    121 	TAILQ_HEAD(csessionlist, csession) csessions;
    122 	TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
    123 	TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
    124 	int		sesn;
    125 	struct selinfo	sinfo;
    126 	u_int32_t	requestid;
    127 	struct timespec atime;
    128 	struct timespec mtime;
    129 	struct timespec btime;
    130 };
    131 
    132 /* For our fixed-size allocations */
    133 static struct pool fcrpl;
    134 static struct pool csepl;
    135 
    136 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
    137 static int	cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
    138 static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
    139 static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
    140 static int	cryptoselect(dev_t dev, int rw, struct lwp *l);
    141 
    142 /* Declaration of cloned-device (per-ctxt) entrypoints */
    143 static int	cryptof_read(struct file *, off_t *, struct uio *,
    144     kauth_cred_t, int);
    145 static int	cryptof_write(struct file *, off_t *, struct uio *,
    146     kauth_cred_t, int);
    147 static int	cryptof_ioctl(struct file *, u_long, void *);
    148 static int	cryptof_close(struct file *);
    149 static int 	cryptof_poll(struct file *, int);
    150 static int 	cryptof_stat(struct file *, struct stat *);
    151 
    152 static const struct fileops cryptofops = {
    153 	.fo_read = cryptof_read,
    154 	.fo_write = cryptof_write,
    155 	.fo_ioctl = cryptof_ioctl,
    156 	.fo_fcntl = fnullop_fcntl,
    157 	.fo_poll = cryptof_poll,
    158 	.fo_stat = cryptof_stat,
    159 	.fo_close = cryptof_close,
    160 	.fo_kqfilter = fnullop_kqfilter,
    161 	.fo_restart = fnullop_restart,
    162 };
    163 
    164 struct csession *cryptodev_csefind(struct fcrypt *, u_int);
    165 static struct	csession *csefind(struct fcrypt *, u_int);
    166 static int	csedelete(struct fcrypt *, struct csession *);
    167 static struct	csession *cseadd(struct fcrypt *, struct csession *);
    168 static struct	csession *csecreate(struct fcrypt *, u_int64_t, void *,
    169     u_int64_t, void *, u_int64_t, u_int32_t, u_int32_t, u_int32_t,
    170     const struct enc_xform *, const struct auth_hash *,
    171     const struct comp_algo *);
    172 static int	csefree(struct csession *);
    173 
    174 static int	cryptodev_key(struct crypt_kop *);
    175 static int	cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
    176 static int	cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
    177 
    178 static int	cryptodev_cb(void *);
    179 static int	cryptodevkey_cb(void *);
    180 
    181 static int	cryptodev_mcb(void *);
    182 static int	cryptodevkey_mcb(void *);
    183 
    184 static int 	cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
    185     int);
    186 static int	cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
    187 
    188 #ifdef COMPAT_50
    189 extern int	ocryptof_ioctl(struct file *, u_long, void *);
    190 #endif
    191 
    192 /*
    193  * sysctl-able control variables for /dev/crypto now defined in crypto.c:
    194  * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
    195  */
    196 
    197 /* ARGSUSED */
    198 int
    199 cryptof_read(file_t *fp, off_t *poff,
    200     struct uio *uio, kauth_cred_t cred, int flags)
    201 {
    202 	return EIO;
    203 }
    204 
    205 /* ARGSUSED */
    206 int
    207 cryptof_write(file_t *fp, off_t *poff,
    208     struct uio *uio, kauth_cred_t cred, int flags)
    209 {
    210 	return EIO;
    211 }
    212 
    213 /* ARGSUSED */
    214 int
    215 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
    216 {
    217 	struct fcrypt *fcr = fp->f_data;
    218 	struct csession *cse;
    219 	struct session_op *sop;
    220 	struct session_n_op *snop;
    221 	struct crypt_op *cop;
    222 	struct crypt_mop *mop;
    223 	struct crypt_mkop *mkop;
    224 	struct crypt_n_op *cnop;
    225 	struct crypt_n_kop *knop;
    226 	struct crypt_sgop *sgop;
    227 	struct crypt_sfop *sfop;
    228 	struct cryptret *crypt_ret;
    229 	struct crypt_result *crypt_res;
    230 	u_int32_t ses;
    231 	u_int32_t *sesid;
    232 	int error = 0;
    233 	size_t count;
    234 
    235 	/* backwards compatibility */
    236         file_t *criofp;
    237 	struct fcrypt *criofcr;
    238 	int criofd;
    239 
    240 	mutex_enter(&crypto_mtx);
    241 	getnanotime(&fcr->atime);
    242 	mutex_exit(&crypto_mtx);
    243 
    244 	switch (cmd) {
    245         case CRIOGET:   /* XXX deprecated, remove after 5.0 */
    246 		if ((error = fd_allocfile(&criofp, &criofd)) != 0)
    247 			return error;
    248 		criofcr = pool_get(&fcrpl, PR_WAITOK);
    249 		mutex_enter(&crypto_mtx);
    250 		TAILQ_INIT(&criofcr->csessions);
    251 		TAILQ_INIT(&criofcr->crp_ret_mq);
    252 		TAILQ_INIT(&criofcr->crp_ret_mkq);
    253 		selinit(&criofcr->sinfo);
    254 
    255                 /*
    256                  * Don't ever return session 0, to allow detection of
    257                  * failed creation attempts with multi-create ioctl.
    258                  */
    259 		criofcr->sesn = 1;
    260 		criofcr->requestid = 1;
    261 		mutex_exit(&crypto_mtx);
    262 		(void)fd_clone(criofp, criofd, (FREAD|FWRITE),
    263 			      &cryptofops, criofcr);
    264 		*(u_int32_t *)data = criofd;
    265 		return error;
    266 		break;
    267 	case CIOCGSESSION:
    268 		sop = (struct session_op *)data;
    269 		error = cryptodev_session(fcr, sop);
    270 		break;
    271 	case CIOCNGSESSION:
    272 		sgop = (struct crypt_sgop *)data;
    273 		snop = kmem_alloc((sgop->count *
    274 				  sizeof(struct session_n_op)), KM_SLEEP);
    275 		error = copyin(sgop->sessions, snop, sgop->count *
    276 			       sizeof(struct session_n_op));
    277 		if (error) {
    278 			goto mbail;
    279 		}
    280 
    281 		mutex_enter(&crypto_mtx);
    282 		fcr->mtime = fcr->atime;
    283 		mutex_exit(&crypto_mtx);
    284 		error = cryptodev_msession(fcr, snop, sgop->count);
    285 		if (error) {
    286 			goto mbail;
    287 		}
    288 
    289 		error = copyout(snop, sgop->sessions, sgop->count *
    290 		    sizeof(struct session_n_op));
    291 mbail:
    292 		kmem_free(snop, sgop->count * sizeof(struct session_n_op));
    293 		break;
    294 	case CIOCFSESSION:
    295 		mutex_enter(&crypto_mtx);
    296 		fcr->mtime = fcr->atime;
    297 		ses = *(u_int32_t *)data;
    298 		cse = csefind(fcr, ses);
    299 		if (cse == NULL) {
    300 			mutex_exit(&crypto_mtx);
    301 			return EINVAL;
    302 		}
    303 		csedelete(fcr, cse);
    304 		mutex_exit(&crypto_mtx);
    305 		error = csefree(cse);
    306 		break;
    307 	case CIOCNFSESSION:
    308 		mutex_enter(&crypto_mtx);
    309 		fcr->mtime = fcr->atime;
    310 		mutex_exit(&crypto_mtx);
    311 		sfop = (struct crypt_sfop *)data;
    312 		sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
    313 		    KM_SLEEP);
    314 		error = copyin(sfop->sesid, sesid,
    315 		    (sfop->count * sizeof(u_int32_t)));
    316 		if (!error) {
    317 			error = cryptodev_msessionfin(fcr, sfop->count, sesid);
    318 		}
    319 		kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
    320 		break;
    321 	case CIOCCRYPT:
    322 		mutex_enter(&crypto_mtx);
    323 		fcr->mtime = fcr->atime;
    324 		cop = (struct crypt_op *)data;
    325 		cse = csefind(fcr, cop->ses);
    326 		mutex_exit(&crypto_mtx);
    327 		if (cse == NULL) {
    328 			DPRINTF(("csefind failed\n"));
    329 			return EINVAL;
    330 		}
    331 		error = cryptodev_op(cse, cop, curlwp);
    332 		DPRINTF(("cryptodev_op error = %d\n", error));
    333 		break;
    334 	case CIOCNCRYPTM:
    335 		mutex_enter(&crypto_mtx);
    336 		fcr->mtime = fcr->atime;
    337 		mutex_exit(&crypto_mtx);
    338 		mop = (struct crypt_mop *)data;
    339 		cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
    340 		    KM_SLEEP);
    341 		error = copyin(mop->reqs, cnop,
    342 		    (mop->count * sizeof(struct crypt_n_op)));
    343 		if(!error) {
    344 			error = cryptodev_mop(fcr, cnop, mop->count, curlwp);
    345 			if (!error) {
    346 				error = copyout(cnop, mop->reqs,
    347 				    (mop->count * sizeof(struct crypt_n_op)));
    348 			}
    349 		}
    350 		kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
    351 		break;
    352 	case CIOCKEY:
    353 		error = cryptodev_key((struct crypt_kop *)data);
    354 		DPRINTF(("cryptodev_key error = %d\n", error));
    355 		break;
    356 	case CIOCNFKEYM:
    357 		mutex_enter(&crypto_mtx);
    358 		fcr->mtime = fcr->atime;
    359 		mutex_exit(&crypto_mtx);
    360 		mkop = (struct crypt_mkop *)data;
    361 		knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
    362 		    KM_SLEEP);
    363 		error = copyin(mkop->reqs, knop,
    364 		    (mkop->count * sizeof(struct crypt_n_kop)));
    365 		if (!error) {
    366 			error = cryptodev_mkey(fcr, knop, mkop->count);
    367 			if (!error)
    368 				error = copyout(knop, mkop->reqs,
    369 				    (mkop->count * sizeof(struct crypt_n_kop)));
    370 		}
    371 		kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
    372 		break;
    373 	case CIOCASYMFEAT:
    374 		error = crypto_getfeat((int *)data);
    375 		break;
    376 	case CIOCNCRYPTRETM:
    377 		mutex_enter(&crypto_mtx);
    378 		fcr->mtime = fcr->atime;
    379 		mutex_exit(&crypto_mtx);
    380 		crypt_ret = (struct cryptret *)data;
    381 		count = crypt_ret->count;
    382 		crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),
    383 		    KM_SLEEP);
    384 		error = copyin(crypt_ret->results, crypt_res,
    385 		    (count * sizeof(struct crypt_result)));
    386 		if (error)
    387 			goto reterr;
    388 		crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
    389 		    crypt_ret->count);
    390 		/* sanity check count */
    391 		if (crypt_ret->count > count) {
    392 			printf("%s.%d: error returned count %zd > original "
    393 			    " count %zd\n",
    394 			    __FILE__, __LINE__, crypt_ret->count, count);
    395 			crypt_ret->count = count;
    396 
    397 		}
    398 		error = copyout(crypt_res, crypt_ret->results,
    399 		    (crypt_ret->count * sizeof(struct crypt_result)));
    400 reterr:
    401 		kmem_free(crypt_res, (count * sizeof(struct crypt_result)));
    402 		break;
    403 	case CIOCNCRYPTRET:
    404 		error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
    405 		break;
    406 	default:
    407 #ifdef COMPAT_50
    408 		/* Check for backward compatible commands */
    409 		error = ocryptof_ioctl(fp, cmd, data);
    410 #else
    411 		return EINVAL;
    412 #endif
    413 	}
    414 	return error;
    415 }
    416 
    417 int
    418 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
    419 {
    420 	struct cryptop *crp = NULL;
    421 	struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
    422 	int error;
    423 	int iov_len = cop->len;
    424 	int flags=0;
    425 	int dst_len;	/* copyout size */
    426 
    427 	if (cop->len > 256*1024-4)
    428 		return E2BIG;
    429 
    430 	if (cse->txform) {
    431 		if (cop->len < cse->txform->blocksize
    432 		    + (cop->iv ? 0 : cse->txform->ivsize) ||
    433 		    (cop->len - (cop->iv ? 0 : cse->txform->ivsize))
    434 		    % cse->txform->blocksize != 0)
    435 			return EINVAL;
    436 	}
    437 
    438 	DPRINTF(("cryptodev_op[%u]: iov_len %d\n",
    439 		CRYPTO_SESID2LID(cse->sid), iov_len));
    440 	if ((cse->tcomp) && cop->dst_len) {
    441 		if (iov_len < cop->dst_len) {
    442 			/* Need larger iov to deal with decompress */
    443 			iov_len = cop->dst_len;
    444 		}
    445 		DPRINTF(("cryptodev_op: iov_len -> %d for decompress\n", iov_len));
    446 	}
    447 
    448 	(void)memset(&cse->uio, 0, sizeof(cse->uio));
    449 	cse->uio.uio_iovcnt = 1;
    450 	cse->uio.uio_resid = 0;
    451 	cse->uio.uio_rw = UIO_WRITE;
    452 	cse->uio.uio_iov = cse->iovec;
    453 	UIO_SETUP_SYSSPACE(&cse->uio);
    454 	memset(&cse->iovec, 0, sizeof(cse->iovec));
    455 
    456 	/* the iov needs to be big enough to handle the uncompressed
    457 	 * data.... */
    458 	cse->uio.uio_iov[0].iov_len = iov_len;
    459 	cse->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
    460 	cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
    461 	DPRINTF(("cryptodev_op[%u]: uio.iov_base %p malloced %d bytes\n",
    462 		CRYPTO_SESID2LID(cse->sid),
    463 		cse->uio.uio_iov[0].iov_base, iov_len));
    464 
    465 	crp = crypto_getreq((cse->tcomp != NULL) + (cse->txform != NULL) + (cse->thash != NULL));
    466 	if (crp == NULL) {
    467 		error = ENOMEM;
    468 		goto bail;
    469 	}
    470 	DPRINTF(("cryptodev_op[%u]: crp %p\n",
    471 		CRYPTO_SESID2LID(cse->sid), crp));
    472 
    473 	/* crds are always ordered tcomp, thash, then txform */
    474 	/* with optional missing links */
    475 
    476 	/* XXX: If we're going to compress then hash or encrypt, we need
    477 	 * to be able to pass on the new size of the data.
    478 	 */
    479 
    480 	if (cse->tcomp) {
    481 		crdc = crp->crp_desc;
    482 	}
    483 
    484 	if (cse->thash) {
    485 		crda = crdc ? crdc->crd_next : crp->crp_desc;
    486 		if (cse->txform && crda)
    487 			crde = crda->crd_next;
    488 	} else {
    489 		if (cse->txform) {
    490 			crde = crdc ? crdc->crd_next : crp->crp_desc;
    491 		} else if (!cse->tcomp) {
    492 			error = EINVAL;
    493 			goto bail;
    494 		}
    495 	}
    496 
    497 	DPRINTF(("ocf[%u]: iov_len %zu, cop->len %u\n",
    498 			CRYPTO_SESID2LID(cse->sid),
    499 			cse->uio.uio_iov[0].iov_len,
    500 			cop->len));
    501 
    502 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    503 	{
    504 		printf("copyin failed %s %d \n", (char *)cop->src, error);
    505 		goto bail;
    506 	}
    507 
    508 	if (crdc) {
    509 		switch (cop->op) {
    510 		case COP_COMP:
    511 			crdc->crd_flags |= CRD_F_COMP;
    512 			break;
    513 		case COP_DECOMP:
    514 			crdc->crd_flags &= ~CRD_F_COMP;
    515 			break;
    516 		default:
    517 			break;
    518 		}
    519 		/* more data to follow? */
    520 		if (cop->flags & COP_F_MORE) {
    521 			flags |= CRYPTO_F_MORE;
    522 		}
    523 		crdc->crd_len = cop->len;
    524 		crdc->crd_inject = 0;
    525 
    526 		crdc->crd_alg = cse->comp_alg;
    527 		crdc->crd_key = NULL;
    528 		crdc->crd_klen = 0;
    529 		DPRINTF(("cryptodev_op[%u]: crdc setup for comp_alg %d.\n",
    530 			CRYPTO_SESID2LID(cse->sid), crdc->crd_alg));
    531 	}
    532 
    533 	if (crda) {
    534 		crda->crd_skip = 0;
    535 		crda->crd_len = cop->len;
    536 		crda->crd_inject = 0;	/* ??? */
    537 
    538 		crda->crd_alg = cse->mac;
    539 		crda->crd_key = cse->mackey;
    540 		crda->crd_klen = cse->mackeylen * 8;
    541 		DPRINTF(("cryptodev_op: crda setup for mac %d.\n", crda->crd_alg));
    542 	}
    543 
    544 	if (crde) {
    545 		switch (cop->op) {
    546 		case COP_ENCRYPT:
    547 			crde->crd_flags |= CRD_F_ENCRYPT;
    548 			break;
    549 		case COP_DECRYPT:
    550 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    551 			break;
    552 		default:
    553 			break;
    554 		}
    555 		crde->crd_len = cop->len;
    556 		crde->crd_inject = 0;
    557 
    558 		crde->crd_alg = cse->cipher;
    559 		crde->crd_key = cse->key;
    560 		crde->crd_klen = cse->keylen * 8;
    561 		DPRINTF(("cryptodev_op: crde setup for cipher %d.\n", crde->crd_alg));
    562 	}
    563 
    564 
    565 	crp->crp_ilen = cop->len;
    566 	/* The reqest is flagged as CRYPTO_F_USER as long as it is running
    567 	 * in the user IOCTL thread.  This flag lets us skip using the retq for
    568 	 * the request if it completes immediately. If the request ends up being
    569 	 * delayed or is not completed immediately the flag is removed.
    570 	 */
    571 	crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH) | CRYPTO_F_USER |
    572 			flags;
    573 	crp->crp_buf = (void *)&cse->uio;
    574 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    575 	crp->crp_sid = cse->sid;
    576 	crp->crp_opaque = (void *)cse;
    577 
    578 	if (cop->iv) {
    579 		if (crde == NULL) {
    580 			error = EINVAL;
    581 			goto bail;
    582 		}
    583 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    584 			error = EINVAL;
    585 			goto bail;
    586 		}
    587 		if ((error = copyin(cop->iv, cse->tmp_iv,
    588 		    cse->txform->ivsize)))
    589 			goto bail;
    590 		(void)memcpy(crde->crd_iv, cse->tmp_iv, cse->txform->ivsize);
    591 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    592 		crde->crd_skip = 0;
    593 	} else if (crde) {
    594 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    595 			crde->crd_skip = 0;
    596 		} else {
    597 			crde->crd_flags |= CRD_F_IV_PRESENT;
    598 			crde->crd_skip = cse->txform->ivsize;
    599 			crde->crd_len -= cse->txform->ivsize;
    600 		}
    601 	}
    602 
    603 	if (cop->mac) {
    604 		if (crda == NULL) {
    605 			error = EINVAL;
    606 			goto bail;
    607 		}
    608 		crp->crp_mac=cse->tmp_mac;
    609 	}
    610 
    611 	/*
    612 	 * XXX there was a comment here which said that we went to
    613 	 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
    614 	 * XXX disabled on NetBSD since 1.6O due to a race condition.
    615 	 * XXX But crypto_dispatch went to splcrypto() itself!  (And
    616 	 * XXX now takes the crypto_mtx mutex itself).  We do, however,
    617 	 * XXX need to hold the mutex across the call to cv_wait().
    618 	 * XXX     (should we arrange for crypto_dispatch to return to
    619 	 * XXX      us with it held?  it seems quite ugly to do so.)
    620 	 */
    621 #ifdef notyet
    622 eagain:
    623 #endif
    624 	error = crypto_dispatch(crp);
    625 	mutex_enter(&crypto_mtx);
    626 
    627 	/*
    628 	 * If the request was going to be completed by the
    629 	 * ioctl thread then it would have been done by now.
    630 	 * Remove the F_USER flag so crypto_done() is not confused
    631 	 * if the crypto device calls it after this point.
    632 	 */
    633 	crp->crp_flags &= ~(CRYPTO_F_USER);
    634 
    635 	switch (error) {
    636 #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */
    637 	case EAGAIN:
    638 		mutex_exit(&crypto_mtx);
    639 		goto eagain;
    640 		break;
    641 #endif
    642 	case 0:
    643 		break;
    644 	default:
    645 		DPRINTF(("cryptodev_op: not waiting, error.\n"));
    646 		mutex_exit(&crypto_mtx);
    647 		goto bail;
    648 	}
    649 
    650 	while (!(crp->crp_flags & CRYPTO_F_DONE)) {
    651 		DPRINTF(("cryptodev_op[%d]: sleeping on cv %p for crp %p\n",
    652 			(uint32_t)cse->sid, &crp->crp_cv, crp));
    653 		cv_wait(&crp->crp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    654 	}
    655 	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
    656 		/* XXX this should never happen now with the CRYPTO_F_USER flag
    657 		 * changes.
    658 		 */
    659 		DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
    660 		(void)crypto_ret_q_remove(crp);
    661 	}
    662 	mutex_exit(&crypto_mtx);
    663 
    664 	if (crp->crp_etype != 0) {
    665 		DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
    666 		error = crp->crp_etype;
    667 		goto bail;
    668 	}
    669 
    670 	if (cse->error) {
    671 		DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
    672 		error = cse->error;
    673 		goto bail;
    674 	}
    675 
    676 	dst_len = crp->crp_ilen;
    677 	/* let the user know how much data was returned */
    678 	if (crp->crp_olen) {
    679 		dst_len = cop->dst_len = crp->crp_olen;
    680 	}
    681 	crp->len = dst_len;
    682 
    683 	if (cop->dst) {
    684 		DPRINTF(("cryptodev_op: copyout %d bytes to %p\n", dst_len, cop->dst));
    685 	}
    686 	if (cop->dst &&
    687 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, dst_len)))
    688 	{
    689 		DPRINTF(("cryptodev_op: copyout error %d\n", error));
    690 		goto bail;
    691 	}
    692 
    693 	if (cop->mac &&
    694 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
    695 		DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
    696 		goto bail;
    697 	}
    698 
    699 
    700 bail:
    701 	if (crp) {
    702 		crypto_freereq(crp);
    703 	}
    704 	if (cse->uio.uio_iov[0].iov_base) {
    705 		kmem_free(cse->uio.uio_iov[0].iov_base,iov_len);
    706 	}
    707 
    708 	return error;
    709 }
    710 
    711 static int
    712 cryptodev_cb(void *op)
    713 {
    714 	struct cryptop *crp = (struct cryptop *) op;
    715 	struct csession *cse = (struct csession *)crp->crp_opaque;
    716 	int error = 0;
    717 
    718 	mutex_enter(&crypto_mtx);
    719 	cse->error = crp->crp_etype;
    720 	if (crp->crp_etype == EAGAIN) {
    721 		/* always drop mutex to call dispatch routine */
    722 		mutex_exit(&crypto_mtx);
    723 		error = crypto_dispatch(crp);
    724 		mutex_enter(&crypto_mtx);
    725 	}
    726 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    727 		cv_signal(&crp->crp_cv);
    728 	}
    729 	mutex_exit(&crypto_mtx);
    730 	return 0;
    731 }
    732 
    733 static int
    734 cryptodev_mcb(void *op)
    735 {
    736 	struct cryptop *crp = (struct cryptop *) op;
    737 	struct csession *cse = (struct csession *)crp->crp_opaque;
    738 	int  error=0;
    739 
    740 	mutex_enter(&crypto_mtx);
    741 	cse->error = crp->crp_etype;
    742 	if (crp->crp_etype == EAGAIN) {
    743 		mutex_exit(&crypto_mtx);
    744 		error = crypto_dispatch(crp);
    745 		mutex_enter(&crypto_mtx);
    746 	}
    747 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    748 		cv_signal(&crp->crp_cv);
    749 	}
    750 
    751 	TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
    752 	selnotify(&crp->fcrp->sinfo, 0, 0);
    753 	mutex_exit(&crypto_mtx);
    754 	return 0;
    755 }
    756 
    757 static int
    758 cryptodevkey_cb(void *op)
    759 {
    760 	struct cryptkop *krp = op;
    761 
    762 	mutex_enter(&crypto_mtx);
    763 	cv_signal(&krp->krp_cv);
    764 	mutex_exit(&crypto_mtx);
    765 	return 0;
    766 }
    767 
    768 static int
    769 cryptodevkey_mcb(void *op)
    770 {
    771 	struct cryptkop *krp = op;
    772 
    773 	mutex_enter(&crypto_mtx);
    774 	cv_signal(&krp->krp_cv);
    775 	TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
    776 	selnotify(&krp->fcrp->sinfo, 0, 0);
    777 	mutex_exit(&crypto_mtx);
    778 	return 0;
    779 }
    780 
    781 static int
    782 cryptodev_key(struct crypt_kop *kop)
    783 {
    784 	struct cryptkop *krp = NULL;
    785 	int error = EINVAL;
    786 	int in, out, size, i;
    787 
    788 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
    789 		return EFBIG;
    790 
    791 	in = kop->crk_iparams;
    792 	out = kop->crk_oparams;
    793 	switch (kop->crk_op) {
    794 	case CRK_MOD_EXP:
    795 		if (in == 3 && out == 1)
    796 			break;
    797 		return EINVAL;
    798 	case CRK_MOD_EXP_CRT:
    799 		if (in == 6 && out == 1)
    800 			break;
    801 		return EINVAL;
    802 	case CRK_DSA_SIGN:
    803 		if (in == 5 && out == 2)
    804 			break;
    805 		return EINVAL;
    806 	case CRK_DSA_VERIFY:
    807 		if (in == 7 && out == 0)
    808 			break;
    809 		return EINVAL;
    810 	case CRK_DH_COMPUTE_KEY:
    811 		if (in == 3 && out == 1)
    812 			break;
    813 		return EINVAL;
    814 	case CRK_MOD_ADD:
    815 		if (in == 3 && out == 1)
    816 			break;
    817 		return EINVAL;
    818 	case CRK_MOD_ADDINV:
    819 		if (in == 2 && out == 1)
    820 			break;
    821 		return EINVAL;
    822 	case CRK_MOD_SUB:
    823 		if (in == 3 && out == 1)
    824 			break;
    825 		return EINVAL;
    826 	case CRK_MOD_MULT:
    827 		if (in == 3 && out == 1)
    828 			break;
    829 		return EINVAL;
    830 	case CRK_MOD_MULTINV:
    831 		if (in == 2 && out == 1)
    832 			break;
    833 		return EINVAL;
    834 	case CRK_MOD:
    835 		if (in == 2 && out == 1)
    836 			break;
    837 		return EINVAL;
    838 	default:
    839 		return EINVAL;
    840 	}
    841 
    842 	krp = pool_get(&cryptkop_pool, PR_WAITOK);
    843 	(void)memset(krp, 0, sizeof *krp);
    844 	cv_init(&krp->krp_cv, "crykdev");
    845 	krp->krp_op = kop->crk_op;
    846 	krp->krp_status = kop->crk_status;
    847 	krp->krp_iparams = kop->crk_iparams;
    848 	krp->krp_oparams = kop->crk_oparams;
    849 	krp->krp_status = 0;
    850 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    851 
    852 	for (i = 0; i < CRK_MAXPARAM; i++)
    853 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    854 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    855 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    856 		if (size == 0)
    857 			continue;
    858 		krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
    859 		if (i >= krp->krp_iparams)
    860 			continue;
    861 		error = copyin(kop->crk_param[i].crp_p,
    862 		    krp->krp_param[i].crp_p, size);
    863 		if (error)
    864 			goto fail;
    865 	}
    866 
    867 	error = crypto_kdispatch(krp);
    868 	if (error != 0) {
    869 		goto fail;
    870 	}
    871 
    872 	mutex_enter(&crypto_mtx);
    873 	while (!(krp->krp_flags & CRYPTO_F_DONE)) {
    874 		cv_wait(&krp->krp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    875 	}
    876 	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
    877 		DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
    878 		(void)crypto_ret_kq_remove(krp);
    879 	}
    880 	mutex_exit(&crypto_mtx);
    881 
    882 	if (krp->krp_status != 0) {
    883 		DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n",
    884 		    krp->krp_status));
    885 		error = krp->krp_status;
    886 		goto fail;
    887 	}
    888 
    889 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
    890 	    i++) {
    891 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    892 		if (size == 0)
    893 			continue;
    894 		error = copyout(krp->krp_param[i].crp_p,
    895 		    kop->crk_param[i].crp_p, size);
    896 		if (error) {
    897 			DPRINTF(("cryptodev_key: copyout oparam %d failed, "
    898 			    "error=%d\n", i-krp->krp_iparams, error));
    899 			goto fail;
    900 		}
    901 	}
    902 
    903 fail:
    904 	kop->crk_status = krp->krp_status;
    905 	for (i = 0; i < CRK_MAXPARAM; i++) {
    906 		struct crparam *kp = &(krp->krp_param[i]);
    907 		if (krp->krp_param[i].crp_p) {
    908 			size = (kp->crp_nbits + 7)  / 8;
    909 			KASSERT(size > 0);
    910 			(void)memset(kp->crp_p, 0, size);
    911 			kmem_free(kp->crp_p, size);
    912 		}
    913 	}
    914 	cv_destroy(&krp->krp_cv);
    915 	pool_put(&cryptkop_pool, krp);
    916 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
    917 	return error;
    918 }
    919 
    920 /* ARGSUSED */
    921 static int
    922 cryptof_close(struct file *fp)
    923 {
    924 	struct fcrypt *fcr = fp->f_data;
    925 	struct csession *cse;
    926 
    927 	mutex_enter(&crypto_mtx);
    928 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    929 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    930 		mutex_exit(&crypto_mtx);
    931 		(void)csefree(cse);
    932 		mutex_enter(&crypto_mtx);
    933 	}
    934 	seldestroy(&fcr->sinfo);
    935 	fp->f_data = NULL;
    936 	mutex_exit(&crypto_mtx);
    937 
    938 	pool_put(&fcrpl, fcr);
    939 	return 0;
    940 }
    941 
    942 /* needed for compatibility module */
    943 struct	csession *cryptodev_csefind(struct fcrypt *fcr, u_int ses)
    944 {
    945 	return csefind(fcr, ses);
    946 }
    947 
    948 /* csefind: call with crypto_mtx held. */
    949 static struct csession *
    950 csefind(struct fcrypt *fcr, u_int ses)
    951 {
    952 	struct csession *cse, *cnext, *ret = NULL;
    953 
    954 	KASSERT(mutex_owned(&crypto_mtx));
    955 	TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext)
    956 		if (cse->ses == ses)
    957 			ret = cse;
    958 
    959 	return ret;
    960 }
    961 
    962 /* csedelete: call with crypto_mtx held. */
    963 static int
    964 csedelete(struct fcrypt *fcr, struct csession *cse_del)
    965 {
    966 	struct csession *cse, *cnext;
    967 	int ret = 0;
    968 
    969 	KASSERT(mutex_owned(&crypto_mtx));
    970 	TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext) {
    971 		if (cse == cse_del) {
    972 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    973 			ret = 1;
    974 		}
    975 	}
    976 	return ret;
    977 }
    978 
    979 static struct csession *
    980 cseadd(struct fcrypt *fcr, struct csession *cse)
    981 {
    982 	mutex_enter(&crypto_mtx);
    983 	/* don't let session ID wrap! */
    984 	if (fcr->sesn + 1 == 0) return NULL;
    985 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    986 	cse->ses = fcr->sesn++;
    987 	mutex_exit(&crypto_mtx);
    988 	return cse;
    989 }
    990 
    991 static struct csession *
    992 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
    993     void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    994     u_int32_t comp_alg, const struct enc_xform *txform,
    995     const struct auth_hash *thash, const struct comp_algo *tcomp)
    996 {
    997 	struct csession *cse;
    998 
    999 	cse = pool_get(&csepl, PR_NOWAIT);
   1000 	if (cse == NULL)
   1001 		return NULL;
   1002 	cse->key = key;
   1003 	cse->keylen = keylen/8;
   1004 	cse->mackey = mackey;
   1005 	cse->mackeylen = mackeylen/8;
   1006 	cse->sid = sid;
   1007 	cse->cipher = cipher;
   1008 	cse->mac = mac;
   1009 	cse->comp_alg = comp_alg;
   1010 	cse->txform = txform;
   1011 	cse->thash = thash;
   1012 	cse->tcomp = tcomp;
   1013 	cse->error = 0;
   1014 	if (cseadd(fcr, cse))
   1015 		return cse;
   1016 	else {
   1017 		pool_put(&csepl, cse);
   1018 		return NULL;
   1019 	}
   1020 }
   1021 
   1022 /* csefree: call with crypto_mtx held. */
   1023 static int
   1024 csefree(struct csession *cse)
   1025 {
   1026 	int error;
   1027 
   1028 	error = crypto_freesession(cse->sid);
   1029 	if (cse->key)
   1030 		free(cse->key, M_XDATA);
   1031 	if (cse->mackey)
   1032 		free(cse->mackey, M_XDATA);
   1033 	pool_put(&csepl, cse);
   1034 	return error;
   1035 }
   1036 
   1037 static int
   1038 cryptoopen(dev_t dev, int flag, int mode,
   1039     struct lwp *l)
   1040 {
   1041 	file_t *fp;
   1042         struct fcrypt *fcr;
   1043         int fd, error;
   1044 
   1045 	if (crypto_usercrypto == 0)
   1046 		return ENXIO;
   1047 
   1048 	if ((error = fd_allocfile(&fp, &fd)) != 0)
   1049 		return error;
   1050 
   1051 	fcr = pool_get(&fcrpl, PR_WAITOK);
   1052 	getnanotime(&fcr->btime);
   1053 	fcr->atime = fcr->mtime = fcr->btime;
   1054 	mutex_enter(&crypto_mtx);
   1055 	TAILQ_INIT(&fcr->csessions);
   1056 	TAILQ_INIT(&fcr->crp_ret_mq);
   1057 	TAILQ_INIT(&fcr->crp_ret_mkq);
   1058 	selinit(&fcr->sinfo);
   1059 	/*
   1060 	 * Don't ever return session 0, to allow detection of
   1061 	 * failed creation attempts with multi-create ioctl.
   1062 	 */
   1063 	fcr->sesn = 1;
   1064 	fcr->requestid = 1;
   1065 	mutex_exit(&crypto_mtx);
   1066 	return fd_clone(fp, fd, flag, &cryptofops, fcr);
   1067 }
   1068 
   1069 static int
   1070 cryptoread(dev_t dev, struct uio *uio, int ioflag)
   1071 {
   1072 	return EIO;
   1073 }
   1074 
   1075 static int
   1076 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
   1077 {
   1078 	return EIO;
   1079 }
   1080 
   1081 int
   1082 cryptoselect(dev_t dev, int rw, struct lwp *l)
   1083 {
   1084 	return 0;
   1085 }
   1086 
   1087 /*static*/
   1088 struct cdevsw crypto_cdevsw = {
   1089 	/* open */	cryptoopen,
   1090 	/* close */	noclose,
   1091 	/* read */	cryptoread,
   1092 	/* write */	cryptowrite,
   1093 	/* ioctl */	noioctl,
   1094 	/* ttstop?*/	nostop,
   1095 	/* ??*/		notty,
   1096 	/* poll */	cryptoselect /*nopoll*/,
   1097 	/* mmap */	nommap,
   1098 	/* kqfilter */	nokqfilter,
   1099 	/* type */	D_OTHER,
   1100 };
   1101 
   1102 int
   1103 cryptodev_mop(struct fcrypt *fcr,
   1104               struct crypt_n_op * cnop,
   1105               int count, struct lwp *l)
   1106 {
   1107 	struct cryptop *crp = NULL;
   1108 	struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
   1109 	int req, error=0;
   1110 	struct csession *cse;
   1111 	int flags=0;
   1112 	int iov_len;
   1113 
   1114 	for (req = 0; req < count; req++) {
   1115 		mutex_enter(&crypto_mtx);
   1116 		cse = csefind(fcr, cnop[req].ses);
   1117 		if (cse == NULL) {
   1118 			DPRINTF(("csefind failed\n"));
   1119 			cnop[req].status = EINVAL;
   1120 			mutex_exit(&crypto_mtx);
   1121 			continue;
   1122 		}
   1123 		mutex_exit(&crypto_mtx);
   1124 
   1125 		if (cnop[req].len > 256*1024-4) {
   1126 			DPRINTF(("length failed\n"));
   1127 			cnop[req].status = EINVAL;
   1128 			continue;
   1129 		}
   1130 		if (cse->txform) {
   1131 			if (cnop[req].len < cse->txform->blocksize -
   1132 			    (cnop[req].iv ? 0 : cse->txform->ivsize) ||
   1133 			    (cnop[req].len -
   1134 			     (cnop[req].iv ? 0 : cse->txform->ivsize))
   1135 			    % cse->txform->blocksize) {
   1136 				cnop[req].status = EINVAL;
   1137 				continue;
   1138 			}
   1139 		}
   1140 
   1141 		crp = crypto_getreq((cse->txform != NULL) +
   1142 				    (cse->thash != NULL) +
   1143 				    (cse->tcomp != NULL));
   1144 		if (crp == NULL) {
   1145 			cnop[req].status = ENOMEM;
   1146 			goto bail;
   1147 		}
   1148 
   1149 		iov_len = cnop[req].len;
   1150 		/* got a compression/decompression max size? */
   1151 		if ((cse->tcomp) && cnop[req].dst_len) {
   1152 			if (iov_len < cnop[req].dst_len) {
   1153 				/* Need larger iov to deal with decompress */
   1154 				iov_len = cnop[req].dst_len;
   1155 			}
   1156 			DPRINTF(("cryptodev_mop: iov_len -> %d for decompress\n", iov_len));
   1157 		}
   1158 
   1159 		(void)memset(&crp->uio, 0, sizeof(crp->uio));
   1160 		crp->uio.uio_iovcnt = 1;
   1161 		crp->uio.uio_resid = 0;
   1162 		crp->uio.uio_rw = UIO_WRITE;
   1163 		crp->uio.uio_iov = crp->iovec;
   1164 		UIO_SETUP_SYSSPACE(&crp->uio);
   1165 		memset(&crp->iovec, 0, sizeof(crp->iovec));
   1166 		crp->uio.uio_iov[0].iov_len = iov_len;
   1167 		DPRINTF(("cryptodev_mop: kmem_alloc(%d) for iov \n", iov_len));
   1168 		crp->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
   1169 		crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
   1170 
   1171 		if (cse->tcomp) {
   1172 			crdc = crp->crp_desc;
   1173 		}
   1174 
   1175 		if (cse->thash) {
   1176 			crda = crdc ? crdc->crd_next : crp->crp_desc;
   1177 			if (cse->txform && crda)
   1178 				crde = crda->crd_next;
   1179 		} else {
   1180 			if (cse->txform) {
   1181 				crde = crdc ? crdc->crd_next : crp->crp_desc;
   1182 			} else if (!cse->tcomp) {
   1183 				error = EINVAL;
   1184 				goto bail;
   1185 			}
   1186 		}
   1187 
   1188 		if ((copyin(cnop[req].src,
   1189 		    crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
   1190 			cnop[req].status = EINVAL;
   1191 			goto bail;
   1192 		}
   1193 
   1194 		if (crdc) {
   1195 			switch (cnop[req].op) {
   1196 			case COP_COMP:
   1197 				crdc->crd_flags |= CRD_F_COMP;
   1198 				break;
   1199 			case COP_DECOMP:
   1200 				crdc->crd_flags &= ~CRD_F_COMP;
   1201 				break;
   1202 			default:
   1203 				break;
   1204 			}
   1205 			/* more data to follow? */
   1206 			if (cnop[req].flags & COP_F_MORE) {
   1207 				flags |= CRYPTO_F_MORE;
   1208 			}
   1209 			crdc->crd_len = cnop[req].len;
   1210 			crdc->crd_inject = 0;
   1211 
   1212 			crdc->crd_alg = cse->comp_alg;
   1213 			crdc->crd_key = NULL;
   1214 			crdc->crd_klen = 0;
   1215 			DPRINTF(("cryptodev_mop[%d]: crdc setup for comp_alg %d"
   1216 				 " len %d.\n",
   1217 				(uint32_t)cse->sid, crdc->crd_alg,
   1218 				crdc->crd_len));
   1219 		}
   1220 
   1221 		if (crda) {
   1222 			crda->crd_skip = 0;
   1223 			crda->crd_len = cnop[req].len;
   1224 			crda->crd_inject = 0;	/* ??? */
   1225 
   1226 			crda->crd_alg = cse->mac;
   1227 			crda->crd_key = cse->mackey;
   1228 			crda->crd_klen = cse->mackeylen * 8;
   1229 		}
   1230 
   1231 		if (crde) {
   1232 			if (cnop[req].op == COP_ENCRYPT)
   1233 				crde->crd_flags |= CRD_F_ENCRYPT;
   1234 			else
   1235 				crde->crd_flags &= ~CRD_F_ENCRYPT;
   1236 			crde->crd_len = cnop[req].len;
   1237 			crde->crd_inject = 0;
   1238 
   1239 			crde->crd_alg = cse->cipher;
   1240 #ifdef notyet		/* XXX must notify h/w driver new key, drain */
   1241 			if(cnop[req].key && cnop[req].keylen) {
   1242 				crde->crd_key = malloc(cnop[req].keylen,
   1243 						    M_XDATA, M_WAITOK);
   1244 				if((error = copyin(cnop[req].key,
   1245 				    crde->crd_key, cnop[req].keylen))) {
   1246 					cnop[req].status = EINVAL;
   1247 					goto bail;
   1248 				}
   1249 				crde->crd_klen =  cnop[req].keylen * 8;
   1250 			} else { ... }
   1251 #endif
   1252 			crde->crd_key = cse->key;
   1253 			crde->crd_klen = cse->keylen * 8;
   1254 		}
   1255 
   1256 		crp->crp_ilen = cnop[req].len;
   1257 		crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM |
   1258 		    (cnop[req].flags & COP_F_BATCH) | flags;
   1259 		crp->crp_buf = (void *)&crp->uio;
   1260 		crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
   1261 		crp->crp_sid = cse->sid;
   1262 		crp->crp_opaque = (void *)cse;
   1263 		crp->fcrp = fcr;
   1264 		crp->dst = cnop[req].dst;
   1265 		crp->len = cnop[req].len; /* input len, iov may be larger */
   1266 		crp->mac = cnop[req].mac;
   1267 		DPRINTF(("cryptodev_mop: iov_base %p dst %p len %d mac %p\n",
   1268 			    crp->uio.uio_iov[0].iov_base, crp->dst, crp->len,
   1269 			    crp->mac));
   1270 
   1271 		if (cnop[req].iv) {
   1272 			if (crde == NULL) {
   1273 				cnop[req].status = EINVAL;
   1274 				goto bail;
   1275 			}
   1276 			if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
   1277 				cnop[req].status = EINVAL;
   1278 				goto bail;
   1279 			}
   1280 			if ((error = copyin(cnop[req].iv, crp->tmp_iv,
   1281 			    cse->txform->ivsize))) {
   1282 				cnop[req].status = EINVAL;
   1283 				goto bail;
   1284 			}
   1285 			(void)memcpy(crde->crd_iv, crp->tmp_iv,
   1286 			    cse->txform->ivsize);
   1287 			crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
   1288 			crde->crd_skip = 0;
   1289 		} else if (crde) {
   1290 			if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
   1291 				crde->crd_skip = 0;
   1292 			} else {
   1293 				crde->crd_flags |= CRD_F_IV_PRESENT;
   1294 				crde->crd_skip = cse->txform->ivsize;
   1295 				crde->crd_len -= cse->txform->ivsize;
   1296 			}
   1297 		}
   1298 
   1299 		if (cnop[req].mac) {
   1300 			if (crda == NULL) {
   1301 				cnop[req].status = EINVAL;
   1302 				goto bail;
   1303 			}
   1304 			crp->crp_mac=cse->tmp_mac;
   1305 		}
   1306 		cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
   1307 		crp->crp_reqid = cnop[req].reqid;
   1308 		crp->crp_usropaque = cnop[req].opaque;
   1309 #ifdef notyet
   1310 eagain:
   1311 #endif
   1312 		cnop[req].status = crypto_dispatch(crp);
   1313 		mutex_enter(&crypto_mtx);	/* XXX why mutex? */
   1314 
   1315 		switch (cnop[req].status) {
   1316 #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */
   1317 		case EAGAIN:
   1318 			mutex_exit(&crypto_mtx);
   1319 			goto eagain;
   1320 			break;
   1321 #endif
   1322 		case 0:
   1323 			break;
   1324 		default:
   1325 			DPRINTF(("cryptodev_op: not waiting, error.\n"));
   1326 			mutex_exit(&crypto_mtx);
   1327 			goto bail;
   1328 		}
   1329 
   1330 		mutex_exit(&crypto_mtx);
   1331 bail:
   1332 		if (cnop[req].status) {
   1333 			if (crp) {
   1334 				if (crp->uio.uio_iov[0].iov_base) {
   1335 					kmem_free(crp->uio.uio_iov[0].iov_base,
   1336 					    crp->uio.uio_iov[0].iov_len);
   1337 				}
   1338 				crypto_freereq(crp);
   1339 			}
   1340 			error = 0;
   1341 		}
   1342 	}
   1343 	return error;
   1344 }
   1345 
   1346 static int
   1347 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
   1348 {
   1349 	struct cryptkop *krp = NULL;
   1350 	int error = EINVAL;
   1351 	int in, out, size, i, req;
   1352 
   1353 	for (req = 0; req < count; req++) {
   1354 		if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
   1355 			return EFBIG;
   1356 
   1357 		in = kop[req].crk_iparams;
   1358 		out = kop[req].crk_oparams;
   1359 		switch (kop[req].crk_op) {
   1360 		case CRK_MOD_EXP:
   1361 			if (in == 3 && out == 1)
   1362 				break;
   1363 			kop[req].crk_status = EINVAL;
   1364 			continue;
   1365 		case CRK_MOD_EXP_CRT:
   1366 			if (in == 6 && out == 1)
   1367 				break;
   1368 			kop[req].crk_status = EINVAL;
   1369 			continue;
   1370 		case CRK_DSA_SIGN:
   1371 			if (in == 5 && out == 2)
   1372 				break;
   1373 			kop[req].crk_status = EINVAL;
   1374 			continue;
   1375 		case CRK_DSA_VERIFY:
   1376 			if (in == 7 && out == 0)
   1377 				break;
   1378 			kop[req].crk_status = EINVAL;
   1379 			continue;
   1380 		case CRK_DH_COMPUTE_KEY:
   1381 			if (in == 3 && out == 1)
   1382 				break;
   1383 			kop[req].crk_status = EINVAL;
   1384 			continue;
   1385 		case CRK_MOD_ADD:
   1386 			if (in == 3 && out == 1)
   1387 				break;
   1388 			kop[req].crk_status = EINVAL;
   1389 			continue;
   1390 		case CRK_MOD_ADDINV:
   1391 			if (in == 2 && out == 1)
   1392 				break;
   1393 			kop[req].crk_status = EINVAL;
   1394 			continue;
   1395 		case CRK_MOD_SUB:
   1396 			if (in == 3 && out == 1)
   1397 				break;
   1398 			kop[req].crk_status = EINVAL;
   1399 			continue;
   1400 		case CRK_MOD_MULT:
   1401 			if (in == 3 && out == 1)
   1402 				break;
   1403 			kop[req].crk_status = EINVAL;
   1404 			continue;
   1405 		case CRK_MOD_MULTINV:
   1406 			if (in == 2 && out == 1)
   1407 				break;
   1408 			kop[req].crk_status = EINVAL;
   1409 			continue;
   1410 		case CRK_MOD:
   1411 			if (in == 2 && out == 1)
   1412 				break;
   1413 			kop[req].crk_status = EINVAL;
   1414 			continue;
   1415 		default:
   1416 			kop[req].crk_status = EINVAL;
   1417 			continue;
   1418 		}
   1419 
   1420 		krp = pool_get(&cryptkop_pool, PR_WAITOK);
   1421 		(void)memset(krp, 0, sizeof *krp);
   1422 		cv_init(&krp->krp_cv, "crykdev");
   1423 		krp->krp_op = kop[req].crk_op;
   1424 		krp->krp_status = kop[req].crk_status;
   1425 		krp->krp_iparams = kop[req].crk_iparams;
   1426 		krp->krp_oparams = kop[req].crk_oparams;
   1427 		krp->krp_status = 0;
   1428 		krp->krp_callback =
   1429 		    (int (*) (struct cryptkop *)) cryptodevkey_mcb;
   1430 		(void)memcpy(krp->crk_param, kop[req].crk_param,
   1431 		    sizeof(kop[req].crk_param));
   1432 
   1433 		krp->krp_flags = CRYPTO_F_CBIMM;
   1434 
   1435 		for (i = 0; i < CRK_MAXPARAM; i++)
   1436 			krp->krp_param[i].crp_nbits =
   1437 			    kop[req].crk_param[i].crp_nbits;
   1438 		for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
   1439 			size = (krp->krp_param[i].crp_nbits + 7) / 8;
   1440 			if (size == 0)
   1441 				continue;
   1442 			krp->krp_param[i].crp_p =
   1443 			    kmem_alloc(size, KM_SLEEP);
   1444 			if (i >= krp->krp_iparams)
   1445 				continue;
   1446 			kop[req].crk_status =
   1447 			    copyin(kop[req].crk_param[i].crp_p,
   1448 			    krp->krp_param[i].crp_p, size);
   1449 			if (kop[req].crk_status)
   1450 				goto fail;
   1451 		}
   1452 		krp->fcrp = fcr;
   1453 
   1454 		kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
   1455 		krp->krp_reqid = kop[req].crk_reqid;
   1456 		krp->krp_usropaque = kop[req].crk_opaque;
   1457 
   1458 		kop[req].crk_status = crypto_kdispatch(krp);
   1459 		if (kop[req].crk_status != 0) {
   1460 			goto fail;
   1461 		}
   1462 
   1463 fail:
   1464 		if(kop[req].crk_status) {
   1465 			if (krp) {
   1466 				kop[req].crk_status = krp->krp_status;
   1467 				for (i = 0; i < CRK_MAXPARAM; i++) {
   1468 					struct crparam *kp =
   1469 						&(krp->krp_param[i]);
   1470 					if (kp->crp_p) {
   1471 						size = (kp->crp_nbits + 7) / 8;
   1472 						KASSERT(size > 0);
   1473 						memset(kp->crp_p, 0, size);
   1474 						kmem_free(kp->crp_p, size);
   1475 					}
   1476 				}
   1477 				cv_destroy(&krp->krp_cv);
   1478 				pool_put(&cryptkop_pool, krp);
   1479 			}
   1480 		}
   1481 		error = 0;
   1482 	}
   1483 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
   1484 	return error;
   1485 }
   1486 
   1487 int
   1488 cryptodev_session(struct fcrypt *fcr, struct session_op *sop)
   1489 {
   1490 	struct cryptoini cria, crie;
   1491 	struct cryptoini cric;		/* compressor */
   1492 	struct cryptoini *crihead = NULL;
   1493 	const struct enc_xform *txform = NULL;
   1494 	const struct auth_hash *thash = NULL;
   1495 	const struct comp_algo *tcomp = NULL;
   1496 	struct csession *cse;
   1497 	u_int64_t sid;
   1498 	int error = 0;
   1499 
   1500 	DPRINTF(("cryptodev_session() cipher=%d, mac=%d\n", sop->cipher, sop->mac));
   1501 
   1502 	/* XXX there must be a way to not embed the list of xforms here */
   1503 	switch (sop->cipher) {
   1504 	case 0:
   1505 		break;
   1506 	case CRYPTO_DES_CBC:
   1507 		txform = &enc_xform_des;
   1508 		break;
   1509 	case CRYPTO_3DES_CBC:
   1510 		txform = &enc_xform_3des;
   1511 		break;
   1512 	case CRYPTO_BLF_CBC:
   1513 		txform = &enc_xform_blf;
   1514 		break;
   1515 	case CRYPTO_CAST_CBC:
   1516 		txform = &enc_xform_cast5;
   1517 		break;
   1518 	case CRYPTO_SKIPJACK_CBC:
   1519 		txform = &enc_xform_skipjack;
   1520 		break;
   1521 	case CRYPTO_AES_CBC:
   1522 		txform = &enc_xform_rijndael128;
   1523 		break;
   1524 	case CRYPTO_CAMELLIA_CBC:
   1525 		txform = &enc_xform_camellia;
   1526 		break;
   1527 	case CRYPTO_AES_CTR:
   1528 		txform = &enc_xform_aes_ctr;
   1529 		break;
   1530 	case CRYPTO_NULL_CBC:
   1531 		txform = &enc_xform_null;
   1532 		break;
   1533 	case CRYPTO_ARC4:
   1534 		txform = &enc_xform_arc4;
   1535 		break;
   1536 	default:
   1537 		DPRINTF(("Invalid cipher %d\n", sop->cipher));
   1538 		return EINVAL;
   1539 	}
   1540 
   1541 	switch (sop->comp_alg) {
   1542 	case 0:
   1543 		break;
   1544 	case CRYPTO_DEFLATE_COMP:
   1545 		tcomp = &comp_algo_deflate;
   1546 		break;
   1547 	case CRYPTO_GZIP_COMP:
   1548 		tcomp = &comp_algo_gzip;
   1549 		DPRINTF(("cryptodev_session() tcomp for GZIP\n"));
   1550 		break;
   1551 	default:
   1552 		DPRINTF(("Invalid compression alg %d\n", sop->comp_alg));
   1553 		return EINVAL;
   1554 	}
   1555 
   1556 	switch (sop->mac) {
   1557 	case 0:
   1558 		break;
   1559 	case CRYPTO_MD5_HMAC:
   1560 		thash = &auth_hash_hmac_md5;
   1561 		break;
   1562 	case CRYPTO_SHA1_HMAC:
   1563 		thash = &auth_hash_hmac_sha1;
   1564 		break;
   1565 	case CRYPTO_MD5_HMAC_96:
   1566 		thash = &auth_hash_hmac_md5_96;
   1567 		break;
   1568 	case CRYPTO_SHA1_HMAC_96:
   1569 		thash = &auth_hash_hmac_sha1_96;
   1570 		break;
   1571 	case CRYPTO_SHA2_HMAC:
   1572 		/* XXX switching on key length seems questionable */
   1573 		if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
   1574 			thash = &auth_hash_hmac_sha2_256;
   1575 		} else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
   1576 			thash = &auth_hash_hmac_sha2_384;
   1577 		} else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
   1578 			thash = &auth_hash_hmac_sha2_512;
   1579 		} else {
   1580 			DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
   1581 			return EINVAL;
   1582 		}
   1583 		break;
   1584 	case CRYPTO_RIPEMD160_HMAC:
   1585 		thash = &auth_hash_hmac_ripemd_160;
   1586 		break;
   1587 	case CRYPTO_RIPEMD160_HMAC_96:
   1588 		thash = &auth_hash_hmac_ripemd_160_96;
   1589 		break;
   1590 	case CRYPTO_MD5:
   1591 		thash = &auth_hash_md5;
   1592 		break;
   1593 	case CRYPTO_SHA1:
   1594 		thash = &auth_hash_sha1;
   1595 		break;
   1596 	case CRYPTO_NULL_HMAC:
   1597 		thash = &auth_hash_null;
   1598 		break;
   1599 	default:
   1600 		DPRINTF(("Invalid mac %d\n", sop->mac));
   1601 		return EINVAL;
   1602 	}
   1603 
   1604 	memset(&crie, 0, sizeof(crie));
   1605 	memset(&cria, 0, sizeof(cria));
   1606 	memset(&cric, 0, sizeof(cric));
   1607 
   1608 	if (tcomp) {
   1609 		cric.cri_alg = tcomp->type;
   1610 		cric.cri_klen = 0;
   1611 		DPRINTF(("tcomp->type = %d\n", tcomp->type));
   1612 
   1613 		crihead = &cric;
   1614 		if (thash) {
   1615 			cric.cri_next = &cria;
   1616 		} else if (txform) {
   1617 			cric.cri_next = &crie;
   1618 		}
   1619 	}
   1620 
   1621 	if (txform) {
   1622 		crie.cri_alg = txform->type;
   1623 		crie.cri_klen = sop->keylen * 8;
   1624 		if (sop->keylen > txform->maxkey ||
   1625 		    sop->keylen < txform->minkey) {
   1626 			DPRINTF(("keylen %d not in [%d,%d]\n",
   1627 			    sop->keylen, txform->minkey, txform->maxkey));
   1628 			error = EINVAL;
   1629 			goto bail;
   1630 		}
   1631 
   1632 		crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
   1633 		if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
   1634 			goto bail;
   1635 		if (!crihead) {
   1636 			crihead = &crie;
   1637 		}
   1638 	}
   1639 
   1640 	if (thash) {
   1641 		cria.cri_alg = thash->type;
   1642 		cria.cri_klen = sop->mackeylen * 8;
   1643 		if (sop->mackeylen != thash->keysize) {
   1644 			DPRINTF(("mackeylen %d != keysize %d\n",
   1645 			    sop->mackeylen, thash->keysize));
   1646 			error = EINVAL;
   1647 			goto bail;
   1648 		}
   1649 		if (cria.cri_klen) {
   1650 			cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
   1651 			    M_WAITOK);
   1652 			if ((error = copyin(sop->mackey, cria.cri_key,
   1653 			    cria.cri_klen / 8))) {
   1654 				goto bail;
   1655 			}
   1656 		}
   1657 		if (txform)
   1658 			cria.cri_next = &crie;	/* XXX forces enc then hash? */
   1659 		if (!crihead) {
   1660 			crihead = &cria;
   1661 		}
   1662 	}
   1663 
   1664 	error = crypto_newsession(&sid, crihead, crypto_devallowsoft);
   1665 	if (!error) {
   1666 		DPRINTF(("cyrptodev_session: got session %d\n", (uint32_t)sid));
   1667 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
   1668 		    cria.cri_key, cria.cri_klen, (txform ? sop->cipher : 0), sop->mac,
   1669 		    (tcomp ? sop->comp_alg : 0), txform, thash, tcomp);
   1670 		if (cse != NULL) {
   1671 			sop->ses = cse->ses;
   1672 		} else {
   1673 			DPRINTF(("csecreate failed\n"));
   1674 			crypto_freesession(sid);
   1675 			error = EINVAL;
   1676 		}
   1677 	} else {
   1678 		DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
   1679 		    error));
   1680 	}
   1681 bail:
   1682 	if (error) {
   1683 		if (crie.cri_key) {
   1684 			memset(crie.cri_key, 0, crie.cri_klen / 8);
   1685 			free(crie.cri_key, M_XDATA);
   1686 		}
   1687 		if (cria.cri_key) {
   1688 			memset(cria.cri_key, 0, cria.cri_klen / 8);
   1689 			free(cria.cri_key, M_XDATA);
   1690 		}
   1691 	}
   1692 	return error;
   1693 }
   1694 
   1695 int
   1696 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
   1697 		   int count)
   1698 {
   1699 	int i;
   1700 
   1701 	for (i = 0; i < count; i++, sn_ops++) {
   1702 		struct session_op s_op;
   1703 		s_op.cipher =		sn_ops->cipher;
   1704 		s_op.mac =		sn_ops->mac;
   1705 		s_op.keylen =		sn_ops->keylen;
   1706 		s_op.key =		sn_ops->key;
   1707 		s_op.mackeylen =	sn_ops->mackeylen;
   1708 		s_op.mackey =		sn_ops->mackey;
   1709 
   1710 		sn_ops->status = cryptodev_session(fcr, &s_op);
   1711 		sn_ops->ses =		s_op.ses;
   1712 	}
   1713 
   1714 	return 0;
   1715 }
   1716 
   1717 static int
   1718 cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
   1719 {
   1720 	struct csession *cse;
   1721 	int req, error = 0;
   1722 
   1723 	mutex_enter(&crypto_mtx);
   1724 	for(req = 0; req < count; req++) {
   1725 		cse = csefind(fcr, sesid[req]);
   1726 		if (cse == NULL)
   1727 			continue;
   1728 		csedelete(fcr, cse);
   1729 		mutex_exit(&crypto_mtx);
   1730 		error = csefree(cse);
   1731 		mutex_enter(&crypto_mtx);
   1732 	}
   1733 	mutex_exit(&crypto_mtx);
   1734 	return 0;
   1735 }
   1736 
   1737 /*
   1738  * collect as many completed requests as are availble, or count completed
   1739  * requests whichever is less.
   1740  * return the number of requests.
   1741  */
   1742 static int
   1743 cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
   1744     int count)
   1745 {
   1746 	struct cryptop *crp = NULL;
   1747 	struct cryptkop *krp = NULL;
   1748 	struct csession *cse;
   1749 	int i, size, req = 0;
   1750 	int completed=0;
   1751 
   1752 	/* On queue so nobody else can grab them
   1753 	 * and copyout can be delayed-- no locking */
   1754 	TAILQ_HEAD(, cryptop) crp_delfree_q =
   1755 		TAILQ_HEAD_INITIALIZER(crp_delfree_q);
   1756 	TAILQ_HEAD(, cryptkop) krp_delfree_q =
   1757 		TAILQ_HEAD_INITIALIZER(krp_delfree_q);
   1758 
   1759 	/* at this point we do not know which response user is requesting for
   1760 	 * (symmetric or asymmetric) so we copyout one from each i.e if the
   1761 	 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
   1762 	 * if 3 then 2 symmetric and 1 asymmetric and so on */
   1763 
   1764 	/* pull off a list of requests while protected from changes */
   1765 	mutex_enter(&crypto_mtx);
   1766 	while (req < count) {
   1767 		crp = TAILQ_FIRST(&fcr->crp_ret_mq);
   1768 		if (crp) {
   1769 			TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
   1770 			TAILQ_INSERT_TAIL(&crp_delfree_q, crp, crp_next);
   1771 			cse = (struct csession *)crp->crp_opaque;
   1772 
   1773 			/* see if the session is still valid */
   1774 			cse = csefind(fcr, cse->ses);
   1775 			if (cse != NULL) {
   1776 				crypt_res[req].status = 0;
   1777 			} else {
   1778 				DPRINTF(("csefind failed\n"));
   1779 				crypt_res[req].status = EINVAL;
   1780 			}
   1781 			req++;
   1782 		}
   1783 		if(req < count) {
   1784 			crypt_res[req].status = 0;
   1785 			krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
   1786 			if (krp) {
   1787 				TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
   1788 				TAILQ_INSERT_TAIL(&krp_delfree_q, krp, krp_next);
   1789 			req++;
   1790 			}
   1791 		}
   1792 	}
   1793 	mutex_exit(&crypto_mtx);
   1794 
   1795 	/* now do all the work outside the mutex */
   1796 	for(req=0; req < count ;) {
   1797 		crp = TAILQ_FIRST(&crp_delfree_q);
   1798 		if (crp) {
   1799 			if (crypt_res[req].status != 0) {
   1800 				/* csefind failed during collection */
   1801 				goto bail;
   1802 			}
   1803 			cse = (struct csession *)crp->crp_opaque;
   1804 			crypt_res[req].reqid = crp->crp_reqid;
   1805 			crypt_res[req].opaque = crp->crp_usropaque;
   1806 			completed++;
   1807 
   1808 			if (crp->crp_etype != 0) {
   1809 				crypt_res[req].status = crp->crp_etype;
   1810 				goto bail;
   1811 			}
   1812 
   1813 			if (cse->error) {
   1814 				crypt_res[req].status = cse->error;
   1815 				goto bail;
   1816 			}
   1817 
   1818 			if (crp->dst && (crypt_res[req].status =
   1819 			    copyout(crp->uio.uio_iov[0].iov_base, crp->dst,
   1820 			    crp->len)))
   1821 				goto bail;
   1822 
   1823 			if (crp->mac && (crypt_res[req].status =
   1824 			    copyout(crp->crp_mac, crp->mac,
   1825 			    cse->thash->authsize)))
   1826 				goto bail;
   1827 
   1828 bail:
   1829 			TAILQ_REMOVE(&crp_delfree_q, crp, crp_next);
   1830 			kmem_free(crp->uio.uio_iov[0].iov_base,
   1831 			    crp->uio.uio_iov[0].iov_len);
   1832 			crypto_freereq(crp);
   1833 			req++;
   1834 		}
   1835 
   1836 		if (req < count) {
   1837 			krp = TAILQ_FIRST(&krp_delfree_q);
   1838 			if (krp) {
   1839 				crypt_res[req].reqid = krp->krp_reqid;
   1840 				crypt_res[req].opaque = krp->krp_usropaque;
   1841 				completed++;
   1842 				if (krp->krp_status != 0) {
   1843 					DPRINTF(("cryptodev_key: "
   1844 					    "krp->krp_status 0x%08x\n",
   1845 					    krp->krp_status));
   1846 					crypt_res[req].status = krp->krp_status;
   1847 					goto fail;
   1848 				}
   1849 
   1850 				for (i = krp->krp_iparams; i < krp->krp_iparams
   1851 				    + krp->krp_oparams; i++) {
   1852 					size = (krp->krp_param[i].crp_nbits
   1853 					    + 7) / 8;
   1854 					if (size == 0)
   1855 						continue;
   1856 					crypt_res[req].status = copyout
   1857 					    (krp->krp_param[i].crp_p,
   1858 					    krp->crk_param[i].crp_p, size);
   1859 					if (crypt_res[req].status) {
   1860 						DPRINTF(("cryptodev_key: "
   1861 						    "copyout oparam %d failed, "
   1862 						    "error=%d\n",
   1863 						    i - krp->krp_iparams,
   1864 						    crypt_res[req].status));
   1865 						goto fail;
   1866 					}
   1867 				}
   1868 fail:
   1869 				TAILQ_REMOVE(&krp_delfree_q, krp, krp_next);
   1870 				/* not sure what to do for this */
   1871 				/* kop[req].crk_status = krp->krp_status; */
   1872 				for (i = 0; i < CRK_MAXPARAM; i++) {
   1873 					struct crparam *kp = &(krp->krp_param[i]);
   1874 					if (kp->crp_p) {
   1875 						size = (kp->crp_nbits + 7) / 8;
   1876 						KASSERT(size > 0);
   1877 						(void)memset(kp->crp_p, 0, size);
   1878 						kmem_free(kp->crp_p, size);
   1879 					}
   1880 				}
   1881 				cv_destroy(&krp->krp_cv);
   1882 				pool_put(&cryptkop_pool, krp);
   1883 				req++;
   1884 			}
   1885 		}
   1886 	}
   1887 
   1888 	return completed;
   1889 }
   1890 
   1891 static int
   1892 cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
   1893 {
   1894         struct cryptop *crp = NULL, *cnext;
   1895         struct cryptkop *krp = NULL, *knext;
   1896         struct csession *cse;
   1897         int i, size, req = 0;
   1898 
   1899 	mutex_enter(&crypto_mtx);
   1900 	/* Here we dont know for which request the user is requesting the
   1901 	 * response so checking in both the queues */
   1902 	TAILQ_FOREACH_SAFE(crp, &fcr->crp_ret_mq, crp_next, cnext) {
   1903 		if(crp && (crp->crp_reqid == crypt_res->reqid)) {
   1904 			cse = (struct csession *)crp->crp_opaque;
   1905 		        crypt_res->opaque = crp->crp_usropaque;
   1906 			cse = csefind(fcr, cse->ses);
   1907 			if (cse == NULL) {
   1908 				DPRINTF(("csefind failed\n"));
   1909 				crypt_res->status = EINVAL;
   1910 				goto bail;
   1911 			}
   1912 
   1913 			if (crp->crp_etype != 0) {
   1914 				crypt_res->status = crp->crp_etype;
   1915 				goto bail;
   1916 			}
   1917 
   1918 			if (cse->error) {
   1919 				crypt_res->status = cse->error;
   1920 				goto bail;
   1921 			}
   1922 
   1923 			if (crp->dst && (crypt_res->status =
   1924 			    copyout(crp->uio.uio_iov[0].iov_base,
   1925 			    crp->dst, crp->len)))
   1926 				goto bail;
   1927 
   1928 			if (crp->mac && (crypt_res->status =
   1929 			    copyout(crp->crp_mac, crp->mac,
   1930 			    cse->thash->authsize)))
   1931 				goto bail;
   1932 bail:
   1933 			TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
   1934 
   1935 			mutex_exit(&crypto_mtx);
   1936 			crypto_freereq(crp);
   1937 			return 0;
   1938 		}
   1939 	}
   1940 
   1941 	TAILQ_FOREACH_SAFE(krp, &fcr->crp_ret_mkq, krp_next, knext) {
   1942 		if(krp && (krp->krp_reqid == crypt_res->reqid)) {
   1943 			crypt_res[req].opaque = krp->krp_usropaque;
   1944 			if (krp->krp_status != 0) {
   1945 				DPRINTF(("cryptodev_key: "
   1946 				    "krp->krp_status 0x%08x\n",
   1947 				    krp->krp_status));
   1948 				crypt_res[req].status = krp->krp_status;
   1949 				goto fail;
   1950 			}
   1951 
   1952 			for (i = krp->krp_iparams; i < krp->krp_iparams +
   1953 			    krp->krp_oparams; i++) {
   1954 				size = (krp->krp_param[i].crp_nbits + 7) / 8;
   1955 				if (size == 0)
   1956 					continue;
   1957 				crypt_res[req].status = copyout(
   1958 				    krp->krp_param[i].crp_p,
   1959 				    krp->crk_param[i].crp_p, size);
   1960 				if (crypt_res[req].status) {
   1961 					DPRINTF(("cryptodev_key: copyout oparam"
   1962 					    "%d failed, error=%d\n",
   1963 					    i - krp->krp_iparams,
   1964 					    crypt_res[req].status));
   1965 					goto fail;
   1966 				}
   1967 			}
   1968 fail:
   1969 			TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
   1970 			mutex_exit(&crypto_mtx);
   1971 			/* not sure what to do for this */
   1972 			/* kop[req].crk_status = krp->krp_status; */
   1973 			for (i = 0; i < CRK_MAXPARAM; i++) {
   1974 				struct crparam *kp = &(krp->krp_param[i]);
   1975 				if (kp->crp_p) {
   1976 					size = (kp->crp_nbits + 7) / 8;
   1977 					KASSERT(size > 0);
   1978 					memset(kp->crp_p, 0, size);
   1979 					kmem_free(kp->crp_p, size);
   1980 				}
   1981 			}
   1982 			cv_destroy(&krp->krp_cv);
   1983 			pool_put(&cryptkop_pool, krp);
   1984 			return 0;
   1985 		}
   1986 	}
   1987 	mutex_exit(&crypto_mtx);
   1988 	return EINPROGRESS;
   1989 }
   1990 
   1991 static int
   1992 cryptof_stat(struct file *fp, struct stat *st)
   1993 {
   1994 	struct fcrypt *fcr = fp->f_data;
   1995 
   1996 	(void)memset(st, 0, sizeof(st));
   1997 
   1998 	mutex_enter(&crypto_mtx);
   1999 	st->st_dev = makedev(cdevsw_lookup_major(&crypto_cdevsw), fcr->sesn);
   2000 	st->st_atimespec = fcr->atime;
   2001 	st->st_mtimespec = fcr->mtime;
   2002 	st->st_ctimespec = st->st_birthtimespec = fcr->btime;
   2003 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   2004 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   2005 	mutex_exit(&crypto_mtx);
   2006 
   2007 	return 0;
   2008 }
   2009 
   2010 static int
   2011 cryptof_poll(struct file *fp, int events)
   2012 {
   2013 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
   2014 	int revents = 0;
   2015 
   2016 	if (!(events & (POLLIN | POLLRDNORM))) {
   2017 		/* only support read and POLLIN */
   2018 		return 0;
   2019 	}
   2020 
   2021 	mutex_enter(&crypto_mtx);
   2022 	if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
   2023 		/* no completed requests pending, save the poll for later */
   2024 		selrecord(curlwp, &fcr->sinfo);
   2025 	} else {
   2026 		/* let the app(s) know that there are completed requests */
   2027 		revents = events & (POLLIN | POLLRDNORM);
   2028 	}
   2029 	mutex_exit(&crypto_mtx);
   2030 
   2031 	return revents;
   2032 }
   2033 
   2034 /*
   2035  * Pseudo-device initialization routine for /dev/crypto
   2036  */
   2037 void	cryptoattach(int);
   2038 
   2039 void
   2040 cryptoattach(int num)
   2041 {
   2042 	pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
   2043 	    NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
   2044 	pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
   2045 	    NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
   2046 
   2047 	/*
   2048 	 * Preallocate space for 64 users, with 5 sessions each.
   2049 	 * (consider that a TLS protocol session requires at least
   2050 	 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
   2051 	 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
   2052 	 * consuming one session here for each algorithm.
   2053 	 */
   2054 	pool_prime(&fcrpl, 64);
   2055 	pool_prime(&csepl, 64 * 5);
   2056 }
   2057