[PATCH 3/3] crypto: adiantum - Use the ChaCha library for the stream cipher
From: Laveesh Bansal
Date: Mon Nov 30 2026 - 00:56:49 EST
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;
};
struct adiantum_tfm_ctx {
struct aes_key aes;
- struct crypto_skcipher *streamcipher;
u32 stream_key[CHACHA_KEY_WORDS];
int nrounds;
union {
@@ -78,17 +76,6 @@ struct nhpoly1305_ctx {
__le64 nh_hash[NH_NUM_PASSES];
};
-struct adiantum_request_ctx {
- /*
- * skcipher sub-request size is unknown at compile-time, so it needs to
- * go after the members with known sizes.
- */
- union {
- struct nhpoly1305_ctx hash_ctx;
- struct skcipher_request streamcipher_req;
- } 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,
@@ -126,15 +113,6 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
struct chacha_state state;
int err;
- /* Set the stream cipher key (K_S) */
- crypto_skcipher_clear_flags(tctx->streamcipher, CRYPTO_TFM_REQ_MASK);
- crypto_skcipher_set_flags(tctx->streamcipher,
- crypto_skcipher_get_flags(tfm) &
- CRYPTO_TFM_REQ_MASK);
- err = crypto_skcipher_setkey(tctx->streamcipher, key, keylen);
- if (err)
- return err;
-
/* Derive the subkeys */
for (int i = 0; i < CHACHA_KEY_WORDS; i++)
tctx->stream_key[i] = get_unaligned_le32(key + 4 * i);
@@ -347,27 +325,63 @@ static void adiantum_hash_message(struct skcipher_request *req,
{
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
- struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
+ struct nhpoly1305_ctx *hash_ctx = skcipher_request_ctx(req);
unsigned int len = req->cryptlen - AES_BLOCK_SIZE;
struct scatter_walk walk;
- nhpoly1305_init(&rctx->u.hash_ctx);
+ nhpoly1305_init(hash_ctx);
scatterwalk_start(&walk, sgl);
while (len) {
unsigned int n = scatterwalk_next(&walk, len);
- nhpoly1305_update(&rctx->u.hash_ctx, tctx, walk.addr, n);
+ nhpoly1305_update(hash_ctx, tctx, walk.addr, n);
scatterwalk_done_src(&walk, n);
len -= n;
}
- nhpoly1305_final(&rctx->u.hash_ctx, tctx, out);
+ nhpoly1305_final(hash_ctx, tctx, out);
+}
+
+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;
+ }
}
static int adiantum_crypt(struct skcipher_request *req, bool enc)
{
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
- struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
+ struct nhpoly1305_ctx *hash_ctx = skcipher_request_ctx(req);
const unsigned int bulk_len = req->cryptlen - AES_BLOCK_SIZE;
struct scatterlist *src = req->src, *dst = req->dst;
/*
@@ -384,8 +398,8 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
le128 bignum; /* interpret as element of Z/(2^{128}Z) */
} rbuf;
le128 header_hash, msg_hash;
+ struct chacha_state state;
unsigned int stream_len;
- int err;
if (req->cryptlen < AES_BLOCK_SIZE)
return -EINVAL;
@@ -401,9 +415,9 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
/* Fast path for single-page source */
void *virt = kmap_local_page(sg_page(src)) + src->offset;
- nhpoly1305_init(&rctx->u.hash_ctx);
- nhpoly1305_update(&rctx->u.hash_ctx, tctx, virt, bulk_len);
- nhpoly1305_final(&rctx->u.hash_ctx, tctx, &msg_hash);
+ nhpoly1305_init(hash_ctx);
+ nhpoly1305_update(hash_ctx, tctx, virt, bulk_len);
+ nhpoly1305_final(hash_ctx, tctx, &msg_hash);
memcpy(&rbuf.bignum, virt + bulk_len, sizeof(le128));
kunmap_local(virt);
} else {
@@ -439,14 +453,11 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen)
stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE);
- skcipher_request_set_tfm(&rctx->u.streamcipher_req, tctx->streamcipher);
- skcipher_request_set_crypt(&rctx->u.streamcipher_req, req->src,
- req->dst, stream_len, &rbuf);
- skcipher_request_set_callback(&rctx->u.streamcipher_req,
- req->base.flags, NULL, NULL);
- err = crypto_skcipher_encrypt(&rctx->u.streamcipher_req);
- if (err)
- return err;
+ adiantum_xchacha_init(&state, tctx->stream_key, tctx->nrounds,
+ rbuf.bytes);
+ adiantum_stream_xor(&state, tctx->nrounds, req->src, req->dst,
+ stream_len);
+ chacha_zeroize_state(&state);
/* If decrypting, decrypt C_M with the block cipher to get P_M */
if (!enc)
@@ -464,9 +475,9 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
struct page *page = sg_page(dst);
void *virt = kmap_local_page(page) + dst->offset;
- nhpoly1305_init(&rctx->u.hash_ctx);
- nhpoly1305_update(&rctx->u.hash_ctx, tctx, virt, bulk_len);
- nhpoly1305_final(&rctx->u.hash_ctx, tctx, &msg_hash);
+ nhpoly1305_init(hash_ctx);
+ nhpoly1305_update(hash_ctx, tctx, virt, bulk_len);
+ nhpoly1305_final(hash_ctx, tctx, &msg_hash);
le128_sub(&rbuf.bignum, &rbuf.bignum, &msg_hash);
memcpy(virt + bulk_len, &rbuf.bignum, sizeof(le128));
flush_dcache_page(page);
@@ -495,37 +506,14 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm)
struct skcipher_instance *inst = skcipher_alg_instance(tfm);
struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
- struct crypto_skcipher *streamcipher;
- streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn);
- if (IS_ERR(streamcipher))
- return PTR_ERR(streamcipher);
-
- tctx->streamcipher = streamcipher;
tctx->nrounds = ictx->nrounds;
-
- BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
- sizeof(struct adiantum_request_ctx));
- crypto_skcipher_set_reqsize(
- tfm, max(sizeof(struct adiantum_request_ctx),
- offsetofend(struct adiantum_request_ctx,
- u.streamcipher_req) +
- crypto_skcipher_reqsize(streamcipher)));
+ crypto_skcipher_set_reqsize(tfm, sizeof(struct nhpoly1305_ctx));
return 0;
}
-static void adiantum_exit_tfm(struct crypto_skcipher *tfm)
-{
- struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
-
- crypto_free_skcipher(tctx->streamcipher);
-}
-
static void adiantum_free_instance(struct skcipher_instance *inst)
{
- struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
-
- crypto_drop_skcipher(&ictx->streamcipher_spawn);
kfree(inst);
}
@@ -535,7 +523,7 @@ static void adiantum_free_instance(struct skcipher_instance *inst)
* See the comment at the beginning of this file.
*/
static int
-adiantum_parse_algorithms(struct skcipher_alg_common *streamcipher_alg,
+adiantum_parse_algorithms(const char *streamcipher_name,
const char *blockcipher_name)
{
if (strcmp(blockcipher_name, "aes") != 0 &&
@@ -543,9 +531,11 @@ adiantum_parse_algorithms(struct skcipher_alg_common *streamcipher_alg,
strcmp(blockcipher_name, "aes-lib") != 0)
return 0;
- if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") == 0)
+ if (strcmp(streamcipher_name, "xchacha12") == 0 ||
+ strcmp(streamcipher_name, "xchacha12-lib") == 0)
return 12;
- if (strcmp(streamcipher_alg->base.cra_name, "xchacha20") == 0)
+ if (strcmp(streamcipher_name, "xchacha20") == 0 ||
+ strcmp(streamcipher_name, "xchacha20-lib") == 0)
return 20;
return 0;
}
@@ -555,7 +545,7 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
u32 mask;
struct skcipher_instance *inst;
struct adiantum_instance_ctx *ictx;
- struct skcipher_alg_common *streamcipher_alg;
+ const char *streamcipher_name;
const char *blockcipher_name;
int err;
@@ -569,13 +559,11 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
ictx = skcipher_instance_ctx(inst);
/* Stream cipher: "xchacha12" or "xchacha20" */
- err = crypto_grab_skcipher(&ictx->streamcipher_spawn,
- skcipher_crypto_instance(inst),
- crypto_attr_alg_name(tb[1]), 0,
- mask | CRYPTO_ALG_ASYNC /* sync only */);
- if (err)
+ streamcipher_name = crypto_attr_alg_name(tb[1]);
+ if (IS_ERR(streamcipher_name)) {
+ err = PTR_ERR(streamcipher_name);
goto err_free_inst;
- streamcipher_alg = crypto_spawn_skcipher_alg_common(&ictx->streamcipher_spawn);
+ }
/* Block cipher: "aes" */
blockcipher_name = crypto_attr_alg_name(tb[2]);
@@ -595,7 +583,7 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
}
/* Check the set of algorithms */
- ictx->nrounds = adiantum_parse_algorithms(streamcipher_alg,
+ ictx->nrounds = adiantum_parse_algorithms(streamcipher_name,
blockcipher_name);
if (!ictx->nrounds) {
err = -ENOENT;
@@ -604,29 +592,21 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
/* Instance fields */
- err = -ENAMETOOLONG;
- if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
- "adiantum(%s,aes)",
- streamcipher_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
- goto err_free_inst;
- if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
- "adiantum(%s,aes-lib)",
- streamcipher_alg->base.cra_driver_name) >=
- CRYPTO_MAX_ALG_NAME)
- goto err_free_inst;
+ snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
+ "adiantum(xchacha%d,aes)", ictx->nrounds);
+ snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
+ "adiantum(xchacha%d-lib,aes-lib)", ictx->nrounds);
inst->alg.base.cra_blocksize = AES_BLOCK_SIZE;
inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx);
- inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask;
- inst->alg.base.cra_priority = streamcipher_alg->base.cra_priority;
+ inst->alg.base.cra_priority = 300;
inst->alg.setkey = adiantum_setkey;
inst->alg.encrypt = adiantum_encrypt;
inst->alg.decrypt = adiantum_decrypt;
inst->alg.init = adiantum_init_tfm;
- inst->alg.exit = adiantum_exit_tfm;
- inst->alg.min_keysize = streamcipher_alg->min_keysize;
- inst->alg.max_keysize = streamcipher_alg->max_keysize;
+ inst->alg.min_keysize = CHACHA_KEY_SIZE;
+ inst->alg.max_keysize = CHACHA_KEY_SIZE;
inst->alg.ivsize = TWEAK_SIZE;
inst->free = adiantum_free_instance;
--
2.43.0