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