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