From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH] lib/crypto: poly1305: Hide the mask value from compilers
Date: Fri, 30 Oct 2026 21:26:37 -0700 [thread overview]
Message-ID: <20261031042637.68815-1-ebiggers@kernel.org> (raw)
poly1305_core_emit() does a constant-time select between h and h - p,
where h is the accumulator and p is the Poly1305 prime of 2^130 - 5.
The mask used in this select isn't hidden from the compiler, though, so
the compiler can infer that it's either all 0's or all 1's.
In theory, the compiler could then use that to recognize the select, and
"optimize" it with a branch.
Fortunately, in practice, I couldn't find any gcc or clang options that
made that happen.
Regardless, add OPTIMIZER_HIDE_VAR() to "hide" the value of the mask for
safety against any future compiler optimizations.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
lib/crypto/poly1305-donna32.c | 1 +
lib/crypto/poly1305-donna64.c | 1 +
2 files changed, 2 insertions(+)
diff --git a/lib/crypto/poly1305-donna32.c b/lib/crypto/poly1305-donna32.c
index 5977785cd76e..73fe9181296c 100644
--- a/lib/crypto/poly1305-donna32.c
+++ b/lib/crypto/poly1305-donna32.c
@@ -167,6 +167,7 @@ void poly1305_core_emit(const struct poly1305_state *state, const u32 nonce[4],
/* select h if h < p, or h + -p if h >= p */
mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
+ OPTIMIZER_HIDE_VAR(mask);
g0 &= mask;
g1 &= mask;
g2 &= mask;
diff --git a/lib/crypto/poly1305-donna64.c b/lib/crypto/poly1305-donna64.c
index d0e4234c1c4c..402a3459ca49 100644
--- a/lib/crypto/poly1305-donna64.c
+++ b/lib/crypto/poly1305-donna64.c
@@ -152,6 +152,7 @@ void poly1305_core_emit(const struct poly1305_state *state, const u32 nonce[4],
/* select h if h < p, or h + -p if h >= p */
c = (g2 >> ((sizeof(u64) * 8) - 1)) - 1;
+ OPTIMIZER_HIDE_VAR(c);
g0 &= c;
g1 &= c;
g2 &= c;
base-commit: 2a500a04d6f969318c05f46aedb6e73fc1963a00
--
2.56.0
reply other threads:[~2026-10-31 4:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261031042637.68815-1-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=ardb@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