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:Correct ECDSA signature malleability handling #13544

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ impl Signature {
if bytes.len() != SIGNATURE_LENGTH {
return Err(CryptoMaterialError::WrongLengthError);
}
if !Signature::check_s_lt_order_half(&bytes[32..]) {
if !Signature::check_s_le_order_half(&bytes[32..]) {
return Err(CryptoMaterialError::CanonicalRepresentationError);
}
Ok(())
}

/// Check if S < ORDER_HALF to capture invalid signatures.
fn check_s_lt_order_half(s: &[u8]) -> bool {
/// Check if S <= ORDER_HALF to capture invalid signatures.
Copy link
Contributor

@alinush alinush Jun 4, 2024

Choose a reason for hiding this comment

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

Can you rename this to check_s_le_order_half?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have renamed the function to check_s_le_order_half as requested. The changes have been committed under hash f46e76c21551f17ca7b0a04bd473b50e7d6c11e8.

fn check_s_le_order_half(s: &[u8]) -> bool {
for i in 0..32 {
match s[i].cmp(&ORDER_HALF[i]) {
Ordering::Less => return true,
Ordering::Greater => return false,
_ => {},
}
}
// At this stage S == ORDER_HALF which implies a non canonical S.
false
// At this stage S == ORDER_HALF , it is still considered a canonical value.
true
}

/// If the signature {R,S} does not have S < n/2 where n is the Ristretto255 order, return
Expand Down
10 changes: 4 additions & 6 deletions crates/aptos-crypto/src/unit_tests/secp256r1_ecdsa_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,11 @@ proptest! {
let sig_unchecked = Signature::from_bytes_unchecked(&serialized);
prop_assert!(sig_unchecked.is_ok());

// Update the signature by setting S = L to make it invalid.
// S = ORDER_HALF should be a canonical signature.
serialized[32..].copy_from_slice(&ORDER_HALF);
let serialized_malleable_l: &[u8] = &serialized;
// try_from will fail with CanonicalRepresentationError.
prop_assert_eq!(
Signature::try_from(serialized_malleable_l),
Err(CryptoMaterialError::CanonicalRepresentationError)
let canonical: &[u8] = &serialized;
prop_assert!(
Signature::try_from(canonical).is_ok()
);
}
}
Loading