Skip to content

Release crypto callback hash contexts on free and trim two client code paths - #689

Merged
aidangarske merged 6 commits into
wolfSSL:masterfrom
dgarske:client_cryptocb_free
Dec 2, 2026
Merged

aidangarske merged 6 commits into
wolfSSL:masterfrom
dgarske:client_cryptocb_free

Conversation

@dgarske

@dgarske dgarske commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Four client library and example findings from the Fenrir review and two Coverity reports. One commit per item.

  • wolfTPM2_CryptoDevCb handles the wolfCrypt copy and free callbacks. A hash or HMAC and its copies share one TPM sequence and cache, which are counted and released when the last of them is freed, with or without a Final and in any order. A hash that was updated and then freed without a Final used to keep its TPM sequence, cache and context. This takes effect when wolfSSL is built with both WOLF_CRYPTO_CB_COPY and WOLF_CRYPTO_CB_FREE (--enable-cryptocbutils=copy,free) and wolfTPM with WOLFTPM_USE_SYMMETRIC; wolfSSL reports the copy and free of a hash from v5.8.4 and of an HMAC from v5.9.4. Without both macros the code is compiled out.
  • The signature size check reads TPM_PT_INPUT_BUFFER and TPM_PT_MAX_COMMAND_SIZE in one TPM2_GetCapability instead of two, and falls back to a second read when the TPM does not return both.
  • XOR parameter encryption clears and wipes its mask over the bytes it used instead of the whole buffer.
  • spdm_ctrl --status reads the Nations identity key flag from the vendor property value, after checking the capability in the response. It read a byte of the parsed buffer that does not hold the flag.
  • Coverity: the PolicyPassword authorization copy is bounded by the command buffer, and the fwTPM test helpers check response sizes directly.

Compatibility:

  • No API change and no new build option. The accept and reject results of the signature size check are unchanged.
  • With both callbacks a copy can outlive its source and the reverse; the first Final still ends a shared TPM sequence for every object sharing it. Without both, a hash copy must not be used after the Final of its source and an HMAC that has been updated must not be copied, as before. An HMAC freed without a Final is released in every build, because wc_HmacFree runs a Final through the callback.

Testing

Unit tests cover Init, Update and Free without a Final for SHA-1, SHA-256 and HMAC, a source and a copy freed in either order, a hash copy whose allocation fails, and a self copy, against the fwTPM. A new sanitizer entry builds wolfSSL with --enable-cryptocbutils=copy,free --enable-smallstackcache, runs them under ASan with leak detection, and fails if one is skipped. The signature size check test covers the sizes on both sides of each limit against the fwTPM and the reference simulator, and counts the TPM2_GetCapability queries against a scripted TPM. The Nations status read was run on an NS350 (firmware 30.30) without an identity key; the provisioned case could not be produced, because the part answers --identity-key-set with TPM_RC_COMMAND_CODE.

Resolves F-16212, F-16213, F-17984, F-18950

@dgarske dgarske self-assigned this Dec 1, 2026
@dgarske
dgarske requested review from aidangarske and wolfSSL-Fenrir-bot and a balanced review from Copilot December 1, 2026 01:23

Copilot AI 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.

🟢 Approval recommended

The supplied review evidence identifies no active issues requiring resolution.

No open issues 🎉

What changed in this PR

This PR improves resource cleanup and bounds handling in wolfTPM’s client library, streamlines signature-limit queries and XOR mask handling, and corrects Nations SPDM status reporting.

Changes:

  • Release hash and HMAC resources on free callbacks, with regression tests and build guidance.
  • Combine signature-limit capability queries with a fallback, and limit XOR mask clearing to the bytes used.
  • Correct Nations vendor-property reads and strengthen authorization-copy bounds and test-helper size checks.
File Description
wolftpm/​tpm2_wrap.h Documents free-callback requirements for hash cleanup.
tests/​unit_tests.c Adds free-without-final tests and signature-limit boundary checks.
tests/​fwtpm_unit_tests.c Adds and strengthens size assertions in test helpers.
src/​tpm2.c Bounds the PolicyPassword authorization copy.
src/​tpm2_wrap.c Combines signature-limit queries while retaining fallback reads.
src/​tpm2_param_enc.c Clears and wipes only the used XOR mask bytes.
src/​tpm2_cryptocb.c Handles free callbacks for TPM-backed hashes and HMACs.
examples/​spdm/​spdm_ctrl.c Reads Nations status and diagnostic bytes from the vendor buffer.
docs/​tls-and-certificates.md Explains the free-callback build requirement for TLS hash cleanup.
docs/​build-options.md Adds free-callback guidance to symmetric-support build documentation.

Review effort: Balanced

@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 #689

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): docs/build-options.md, docs/tls-and-certificates.md
Coverage: 7 of 8 in-scope changed file(s) opened by the reviewer; not opened: wolftpm/tpm2_wrap.h

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

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

Review tier: Max

Comment thread src/tpm2_cryptocb.c Resolved

@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 #689

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): docs/build-options.md, docs/tls-and-certificates.md
Coverage: 1 of 2 in-scope changed file(s) opened by the reviewer; not opened: tests/unit_tests.c

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review December 1, 2026 17:55

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

@dgarske dgarske removed their assignment Dec 1, 2026

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

Skoll Code Review

Scan type: review

Overall recommendation: REQUEST_CHANGES
Findings: 2 total — 2 posted, 0 skipped
2 finding(s) posted as inline comments (see file-level comments below)

Posted findings

  • [High] Freeing a source hash leaves its copies with a dangling device context — src/tpm2_cryptocb.c:1238-1246
  • [High] Freeing a copied HMAC destroys the context still referenced by its peer — src/tpm2_cryptocb.c:1250-1257

Review generated by Skoll

Comment thread src/tpm2_cryptocb.c Resolved
Comment thread src/tpm2_cryptocb.c Resolved

@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 #689

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): docs/build-options.md, docs/tls-and-certificates.md
Coverage: 2 of 3 in-scope changed file(s) opened by the reviewer; not opened: wolftpm/tpm2_wrap.h

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

Comment thread src/tpm2_cryptocb.c Resolved
Comment thread src/tpm2_cryptocb.c Resolved
Comment thread tests/unit_tests.c Resolved
Comment thread docs/build-options.md Resolved
Comment thread examples/spdm/spdm_ctrl.c Resolved
Comment thread tests/unit_tests.c Resolved
Comment thread tests/fwtpm_unit_tests.c Resolved
@dgarske dgarske assigned dgarske and unassigned aidangarske Dec 1, 2026
@dgarske
dgarske force-pushed the client_cryptocb_free branch from eb727a0 to cc46226 Compare December 2, 2026 17:26
@dgarske
dgarske requested a review from aidangarske December 2, 2026 17:26
@dgarske dgarske assigned aidangarske and unassigned dgarske Dec 2, 2026
@aidangarske
aidangarske merged commit e6df8ec into wolfSSL:master Dec 2, 2026
@aidangarske
aidangarske deleted the client_cryptocb_free branch December 2, 2026 17:37

@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 #689

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): .github/workflows/sanitizer.yml, docs/build-options.md, docs/tls-and-certificates.md, tests/fwtpm_check.sh
Coverage: 6 of 8 in-scope changed file(s) opened by the reviewer; not opened: tests/fwtpm_unit_tests.c, wolftpm/tpm2_wrap.h

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

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.

5 participants