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