RIPEMD-160 precompile yields wrong hashes for large set of inputs due to off-by-one error #50
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
H-05
primary issue
Highest quality submission among a set of duplicates
🤖_primary
AI based primary recommendation
🤖_01_group
AI based duplicate group recommendation
selected for report
This submission will be included/highlighted in the audit report
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/kkrt-labs/kakarot/blob/a4c45215f7fd5cdd9fbbfd11196a9a787c6468df/cairo_zero/kakarot/precompiles/ripemd160.cairo#L455
Vulnerability details
The RIPEMD-160 digest integrates the input data with one additional block including the message length.
If we look at the Cairo code that achieves this:
... we see that the
if (next_block == false)
executes one block of code in caselen >= 55
, and the other otherwise.If we compare this check with the equivalent implementation in Go (used by Geth), for example:
we see that this time, the selection is made with
len < 56
as discriminator; if we imagine swapping theif
and theelse
blocks of the Go implementation, the condition becomeslen >= 56
.So for the edge case of
len == 55
, Kakarot and Geth will take different actions, which, as shown in the PoC, results in differing hashes.len
here represents the length of the input modulo 64, hence the RIPEMD-160 precompile will yield wrong outputs for inputs of length55 + k*64
.Because this precompile is a cryptographic hash function, it can be used in many ways by EVM applications, including access control on funds.
Proof of Concept
To prove the point, the following test can be added to the
test_ripemd160.py
unit test suite:... which yields the following output:
Recommended Mitigation Steps
Change the check at L455 to
is_nn_le(56, len)
.Assessed type
Other
The text was updated successfully, but these errors were encountered: