Skip to content

Fix TPM2_ASN_TrimZeros skipping bytes - #644

Merged
dgarske merged 1 commit into
wolfSSL:masterfrom
deadcaf3:fix-asn-trimzeros
Oct 21, 2026
Merged

dgarske merged 1 commit into
wolfSSL:masterfrom
deadcaf3:fix-asn-trimzeros

Conversation

@deadcaf3

@deadcaf3 deadcaf3 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

TPM2_ASN_TrimZeros incremented both idx and in, so after the first leading zero it checked bytes two positions ahead. An ECDSA R or S starting 00 <0x80 00 <0x80 lost a real byte, and wolfTPM2_PK_EccSign sent a bad signature.

This caused a rare flake in the PQC TLS examples job (tls client ecc -pk -aes failed!, client error -330). There, S padded to 00 71 00 4f ... was trimmed to 00 4f ..., giving a 67-byte signature instead of 69.

Fix: only advance idx and return in + idx.

Testing

  • Added test_TPM2_ASN_TrimZeros with the bytes from the failed run. It fails without the fix.
  • Rebuilt the failed run's signature from its logged public key, digest, R and S. The old trim gives 67 bytes and fails wc_ecc_verify_hash. The fix gives 69 bytes and verifies.
  • make check (fwTPM + swtpm + v1.85) passes.

The loop advanced both idx and the in pointer, so each check after the
first read two bytes past the intended one. An ECDSA R or S starting
00 <0x80 00 <0x80 lost a significant byte, and wolfTPM2_PK_EccSign then
DER-encoded a wrong signature that the TLS peer rejected (-330).

Only advance idx and return in + idx. Add a unit test using the S bytes
from the failing CI run.
Copilot AI balanced review requested due to automatic review settings October 21, 2026 18:59

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

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 localized fix corrects the indexing defect, with regression coverage and no identified blockers.

No open issues 🎉

What changed in this PR

Fixes leading-zero trimming in ASN.1 integers to prevent corruption of ECDSA signatures.

Changes:

  • Advances only the index while trimming, keeping the returned pointer and length consistent.
  • Adds regression tests for the reported failure, required sign padding, and consecutive leading zeros.
File Description
tests/​unit_tests.c Adds and registers trimming regression tests.
src/​tpm2_asn.c Removes double advancement that skipped significant bytes.

Review effort: Balanced

@dgarske dgarske self-assigned this Oct 21, 2026
@dgarske

dgarske commented Oct 21, 2026

Copy link
Copy Markdown
Member

Okay to test. Contributor agreement on file.

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

Thanks for the contribution @deadcaf3

@aidangarske aidangarske removed their assignment Oct 21, 2026
@aidangarske
aidangarske requested a review from dgarske October 21, 2026 22:45
@dgarske
dgarske merged commit 176c28f into wolfSSL:master Oct 21, 2026
242 checks passed
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