xref: /dragonfly/sys/opencrypto/cryptodev.c (revision ffe53622)
1 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.41 2009/09/04 09:48:18 pjd Exp $	*/
2 /*	$OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 Theo de Raadt
6  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *   notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in the
16  *   documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *   derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Effort sponsored in part by the Defense Advanced Research Projects
32  * Agency (DARPA) and Air Force Research Laboratory, Air Force
33  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 #include <sys/mbuf.h>
41 #include <sys/lock.h>
42 #include <sys/sysctl.h>
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/errno.h>
46 #include <sys/uio.h>
47 #include <sys/random.h>
48 #include <sys/conf.h>
49 #include <sys/module.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/proc.h>
53 
54 #include <sys/file2.h>
55 #include <sys/thread2.h>
56 
57 #include <opencrypto/cryptodev.h>
58 #include <opencrypto/xform.h>
59 
60 struct csession {
61 	TAILQ_ENTRY(csession) next;
62 	u_int64_t	sid;
63 	u_int32_t	ses;
64 	struct lock	lock;
65 
66 	u_int32_t	cipher;
67 	struct enc_xform *txform;
68 	u_int32_t	mac;
69 	struct auth_hash *thash;
70 
71 	caddr_t		key;
72 	int		keylen;
73 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
74 
75 	caddr_t		mackey;
76 	int		mackeylen;
77 
78 	struct iovec	iovec;
79 	struct uio	uio;
80 	int		error;
81 	uint32_t	busy;
82 };
83 
84 struct fcrypt {
85 	TAILQ_HEAD(csessionlist, csession) csessions;
86 	int		sesn;
87 };
88 
89 static	int cryptof_rw(struct file *fp, struct uio *uio,
90 		    struct ucred *cred, int flags);
91 static	int cryptof_ioctl(struct file *, u_long, caddr_t,
92 		    struct ucred *, struct sysmsg *);
93 static	int cryptof_kqfilter(struct file *, struct knote *);
94 static	int cryptof_stat(struct file *, struct stat *, struct ucred *);
95 static	int cryptof_close(struct file *);
96 
97 static struct fileops cryptofops = {
98     .fo_read = cryptof_rw,
99     .fo_write = cryptof_rw,
100     .fo_ioctl = cryptof_ioctl,
101     .fo_kqfilter = cryptof_kqfilter,
102     .fo_stat = cryptof_stat,
103     .fo_close = cryptof_close,
104     .fo_shutdown = nofo_shutdown
105 };
106 
107 static struct csession *csefind(struct fcrypt *, u_int, int *);
108 static void csedrop(struct csession *);
109 static int csedelete(struct fcrypt *, struct csession *);
110 static struct csession *cseadd(struct fcrypt *, struct csession *);
111 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
112     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
113     struct auth_hash *);
114 static int csefree(struct csession *);
115 
116 static	int cryptodev_op(struct csession *, struct crypt_op *, struct ucred *);
117 static	int cryptodev_key(struct crypt_kop *);
118 static	int cryptodev_find(struct crypt_find_op *);
119 
120 static struct lock cryptodev_lock = LOCK_INITIALIZER("cryptodev", 0, 0);
121 
122 static int
123 cryptof_rw(
124 	struct file *fp,
125 	struct uio *uio,
126 	struct ucred *active_cred,
127 	int flags)
128 {
129 	return (EIO);
130 }
131 
132 /*
133  * Check a crypto identifier to see if it requested
134  * a software device/driver.  This can be done either
135  * by device name/class or through search constraints.
136  */
137 static int
138 checkforsoftware(int crid)
139 {
140 	if (crid & CRYPTOCAP_F_SOFTWARE)
141 		return EINVAL;		/* XXX */
142 	if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
143 	    (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
144 		return EINVAL;		/* XXX */
145 	return 0;
146 }
147 
148 /* ARGSUSED */
149 static int
150 cryptof_ioctl(struct file *fp, u_long cmd, caddr_t data,
151 	      struct ucred *cred, struct sysmsg *msg)
152 {
153 #define	SES2(p)	((struct session2_op *)p)
154 	struct cryptoini cria, crie;
155 	struct fcrypt *fcr = fp->f_data;
156 	struct csession *cse;
157 	struct session_op *sop;
158 	struct crypt_op *cop;
159 	struct enc_xform *txform = NULL;
160 	struct auth_hash *thash = NULL;
161 	struct crypt_kop *kop;
162 	u_int64_t sid;
163 	u_int32_t ses;
164 	int error = 0, crid;
165 
166 	switch (cmd) {
167 	case CIOCGSESSION:
168 	case CIOCGSESSION2:
169 		sop = (struct session_op *)data;
170 		switch (sop->cipher) {
171 		case 0:
172 			break;
173 		case CRYPTO_DES_CBC:
174 			txform = &enc_xform_des;
175 			break;
176 		case CRYPTO_3DES_CBC:
177 			txform = &enc_xform_3des;
178 			break;
179 		case CRYPTO_BLF_CBC:
180 			txform = &enc_xform_blf;
181 			break;
182 		case CRYPTO_CAST_CBC:
183 			txform = &enc_xform_cast5;
184 			break;
185 		case CRYPTO_SKIPJACK_CBC:
186 			txform = &enc_xform_skipjack;
187 			break;
188 		case CRYPTO_AES_CBC:
189 			txform = &enc_xform_rijndael128;
190 			break;
191 		case CRYPTO_AES_XTS:
192 			txform = &enc_xform_aes_xts;
193 			break;
194 		case CRYPTO_AES_CTR:
195 			txform = &enc_xform_aes_ctr;
196 			break;
197 		case CRYPTO_NULL_CBC:
198 			txform = &enc_xform_null;
199 			break;
200 		case CRYPTO_ARC4:
201 			txform = &enc_xform_arc4;
202 			break;
203 		case CRYPTO_CAMELLIA_CBC:
204 			txform = &enc_xform_camellia;
205 			break;
206 		case CRYPTO_TWOFISH_CBC:
207 			txform = &enc_xform_twofish;
208 			break;
209 		case CRYPTO_SERPENT_CBC:
210 			txform = &enc_xform_serpent;
211 			break;
212 		case CRYPTO_TWOFISH_XTS:
213 			txform = &enc_xform_twofish_xts;
214 			break;
215 		case CRYPTO_SERPENT_XTS:
216 			txform = &enc_xform_serpent_xts;
217 			break;
218 		default:
219 			return (EINVAL);
220 		}
221 
222 		switch (sop->mac) {
223 		case 0:
224 			break;
225 		case CRYPTO_MD5_HMAC:
226 			thash = &auth_hash_hmac_md5;
227 			break;
228 		case CRYPTO_SHA1_HMAC:
229 			thash = &auth_hash_hmac_sha1;
230 			break;
231 		case CRYPTO_SHA2_256_HMAC:
232 			thash = &auth_hash_hmac_sha2_256;
233 			break;
234 		case CRYPTO_SHA2_384_HMAC:
235 			thash = &auth_hash_hmac_sha2_384;
236 			break;
237 		case CRYPTO_SHA2_512_HMAC:
238 			thash = &auth_hash_hmac_sha2_512;
239 			break;
240 		case CRYPTO_RIPEMD160_HMAC:
241 			thash = &auth_hash_hmac_ripemd_160;
242 			break;
243 #ifdef notdef
244 		case CRYPTO_MD5:
245 			thash = &auth_hash_md5;
246 			break;
247 		case CRYPTO_SHA1:
248 			thash = &auth_hash_sha1;
249 			break;
250 #endif
251 		case CRYPTO_NULL_HMAC:
252 			thash = &auth_hash_null;
253 			break;
254 		default:
255 			return (EINVAL);
256 		}
257 
258 		bzero(&crie, sizeof(crie));
259 		bzero(&cria, sizeof(cria));
260 
261 		if (txform) {
262 			crie.cri_alg = txform->type;
263 			crie.cri_klen = sop->keylen * 8;
264 			if (sop->keylen > txform->maxkey ||
265 			    sop->keylen < txform->minkey) {
266 				error = EINVAL;
267 				goto bail;
268 			}
269 
270 			crie.cri_key = kmalloc(crie.cri_klen / 8,
271 			    M_XDATA, M_WAITOK);
272 			if ((error = copyin(sop->key, crie.cri_key,
273 			    crie.cri_klen / 8)))
274 				goto bail;
275 			if (thash)
276 				crie.cri_next = &cria;
277 		}
278 
279 		if (thash) {
280 			cria.cri_alg = thash->type;
281 			cria.cri_klen = sop->mackeylen * 8;
282 			if (sop->mackeylen != thash->keysize) {
283 				error = EINVAL;
284 				goto bail;
285 			}
286 
287 			if (cria.cri_klen) {
288 				cria.cri_key = kmalloc(cria.cri_klen / 8,
289 				    M_XDATA, M_WAITOK);
290 				if ((error = copyin(sop->mackey, cria.cri_key,
291 				    cria.cri_klen / 8)))
292 					goto bail;
293 			}
294 		}
295 
296 		/* NB: CIOGSESSION2 has the crid */
297 		if (cmd == CIOCGSESSION2) {
298 			crid = SES2(sop)->crid;
299 			error = checkforsoftware(crid);
300 			if (error)
301 				goto bail;
302 		} else {
303 			crid = CRYPTOCAP_F_HARDWARE;
304 			if (crypto_devallowsoft)
305 				crid |= CRYPTOCAP_F_SOFTWARE;
306 		}
307 		error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
308 		if (error)
309 			goto bail;
310 
311 		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
312 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
313 				cria.cri_key, cria.cri_klen, sop->cipher,
314 				sop->mac, txform, thash);
315 		lockmgr(&cryptodev_lock, LK_RELEASE);
316 
317 		if (cse == NULL) {
318 			crypto_freesession(sid);
319 			error = EINVAL;
320 			goto bail;
321 		}
322 		sop->ses = cse->ses;
323 		if (cmd == CIOCGSESSION2) {
324 			/* return hardware/driver id */
325 			SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
326 		}
327 bail:
328 		if (error) {
329 			if (crie.cri_key) {
330 				bzero(crie.cri_key, crie.cri_klen / 8);
331 				kfree(crie.cri_key, M_XDATA);
332 			}
333 			if (cria.cri_key) {
334 				bzero(cria.cri_key, cria.cri_klen / 8);
335 				kfree(cria.cri_key, M_XDATA);
336 			}
337 		}
338 		break;
339 	case CIOCFSESSION:
340 		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
341 		ses = *(u_int32_t *)data;
342 		cse = csefind(fcr, ses, &error);
343 		if (cse) {
344 			csedelete(fcr, cse);
345 			error = csefree(cse);
346 		}
347 		lockmgr(&cryptodev_lock, LK_RELEASE);
348 		break;
349 	case CIOCCRYPT:
350 		cop = (struct crypt_op *)data;
351 		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
352 		cse = csefind(fcr, cop->ses, &error);
353 		lockmgr(&cryptodev_lock, LK_RELEASE);
354 		if (cse) {
355 			error = cryptodev_op(cse, cop, cred);
356 			csedrop(cse);
357 		}
358 		break;
359 	case CIOCKEY:
360 	case CIOCKEY2:
361 		if (!crypto_userasymcrypto)
362 			return (EPERM);		/* XXX compat? */
363 		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
364 		kop = (struct crypt_kop *)data;
365 		if (cmd == CIOCKEY) {
366 			/* NB: crypto core enforces s/w driver use */
367 			kop->crk_crid =
368 			    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
369 		}
370 		error = cryptodev_key(kop);
371 		lockmgr(&cryptodev_lock, LK_RELEASE);
372 		break;
373 	case CIOCASYMFEAT:
374 		if (!crypto_userasymcrypto) {
375 			/*
376 			 * NB: if user asym crypto operations are
377 			 * not permitted return "no algorithms"
378 			 * so well-behaved applications will just
379 			 * fallback to doing them in software.
380 			 */
381 			*(int *)data = 0;
382 		} else {
383 			error = crypto_getfeat((int *)data);
384 		}
385 		break;
386 	case CIOCFINDDEV:
387 		error = cryptodev_find((struct crypt_find_op *)data);
388 		break;
389 	default:
390 		error = EINVAL;
391 		break;
392 	}
393 	return (error);
394 #undef SES2
395 }
396 
397 static int cryptodev_cb(void *);
398 
399 static int
400 cryptodev_op(struct csession *cse, struct crypt_op *cop,
401 	     struct ucred *active_cred)
402 {
403 	struct cryptop *crp = NULL;
404 	struct cryptodesc *crde = NULL, *crda = NULL;
405 	int error;
406 
407 	if (cop->len > 256*1024-4)
408 		return (E2BIG);
409 
410 	if (cse->txform) {
411 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
412 			return (EINVAL);
413 	}
414 
415 	bzero(&cse->uio, sizeof(cse->uio));
416 	cse->uio.uio_iov = &cse->iovec;
417 	cse->uio.uio_iovcnt = 1;
418 	cse->uio.uio_offset = 0;
419 	cse->uio.uio_resid = cop->len;
420 	cse->uio.uio_segflg = UIO_SYSSPACE;
421 	cse->uio.uio_rw = UIO_WRITE;
422 	/* XXX: not sure, was td, now curthread? */
423 	cse->uio.uio_td = curthread;
424 	cse->uio.uio_iov[0].iov_len = cop->len;
425 	if (cse->thash) {
426 		cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
427 		cse->uio.uio_resid += cse->thash->hashsize;
428 	}
429 	cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
430 					       M_XDATA, M_WAITOK);
431 
432 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
433 	if (crp == NULL) {
434 		error = ENOMEM;
435 		goto bail;
436 	}
437 
438 	if (cse->thash) {
439 		crda = crp->crp_desc;
440 		if (cse->txform)
441 			crde = crda->crd_next;
442 	} else {
443 		if (cse->txform)
444 			crde = crp->crp_desc;
445 		else {
446 			error = EINVAL;
447 			goto bail;
448 		}
449 	}
450 
451 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
452 		goto bail;
453 
454 	if (crda) {
455 		crda->crd_skip = 0;
456 		crda->crd_len = cop->len;
457 		crda->crd_inject = cop->len;
458 
459 		crda->crd_alg = cse->mac;
460 		crda->crd_key = cse->mackey;
461 		crda->crd_klen = cse->mackeylen * 8;
462 	}
463 
464 	if (crde) {
465 		if (cop->op == COP_ENCRYPT)
466 			crde->crd_flags |= CRD_F_ENCRYPT;
467 		else
468 			crde->crd_flags &= ~CRD_F_ENCRYPT;
469 		crde->crd_len = cop->len;
470 		crde->crd_inject = 0;
471 
472 		crde->crd_alg = cse->cipher;
473 		crde->crd_key = cse->key;
474 		crde->crd_klen = cse->keylen * 8;
475 	}
476 
477 	crp->crp_ilen = cop->len;
478 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
479 		       | (cop->flags & COP_F_BATCH);
480 	crp->crp_buf = (caddr_t)&cse->uio;
481 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
482 	crp->crp_sid = cse->sid;
483 	crp->crp_opaque = (void *)cse;
484 
485 	if (cop->iv) {
486 		if (crde == NULL) {
487 			error = EINVAL;
488 			goto bail;
489 		}
490 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
491 			error = EINVAL;
492 			goto bail;
493 		}
494 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
495 			goto bail;
496 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
497 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
498 		crde->crd_skip = 0;
499 	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
500 		crde->crd_skip = 0;
501 	} else if (crde) {
502 		crde->crd_flags |= CRD_F_IV_PRESENT;
503 		crde->crd_skip = cse->txform->blocksize;
504 		crde->crd_len -= cse->txform->blocksize;
505 	}
506 
507 	if (cop->mac && crda == NULL) {
508 		error = EINVAL;
509 		goto bail;
510 	}
511 
512 again:
513 	/*
514 	 * Let the dispatch run unlocked, then, interlock against the
515 	 * callback before checking if the operation completed and going
516 	 * to sleep.  This insures drivers don't inherit our lock which
517 	 * results in a lock order reversal between crypto_dispatch forced
518 	 * entry and the crypto_done callback into us.
519 	 */
520 	error = crypto_dispatch(crp);
521 	lockmgr(&cse->lock, LK_EXCLUSIVE);
522 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
523 		error = lksleep(crp, &cse->lock, 0, "crydev", 0);
524 	lockmgr(&cse->lock, LK_RELEASE);
525 
526 	if (error != 0)
527 		goto bail;
528 
529 	if (crp->crp_etype == EAGAIN) {
530 		crp->crp_etype = 0;
531 		crp->crp_flags &= ~CRYPTO_F_DONE;
532 		goto again;
533 	}
534 
535 	if (crp->crp_etype != 0) {
536 		error = crp->crp_etype;
537 		goto bail;
538 	}
539 
540 	if (cse->error) {
541 		error = cse->error;
542 		goto bail;
543 	}
544 
545 	if (cop->dst &&
546 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
547 		goto bail;
548 
549 	if (cop->mac &&
550 	    (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
551 	    cop->mac, cse->thash->hashsize)))
552 		goto bail;
553 
554 bail:
555 	if (crp)
556 		crypto_freereq(crp);
557 	if (cse->uio.uio_iov[0].iov_base)
558 		kfree(cse->uio.uio_iov[0].iov_base, M_XDATA);
559 
560 	return (error);
561 }
562 
563 static int
564 cryptodev_cb(void *op)
565 {
566 	struct cryptop *crp = (struct cryptop *) op;
567 	struct csession *cse = (struct csession *)crp->crp_opaque;
568 
569 	lockmgr(&cse->lock, LK_EXCLUSIVE);
570 	cse->error = crp->crp_etype;
571 	wakeup_one(crp);
572 	lockmgr(&cse->lock, LK_RELEASE);
573 	return (0);
574 }
575 
576 static int
577 cryptodevkey_cb(void *op)
578 {
579 	struct cryptkop *krp = (struct cryptkop *) op;
580 
581 	wakeup_one(krp);
582 	return (0);
583 }
584 
585 static int
586 cryptodev_key(struct crypt_kop *kop)
587 {
588 	struct cryptkop *krp = NULL;
589 	int error = EINVAL;
590 	int in, out, size, i;
591 
592 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
593 		return (EFBIG);
594 	}
595 
596 	in = kop->crk_iparams;
597 	out = kop->crk_oparams;
598 	switch (kop->crk_op) {
599 	case CRK_MOD_EXP:
600 		if (in == 3 && out == 1)
601 			break;
602 		return (EINVAL);
603 	case CRK_MOD_EXP_CRT:
604 		if (in == 6 && out == 1)
605 			break;
606 		return (EINVAL);
607 	case CRK_DSA_SIGN:
608 		if (in == 5 && out == 2)
609 			break;
610 		return (EINVAL);
611 	case CRK_DSA_VERIFY:
612 		if (in == 7 && out == 0)
613 			break;
614 		return (EINVAL);
615 	case CRK_DH_COMPUTE_KEY:
616 		if (in == 3 && out == 1)
617 			break;
618 		return (EINVAL);
619 	default:
620 		return (EINVAL);
621 	}
622 
623 	krp = (struct cryptkop *)kmalloc(sizeof *krp, M_XDATA, M_WAITOK | M_ZERO);
624 	krp->krp_op = kop->crk_op;
625 	krp->krp_status = kop->crk_status;
626 	krp->krp_iparams = kop->crk_iparams;
627 	krp->krp_oparams = kop->crk_oparams;
628 	krp->krp_crid = kop->crk_crid;
629 	krp->krp_status = 0;
630 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
631 
632 	for (i = 0; i < CRK_MAXPARAM; i++) {
633 		if (kop->crk_param[i].crp_nbits > 65536)
634 			/* Limit is the same as in OpenBSD */
635 			goto fail;
636 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
637 	}
638 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
639 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
640 		if (size == 0)
641 			continue;
642 		krp->krp_param[i].crp_p = kmalloc(size, M_XDATA, M_WAITOK);
643 		if (i >= krp->krp_iparams)
644 			continue;
645 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
646 		if (error)
647 			goto fail;
648 	}
649 
650 	error = crypto_kdispatch(krp);
651 	if (error)
652 		goto fail;
653 	error = tsleep(krp, 0, "crydev", 0);
654 	if (error) {
655 		/* XXX can this happen?  if so, how do we recover? */
656 		goto fail;
657 	}
658 
659 	kop->crk_crid = krp->krp_crid;		/* device that did the work */
660 	if (krp->krp_status != 0) {
661 		error = krp->krp_status;
662 		goto fail;
663 	}
664 
665 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
666 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
667 		if (size == 0)
668 			continue;
669 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
670 		if (error)
671 			goto fail;
672 	}
673 
674 fail:
675 	if (krp) {
676 		kop->crk_status = krp->krp_status;
677 		for (i = 0; i < CRK_MAXPARAM; i++) {
678 			if (krp->krp_param[i].crp_p) {
679 				bzero(krp->krp_param[i].crp_p,
680 				    (krp->krp_param[i].crp_nbits + 7) / 8);
681 				kfree(krp->krp_param[i].crp_p, M_XDATA);
682 			}
683 		}
684 		kfree(krp, M_XDATA);
685 	}
686 	return (error);
687 }
688 
689 static int
690 cryptodev_find(struct crypt_find_op *find)
691 {
692 	device_t dev;
693 
694 	if (find->crid != -1) {
695 		dev = crypto_find_device_byhid(find->crid);
696 		if (dev == NULL)
697 			return (ENOENT);
698 		strlcpy(find->name, device_get_nameunit(dev),
699 		    sizeof(find->name));
700 	} else {
701 		find->crid = crypto_find_driver(find->name);
702 		if (find->crid == -1)
703 			return (ENOENT);
704 	}
705 	return (0);
706 }
707 
708 /*
709  * MPSAFE
710  */
711 static int
712 cryptof_kqfilter(struct file *fp, struct knote *kn)
713 {
714 
715 	return (0);
716 }
717 
718 /*
719  * MPSAFE
720  */
721 static int
722 cryptof_stat(struct file *fp, struct stat *sb, struct ucred *cred)
723 {
724 	return (EOPNOTSUPP);
725 }
726 
727 static int
728 cryptof_close(struct file *fp)
729 {
730 	struct fcrypt *fcr = fp->f_data;
731 	struct csession *cse;
732 
733 	lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
734 	fcr = (struct fcrypt *)fp->f_data;
735 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
736 		TAILQ_REMOVE(&fcr->csessions, cse, next);
737 		(void)csefree(cse);
738 	}
739 	fp->f_data = NULL;
740 	lockmgr(&cryptodev_lock, LK_RELEASE);
741 
742 	kfree(fcr, M_XDATA);
743 	return (0);
744 }
745 
746 static struct csession *
747 csefind(struct fcrypt *fcr, u_int ses, int *errorp)
748 {
749 	struct csession *cse;
750 
751 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
752 		if (cse->ses == ses) {
753 			if (cse->busy) {
754 				kprintf("csefind: cse %p BUSY\n", cse);
755 				*errorp = EBUSY;
756 				return NULL;
757 			}
758 			cse->busy = 1;
759 			return (cse);
760 		}
761 	}
762 	*errorp = EINVAL;
763 	return (NULL);
764 }
765 
766 static void
767 csedrop(struct csession *cse)
768 {
769 	cse->busy = 0;
770 }
771 
772 static int
773 csedelete(struct fcrypt *fcr, struct csession *cse_del)
774 {
775 	struct csession *cse;
776 
777 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
778 		if (cse == cse_del) {
779 			TAILQ_REMOVE(&fcr->csessions, cse, next);
780 			return (1);
781 		}
782 	}
783 	return (0);
784 }
785 
786 static struct csession *
787 cseadd(struct fcrypt *fcr, struct csession *cse)
788 {
789 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
790 	cse->ses = fcr->sesn++;
791 	return (cse);
792 }
793 
794 struct csession *
795 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
796     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
797     struct enc_xform *txform, struct auth_hash *thash)
798 {
799 	struct csession *cse;
800 
801 	cse = kmalloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
802 	lockinit(&cse->lock, "cryptodev", 0, LK_CANRECURSE);
803 	cse->key = key;
804 	cse->keylen = keylen/8;
805 	cse->mackey = mackey;
806 	cse->mackeylen = mackeylen/8;
807 	cse->sid = sid;
808 	cse->cipher = cipher;
809 	cse->mac = mac;
810 	cse->txform = txform;
811 	cse->thash = thash;
812 	cseadd(fcr, cse);
813 	return (cse);
814 }
815 
816 static int
817 csefree(struct csession *cse)
818 {
819 	int error;
820 
821 	error = crypto_freesession(cse->sid);
822 	lockuninit(&cse->lock);
823 	if (cse->key)
824 		kfree(cse->key, M_XDATA);
825 	if (cse->mackey)
826 		kfree(cse->mackey, M_XDATA);
827 	kfree(cse, M_XDATA);
828 	return (error);
829 }
830 
831 static int
832 cryptoopen(struct dev_open_args *ap)
833 {
834 	return (0);
835 }
836 
837 static int
838 cryptoread(struct dev_read_args *ap)
839 {
840 	return (EIO);
841 }
842 
843 static int
844 cryptowrite(struct dev_write_args *ap)
845 {
846 	return (EIO);
847 }
848 
849 static int
850 cryptoioctl(struct dev_ioctl_args *ap)
851 {
852 	struct thread *td = curthread;
853 	struct file *f;
854 	struct fcrypt *fcr;
855 	int fd, error;
856 
857 	switch (ap->a_cmd) {
858 	case CRIOGET:
859 		fcr = kmalloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
860 		TAILQ_INIT(&fcr->csessions);
861 		fcr->sesn = 0;
862 
863 		KKASSERT(td->td_lwp);
864 		error = falloc(td->td_lwp, &f, &fd);
865 
866 		if (error) {
867 			kfree(fcr, M_XDATA);
868 			return (error);
869 		}
870 		/* falloc automatically provides an extra reference to 'f'. */
871 		f->f_flag = FREAD | FWRITE;
872 		f->f_type = DTYPE_CRYPTO;
873 		f->f_ops = &cryptofops;
874 		f->f_data = fcr;
875 		fsetfd(td->td_proc->p_fd, f, fd);
876 		*(u_int32_t *)ap->a_data = fd;
877 		fdrop(f);
878 
879 		break;
880 	case CRIOFINDDEV:
881 		error = cryptodev_find((struct crypt_find_op *)ap->a_data);
882 		break;
883 	case CRIOASYMFEAT:
884 		error = crypto_getfeat((int *)ap->a_data);
885 		break;
886 	default:
887 		error = EINVAL;
888 		break;
889 	}
890 	return (error);
891 }
892 
893 static struct dev_ops crypto_ops = {
894 	{ "crypto", 0, D_MPSAFE },
895 	.d_open =	cryptoopen,
896 	.d_read =	cryptoread,
897 	.d_write =	cryptowrite,
898 	.d_ioctl =	cryptoioctl,
899 };
900 
901 /*
902  * Initialization code, both for static and dynamic loading.
903  */
904 static int
905 cryptodev_modevent(module_t mod, int type, void *unused)
906 {
907 	switch (type) {
908 	case MOD_LOAD:
909 		if (bootverbose)
910 			kprintf("crypto: <crypto device>\n");
911 		make_dev(&crypto_ops, 0, UID_ROOT, GID_WHEEL,
912 			 0666, "crypto");
913 		return 0;
914 	case MOD_UNLOAD:
915 		/*XXX disallow if active sessions */
916 		//dev_ops_remove(&crypto_ops, 0, 0);
917 		dev_ops_remove_all(&crypto_ops);
918 		return 0;
919 	}
920 	return EINVAL;
921 }
922 
923 static moduledata_t cryptodev_mod = {
924 	"cryptodev",
925 	cryptodev_modevent,
926 	0
927 };
928 MODULE_VERSION(cryptodev, 1);
929 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
930 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
931