Skip to content

Commit

Permalink
sha2: replace soft crate feature with configuration flag (#615)
Browse files Browse the repository at this point in the history
To force use of the software backend users now have to use
`RUSTFLAGS='--cfg sha2_backend="soft"'`.
  • Loading branch information
newpavlov authored Aug 23, 2024
1 parent 03e1d29 commit bca5389
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
34 changes: 24 additions & 10 deletions .github/workflows/sha2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defaults:
working-directory: sha2

env:
RUSTFLAGS: "-Dwarnings"
RUSTFLAGS: -Dwarnings
CARGO_INCREMENTAL: 0

jobs:
Expand Down Expand Up @@ -76,6 +76,9 @@ jobs:
- uses: RustCrypto/actions/cargo-hack-install@master
- run: ${{ matrix.deps }}
- run: cargo hack test --feature-powerset
- run: cargo test --all-features
env:
RUSTFLAGS: -Dwarnings --cfg sha2_backend="soft"

# macOS tests
macos:
Expand All @@ -93,10 +96,12 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
targets: x86_64-apple-darwin
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo test --no-default-features
- run: cargo test
- run: cargo test --all-features
- run: cargo test --all-features
env:
RUSTFLAGS: -Dwarnings --cfg sha2_backend="soft"

# Windows tests
windows:
Expand All @@ -118,6 +123,9 @@ jobs:
targets: ${{ matrix.target }}
- uses: msys2/setup-msys2@v2
- run: cargo test --target ${{ matrix.target }}
- run: cargo test --all-features
env:
RUSTFLAGS: -Dwarnings --cfg sha2_backend="soft"

# Cross-compiled tests
cross:
Expand Down Expand Up @@ -159,12 +167,15 @@ jobs:
with:
toolchain: nightly
- run: cargo install cross --git https://github.com/cross-rs/cross
- run: cross test --package sha2 --target riscv64gc-unknown-linux-gnu
- run: cross test --package sha2 --all-features --target riscv64gc-unknown-linux-gnu
env:
RUSTFLAGS: '-Dwarnings --cfg sha2_backend="riscv-zknh" -C target-feature=+zknh'
- run: cross test --package sha2 --target riscv64gc-unknown-linux-gnu
RUSTFLAGS: -Dwarnings --cfg sha2_backend="soft" -C target-feature=+zknh
- run: cross test --package sha2 --all-features --target riscv64gc-unknown-linux-gnu
env:
RUSTFLAGS: '-Dwarnings --cfg sha2_backend="riscv-zknh-compact" -C target-feature=+zknh'
RUSTFLAGS: -Dwarnings --cfg sha2_backend="riscv-zknh" -C target-feature=+zknh
- run: cross test --package sha2 --all-features --target riscv64gc-unknown-linux-gnu
env:
RUSTFLAGS: -Dwarnings --cfg sha2_backend="riscv-zknh-compact" -C target-feature=+zknh

riscv32-zknh:
runs-on: ubuntu-latest
Expand All @@ -175,12 +186,15 @@ jobs:
with:
toolchain: nightly
components: rust-src
- run: cargo build --target riscv32gc-unknown-linux-gnu -Z build-std
- run: cargo build --all-features --target riscv32gc-unknown-linux-gnu -Z build-std
env:
RUSTFLAGS: -Dwarnings --cfg sha2_backend="soft" -C target-feature=+zknh
- run: cargo build --all-features --target riscv32gc-unknown-linux-gnu -Z build-std
env:
RUSTFLAGS: '-Dwarnings --cfg sha2_backend="riscv-zknh" -C target-feature=+zknh'
- run: cargo build --target riscv32gc-unknown-linux-gnu -Z build-std
RUSTFLAGS: -Dwarnings --cfg sha2_backend="riscv-zknh" -C target-feature=+zknh
- run: cargo build --all-features --target riscv32gc-unknown-linux-gnu -Z build-std
env:
RUSTFLAGS: '-Dwarnings --cfg sha2_backend="riscv-zknh-compact" -C target-feature=+zknh'
RUSTFLAGS: -Dwarnings --cfg sha2_backend="riscv-zknh-compact" -C target-feature=+zknh

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
Expand Down
7 changes: 7 additions & 0 deletions sha2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## UNRELEASED
### Added
- RISC-V scalar crypto extension support gated behind `sha2_backend = "riscv-zknh"` or
`sha2_backend = "riscv-zknh-compact"` configuration flags [#614]

### Removed
- `asm`, `asm-aarch64`, `loongarch64_asm`, and `compress` crate features [#542]
- `soft` crate feature. Replaced with `sha2_backend = "soft"` configuration flag [#615]

[#542]: https://github.com/RustCrypto/hashes/pull/542
[#614]: https://github.com/RustCrypto/hashes/pull/614
[#615]: https://github.com/RustCrypto/hashes/pull/615

## 0.10.8 (2023-09-26)
### Added
Expand Down
7 changes: 4 additions & 3 deletions sha2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ base16ct = { version = "0.2", features = ["alloc"] }
[features]
default = ["oid", "std"]
std = ["digest/std"]
oid = ["digest/oid"] # Enable OID support
zeroize = ["digest/zeroize"]
force-soft = [] # Force software implementation
oid = ["digest/oid"]

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ['cfg(sha2_backend, values("riscv-zknh", "riscv-zknh-compact"))']
check-cfg = [
'cfg(sha2_backend, values("soft", "riscv-zknh", "riscv-zknh-compact"))',
]

[package.metadata.docs.rs]
all-features = true
Expand Down
6 changes: 3 additions & 3 deletions sha2/src/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cfg_if::cfg_if! {
if #[cfg(feature = "force-soft")] {
if #[cfg(sha2_backend = "soft")] {
mod soft;
use soft::compress;
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
mod soft;
mod x86;
use x86::compress;
mod x86_shani;
use x86_shani::compress;
} else if #[cfg(all(
any(target_arch = "riscv32", target_arch = "riscv64"),
sha2_backend = "riscv-zknh"
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions sha2/src/sha512.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cfg_if::cfg_if! {
if #[cfg(feature = "force-soft")] {
if #[cfg(sha2_backend = "soft")] {
mod soft;
use soft::compress;
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
mod soft;
mod x86;
use x86::compress;
mod x86_avx2;
use x86_avx2::compress;
} else if #[cfg(all(
any(target_arch = "riscv32", target_arch = "riscv64"),
sha2_backend = "riscv-zknh"
Expand Down
File renamed without changes.

0 comments on commit bca5389

Please sign in to comment.