Skip to content

SHA3/SHAKE: unify wire format and handlers, enable Keccak-256 - #562

Open
padelsbach wants to merge 7 commits into
wolfSSL:mainfrom
padelsbach:sha3-shake
Open

padelsbach wants to merge 7 commits into
wolfSSL:mainfrom
padelsbach:sha3-shake

Conversation

@padelsbach

@padelsbach padelsbach commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Follow up to #523.

Changelist:

  1. Unify SHA3 and SHAKE to use one set of req/rsp handlers. Existing public API is unchanged, but message format is updated
  2. Route Keccak-256 to the server rather than erroring out. Send a flag with the request which gets translated to the wolfcrypt flag option, when enabled
  3. Treat zero-len SHAKE final operation as a reset op, and handle it internally rather than falling back

Copilot AI balanced review requested due to automatic review settings November 11, 2026 20:28
@padelsbach padelsbach mentioned this pull request Nov 11, 2026

Copilot AI left a comment

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@padelsbach
padelsbach marked this pull request as ready for review December 3, 2026 20:30
@padelsbach
padelsbach requested a balanced review from Copilot December 3, 2026 20:30

Copilot AI left a comment

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.

🟡 Changes recommended

Replies can leak stale data through uninitialized padding, and mixed-version peers can report success on incompatible SHA3/SHAKE messages.

4 open issues

Review effort: Balanced

Comment thread src/wh_message_crypto.c Resolved
Comment thread wolfhsm/wh_message_crypto.h Resolved
Comment thread wolfhsm/wh_message_crypto.h Resolved
Comment thread src/wh_client_crypto.c Resolved

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Most comments are on the wire encoding and the Keccak-256 policy; the handler unification itself looks good.


/* SHA3 Request. Data follows: in[inSz] */
typedef struct {
uint32_t isLastBlock;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The op is inferred from isLastBlock and outSz, which leaves a zero-length final with no encoding and forces the client-only reset. Since the format is changing anyway, can we replace isLastBlock with an explicit op field from whMessageCrypto_Sha3Op?

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.

In #523 it was that way initially, but Marco requested to infer it. I don't have a strong opinion either way.

Comment thread src/wh_client_crypto.c
ret = _ShakeUpdateAll(ctx, sha, v, in, inLen);
if ((ret == WH_ERROR_OK) && (op == WH_MESSAGE_CRYPTO_SHAKE_OP_ABSORB)) {
ret = _ShakeStateOp(ctx, sha, v, op, NULL, 0);
/* A final with no output is only a reset, which discards the input */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A one-shot SHAKE call with outSz == 0 now drops in and resets, while FinalRequest still returns BADARGS for the same input. Can we make both paths agree and document the behaviour in the SHAKE section of wh_client_crypto.h?

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.

Done

Comment thread src/wh_server_crypto.c
memcpy(sha->s, req->resumeState.s, sizeof(sha->s));
#if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_NO_KECCAK256)
if (keccak) {
ret = wc_Sha3_SetFlags(sha, WC_HASH_SHA3_KECCAK256);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any client now gets Keccak-256 whenever the server has WOLFSSL_HASH_FLAGS, including FIPS v5/v6 builds where it isn't approved. Can we gate it behind a WOLFHSM_CFG_* opt-in and accept the flag only for SHA3-256, since wolfCrypt ignores it on 224/384/512 and applies it to SHAKE256?

@padelsbach padelsbach Dec 4, 2026 •

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.

Added WOLFHSM_CFG_KECCAK256 opt-in option

Comment thread wolfhsm/wh_client_crypto.h Outdated
* padding and has no Keccak fallback. Use the cryptocb path, which routes
* Keccak to software.
* Keccak-mode contexts (sha->flags & WC_HASH_SHA3_KECCAK256) are hashed on
* the server. A server without Keccak-256 returns NOT_COMPILED_IN.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Keccak contexts used to fall back to software through the cryptocb, but against a server without WOLFSSL_HASH_FLAGS they now fail with NOT_COMPILED_IN. If that's intended, can we update the wh_Client_CryptoCb comment that still promises software fallback for unsupported algorithms?

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.

Comments updated

Comment thread wolfhsm/wh_message_crypto.h Outdated
uint32_t inSz;
whMessageCrypto_Sha3State resumeState;
whMessageCrypto_DmaBuffer input;
whMessageCrypto_DmaBuffer output;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The server rejects SHAKE over DMA, so this reserved output buffer adds 16 wire bytes plus validation and tests for a path that doesn't exist yet. Since SHA3 isn't released, can we drop it and add it with DMA 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.

Done, dropped the field

/* The update sends a block, so check only the final went out */
seq = ctx->comm->seq;
if (ret == 0) {
ret = v->finalFn(sha, out, sizeof(dev));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This only covers the single FINAL request, so the absorb/squeeze path for long output and multi-block updates never run with the Keccak flag. Can we add a case with output above WH_MESSAGE_CRYPTO_SHA3_MAX_INLINE_OUTPUT_SZ and input longer than one block?

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.

Done

@padelsbach padelsbach removed their assignment Dec 4, 2026
@padelsbach
padelsbach requested a review from Frauschi December 4, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants