Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.36
      1 /*	$NetBSD: cryptodev.c,v 1.36 2008/04/10 22:48:42 tls 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  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the NetBSD
     23  *	Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Copyright (c) 2001 Theo de Raadt
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  *
     48  * 1. Redistributions of source code must retain the above copyright
     49  *   notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *   notice, this list of conditions and the following disclaimer in the
     52  *   documentation and/or other materials provided with the distribution.
     53  * 3. The name of the author may not be used to endorse or promote products
     54  *   derived from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     58  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     59  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     60  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     61  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     62  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     63  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     64  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     65  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     66  *
     67  * Effort sponsored in part by the Defense Advanced Research Projects
     68  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     69  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     70  *
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.36 2008/04/10 22:48:42 tls Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kmem.h>
     79 #include <sys/malloc.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/pool.h>
     82 #include <sys/sysctl.h>
     83 #include <sys/file.h>
     84 #include <sys/filedesc.h>
     85 #include <sys/errno.h>
     86 #include <sys/md5.h>
     87 #include <sys/sha1.h>
     88 #include <sys/conf.h>
     89 #include <sys/device.h>
     90 #include <sys/kauth.h>
     91 #include <sys/select.h>
     92 #include <sys/poll.h>
     93 
     94 #include "opt_ocf.h"
     95 #include <opencrypto/cryptodev.h>
     96 #include <opencrypto/xform.h>
     97 
     98 struct csession {
     99 	TAILQ_ENTRY(csession) next;
    100 	u_int64_t	sid;
    101 	u_int32_t	ses;
    102 
    103 	u_int32_t	cipher;
    104 	struct enc_xform *txform;
    105 	u_int32_t	mac;
    106 	struct auth_hash *thash;
    107 
    108 	void *		key;
    109 	int		keylen;
    110 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
    111 
    112 	void *		mackey;
    113 	int		mackeylen;
    114 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
    115 
    116 	struct iovec	iovec[1];	/* user requests never have more */
    117 	struct uio	uio;
    118 	int		error;
    119 };
    120 
    121 struct fcrypt {
    122 	TAILQ_HEAD(csessionlist, csession) csessions;
    123 	TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
    124 	TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
    125 	int		sesn;
    126 	struct selinfo	sinfo;
    127 	u_int32_t	requestid;
    128 };
    129 
    130 /* For our fixed-size allocations */
    131 struct pool fcrpl;
    132 struct pool csepl;
    133 
    134 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
    135 static int	cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
    136 static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
    137 static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
    138 static int	cryptoselect(dev_t dev, int rw, struct lwp *l);
    139 
    140 /* Declaration of cloned-device (per-ctxt) entrypoints */
    141 static int	cryptof_read(struct file *, off_t *, struct uio *,
    142 			     kauth_cred_t, int);
    143 static int	cryptof_write(struct file *, off_t *, struct uio *,
    144 			      kauth_cred_t, int);
    145 static int	cryptof_ioctl(struct file *, u_long, void *);
    146 static int	cryptof_close(struct file *);
    147 static int 	cryptof_poll(struct file *, int);
    148 
    149 static const struct fileops cryptofops = {
    150     cryptof_read,
    151     cryptof_write,
    152     cryptof_ioctl,
    153     fnullop_fcntl,
    154     cryptof_poll,
    155     fbadop_stat,
    156     cryptof_close,
    157     fnullop_kqfilter
    158 };
    159 
    160 static struct	csession *csefind(struct fcrypt *, u_int);
    161 static int	csedelete(struct fcrypt *, struct csession *);
    162 static struct	csession *cseadd(struct fcrypt *, struct csession *);
    163 static struct	csession *csecreate(struct fcrypt *, u_int64_t, void *, u_int64_t,
    164     void *, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    165     struct auth_hash *);
    166 static int	csefree(struct csession *);
    167 
    168 static int	cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
    169 static int	cryptodev_mop(struct fcrypt *, struct crypt_n_op *, int, struct
    170 			      lwp *);
    171 static int	cryptodev_key(struct crypt_kop *);
    172 static int	cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
    173 int	cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
    174 static int	cryptodev_session(struct fcrypt *, struct session_op *);
    175 static int	cryptodev_msession(struct fcrypt *, struct session_n_op *,
    176 				       int);
    177 static int	cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
    178 
    179 static int	cryptodev_cb(void *);
    180 static int	cryptodevkey_cb(void *);
    181 
    182 static int	cryptodev_mcb(void *);
    183 static int	cryptodevkey_mcb(void *);
    184 
    185 static int 	cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
    186 				     int);
    187 static int	cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
    188 
    189 /*
    190  * sysctl-able control variables for /dev/crypto now defined in crypto.c:
    191  * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
    192  */
    193 
    194 /* ARGSUSED */
    195 int
    196 cryptof_read(file_t *fp, off_t *poff,
    197     struct uio *uio, kauth_cred_t cred, int flags)
    198 {
    199 	return (EIO);
    200 }
    201 
    202 /* ARGSUSED */
    203 int
    204 cryptof_write(file_t *fp, off_t *poff,
    205     struct uio *uio, kauth_cred_t cred, int flags)
    206 {
    207 	return (EIO);
    208 }
    209 
    210 /* ARGSUSED */
    211 int
    212 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
    213 {
    214 	struct fcrypt *fcr = fp->f_data;
    215 	struct csession *cse;
    216 	struct session_op *sop;
    217 	struct session_n_op *snop;
    218 	struct crypt_op *cop;
    219 	struct crypt_mop *mop;
    220 	struct crypt_mkop *mkop;
    221 	struct crypt_n_op *cnop;
    222 	struct crypt_n_kop *knop;
    223 	struct crypt_sgop *sgop;
    224 	struct crypt_sfop *sfop;
    225 	struct cryptret *crypt_ret;
    226 	struct crypt_result *crypt_res;
    227 	u_int32_t ses;
    228 	u_int32_t *sesid;
    229 	int error = 0;
    230 	size_t count;
    231 
    232 	/* backwards compatibility */
    233         file_t *criofp;
    234 	struct fcrypt *criofcr;
    235 	int criofd;
    236 
    237 	switch (cmd) {
    238         case CRIOGET:   /* XXX deprecated, remove after 5.0 */
    239 		if ((error = fd_allocfile(&criofp, &criofd)) != 0)
    240 			return error;
    241 		criofcr = pool_get(&fcrpl, PR_WAITOK);
    242 		if (criofcr == NULL) {
    243 			fd_abort(curproc, criofp, criofd);
    244 			return ENOMEM;
    245 		}
    246 		mutex_spin_enter(&crypto_mtx);
    247 		TAILQ_INIT(&criofcr->csessions);
    248 		TAILQ_INIT(&criofcr->crp_ret_mq);
    249 		TAILQ_INIT(&criofcr->crp_ret_mkq);
    250 		selinit(&criofcr->sinfo);
    251 
    252                 /*
    253                  * Don't ever return session 0, to allow detection of
    254                  * failed creation attempts with multi-create ioctl.
    255                  */
    256 		criofcr->sesn = 1;
    257 		criofcr->requestid = 1;
    258 		mutex_spin_exit(&crypto_mtx);
    259 		(void)fd_clone(criofp, criofd, (FREAD|FWRITE),
    260 			      &cryptofops, criofcr);
    261 		*(u_int32_t *)data = criofd;
    262 		return error;
    263 		break;
    264 	case CIOCGSESSION:
    265 		sop = (struct session_op *)data;
    266 		error = cryptodev_session(fcr, sop);
    267 		break;
    268 	case CIOCNGSESSION:
    269 		sgop = (struct crypt_sgop *)data;
    270 		snop = kmem_alloc((sgop->count *
    271 				  sizeof(struct session_n_op)), KM_SLEEP);
    272 		if (!snop) {
    273 			error = EINVAL;
    274 			goto mbail;
    275 		}
    276 		error = copyin(sgop->sessions, snop, sgop->count *
    277 			       sizeof(struct session_n_op));
    278 		if (error) {
    279 			goto mbail;
    280 		}
    281 
    282 		error = cryptodev_msession(fcr, snop, sgop->count);
    283 		if (error) {
    284 			goto mbail;
    285 		}
    286 
    287 		error = copyout(snop, sgop->sessions, sgop->count *
    288 					sizeof(struct session_n_op));
    289 mbail:
    290 		if(snop) {
    291 			kmem_free(snop, sgop->count *
    292 				  sizeof(struct session_n_op));
    293 		}
    294 		break;
    295 	case CIOCFSESSION:
    296 		mutex_spin_enter(&crypto_mtx);
    297 		ses = *(u_int32_t *)data;
    298 		cse = csefind(fcr, ses);
    299 		if (cse == NULL)
    300 			return (EINVAL);
    301 		csedelete(fcr, cse);
    302 		error = csefree(cse);
    303 		mutex_spin_exit(&crypto_mtx);
    304 		break;
    305 	case CIOCNFSESSION:
    306 		sfop = (struct crypt_sfop *)data;
    307 		sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
    308 				   KM_SLEEP);
    309 		if (!sesid)
    310 			return (EINVAL);
    311 		error = copyin(sfop->sesid, sesid,
    312 			    (sfop->count * sizeof(u_int32_t)));
    313 		if (!error) {
    314 			error = cryptodev_msessionfin(fcr, sfop->count, sesid);
    315 		}
    316 		kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
    317 		break;
    318 	case CIOCCRYPT:
    319 		mutex_spin_enter(&crypto_mtx);
    320 		cop = (struct crypt_op *)data;
    321 		cse = csefind(fcr, cop->ses);
    322 		mutex_spin_exit(&crypto_mtx);
    323 		if (cse == NULL) {
    324 			DPRINTF(("csefind failed\n"));
    325 			return (EINVAL);
    326 		}
    327 		error = cryptodev_op(cse, cop, curlwp);
    328 		DPRINTF(("cryptodev_op error = %d\n", error));
    329 		break;
    330 	case CIOCNCRYPTM:
    331 		mop = (struct crypt_mop *)data;
    332 		cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
    333 				  KM_SLEEP);
    334 		if(!cnop) {
    335 			return(EINVAL);
    336 		}
    337 		error = copyin(mop->reqs, cnop,
    338 			    (mop->count * sizeof(struct crypt_n_op)));
    339 		if(!error) {
    340 			error = cryptodev_mop(fcr, cnop, mop->count,
    341 					      curlwp);
    342 			if (!error) {
    343 				error = copyout(cnop, mop->reqs,
    344 					        (mop->count *
    345 						 sizeof(struct crypt_n_op)));
    346 			}
    347 		}
    348 		kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
    349 		break;
    350 	case CIOCKEY:
    351 		error = cryptodev_key((struct crypt_kop *)data);
    352 		DPRINTF(("cryptodev_key error = %d\n", error));
    353 		break;
    354 	case CIOCNFKEYM:
    355 		mkop = (struct crypt_mkop *)data;
    356 		knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
    357 				  KM_SLEEP);
    358 		if (!knop) {
    359 			return(EINVAL);
    360 		}
    361 		error = copyin(mkop->reqs, knop,
    362 			    (mkop->count * sizeof(struct crypt_n_kop)));
    363 		if (!error) {
    364 			error = cryptodev_mkey(fcr, knop, mkop->count);
    365 			if (!error)
    366 				error = copyout(knop, mkop->reqs,
    367 			    			(mkop->count *
    368 						 sizeof(struct crypt_n_kop)));
    369 		}
    370 		kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
    371 		break;
    372 	case CIOCASYMFEAT:
    373 		error = crypto_getfeat((int *)data);
    374 		break;
    375 	case CIOCNCRYPTRETM:
    376 		crypt_ret = (struct cryptret *)data;
    377 		count = crypt_ret->count;
    378 		crypt_res = kmem_alloc((count *
    379 				      sizeof(struct crypt_result)),
    380 				      KM_SLEEP);
    381 		if(!crypt_res)
    382 			return(EINVAL);
    383 		error = copyin(crypt_ret->results, crypt_res,
    384 			    (count * sizeof(struct crypt_result)));
    385 		if (error)
    386 			goto reterr;
    387 		crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
    388 					crypt_ret->count);
    389 		/* sanity check count */
    390 		if (crypt_ret->count > count) {
    391 			printf("%s.%d: error returned count %d > original "
    392 			       " count %d\n",
    393 				__FILE__, __LINE__, crypt_ret->count, count);
    394 			crypt_ret->count = count;
    395 
    396 		}
    397 		error = copyout(crypt_res, crypt_ret->results,
    398 			    (crypt_ret->count * sizeof(struct crypt_result)));
    399 reterr:
    400 		kmem_free(crypt_res,
    401 			  (count * sizeof(struct crypt_result)));
    402 		break;
    403 	case CIOCNCRYPTRET:
    404 		error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
    405 		break;
    406 	default:
    407 		DPRINTF(("invalid ioctl cmd %ld\n", cmd));
    408 		error = EINVAL;
    409 	}
    410 	return (error);
    411 }
    412 
    413 static int
    414 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
    415 {
    416 	struct cryptop *crp = NULL;
    417 	struct cryptodesc *crde = NULL, *crda = NULL;
    418 	int error;
    419 
    420 	if (cop->len > 256*1024-4)
    421 		return (E2BIG);
    422 
    423 	if (cse->txform) {
    424 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
    425 			return (EINVAL);
    426 	}
    427 
    428 	bzero(&cse->uio, sizeof(cse->uio));
    429 	cse->uio.uio_iovcnt = 1;
    430 	cse->uio.uio_resid = 0;
    431 	cse->uio.uio_rw = UIO_WRITE;
    432 	cse->uio.uio_iov = cse->iovec;
    433 	UIO_SETUP_SYSSPACE(&cse->uio);
    434 	memset(&cse->iovec, 0, sizeof(cse->iovec));
    435 	cse->uio.uio_iov[0].iov_len = cop->len;
    436 	cse->uio.uio_iov[0].iov_base = kmem_alloc(cop->len, KM_SLEEP);
    437 	cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
    438 
    439 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    440 	if (crp == NULL) {
    441 		error = ENOMEM;
    442 		goto bail;
    443 	}
    444 
    445 	if (cse->thash) {
    446 		crda = crp->crp_desc;
    447 		if (cse->txform)
    448 			crde = crda->crd_next;
    449 	} else {
    450 		if (cse->txform)
    451 			crde = crp->crp_desc;
    452 		else {
    453 			error = EINVAL;
    454 			goto bail;
    455 		}
    456 	}
    457 
    458 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    459 	{
    460 		printf("copyin failed %s %d \n", (char *)cop->src, error);
    461 		goto bail;
    462 	}
    463 
    464 	if (crda) {
    465 		crda->crd_skip = 0;
    466 		crda->crd_len = cop->len;
    467 		crda->crd_inject = 0;	/* ??? */
    468 
    469 		crda->crd_alg = cse->mac;
    470 		crda->crd_key = cse->mackey;
    471 		crda->crd_klen = cse->mackeylen * 8;
    472 	}
    473 
    474 	if (crde) {
    475 		if (cop->op == COP_ENCRYPT)
    476 			crde->crd_flags |= CRD_F_ENCRYPT;
    477 		else
    478 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    479 		crde->crd_len = cop->len;
    480 		crde->crd_inject = 0;
    481 
    482 		crde->crd_alg = cse->cipher;
    483 		crde->crd_key = cse->key;
    484 		crde->crd_klen = cse->keylen * 8;
    485 	}
    486 
    487 	crp->crp_ilen = cop->len;
    488 	crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH);
    489 	crp->crp_buf = (void *)&cse->uio;
    490 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    491 	crp->crp_sid = cse->sid;
    492 	crp->crp_opaque = (void *)cse;
    493 
    494 	if (cop->iv) {
    495 		if (crde == NULL) {
    496 			error = EINVAL;
    497 			goto bail;
    498 		}
    499 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    500 			error = EINVAL;
    501 			goto bail;
    502 		}
    503 		if ((error = copyin(cop->iv, cse->tmp_iv,
    504 				    cse->txform->blocksize)))
    505 			goto bail;
    506 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
    507 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    508 		crde->crd_skip = 0;
    509 	} else if (crde) {
    510 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    511 			crde->crd_skip = 0;
    512 		} else {
    513 			crde->crd_flags |= CRD_F_IV_PRESENT;
    514 			crde->crd_skip = cse->txform->blocksize;
    515 			crde->crd_len -= cse->txform->blocksize;
    516 		}
    517 	}
    518 
    519 	if (cop->mac) {
    520 		if (crda == NULL) {
    521 			error = EINVAL;
    522 			goto bail;
    523 		}
    524 		crp->crp_mac=cse->tmp_mac;
    525 	}
    526 
    527 	/*
    528 	 * XXX there was a comment here which said that we went to
    529 	 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
    530 	 * XXX disabled on NetBSD since 1.6O due to a race condition.
    531 	 * XXX But crypto_dispatch went to splcrypto() itself!  (And
    532 	 * XXX now takes the crypto_mtx mutex itself).  We do, however,
    533 	 * XXX need to hold the mutex across the call to cv_wait().
    534 	 * XXX     (should we arrange for crypto_dispatch to return to
    535 	 * XXX      us with it held?  it seems quite ugly to do so.)
    536 	 */
    537 #ifdef notyet
    538 eagain:
    539 #endif
    540 	error = crypto_dispatch(crp);
    541 	mutex_spin_enter(&crypto_mtx);
    542 
    543 	switch (error) {
    544 #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */
    545 	case EAGAIN:
    546 		mutex_spin_exit(&crypto_mtx);
    547 		goto eagain;
    548 		break;
    549 #endif
    550 	case 0:
    551 		break;
    552 	default:
    553 		DPRINTF(("cryptodev_op: not waiting, error.\n"));
    554 		mutex_spin_exit(&crypto_mtx);
    555 		goto bail;
    556 	}
    557 
    558 	while (!(crp->crp_flags & CRYPTO_F_DONE)) {
    559 		DPRINTF(("cryptodev_op: sleeping on cv %08x for crp %08x\n", \
    560 			(uint32_t)&crp->crp_cv, (uint32_t)crp));
    561 		cv_wait(&crp->crp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    562 	}
    563 	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
    564 		DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
    565 		(void)crypto_ret_q_remove(crp);
    566 	}
    567 	mutex_spin_exit(&crypto_mtx);
    568 
    569 	if (crp->crp_etype != 0) {
    570 		DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
    571 		error = crp->crp_etype;
    572 		goto bail;
    573 	}
    574 
    575 	if (cse->error) {
    576 		DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
    577 		error = cse->error;
    578 		goto bail;
    579 	}
    580 
    581 	if (cop->dst &&
    582 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
    583 	{
    584 		DPRINTF(("cryptodev_op: copyout error %d\n", error));
    585 		goto bail;
    586 	}
    587 
    588 	if (cop->mac &&
    589 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
    590 		DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
    591 		goto bail;
    592 	}
    593 
    594 bail:
    595 	if (crp)
    596 		crypto_freereq(crp);
    597 	if (cse->uio.uio_iov[0].iov_base)
    598 		kmem_free(cse->uio.uio_iov[0].iov_base,
    599 			  cse->uio.uio_iov[0].iov_len);
    600 
    601 	return (error);
    602 }
    603 
    604 static int
    605 cryptodev_cb(void *op)
    606 {
    607 	struct cryptop *crp = (struct cryptop *) op;
    608 	struct csession *cse = (struct csession *)crp->crp_opaque;
    609 	int error = 0;
    610 
    611 	mutex_spin_enter(&crypto_mtx);
    612 	cse->error = crp->crp_etype;
    613 	if (crp->crp_etype == EAGAIN) {
    614 		/* always drop mutex to call dispatch routine */
    615 		mutex_spin_exit(&crypto_mtx);
    616 		error = crypto_dispatch(crp);
    617 		mutex_spin_enter(&crypto_mtx);
    618 	}
    619 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    620 		cv_signal(&crp->crp_cv);
    621 	}
    622 	mutex_spin_exit(&crypto_mtx);
    623 	return (0);
    624 }
    625 
    626 static int
    627 cryptodev_mcb(void *op)
    628 {
    629 	struct cryptop *crp = (struct cryptop *) op;
    630 	struct csession *cse = (struct csession *)crp->crp_opaque;
    631 	int  error=0;
    632 
    633 	mutex_spin_enter(&crypto_mtx);
    634 	cse->error = crp->crp_etype;
    635 	if (crp->crp_etype == EAGAIN) {
    636 		mutex_spin_exit(&crypto_mtx);
    637 		error = crypto_dispatch(crp);
    638 		mutex_spin_enter(&crypto_mtx);
    639 	}
    640 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    641 		cv_signal(&crp->crp_cv);
    642 	}
    643 
    644 	TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
    645 	mutex_spin_exit(&crypto_mtx);
    646 	selnotify(&crp->fcrp->sinfo, 0, 0);
    647 	return (0);
    648 }
    649 
    650 static int
    651 cryptodevkey_cb(void *op)
    652 {
    653 	struct cryptkop *krp = (struct cryptkop *) op;
    654 
    655 	mutex_spin_enter(&crypto_mtx);
    656 	cv_signal(&krp->krp_cv);
    657 	mutex_spin_exit(&crypto_mtx);
    658 	return (0);
    659 }
    660 
    661 static int
    662 cryptodevkey_mcb(void *op)
    663 {
    664 	struct cryptkop *krp = (struct cryptkop *) op;
    665 
    666 	mutex_spin_enter(&crypto_mtx);
    667 	cv_signal(&krp->krp_cv);
    668 	TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
    669 	mutex_spin_exit(&crypto_mtx);
    670 	selnotify(&krp->fcrp->sinfo, 0, 0);
    671 	return (0);
    672 }
    673 
    674 static int
    675 cryptodev_key(struct crypt_kop *kop)
    676 {
    677 	struct cryptkop *krp = NULL;
    678 	int error = EINVAL;
    679 	int in, out, size, i;
    680 
    681 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
    682 		return (EFBIG);
    683 	}
    684 
    685 	in = kop->crk_iparams;
    686 	out = kop->crk_oparams;
    687 	switch (kop->crk_op) {
    688 	case CRK_MOD_EXP:
    689 		if (in == 3 && out == 1)
    690 			break;
    691 		return (EINVAL);
    692 	case CRK_MOD_EXP_CRT:
    693 		if (in == 6 && out == 1)
    694 			break;
    695 		return (EINVAL);
    696 	case CRK_DSA_SIGN:
    697 		if (in == 5 && out == 2)
    698 			break;
    699 		return (EINVAL);
    700 	case CRK_DSA_VERIFY:
    701 		if (in == 7 && out == 0)
    702 			break;
    703 		return (EINVAL);
    704 	case CRK_DH_COMPUTE_KEY:
    705 		if (in == 3 && out == 1)
    706 			break;
    707 		return (EINVAL);
    708 	case CRK_MOD_ADD:
    709 		if (in == 3 && out == 1)
    710 			break;
    711 		return (EINVAL);
    712 	case CRK_MOD_ADDINV:
    713 		if (in == 2 && out == 1)
    714 			break;
    715 		return (EINVAL);
    716 	case CRK_MOD_SUB:
    717 		if (in == 3 && out == 1)
    718 			break;
    719 		return (EINVAL);
    720 	case CRK_MOD_MULT:
    721 		if (in == 3 && out == 1)
    722 			break;
    723 		return (EINVAL);
    724 	case CRK_MOD_MULTINV:
    725 		if (in == 2 && out == 1)
    726 			break;
    727 		return (EINVAL);
    728 	case CRK_MOD:
    729 		if (in == 2 && out == 1)
    730 			break;
    731 		return (EINVAL);
    732 	default:
    733 		return (EINVAL);
    734 	}
    735 
    736 	krp = pool_get(&cryptkop_pool, PR_WAITOK);
    737 	if (!krp)
    738 		return (ENOMEM);
    739 	bzero(krp, sizeof *krp);
    740 	cv_init(&krp->krp_cv, "crykdev");
    741 	krp->krp_op = kop->crk_op;
    742 	krp->krp_status = kop->crk_status;
    743 	krp->krp_iparams = kop->crk_iparams;
    744 	krp->krp_oparams = kop->crk_oparams;
    745 	krp->krp_status = 0;
    746 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    747 
    748 	for (i = 0; i < CRK_MAXPARAM; i++)
    749 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    750 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    751 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    752 		if (size == 0)
    753 			continue;
    754 		krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
    755 		if (i >= krp->krp_iparams)
    756 			continue;
    757 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
    758 		if (error)
    759 			goto fail;
    760 	}
    761 
    762 	error = crypto_kdispatch(krp);
    763 	if (error != 0) {
    764 		goto fail;
    765 	}
    766 
    767 	mutex_spin_enter(&crypto_mtx);
    768 	while (!(krp->krp_flags & CRYPTO_F_DONE)) {
    769 		cv_wait(&krp->krp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    770 	}
    771 	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
    772 		DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
    773 		(void)crypto_ret_kq_remove(krp);
    774 	}
    775 	mutex_spin_exit(&crypto_mtx);
    776 
    777 	if (krp->krp_status != 0) {
    778 		DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n", krp->krp_status));
    779 		error = krp->krp_status;
    780 		goto fail;
    781 	}
    782 
    783 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
    784 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    785 		if (size == 0)
    786 			continue;
    787 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
    788 		if (error) {
    789 			DPRINTF(("cryptodev_key: copyout oparam %d failed, error=%d\n", i-krp->krp_iparams, error));
    790 			goto fail;
    791 		}
    792 	}
    793 
    794 fail:
    795 	if (krp) {
    796 		kop->crk_status = krp->krp_status;
    797 		for (i = 0; i < CRK_MAXPARAM; i++) {
    798 			struct crparam *kp = &(krp->krp_param[i]);
    799 			if (krp->krp_param[i].crp_p) {
    800 				size = (kp->crp_nbits + 7)  / 8;
    801 				KASSERT(size > 0);
    802 				memset(kp->crp_p, 0, size);
    803 				kmem_free(kp->crp_p, size);
    804 			}
    805 		}
    806 		pool_put(&cryptkop_pool, krp);
    807 	}
    808 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
    809 	return (error);
    810 }
    811 
    812 /* ARGSUSED */
    813 static int
    814 cryptof_close(struct file *fp)
    815 {
    816 	struct fcrypt *fcr = fp->f_data;
    817 	struct csession *cse;
    818 
    819 	mutex_spin_enter(&crypto_mtx);
    820 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    821 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    822 		(void)csefree(cse);
    823 	}
    824 	seldestroy(&fcr->sinfo);
    825 	pool_put(&fcrpl, fcr);
    826 
    827 	fp->f_data = NULL;
    828 	mutex_spin_exit(&crypto_mtx);
    829 
    830 	return 0;
    831 }
    832 
    833 /* csefind: call with crypto_mtx held. */
    834 static struct csession *
    835 csefind(struct fcrypt *fcr, u_int ses)
    836 {
    837 	struct csession *cse, *ret = NULL;
    838 
    839 	KASSERT(mutex_owned(&crypto_mtx));
    840 	TAILQ_FOREACH(cse, &fcr->csessions, next)
    841 		if (cse->ses == ses)
    842 			ret = cse;
    843 
    844 	return (ret);
    845 }
    846 
    847 /* csedelete: call with crypto_mtx held. */
    848 static int
    849 csedelete(struct fcrypt *fcr, struct csession *cse_del)
    850 {
    851 	struct csession *cse;
    852 	int ret = 0;
    853 
    854 	KASSERT(mutex_owned(&crypto_mtx));
    855 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
    856 		if (cse == cse_del) {
    857 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    858 			ret = 1;
    859 		}
    860 	}
    861 	return (ret);
    862 }
    863 
    864 /* cseadd: call with crypto_mtx held. */
    865 static struct csession *
    866 cseadd(struct fcrypt *fcr, struct csession *cse)
    867 {
    868 	KASSERT(mutex_owned(&crypto_mtx));
    869 	/* don't let session ID wrap! */
    870 	if (fcr->sesn + 1 == 0) return NULL;
    871 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    872 	cse->ses = fcr->sesn++;
    873 	return (cse);
    874 }
    875 
    876 /* csecreate: call with crypto_mtx held. */
    877 static struct csession *
    878 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
    879     void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    880     struct enc_xform *txform, struct auth_hash *thash)
    881 {
    882 	struct csession *cse;
    883 
    884 	KASSERT(mutex_owned(&crypto_mtx));
    885 	cse = pool_get(&csepl, PR_NOWAIT);
    886 	if (cse == NULL)
    887 		return NULL;
    888 	cse->key = key;
    889 	cse->keylen = keylen/8;
    890 	cse->mackey = mackey;
    891 	cse->mackeylen = mackeylen/8;
    892 	cse->sid = sid;
    893 	cse->cipher = cipher;
    894 	cse->mac = mac;
    895 	cse->txform = txform;
    896 	cse->thash = thash;
    897 	cse->error = 0;
    898 	if (cseadd(fcr, cse))
    899 		return (cse);
    900 	else {
    901 		pool_put(&csepl, cse);
    902 		return NULL;
    903 	}
    904 }
    905 
    906 /* csefree: call with crypto_mtx held. */
    907 static int
    908 csefree(struct csession *cse)
    909 {
    910 	int error;
    911 
    912 	KASSERT(mutex_owned(&crypto_mtx));
    913 	error = crypto_freesession(cse->sid);
    914 	if (cse->key)
    915 		free(cse->key, M_XDATA);
    916 	if (cse->mackey)
    917 		free(cse->mackey, M_XDATA);
    918 	pool_put(&csepl, cse);
    919 	return (error);
    920 }
    921 
    922 static int
    923 cryptoopen(dev_t dev, int flag, int mode,
    924     struct lwp *l)
    925 {
    926 	file_t *fp;
    927         struct fcrypt *fcr;
    928         int fd, error;
    929 
    930 	if (crypto_usercrypto == 0)
    931 		return (ENXIO);
    932 
    933 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    934 		return error;
    935 
    936 	fcr = pool_get(&fcrpl, PR_WAITOK);
    937 	if (fcr == NULL) {
    938 		fd_abort(curproc, fp, fd);
    939 		return ENOMEM;
    940 	}
    941 	mutex_spin_enter(&crypto_mtx);
    942 	TAILQ_INIT(&fcr->csessions);
    943 	TAILQ_INIT(&fcr->crp_ret_mq);
    944 	TAILQ_INIT(&fcr->crp_ret_mkq);
    945 	selinit(&fcr->sinfo);
    946 	/*
    947 	 * Don't ever return session 0, to allow detection of
    948 	 * failed creation attempts with multi-create ioctl.
    949 	 */
    950 	fcr->sesn = 1;
    951 	fcr->requestid = 1;
    952 	mutex_spin_exit(&crypto_mtx);
    953 	return fd_clone(fp, fd, flag, &cryptofops, fcr);
    954 }
    955 
    956 static int
    957 cryptoread(dev_t dev, struct uio *uio, int ioflag)
    958 {
    959 	return (EIO);
    960 }
    961 
    962 static int
    963 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
    964 {
    965 	return (EIO);
    966 }
    967 
    968 int
    969 cryptoselect(dev_t dev, int rw, struct lwp *l)
    970 {
    971 	return (0);
    972 }
    973 
    974 /*static*/
    975 struct cdevsw crypto_cdevsw = {
    976 	/* open */	cryptoopen,
    977 	/* close */	noclose,
    978 	/* read */	cryptoread,
    979 	/* write */	cryptowrite,
    980 	/* ioctl */	noioctl,
    981 	/* ttstop?*/	nostop,
    982 	/* ??*/		notty,
    983 	/* poll */	cryptoselect /*nopoll*/,
    984 	/* mmap */	nommap,
    985 	/* kqfilter */	nokqfilter,
    986 	/* type */	D_OTHER,
    987 };
    988 
    989 static int
    990 cryptodev_mop(struct fcrypt *fcr,
    991               struct crypt_n_op * cnop,
    992               int count, struct lwp *l)
    993 {
    994 	struct cryptop *crp = NULL;
    995 	struct cryptodesc *crde = NULL, *crda = NULL;
    996 	int req, error=0;
    997 	struct csession *cse;
    998 
    999 	for (req = 0; req < count; req++) {
   1000 		mutex_spin_enter(&crypto_mtx);
   1001 		cse = csefind(fcr, cnop[req].ses);
   1002 		if (cse == NULL) {
   1003 			DPRINTF(("csefind failed\n"));
   1004 			cnop[req].status = EINVAL;
   1005 			mutex_spin_exit(&crypto_mtx);
   1006 			continue;
   1007 		}
   1008 		mutex_spin_exit(&crypto_mtx);
   1009 
   1010 		if (cnop[req].len > 256*1024-4) {
   1011 			DPRINTF(("length failed\n"));
   1012 			cnop[req].status = EINVAL;
   1013 			continue;
   1014 		}
   1015 		if (cse->txform) {
   1016 			if (cnop[req].len == 0 ||
   1017 			    (cnop[req].len % cse->txform->blocksize) != 0) {
   1018 				cnop[req].status = EINVAL;
   1019 				continue;
   1020 			}
   1021 		}
   1022 
   1023 		crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
   1024 		if (crp == NULL) {
   1025 			cnop[req].status = ENOMEM;
   1026 			goto bail;
   1027 		}
   1028 
   1029 		bzero(&crp->uio, sizeof(crp->uio));
   1030 		crp->uio.uio_iovcnt = 1;
   1031 		crp->uio.uio_resid = 0;
   1032 		crp->uio.uio_rw = UIO_WRITE;
   1033 		crp->uio.uio_iov = crp->iovec;
   1034 		UIO_SETUP_SYSSPACE(&crp->uio);
   1035 		memset(&crp->iovec, 0, sizeof(crp->iovec));
   1036 		crp->uio.uio_iov[0].iov_len = cnop[req].len;
   1037 		crp->uio.uio_iov[0].iov_base = kmem_alloc(cnop[req].len,
   1038 							  KM_SLEEP);
   1039 		crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
   1040 
   1041 		if (cse->thash) {
   1042 			crda = crp->crp_desc;
   1043 			if (cse->txform)
   1044 				crde = crda->crd_next;
   1045 		} else {
   1046 			if (cse->txform)
   1047 				crde = crp->crp_desc;
   1048 			else {
   1049 				cnop[req].status = EINVAL;
   1050 				goto bail;
   1051 			}
   1052 		}
   1053 
   1054 		if ((copyin(cnop[req].src,
   1055 		    crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
   1056 			cnop[req].status = EINVAL;
   1057 			goto bail;
   1058 		}
   1059 
   1060 		if (crda) {
   1061 			crda->crd_skip = 0;
   1062 			crda->crd_len = cnop[req].len;
   1063 			crda->crd_inject = 0;	/* ??? */
   1064 
   1065 			crda->crd_alg = cse->mac;
   1066 			crda->crd_key = cse->mackey;
   1067 			crda->crd_klen = cse->mackeylen * 8;
   1068 		}
   1069 
   1070 		if (crde) {
   1071 			if (cnop[req].op == COP_ENCRYPT)
   1072 				crde->crd_flags |= CRD_F_ENCRYPT;
   1073 			else
   1074 				crde->crd_flags &= ~CRD_F_ENCRYPT;
   1075 			crde->crd_len = cnop[req].len;
   1076 			crde->crd_inject = 0;
   1077 
   1078 			crde->crd_alg = cse->cipher;
   1079 #ifdef notyet		/* XXX must notify h/w driver new key, drain */
   1080 			if(cnop[req].key && cnop[req].keylen) {
   1081 				crde->crd_key = malloc(cnop[req].keylen,
   1082 						    M_XDATA, M_WAITOK);
   1083 				if((error = copyin(cnop[req].key,
   1084 				    crde->crd_key, cnop[req].keylen))) {
   1085 					cnop[req].status = EINVAL;
   1086 					goto bail;
   1087 				}
   1088 				crde->crd_klen =  cnop[req].keylen * 8;
   1089 			} else { ... }
   1090 #endif
   1091 			crde->crd_key = cse->key;
   1092 			crde->crd_klen = cse->keylen * 8;
   1093 		}
   1094 
   1095 		crp->crp_ilen = cnop[req].len;
   1096 		crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
   1097 				    | (cnop[req].flags & COP_F_BATCH);
   1098 		crp->crp_buf = (void *)&crp->uio;
   1099 		crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
   1100 		crp->crp_sid = cse->sid;
   1101 		crp->crp_opaque = (void *)cse;
   1102 		crp->fcrp = fcr;
   1103 		crp->dst = cnop[req].dst;
   1104 		/* we can use the crp_ilen in cryptop(crp) for this */
   1105 		crp->len = cnop[req].len;
   1106 		crp->mac = cnop[req].mac;
   1107 
   1108 		if (cnop[req].iv) {
   1109 			if (crde == NULL) {
   1110 				cnop[req].status = EINVAL;
   1111 				goto bail;
   1112 			}
   1113 			if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
   1114 				cnop[req].status = EINVAL;
   1115 				goto bail;
   1116 			}
   1117 			if ((error = copyin(cnop[req].iv, crp->tmp_iv,
   1118 			    cse->txform->blocksize))) {
   1119 				cnop[req].status = EINVAL;
   1120 				goto bail;
   1121 			}
   1122 			bcopy(crp->tmp_iv, crde->crd_iv, cse->txform->blocksize);
   1123 			crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
   1124 			crde->crd_skip = 0;
   1125 		} else if (crde) {
   1126 			if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
   1127 				crde->crd_skip = 0;
   1128 			} else {
   1129 				crde->crd_flags |= CRD_F_IV_PRESENT;
   1130 				crde->crd_skip = cse->txform->blocksize;
   1131 				crde->crd_len -= cse->txform->blocksize;
   1132 			}
   1133 		}
   1134 
   1135 		if (cnop[req].mac) {
   1136 			if (crda == NULL) {
   1137 				cnop[req].status = EINVAL;
   1138 				goto bail;
   1139 			}
   1140 			crp->crp_mac=cse->tmp_mac;
   1141 		}
   1142 		cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
   1143 		crp->crp_reqid = cnop[req].reqid;
   1144 		crp->crp_usropaque = cnop[req].opaque;
   1145 #ifdef notyet
   1146 eagain:
   1147 #endif
   1148 		cnop[req].status = crypto_dispatch(crp);
   1149 		mutex_spin_enter(&crypto_mtx);	/* XXX why mutex? */
   1150 
   1151 		switch (cnop[req].status) {
   1152 #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */
   1153 		case EAGAIN:
   1154 			mutex_spin_exit(&crypto_mtx);
   1155 			goto eagain;
   1156 			break;
   1157 #endif
   1158 		case 0:
   1159 			break;
   1160 		default:
   1161 			DPRINTF(("cryptodev_op: not waiting, error.\n"));
   1162 			mutex_spin_exit(&crypto_mtx);
   1163 			goto bail;
   1164 		}
   1165 
   1166 		mutex_spin_exit(&crypto_mtx);
   1167 bail:
   1168 		if (cnop[req].status) {
   1169 			if (crp) {
   1170 				crypto_freereq(crp);
   1171 				if(cse->uio.uio_iov[0].iov_base) {
   1172 					kmem_free(cse->uio.uio_iov[0].iov_base,
   1173 						  cse->uio.uio_iov[0].iov_len);
   1174 				}
   1175 			}
   1176 			error = 0;
   1177 		}
   1178 	}
   1179 	return (error);
   1180 }
   1181 
   1182 static int
   1183 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
   1184 {
   1185 	struct cryptkop *krp = NULL;
   1186 	int error = EINVAL;
   1187 	int in, out, size, i, req;
   1188 
   1189 	for (req = 0; req < count; req++) {
   1190 		if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM) {
   1191 			return (EFBIG);
   1192 		}
   1193 
   1194 		in = kop[req].crk_iparams;
   1195 		out = kop[req].crk_oparams;
   1196 		switch (kop[req].crk_op) {
   1197 		case CRK_MOD_EXP:
   1198 			if (in == 3 && out == 1)
   1199 				break;
   1200 			kop[req].crk_status = EINVAL;
   1201 			continue;
   1202 		case CRK_MOD_EXP_CRT:
   1203 			if (in == 6 && out == 1)
   1204 				break;
   1205 			kop[req].crk_status = EINVAL;
   1206 			continue;
   1207 		case CRK_DSA_SIGN:
   1208 			if (in == 5 && out == 2)
   1209 				break;
   1210 			kop[req].crk_status = EINVAL;
   1211 			continue;
   1212 		case CRK_DSA_VERIFY:
   1213 			if (in == 7 && out == 0)
   1214 				break;
   1215 			kop[req].crk_status = EINVAL;
   1216 			continue;
   1217 		case CRK_DH_COMPUTE_KEY:
   1218 			if (in == 3 && out == 1)
   1219 				break;
   1220 			kop[req].crk_status = EINVAL;
   1221 			continue;
   1222 		case CRK_MOD_ADD:
   1223 			if (in == 3 && out == 1)
   1224 				break;
   1225 			kop[req].crk_status = EINVAL;
   1226 			continue;
   1227 		case CRK_MOD_ADDINV:
   1228 			if (in == 2 && out == 1)
   1229 				break;
   1230 			kop[req].crk_status = EINVAL;
   1231 			continue;
   1232 		case CRK_MOD_SUB:
   1233 			if (in == 3 && out == 1)
   1234 				break;
   1235 			kop[req].crk_status = EINVAL;
   1236 			continue;
   1237 		case CRK_MOD_MULT:
   1238 			if (in == 3 && out == 1)
   1239 				break;
   1240 			kop[req].crk_status = EINVAL;
   1241 			continue;
   1242 		case CRK_MOD_MULTINV:
   1243 			if (in == 2 && out == 1)
   1244 				break;
   1245 			kop[req].crk_status = EINVAL;
   1246 			continue;
   1247 		case CRK_MOD:
   1248 			if (in == 2 && out == 1)
   1249 				break;
   1250 			kop[req].crk_status = EINVAL;
   1251 			continue;
   1252 		default:
   1253 			kop[req].crk_status = EINVAL;
   1254 			continue;
   1255 		}
   1256 
   1257 		krp = pool_get(&cryptkop_pool, PR_WAITOK);
   1258 		if (!krp) {
   1259 			kop[req].crk_status = ENOMEM;
   1260 			goto fail;
   1261 		}
   1262 		bzero(krp, sizeof *krp);
   1263 		cv_init(&krp->krp_cv, "crykdev");
   1264 		krp->krp_op = kop[req].crk_op;
   1265 		krp->krp_status = kop[req].crk_status;
   1266 		krp->krp_iparams = kop[req].crk_iparams;
   1267 		krp->krp_oparams = kop[req].crk_oparams;
   1268 		krp->krp_status = 0;
   1269 		krp->krp_callback =
   1270 		    (int (*) (struct cryptkop *)) cryptodevkey_mcb;
   1271 		bcopy(kop[req].crk_param,
   1272 		      krp->crk_param,
   1273 		      sizeof(kop[req].crk_param));
   1274 
   1275 		krp->krp_flags = CRYPTO_F_CBIMM;
   1276 
   1277 		for (i = 0; i < CRK_MAXPARAM; i++)
   1278 			krp->krp_param[i].crp_nbits =
   1279 			    kop[req].crk_param[i].crp_nbits;
   1280 		for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
   1281 			size = (krp->krp_param[i].crp_nbits + 7) / 8;
   1282 			if (size == 0)
   1283 				continue;
   1284 			krp->krp_param[i].crp_p =
   1285 			    kmem_alloc(size, KM_SLEEP);
   1286 			if (i >= krp->krp_iparams)
   1287 				continue;
   1288 			kop[req].crk_status = copyin(kop[req].crk_param[i].crp_p,
   1289 						krp->krp_param[i].crp_p, size);
   1290 			if (kop[req].crk_status)
   1291 				goto fail;
   1292 		}
   1293 		krp->fcrp = fcr;
   1294 
   1295 		kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
   1296 		krp->krp_reqid = kop[req].crk_reqid;
   1297 		krp->krp_usropaque = kop[req].crk_opaque;
   1298 
   1299 		kop[req].crk_status = crypto_kdispatch(krp);
   1300 		if (kop[req].crk_status != 0) {
   1301 			goto fail;
   1302 		}
   1303 
   1304 fail:
   1305 		if(kop[req].crk_status) {
   1306 			if (krp) {
   1307 				kop[req].crk_status = krp->krp_status;
   1308 				for (i = 0; i < CRK_MAXPARAM; i++) {
   1309 					struct crparam *kp =
   1310 						&(krp->krp_param[i]);
   1311 					if (kp->crp_p) {
   1312 						size = (kp->crp_nbits + 7) / 8;
   1313 						KASSERT(size > 0);
   1314 						memset(kp->crp_p, 0, size);
   1315 						kmem_free(kp->crp_p, size);
   1316 					}
   1317 				}
   1318 				pool_put(&cryptkop_pool, krp);
   1319 			}
   1320 		}
   1321 		error = 0;
   1322 	}
   1323 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
   1324 	return (error);
   1325 }
   1326 
   1327 static int
   1328 cryptodev_session(struct fcrypt *fcr, struct session_op *sop) {
   1329 	struct cryptoini cria, crie;
   1330 	struct enc_xform *txform = NULL;
   1331 	struct auth_hash *thash = NULL;
   1332 	struct csession *cse;
   1333 	u_int64_t sid;
   1334 	int error = 0;
   1335 
   1336 	/* XXX there must be a way to not embed the list of xforms here */
   1337 	switch (sop->cipher) {
   1338 	case 0:
   1339 		break;
   1340 	case CRYPTO_DES_CBC:
   1341 		txform = &enc_xform_des;
   1342 		break;
   1343 	case CRYPTO_3DES_CBC:
   1344 		txform = &enc_xform_3des;
   1345 		break;
   1346 	case CRYPTO_BLF_CBC:
   1347 		txform = &enc_xform_blf;
   1348 		break;
   1349 	case CRYPTO_CAST_CBC:
   1350 		txform = &enc_xform_cast5;
   1351 	case CRYPTO_SKIPJACK_CBC:
   1352 		txform = &enc_xform_skipjack;
   1353 		break;
   1354 	case CRYPTO_AES_CBC:
   1355 		txform = &enc_xform_rijndael128;
   1356 		break;
   1357 	case CRYPTO_NULL_CBC:
   1358 		txform = &enc_xform_null;
   1359 		break;
   1360 	case CRYPTO_ARC4:
   1361 		txform = &enc_xform_arc4;
   1362 		break;
   1363 	default:
   1364 		DPRINTF(("Invalid cipher %d\n", sop->cipher));
   1365 		return EINVAL;
   1366 	}
   1367 
   1368 	switch (sop->mac) {
   1369 	case 0:
   1370 		break;
   1371 	case CRYPTO_MD5_HMAC:
   1372 		thash = &auth_hash_hmac_md5;
   1373 		break;
   1374 	case CRYPTO_SHA1_HMAC:
   1375 		thash = &auth_hash_hmac_sha1;
   1376 		break;
   1377 	case CRYPTO_MD5_HMAC_96:
   1378 		thash = &auth_hash_hmac_md5_96;
   1379 		break;
   1380 	case CRYPTO_SHA1_HMAC_96:
   1381 		thash = &auth_hash_hmac_sha1_96;
   1382 		break;
   1383 	case CRYPTO_SHA2_HMAC:
   1384 		/* XXX switching on key length seems questionable */
   1385 		if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
   1386 			thash = &auth_hash_hmac_sha2_256;
   1387 		} else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
   1388 			thash = &auth_hash_hmac_sha2_384;
   1389 		} else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
   1390 			thash = &auth_hash_hmac_sha2_512;
   1391 		} else {
   1392 			DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
   1393 			return EINVAL;
   1394 		}
   1395 		break;
   1396 	case CRYPTO_RIPEMD160_HMAC:
   1397 		thash = &auth_hash_hmac_ripemd_160;
   1398 		break;
   1399 	case CRYPTO_RIPEMD160_HMAC_96:
   1400 		thash = &auth_hash_hmac_ripemd_160_96;
   1401 		break;
   1402 	case CRYPTO_MD5:
   1403 		thash = &auth_hash_md5;
   1404 		break;
   1405 	case CRYPTO_SHA1:
   1406 		thash = &auth_hash_sha1;
   1407 		break;
   1408 	case CRYPTO_NULL_HMAC:
   1409 		thash = &auth_hash_null;
   1410 		break;
   1411 	default:
   1412 		DPRINTF(("Invalid mac %d\n", sop->mac));
   1413 		return (EINVAL);
   1414 	}
   1415 
   1416 	memset(&crie, 0, sizeof(crie));
   1417 	memset(&cria, 0, sizeof(cria));
   1418 
   1419 	if (txform) {
   1420 		crie.cri_alg = txform->type;
   1421 		crie.cri_klen = sop->keylen * 8;
   1422 		if (sop->keylen > txform->maxkey ||
   1423 		    sop->keylen < txform->minkey) {
   1424 			DPRINTF(("keylen %d not in [%d,%d]\n",
   1425 				 sop->keylen, txform->minkey,
   1426 				 txform->maxkey));
   1427 			error = EINVAL ;
   1428 			goto bail;
   1429 		}
   1430 
   1431 		crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
   1432 		if ((error = copyin(sop->key, crie.cri_key,
   1433 				    crie.cri_klen / 8))) {
   1434 			goto bail;
   1435 		}
   1436 		if (thash) {
   1437 			crie.cri_next = &cria;	/* XXX forces enc then hash? */
   1438 		}
   1439 	}
   1440 
   1441 	if (thash) {
   1442 		cria.cri_alg = thash->type;
   1443 		cria.cri_klen = sop->mackeylen * 8;
   1444 		if (sop->mackeylen != thash->keysize) {
   1445 			DPRINTF(("mackeylen %d != keysize %d\n",
   1446 				 sop->mackeylen, thash->keysize));
   1447 			error = EINVAL;
   1448 			goto bail;
   1449 		}
   1450 		if (cria.cri_klen) {
   1451 			cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
   1452 					      M_WAITOK);
   1453 			if ((error = copyin(sop->mackey, cria.cri_key,
   1454 					    cria.cri_klen / 8))) {
   1455 				goto bail;
   1456 			}
   1457 		}
   1458 	}
   1459 	/* crypto_newsession requires that we hold the mutex. */
   1460 	mutex_spin_enter(&crypto_mtx);
   1461 	error = crypto_newsession(&sid, (txform ? &crie : &cria),
   1462 				  crypto_devallowsoft);
   1463 	if (!error) {
   1464 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
   1465 				cria.cri_key, cria.cri_klen, sop->cipher,
   1466 				sop->mac, txform, thash);
   1467 		if (cse != NULL) {
   1468 			sop->ses = cse->ses;
   1469 		} else {
   1470 			DPRINTF(("csecreate failed\n"));
   1471 			crypto_freesession(sid);
   1472 			error = EINVAL;
   1473 		}
   1474 	} else {
   1475 		DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
   1476 			 error));
   1477 	}
   1478 	mutex_spin_exit(&crypto_mtx);
   1479 bail:
   1480 	if (error) {
   1481 		if (crie.cri_key) {
   1482 			memset(crie.cri_key, 0, crie.cri_klen / 8);
   1483 			free(crie.cri_key, M_XDATA);
   1484 		}
   1485 		if (cria.cri_key) {
   1486 			memset(cria.cri_key, 0, cria.cri_klen / 8);
   1487 			free(cria.cri_key, M_XDATA);
   1488 		}
   1489 	}
   1490 	return error;
   1491 }
   1492 
   1493 static int
   1494 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
   1495 		   int count)
   1496 {
   1497 	int i;
   1498 
   1499 	for (i = 0; i < count; i++, sn_ops++) {
   1500 		struct session_op s_op;
   1501 		s_op.cipher =		sn_ops->cipher;
   1502 		s_op.mac =		sn_ops->mac;
   1503 		s_op.keylen =		sn_ops->keylen;
   1504 		s_op.key =		sn_ops->key;
   1505 		s_op.mackeylen =	sn_ops->mackeylen;
   1506 		s_op.mackey =		sn_ops->mackey;
   1507 
   1508 		sn_ops->status = cryptodev_session(fcr, &s_op);
   1509 		sn_ops->ses =		s_op.ses;
   1510 	}
   1511 
   1512 	return 0;
   1513 }
   1514 
   1515 static int
   1516 cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
   1517 {
   1518 	struct csession *cse;
   1519 	int req, error = 0;
   1520 
   1521 	mutex_spin_enter(&crypto_mtx);
   1522 	for(req = 0; req < count; req++) {
   1523 		cse = csefind(fcr, sesid[req]);
   1524 		if (cse == NULL)
   1525 			continue;
   1526 		csedelete(fcr, cse);
   1527 		error = csefree(cse);
   1528 	}
   1529 	mutex_spin_exit(&crypto_mtx);
   1530 	return 0;
   1531 }
   1532 
   1533 /*
   1534  * collect as many completed requests as are availble, or count completed requests
   1535  * whichever is less.
   1536  * return the number of requests.
   1537  */
   1538 static int
   1539 cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
   1540 		  int count)
   1541 {
   1542 	struct cryptop *crp = NULL;
   1543 	struct cryptkop *krp = NULL;
   1544 	struct csession *cse;
   1545 	int i, size, req = 0;
   1546 	int completed=0;
   1547 
   1548 	/* On stack so nobody else can grab them -- no locking */
   1549 	SLIST_HEAD(, cryptop) crp_delfree_l =
   1550 	    SLIST_HEAD_INITIALIZER(crp_delfree_l);
   1551 	SLIST_HEAD(, cryptkop) krp_delfree_l =
   1552 	    SLIST_HEAD_INITIALIZER(krp_delfree_l);
   1553 
   1554 	mutex_spin_enter(&crypto_mtx);
   1555 
   1556 	/* at this point we do not know which response user is requesting for
   1557 	 * (symmetric or asymmetric) so we copyout one from each i.e if the
   1558 	 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
   1559 	 * if 3 then 2 symmetric and 1 asymmetric and so on */
   1560 	for(; req < count ;) {
   1561 		crp = TAILQ_FIRST(&fcr->crp_ret_mq);
   1562 		if(crp) {
   1563 			cse = (struct csession *)crp->crp_opaque;
   1564 			crypt_res[req].reqid = crp->crp_reqid;
   1565 			crypt_res[req].opaque = crp->crp_usropaque;
   1566 			completed++;
   1567 			cse = csefind(fcr, cse->ses);
   1568 			if (cse == NULL) {
   1569 				DPRINTF(("csefind failed\n"));
   1570 				crypt_res[req].status = EINVAL;
   1571 				goto bail;
   1572 			}
   1573 
   1574 			if (crp->crp_etype != 0) {
   1575 				crypt_res[req].status = crp->crp_etype;
   1576 				goto bail;
   1577 			}
   1578 
   1579 			if (cse->error) {
   1580 				crypt_res[req].status = cse->error;
   1581 				goto bail;
   1582 			}
   1583 
   1584 			if (crp->dst &&
   1585 			    (crypt_res[req].status = copyout
   1586 						(crp->uio.uio_iov[0].iov_base,
   1587 			    			crp->dst, crp->len)))
   1588 				goto bail;
   1589 
   1590 			if (crp->mac &&
   1591 			    (crypt_res[req].status = copyout
   1592 						(crp->crp_mac, crp->mac,
   1593 			    			cse->thash->authsize)))
   1594 				goto bail;
   1595 bail:
   1596 			TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
   1597 			SLIST_INSERT_HEAD(&crp_delfree_l, crp,
   1598 					  crp_qun.crp_lnext);
   1599 			req++;
   1600 		}
   1601 
   1602 		if(req < count) {
   1603 			krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
   1604 			if (krp) {
   1605 				crypt_res[req].reqid = krp->krp_reqid;
   1606 				crypt_res[req].opaque = krp->krp_usropaque;
   1607 				completed++;
   1608 				if (krp->krp_status != 0) {
   1609 					DPRINTF(("cryptodev_key: "
   1610 						 "krp->krp_status"
   1611 						"0x%08x\n", krp->krp_status));
   1612 					crypt_res[req].status =
   1613 					    krp->krp_status;
   1614 					goto fail;
   1615 				}
   1616 
   1617 				for (i = krp->krp_iparams; i < krp->krp_iparams
   1618 						 + krp->krp_oparams; i++) {
   1619 					size = (krp->krp_param[i].crp_nbits
   1620 						+ 7) / 8;
   1621 					if (size == 0)
   1622 						continue;
   1623 					crypt_res[req].status = copyout
   1624 					    (krp->krp_param[i].crp_p,
   1625 					    krp->crk_param[i].crp_p, size);
   1626 					if (crypt_res[req].status) {
   1627 						DPRINTF(("cryptodev_key: "
   1628 							 "copyout oparam "
   1629 						  	 "%d failed, "
   1630 							 "error=%d\n",
   1631 							 i-krp->krp_iparams,
   1632 						  crypt_res[req].status));
   1633 						goto fail;
   1634 					}
   1635 				}
   1636 fail:
   1637 				TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
   1638 				/* not sure what to do for this */
   1639 				/* kop[req].crk_status = krp->krp_status; */
   1640 				SLIST_INSERT_HEAD(&krp_delfree_l, krp,
   1641 						  krp_qun.krp_lnext);
   1642 			}
   1643 			req++;
   1644 		}
   1645 	}
   1646 	mutex_spin_exit(&crypto_mtx);
   1647 
   1648 	while(!SLIST_EMPTY(&crp_delfree_l)) {
   1649 		crp = SLIST_FIRST(&crp_delfree_l);
   1650 		SLIST_REMOVE_HEAD(&crp_delfree_l, crp_qun.crp_lnext);
   1651 		kmem_free(crp->uio.uio_iov[0].iov_base,
   1652 			  crp->uio.uio_iov[0].iov_len);
   1653 		crypto_freereq(crp);
   1654 	}
   1655 
   1656 	while(!SLIST_EMPTY(&krp_delfree_l)) {
   1657 		krp = SLIST_FIRST(&krp_delfree_l);
   1658 		for (i = 0; i < CRK_MAXPARAM; i++) {
   1659 			struct crparam *kp = &(krp->krp_param[i]);
   1660 			if (kp->crp_p) {
   1661 			    size = (kp->crp_nbits + 7) / 8;
   1662 			    KASSERT(size > 0);
   1663 			    memset(kp->crp_p, 0, size);
   1664 			    kmem_free(kp->crp_p, size);
   1665 			}
   1666 		}
   1667 		SLIST_REMOVE_HEAD(&krp_delfree_l, krp_qun.krp_lnext);
   1668 		pool_put(&cryptkop_pool, krp);
   1669 	}
   1670 	return completed;
   1671 }
   1672 
   1673 static int
   1674 cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
   1675 {
   1676         struct cryptop *crp = NULL;
   1677         struct cryptkop *krp = NULL;
   1678         struct csession *cse;
   1679         int i, size, req = 0;
   1680 
   1681 	mutex_spin_enter(&crypto_mtx);
   1682 	/* Here we dont know for which request the user is requesting the
   1683 	 * response so checking in both the queues */
   1684 	TAILQ_FOREACH(crp, &fcr->crp_ret_mq, crp_next) {
   1685 		if(crp && (crp->crp_reqid == crypt_res->reqid)) {
   1686 			cse = (struct csession *)crp->crp_opaque;
   1687 		        crypt_res->opaque = crp->crp_usropaque;
   1688 			cse = csefind(fcr, cse->ses);
   1689 			if (cse == NULL) {
   1690 				DPRINTF(("csefind failed\n"));
   1691 				crypt_res->status = EINVAL;
   1692 				goto bail;
   1693 			}
   1694 
   1695 			if (crp->crp_etype != 0) {
   1696 				crypt_res->status = crp->crp_etype;
   1697 				goto bail;
   1698 			}
   1699 
   1700 			if (cse->error) {
   1701 				crypt_res->status = cse->error;
   1702 				goto bail;
   1703 			}
   1704 
   1705 			if (crp->dst &&
   1706 			    (crypt_res->status = copyout
   1707 						(crp->uio.uio_iov[0].iov_base,
   1708 						crp->dst, crp->len)))
   1709 				goto bail;
   1710 
   1711 			if (crp->mac &&
   1712 			    (crypt_res->status = copyout(crp->crp_mac,
   1713 					crp->mac, cse->thash->authsize)))
   1714 				goto bail;
   1715 bail:
   1716 			TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
   1717 
   1718 			mutex_spin_exit(&crypto_mtx);
   1719 			crypto_freereq(crp);
   1720 			return 0;
   1721 		}
   1722 	}
   1723 
   1724 	TAILQ_FOREACH(krp, &fcr->crp_ret_mkq, krp_next) {
   1725 		if(krp && (krp->krp_reqid == crypt_res->reqid)) {
   1726 			crypt_res[req].opaque = krp->krp_usropaque;
   1727 			if (krp->krp_status != 0) {
   1728 				DPRINTF(("cryptodev_key: "
   1729 					 "krp->krp_status 0x%08x\n",
   1730 					 krp->krp_status));
   1731 				crypt_res[req].status = krp->krp_status;
   1732 				goto fail;
   1733 			}
   1734 
   1735 			for (i = krp->krp_iparams; i < krp->krp_iparams
   1736 						+ krp->krp_oparams; i++) {
   1737 				size = (krp->krp_param[i].crp_nbits + 7) / 8;
   1738 				if (size == 0)
   1739 					continue;
   1740 				crypt_res[req].status = copyout
   1741 						(krp->krp_param[i].crp_p,
   1742 						krp->crk_param[i].crp_p, size);
   1743 				if (crypt_res[req].status) {
   1744 					DPRINTF(("cryptodev_key: copyout oparam"
   1745 						"%d failed, error=%d\n",
   1746 						i-krp->krp_iparams,
   1747 						crypt_result[req].status));
   1748 					goto fail;
   1749 				}
   1750 			}
   1751 fail:
   1752 			TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
   1753 			mutex_spin_exit(&crypto_mtx);
   1754 			/* not sure what to do for this */
   1755 			/* kop[req].crk_status = krp->krp_status; */
   1756 			for (i = 0; i < CRK_MAXPARAM; i++) {
   1757 				struct crparam *kp = &(krp->krp_param[i]);
   1758 				if (kp->crp_p) {
   1759 					size = (kp->crp_nbits + 7) / 8;
   1760 					KASSERT(size > 0);
   1761 					memset(kp->crp_p, 0, size);
   1762 					kmem_free(kp->crp_p, size);
   1763 				}
   1764 			}
   1765 			pool_put(&cryptkop_pool, krp);
   1766 			return 0;
   1767 		}
   1768 	}
   1769 	mutex_spin_exit(&crypto_mtx);
   1770 	return EINPROGRESS;
   1771 }
   1772 
   1773 static int
   1774 cryptof_poll(struct file *fp, int events)
   1775 {
   1776 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
   1777 
   1778 	if (!(events & (POLLIN | POLLRDNORM))) {
   1779 		/* only support read and POLLIN */
   1780 		return 0;
   1781 	}
   1782 
   1783 	if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
   1784 		/* no completed requests pending,
   1785 		 * save the poll for later (for selnotify()). */
   1786 		selrecord(curlwp, &fcr->sinfo);
   1787 		return 0;
   1788 	} else {
   1789 		/* let the app(s) know that there are completed requests */
   1790 		return events & (POLLIN | POLLRDNORM);
   1791 	}
   1792 }
   1793 
   1794 /*
   1795  * Pseudo-device initialization routine for /dev/crypto
   1796  */
   1797 void	cryptoattach(int);
   1798 
   1799 void
   1800 cryptoattach(int num)
   1801 {
   1802 	pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
   1803 		  NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
   1804 	pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
   1805 		  NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
   1806 
   1807 	/*
   1808 	 * Preallocate space for 64 users, with 5 sessions each.
   1809 	 * (consider that a TLS protocol session requires at least
   1810 	 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
   1811 	 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
   1812 	 * consuming one session here for each algorithm.
   1813 	 */
   1814 	pool_prime(&fcrpl, 64);
   1815 	pool_prime(&csepl, 64 * 5);
   1816 }
   1817