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