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