[PATCH 2/3] crypto: adiantum - Derive the subkeys with the ChaCha library

From: Laveesh Bansal

Date: Mon Nov 30 2026 - 00:56:32 EST


adiantum_setkey() derives K_E and K_H with a crypto_skcipher request
on the stack. SYNC_SKCIPHER_REQUEST_ON_STACK() reserves much more
stack than this needs.

Derive the subkeys with the ChaCha library instead. This replaces
the skcipher request on the stack with a much smaller ChaCha state.
Drop the request-size checks that only guarded that request.

Suggested-by: Eric Biggers <ebiggers@xxxxxxxxxx>
Signed-off-by: Laveesh Bansal <laveeshb@xxxxxxxxxxxxxxxxx>
---
Notes:

1. There are already two places where we have similar code to do
xchacha initialization --
xchacha_init() in lib/crypto/chacha20poly1305.c and
crypto_xchacha_crypt() in crypto/chacha.c.

To keep the patch focused, I have added another helper
that does similar logic --
adiantum_xchacha_init() in crypto/adiantum.c.

We should consolidate these into chacha library as a follow up.

2. Test: I tested this patch on arm64, x86_64 and big-endian s390x;
all on QEMU. I had the following in all 3 setups -- KASAN, lockdep
and CRYPTO_SELFTESTS enabled. I applied the setkey, aeslib patches
and patch 1 first (in that order).

The selftests passed for both xchacha12 and xchacha20.
I ran random key test of 400 cases per algorithm. They gave the same
digests as without this patch.

On arm64 and x86_64 -- I did not see any KASAN or lockdep reports.

On s390x -- I found one report in KASAN build of an out-of-bounds
read in aes_decrypt_generic() and lockdep was disabled after it.
The same happened on the unpatched base, so the result is unchanged
with my patch.

crypto/adiantum.c | 75 ++++++++++++++++++++++++++++-------------------
1 file changed, 45 insertions(+), 30 deletions(-)

diff --git a/crypto/adiantum.c b/crypto/adiantum.c
index 5e16d2c4699b6..f53d6d5f56fb9 100644
--- a/crypto/adiantum.c
+++ b/crypto/adiantum.c
@@ -42,11 +42,14 @@

struct adiantum_instance_ctx {
struct crypto_skcipher_spawn streamcipher_spawn;
+ int nrounds;
};

struct adiantum_tfm_ctx {
struct aes_key aes;
struct crypto_skcipher *streamcipher;
+ u32 stream_key[CHACHA_KEY_WORDS];
+ int nrounds;
union {
struct {
struct poly1305_core_key header_hash_key;
@@ -86,6 +89,25 @@ struct adiantum_request_ctx {
} u;
};

+/* The IV is the 24-byte nonce followed by the 8-byte stream position. */
+static void adiantum_xchacha_init(struct chacha_state *state,
+ const u32 key[CHACHA_KEY_WORDS], int nrounds,
+ const u8 iv[XCHACHA_IV_SIZE])
+{
+ u32 subkey[HCHACHA_OUT_WORDS];
+ u8 real_iv[CHACHA_IV_SIZE];
+
+ chacha_init(state, key, iv);
+ hchacha_block(state, subkey, nrounds);
+
+ memcpy(&real_iv[0], iv + 24, 8);
+ memcpy(&real_iv[8], iv + 16, 8);
+
+ chacha_init(state, subkey, real_iv);
+
+ memzero_explicit(subkey, sizeof(subkey));
+}
+
/*
* Given the XChaCha stream key K_S, derive the block cipher key K_E and the
* hash key K_H as follows:
@@ -99,11 +121,9 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
unsigned int keylen)
{
struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
- SYNC_SKCIPHER_REQUEST_ON_STACK(
- req, (struct crypto_sync_skcipher *)tctx->streamcipher);
u8 poly_keys[2 * POLY1305_BLOCK_SIZE];
u8 iv[XCHACHA_IV_SIZE] = { 1 };
- struct scatterlist sg;
+ struct chacha_state state;
int err;

/* Set the stream cipher key (K_S) */
@@ -116,14 +136,13 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
return err;

/* Derive the subkeys */
+ for (int i = 0; i < CHACHA_KEY_WORDS; i++)
+ tctx->stream_key[i] = get_unaligned_le32(key + 4 * i);
memset(tctx->derived_keys, 0, sizeof(tctx->derived_keys));
- sg_init_one(&sg, tctx->derived_keys, sizeof(tctx->derived_keys));
- skcipher_request_set_callback(req, 0, NULL, NULL);
- skcipher_request_set_crypt(req, &sg, &sg, sizeof(tctx->derived_keys),
- iv);
- err = crypto_skcipher_encrypt(req);
- if (err)
- goto out;
+ adiantum_xchacha_init(&state, tctx->stream_key, tctx->nrounds, iv);
+ chacha_crypt(&state, tctx->derived_keys, tctx->derived_keys,
+ sizeof(tctx->derived_keys), tctx->nrounds);
+ chacha_zeroize_state(&state);

/* Set the block cipher key (K_E) */
err = aes_preparekey(&tctx->aes, tctx->derived_keys, AES_KEYSIZE_256);
@@ -145,7 +164,6 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
if (err)
memzero_explicit(tctx->derived_keys,
sizeof(tctx->derived_keys));
- skcipher_request_zero(req);
return err;
}

@@ -482,13 +500,9 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm)
streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn);
if (IS_ERR(streamcipher))
return PTR_ERR(streamcipher);
- if (WARN_ON(crypto_skcipher_reqsize(streamcipher) >
- MAX_SYNC_SKCIPHER_REQSIZE)) {
- crypto_free_skcipher(streamcipher);
- return -EINVAL;
- }

tctx->streamcipher = streamcipher;
+ tctx->nrounds = ictx->nrounds;

BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
sizeof(struct adiantum_request_ctx));
@@ -516,23 +530,24 @@ static void adiantum_free_instance(struct skcipher_instance *inst)
}

/*
- * Check for a supported set of inner algorithms.
+ * Check for a supported set of inner algorithms and return
+ * the number of XChaCha rounds, or 0 if they are not supported.
* See the comment at the beginning of this file.
*/
-static bool
-adiantum_supported_algorithms(struct skcipher_alg_common *streamcipher_alg,
- const char *blockcipher_name)
+static int
+adiantum_parse_algorithms(struct skcipher_alg_common *streamcipher_alg,
+ const char *blockcipher_name)
{
- if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") != 0 &&
- strcmp(streamcipher_alg->base.cra_name, "xchacha20") != 0)
- return false;
-
if (strcmp(blockcipher_name, "aes") != 0 &&
/* This is to support the usage by testmgr. */
strcmp(blockcipher_name, "aes-lib") != 0)
- return false;
+ return 0;

- return true;
+ if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") == 0)
+ return 12;
+ if (strcmp(streamcipher_alg->base.cra_name, "xchacha20") == 0)
+ return 20;
+ return 0;
}

static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
@@ -557,8 +572,7 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
err = crypto_grab_skcipher(&ictx->streamcipher_spawn,
skcipher_crypto_instance(inst),
crypto_attr_alg_name(tb[1]), 0,
- mask | CRYPTO_ALG_ASYNC /* sync only */ |
- CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE);
+ mask | CRYPTO_ALG_ASYNC /* sync only */);
if (err)
goto err_free_inst;
streamcipher_alg = crypto_spawn_skcipher_alg_common(&ictx->streamcipher_spawn);
@@ -581,8 +595,9 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
}

/* Check the set of algorithms */
- if (!adiantum_supported_algorithms(streamcipher_alg,
- blockcipher_name)) {
+ ictx->nrounds = adiantum_parse_algorithms(streamcipher_alg,
+ blockcipher_name);
+ if (!ictx->nrounds) {
err = -ENOENT;
goto err_free_inst;
}
--
2.43.0