xref: /freebsd/share/man/man9/crypto_request.9 (revision 002c47badee59be58cb84fd74624594d22e14276)
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*002c47baSAlan Somers.Dd November 2, 2022
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
4174d3f1b6SJohn Baldwin.Ft "struct cryptop *"
4274d3f1b6SJohn Baldwin.Fn crypto_clonereq "crypto_session_t cses" "struct cryptop *crp" "int how"
43c0341432SJohn Baldwin.Ft int
44c0341432SJohn Baldwin.Fn crypto_dispatch "struct cryptop *crp"
4568f6800cSMark Johnston.Ft int
4668f6800cSMark Johnston.Fn crypto_dispatch_async "struct cryptop *crp" "int flags"
4768f6800cSMark Johnston.Ft void
4868f6800cSMark Johnston.Fn crypto_dispatch_batch "struct cryptopq *crpq" "int flags"
49c0341432SJohn Baldwin.Ft void
50946b8f6fSJohn Baldwin.Fn crypto_destroyreq "struct cryptop *crp"
51946b8f6fSJohn Baldwin.Ft void
52c0341432SJohn Baldwin.Fn crypto_freereq "struct cryptop *crp"
53c0341432SJohn Baldwin.Ft "struct cryptop *"
54c0341432SJohn Baldwin.Fn crypto_getreq "crypto_session_t cses" "int how"
559c0e3d3aSJohn Baldwin.Ft void
56946b8f6fSJohn Baldwin.Fn crypto_initreq "crypto_session_t cses" "int how"
57946b8f6fSJohn Baldwin.Ft void
589c0e3d3aSJohn Baldwin.Fn crypto_use_buf "struct cryptop *crp" "void *buf" "int len"
599c0e3d3aSJohn Baldwin.Ft void
609c0e3d3aSJohn Baldwin.Fn crypto_use_mbuf "struct cryptop *crp" "struct mbuf *m"
619c0e3d3aSJohn Baldwin.Ft void
629c0e3d3aSJohn Baldwin.Fn crypto_use_uio "struct cryptop *crp" "struct uio *uio"
639c0e3d3aSJohn Baldwin.Ft void
64e6f6d0c9SAlan Somers.Fn crypto_use_vmpage "struct cryptop *crp" "vm_page_t *pages" "int len" "int offset"
65e6f6d0c9SAlan Somers.Ft void
669c0e3d3aSJohn Baldwin.Fn crypto_use_output_buf "struct cryptop *crp" "void *buf" "int len"
679c0e3d3aSJohn Baldwin.Ft void
689c0e3d3aSJohn Baldwin.Fn crypto_use_output_mbuf "struct cryptop *crp" "struct mbuf *m"
699c0e3d3aSJohn Baldwin.Ft void
709c0e3d3aSJohn Baldwin.Fn crypto_use_output_uio "struct cryptop *crp" "struct uio *uio"
71e6f6d0c9SAlan Somers.Ft void
72e6f6d0c9SAlan Somers.Fn crypto_use_output_vmpage "struct cryptop *crp" "vm_page_t *pages" "int len" "int offset"
73c0341432SJohn Baldwin.Sh DESCRIPTION
74c0341432SJohn BaldwinEach symmetric cryptographic operation in the kernel is described by
75c0341432SJohn Baldwinan instance of
76c0341432SJohn Baldwin.Vt struct cryptop
77c0341432SJohn Baldwinand is associated with an active session.
78c0341432SJohn Baldwin.Pp
79946b8f6fSJohn BaldwinRequests can either be allocated dynamically or use caller-supplied
80946b8f6fSJohn Baldwinstorage.
8174d3f1b6SJohn BaldwinDynamically allocated requests should be allocated by either
82946b8f6fSJohn Baldwin.Fn crypto_getreq
8374d3f1b6SJohn Baldwinor
8474d3f1b6SJohn Baldwin.Fn crypto_clonereq ,
85946b8f6fSJohn Baldwinand freed by
86946b8f6fSJohn Baldwin.Fn crypto_freereq
87946b8f6fSJohn Baldwinonce the request has completed.
88946b8f6fSJohn BaldwinRequests using caller-supplied storage should be initialized by
89946b8f6fSJohn Baldwin.Fn crypto_initreq
90946b8f6fSJohn Baldwinat the start of each operation and destroyed by
91946b8f6fSJohn Baldwin.Fn crypto_destroyreq
92946b8f6fSJohn Baldwinonce the request has completed.
93946b8f6fSJohn Baldwin.Pp
9474d3f1b6SJohn BaldwinFor
9574d3f1b6SJohn Baldwin.Fn crypto_clonereq ,
9674d3f1b6SJohn Baldwin.Fn crypto_getreq ,
97946b8f6fSJohn Baldwinand
98946b8f6fSJohn Baldwin.Fn crypto_initreq ,
99c0341432SJohn Baldwin.Fa cses
100c0341432SJohn Baldwinis a reference to an active session.
101946b8f6fSJohn BaldwinFor
10274d3f1b6SJohn Baldwin.Fn crypto_clonereq
10374d3f1b6SJohn Baldwinand
104946b8f6fSJohn Baldwin.Fn crypto_getreq ,
105c0341432SJohn Baldwin.Fa how
106c0341432SJohn Baldwinis passed to
107c0341432SJohn Baldwin.Xr malloc 9
108c0341432SJohn Baldwinand should be set to either
109c0341432SJohn Baldwin.Dv M_NOWAIT
110c0341432SJohn Baldwinor
111c0341432SJohn Baldwin.Dv M_WAITOK .
112946b8f6fSJohn Baldwin.Pp
11374d3f1b6SJohn Baldwin.Fn crypto_clonereq
11474d3f1b6SJohn Baldwinallocates a new request that inherits request inputs such as request buffers
11574d3f1b6SJohn Baldwinfrom the original
11674d3f1b6SJohn Baldwin.Fa crp
11774d3f1b6SJohn Baldwinrequest.
11874d3f1b6SJohn BaldwinHowever, the new request is associated with the
11974d3f1b6SJohn Baldwin.Fa cses
12074d3f1b6SJohn Baldwinsession rather than inheriting the session from
12174d3f1b6SJohn Baldwin.Fa crp .
12274d3f1b6SJohn Baldwin.Fa crp
12374d3f1b6SJohn Baldwinmust not be a completed request.
12474d3f1b6SJohn Baldwin.Pp
125946b8f6fSJohn BaldwinOnce a request has been initialized,
126946b8f6fSJohn Baldwinthe caller should set fields in the structure to describe
127c0341432SJohn Baldwinrequest-specific parameters.
128c0341432SJohn BaldwinUnused fields should be left as-is.
129c0341432SJohn Baldwin.Pp
13068f6800cSMark JohnstonThe
13168f6800cSMark Johnston.Fn crypto_dispatch ,
13268f6800cSMark Johnston.Fn crypto_dispatch_async ,
13368f6800cSMark Johnstonand
13468f6800cSMark Johnston.Fn crypto_dispatch_batch
13568f6800cSMark Johnstonfunctions pass one or more crypto requests to the driver attached to the
13668f6800cSMark Johnstonrequest's session.
13768f6800cSMark JohnstonIf there are errors in the request's fields, these functions may return an
13868f6800cSMark Johnstonerror to the caller.
139c0341432SJohn BaldwinIf errors are encountered while servicing the request, they will instead
140c0341432SJohn Baldwinbe reported to the request's callback function
141c0341432SJohn Baldwin.Pq Fa crp_callback
142c0341432SJohn Baldwinvia
143c0341432SJohn Baldwin.Fa crp_etype .
144c0341432SJohn Baldwin.Pp
145c0341432SJohn BaldwinNote that a request's callback function may be invoked before
146c0341432SJohn Baldwin.Fn crypto_dispatch
147c0341432SJohn Baldwinreturns.
148c0341432SJohn Baldwin.Pp
149c0341432SJohn BaldwinOnce a request has signaled completion by invoking its callback function,
150946b8f6fSJohn Baldwinit should be freed via
151946b8f6fSJohn Baldwin.Fn crypto_destroyreq
152946b8f6fSJohn Baldwinor
153c0341432SJohn Baldwin.Fn crypto_freereq .
154c0341432SJohn Baldwin.Pp
155c0341432SJohn BaldwinCryptographic operations include several fields to describe the request.
1569c0e3d3aSJohn Baldwin.Ss Request Buffers
1579c0e3d3aSJohn BaldwinRequests can either specify a single data buffer that is modified in place
1589c0e3d3aSJohn Baldwin.Po
1599c0e3d3aSJohn Baldwin.Fa crp_buf
1609c0e3d3aSJohn Baldwin.Pc
1619c0e3d3aSJohn Baldwinor separate input
1629c0e3d3aSJohn Baldwin.Po
1639c0e3d3aSJohn Baldwin.Fa crp_buf
1649c0e3d3aSJohn Baldwin.Pc
1659c0e3d3aSJohn Baldwinand output
1669c0e3d3aSJohn Baldwin.Po
1679c0e3d3aSJohn Baldwin.Fa crp_obuf
1689c0e3d3aSJohn Baldwin.Pc
1699c0e3d3aSJohn Baldwinbuffers.
1709c0e3d3aSJohn BaldwinNote that separate input and output buffers are not supported for compression
1719c0e3d3aSJohn Baldwinmode requests.
172c0341432SJohn Baldwin.Pp
1739c0e3d3aSJohn BaldwinAll requests must have a valid
1749c0e3d3aSJohn Baldwin.Fa crp_buf
1759c0e3d3aSJohn Baldwininitialized by one of the following functions:
176e6f6d0c9SAlan Somers.Bl -tag -width "Fn crypto_use_vmpage"
1779c0e3d3aSJohn Baldwin.It Fn crypto_use_buf
1789c0e3d3aSJohn BaldwinUses an array of
1799c0e3d3aSJohn Baldwin.Fa len
1809c0e3d3aSJohn Baldwinbytes pointed to by
1819c0e3d3aSJohn Baldwin.Fa buf
1829c0e3d3aSJohn Baldwinas the data buffer.
1839c0e3d3aSJohn Baldwin.It Fn crypto_use_mbuf
1849c0e3d3aSJohn BaldwinUses the network memory buffer
1859c0e3d3aSJohn Baldwin.Fa m
1869c0e3d3aSJohn Baldwinas the data buffer.
1879c0e3d3aSJohn Baldwin.It Fn crypto_use_uio
1889c0e3d3aSJohn BaldwinUses the scatter/gather list
1899c0e3d3aSJohn Baldwin.Fa uio
1909c0e3d3aSJohn Baldwinas the data buffer.
191e6f6d0c9SAlan Somers.It Fn crypto_use_vmpage
192e6f6d0c9SAlan SomersUses the array of
193e6f6d0c9SAlan Somers.Vt vm_page_t
194e6f6d0c9SAlan Somersstructures as the data buffer.
1959c0e3d3aSJohn Baldwin.El
1969c0e3d3aSJohn Baldwin.Pp
1979c0e3d3aSJohn BaldwinOne of the following functions should be used to initialize
1989c0e3d3aSJohn Baldwin.Fa crp_obuf
1999c0e3d3aSJohn Baldwinfor requests that use separate input and output buffers:
200e6f6d0c9SAlan Somers.Bl -tag -width "Fn crypto_use_output_vmpage"
2019c0e3d3aSJohn Baldwin.It Fn crypto_use_output_buf
2029c0e3d3aSJohn BaldwinUses an array of
2039c0e3d3aSJohn Baldwin.Fa len
2049c0e3d3aSJohn Baldwinbytes pointed to by
2059c0e3d3aSJohn Baldwin.Fa buf
2069c0e3d3aSJohn Baldwinas the output buffer.
2079c0e3d3aSJohn Baldwin.It Fn crypto_use_output_mbuf
2089c0e3d3aSJohn BaldwinUses the network memory buffer
2099c0e3d3aSJohn Baldwin.Fa m
2109c0e3d3aSJohn Baldwinas the output buffer.
2119c0e3d3aSJohn Baldwin.It Fn crypto_use_output_uio
2129c0e3d3aSJohn BaldwinUses the scatter/gather list
2139c0e3d3aSJohn Baldwin.Fa uio
2149c0e3d3aSJohn Baldwinas the output buffer.
215e6f6d0c9SAlan Somers.It Fn crypto_use_output_vmpage
216e6f6d0c9SAlan SomersUses the array of
217e6f6d0c9SAlan Somers.Vt vm_page_t
218e6f6d0c9SAlan Somersstructures as the output buffer.
219c0341432SJohn Baldwin.El
220c0341432SJohn Baldwin.Ss Request Regions
2219c0e3d3aSJohn BaldwinEach request describes one or more regions in the data buffers.
2229c0e3d3aSJohn BaldwinEach region is described by an offset relative to the start of a
223c0341432SJohn Baldwindata buffer and a length.
224c0341432SJohn BaldwinThe length of some regions is the same for all requests belonging to
225c0341432SJohn Baldwina session.
226c0341432SJohn BaldwinThose lengths are set in the session parameters of the associated
227c0341432SJohn Baldwinsession.
228c0341432SJohn BaldwinAll requests must define a payload region.
229c0341432SJohn BaldwinOther regions are only required for specific session modes.
2309c0e3d3aSJohn Baldwin.Pp
2319c0e3d3aSJohn BaldwinFor requests with separate input and output data buffers,
2329c0e3d3aSJohn Baldwinthe AAD, IV, and payload regions are always defined as regions in the
2339c0e3d3aSJohn Baldwininput buffer,
2349c0e3d3aSJohn Baldwinand a separate payload output region is defined to hold the output of
2359c0e3d3aSJohn Baldwinencryption or decryption in the output buffer.
2369c0e3d3aSJohn BaldwinThe digest region describes a region in the input data buffer for
2379c0e3d3aSJohn Baldwinrequests that verify an existing digest.
2389c0e3d3aSJohn BaldwinFor requests that compute a digest,
2399c0e3d3aSJohn Baldwinthe digest region describes a region in the output data buffer.
2409c0e3d3aSJohn BaldwinNote that the only data written to the output buffer is the encryption
2419c0e3d3aSJohn Baldwinor decryption result and any computed digest.
2429c0e3d3aSJohn BaldwinAAD and IV regions are not copied from the input buffer into the output
2439c0e3d3aSJohn Baldwinbuffer but are only used as inputs.
2449c0e3d3aSJohn Baldwin.Pp
245c0341432SJohn BaldwinThe following regions are defined:
2469c0e3d3aSJohn Baldwin.Bl -column "Payload Output" "Input/Output"
2479c0e3d3aSJohn Baldwin.It Sy Region Ta Sy Buffer Ta Sy Description
2489c0e3d3aSJohn Baldwin.It AAD Ta Input Ta
2499b774dc0SJohn BaldwinEmbedded Additional Authenticated Data
2509c0e3d3aSJohn Baldwin.It IV Ta Input Ta
251c0341432SJohn BaldwinEmbedded IV or nonce
2529c0e3d3aSJohn Baldwin.It Payload Ta Input Ta
253c0341432SJohn BaldwinData to encrypt, decrypt, compress, or decompress
2549c0e3d3aSJohn Baldwin.It Payload Output Ta Output Ta
2559c0e3d3aSJohn BaldwinEncrypted or decrypted data
2569c0e3d3aSJohn Baldwin.It Digest Ta Input/Output Ta
257c0341432SJohn BaldwinAuthentication digest, hash, or tag
258c0341432SJohn Baldwin.El
2599c0e3d3aSJohn Baldwin.Bl -column "Payload Output" ".Fa crp_payload_output_start"
2609c0e3d3aSJohn Baldwin.It Sy Region Ta Sy Start Ta Sy Length
2619c0e3d3aSJohn Baldwin.It AAD Ta Fa crp_aad_start Ta Fa crp_aad_length
2629c0e3d3aSJohn Baldwin.It IV Ta Fa crp_iv_start Ta Fa csp_ivlen
2639c0e3d3aSJohn Baldwin.It Payload Ta Fa crp_payload_start Ta Fa crp_payload_length
2649c0e3d3aSJohn Baldwin.It Payload Output Ta Fa crp_payload_output_start Ta Fa crp_payload_length
2659c0e3d3aSJohn Baldwin.It Digest Ta Fa crp_digest_start Ta Fa csp_auth_mlen
2669c0e3d3aSJohn Baldwin.El
267c0341432SJohn Baldwin.Pp
268c0341432SJohn BaldwinRequests are permitted to operate on only a subset of the data buffer.
269c0341432SJohn BaldwinFor example,
270c0341432SJohn Baldwinrequests from IPsec operate on network packets that include headers not
271c0341432SJohn Baldwinused as either additional authentication data (AAD) or payload data.
272c0341432SJohn Baldwin.Ss Request Operations
273c0341432SJohn BaldwinAll requests must specify the type of operation to perform in
274c0341432SJohn Baldwin.Fa crp_op .
275c0341432SJohn BaldwinAvailable operations depend on the session's mode.
276c0341432SJohn Baldwin.Pp
277c0341432SJohn BaldwinCompression requests support the following operations:
278c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_DECOMPRESS
279c0341432SJohn Baldwin.It Dv CRYPTO_OP_COMPRESS
280c0341432SJohn BaldwinCompress the data in the payload region of the data buffer.
281c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECOMPRESS
282c0341432SJohn BaldwinDecompress the data in the payload region of the data buffer.
283c0341432SJohn Baldwin.El
284c0341432SJohn Baldwin.Pp
285c0341432SJohn BaldwinCipher requests support the following operations:
286c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_DECRYPT
287c0341432SJohn Baldwin.It Dv CRYPTO_OP_ENCRYPT
288c0341432SJohn BaldwinEncrypt the data in the payload region of the data buffer.
289c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECRYPT
290c0341432SJohn BaldwinDecrypt the data in the payload region of the data buffer.
291c0341432SJohn Baldwin.El
292c0341432SJohn Baldwin.Pp
293c0341432SJohn BaldwinDigest requests support the following operations:
294c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP_COMPUTE_DIGEST
295c0341432SJohn Baldwin.It Dv CRYPTO_OP_COMPUTE_DIGEST
296c0341432SJohn BaldwinCalculate a digest over the payload region of the data buffer
297c0341432SJohn Baldwinand store the result in the digest region.
298c0341432SJohn Baldwin.It Dv CRYPTO_OP_VERIFY_DIGEST
299c0341432SJohn BaldwinCalculate a digest over the payload region of the data buffer.
300c0341432SJohn BaldwinCompare the calculated digest to the existing digest from the digest region.
301c0341432SJohn BaldwinIf the digests match,
302c0341432SJohn Baldwincomplete the request successfully.
303c0341432SJohn BaldwinIf the digests do not match,
304c0341432SJohn Baldwinfail the request with
305c0341432SJohn Baldwin.Er EBADMSG .
306c0341432SJohn Baldwin.El
307c0341432SJohn Baldwin.Pp
308c0341432SJohn BaldwinAEAD and Encrypt-then-Authenticate requests support the following
309c0341432SJohn Baldwinoperations:
310c0341432SJohn Baldwin.Bl -tag -width CRYPTO_OP
311c0341432SJohn Baldwin.It Dv CRYPTO_OP_ENCRYPT | Dv CRYPTO_OP_COMPUTE_DIGEST
312c0341432SJohn BaldwinEncrypt the data in the payload region of the data buffer.
313c0341432SJohn BaldwinCalculate a digest over the AAD and payload regions and store the
314c0341432SJohn Baldwinresult in the data buffer.
315c0341432SJohn Baldwin.It Dv CRYPTO_OP_DECRYPT | Dv CRYPTO_OP_VERIFY_DIGEST
316c0341432SJohn BaldwinCalculate a digest over the AAD and payload regions of the data buffer.
317c0341432SJohn BaldwinCompare the calculated digest to the existing digest from the digest region.
318c0341432SJohn BaldwinIf the digests match,
319c0341432SJohn Baldwindecrypt the payload region.
320c0341432SJohn BaldwinIf the digests do not match,
321c0341432SJohn Baldwinfail the request with
322c0341432SJohn Baldwin.Er EBADMSG .
323c0341432SJohn Baldwin.El
3249b774dc0SJohn Baldwin.Ss Request AAD
3259b774dc0SJohn BaldwinAEAD and Encrypt-then-Authenticate requests may optionally include
3269b774dc0SJohn BaldwinAdditional Authenticated Data.
3279b774dc0SJohn BaldwinAAD may either be supplied in the AAD region of the input buffer or
3289b774dc0SJohn Baldwinas a single buffer pointed to by
3299b774dc0SJohn Baldwin.Fa crp_aad .
3309b774dc0SJohn BaldwinIn either case,
3319b774dc0SJohn Baldwin.Fa crp_aad_length
3329b774dc0SJohn Baldwinalways indicates the amount of AAD in bytes.
3337e89ae49SMarcin Wojtas.Ss Request ESN
3347e89ae49SMarcin WojtasIPsec requests may optionally include Extended Sequence Numbers (ESN).
3357e89ae49SMarcin WojtasESN may either be supplied in
3367e89ae49SMarcin Wojtas.Fa crp_esn
3377e89ae49SMarcin Wojtasor as part of the AAD pointed to by
3387e89ae49SMarcin Wojtas.Fa crp_aad .
3397e89ae49SMarcin Wojtas.Pp
3407e89ae49SMarcin WojtasIf the ESN is stored in
3417e89ae49SMarcin Wojtas.Fa crp_esn ,
3427e89ae49SMarcin Wojtas.Dv CSP_F_ESN
3437e89ae49SMarcin Wojtasshould be set in
3447e89ae49SMarcin Wojtas.Fa csp_flags .
3457e89ae49SMarcin WojtasThis use case is dedicated for encrypt and authenticate mode, since the
3467e89ae49SMarcin Wojtashigh-order 32 bits of the sequence number are appended after the Next Header
3477e89ae49SMarcin Wojtas(RFC 4303).
3487e89ae49SMarcin Wojtas.Pp
3497e89ae49SMarcin WojtasAEAD modes supply the ESN in a separate AAD buffer (see e.g. RFC 4106, Chapter 5
3507e89ae49SMarcin WojtasAAD Construction).
351c0341432SJohn Baldwin.Ss Request IV and/or Nonce
352c0341432SJohn BaldwinSome cryptographic operations require an IV or nonce as an input.
353c0341432SJohn BaldwinAn IV may be stored either in the IV region of the data buffer or in
354c0341432SJohn Baldwin.Fa crp_iv .
355c0341432SJohn BaldwinBy default,
356c0341432SJohn Baldwinthe IV is assumed to be stored in the IV region.
357c0341432SJohn BaldwinIf the IV is stored in
358c0341432SJohn Baldwin.Fa crp_iv ,
359c0341432SJohn Baldwin.Dv CRYPTO_F_IV_SEPARATE
360c0341432SJohn Baldwinshould be set in
361c0341432SJohn Baldwin.Fa crp_flags
362c0341432SJohn Baldwinand
3639c0e3d3aSJohn Baldwin.Fa crp_iv_start
364c0341432SJohn Baldwinshould be left as zero.
365c0341432SJohn Baldwin.Pp
366c0341432SJohn BaldwinRequests that store part, but not all, of the IV in the data buffer should
367c0341432SJohn Baldwinstore the partial IV in the data buffer and pass the full IV separately in
368c0341432SJohn Baldwin.Fa crp_iv .
369c0341432SJohn Baldwin.Ss Request and Callback Scheduling
370c0341432SJohn BaldwinThe crypto framework provides multiple methods of scheduling the dispatch
371c0341432SJohn Baldwinof requests to drivers along with the processing of driver callbacks.
37268f6800cSMark JohnstonThe
373c0341432SJohn Baldwin.Fn crypto_dispatch ,
37468f6800cSMark Johnston.Fn crypto_dispatch_async ,
37568f6800cSMark Johnstonand
37668f6800cSMark Johnston.Fn crypto_dispatch_batch
37768f6800cSMark Johnstonfunctions can be used to request different dispatch scheduling policies.
37868f6800cSMark Johnston.Pp
37968f6800cSMark Johnston.Fn crypto_dispatch
38068f6800cSMark Johnstonsynchronously passes the request to the driver.
38168f6800cSMark JohnstonThe driver itself may process the request synchronously or asynchronously
38268f6800cSMark Johnstondepending on whether the driver is implemented by software or hardware.
38368f6800cSMark Johnston.Pp
38468f6800cSMark Johnston.Fn crypto_dispatch_async
38568f6800cSMark Johnstondispatches the request asynchronously.
38668f6800cSMark JohnstonIf the driver is inherently synchronous, the request is queued to a taskqueue
38768f6800cSMark Johnstonbacked by a pool of worker threads.
38868f6800cSMark JohnstonThis can increase througput by allowing requests from a single producer to be
38968f6800cSMark Johnstonprocessed in parallel.
39068f6800cSMark JohnstonBy default the pool is sized to provide one thread for each CPU.
39168f6800cSMark JohnstonWorker threads dequeue requests and pass them to the driver asynchronously.
39268f6800cSMark Johnston.Fn crypto_dispatch_async
39368f6800cSMark Johnstonadditionally takes a
39468f6800cSMark Johnston.Va flags
39568f6800cSMark Johnstonparameter.
39668f6800cSMark JohnstonThe
39768f6800cSMark Johnston.Dv CRYPTO_ASYNC_ORDERED
39868f6800cSMark Johnstonflag indicates that completion callbacks for requests must be called in the
39968f6800cSMark Johnstonsame order as requests were dispatched.
40068f6800cSMark JohnstonIf the driver is asynchronous, the behavior of
40168f6800cSMark Johnston.Fn crypto_dispatch_async
40268f6800cSMark Johnstonis identical to that of
40368f6800cSMark Johnston.Fn crypto_dispatch .
40468f6800cSMark Johnston.Pp
40568f6800cSMark Johnston.Fn crypto_dispatch_batch
40668f6800cSMark Johnstonallows the caller to collect a batch of requests and submit them to the driver
40768f6800cSMark Johnstonat the same time.
40868f6800cSMark JohnstonThis allows hardware drivers to optimize the scheduling of request processing
40968f6800cSMark Johnstonand batch completion interrupts.
41068f6800cSMark JohnstonA batch is submitted to the driver by invoking the driver's process method on
41168f6800cSMark Johnstoneach request, specifying
412c0341432SJohn Baldwin.Dv CRYPTO_HINT_MORE
41368f6800cSMark Johnstonwith each request except for the last.
41468f6800cSMark JohnstonThe
41568f6800cSMark Johnston.Fa flags
41668f6800cSMark Johnstonparameter to
41768f6800cSMark Johnston.Fn crypto_dispatch_batch
41868f6800cSMark Johnstonis currently ignored.
419c0341432SJohn Baldwin.Pp
420c0341432SJohn BaldwinCallback function scheduling is simpler than request scheduling.
421c0341432SJohn BaldwinCallbacks can either be invoked synchronously from
422c0341432SJohn Baldwin.Fn crypto_done ,
423c0341432SJohn Baldwinor they can be queued to a pool of worker threads.
424c0341432SJohn BaldwinThis pool of worker threads is also sized to provide one worker thread
425c0341432SJohn Baldwinfor each CPU by default.
426c0341432SJohn BaldwinNote that a callback function invoked synchronously from
427c0341432SJohn Baldwin.Fn crypto_done
428c0341432SJohn Baldwinmust follow the same restrictions placed on threaded interrupt handlers.
429c0341432SJohn Baldwin.Pp
430c0341432SJohn BaldwinBy default,
431c0341432SJohn Baldwincallbacks are invoked asynchronously by a worker thread.
432c0341432SJohn BaldwinIf
433c0341432SJohn Baldwin.Dv CRYPTO_F_CBIMM
434c0341432SJohn Baldwinis set,
435c0341432SJohn Baldwinthe callback is always invoked synchronously from
436c0341432SJohn Baldwin.Fn crypto_done .
437c0341432SJohn BaldwinIf
438c0341432SJohn Baldwin.Dv CRYPTO_F_CBIFSYNC
439c0341432SJohn Baldwinis set,
440c0341432SJohn Baldwinthe callback is invoked synchronously if the request was processed by a
441c0341432SJohn Baldwinsoftware driver or asynchronously if the request was processed by a
442c0341432SJohn Baldwinhardware driver.
443c0341432SJohn Baldwin.Pp
444*002c47baSAlan SomersIf a request was scheduled to the taskqueue with
445*002c47baSAlan Somers.Dv CRYPTO_ASYNC_ORDERED ,
446c0341432SJohn Baldwincallbacks are always invoked asynchronously ignoring
447c0341432SJohn Baldwin.Dv CRYPTO_F_CBIMM
448c0341432SJohn Baldwinand
449c0341432SJohn Baldwin.Dv CRYPTO_F_CBIFSYNC .
450c0341432SJohn BaldwinThis flag is used by IPsec to ensure that decrypted network packets are
451c0341432SJohn Baldwinpassed up the network stack in roughly the same order they were received.
452c0341432SJohn Baldwin.Ss Other Request Fields
453c0341432SJohn BaldwinIn addition to the fields and flags enumerated above,
454c0341432SJohn Baldwin.Vt struct cryptop
455c0341432SJohn Baldwinincludes the following:
456c0341432SJohn Baldwin.Bl -tag -width crp_payload_length
457c0341432SJohn Baldwin.It Fa crp_session
458c0341432SJohn BaldwinA reference to the active session.
459c0341432SJohn BaldwinThis is set when the request is created by
460c0341432SJohn Baldwin.Fn crypto_getreq
461c0341432SJohn Baldwinand should not be modified.
462c0341432SJohn BaldwinDrivers can use this to fetch driver-specific session state or
463c0341432SJohn Baldwinsession parameters.
464c0341432SJohn Baldwin.It Fa crp_etype
465c0341432SJohn BaldwinError status.
466c0341432SJohn BaldwinEither zero on success, or an error if a request fails.
467c0341432SJohn BaldwinSet by drivers prior to completing a request via
468c0341432SJohn Baldwin.Fn crypto_done .
469c0341432SJohn Baldwin.It Fa crp_flags
470c0341432SJohn BaldwinA bitmask of flags.
471c0341432SJohn BaldwinThe following flags are available in addition to flags discussed previously:
472c0341432SJohn Baldwin.Bl -tag -width CRYPTO_F_DONE
473c0341432SJohn Baldwin.It Dv CRYPTO_F_DONE
474c0341432SJohn BaldwinSet by
475c0341432SJohn Baldwin.Fa crypto_done
476c0341432SJohn Baldwinbefore calling
477c0341432SJohn Baldwin.Fa crp_callback .
478c0341432SJohn BaldwinThis flag is not very useful and will likely be removed in the future.
479c0341432SJohn BaldwinIt can only be safely checked from the callback routine at which point
480c0341432SJohn Baldwinit is always set.
481c0341432SJohn Baldwin.El
482c0341432SJohn Baldwin.It Fa crp_cipher_key
483c0341432SJohn BaldwinPointer to a request-specific encryption key.
484c0341432SJohn BaldwinIf this value is not set,
485c0341432SJohn Baldwinthe request uses the session encryption key.
486c0341432SJohn Baldwin.It Fa crp_auth_key
487c0341432SJohn BaldwinPointer to a request-specific authentication key.
488c0341432SJohn BaldwinIf this value is not set,
489c0341432SJohn Baldwinthe request uses the session authentication key.
490c0341432SJohn Baldwin.It Fa crp_opaque
491c0341432SJohn BaldwinAn opaque pointer.
492c0341432SJohn BaldwinThis pointer permits users of the cryptographic framework to store
493c0341432SJohn Baldwininformation about a request to be used in the callback.
494c0341432SJohn Baldwin.It Fa crp_callback
495c0341432SJohn BaldwinCallback function.
496c0341432SJohn BaldwinThis must point to a callback function of type
497c0341432SJohn Baldwin.Vt void (*)(struct cryptop *) .
498c0341432SJohn BaldwinThe callback function should inspect
499c0341432SJohn Baldwin.Fa crp_etype
500c0341432SJohn Baldwinto determine the status of the completed operation.
501c0341432SJohn BaldwinIt should also arrange for the request to be freed via
502c0341432SJohn Baldwin.Fn crypto_freereq .
5039c0e3d3aSJohn Baldwin.It Fa crp_olen
5049c0e3d3aSJohn BaldwinUsed with compression and decompression requests to describe the updated
5059c0e3d3aSJohn Baldwinlength of the payload region in the data buffer.
5069c0e3d3aSJohn Baldwin.Pp
5079c0e3d3aSJohn BaldwinIf a compression request increases the size of the payload,
5089c0e3d3aSJohn Baldwinthen the data buffer is unmodified, the request completes successfully,
5099c0e3d3aSJohn Baldwinand
5109c0e3d3aSJohn Baldwin.Fa crp_olen
5119c0e3d3aSJohn Baldwinis set to the size the compressed data would have used.
5129c0e3d3aSJohn BaldwinCallers can compare this to the payload region length to determine if
5139c0e3d3aSJohn Baldwinthe compressed data was discarded.
514c0341432SJohn Baldwin.El
515c0341432SJohn Baldwin.Sh RETURN VALUES
516c0341432SJohn Baldwin.Fn crypto_dispatch
517c0341432SJohn Baldwinreturns an error if the request contained invalid fields,
518c0341432SJohn Baldwinor zero if the request was valid.
519c0341432SJohn Baldwin.Fn crypto_getreq
520c0341432SJohn Baldwinreturns a pointer to a new request structure on success,
521c0341432SJohn Baldwinor
522c0341432SJohn Baldwin.Dv NULL
523c0341432SJohn Baldwinon failure.
524c0341432SJohn Baldwin.Dv NULL
525c0341432SJohn Baldwincan only be returned if
526c0341432SJohn Baldwin.Dv M_NOWAIT
527c0341432SJohn Baldwinwas passed in
528c0341432SJohn Baldwin.Fa how .
529c0341432SJohn Baldwin.Sh SEE ALSO
530c0341432SJohn Baldwin.Xr ipsec 4 ,
531c0341432SJohn Baldwin.Xr crypto 7 ,
532c0341432SJohn Baldwin.Xr crypto 9 ,
533c0341432SJohn Baldwin.Xr crypto_session 9 ,
53432068667SChristian Brueffer.Xr mbuf 9 ,
535c0341432SJohn Baldwin.Xr uio 9
536c0341432SJohn Baldwin.Sh BUGS
537c0341432SJohn BaldwinNot all drivers properly handle mixing session and per-request keys
538c0341432SJohn Baldwinwithin a single session.
539c0341432SJohn BaldwinConsumers should either use a single key for a session specified in
540c0341432SJohn Baldwinthe session parameters or always use per-request keys.
541