Linux cryptographic layer development
 help / color / mirror / Atom feed
From: "Ard Biesheuvel" <ardb@kernel.org>
To: "Eric Biggers" <ebiggers@kernel.org>, linux-crypto@vger.kernel.org
Cc: "Jason A . Donenfeld" <Jason@zx2c4.com>,
	"Herbert Xu" <herbert@gondor.apana.org.au>
Subject: Re: [PATCH] lib/crypto: gf128hash: Hide selection mask values from compilers
Date: Mon, 02 Nov 2026 10:15:11 +0100	[thread overview]
Message-ID: <2c122f4c-bd28-4617-90fd-ca73e95177f4@app.fastmail.com> (raw)
In-Reply-To: <20261031052149.201983-1-ebiggers@kernel.org>



On Sat, 31 Oct 2026, at 06:21, Eric Biggers wrote:
> Hide selection mask values from compilers, so that they cannot see that
> they are used to select between two values and potentially generate a
> secret-dependent branch.  This is not known to be happening here, but we
> might as well be extra careful.
>
> Signed-off-by: Eric Biggers <ebiggers@kernel.org>
> ---
>  lib/crypto/gf128hash.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
>

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>


> diff --git a/lib/crypto/gf128hash.c b/lib/crypto/gf128hash.c
> index 8dcdf5ec98be..5b8ae98cafed 100644
> --- a/lib/crypto/gf128hash.c
> +++ b/lib/crypto/gf128hash.c
> @@ -88,10 +88,18 @@ static void clmul64(u64 a, u64 b, u64 *out_lo, u64 *out_hi)
>  		  (a2 * (u128)b1) ^ (a3 * (u128)b0);
> 
>  	/* Multiply the low 4 bits of @a by @b. */
> -	u64 e0 = -(a & 1) & b;
> -	u64 e1 = -((a >> 1) & 1) & b;
> -	u64 e2 = -((a >> 2) & 1) & b;
> -	u64 e3 = -((a >> 3) & 1) & b;
> +	u64 e0 = -(a & 1);
> +	u64 e1 = -((a >> 1) & 1);
> +	u64 e2 = -((a >> 2) & 1);
> +	u64 e3 = -((a >> 3) & 1);
> +	OPTIMIZER_HIDE_VAR(e0);
> +	OPTIMIZER_HIDE_VAR(e1);
> +	OPTIMIZER_HIDE_VAR(e2);
> +	OPTIMIZER_HIDE_VAR(e3);
> +	e0 &= b;
> +	e1 &= b;
> +	e2 &= b;
> +	e3 &= b;
>  	u64 extra_lo = e0 ^ (e1 << 1) ^ (e2 << 2) ^ (e3 << 3);
>  	u64 extra_hi = (e1 >> 63) ^ (e2 >> 62) ^ (e3 >> 61);
> 
> @@ -245,6 +253,7 @@ static void __maybe_unused 
> ghash_key_to_polyval(const u8 in[GHASH_BLOCK_SIZE],
>  	u64 lo = get_unaligned_be64(&in[8]);
>  	u64 mask = (s64)hi >> 63;
> 
> +	OPTIMIZER_HIDE_VAR(mask);
>  	hi = (hi << 1) ^ (lo >> 63) ^ (mask & ((u64)0xc2 << 56));
>  	lo = (lo << 1) ^ (mask & 1);
>  	out->lo = cpu_to_le64(lo);
>
> base-commit: 2a500a04d6f969318c05f46aedb6e73fc1963a00
> -- 
> 2.56.0

  reply	other threads:[~2026-11-02  9:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-31  5:21 [PATCH] lib/crypto: gf128hash: Hide selection mask values from compilers Eric Biggers
2026-11-02  9:15 ` Ard Biesheuvel [this message]
2026-11-03 23:26 ` Eric Biggers

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=2c122f4c-bd28-4617-90fd-ca73e95177f4@app.fastmail.com \
    --to=ardb@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=ebiggers@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@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