xref: /freebsd/sys/opencrypto/cryptodev.h (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
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 #include <sys/_task.h>
67 
68 /* Some initial values */
69 #define CRYPTO_DRIVERS_INITIAL	4
70 #define CRYPTO_SW_SESSIONS	32
71 
72 /* Hash values */
73 #define	NULL_HASH_LEN		16
74 #define	MD5_HASH_LEN		16
75 #define	SHA1_HASH_LEN		20
76 #define	RIPEMD160_HASH_LEN	20
77 #define	SHA2_256_HASH_LEN	32
78 #define	SHA2_384_HASH_LEN	48
79 #define	SHA2_512_HASH_LEN	64
80 #define	MD5_KPDK_HASH_LEN	16
81 #define	SHA1_KPDK_HASH_LEN	20
82 #define	AES_GMAC_HASH_LEN	16
83 /* Maximum hash algorithm result length */
84 #define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
85 
86 /* HMAC values */
87 #define	NULL_HMAC_BLOCK_LEN		64
88 #define	MD5_HMAC_BLOCK_LEN		64
89 #define	SHA1_HMAC_BLOCK_LEN		64
90 #define	RIPEMD160_HMAC_BLOCK_LEN	64
91 #define	SHA2_256_HMAC_BLOCK_LEN	64
92 #define	SHA2_384_HMAC_BLOCK_LEN	128
93 #define	SHA2_512_HMAC_BLOCK_LEN	128
94 /* Maximum HMAC block length */
95 #define	HMAC_MAX_BLOCK_LEN	SHA2_512_HMAC_BLOCK_LEN /* Keep this updated */
96 #define	HMAC_IPAD_VAL			0x36
97 #define	HMAC_OPAD_VAL			0x5C
98 /* HMAC Key Length */
99 #define	AES_128_GMAC_KEY_LEN		16
100 #define	AES_192_GMAC_KEY_LEN		24
101 #define	AES_256_GMAC_KEY_LEN		32
102 
103 /* Encryption algorithm block sizes */
104 #define	NULL_BLOCK_LEN		4	/* IPsec to maintain alignment */
105 #define	DES_BLOCK_LEN		8
106 #define	DES3_BLOCK_LEN		8
107 #define	BLOWFISH_BLOCK_LEN	8
108 #define	SKIPJACK_BLOCK_LEN	8
109 #define	CAST128_BLOCK_LEN	8
110 #define	RIJNDAEL128_BLOCK_LEN	16
111 #define	AES_BLOCK_LEN		16
112 #define	AES_ICM_BLOCK_LEN	1
113 #define	ARC4_BLOCK_LEN		1
114 #define	CAMELLIA_BLOCK_LEN	16
115 #define	EALG_MAX_BLOCK_LEN	AES_BLOCK_LEN /* Keep this updated */
116 
117 /* IV Lengths */
118 
119 #define	ARC4_IV_LEN		1
120 #define	AES_GCM_IV_LEN		12
121 #define	AES_XTS_IV_LEN		8
122 #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
123 
124 /* Min and Max Encryption Key Sizes */
125 #define	NULL_MIN_KEY		0
126 #define	NULL_MAX_KEY		256 /* 2048 bits, max key */
127 #define	DES_MIN_KEY		8
128 #define	DES_MAX_KEY		DES_MIN_KEY
129 #define	TRIPLE_DES_MIN_KEY	24
130 #define	TRIPLE_DES_MAX_KEY	TRIPLE_DES_MIN_KEY
131 #define	BLOWFISH_MIN_KEY	5
132 #define	BLOWFISH_MAX_KEY	56 /* 448 bits, max key */
133 #define	CAST_MIN_KEY		5
134 #define	CAST_MAX_KEY		16
135 #define	SKIPJACK_MIN_KEY	10
136 #define	SKIPJACK_MAX_KEY	SKIPJACK_MIN_KEY
137 #define	RIJNDAEL_MIN_KEY	16
138 #define	RIJNDAEL_MAX_KEY	32
139 #define	AES_MIN_KEY		RIJNDAEL_MIN_KEY
140 #define	AES_MAX_KEY		RIJNDAEL_MAX_KEY
141 #define	AES_XTS_MIN_KEY		(2 * AES_MIN_KEY)
142 #define	AES_XTS_MAX_KEY		(2 * AES_MAX_KEY)
143 #define	ARC4_MIN_KEY		1
144 #define	ARC4_MAX_KEY		32
145 #define	CAMELLIA_MIN_KEY	8
146 #define	CAMELLIA_MAX_KEY	32
147 
148 /* Maximum hash algorithm result length */
149 #define	AALG_MAX_RESULT_LEN	64 /* Keep this updated */
150 
151 #define	CRYPTO_ALGORITHM_MIN	1
152 #define	CRYPTO_DES_CBC		1
153 #define	CRYPTO_3DES_CBC		2
154 #define	CRYPTO_BLF_CBC		3
155 #define	CRYPTO_CAST_CBC		4
156 #define	CRYPTO_SKIPJACK_CBC	5
157 #define	CRYPTO_MD5_HMAC		6
158 #define	CRYPTO_SHA1_HMAC	7
159 #define	CRYPTO_RIPEMD160_HMAC	8
160 #define	CRYPTO_MD5_KPDK		9
161 #define	CRYPTO_SHA1_KPDK	10
162 #define	CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
163 #define	CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
164 #define	CRYPTO_ARC4		12
165 #define	CRYPTO_MD5		13
166 #define	CRYPTO_SHA1		14
167 #define	CRYPTO_NULL_HMAC	15
168 #define	CRYPTO_NULL_CBC		16
169 #define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
170 #define	CRYPTO_SHA2_256_HMAC	18
171 #define	CRYPTO_SHA2_384_HMAC	19
172 #define	CRYPTO_SHA2_512_HMAC	20
173 #define	CRYPTO_CAMELLIA_CBC	21
174 #define	CRYPTO_AES_XTS		22
175 #define	CRYPTO_AES_ICM		23 /* commonly known as CTR mode */
176 #define	CRYPTO_AES_NIST_GMAC	24 /* cipher side */
177 #define	CRYPTO_AES_NIST_GCM_16	25 /* 16 byte ICV */
178 #define	CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
179 #define	CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
180 #define	CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
181 #define	CRYPTO_ALGORITHM_MAX	28 /* Keep updated - see below */
182 
183 #define	CRYPTO_ALGO_VALID(x)	((x) >= CRYPTO_ALGORITHM_MIN && \
184 				 (x) <= CRYPTO_ALGORITHM_MAX)
185 
186 /* Algorithm flags */
187 #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
188 #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
189 #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
190 
191 /*
192  * Crypto driver/device flags.  They can set in the crid
193  * parameter when creating a session or submitting a key
194  * op to affect the device/driver assigned.  If neither
195  * of these are specified then the crid is assumed to hold
196  * the driver id of an existing (and suitable) device that
197  * must be used to satisfy the request.
198  */
199 #define CRYPTO_FLAG_HARDWARE	0x01000000	/* hardware accelerated */
200 #define CRYPTO_FLAG_SOFTWARE	0x02000000	/* software implementation */
201 
202 /* NB: deprecated */
203 struct session_op {
204 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
205 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
206 
207 	u_int32_t	keylen;		/* cipher key */
208 	c_caddr_t	key;
209 	int		mackeylen;	/* mac key */
210 	c_caddr_t	mackey;
211 
212   	u_int32_t	ses;		/* returns: session # */
213 };
214 
215 struct session2_op {
216 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
217 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
218 
219 	u_int32_t	keylen;		/* cipher key */
220 	c_caddr_t	key;
221 	int		mackeylen;	/* mac key */
222 	c_caddr_t	mackey;
223 
224   	u_int32_t	ses;		/* returns: session # */
225 	int		crid;		/* driver id + flags (rw) */
226 	int		pad[4];		/* for future expansion */
227 };
228 
229 struct crypt_op {
230 	u_int32_t	ses;
231 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
232 #define COP_ENCRYPT	1
233 #define COP_DECRYPT	2
234 	u_int16_t	flags;
235 #define	COP_F_CIPHER_FIRST	0x0001	/* Cipher before MAC. */
236 #define	COP_F_BATCH		0x0008	/* Batch op if possible */
237 	u_int		len;
238 	c_caddr_t	src;		/* become iov[] inside kernel */
239 	caddr_t		dst;
240 	caddr_t		mac;		/* must be big enough for chosen MAC */
241 	c_caddr_t	iv;
242 };
243 
244 /* op and flags the same as crypt_op */
245 struct crypt_aead {
246 	u_int32_t	ses;
247 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
248 	u_int16_t	flags;
249 	u_int		len;
250 	u_int		aadlen;
251 	u_int		ivlen;
252 	c_caddr_t	src;		/* become iov[] inside kernel */
253 	caddr_t		dst;
254 	c_caddr_t	aad;		/* additional authenticated data */
255 	caddr_t		tag;		/* must fit for chosen TAG length */
256 	c_caddr_t	iv;
257 };
258 
259 /*
260  * Parameters for looking up a crypto driver/device by
261  * device name or by id.  The latter are returned for
262  * created sessions (crid) and completed key operations.
263  */
264 struct crypt_find_op {
265 	int		crid;		/* driver id + flags */
266 	char		name[32];	/* device/driver name */
267 };
268 
269 /* bignum parameter, in packed bytes, ... */
270 struct crparam {
271 	caddr_t		crp_p;
272 	u_int		crp_nbits;
273 };
274 
275 #define CRK_MAXPARAM	8
276 
277 struct crypt_kop {
278 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
279 	u_int		crk_status;	/* return status */
280 	u_short		crk_iparams;	/* # of input parameters */
281 	u_short		crk_oparams;	/* # of output parameters */
282 	u_int		crk_crid;	/* NB: only used by CIOCKEY2 (rw) */
283 	struct crparam	crk_param[CRK_MAXPARAM];
284 };
285 #define	CRK_ALGORITM_MIN	0
286 #define CRK_MOD_EXP		0
287 #define CRK_MOD_EXP_CRT		1
288 #define CRK_DSA_SIGN		2
289 #define CRK_DSA_VERIFY		3
290 #define CRK_DH_COMPUTE_KEY	4
291 #define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
292 
293 #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
294 #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
295 #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
296 #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
297 #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
298 
299 /*
300  * done against open of /dev/crypto, to get a cloned descriptor.
301  * Please use F_SETFD against the cloned descriptor.
302  */
303 #define	CRIOGET		_IOWR('c', 100, u_int32_t)
304 #define	CRIOASYMFEAT	CIOCASYMFEAT
305 #define	CRIOFINDDEV	CIOCFINDDEV
306 
307 /* the following are done against the cloned descriptor */
308 #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
309 #define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
310 #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
311 #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
312 #define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
313 #define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
314 #define	CIOCKEY2	_IOWR('c', 107, struct crypt_kop)
315 #define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
316 #define	CIOCCRYPTAEAD	_IOWR('c', 109, struct crypt_aead)
317 
318 struct cryptotstat {
319 	struct timespec	acc;		/* total accumulated time */
320 	struct timespec	min;		/* min time */
321 	struct timespec	max;		/* max time */
322 	u_int32_t	count;		/* number of observations */
323 };
324 
325 struct cryptostats {
326 	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
327 	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
328 	u_int32_t	cs_kops;	/* asymetric/key ops submitted */
329 	u_int32_t	cs_kerrs;	/* asymetric/key ops that failed */
330 	u_int32_t	cs_intrs;	/* crypto swi thread activations */
331 	u_int32_t	cs_rets;	/* crypto return thread activations */
332 	u_int32_t	cs_blocks;	/* symmetric op driver block */
333 	u_int32_t	cs_kblocks;	/* symmetric op driver block */
334 	/*
335 	 * When CRYPTO_TIMING is defined at compile time and the
336 	 * sysctl debug.crypto is set to 1, the crypto system will
337 	 * accumulate statistics about how long it takes to process
338 	 * crypto requests at various points during processing.
339 	 */
340 	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
341 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
342 	struct cryptotstat cs_cb;	/* crypto_done -> callback */
343 	struct cryptotstat cs_finis;	/* callback -> callback return */
344 };
345 
346 #ifdef _KERNEL
347 
348 #if 0
349 #define CRYPTDEB(s)	do { printf("%s:%d: %s\n", __FILE__, __LINE__, s); \
350 			} while (0)
351 #else
352 #define CRYPTDEB(s)	do { } while (0)
353 #endif
354 
355 /* Standard initialization structure beginning */
356 struct cryptoini {
357 	int		cri_alg;	/* Algorithm to use */
358 	int		cri_klen;	/* Key length, in bits */
359 	int		cri_mlen;	/* Number of bytes we want from the
360 					   entire hash. 0 means all. */
361 	caddr_t		cri_key;	/* key to use */
362 	u_int8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */
363 	struct cryptoini *cri_next;
364 };
365 
366 /* Describe boundaries of a single crypto operation */
367 struct cryptodesc {
368 	int		crd_skip;	/* How many bytes to ignore from start */
369 	int		crd_len;	/* How many bytes to process */
370 	int		crd_inject;	/* Where to inject results, if applicable */
371 	int		crd_flags;
372 
373 #define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */
374 #define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in
375 					   place, so don't copy. */
376 #define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */
377 #define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */
378 #define	CRD_F_COMP		0x0f    /* Set when doing compression */
379 #define	CRD_F_KEY_EXPLICIT	0x10	/* Key explicitly provided */
380 
381 	struct cryptoini	CRD_INI; /* Initialization/context data */
382 #define	crd_esn		CRD_INI.cri_esn
383 #define	crd_iv		CRD_INI.cri_iv
384 #define	crd_key		CRD_INI.cri_key
385 #define	crd_alg		CRD_INI.cri_alg
386 #define	crd_klen	CRD_INI.cri_klen
387 
388 	struct cryptodesc *crd_next;
389 };
390 
391 /* Structure describing complete operation */
392 struct cryptop {
393 	TAILQ_ENTRY(cryptop) crp_next;
394 
395 	struct task	crp_task;
396 
397 	u_int64_t	crp_sid;	/* Session ID */
398 	int		crp_ilen;	/* Input data total length */
399 	int		crp_olen;	/* Result total length */
400 
401 	int		crp_etype;	/*
402 					 * Error type (zero means no error).
403 					 * All error codes except EAGAIN
404 					 * indicate possible data corruption (as in,
405 					 * the data have been touched). On all
406 					 * errors, the crp_sid may have changed
407 					 * (reset to a new one), so the caller
408 					 * should always check and use the new
409 					 * value on future requests.
410 					 */
411 	int		crp_flags;
412 
413 #define	CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */
414 #define	CRYPTO_F_IOV		0x0002	/* Input/output are uio */
415 #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible */
416 #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
417 #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
418 #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
419 #define	CRYPTO_F_ASYNC		0x0080	/* Dispatch crypto jobs on several threads
420 					 * if op is synchronous
421 					 */
422 #define	CRYPTO_F_ASYNC_KEEPORDER	0x0100	/*
423 					 * Dispatch the crypto jobs in the same
424 					 * order there are submitted. Applied only
425 					 * if CRYPTO_F_ASYNC flags is set
426 					 */
427 
428 	union {
429 		caddr_t		crp_buf;	/* Data to be processed */
430 		struct mbuf	*crp_mbuf;
431 		struct uio	*crp_uio;
432 	};
433 	void *		crp_opaque;	/* Opaque pointer, passed along */
434 	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */
435 
436 	int (*crp_callback)(struct cryptop *); /* Callback function */
437 
438 	struct bintime	crp_tstamp;	/* performance time stamp */
439 	uint32_t	crp_seq;	/* used for ordered dispatch */
440 	uint32_t	crp_retw_id;	/*
441 					 * the return worker to be used,
442 					 *  used for ordered dispatch
443 					 */
444 };
445 
446 #define	CRYPTOP_ASYNC(crp) \
447 	(((crp)->crp_flags & CRYPTO_F_ASYNC) && \
448 	CRYPTO_SESID2CAPS((crp)->crp_sid) & CRYPTOCAP_F_SYNC)
449 #define	CRYPTOP_ASYNC_KEEPORDER(crp) \
450 	(CRYPTOP_ASYNC(crp) && \
451 	(crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
452 
453 #define	CRYPTO_BUF_CONTIG	0x0
454 #define	CRYPTO_BUF_IOV		0x1
455 #define	CRYPTO_BUF_MBUF		0x2
456 
457 #define	CRYPTO_OP_DECRYPT	0x0
458 #define	CRYPTO_OP_ENCRYPT	0x1
459 
460 /*
461  * Hints passed to process methods.
462  */
463 #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
464 
465 struct cryptkop {
466 	TAILQ_ENTRY(cryptkop) krp_next;
467 
468 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
469 	u_int		krp_status;	/* return status */
470 	u_short		krp_iparams;	/* # of input parameters */
471 	u_short		krp_oparams;	/* # of output parameters */
472 	u_int		krp_crid;	/* desired device, etc. */
473 	u_int32_t	krp_hid;
474 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
475 	int		(*krp_callback)(struct cryptkop *);
476 };
477 
478 /*
479  * Session ids are 64 bits.  The lower 32 bits contain a "local id" which
480  * is a driver-private session identifier.  The upper 32 bits contain a
481  * "hardware id" used by the core crypto code to identify the driver and
482  * a copy of the driver's capabilities that can be used by client code to
483  * optimize operation.
484  */
485 #define	CRYPTO_SESID2HID(_sid)	(((_sid) >> 32) & 0x00ffffff)
486 #define	CRYPTO_SESID2CAPS(_sid)	(((_sid) >> 32) & 0xff000000)
487 #define	CRYPTO_SESID2LID(_sid)	(((u_int32_t) (_sid)) & 0xffffffff)
488 
489 MALLOC_DECLARE(M_CRYPTO_DATA);
490 
491 extern	int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
492 extern	int crypto_freesession(u_int64_t sid);
493 #define	CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
494 #define	CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
495 #define	CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
496 extern	int32_t crypto_get_driverid(device_t dev, int flags);
497 extern	int crypto_find_driver(const char *);
498 extern	device_t crypto_find_device_byhid(int hid);
499 extern	int crypto_getcaps(int hid);
500 extern	int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
501 	    u_int32_t flags);
502 extern	int crypto_kregister(u_int32_t, int, u_int32_t);
503 extern	int crypto_unregister(u_int32_t driverid, int alg);
504 extern	int crypto_unregister_all(u_int32_t driverid);
505 extern	int crypto_dispatch(struct cryptop *crp);
506 extern	int crypto_kdispatch(struct cryptkop *);
507 #define	CRYPTO_SYMQ	0x1
508 #define	CRYPTO_ASYMQ	0x2
509 extern	int crypto_unblock(u_int32_t, int);
510 extern	void crypto_done(struct cryptop *crp);
511 extern	void crypto_kdone(struct cryptkop *);
512 extern	int crypto_getfeat(int *);
513 
514 extern	void crypto_freereq(struct cryptop *crp);
515 extern	struct cryptop *crypto_getreq(int num);
516 
517 extern	int crypto_usercrypto;		/* userland may do crypto requests */
518 extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
519 extern	int crypto_devallowsoft;	/* only use hardware crypto */
520 
521 /*
522  * Crypto-related utility routines used mainly by drivers.
523  *
524  * XXX these don't really belong here; but for now they're
525  *     kept apart from the rest of the system.
526  */
527 struct uio;
528 extern	void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
529 extern	void cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp);
530 extern	int cuio_getptr(struct uio *uio, int loc, int *off);
531 extern	int cuio_apply(struct uio *uio, int off, int len,
532 	    int (*f)(void *, void *, u_int), void *arg);
533 
534 struct mbuf;
535 struct iovec;
536 extern	int crypto_mbuftoiov(struct mbuf *mbuf, struct iovec **iovptr,
537 	    int *cnt, int *allocated);
538 
539 extern	void crypto_copyback(int flags, caddr_t buf, int off, int size,
540 	    c_caddr_t in);
541 extern	void crypto_copydata(int flags, caddr_t buf, int off, int size,
542 	    caddr_t out);
543 extern	int crypto_apply(int flags, caddr_t buf, int off, int len,
544 	    int (*f)(void *, void *, u_int), void *arg);
545 
546 #endif /* _KERNEL */
547 #endif /* _CRYPTO_CRYPTO_H_ */
548