From: Eric Biggers <ebiggers@kernel.org>
To: James Bottomley <James.Bottomley@hansenpartnership.com>
Cc: linux-crypto@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>
Subject: Re: Adding algorithm agility to the crypto library functions
Date: Tue, 14 Oct 2025 09:55:41 -0700 [thread overview]
Message-ID: <20251014165541.GA1544@sol> (raw)
In-Reply-To: <d4449ec09cf103bf3d937f78a13720dcae93fb4e.camel@HansenPartnership.com>
On Tue, Oct 14, 2025 at 12:01:34PM -0400, James Bottomley wrote:
> The TPM session calculation functions recently got converted to use the
> rawkey functions instead of open coding it 64a7cfbcf548 ("tpm: Use
> HMAC-SHA256 library instead of open-coded HMAC").
To be clear, drivers/char/tpm/tpm2-sessions.c has always been hard-coded
to use SHA-256 and HMAC-SHA256, and it's always used the SHA-256
library. It just open-coded the HMAC construction.
> because the user has no input on the hmac hash algorithm so, although
> the TPM specifies it to be agile, we can simply choose sha256.
> However, we have plans to use what are called policy sessions, which
> have require the same hash as the user supplied object used for its
> name (essentially a hash chosen by the user). In a TPM these hashes
> can be any of the family sha1 sha256, sha384 sha512 plus a few esoteric
> ones like sm3. So the question becomes: to avoid going back to open
> coding the hmac and using the shash API, is there a way of adding hash
> agility to the library algorithms? I suppose I could also do this
> inside our hmac code using a large set of switch statements, but that
> would be a bit gross.
>
> If no-one's planning to do this I can take a stab ... it would probably
> still be a bunch of switch statements, but not in my code ...
This isn't the job of lib/crypto/.
If a caller would like to support a certain set of algorithms, it should
just write the 'if' or 'switch' statement itself.
The nice thing about that is that it results in the minimum number of
branches and the minimum stack usage for the possible set of algorithms
at that particular call site. (Compare to crypto_shash which always
uses indirect calls and always uses almost 400 bytes for each
SHASH_DESC_ON_STACK(). SHASH_DESC_ON_STACK() has to be sized for the
worst possible case among every hash algorithm in existence, regardless
of whether the caller is actually using it or not.)
That approach is already used successfully in fs/verity/ and net/sctp/.
I'm planning to make fs/btrfs/ adopt it too. It works fine, and it's
the most efficient solution.
If a particular caller has a super long list of algorithms or is dealing
with a legacy arbitrary user-specified string, then it's of course still
free to use crypto_shash.
But I have to wonder, do you really need to add support for all these
hash algorithms? Adding SHA-1 and SM3 support, really?
What stops you from just saying that the kernel supports SHA-256 for
these user supplied objects, and that's it?
Getting kernel developers to think carefully about what set of crypto
algorithms they'd like to support in their features, rather than punting
the problem to a generic crypto layer that supports all sorts of
insecure and esoteric options, isn't necessarily a bad thing...
- Eric
next prev parent reply other threads:[~2025-10-14 16:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-14 16:01 Adding algorithm agility to the crypto library functions James Bottomley
2025-10-14 16:55 ` Eric Biggers [this message]
2025-10-14 17:08 ` Ard Biesheuvel
2025-10-14 17:30 ` Eric Biggers
2025-10-14 17:32 ` James Bottomley
2025-10-14 17:39 ` Eric Biggers
2025-10-14 20:17 ` James Bottomley
2025-10-15 4:59 ` Simon Richter
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=20251014165541.GA1544@sol \
--to=ebiggers@kernel.org \
--cc=James.Bottomley@hansenpartnership.com \
--cc=ardb@kernel.org \
--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