xref: /freebsd/share/man/man9/crypto_buffer.9 (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1.\" Copyright (c) 2020, Chelsio Inc
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions are met:
5.\"
6.\" 1. Redistributions of source code must retain the above copyright notice,
7.\"    this list of conditions and the following disclaimer.
8.\"
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" 3. Neither the name of the Chelsio Inc nor the names of its
14.\"    contributors may be used to endorse or promote products derived from
15.\"    this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27.\" POSSIBILITY OF SUCH DAMAGE.
28.\"
29.\" * Other names and brands may be claimed as the property of others.
30.\"
31.\" $FreeBSD$
32.\"
33.Dd June 16, 2021
34.Dt CRYPTO_BUFFER 9
35.Os
36.Sh NAME
37.Nm crypto_buffer
38.Nd symmetric cryptographic request buffers
39.Sh SYNOPSIS
40.In opencrypto/cryptodev.h
41.Ft int
42.Fo crypto_apply
43.Fa "struct cryptop *crp"
44.Fa "int off"
45.Fa "int len"
46.Fa "int (*f)(void *, void *, u_int)"
47.Fa "void *arg"
48.Fc
49.Ft int
50.Fo crypto_apply_buf
51.Fa "struct crypto_buffer *cb"
52.Fa "int off"
53.Fa "int len"
54.Fa "int (*f)(void *, void *, u_int)"
55.Fa "void *arg"
56.Fc
57.Ft void *
58.Fo crypto_buffer_contiguous_subsegment
59.Fa "struct crypto_buffer *cb"
60.Fa "size_t skip"
61.Fa "size_t len"
62.Fc
63.Ft size_t
64.Fn crypto_buffer_len "struct crypto_buffer *cb"
65.Ft void *
66.Fo crypto_contiguous_subsegment
67.Fa "struct cryptop *crp"
68.Fa "size_t skip"
69.Fa "size_t len"
70.Fc
71.Ft void
72.Fo crypto_cursor_init
73.Fa "struct crypto_buffer_cursor *cc"
74.Fa "const struct crypto_buffer *cb"
75.Fc
76.Ft void
77.Fn crypto_cursor_advance "struct crypto_buffer_cursor *cc" "size_t amount"
78.Ft void
79.Fo crypto_cursor_copyback
80.Fa "struct crypto_buffer_cursor *cc"
81.Fa "int size"
82.Fa "const void *src"
83.Fc
84.Ft void
85.Fo crypto_cursor_copydata
86.Fa "struct crypto_buffer_cursor *cc"
87.Fa "int size"
88.Fa "void *dst"
89.Fc
90.Ft void
91.Fo crypto_cursor_copydata_noadv
92.Fa "struct crypto_buffer_cursor *cc"
93.Fa "int size"
94.Fa "void *dst"
95.Fc
96.Ft void *
97.Fn crypto_cursor_segment "struct crypto_buffer_cursor *cc" "size_t *len"
98.Ft bool
99.Fn CRYPTO_HAS_OUTPUT_BUFFER "struct cryptop *crp"
100.Sh DESCRIPTION
101Symmetric cryptographic requests use data buffers to describe the data to
102be modified.
103Requests can either specify a single data buffer whose contents are modified
104in place,
105or requests may specify separate data buffers for input and output.
106.Vt struct crypto_buffer
107provides an abstraction that permits cryptographic requests to operate on
108different types of buffers.
109.Vt struct crypto_cursor
110allows cryptographic drivers to iterate over a data buffer.
111.Pp
112.Fn CRYPTO_HAS_OUTPUT_BUFFER
113returns true if
114.Fa crp
115uses separate buffers for input and output and false if
116.Fa crp
117uses a single buffer.
118.Pp
119.Fn crypto_buffer_len
120returns the length of data buffer
121.Fa cb
122in bytes.
123.Pp
124.Fn crypto_apply_buf
125invokes a caller-supplied function
126to a region of the data buffer
127.Fa cb .
128The function
129.Fa f
130is called one or more times.
131For each invocation,
132the first argument to
133.Fa f
134is the value of
135.Fa arg
136passed to
137.Fn crypto_apply_buf .
138The second and third arguments to
139.Fa f
140are a pointer and length to a segment of the buffer mapped into the kernel.
141The function is called enough times to cover the
142.Fa len
143bytes of the data buffer which starts at an offset
144.Fa off .
145If any invocation of
146.Fa f
147returns a non-zero value,
148.Fn crypto_apply_buf
149immediately returns that value without invoking
150.Fa f
151on any remaining segments of the region,
152otherwise
153.Fn crypto_apply_buf
154returns the value from the final call to
155.Fa f .
156.Fn crypto_apply
157invokes the callback
158.Fa f
159on a region of the input data buffer for
160.Fa crp .
161.Pp
162.Fn crypto_buffer_contiguous_subsegment
163attempts to locate a single, virtually-contiguous segment of the data buffer
164.Fa cb .
165The segment must be
166.Fa len
167bytes long and start at an offset of
168.Fa skip
169bytes.
170If a segment is found,
171a pointer to the start of the segment is returned.
172Otherwise,
173.Dv NULL
174is returned.
175.Fn crypto_contiguous_subsegment
176attempts to locate a single, virtually-contiguous segment in the input data
177buffer for
178.Fa crp .
179.Ss Data Buffers
180Data buffers are described by an instance of
181.Vt struct crypto buffer .
182The
183.Fa cb_type
184member contains the type of the data buffer.
185The following types are supported:
186.Bl -tag -width "  CRYPTO_BUF_CONTIG"
187.It Dv CRYPTO_BUF_NONE
188An invalid buffer.
189Used to mark the output buffer when a crypto request uses a single data buffer.
190.It Dv CRYPTO_BUF_CONTIG
191An array of bytes mapped into the kernel's address space.
192.It Dv CRYPTO_BUF_UIO
193A scatter/gather list of kernel buffers as described in
194.Xr uio 9 .
195.It Dv CRYPTO_BUF_MBUF
196A chain of network memory buffers as described in
197.Xr mbuf 9 .
198.It Dv CRYPTO_BUF_SINGLE_MBUF
199A single network memory buffer as described in
200.Xr mbuf 9 .
201.It Dv CRYPTO_BUF_VMPAGE
202A scatter/gather list of
203.Vt vm_page_t
204structures describing pages in the kernel's address space.
205This buffer type is only available if
206.Dv CRYPTO_HAS_VMPAGE
207is true.
208.El
209.Pp
210The structure also contains the following type-specific fields:
211.Bl -tag -width "  cb_vm_page_offset"
212.It Fa cb_buf
213A pointer to the start of a
214.Dv CRYPTO_BUF_CONTIG
215data buffer.
216.It Fa cb_buf_len
217The length of a
218.Dv CRYPTO_BUF_CONTIG
219data buffer
220.It Fa cb_mbuf
221A pointer to a
222.Vt struct mbuf
223for
224.Dv CRYPTO_BUF_MBUF
225and
226.Dv CRYPTO_BUF_SINGLE_MBUF .
227.It Fa cb_uio
228A pointer to a
229.Vt struct uio
230for
231.Dv CRYPTO_BUF_UIO .
232.It Fa cb_vm_page
233A pointer to an array of
234.Vt struct vm_page
235for
236.Dv CRYPTO_BUF_VMPAGE .
237.It Fa cb_vm_page_len
238The total amount of data included in the
239.Fa cb_vm_page
240array, in bytes.
241.It Fa cb_vm_page_offset
242Offset in bytes in the first page of
243.Fa cb_vm_page
244where valid data begins.
245.El
246.Ss Cursors
247Cursors provide a mechanism for iterating over a data buffer.
248They are primarily intended for use in software drivers which access data
249buffers via virtual addresses.
250.Pp
251.Fn crypto_cursor_init
252initializes the cursor
253.Fa cc
254to reference the start of the data buffer
255.Fa cb .
256.Pp
257.Fn crypto_cursor_advance
258advances the cursor
259.Fa amount
260bytes forward in the data buffer.
261.Pp
262.Fn crypto_cursor_copyback
263copies
264.Fa size
265bytes from the local buffer pointed to by
266.Fa src
267into the data buffer associated with
268.Fa cc .
269The bytes are written to the current position of
270.Fa cc ,
271and the cursor is then advanced by
272.Fa size
273bytes.
274.Pp
275.Fn crypto_cursor_copydata
276copies
277.Fa size
278bytes out of the data buffer associated with
279.Fa cc
280into a local buffer pointed to by
281.Fa dst .
282The bytes are read from the current position of
283.Fa cc ,
284and the cursor is then advanced by
285.Fa size
286bytes.
287.Pp
288.Fn crypto_cursor_copydata_noadv
289is similar to
290.Fn crypto_cursor_copydata
291except that it does not change the current position of
292.Fa cc .
293.Pp
294.Fn crypto_cursor_segment
295returns the start of the virtually-contiguous segment at the current position of
296.Fa cc .
297The length of the segment is stored in
298.Fa len .
299.Sh RETURN VALUES
300.Fn crypto_apply
301and
302.Fn crypto_apply_buf
303return the return value from the caller-supplied callback function.
304.Pp
305.Fn crypto_buffer_contiguous_subsegment ,
306.Fn crypto_contiguous_subsegment ,
307and
308.Fn crypto_cursor_segment
309return a pointer to a contiguous segment or
310.Dv NULL .
311.Pp
312.Fn crypto_buffer_len
313returns the length of a buffer in bytes.
314.Pp
315.Fn crypto_cursor_seglen
316returns the length in bytes of a contiguous segment.
317.Pp
318.Fn CRYPTO_HAS_OUTPUT_BUFFER
319returns true if the request uses a separate output buffer.
320.Sh SEE ALSO
321.Xr ipsec 4 ,
322.Xr crypto 7 ,
323.Xr bus_dma 9 ,
324.Xr crypto 9 ,
325.Xr crypto_driver 9 ,
326.Xr crypto_request 9 ,
327.Xr crypto_session 9 ,
328.Xr mbuf 9 ,
329.Xr uio 9
330.Sh HISTORY
331The
332.Nm
333functions first appeared in
334.Fx 13 .
335.Sh AUTHORS
336The
337.Nm
338functions and this manual page were written by
339.An John Baldwin Aq Mt jhb@FreeBSD.org .
340