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