Skip to content

Commit

Permalink
Merge pull request #53 from trilitech/emturner@base58-prefix-check
Browse files Browse the repository at this point in the history
crypto: add explicit base58 prefix check
  • Loading branch information
emturner authored Dec 13, 2023
2 parents f34d805 + 169f0ae commit caefa03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Added

- Nothing.
- Add `FromBase58CheckError::IncorrectBase58Prefix` variant.

### Changed

Expand All @@ -24,7 +24,8 @@ parameterized by the lifetime of the input byte slice.

### Fixed

- Nothing.
- Fix prefix used in `SeedEd25519` encoding.
- Add explicit prefix check during base58check decoding.

### Security

Expand Down
3 changes: 3 additions & 0 deletions crypto/src/base58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub enum FromBase58CheckError {
DataTooLong,
#[error("mismatched data lenght: expected {expected}, actual {actual}")]
MismatchedLength { expected: usize, actual: usize },
/// Prefix does not match expected.
#[error("incorrect base58 prefix for hash type")]
IncorrectBase58Prefix,
}

/// Possible errors for ToBase58Check
Expand Down
31 changes: 27 additions & 4 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod prefix_bytes {
pub const PUBLIC_KEY_SECP256K1: [u8; 4] = [3, 254, 226, 86];
pub const PUBLIC_KEY_P256: [u8; 4] = [3, 178, 139, 127];
pub const PUBLIC_KEY_BLS: [u8; 4] = [6, 149, 135, 204];
pub const SEED_ED25519: [u8; 4] = [43, 246, 78, 7];
pub const SEED_ED25519: [u8; 4] = [13, 15, 58, 7];
pub const SECRET_KEY_ED25519: [u8; 4] = [43, 246, 78, 7];
pub const SECRET_KEY_BLS: [u8; 4] = [3, 150, 192, 40];
pub const ED22519_SIGNATURE_HASH: [u8; 5] = [9, 245, 205, 134, 18];
Expand Down Expand Up @@ -482,14 +482,19 @@ impl HashType {
return Ok(hash.to_vec());
}
}
} else if !hash.starts_with(self.base58check_prefix()) {
println!("expected: {:?}, found: {hash:?}", self.base58check_prefix());
return Err(FromBase58CheckError::IncorrectBase58Prefix);
}

let expected_len = self.size() + self.base58check_prefix().len();
if expected_len != hash.len() {
return Err(FromBase58CheckError::MismatchedLength {
expected: expected_len,
actual: hash.len(),
});
}

// prefix is not present in a binary representation
hash.drain(0..self.base58check_prefix().len());
Ok(hash)
Expand Down Expand Up @@ -1056,9 +1061,10 @@ mod tests {
}

#[test]
fn test_b58_to_hash_mismatched_lenght() -> Result<(), anyhow::Error> {
let b58 = HashType::ChainId.hash_to_b58check(&[0, 0, 0, 0])?;
let result = HashType::BlockHash.b58check_to_hash(&b58);
fn test_b58_to_hash_mismatched_length() -> Result<(), anyhow::Error> {
let b58 = "BwKZdq9yAc1ucmPPoUeRxRQRUeks64eswrLoSa2eZipYwB3UftmTd1pmg4uyiwU6Ge3guh7CoZdpL4YPm35Ajvu5gQu5mYwEgwA8UmjZNaXV7ecc7qkcoe6xro";
let result = HashType::BlockHash.b58check_to_hash(b58);
println!("{result:?}");
assert!(matches!(
result,
Err(FromBase58CheckError::MismatchedLength {
Expand Down Expand Up @@ -1268,6 +1274,23 @@ mod tests {
);
}

#[test]
fn from_base58check_incorrect_prefix() {
let h = ContractTz1Hash::from_base58_check("tz4FENGt5zkiGaHPm1ya4MgLomgkL1k7Dy7q");

assert!(matches!(
h,
Err(FromBase58CheckError::IncorrectBase58Prefix)
));

let h = ContractTz4Hash::from_base58_check("tz1ei4WtWEMEJekSv8qDnu9PExG6Q8HgRGr3");

assert!(matches!(
h,
Err(FromBase58CheckError::IncorrectBase58Prefix)
));
}

#[test]
fn block_payload_hash() {
let operation_0 = "oom9d3PpjjaMzgg9mZ1pDrF8kjdyzDb41Bd2XE6Y3kRtFHXLku3";
Expand Down

0 comments on commit caefa03

Please sign in to comment.