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