xref: /freebsd/share/man/man9/crypto_buffer.9 (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
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 February 11, 2022
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 void
99.Fo crypto_cursor_copy
100.Fa "const struct crypto_buffer_cursor *fromc"
101.Fa "struct crypto_buffer_cursor *toc"
102.Fc
103.Ft bool
104.Fn CRYPTO_HAS_OUTPUT_BUFFER "struct cryptop *crp"
105.Sh DESCRIPTION
106Symmetric cryptographic requests use data buffers to describe the data to
107be modified.
108Requests can either specify a single data buffer whose contents are modified
109in place,
110or requests may specify separate data buffers for input and output.
111.Vt struct crypto_buffer
112provides an abstraction that permits cryptographic requests to operate on
113different types of buffers.
114.Vt struct crypto_cursor
115allows cryptographic drivers to iterate over a data buffer.
116.Pp
117.Fn CRYPTO_HAS_OUTPUT_BUFFER
118returns true if
119.Fa crp
120uses separate buffers for input and output and false if
121.Fa crp
122uses a single buffer.
123.Pp
124.Fn crypto_buffer_len
125returns the length of data buffer
126.Fa cb
127in bytes.
128.Pp
129.Fn crypto_apply_buf
130invokes a caller-supplied function
131to a region of the data buffer
132.Fa cb .
133The function
134.Fa f
135is called one or more times.
136For each invocation,
137the first argument to
138.Fa f
139is the value of
140.Fa arg
141passed to
142.Fn crypto_apply_buf .
143The second and third arguments to
144.Fa f
145are a pointer and length to a segment of the buffer mapped into the kernel.
146The function is called enough times to cover the
147.Fa len
148bytes of the data buffer which starts at an offset
149.Fa off .
150If any invocation of
151.Fa f
152returns a non-zero value,
153.Fn crypto_apply_buf
154immediately returns that value without invoking
155.Fa f
156on any remaining segments of the region,
157otherwise
158.Fn crypto_apply_buf
159returns the value from the final call to
160.Fa f .
161.Fn crypto_apply
162invokes the callback
163.Fa f
164on a region of the input data buffer for
165.Fa crp .
166.Pp
167.Fn crypto_buffer_contiguous_subsegment
168attempts to locate a single, virtually-contiguous segment of the data buffer
169.Fa cb .
170The segment must be
171.Fa len
172bytes long and start at an offset of
173.Fa skip
174bytes.
175If a segment is found,
176a pointer to the start of the segment is returned.
177Otherwise,
178.Dv NULL
179is returned.
180.Fn crypto_contiguous_subsegment
181attempts to locate a single, virtually-contiguous segment in the input data
182buffer for
183.Fa crp .
184.Ss Data Buffers
185Data buffers are described by an instance of
186.Vt struct crypto buffer .
187The
188.Fa cb_type
189member contains the type of the data buffer.
190The following types are supported:
191.Bl -tag -width "  CRYPTO_BUF_CONTIG"
192.It Dv CRYPTO_BUF_NONE
193An invalid buffer.
194Used to mark the output buffer when a crypto request uses a single data buffer.
195.It Dv CRYPTO_BUF_CONTIG
196An array of bytes mapped into the kernel's address space.
197.It Dv CRYPTO_BUF_UIO
198A scatter/gather list of kernel buffers as described in
199.Xr uio 9 .
200.It Dv CRYPTO_BUF_MBUF
201A chain of network memory buffers as described in
202.Xr mbuf 9 .
203.It Dv CRYPTO_BUF_SINGLE_MBUF
204A single network memory buffer as described in
205.Xr mbuf 9 .
206.It Dv CRYPTO_BUF_VMPAGE
207A scatter/gather list of
208.Vt vm_page_t
209structures describing pages in the kernel's address space.
210This buffer type is only available if
211.Dv CRYPTO_HAS_VMPAGE
212is true.
213.El
214.Pp
215The structure also contains the following type-specific fields:
216.Bl -tag -width "  cb_vm_page_offset"
217.It Fa cb_buf
218A pointer to the start of a
219.Dv CRYPTO_BUF_CONTIG
220data buffer.
221.It Fa cb_buf_len
222The length of a
223.Dv CRYPTO_BUF_CONTIG
224data buffer
225.It Fa cb_mbuf
226A pointer to a
227.Vt struct mbuf
228for
229.Dv CRYPTO_BUF_MBUF
230and
231.Dv CRYPTO_BUF_SINGLE_MBUF .
232.It Fa cb_uio
233A pointer to a
234.Vt struct uio
235for
236.Dv CRYPTO_BUF_UIO .
237.It Fa cb_vm_page
238A pointer to an array of
239.Vt struct vm_page
240for
241.Dv CRYPTO_BUF_VMPAGE .
242.It Fa cb_vm_page_len
243The total amount of data included in the
244.Fa cb_vm_page
245array, in bytes.
246.It Fa cb_vm_page_offset
247Offset in bytes in the first page of
248.Fa cb_vm_page
249where valid data begins.
250.El
251.Ss Cursors
252Cursors provide a mechanism for iterating over a data buffer.
253They are primarily intended for use in software drivers which access data
254buffers via virtual addresses.
255.Pp
256.Fn crypto_cursor_init
257initializes the cursor
258.Fa cc
259to reference the start of the data buffer
260.Fa cb .
261.Pp
262.Fn crypto_cursor_advance
263advances the cursor
264.Fa amount
265bytes forward in the data buffer.
266.Pp
267.Fn crypto_cursor_copyback
268copies
269.Fa size
270bytes from the local buffer pointed to by
271.Fa src
272into the data buffer associated with
273.Fa cc .
274The bytes are written to the current position of
275.Fa cc ,
276and the cursor is then advanced by
277.Fa size
278bytes.
279.Pp
280.Fn crypto_cursor_copydata
281copies
282.Fa size
283bytes out of the data buffer associated with
284.Fa cc
285into a local buffer pointed to by
286.Fa dst .
287The bytes are read from the current position of
288.Fa cc ,
289and the cursor is then advanced by
290.Fa size
291bytes.
292.Pp
293.Fn crypto_cursor_copydata_noadv
294is similar to
295.Fn crypto_cursor_copydata
296except that it does not change the current position of
297.Fa cc .
298.Pp
299.Fn crypto_cursor_segment
300returns the start of the virtually-contiguous segment at the current position of
301.Fa cc .
302The length of the segment is stored in
303.Fa len .
304.Sh RETURN VALUES
305.Fn crypto_apply
306and
307.Fn crypto_apply_buf
308return the return value from the caller-supplied callback function.
309.Pp
310.Fn crypto_buffer_contiguous_subsegment ,
311.Fn crypto_contiguous_subsegment ,
312and
313.Fn crypto_cursor_segment
314return a pointer to a contiguous segment or
315.Dv NULL .
316.Pp
317.Fn crypto_buffer_len
318returns the length of a buffer in bytes.
319.Pp
320.Fn crypto_cursor_seglen
321returns the length in bytes of a contiguous segment.
322.Pp
323.Fn crypto_cursor_copy
324makes a deep copy of the cursor
325.Fa fromc .
326The two copies do not share any state and can thus be used
327independently.
328.Pp
329.Fn CRYPTO_HAS_OUTPUT_BUFFER
330returns true if the request uses a separate output buffer.
331.Sh SEE ALSO
332.Xr ipsec 4 ,
333.Xr crypto 7 ,
334.Xr bus_dma 9 ,
335.Xr crypto 9 ,
336.Xr crypto_driver 9 ,
337.Xr crypto_request 9 ,
338.Xr crypto_session 9 ,
339.Xr mbuf 9 ,
340.Xr uio 9
341.Sh HISTORY
342The
343.Nm
344functions first appeared in
345.Fx 13 .
346.Sh AUTHORS
347The
348.Nm
349functions and this manual page were written by
350.An John Baldwin Aq Mt jhb@FreeBSD.org .
351