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