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