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