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