Skip to content

Fix parameter encryption, argument checks and cleanup in the wolfTPM examples - #679

Merged
aidangarske merged 50 commits into
wolfSSL:masterfrom
dgarske:fenrir_examples
Nov 24, 2026
Merged

aidangarske merged 50 commits into
wolfSSL:masterfrom
dgarske:fenrir_examples

Conversation

@dgarske

@dgarske dgarske commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

This PR fixes defects in the examples found by review. One commit per finding. Key changes:

  • Parameter encryption: the -aes and -xor session was installed in an authorization slot that the next wrapper call overwrote or that was never sent, so nothing was encrypted. wrap_test, hmac, tls_client, tls_server, seal_nv, policy_nv, nvram/extend, keygen and keyload now keep the session in slot 1 and release it at exit. seal_nv and policy_nv salt that session with the storage key, since a session that authorizes nothing would otherwise have a key derived from public nonces.
  • wolfTPM2_HmacStart sets the key Name for its authorization, so an HMAC session on HMAC_Start no longer fails with TPM_RC_BAD_AUTH. This is the one library change, with a unit test.
  • Argument checks: PCR indexes are range checked before narrowing to a byte, and out-of-range values return an error instead of exiting 0. gpio_set and activate_credential parse their documented argument forms.
  • Exit status: quote, wrap_test, csr, keygen, policy_sign and tls_client_notpm report a failed check or file error instead of success.
  • Handles and sockets: ML-DSA sequences, the EK, a key that failed to persist and the accepted TLS socket are released on error paths. TLS read and write loops track the error separately from the byte count.
  • NV examples: the first index of each range is accepted, the auth hierarchy follows the index range, PLATFORMCREATE is set only for platform indexes, and nv_bind refuses to replace an existing index.
  • Firmware update: the ST33 and Infineon examples clear the firmware policy when the start command never ran.
  • da_check restores the dictionary attack parameters after its lockout test.
  • Key buffers and the ECDH shared secret are zeroed before release.
  • run_examples.sh copies the generated CRLs to names a CRL-enabled wolfSSL loads as PEM.
  • Header and comment corrections, including notes that seal_policy_auth and verify_ek_cert are demonstrations with stated limits.

Testing: the examples were run against the fwTPM and the reference simulator with and without parameter encryption, and the session was confirmed on the wire for each fixed example. No hardware TPM was used.

Scope: seal_policy_auth and verify_ek_cert are documented, not redesigned.

Resolves F-16127, F-16792, F-16793, F-16794, F-16795, F-16796, F-16797, F-16799, F-16801, F-16802, F-16804, F-16805, F-16806, F-16807, F-16813, F-16814, F-16829, F-16830, F-17114, F-17115, F-17116, F-17117, F-17118, F-17119, F-17120, F-17121, F-17122, F-17123, F-17124, F-17125, F-17127, F-17128, F-17129, F-17135, F-17136, F-17139, F-17975, F-17976, F-17977, F-17978, F-17979, F-17980, F-17981, F-17982, F-17983, F-17985, F-18061

@dgarske dgarske self-assigned this Nov 23, 2026
Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:11

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.

🟡 Changes recommended

Platform NV indexes can use the wrong authorization hierarchy, negative GPIO pins can trigger writes to the default pin, and ECDH secret erasure can be optimized away.

3 open issues
What changed in this PR

This PR hardens wolfTPM’s examples around parameter encryption, argument validation, error reporting, and cleanup. It also corrects HMAC authorization in the wrapper library and adds a regression test.

Changes:

  • Keeps parameter-encryption sessions separate from handle authorization and policy sessions.
  • Improves input checks, failure exit statuses, NV handling, and firmware-policy rollback.
  • Adds resource cleanup and secret-buffer wiping, and clarifies demonstration limits.
File Description
tests/​unit_tests.c Adds HMAC parameter-encryption regression coverage.
src/​tpm2_wrap.c Supplies the key Name for HMAC_Start authorization.
examples/​wrap/​wrap_test.c Adjusts encryption sessions, cleanup, and failure reporting.
examples/​wrap/​hmac.c Moves parameter encryption to slot 1.
examples/​tls/​tls_server.c Adjusts sessions, separates I/O errors, and closes accepted sockets.
examples/​tls/​tls_client.c Adjusts sessions and separates I/O errors from byte counts.
examples/​tls/​tls_client_notpm.c Improves cipher-selection failure and I/O error handling.
examples/​timestamp/​signed_timestamp.c Corrects the session description in a comment.
examples/​spdm/​spdm_ctrl.c Distinguishes unparsed capability responses from unsupported capabilities.
examples/​spdm/​nv_bind.c Refuses to replace an existing NV index.
examples/​seal/​seal_policy_auth.c Documents demonstration limits and validates PCR indexes before narrowing.
examples/​seal/​seal_pcr.c Validates PCR indexes before narrowing.
examples/​run_examples.sh Copies CRLs under PEM-compatible filenames.
examples/​pqc/​mldsa_verify_neg.c Flushes verification sequences after failures.
examples/​pqc/​mldsa_sign.c Tracks and releases unfinished sequences.
examples/​pcr/​read_pcr.c Returns an error for out-of-range PCR indexes.
examples/​pcr/​quote.c Propagates argument, hashing, and verification failures.
examples/​pcr/​policy.c Corrects the session description in a comment.
examples/​pcr/​policy_sign.c Checks signature capacity, preserves errors, and wipes key buffers.
examples/​pcr/​extend.c Returns an error for out-of-range PCR indexes.
examples/​nvram/​store.c Selects authorization by NV range and accepts range boundaries.
examples/​nvram/​seal_nv.c Validates PCR indexes and moves encryption to slot 1.
examples/​nvram/​policy_nv.c Separates policy and encryption sessions and refreshes the NV Name.
examples/​nvram/​extend.c Adjusts sessions, platform attributes, and endorsement-key cleanup.
examples/​nvram/​counter.c Selects authorization by NV range and accepts range boundaries.
examples/​management/​da_check.c Saves and restores dictionary-attack parameters.
examples/​keygen/​keyload.c Separates sessions and cleans up keys after persistence failures.
examples/​keygen/​keyimport.c Wipes the imported key buffer before release.
examples/​keygen/​keygen.c Separates sessions and propagates artifact-writing failures.
examples/​keygen/​ecdh.c Adds shared-secret wiping at cleanup.
examples/​keygen/​create_primary.c Corrects the session description in a comment.
examples/​gpio/​gpio_set.c Separates optional pin parsing from state options.
examples/​firmware/​st33_fw_update.c Preserves start-command state for policy rollback.
examples/​firmware/​ifx_fw_update.c Preserves start-command state for policy rollback.
examples/​firmware/​ifx_fw_extract.c Uses the supplied buffer in extraction macros.
examples/​endorsement/​verify_ek_cert.c Documents the limits of TPM-side certificate verification.
examples/​csr/​csr.c Normalizes success and reports output-file opening failures.
examples/​boot/​secret_unseal.c Tightens PCR validation and corrects the example description.
examples/​bench/​bench.c Flushes ML-DSA verification sequences after completion failures.
examples/​attestation/​activate_credential.c Accepts an input filename alongside the endorsement-key option.

Review effort: Balanced

Comment thread examples/gpio/gpio_set.c Resolved
Comment thread examples/keygen/ecdh.c Resolved
Comment thread examples/nvram/policy_nv.c Resolved

@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] Platform-range indices retain owner authorization — examples/nvram/policy_nv.c:95-107
  • [Medium] NV hierarchy selection lacks boundary coverage — examples/nvram/policy_nv.c:95-107

Review generated by Skoll

Comment thread examples/nvram/policy_nv.c Resolved
Comment thread examples/nvram/policy_nv.c Resolved
@dgarske
dgarske requested review from aidangarske and a balanced review from Copilot November 23, 2026 20:56

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.

🟡 Changes recommended

The unsalted, unbound sessions used with -aes or -xor in both NV examples let a bus observer derive the encryption key from public nonces and recover NV data.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread examples/nvram/policy_nv.c Resolved
Comment thread examples/wrap/wrap_test.c Resolved
Comment thread src/tpm2_wrap.c Resolved
Comment thread examples/nvram/policy_nv.c Resolved
Comment thread examples/spdm/spdm_ctrl.c Resolved
Comment thread examples/spdm/nv_bind.c Resolved
@aidangarske
aidangarske merged commit a922608 into wolfSSL:master Nov 24, 2026
249 checks passed
@aidangarske
aidangarske deleted the fenrir_examples branch November 24, 2026 21:41
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.

3 participants