xref: /freebsd/sys/opencrypto/cryptodev.h (revision 5ab1c5846ff41be24b1f6beb0317bf8258cd4409)
1 /*	$FreeBSD$	*/
2 /*	$OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $	*/
3 
4 /*-
5  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
6  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
7  *
8  * This code was written by Angelos D. Keromytis in Athens, Greece, in
9  * February 2000. Network Security Technologies Inc. (NSTI) kindly
10  * supported the development of this code.
11  *
12  * Copyright (c) 2000 Angelos D. Keromytis
13  *
14  * Permission to use, copy, and modify this software with or without fee
15  * is hereby granted, provided that this entire notice is included in
16  * all source code copies of any software which is or includes a copy or
17  * modification of this software.
18  *
19  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
20  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
21  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
22  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
23  * PURPOSE.
24  *
25  * Copyright (c) 2001 Theo de Raadt
26  * Copyright (c) 2014 The FreeBSD Foundation
27  * All rights reserved.
28  *
29  * Portions of this software were developed by John-Mark Gurney
30  * under sponsorship of the FreeBSD Foundation and
31  * Rubicon Communications, LLC (Netgate).
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *
37  * 1. Redistributions of source code must retain the above copyright
38  *   notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *   notice, this list of conditions and the following disclaimer in the
41  *   documentation and/or other materials provided with the distribution.
42  * 3. The name of the author may not be used to endorse or promote products
43  *   derived from this software without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  * Effort sponsored in part by the Defense Advanced Research Projects
57  * Agency (DARPA) and Air Force Research Laboratory, Air Force
58  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
59  *
60  */
61 
62 #ifndef _CRYPTO_CRYPTO_H_
63 #define _CRYPTO_CRYPTO_H_
64 
65 #include <sys/ioccom.h>
66 
67 #ifdef _KERNEL
68 #include <opencrypto/_cryptodev.h>
69 #include <sys/_task.h>
70 #endif
71 
72 /* Some initial values */
73 #define CRYPTO_DRIVERS_INITIAL	4
74 #define CRYPTO_SW_SESSIONS	32
75 
76 /* Hash values */
77 #define	NULL_HASH_LEN		16
78 #define	MD5_HASH_LEN		16
79 #define	SHA1_HASH_LEN		20
80 #define	RIPEMD160_HASH_LEN	20
81 #define	SHA2_224_HASH_LEN	28
82 #define	SHA2_256_HASH_LEN	32
83 #define	SHA2_384_HASH_LEN	48
84 #define	SHA2_512_HASH_LEN	64
85 #define	MD5_KPDK_HASH_LEN	16
86 #define	SHA1_KPDK_HASH_LEN	20
87 #define	AES_GMAC_HASH_LEN	16
88 #define	POLY1305_HASH_LEN	16
89 #define	AES_CBC_MAC_HASH_LEN	16
90 /* Maximum hash algorithm result length */
91 #define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
92 
93 #define	MD5_BLOCK_LEN		64
94 #define	SHA1_BLOCK_LEN		64
95 #define	RIPEMD160_BLOCK_LEN	64
96 #define	SHA2_224_BLOCK_LEN	64
97 #define	SHA2_256_BLOCK_LEN	64
98 #define	SHA2_384_BLOCK_LEN	128
99 #define	SHA2_512_BLOCK_LEN	128
100 
101 /* HMAC values */
102 #define	NULL_HMAC_BLOCK_LEN		64
103 /* Maximum HMAC block length */
104 #define	HMAC_MAX_BLOCK_LEN	SHA2_512_BLOCK_LEN /* Keep this updated */
105 #define	HMAC_IPAD_VAL			0x36
106 #define	HMAC_OPAD_VAL			0x5C
107 /* HMAC Key Length */
108 #define	AES_128_GMAC_KEY_LEN		16
109 #define	AES_192_GMAC_KEY_LEN		24
110 #define	AES_256_GMAC_KEY_LEN		32
111 #define	AES_128_CBC_MAC_KEY_LEN		16
112 #define	AES_192_CBC_MAC_KEY_LEN		24
113 #define	AES_256_CBC_MAC_KEY_LEN		32
114 
115 #define	POLY1305_KEY_LEN		32
116 
117 /* Encryption algorithm block sizes */
118 #define	NULL_BLOCK_LEN		4	/* IPsec to maintain alignment */
119 #define	DES_BLOCK_LEN		8
120 #define	DES3_BLOCK_LEN		8
121 #define	BLOWFISH_BLOCK_LEN	8
122 #define	SKIPJACK_BLOCK_LEN	8
123 #define	CAST128_BLOCK_LEN	8
124 #define	RIJNDAEL128_BLOCK_LEN	16
125 #define	AES_BLOCK_LEN		16
126 #define	AES_ICM_BLOCK_LEN	1
127 #define	ARC4_BLOCK_LEN		1
128 #define	CAMELLIA_BLOCK_LEN	16
129 #define	CHACHA20_NATIVE_BLOCK_LEN	64
130 #define	EALG_MAX_BLOCK_LEN	CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
131 
132 /* IV Lengths */
133 
134 #define	ARC4_IV_LEN		1
135 #define	AES_GCM_IV_LEN		12
136 #define	AES_CCM_IV_LEN		12
137 #define	AES_XTS_IV_LEN		8
138 #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
139 
140 /* Min and Max Encryption Key Sizes */
141 #define	NULL_MIN_KEY		0
142 #define	NULL_MAX_KEY		256 /* 2048 bits, max key */
143 #define	DES_MIN_KEY		8
144 #define	DES_MAX_KEY		DES_MIN_KEY
145 #define	TRIPLE_DES_MIN_KEY	24
146 #define	TRIPLE_DES_MAX_KEY	TRIPLE_DES_MIN_KEY
147 #define	BLOWFISH_MIN_KEY	5
148 #define	BLOWFISH_MAX_KEY	56 /* 448 bits, max key */
149 #define	CAST_MIN_KEY		5
150 #define	CAST_MAX_KEY		16
151 #define	SKIPJACK_MIN_KEY	10
152 #define	SKIPJACK_MAX_KEY	SKIPJACK_MIN_KEY
153 #define	RIJNDAEL_MIN_KEY	16
154 #define	RIJNDAEL_MAX_KEY	32
155 #define	AES_MIN_KEY		RIJNDAEL_MIN_KEY
156 #define	AES_MAX_KEY		RIJNDAEL_MAX_KEY
157 #define	AES_XTS_MIN_KEY		(2 * AES_MIN_KEY)
158 #define	AES_XTS_MAX_KEY		(2 * AES_MAX_KEY)
159 #define	ARC4_MIN_KEY		1
160 #define	ARC4_MAX_KEY		32
161 #define	CAMELLIA_MIN_KEY	8
162 #define	CAMELLIA_MAX_KEY	32
163 
164 /* Maximum hash algorithm result length */
165 #define	AALG_MAX_RESULT_LEN	64 /* Keep this updated */
166 
167 #define	CRYPTO_ALGORITHM_MIN	1
168 #define	CRYPTO_DES_CBC		1
169 #define	CRYPTO_3DES_CBC		2
170 #define	CRYPTO_BLF_CBC		3
171 #define	CRYPTO_CAST_CBC		4
172 #define	CRYPTO_SKIPJACK_CBC	5
173 #define	CRYPTO_MD5_HMAC		6
174 #define	CRYPTO_SHA1_HMAC	7
175 #define	CRYPTO_RIPEMD160_HMAC	8
176 #define	CRYPTO_MD5_KPDK		9
177 #define	CRYPTO_SHA1_KPDK	10
178 #define	CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
179 #define	CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
180 #define	CRYPTO_ARC4		12
181 #define	CRYPTO_MD5		13
182 #define	CRYPTO_SHA1		14
183 #define	CRYPTO_NULL_HMAC	15
184 #define	CRYPTO_NULL_CBC		16
185 #define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
186 #define	CRYPTO_SHA2_256_HMAC	18
187 #define	CRYPTO_SHA2_384_HMAC	19
188 #define	CRYPTO_SHA2_512_HMAC	20
189 #define	CRYPTO_CAMELLIA_CBC	21
190 #define	CRYPTO_AES_XTS		22
191 #define	CRYPTO_AES_ICM		23 /* commonly known as CTR mode */
192 #define	CRYPTO_AES_NIST_GMAC	24 /* cipher side */
193 #define	CRYPTO_AES_NIST_GCM_16	25 /* 16 byte ICV */
194 #define	CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
195 #define	CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
196 #define	CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
197 #define	CRYPTO_BLAKE2B		29 /* Blake2b hash */
198 #define	CRYPTO_BLAKE2S		30 /* Blake2s hash */
199 #define	CRYPTO_CHACHA20		31 /* Chacha20 stream cipher */
200 #define	CRYPTO_SHA2_224_HMAC	32
201 #define	CRYPTO_RIPEMD160	33
202 #define	CRYPTO_SHA2_224		34
203 #define	CRYPTO_SHA2_256		35
204 #define	CRYPTO_SHA2_384		36
205 #define	CRYPTO_SHA2_512		37
206 #define	CRYPTO_POLY1305		38
207 #define	CRYPTO_AES_CCM_CBC_MAC	39	/* auth side */
208 #define	CRYPTO_AES_CCM_16	40	/* cipher side */
209 #define	CRYPTO_ALGORITHM_MAX	40	/* Keep updated - see below */
210 
211 #define	CRYPTO_ALGO_VALID(x)	((x) >= CRYPTO_ALGORITHM_MIN && \
212 				 (x) <= CRYPTO_ALGORITHM_MAX)
213 
214 /* Algorithm flags */
215 #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
216 #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
217 #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
218 
219 /*
220  * Crypto driver/device flags.  They can set in the crid
221  * parameter when creating a session or submitting a key
222  * op to affect the device/driver assigned.  If neither
223  * of these are specified then the crid is assumed to hold
224  * the driver id of an existing (and suitable) device that
225  * must be used to satisfy the request.
226  */
227 #define CRYPTO_FLAG_HARDWARE	0x01000000	/* hardware accelerated */
228 #define CRYPTO_FLAG_SOFTWARE	0x02000000	/* software implementation */
229 
230 /* NB: deprecated */
231 struct session_op {
232 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
233 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
234 
235 	u_int32_t	keylen;		/* cipher key */
236 	c_caddr_t	key;
237 	int		mackeylen;	/* mac key */
238 	c_caddr_t	mackey;
239 
240   	u_int32_t	ses;		/* returns: session # */
241 };
242 
243 /*
244  * session and crypt _op structs are used by userspace programs to interact
245  * with /dev/crypto.  Confusingly, the internal kernel interface is named
246  * "cryptop" (no underscore).
247  */
248 struct session2_op {
249 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
250 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
251 
252 	u_int32_t	keylen;		/* cipher key */
253 	c_caddr_t	key;
254 	int		mackeylen;	/* mac key */
255 	c_caddr_t	mackey;
256 
257   	u_int32_t	ses;		/* returns: session # */
258 	int		crid;		/* driver id + flags (rw) */
259 	int		pad[4];		/* for future expansion */
260 };
261 
262 struct crypt_op {
263 	u_int32_t	ses;
264 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
265 #define COP_ENCRYPT	1
266 #define COP_DECRYPT	2
267 	u_int16_t	flags;
268 #define	COP_F_CIPHER_FIRST	0x0001	/* Cipher before MAC. */
269 #define	COP_F_BATCH		0x0008	/* Batch op if possible */
270 	u_int		len;
271 	c_caddr_t	src;		/* become iov[] inside kernel */
272 	caddr_t		dst;
273 	caddr_t		mac;		/* must be big enough for chosen MAC */
274 	c_caddr_t	iv;
275 };
276 
277 /* op and flags the same as crypt_op */
278 struct crypt_aead {
279 	u_int32_t	ses;
280 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
281 	u_int16_t	flags;
282 	u_int		len;
283 	u_int		aadlen;
284 	u_int		ivlen;
285 	c_caddr_t	src;		/* become iov[] inside kernel */
286 	caddr_t		dst;
287 	c_caddr_t	aad;		/* additional authenticated data */
288 	caddr_t		tag;		/* must fit for chosen TAG length */
289 	c_caddr_t	iv;
290 };
291 
292 /*
293  * Parameters for looking up a crypto driver/device by
294  * device name or by id.  The latter are returned for
295  * created sessions (crid) and completed key operations.
296  */
297 struct crypt_find_op {
298 	int		crid;		/* driver id + flags */
299 	char		name[32];	/* device/driver name */
300 };
301 
302 /* bignum parameter, in packed bytes, ... */
303 struct crparam {
304 	caddr_t		crp_p;
305 	u_int		crp_nbits;
306 };
307 
308 #define CRK_MAXPARAM	8
309 
310 struct crypt_kop {
311 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
312 	u_int		crk_status;	/* return status */
313 	u_short		crk_iparams;	/* # of input parameters */
314 	u_short		crk_oparams;	/* # of output parameters */
315 	u_int		crk_crid;	/* NB: only used by CIOCKEY2 (rw) */
316 	struct crparam	crk_param[CRK_MAXPARAM];
317 };
318 #define	CRK_ALGORITM_MIN	0
319 #define CRK_MOD_EXP		0
320 #define CRK_MOD_EXP_CRT		1
321 #define CRK_DSA_SIGN		2
322 #define CRK_DSA_VERIFY		3
323 #define CRK_DH_COMPUTE_KEY	4
324 #define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
325 
326 #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
327 #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
328 #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
329 #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
330 #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
331 
332 /*
333  * done against open of /dev/crypto, to get a cloned descriptor.
334  * Please use F_SETFD against the cloned descriptor.
335  */
336 #define	CRIOGET		_IOWR('c', 100, u_int32_t)
337 #define	CRIOASYMFEAT	CIOCASYMFEAT
338 #define	CRIOFINDDEV	CIOCFINDDEV
339 
340 /* the following are done against the cloned descriptor */
341 #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
342 #define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
343 #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
344 #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
345 #define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
346 #define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
347 #define	CIOCKEY2	_IOWR('c', 107, struct crypt_kop)
348 #define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
349 #define	CIOCCRYPTAEAD	_IOWR('c', 109, struct crypt_aead)
350 
351 struct cryptotstat {
352 	struct timespec	acc;		/* total accumulated time */
353 	struct timespec	min;		/* min time */
354 	struct timespec	max;		/* max time */
355 	u_int32_t	count;		/* number of observations */
356 };
357 
358 struct cryptostats {
359 	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
360 	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
361 	u_int32_t	cs_kops;	/* asymetric/key ops submitted */
362 	u_int32_t	cs_kerrs;	/* asymetric/key ops that failed */
363 	u_int32_t	cs_intrs;	/* crypto swi thread activations */
364 	u_int32_t	cs_rets;	/* crypto return thread activations */
365 	u_int32_t	cs_blocks;	/* symmetric op driver block */
366 	u_int32_t	cs_kblocks;	/* symmetric op driver block */
367 	/*
368 	 * When CRYPTO_TIMING is defined at compile time and the
369 	 * sysctl debug.crypto is set to 1, the crypto system will
370 	 * accumulate statistics about how long it takes to process
371 	 * crypto requests at various points during processing.
372 	 */
373 	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
374 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
375 	struct cryptotstat cs_cb;	/* crypto_done -> callback */
376 	struct cryptotstat cs_finis;	/* callback -> callback return */
377 };
378 
379 #ifdef _KERNEL
380 
381 #if 0
382 #define CRYPTDEB(s, ...) do {						\
383 	printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);	\
384 } while (0)
385 #else
386 #define CRYPTDEB(...)	do { } while (0)
387 #endif
388 
389 /* Standard initialization structure beginning */
390 struct cryptoini {
391 	int		cri_alg;	/* Algorithm to use */
392 	int		cri_klen;	/* Key length, in bits */
393 	int		cri_mlen;	/* Number of bytes we want from the
394 					   entire hash. 0 means all. */
395 	caddr_t		cri_key;	/* key to use */
396 	u_int8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */
397 	struct cryptoini *cri_next;
398 };
399 
400 /* Describe boundaries of a single crypto operation */
401 struct cryptodesc {
402 	int		crd_skip;	/* How many bytes to ignore from start */
403 	int		crd_len;	/* How many bytes to process */
404 	int		crd_inject;	/* Where to inject results, if applicable */
405 	int		crd_flags;
406 
407 #define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */
408 #define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in
409 					   place, so don't copy. */
410 #define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */
411 #define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */
412 #define	CRD_F_COMP		0x0f    /* Set when doing compression */
413 #define	CRD_F_KEY_EXPLICIT	0x10	/* Key explicitly provided */
414 
415 	struct cryptoini	CRD_INI; /* Initialization/context data */
416 #define	crd_esn		CRD_INI.cri_esn
417 #define	crd_iv		CRD_INI.cri_iv
418 #define	crd_key		CRD_INI.cri_key
419 #define	crd_alg		CRD_INI.cri_alg
420 #define	crd_klen	CRD_INI.cri_klen
421 
422 	struct cryptodesc *crd_next;
423 };
424 
425 /* Structure describing complete operation */
426 struct cryptop {
427 	TAILQ_ENTRY(cryptop) crp_next;
428 
429 	struct task	crp_task;
430 
431 	crypto_session_t crp_session;	/* Session */
432 	int		crp_ilen;	/* Input data total length */
433 	int		crp_olen;	/* Result total length */
434 
435 	int		crp_etype;	/*
436 					 * Error type (zero means no error).
437 					 * All error codes except EAGAIN
438 					 * indicate possible data corruption (as in,
439 					 * the data have been touched). On all
440 					 * errors, the crp_session may have changed
441 					 * (reset to a new one), so the caller
442 					 * should always check and use the new
443 					 * value on future requests.
444 					 */
445 	int		crp_flags;
446 
447 #define	CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */
448 #define	CRYPTO_F_IOV		0x0002	/* Input/output are uio */
449 #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible */
450 #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
451 #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
452 #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
453 #define	CRYPTO_F_ASYNC		0x0080	/* Dispatch crypto jobs on several threads
454 					 * if op is synchronous
455 					 */
456 #define	CRYPTO_F_ASYNC_KEEPORDER	0x0100	/*
457 					 * Dispatch the crypto jobs in the same
458 					 * order there are submitted. Applied only
459 					 * if CRYPTO_F_ASYNC flags is set
460 					 */
461 
462 	union {
463 		caddr_t		crp_buf;	/* Data to be processed */
464 		struct mbuf	*crp_mbuf;
465 		struct uio	*crp_uio;
466 	};
467 	void *		crp_opaque;	/* Opaque pointer, passed along */
468 	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */
469 
470 	int (*crp_callback)(struct cryptop *); /* Callback function */
471 
472 	struct bintime	crp_tstamp;	/* performance time stamp */
473 	uint32_t	crp_seq;	/* used for ordered dispatch */
474 	uint32_t	crp_retw_id;	/*
475 					 * the return worker to be used,
476 					 *  used for ordered dispatch
477 					 */
478 };
479 
480 #define	CRYPTOP_ASYNC(crp) \
481 	(((crp)->crp_flags & CRYPTO_F_ASYNC) && \
482 	crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
483 #define	CRYPTOP_ASYNC_KEEPORDER(crp) \
484 	(CRYPTOP_ASYNC(crp) && \
485 	(crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
486 
487 #define	CRYPTO_BUF_CONTIG	0x0
488 #define	CRYPTO_BUF_IOV		0x1
489 #define	CRYPTO_BUF_MBUF		0x2
490 
491 #define	CRYPTO_OP_DECRYPT	0x0
492 #define	CRYPTO_OP_ENCRYPT	0x1
493 
494 /*
495  * Hints passed to process methods.
496  */
497 #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
498 
499 struct cryptkop {
500 	TAILQ_ENTRY(cryptkop) krp_next;
501 
502 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
503 	u_int		krp_status;	/* return status */
504 	u_short		krp_iparams;	/* # of input parameters */
505 	u_short		krp_oparams;	/* # of output parameters */
506 	u_int		krp_crid;	/* desired device, etc. */
507 	u_int32_t	krp_hid;
508 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
509 	int		(*krp_callback)(struct cryptkop *);
510 };
511 
512 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
513 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
514 void *crypto_get_driver_session(crypto_session_t crypto_session);
515 
516 MALLOC_DECLARE(M_CRYPTO_DATA);
517 
518 extern	int crypto_newsession(crypto_session_t *cses, struct cryptoini *cri, int hard);
519 extern	void crypto_freesession(crypto_session_t cses);
520 #define	CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
521 #define	CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
522 #define	CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
523 extern	int32_t crypto_get_driverid(device_t dev, size_t session_size,
524     int flags);
525 extern	int crypto_find_driver(const char *);
526 extern	device_t crypto_find_device_byhid(int hid);
527 extern	int crypto_getcaps(int hid);
528 extern	int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
529 	    u_int32_t flags);
530 extern	int crypto_kregister(u_int32_t, int, u_int32_t);
531 extern	int crypto_unregister(u_int32_t driverid, int alg);
532 extern	int crypto_unregister_all(u_int32_t driverid);
533 extern	int crypto_dispatch(struct cryptop *crp);
534 extern	int crypto_kdispatch(struct cryptkop *);
535 #define	CRYPTO_SYMQ	0x1
536 #define	CRYPTO_ASYMQ	0x2
537 extern	int crypto_unblock(u_int32_t, int);
538 extern	void crypto_done(struct cryptop *crp);
539 extern	void crypto_kdone(struct cryptkop *);
540 extern	int crypto_getfeat(int *);
541 
542 extern	void crypto_freereq(struct cryptop *crp);
543 extern	struct cryptop *crypto_getreq(int num);
544 
545 extern	int crypto_usercrypto;		/* userland may do crypto requests */
546 extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
547 extern	int crypto_devallowsoft;	/* only use hardware crypto */
548 
549 /*
550  * Crypto-related utility routines used mainly by drivers.
551  *
552  * XXX these don't really belong here; but for now they're
553  *     kept apart from the rest of the system.
554  */
555 struct uio;
556 extern	void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
557 extern	void cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp);
558 extern	int cuio_getptr(struct uio *uio, int loc, int *off);
559 extern	int cuio_apply(struct uio *uio, int off, int len,
560 	    int (*f)(void *, void *, u_int), void *arg);
561 
562 struct mbuf;
563 struct iovec;
564 extern	int crypto_mbuftoiov(struct mbuf *mbuf, struct iovec **iovptr,
565 	    int *cnt, int *allocated);
566 
567 extern	void crypto_copyback(int flags, caddr_t buf, int off, int size,
568 	    c_caddr_t in);
569 extern	void crypto_copydata(int flags, caddr_t buf, int off, int size,
570 	    caddr_t out);
571 extern	int crypto_apply(int flags, caddr_t buf, int off, int len,
572 	    int (*f)(void *, void *, u_int), void *arg);
573 
574 extern void *crypto_contiguous_subsegment(int, void *, size_t, size_t);
575 
576 #endif /* _KERNEL */
577 #endif /* _CRYPTO_CRYPTO_H_ */
578