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