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