2 /* $OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $ */
5 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
6 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
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.
12 * Copyright (c) 2000 Angelos D. Keromytis
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.
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
25 * Copyright (c) 2001 Theo de Raadt
26 * Copyright (c) 2014-2021 The FreeBSD Foundation
27 * All rights reserved.
29 * Portions of this software were developed by John-Mark Gurney
30 * under sponsorship of the FreeBSD Foundation and
31 * Rubicon Communications, LLC (Netgate).
33 * Portions of this software were developed by Ararat River
34 * Consulting, LLC under sponsorship of the FreeBSD Foundation.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. The name of the author may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 * Effort sponsored in part by the Defense Advanced Research Projects
60 * Agency (DARPA) and Air Force Research Laboratory, Air Force
61 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
65 #ifndef _CRYPTO_CRYPTO_H_
66 #define _CRYPTO_CRYPTO_H_
68 #include <sys/ioccom.h>
71 #include <opencrypto/_cryptodev.h>
72 #include <sys/_task.h>
73 #include <sys/libkern.h>
77 /* Some initial values */
78 #define CRYPTO_DRIVERS_INITIAL 4
81 #define NULL_HASH_LEN 16
82 #define SHA1_HASH_LEN 20
83 #define RIPEMD160_HASH_LEN 20
84 #define SHA2_224_HASH_LEN 28
85 #define SHA2_256_HASH_LEN 32
86 #define SHA2_384_HASH_LEN 48
87 #define SHA2_512_HASH_LEN 64
88 #define AES_GMAC_HASH_LEN 16
89 #define POLY1305_HASH_LEN 16
90 #define AES_CBC_MAC_HASH_LEN 16
91 /* Maximum hash algorithm result length */
92 #define HASH_MAX_LEN SHA2_512_HASH_LEN /* Keep this updated */
94 #define SHA1_BLOCK_LEN 64
95 #define RIPEMD160_BLOCK_LEN 64
96 #define SHA2_224_BLOCK_LEN 64
97 #define SHA2_256_BLOCK_LEN 64
98 #define SHA2_384_BLOCK_LEN 128
99 #define SHA2_512_BLOCK_LEN 128
100 #define POLY1305_BLOCK_LEN 16
103 #define NULL_HMAC_BLOCK_LEN 64
104 /* Maximum HMAC block length */
105 #define HMAC_MAX_BLOCK_LEN SHA2_512_BLOCK_LEN /* Keep this updated */
106 #define HMAC_IPAD_VAL 0x36
107 #define HMAC_OPAD_VAL 0x5C
108 /* HMAC Key Length */
109 #define AES_128_GMAC_KEY_LEN 16
110 #define AES_192_GMAC_KEY_LEN 24
111 #define AES_256_GMAC_KEY_LEN 32
112 #define AES_128_CBC_MAC_KEY_LEN 16
113 #define AES_192_CBC_MAC_KEY_LEN 24
114 #define AES_256_CBC_MAC_KEY_LEN 32
116 #define POLY1305_KEY_LEN 32
118 /* Encryption algorithm block sizes */
119 #define NULL_BLOCK_LEN 4 /* IPsec to maintain alignment */
120 #define RIJNDAEL128_BLOCK_LEN 16
121 #define AES_BLOCK_LEN 16
122 #define AES_ICM_BLOCK_LEN 1
123 #define CAMELLIA_BLOCK_LEN 16
124 #define CHACHA20_NATIVE_BLOCK_LEN 64
125 #define EALG_MAX_BLOCK_LEN CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
129 #define AES_GCM_IV_LEN 12
130 #define AES_CCM_IV_LEN 12
131 #define AES_XTS_IV_LEN 8
132 #define AES_XTS_ALPHA 0x87 /* GF(2^128) generator polynomial */
133 #define CHACHA20_POLY1305_IV_LEN 12
134 #define XCHACHA20_POLY1305_IV_LEN 24
136 /* Min and Max Encryption Key Sizes */
137 #define NULL_MIN_KEY 0
138 #define NULL_MAX_KEY 256 /* 2048 bits, max key */
139 #define RIJNDAEL_MIN_KEY 16
140 #define RIJNDAEL_MAX_KEY 32
141 #define AES_MIN_KEY RIJNDAEL_MIN_KEY
142 #define AES_MAX_KEY RIJNDAEL_MAX_KEY
143 #define AES_XTS_MIN_KEY (2 * AES_MIN_KEY)
144 #define AES_XTS_MAX_KEY (2 * AES_MAX_KEY)
145 #define CAMELLIA_MIN_KEY 16
146 #define CAMELLIA_MAX_KEY 32
147 #define CHACHA20_POLY1305_KEY 32
148 #define XCHACHA20_POLY1305_KEY 32
150 /* Maximum hash algorithm result length */
151 #define AALG_MAX_RESULT_LEN 64 /* Keep this updated */
153 #define CRYPTO_ALGORITHM_MIN 1
154 #define CRYPTO_DES_CBC 1
155 #define CRYPTO_3DES_CBC 2
156 #define CRYPTO_BLF_CBC 3
157 #define CRYPTO_CAST_CBC 4
158 #define CRYPTO_SKIPJACK_CBC 5
159 #define CRYPTO_MD5_HMAC 6
160 #define CRYPTO_SHA1_HMAC 7
161 #define CRYPTO_RIPEMD160_HMAC 8
162 #define CRYPTO_MD5_KPDK 9
163 #define CRYPTO_SHA1_KPDK 10
164 #define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */
165 #define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */
166 #define CRYPTO_ARC4 12
167 #define CRYPTO_MD5 13
168 #define CRYPTO_SHA1 14
169 #define CRYPTO_NULL_HMAC 15
170 #define CRYPTO_NULL_CBC 16
171 #define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */
172 #define CRYPTO_SHA2_256_HMAC 18
173 #define CRYPTO_SHA2_384_HMAC 19
174 #define CRYPTO_SHA2_512_HMAC 20
175 #define CRYPTO_CAMELLIA_CBC 21
176 #define CRYPTO_AES_XTS 22
177 #define CRYPTO_AES_ICM 23 /* commonly known as CTR mode */
178 #define CRYPTO_AES_NIST_GMAC 24 /* GMAC only */
179 #define CRYPTO_AES_NIST_GCM_16 25 /* 16 byte ICV */
181 #define CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
182 #define CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
183 #define CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
185 #define CRYPTO_BLAKE2B 29 /* Blake2b hash */
186 #define CRYPTO_BLAKE2S 30 /* Blake2s hash */
187 #define CRYPTO_CHACHA20 31 /* Chacha20 stream cipher */
188 #define CRYPTO_SHA2_224_HMAC 32
189 #define CRYPTO_RIPEMD160 33
190 #define CRYPTO_SHA2_224 34
191 #define CRYPTO_SHA2_256 35
192 #define CRYPTO_SHA2_384 36
193 #define CRYPTO_SHA2_512 37
194 #define CRYPTO_POLY1305 38
195 #define CRYPTO_AES_CCM_CBC_MAC 39 /* auth side */
196 #define CRYPTO_AES_CCM_16 40 /* cipher side */
197 #define CRYPTO_CHACHA20_POLY1305 41 /* combined AEAD cipher per RFC 8439 */
198 #define CRYPTO_XCHACHA20_POLY1305 42
199 #define CRYPTO_ALGORITHM_MAX 42 /* Keep updated - see below */
201 #define CRYPTO_ALGO_VALID(x) ((x) >= CRYPTO_ALGORITHM_MIN && \
202 (x) <= CRYPTO_ALGORITHM_MAX)
205 * Crypto driver/device flags. They can set in the crid
206 * parameter when creating a session or submitting a key
207 * op to affect the device/driver assigned. If neither
208 * of these are specified then the crid is assumed to hold
209 * the driver id of an existing (and suitable) device that
210 * must be used to satisfy the request.
212 #define CRYPTO_FLAG_HARDWARE 0x01000000 /* hardware accelerated */
213 #define CRYPTO_FLAG_SOFTWARE 0x02000000 /* software implementation */
215 /* Does the kernel support vmpage buffers on this platform? */
217 #define CRYPTO_MAY_HAVE_VMPAGE 1
219 #define CRYPTO_MAY_HAVE_VMPAGE ( PMAP_HAS_DMAP )
221 /* Does the currently running system support vmpage buffers on this platform? */
222 #define CRYPTO_HAS_VMPAGE ( PMAP_HAS_DMAP )
226 uint32_t cipher; /* ie. CRYPTO_AES_CBC */
227 uint32_t mac; /* ie. CRYPTO_SHA2_256_HMAC */
229 uint32_t keylen; /* cipher key */
231 int mackeylen; /* mac key */
234 uint32_t ses; /* returns: session # */
238 * session and crypt _op structs are used by userspace programs to interact
239 * with /dev/crypto. Confusingly, the internal kernel interface is named
240 * "cryptop" (no underscore).
243 uint32_t cipher; /* ie. CRYPTO_AES_CBC */
244 uint32_t mac; /* ie. CRYPTO_SHA2_256_HMAC */
246 uint32_t keylen; /* cipher key */
248 int mackeylen; /* mac key */
251 uint32_t ses; /* returns: session # */
252 int crid; /* driver id + flags (rw) */
253 int ivlen; /* length of nonce/IV */
254 int maclen; /* length of MAC/tag */
255 int pad[2]; /* for future expansion */
260 uint16_t op; /* i.e. COP_ENCRYPT */
261 #define COP_ENCRYPT 1
262 #define COP_DECRYPT 2
264 #define COP_F_CIPHER_FIRST 0x0001 /* Cipher before MAC. */
265 #define COP_F_BATCH 0x0008 /* Batch op if possible */
267 const void *src; /* become iov[] inside kernel */
269 void *mac; /* must be big enough for chosen MAC */
273 /* op and flags the same as crypt_op */
276 uint16_t op; /* i.e. COP_ENCRYPT */
281 const void *src; /* become iov[] inside kernel */
283 const void *aad; /* additional authenticated data */
284 void *tag; /* must fit for chosen TAG length */
289 * Parameters for looking up a crypto driver/device by
290 * device name or by id. The latter are returned for
291 * created sessions (crid) and completed key operations.
293 struct crypt_find_op {
294 int crid; /* driver id + flags */
295 char name[32]; /* device/driver name */
298 #define CIOCGSESSION _IOWR('c', 101, struct session_op)
299 #define CIOCFSESSION _IOW('c', 102, uint32_t)
300 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
301 #define CIOCGSESSION2 _IOWR('c', 106, struct session2_op)
302 #define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op)
303 #define CIOCCRYPTAEAD _IOWR('c', 109, struct crypt_aead)
306 uint64_t cs_ops; /* symmetric crypto ops submitted */
307 uint64_t cs_errs; /* symmetric crypto ops that failed */
308 uint64_t cs_kops; /* asymetric/key ops submitted */
309 uint64_t cs_kerrs; /* asymetric/key ops that failed */
310 uint64_t cs_intrs; /* crypto swi thread activations */
311 uint64_t cs_rets; /* crypto return thread activations */
312 uint64_t cs_blocks; /* symmetric op driver block */
313 uint64_t cs_kblocks; /* symmetric op driver block */
319 * Return values for cryptodev_probesession methods.
321 #define CRYPTODEV_PROBE_HARDWARE (-100)
322 #define CRYPTODEV_PROBE_ACCEL_SOFTWARE (-200)
323 #define CRYPTODEV_PROBE_SOFTWARE (-500)
326 #define CRYPTDEB(s, ...) do { \
327 printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__); \
330 #define CRYPTDEB(...) do { } while (0)
333 struct crypto_session_params {
334 int csp_mode; /* Type of operations to perform. */
336 #define CSP_MODE_NONE 0
337 #define CSP_MODE_COMPRESS 1 /* Compression/decompression. */
338 #define CSP_MODE_CIPHER 2 /* Encrypt/decrypt. */
339 #define CSP_MODE_DIGEST 3 /* Compute/verify digest. */
340 #define CSP_MODE_AEAD 4 /* Combined auth/encryption. */
341 #define CSP_MODE_ETA 5 /* IPsec style encrypt-then-auth */
345 #define CSP_F_SEPARATE_OUTPUT 0x0001 /* Requests can use separate output */
346 #define CSP_F_SEPARATE_AAD 0x0002 /* Requests can use separate AAD */
347 #define CSP_F_ESN 0x0004 /* Requests can use seperate ESN field */
349 int csp_ivlen; /* IV length in bytes. */
352 int csp_cipher_klen; /* Key length in bytes. */
353 const void *csp_cipher_key;
356 int csp_auth_klen; /* Key length in bytes. */
357 const void *csp_auth_key;
358 int csp_auth_mlen; /* Number of digest bytes to use.
362 enum crypto_buffer_type {
368 CRYPTO_BUF_SINGLE_MBUF,
369 CRYPTO_BUF_LAST = CRYPTO_BUF_SINGLE_MBUF
373 * Description of a data buffer for a request. Requests can either
374 * have a single buffer that is modified in place or separate input
375 * and output buffers.
377 struct crypto_buffer {
383 struct mbuf *cb_mbuf;
385 vm_page_t *cb_vm_page;
387 int cb_vm_page_offset;
391 enum crypto_buffer_type cb_type;
395 * A cursor is used to iterate through a crypto request data buffer.
397 struct crypto_buffer_cursor {
400 struct mbuf *cc_mbuf;
401 struct iovec *cc_iov;
402 vm_page_t *cc_vmpage;
404 /* Optional bytes of valid data remaining */
407 * Optional offset within the current buffer segment where
411 enum crypto_buffer_type cc_type;
414 /* Structure describing complete operation */
416 TAILQ_ENTRY(cryptop) crp_next;
418 struct task crp_task;
420 crypto_session_t crp_session; /* Session */
421 int crp_olen; /* Result total length */
424 * Error type (zero means no error).
425 * All error codes except EAGAIN
426 * indicate possible data corruption (as in,
427 * the data have been touched). On all
428 * errors, the crp_session may have changed
429 * (reset to a new one), so the caller
430 * should always check and use the new
431 * value on future requests.
433 #define crp_startcopy crp_flags
436 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
437 #define CRYPTO_F_DONE 0x0020 /* Operation completed */
438 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */
439 #define CRYPTO_F_ASYNC_ORDERED 0x0100 /* Completions must happen in order */
440 #define CRYPTO_F_IV_SEPARATE 0x0200 /* Use crp_iv[] as IV. */
444 struct crypto_buffer crp_buf;
445 struct crypto_buffer crp_obuf;
447 void *crp_aad; /* AAD buffer. */
448 int crp_aad_start; /* Location of AAD. */
449 int crp_aad_length; /* 0 => no AAD. */
450 uint8_t crp_esn[4]; /* high-order ESN */
452 int crp_iv_start; /* Location of IV. IV length is from
455 int crp_payload_start; /* Location of ciphertext. */
456 int crp_payload_output_start;
457 int crp_payload_length;
458 int crp_digest_start; /* Location of MAC/tag. Length is
462 uint8_t crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
464 const void *crp_cipher_key; /* New cipher key if non-NULL. */
465 const void *crp_auth_key; /* New auth key if non-NULL. */
466 #define crp_endcopy crp_opaque
468 void *crp_opaque; /* Opaque pointer, passed along */
470 int (*crp_callback)(struct cryptop *); /* Callback function */
472 struct bintime crp_tstamp; /* performance time stamp */
473 uint32_t crp_seq; /* used for ordered dispatch */
474 uint32_t crp_retw_id; /*
475 * the return worker to be used,
476 * used for ordered dispatch
480 TAILQ_HEAD(cryptopq, cryptop);
483 _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
486 cb->cb_buf_len = len;
487 cb->cb_type = CRYPTO_BUF_CONTIG;
491 _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
494 cb->cb_type = CRYPTO_BUF_MBUF;
498 _crypto_use_single_mbuf(struct crypto_buffer *cb, struct mbuf *m)
501 cb->cb_type = CRYPTO_BUF_SINGLE_MBUF;
505 _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
508 cb->cb_vm_page = pages;
509 cb->cb_vm_page_len = len;
510 cb->cb_vm_page_offset = offset;
511 cb->cb_type = CRYPTO_BUF_VMPAGE;
515 _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
518 cb->cb_type = CRYPTO_BUF_UIO;
522 crypto_use_buf(struct cryptop *crp, void *buf, int len)
524 _crypto_use_buf(&crp->crp_buf, buf, len);
528 crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
530 _crypto_use_mbuf(&crp->crp_buf, m);
534 crypto_use_single_mbuf(struct cryptop *crp, struct mbuf *m)
536 _crypto_use_single_mbuf(&crp->crp_buf, m);
540 crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
542 _crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
546 crypto_use_uio(struct cryptop *crp, struct uio *uio)
548 _crypto_use_uio(&crp->crp_buf, uio);
552 crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
554 _crypto_use_buf(&crp->crp_obuf, buf, len);
558 crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
560 _crypto_use_mbuf(&crp->crp_obuf, m);
564 crypto_use_output_single_mbuf(struct cryptop *crp, struct mbuf *m)
566 _crypto_use_single_mbuf(&crp->crp_obuf, m);
570 crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
573 _crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
577 crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
579 _crypto_use_uio(&crp->crp_obuf, uio);
582 #define CRYPTO_HAS_OUTPUT_BUFFER(crp) \
583 ((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
585 /* Flags in crp_op. */
586 #define CRYPTO_OP_DECRYPT 0x0
587 #define CRYPTO_OP_ENCRYPT 0x1
588 #define CRYPTO_OP_IS_ENCRYPT(op) ((op) & CRYPTO_OP_ENCRYPT)
589 #define CRYPTO_OP_COMPUTE_DIGEST 0x0
590 #define CRYPTO_OP_VERIFY_DIGEST 0x2
591 #define CRYPTO_OP_DECOMPRESS CRYPTO_OP_DECRYPT
592 #define CRYPTO_OP_COMPRESS CRYPTO_OP_ENCRYPT
593 #define CRYPTO_OP_IS_COMPRESS(op) ((op) & CRYPTO_OP_COMPRESS)
596 * Hints passed to process methods.
598 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
600 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
601 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
602 void *crypto_get_driver_session(crypto_session_t crypto_session);
603 const struct crypto_session_params *crypto_get_params(
604 crypto_session_t crypto_session);
605 const struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
606 const struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
608 #ifdef MALLOC_DECLARE
609 MALLOC_DECLARE(M_CRYPTO_DATA);
612 int crypto_newsession(crypto_session_t *cses,
613 const struct crypto_session_params *params, int crid);
614 void crypto_freesession(crypto_session_t cses);
615 #define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE
616 #define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE
617 #define CRYPTOCAP_F_SYNC 0x04000000 /* operates synchronously */
618 #define CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
619 #define CRYPTO_SESS_SYNC(sess) \
620 ((crypto_ses2caps(sess) & CRYPTOCAP_F_SYNC) != 0)
621 int32_t crypto_get_driverid(device_t dev, size_t session_size, int flags);
622 int crypto_find_driver(const char *);
623 device_t crypto_find_device_byhid(int hid);
624 int crypto_getcaps(int hid);
625 int crypto_unregister_all(uint32_t driverid);
626 int crypto_dispatch(struct cryptop *crp);
627 #define CRYPTO_ASYNC_ORDERED 0x1 /* complete in order dispatched */
628 int crypto_dispatch_async(struct cryptop *crp, int flags);
629 void crypto_dispatch_batch(struct cryptopq *crpq, int flags);
630 #define CRYPTO_SYMQ 0x1
631 int crypto_unblock(uint32_t, int);
632 void crypto_done(struct cryptop *crp);
634 struct cryptop *crypto_clonereq(struct cryptop *crp, crypto_session_t cses,
636 void crypto_destroyreq(struct cryptop *crp);
637 void crypto_initreq(struct cryptop *crp, crypto_session_t cses);
638 void crypto_freereq(struct cryptop *crp);
639 struct cryptop *crypto_getreq(crypto_session_t cses, int how);
641 extern int crypto_usercrypto; /* userland may do crypto requests */
642 extern int crypto_devallowsoft; /* only use hardware crypto */
645 SYSCTL_DECL(_kern_crypto);
648 /* Helper routines for drivers to initialize auth contexts for HMAC. */
651 void hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
653 void hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
657 * Crypto-related utility routines used mainly by drivers.
659 * Similar to m_copyback/data, *_copyback copy data from the 'src'
660 * buffer into the crypto request's data buffer while *_copydata copy
661 * data from the crypto request's data buffer into the the 'dst'
664 void crypto_copyback(struct cryptop *crp, int off, int size,
666 void crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
667 int crypto_apply(struct cryptop *crp, int off, int len,
668 int (*f)(void *, const void *, u_int), void *arg);
669 void *crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
672 int crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
673 int (*f)(void *, const void *, u_int), void *arg);
674 void *crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
675 size_t skip, size_t len);
676 size_t crypto_buffer_len(struct crypto_buffer *cb);
677 void crypto_cursor_init(struct crypto_buffer_cursor *cc,
678 const struct crypto_buffer *cb);
679 void crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
680 void *crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len);
681 void crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
683 void crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
685 void crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
689 crypto_cursor_copy(const struct crypto_buffer_cursor *fromc,
690 struct crypto_buffer_cursor *toc)
692 memcpy(toc, fromc, sizeof(*toc));
696 crypto_read_iv(struct cryptop *crp, void *iv)
698 const struct crypto_session_params *csp;
700 csp = crypto_get_params(crp->crp_session);
701 if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
702 memcpy(iv, crp->crp_iv, csp->csp_ivlen);
704 crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
707 static __inline size_t
708 ccm_max_payload_length(const struct crypto_session_params *csp)
711 const u_int L = 15 - csp->csp_ivlen;
722 return (0xffffffffff);
724 return (0xffffffffffff);
726 return (0xffffffffffffff);
728 return (0xffffffffffffffff);
737 #endif /* _CRYPTO_CRYPTO_H_ */