Commit Graph

357 Commits

Author SHA1 Message Date
Warner Losh
8bb9cd271e stand: Fix KASSERT use
Explicitly include sys/systm.h to pull in KASSERT.

Sponsored by:		Netflix
Reviewed by:		jhb
Differential Revision:	https://reviews.freebsd.org/D33855
2022-01-11 16:43:18 -07:00
John Baldwin
5022c68732 cryptosoft: Use multi-block encrypt/decrypt for ChaCha20-Poly1305.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33758
2022-01-11 14:21:31 -08:00
John Baldwin
f8580fcaa1 cryptosoft: Use multi-block encrypt/decrypt for AES-CCM.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33757
2022-01-11 14:21:00 -08:00
John Baldwin
a221a8f4a0 cryptosoft: Use multi-block encrypt/decrypt for AES-GCM.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33756
2022-01-11 14:20:32 -08:00
John Baldwin
69e533c3ff swcr_encdec: Rename blks to blksz.
This better reflects the variables purpose and matches other functions
in this file.

Requested by:	markj
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33755
2022-01-11 14:20:10 -08:00
John Baldwin
cfb7b942be cryptosoft: Use multi-block encrypt/decrypt for non-AEAD ciphers.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33531
2022-01-11 14:18:57 -08:00
John Baldwin
d7f0b3ce6d crypto: Re-add encrypt/decrypt_multi hooks to enc_xform.
These callbacks allow multiple contiguous blocks to be manipulated in
a single call.  Note that any trailing partial block for a stream
cipher must still be passed to encrypt/decrypt_last.

While here, document the setkey and reinit hooks and reorder the hooks
in 'struct enc_xform' to better reflect the life cycle.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33529
2022-01-11 14:17:41 -08:00
John Baldwin
8f35841f1f crypto: Add support for the XChaCha20-Poly1305 AEAD cipher.
This cipher is a wrapper around the ChaCha20-Poly1305 AEAD cipher
which accepts a larger nonce.  Part of the nonce is used along with
the key as an input to HChaCha20 to generate a derived key used for
ChaCha20-Poly1305.

This cipher is used by WireGuard.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33523
2022-01-11 14:16:41 -08:00
John Baldwin
74d3f1b63d OCF: Add crypto_clonereq().
This function clones an existing crypto request, but associates the
new request with a specified session.  The intended use case is for
drivers to be able to fall back to software by cloning a request and
dispatch it to an internally allocated software session.

Reviewed by:	markj
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D33607
2022-01-04 14:22:12 -08:00
John Baldwin
61ff0a1f81 /dev/crypto: Store blocksize in cse rather than txform pointer.
Reviewed by:	markj
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D33614
2021-12-29 17:50:23 -08:00
John Baldwin
c3907ef482 /dev/crypto: Minimize cipher-specific logic.
Rather than duplicating the switches in crypto_auth_hash() and
crypto_cipher(), copy the algorithm constants from the new session
ioctl into a csp directly which permits using the functions in
crypto.c.

Reviewed by:	markj
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D33613
2021-12-29 17:50:03 -08:00
John Baldwin
c3a688ef4d OCF: Hook up plain RIPEMD160 in cryptosoft and /dev/crypto.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33612
2021-12-29 16:46:48 -08:00
John Baldwin
d074adf18b cryptodev.h: Drop 'extern' from function prototypes.
Sponsored by:	Chelsio Communications
2021-12-21 12:33:49 -08:00
John Baldwin
33d56e57ec crypto: Encrypt the XORed input block for Camellia-CBC.
This fixes a regression in the previous change to move CBC chaining
into enc_xform_camellia which passed the raw input into the encrypt
function (thus not actually doing the chaining).  This still works
when using the same buffer for input and output which is why my
initial testing with cryptocheck didn't catch it.

Fixes:		f84d708b48 crypto: Move CBC handling into enc_xform_camellia.
Sponsored by:	The FreeBSD Foundation
2021-12-17 16:46:09 -08:00
John Baldwin
04781697f8 cryptosoft: Use POLY1305_BLOCK_LEN instead of a magic number.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33528
2021-12-17 13:58:59 -08:00
John Baldwin
7051c5796f cryptosoft: Consolidate calls to explicit_bzero.
Group sensitive on-stack variables into anonymous structs so that they
can be cleared with a single call to explicit_bzero rather than
multiple calls.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33527
2021-12-17 13:58:58 -08:00
John Baldwin
1c9b25a5e8 cryptosoft: Reuse 'blk' to hold the initial nonce in swcr_gmac.
Reusing the storage removes the need for an additional explicit_bzero.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33526
2021-12-17 13:58:58 -08:00
John Baldwin
49bb130765 cryptosoft: Remove unused 'iv' in swcr_ccm_cbc_mac.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33525
2021-12-17 13:58:58 -08:00
John Baldwin
2cd838281b cryptosoft: Remove CBC handling from swcr_encdec.
All of the CBC ciphers now handle block chaining in the enc_xform.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33489
2021-12-16 13:48:15 -08:00
John Baldwin
f84d708b48 crypto: Move CBC handling into enc_xform_camellia.
Add an IV to the context that is initialized by the reinit hook.
Encrypt and decrypt operations handle cipher block chaining along with
updating the IV saved in the context.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33488
2021-12-16 13:48:02 -08:00
John Baldwin
d55df8dcf7 crypto: Move CBC handling into enc_xform_aes_cbc.
Add an IV to the context that is initialized by the reinit hook.
Encrypt and decrypt operations handle cipher block chaining along with
updating the IV saved in the context.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33487
2021-12-16 13:47:44 -08:00
John Baldwin
246982c196 crypto: Consistently use AES instead of Rijndael128 for the AES-CBC cipher.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33486
2021-12-16 13:47:27 -08:00
John Baldwin
47fc049585 crypto: Define POLY1305_BLOCK_LEN constant.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33485
2021-12-16 13:47:16 -08:00
John Baldwin
aa2681752d cryptosoft: Don't treat CRYPTO_NULL_HMAC as an hmac algorithm.
Despite it's name, CRYPTO_NULL_HMAC is actually an unkeyed digest
similar to plain SHA-1 rather than a keyed HMAC algorithm.

Reported by:	syzbot+17d7c656f1a797b0faae@syzkaller.appspotmail.com
Discussed with:	markj
Fixes:		6113a08b98 cryptosoft: Fully support per-operation keys for auth algorithms.
Sponsored by:	The FreeBSD Foundation
2021-12-15 13:33:15 -08:00
John Baldwin
05a1d0f5d7 ktls: Support for TLS 1.3 receive offload.
Note that support for TLS 1.3 receive offload in OpenSSL is still an
open pull request in active development.  However, potential changes
to that pull request should not affect the kernel interface.

Reviewed by:	hselasky
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D33007
2021-12-14 11:01:05 -08:00
John Baldwin
dc475c9bee cryptosoft: Stop single-threading requests within a session.
All of the request handlers no longer modify session state, so remove
the mutex limiting operations to one per session.  In addition, change
the pointer to the session state passed to process callbacks to const.

Suggested by:	mjg
Reviewed by:	mjg, markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33317
2021-12-09 11:52:43 -08:00
John Baldwin
6113a08b98 cryptosoft: Fully support per-operation keys for auth algorithms.
Only pre-allocate auth contexts when a session-wide key is provided or
for sessions without keys.  For sessions with per-operation keys,
always initialize the on-stack context directly rather than
initializing the session context in swcr_authprepare (now removed) and
then copying that session context into the on-stack context.

This approach permits parallel auth operations without needing a
serializing lock.  In addition, the previous code assumed that auth
sessions always provided an initial key unlike cipher sessions which
assume either an initial key or per-op keys.

While here, fix the Blake2 auth transforms to function like other auth
transforms where Setkey is invoked after Init rather than before.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33316
2021-12-09 11:52:42 -08:00
John Baldwin
b54d12841e cryptosoft: Allocate cipher contexts on the stack during operations.
As is done with authentication contexts, allocate cipher contexts on
the stack while completing requests.  This permits safely dispatching
concurrent requests on a single session.  The cipher context in the
session is now only allocated when a session key is provided during
session setup to serve as a template to initialize the on-stack
context similar to auth operations.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33198
2021-12-09 11:52:42 -08:00
John Baldwin
f313909a50 cryptosoft: Rename sw_kschedule member to sw_ctx.
The cipher context isn't always a key schedule, so use a more generic
name.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33197
2021-12-09 11:52:42 -08:00
John Baldwin
ab91fb6c21 crypto: Refactor software support for AEAD ciphers.
Extend struct enc_xform to add new members to handle auth operations
for AEAD ciphers.  In particular, AEAD operations in cryptosoft no
longer use a struct auth_hash.  Instead, the setkey and reinit methods
of struct enc_xform are responsible for initializing both the cipher
and auth state.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33196
2021-12-09 11:52:42 -08:00
John Baldwin
356c922f74 GMAC: Reset initial hash value and counter in AES_GMAC_Reinit().
Previously, these values were only cleared in AES_GMAC_Init(), so a
second set of operations could reuse the final hash as the initial
hash.  Currently this bug does not trigger in cryptosoft as existing
GMAC and GCM operations always use an on-stack auth context
initialized from a template context.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33315
2021-12-09 11:52:42 -08:00
John Baldwin
c172a407fb cryptosoft: Reject AES-CCM/GCM sessions with invalid key lengths.
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33195
2021-12-09 11:52:41 -08:00
John Baldwin
6e17a2e00d crypto: Validate AES-GCM IV length in check_csp().
This centralizes the check for valid nonce lengths for AES-GCM.

While here, remove some duplicate checks for valid AES-GCM tag lengths
from ccp(4) and ccr(4).

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33194
2021-12-09 11:52:41 -08:00
John Baldwin
ec498562b7 crypto: Don't assert for empty output buffers.
It is always valid for crp_payload_output_start to be 0.  However, if
an output buffer is empty (e.g. a decryption request with a tag but an
empty payload), the existing assertion failed since 0 is not less than
0.

Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33193
2021-12-09 11:52:41 -08:00
Scott Long
4d54d1b7fd Fix "set but not used" in opencrypto, with a correction for the previous
commit.

Sponsored by: Rubicon Communications, LLC ("Netgate")
2021-12-05 15:30:36 -07:00
Scott Long
5a052b6114 Fix "set but not used" in opencrypto
Sponsored by: Rubicon Communications, LLC ("Netgate")
2021-12-05 15:25:44 -07:00
John Baldwin
16bea05ac3 ktls: Split encrypt vs decrypt OCF counters.
Reviewed by:	gallatin, markj
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D33006
2021-11-16 09:58:52 -08:00
John Baldwin
522a2aa761 Drop "All rights reserved" from a Netflix copyright.
Reviewed by:	imp
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D32778
2021-11-11 14:41:16 -08:00
John Baldwin
442ad83e38 crypto: Don't assert on valid IV length for Chacha20-Poly1305.
The assertion checking for valid IV lengths added in 1833d6042c
was not properly updated to permit an IV length of 8 in commit
42dcd39528.

Reported by:	syzbot+f0c0559b8be1d6eb28c7@syzkaller.appspotmail.com
Reviewed by:	markj
Fixes:		42dcd39528 crypto: Support Chacha20-Poly1305 with a nonce size of 8 bytes.
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32860
2021-11-09 10:52:30 -08:00
Hans Petter Selasky
63378c84b4 Remove unused ocf_operation structure from stack.
Reviewed by:	jhb@
MFC after:	1 week
Sponsored by:	NVIDIA Networking
2021-11-04 18:52:06 +01:00
John Baldwin
4e057806cf crypto: Cleanup mtx_init() calls.
Don't pass the same name to multiple mutexes while using unique types
for WITNESS.  Just use the unique types as the mutex names.

Reviewed by:	markj
MFC after:	1 week
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D32740
2021-11-02 12:18:05 -07:00
John Baldwin
7178578192 crypto: Use a single "crypto" kproc for all of the OCF kthreads.
Reported by:	julian
Reviewed by:	markj
MFC after:	1 week
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D32739
2021-11-02 12:18:05 -07:00
John Baldwin
4827bf76bc ktls: Fix assertion for TLS 1.0 CBC when using non-zero starting seqno.
The starting sequence number used to verify that TLS 1.0 CBC records
are encrypted in-order in the OCF layer was always set to 0 and not to
the initial sequence number from the struct tls_enable.

In practice, OpenSSL always starts TLS transmit offload with a
sequence number of zero, so this only matters for tests that use a
random starting sequence number.

Reviewed by:	markj
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D32676
2021-10-27 16:35:56 -07:00
John Baldwin
b33ff94123 ktls: Change struct ktls_session.cipher to an OCF-specific type.
As a followup to SW KTLS assuming an OCF backend, rename
struct ocf_session to struct ktls_ocf_session and forward
declare it in <sys/ktls.h> to use as the type of
struct ktls_session.cipher.

Reviewed by:	gallatin, hselasky
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D32565
2021-10-21 09:36:53 -07:00
Kyle Evans
d586c978b9 stand: fix build after recent opencrypto changes
Pass the ivlen along through, and just drop this KASSERT() if we're
building _STANDALONE for the time being.

Fixes:	1833d6042c ("crypto: Permit variable-sized IVs ...")
2021-10-06 20:23:44 -05:00
John Baldwin
42dcd39528 crypto: Support Chacha20-Poly1305 with a nonce size of 8 bytes.
This is useful for WireGuard which uses a nonce of 8 bytes rather
than the 12 bytes used for IPsec and TLS.

Note that this also fixes a (should be) harmless bug in ossl(4) where
the counter was incorrectly treated as a 64-bit counter instead of a
32-bit counter in terms of wrapping when using a 12 byte nonce.
However, this required a single message (TLS record) longer than 64 *
(2^32 - 1) bytes (about 256 GB) to trigger.

Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32122
2021-10-06 14:08:49 -07:00
John Baldwin
4361c4eb6e cryptosoft: Fix support for variable tag lengths in AES-CCM.
The tag length is included as one of the values in the flags byte of
block 0 passed to CBC_MAC, so merely copying the first N bytes is
insufficient.

To avoid adding more sideband data to the CBC MAC software context,
pull the generation of block 0, the AAD length, and AAD padding out of
cbc_mac.c and into cryptosoft.c.  This matches how GCM/GMAC are
handled where the length block is constructed in cryptosoft.c and
passed as an input to the Update callback.  As a result, the CBC MAC
Update() routine is now much simpler and simply performs the
XOR-and-encrypt step on each input block.

While here, avoid a copy to the staging block in the Update routine
when one or more full blocks are passed as input to the Update
callback.

Reviewed by:	sef
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32120
2021-10-06 14:08:48 -07:00
John Baldwin
ae18720d27 crypto: Support multiple nonce lengths for AES-CCM.
Permit nonces of lengths 7 through 13 in the OCF framework and the
cryptosoft driver.  A helper function (ccm_max_payload_length) can be
used in OCF drivers to reject CCM requests which are too large for the
specified nonce length.

Reviewed by:	sef
Sponsored by:	Chelsio Communications, The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32111
2021-10-06 14:08:47 -07:00
John Baldwin
a0cbcbb791 cryptodev: Allow some CIOCCRYPT operations with an empty payload.
If an operation would generate a MAC output (e.g. for digest operation
or for an AEAD or EtA operation), then an empty payload buffer is
valid.  Only reject requests with an empty buffer for "plain" cipher
sessions.

Some of the AES-CCM NIST KAT vectors use an empty payload.

While here, don't advance crp_payload_start for requests that use an
empty payload with an inline IV. (*)

Reported by:	syzbot+d4b94fbd9a44b032f428@syzkaller.appspotmail.com (*)
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32109
2021-10-06 14:08:47 -07:00
John Baldwin
70dbebea12 cryptodev: Permit CIOCCRYPT for AEAD ciphers.
A request without AAD for an AEAD cipher can be submitted via
CIOCCRYPT rather than CIOCCRYPTAEAD.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32108
2021-10-06 14:08:47 -07:00