Repository navigation
Fix TPM2_ASN_TrimZeros skipping bytes - #644
Merged
Merged
Conversation
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.
There was a problem hiding this comment.
🟢 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
Member
|
Okay to test. Contributor agreement on file. |
aidangarske
approved these changes
Oct 21, 2026
aidangarske
left a comment
Member
There was a problem hiding this comment.
Thanks for the contribution @deadcaf3
dgarske
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TPM2_ASN_TrimZerosincremented bothidxandin, so after the first leading zero it checked bytes two positions ahead. An ECDSA R or S starting00 <0x80 00 <0x80lost a real byte, andwolfTPM2_PK_EccSignsent 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 to00 71 00 4f ...was trimmed to00 4f ..., giving a 67-byte signature instead of 69.Fix: only advance
idxand returnin + idx.Testing
test_TPM2_ASN_TrimZeroswith the bytes from the failed run. It fails without the fix.wc_ecc_verify_hash. The fix gives 69 bytes and verifies.make check(fwTPM + swtpm + v1.85) passes.