Skip to content

Commit 7a1f8db

Browse files
authored
sha2: respect sha2_backend precedence over sha2_256/512_backend (#948)
1 parent c50eeb4 commit 7a1f8db

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎sha2/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## 0.11.2 (UNRELEASED)
99
### Fixed
1010
- SHA-512 `riscv-zknh` backend selection ([#935])
11+
- Respect precedence of the `sha2_backend` configuration flag over `sha2_256/512_backend` ([#948])
1112

1213
### Changed
1314
- Improved performance of the SHA-512 `x86-avx2` backend ([#940], [#943])
1415

1516
[#935]: https://github.com/RustCrypto/hashes/pull/935
1617
[#940]: https://github.com/RustCrypto/hashes/pull/940
1718
[#943]: https://github.com/RustCrypto/hashes/pull/943
19+
[#948]: https://github.com/RustCrypto/hashes/pull/948
1820

1921
## 0.11.1 (2026-10-12)
2022
### Changed

‎sha2/src/sha256.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
cfg_if::cfg_if! {
2-
if #[cfg(any(sha2_backend = "soft", sha2_256_backend = "soft"))] {
2+
if #[cfg(sha2_backend = "soft")] {
33
mod soft;
44
use soft::compress;
5-
} else if #[cfg(any(sha2_backend = "riscv-zknh", sha2_256_backend = "riscv-zknh"))] {
5+
} else if #[cfg(sha2_backend = "riscv-zknh")] {
6+
mod riscv_zknh;
7+
8+
#[cfg(not(target_feature = "zknh"))]
9+
compile_error!("riscv-zknh backend requires `zknh` target feature");
10+
11+
fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
12+
// SAFETY: we checked above that the required target features are enabled
13+
unsafe { riscv_zknh::compress(state, blocks) }
14+
}
15+
} else if #[cfg(sha2_256_backend = "soft")] {
16+
mod soft;
17+
use soft::compress;
18+
} else if #[cfg(sha2_256_backend = "riscv-zknh")] {
619
mod riscv_zknh;
720

821
#[cfg(not(target_feature = "zknh"))]

‎sha2/src/sha512.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
cfg_if::cfg_if! {
2-
if #[cfg(any(sha2_backend = "soft", sha2_512_backend = "soft"))] {
2+
if #[cfg(sha2_backend = "soft")] {
33
mod soft;
44
use soft::compress;
5-
} else if #[cfg(any(sha2_backend = "riscv-zknh", sha2_512_backend = "riscv-zknh"))] {
5+
} else if #[cfg(sha2_backend = "riscv-zknh")] {
6+
mod riscv_zknh;
7+
8+
#[cfg(not(target_feature = "zknh"))]
9+
compile_error!("riscv-zknh backend requires `zknh` target features");
10+
11+
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
12+
// SAFETY: we checked above that the required target features are enabled
13+
unsafe { riscv_zknh::compress(state, blocks) }
14+
}
15+
} else if #[cfg(sha2_512_backend = "soft")] {
16+
mod soft;
17+
use soft::compress;
18+
} else if #[cfg(sha2_512_backend = "riscv-zknh")] {
619
mod riscv_zknh;
720

821
#[cfg(not(target_feature = "zknh"))]

0 commit comments

Comments
 (0)