Skip to content

Commit cf01056

Browse files
authored
sha2: fix unsafe_op_in_unsafe_fn lint, bump MSRV to 1.88 (#926)
1 parent 7c59cad commit cf01056

17 files changed

Lines changed: 278 additions & 217 deletions

File tree

‎.github/workflows/sha2.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
set-msrv:
2727
uses: RustCrypto/actions/.github/workflows/set-msrv.yml@master
2828
with:
29-
msrv: 1.85.0
29+
msrv: 1.88.0
3030

3131
# Builds for no_std platforms
3232
build:

‎.github/workflows/workspace.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- uses: RustCrypto/actions/cargo-cache@master
2323
- uses: dtolnay/rust-toolchain@master
2424
with:
25-
toolchain: 1.85.0
25+
toolchain: 1.99.0
2626
components: clippy
2727
- run: cargo clippy --all -- -D warnings
2828

‎sha2/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Removed workaround for unaligned loads in `riscv-zknh` backend ([#879])
1111
- `riscv-zknh` no longer requires `zbkb` (or `zbb`) target feature ([#879])
12+
- Bump MSRV to 1.88 ([#926])
1213

1314
### Fixed
14-
- Loading of round constants on AArch64 backends to satisfy the Stacked Borrows model ([#922])
15+
- Loading of round constants in AArch64 backends to satisfy the Stacked Borrows model ([#922])
1516

1617
[#879]: https://github.com/RustCrypto/hashes/pull/879
1718
[#922]: https://github.com/RustCrypto/hashes/pull/922
19+
[#926]: https://github.com/RustCrypto/hashes/pull/926
1820

1921
## 0.11.0 (2026-03-25)
2022
### Changed

‎sha2/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "sha2"
33
version = "0.11.0"
44
authors = ["RustCrypto Developers"]
55
edition = "2024"
6-
rust-version = "1.85"
6+
rust-version = "1.88"
77
documentation = "https://docs.rs/sha2"
88
repository = "https://github.com/RustCrypto/hashes"
99
license = "MIT OR Apache-2.0"

‎sha2/src/block_api.rs‎

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ impl VariableOutputCore for Sha256VarCore {
6666
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
6767
buffer.len64_padding_be(bit_len, |b| compress256(&mut self.state, &[b.0]));
6868

69-
for (chunk, v) in out.chunks_exact_mut(4).zip(self.state.iter()) {
70-
chunk.copy_from_slice(&v.to_be_bytes());
69+
let (chunks, tail) = out.as_chunks_mut();
70+
assert!(tail.is_empty());
71+
72+
for (chunk, val) in chunks.iter_mut().zip(self.state.iter()) {
73+
*chunk = val.to_be_bytes();
7174
}
7275
}
7376
}
@@ -104,25 +107,28 @@ impl SerializableState for Sha256VarCore {
104107
type SerializedStateSize = U40;
105108

106109
fn serialize(&self) -> SerializedState<Self> {
107-
let mut serialized_state = SerializedState::<Self>::default();
110+
let mut res = SerializedState::<Self>::default();
111+
let (state_dst, len_dst) = res.split_at_mut(32);
108112

109-
for (val, chunk) in self.state.iter().zip(serialized_state.chunks_exact_mut(4)) {
110-
chunk.copy_from_slice(&val.to_le_bytes());
113+
let (chunks, tail) = state_dst.as_chunks_mut();
114+
assert!(tail.is_empty());
115+
for (val, chunk) in self.state.iter().zip(chunks.iter_mut()) {
116+
*chunk = val.to_le_bytes();
111117
}
112118

113-
serialized_state[32..].copy_from_slice(&self.block_len.to_le_bytes());
114-
serialized_state
119+
len_dst.copy_from_slice(&self.block_len.to_le_bytes());
120+
121+
res
115122
}
116123

117124
fn deserialize(
118125
serialized_state: &SerializedState<Self>,
119126
) -> Result<Self, DeserializeStateError> {
120127
let (serialized_state, serialized_block_len) = serialized_state.split::<U32>();
121128

122-
let mut state = consts::State256::default();
123-
for (val, chunk) in state.iter_mut().zip(serialized_state.chunks_exact(4)) {
124-
*val = u32::from_le_bytes(chunk.try_into().unwrap());
125-
}
129+
let (chunks, tail) = serialized_state.as_chunks();
130+
assert!(tail.is_empty());
131+
let state = core::array::from_fn(|i| u32::from_le_bytes(chunks[i]));
126132

127133
let block_len = u64::from_le_bytes(*serialized_block_len.as_ref());
128134

@@ -185,8 +191,11 @@ impl VariableOutputCore for Sha512VarCore {
185191
let bit_len = 8 * (buffer.get_pos() as u128 + bs * self.block_len);
186192
buffer.len128_padding_be(bit_len, |b| compress512(&mut self.state, &[b.0]));
187193

188-
for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) {
189-
chunk.copy_from_slice(&v.to_be_bytes());
194+
let (chunks, tail) = out.as_chunks_mut();
195+
assert!(tail.is_empty());
196+
197+
for (chunk, val) in chunks.iter_mut().zip(self.state.iter()) {
198+
*chunk = val.to_be_bytes();
190199
}
191200
}
192201
}
@@ -222,26 +231,28 @@ impl SerializableState for Sha512VarCore {
222231
type SerializedStateSize = U80;
223232

224233
fn serialize(&self) -> SerializedState<Self> {
225-
let mut serialized_state = SerializedState::<Self>::default();
234+
let mut res = SerializedState::<Self>::default();
235+
let (state_dst, len_dst) = res.split_at_mut(64);
226236

227-
for (val, chunk) in self.state.iter().zip(serialized_state.chunks_exact_mut(8)) {
228-
chunk.copy_from_slice(&val.to_le_bytes());
237+
let (chunks, tail) = state_dst.as_chunks_mut();
238+
assert!(tail.is_empty());
239+
for (val, chunk) in self.state.iter().zip(chunks.iter_mut()) {
240+
*chunk = val.to_le_bytes();
229241
}
230242

231-
serialized_state[64..].copy_from_slice(&self.block_len.to_le_bytes());
243+
len_dst.copy_from_slice(&self.block_len.to_le_bytes());
232244

233-
serialized_state
245+
res
234246
}
235247

236248
fn deserialize(
237249
serialized_state: &SerializedState<Self>,
238250
) -> Result<Self, DeserializeStateError> {
239251
let (serialized_state, serialized_block_len) = serialized_state.split::<U64>();
240252

241-
let mut state = consts::State512::default();
242-
for (val, chunk) in state.iter_mut().zip(serialized_state.chunks_exact(8)) {
243-
*val = u64::from_le_bytes(chunk.try_into().unwrap());
244-
}
253+
let (chunks, tail) = serialized_state.as_chunks();
254+
assert!(tail.is_empty());
255+
let state = core::array::from_fn(|i| u64::from_le_bytes(chunks[i]));
245256

246257
let block_len = u128::from_le_bytes(*serialized_block_len.as_ref());
247258

‎sha2/src/sha256/aarch64_sha2.rs‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
//! SHA-256 `aarch64` backend.
22
//!
33
//! Implementation adapted from mbedtls.
4-
#![allow(unsafe_op_in_unsafe_fn)]
54
use core::arch::aarch64::*;
65

76
#[cfg(not(target_arch = "aarch64"))]
8-
compile_error!("aarch64-sha2 backend can be used only aarch64 target arches");
7+
compile_error!("aarch64-sha2 backend can be used only on aarch64 target arches");
98

109
#[target_feature(enable = "sha2")]
11-
pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
10+
pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
1211
// Load state into vectors.
13-
let mut abcd = vld1q_u32(state[0..4].as_ptr());
14-
let mut efgh = vld1q_u32(state[4..8].as_ptr());
12+
let [mut abcd, mut efgh] = load_state(state);
1513

1614
// Iterate through the message blocks.
1715
for block in blocks {
@@ -20,10 +18,7 @@ pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
2018
let efgh_orig = efgh;
2119

2220
// Load the message block into vectors, assuming little endianness.
23-
let mut s0 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[0..16].as_ptr())));
24-
let mut s1 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[16..32].as_ptr())));
25-
let mut s2 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[32..48].as_ptr())));
26-
let mut s3 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[48..64].as_ptr())));
21+
let [mut s0, mut s1, mut s2, mut s3] = load_block(block);
2722

2823
// Rounds 0 to 3
2924
let mut tmp = vaddq_u32(s0, rk(0));
@@ -85,13 +80,40 @@ pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
8580
}
8681

8782
// Store vectors into state.
88-
vst1q_u32(state[0..4].as_mut_ptr(), abcd);
89-
vst1q_u32(state[4..8].as_mut_ptr(), efgh);
83+
store_state(state, [abcd, efgh]);
9084
}
9185

86+
#[inline]
9287
#[target_feature(enable = "neon")]
93-
unsafe fn rk(i: usize) -> uint32x4_t {
94-
use crate::consts::K32;
95-
assert!(i + 4 <= K32.len());
96-
vld1q_u32(K32.as_ptr().add(i))
88+
fn rk(i: usize) -> uint32x4_t {
89+
let chunk = &crate::consts::K32[i..][..4];
90+
unsafe { vld1q_u32(chunk.as_ptr()) }
91+
}
92+
93+
#[inline]
94+
#[target_feature(enable = "neon")]
95+
fn load_block(block: &[u8; 64]) -> [uint32x4_t; 4] {
96+
core::array::from_fn(|i| {
97+
let chunk = &block[16 * i..][..16];
98+
let c = unsafe { vld1q_u8(chunk.as_ptr()) };
99+
vreinterpretq_u32_u8(vrev32q_u8(c))
100+
})
101+
}
102+
103+
#[inline]
104+
#[target_feature(enable = "neon")]
105+
fn load_state(state: &[u32; 8]) -> [uint32x4_t; 2] {
106+
core::array::from_fn(|i| {
107+
let chunk = &state[4 * i..][..4];
108+
unsafe { vld1q_u32(chunk.as_ptr()) }
109+
})
110+
}
111+
112+
#[inline]
113+
#[target_feature(enable = "neon")]
114+
fn store_state(state_dst: &mut [u32; 8], [abcd, efgh]: [uint32x4_t; 2]) {
115+
unsafe {
116+
vst1q_u32(state_dst[0..4].as_mut_ptr(), abcd);
117+
vst1q_u32(state_dst[4..8].as_mut_ptr(), efgh);
118+
}
97119
}

‎sha2/src/sha256/riscv_zknh.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ cfg_if::cfg_if! {
1818

1919
#[target_feature(enable = "zknh")]
2020
pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
21-
for block in blocks {
22-
let block: [u32; 16] = core::array::from_fn(|i| {
23-
let chunk = block[4 * i..][..4].try_into().unwrap();
24-
u32::from_be_bytes(chunk)
25-
});
21+
for block in blocks.iter().map(load_block) {
2622
compress_block(state, block);
2723
}
2824
}
25+
26+
fn load_block(block: &[u8; 64]) -> [u32; 16] {
27+
let (chunks, tail) = block.as_chunks();
28+
assert!(tail.is_empty());
29+
core::array::from_fn(|i| u32::from_be_bytes(chunks[i]))
30+
}

‎sha2/src/sha256/riscv_zknh/compact.rs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
22
use crate::consts::K32;
33

4+
#[inline]
45
#[target_feature(enable = "zknh")]
56
pub(super) fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
67
let mut s = *state;
@@ -17,6 +18,7 @@ pub(super) fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
1718
}
1819
}
1920

21+
#[inline]
2022
#[target_feature(enable = "zknh")]
2123
fn schedule(block: &mut [u32; 16], r: usize) {
2224
block[r % 16] = block[r % 16]
@@ -25,18 +27,11 @@ fn schedule(block: &mut [u32; 16], r: usize) {
2527
.wrapping_add(sha256sig0(block[(r + 1) % 16]));
2628
}
2729

30+
#[inline]
2831
#[target_feature(enable = "zknh")]
2932
fn round(state: &mut [u32; 8], block: &[u32; 16], r: usize) {
3033
let n = K32.len() - r;
31-
#[allow(clippy::identity_op)]
32-
let a = (n + 0) % 8;
33-
let b = (n + 1) % 8;
34-
let c = (n + 2) % 8;
35-
let d = (n + 3) % 8;
36-
let e = (n + 4) % 8;
37-
let f = (n + 5) % 8;
38-
let g = (n + 6) % 8;
39-
let h = (n + 7) % 8;
34+
let [a, b, c, d, e, f, g, h] = core::array::from_fn(|i| (n + i) % 8);
4035

4136
state[h] = state[h]
4237
.wrapping_add(sha256sum1(state[e]))

‎sha2/src/sha256/riscv_zknh/unroll.rs‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
22
use crate::consts::K32;
33

4+
#[inline]
45
#[target_feature(enable = "zknh")]
56
pub(super) fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
67
let s = &mut state.clone();
@@ -49,6 +50,7 @@ pub(super) fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
4950
}
5051
}
5152

53+
#[inline]
5254
#[target_feature(enable = "zknh")]
5355
fn round_schedule<const R: usize>(state: &mut [u32; 8], block: &mut [u32; 16], k: &[u32]) {
5456
round::<R>(state, block, k);
@@ -59,18 +61,11 @@ fn round_schedule<const R: usize>(state: &mut [u32; 8], block: &mut [u32; 16], k
5961
.wrapping_add(sha256sig0(block[(R + 1) % 16]));
6062
}
6163

64+
#[inline]
6265
#[target_feature(enable = "zknh")]
6366
fn round<const R: usize>(state: &mut [u32; 8], block: &[u32; 16], k: &[u32]) {
6467
let n = K32.len() - R;
65-
#[allow(clippy::identity_op)]
66-
let a = (n + 0) % 8;
67-
let b = (n + 1) % 8;
68-
let c = (n + 2) % 8;
69-
let d = (n + 3) % 8;
70-
let e = (n + 4) % 8;
71-
let f = (n + 5) % 8;
72-
let g = (n + 6) % 8;
73-
let h = (n + 7) % 8;
68+
let [a, b, c, d, e, f, g, h] = core::array::from_fn(|i| (n + i) % 8);
7469

7570
state[h] = state[h]
7671
.wrapping_add(sha256sum1(state[e]))
@@ -94,6 +89,7 @@ fn maj(x: u32, y: u32, z: u32) -> u32 {
9489
}
9590

9691
/// This function returns `k[R]`, but prevents the compiler from inlining the indexed value
92+
#[inline(always)]
9793
fn opaque_load<const R: usize>(k: &[u32]) -> u32 {
9894
assert!(R < k.len());
9995
let dst;

0 commit comments

Comments
 (0)