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