Skip to content

Implement SHAKE in sha3.c#648

Merged
valeriosetti merged 14 commits intoMbed-TLS:developmentfrom
gilles-peskine-arm:sha3-shake-builtin
Jan 20, 2026
Merged

Implement SHAKE in sha3.c#648
valeriosetti merged 14 commits intoMbed-TLS:developmentfrom
gilles-peskine-arm:sha3-shake-builtin

Conversation

@gilles-peskine-arm
Copy link
Copy Markdown
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Jan 9, 2026

Implement SHAKE in the SHA3 module. Resolves #621

An earlier version of this pull request also implemented the SHAKE128/256 and SHAKE256/512 hash algorithms. We don't need those urgently: they'll be needed for HashML-DSA and for Ed448ph respectively. That part is at https://github.com/gilles-peskine-arm/TF-PSA-Crypto/tree/sha3-shake-builtin-2 (with unresolved problems with some test driver builds) and needs preceding PR: Mbed-TLS/mbedtls-framework#267.

PR checklist

  • changelog no (nothing user-visible)
  • framework PR not required
  • mbedtls development PR not required because: crypto only
  • mbedtls 3.6 PR not required because: new feature
  • tests provided

@gilles-peskine-arm gilles-peskine-arm added the size-s Estimated task size: small (~2d) label Jan 9, 2026
@gilles-peskine-arm gilles-peskine-arm added priority-high High priority - will be reviewed soon needs-preceding-pr Requires another PR to be merged first needs-design-approval Needs design discussion / approval needs-ci Needs to pass CI tests labels Jan 9, 2026
@gilles-peskine-arm gilles-peskine-arm removed the needs-design-approval Needs design discussion / approval label Jan 12, 2026
@gilles-peskine-arm gilles-peskine-arm changed the title Implement SHAKE in sha3.c, and add SHAKE128/256 and SHAKE256/512 hash algorithms Implement SHAKE in sha3.c Jan 12, 2026
@gilles-peskine-arm gilles-peskine-arm removed the needs-preceding-pr Requires another PR to be merged first label Jan 12, 2026
@gilles-peskine-arm gilles-peskine-arm force-pushed the sha3-shake-builtin branch 2 times, most recently from 55b4391 to 54a0ef2 Compare January 12, 2026 12:18
@gilles-peskine-arm gilles-peskine-arm marked this pull request as ready for review January 13, 2026 08:37
@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members and removed needs-ci Needs to pass CI tests labels Jan 13, 2026
Copy link
Copy Markdown
Contributor

@bjwtaylor bjwtaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One quick question, though still need to review in more detail when I get a chance.

break;
#endif

#if defined(MBEDTLS_SHA3_WANT_SHAKE128)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the SHAKE variants be setting the ctx->olen to 0, to clear it to prevent any prior length being retained?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a good idea. And that shows we're missing a context reuse test case for SHAKE.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More precisely, the case where it can go wrong is if there's a non-SHAKE operation without finish, then a SHAKE operation. The SHAKE operation will be limited to a single finish() call, which will output exactly the length of the hash from the first operation. We aren't currently testing a context reuse without finish (i.e. init, starts, update, starts, update, finish).

Comment thread drivers/builtin/src/sha3.c
/*
* SHA-3 process buffer
*/
int mbedtls_sha3_update(mbedtls_sha3_context *ctx,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this function check for the presence of ctx->finished to prevent an API misuse of using an update after a finish and produce an error if it is set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally yes, but this is now a private interface.

It would be good to do that in 3.6 though, since sha3.h is public there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on Thursday, this is an internal interface, we don't particularly need to protect against misuse.

#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_SOME_HASH) */

#if defined(MBEDTLS_SHA3_WANT_SHAKE128)
static const unsigned char shake128_test_input[2][16] =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we document anywhere where this test data comes from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should, yes. Do, unfortunately, no. But we don't actually rely on it for correctness (we have the unit tests for that), so I can live with it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was more thinking if anybody has to modify it or wants to use it for other tests it's useful to know where it comes from.

valeriosetti
valeriosetti previously approved these changes Jan 19, 2026
Copy link
Copy Markdown
Contributor

@valeriosetti valeriosetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found only a minor typo in a comment, but the rest LGTM.

Comment thread drivers/builtin/include/mbedtls/private/crypto_adjust_config_tweak_builtins.h Outdated
bjwtaylor
bjwtaylor previously approved these changes Jan 19, 2026
@github-project-automation github-project-automation Bot moved this from In Development to Has Approval in Roadmap pull requests (new board) Jan 19, 2026
@valeriosetti valeriosetti added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members labels Jan 19, 2026
@valeriosetti valeriosetti removed the approved Design and code approved - may be waiting for CI or backports label Jan 19, 2026
gilles-peskine-arm and others added 14 commits January 19, 2026 13:12
This is in preparation for adding SHAKE support: if e.g. SHAKE128 is enabled
but no SHA3 hash, then psa_crypto_hash.c should not include SHA3 support.
Conversely, if e.g. SHAKE128/256 is enabled but neither SHAKE128 nor
SHAKE256, then psa_crypto_xof.c should not include SHAKE support.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
We don't have a XOF API yet in PSA. To provide a way to enable built-in
SHAKE support, for now, add options to `crypto_config.h` that are enabled in
the `full` config.

Define the MBEDTLS_PSA_BUILTIN_xxx symbols as macros with no parameter,
rather than with the value 1 as usual, so that `config.py full` enables them.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
SHAKE, as it is a XOF function, it allows arbitrary output lengths that can be called multiple times and concatenated. The property SHAKE_finish(olen1+olen2)=SHAKE_finish(olen1)||SHAKE_finish(olen2) always holds.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Choose some representative lengths: 0, a few short inputs,
one block -1/+0/+1, two blocks -1/+0/+1.

Test cases generated by the following Python code:
```
from hashlib import shake_128, shake_256;
def tc(f, shake, v, n): msg = shake(bytes(str(n), encoding="ascii")).digest(n); output = shake(msg).digest(300); return f"SHAKE{v} {f} {n}\ndepends_on:MBEDTLS_SHA3_WANT_SHAKE{v}\nshake_{f}:MBEDTLS_SHA3_SHAKE{v}:\"{msg.hex()}\":\"{output.hex()}\":\n"
for n in [0, 1, 2, 4, 8, 16, 42, 55, 100, 167, 168, 169, 335, 336, 337]: print(tc("input", shake_128, 128, n))
for n in [0, 1, 2, 4, 8, 16, 42, 55, 100, 135, 136, 137, 271, 272, 273]: print(tc("input", shake_256, 256, n))
for n in [0, 1, 200]: print(tc("output", shake_128, 128, n))
for n in [0, 1, 200]: print(tc("output", shake_256, 256, n))
```

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members and removed needs-work labels Jan 19, 2026
@gilles-peskine-arm
Copy link
Copy Markdown
Contributor Author

I rewrote the history to remove the framework update commit, since it isn't needed after all. I also fixed the typo that Valerio pointed out while I was at it. The previous version is at https://github.com/gilles-peskine-arm/TF-PSA-Crypto/tree/sha3-shake-builtin-4

@valeriosetti valeriosetti added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members labels Jan 20, 2026
@valeriosetti valeriosetti added this pull request to the merge queue Jan 20, 2026
Merged via the queue into Mbed-TLS:development with commit dc5a2f2 Jan 20, 2026
2 checks passed
@github-project-automation github-project-automation Bot moved this from Has Approval to Done in Roadmap pull requests (new board) Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Design and code approved - may be waiting for CI or backports priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)

Development

Successfully merging this pull request may close these issues.

Implement SHAKE (built-in, no API)

4 participants