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