diff --git a/Cargo.lock b/Cargo.lock index 1e2d3b9f..b24ef832 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,7 +257,7 @@ dependencies = [ [[package]] name = "sha1-checked" -version = "0.11.0-pre.3" +version = "0.11.0" dependencies = [ "digest", "hex-literal", diff --git a/sha1-checked/Cargo.toml b/sha1-checked/Cargo.toml index 5e424e2a..2387b124 100644 --- a/sha1-checked/Cargo.toml +++ b/sha1-checked/Cargo.toml @@ -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" diff --git a/sha1-checked/src/compress.rs b/sha1-checked/src/compress.rs index 92a02cf3..60e4bf87 100644 --- a/sha1-checked/src/compress.rs +++ b/sha1-checked/src/compress.rs @@ -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()); @@ -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) @@ -726,14 +723,14 @@ 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; } @@ -741,7 +738,6 @@ pub(super) fn compress( } } } - *state = state_cpy; } const SHA1_PADDING: [u8; 64] = [ diff --git a/sha1-checked/src/lib.rs b/sha1-checked/src/lib.rs index 25baf66e..0ba048d3 100644 --- a/sha1-checked/src/lib.rs +++ b/sha1-checked/src/lib.rs @@ -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, }; @@ -49,19 +49,11 @@ pub struct Sha1 { h: [u32; STATE_LEN], block_len: u64, detection: Option, - buffer: BlockBuffer<::BlockSize, ::BufferKind>, + buffer: BlockBuffer, } 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() @@ -114,7 +106,7 @@ impl Sha1 { } fn finalize_inner(&mut self, out: &mut Output) { - let bs = ::BlockSize::U64; + let bs = 64; let buffer = &mut self.buffer; let h = &mut self.h; diff --git a/sha1-checked/tests/mod.rs b/sha1-checked/tests/mod.rs index 7f09ab45..7ee0a57f 100644 --- a/sha1-checked/tests/mod.rs +++ b/sha1-checked/tests/mod.rs @@ -24,10 +24,11 @@ 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, @@ -35,10 +36,11 @@ fn shambles_1() { ); } +#[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, @@ -46,10 +48,11 @@ fn shambles_2() { ); } +#[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")), @@ -57,10 +60,11 @@ fn shattered_1() { ); } +#[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")), @@ -68,10 +72,11 @@ fn shattered_2() { ); } +#[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")), @@ -79,18 +84,24 @@ fn reducedsha_coll() { ); } +#[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,); @@ -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); @@ -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); @@ -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);