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