Skip to content

Commit 461de5b

Browse files
committed
sha1: make compress consume blocks
To align with the semantics of `block_buffer::BlockBuffer::digest_blocks` signature which works with `&[Block]` and not `&[[u8; N]]`.
1 parent 470c789 commit 461de5b

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

sha1/src/compress.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::BLOCK_SIZE;
1+
use crate::Block;
22

33
cfg_if::cfg_if! {
44
if #[cfg(feature = "force-soft")] {
@@ -22,6 +22,6 @@ cfg_if::cfg_if! {
2222
}
2323

2424
/// SHA-1 compression function
25-
pub fn compress(state: &mut [u32; 5], blocks: &[[u8; BLOCK_SIZE]]) {
25+
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
2626
compress_inner(state, blocks);
2727
}

sha1/src/compress/aarch64.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! SHA-1 `aarch64` backend.
22
3+
use crate::Block;
4+
35
// Per rustc target feature docs for `aarch64-unknown-linux-gnu` and
46
// `aarch64-apple-darwin` platforms, the `sha2` target feature enables
57
// SHA-1 as well:
68
//
79
// > Enable SHA1 and SHA256 support.
810
cpufeatures::new!(sha1_hwcap, "sha2");
911

10-
pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
12+
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
1113
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
1214
// after stabilization
1315
if sha1_hwcap::get() {

sha1/src/compress/loongarch64_asm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use core::arch::asm;
44

5+
use crate::Block;
6+
57
const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6];
68

79
macro_rules! c {
@@ -102,7 +104,7 @@ macro_rules! roundtail {
102104
};
103105
}
104106

105-
pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
107+
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
106108
if blocks.is_empty() {
107109
return;
108110
}

sha1/src/compress/soft.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(clippy::many_single_char_names)]
2-
use super::BLOCK_SIZE;
2+
use crate::{Block, BLOCK_SIZE};
33

44
const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6];
55

@@ -244,7 +244,7 @@ fn sha1_digest_block_u32(state: &mut [u32; 5], block: &[u32; 16]) {
244244
state[4] = state[4].wrapping_add(e);
245245
}
246246

247-
pub fn compress(state: &mut [u32; 5], blocks: &[[u8; BLOCK_SIZE]]) {
247+
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
248248
let mut block_u32 = [0u32; BLOCK_SIZE / 4];
249249
// since LLVM can't properly use aliasing yet it will make
250250
// unnecessary state stores without this copy

sha1/src/compress/x86.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use core::arch::x86::*;
77
#[cfg(target_arch = "x86_64")]
88
use core::arch::x86_64::*;
99

10+
use crate::Block;
11+
1012
macro_rules! rounds4 {
1113
($h0:ident, $h1:ident, $wk:expr, $i:expr) => {
1214
_mm_sha1rnds4_epu32($h0, _mm_sha1nexte_epu32($h1, $wk), $i)
@@ -31,7 +33,7 @@ macro_rules! schedule_rounds4 {
3133
}
3234

3335
#[target_feature(enable = "sha,sse2,ssse3,sse4.1")]
34-
unsafe fn digest_blocks(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
36+
unsafe fn digest_blocks(state: &mut [u32; 5], blocks: &[Block]) {
3537
#[allow(non_snake_case)]
3638
let MASK: __m128i = _mm_set_epi64x(0x0001_0203_0405_0607, 0x0809_0A0B_0C0D_0E0F);
3739

@@ -91,7 +93,7 @@ unsafe fn digest_blocks(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
9193

9294
cpufeatures::new!(shani_cpuid, "sha", "sse2", "ssse3", "sse4.1");
9395

94-
pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
96+
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
9597
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
9698
// after stabilization
9799
if shani_cpuid::get() {

sha1/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ pub use digest::{self, Digest};
1111

1212
use core::{convert::TryInto, fmt, slice::from_ref};
1313
use digest::{
14-
array::Array,
1514
block_buffer::Eager,
1615
core_api::{
17-
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
16+
AlgorithmName, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
1817
OutputSizeUser, Reset, UpdateCore,
1918
},
2019
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
@@ -34,6 +33,9 @@ pub use compress::compress;
3433
const STATE_LEN: usize = 5;
3534
const BLOCK_SIZE: usize = <Sha1Core as BlockSizeUser>::BlockSize::USIZE;
3635

36+
/// Block for SHA-1
37+
pub type Block = digest::block_buffer::Block<Sha1Core>;
38+
3739
/// Core SHA-1 hasher state.
3840
#[derive(Clone)]
3941
pub struct Sha1Core {
@@ -60,9 +62,7 @@ impl OutputSizeUser for Sha1Core {
6062

6163
impl UpdateCore for Sha1Core {
6264
#[inline]
63-
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
64-
self.block_len += blocks.len() as u64;
65-
let blocks = Array::cast_slice_to_core(blocks);
65+
fn update_blocks(&mut self, blocks: &[Block]) {
6666
compress(&mut self.h, blocks);
6767
}
6868
}
@@ -74,7 +74,7 @@ impl FixedOutputCore for Sha1Core {
7474
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
7575

7676
let mut h = self.h;
77-
buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(&b.0)));
77+
buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(b)));
7878
for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) {
7979
chunk.copy_from_slice(&v.to_be_bytes());
8080
}

0 commit comments

Comments
 (0)