Skip to content

Commit c9bb35c

Browse files
committed
sha2: make compress consume blocks
1 parent 51a0a1f commit c9bb35c

File tree

11 files changed

+38
-20
lines changed

11 files changed

+38
-20
lines changed

sha2/src/core_api.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{consts, sha256::compress256, sha512::compress512};
22
use core::{convert::TryInto, fmt, slice::from_ref};
33
use digest::{
4-
array::Array,
54
block_buffer::Eager,
65
core_api::{
76
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser, TruncSide,
@@ -39,7 +38,6 @@ impl UpdateCore for Sha256VarCore {
3938
#[inline]
4039
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
4140
self.block_len += blocks.len() as u64;
42-
let blocks = Array::cast_slice_to_core(blocks);
4341
compress256(&mut self.state, blocks);
4442
}
4543
}
@@ -66,7 +64,7 @@ impl VariableOutputCore for Sha256VarCore {
6664
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
6765
let bs = Self::BlockSize::U64;
6866
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
69-
buffer.len64_padding_be(bit_len, |b| compress256(&mut self.state, from_ref(&b.0)));
67+
buffer.len64_padding_be(bit_len, |b| compress256(&mut self.state, from_ref(b)));
7068

7169
for (chunk, v) in out.chunks_exact_mut(4).zip(self.state.iter()) {
7270
chunk.copy_from_slice(&v.to_be_bytes());
@@ -155,7 +153,6 @@ impl UpdateCore for Sha512VarCore {
155153
#[inline]
156154
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
157155
self.block_len += blocks.len() as u128;
158-
let blocks = Array::cast_slice_to_core(blocks);
159156
compress512(&mut self.state, blocks);
160157
}
161158
}
@@ -184,7 +181,7 @@ impl VariableOutputCore for Sha512VarCore {
184181
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
185182
let bs = Self::BlockSize::U64 as u128;
186183
let bit_len = 8 * (buffer.get_pos() as u128 + bs * self.block_len);
187-
buffer.len128_padding_be(bit_len, |b| compress512(&mut self.state, from_ref(&b.0)));
184+
buffer.len128_padding_be(bit_len, |b| compress512(&mut self.state, from_ref(b)));
188185

189186
for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) {
190187
chunk.copy_from_slice(&v.to_be_bytes());

sha2/src/sha256.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::Sha256VarCore;
2+
3+
type Block = digest::core_api::Block<Sha256VarCore>;
4+
15
cfg_if::cfg_if! {
26
if #[cfg(feature = "force-soft")] {
37
mod soft;
@@ -24,6 +28,6 @@ cfg_if::cfg_if! {
2428
/// This is a low-level "hazmat" API which provides direct access to the core
2529
/// functionality of SHA-256.
2630
#[cfg_attr(docsrs, doc(cfg(feature = "compress")))]
27-
pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
31+
pub fn compress256(state: &mut [u32; 8], blocks: &[Block]) {
2832
compress(state, blocks)
2933
}

sha2/src/sha256/aarch64.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
use core::arch::{aarch64::*, asm};
88

9+
use super::Block;
910
use crate::consts::K32;
1011

1112
cpufeatures::new!(sha2_hwcap, "sha2");
1213

13-
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
14+
pub fn compress(state: &mut [u32; 8], blocks: &[Block]) {
1415
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
1516
// after stabilization
1617
if sha2_hwcap::get() {
@@ -21,7 +22,7 @@ pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
2122
}
2223

2324
#[target_feature(enable = "sha2")]
24-
unsafe fn sha256_compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
25+
unsafe fn sha256_compress(state: &mut [u32; 8], blocks: &[Block]) {
2526
// SAFETY: Requires the sha2 feature.
2627

2728
// Load state into vectors.

sha2/src/sha256/loongarch64_asm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! LoongArch64 assembly backend
22
3+
use super::Block;
4+
35
macro_rules! c {
46
($($l:expr)*) => {
57
concat!($($l ,)*)
@@ -78,7 +80,7 @@ macro_rules! roundtail {
7880
};
7981
}
8082

81-
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
83+
pub fn compress(state: &mut [u32; 8], blocks: &[Block]) {
8284
if blocks.is_empty() {
8385
return;
8486
}

sha2/src/sha256/soft.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![allow(clippy::many_single_char_names)]
22
use crate::consts::K32;
33

4+
use super::Block;
5+
46
#[inline(always)]
57
fn shr(v: [u32; 4], o: u32) -> [u32; 4] {
68
[v[0] >> o, v[1] >> o, v[2] >> o, v[3] >> o]
@@ -227,7 +229,7 @@ fn sha256_digest_block_u32(state: &mut [u32; 8], block: &[u32; 16]) {
227229
state[7] = state[7].wrapping_add(h);
228230
}
229231

230-
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
232+
pub fn compress(state: &mut [u32; 8], blocks: &[Block]) {
231233
for block in blocks {
232234
let mut block_u32 = [0u32; 16];
233235
for (o, chunk) in block_u32.iter_mut().zip(block.chunks_exact(4)) {

sha2/src/sha256/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 super::Block;
11+
1012
unsafe fn schedule(v0: __m128i, v1: __m128i, v2: __m128i, v3: __m128i) -> __m128i {
1113
let t1 = _mm_sha256msg1_epu32(v0, v1);
1214
let t2 = _mm_alignr_epi8(v3, v2, 4);
@@ -39,7 +41,7 @@ macro_rules! schedule_rounds4 {
3941
// we use unaligned loads with `__m128i` pointers
4042
#[allow(clippy::cast_ptr_alignment)]
4143
#[target_feature(enable = "sha,sse2,ssse3,sse4.1")]
42-
unsafe fn digest_blocks(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
44+
unsafe fn digest_blocks(state: &mut [u32; 8], blocks: &[Block]) {
4345
#[allow(non_snake_case)]
4446
let MASK: __m128i = _mm_set_epi64x(
4547
0x0C0D_0E0F_0809_0A0Bu64 as i64,
@@ -99,7 +101,7 @@ unsafe fn digest_blocks(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
99101

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

102-
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
104+
pub fn compress(state: &mut [u32; 8], blocks: &[Block]) {
103105
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
104106
// after stabilization
105107
if shani_cpuid::get() {

sha2/src/sha512.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::Sha512VarCore;
2+
3+
type Block = digest::core_api::Block<Sha512VarCore>;
4+
15
cfg_if::cfg_if! {
26
if #[cfg(feature = "force-soft")] {
37
mod soft;
@@ -24,6 +28,6 @@ cfg_if::cfg_if! {
2428
/// This is a low-level "hazmat" API which provides direct access to the core
2529
/// functionality of SHA-512.
2630
#[cfg_attr(docsrs, doc(cfg(feature = "compress")))]
27-
pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
31+
pub fn compress512(state: &mut [u64; 8], blocks: &[Block]) {
2832
compress(state, blocks)
2933
}

sha2/src/sha512/aarch64.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
use core::arch::{aarch64::*, asm};
44

5+
use super::Block;
56
use crate::consts::K64;
67

78
cpufeatures::new!(sha3_hwcap, "sha3");
89

9-
pub fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
10+
pub fn compress(state: &mut [u64; 8], blocks: &[Block]) {
1011
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
1112
// after stabilization
1213
if sha3_hwcap::get() {
@@ -17,7 +18,7 @@ pub fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
1718
}
1819

1920
#[target_feature(enable = "sha3")]
20-
unsafe fn sha512_compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
21+
unsafe fn sha512_compress(state: &mut [u64; 8], blocks: &[Block]) {
2122
// SAFETY: Requires the sha3 feature.
2223

2324
// Load state into vectors.

sha2/src/sha512/loongarch64_asm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! LoongArch64 assembly backend
22
3+
use super::Block;
4+
35
macro_rules! c {
46
($($l:expr)*) => {
57
concat!($($l ,)*)
@@ -77,7 +79,7 @@ macro_rules! roundtail {
7779
};
7880
}
7981

80-
pub fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
82+
pub fn compress(state: &mut [u64; 8], blocks: &[Block]) {
8183
if blocks.is_empty() {
8284
return;
8385
}

sha2/src/sha512/soft.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![allow(clippy::many_single_char_names)]
22
use crate::consts::K64;
33

4+
use super::Block;
5+
46
/// Not an intrinsic, but works like an unaligned load.
57
fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] {
68
[v1[1], v0[0]]
@@ -208,7 +210,7 @@ pub fn sha512_digest_block_u64(state: &mut [u64; 8], block: &[u64; 16]) {
208210
state[7] = state[7].wrapping_add(h);
209211
}
210212

211-
pub fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
213+
pub fn compress(state: &mut [u64; 8], blocks: &[Block]) {
212214
for block in blocks {
213215
let mut block_u32 = [0u64; 16];
214216
for (o, chunk) in block_u32.iter_mut().zip(block.chunks_exact(8)) {

0 commit comments

Comments
 (0)