xref: /f-stack/freebsd/opencrypto/cryptodev.h (revision 22ce4aff)
1a9643ea8Slogwang /*	$FreeBSD$	*/
2a9643ea8Slogwang /*	$OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $	*/
3a9643ea8Slogwang 
4a9643ea8Slogwang /*-
5a9643ea8Slogwang  * The author of this code is Angelos D. Keromytis ([email protected])
6a9643ea8Slogwang  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
7a9643ea8Slogwang  *
8a9643ea8Slogwang  * This code was written by Angelos D. Keromytis in Athens, Greece, in
9a9643ea8Slogwang  * February 2000. Network Security Technologies Inc. (NSTI) kindly
10a9643ea8Slogwang  * supported the development of this code.
11a9643ea8Slogwang  *
12a9643ea8Slogwang  * Copyright (c) 2000 Angelos D. Keromytis
13a9643ea8Slogwang  *
14a9643ea8Slogwang  * Permission to use, copy, and modify this software with or without fee
15a9643ea8Slogwang  * is hereby granted, provided that this entire notice is included in
16a9643ea8Slogwang  * all source code copies of any software which is or includes a copy or
17a9643ea8Slogwang  * modification of this software.
18a9643ea8Slogwang  *
19a9643ea8Slogwang  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
20a9643ea8Slogwang  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
21a9643ea8Slogwang  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
22a9643ea8Slogwang  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
23a9643ea8Slogwang  * PURPOSE.
24a9643ea8Slogwang  *
25a9643ea8Slogwang  * Copyright (c) 2001 Theo de Raadt
26a9643ea8Slogwang  * Copyright (c) 2014 The FreeBSD Foundation
27a9643ea8Slogwang  * All rights reserved.
28a9643ea8Slogwang  *
29a9643ea8Slogwang  * Portions of this software were developed by John-Mark Gurney
30a9643ea8Slogwang  * under sponsorship of the FreeBSD Foundation and
31a9643ea8Slogwang  * Rubicon Communications, LLC (Netgate).
32a9643ea8Slogwang  *
33a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
34a9643ea8Slogwang  * modification, are permitted provided that the following conditions
35a9643ea8Slogwang  * are met:
36a9643ea8Slogwang  *
37a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
38a9643ea8Slogwang  *   notice, this list of conditions and the following disclaimer.
39a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
40a9643ea8Slogwang  *   notice, this list of conditions and the following disclaimer in the
41a9643ea8Slogwang  *   documentation and/or other materials provided with the distribution.
42a9643ea8Slogwang  * 3. The name of the author may not be used to endorse or promote products
43a9643ea8Slogwang  *   derived from this software without specific prior written permission.
44a9643ea8Slogwang  *
45a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46a9643ea8Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47a9643ea8Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48a9643ea8Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49a9643ea8Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50a9643ea8Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51a9643ea8Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52a9643ea8Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53a9643ea8Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54a9643ea8Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55a9643ea8Slogwang  *
56a9643ea8Slogwang  * Effort sponsored in part by the Defense Advanced Research Projects
57a9643ea8Slogwang  * Agency (DARPA) and Air Force Research Laboratory, Air Force
58a9643ea8Slogwang  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
59a9643ea8Slogwang  *
60a9643ea8Slogwang  */
61a9643ea8Slogwang 
62a9643ea8Slogwang #ifndef _CRYPTO_CRYPTO_H_
63a9643ea8Slogwang #define _CRYPTO_CRYPTO_H_
64a9643ea8Slogwang 
65a9643ea8Slogwang #include <sys/ioccom.h>
66a9643ea8Slogwang 
67*22ce4affSfengbojiang #ifdef _KERNEL
68*22ce4affSfengbojiang #include <opencrypto/_cryptodev.h>
69*22ce4affSfengbojiang #include <sys/_task.h>
70*22ce4affSfengbojiang #endif
71*22ce4affSfengbojiang 
72a9643ea8Slogwang /* Some initial values */
73a9643ea8Slogwang #define CRYPTO_DRIVERS_INITIAL	4
74a9643ea8Slogwang 
75a9643ea8Slogwang /* Hash values */
76a9643ea8Slogwang #define	NULL_HASH_LEN		16
77a9643ea8Slogwang #define	SHA1_HASH_LEN		20
78a9643ea8Slogwang #define	RIPEMD160_HASH_LEN	20
79*22ce4affSfengbojiang #define	SHA2_224_HASH_LEN	28
80a9643ea8Slogwang #define	SHA2_256_HASH_LEN	32
81a9643ea8Slogwang #define	SHA2_384_HASH_LEN	48
82a9643ea8Slogwang #define	SHA2_512_HASH_LEN	64
83a9643ea8Slogwang #define	AES_GMAC_HASH_LEN	16
84*22ce4affSfengbojiang #define	POLY1305_HASH_LEN	16
85*22ce4affSfengbojiang #define	AES_CBC_MAC_HASH_LEN	16
86a9643ea8Slogwang /* Maximum hash algorithm result length */
87a9643ea8Slogwang #define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
88a9643ea8Slogwang 
89*22ce4affSfengbojiang #define	SHA1_BLOCK_LEN		64
90*22ce4affSfengbojiang #define	RIPEMD160_BLOCK_LEN	64
91*22ce4affSfengbojiang #define	SHA2_224_BLOCK_LEN	64
92*22ce4affSfengbojiang #define	SHA2_256_BLOCK_LEN	64
93*22ce4affSfengbojiang #define	SHA2_384_BLOCK_LEN	128
94*22ce4affSfengbojiang #define	SHA2_512_BLOCK_LEN	128
95*22ce4affSfengbojiang 
96a9643ea8Slogwang /* HMAC values */
97a9643ea8Slogwang #define	NULL_HMAC_BLOCK_LEN		64
98a9643ea8Slogwang /* Maximum HMAC block length */
99*22ce4affSfengbojiang #define	HMAC_MAX_BLOCK_LEN	SHA2_512_BLOCK_LEN /* Keep this updated */
100a9643ea8Slogwang #define	HMAC_IPAD_VAL			0x36
101a9643ea8Slogwang #define	HMAC_OPAD_VAL			0x5C
102a9643ea8Slogwang /* HMAC Key Length */
103a9643ea8Slogwang #define	AES_128_GMAC_KEY_LEN		16
104a9643ea8Slogwang #define	AES_192_GMAC_KEY_LEN		24
105a9643ea8Slogwang #define	AES_256_GMAC_KEY_LEN		32
106*22ce4affSfengbojiang #define	AES_128_CBC_MAC_KEY_LEN		16
107*22ce4affSfengbojiang #define	AES_192_CBC_MAC_KEY_LEN		24
108*22ce4affSfengbojiang #define	AES_256_CBC_MAC_KEY_LEN		32
109*22ce4affSfengbojiang 
110*22ce4affSfengbojiang #define	POLY1305_KEY_LEN		32
111a9643ea8Slogwang 
112a9643ea8Slogwang /* Encryption algorithm block sizes */
113a9643ea8Slogwang #define	NULL_BLOCK_LEN		4	/* IPsec to maintain alignment */
114a9643ea8Slogwang #define	RIJNDAEL128_BLOCK_LEN	16
115a9643ea8Slogwang #define	AES_BLOCK_LEN		16
116a9643ea8Slogwang #define	AES_ICM_BLOCK_LEN	1
117a9643ea8Slogwang #define	CAMELLIA_BLOCK_LEN	16
118*22ce4affSfengbojiang #define	CHACHA20_NATIVE_BLOCK_LEN	64
119*22ce4affSfengbojiang #define	EALG_MAX_BLOCK_LEN	CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
120a9643ea8Slogwang 
121a9643ea8Slogwang /* IV Lengths */
122a9643ea8Slogwang 
123a9643ea8Slogwang #define	AES_GCM_IV_LEN		12
124*22ce4affSfengbojiang #define	AES_CCM_IV_LEN		12
125a9643ea8Slogwang #define	AES_XTS_IV_LEN		8
126a9643ea8Slogwang #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
127a9643ea8Slogwang 
128a9643ea8Slogwang /* Min and Max Encryption Key Sizes */
129a9643ea8Slogwang #define	NULL_MIN_KEY		0
130a9643ea8Slogwang #define	NULL_MAX_KEY		256 /* 2048 bits, max key */
131a9643ea8Slogwang #define	RIJNDAEL_MIN_KEY	16
132a9643ea8Slogwang #define	RIJNDAEL_MAX_KEY	32
133a9643ea8Slogwang #define	AES_MIN_KEY		RIJNDAEL_MIN_KEY
134a9643ea8Slogwang #define	AES_MAX_KEY		RIJNDAEL_MAX_KEY
135a9643ea8Slogwang #define	AES_XTS_MIN_KEY		(2 * AES_MIN_KEY)
136a9643ea8Slogwang #define	AES_XTS_MAX_KEY		(2 * AES_MAX_KEY)
137*22ce4affSfengbojiang #define	CAMELLIA_MIN_KEY	16
138a9643ea8Slogwang #define	CAMELLIA_MAX_KEY	32
139a9643ea8Slogwang 
140a9643ea8Slogwang /* Maximum hash algorithm result length */
141a9643ea8Slogwang #define	AALG_MAX_RESULT_LEN	64 /* Keep this updated */
142a9643ea8Slogwang 
143a9643ea8Slogwang #define	CRYPTO_ALGORITHM_MIN	1
144a9643ea8Slogwang #define	CRYPTO_DES_CBC		1
145a9643ea8Slogwang #define	CRYPTO_3DES_CBC		2
146a9643ea8Slogwang #define	CRYPTO_BLF_CBC		3
147a9643ea8Slogwang #define	CRYPTO_CAST_CBC		4
148a9643ea8Slogwang #define	CRYPTO_SKIPJACK_CBC	5
149a9643ea8Slogwang #define	CRYPTO_MD5_HMAC		6
150a9643ea8Slogwang #define	CRYPTO_SHA1_HMAC	7
151a9643ea8Slogwang #define	CRYPTO_RIPEMD160_HMAC	8
152a9643ea8Slogwang #define	CRYPTO_MD5_KPDK		9
153a9643ea8Slogwang #define	CRYPTO_SHA1_KPDK	10
154a9643ea8Slogwang #define	CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
155a9643ea8Slogwang #define	CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
156a9643ea8Slogwang #define	CRYPTO_ARC4		12
157a9643ea8Slogwang #define	CRYPTO_MD5		13
158a9643ea8Slogwang #define	CRYPTO_SHA1		14
159a9643ea8Slogwang #define	CRYPTO_NULL_HMAC	15
160a9643ea8Slogwang #define	CRYPTO_NULL_CBC		16
161a9643ea8Slogwang #define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
162a9643ea8Slogwang #define	CRYPTO_SHA2_256_HMAC	18
163a9643ea8Slogwang #define	CRYPTO_SHA2_384_HMAC	19
164a9643ea8Slogwang #define	CRYPTO_SHA2_512_HMAC	20
165a9643ea8Slogwang #define	CRYPTO_CAMELLIA_CBC	21
166a9643ea8Slogwang #define	CRYPTO_AES_XTS		22
167a9643ea8Slogwang #define	CRYPTO_AES_ICM		23 /* commonly known as CTR mode */
168*22ce4affSfengbojiang #define	CRYPTO_AES_NIST_GMAC	24 /* GMAC only */
169a9643ea8Slogwang #define	CRYPTO_AES_NIST_GCM_16	25 /* 16 byte ICV */
170*22ce4affSfengbojiang #ifdef _KERNEL
171a9643ea8Slogwang #define	CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
172a9643ea8Slogwang #define	CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
173a9643ea8Slogwang #define	CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
174*22ce4affSfengbojiang #endif
175*22ce4affSfengbojiang #define	CRYPTO_BLAKE2B		29 /* Blake2b hash */
176*22ce4affSfengbojiang #define	CRYPTO_BLAKE2S		30 /* Blake2s hash */
177*22ce4affSfengbojiang #define	CRYPTO_CHACHA20		31 /* Chacha20 stream cipher */
178*22ce4affSfengbojiang #define	CRYPTO_SHA2_224_HMAC	32
179*22ce4affSfengbojiang #define	CRYPTO_RIPEMD160	33
180*22ce4affSfengbojiang #define	CRYPTO_SHA2_224		34
181*22ce4affSfengbojiang #define	CRYPTO_SHA2_256		35
182*22ce4affSfengbojiang #define	CRYPTO_SHA2_384		36
183*22ce4affSfengbojiang #define	CRYPTO_SHA2_512		37
184*22ce4affSfengbojiang #define	CRYPTO_POLY1305		38
185*22ce4affSfengbojiang #define	CRYPTO_AES_CCM_CBC_MAC	39	/* auth side */
186*22ce4affSfengbojiang #define	CRYPTO_AES_CCM_16	40	/* cipher side */
187*22ce4affSfengbojiang #define	CRYPTO_ALGORITHM_MAX	40	/* Keep updated - see below */
188a9643ea8Slogwang 
189a9643ea8Slogwang #define	CRYPTO_ALGO_VALID(x)	((x) >= CRYPTO_ALGORITHM_MIN && \
190a9643ea8Slogwang 				 (x) <= CRYPTO_ALGORITHM_MAX)
191a9643ea8Slogwang 
192a9643ea8Slogwang /* Algorithm flags */
193a9643ea8Slogwang #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
194a9643ea8Slogwang #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
195a9643ea8Slogwang #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
196a9643ea8Slogwang 
197a9643ea8Slogwang /*
198a9643ea8Slogwang  * Crypto driver/device flags.  They can set in the crid
199a9643ea8Slogwang  * parameter when creating a session or submitting a key
200a9643ea8Slogwang  * op to affect the device/driver assigned.  If neither
201a9643ea8Slogwang  * of these are specified then the crid is assumed to hold
202a9643ea8Slogwang  * the driver id of an existing (and suitable) device that
203a9643ea8Slogwang  * must be used to satisfy the request.
204a9643ea8Slogwang  */
205a9643ea8Slogwang #define CRYPTO_FLAG_HARDWARE	0x01000000	/* hardware accelerated */
206a9643ea8Slogwang #define CRYPTO_FLAG_SOFTWARE	0x02000000	/* software implementation */
207a9643ea8Slogwang 
208*22ce4affSfengbojiang /* Does the kernel support vmpage buffers on this platform? */
209*22ce4affSfengbojiang #ifdef __powerpc__
210*22ce4affSfengbojiang #define CRYPTO_MAY_HAVE_VMPAGE	1
211*22ce4affSfengbojiang #else
212*22ce4affSfengbojiang #define CRYPTO_MAY_HAVE_VMPAGE	( PMAP_HAS_DMAP )
213*22ce4affSfengbojiang #endif
214*22ce4affSfengbojiang /* Does the currently running system support vmpage buffers on this platform? */
215*22ce4affSfengbojiang #define CRYPTO_HAS_VMPAGE	( PMAP_HAS_DMAP )
216*22ce4affSfengbojiang 
217a9643ea8Slogwang /* NB: deprecated */
218a9643ea8Slogwang struct session_op {
219*22ce4affSfengbojiang 	uint32_t	cipher;		/* ie. CRYPTO_AES_CBC */
220*22ce4affSfengbojiang 	uint32_t	mac;		/* ie. CRYPTO_SHA2_256_HMAC */
221a9643ea8Slogwang 
222*22ce4affSfengbojiang 	uint32_t	keylen;		/* cipher key */
223*22ce4affSfengbojiang 	const void	*key;
224a9643ea8Slogwang 	int		mackeylen;	/* mac key */
225*22ce4affSfengbojiang 	const void	*mackey;
226a9643ea8Slogwang 
227*22ce4affSfengbojiang   	uint32_t	ses;		/* returns: session # */
228a9643ea8Slogwang };
229a9643ea8Slogwang 
230*22ce4affSfengbojiang /*
231*22ce4affSfengbojiang  * session and crypt _op structs are used by userspace programs to interact
232*22ce4affSfengbojiang  * with /dev/crypto.  Confusingly, the internal kernel interface is named
233*22ce4affSfengbojiang  * "cryptop" (no underscore).
234*22ce4affSfengbojiang  */
235a9643ea8Slogwang struct session2_op {
236*22ce4affSfengbojiang 	uint32_t	cipher;		/* ie. CRYPTO_AES_CBC */
237*22ce4affSfengbojiang 	uint32_t	mac;		/* ie. CRYPTO_SHA2_256_HMAC */
238a9643ea8Slogwang 
239*22ce4affSfengbojiang 	uint32_t	keylen;		/* cipher key */
240*22ce4affSfengbojiang 	const void	*key;
241a9643ea8Slogwang 	int		mackeylen;	/* mac key */
242*22ce4affSfengbojiang 	const void	*mackey;
243a9643ea8Slogwang 
244*22ce4affSfengbojiang   	uint32_t	ses;		/* returns: session # */
245a9643ea8Slogwang 	int		crid;		/* driver id + flags (rw) */
246a9643ea8Slogwang 	int		pad[4];		/* for future expansion */
247a9643ea8Slogwang };
248a9643ea8Slogwang 
249a9643ea8Slogwang struct crypt_op {
250*22ce4affSfengbojiang 	uint32_t	ses;
251*22ce4affSfengbojiang 	uint16_t	op;		/* i.e. COP_ENCRYPT */
252a9643ea8Slogwang #define COP_ENCRYPT	1
253a9643ea8Slogwang #define COP_DECRYPT	2
254*22ce4affSfengbojiang 	uint16_t	flags;
255*22ce4affSfengbojiang #define	COP_F_CIPHER_FIRST	0x0001	/* Cipher before MAC. */
256a9643ea8Slogwang #define	COP_F_BATCH		0x0008	/* Batch op if possible */
257a9643ea8Slogwang 	u_int		len;
258*22ce4affSfengbojiang 	const void	*src;		/* become iov[] inside kernel */
259*22ce4affSfengbojiang 	void		*dst;
260*22ce4affSfengbojiang 	void		*mac;		/* must be big enough for chosen MAC */
261*22ce4affSfengbojiang 	const void	*iv;
262a9643ea8Slogwang };
263a9643ea8Slogwang 
264a9643ea8Slogwang /* op and flags the same as crypt_op */
265a9643ea8Slogwang struct crypt_aead {
266*22ce4affSfengbojiang 	uint32_t	ses;
267*22ce4affSfengbojiang 	uint16_t	op;		/* i.e. COP_ENCRYPT */
268*22ce4affSfengbojiang 	uint16_t	flags;
269a9643ea8Slogwang 	u_int		len;
270a9643ea8Slogwang 	u_int		aadlen;
271a9643ea8Slogwang 	u_int		ivlen;
272*22ce4affSfengbojiang 	const void	*src;		/* become iov[] inside kernel */
273*22ce4affSfengbojiang 	void		*dst;
274*22ce4affSfengbojiang 	const void	*aad;		/* additional authenticated data */
275*22ce4affSfengbojiang 	void		*tag;		/* must fit for chosen TAG length */
276*22ce4affSfengbojiang 	const void	*iv;
277a9643ea8Slogwang };
278a9643ea8Slogwang 
279a9643ea8Slogwang /*
280a9643ea8Slogwang  * Parameters for looking up a crypto driver/device by
281a9643ea8Slogwang  * device name or by id.  The latter are returned for
282a9643ea8Slogwang  * created sessions (crid) and completed key operations.
283a9643ea8Slogwang  */
284a9643ea8Slogwang struct crypt_find_op {
285a9643ea8Slogwang 	int		crid;		/* driver id + flags */
286a9643ea8Slogwang 	char		name[32];	/* device/driver name */
287a9643ea8Slogwang };
288a9643ea8Slogwang 
289a9643ea8Slogwang /* bignum parameter, in packed bytes, ... */
290a9643ea8Slogwang struct crparam {
291*22ce4affSfengbojiang 	void		*crp_p;
292a9643ea8Slogwang 	u_int		crp_nbits;
293a9643ea8Slogwang };
294a9643ea8Slogwang 
295a9643ea8Slogwang #define CRK_MAXPARAM	8
296a9643ea8Slogwang 
297a9643ea8Slogwang struct crypt_kop {
298a9643ea8Slogwang 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
299a9643ea8Slogwang 	u_int		crk_status;	/* return status */
300a9643ea8Slogwang 	u_short		crk_iparams;	/* # of input parameters */
301a9643ea8Slogwang 	u_short		crk_oparams;	/* # of output parameters */
302a9643ea8Slogwang 	u_int		crk_crid;	/* NB: only used by CIOCKEY2 (rw) */
303a9643ea8Slogwang 	struct crparam	crk_param[CRK_MAXPARAM];
304a9643ea8Slogwang };
305a9643ea8Slogwang #define	CRK_ALGORITM_MIN	0
306a9643ea8Slogwang #define CRK_MOD_EXP		0
307a9643ea8Slogwang #define CRK_MOD_EXP_CRT		1
308a9643ea8Slogwang #define CRK_DSA_SIGN		2
309a9643ea8Slogwang #define CRK_DSA_VERIFY		3
310a9643ea8Slogwang #define CRK_DH_COMPUTE_KEY	4
311a9643ea8Slogwang #define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
312a9643ea8Slogwang 
313a9643ea8Slogwang #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
314a9643ea8Slogwang #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
315a9643ea8Slogwang #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
316a9643ea8Slogwang #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
317a9643ea8Slogwang #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
318a9643ea8Slogwang 
319a9643ea8Slogwang #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
320*22ce4affSfengbojiang #define	CIOCFSESSION	_IOW('c', 102, uint32_t)
321a9643ea8Slogwang #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
322a9643ea8Slogwang #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
323*22ce4affSfengbojiang #define CIOCASYMFEAT	_IOR('c', 105, uint32_t)
324a9643ea8Slogwang #define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
325a9643ea8Slogwang #define	CIOCKEY2	_IOWR('c', 107, struct crypt_kop)
326a9643ea8Slogwang #define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
327a9643ea8Slogwang #define	CIOCCRYPTAEAD	_IOWR('c', 109, struct crypt_aead)
328a9643ea8Slogwang 
329a9643ea8Slogwang struct cryptostats {
330*22ce4affSfengbojiang 	uint64_t	cs_ops;		/* symmetric crypto ops submitted */
331*22ce4affSfengbojiang 	uint64_t	cs_errs;	/* symmetric crypto ops that failed */
332*22ce4affSfengbojiang 	uint64_t	cs_kops;	/* asymetric/key ops submitted */
333*22ce4affSfengbojiang 	uint64_t	cs_kerrs;	/* asymetric/key ops that failed */
334*22ce4affSfengbojiang 	uint64_t	cs_intrs;	/* crypto swi thread activations */
335*22ce4affSfengbojiang 	uint64_t	cs_rets;	/* crypto return thread activations */
336*22ce4affSfengbojiang 	uint64_t	cs_blocks;	/* symmetric op driver block */
337*22ce4affSfengbojiang 	uint64_t	cs_kblocks;	/* symmetric op driver block */
338a9643ea8Slogwang };
339a9643ea8Slogwang 
340a9643ea8Slogwang #ifdef _KERNEL
341a9643ea8Slogwang 
342*22ce4affSfengbojiang /*
343*22ce4affSfengbojiang  * Return values for cryptodev_probesession methods.
344*22ce4affSfengbojiang  */
345*22ce4affSfengbojiang #define	CRYPTODEV_PROBE_HARDWARE	(-100)
346*22ce4affSfengbojiang #define	CRYPTODEV_PROBE_ACCEL_SOFTWARE	(-200)
347*22ce4affSfengbojiang #define	CRYPTODEV_PROBE_SOFTWARE	(-500)
348*22ce4affSfengbojiang 
349a9643ea8Slogwang #if 0
350*22ce4affSfengbojiang #define CRYPTDEB(s, ...) do {						\
351*22ce4affSfengbojiang 	printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);	\
352a9643ea8Slogwang } while (0)
353a9643ea8Slogwang #else
354*22ce4affSfengbojiang #define CRYPTDEB(...)	do { } while (0)
355a9643ea8Slogwang #endif
356a9643ea8Slogwang 
357*22ce4affSfengbojiang struct crypto_session_params {
358*22ce4affSfengbojiang 	int		csp_mode;	/* Type of operations to perform. */
359*22ce4affSfengbojiang 
360*22ce4affSfengbojiang #define	CSP_MODE_NONE		0
361*22ce4affSfengbojiang #define	CSP_MODE_COMPRESS	1	/* Compression/decompression. */
362*22ce4affSfengbojiang #define	CSP_MODE_CIPHER		2	/* Encrypt/decrypt. */
363*22ce4affSfengbojiang #define	CSP_MODE_DIGEST		3	/* Compute/verify digest. */
364*22ce4affSfengbojiang #define	CSP_MODE_AEAD		4	/* Combined auth/encryption. */
365*22ce4affSfengbojiang #define	CSP_MODE_ETA		5	/* IPsec style encrypt-then-auth */
366*22ce4affSfengbojiang 
367*22ce4affSfengbojiang 	int		csp_flags;
368*22ce4affSfengbojiang 
369*22ce4affSfengbojiang #define	CSP_F_SEPARATE_OUTPUT	0x0001	/* Requests can use separate output */
370*22ce4affSfengbojiang #define	CSP_F_SEPARATE_AAD	0x0002	/* Requests can use separate AAD */
371*22ce4affSfengbojiang #define CSP_F_ESN		0x0004  /* Requests can use seperate ESN field */
372*22ce4affSfengbojiang 
373*22ce4affSfengbojiang 	int		csp_ivlen;	/* IV length in bytes. */
374*22ce4affSfengbojiang 
375*22ce4affSfengbojiang 	int		csp_cipher_alg;
376*22ce4affSfengbojiang 	int		csp_cipher_klen; /* Key length in bytes. */
377*22ce4affSfengbojiang 	const void	*csp_cipher_key;
378*22ce4affSfengbojiang 
379*22ce4affSfengbojiang 	int		csp_auth_alg;
380*22ce4affSfengbojiang 	int		csp_auth_klen;	/* Key length in bytes. */
381*22ce4affSfengbojiang 	const void	*csp_auth_key;
382*22ce4affSfengbojiang 	int		csp_auth_mlen;	/* Number of digest bytes to use.
383*22ce4affSfengbojiang 					   0 means all. */
384a9643ea8Slogwang };
385a9643ea8Slogwang 
386*22ce4affSfengbojiang enum crypto_buffer_type {
387*22ce4affSfengbojiang 	CRYPTO_BUF_NONE = 0,
388*22ce4affSfengbojiang 	CRYPTO_BUF_CONTIG,
389*22ce4affSfengbojiang 	CRYPTO_BUF_UIO,
390*22ce4affSfengbojiang 	CRYPTO_BUF_MBUF,
391*22ce4affSfengbojiang 	CRYPTO_BUF_VMPAGE,
392*22ce4affSfengbojiang 	CRYPTO_BUF_LAST = CRYPTO_BUF_VMPAGE
393*22ce4affSfengbojiang };
394a9643ea8Slogwang 
395*22ce4affSfengbojiang /*
396*22ce4affSfengbojiang  * Description of a data buffer for a request.  Requests can either
397*22ce4affSfengbojiang  * have a single buffer that is modified in place or separate input
398*22ce4affSfengbojiang  * and output buffers.
399*22ce4affSfengbojiang  */
400*22ce4affSfengbojiang struct crypto_buffer {
401*22ce4affSfengbojiang 	union {
402*22ce4affSfengbojiang 		struct {
403*22ce4affSfengbojiang 			char	*cb_buf;
404*22ce4affSfengbojiang 			int	cb_buf_len;
405*22ce4affSfengbojiang 		};
406*22ce4affSfengbojiang 		struct mbuf *cb_mbuf;
407*22ce4affSfengbojiang 		struct {
408*22ce4affSfengbojiang 			vm_page_t *cb_vm_page;
409*22ce4affSfengbojiang 			int cb_vm_page_len;
410*22ce4affSfengbojiang 			int cb_vm_page_offset;
411*22ce4affSfengbojiang 		};
412*22ce4affSfengbojiang 		struct uio *cb_uio;
413*22ce4affSfengbojiang 	};
414*22ce4affSfengbojiang 	enum crypto_buffer_type cb_type;
415*22ce4affSfengbojiang };
416a9643ea8Slogwang 
417*22ce4affSfengbojiang /*
418*22ce4affSfengbojiang  * A cursor is used to iterate through a crypto request data buffer.
419*22ce4affSfengbojiang  */
420*22ce4affSfengbojiang struct crypto_buffer_cursor {
421*22ce4affSfengbojiang 	union {
422*22ce4affSfengbojiang 		char *cc_buf;
423*22ce4affSfengbojiang 		struct mbuf *cc_mbuf;
424*22ce4affSfengbojiang 		struct iovec *cc_iov;
425*22ce4affSfengbojiang 		vm_page_t *cc_vmpage;
426*22ce4affSfengbojiang 	};
427*22ce4affSfengbojiang 	/* Optional bytes of valid data remaining */
428*22ce4affSfengbojiang 	int cc_buf_len;
429*22ce4affSfengbojiang 	/*
430*22ce4affSfengbojiang 	 * Optional offset within the current buffer segment where
431*22ce4affSfengbojiang 	 * valid data begins
432*22ce4affSfengbojiang 	 */
433*22ce4affSfengbojiang 	size_t cc_offset;
434*22ce4affSfengbojiang 	enum crypto_buffer_type cc_type;
435a9643ea8Slogwang };
436a9643ea8Slogwang 
437a9643ea8Slogwang /* Structure describing complete operation */
438a9643ea8Slogwang struct cryptop {
439a9643ea8Slogwang 	TAILQ_ENTRY(cryptop) crp_next;
440a9643ea8Slogwang 
441*22ce4affSfengbojiang 	struct task	crp_task;
442*22ce4affSfengbojiang 
443*22ce4affSfengbojiang 	crypto_session_t crp_session;	/* Session */
444a9643ea8Slogwang 	int		crp_olen;	/* Result total length */
445a9643ea8Slogwang 
446a9643ea8Slogwang 	int		crp_etype;	/*
447a9643ea8Slogwang 					 * Error type (zero means no error).
448a9643ea8Slogwang 					 * All error codes except EAGAIN
449a9643ea8Slogwang 					 * indicate possible data corruption (as in,
450a9643ea8Slogwang 					 * the data have been touched). On all
451*22ce4affSfengbojiang 					 * errors, the crp_session may have changed
452a9643ea8Slogwang 					 * (reset to a new one), so the caller
453a9643ea8Slogwang 					 * should always check and use the new
454a9643ea8Slogwang 					 * value on future requests.
455a9643ea8Slogwang 					 */
456a9643ea8Slogwang 	int		crp_flags;
457a9643ea8Slogwang 
458a9643ea8Slogwang #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible */
459a9643ea8Slogwang #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
460a9643ea8Slogwang #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
461a9643ea8Slogwang #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
462*22ce4affSfengbojiang #define	CRYPTO_F_ASYNC		0x0080	/* Dispatch crypto jobs on several threads
463*22ce4affSfengbojiang 					 * if op is synchronous
464*22ce4affSfengbojiang 					 */
465*22ce4affSfengbojiang #define	CRYPTO_F_ASYNC_KEEPORDER	0x0100	/*
466*22ce4affSfengbojiang 					 * Dispatch the crypto jobs in the same
467*22ce4affSfengbojiang 					 * order there are submitted. Applied only
468*22ce4affSfengbojiang 					 * if CRYPTO_F_ASYNC flags is set
469*22ce4affSfengbojiang 					 */
470*22ce4affSfengbojiang #define	CRYPTO_F_IV_SEPARATE	0x0200	/* Use crp_iv[] as IV. */
471a9643ea8Slogwang 
472*22ce4affSfengbojiang 	int		crp_op;
473*22ce4affSfengbojiang 
474*22ce4affSfengbojiang 	struct crypto_buffer crp_buf;
475*22ce4affSfengbojiang 	struct crypto_buffer crp_obuf;
476*22ce4affSfengbojiang 
477*22ce4affSfengbojiang 	void		*crp_aad;	/* AAD buffer. */
478*22ce4affSfengbojiang 	int		crp_aad_start;	/* Location of AAD. */
479*22ce4affSfengbojiang 	int		crp_aad_length;	/* 0 => no AAD. */
480*22ce4affSfengbojiang 	uint8_t		crp_esn[4];	/* high-order ESN */
481*22ce4affSfengbojiang 
482*22ce4affSfengbojiang 	int		crp_iv_start;	/* Location of IV.  IV length is from
483*22ce4affSfengbojiang 					 * the session.
484*22ce4affSfengbojiang 					 */
485*22ce4affSfengbojiang 	int		crp_payload_start; /* Location of ciphertext. */
486*22ce4affSfengbojiang 	int		crp_payload_output_start;
487*22ce4affSfengbojiang 	int		crp_payload_length;
488*22ce4affSfengbojiang 	int		crp_digest_start; /* Location of MAC/tag.  Length is
489*22ce4affSfengbojiang 					   * from the session.
490*22ce4affSfengbojiang 					   */
491*22ce4affSfengbojiang 
492*22ce4affSfengbojiang 	uint8_t		crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
493*22ce4affSfengbojiang 
494*22ce4affSfengbojiang 	const void	*crp_cipher_key; /* New cipher key if non-NULL. */
495*22ce4affSfengbojiang 	const void	*crp_auth_key;	/* New auth key if non-NULL. */
496*22ce4affSfengbojiang 
497*22ce4affSfengbojiang 	void		*crp_opaque;	/* Opaque pointer, passed along */
498a9643ea8Slogwang 
499a9643ea8Slogwang 	int (*crp_callback)(struct cryptop *); /* Callback function */
500a9643ea8Slogwang 
501a9643ea8Slogwang 	struct bintime	crp_tstamp;	/* performance time stamp */
502*22ce4affSfengbojiang 	uint32_t	crp_seq;	/* used for ordered dispatch */
503*22ce4affSfengbojiang 	uint32_t	crp_retw_id;	/*
504*22ce4affSfengbojiang 					 * the return worker to be used,
505*22ce4affSfengbojiang 					 *  used for ordered dispatch
506*22ce4affSfengbojiang 					 */
507a9643ea8Slogwang };
508a9643ea8Slogwang 
509*22ce4affSfengbojiang static __inline void
_crypto_use_buf(struct crypto_buffer * cb,void * buf,int len)510*22ce4affSfengbojiang _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
511*22ce4affSfengbojiang {
512*22ce4affSfengbojiang 	cb->cb_buf = buf;
513*22ce4affSfengbojiang 	cb->cb_buf_len = len;
514*22ce4affSfengbojiang 	cb->cb_type = CRYPTO_BUF_CONTIG;
515*22ce4affSfengbojiang }
516a9643ea8Slogwang 
517*22ce4affSfengbojiang static __inline void
_crypto_use_mbuf(struct crypto_buffer * cb,struct mbuf * m)518*22ce4affSfengbojiang _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
519*22ce4affSfengbojiang {
520*22ce4affSfengbojiang 	cb->cb_mbuf = m;
521*22ce4affSfengbojiang 	cb->cb_type = CRYPTO_BUF_MBUF;
522*22ce4affSfengbojiang }
523*22ce4affSfengbojiang 
524*22ce4affSfengbojiang static __inline void
_crypto_use_vmpage(struct crypto_buffer * cb,vm_page_t * pages,int len,int offset)525*22ce4affSfengbojiang _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
526*22ce4affSfengbojiang     int offset)
527*22ce4affSfengbojiang {
528*22ce4affSfengbojiang 	cb->cb_vm_page = pages;
529*22ce4affSfengbojiang 	cb->cb_vm_page_len = len;
530*22ce4affSfengbojiang 	cb->cb_vm_page_offset = offset;
531*22ce4affSfengbojiang 	cb->cb_type = CRYPTO_BUF_VMPAGE;
532*22ce4affSfengbojiang }
533*22ce4affSfengbojiang 
534*22ce4affSfengbojiang static __inline void
_crypto_use_uio(struct crypto_buffer * cb,struct uio * uio)535*22ce4affSfengbojiang _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
536*22ce4affSfengbojiang {
537*22ce4affSfengbojiang 	cb->cb_uio = uio;
538*22ce4affSfengbojiang 	cb->cb_type = CRYPTO_BUF_UIO;
539*22ce4affSfengbojiang }
540*22ce4affSfengbojiang 
541*22ce4affSfengbojiang static __inline void
crypto_use_buf(struct cryptop * crp,void * buf,int len)542*22ce4affSfengbojiang crypto_use_buf(struct cryptop *crp, void *buf, int len)
543*22ce4affSfengbojiang {
544*22ce4affSfengbojiang 	_crypto_use_buf(&crp->crp_buf, buf, len);
545*22ce4affSfengbojiang }
546*22ce4affSfengbojiang 
547*22ce4affSfengbojiang static __inline void
crypto_use_mbuf(struct cryptop * crp,struct mbuf * m)548*22ce4affSfengbojiang crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
549*22ce4affSfengbojiang {
550*22ce4affSfengbojiang 	_crypto_use_mbuf(&crp->crp_buf, m);
551*22ce4affSfengbojiang }
552*22ce4affSfengbojiang 
553*22ce4affSfengbojiang static __inline void
crypto_use_vmpage(struct cryptop * crp,vm_page_t * pages,int len,int offset)554*22ce4affSfengbojiang crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
555*22ce4affSfengbojiang {
556*22ce4affSfengbojiang 	_crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
557*22ce4affSfengbojiang }
558*22ce4affSfengbojiang 
559*22ce4affSfengbojiang static __inline void
crypto_use_uio(struct cryptop * crp,struct uio * uio)560*22ce4affSfengbojiang crypto_use_uio(struct cryptop *crp, struct uio *uio)
561*22ce4affSfengbojiang {
562*22ce4affSfengbojiang 	_crypto_use_uio(&crp->crp_buf, uio);
563*22ce4affSfengbojiang }
564*22ce4affSfengbojiang 
565*22ce4affSfengbojiang static __inline void
crypto_use_output_buf(struct cryptop * crp,void * buf,int len)566*22ce4affSfengbojiang crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
567*22ce4affSfengbojiang {
568*22ce4affSfengbojiang 	_crypto_use_buf(&crp->crp_obuf, buf, len);
569*22ce4affSfengbojiang }
570*22ce4affSfengbojiang 
571*22ce4affSfengbojiang static __inline void
crypto_use_output_mbuf(struct cryptop * crp,struct mbuf * m)572*22ce4affSfengbojiang crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
573*22ce4affSfengbojiang {
574*22ce4affSfengbojiang 	_crypto_use_mbuf(&crp->crp_obuf, m);
575*22ce4affSfengbojiang }
576*22ce4affSfengbojiang 
577*22ce4affSfengbojiang static __inline void
crypto_use_output_vmpage(struct cryptop * crp,vm_page_t * pages,int len,int offset)578*22ce4affSfengbojiang crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
579*22ce4affSfengbojiang     int offset)
580*22ce4affSfengbojiang {
581*22ce4affSfengbojiang 	_crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
582*22ce4affSfengbojiang }
583*22ce4affSfengbojiang 
584*22ce4affSfengbojiang static __inline void
crypto_use_output_uio(struct cryptop * crp,struct uio * uio)585*22ce4affSfengbojiang crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
586*22ce4affSfengbojiang {
587*22ce4affSfengbojiang 	_crypto_use_uio(&crp->crp_obuf, uio);
588*22ce4affSfengbojiang }
589*22ce4affSfengbojiang 
590*22ce4affSfengbojiang #define	CRYPTOP_ASYNC(crp)			\
591*22ce4affSfengbojiang 	(((crp)->crp_flags & CRYPTO_F_ASYNC) && \
592*22ce4affSfengbojiang 	crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
593*22ce4affSfengbojiang #define	CRYPTOP_ASYNC_KEEPORDER(crp) \
594*22ce4affSfengbojiang 	(CRYPTOP_ASYNC(crp) && \
595*22ce4affSfengbojiang 	(crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
596*22ce4affSfengbojiang #define	CRYPTO_HAS_OUTPUT_BUFFER(crp)					\
597*22ce4affSfengbojiang 	((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
598*22ce4affSfengbojiang 
599*22ce4affSfengbojiang /* Flags in crp_op. */
600a9643ea8Slogwang #define	CRYPTO_OP_DECRYPT		0x0
601a9643ea8Slogwang #define	CRYPTO_OP_ENCRYPT		0x1
602*22ce4affSfengbojiang #define	CRYPTO_OP_IS_ENCRYPT(op)	((op) & CRYPTO_OP_ENCRYPT)
603*22ce4affSfengbojiang #define	CRYPTO_OP_COMPUTE_DIGEST	0x0
604*22ce4affSfengbojiang #define	CRYPTO_OP_VERIFY_DIGEST		0x2
605*22ce4affSfengbojiang #define	CRYPTO_OP_DECOMPRESS		CRYPTO_OP_DECRYPT
606*22ce4affSfengbojiang #define	CRYPTO_OP_COMPRESS		CRYPTO_OP_ENCRYPT
607*22ce4affSfengbojiang #define	CRYPTO_OP_IS_COMPRESS(op)	((op) & CRYPTO_OP_COMPRESS)
608a9643ea8Slogwang 
609a9643ea8Slogwang /*
610a9643ea8Slogwang  * Hints passed to process methods.
611a9643ea8Slogwang  */
612a9643ea8Slogwang #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
613a9643ea8Slogwang 
614a9643ea8Slogwang struct cryptkop {
615a9643ea8Slogwang 	TAILQ_ENTRY(cryptkop) krp_next;
616a9643ea8Slogwang 
617a9643ea8Slogwang 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
618a9643ea8Slogwang 	u_int		krp_status;	/* return status */
619a9643ea8Slogwang 	u_short		krp_iparams;	/* # of input parameters */
620a9643ea8Slogwang 	u_short		krp_oparams;	/* # of output parameters */
621a9643ea8Slogwang 	u_int		krp_crid;	/* desired device, etc. */
622*22ce4affSfengbojiang 	uint32_t	krp_hid;	/* device used */
623a9643ea8Slogwang 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
624*22ce4affSfengbojiang 	void		(*krp_callback)(struct cryptkop *);
625*22ce4affSfengbojiang 	struct cryptocap *krp_cap;
626a9643ea8Slogwang };
627a9643ea8Slogwang 
628*22ce4affSfengbojiang uint32_t crypto_ses2hid(crypto_session_t crypto_session);
629*22ce4affSfengbojiang uint32_t crypto_ses2caps(crypto_session_t crypto_session);
630*22ce4affSfengbojiang void *crypto_get_driver_session(crypto_session_t crypto_session);
631*22ce4affSfengbojiang const struct crypto_session_params *crypto_get_params(
632*22ce4affSfengbojiang     crypto_session_t crypto_session);
633*22ce4affSfengbojiang struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
634*22ce4affSfengbojiang struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
635a9643ea8Slogwang 
636a9643ea8Slogwang MALLOC_DECLARE(M_CRYPTO_DATA);
637a9643ea8Slogwang 
638*22ce4affSfengbojiang extern	int crypto_newsession(crypto_session_t *cses,
639*22ce4affSfengbojiang     const struct crypto_session_params *params, int hard);
640*22ce4affSfengbojiang extern	void crypto_freesession(crypto_session_t cses);
641a9643ea8Slogwang #define	CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
642a9643ea8Slogwang #define	CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
643a9643ea8Slogwang #define	CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
644*22ce4affSfengbojiang #define	CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
645*22ce4affSfengbojiang extern	int32_t crypto_get_driverid(device_t dev, size_t session_size,
646*22ce4affSfengbojiang     int flags);
647a9643ea8Slogwang extern	int crypto_find_driver(const char *);
648a9643ea8Slogwang extern	device_t crypto_find_device_byhid(int hid);
649a9643ea8Slogwang extern	int crypto_getcaps(int hid);
650*22ce4affSfengbojiang extern	int crypto_kregister(uint32_t, int, uint32_t);
651*22ce4affSfengbojiang extern	int crypto_unregister_all(uint32_t driverid);
652a9643ea8Slogwang extern	int crypto_dispatch(struct cryptop *crp);
653a9643ea8Slogwang extern	int crypto_kdispatch(struct cryptkop *);
654a9643ea8Slogwang #define	CRYPTO_SYMQ	0x1
655a9643ea8Slogwang #define	CRYPTO_ASYMQ	0x2
656*22ce4affSfengbojiang extern	int crypto_unblock(uint32_t, int);
657a9643ea8Slogwang extern	void crypto_done(struct cryptop *crp);
658a9643ea8Slogwang extern	void crypto_kdone(struct cryptkop *);
659a9643ea8Slogwang extern	int crypto_getfeat(int *);
660a9643ea8Slogwang 
661*22ce4affSfengbojiang extern	void crypto_destroyreq(struct cryptop *crp);
662*22ce4affSfengbojiang extern	void crypto_initreq(struct cryptop *crp, crypto_session_t cses);
663a9643ea8Slogwang extern	void crypto_freereq(struct cryptop *crp);
664*22ce4affSfengbojiang extern	struct cryptop *crypto_getreq(crypto_session_t cses, int how);
665a9643ea8Slogwang 
666a9643ea8Slogwang extern	int crypto_usercrypto;		/* userland may do crypto requests */
667a9643ea8Slogwang extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
668a9643ea8Slogwang extern	int crypto_devallowsoft;	/* only use hardware crypto */
669a9643ea8Slogwang 
670*22ce4affSfengbojiang #ifdef SYSCTL_DECL
671*22ce4affSfengbojiang SYSCTL_DECL(_kern_crypto);
672*22ce4affSfengbojiang #endif
673*22ce4affSfengbojiang 
674*22ce4affSfengbojiang /* Helper routines for drivers to initialize auth contexts for HMAC. */
675*22ce4affSfengbojiang struct auth_hash;
676*22ce4affSfengbojiang 
677*22ce4affSfengbojiang void	hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
678*22ce4affSfengbojiang     void *auth_ctx);
679*22ce4affSfengbojiang void	hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
680*22ce4affSfengbojiang     void *auth_ctx);
681*22ce4affSfengbojiang 
682a9643ea8Slogwang /*
683a9643ea8Slogwang  * Crypto-related utility routines used mainly by drivers.
684a9643ea8Slogwang  *
685*22ce4affSfengbojiang  * Similar to m_copyback/data, *_copyback copy data from the 'src'
686*22ce4affSfengbojiang  * buffer into the crypto request's data buffer while *_copydata copy
687*22ce4affSfengbojiang  * data from the crypto request's data buffer into the the 'dst'
688*22ce4affSfengbojiang  * buffer.
689a9643ea8Slogwang  */
690*22ce4affSfengbojiang void	crypto_copyback(struct cryptop *crp, int off, int size,
691*22ce4affSfengbojiang 	    const void *src);
692*22ce4affSfengbojiang void	crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
693*22ce4affSfengbojiang int	crypto_apply(struct cryptop *crp, int off, int len,
694*22ce4affSfengbojiang 	    int (*f)(void *, const void *, u_int), void *arg);
695*22ce4affSfengbojiang void	*crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
696*22ce4affSfengbojiang 	    size_t len);
697a9643ea8Slogwang 
698*22ce4affSfengbojiang int	crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
699*22ce4affSfengbojiang 	    int (*f)(void *, const void *, u_int), void *arg);
700*22ce4affSfengbojiang void	*crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
701*22ce4affSfengbojiang 	    size_t skip, size_t len);
702*22ce4affSfengbojiang size_t	crypto_buffer_len(struct crypto_buffer *cb);
703*22ce4affSfengbojiang void	crypto_cursor_init(struct crypto_buffer_cursor *cc,
704*22ce4affSfengbojiang 	    const struct crypto_buffer *cb);
705*22ce4affSfengbojiang void	crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
706*22ce4affSfengbojiang void	*crypto_cursor_segbase(struct crypto_buffer_cursor *cc);
707*22ce4affSfengbojiang size_t	crypto_cursor_seglen(struct crypto_buffer_cursor *cc);
708*22ce4affSfengbojiang void	crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
709*22ce4affSfengbojiang 	    const void *vsrc);
710*22ce4affSfengbojiang void	crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
711*22ce4affSfengbojiang 	    void *vdst);
712*22ce4affSfengbojiang void	crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
713*22ce4affSfengbojiang 	    void *vdst);
714a9643ea8Slogwang 
715*22ce4affSfengbojiang static __inline void
crypto_read_iv(struct cryptop * crp,void * iv)716*22ce4affSfengbojiang crypto_read_iv(struct cryptop *crp, void *iv)
717*22ce4affSfengbojiang {
718*22ce4affSfengbojiang 	const struct crypto_session_params *csp;
719*22ce4affSfengbojiang 
720*22ce4affSfengbojiang 	csp = crypto_get_params(crp->crp_session);
721*22ce4affSfengbojiang 	if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
722*22ce4affSfengbojiang 		memcpy(iv, crp->crp_iv, csp->csp_ivlen);
723*22ce4affSfengbojiang 	else
724*22ce4affSfengbojiang 		crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
725*22ce4affSfengbojiang }
726*22ce4affSfengbojiang 
727a9643ea8Slogwang #endif /* _KERNEL */
728a9643ea8Slogwang #endif /* _CRYPTO_CRYPTO_H_ */
729