Skip to content

Add support for SHAKE - #523

Open
padelsbach wants to merge 10 commits into
wolfSSL:mainfrom
padelsbach:crypto-cb-shake
Open

padelsbach wants to merge 10 commits into
wolfSSL:mainfrom
padelsbach:crypto-cb-shake

Conversation

@padelsbach

@padelsbach padelsbach commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Follows patterns from SHA3

@padelsbach
padelsbach marked this pull request as ready for review September 18, 2026 20:41
@padelsbach padelsbach assigned padelsbach and bigbrett and unassigned padelsbach Sep 18, 2026

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #523

Scan targets checked: wolfhsm-core-bugs, wolfhsm-crypto-bugs, wolfhsm-src

Findings: 5
5 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Comment thread test-refactor/client-server/wh_test_crypto_shake.c
Comment thread src/wh_client_crypto.c
Comment thread src/wh_client_crypto.c Outdated
Comment thread src/wh_client_crypto.c Outdated
Comment thread test-refactor/client-server/wh_test_crypto_shake.c
@bigbrett

Copy link
Copy Markdown
Contributor

@padelsbach plz investigate fenrir issues and resolve + assign me back the bot if ready to go. That said we don't want to merge unless the upstream wolfCrypt stuff is all in first

@padelsbach padelsbach assigned bigbrett and padelsbach and unassigned padelsbach and bigbrett Sep 21, 2026
@padelsbach padelsbach changed the title Add crypto callback for SHAKE Add support for SHAKE Sep 25, 2026
Copilot AI balanced review requested due to automatic review settings October 3, 2026 03:32

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 review overview

🟡 Changes recommended

Missing-server responses are not consistently converted into software fallback signals, causing SHAKE operations to fail in mixed-feature deployments.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds SHAKE128/256 offload support following the existing SHA3 client/server architecture.

Changes:

  • Adds SHAKE wire messages, client APIs, callbacks, and server handling.
  • Adds extensive functional, validation, and fallback tests.
  • Adds SHAKE build variants and updates the wolfSSL dependency revision.
File Description
wolfhsm/​wh_message_crypto.h Defines SHAKE wire formats and limits.
wolfhsm/​wh_client_crypto.h Declares SHAKE client APIs.
src/​wh_message_crypto.c Implements message translation.
src/​wh_client_crypto.c Implements SHAKE client offload.
src/​wh_client_cryptocb.c Adds wolfCrypt callback dispatch.
src/​wh_server_crypto.c Adds server-side SHAKE processing.
test-refactor/​client-server/​wh_test_crypto_shake.c Adds comprehensive SHAKE tests.
test-refactor/​wh_test_list.c Registers the new tests.
test-refactor/​posix/​Makefile Adds SHAKE build variants.
test/​config/​user_settings.h Configures SHAKE and dependent algorithms.
test/​Makefile Configures wolfCrypt certificate paths.
test/​wh_test_check_struct_padding.c Checks new message padding.
test-refactor/​misc/​wh_test_check_struct_padding.c Checks refactored-build padding.
.github/​workflows/​build-and-test-refactor.yml Tests individual SHAKE configurations.
.github/​actions/​checkout-wolfssl/​action.yml Pins the required wolfSSL revision.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/wh_client_cryptocb.c
@bigbrett bigbrett assigned Frauschi and rizlik and unassigned bigbrett Oct 26, 2026
@bigbrett
bigbrett requested review from Frauschi and rizlik October 26, 2026 15:15

@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.

Looks good overall. Main issue is the silent software fallback for large outputs; a few smaller things inline.

Comment thread src/wh_client_cryptocb.c Outdated
#endif
}
/* Output too big for one response; let software produce it */
if (ret == WH_ERROR_NOSPACE) {

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.

Output above WH_MESSAGE_CRYPTO_SHAKE_MAX_INLINE_OUTPUT_SZ turns into CRYPTOCB_UNAVAILABLE here, so the hash silently finishes in software. We don't want a silent fallback on the offload path; this should either run on the HSM or fail. Absorb plus the chunked squeeze you already added could produce long outputs on the server.

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.

Dropped the fallback

Comment thread src/wh_client_crypto.c Outdated

capacity = _ShakeUpdatePerCallCapacity(sha, v);
if (inLen > capacity) {
return WH_ERROR_BADARGS;

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.

Other hash UpdateRequests return WH_ERROR_REQUEST_SIZE for an oversized inLen; SHAKE returns BADARGS. These SHAKE helpers are mostly copies of the SHA3 ones and already drifting, could they be shared since wc_Shake is a wc_Sha3?

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.

Yep, added helper _Sha3PackUpdate

Comment thread src/wh_server_crypto.c Outdated
res.outSz = req.outSz;
}

ops.freeFn(shake);

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.

shake, req and res hold sponge state derived from secret input (ML-DSA/ML-KEM seeds) and aren't zeroed. Can we wc_ForceZero them before returning, like the client side does?

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, good catch. Refactored _HandleShake to cleanup at the end

Comment thread src/wh_client_crypto.c Outdated
return WH_ERROR_BADARGS;
}
if ((op != WH_MESSAGE_CRYPTO_SHAKE_OP_ABSORB) &&
((out == NULL) || (outSz == 0))) {

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.

Rejecting out == NULL before receiving leaves the server's reply in the comm buffer, so the next request reads a stale response. Validate in the request path instead.

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.

Reworked to receive the reply first, then reject

Comment thread src/wh_client_crypto.c
/* Save state so a software fallback does not absorb input twice */
_ShakeSaveState(sha, &saved);

ret = _ShakeUpdateAll(ctx, sha, v, in, inLen);

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.

Move the outSz limit check before _ShakeUpdateAll, otherwise a too-large output streams all of in to the server first and then throws it away.

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.

Limit and check is only on the server now

Comment thread src/wh_client_crypto.c Outdated
memcpy(out, (uint8_t*)(res + 1), outSz);
}
if (op == WH_MESSAGE_CRYPTO_SHAKE_OP_HASH) {
ret = v->resetFn(sha);

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.

Minor: SHA3 resets with v->initFn, SHAKE with wc_Shake*_Reset, which frees and re-inits and can dispatch a FREE back into the callback. Using initFn would keep them consistent.

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.

Switched to v->initFn

Comment thread src/wh_client_cryptocb.c Outdated

#if defined(WOLF_CRYPTO_CB_SHAKE_XOF) && \
(defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256))
/* Declining an unknown op is safe as the wc_Shake state is still current */

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.

Nit: some new comments explain reasoning rather than code (this one, "Additive to avoid unsigned wrap", "A silent decline would still match software..."). I'd drop or shorten them.

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.

I usually comment about "why" and let the code describe "how". But these have been cleaned up

@Frauschi Frauschi assigned padelsbach and unassigned Frauschi Oct 27, 2026
@padelsbach padelsbach assigned Frauschi and unassigned padelsbach Oct 28, 2026
@padelsbach
padelsbach requested a review from Frauschi October 28, 2026 18:09
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.

6 participants