Skip to content

Fix uint16_t truncation#314

Merged
bigbrett merged 1 commit intomainfrom
uint16
Mar 20, 2026
Merged

Fix uint16_t truncation#314
bigbrett merged 1 commit intomainfrom
uint16

Conversation

@jackctj117
Copy link
Contributor

… functions by using uint32_t for intermediate length computation before bounds check
This pull request addresses a medium-severity bug involving potential integer truncation when calculating request lengths in several cryptographic functions. The main fix is to use a wider integer type (uint32_t) for intermediate length computations, ensuring that values are properly validated before being cast to uint16_t. This prevents silent wrapping and potential security issues when handling large data sizes.

Bug documentation and guidance:

  • Added a new file, bug-fix.txt, documenting the bug, its impact, and recommended steps for confirming and fixing similar issues in the future. The file specifically describes the integer truncation bug in wh_Client_MlDsaSign and wh_Client_MlDsaVerify and provides a pattern for safe length computation.

Length computation and validation fixes:

  • Updated wh_Client_Ed25519Sign, wh_Client_Ed25519Verify, wh_Client_MlDsaSign, wh_Client_MlDsaVerify, and _HkdfMakeKey functions in src/wh_client_crypto.c to:
    • Compute request lengths using uint32_t for all intermediate calculations.
    • Validate the uint32_t value against WOLFHSM_CFG_COMM_DATA_LEN before casting to uint16_t.
    • Only assign to uint16_t req_len after passing the bounds check, following the recommended safe pattern. [1] [2] [3] [4] [5] [6] [7] [8]

@jackctj117 jackctj117 self-assigned this Mar 19, 2026
Copilot AI review requested due to automatic review settings March 19, 2026 16:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes potential request-length truncation by widening intermediate length computations and validating bounds before casting to uint16_t, reducing the risk of wrapped lengths in several crypto client request builders.

Changes:

  • Switched intermediate request length calculations from uint16_t to uint32_t and deferred casting until after bounds checks.
  • Added bounds checks in _HkdfMakeKey and updated length validation in ML-DSA sign/verify paths.
  • Added a new bug-fix.txt file documenting the bug and a recommended safe computation pattern.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/wh_client_crypto.c Uses uint32_t intermediates for request sizes and checks bounds before casting to uint16_t.
bug-fix.txt Adds documentation describing the truncation bug and how to address similar issues.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

… functions by using uint32_t for intermediate length computation before bounds check
@dgarske dgarske assigned bigbrett and unassigned jackctj117 Mar 19, 2026
@dgarske dgarske requested a review from bigbrett March 19, 2026 18:34
@bigbrett bigbrett merged commit 977bf18 into main Mar 20, 2026
51 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.

6 participants