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

cksum: Added check for sum length #6606

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where
(
if let Some(length) = options.length {
// Multiply by 8 here, as we want to print the length in bits.
format!("BLAKE2b-{} (", length * 8)
format!("BLAKE2b-{} (", length)
} else {
"BLAKE2b (".to_owned()
},
Expand Down Expand Up @@ -552,9 +552,8 @@ mod tests {

#[test]
fn test_calculate_length() {
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(256));
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
calculate_blake2b_length(255).unwrap_err();

calculate_blake2b_length(33).unwrap_err();
Expand Down
21 changes: 13 additions & 8 deletions src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
} else {
Ok(HashAlgorithm {
name: ALGORITHM_OPTIONS_BLAKE2B,
create_fn: Box::new(move || Box::new(Blake2b::with_output_bytes(bits))),
create_fn: Box::new(move || Box::new(Blake2b::with_output_bytes(bits / 8))),
bits,
})
}
Expand All @@ -248,7 +248,7 @@
ALGORITHM_OPTIONS_SM3 => Ok(HashAlgorithm {
name: ALGORITHM_OPTIONS_SM3,
create_fn: Box::new(|| Box::new(Sm3::new())),
bits: 512,
bits: 256,
}),
ALGORITHM_OPTIONS_SHAKE128 | "shake128sum" => {
let bits =
Expand Down Expand Up @@ -438,7 +438,7 @@
let bits = caps.name("bits").map_or(Some(None), |m| {
let bits_value = m.as_str().parse::<usize>().unwrap();
if bits_value % 8 == 0 {
Some(Some(bits_value / 8))
Some(Some(bits_value))
} else {
*properly_formatted = false;
None // Return None to signal a divisibility issue
Expand Down Expand Up @@ -516,7 +516,7 @@
get_expected_checksum(filename_to_check, &caps, &chosen_regex)?;

// If the algo_name is provided, we use it, otherwise we try to detect it
let (algo_name, length) = if is_algo_based_format {
let (algo_name, length_in_bits) = if is_algo_based_format {
identify_algo_name_and_length(
&caps,
algo_name_input,
Expand All @@ -530,7 +530,7 @@
if algo_name_input == Some(ALGORITHM_OPTIONS_BLAKE2B) {
// division by 2 converts the length of the Blake2b checksum from hexadecimal
// characters to bytes, as each byte is represented by two hexadecimal characters.
let length = Some(expected_checksum.len() / 2);
let length = Some(expected_checksum.len() * 4);
(ALGORITHM_OPTIONS_BLAKE2B.to_string(), length)
} else {
(a.to_lowercase(), length_input)
Expand All @@ -545,7 +545,12 @@
properly_formatted = false;
continue;
}
let mut algo = detect_algo(&algo_name, length)?;
let mut algo = detect_algo(&algo_name, length_in_bits)?;

if length_in_bits.unwrap_or(algo.bits) != (expected_checksum.len() * 4) {
res.bad_format += 1;

Check warning on line 551 in src/uucore/src/lib/features/checksum.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/checksum.rs#L551

Added line #L551 was not covered by tests
continue;
}

let (filename_to_check_unescaped, prefix) = unescape_filename(filename_to_check);

Expand Down Expand Up @@ -699,7 +704,7 @@
// So, don't show it
Ok(None)
} else {
Ok(Some(n / 8))
Ok(Some(n))
}
}
}
Expand Down Expand Up @@ -771,7 +776,7 @@
assert!(calculate_blake2b_length(10).is_err());
assert!(calculate_blake2b_length(520).is_err());
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(256));
}

#[test]
Expand Down
Loading