Skip to content

Bug in signatureToKeyBytes when parsing v (recovery id): #6352

Closed
@jumbobaam

Description

@jumbobaam

Summary
In ECKey.java, the computation of the ECDSA recovery ID (recId) from a signature header byte was incorrect in earlier versions. Specifically, the adjustment of the header byte was done after computing recId, which could lead to recId = 4 — an invalid value.

Location
File: crypto/src/main/java/org/tron/common/crypto/ECKey.java
Original (buggy) logic:

int header = signatureEncoded[64] & 0xFF;
if (header < 27 || header > 34) {
throw new SignatureException("Header byte out of range: " + header);
}
int recId = header - 27;
if (recId >= 4) {
header -= 4;
}
Bug Detail
When header == 31, the code computes recId = 4 (invalid).

It then adjusts header -= 4, but does not recompute recId, which remains 4.

This leads to an invalid call to recoverFromSignature(4, ...), which expects recId ∈ [0..3].

Impact
Incorrect or failed public key recovery from ECDSA signatures.

Possible DoS if crafted signatures are processed.

Incompatible behavior with Ethereum/Bitcoin-style ECDSA recovery.

Cryptographic misuse in consensus/security-sensitive contexts.

Fixed Code ✅
This logic ensures recId is always valid:

int header = signatureEncoded[64] & 0xFF;
if (header < 27 || header > 34) {
throw new SignatureException("Header byte out of range: " + header);
}
if (header >= 31) {
header -= 4;
}
int recId = header - 27;
Or equivalently (one-liner version):

int recId = (header >= 31) ? header - 31 : header - 27;
Resolution
This issue has been resolved in the current version of the code by adjusting the header before computing recId.

This fix was directly based on my original report and suggestion.

Request for Bounty Consideration
Since this issue:

Was triggered by crafted input (malformed signatures),

Affected cryptographic logic critical to consensus and wallet behavior,

Was confirmed, fixed, and now reflected in production code,

And posed a security concern with real-world implications,

…I would like to kindly request consideration for a security bounty, if this falls under your vulnerability disclosure or rewards program.

I'm grateful for the opportunity to help improve Java-Tron's security and appreciate your review of this request.

Best regards,
@jumbobalm

3149450

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions