xref: /freebsd/sys/opencrypto/cryptodev.c (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
1 /*	$OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Theo de Raadt
5  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6  * Copyright (c) 2014 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * Portions of this software were developed by John-Mark Gurney
10  * under sponsorship of the FreeBSD Foundation and
11  * Rubicon Communications, LLC (Netgate).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *   notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *   notice, this list of conditions and the following disclaimer in the
21  *   documentation and/or other materials provided with the distribution.
22  * 3. The name of the author may not be used to endorse or promote products
23  *   derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Effort sponsored in part by the Defense Advanced Research Projects
37  * Agency (DARPA) and Air Force Research Laboratory, Air Force
38  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_compat.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
53 #include <sys/file.h>
54 #include <sys/filedesc.h>
55 #include <sys/errno.h>
56 #include <sys/uio.h>
57 #include <sys/random.h>
58 #include <sys/conf.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
61 #include <sys/fcntl.h>
62 #include <sys/bus.h>
63 #include <sys/user.h>
64 #include <sys/sdt.h>
65 
66 #include <opencrypto/cryptodev.h>
67 #include <opencrypto/xform.h>
68 
69 SDT_PROVIDER_DECLARE(opencrypto);
70 
71 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
72 
73 #ifdef COMPAT_FREEBSD32
74 #include <sys/mount.h>
75 #include <compat/freebsd32/freebsd32.h>
76 
77 struct session_op32 {
78 	u_int32_t	cipher;
79 	u_int32_t	mac;
80 	u_int32_t	keylen;
81 	u_int32_t	key;
82 	int		mackeylen;
83 	u_int32_t	mackey;
84 	u_int32_t	ses;
85 };
86 
87 struct session2_op32 {
88 	u_int32_t	cipher;
89 	u_int32_t	mac;
90 	u_int32_t	keylen;
91 	u_int32_t	key;
92 	int		mackeylen;
93 	u_int32_t	mackey;
94 	u_int32_t	ses;
95 	int		crid;
96 	int		pad[4];
97 };
98 
99 struct crypt_op32 {
100 	u_int32_t	ses;
101 	u_int16_t	op;
102 	u_int16_t	flags;
103 	u_int		len;
104 	u_int32_t	src, dst;
105 	u_int32_t	mac;
106 	u_int32_t	iv;
107 };
108 
109 struct crparam32 {
110 	u_int32_t	crp_p;
111 	u_int		crp_nbits;
112 };
113 
114 struct crypt_kop32 {
115 	u_int		crk_op;
116 	u_int		crk_status;
117 	u_short		crk_iparams;
118 	u_short		crk_oparams;
119 	u_int		crk_crid;
120 	struct crparam32	crk_param[CRK_MAXPARAM];
121 };
122 
123 struct cryptotstat32 {
124 	struct timespec32	acc;
125 	struct timespec32	min;
126 	struct timespec32	max;
127 	u_int32_t	count;
128 };
129 
130 struct cryptostats32 {
131 	u_int32_t	cs_ops;
132 	u_int32_t	cs_errs;
133 	u_int32_t	cs_kops;
134 	u_int32_t	cs_kerrs;
135 	u_int32_t	cs_intrs;
136 	u_int32_t	cs_rets;
137 	u_int32_t	cs_blocks;
138 	u_int32_t	cs_kblocks;
139 	struct cryptotstat32 cs_invoke;
140 	struct cryptotstat32 cs_done;
141 	struct cryptotstat32 cs_cb;
142 	struct cryptotstat32 cs_finis;
143 };
144 
145 #define	CIOCGSESSION32	_IOWR('c', 101, struct session_op32)
146 #define	CIOCCRYPT32	_IOWR('c', 103, struct crypt_op32)
147 #define	CIOCKEY32	_IOWR('c', 104, struct crypt_kop32)
148 #define	CIOCGSESSION232	_IOWR('c', 106, struct session2_op32)
149 #define	CIOCKEY232	_IOWR('c', 107, struct crypt_kop32)
150 
151 static void
152 session_op_from_32(const struct session_op32 *from, struct session_op *to)
153 {
154 
155 	CP(*from, *to, cipher);
156 	CP(*from, *to, mac);
157 	CP(*from, *to, keylen);
158 	PTRIN_CP(*from, *to, key);
159 	CP(*from, *to, mackeylen);
160 	PTRIN_CP(*from, *to, mackey);
161 	CP(*from, *to, ses);
162 }
163 
164 static void
165 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
166 {
167 
168 	session_op_from_32((const struct session_op32 *)from,
169 	    (struct session_op *)to);
170 	CP(*from, *to, crid);
171 }
172 
173 static void
174 session_op_to_32(const struct session_op *from, struct session_op32 *to)
175 {
176 
177 	CP(*from, *to, cipher);
178 	CP(*from, *to, mac);
179 	CP(*from, *to, keylen);
180 	PTROUT_CP(*from, *to, key);
181 	CP(*from, *to, mackeylen);
182 	PTROUT_CP(*from, *to, mackey);
183 	CP(*from, *to, ses);
184 }
185 
186 static void
187 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
188 {
189 
190 	session_op_to_32((const struct session_op *)from,
191 	    (struct session_op32 *)to);
192 	CP(*from, *to, crid);
193 }
194 
195 static void
196 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
197 {
198 
199 	CP(*from, *to, ses);
200 	CP(*from, *to, op);
201 	CP(*from, *to, flags);
202 	CP(*from, *to, len);
203 	PTRIN_CP(*from, *to, src);
204 	PTRIN_CP(*from, *to, dst);
205 	PTRIN_CP(*from, *to, mac);
206 	PTRIN_CP(*from, *to, iv);
207 }
208 
209 static void
210 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
211 {
212 
213 	CP(*from, *to, ses);
214 	CP(*from, *to, op);
215 	CP(*from, *to, flags);
216 	CP(*from, *to, len);
217 	PTROUT_CP(*from, *to, src);
218 	PTROUT_CP(*from, *to, dst);
219 	PTROUT_CP(*from, *to, mac);
220 	PTROUT_CP(*from, *to, iv);
221 }
222 
223 static void
224 crparam_from_32(const struct crparam32 *from, struct crparam *to)
225 {
226 
227 	PTRIN_CP(*from, *to, crp_p);
228 	CP(*from, *to, crp_nbits);
229 }
230 
231 static void
232 crparam_to_32(const struct crparam *from, struct crparam32 *to)
233 {
234 
235 	PTROUT_CP(*from, *to, crp_p);
236 	CP(*from, *to, crp_nbits);
237 }
238 
239 static void
240 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
241 {
242 	int i;
243 
244 	CP(*from, *to, crk_op);
245 	CP(*from, *to, crk_status);
246 	CP(*from, *to, crk_iparams);
247 	CP(*from, *to, crk_oparams);
248 	CP(*from, *to, crk_crid);
249 	for (i = 0; i < CRK_MAXPARAM; i++)
250 		crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
251 }
252 
253 static void
254 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
255 {
256 	int i;
257 
258 	CP(*from, *to, crk_op);
259 	CP(*from, *to, crk_status);
260 	CP(*from, *to, crk_iparams);
261 	CP(*from, *to, crk_oparams);
262 	CP(*from, *to, crk_crid);
263 	for (i = 0; i < CRK_MAXPARAM; i++)
264 		crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
265 }
266 #endif
267 
268 struct csession {
269 	TAILQ_ENTRY(csession) next;
270 	u_int64_t	sid;
271 	u_int32_t	ses;
272 	struct mtx	lock;		/* for op submission */
273 
274 	u_int32_t	cipher;
275 	struct enc_xform *txform;
276 	u_int32_t	mac;
277 	struct auth_hash *thash;
278 
279 	caddr_t		key;
280 	int		keylen;
281 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
282 
283 	caddr_t		mackey;
284 	int		mackeylen;
285 
286 	struct iovec	iovec;
287 	struct uio	uio;
288 	int		error;
289 };
290 
291 struct fcrypt {
292 	TAILQ_HEAD(csessionlist, csession) csessions;
293 	int		sesn;
294 };
295 
296 static	int cryptof_ioctl(struct file *, u_long, void *,
297 		    struct ucred *, struct thread *);
298 static	int cryptof_stat(struct file *, struct stat *,
299 		    struct ucred *, struct thread *);
300 static	int cryptof_close(struct file *, struct thread *);
301 static	int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
302 		    struct filedesc *);
303 
304 static struct fileops cryptofops = {
305     .fo_read = invfo_rdwr,
306     .fo_write = invfo_rdwr,
307     .fo_truncate = invfo_truncate,
308     .fo_ioctl = cryptof_ioctl,
309     .fo_poll = invfo_poll,
310     .fo_kqfilter = invfo_kqfilter,
311     .fo_stat = cryptof_stat,
312     .fo_close = cryptof_close,
313     .fo_chmod = invfo_chmod,
314     .fo_chown = invfo_chown,
315     .fo_sendfile = invfo_sendfile,
316     .fo_fill_kinfo = cryptof_fill_kinfo,
317 };
318 
319 static struct csession *csefind(struct fcrypt *, u_int);
320 static int csedelete(struct fcrypt *, struct csession *);
321 static struct csession *cseadd(struct fcrypt *, struct csession *);
322 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
323     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
324     struct auth_hash *);
325 static int csefree(struct csession *);
326 
327 static	int cryptodev_op(struct csession *, struct crypt_op *,
328 			struct ucred *, struct thread *td);
329 static	int cryptodev_aead(struct csession *, struct crypt_aead *,
330 			struct ucred *, struct thread *);
331 static	int cryptodev_key(struct crypt_kop *);
332 static	int cryptodev_find(struct crypt_find_op *);
333 
334 /*
335  * Check a crypto identifier to see if it requested
336  * a software device/driver.  This can be done either
337  * by device name/class or through search constraints.
338  */
339 static int
340 checkforsoftware(int *cridp)
341 {
342 	int crid;
343 
344 	crid = *cridp;
345 
346 	if (!crypto_devallowsoft) {
347 		if (crid & CRYPTOCAP_F_SOFTWARE) {
348 			if (crid & CRYPTOCAP_F_HARDWARE) {
349 				*cridp = CRYPTOCAP_F_HARDWARE;
350 				return 0;
351 			}
352 			return EINVAL;
353 		}
354 		if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
355 		    (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
356 			return EINVAL;
357 	}
358 	return 0;
359 }
360 
361 /* ARGSUSED */
362 static int
363 cryptof_ioctl(
364 	struct file *fp,
365 	u_long cmd,
366 	void *data,
367 	struct ucred *active_cred,
368 	struct thread *td)
369 {
370 #define	SES2(p)	((struct session2_op *)p)
371 	struct cryptoini cria, crie;
372 	struct fcrypt *fcr = fp->f_data;
373 	struct csession *cse;
374 	struct session_op *sop;
375 	struct crypt_op *cop;
376 	struct crypt_aead *caead;
377 	struct enc_xform *txform = NULL;
378 	struct auth_hash *thash = NULL;
379 	struct crypt_kop *kop;
380 	u_int64_t sid;
381 	u_int32_t ses;
382 	int error = 0, crid;
383 #ifdef COMPAT_FREEBSD32
384 	struct session2_op sopc;
385 	struct crypt_op copc;
386 	struct crypt_kop kopc;
387 #endif
388 
389 	switch (cmd) {
390 	case CIOCGSESSION:
391 	case CIOCGSESSION2:
392 #ifdef COMPAT_FREEBSD32
393 	case CIOCGSESSION32:
394 	case CIOCGSESSION232:
395 		if (cmd == CIOCGSESSION32) {
396 			session_op_from_32(data, (struct session_op *)&sopc);
397 			sop = (struct session_op *)&sopc;
398 		} else if (cmd == CIOCGSESSION232) {
399 			session2_op_from_32(data, &sopc);
400 			sop = (struct session_op *)&sopc;
401 		} else
402 #endif
403 			sop = (struct session_op *)data;
404 		switch (sop->cipher) {
405 		case 0:
406 			break;
407 		case CRYPTO_DES_CBC:
408 			txform = &enc_xform_des;
409 			break;
410 		case CRYPTO_3DES_CBC:
411 			txform = &enc_xform_3des;
412 			break;
413 		case CRYPTO_BLF_CBC:
414 			txform = &enc_xform_blf;
415 			break;
416 		case CRYPTO_CAST_CBC:
417 			txform = &enc_xform_cast5;
418 			break;
419 		case CRYPTO_SKIPJACK_CBC:
420 			txform = &enc_xform_skipjack;
421 			break;
422 		case CRYPTO_AES_CBC:
423 			txform = &enc_xform_rijndael128;
424 			break;
425 		case CRYPTO_AES_XTS:
426 			txform = &enc_xform_aes_xts;
427 			break;
428 		case CRYPTO_NULL_CBC:
429 			txform = &enc_xform_null;
430 			break;
431 		case CRYPTO_ARC4:
432 			txform = &enc_xform_arc4;
433 			break;
434  		case CRYPTO_CAMELLIA_CBC:
435  			txform = &enc_xform_camellia;
436  			break;
437 		case CRYPTO_AES_ICM:
438 			txform = &enc_xform_aes_icm;
439  			break;
440 		case CRYPTO_AES_NIST_GCM_16:
441 			txform = &enc_xform_aes_nist_gcm;
442  			break;
443 
444 		default:
445 			CRYPTDEB("invalid cipher");
446 			return (EINVAL);
447 		}
448 
449 		switch (sop->mac) {
450 		case 0:
451 			break;
452 		case CRYPTO_MD5_HMAC:
453 			thash = &auth_hash_hmac_md5;
454 			break;
455 		case CRYPTO_SHA1_HMAC:
456 			thash = &auth_hash_hmac_sha1;
457 			break;
458 		case CRYPTO_SHA2_256_HMAC:
459 			thash = &auth_hash_hmac_sha2_256;
460 			break;
461 		case CRYPTO_SHA2_384_HMAC:
462 			thash = &auth_hash_hmac_sha2_384;
463 			break;
464 		case CRYPTO_SHA2_512_HMAC:
465 			thash = &auth_hash_hmac_sha2_512;
466 			break;
467 		case CRYPTO_RIPEMD160_HMAC:
468 			thash = &auth_hash_hmac_ripemd_160;
469 			break;
470 		case CRYPTO_AES_128_NIST_GMAC:
471 			thash = &auth_hash_nist_gmac_aes_128;
472 			break;
473 		case CRYPTO_AES_192_NIST_GMAC:
474 			thash = &auth_hash_nist_gmac_aes_192;
475 			break;
476 		case CRYPTO_AES_256_NIST_GMAC:
477 			thash = &auth_hash_nist_gmac_aes_256;
478 			break;
479 
480 #ifdef notdef
481 		case CRYPTO_MD5:
482 			thash = &auth_hash_md5;
483 			break;
484 		case CRYPTO_SHA1:
485 			thash = &auth_hash_sha1;
486 			break;
487 #endif
488 		case CRYPTO_NULL_HMAC:
489 			thash = &auth_hash_null;
490 			break;
491 		default:
492 			CRYPTDEB("invalid mac");
493 			return (EINVAL);
494 		}
495 
496 		bzero(&crie, sizeof(crie));
497 		bzero(&cria, sizeof(cria));
498 
499 		if (txform) {
500 			crie.cri_alg = txform->type;
501 			crie.cri_klen = sop->keylen * 8;
502 			if (sop->keylen > txform->maxkey ||
503 			    sop->keylen < txform->minkey) {
504 				CRYPTDEB("invalid cipher parameters");
505 				error = EINVAL;
506 				goto bail;
507 			}
508 
509 			crie.cri_key = malloc(crie.cri_klen / 8,
510 			    M_XDATA, M_WAITOK);
511 			if ((error = copyin(sop->key, crie.cri_key,
512 			    crie.cri_klen / 8))) {
513 				CRYPTDEB("invalid key");
514 				goto bail;
515 			}
516 			if (thash)
517 				crie.cri_next = &cria;
518 		}
519 
520 		if (thash) {
521 			cria.cri_alg = thash->type;
522 			cria.cri_klen = sop->mackeylen * 8;
523 			if (sop->mackeylen != thash->keysize) {
524 				CRYPTDEB("invalid mac key length");
525 				error = EINVAL;
526 				goto bail;
527 			}
528 
529 			if (cria.cri_klen) {
530 				cria.cri_key = malloc(cria.cri_klen / 8,
531 				    M_XDATA, M_WAITOK);
532 				if ((error = copyin(sop->mackey, cria.cri_key,
533 				    cria.cri_klen / 8))) {
534 					CRYPTDEB("invalid mac key");
535 					goto bail;
536 				}
537 			}
538 		}
539 
540 		/* NB: CIOCGSESSION2 has the crid */
541 		if (cmd == CIOCGSESSION2
542 #ifdef COMPAT_FREEBSD32
543 		    || cmd == CIOCGSESSION232
544 #endif
545 			) {
546 			crid = SES2(sop)->crid;
547 			error = checkforsoftware(&crid);
548 			if (error) {
549 				CRYPTDEB("checkforsoftware");
550 				goto bail;
551 			}
552 		} else
553 			crid = CRYPTOCAP_F_HARDWARE;
554 		error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
555 		if (error) {
556 			CRYPTDEB("crypto_newsession");
557 			goto bail;
558 		}
559 
560 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
561 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
562 		    thash);
563 
564 		if (cse == NULL) {
565 			crypto_freesession(sid);
566 			error = EINVAL;
567 			CRYPTDEB("csecreate");
568 			goto bail;
569 		}
570 		sop->ses = cse->ses;
571 		if (cmd == CIOCGSESSION2
572 #ifdef COMPAT_FREEBSD32
573 		    || cmd == CIOCGSESSION232
574 #endif
575 		    ) {
576 			/* return hardware/driver id */
577 			SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
578 		}
579 bail:
580 		if (error) {
581 			if (crie.cri_key)
582 				free(crie.cri_key, M_XDATA);
583 			if (cria.cri_key)
584 				free(cria.cri_key, M_XDATA);
585 		}
586 #ifdef COMPAT_FREEBSD32
587 		else {
588 			if (cmd == CIOCGSESSION32)
589 				session_op_to_32(sop, data);
590 			else if (cmd == CIOCGSESSION232)
591 				session2_op_to_32((struct session2_op *)sop,
592 				    data);
593 		}
594 #endif
595 		break;
596 	case CIOCFSESSION:
597 		ses = *(u_int32_t *)data;
598 		cse = csefind(fcr, ses);
599 		if (cse == NULL)
600 			return (EINVAL);
601 		csedelete(fcr, cse);
602 		error = csefree(cse);
603 		break;
604 	case CIOCCRYPT:
605 #ifdef COMPAT_FREEBSD32
606 	case CIOCCRYPT32:
607 		if (cmd == CIOCCRYPT32) {
608 			cop = &copc;
609 			crypt_op_from_32(data, cop);
610 		} else
611 #endif
612 			cop = (struct crypt_op *)data;
613 		cse = csefind(fcr, cop->ses);
614 		if (cse == NULL) {
615 			SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
616 			return (EINVAL);
617 		}
618 		error = cryptodev_op(cse, cop, active_cred, td);
619 #ifdef COMPAT_FREEBSD32
620 		if (error == 0 && cmd == CIOCCRYPT32)
621 			crypt_op_to_32(cop, data);
622 #endif
623 		break;
624 	case CIOCKEY:
625 	case CIOCKEY2:
626 #ifdef COMPAT_FREEBSD32
627 	case CIOCKEY32:
628 	case CIOCKEY232:
629 #endif
630 		if (!crypto_userasymcrypto)
631 			return (EPERM);		/* XXX compat? */
632 #ifdef COMPAT_FREEBSD32
633 		if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
634 			kop = &kopc;
635 			crypt_kop_from_32(data, kop);
636 		} else
637 #endif
638 			kop = (struct crypt_kop *)data;
639 		if (cmd == CIOCKEY
640 #ifdef COMPAT_FREEBSD32
641 		    || cmd == CIOCKEY32
642 #endif
643 		    ) {
644 			/* NB: crypto core enforces s/w driver use */
645 			kop->crk_crid =
646 			    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
647 		}
648 		mtx_lock(&Giant);
649 		error = cryptodev_key(kop);
650 		mtx_unlock(&Giant);
651 #ifdef COMPAT_FREEBSD32
652 		if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
653 			crypt_kop_to_32(kop, data);
654 #endif
655 		break;
656 	case CIOCASYMFEAT:
657 		if (!crypto_userasymcrypto) {
658 			/*
659 			 * NB: if user asym crypto operations are
660 			 * not permitted return "no algorithms"
661 			 * so well-behaved applications will just
662 			 * fallback to doing them in software.
663 			 */
664 			*(int *)data = 0;
665 		} else
666 			error = crypto_getfeat((int *)data);
667 		break;
668 	case CIOCFINDDEV:
669 		error = cryptodev_find((struct crypt_find_op *)data);
670 		break;
671 	case CIOCCRYPTAEAD:
672 		caead = (struct crypt_aead *)data;
673 		cse = csefind(fcr, caead->ses);
674 		if (cse == NULL)
675 			return (EINVAL);
676 		error = cryptodev_aead(cse, caead, active_cred, td);
677 		break;
678 	default:
679 		error = EINVAL;
680 		break;
681 	}
682 	return (error);
683 #undef SES2
684 }
685 
686 static int cryptodev_cb(void *);
687 
688 
689 static int
690 cryptodev_op(
691 	struct csession *cse,
692 	struct crypt_op *cop,
693 	struct ucred *active_cred,
694 	struct thread *td)
695 {
696 	struct cryptop *crp = NULL;
697 	struct cryptodesc *crde = NULL, *crda = NULL;
698 	int error;
699 
700 	if (cop->len > 256*1024-4) {
701 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
702 		return (E2BIG);
703 	}
704 
705 	if (cse->txform) {
706 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
707 			SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
708 			return (EINVAL);
709 		}
710 	}
711 
712 	cse->uio.uio_iov = &cse->iovec;
713 	cse->uio.uio_iovcnt = 1;
714 	cse->uio.uio_offset = 0;
715 	cse->uio.uio_resid = cop->len;
716 	cse->uio.uio_segflg = UIO_SYSSPACE;
717 	cse->uio.uio_rw = UIO_WRITE;
718 	cse->uio.uio_td = td;
719 	cse->uio.uio_iov[0].iov_len = cop->len;
720 	if (cse->thash) {
721 		cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
722 		cse->uio.uio_resid += cse->thash->hashsize;
723 	}
724 	cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
725 	    M_XDATA, M_WAITOK);
726 
727 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
728 	if (crp == NULL) {
729 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
730 		error = ENOMEM;
731 		goto bail;
732 	}
733 
734 	if (cse->thash) {
735 		crda = crp->crp_desc;
736 		if (cse->txform)
737 			crde = crda->crd_next;
738 	} else {
739 		if (cse->txform)
740 			crde = crp->crp_desc;
741 		else {
742 			SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
743 			error = EINVAL;
744 			goto bail;
745 		}
746 	}
747 
748 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base,
749 	    cop->len))) {
750 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
751 		goto bail;
752 	}
753 
754 	if (crda) {
755 		crda->crd_skip = 0;
756 		crda->crd_len = cop->len;
757 		crda->crd_inject = cop->len;
758 
759 		crda->crd_alg = cse->mac;
760 		crda->crd_key = cse->mackey;
761 		crda->crd_klen = cse->mackeylen * 8;
762 	}
763 
764 	if (crde) {
765 		if (cop->op == COP_ENCRYPT)
766 			crde->crd_flags |= CRD_F_ENCRYPT;
767 		else
768 			crde->crd_flags &= ~CRD_F_ENCRYPT;
769 		crde->crd_len = cop->len;
770 		crde->crd_inject = 0;
771 
772 		crde->crd_alg = cse->cipher;
773 		crde->crd_key = cse->key;
774 		crde->crd_klen = cse->keylen * 8;
775 	}
776 
777 	crp->crp_ilen = cop->len;
778 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
779 		       | (cop->flags & COP_F_BATCH);
780 	crp->crp_buf = (caddr_t)&cse->uio;
781 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
782 	crp->crp_sid = cse->sid;
783 	crp->crp_opaque = (void *)cse;
784 
785 	if (cop->iv) {
786 		if (crde == NULL) {
787 			SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
788 			error = EINVAL;
789 			goto bail;
790 		}
791 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
792 			SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
793 			error = EINVAL;
794 			goto bail;
795 		}
796 		if ((error = copyin(cop->iv, cse->tmp_iv,
797 		    cse->txform->blocksize))) {
798 			SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
799 			goto bail;
800 		}
801 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
802 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
803 		crde->crd_skip = 0;
804 	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
805 		crde->crd_skip = 0;
806 	} else if (crde) {
807 		crde->crd_flags |= CRD_F_IV_PRESENT;
808 		crde->crd_skip = cse->txform->blocksize;
809 		crde->crd_len -= cse->txform->blocksize;
810 	}
811 
812 	if (cop->mac && crda == NULL) {
813 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
814 		error = EINVAL;
815 		goto bail;
816 	}
817 
818 again:
819 	/*
820 	 * Let the dispatch run unlocked, then, interlock against the
821 	 * callback before checking if the operation completed and going
822 	 * to sleep.  This insures drivers don't inherit our lock which
823 	 * results in a lock order reversal between crypto_dispatch forced
824 	 * entry and the crypto_done callback into us.
825 	 */
826 	error = crypto_dispatch(crp);
827 	mtx_lock(&cse->lock);
828 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
829 		error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
830 	mtx_unlock(&cse->lock);
831 
832 	if (error != 0) {
833 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
834 		goto bail;
835 	}
836 
837 	if (crp->crp_etype == EAGAIN) {
838 		crp->crp_etype = 0;
839 		crp->crp_flags &= ~CRYPTO_F_DONE;
840 		goto again;
841 	}
842 
843 	if (crp->crp_etype != 0) {
844 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
845 		error = crp->crp_etype;
846 		goto bail;
847 	}
848 
849 	if (cse->error) {
850 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
851 		error = cse->error;
852 		goto bail;
853 	}
854 
855 	if (cop->dst &&
856 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst,
857 	    cop->len))) {
858 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
859 		goto bail;
860 	}
861 
862 	if (cop->mac &&
863 	    (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
864 	    cop->mac, cse->thash->hashsize))) {
865 		SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
866 		goto bail;
867 	}
868 
869 bail:
870 	if (crp)
871 		crypto_freereq(crp);
872 	if (cse->uio.uio_iov[0].iov_base)
873 		free(cse->uio.uio_iov[0].iov_base, M_XDATA);
874 
875 	return (error);
876 }
877 
878 static int
879 cryptodev_aead(
880 	struct csession *cse,
881 	struct crypt_aead *caead,
882 	struct ucred *active_cred,
883 	struct thread *td)
884 {
885 	struct uio *uio;
886 	struct cryptop *crp = NULL;
887 	struct cryptodesc *crde = NULL, *crda = NULL;
888 	int error;
889 
890 	if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4)
891 		return (E2BIG);
892 
893 	if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL ||
894 	    (caead->len % cse->txform->blocksize) != 0)
895 		return (EINVAL);
896 
897 	uio = &cse->uio;
898 	uio->uio_iov = &cse->iovec;
899 	uio->uio_iovcnt = 1;
900 	uio->uio_offset = 0;
901 	uio->uio_resid = caead->len + caead->aadlen + cse->thash->hashsize;
902 	uio->uio_segflg = UIO_SYSSPACE;
903 	uio->uio_rw = UIO_WRITE;
904 	uio->uio_td = td;
905 	uio->uio_iov[0].iov_len = uio->uio_resid;
906 
907 	uio->uio_iov[0].iov_base = malloc(uio->uio_iov[0].iov_len,
908 	    M_XDATA, M_WAITOK);
909 
910 	crp = crypto_getreq(2);
911 	if (crp == NULL) {
912 		error = ENOMEM;
913 		goto bail;
914 	}
915 
916 	crda = crp->crp_desc;
917 	crde = crda->crd_next;
918 
919 	if ((error = copyin(caead->src, cse->uio.uio_iov[0].iov_base,
920 	    caead->len)))
921 		goto bail;
922 
923 	if ((error = copyin(caead->aad, (char *)cse->uio.uio_iov[0].iov_base +
924 	    caead->len, caead->aadlen)))
925 		goto bail;
926 
927 	crda->crd_skip = caead->len;
928 	crda->crd_len = caead->aadlen;
929 	crda->crd_inject = caead->len + caead->aadlen;
930 
931 	crda->crd_alg = cse->mac;
932 	crda->crd_key = cse->mackey;
933 	crda->crd_klen = cse->mackeylen * 8;
934 
935 	if (caead->op == COP_ENCRYPT)
936 		crde->crd_flags |= CRD_F_ENCRYPT;
937 	else
938 		crde->crd_flags &= ~CRD_F_ENCRYPT;
939 	/* crde->crd_skip set below */
940 	crde->crd_len = caead->len;
941 	crde->crd_inject = 0;
942 
943 	crde->crd_alg = cse->cipher;
944 	crde->crd_key = cse->key;
945 	crde->crd_klen = cse->keylen * 8;
946 
947 	crp->crp_ilen = caead->len + caead->aadlen;
948 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
949 		       | (caead->flags & COP_F_BATCH);
950 	crp->crp_buf = (caddr_t)&cse->uio.uio_iov;
951 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
952 	crp->crp_sid = cse->sid;
953 	crp->crp_opaque = (void *)cse;
954 
955 	if (caead->iv) {
956 		if (caead->ivlen > sizeof cse->tmp_iv) {
957 			error = EINVAL;
958 			goto bail;
959 		}
960 
961 		if ((error = copyin(caead->iv, cse->tmp_iv, caead->ivlen)))
962 			goto bail;
963 		bcopy(cse->tmp_iv, crde->crd_iv, caead->ivlen);
964 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
965 		crde->crd_skip = 0;
966 	} else {
967 		crde->crd_flags |= CRD_F_IV_PRESENT;
968 		crde->crd_skip = cse->txform->blocksize;
969 		crde->crd_len -= cse->txform->blocksize;
970 	}
971 
972 	if ((error = copyin(caead->tag, (caddr_t)cse->uio.uio_iov[0].iov_base +
973 	    caead->len + caead->aadlen, cse->thash->hashsize)))
974 		goto bail;
975 again:
976 	/*
977 	 * Let the dispatch run unlocked, then, interlock against the
978 	 * callback before checking if the operation completed and going
979 	 * to sleep.  This insures drivers don't inherit our lock which
980 	 * results in a lock order reversal between crypto_dispatch forced
981 	 * entry and the crypto_done callback into us.
982 	 */
983 	error = crypto_dispatch(crp);
984 	mtx_lock(&cse->lock);
985 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
986 		error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
987 	mtx_unlock(&cse->lock);
988 
989 	if (error != 0)
990 		goto bail;
991 
992 	if (crp->crp_etype == EAGAIN) {
993 		crp->crp_etype = 0;
994 		crp->crp_flags &= ~CRYPTO_F_DONE;
995 		goto again;
996 	}
997 
998 	if (crp->crp_etype != 0) {
999 		error = crp->crp_etype;
1000 		goto bail;
1001 	}
1002 
1003 	if (cse->error) {
1004 		error = cse->error;
1005 		goto bail;
1006 	}
1007 
1008 	if (caead->dst && (error = copyout(cse->uio.uio_iov[0].iov_base,
1009 	    caead->dst, caead->len)))
1010 		goto bail;
1011 
1012 	if ((error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base +
1013 	    caead->len + caead->aadlen, caead->tag, cse->thash->hashsize)))
1014 		goto bail;
1015 
1016 bail:
1017 	crypto_freereq(crp);
1018 	free(cse->uio.uio_iov[0].iov_base, M_XDATA);
1019 
1020 	return (error);
1021 }
1022 
1023 static int
1024 cryptodev_cb(void *op)
1025 {
1026 	struct cryptop *crp = (struct cryptop *) op;
1027 	struct csession *cse = (struct csession *)crp->crp_opaque;
1028 
1029 	mtx_lock(&cse->lock);
1030 	cse->error = crp->crp_etype;
1031 	wakeup_one(crp);
1032 	mtx_unlock(&cse->lock);
1033 	return (0);
1034 }
1035 
1036 static int
1037 cryptodevkey_cb(void *op)
1038 {
1039 	struct cryptkop *krp = (struct cryptkop *) op;
1040 
1041 	wakeup_one(krp);
1042 	return (0);
1043 }
1044 
1045 static int
1046 cryptodev_key(struct crypt_kop *kop)
1047 {
1048 	struct cryptkop *krp = NULL;
1049 	int error = EINVAL;
1050 	int in, out, size, i;
1051 
1052 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1053 		return (EFBIG);
1054 	}
1055 
1056 	in = kop->crk_iparams;
1057 	out = kop->crk_oparams;
1058 	switch (kop->crk_op) {
1059 	case CRK_MOD_EXP:
1060 		if (in == 3 && out == 1)
1061 			break;
1062 		return (EINVAL);
1063 	case CRK_MOD_EXP_CRT:
1064 		if (in == 6 && out == 1)
1065 			break;
1066 		return (EINVAL);
1067 	case CRK_DSA_SIGN:
1068 		if (in == 5 && out == 2)
1069 			break;
1070 		return (EINVAL);
1071 	case CRK_DSA_VERIFY:
1072 		if (in == 7 && out == 0)
1073 			break;
1074 		return (EINVAL);
1075 	case CRK_DH_COMPUTE_KEY:
1076 		if (in == 3 && out == 1)
1077 			break;
1078 		return (EINVAL);
1079 	default:
1080 		return (EINVAL);
1081 	}
1082 
1083 	krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
1084 	if (!krp)
1085 		return (ENOMEM);
1086 	krp->krp_op = kop->crk_op;
1087 	krp->krp_status = kop->crk_status;
1088 	krp->krp_iparams = kop->crk_iparams;
1089 	krp->krp_oparams = kop->crk_oparams;
1090 	krp->krp_crid = kop->crk_crid;
1091 	krp->krp_status = 0;
1092 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
1093 
1094 	for (i = 0; i < CRK_MAXPARAM; i++) {
1095 		if (kop->crk_param[i].crp_nbits > 65536)
1096 			/* Limit is the same as in OpenBSD */
1097 			goto fail;
1098 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1099 	}
1100 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1101 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
1102 		if (size == 0)
1103 			continue;
1104 		krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1105 		if (i >= krp->krp_iparams)
1106 			continue;
1107 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1108 		if (error)
1109 			goto fail;
1110 	}
1111 
1112 	error = crypto_kdispatch(krp);
1113 	if (error)
1114 		goto fail;
1115 	error = tsleep(krp, PSOCK, "crydev", 0);
1116 	if (error) {
1117 		/* XXX can this happen?  if so, how do we recover? */
1118 		goto fail;
1119 	}
1120 
1121 	kop->crk_crid = krp->krp_crid;		/* device that did the work */
1122 	if (krp->krp_status != 0) {
1123 		error = krp->krp_status;
1124 		goto fail;
1125 	}
1126 
1127 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1128 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
1129 		if (size == 0)
1130 			continue;
1131 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1132 		if (error)
1133 			goto fail;
1134 	}
1135 
1136 fail:
1137 	if (krp) {
1138 		kop->crk_status = krp->krp_status;
1139 		for (i = 0; i < CRK_MAXPARAM; i++) {
1140 			if (krp->krp_param[i].crp_p)
1141 				free(krp->krp_param[i].crp_p, M_XDATA);
1142 		}
1143 		free(krp, M_XDATA);
1144 	}
1145 	return (error);
1146 }
1147 
1148 static int
1149 cryptodev_find(struct crypt_find_op *find)
1150 {
1151 	device_t dev;
1152 	size_t fnlen = sizeof find->name;
1153 
1154 	if (find->crid != -1) {
1155 		dev = crypto_find_device_byhid(find->crid);
1156 		if (dev == NULL)
1157 			return (ENOENT);
1158 		strncpy(find->name, device_get_nameunit(dev), fnlen);
1159 		find->name[fnlen - 1] = '\x0';
1160 	} else {
1161 		find->name[fnlen - 1] = '\x0';
1162 		find->crid = crypto_find_driver(find->name);
1163 		if (find->crid == -1)
1164 			return (ENOENT);
1165 	}
1166 	return (0);
1167 }
1168 
1169 /* ARGSUSED */
1170 static int
1171 cryptof_stat(
1172 	struct file *fp,
1173 	struct stat *sb,
1174 	struct ucred *active_cred,
1175 	struct thread *td)
1176 {
1177 
1178 	return (EOPNOTSUPP);
1179 }
1180 
1181 /* ARGSUSED */
1182 static int
1183 cryptof_close(struct file *fp, struct thread *td)
1184 {
1185 	struct fcrypt *fcr = fp->f_data;
1186 	struct csession *cse;
1187 
1188 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1189 		TAILQ_REMOVE(&fcr->csessions, cse, next);
1190 		(void)csefree(cse);
1191 	}
1192 	free(fcr, M_XDATA);
1193 	fp->f_data = NULL;
1194 	return 0;
1195 }
1196 
1197 static int
1198 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1199 {
1200 
1201 	kif->kf_type = KF_TYPE_CRYPTO;
1202 	return (0);
1203 }
1204 
1205 static struct csession *
1206 csefind(struct fcrypt *fcr, u_int ses)
1207 {
1208 	struct csession *cse;
1209 
1210 	TAILQ_FOREACH(cse, &fcr->csessions, next)
1211 		if (cse->ses == ses)
1212 			return (cse);
1213 	return (NULL);
1214 }
1215 
1216 static int
1217 csedelete(struct fcrypt *fcr, struct csession *cse_del)
1218 {
1219 	struct csession *cse;
1220 
1221 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
1222 		if (cse == cse_del) {
1223 			TAILQ_REMOVE(&fcr->csessions, cse, next);
1224 			return (1);
1225 		}
1226 	}
1227 	return (0);
1228 }
1229 
1230 static struct csession *
1231 cseadd(struct fcrypt *fcr, struct csession *cse)
1232 {
1233 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1234 	cse->ses = fcr->sesn++;
1235 	return (cse);
1236 }
1237 
1238 struct csession *
1239 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
1240     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1241     struct enc_xform *txform, struct auth_hash *thash)
1242 {
1243 	struct csession *cse;
1244 
1245 	cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1246 	if (cse == NULL)
1247 		return NULL;
1248 	mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1249 	cse->key = key;
1250 	cse->keylen = keylen/8;
1251 	cse->mackey = mackey;
1252 	cse->mackeylen = mackeylen/8;
1253 	cse->sid = sid;
1254 	cse->cipher = cipher;
1255 	cse->mac = mac;
1256 	cse->txform = txform;
1257 	cse->thash = thash;
1258 	cseadd(fcr, cse);
1259 	return (cse);
1260 }
1261 
1262 static int
1263 csefree(struct csession *cse)
1264 {
1265 	int error;
1266 
1267 	error = crypto_freesession(cse->sid);
1268 	mtx_destroy(&cse->lock);
1269 	if (cse->key)
1270 		free(cse->key, M_XDATA);
1271 	if (cse->mackey)
1272 		free(cse->mackey, M_XDATA);
1273 	free(cse, M_XDATA);
1274 	return (error);
1275 }
1276 
1277 static int
1278 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
1279 {
1280 	return (0);
1281 }
1282 
1283 static int
1284 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
1285 {
1286 	return (EIO);
1287 }
1288 
1289 static int
1290 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
1291 {
1292 	return (EIO);
1293 }
1294 
1295 static int
1296 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1297 {
1298 	struct file *f;
1299 	struct fcrypt *fcr;
1300 	int fd, error;
1301 
1302 	switch (cmd) {
1303 	case CRIOGET:
1304 		fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
1305 		TAILQ_INIT(&fcr->csessions);
1306 		fcr->sesn = 0;
1307 
1308 		error = falloc(td, &f, &fd, 0);
1309 
1310 		if (error) {
1311 			free(fcr, M_XDATA);
1312 			return (error);
1313 		}
1314 		/* falloc automatically provides an extra reference to 'f'. */
1315 		finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1316 		*(u_int32_t *)data = fd;
1317 		fdrop(f, td);
1318 		break;
1319 	case CRIOFINDDEV:
1320 		error = cryptodev_find((struct crypt_find_op *)data);
1321 		break;
1322 	case CRIOASYMFEAT:
1323 		error = crypto_getfeat((int *)data);
1324 		break;
1325 	default:
1326 		error = EINVAL;
1327 		break;
1328 	}
1329 	return (error);
1330 }
1331 
1332 static struct cdevsw crypto_cdevsw = {
1333 	.d_version =	D_VERSION,
1334 	.d_flags =	D_NEEDGIANT,
1335 	.d_open =	cryptoopen,
1336 	.d_read =	cryptoread,
1337 	.d_write =	cryptowrite,
1338 	.d_ioctl =	cryptoioctl,
1339 	.d_name =	"crypto",
1340 };
1341 static struct cdev *crypto_dev;
1342 
1343 /*
1344  * Initialization code, both for static and dynamic loading.
1345  */
1346 static int
1347 cryptodev_modevent(module_t mod, int type, void *unused)
1348 {
1349 	switch (type) {
1350 	case MOD_LOAD:
1351 		if (bootverbose)
1352 			printf("crypto: <crypto device>\n");
1353 		crypto_dev = make_dev(&crypto_cdevsw, 0,
1354 				      UID_ROOT, GID_WHEEL, 0666,
1355 				      "crypto");
1356 		return 0;
1357 	case MOD_UNLOAD:
1358 		/*XXX disallow if active sessions */
1359 		destroy_dev(crypto_dev);
1360 		return 0;
1361 	}
1362 	return EINVAL;
1363 }
1364 
1365 static moduledata_t cryptodev_mod = {
1366 	"cryptodev",
1367 	cryptodev_modevent,
1368 	0
1369 };
1370 MODULE_VERSION(cryptodev, 1);
1371 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1372 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1373 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);
1374