xref: /dragonfly/sys/opencrypto/cryptodev.c (revision 956939d5)
1 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
2 /*	$DragonFly: src/sys/opencrypto/cryptodev.c,v 1.23 2008/01/06 16:55:53 swildner Exp $	*/
3 /*	$OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $	*/
4 
5 /*
6  * Copyright (c) 2001 Theo de Raadt
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 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/sysctl.h>
42 #include <sys/file.h>
43 #include <sys/lock.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/device.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/proc.h>
53 #include <sys/file2.h>
54 #include <sys/thread2.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 
64 	u_int32_t	cipher;
65 	struct enc_xform *txform;
66 	u_int32_t	mac;
67 	struct auth_hash *thash;
68 
69 	caddr_t		key;
70 	int		keylen;
71 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
72 
73 	caddr_t		mackey;
74 	int		mackeylen;
75 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
76 
77 	struct iovec	iovec[UIO_MAXIOV];
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);
89 static	int cryptof_ioctl(struct file *, u_long, caddr_t,
90 		    struct ucred *, struct sysmsg *);
91 static	int cryptof_poll(struct file *, int, struct ucred *);
92 static	int cryptof_kqfilter(struct file *, struct knote *);
93 static	int cryptof_stat(struct file *, struct stat *, struct ucred *);
94 static	int cryptof_close(struct file *);
95 
96 static struct fileops cryptofops = {
97     .fo_read = cryptof_rw,
98     .fo_write = cryptof_rw,
99     .fo_ioctl = cryptof_ioctl,
100     .fo_poll = cryptof_poll,
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);
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 static	int cryptodev_key(struct crypt_kop *);
117 
118 /*
119  * MPSAFE
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  * MPALMOSTSAFE - acquires mplock
133  */
134 static int
135 cryptof_ioctl(struct file *fp, u_long cmd, caddr_t data,
136 	      struct ucred *cred, struct sysmsg *msg)
137 {
138 	struct cryptoini cria, crie;
139 	struct fcrypt *fcr;
140 	struct csession *cse;
141 	struct session_op *sop;
142 	struct crypt_op *cop;
143 	struct enc_xform *txform = NULL;
144 	struct auth_hash *thash = NULL;
145 	u_int64_t sid;
146 	u_int32_t ses;
147 	int error = 0;
148 
149 	get_mplock();
150 	fcr = (struct fcrypt *)fp->f_data;
151 
152 	switch (cmd) {
153 	case CIOCGSESSION:
154 		sop = (struct session_op *)data;
155 		switch (sop->cipher) {
156 		case 0:
157 			break;
158 		case CRYPTO_DES_CBC:
159 			txform = &enc_xform_des;
160 			break;
161 		case CRYPTO_3DES_CBC:
162 			txform = &enc_xform_3des;
163 			break;
164 		case CRYPTO_BLF_CBC:
165 			txform = &enc_xform_blf;
166 			break;
167 		case CRYPTO_CAST_CBC:
168 			txform = &enc_xform_cast5;
169 			break;
170 		case CRYPTO_SKIPJACK_CBC:
171 			txform = &enc_xform_skipjack;
172 			break;
173 		case CRYPTO_AES_CBC:
174 			txform = &enc_xform_rijndael128;
175 			break;
176 		case CRYPTO_NULL_CBC:
177 			txform = &enc_xform_null;
178 			break;
179 		case CRYPTO_ARC4:
180 			txform = &enc_xform_arc4;
181 			break;
182 		default:
183 			error = EINVAL;
184 			break;
185 		}
186 		if (error)
187 			break;
188 
189 		switch (sop->mac) {
190 		case 0:
191 			break;
192 		case CRYPTO_MD5_HMAC:
193 			thash = &auth_hash_hmac_md5_96;
194 			break;
195 		case CRYPTO_SHA1_HMAC:
196 			thash = &auth_hash_hmac_sha1_96;
197 			break;
198 		case CRYPTO_SHA2_HMAC:
199 			if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
200 				thash = &auth_hash_hmac_sha2_256;
201 			else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
202 				thash = &auth_hash_hmac_sha2_384;
203 			else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
204 				thash = &auth_hash_hmac_sha2_512;
205 			else {
206 				error = EINVAL;
207 				break;
208 			}
209 			break;
210 		case CRYPTO_RIPEMD160_HMAC:
211 			thash = &auth_hash_hmac_ripemd_160_96;
212 			break;
213 #ifdef notdef
214 		case CRYPTO_MD5:
215 			thash = &auth_hash_md5;
216 			break;
217 		case CRYPTO_SHA1:
218 			thash = &auth_hash_sha1;
219 			break;
220 #endif
221 		case CRYPTO_NULL_HMAC:
222 			thash = &auth_hash_null;
223 			break;
224 		default:
225 			error = EINVAL;
226 			break;
227 		}
228 		if (error)
229 			break;
230 
231 		bzero(&crie, sizeof(crie));
232 		bzero(&cria, sizeof(cria));
233 
234 		if (txform) {
235 			crie.cri_alg = txform->type;
236 			crie.cri_klen = sop->keylen * 8;
237 			if (sop->keylen > txform->maxkey ||
238 			    sop->keylen < txform->minkey) {
239 				error = EINVAL;
240 				goto bail;
241 			}
242 
243 			MALLOC(crie.cri_key, u_int8_t *,
244 			    crie.cri_klen / 8, M_XDATA, M_WAITOK);
245 			if ((error = copyin(sop->key, crie.cri_key,
246 			    crie.cri_klen / 8)))
247 				goto bail;
248 			if (thash)
249 				crie.cri_next = &cria;
250 		}
251 
252 		if (thash) {
253 			cria.cri_alg = thash->type;
254 			cria.cri_klen = sop->mackeylen * 8;
255 			if (sop->mackeylen != thash->keysize) {
256 				error = EINVAL;
257 				goto bail;
258 			}
259 
260 			if (cria.cri_klen) {
261 				MALLOC(cria.cri_key, u_int8_t *,
262 				    cria.cri_klen / 8, M_XDATA, M_WAITOK);
263 				if ((error = copyin(sop->mackey, cria.cri_key,
264 				    cria.cri_klen / 8)))
265 					goto bail;
266 			}
267 		}
268 
269 		error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
270 		if (error)
271 			goto bail;
272 
273 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
274 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
275 		    thash);
276 
277 		if (cse == NULL) {
278 			crypto_freesession(sid);
279 			error = EINVAL;
280 			goto bail;
281 		}
282 		sop->ses = cse->ses;
283 
284 bail:
285 		if (error) {
286 			if (crie.cri_key)
287 				FREE(crie.cri_key, M_XDATA);
288 			if (cria.cri_key)
289 				FREE(cria.cri_key, M_XDATA);
290 		}
291 		break;
292 	case CIOCFSESSION:
293 		ses = *(u_int32_t *)data;
294 		cse = csefind(fcr, ses);
295 		if (cse == NULL) {
296 			error = EINVAL;
297 			break;
298 		}
299 		csedelete(fcr, cse);
300 		error = csefree(cse);
301 		break;
302 	case CIOCCRYPT:
303 		cop = (struct crypt_op *)data;
304 		cse = csefind(fcr, cop->ses);
305 		if (cse == NULL) {
306 			error = EINVAL;
307 			break;
308 		}
309 		error = cryptodev_op(cse, cop);
310 		break;
311 	case CIOCKEY:
312 		error = cryptodev_key((struct crypt_kop *)data);
313 		break;
314 	case CIOCASYMFEAT:
315 		error = crypto_getfeat((int *)data);
316 		break;
317 	default:
318 		error = EINVAL;
319 		break;
320 	}
321 	rel_mplock();
322 	return (error);
323 }
324 
325 static int cryptodev_cb(void *);
326 
327 
328 static int
329 cryptodev_op(struct csession *cse, struct crypt_op *cop)
330 {
331 	struct cryptop *crp = NULL;
332 	struct cryptodesc *crde = NULL, *crda = NULL;
333 	int i, error;
334 
335 	if (cop->len > 256*1024-4)
336 		return (E2BIG);
337 
338 	if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
339 		return (EINVAL);
340 
341 	bzero(&cse->uio, sizeof(cse->uio));
342 	cse->uio.uio_iovcnt = 1;
343 	cse->uio.uio_resid = 0;
344 	cse->uio.uio_segflg = UIO_SYSSPACE;
345 	cse->uio.uio_rw = UIO_WRITE;
346 	cse->uio.uio_td = curthread;
347 	cse->uio.uio_iov = cse->iovec;
348 	bzero(&cse->iovec, sizeof(cse->iovec));
349 	cse->uio.uio_iov[0].iov_len = cop->len;
350 	cse->uio.uio_iov[0].iov_base = kmalloc(cop->len, M_XDATA, M_WAITOK);
351 	for (i = 0; i < cse->uio.uio_iovcnt; i++)
352 		cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
353 
354 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
355 	if (crp == NULL) {
356 		error = ENOMEM;
357 		goto bail;
358 	}
359 
360 	if (cse->thash) {
361 		crda = crp->crp_desc;
362 		if (cse->txform)
363 			crde = crda->crd_next;
364 	} else {
365 		if (cse->txform)
366 			crde = crp->crp_desc;
367 		else {
368 			error = EINVAL;
369 			goto bail;
370 		}
371 	}
372 
373 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
374 		goto bail;
375 
376 	if (crda) {
377 		crda->crd_skip = 0;
378 		crda->crd_len = cop->len;
379 		crda->crd_inject = 0;	/* ??? */
380 
381 		crda->crd_alg = cse->mac;
382 		crda->crd_key = cse->mackey;
383 		crda->crd_klen = cse->mackeylen * 8;
384 	}
385 
386 	if (crde) {
387 		if (cop->op == COP_ENCRYPT)
388 			crde->crd_flags |= CRD_F_ENCRYPT;
389 		else
390 			crde->crd_flags &= ~CRD_F_ENCRYPT;
391 		crde->crd_len = cop->len;
392 		crde->crd_inject = 0;
393 
394 		crde->crd_alg = cse->cipher;
395 		crde->crd_key = cse->key;
396 		crde->crd_klen = cse->keylen * 8;
397 	}
398 
399 	crp->crp_ilen = cop->len;
400 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
401 		       | (cop->flags & COP_F_BATCH);
402 	crp->crp_buf = (caddr_t)&cse->uio;
403 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
404 	crp->crp_sid = cse->sid;
405 	crp->crp_opaque = (void *)cse;
406 
407 	if (cop->iv) {
408 		if (crde == NULL) {
409 			error = EINVAL;
410 			goto bail;
411 		}
412 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
413 			error = EINVAL;
414 			goto bail;
415 		}
416 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
417 			goto bail;
418 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
419 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
420 		crde->crd_skip = 0;
421 	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
422 		crde->crd_skip = 0;
423 	} else if (crde) {
424 		crde->crd_flags |= CRD_F_IV_PRESENT;
425 		crde->crd_skip = cse->txform->blocksize;
426 		crde->crd_len -= cse->txform->blocksize;
427 	}
428 
429 	if (cop->mac) {
430 		if (crda == NULL) {
431 			error = EINVAL;
432 			goto bail;
433 		}
434 		crp->crp_mac=cse->tmp_mac;
435 	}
436 
437 	crit_enter();
438 	error = crypto_dispatch(crp);
439 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
440 		error = tsleep(crp, 0, "crydev", 0);
441 	crit_exit();
442 	if (error)
443 		goto bail;
444 
445 	if (crp->crp_etype != 0) {
446 		error = crp->crp_etype;
447 		goto bail;
448 	}
449 
450 	if (cse->error) {
451 		error = cse->error;
452 		goto bail;
453 	}
454 
455 	if (cop->dst &&
456 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
457 		goto bail;
458 
459 	if (cop->mac &&
460 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
461 		goto bail;
462 
463 bail:
464 	if (crp)
465 		crypto_freereq(crp);
466 	if (cse->uio.uio_iov[0].iov_base)
467 		kfree(cse->uio.uio_iov[0].iov_base, M_XDATA);
468 
469 	return (error);
470 }
471 
472 static int
473 cryptodev_cb(void *op)
474 {
475 	struct cryptop *crp = (struct cryptop *) op;
476 	struct csession *cse = (struct csession *)crp->crp_opaque;
477 
478 	cse->error = crp->crp_etype;
479 	if (crp->crp_etype == EAGAIN)
480 		return crypto_dispatch(crp);
481 	wakeup_one(crp);
482 	return (0);
483 }
484 
485 static int
486 cryptodevkey_cb(void *op)
487 {
488 	struct cryptkop *krp = (struct cryptkop *) op;
489 
490 	wakeup_one(krp);
491 	return (0);
492 }
493 
494 static int
495 cryptodev_key(struct crypt_kop *kop)
496 {
497 	struct cryptkop *krp = NULL;
498 	int error = EINVAL;
499 	int in, out, size, i;
500 
501 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
502 		return (EFBIG);
503 	}
504 
505 	in = kop->crk_iparams;
506 	out = kop->crk_oparams;
507 	switch (kop->crk_op) {
508 	case CRK_MOD_EXP:
509 		if (in == 3 && out == 1)
510 			break;
511 		return (EINVAL);
512 	case CRK_MOD_EXP_CRT:
513 		if (in == 6 && out == 1)
514 			break;
515 		return (EINVAL);
516 	case CRK_DSA_SIGN:
517 		if (in == 5 && out == 2)
518 			break;
519 		return (EINVAL);
520 	case CRK_DSA_VERIFY:
521 		if (in == 7 && out == 0)
522 			break;
523 		return (EINVAL);
524 	case CRK_DH_COMPUTE_KEY:
525 		if (in == 3 && out == 1)
526 			break;
527 		return (EINVAL);
528 	default:
529 		return (EINVAL);
530 	}
531 
532 	krp = (struct cryptkop *)kmalloc(sizeof *krp, M_XDATA, M_WAITOK | M_ZERO);
533 	krp->krp_op = kop->crk_op;
534 	krp->krp_status = kop->crk_status;
535 	krp->krp_iparams = kop->crk_iparams;
536 	krp->krp_oparams = kop->crk_oparams;
537 	krp->krp_status = 0;
538 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
539 
540 	for (i = 0; i < CRK_MAXPARAM; i++)
541 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
542 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
543 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
544 		if (size == 0)
545 			continue;
546 		MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
547 		if (i >= krp->krp_iparams)
548 			continue;
549 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
550 		if (error)
551 			goto fail;
552 	}
553 
554 	error = crypto_kdispatch(krp);
555 	if (error == 0)
556 		error = tsleep(krp, 0, "crydev", 0);
557 	if (error)
558 		goto fail;
559 
560 	if (krp->krp_status != 0) {
561 		error = krp->krp_status;
562 		goto fail;
563 	}
564 
565 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
566 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
567 		if (size == 0)
568 			continue;
569 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
570 		if (error)
571 			goto fail;
572 	}
573 
574 fail:
575 	if (krp) {
576 		kop->crk_status = krp->krp_status;
577 		for (i = 0; i < CRK_MAXPARAM; i++) {
578 			if (krp->krp_param[i].crp_p)
579 				FREE(krp->krp_param[i].crp_p, M_XDATA);
580 		}
581 		kfree(krp, M_XDATA);
582 	}
583 	return (error);
584 }
585 
586 /*
587  * MPSAFE
588  */
589 static int
590 cryptof_poll(struct file *fp, int events, struct ucred *active_cred)
591 {
592 	return (0);
593 }
594 
595 /*
596  * MPSAFE
597  */
598 static int
599 cryptof_kqfilter(struct file *fp, struct knote *kn)
600 {
601 
602 	return (0);
603 }
604 
605 /*
606  * MPSAFE
607  */
608 static int
609 cryptof_stat(struct file *fp, struct stat *sb, struct ucred *cred)
610 {
611 	return (EOPNOTSUPP);
612 }
613 
614 /*
615  * MPALMOSTSAFE - acquires mplock
616  */
617 static int
618 cryptof_close(struct file *fp)
619 {
620 	struct fcrypt *fcr;
621 	struct csession *cse;
622 
623 	get_mplock();
624 	fcr = (struct fcrypt *)fp->f_data;
625 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
626 		TAILQ_REMOVE(&fcr->csessions, cse, next);
627 		(void)csefree(cse);
628 	}
629 	fp->f_data = NULL;
630 	rel_mplock();
631 
632 	FREE(fcr, M_XDATA);
633 	return (0);
634 }
635 
636 static struct csession *
637 csefind(struct fcrypt *fcr, u_int ses)
638 {
639 	struct csession *cse;
640 
641 	TAILQ_FOREACH(cse, &fcr->csessions, next)
642 		if (cse->ses == ses)
643 			return (cse);
644 	return (NULL);
645 }
646 
647 static int
648 csedelete(struct fcrypt *fcr, struct csession *cse_del)
649 {
650 	struct csession *cse;
651 
652 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
653 		if (cse == cse_del) {
654 			TAILQ_REMOVE(&fcr->csessions, cse, next);
655 			return (1);
656 		}
657 	}
658 	return (0);
659 }
660 
661 static struct csession *
662 cseadd(struct fcrypt *fcr, struct csession *cse)
663 {
664 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
665 	cse->ses = fcr->sesn++;
666 	return (cse);
667 }
668 
669 struct csession *
670 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
671     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
672     struct enc_xform *txform, struct auth_hash *thash)
673 {
674 	struct csession *cse;
675 
676 	MALLOC(cse, struct csession *, sizeof(struct csession),
677 	    M_XDATA, M_NOWAIT);
678 	if (cse == NULL)
679 		return NULL;
680 	cse->key = key;
681 	cse->keylen = keylen/8;
682 	cse->mackey = mackey;
683 	cse->mackeylen = mackeylen/8;
684 	cse->sid = sid;
685 	cse->cipher = cipher;
686 	cse->mac = mac;
687 	cse->txform = txform;
688 	cse->thash = thash;
689 	cseadd(fcr, cse);
690 	return (cse);
691 }
692 
693 static int
694 csefree(struct csession *cse)
695 {
696 	int error;
697 
698 	error = crypto_freesession(cse->sid);
699 	if (cse->key)
700 		FREE(cse->key, M_XDATA);
701 	if (cse->mackey)
702 		FREE(cse->mackey, M_XDATA);
703 	FREE(cse, M_XDATA);
704 	return (error);
705 }
706 
707 static int
708 cryptoopen(struct dev_open_args *ap)
709 {
710 	if (crypto_usercrypto == 0)
711 		return (ENXIO);
712 	return (0);
713 }
714 
715 static int
716 cryptoread(struct dev_read_args *ap)
717 {
718 	return (EIO);
719 }
720 
721 static int
722 cryptowrite(struct dev_write_args *ap)
723 {
724 	return (EIO);
725 }
726 
727 static int
728 cryptoioctl(struct dev_ioctl_args *ap)
729 {
730 	struct file *f;
731 	struct fcrypt *fcr;
732 	int fd, error;
733 
734 	switch (ap->a_cmd) {
735 	case CRIOGET:
736 		MALLOC(fcr, struct fcrypt *,
737 		    sizeof(struct fcrypt), M_XDATA, M_WAITOK);
738 		TAILQ_INIT(&fcr->csessions);
739 		fcr->sesn = 0;
740 
741 		KKASSERT(curproc);
742 		error = falloc(curproc, &f, &fd);
743 
744 		if (error) {
745 			FREE(fcr, M_XDATA);
746 			return (error);
747 		}
748 		f->f_type = DTYPE_CRYPTO;
749 		f->f_flag = FREAD | FWRITE;
750 		f->f_ops = &cryptofops;
751 		f->f_data = fcr;
752 		fsetfd(curproc, f, fd);
753 		*(u_int32_t *)ap->a_data = fd;
754 		fdrop(f);
755 		break;
756 	default:
757 		error = EINVAL;
758 		break;
759 	}
760 	return (error);
761 }
762 
763 #define	CRYPTO_MAJOR	70		/* from openbsd */
764 static struct dev_ops crypto_ops = {
765 	{ "crypto", CRYPTO_MAJOR, 0 },
766 	.d_open =	cryptoopen,
767 	.d_close =	nullclose,
768 	.d_read =	cryptoread,
769 	.d_write =	cryptowrite,
770 	.d_ioctl =	cryptoioctl,
771 };
772 
773 /*
774  * Initialization code, both for static and dynamic loading.
775  */
776 static int
777 cryptodev_modevent(module_t mod, int type, void *unused)
778 {
779 	switch (type) {
780 	case MOD_LOAD:
781 		if (bootverbose)
782 			kprintf("crypto: <crypto device>\n");
783 		make_dev(&crypto_ops, 0, UID_ROOT, GID_WHEEL,
784 			 0666, "crypto");
785 		return 0;
786 	case MOD_UNLOAD:
787 		/*XXX disallow if active sessions */
788 		//dev_ops_remove(&crypto_ops, 0, 0);
789 		dev_ops_remove_all(&crypto_ops);
790 		return 0;
791 	}
792 	return EINVAL;
793 }
794 
795 static moduledata_t cryptodev_mod = {
796 	"cryptodev",
797 	cryptodev_modevent,
798 	0
799 };
800 MODULE_VERSION(cryptodev, 1);
801 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
802 #if 0
803 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
804 #endif
805