xref: /freebsd/sys/opencrypto/cryptodev.h (revision 29fe41ddd714bae92a09fd4098fad614945bedf5)
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 
75 /* Hash values */
76 #define	NULL_HASH_LEN		16
77 #define	MD5_HASH_LEN		16
78 #define	SHA1_HASH_LEN		20
79 #define	RIPEMD160_HASH_LEN	20
80 #define	SHA2_224_HASH_LEN	28
81 #define	SHA2_256_HASH_LEN	32
82 #define	SHA2_384_HASH_LEN	48
83 #define	SHA2_512_HASH_LEN	64
84 #define	MD5_KPDK_HASH_LEN	16
85 #define	SHA1_KPDK_HASH_LEN	20
86 #define	AES_GMAC_HASH_LEN	16
87 #define	POLY1305_HASH_LEN	16
88 #define	AES_CBC_MAC_HASH_LEN	16
89 /* Maximum hash algorithm result length */
90 #define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
91 
92 #define	MD5_BLOCK_LEN		64
93 #define	SHA1_BLOCK_LEN		64
94 #define	RIPEMD160_BLOCK_LEN	64
95 #define	SHA2_224_BLOCK_LEN	64
96 #define	SHA2_256_BLOCK_LEN	64
97 #define	SHA2_384_BLOCK_LEN	128
98 #define	SHA2_512_BLOCK_LEN	128
99 
100 /* HMAC values */
101 #define	NULL_HMAC_BLOCK_LEN		64
102 /* Maximum HMAC block length */
103 #define	HMAC_MAX_BLOCK_LEN	SHA2_512_BLOCK_LEN /* Keep this updated */
104 #define	HMAC_IPAD_VAL			0x36
105 #define	HMAC_OPAD_VAL			0x5C
106 /* HMAC Key Length */
107 #define	AES_128_GMAC_KEY_LEN		16
108 #define	AES_192_GMAC_KEY_LEN		24
109 #define	AES_256_GMAC_KEY_LEN		32
110 #define	AES_128_CBC_MAC_KEY_LEN		16
111 #define	AES_192_CBC_MAC_KEY_LEN		24
112 #define	AES_256_CBC_MAC_KEY_LEN		32
113 
114 #define	POLY1305_KEY_LEN		32
115 
116 /* Encryption algorithm block sizes */
117 #define	NULL_BLOCK_LEN		4	/* IPsec to maintain alignment */
118 #define	DES_BLOCK_LEN		8
119 #define	DES3_BLOCK_LEN		8
120 #define	BLOWFISH_BLOCK_LEN	8
121 #define	SKIPJACK_BLOCK_LEN	8
122 #define	CAST128_BLOCK_LEN	8
123 #define	RIJNDAEL128_BLOCK_LEN	16
124 #define	AES_BLOCK_LEN		16
125 #define	AES_ICM_BLOCK_LEN	1
126 #define	ARC4_BLOCK_LEN		1
127 #define	CAMELLIA_BLOCK_LEN	16
128 #define	CHACHA20_NATIVE_BLOCK_LEN	64
129 #define	EALG_MAX_BLOCK_LEN	CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
130 
131 /* IV Lengths */
132 
133 #define	ARC4_IV_LEN		1
134 #define	AES_GCM_IV_LEN		12
135 #define	AES_CCM_IV_LEN		12
136 #define	AES_XTS_IV_LEN		8
137 #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
138 
139 /* Min and Max Encryption Key Sizes */
140 #define	NULL_MIN_KEY		0
141 #define	NULL_MAX_KEY		256 /* 2048 bits, max key */
142 #define	DES_MIN_KEY		8
143 #define	DES_MAX_KEY		DES_MIN_KEY
144 #define	TRIPLE_DES_MIN_KEY	24
145 #define	TRIPLE_DES_MAX_KEY	TRIPLE_DES_MIN_KEY
146 #define	BLOWFISH_MIN_KEY	5
147 #define	BLOWFISH_MAX_KEY	56 /* 448 bits, max key */
148 #define	CAST_MIN_KEY		5
149 #define	CAST_MAX_KEY		16
150 #define	SKIPJACK_MIN_KEY	10
151 #define	SKIPJACK_MAX_KEY	SKIPJACK_MIN_KEY
152 #define	RIJNDAEL_MIN_KEY	16
153 #define	RIJNDAEL_MAX_KEY	32
154 #define	AES_MIN_KEY		RIJNDAEL_MIN_KEY
155 #define	AES_MAX_KEY		RIJNDAEL_MAX_KEY
156 #define	AES_XTS_MIN_KEY		(2 * AES_MIN_KEY)
157 #define	AES_XTS_MAX_KEY		(2 * AES_MAX_KEY)
158 #define	ARC4_MIN_KEY		1
159 #define	ARC4_MAX_KEY		32
160 #define	CAMELLIA_MIN_KEY	8
161 #define	CAMELLIA_MAX_KEY	32
162 
163 /* Maximum hash algorithm result length */
164 #define	AALG_MAX_RESULT_LEN	64 /* Keep this updated */
165 
166 #define	CRYPTO_ALGORITHM_MIN	1
167 #define	CRYPTO_DES_CBC		1
168 #define	CRYPTO_3DES_CBC		2
169 #define	CRYPTO_BLF_CBC		3
170 #define	CRYPTO_CAST_CBC		4
171 #define	CRYPTO_SKIPJACK_CBC	5
172 #define	CRYPTO_MD5_HMAC		6
173 #define	CRYPTO_SHA1_HMAC	7
174 #define	CRYPTO_RIPEMD160_HMAC	8
175 #define	CRYPTO_MD5_KPDK		9
176 #define	CRYPTO_SHA1_KPDK	10
177 #define	CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
178 #define	CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
179 #define	CRYPTO_ARC4		12
180 #define	CRYPTO_MD5		13
181 #define	CRYPTO_SHA1		14
182 #define	CRYPTO_NULL_HMAC	15
183 #define	CRYPTO_NULL_CBC		16
184 #define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
185 #define	CRYPTO_SHA2_256_HMAC	18
186 #define	CRYPTO_SHA2_384_HMAC	19
187 #define	CRYPTO_SHA2_512_HMAC	20
188 #define	CRYPTO_CAMELLIA_CBC	21
189 #define	CRYPTO_AES_XTS		22
190 #define	CRYPTO_AES_ICM		23 /* commonly known as CTR mode */
191 #define	CRYPTO_AES_NIST_GMAC	24 /* GMAC only */
192 #define	CRYPTO_AES_NIST_GCM_16	25 /* 16 byte ICV */
193 #ifdef _KERNEL
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 #endif
198 #define	CRYPTO_BLAKE2B		29 /* Blake2b hash */
199 #define	CRYPTO_BLAKE2S		30 /* Blake2s hash */
200 #define	CRYPTO_CHACHA20		31 /* Chacha20 stream cipher */
201 #define	CRYPTO_SHA2_224_HMAC	32
202 #define	CRYPTO_RIPEMD160	33
203 #define	CRYPTO_SHA2_224		34
204 #define	CRYPTO_SHA2_256		35
205 #define	CRYPTO_SHA2_384		36
206 #define	CRYPTO_SHA2_512		37
207 #define	CRYPTO_POLY1305		38
208 #define	CRYPTO_AES_CCM_CBC_MAC	39	/* auth side */
209 #define	CRYPTO_AES_CCM_16	40	/* cipher side */
210 #define	CRYPTO_ALGORITHM_MAX	40	/* Keep updated - see below */
211 
212 #define	CRYPTO_ALGO_VALID(x)	((x) >= CRYPTO_ALGORITHM_MIN && \
213 				 (x) <= CRYPTO_ALGORITHM_MAX)
214 
215 /* Algorithm flags */
216 #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
217 #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
218 #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
219 
220 /*
221  * Crypto driver/device flags.  They can set in the crid
222  * parameter when creating a session or submitting a key
223  * op to affect the device/driver assigned.  If neither
224  * of these are specified then the crid is assumed to hold
225  * the driver id of an existing (and suitable) device that
226  * must be used to satisfy the request.
227  */
228 #define CRYPTO_FLAG_HARDWARE	0x01000000	/* hardware accelerated */
229 #define CRYPTO_FLAG_SOFTWARE	0x02000000	/* software implementation */
230 
231 /* NB: deprecated */
232 struct session_op {
233 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
234 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
235 
236 	u_int32_t	keylen;		/* cipher key */
237 	c_caddr_t	key;
238 	int		mackeylen;	/* mac key */
239 	c_caddr_t	mackey;
240 
241   	u_int32_t	ses;		/* returns: session # */
242 };
243 
244 /*
245  * session and crypt _op structs are used by userspace programs to interact
246  * with /dev/crypto.  Confusingly, the internal kernel interface is named
247  * "cryptop" (no underscore).
248  */
249 struct session2_op {
250 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
251 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
252 
253 	u_int32_t	keylen;		/* cipher key */
254 	c_caddr_t	key;
255 	int		mackeylen;	/* mac key */
256 	c_caddr_t	mackey;
257 
258   	u_int32_t	ses;		/* returns: session # */
259 	int		crid;		/* driver id + flags (rw) */
260 	int		pad[4];		/* for future expansion */
261 };
262 
263 struct crypt_op {
264 	u_int32_t	ses;
265 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
266 #define COP_ENCRYPT	1
267 #define COP_DECRYPT	2
268 	u_int16_t	flags;
269 #define	COP_F_CIPHER_FIRST	0x0001	/* Cipher before MAC. */
270 #define	COP_F_BATCH		0x0008	/* Batch op if possible */
271 	u_int		len;
272 	c_caddr_t	src;		/* become iov[] inside kernel */
273 	caddr_t		dst;
274 	caddr_t		mac;		/* must be big enough for chosen MAC */
275 	c_caddr_t	iv;
276 };
277 
278 /* op and flags the same as crypt_op */
279 struct crypt_aead {
280 	u_int32_t	ses;
281 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
282 	u_int16_t	flags;
283 	u_int		len;
284 	u_int		aadlen;
285 	u_int		ivlen;
286 	c_caddr_t	src;		/* become iov[] inside kernel */
287 	caddr_t		dst;
288 	c_caddr_t	aad;		/* additional authenticated data */
289 	caddr_t		tag;		/* must fit for chosen TAG length */
290 	c_caddr_t	iv;
291 };
292 
293 /*
294  * Parameters for looking up a crypto driver/device by
295  * device name or by id.  The latter are returned for
296  * created sessions (crid) and completed key operations.
297  */
298 struct crypt_find_op {
299 	int		crid;		/* driver id + flags */
300 	char		name[32];	/* device/driver name */
301 };
302 
303 /* bignum parameter, in packed bytes, ... */
304 struct crparam {
305 	caddr_t		crp_p;
306 	u_int		crp_nbits;
307 };
308 
309 #define CRK_MAXPARAM	8
310 
311 struct crypt_kop {
312 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
313 	u_int		crk_status;	/* return status */
314 	u_short		crk_iparams;	/* # of input parameters */
315 	u_short		crk_oparams;	/* # of output parameters */
316 	u_int		crk_crid;	/* NB: only used by CIOCKEY2 (rw) */
317 	struct crparam	crk_param[CRK_MAXPARAM];
318 };
319 #define	CRK_ALGORITM_MIN	0
320 #define CRK_MOD_EXP		0
321 #define CRK_MOD_EXP_CRT		1
322 #define CRK_DSA_SIGN		2
323 #define CRK_DSA_VERIFY		3
324 #define CRK_DH_COMPUTE_KEY	4
325 #define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
326 
327 #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
328 #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
329 #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
330 #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
331 #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
332 
333 /*
334  * done against open of /dev/crypto, to get a cloned descriptor.
335  * Please use F_SETFD against the cloned descriptor.
336  */
337 #define	CRIOGET		_IOWR('c', 100, u_int32_t)
338 #define	CRIOASYMFEAT	CIOCASYMFEAT
339 #define	CRIOFINDDEV	CIOCFINDDEV
340 
341 /* the following are done against the cloned descriptor */
342 #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
343 #define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
344 #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
345 #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
346 #define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
347 #define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
348 #define	CIOCKEY2	_IOWR('c', 107, struct crypt_kop)
349 #define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
350 #define	CIOCCRYPTAEAD	_IOWR('c', 109, struct crypt_aead)
351 
352 struct cryptotstat {
353 	struct timespec	acc;		/* total accumulated time */
354 	struct timespec	min;		/* min time */
355 	struct timespec	max;		/* max time */
356 	u_int32_t	count;		/* number of observations */
357 };
358 
359 struct cryptostats {
360 	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
361 	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
362 	u_int32_t	cs_kops;	/* asymetric/key ops submitted */
363 	u_int32_t	cs_kerrs;	/* asymetric/key ops that failed */
364 	u_int32_t	cs_intrs;	/* crypto swi thread activations */
365 	u_int32_t	cs_rets;	/* crypto return thread activations */
366 	u_int32_t	cs_blocks;	/* symmetric op driver block */
367 	u_int32_t	cs_kblocks;	/* symmetric op driver block */
368 	/*
369 	 * When CRYPTO_TIMING is defined at compile time and the
370 	 * sysctl debug.crypto is set to 1, the crypto system will
371 	 * accumulate statistics about how long it takes to process
372 	 * crypto requests at various points during processing.
373 	 */
374 	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
375 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
376 	struct cryptotstat cs_cb;	/* crypto_done -> callback */
377 	struct cryptotstat cs_finis;	/* callback -> callback return */
378 };
379 
380 #ifdef _KERNEL
381 
382 /*
383  * Return values for cryptodev_probesession methods.
384  */
385 #define	CRYPTODEV_PROBE_HARDWARE	(-100)
386 #define	CRYPTODEV_PROBE_ACCEL_SOFTWARE	(-200)
387 #define	CRYPTODEV_PROBE_SOFTWARE	(-500)
388 
389 #if 0
390 #define CRYPTDEB(s, ...) do {						\
391 	printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);	\
392 } while (0)
393 #else
394 #define CRYPTDEB(...)	do { } while (0)
395 #endif
396 
397 struct crypto_session_params {
398 	int		csp_mode;	/* Type of operations to perform. */
399 
400 #define	CSP_MODE_NONE		0
401 #define	CSP_MODE_COMPRESS	1	/* Compression/decompression. */
402 #define	CSP_MODE_CIPHER		2	/* Encrypt/decrypt. */
403 #define	CSP_MODE_DIGEST		3	/* Compute/verify digest. */
404 #define	CSP_MODE_AEAD		4	/* Combined auth/encryption. */
405 #define	CSP_MODE_ETA		5	/* IPsec style encrypt-then-auth */
406 
407 	int		csp_flags;
408 
409 	int		csp_ivlen;	 /* IV length in bytes. */
410 
411 	int		csp_cipher_alg;
412 	int		csp_cipher_klen; /* Key length in bytes. */
413 	const void	*csp_cipher_key;
414 
415 	int		csp_auth_alg;
416 	int		csp_auth_klen;	/* Key length in bytes. */
417 	const void	*csp_auth_key;
418 	int		csp_auth_mlen;	/* Number of digest bytes to use.
419 					   0 means all. */
420 };
421 
422 /* Structure describing complete operation */
423 struct cryptop {
424 	TAILQ_ENTRY(cryptop) crp_next;
425 
426 	struct task	crp_task;
427 
428 	crypto_session_t crp_session;	/* Session */
429 	int		crp_ilen;	/* Input data total length */
430 	int		crp_olen;	/* Result total length */
431 
432 	int		crp_etype;	/*
433 					 * Error type (zero means no error).
434 					 * All error codes except EAGAIN
435 					 * indicate possible data corruption (as in,
436 					 * the data have been touched). On all
437 					 * errors, the crp_session may have changed
438 					 * (reset to a new one), so the caller
439 					 * should always check and use the new
440 					 * value on future requests.
441 					 */
442 	int		crp_flags;
443 
444 #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible */
445 #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
446 #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
447 #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
448 #define	CRYPTO_F_ASYNC		0x0080	/* Dispatch crypto jobs on several threads
449 					 * if op is synchronous
450 					 */
451 #define	CRYPTO_F_ASYNC_KEEPORDER	0x0100	/*
452 					 * Dispatch the crypto jobs in the same
453 					 * order there are submitted. Applied only
454 					 * if CRYPTO_F_ASYNC flags is set
455 					 */
456 #define	CRYPTO_F_IV_SEPARATE	0x0200	/* Use crp_iv[] as IV. */
457 
458 	int		crp_op;
459 
460 	union {
461 		caddr_t		crp_buf;	/* Data to be processed */
462 		struct mbuf	*crp_mbuf;
463 		struct uio	*crp_uio;
464 	};
465 	int		crp_buf_type;	/* Which union member describes data. */
466 
467 	int		crp_aad_start;	/* Location of AAD. */
468 	int		crp_aad_length;	/* 0 => no AAD. */
469 	int		crp_iv_start;	/* Location of IV.  IV length is from
470 					 * the session.
471 					 */
472 	int		crp_payload_start; /* Location of ciphertext. */
473 	int		crp_payload_length;
474 	int		crp_digest_start; /* Location of MAC/tag.  Length is
475 					   * from the session.
476 					   */
477 
478 	uint8_t		crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
479 
480 	const void	*crp_cipher_key; /* New cipher key if non-NULL. */
481 	const void	*crp_auth_key;	/* New auth key if non-NULL. */
482 
483 	void		*crp_opaque;	/* Opaque pointer, passed along */
484 
485 	int (*crp_callback)(struct cryptop *); /* Callback function */
486 
487 	struct bintime	crp_tstamp;	/* performance time stamp */
488 	uint32_t	crp_seq;	/* used for ordered dispatch */
489 	uint32_t	crp_retw_id;	/*
490 					 * the return worker to be used,
491 					 *  used for ordered dispatch
492 					 */
493 };
494 
495 #define	CRYPTOP_ASYNC(crp) \
496 	(((crp)->crp_flags & CRYPTO_F_ASYNC) && \
497 	crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
498 #define	CRYPTOP_ASYNC_KEEPORDER(crp) \
499 	(CRYPTOP_ASYNC(crp) && \
500 	(crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
501 
502 #define	CRYPTO_BUF_CONTIG	0x0
503 #define	CRYPTO_BUF_UIO		0x1
504 #define	CRYPTO_BUF_MBUF		0x2
505 
506 /* Flags in crp_op. */
507 #define	CRYPTO_OP_DECRYPT		0x0
508 #define	CRYPTO_OP_ENCRYPT		0x1
509 #define	CRYPTO_OP_IS_ENCRYPT(op)	((op) & CRYPTO_OP_ENCRYPT)
510 #define	CRYPTO_OP_COMPUTE_DIGEST	0x0
511 #define	CRYPTO_OP_VERIFY_DIGEST		0x2
512 #define	CRYPTO_OP_DECOMPRESS		CRYPTO_OP_DECRYPT
513 #define	CRYPTO_OP_COMPRESS		CRYPTO_OP_ENCRYPT
514 #define	CRYPTO_OP_IS_COMPRESS(op)	((op) & CRYPTO_OP_COMPRESS)
515 
516 /*
517  * Hints passed to process methods.
518  */
519 #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
520 
521 struct cryptkop {
522 	TAILQ_ENTRY(cryptkop) krp_next;
523 
524 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
525 	u_int		krp_status;	/* return status */
526 	u_short		krp_iparams;	/* # of input parameters */
527 	u_short		krp_oparams;	/* # of output parameters */
528 	u_int		krp_crid;	/* desired device, etc. */
529 	uint32_t	krp_hid;	/* device used */
530 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
531 	void		(*krp_callback)(struct cryptkop *);
532 	struct cryptocap *krp_cap;
533 };
534 
535 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
536 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
537 void *crypto_get_driver_session(crypto_session_t crypto_session);
538 const struct crypto_session_params *crypto_get_params(
539     crypto_session_t crypto_session);
540 struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
541 struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
542 
543 MALLOC_DECLARE(M_CRYPTO_DATA);
544 
545 extern	int crypto_newsession(crypto_session_t *cses,
546     const struct crypto_session_params *params, int hard);
547 extern	void crypto_freesession(crypto_session_t cses);
548 #define	CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
549 #define	CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
550 #define	CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
551 extern	int32_t crypto_get_driverid(device_t dev, size_t session_size,
552     int flags);
553 extern	int crypto_find_driver(const char *);
554 extern	device_t crypto_find_device_byhid(int hid);
555 extern	int crypto_getcaps(int hid);
556 extern	int crypto_kregister(u_int32_t, int, u_int32_t);
557 extern	int crypto_unregister_all(u_int32_t driverid);
558 extern	int crypto_dispatch(struct cryptop *crp);
559 extern	int crypto_kdispatch(struct cryptkop *);
560 #define	CRYPTO_SYMQ	0x1
561 #define	CRYPTO_ASYMQ	0x2
562 extern	int crypto_unblock(u_int32_t, int);
563 extern	void crypto_done(struct cryptop *crp);
564 extern	void crypto_kdone(struct cryptkop *);
565 extern	int crypto_getfeat(int *);
566 
567 extern	void crypto_freereq(struct cryptop *crp);
568 extern	struct cryptop *crypto_getreq(crypto_session_t cses, int how);
569 
570 extern	int crypto_usercrypto;		/* userland may do crypto requests */
571 extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
572 extern	int crypto_devallowsoft;	/* only use hardware crypto */
573 
574 /* Helper routines for drivers to initialize auth contexts for HMAC. */
575 struct auth_hash;
576 
577 void	hmac_init_ipad(struct auth_hash *axf, const char *key, int klen,
578     void *auth_ctx);
579 void	hmac_init_opad(struct auth_hash *axf, const char *key, int klen,
580     void *auth_ctx);
581 
582 /*
583  * Crypto-related utility routines used mainly by drivers.
584  *
585  * XXX these don't really belong here; but for now they're
586  *     kept apart from the rest of the system.
587  *
588  * Similar to m_copyback/data, *_copyback copy data from the 'src'
589  * buffer into the crypto request's data buffer while *_copydata copy
590  * data from the crypto request's data buffer into the the 'dst'
591  * buffer.
592  */
593 struct uio;
594 extern	void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
595 extern	void cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp);
596 extern	int cuio_getptr(struct uio *uio, int loc, int *off);
597 extern	int cuio_apply(struct uio *uio, int off, int len,
598 	    int (*f)(void *, void *, u_int), void *arg);
599 
600 struct mbuf;
601 struct iovec;
602 extern	int crypto_mbuftoiov(struct mbuf *mbuf, struct iovec **iovptr,
603 	    int *cnt, int *allocated);
604 
605 void	crypto_copyback(struct cryptop *crp, int off, int size,
606 	    const void *src);
607 void	crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
608 int	crypto_apply(struct cryptop *crp, int off, int len,
609 	    int (*f)(void *, void *, u_int), void *arg);
610 void	*crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
611 	    size_t len);
612 
613 static __inline void
614 crypto_read_iv(struct cryptop *crp, void *iv)
615 {
616 	const struct crypto_session_params *csp;
617 
618 	csp = crypto_get_params(crp->crp_session);
619 	if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
620 		memcpy(iv, crp->crp_iv, csp->csp_ivlen);
621 	else
622 		crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
623 }
624 
625 #endif /* _KERNEL */
626 #endif /* _CRYPTO_CRYPTO_H_ */
627