Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return error if signature len does not eq 65 #717

Merged
merged 11 commits into from
Mar 21, 2023
18 changes: 16 additions & 2 deletions engine-precompiles/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod costs {

mod consts {
pub(super) const INPUT_LEN: usize = 128;
pub(super) const SIGNATURE_LENGTH: usize = 65;
}

/// See: `https://ethereum.github.io/yellowpaper/paper.pdf`
Expand All @@ -19,7 +20,9 @@ mod consts {
// Quite a few library methods rely on this and that should be changed. This
// should only be for precompiles.
pub fn ecrecover(hash: H256, signature: &[u8]) -> Result<Address, ExitError> {
assert_eq!(signature.len(), 65);
if signature.len() != consts::SIGNATURE_LENGTH {
return Err(ExitError::Other(Borrowed("INVALID_SIGNATURE_LENGTH")));
}
birchmd marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "contract")]
return sdk::ecrecover(hash, signature).map_err(|e| ExitError::Other(Borrowed(e.as_str())));
Expand Down Expand Up @@ -87,7 +90,7 @@ impl Precompile for ECRecover {
let mut v = [0; 32];
v.copy_from_slice(&input[32..64]);

let mut signature = [0; 65]; // signature is (r, s, v), typed (uint256, uint256, uint8)
let mut signature = [0; consts::SIGNATURE_LENGTH]; // signature is (r, s, v), typed (uint256, uint256, uint8)
signature[0..32].copy_from_slice(&input[64..96]); // r
signature[32..64].copy_from_slice(&input[96..128]); // s

Expand Down Expand Up @@ -247,4 +250,15 @@ mod tests {
.output;
assert_eq!(res, expected);
}

#[test]
pub fn test_invalid_signature_length() {
let hash = H256::from_slice(
&hex::decode("1111111111111111111111111111111111111111111111111111111111111111")
.unwrap(),
);
let signature = hex::decode("1111").unwrap();
let res = ecrecover(hash, &signature);
assert!(res.is_err());
}
}