1c0341432SJohn Baldwin.\" Copyright (c) 2020, Chelsio Inc 2c0341432SJohn Baldwin.\" 3c0341432SJohn Baldwin.\" Redistribution and use in source and binary forms, with or without 4c0341432SJohn Baldwin.\" modification, are permitted provided that the following conditions are met: 5c0341432SJohn Baldwin.\" 6c0341432SJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright notice, 7c0341432SJohn Baldwin.\" this list of conditions and the following disclaimer. 8c0341432SJohn Baldwin.\" 9c0341432SJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright 10c0341432SJohn Baldwin.\" notice, this list of conditions and the following disclaimer in the 11c0341432SJohn Baldwin.\" documentation and/or other materials provided with the distribution. 12c0341432SJohn Baldwin.\" 13c0341432SJohn Baldwin.\" 3. Neither the name of the Chelsio Inc nor the names of its 14c0341432SJohn Baldwin.\" contributors may be used to endorse or promote products derived from 15c0341432SJohn Baldwin.\" this software without specific prior written permission. 16c0341432SJohn Baldwin.\" 17c0341432SJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18c0341432SJohn Baldwin.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19c0341432SJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20c0341432SJohn Baldwin.\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21c0341432SJohn Baldwin.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22c0341432SJohn Baldwin.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23c0341432SJohn Baldwin.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24c0341432SJohn Baldwin.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25c0341432SJohn Baldwin.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26c0341432SJohn Baldwin.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27c0341432SJohn Baldwin.\" POSSIBILITY OF SUCH DAMAGE. 28c0341432SJohn Baldwin.\" 29c0341432SJohn Baldwin.\" * Other names and brands may be claimed as the property of others. 30c0341432SJohn Baldwin.\" 31c0341432SJohn Baldwin.\" $FreeBSD$ 32c0341432SJohn Baldwin.\" 33*68f6800cSMark Johnston.Dd February 8, 2021 34c0341432SJohn Baldwin.Dt CRYPTO_REQUEST 9 35c0341432SJohn Baldwin.Os 36c0341432SJohn Baldwin.Sh NAME 37c0341432SJohn Baldwin.Nm crypto_request 38c0341432SJohn Baldwin.Nd symmetric cryptographic operations 39c0341432SJohn Baldwin.Sh SYNOPSIS 40c0341432SJohn Baldwin.In opencrypto/cryptodev.h 41c0341432SJohn Baldwin.Ft int 42c0341432SJohn Baldwin.Fn crypto_dispatch "struct cryptop *crp" 43*68f6800cSMark Johnston.Ft int 44*68f6800cSMark Johnston.Fn crypto_dispatch_async "struct cryptop *crp" "int flags" 45*68f6800cSMark Johnston.Ft void 46*68f6800cSMark Johnston.Fn crypto_dispatch_batch "struct cryptopq *crpq" "int flags" 47c0341432SJohn Baldwin.Ft void 48946b8f6fSJohn Baldwin.Fn crypto_destroyreq "struct cryptop *crp" 49946b8f6fSJohn Baldwin.Ft void 50c0341432SJohn Baldwin.Fn crypto_freereq "struct cryptop *crp" 51c0341432SJohn Baldwin.Ft "struct cryptop *" 52c0341432SJohn Baldwin.Fn crypto_getreq "crypto_session_t cses" "int how" 539c0e3d3aSJohn Baldwin.Ft void 54946b8f6fSJohn Baldwin.Fn crypto_initreq "crypto_session_t cses" "int how" 55946b8f6fSJohn Baldwin.Ft void 569c0e3d3aSJohn Baldwin.Fn crypto_use_buf "struct cryptop *crp" "void *buf" "int len" 579c0e3d3aSJohn Baldwin.Ft void 589c0e3d3aSJohn Baldwin.Fn crypto_use_mbuf "struct cryptop *crp" "struct mbuf *m" 599c0e3d3aSJohn Baldwin.Ft void 609c0e3d3aSJohn Baldwin.Fn crypto_use_uio "struct cryptop *crp" "struct uio *uio" 619c0e3d3aSJohn Baldwin.Ft void 62e6f6d0c9SAlan Somers.Fn crypto_use_vmpage "struct cryptop *crp" "vm_page_t *pages" "int len" "int offset" 63e6f6d0c9SAlan Somers.Ft void 649c0e3d3aSJohn Baldwin.Fn crypto_use_output_buf "struct cryptop *crp" "void *buf" "int len" 659c0e3d3aSJohn Baldwin.Ft void 669c0e3d3aSJohn Baldwin.Fn crypto_use_output_mbuf "struct cryptop *crp" "struct mbuf *m" 679c0e3d3aSJohn Baldwin.Ft void 689c0e3d3aSJohn Baldwin.Fn crypto_use_output_uio "struct cryptop *crp" "struct uio *uio" 69e6f6d0c9SAlan Somers.Ft void 70e6f6d0c9SAlan Somers.Fn crypto_use_output_vmpage "struct cryptop *crp" "vm_page_t *pages" "int len" "int offset" 71c0341432SJohn Baldwin.Sh DESCRIPTION 72c0341432SJohn BaldwinEach symmetric cryptographic operation in the kernel is described by 73c0341432SJohn Baldwinan instance of 74c0341432SJohn Baldwin.Vt struct cryptop 75c0341432SJohn Baldwinand is associated with an active session. 76c0341432SJohn Baldwin.Pp 77946b8f6fSJohn BaldwinRequests can either be allocated dynamically or use caller-supplied 78946b8f6fSJohn Baldwinstorage. 79946b8f6fSJohn BaldwinDynamically allocated requests should be allocated by 80946b8f6fSJohn Baldwin.Fn crypto_getreq 81946b8f6fSJohn Baldwinand freed by 82946b8f6fSJohn Baldwin.Fn crypto_freereq 83946b8f6fSJohn Baldwinonce the request has completed. 84946b8f6fSJohn BaldwinRequests using caller-supplied storage should be initialized by 85946b8f6fSJohn Baldwin.Fn crypto_initreq 86946b8f6fSJohn Baldwinat the start of each operation and destroyed by 87946b8f6fSJohn Baldwin.Fn crypto_destroyreq 88946b8f6fSJohn Baldwinonce the request has completed. 89946b8f6fSJohn Baldwin.Pp 90946b8f6fSJohn BaldwinFor both 91946b8f6fSJohn Baldwin.Fn crypto_getreq 92946b8f6fSJohn Baldwinand 93946b8f6fSJohn Baldwin.Fn crypto_initreq , 94c0341432SJohn Baldwin.Fa cses 95c0341432SJohn Baldwinis a reference to an active session. 96946b8f6fSJohn BaldwinFor 97946b8f6fSJohn Baldwin.Fn crypto_getreq , 98c0341432SJohn Baldwin.Fa how 99c0341432SJohn Baldwinis passed to 100c0341432SJohn Baldwin.Xr malloc 9 101c0341432SJohn Baldwinand should be set to either 102c0341432SJohn Baldwin.Dv M_NOWAIT 103c0341432SJohn Baldwinor 104c0341432SJohn Baldwin.Dv M_WAITOK . 105946b8f6fSJohn Baldwin.Pp 106946b8f6fSJohn BaldwinOnce a request has been initialized, 107946b8f6fSJohn Baldwinthe caller should set fields in the structure to describe 108c0341432SJohn Baldwinrequest-specific parameters. 109c0341432SJohn BaldwinUnused fields should be left as-is. 110c0341432SJohn Baldwin.Pp 111*68f6800cSMark JohnstonThe 112*68f6800cSMark Johnston.Fn crypto_dispatch , 113*68f6800cSMark Johnston.Fn crypto_dispatch_async , 114*68f6800cSMark Johnstonand 115*68f6800cSMark Johnston.Fn crypto_dispatch_batch 116*68f6800cSMark Johnstonfunctions pass one or more crypto requests to the driver attached to the 117*68f6800cSMark Johnstonrequest's session. 118*68f6800cSMark JohnstonIf there are errors in the request's fields, these functions may return an 119*68f6800cSMark Johnstonerror to the caller. 120c0341432SJohn BaldwinIf errors are encountered while servicing the request, they will instead 121c0341432SJohn Baldwinbe reported to the request's callback function 122c0341432SJohn Baldwin.Pq Fa crp_callback 123c0341432SJohn Baldwinvia 124c0341432SJohn Baldwin.Fa crp_etype . 125c0341432SJohn Baldwin.Pp 126c0341432SJohn BaldwinNote that a request's callback function may be invoked before 127c0341432SJohn Baldwin.Fn crypto_dispatch 128c0341432SJohn Baldwinreturns. 129c0341432SJohn Baldwin.Pp 130c0341432SJohn BaldwinOnce a request has signaled completion by invoking its callback function, 131946b8f6fSJohn Baldwinit should be freed via 132946b8f6fSJohn Baldwin.Fn crypto_destroyreq 133946b8f6fSJohn Baldwinor 134c0341432SJohn Baldwin.Fn crypto_freereq . 135c0341432SJohn Baldwin.Pp 136c0341432SJohn BaldwinCryptographic operations include several fields to describe the request. 1379c0e3d3aSJohn Baldwin.Ss Request Buffers 1389c0e3d3aSJohn BaldwinRequests can either specify a single data buffer that is modified in place 1399c0e3d3aSJohn Baldwin.Po 1409c0e3d3aSJohn Baldwin.Fa crp_buf 1419c0e3d3aSJohn Baldwin.Pc 1429c0e3d3aSJohn Baldwinor separate input 1439c0e3d3aSJohn Baldwin.Po 1449c0e3d3aSJohn Baldwin.Fa crp_buf 1459c0e3d3aSJohn Baldwin.Pc 1469c0e3d3aSJohn Baldwinand output 1479c0e3d3aSJohn Baldwin.Po 1489c0e3d3aSJohn Baldwin.Fa crp_obuf 1499c0e3d3aSJohn Baldwin.Pc 1509c0e3d3aSJohn Baldwinbuffers. 1519c0e3d3aSJohn BaldwinNote that separate input and output buffers are not supported for compression 1529c0e3d3aSJohn Baldwinmode requests. 153c0341432SJohn Baldwin.Pp 1549c0e3d3aSJohn BaldwinAll requests must have a valid 1559c0e3d3aSJohn Baldwin.Fa crp_buf 1569c0e3d3aSJohn Baldwininitialized by one of the following functions: 157e6f6d0c9SAlan Somers.Bl -tag -width "Fn crypto_use_vmpage" 1589c0e3d3aSJohn Baldwin.It Fn crypto_use_buf 1599c0e3d3aSJohn BaldwinUses an array of 1609c0e3d3aSJohn Baldwin.Fa len 1619c0e3d3aSJohn Baldwinbytes pointed to by 1629c0e3d3aSJohn Baldwin.Fa buf 1639c0e3d3aSJohn Baldwinas the data buffer. 1649c0e3d3aSJohn Baldwin.It Fn crypto_use_mbuf 1659c0e3d3aSJohn BaldwinUses the network memory buffer 1669c0e3d3aSJohn Baldwin.Fa m 1679c0e3d3aSJohn Baldwinas the data buffer. 1689c0e3d3aSJohn Baldwin.It Fn crypto_use_uio 1699c0e3d3aSJohn BaldwinUses the scatter/gather list 1709c0e3d3aSJohn Baldwin.Fa uio 1719c0e3d3aSJohn Baldwinas the data buffer. 172e6f6d0c9SAlan Somers.It Fn crypto_use_vmpage 173e6f6d0c9SAlan SomersUses the array of 174e6f6d0c9SAlan Somers.Vt vm_page_t 175e6f6d0c9SAlan Somersstructures as the data buffer. 1769c0e3d3aSJohn Baldwin.El 1779c0e3d3aSJohn Baldwin.Pp 1789c0e3d3aSJohn BaldwinOne of the following functions should be used to initialize 1799c0e3d3aSJohn Baldwin.Fa crp_obuf 1809c0e3d3aSJohn Baldwinfor requests that use separate input and output buffers: 181e6f6d0c9SAlan Somers.Bl -tag -width "Fn crypto_use_output_vmpage" 1829c0e3d3aSJohn Baldwin.It Fn crypto_use_output_buf 1839c0e3d3aSJohn BaldwinUses an array of 1849c0e3d3aSJohn Baldwin.Fa len 1859c0e3d3aSJohn Baldwinbytes pointed to by 1869c0e3d3aSJohn Baldwin.Fa buf 1879c0e3d3aSJohn Baldwinas the output buffer. 1889c0e3d3aSJohn Baldwin.It Fn crypto_use_output_mbuf 1899c0e3d3aSJohn BaldwinUses the network memory buffer 1909c0e3d3aSJohn Baldwin.Fa m 1919c0e3d3aSJohn Baldwinas the output buffer. 1929c0e3d3aSJohn Baldwin.It Fn crypto_use_output_uio 1939c0e3d3aSJohn BaldwinUses the scatter/gather list 1949c0e3d3aSJohn Baldwin.Fa uio 1959c0e3d3aSJohn Baldwinas the output buffer. 196e6f6d0c9SAlan Somers.It Fn crypto_use_output_vmpage 197e6f6d0c9SAlan SomersUses the array of 198e6f6d0c9SAlan Somers.Vt vm_page_t 199e6f6d0c9SAlan Somersstructures as the output buffer. 200c0341432SJohn Baldwin.El 201c0341432SJohn Baldwin.Ss Request Regions 2029c0e3d3aSJohn BaldwinEach request describes one or more regions in the data buffers. 2039c0e3d3aSJohn BaldwinEach region is described by an offset relative to the start of a 204c0341432SJohn Baldwindata buffer and a length. 205c0341432SJohn BaldwinThe length of some regions is the same for all requests belonging to 206c0341432SJohn Baldwina session. 207c0341432SJohn BaldwinThose lengths are set in the session parameters of the associated 208c0341432SJohn Baldwinsession. 209c0341432SJohn BaldwinAll requests must define a payload region. 210c0341432SJohn BaldwinOther regions are only required for specific session modes. 2119c0e3d3aSJohn Baldwin.Pp 2129c0e3d3aSJohn BaldwinFor requests with separate input and output data buffers, 2139c0e3d3aSJohn Baldwinthe AAD, IV, and payload regions are always defined as regions in the 2149c0e3d3aSJohn Baldwininput buffer, 2159c0e3d3aSJohn Baldwinand a separate payload output region is defined to hold the output of 2169c0e3d3aSJohn Baldwinencryption or decryption in the output buffer. 2179c0e3d3aSJohn BaldwinThe digest region describes a region in the input data buffer for 2189c0e3d3aSJohn Baldwinrequests that verify an existing digest. 2199c0e3d3aSJohn BaldwinFor requests that compute a digest, 2209c0e3d3aSJohn Baldwinthe digest region describes a region in the output data buffer. 2219c0e3d3aSJohn BaldwinNote that the only data written to the output buffer is the encryption 2229c0e3d3aSJohn Baldwinor decryption result and any computed digest. 2239c0e3d3aSJohn BaldwinAAD and IV regions are not copied from the input buffer into the output 2249c0e3d3aSJohn Baldwinbuffer but are only used as inputs. 2259c0e3d3aSJohn Baldwin.Pp 226c0341432SJohn BaldwinThe following regions are defined: 2279c0e3d3aSJohn Baldwin.Bl -column "Payload Output" "Input/Output" 2289c0e3d3aSJohn Baldwin.It Sy Region Ta Sy Buffer Ta Sy Description 2299c0e3d3aSJohn Baldwin.It AAD Ta Input Ta 2309b774dc0SJohn BaldwinEmbedded Additional Authenticated Data 2319c0e3d3aSJohn Baldwin.It IV Ta Input Ta 232c0341432SJohn BaldwinEmbedded IV or nonce 2339c0e3d3aSJohn Baldwin.It Payload Ta Input Ta 234c0341432SJohn BaldwinData to encrypt, decrypt, compress, or decompress 2359c0e3d3aSJohn Baldwin.It Payload Output Ta Output Ta 2369c0e3d3aSJohn BaldwinEncrypted or decrypted data 2379c0e3d3aSJohn Baldwin.It Digest Ta Input/Output Ta 238c0341432SJohn BaldwinAuthentication digest, hash, or tag 239c0341432SJohn Baldwin.El 2409c0e3d3aSJohn Baldwin.Bl -column "Payload Output" ".Fa crp_payload_output_start" 2419c0e3d3aSJohn Baldwin.It Sy Region Ta Sy Start Ta Sy Length 2429c0e3d3aSJohn Baldwin.It AAD Ta Fa crp_aad_start Ta Fa crp_aad_length 2439c0e3d3aSJohn Baldwin.It IV Ta Fa crp_iv_start Ta Fa csp_ivlen 2449c0e3d3aSJohn Baldwin.It Payload Ta Fa crp_payload_start Ta Fa crp_payload_length 2459c0e3d3aSJohn Baldwin.It Payload Output Ta Fa crp_payload_output_start Ta Fa crp_payload_length 2469c0e3d3aSJohn Baldwin.It Digest Ta Fa crp_digest_start Ta Fa csp_auth_mlen 2479c0e3d3aSJohn Baldwin.El 248c0341432SJohn Baldwin.Pp 249c0341432SJohn BaldwinRequests are permitted to operate on only a subset of the data buffer. 250c0341432SJohn BaldwinFor example, 251c0341432SJohn Baldwinrequests from IPsec operate on network packets that include headers not 252c0341432SJohn Baldwinused as either additional authentication data (AAD) or payload data. 253c0341432SJohn Baldwin.Ss Request Operations 254c0341432SJohn BaldwinAll requests must specify the type of operation to perform in 255c0341432SJohn Baldwin.Fa crp_op . 256c0341432SJohn BaldwinAvailable operations depend on the session's mode. 257c0341432SJohn Baldwin.Pp 258c0341432SJohn BaldwinCompression requests support the following operations: 259c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_DECOMPRESS 260c0341432SJohn Baldwin.It Dv CRYPTO_OP_COMPRESS 261c0341432SJohn BaldwinCompress the data in the payload region of the data buffer. 262c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECOMPRESS 263c0341432SJohn BaldwinDecompress the data in the payload region of the data buffer. 264c0341432SJohn Baldwin.El 265c0341432SJohn Baldwin.Pp 266c0341432SJohn BaldwinCipher requests support the following operations: 267c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_DECRYPT 268c0341432SJohn Baldwin.It Dv CRYPTO_OP_ENCRYPT 269c0341432SJohn BaldwinEncrypt the data in the payload region of the data buffer. 270c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECRYPT 271c0341432SJohn BaldwinDecrypt the data in the payload region of the data buffer. 272c0341432SJohn Baldwin.El 273c0341432SJohn Baldwin.Pp 274c0341432SJohn BaldwinDigest requests support the following operations: 275c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_COMPUTE_DIGEST 276c0341432SJohn Baldwin.It Dv CRYPTO_OP_COMPUTE_DIGEST 277c0341432SJohn BaldwinCalculate a digest over the payload region of the data buffer 278c0341432SJohn Baldwinand store the result in the digest region. 279c0341432SJohn Baldwin.It Dv CRYPTO_OP_VERIFY_DIGEST 280c0341432SJohn BaldwinCalculate a digest over the payload region of the data buffer. 281c0341432SJohn BaldwinCompare the calculated digest to the existing digest from the digest region. 282c0341432SJohn BaldwinIf the digests match, 283c0341432SJohn Baldwincomplete the request successfully. 284c0341432SJohn BaldwinIf the digests do not match, 285c0341432SJohn Baldwinfail the request with 286c0341432SJohn Baldwin.Er EBADMSG . 287c0341432SJohn Baldwin.El 288c0341432SJohn Baldwin.Pp 289c0341432SJohn BaldwinAEAD and Encrypt-then-Authenticate requests support the following 290c0341432SJohn Baldwinoperations: 291c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP 292c0341432SJohn Baldwin.It Dv CRYPTO_OP_ENCRYPT | Dv CRYPTO_OP_COMPUTE_DIGEST 293c0341432SJohn BaldwinEncrypt the data in the payload region of the data buffer. 294c0341432SJohn BaldwinCalculate a digest over the AAD and payload regions and store the 295c0341432SJohn Baldwinresult in the data buffer. 296c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECRYPT | Dv CRYPTO_OP_VERIFY_DIGEST 297c0341432SJohn BaldwinCalculate a digest over the AAD and payload regions of the data buffer. 298c0341432SJohn BaldwinCompare the calculated digest to the existing digest from the digest region. 299c0341432SJohn BaldwinIf the digests match, 300c0341432SJohn Baldwindecrypt the payload region. 301c0341432SJohn BaldwinIf the digests do not match, 302c0341432SJohn Baldwinfail the request with 303c0341432SJohn Baldwin.Er EBADMSG . 304c0341432SJohn Baldwin.El 3059b774dc0SJohn Baldwin.Ss Request AAD 3069b774dc0SJohn BaldwinAEAD and Encrypt-then-Authenticate requests may optionally include 3079b774dc0SJohn BaldwinAdditional Authenticated Data. 3089b774dc0SJohn BaldwinAAD may either be supplied in the AAD region of the input buffer or 3099b774dc0SJohn Baldwinas a single buffer pointed to by 3109b774dc0SJohn Baldwin.Fa crp_aad . 3119b774dc0SJohn BaldwinIn either case, 3129b774dc0SJohn Baldwin.Fa crp_aad_length 3139b774dc0SJohn Baldwinalways indicates the amount of AAD in bytes. 3147e89ae49SMarcin Wojtas.Ss Request ESN 3157e89ae49SMarcin WojtasIPsec requests may optionally include Extended Sequence Numbers (ESN). 3167e89ae49SMarcin WojtasESN may either be supplied in 3177e89ae49SMarcin Wojtas.Fa crp_esn 3187e89ae49SMarcin Wojtasor as part of the AAD pointed to by 3197e89ae49SMarcin Wojtas.Fa crp_aad . 3207e89ae49SMarcin Wojtas.Pp 3217e89ae49SMarcin WojtasIf the ESN is stored in 3227e89ae49SMarcin Wojtas.Fa crp_esn , 3237e89ae49SMarcin Wojtas.Dv CSP_F_ESN 3247e89ae49SMarcin Wojtasshould be set in 3257e89ae49SMarcin Wojtas.Fa csp_flags . 3267e89ae49SMarcin WojtasThis use case is dedicated for encrypt and authenticate mode, since the 3277e89ae49SMarcin Wojtashigh-order 32 bits of the sequence number are appended after the Next Header 3287e89ae49SMarcin Wojtas(RFC 4303). 3297e89ae49SMarcin Wojtas.Pp 3307e89ae49SMarcin WojtasAEAD modes supply the ESN in a separate AAD buffer (see e.g. RFC 4106, Chapter 5 3317e89ae49SMarcin WojtasAAD Construction). 332c0341432SJohn Baldwin.Ss Request IV and/or Nonce 333c0341432SJohn BaldwinSome cryptographic operations require an IV or nonce as an input. 334c0341432SJohn BaldwinAn IV may be stored either in the IV region of the data buffer or in 335c0341432SJohn Baldwin.Fa crp_iv . 336c0341432SJohn BaldwinBy default, 337c0341432SJohn Baldwinthe IV is assumed to be stored in the IV region. 338c0341432SJohn BaldwinIf the IV is stored in 339c0341432SJohn Baldwin.Fa crp_iv , 340c0341432SJohn Baldwin.Dv CRYPTO_F_IV_SEPARATE 341c0341432SJohn Baldwinshould be set in 342c0341432SJohn Baldwin.Fa crp_flags 343c0341432SJohn Baldwinand 3449c0e3d3aSJohn Baldwin.Fa crp_iv_start 345c0341432SJohn Baldwinshould be left as zero. 346c0341432SJohn Baldwin.Pp 347c0341432SJohn BaldwinRequests that store part, but not all, of the IV in the data buffer should 348c0341432SJohn Baldwinstore the partial IV in the data buffer and pass the full IV separately in 349c0341432SJohn Baldwin.Fa crp_iv . 350c0341432SJohn Baldwin.Ss Request and Callback Scheduling 351c0341432SJohn BaldwinThe crypto framework provides multiple methods of scheduling the dispatch 352c0341432SJohn Baldwinof requests to drivers along with the processing of driver callbacks. 353*68f6800cSMark JohnstonThe 354c0341432SJohn Baldwin.Fn crypto_dispatch , 355*68f6800cSMark Johnston.Fn crypto_dispatch_async , 356*68f6800cSMark Johnstonand 357*68f6800cSMark Johnston.Fn crypto_dispatch_batch 358*68f6800cSMark Johnstonfunctions can be used to request different dispatch scheduling policies. 359*68f6800cSMark Johnston.Pp 360*68f6800cSMark Johnston.Fn crypto_dispatch 361*68f6800cSMark Johnstonsynchronously passes the request to the driver. 362*68f6800cSMark JohnstonThe driver itself may process the request synchronously or asynchronously 363*68f6800cSMark Johnstondepending on whether the driver is implemented by software or hardware. 364*68f6800cSMark Johnston.Pp 365*68f6800cSMark Johnston.Fn crypto_dispatch_async 366*68f6800cSMark Johnstondispatches the request asynchronously. 367*68f6800cSMark JohnstonIf the driver is inherently synchronous, the request is queued to a taskqueue 368*68f6800cSMark Johnstonbacked by a pool of worker threads. 369*68f6800cSMark JohnstonThis can increase througput by allowing requests from a single producer to be 370*68f6800cSMark Johnstonprocessed in parallel. 371*68f6800cSMark JohnstonBy default the pool is sized to provide one thread for each CPU. 372*68f6800cSMark JohnstonWorker threads dequeue requests and pass them to the driver asynchronously. 373*68f6800cSMark Johnston.Fn crypto_dispatch_async 374*68f6800cSMark Johnstonadditionally takes a 375*68f6800cSMark Johnston.Va flags 376*68f6800cSMark Johnstonparameter. 377*68f6800cSMark JohnstonThe 378*68f6800cSMark Johnston.Dv CRYPTO_ASYNC_ORDERED 379*68f6800cSMark Johnstonflag indicates that completion callbacks for requests must be called in the 380*68f6800cSMark Johnstonsame order as requests were dispatched. 381*68f6800cSMark JohnstonIf the driver is asynchronous, the behavior of 382*68f6800cSMark Johnston.Fn crypto_dispatch_async 383*68f6800cSMark Johnstonis identical to that of 384*68f6800cSMark Johnston.Fn crypto_dispatch . 385*68f6800cSMark Johnston.Pp 386*68f6800cSMark Johnston.Fn crypto_dispatch_batch 387*68f6800cSMark Johnstonallows the caller to collect a batch of requests and submit them to the driver 388*68f6800cSMark Johnstonat the same time. 389*68f6800cSMark JohnstonThis allows hardware drivers to optimize the scheduling of request processing 390*68f6800cSMark Johnstonand batch completion interrupts. 391*68f6800cSMark JohnstonA batch is submitted to the driver by invoking the driver's process method on 392*68f6800cSMark Johnstoneach request, specifying 393c0341432SJohn Baldwin.Dv CRYPTO_HINT_MORE 394*68f6800cSMark Johnstonwith each request except for the last. 395*68f6800cSMark JohnstonThe 396*68f6800cSMark Johnston.Fa flags 397*68f6800cSMark Johnstonparameter to 398*68f6800cSMark Johnston.Fn crypto_dispatch_batch 399*68f6800cSMark Johnstonis currently ignored. 400c0341432SJohn Baldwin.Pp 401c0341432SJohn BaldwinCallback function scheduling is simpler than request scheduling. 402c0341432SJohn BaldwinCallbacks can either be invoked synchronously from 403c0341432SJohn Baldwin.Fn crypto_done , 404c0341432SJohn Baldwinor they can be queued to a pool of worker threads. 405c0341432SJohn BaldwinThis pool of worker threads is also sized to provide one worker thread 406c0341432SJohn Baldwinfor each CPU by default. 407c0341432SJohn BaldwinNote that a callback function invoked synchronously from 408c0341432SJohn Baldwin.Fn crypto_done 409c0341432SJohn Baldwinmust follow the same restrictions placed on threaded interrupt handlers. 410c0341432SJohn Baldwin.Pp 411c0341432SJohn BaldwinBy default, 412c0341432SJohn Baldwincallbacks are invoked asynchronously by a worker thread. 413c0341432SJohn BaldwinIf 414c0341432SJohn Baldwin.Dv CRYPTO_F_CBIMM 415c0341432SJohn Baldwinis set, 416c0341432SJohn Baldwinthe callback is always invoked synchronously from 417c0341432SJohn Baldwin.Fn crypto_done . 418c0341432SJohn BaldwinIf 419c0341432SJohn Baldwin.Dv CRYPTO_F_CBIFSYNC 420c0341432SJohn Baldwinis set, 421c0341432SJohn Baldwinthe callback is invoked synchronously if the request was processed by a 422c0341432SJohn Baldwinsoftware driver or asynchronously if the request was processed by a 423c0341432SJohn Baldwinhardware driver. 424c0341432SJohn Baldwin.Pp 425c0341432SJohn BaldwinIf a request was scheduled to the taskqueue via 426c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC , 427c0341432SJohn Baldwincallbacks are always invoked asynchronously ignoring 428c0341432SJohn Baldwin.Dv CRYPTO_F_CBIMM 429c0341432SJohn Baldwinand 430c0341432SJohn Baldwin.Dv CRYPTO_F_CBIFSYNC . 431c0341432SJohn BaldwinIn this case, 432c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC_KEEPORDER 433c0341432SJohn Baldwinmay be set to ensure that callbacks for requests on a given session are 434c0341432SJohn Baldwininvoked in the same order that requests were queued to the session via 435c0341432SJohn Baldwin.Fn crypto_dispatch . 436c0341432SJohn BaldwinThis flag is used by IPsec to ensure that decrypted network packets are 437c0341432SJohn Baldwinpassed up the network stack in roughly the same order they were received. 438c0341432SJohn Baldwin.Pp 439c0341432SJohn Baldwin.Ss Other Request Fields 440c0341432SJohn BaldwinIn addition to the fields and flags enumerated above, 441c0341432SJohn Baldwin.Vt struct cryptop 442c0341432SJohn Baldwinincludes the following: 443c0341432SJohn Baldwin.Bl -tag -width crp_payload_length 444c0341432SJohn Baldwin.It Fa crp_session 445c0341432SJohn BaldwinA reference to the active session. 446c0341432SJohn BaldwinThis is set when the request is created by 447c0341432SJohn Baldwin.Fn crypto_getreq 448c0341432SJohn Baldwinand should not be modified. 449c0341432SJohn BaldwinDrivers can use this to fetch driver-specific session state or 450c0341432SJohn Baldwinsession parameters. 451c0341432SJohn Baldwin.It Fa crp_etype 452c0341432SJohn BaldwinError status. 453c0341432SJohn BaldwinEither zero on success, or an error if a request fails. 454c0341432SJohn BaldwinSet by drivers prior to completing a request via 455c0341432SJohn Baldwin.Fn crypto_done . 456c0341432SJohn Baldwin.It Fa crp_flags 457c0341432SJohn BaldwinA bitmask of flags. 458c0341432SJohn BaldwinThe following flags are available in addition to flags discussed previously: 459c0341432SJohn Baldwin.Bl -tag -width CRYPTO_F_DONE 460c0341432SJohn Baldwin.It Dv CRYPTO_F_DONE 461c0341432SJohn BaldwinSet by 462c0341432SJohn Baldwin.Fa crypto_done 463c0341432SJohn Baldwinbefore calling 464c0341432SJohn Baldwin.Fa crp_callback . 465c0341432SJohn BaldwinThis flag is not very useful and will likely be removed in the future. 466c0341432SJohn BaldwinIt can only be safely checked from the callback routine at which point 467c0341432SJohn Baldwinit is always set. 468c0341432SJohn Baldwin.El 469c0341432SJohn Baldwin.It Fa crp_cipher_key 470c0341432SJohn BaldwinPointer to a request-specific encryption key. 471c0341432SJohn BaldwinIf this value is not set, 472c0341432SJohn Baldwinthe request uses the session encryption key. 473c0341432SJohn Baldwin.It Fa crp_auth_key 474c0341432SJohn BaldwinPointer to a request-specific authentication key. 475c0341432SJohn BaldwinIf this value is not set, 476c0341432SJohn Baldwinthe request uses the session authentication key. 477c0341432SJohn Baldwin.It Fa crp_opaque 478c0341432SJohn BaldwinAn opaque pointer. 479c0341432SJohn BaldwinThis pointer permits users of the cryptographic framework to store 480c0341432SJohn Baldwininformation about a request to be used in the callback. 481c0341432SJohn Baldwin.It Fa crp_callback 482c0341432SJohn BaldwinCallback function. 483c0341432SJohn BaldwinThis must point to a callback function of type 484c0341432SJohn Baldwin.Vt void (*)(struct cryptop *) . 485c0341432SJohn BaldwinThe callback function should inspect 486c0341432SJohn Baldwin.Fa crp_etype 487c0341432SJohn Baldwinto determine the status of the completed operation. 488c0341432SJohn BaldwinIt should also arrange for the request to be freed via 489c0341432SJohn Baldwin.Fn crypto_freereq . 4909c0e3d3aSJohn Baldwin.It Fa crp_olen 4919c0e3d3aSJohn BaldwinUsed with compression and decompression requests to describe the updated 4929c0e3d3aSJohn Baldwinlength of the payload region in the data buffer. 4939c0e3d3aSJohn Baldwin.Pp 4949c0e3d3aSJohn BaldwinIf a compression request increases the size of the payload, 4959c0e3d3aSJohn Baldwinthen the data buffer is unmodified, the request completes successfully, 4969c0e3d3aSJohn Baldwinand 4979c0e3d3aSJohn Baldwin.Fa crp_olen 4989c0e3d3aSJohn Baldwinis set to the size the compressed data would have used. 4999c0e3d3aSJohn BaldwinCallers can compare this to the payload region length to determine if 5009c0e3d3aSJohn Baldwinthe compressed data was discarded. 501c0341432SJohn Baldwin.El 502c0341432SJohn Baldwin.Sh RETURN VALUES 503c0341432SJohn Baldwin.Fn crypto_dispatch 504c0341432SJohn Baldwinreturns an error if the request contained invalid fields, 505c0341432SJohn Baldwinor zero if the request was valid. 506c0341432SJohn Baldwin.Fn crypto_getreq 507c0341432SJohn Baldwinreturns a pointer to a new request structure on success, 508c0341432SJohn Baldwinor 509c0341432SJohn Baldwin.Dv NULL 510c0341432SJohn Baldwinon failure. 511c0341432SJohn Baldwin.Dv NULL 512c0341432SJohn Baldwincan only be returned if 513c0341432SJohn Baldwin.Dv M_NOWAIT 514c0341432SJohn Baldwinwas passed in 515c0341432SJohn Baldwin.Fa how . 516c0341432SJohn Baldwin.Sh SEE ALSO 517c0341432SJohn Baldwin.Xr ipsec 4 , 518c0341432SJohn Baldwin.Xr crypto 7 , 519c0341432SJohn Baldwin.Xr crypto 9 , 520c0341432SJohn Baldwin.Xr crypto_session 9 , 521c0341432SJohn Baldwin.Xr mbuf 9 522c0341432SJohn Baldwin.Xr uio 9 523c0341432SJohn Baldwin.Sh BUGS 524c0341432SJohn BaldwinNot all drivers properly handle mixing session and per-request keys 525c0341432SJohn Baldwinwithin a single session. 526c0341432SJohn BaldwinConsumers should either use a single key for a session specified in 527c0341432SJohn Baldwinthe session parameters or always use per-request keys. 528