Skip to content

Commit

Permalink
apply CR
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 13, 2024
1 parent d2afd9e commit 6a0da53
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sha1-checked/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sha1-checked"
version = "0.11.0-pre.3"
version = "0.11.0"
description = "SHA-1 hash function with collision detection"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
Expand Down
14 changes: 5 additions & 9 deletions sha1-checked/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,9 @@ pub(super) fn compress(
blocks: &[[u8; BLOCK_SIZE]],
) {
let mut block_u32 = [0u32; BLOCK_SIZE / 4];
// since LLVM can't properly use aliasing yet it will make
// unnecessary state stores without this copy
let mut state_cpy = *state;

for block in blocks.iter() {
ctx.ihv1.copy_from_slice(&state_cpy);
ctx.ihv1.copy_from_slice(&*state);

for (o, chunk) in block_u32.iter_mut().zip(block.chunks_exact(4)) {
*o = u32::from_be_bytes(chunk.try_into().unwrap());
Expand All @@ -689,7 +686,7 @@ pub(super) fn compress(
..
} = ctx;

compression_states(&mut state_cpy, &block_u32, m1, state_58, state_65);
compression_states(state, &block_u32, m1, state_58, state_65);

let ubc_mask = if ctx.ubc_check {
crate::ubc_check::ubc_check(&ctx.m1)
Expand Down Expand Up @@ -726,22 +723,21 @@ pub(super) fn compress(
);

// to verify SHA-1 collision detection code with collisions for reduced-step SHA-1
if (0 == xor(&ihvtmp, &state_cpy))
if (0 == xor(&ihvtmp, &*state))
|| (ctx.reduced_round_collision && 0 == xor(&ctx.ihv1, &ctx.ihv2))
{
ctx.found_collision = true;

if ctx.safe_hash {
compression_w(&mut state_cpy, &ctx.m1);
compression_w(&mut state_cpy, &ctx.m1);
compression_w(state, &ctx.m1);
compression_w(state, &ctx.m1);
}
break;
}
}
}
}
}
*state = state_cpy;
}

const SHA1_PADDING: [u8; 64] = [
Expand Down
14 changes: 3 additions & 11 deletions sha1-checked/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use digest::zeroize::{Zeroize, ZeroizeOnDrop};
use digest::{
array::Array,
block_buffer::{BlockBuffer, Eager},
core_api::{BlockSizeUser, BufferKindUser},
core_api::BlockSizeUser,
typenum::{Unsigned, U20, U64},
FixedOutput, FixedOutputReset, HashMarker, Output, OutputSizeUser, Reset, Update,
};
Expand All @@ -49,19 +49,11 @@ pub struct Sha1 {
h: [u32; STATE_LEN],
block_len: u64,
detection: Option<DetectionState>,
buffer: BlockBuffer<<Self as BlockSizeUser>::BlockSize, <Self as BufferKindUser>::BufferKind>,
buffer: BlockBuffer<U64, Eager>,
}

impl HashMarker for Sha1 {}

impl BlockSizeUser for Sha1 {
type BlockSize = U64;
}

impl BufferKindUser for Sha1 {
type BufferKind = Eager;
}

impl Default for Sha1 {
fn default() -> Self {
Builder::default().build()
Expand Down Expand Up @@ -114,7 +106,7 @@ impl Sha1 {
}

fn finalize_inner(&mut self, out: &mut Output<Self>) {
let bs = <Self as BlockSizeUser>::BlockSize::U64;
let bs = 64;
let buffer = &mut self.buffer;
let h = &mut self.h;

Expand Down
33 changes: 22 additions & 11 deletions sha1-checked/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,73 +24,84 @@ fn sha1_collision_rand() {
);
}

#[cfg(feature = "std")]
#[test]
fn shambles_1() {
collision_test(
&include_bytes!("./data/sha-mbles-1.bin")[..],
"./data/sha-mbles-1.bin",
hex!("8ac60ba76f1999a1ab70223f225aefdc78d4ddc0"),
hex!("4f3d9be4a472c4dae83c6314aa6c36a064c1fd14"),
None,
false,
);
}

#[cfg(feature = "std")]
#[test]
fn shambles_2() {
collision_test(
&include_bytes!("./data/sha-mbles-2.bin")[..],
"./data/sha-mbles-2.bin",
hex!("8ac60ba76f1999a1ab70223f225aefdc78d4ddc0"),
hex!("9ed5d77a4f48be1dbf3e9e15650733eb850897f2"),
None,
false,
);
}

#[cfg(feature = "std")]
#[test]
fn shattered_1() {
collision_test(
&include_bytes!("./data/shattered-1.pdf")[..],
"./data/shattered-1.pdf",
hex!("38762cf7f55934b34d179ae6a4c80cadccbb7f0a"),
hex!("16e96b70000dd1e7c85b8368ee197754400e58ec"),
Some(hex!("d3a1d09969c3b57113fd17b23e01dd3de74a99bb")),
false,
);
}

#[cfg(feature = "std")]
#[test]
fn shattered_2() {
collision_test(
&include_bytes!("./data/shattered-2.pdf")[..],
"./data/shattered-2.pdf",
hex!("38762cf7f55934b34d179ae6a4c80cadccbb7f0a"),
hex!("e1761773e6a35916d99f891b77663e6405313587"),
Some(hex!("92246b0b718f4c704d37bb025717cbc66babf102")),
false,
);
}

#[cfg(feature = "std")]
#[test]
fn reducedsha_coll() {
collision_test(
&include_bytes!("./data/sha1_reducedsha_coll.bin")[..],
"./data/sha1_reducedsha_coll.bin",
hex!("a56374e1cf4c3746499bc7c0acb39498ad2ee185"),
hex!("dd39885a2a5d8f59030b451e00cb45da9f9d3828"),
Some(hex!("dd39885a2a5d8f59030b451e00cb45da9f9d3828")),
true,
);
}

#[cfg(feature = "std")]
fn collision_test(
input: &[u8],
input_path: &str,
hash: [u8; 20],
mitigated_hash: [u8; 20],
reduced_rounds_mitigated: Option<[u8; 20]>,
reduced_rounds: bool,
) {
let p = std::env::current_dir()
.unwrap()
.join("tests")
.join(input_path);
let input = std::fs::read(p).unwrap();
let has_collision = true;

// No detection.
let mut ctx = Sha1::builder().detect_collision(false).build();
ctx.update(input);
ctx.update(&input);
let d = ctx.try_finalize();
assert!(!d.has_collision());
assert_eq!(&d.hash()[..], hash,);
Expand All @@ -100,7 +111,7 @@ fn collision_test(
.safe_hash(false)
.reduced_round_collision(reduced_rounds)
.build();
ctx.update(input);
ctx.update(&input);

let d = ctx.try_finalize();
assert_eq!(d.has_collision(), has_collision);
Expand All @@ -112,7 +123,7 @@ fn collision_test(
.use_ubc(false)
.reduced_round_collision(reduced_rounds)
.build();
ctx.update(input);
ctx.update(&input);
let d = ctx.try_finalize();
assert_eq!(d.has_collision(), has_collision);
assert_eq!(&d.hash()[..], hash);
Expand All @@ -121,14 +132,14 @@ fn collision_test(
let mut ctx = Sha1::builder()
.reduced_round_collision(reduced_rounds)
.build();
ctx.update(input);
ctx.update(&input);
let d = ctx.try_finalize();
assert_eq!(d.has_collision(), has_collision);
assert_eq!(&d.hash()[..], mitigated_hash);

if let Some(rr) = reduced_rounds_mitigated {
let mut ctx = Sha1::builder().reduced_round_collision(true).build();
ctx.update(input);
ctx.update(&input);
let d = ctx.try_finalize();
assert_eq!(d.has_collision(), has_collision);
assert_eq!(&d.hash()[..], rr);
Expand Down

0 comments on commit 6a0da53

Please sign in to comment.