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