xref: /freebsd/sys/opencrypto/cryptodev.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
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 #include <sys/libkern.h>
73 #include <sys/time.h>
74 #endif
75 
76 /* Some initial values */
77 #define CRYPTO_DRIVERS_INITIAL	4
78 
79 /* Hash values */
80 #define	NULL_HASH_LEN		16
81 #define	SHA1_HASH_LEN		20
82 #define	RIPEMD160_HASH_LEN	20
83 #define	SHA2_224_HASH_LEN	28
84 #define	SHA2_256_HASH_LEN	32
85 #define	SHA2_384_HASH_LEN	48
86 #define	SHA2_512_HASH_LEN	64
87 #define	AES_GMAC_HASH_LEN	16
88 #define	POLY1305_HASH_LEN	16
89 #define	AES_CBC_MAC_HASH_LEN	16
90 /* Maximum hash algorithm result length */
91 #define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
92 
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 #define	POLY1305_BLOCK_LEN	16
100 
101 /* HMAC values */
102 #define	NULL_HMAC_BLOCK_LEN		64
103 /* Maximum HMAC block length */
104 #define	HMAC_MAX_BLOCK_LEN	SHA2_512_BLOCK_LEN /* Keep this updated */
105 #define	HMAC_IPAD_VAL			0x36
106 #define	HMAC_OPAD_VAL			0x5C
107 /* HMAC Key Length */
108 #define	AES_128_GMAC_KEY_LEN		16
109 #define	AES_192_GMAC_KEY_LEN		24
110 #define	AES_256_GMAC_KEY_LEN		32
111 #define	AES_128_CBC_MAC_KEY_LEN		16
112 #define	AES_192_CBC_MAC_KEY_LEN		24
113 #define	AES_256_CBC_MAC_KEY_LEN		32
114 
115 #define	POLY1305_KEY_LEN		32
116 
117 /* Encryption algorithm block sizes */
118 #define	NULL_BLOCK_LEN		4	/* IPsec to maintain alignment */
119 #define	RIJNDAEL128_BLOCK_LEN	16
120 #define	AES_BLOCK_LEN		16
121 #define	AES_ICM_BLOCK_LEN	1
122 #define	CAMELLIA_BLOCK_LEN	16
123 #define	CHACHA20_NATIVE_BLOCK_LEN	64
124 #define	EALG_MAX_BLOCK_LEN	CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
125 
126 /* IV Lengths */
127 
128 #define	AES_GCM_IV_LEN		12
129 #define	AES_CCM_IV_LEN		12
130 #define	AES_XTS_IV_LEN		8
131 #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
132 #define	CHACHA20_POLY1305_IV_LEN	12
133 #define	XCHACHA20_POLY1305_IV_LEN	24
134 
135 /* Min and Max Encryption Key Sizes */
136 #define	NULL_MIN_KEY		0
137 #define	NULL_MAX_KEY		256 /* 2048 bits, max key */
138 #define	RIJNDAEL_MIN_KEY	16
139 #define	RIJNDAEL_MAX_KEY	32
140 #define	AES_MIN_KEY		RIJNDAEL_MIN_KEY
141 #define	AES_MAX_KEY		RIJNDAEL_MAX_KEY
142 #define	AES_XTS_MIN_KEY		(2 * AES_MIN_KEY)
143 #define	AES_XTS_MAX_KEY		(2 * AES_MAX_KEY)
144 #define	CAMELLIA_MIN_KEY	16
145 #define	CAMELLIA_MAX_KEY	32
146 #define	CHACHA20_POLY1305_KEY	32
147 #define	XCHACHA20_POLY1305_KEY	32
148 
149 /* Maximum hash algorithm result length */
150 #define	AALG_MAX_RESULT_LEN	64 /* Keep this updated */
151 
152 #define	CRYPTO_ALGORITHM_MIN	1
153 #define	CRYPTO_DES_CBC		1
154 #define	CRYPTO_3DES_CBC		2
155 #define	CRYPTO_BLF_CBC		3
156 #define	CRYPTO_CAST_CBC		4
157 #define	CRYPTO_SKIPJACK_CBC	5
158 #define	CRYPTO_MD5_HMAC		6
159 #define	CRYPTO_SHA1_HMAC	7
160 #define	CRYPTO_RIPEMD160_HMAC	8
161 #define	CRYPTO_MD5_KPDK		9
162 #define	CRYPTO_SHA1_KPDK	10
163 #define	CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
164 #define	CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
165 #define	CRYPTO_ARC4		12
166 #define	CRYPTO_MD5		13
167 #define	CRYPTO_SHA1		14
168 #define	CRYPTO_NULL_HMAC	15
169 #define	CRYPTO_NULL_CBC		16
170 #define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
171 #define	CRYPTO_SHA2_256_HMAC	18
172 #define	CRYPTO_SHA2_384_HMAC	19
173 #define	CRYPTO_SHA2_512_HMAC	20
174 #define	CRYPTO_CAMELLIA_CBC	21
175 #define	CRYPTO_AES_XTS		22
176 #define	CRYPTO_AES_ICM		23 /* commonly known as CTR mode */
177 #define	CRYPTO_AES_NIST_GMAC	24 /* GMAC only */
178 #define	CRYPTO_AES_NIST_GCM_16	25 /* 16 byte ICV */
179 #ifdef _KERNEL
180 #define	CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
181 #define	CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
182 #define	CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
183 #endif
184 #define	CRYPTO_BLAKE2B		29 /* Blake2b hash */
185 #define	CRYPTO_BLAKE2S		30 /* Blake2s hash */
186 #define	CRYPTO_CHACHA20		31 /* Chacha20 stream cipher */
187 #define	CRYPTO_SHA2_224_HMAC	32
188 #define	CRYPTO_RIPEMD160	33
189 #define	CRYPTO_SHA2_224		34
190 #define	CRYPTO_SHA2_256		35
191 #define	CRYPTO_SHA2_384		36
192 #define	CRYPTO_SHA2_512		37
193 #define	CRYPTO_POLY1305		38
194 #define	CRYPTO_AES_CCM_CBC_MAC	39	/* auth side */
195 #define	CRYPTO_AES_CCM_16	40	/* cipher side */
196 #define	CRYPTO_CHACHA20_POLY1305 41	/* combined AEAD cipher per RFC 8439 */
197 #define	CRYPTO_XCHACHA20_POLY1305 42
198 #define	CRYPTO_ALGORITHM_MAX	42	/* Keep updated - see below */
199 
200 #define	CRYPTO_ALGO_VALID(x)	((x) >= CRYPTO_ALGORITHM_MIN && \
201 				 (x) <= CRYPTO_ALGORITHM_MAX)
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 #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
298 #define	CIOCFSESSION	_IOW('c', 102, uint32_t)
299 #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
300 #define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
301 #define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
302 #define	CIOCCRYPTAEAD	_IOWR('c', 109, struct crypt_aead)
303 
304 struct cryptostats {
305 	uint64_t	cs_ops;		/* symmetric crypto ops submitted */
306 	uint64_t	cs_errs;	/* symmetric crypto ops that failed */
307 	uint64_t	cs_kops;	/* asymetric/key ops submitted */
308 	uint64_t	cs_kerrs;	/* asymetric/key ops that failed */
309 	uint64_t	cs_intrs;	/* crypto swi thread activations */
310 	uint64_t	cs_rets;	/* crypto return thread activations */
311 	uint64_t	cs_blocks;	/* symmetric op driver block */
312 	uint64_t	cs_kblocks;	/* symmetric op driver block */
313 };
314 
315 #ifdef _KERNEL
316 
317 /*
318  * Return values for cryptodev_probesession methods.
319  */
320 #define	CRYPTODEV_PROBE_HARDWARE	(-100)
321 #define	CRYPTODEV_PROBE_ACCEL_SOFTWARE	(-200)
322 #define	CRYPTODEV_PROBE_SOFTWARE	(-500)
323 
324 #if 0
325 #define CRYPTDEB(s, ...) do {						\
326 	printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);	\
327 } while (0)
328 #else
329 #define CRYPTDEB(...)	do { } while (0)
330 #endif
331 
332 struct crypto_session_params {
333 	int		csp_mode;	/* Type of operations to perform. */
334 
335 #define	CSP_MODE_NONE		0
336 #define	CSP_MODE_COMPRESS	1	/* Compression/decompression. */
337 #define	CSP_MODE_CIPHER		2	/* Encrypt/decrypt. */
338 #define	CSP_MODE_DIGEST		3	/* Compute/verify digest. */
339 #define	CSP_MODE_AEAD		4	/* Combined auth/encryption. */
340 #define	CSP_MODE_ETA		5	/* IPsec style encrypt-then-auth */
341 
342 	int		csp_flags;
343 
344 #define	CSP_F_SEPARATE_OUTPUT	0x0001	/* Requests can use separate output */
345 #define	CSP_F_SEPARATE_AAD	0x0002	/* Requests can use separate AAD */
346 #define CSP_F_ESN		0x0004  /* Requests can use seperate ESN field */
347 
348 	int		csp_ivlen;	/* IV length in bytes. */
349 
350 	int		csp_cipher_alg;
351 	int		csp_cipher_klen; /* Key length in bytes. */
352 	const void	*csp_cipher_key;
353 
354 	int		csp_auth_alg;
355 	int		csp_auth_klen;	/* Key length in bytes. */
356 	const void	*csp_auth_key;
357 	int		csp_auth_mlen;	/* Number of digest bytes to use.
358 					   0 means all. */
359 };
360 
361 enum crypto_buffer_type {
362 	CRYPTO_BUF_NONE = 0,
363 	CRYPTO_BUF_CONTIG,
364 	CRYPTO_BUF_UIO,
365 	CRYPTO_BUF_MBUF,
366 	CRYPTO_BUF_VMPAGE,
367 	CRYPTO_BUF_SINGLE_MBUF,
368 	CRYPTO_BUF_LAST = CRYPTO_BUF_SINGLE_MBUF
369 };
370 
371 /*
372  * Description of a data buffer for a request.  Requests can either
373  * have a single buffer that is modified in place or separate input
374  * and output buffers.
375  */
376 struct crypto_buffer {
377 	union {
378 		struct {
379 			char	*cb_buf;
380 			int	cb_buf_len;
381 		};
382 		struct mbuf *cb_mbuf;
383 		struct {
384 			vm_page_t *cb_vm_page;
385 			int cb_vm_page_len;
386 			int cb_vm_page_offset;
387 		};
388 		struct uio *cb_uio;
389 	};
390 	enum crypto_buffer_type cb_type;
391 };
392 
393 /*
394  * A cursor is used to iterate through a crypto request data buffer.
395  */
396 struct crypto_buffer_cursor {
397 	union {
398 		char *cc_buf;
399 		struct mbuf *cc_mbuf;
400 		struct iovec *cc_iov;
401 		vm_page_t *cc_vmpage;
402 	};
403 	/* Optional bytes of valid data remaining */
404 	int cc_buf_len;
405 	/*
406 	 * Optional offset within the current buffer segment where
407 	 * valid data begins
408 	 */
409 	size_t cc_offset;
410 	enum crypto_buffer_type cc_type;
411 };
412 
413 /* Structure describing complete operation */
414 struct cryptop {
415 	TAILQ_ENTRY(cryptop) crp_next;
416 
417 	struct task	crp_task;
418 
419 	crypto_session_t crp_session;	/* Session */
420 	int		crp_olen;	/* Result total length */
421 
422 	int		crp_etype;	/*
423 					 * Error type (zero means no error).
424 					 * All error codes except EAGAIN
425 					 * indicate possible data corruption (as in,
426 					 * the data have been touched). On all
427 					 * errors, the crp_session may have changed
428 					 * (reset to a new one), so the caller
429 					 * should always check and use the new
430 					 * value on future requests.
431 					 */
432 #define	crp_startcopy	crp_flags
433 	int		crp_flags;
434 
435 #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
436 #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
437 #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
438 #define	CRYPTO_F_ASYNC_ORDERED	0x0100	/* Completions must happen in order */
439 #define	CRYPTO_F_IV_SEPARATE	0x0200	/* Use crp_iv[] as IV. */
440 
441 	int		crp_op;
442 
443 	struct crypto_buffer crp_buf;
444 	struct crypto_buffer crp_obuf;
445 
446 	void		*crp_aad;	/* AAD buffer. */
447 	int		crp_aad_start;	/* Location of AAD. */
448 	int		crp_aad_length;	/* 0 => no AAD. */
449 	uint8_t		crp_esn[4];	/* high-order ESN */
450 
451 	int		crp_iv_start;	/* Location of IV.  IV length is from
452 					 * the session.
453 					 */
454 	int		crp_payload_start; /* Location of ciphertext. */
455 	int		crp_payload_output_start;
456 	int		crp_payload_length;
457 	int		crp_digest_start; /* Location of MAC/tag.  Length is
458 					   * from the session.
459 					   */
460 
461 	uint8_t		crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
462 
463 	const void	*crp_cipher_key; /* New cipher key if non-NULL. */
464 	const void	*crp_auth_key;	/* New auth key if non-NULL. */
465 #define	crp_endcopy	crp_opaque
466 
467 	void		*crp_opaque;	/* Opaque pointer, passed along */
468 
469 	int (*crp_callback)(struct cryptop *); /* Callback function */
470 
471 	struct bintime	crp_tstamp;	/* performance time stamp */
472 	uint32_t	crp_seq;	/* used for ordered dispatch */
473 	uint32_t	crp_retw_id;	/*
474 					 * the return worker to be used,
475 					 *  used for ordered dispatch
476 					 */
477 };
478 
479 TAILQ_HEAD(cryptopq, cryptop);
480 
481 static __inline void
_crypto_use_buf(struct crypto_buffer * cb,void * buf,int len)482 _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
483 {
484 	cb->cb_buf = buf;
485 	cb->cb_buf_len = len;
486 	cb->cb_type = CRYPTO_BUF_CONTIG;
487 }
488 
489 static __inline void
_crypto_use_mbuf(struct crypto_buffer * cb,struct mbuf * m)490 _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
491 {
492 	cb->cb_mbuf = m;
493 	cb->cb_type = CRYPTO_BUF_MBUF;
494 }
495 
496 static __inline void
_crypto_use_single_mbuf(struct crypto_buffer * cb,struct mbuf * m)497 _crypto_use_single_mbuf(struct crypto_buffer *cb, struct mbuf *m)
498 {
499 	cb->cb_mbuf = m;
500 	cb->cb_type = CRYPTO_BUF_SINGLE_MBUF;
501 }
502 
503 static __inline void
_crypto_use_vmpage(struct crypto_buffer * cb,vm_page_t * pages,int len,int offset)504 _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
505     int offset)
506 {
507 	cb->cb_vm_page = pages;
508 	cb->cb_vm_page_len = len;
509 	cb->cb_vm_page_offset = offset;
510 	cb->cb_type = CRYPTO_BUF_VMPAGE;
511 }
512 
513 static __inline void
_crypto_use_uio(struct crypto_buffer * cb,struct uio * uio)514 _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
515 {
516 	cb->cb_uio = uio;
517 	cb->cb_type = CRYPTO_BUF_UIO;
518 }
519 
520 static __inline void
crypto_use_buf(struct cryptop * crp,void * buf,int len)521 crypto_use_buf(struct cryptop *crp, void *buf, int len)
522 {
523 	_crypto_use_buf(&crp->crp_buf, buf, len);
524 }
525 
526 static __inline void
crypto_use_mbuf(struct cryptop * crp,struct mbuf * m)527 crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
528 {
529 	_crypto_use_mbuf(&crp->crp_buf, m);
530 }
531 
532 static __inline void
crypto_use_single_mbuf(struct cryptop * crp,struct mbuf * m)533 crypto_use_single_mbuf(struct cryptop *crp, struct mbuf *m)
534 {
535 	_crypto_use_single_mbuf(&crp->crp_buf, m);
536 }
537 
538 static __inline void
crypto_use_vmpage(struct cryptop * crp,vm_page_t * pages,int len,int offset)539 crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
540 {
541 	_crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
542 }
543 
544 static __inline void
crypto_use_uio(struct cryptop * crp,struct uio * uio)545 crypto_use_uio(struct cryptop *crp, struct uio *uio)
546 {
547 	_crypto_use_uio(&crp->crp_buf, uio);
548 }
549 
550 static __inline void
crypto_use_output_buf(struct cryptop * crp,void * buf,int len)551 crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
552 {
553 	_crypto_use_buf(&crp->crp_obuf, buf, len);
554 }
555 
556 static __inline void
crypto_use_output_mbuf(struct cryptop * crp,struct mbuf * m)557 crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
558 {
559 	_crypto_use_mbuf(&crp->crp_obuf, m);
560 }
561 
562 static __inline void
crypto_use_output_single_mbuf(struct cryptop * crp,struct mbuf * m)563 crypto_use_output_single_mbuf(struct cryptop *crp, struct mbuf *m)
564 {
565 	_crypto_use_single_mbuf(&crp->crp_obuf, m);
566 }
567 
568 static __inline void
crypto_use_output_vmpage(struct cryptop * crp,vm_page_t * pages,int len,int offset)569 crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
570     int offset)
571 {
572 	_crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
573 }
574 
575 static __inline void
crypto_use_output_uio(struct cryptop * crp,struct uio * uio)576 crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
577 {
578 	_crypto_use_uio(&crp->crp_obuf, uio);
579 }
580 
581 #define	CRYPTO_HAS_OUTPUT_BUFFER(crp)					\
582 	((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
583 
584 /* Flags in crp_op. */
585 #define	CRYPTO_OP_DECRYPT		0x0
586 #define	CRYPTO_OP_ENCRYPT		0x1
587 #define	CRYPTO_OP_IS_ENCRYPT(op)	((op) & CRYPTO_OP_ENCRYPT)
588 #define	CRYPTO_OP_COMPUTE_DIGEST	0x0
589 #define	CRYPTO_OP_VERIFY_DIGEST		0x2
590 #define	CRYPTO_OP_DECOMPRESS		CRYPTO_OP_DECRYPT
591 #define	CRYPTO_OP_COMPRESS		CRYPTO_OP_ENCRYPT
592 #define	CRYPTO_OP_IS_COMPRESS(op)	((op) & CRYPTO_OP_COMPRESS)
593 
594 /*
595  * Hints passed to process methods.
596  */
597 #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
598 
599 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
600 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
601 void	*crypto_get_driver_session(crypto_session_t crypto_session);
602 const struct crypto_session_params *crypto_get_params(
603     crypto_session_t crypto_session);
604 const struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
605 const struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
606 
607 #ifdef MALLOC_DECLARE
608 MALLOC_DECLARE(M_CRYPTO_DATA);
609 #endif
610 
611 int	crypto_newsession(crypto_session_t *cses,
612     const struct crypto_session_params *params, int crid);
613 void	crypto_freesession(crypto_session_t cses);
614 #define	CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
615 #define	CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
616 #define	CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
617 #define	CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
618 #define	CRYPTO_SESS_SYNC(sess)	\
619 	((crypto_ses2caps(sess) & CRYPTOCAP_F_SYNC) != 0)
620 int32_t	crypto_get_driverid(device_t dev, size_t session_size, int flags);
621 int	crypto_find_driver(const char *);
622 device_t crypto_find_device_byhid(int hid);
623 int	crypto_getcaps(int hid);
624 int	crypto_unregister_all(uint32_t driverid);
625 int	crypto_dispatch(struct cryptop *crp);
626 #define	CRYPTO_ASYNC_ORDERED	0x1	/* complete in order dispatched */
627 int	crypto_dispatch_async(struct cryptop *crp, int flags);
628 void	crypto_dispatch_batch(struct cryptopq *crpq, int flags);
629 #define	CRYPTO_SYMQ	0x1
630 int	crypto_unblock(uint32_t, int);
631 void	crypto_done(struct cryptop *crp);
632 
633 struct cryptop *crypto_clonereq(struct cryptop *crp, crypto_session_t cses,
634     int how);
635 void	crypto_destroyreq(struct cryptop *crp);
636 void	crypto_initreq(struct cryptop *crp, crypto_session_t cses);
637 void	crypto_freereq(struct cryptop *crp);
638 struct cryptop *crypto_getreq(crypto_session_t cses, int how);
639 
640 extern	int crypto_usercrypto;		/* userland may do crypto requests */
641 extern	int crypto_devallowsoft;	/* only use hardware crypto */
642 
643 #ifdef SYSCTL_DECL
644 SYSCTL_DECL(_kern_crypto);
645 #endif
646 
647 /* Helper routines for drivers to initialize auth contexts for HMAC. */
648 struct auth_hash;
649 
650 void	hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
651     void *auth_ctx);
652 void	hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
653     void *auth_ctx);
654 
655 /*
656  * Crypto-related utility routines used mainly by drivers.
657  *
658  * Similar to m_copyback/data, *_copyback copy data from the 'src'
659  * buffer into the crypto request's data buffer while *_copydata copy
660  * data from the crypto request's data buffer into the the 'dst'
661  * buffer.
662  */
663 void	crypto_copyback(struct cryptop *crp, int off, int size,
664 	    const void *src);
665 void	crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
666 int	crypto_apply(struct cryptop *crp, int off, int len,
667 	    int (*f)(void *, const void *, u_int), void *arg);
668 void	*crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
669 	    size_t len);
670 
671 int	crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
672 	    int (*f)(void *, const void *, u_int), void *arg);
673 void	*crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
674 	    size_t skip, size_t len);
675 size_t	crypto_buffer_len(struct crypto_buffer *cb);
676 void	crypto_cursor_init(struct crypto_buffer_cursor *cc,
677 	    const struct crypto_buffer *cb);
678 void	crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
679 void	*crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len);
680 void	crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
681 	    const void *vsrc);
682 void	crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
683 	    void *vdst);
684 void	crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
685 	    void *vdst);
686 
687 static __inline void
crypto_cursor_copy(const struct crypto_buffer_cursor * fromc,struct crypto_buffer_cursor * toc)688 crypto_cursor_copy(const struct crypto_buffer_cursor *fromc,
689     struct crypto_buffer_cursor *toc)
690 {
691 	memcpy(toc, fromc, sizeof(*toc));
692 }
693 
694 static __inline void
crypto_read_iv(struct cryptop * crp,void * iv)695 crypto_read_iv(struct cryptop *crp, void *iv)
696 {
697 	const struct crypto_session_params *csp;
698 
699 	csp = crypto_get_params(crp->crp_session);
700 	if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
701 		memcpy(iv, crp->crp_iv, csp->csp_ivlen);
702 	else
703 		crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
704 }
705 
706 static __inline size_t
ccm_max_payload_length(const struct crypto_session_params * csp)707 ccm_max_payload_length(const struct crypto_session_params *csp)
708 {
709 	/* RFC 3160 */
710 	const u_int L = 15 - csp->csp_ivlen;
711 
712 	switch (L) {
713 	case 2:
714 		return (0xffff);
715 	case 3:
716 		return (0xffffff);
717 #ifdef __LP64__
718 	case 4:
719 		return (0xffffffff);
720 	case 5:
721 		return (0xffffffffff);
722 	case 6:
723 		return (0xffffffffffff);
724 	case 7:
725 		return (0xffffffffffffff);
726 	default:
727 		return (0xffffffffffffffff);
728 #else
729 	default:
730 		return (0xffffffff);
731 #endif
732 	}
733 }
734 
735 #endif /* _KERNEL */
736 #endif /* _CRYPTO_CRYPTO_H_ */
737