From: Eric Biggers <ebiggers@kernel.org>
To: Jang Ingyu <ingyujang25@korea.ac.kr>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [Question] Redundant ternary operators in nhpoly1305/sha256 digest functions?
Date: Wed, 14 Jan 2026 22:32:36 +0000 [thread overview]
Message-ID: <20260114223236.GA1449008@google.com> (raw)
In-Reply-To: <20260114153839.3649359-1-ingyujang25@korea.ac.kr>
On Thu, Jan 15, 2026 at 12:38:39AM +0900, Jang Ingyu wrote:
> From: Ingyu Jang <ingyujang25@korea.ac.kr>
>
> Hi,
>
> I noticed that in arch/x86/crypto/, several digest functions use
> the ternary operator (?:) to chain function calls:
>
> In nhpoly1305-avx2-glue.c and nhpoly1305-sse2-glue.c:
>
> return crypto_nhpoly1305_init(desc) ?:
> nhpoly1305_xxx_update(desc, src, srclen) ?:
> crypto_nhpoly1305_final(desc, out);
>
> In sha256_ssse3_glue.c (sha256_ssse3_digest, sha256_avx_digest,
> sha256_avx2_digest, sha256_ni_digest):
>
> return sha256_base_init(desc) ?:
> sha256_xxx_finup(desc, data, len, out);
>
> However, all the functions being checked always return 0:
> - crypto_nhpoly1305_init() always returns 0
> - nhpoly1305_xxx_update() always returns 0
> - crypto_nhpoly1305_final() always returns 0
> - sha256_base_init() always returns 0
>
> This makes the short-circuit evaluation of ?: unnecessary.
This code was written to be compatible with the crypto_shash API, which
is defined to have an error code for every hashing operation. So it was
defensive coding to assume that an error might occur, even though
apparently in these particular cases no error was possible.
> Is this intentional defensive coding for potential future changes,
> or could this be cleaned up?
It was already cleaned up as part of the migration into lib/crypto/,
where hashing operations now always succeed and return void. SHA-256
was cleaned up in 6.17. The NH cleanups are queued for 6.20.
- Eric
prev parent reply other threads:[~2026-01-14 22:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-14 15:38 [Question] Redundant ternary operators in nhpoly1305/sha256 digest functions? Jang Ingyu
2026-01-14 22:32 ` Eric Biggers [this message]
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=20260114223236.GA1449008@google.com \
--to=ebiggers@kernel.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=hpa@zytor.com \
--cc=ingyujang25@korea.ac.kr \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@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