Re: [PATCH v2 2/2] crypto: adiantum - Use the ChaCha library

From: Eric Biggers

Date: Tue Dec 01 2026 - 00:18:02 EST


Thanks for the patch!

On Mon, Nov 30, 2026 at 08:39:45PM -0800, Laveesh Bansal wrote:
> 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
> select CRYPTO_LIB_POLY1305_GENERIC
> select CRYPTO_MANAGER

CRYPTO_MANAGER should be removed too, since it was only needed for
template support. Also, please keep the options in alphabetical order.

> /*
> * Given the XChaCha stream key K_S, derive the block cipher key K_E and the
> @@ -99,31 +104,21 @@ 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 };

'iv' can be made 'static const', now that it no longer is passed to the
crypto_skcipher API which requires mutability.

> - 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);

This should all be under 'if (bulk_len)', so that if nothing needs to be
encrypted with the stream cipher then an unnecessary HChaCha computation
isn't done.

Also, this uses 'req->src' and 'req->dst' instead of the src and dst
local variables used elsewhere in the function.

> +static int adiantum_init_tfm(struct crypto_skcipher *tfm, int nrounds)
> {
> - 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);
> - if (WARN_ON(crypto_skcipher_reqsize(streamcipher) >
> - MAX_SYNC_SKCIPHER_REQSIZE)) {
> - crypto_free_skcipher(streamcipher);
> - return -EINVAL;
> - }
>
> - tctx->streamcipher = streamcipher;
> -
> - 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)));
> + tctx->nrounds = nrounds;
> + crypto_skcipher_set_reqsize(tfm, sizeof(struct nhpoly1305_ctx));
> return 0;

Instead of crypto_skcipher_set_reqsize(), just set .base.cra_reqsize in
the struct skcipher_alg.

That would leave setting tctx->nrounds as the only thing here. It would
be a bit simpler to handle that in setkey instead.

> +#define ADIANTUM_ALG(rounds) \
> + { \
> + .base.cra_name = "adiantum(xchacha" #rounds ",aes)", \
> + .base.cra_driver_name = \
> + "adiantum(xchacha" #rounds "-lib,aes-lib)", \
> + .base.cra_priority = 300, \
> + .base.cra_blocksize = AES_BLOCK_SIZE, \
> + .base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx), \
> + .base.cra_module = THIS_MODULE, \
> + .setkey = adiantum_setkey, \
> + .encrypt = adiantum_encrypt, \
> + .decrypt = adiantum_decrypt, \
> + .init = adiantum_init_xchacha##rounds##_tfm, \
> + .min_keysize = CHACHA_KEY_SIZE, \
> + .max_keysize = CHACHA_KEY_SIZE, \
> + .ivsize = TWEAK_SIZE, \

Since there are only two of these, I think I'd prefer that they each
just be written out explicitly. Especially since the above proposal
would generate the strings using concatenation, making them difficult to
find in searches.

- Eric