xref: /freebsd-13-stable/sys/opencrypto/cryptodev.h (revision 4fbf14e22d7b83de7080a8e491ba14a5785a0ff4)
1 /*	$OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $	*/
2 
3 /*-
4  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6  *
7  * This code was written by Angelos D. Keromytis in Athens, Greece, in
8  * February 2000. Network Security Technologies Inc. (NSTI) kindly
9  * supported the development of this code.
10  *
11  * Copyright (c) 2000 Angelos D. Keromytis
12  *
13  * Permission to use, copy, and modify this software with or without fee
14  * is hereby granted, provided that this entire notice is included in
15  * all source code copies of any software which is or includes a copy or
16  * modification of this software.
17  *
18  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
20  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
21  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
22  * PURPOSE.
23  *
24  * Copyright (c) 2001 Theo de Raadt
25  * Copyright (c) 2014-2021 The FreeBSD Foundation
26  * All rights reserved.
27  *
28  * Portions of this software were developed by John-Mark Gurney
29  * under sponsorship of the FreeBSD Foundation and
30  * Rubicon Communications, LLC (Netgate).
31  *
32  * Portions of this software were developed by Ararat River
33  * Consulting, LLC under sponsorship of the FreeBSD Foundation.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright
40  *   notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *   notice, this list of conditions and the following disclaimer in the
43  *   documentation and/or other materials provided with the distribution.
44  * 3. The name of the author may not be used to endorse or promote products
45  *   derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  *
58  * Effort sponsored in part by the Defense Advanced Research Projects
59  * Agency (DARPA) and Air Force Research Laboratory, Air Force
60  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
61  *
62  */
63 
64 #ifndef _CRYPTO_CRYPTO_H_
65 #define _CRYPTO_CRYPTO_H_
66 
67 #include <sys/ioccom.h>
68 
69 #ifdef _KERNEL
70 #include <opencrypto/_cryptodev.h>
71 #include <sys/_task.h>
72 #endif
73 
74 /* Some initial values */
75 #define CRYPTO_DRIVERS_INITIAL	4
76 
77 /* Hash values */
78 #define	NULL_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	AES_GMAC_HASH_LEN	16
86 #define	POLY1305_HASH_LEN	16
87 #define	AES_CBC_MAC_HASH_LEN	16
88 /* Maximum hash algorithm result length */
89 #define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
90 
91 #define	SHA1_BLOCK_LEN		64
92 #define	RIPEMD160_BLOCK_LEN	64
93 #define	SHA2_224_BLOCK_LEN	64
94 #define	SHA2_256_BLOCK_LEN	64
95 #define	SHA2_384_BLOCK_LEN	128
96 #define	SHA2_512_BLOCK_LEN	128
97 #define	POLY1305_BLOCK_LEN	16
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 #define	AES_128_CBC_MAC_KEY_LEN		16
110 #define	AES_192_CBC_MAC_KEY_LEN		24
111 #define	AES_256_CBC_MAC_KEY_LEN		32
112 
113 #define	POLY1305_KEY_LEN		32
114 
115 /* Encryption algorithm block sizes */
116 #define	NULL_BLOCK_LEN		4	/* IPsec to maintain alignment */
117 #define	RIJNDAEL128_BLOCK_LEN	16
118 #define	AES_BLOCK_LEN		16
119 #define	AES_ICM_BLOCK_LEN	1
120 #define	CAMELLIA_BLOCK_LEN	16
121 #define	CHACHA20_NATIVE_BLOCK_LEN	64
122 #define	EALG_MAX_BLOCK_LEN	CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
123 
124 /* IV Lengths */
125 
126 #define	AES_GCM_IV_LEN		12
127 #define	AES_CCM_IV_LEN		12
128 #define	AES_XTS_IV_LEN		8
129 #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
130 #define	CHACHA20_POLY1305_IV_LEN	12
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	RIJNDAEL_MIN_KEY	16
136 #define	RIJNDAEL_MAX_KEY	32
137 #define	AES_MIN_KEY		RIJNDAEL_MIN_KEY
138 #define	AES_MAX_KEY		RIJNDAEL_MAX_KEY
139 #define	AES_XTS_MIN_KEY		(2 * AES_MIN_KEY)
140 #define	AES_XTS_MAX_KEY		(2 * AES_MAX_KEY)
141 #define	CAMELLIA_MIN_KEY	16
142 #define	CAMELLIA_MAX_KEY	32
143 #define	CHACHA20_POLY1305_KEY	32
144 
145 /* Maximum hash algorithm result length */
146 #define	AALG_MAX_RESULT_LEN	64 /* Keep this updated */
147 
148 #define	CRYPTO_ALGORITHM_MIN	1
149 #define	CRYPTO_DES_CBC		1
150 #define	CRYPTO_3DES_CBC		2
151 #define	CRYPTO_BLF_CBC		3
152 #define	CRYPTO_CAST_CBC		4
153 #define	CRYPTO_SKIPJACK_CBC	5
154 #define	CRYPTO_MD5_HMAC		6
155 #define	CRYPTO_SHA1_HMAC	7
156 #define	CRYPTO_RIPEMD160_HMAC	8
157 #define	CRYPTO_MD5_KPDK		9
158 #define	CRYPTO_SHA1_KPDK	10
159 #define	CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
160 #define	CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
161 #define	CRYPTO_ARC4		12
162 #define	CRYPTO_MD5		13
163 #define	CRYPTO_SHA1		14
164 #define	CRYPTO_NULL_HMAC	15
165 #define	CRYPTO_NULL_CBC		16
166 #define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
167 #define	CRYPTO_SHA2_256_HMAC	18
168 #define	CRYPTO_SHA2_384_HMAC	19
169 #define	CRYPTO_SHA2_512_HMAC	20
170 #define	CRYPTO_CAMELLIA_CBC	21
171 #define	CRYPTO_AES_XTS		22
172 #define	CRYPTO_AES_ICM		23 /* commonly known as CTR mode */
173 #define	CRYPTO_AES_NIST_GMAC	24 /* GMAC only */
174 #define	CRYPTO_AES_NIST_GCM_16	25 /* 16 byte ICV */
175 #ifdef _KERNEL
176 #define	CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
177 #define	CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
178 #define	CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
179 #endif
180 #define	CRYPTO_BLAKE2B		29 /* Blake2b hash */
181 #define	CRYPTO_BLAKE2S		30 /* Blake2s hash */
182 #define	CRYPTO_CHACHA20		31 /* Chacha20 stream cipher */
183 #define	CRYPTO_SHA2_224_HMAC	32
184 #define	CRYPTO_RIPEMD160	33
185 #define	CRYPTO_SHA2_224		34
186 #define	CRYPTO_SHA2_256		35
187 #define	CRYPTO_SHA2_384		36
188 #define	CRYPTO_SHA2_512		37
189 #define	CRYPTO_POLY1305		38
190 #define	CRYPTO_AES_CCM_CBC_MAC	39	/* auth side */
191 #define	CRYPTO_AES_CCM_16	40	/* cipher side */
192 #define	CRYPTO_CHACHA20_POLY1305 41	/* combined AEAD cipher per RFC 8439 */
193 #define	CRYPTO_ALGORITHM_MAX	41	/* Keep updated - see below */
194 
195 #define	CRYPTO_ALGO_VALID(x)	((x) >= CRYPTO_ALGORITHM_MIN && \
196 				 (x) <= CRYPTO_ALGORITHM_MAX)
197 
198 /* Algorithm flags */
199 #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
200 #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
201 #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
202 
203 /*
204  * Crypto driver/device flags.  They can set in the crid
205  * parameter when creating a session or submitting a key
206  * op to affect the device/driver assigned.  If neither
207  * of these are specified then the crid is assumed to hold
208  * the driver id of an existing (and suitable) device that
209  * must be used to satisfy the request.
210  */
211 #define CRYPTO_FLAG_HARDWARE	0x01000000	/* hardware accelerated */
212 #define CRYPTO_FLAG_SOFTWARE	0x02000000	/* software implementation */
213 
214 /* Does the kernel support vmpage buffers on this platform? */
215 #ifdef __powerpc__
216 #define CRYPTO_MAY_HAVE_VMPAGE	1
217 #else
218 #define CRYPTO_MAY_HAVE_VMPAGE	( PMAP_HAS_DMAP )
219 #endif
220 /* Does the currently running system support vmpage buffers on this platform? */
221 #define CRYPTO_HAS_VMPAGE	( PMAP_HAS_DMAP )
222 
223 /* NB: deprecated */
224 struct session_op {
225 	uint32_t	cipher;		/* ie. CRYPTO_AES_CBC */
226 	uint32_t	mac;		/* ie. CRYPTO_SHA2_256_HMAC */
227 
228 	uint32_t	keylen;		/* cipher key */
229 	const void	*key;
230 	int		mackeylen;	/* mac key */
231 	const void	*mackey;
232 
233   	uint32_t	ses;		/* returns: session # */
234 };
235 
236 /*
237  * session and crypt _op structs are used by userspace programs to interact
238  * with /dev/crypto.  Confusingly, the internal kernel interface is named
239  * "cryptop" (no underscore).
240  */
241 struct session2_op {
242 	uint32_t	cipher;		/* ie. CRYPTO_AES_CBC */
243 	uint32_t	mac;		/* ie. CRYPTO_SHA2_256_HMAC */
244 
245 	uint32_t	keylen;		/* cipher key */
246 	const void	*key;
247 	int		mackeylen;	/* mac key */
248 	const void	*mackey;
249 
250   	uint32_t	ses;		/* returns: session # */
251 	int		crid;		/* driver id + flags (rw) */
252 	int		ivlen;		/* length of nonce/IV */
253 	int		maclen;		/* length of MAC/tag */
254 	int		pad[2];		/* for future expansion */
255 };
256 
257 struct crypt_op {
258 	uint32_t	ses;
259 	uint16_t	op;		/* i.e. COP_ENCRYPT */
260 #define COP_ENCRYPT	1
261 #define COP_DECRYPT	2
262 	uint16_t	flags;
263 #define	COP_F_CIPHER_FIRST	0x0001	/* Cipher before MAC. */
264 #define	COP_F_BATCH		0x0008	/* Batch op if possible */
265 	u_int		len;
266 	const void	*src;		/* become iov[] inside kernel */
267 	void		*dst;
268 	void		*mac;		/* must be big enough for chosen MAC */
269 	const void	*iv;
270 };
271 
272 /* op and flags the same as crypt_op */
273 struct crypt_aead {
274 	uint32_t	ses;
275 	uint16_t	op;		/* i.e. COP_ENCRYPT */
276 	uint16_t	flags;
277 	u_int		len;
278 	u_int		aadlen;
279 	u_int		ivlen;
280 	const void	*src;		/* become iov[] inside kernel */
281 	void		*dst;
282 	const void	*aad;		/* additional authenticated data */
283 	void		*tag;		/* must fit for chosen TAG length */
284 	const void	*iv;
285 };
286 
287 /*
288  * Parameters for looking up a crypto driver/device by
289  * device name or by id.  The latter are returned for
290  * created sessions (crid) and completed key operations.
291  */
292 struct crypt_find_op {
293 	int		crid;		/* driver id + flags */
294 	char		name[32];	/* device/driver name */
295 };
296 
297 /* bignum parameter, in packed bytes, ... */
298 struct crparam {
299 	void		*crp_p;
300 	u_int		crp_nbits;
301 };
302 
303 #define CRK_MAXPARAM	8
304 
305 struct crypt_kop {
306 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
307 	u_int		crk_status;	/* return status */
308 	u_short		crk_iparams;	/* # of input parameters */
309 	u_short		crk_oparams;	/* # of output parameters */
310 	u_int		crk_crid;	/* NB: only used by CIOCKEY2 (rw) */
311 	struct crparam	crk_param[CRK_MAXPARAM];
312 };
313 #define	CRK_ALGORITM_MIN	0
314 #define CRK_MOD_EXP		0
315 #define CRK_MOD_EXP_CRT		1
316 #define CRK_DSA_SIGN		2
317 #define CRK_DSA_VERIFY		3
318 #define CRK_DH_COMPUTE_KEY	4
319 #define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
320 
321 #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
322 #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
323 #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
324 #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
325 #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
326 
327 #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
328 #define	CIOCFSESSION	_IOW('c', 102, uint32_t)
329 #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
330 #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
331 #define CIOCASYMFEAT	_IOR('c', 105, uint32_t)
332 #define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
333 #define	CIOCKEY2	_IOWR('c', 107, struct crypt_kop)
334 #define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
335 #define	CIOCCRYPTAEAD	_IOWR('c', 109, struct crypt_aead)
336 
337 struct cryptostats {
338 	uint64_t	cs_ops;		/* symmetric crypto ops submitted */
339 	uint64_t	cs_errs;	/* symmetric crypto ops that failed */
340 	uint64_t	cs_kops;	/* asymetric/key ops submitted */
341 	uint64_t	cs_kerrs;	/* asymetric/key ops that failed */
342 	uint64_t	cs_intrs;	/* crypto swi thread activations */
343 	uint64_t	cs_rets;	/* crypto return thread activations */
344 	uint64_t	cs_blocks;	/* symmetric op driver block */
345 	uint64_t	cs_kblocks;	/* symmetric op driver block */
346 };
347 
348 #ifdef _KERNEL
349 
350 /*
351  * Return values for cryptodev_probesession methods.
352  */
353 #define	CRYPTODEV_PROBE_HARDWARE	(-100)
354 #define	CRYPTODEV_PROBE_ACCEL_SOFTWARE	(-200)
355 #define	CRYPTODEV_PROBE_SOFTWARE	(-500)
356 
357 #if 0
358 #define CRYPTDEB(s, ...) do {						\
359 	printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);	\
360 } while (0)
361 #else
362 #define CRYPTDEB(...)	do { } while (0)
363 #endif
364 
365 struct crypto_session_params {
366 	int		csp_mode;	/* Type of operations to perform. */
367 
368 #define	CSP_MODE_NONE		0
369 #define	CSP_MODE_COMPRESS	1	/* Compression/decompression. */
370 #define	CSP_MODE_CIPHER		2	/* Encrypt/decrypt. */
371 #define	CSP_MODE_DIGEST		3	/* Compute/verify digest. */
372 #define	CSP_MODE_AEAD		4	/* Combined auth/encryption. */
373 #define	CSP_MODE_ETA		5	/* IPsec style encrypt-then-auth */
374 
375 	int		csp_flags;
376 
377 #define	CSP_F_SEPARATE_OUTPUT	0x0001	/* Requests can use separate output */
378 #define	CSP_F_SEPARATE_AAD	0x0002	/* Requests can use separate AAD */
379 #define CSP_F_ESN		0x0004  /* Requests can use seperate ESN field */
380 
381 	int		csp_ivlen;	/* IV length in bytes. */
382 
383 	int		csp_cipher_alg;
384 	int		csp_cipher_klen; /* Key length in bytes. */
385 	const void	*csp_cipher_key;
386 
387 	int		csp_auth_alg;
388 	int		csp_auth_klen;	/* Key length in bytes. */
389 	const void	*csp_auth_key;
390 	int		csp_auth_mlen;	/* Number of digest bytes to use.
391 					   0 means all. */
392 };
393 
394 enum crypto_buffer_type {
395 	CRYPTO_BUF_NONE = 0,
396 	CRYPTO_BUF_CONTIG,
397 	CRYPTO_BUF_UIO,
398 	CRYPTO_BUF_MBUF,
399 	CRYPTO_BUF_VMPAGE,
400 	CRYPTO_BUF_SINGLE_MBUF,
401 	CRYPTO_BUF_LAST = CRYPTO_BUF_SINGLE_MBUF
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 #define	crp_startcopy	crp_flags
466 	int		crp_flags;
467 
468 #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible */
469 #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
470 #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
471 #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
472 #define	CRYPTO_F_ASYNC		0x0080	/* Dispatch crypto jobs on several threads
473 					 * if op is synchronous
474 					 */
475 #define	CRYPTO_F_ASYNC_KEEPORDER	0x0100	/*
476 					 * Dispatch the crypto jobs in the same
477 					 * order there are submitted. Applied only
478 					 * if CRYPTO_F_ASYNC flags is set
479 					 */
480 #define	CRYPTO_F_IV_SEPARATE	0x0200	/* Use crp_iv[] as IV. */
481 
482 	int		crp_op;
483 
484 	struct crypto_buffer crp_buf;
485 	struct crypto_buffer crp_obuf;
486 
487 	void		*crp_aad;	/* AAD buffer. */
488 	int		crp_aad_start;	/* Location of AAD. */
489 	int		crp_aad_length;	/* 0 => no AAD. */
490 	uint8_t		crp_esn[4];	/* high-order ESN */
491 
492 	int		crp_iv_start;	/* Location of IV.  IV length is from
493 					 * the session.
494 					 */
495 	int		crp_payload_start; /* Location of ciphertext. */
496 	int		crp_payload_output_start;
497 	int		crp_payload_length;
498 	int		crp_digest_start; /* Location of MAC/tag.  Length is
499 					   * from the session.
500 					   */
501 
502 	uint8_t		crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
503 
504 	const void	*crp_cipher_key; /* New cipher key if non-NULL. */
505 	const void	*crp_auth_key;	/* New auth key if non-NULL. */
506 #define	crp_endcopy	crp_opaque
507 
508 	void		*crp_opaque;	/* Opaque pointer, passed along */
509 
510 	int (*crp_callback)(struct cryptop *); /* Callback function */
511 
512 	struct bintime	crp_tstamp;	/* performance time stamp */
513 	uint32_t	crp_seq;	/* used for ordered dispatch */
514 	uint32_t	crp_retw_id;	/*
515 					 * the return worker to be used,
516 					 *  used for ordered dispatch
517 					 */
518 };
519 
520 static __inline void
_crypto_use_buf(struct crypto_buffer * cb,void * buf,int len)521 _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
522 {
523 	cb->cb_buf = buf;
524 	cb->cb_buf_len = len;
525 	cb->cb_type = CRYPTO_BUF_CONTIG;
526 }
527 
528 static __inline void
_crypto_use_mbuf(struct crypto_buffer * cb,struct mbuf * m)529 _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
530 {
531 	cb->cb_mbuf = m;
532 	cb->cb_type = CRYPTO_BUF_MBUF;
533 }
534 
535 static __inline void
_crypto_use_single_mbuf(struct crypto_buffer * cb,struct mbuf * m)536 _crypto_use_single_mbuf(struct crypto_buffer *cb, struct mbuf *m)
537 {
538 	cb->cb_mbuf = m;
539 	cb->cb_type = CRYPTO_BUF_SINGLE_MBUF;
540 }
541 
542 static __inline void
_crypto_use_vmpage(struct crypto_buffer * cb,vm_page_t * pages,int len,int offset)543 _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
544     int offset)
545 {
546 	cb->cb_vm_page = pages;
547 	cb->cb_vm_page_len = len;
548 	cb->cb_vm_page_offset = offset;
549 	cb->cb_type = CRYPTO_BUF_VMPAGE;
550 }
551 
552 static __inline void
_crypto_use_uio(struct crypto_buffer * cb,struct uio * uio)553 _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
554 {
555 	cb->cb_uio = uio;
556 	cb->cb_type = CRYPTO_BUF_UIO;
557 }
558 
559 static __inline void
crypto_use_buf(struct cryptop * crp,void * buf,int len)560 crypto_use_buf(struct cryptop *crp, void *buf, int len)
561 {
562 	_crypto_use_buf(&crp->crp_buf, buf, len);
563 }
564 
565 static __inline void
crypto_use_mbuf(struct cryptop * crp,struct mbuf * m)566 crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
567 {
568 	_crypto_use_mbuf(&crp->crp_buf, m);
569 }
570 
571 static __inline void
crypto_use_single_mbuf(struct cryptop * crp,struct mbuf * m)572 crypto_use_single_mbuf(struct cryptop *crp, struct mbuf *m)
573 {
574 	_crypto_use_single_mbuf(&crp->crp_buf, m);
575 }
576 
577 static __inline void
crypto_use_vmpage(struct cryptop * crp,vm_page_t * pages,int len,int offset)578 crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
579 {
580 	_crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
581 }
582 
583 static __inline void
crypto_use_uio(struct cryptop * crp,struct uio * uio)584 crypto_use_uio(struct cryptop *crp, struct uio *uio)
585 {
586 	_crypto_use_uio(&crp->crp_buf, uio);
587 }
588 
589 static __inline void
crypto_use_output_buf(struct cryptop * crp,void * buf,int len)590 crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
591 {
592 	_crypto_use_buf(&crp->crp_obuf, buf, len);
593 }
594 
595 static __inline void
crypto_use_output_mbuf(struct cryptop * crp,struct mbuf * m)596 crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
597 {
598 	_crypto_use_mbuf(&crp->crp_obuf, m);
599 }
600 
601 static __inline void
crypto_use_output_single_mbuf(struct cryptop * crp,struct mbuf * m)602 crypto_use_output_single_mbuf(struct cryptop *crp, struct mbuf *m)
603 {
604 	_crypto_use_single_mbuf(&crp->crp_obuf, m);
605 }
606 
607 static __inline void
crypto_use_output_vmpage(struct cryptop * crp,vm_page_t * pages,int len,int offset)608 crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
609     int offset)
610 {
611 	_crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
612 }
613 
614 static __inline void
crypto_use_output_uio(struct cryptop * crp,struct uio * uio)615 crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
616 {
617 	_crypto_use_uio(&crp->crp_obuf, uio);
618 }
619 
620 #define	CRYPTOP_ASYNC(crp)			\
621 	(((crp)->crp_flags & CRYPTO_F_ASYNC) && \
622 	crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
623 #define	CRYPTOP_ASYNC_KEEPORDER(crp) \
624 	(CRYPTOP_ASYNC(crp) && \
625 	(crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
626 #define	CRYPTO_HAS_OUTPUT_BUFFER(crp)					\
627 	((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
628 
629 /* Flags in crp_op. */
630 #define	CRYPTO_OP_DECRYPT		0x0
631 #define	CRYPTO_OP_ENCRYPT		0x1
632 #define	CRYPTO_OP_IS_ENCRYPT(op)	((op) & CRYPTO_OP_ENCRYPT)
633 #define	CRYPTO_OP_COMPUTE_DIGEST	0x0
634 #define	CRYPTO_OP_VERIFY_DIGEST		0x2
635 #define	CRYPTO_OP_DECOMPRESS		CRYPTO_OP_DECRYPT
636 #define	CRYPTO_OP_COMPRESS		CRYPTO_OP_ENCRYPT
637 #define	CRYPTO_OP_IS_COMPRESS(op)	((op) & CRYPTO_OP_COMPRESS)
638 
639 /*
640  * Hints passed to process methods.
641  */
642 #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
643 
644 struct cryptkop {
645 	TAILQ_ENTRY(cryptkop) krp_next;
646 
647 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
648 	u_int		krp_status;	/* return status */
649 	u_short		krp_iparams;	/* # of input parameters */
650 	u_short		krp_oparams;	/* # of output parameters */
651 	u_int		krp_crid;	/* desired device, etc. */
652 	uint32_t	krp_hid;	/* device used */
653 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
654 	void		(*krp_callback)(struct cryptkop *);
655 	struct cryptocap *krp_cap;
656 };
657 
658 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
659 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
660 void	*crypto_get_driver_session(crypto_session_t crypto_session);
661 const struct crypto_session_params *crypto_get_params(
662     crypto_session_t crypto_session);
663 struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
664 struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
665 
666 MALLOC_DECLARE(M_CRYPTO_DATA);
667 
668 int	crypto_newsession(crypto_session_t *cses,
669     const struct crypto_session_params *params, int crid);
670 void	crypto_freesession(crypto_session_t cses);
671 #define	CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
672 #define	CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
673 #define	CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
674 #define	CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
675 int32_t	crypto_get_driverid(device_t dev, size_t session_size, int flags);
676 int	crypto_find_driver(const char *);
677 device_t crypto_find_device_byhid(int hid);
678 int	crypto_getcaps(int hid);
679 int	crypto_kregister(uint32_t, int, uint32_t);
680 int	crypto_unregister_all(uint32_t driverid);
681 int	crypto_dispatch(struct cryptop *crp);
682 int	crypto_kdispatch(struct cryptkop *);
683 #define	CRYPTO_SYMQ	0x1
684 #define	CRYPTO_ASYMQ	0x2
685 int	crypto_unblock(uint32_t, int);
686 void	crypto_done(struct cryptop *crp);
687 void	crypto_kdone(struct cryptkop *);
688 int	crypto_getfeat(int *);
689 
690 struct cryptop *crypto_clonereq(struct cryptop *crp, crypto_session_t cses,
691     int how);
692 void	crypto_destroyreq(struct cryptop *crp);
693 void	crypto_initreq(struct cryptop *crp, crypto_session_t cses);
694 void	crypto_freereq(struct cryptop *crp);
695 struct cryptop *crypto_getreq(crypto_session_t cses, int how);
696 
697 extern	int crypto_usercrypto;		/* userland may do crypto requests */
698 extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
699 extern	int crypto_devallowsoft;	/* only use hardware crypto */
700 
701 #ifdef SYSCTL_DECL
702 SYSCTL_DECL(_kern_crypto);
703 #endif
704 
705 /* Helper routines for drivers to initialize auth contexts for HMAC. */
706 struct auth_hash;
707 
708 void	hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
709     void *auth_ctx);
710 void	hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
711     void *auth_ctx);
712 
713 /*
714  * Crypto-related utility routines used mainly by drivers.
715  *
716  * Similar to m_copyback/data, *_copyback copy data from the 'src'
717  * buffer into the crypto request's data buffer while *_copydata copy
718  * data from the crypto request's data buffer into the the 'dst'
719  * buffer.
720  */
721 void	crypto_copyback(struct cryptop *crp, int off, int size,
722 	    const void *src);
723 void	crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
724 int	crypto_apply(struct cryptop *crp, int off, int len,
725 	    int (*f)(void *, const void *, u_int), void *arg);
726 void	*crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
727 	    size_t len);
728 
729 int	crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
730 	    int (*f)(void *, const void *, u_int), void *arg);
731 void	*crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
732 	    size_t skip, size_t len);
733 size_t	crypto_buffer_len(struct crypto_buffer *cb);
734 void	crypto_cursor_init(struct crypto_buffer_cursor *cc,
735 	    const struct crypto_buffer *cb);
736 void	crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
737 void	*crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len);
738 void	*crypto_cursor_segbase(struct crypto_buffer_cursor *cc);
739 size_t	crypto_cursor_seglen(struct crypto_buffer_cursor *cc);
740 void	crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
741 	    const void *vsrc);
742 void	crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
743 	    void *vdst);
744 void	crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
745 	    void *vdst);
746 
747 static __inline void
crypto_cursor_copy(const struct crypto_buffer_cursor * fromc,struct crypto_buffer_cursor * toc)748 crypto_cursor_copy(const struct crypto_buffer_cursor *fromc,
749     struct crypto_buffer_cursor *toc)
750 {
751 	memcpy(toc, fromc, sizeof(*toc));
752 }
753 
754 static __inline void
crypto_read_iv(struct cryptop * crp,void * iv)755 crypto_read_iv(struct cryptop *crp, void *iv)
756 {
757 	const struct crypto_session_params *csp;
758 
759 	csp = crypto_get_params(crp->crp_session);
760 	if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
761 		memcpy(iv, crp->crp_iv, csp->csp_ivlen);
762 	else
763 		crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
764 }
765 
766 static __inline size_t
ccm_max_payload_length(const struct crypto_session_params * csp)767 ccm_max_payload_length(const struct crypto_session_params *csp)
768 {
769 	/* RFC 3160 */
770 	const u_int L = 15 - csp->csp_ivlen;
771 
772 	switch (L) {
773 	case 2:
774 		return (0xffff);
775 	case 3:
776 		return (0xffffff);
777 #ifdef __LP64__
778 	case 4:
779 		return (0xffffffff);
780 	case 5:
781 		return (0xffffffffff);
782 	case 6:
783 		return (0xffffffffffff);
784 	case 7:
785 		return (0xffffffffffffff);
786 	default:
787 		return (0xffffffffffffffff);
788 #else
789 	default:
790 		return (0xffffffff);
791 #endif
792 	}
793 }
794 
795 #endif /* _KERNEL */
796 #endif /* _CRYPTO_CRYPTO_H_ */
797