Re: [PATCH 3/3] crypto: adiantum - Use the ChaCha library for the stream cipher
From: Eric Biggers
Date: Mon Nov 30 2026 - 14:40:24 EST
On Sun, Nov 29, 2026 at 09:55:39PM -0800, Laveesh Bansal wrote:
> adiantum_crypt() runs the XChaCha step through a child crypto_skcipher.
> That needs a spawn, a child tfm and a request sized for the child.
>
> Call chacha_crypt() directly instead and remove the child. The library
> works on plain buffers, so adiantum walks the source and destination
> scatterlists itself.
>
> Suggested-by: Eric Biggers <ebiggers@xxxxxxxxxx>
> Signed-off-by: Laveesh Bansal <laveeshb@xxxxxxxxxxxxxxxxx>
> ---
> Notes:
>
> I tested this patch in the same environments as patch 2.
>
> I also ran a test module that splits src and dst at random offsets -
> in place and out of place - and compares the result with a plain
> buffer. They match. This one I did on arm64 and x86_64.
>
> Another one I ran is the tcrypt adiantum speed test;
> I did this on arm64 in a KVM guest with debug options off.
> I did not find any slowness in performance when compared
> with results on base + setkey + aeslib. I can share specific test
> results if anybody is interested.
>
> crypto/Kconfig | 3 +-
> crypto/adiantum.c | 164 ++++++++++++++++++++--------------------------
> 2 files changed, 74 insertions(+), 93 deletions(-)
>
> diff --git a/crypto/Kconfig b/crypto/Kconfig
> index 7cac0141a73cb..93996d1240380 100644
> --- a/crypto/Kconfig
> +++ b/crypto/Kconfig
> @@ -578,7 +578,8 @@ menu "Length-preserving ciphers and modes"
>
> config CRYPTO_ADIANTUM
> tristate "Adiantum"
> - select CRYPTO_CHACHA20
> + select CRYPTO_LIB_CHACHA
> + select CRYPTO_SKCIPHER
> select CRYPTO_LIB_AES
> select CRYPTO_LIB_NH
> select CRYPTO_LIB_POLY1305
> diff --git a/crypto/adiantum.c b/crypto/adiantum.c
> index f53d6d5f56fb9..2358f61e88ce9 100644
> --- a/crypto/adiantum.c
> +++ b/crypto/adiantum.c
> @@ -41,13 +41,11 @@
> #define TWEAK_SIZE 32
>
> struct adiantum_instance_ctx {
> - struct crypto_skcipher_spawn streamcipher_spawn;
> int nrounds;
> };
Now that there are no inner algorithms ("spawns") left, it doesn't make
sense for this to be a template anymore. This file should instead just
register two crypto_skcipher algorithms,
"adiantum(xchacha12,aes)" (driver name "adiantum(xchacha12-lib,aes-lib)"), and
"adiantum(xchacha20,aes)" (driver name "adiantum(xchacha20-lib,aes-lib)").
> +static void adiantum_stream_xor(struct chacha_state *state, int nrounds,
> + struct scatterlist *src,
> + struct scatterlist *dst, unsigned int len)
> +{
> + struct scatter_walk swalk, dwalk;
> +
> + scatterwalk_start(&swalk, src);
> + scatterwalk_start(&dwalk, dst);
> + while (len) {
> + unsigned int n = min(scatterwalk_clamp(&swalk, len),
> + scatterwalk_clamp(&dwalk, len));
> +
> + /*
> + * The ChaCha counter advances in whole blocks, so every
> + * step except the last must be a whole number of blocks.
> + */
> + if (n < len)
> + n = round_down(n, CHACHA_BLOCK_SIZE);
> + if (n) {
> + scatterwalk_map(&swalk);
> + scatterwalk_map(&dwalk);
> + chacha_crypt(state, dwalk.addr, swalk.addr, n, nrounds);
> + scatterwalk_done_dst(&dwalk, n);
> + scatterwalk_done_src(&swalk, n);
> + } else {
> + u8 buf[CHACHA_BLOCK_SIZE];
> +
> + n = min(len, CHACHA_BLOCK_SIZE);
> + memcpy_from_scatterwalk(buf, &swalk, n);
> + chacha_crypt(state, buf, buf, n, nrounds);
> + memcpy_to_scatterwalk(&dwalk, buf, n);
> + }
> + len -= n;
This is not correct, since for in-place operations with CONFIG_HIGHMEM=y
the same memory is being mapped multiple times. The in-place case
either needs to be handled directly, like crypto/aes.c does, or
skcipher_walk should be used which handles this.
(Also, 'buf' is not being zeroized when it goes out of scope.)
- Eric