From 1d4e41617581ad6be645804bafe6bfd27228d486 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 12 Oct 2023 22:24:07 -0700 Subject: [PATCH] Digest/Polyfill: Remove SHA-1 use of `ChunksFixed` and delete it. This is the last step in the removal of `ChunksFixed`, which contains one line of `unsafe` code. --- src/digest/sha1.rs | 15 ++++++++------- src/polyfill.rs | 5 +---- src/polyfill/chunks_fixed.rs | 30 ------------------------------ 3 files changed, 9 insertions(+), 41 deletions(-) delete mode 100644 src/polyfill/chunks_fixed.rs diff --git a/src/digest/sha1.rs b/src/digest/sha1.rs index 3736630d03..496143e2c4 100644 --- a/src/digest/sha1.rs +++ b/src/digest/sha1.rs @@ -14,7 +14,7 @@ // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. use super::sha2::{ch, maj, Word}; -use crate::{c, polyfill::ChunksFixed}; +use crate::c; use core::num::Wrapping; pub const BLOCK_LEN: usize = 512 / 8; @@ -63,11 +63,10 @@ fn block_data_order_(mut H: State, M: &[[::InputBytes; 16]]) -> Sta let [a, b, c, d, e] = H; // FIPS 180-4 6.1.2 Step 3 with constants and functions from FIPS 180-4 {4.1.1, 4.2.1} - let W: &[[W32; 20]; 4] = W.chunks_fixed(); - let (a, b, c, d, e) = step3(a, b, c, d, e, W[0], Wrapping(0x5a827999), ch); - let (a, b, c, d, e) = step3(a, b, c, d, e, W[1], Wrapping(0x6ed9eba1), parity); - let (a, b, c, d, e) = step3(a, b, c, d, e, W[2], Wrapping(0x8f1bbcdc), maj); - let (a, b, c, d, e) = step3(a, b, c, d, e, W[3], Wrapping(0xca62c1d6), parity); + let (a, b, c, d, e) = step3(a, b, c, d, e, &W, 0, Wrapping(0x5a827999), ch); + let (a, b, c, d, e) = step3(a, b, c, d, e, &W, 20, Wrapping(0x6ed9eba1), parity); + let (a, b, c, d, e) = step3(a, b, c, d, e, &W, 40, Wrapping(0x8f1bbcdc), maj); + let (a, b, c, d, e) = step3(a, b, c, d, e, &W, 60, Wrapping(0xca62c1d6), parity); // FIPS 180-4 6.1.2 Step 4 H[0] += a; @@ -87,10 +86,12 @@ fn step3( mut c: W32, mut d: W32, mut e: W32, - W: [W32; 20], + W: &[W32; 80], + t: usize, k: W32, f: impl Fn(W32, W32, W32) -> W32, ) -> (W32, W32, W32, W32, W32) { + let W = &W[t..(t + 20)]; for W_t in W.iter() { let T = rotl(a, 5) + f(b, c, d) + e + k + W_t; e = d; diff --git a/src/polyfill.rs b/src/polyfill.rs index 3fd5b11ae5..4533d42b67 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -24,9 +24,6 @@ pub fn usize_from_u32(x: u32) -> usize { x as usize } -#[macro_use] -mod chunks_fixed; - mod array_flat_map; mod array_flatten; mod array_split_map; @@ -41,7 +38,7 @@ mod unwrap_const; pub use self::{ array_flat_map::ArrayFlatMap, array_flatten::ArrayFlatten, array_split_map::ArraySplitMap, - chunks_fixed::*, unwrap_const::unwrap_const, + unwrap_const::unwrap_const, }; #[cfg(feature = "alloc")] diff --git a/src/polyfill/chunks_fixed.rs b/src/polyfill/chunks_fixed.rs deleted file mode 100644 index 152cd8da03..0000000000 --- a/src/polyfill/chunks_fixed.rs +++ /dev/null @@ -1,30 +0,0 @@ -/// Allows splitting a reference to an array type into fixed-length chunks. -pub trait ChunksFixed<'a, Chunks> -where - Chunks: 'a, -{ - fn chunks_fixed(self) -> Chunks; -} - -/// `$unchuncked_len` must be divisible by `$chunk_len`. -macro_rules! define_chunks_fixed { - ( $unchuncked_len:expr, $chunk_len:expr ) => { - define_chunks_fixed!($unchuncked_len, $chunk_len, $unchuncked_len / $chunk_len); - }; - - ( $unchuncked_len:expr, $chunk_len:expr, $chunked_len:expr ) => { - impl<'a, T> ChunksFixed<'a, &'a [[T; $chunk_len]; $chunked_len]> - for &'a [T; $unchuncked_len] - { - #[inline(always)] - fn chunks_fixed(self) -> &'a [[T; $chunk_len]; $chunked_len] { - let as_ptr = self.as_ptr().cast::<[T; $chunk_len]>(); - let as_ptr = as_ptr.cast::<[[T; $chunk_len]; $chunked_len]>(); - unsafe { &*as_ptr } - } - } - }; -} - -// Sorted by the first value, then the second value. -define_chunks_fixed!(80, 20);