xref: /freebsd/share/man/man9/crypto_request.9 (revision 9b774dc0c586235e3f742dd487fd79e9fa90eedb)
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*9b774dc0SJohn Baldwin.Dd June 22, 2020
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"
43c0341432SJohn Baldwin.Ft void
44c0341432SJohn Baldwin.Fn crypto_freereq "struct cryptop *crp"
45c0341432SJohn Baldwin.Ft "struct cryptop *"
46c0341432SJohn Baldwin.Fn crypto_getreq "crypto_session_t cses" "int how"
479c0e3d3aSJohn Baldwin.Ft void
489c0e3d3aSJohn Baldwin.Fn crypto_use_buf "struct cryptop *crp" "void *buf" "int len"
499c0e3d3aSJohn Baldwin.Ft void
509c0e3d3aSJohn Baldwin.Fn crypto_use_mbuf "struct cryptop *crp" "struct mbuf *m"
519c0e3d3aSJohn Baldwin.Ft void
529c0e3d3aSJohn Baldwin.Fn crypto_use_uio "struct cryptop *crp" "struct uio *uio"
539c0e3d3aSJohn Baldwin.Ft void
549c0e3d3aSJohn Baldwin.Fn crypto_use_output_buf "struct cryptop *crp" "void *buf" "int len"
559c0e3d3aSJohn Baldwin.Ft void
569c0e3d3aSJohn Baldwin.Fn crypto_use_output_mbuf "struct cryptop *crp" "struct mbuf *m"
579c0e3d3aSJohn Baldwin.Ft void
589c0e3d3aSJohn Baldwin.Fn crypto_use_output_uio "struct cryptop *crp" "struct uio *uio"
59c0341432SJohn Baldwin.Sh DESCRIPTION
60c0341432SJohn BaldwinEach symmetric cryptographic operation in the kernel is described by
61c0341432SJohn Baldwinan instance of
62c0341432SJohn Baldwin.Vt struct cryptop
63c0341432SJohn Baldwinand is associated with an active session.
64c0341432SJohn Baldwin.Pp
65c0341432SJohn BaldwinNew requests are allocated by
66c0341432SJohn Baldwin.Fn crypto_getreq .
67c0341432SJohn Baldwin.Fa cses
68c0341432SJohn Baldwinis a reference to an active session.
69c0341432SJohn Baldwin.Fa how
70c0341432SJohn Baldwinis passed to
71c0341432SJohn Baldwin.Xr malloc 9
72c0341432SJohn Baldwinand should be set to either
73c0341432SJohn Baldwin.Dv M_NOWAIT
74c0341432SJohn Baldwinor
75c0341432SJohn Baldwin.Dv M_WAITOK .
76c0341432SJohn BaldwinThe caller should then set fields in the returned structure to describe
77c0341432SJohn Baldwinrequest-specific parameters.
78c0341432SJohn BaldwinUnused fields should be left as-is.
79c0341432SJohn Baldwin.Pp
80c0341432SJohn Baldwin.Fn crypto_dispatch
81c0341432SJohn Baldwinpasses a crypto request to the driver attached to the request's session.
82c0341432SJohn BaldwinIf there are errors in the request's fields, this function may return
83c0341432SJohn Baldwinan error to the caller.
84c0341432SJohn BaldwinIf errors are encountered while servicing the request, they will instead
85c0341432SJohn Baldwinbe reported to the request's callback function
86c0341432SJohn Baldwin.Pq Fa crp_callback
87c0341432SJohn Baldwinvia
88c0341432SJohn Baldwin.Fa crp_etype .
89c0341432SJohn Baldwin.Pp
90c0341432SJohn BaldwinNote that a request's callback function may be invoked before
91c0341432SJohn Baldwin.Fn crypto_dispatch
92c0341432SJohn Baldwinreturns.
93c0341432SJohn Baldwin.Pp
94c0341432SJohn BaldwinOnce a request has signaled completion by invoking its callback function,
95c0341432SJohn Baldwinit should be feed via
96c0341432SJohn Baldwin.Fn crypto_freereq .
97c0341432SJohn Baldwin.Pp
98c0341432SJohn BaldwinCryptographic operations include several fields to describe the request.
999c0e3d3aSJohn Baldwin.Ss Request Buffers
1009c0e3d3aSJohn BaldwinRequests can either specify a single data buffer that is modified in place
1019c0e3d3aSJohn Baldwin.Po
1029c0e3d3aSJohn Baldwin.Fa crp_buf
1039c0e3d3aSJohn Baldwin.Pc
1049c0e3d3aSJohn Baldwinor separate input
1059c0e3d3aSJohn Baldwin.Po
1069c0e3d3aSJohn Baldwin.Fa crp_buf
1079c0e3d3aSJohn Baldwin.Pc
1089c0e3d3aSJohn Baldwinand output
1099c0e3d3aSJohn Baldwin.Po
1109c0e3d3aSJohn Baldwin.Fa crp_obuf
1119c0e3d3aSJohn Baldwin.Pc
1129c0e3d3aSJohn Baldwinbuffers.
1139c0e3d3aSJohn BaldwinNote that separate input and output buffers are not supported for compression
1149c0e3d3aSJohn Baldwinmode requests.
115c0341432SJohn Baldwin.Pp
1169c0e3d3aSJohn BaldwinAll requests must have a valid
1179c0e3d3aSJohn Baldwin.Fa crp_buf
1189c0e3d3aSJohn Baldwininitialized by one of the following functions:
1199c0e3d3aSJohn Baldwin.Bl -tag -width "Fn crypto_use_mbuf"
1209c0e3d3aSJohn Baldwin.It Fn crypto_use_buf
1219c0e3d3aSJohn BaldwinUses an array of
1229c0e3d3aSJohn Baldwin.Fa len
1239c0e3d3aSJohn Baldwinbytes pointed to by
1249c0e3d3aSJohn Baldwin.Fa buf
1259c0e3d3aSJohn Baldwinas the data buffer.
1269c0e3d3aSJohn Baldwin.It Fn crypto_use_mbuf
1279c0e3d3aSJohn BaldwinUses the network memory buffer
1289c0e3d3aSJohn Baldwin.Fa m
1299c0e3d3aSJohn Baldwinas the data buffer.
1309c0e3d3aSJohn Baldwin.It Fn crypto_use_uio
1319c0e3d3aSJohn BaldwinUses the scatter/gather list
1329c0e3d3aSJohn Baldwin.Fa uio
1339c0e3d3aSJohn Baldwinas the data buffer.
1349c0e3d3aSJohn Baldwin.El
1359c0e3d3aSJohn Baldwin.Pp
1369c0e3d3aSJohn BaldwinOne of the following functions should be used to initialize
1379c0e3d3aSJohn Baldwin.Fa crp_obuf
1389c0e3d3aSJohn Baldwinfor requests that use separate input and output buffers:
1399c0e3d3aSJohn Baldwin.Bl -tag -width "Fn crypto_use_output_mbuf"
1409c0e3d3aSJohn Baldwin.It Fn crypto_use_output_buf
1419c0e3d3aSJohn BaldwinUses an array of
1429c0e3d3aSJohn Baldwin.Fa len
1439c0e3d3aSJohn Baldwinbytes pointed to by
1449c0e3d3aSJohn Baldwin.Fa buf
1459c0e3d3aSJohn Baldwinas the output buffer.
1469c0e3d3aSJohn Baldwin.It Fn crypto_use_output_mbuf
1479c0e3d3aSJohn BaldwinUses the network memory buffer
1489c0e3d3aSJohn Baldwin.Fa m
1499c0e3d3aSJohn Baldwinas the output buffer.
1509c0e3d3aSJohn Baldwin.It Fn crypto_use_output_uio
1519c0e3d3aSJohn BaldwinUses the scatter/gather list
1529c0e3d3aSJohn Baldwin.Fa uio
1539c0e3d3aSJohn Baldwinas the output buffer.
154c0341432SJohn Baldwin.El
155c0341432SJohn Baldwin.Ss Request Regions
1569c0e3d3aSJohn BaldwinEach request describes one or more regions in the data buffers.
1579c0e3d3aSJohn BaldwinEach region is described by an offset relative to the start of a
158c0341432SJohn Baldwindata buffer and a length.
159c0341432SJohn BaldwinThe length of some regions is the same for all requests belonging to
160c0341432SJohn Baldwina session.
161c0341432SJohn BaldwinThose lengths are set in the session parameters of the associated
162c0341432SJohn Baldwinsession.
163c0341432SJohn BaldwinAll requests must define a payload region.
164c0341432SJohn BaldwinOther regions are only required for specific session modes.
1659c0e3d3aSJohn Baldwin.Pp
1669c0e3d3aSJohn BaldwinFor requests with separate input and output data buffers,
1679c0e3d3aSJohn Baldwinthe AAD, IV, and payload regions are always defined as regions in the
1689c0e3d3aSJohn Baldwininput buffer,
1699c0e3d3aSJohn Baldwinand a separate payload output region is defined to hold the output of
1709c0e3d3aSJohn Baldwinencryption or decryption in the output buffer.
1719c0e3d3aSJohn BaldwinThe digest region describes a region in the input data buffer for
1729c0e3d3aSJohn Baldwinrequests that verify an existing digest.
1739c0e3d3aSJohn BaldwinFor requests that compute a digest,
1749c0e3d3aSJohn Baldwinthe digest region describes a region in the output data buffer.
1759c0e3d3aSJohn BaldwinNote that the only data written to the output buffer is the encryption
1769c0e3d3aSJohn Baldwinor decryption result and any computed digest.
1779c0e3d3aSJohn BaldwinAAD and IV regions are not copied from the input buffer into the output
1789c0e3d3aSJohn Baldwinbuffer but are only used as inputs.
1799c0e3d3aSJohn Baldwin.Pp
180c0341432SJohn BaldwinThe following regions are defined:
1819c0e3d3aSJohn Baldwin.Bl -column "Payload Output" "Input/Output"
1829c0e3d3aSJohn Baldwin.It Sy Region Ta Sy Buffer Ta Sy Description
1839c0e3d3aSJohn Baldwin.It AAD Ta Input Ta
184*9b774dc0SJohn BaldwinEmbedded Additional Authenticated Data
1859c0e3d3aSJohn Baldwin.It IV Ta Input Ta
186c0341432SJohn BaldwinEmbedded IV or nonce
1879c0e3d3aSJohn Baldwin.It Payload Ta Input Ta
188c0341432SJohn BaldwinData to encrypt, decrypt, compress, or decompress
1899c0e3d3aSJohn Baldwin.It Payload Output Ta Output Ta
1909c0e3d3aSJohn BaldwinEncrypted or decrypted data
1919c0e3d3aSJohn Baldwin.It Digest Ta Input/Output Ta
192c0341432SJohn BaldwinAuthentication digest, hash, or tag
193c0341432SJohn Baldwin.El
1949c0e3d3aSJohn Baldwin.Bl -column "Payload Output" ".Fa crp_payload_output_start"
1959c0e3d3aSJohn Baldwin.It Sy Region Ta Sy Start Ta Sy Length
1969c0e3d3aSJohn Baldwin.It AAD Ta Fa crp_aad_start Ta Fa crp_aad_length
1979c0e3d3aSJohn Baldwin.It IV Ta Fa crp_iv_start Ta Fa csp_ivlen
1989c0e3d3aSJohn Baldwin.It Payload Ta Fa crp_payload_start Ta Fa crp_payload_length
1999c0e3d3aSJohn Baldwin.It Payload Output Ta Fa crp_payload_output_start Ta Fa crp_payload_length
2009c0e3d3aSJohn Baldwin.It Digest Ta Fa crp_digest_start Ta Fa csp_auth_mlen
2019c0e3d3aSJohn Baldwin.El
202c0341432SJohn Baldwin.Pp
203c0341432SJohn BaldwinRequests are permitted to operate on only a subset of the data buffer.
204c0341432SJohn BaldwinFor example,
205c0341432SJohn Baldwinrequests from IPsec operate on network packets that include headers not
206c0341432SJohn Baldwinused as either additional authentication data (AAD) or payload data.
207c0341432SJohn Baldwin.Ss Request Operations
208c0341432SJohn BaldwinAll requests must specify the type of operation to perform in
209c0341432SJohn Baldwin.Fa crp_op .
210c0341432SJohn BaldwinAvailable operations depend on the session's mode.
211c0341432SJohn Baldwin.Pp
212c0341432SJohn BaldwinCompression requests support the following operations:
213c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_DECOMPRESS
214c0341432SJohn Baldwin.It Dv CRYPTO_OP_COMPRESS
215c0341432SJohn BaldwinCompress the data in the payload region of the data buffer.
216c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECOMPRESS
217c0341432SJohn BaldwinDecompress the data in the payload region of the data buffer.
218c0341432SJohn Baldwin.El
219c0341432SJohn Baldwin.Pp
220c0341432SJohn BaldwinCipher requests support the following operations:
221c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_DECRYPT
222c0341432SJohn Baldwin.It Dv CRYPTO_OP_ENCRYPT
223c0341432SJohn BaldwinEncrypt the data in the payload region of the data buffer.
224c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECRYPT
225c0341432SJohn BaldwinDecrypt the data in the payload region of the data buffer.
226c0341432SJohn Baldwin.El
227c0341432SJohn Baldwin.Pp
228c0341432SJohn BaldwinDigest requests support the following operations:
229c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_COMPUTE_DIGEST
230c0341432SJohn Baldwin.It Dv CRYPTO_OP_COMPUTE_DIGEST
231c0341432SJohn BaldwinCalculate a digest over the payload region of the data buffer
232c0341432SJohn Baldwinand store the result in the digest region.
233c0341432SJohn Baldwin.It Dv CRYPTO_OP_VERIFY_DIGEST
234c0341432SJohn BaldwinCalculate a digest over the payload region of the data buffer.
235c0341432SJohn BaldwinCompare the calculated digest to the existing digest from the digest region.
236c0341432SJohn BaldwinIf the digests match,
237c0341432SJohn Baldwincomplete the request successfully.
238c0341432SJohn BaldwinIf the digests do not match,
239c0341432SJohn Baldwinfail the request with
240c0341432SJohn Baldwin.Er EBADMSG .
241c0341432SJohn Baldwin.El
242c0341432SJohn Baldwin.Pp
243c0341432SJohn BaldwinAEAD and Encrypt-then-Authenticate requests support the following
244c0341432SJohn Baldwinoperations:
245c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP
246c0341432SJohn Baldwin.It Dv CRYPTO_OP_ENCRYPT | Dv CRYPTO_OP_COMPUTE_DIGEST
247c0341432SJohn BaldwinEncrypt the data in the payload region of the data buffer.
248c0341432SJohn BaldwinCalculate a digest over the AAD and payload regions and store the
249c0341432SJohn Baldwinresult in the data buffer.
250c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECRYPT | Dv CRYPTO_OP_VERIFY_DIGEST
251c0341432SJohn BaldwinCalculate a digest over the AAD and payload regions of the data buffer.
252c0341432SJohn BaldwinCompare the calculated digest to the existing digest from the digest region.
253c0341432SJohn BaldwinIf the digests match,
254c0341432SJohn Baldwindecrypt the payload region.
255c0341432SJohn BaldwinIf the digests do not match,
256c0341432SJohn Baldwinfail the request with
257c0341432SJohn Baldwin.Er EBADMSG .
258c0341432SJohn Baldwin.El
259*9b774dc0SJohn Baldwin.Ss Request AAD
260*9b774dc0SJohn BaldwinAEAD and Encrypt-then-Authenticate requests may optionally include
261*9b774dc0SJohn BaldwinAdditional Authenticated Data.
262*9b774dc0SJohn BaldwinAAD may either be supplied in the AAD region of the input buffer or
263*9b774dc0SJohn Baldwinas a single buffer pointed to by
264*9b774dc0SJohn Baldwin.Fa crp_aad .
265*9b774dc0SJohn BaldwinIn either case,
266*9b774dc0SJohn Baldwin.Fa crp_aad_length
267*9b774dc0SJohn Baldwinalways indicates the amount of AAD in bytes.
268c0341432SJohn Baldwin.Ss Request IV and/or Nonce
269c0341432SJohn BaldwinSome cryptographic operations require an IV or nonce as an input.
270c0341432SJohn BaldwinAn IV may be stored either in the IV region of the data buffer or in
271c0341432SJohn Baldwin.Fa crp_iv .
272c0341432SJohn BaldwinBy default,
273c0341432SJohn Baldwinthe IV is assumed to be stored in the IV region.
274c0341432SJohn BaldwinIf the IV is stored in
275c0341432SJohn Baldwin.Fa crp_iv ,
276c0341432SJohn Baldwin.Dv CRYPTO_F_IV_SEPARATE
277c0341432SJohn Baldwinshould be set in
278c0341432SJohn Baldwin.Fa crp_flags
279c0341432SJohn Baldwinand
2809c0e3d3aSJohn Baldwin.Fa crp_iv_start
281c0341432SJohn Baldwinshould be left as zero.
282c0341432SJohn Baldwin.Pp
283c0341432SJohn BaldwinRequests that store part, but not all, of the IV in the data buffer should
284c0341432SJohn Baldwinstore the partial IV in the data buffer and pass the full IV separately in
285c0341432SJohn Baldwin.Fa crp_iv .
286c0341432SJohn Baldwin.Ss Request and Callback Scheduling
287c0341432SJohn BaldwinThe crypto framework provides multiple methods of scheduling the dispatch
288c0341432SJohn Baldwinof requests to drivers along with the processing of driver callbacks.
289c0341432SJohn BaldwinRequests use flags in
290c0341432SJohn Baldwin.Fa crp_flags
291c0341432SJohn Baldwinto select the desired scheduling methods.
292c0341432SJohn Baldwin.Pp
293c0341432SJohn Baldwin.Fn crypto_dispatch
294c0341432SJohn Baldwincan pass the request to the session's driver via three different methods:
295c0341432SJohn Baldwin.Bl -enum
296c0341432SJohn Baldwin.It
297c0341432SJohn BaldwinThe request is queued to a taskqueue backed by a pool of worker threads.
298c0341432SJohn BaldwinBy default the pool is sized to provide one thread for each CPU.
299c0341432SJohn BaldwinWorker threads dequeue requests and pass them to the driver
300c0341432SJohn Baldwinasynchronously.
301c0341432SJohn Baldwin.It
302c0341432SJohn BaldwinThe request is passed to the driver synchronously in the context of the
303c0341432SJohn Baldwinthread invoking
304c0341432SJohn Baldwin.Fn crypto_dispatch .
305c0341432SJohn Baldwin.It
306c0341432SJohn BaldwinThe request is queued to a queue of pending requests.
307c0341432SJohn BaldwinA single worker thread dequeues requests and passes them to the driver
308c0341432SJohn Baldwinasynchronously.
309c0341432SJohn Baldwin.El
310c0341432SJohn Baldwin.Pp
311c0341432SJohn BaldwinTo select the first method (taskqueue backed by multiple threads),
312c0341432SJohn Baldwinrequests should set
313c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC .
314c0341432SJohn BaldwinTo always use the third method (queue to single worker thread),
315c0341432SJohn Baldwinrequests should set
316c0341432SJohn Baldwin.Dv CRYPTO_F_BATCH .
317c0341432SJohn BaldwinIf both flags are set,
318c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC
319c0341432SJohn Baldwintakes precedence.
320c0341432SJohn BaldwinIf neither flag is set,
321c0341432SJohn Baldwin.Fn crypto_dispatch
322c0341432SJohn Baldwinwill first attempt the second method (invoke driver synchronously).
323c0341432SJohn BaldwinIf the driver is blocked,
324c0341432SJohn Baldwinthe request will be queued using the third method.
325c0341432SJohn BaldwinOne caveat is that the first method is only used for requests using software
326c0341432SJohn Baldwindrivers which use host CPUs to process requests.
327c0341432SJohn BaldwinRequests whose session is associated with a hardware driver will ignore
328c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC
329c0341432SJohn Baldwinand only use
330c0341432SJohn Baldwin.Dv CRYPTO_F_BATCH
331c0341432SJohn Baldwinto determine how requests should be scheduled.
332c0341432SJohn Baldwin.Pp
333c0341432SJohn BaldwinIn addition to bypassing synchronous dispatch in
334c0341432SJohn Baldwin.Fn crypto_dispatch ,
335c0341432SJohn Baldwin.Dv CRYPTO_F_BATCH
336c0341432SJohn Baldwinrequests additional changes aimed at optimizing batches of requests to
337c0341432SJohn Baldwinthe same driver.
338c0341432SJohn BaldwinWhen the worker thread processes a request with
339c0341432SJohn Baldwin.Dv CRYPTO_F_BATCH ,
340c0341432SJohn Baldwinit will search the pending request queue for any other requests for the same
341c0341432SJohn Baldwindriver,
342c0341432SJohn Baldwinincluding requests from different sessions.
343c0341432SJohn BaldwinIf any other requests are present,
344c0341432SJohn Baldwin.Dv CRYPTO_HINT_MORE
345c0341432SJohn Baldwinis passed to the driver's process method.
346c0341432SJohn BaldwinDrivers may use this to batch completion interrupts.
347c0341432SJohn Baldwin.Pp
348c0341432SJohn BaldwinCallback function scheduling is simpler than request scheduling.
349c0341432SJohn BaldwinCallbacks can either be invoked synchronously from
350c0341432SJohn Baldwin.Fn crypto_done ,
351c0341432SJohn Baldwinor they can be queued to a pool of worker threads.
352c0341432SJohn BaldwinThis pool of worker threads is also sized to provide one worker thread
353c0341432SJohn Baldwinfor each CPU by default.
354c0341432SJohn BaldwinNote that a callback function invoked synchronously from
355c0341432SJohn Baldwin.Fn crypto_done
356c0341432SJohn Baldwinmust follow the same restrictions placed on threaded interrupt handlers.
357c0341432SJohn Baldwin.Pp
358c0341432SJohn BaldwinBy default,
359c0341432SJohn Baldwincallbacks are invoked asynchronously by a worker thread.
360c0341432SJohn BaldwinIf
361c0341432SJohn Baldwin.Dv CRYPTO_F_CBIMM
362c0341432SJohn Baldwinis set,
363c0341432SJohn Baldwinthe callback is always invoked synchronously from
364c0341432SJohn Baldwin.Fn crypto_done .
365c0341432SJohn BaldwinIf
366c0341432SJohn Baldwin.Dv CRYPTO_F_CBIFSYNC
367c0341432SJohn Baldwinis set,
368c0341432SJohn Baldwinthe callback is invoked synchronously if the request was processed by a
369c0341432SJohn Baldwinsoftware driver or asynchronously if the request was processed by a
370c0341432SJohn Baldwinhardware driver.
371c0341432SJohn Baldwin.Pp
372c0341432SJohn BaldwinIf a request was scheduled to the taskqueue via
373c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC ,
374c0341432SJohn Baldwincallbacks are always invoked asynchronously ignoring
375c0341432SJohn Baldwin.Dv CRYPTO_F_CBIMM
376c0341432SJohn Baldwinand
377c0341432SJohn Baldwin.Dv CRYPTO_F_CBIFSYNC .
378c0341432SJohn BaldwinIn this case,
379c0341432SJohn Baldwin.Dv CRYPTO_F_ASYNC_KEEPORDER
380c0341432SJohn Baldwinmay be set to ensure that callbacks for requests on a given session are
381c0341432SJohn Baldwininvoked in the same order that requests were queued to the session via
382c0341432SJohn Baldwin.Fn crypto_dispatch .
383c0341432SJohn BaldwinThis flag is used by IPsec to ensure that decrypted network packets are
384c0341432SJohn Baldwinpassed up the network stack in roughly the same order they were received.
385c0341432SJohn Baldwin.Pp
386c0341432SJohn Baldwin.Ss Other Request Fields
387c0341432SJohn BaldwinIn addition to the fields and flags enumerated above,
388c0341432SJohn Baldwin.Vt struct cryptop
389c0341432SJohn Baldwinincludes the following:
390c0341432SJohn Baldwin.Bl -tag -width crp_payload_length
391c0341432SJohn Baldwin.It Fa crp_session
392c0341432SJohn BaldwinA reference to the active session.
393c0341432SJohn BaldwinThis is set when the request is created by
394c0341432SJohn Baldwin.Fn crypto_getreq
395c0341432SJohn Baldwinand should not be modified.
396c0341432SJohn BaldwinDrivers can use this to fetch driver-specific session state or
397c0341432SJohn Baldwinsession parameters.
398c0341432SJohn Baldwin.It Fa crp_etype
399c0341432SJohn BaldwinError status.
400c0341432SJohn BaldwinEither zero on success, or an error if a request fails.
401c0341432SJohn BaldwinSet by drivers prior to completing a request via
402c0341432SJohn Baldwin.Fn crypto_done .
403c0341432SJohn Baldwin.It Fa crp_flags
404c0341432SJohn BaldwinA bitmask of flags.
405c0341432SJohn BaldwinThe following flags are available in addition to flags discussed previously:
406c0341432SJohn Baldwin.Bl -tag -width CRYPTO_F_DONE
407c0341432SJohn Baldwin.It Dv CRYPTO_F_DONE
408c0341432SJohn BaldwinSet by
409c0341432SJohn Baldwin.Fa crypto_done
410c0341432SJohn Baldwinbefore calling
411c0341432SJohn Baldwin.Fa crp_callback .
412c0341432SJohn BaldwinThis flag is not very useful and will likely be removed in the future.
413c0341432SJohn BaldwinIt can only be safely checked from the callback routine at which point
414c0341432SJohn Baldwinit is always set.
415c0341432SJohn Baldwin.El
416c0341432SJohn Baldwin.It Fa crp_cipher_key
417c0341432SJohn BaldwinPointer to a request-specific encryption key.
418c0341432SJohn BaldwinIf this value is not set,
419c0341432SJohn Baldwinthe request uses the session encryption key.
420c0341432SJohn Baldwin.It Fa crp_auth_key
421c0341432SJohn BaldwinPointer to a request-specific authentication key.
422c0341432SJohn BaldwinIf this value is not set,
423c0341432SJohn Baldwinthe request uses the session authentication key.
424c0341432SJohn Baldwin.It Fa crp_opaque
425c0341432SJohn BaldwinAn opaque pointer.
426c0341432SJohn BaldwinThis pointer permits users of the cryptographic framework to store
427c0341432SJohn Baldwininformation about a request to be used in the callback.
428c0341432SJohn Baldwin.It Fa crp_callback
429c0341432SJohn BaldwinCallback function.
430c0341432SJohn BaldwinThis must point to a callback function of type
431c0341432SJohn Baldwin.Vt void (*)(struct cryptop *) .
432c0341432SJohn BaldwinThe callback function should inspect
433c0341432SJohn Baldwin.Fa crp_etype
434c0341432SJohn Baldwinto determine the status of the completed operation.
435c0341432SJohn BaldwinIt should also arrange for the request to be freed via
436c0341432SJohn Baldwin.Fn crypto_freereq .
4379c0e3d3aSJohn Baldwin.It Fa crp_olen
4389c0e3d3aSJohn BaldwinUsed with compression and decompression requests to describe the updated
4399c0e3d3aSJohn Baldwinlength of the payload region in the data buffer.
4409c0e3d3aSJohn Baldwin.Pp
4419c0e3d3aSJohn BaldwinIf a compression request increases the size of the payload,
4429c0e3d3aSJohn Baldwinthen the data buffer is unmodified, the request completes successfully,
4439c0e3d3aSJohn Baldwinand
4449c0e3d3aSJohn Baldwin.Fa crp_olen
4459c0e3d3aSJohn Baldwinis set to the size the compressed data would have used.
4469c0e3d3aSJohn BaldwinCallers can compare this to the payload region length to determine if
4479c0e3d3aSJohn Baldwinthe compressed data was discarded.
448c0341432SJohn Baldwin.El
449c0341432SJohn Baldwin.Sh RETURN VALUES
450c0341432SJohn Baldwin.Fn crypto_dispatch
451c0341432SJohn Baldwinreturns an error if the request contained invalid fields,
452c0341432SJohn Baldwinor zero if the request was valid.
453c0341432SJohn Baldwin.Fn crypto_getreq
454c0341432SJohn Baldwinreturns a pointer to a new request structure on success,
455c0341432SJohn Baldwinor
456c0341432SJohn Baldwin.Dv NULL
457c0341432SJohn Baldwinon failure.
458c0341432SJohn Baldwin.Dv NULL
459c0341432SJohn Baldwincan only be returned if
460c0341432SJohn Baldwin.Dv M_NOWAIT
461c0341432SJohn Baldwinwas passed in
462c0341432SJohn Baldwin.Fa how .
463c0341432SJohn Baldwin.Sh SEE ALSO
464c0341432SJohn Baldwin.Xr ipsec 4 ,
465c0341432SJohn Baldwin.Xr crypto 7 ,
466c0341432SJohn Baldwin.Xr crypto 9 ,
467c0341432SJohn Baldwin.Xr crypto_session 9 ,
468c0341432SJohn Baldwin.Xr mbuf 9
469c0341432SJohn Baldwin.Xr uio 9
470c0341432SJohn Baldwin.Sh BUGS
471c0341432SJohn BaldwinNot all drivers properly handle mixing session and per-request keys
472c0341432SJohn Baldwinwithin a single session.
473c0341432SJohn BaldwinConsumers should either use a single key for a session specified in
474c0341432SJohn Baldwinthe session parameters or always use per-request keys.
475