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