mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Laveesh Bansal <laveeshb@laveeshbansal.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] crypto: adiantum - Use the ChaCha library
Date: Mon, 30 Nov 2026 21:17:52 -0800	[thread overview]
Message-ID: <20261201051752.GB7060@sol> (raw)
In-Reply-To: <20261201043945.50022-3-laveeshb@laveeshbansal.com>

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

  reply	other threads:[~2026-12-01  5:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  4:39 [PATCH v2 0/2] " Laveesh Bansal
2026-12-01  4:39 ` [PATCH v2 1/2] crypto: testmgr - Add Adiantum vectors with a partial last ChaCha block Laveesh Bansal
2026-12-01  4:39 ` [PATCH v2 2/2] crypto: adiantum - Use the ChaCha library Laveesh Bansal
2026-12-01  5:17   ` Eric Biggers [this message]
2026-12-01  8:11     ` Laveesh Bansal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201051752.GB7060@sol \
    --to=ebiggers@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=laveeshb@laveeshbansal.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®