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