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 May 25, 2020 34.Dt CRYPTO_REQUEST 9 35.Os 36.Sh NAME 37.Nm crypto_request 38.Nd symmetric cryptographic operations 39.Sh SYNOPSIS 40.In opencrypto/cryptodev.h 41.Ft int 42.Fn crypto_dispatch "struct cryptop *crp" 43.Ft void 44.Fn crypto_freereq "struct cryptop *crp" 45.Ft "struct cryptop *" 46.Fn crypto_getreq "crypto_session_t cses" "int how" 47.Ft void 48.Fn crypto_use_buf "struct cryptop *crp" "void *buf" "int len" 49.Ft void 50.Fn crypto_use_mbuf "struct cryptop *crp" "struct mbuf *m" 51.Ft void 52.Fn crypto_use_uio "struct cryptop *crp" "struct uio *uio" 53.Ft void 54.Fn crypto_use_output_buf "struct cryptop *crp" "void *buf" "int len" 55.Ft void 56.Fn crypto_use_output_mbuf "struct cryptop *crp" "struct mbuf *m" 57.Ft void 58.Fn crypto_use_output_uio "struct cryptop *crp" "struct uio *uio" 59.Sh DESCRIPTION 60Each symmetric cryptographic operation in the kernel is described by 61an instance of 62.Vt struct cryptop 63and is associated with an active session. 64.Pp 65New requests are allocated by 66.Fn crypto_getreq . 67.Fa cses 68is a reference to an active session. 69.Fa how 70is passed to 71.Xr malloc 9 72and should be set to either 73.Dv M_NOWAIT 74or 75.Dv M_WAITOK . 76The caller should then set fields in the returned structure to describe 77request-specific parameters. 78Unused fields should be left as-is. 79.Pp 80.Fn crypto_dispatch 81passes a crypto request to the driver attached to the request's session. 82If there are errors in the request's fields, this function may return 83an error to the caller. 84If errors are encountered while servicing the request, they will instead 85be reported to the request's callback function 86.Pq Fa crp_callback 87via 88.Fa crp_etype . 89.Pp 90Note that a request's callback function may be invoked before 91.Fn crypto_dispatch 92returns. 93.Pp 94Once a request has signaled completion by invoking its callback function, 95it should be feed via 96.Fn crypto_freereq . 97.Pp 98Cryptographic operations include several fields to describe the request. 99.Ss Request Buffers 100Requests can either specify a single data buffer that is modified in place 101.Po 102.Fa crp_buf 103.Pc 104or separate input 105.Po 106.Fa crp_buf 107.Pc 108and output 109.Po 110.Fa crp_obuf 111.Pc 112buffers. 113Note that separate input and output buffers are not supported for compression 114mode requests. 115.Pp 116All requests must have a valid 117.Fa crp_buf 118initialized by one of the following functions: 119.Bl -tag -width "Fn crypto_use_mbuf" 120.It Fn crypto_use_buf 121Uses an array of 122.Fa len 123bytes pointed to by 124.Fa buf 125as the data buffer. 126.It Fn crypto_use_mbuf 127Uses the network memory buffer 128.Fa m 129as the data buffer. 130.It Fn crypto_use_uio 131Uses the scatter/gather list 132.Fa uio 133as the data buffer. 134.El 135.Pp 136One of the following functions should be used to initialize 137.Fa crp_obuf 138for requests that use separate input and output buffers: 139.Bl -tag -width "Fn crypto_use_output_mbuf" 140.It Fn crypto_use_output_buf 141Uses an array of 142.Fa len 143bytes pointed to by 144.Fa buf 145as the output buffer. 146.It Fn crypto_use_output_mbuf 147Uses the network memory buffer 148.Fa m 149as the output buffer. 150.It Fn crypto_use_output_uio 151Uses the scatter/gather list 152.Fa uio 153as the output buffer. 154.El 155.Ss Request Regions 156Each request describes one or more regions in the data buffers. 157Each region is described by an offset relative to the start of a 158data buffer and a length. 159The length of some regions is the same for all requests belonging to 160a session. 161Those lengths are set in the session parameters of the associated 162session. 163All requests must define a payload region. 164Other regions are only required for specific session modes. 165.Pp 166For requests with separate input and output data buffers, 167the AAD, IV, and payload regions are always defined as regions in the 168input buffer, 169and a separate payload output region is defined to hold the output of 170encryption or decryption in the output buffer. 171The digest region describes a region in the input data buffer for 172requests that verify an existing digest. 173For requests that compute a digest, 174the digest region describes a region in the output data buffer. 175Note that the only data written to the output buffer is the encryption 176or decryption result and any computed digest. 177AAD and IV regions are not copied from the input buffer into the output 178buffer but are only used as inputs. 179.Pp 180The following regions are defined: 181.Bl -column "Payload Output" "Input/Output" 182.It Sy Region Ta Sy Buffer Ta Sy Description 183.It AAD Ta Input Ta 184Additional Authenticated Data 185.It IV Ta Input Ta 186Embedded IV or nonce 187.It Payload Ta Input Ta 188Data to encrypt, decrypt, compress, or decompress 189.It Payload Output Ta Output Ta 190Encrypted or decrypted data 191.It Digest Ta Input/Output Ta 192Authentication digest, hash, or tag 193.El 194.Bl -column "Payload Output" ".Fa crp_payload_output_start" 195.It Sy Region Ta Sy Start Ta Sy Length 196.It AAD Ta Fa crp_aad_start Ta Fa crp_aad_length 197.It IV Ta Fa crp_iv_start Ta Fa csp_ivlen 198.It Payload Ta Fa crp_payload_start Ta Fa crp_payload_length 199.It Payload Output Ta Fa crp_payload_output_start Ta Fa crp_payload_length 200.It Digest Ta Fa crp_digest_start Ta Fa csp_auth_mlen 201.El 202.Pp 203Requests are permitted to operate on only a subset of the data buffer. 204For example, 205requests from IPsec operate on network packets that include headers not 206used as either additional authentication data (AAD) or payload data. 207.Ss Request Operations 208All requests must specify the type of operation to perform in 209.Fa crp_op . 210Available operations depend on the session's mode. 211.Pp 212Compression requests support the following operations: 213.Bl -tag -width CRYPTO_OP_DECOMPRESS 214.It Dv CRYPTO_OP_COMPRESS 215Compress the data in the payload region of the data buffer. 216.It Dv CRYPTO_OP_DECOMPRESS 217Decompress the data in the payload region of the data buffer. 218.El 219.Pp 220Cipher requests support the following operations: 221.Bl -tag -width CRYPTO_OP_DECRYPT 222.It Dv CRYPTO_OP_ENCRYPT 223Encrypt the data in the payload region of the data buffer. 224.It Dv CRYPTO_OP_DECRYPT 225Decrypt the data in the payload region of the data buffer. 226.El 227.Pp 228Digest requests support the following operations: 229.Bl -tag -width CRYPTO_OP_COMPUTE_DIGEST 230.It Dv CRYPTO_OP_COMPUTE_DIGEST 231Calculate a digest over the payload region of the data buffer 232and store the result in the digest region. 233.It Dv CRYPTO_OP_VERIFY_DIGEST 234Calculate a digest over the payload region of the data buffer. 235Compare the calculated digest to the existing digest from the digest region. 236If the digests match, 237complete the request successfully. 238If the digests do not match, 239fail the request with 240.Er EBADMSG . 241.El 242.Pp 243AEAD and Encrypt-then-Authenticate requests support the following 244operations: 245.Bl -tag -width CRYPTO_OP 246.It Dv CRYPTO_OP_ENCRYPT | Dv CRYPTO_OP_COMPUTE_DIGEST 247Encrypt the data in the payload region of the data buffer. 248Calculate a digest over the AAD and payload regions and store the 249result in the data buffer. 250.It Dv CRYPTO_OP_DECRYPT | Dv CRYPTO_OP_VERIFY_DIGEST 251Calculate a digest over the AAD and payload regions of the data buffer. 252Compare the calculated digest to the existing digest from the digest region. 253If the digests match, 254decrypt the payload region. 255If the digests do not match, 256fail the request with 257.Er EBADMSG . 258.El 259.Ss Request IV and/or Nonce 260Some cryptographic operations require an IV or nonce as an input. 261An IV may be stored either in the IV region of the data buffer or in 262.Fa crp_iv . 263By default, 264the IV is assumed to be stored in the IV region. 265If the IV is stored in 266.Fa crp_iv , 267.Dv CRYPTO_F_IV_SEPARATE 268should be set in 269.Fa crp_flags 270and 271.Fa crp_iv_start 272should be left as zero. 273.Pp 274Requests that store part, but not all, of the IV in the data buffer should 275store the partial IV in the data buffer and pass the full IV separately in 276.Fa crp_iv . 277.Ss Request and Callback Scheduling 278The crypto framework provides multiple methods of scheduling the dispatch 279of requests to drivers along with the processing of driver callbacks. 280Requests use flags in 281.Fa crp_flags 282to select the desired scheduling methods. 283.Pp 284.Fn crypto_dispatch 285can pass the request to the session's driver via three different methods: 286.Bl -enum 287.It 288The request is queued to a taskqueue backed by a pool of worker threads. 289By default the pool is sized to provide one thread for each CPU. 290Worker threads dequeue requests and pass them to the driver 291asynchronously. 292.It 293The request is passed to the driver synchronously in the context of the 294thread invoking 295.Fn crypto_dispatch . 296.It 297The request is queued to a queue of pending requests. 298A single worker thread dequeues requests and passes them to the driver 299asynchronously. 300.El 301.Pp 302To select the first method (taskqueue backed by multiple threads), 303requests should set 304.Dv CRYPTO_F_ASYNC . 305To always use the third method (queue to single worker thread), 306requests should set 307.Dv CRYPTO_F_BATCH . 308If both flags are set, 309.Dv CRYPTO_F_ASYNC 310takes precedence. 311If neither flag is set, 312.Fn crypto_dispatch 313will first attempt the second method (invoke driver synchronously). 314If the driver is blocked, 315the request will be queued using the third method. 316One caveat is that the first method is only used for requests using software 317drivers which use host CPUs to process requests. 318Requests whose session is associated with a hardware driver will ignore 319.Dv CRYPTO_F_ASYNC 320and only use 321.Dv CRYPTO_F_BATCH 322to determine how requests should be scheduled. 323.Pp 324In addition to bypassing synchronous dispatch in 325.Fn crypto_dispatch , 326.Dv CRYPTO_F_BATCH 327requests additional changes aimed at optimizing batches of requests to 328the same driver. 329When the worker thread processes a request with 330.Dv CRYPTO_F_BATCH , 331it will search the pending request queue for any other requests for the same 332driver, 333including requests from different sessions. 334If any other requests are present, 335.Dv CRYPTO_HINT_MORE 336is passed to the driver's process method. 337Drivers may use this to batch completion interrupts. 338.Pp 339Callback function scheduling is simpler than request scheduling. 340Callbacks can either be invoked synchronously from 341.Fn crypto_done , 342or they can be queued to a pool of worker threads. 343This pool of worker threads is also sized to provide one worker thread 344for each CPU by default. 345Note that a callback function invoked synchronously from 346.Fn crypto_done 347must follow the same restrictions placed on threaded interrupt handlers. 348.Pp 349By default, 350callbacks are invoked asynchronously by a worker thread. 351If 352.Dv CRYPTO_F_CBIMM 353is set, 354the callback is always invoked synchronously from 355.Fn crypto_done . 356If 357.Dv CRYPTO_F_CBIFSYNC 358is set, 359the callback is invoked synchronously if the request was processed by a 360software driver or asynchronously if the request was processed by a 361hardware driver. 362.Pp 363If a request was scheduled to the taskqueue via 364.Dv CRYPTO_F_ASYNC , 365callbacks are always invoked asynchronously ignoring 366.Dv CRYPTO_F_CBIMM 367and 368.Dv CRYPTO_F_CBIFSYNC . 369In this case, 370.Dv CRYPTO_F_ASYNC_KEEPORDER 371may be set to ensure that callbacks for requests on a given session are 372invoked in the same order that requests were queued to the session via 373.Fn crypto_dispatch . 374This flag is used by IPsec to ensure that decrypted network packets are 375passed up the network stack in roughly the same order they were received. 376.Pp 377.Ss Other Request Fields 378In addition to the fields and flags enumerated above, 379.Vt struct cryptop 380includes the following: 381.Bl -tag -width crp_payload_length 382.It Fa crp_session 383A reference to the active session. 384This is set when the request is created by 385.Fn crypto_getreq 386and should not be modified. 387Drivers can use this to fetch driver-specific session state or 388session parameters. 389.It Fa crp_etype 390Error status. 391Either zero on success, or an error if a request fails. 392Set by drivers prior to completing a request via 393.Fn crypto_done . 394.It Fa crp_flags 395A bitmask of flags. 396The following flags are available in addition to flags discussed previously: 397.Bl -tag -width CRYPTO_F_DONE 398.It Dv CRYPTO_F_DONE 399Set by 400.Fa crypto_done 401before calling 402.Fa crp_callback . 403This flag is not very useful and will likely be removed in the future. 404It can only be safely checked from the callback routine at which point 405it is always set. 406.El 407.It Fa crp_cipher_key 408Pointer to a request-specific encryption key. 409If this value is not set, 410the request uses the session encryption key. 411.It Fa crp_auth_key 412Pointer to a request-specific authentication key. 413If this value is not set, 414the request uses the session authentication key. 415.It Fa crp_opaque 416An opaque pointer. 417This pointer permits users of the cryptographic framework to store 418information about a request to be used in the callback. 419.It Fa crp_callback 420Callback function. 421This must point to a callback function of type 422.Vt void (*)(struct cryptop *) . 423The callback function should inspect 424.Fa crp_etype 425to determine the status of the completed operation. 426It should also arrange for the request to be freed via 427.Fn crypto_freereq . 428.It Fa crp_olen 429Used with compression and decompression requests to describe the updated 430length of the payload region in the data buffer. 431.Pp 432If a compression request increases the size of the payload, 433then the data buffer is unmodified, the request completes successfully, 434and 435.Fa crp_olen 436is set to the size the compressed data would have used. 437Callers can compare this to the payload region length to determine if 438the compressed data was discarded. 439.El 440.Sh RETURN VALUES 441.Fn crypto_dispatch 442returns an error if the request contained invalid fields, 443or zero if the request was valid. 444.Fn crypto_getreq 445returns a pointer to a new request structure on success, 446or 447.Dv NULL 448on failure. 449.Dv NULL 450can only be returned if 451.Dv M_NOWAIT 452was passed in 453.Fa how . 454.Sh SEE ALSO 455.Xr ipsec 4 , 456.Xr crypto 7 , 457.Xr crypto 9 , 458.Xr crypto_session 9 , 459.Xr mbuf 9 460.Xr uio 9 461.Sh BUGS 462Not all drivers properly handle mixing session and per-request keys 463within a single session. 464Consumers should either use a single key for a session specified in 465the session parameters or always use per-request keys. 466