Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"d7f9b6989a6dac1a7baf269197066d0ae6b5e1cfab0a3d4a1e40b1cf42b3fe91","CHANGELOG.md":"6e481a517de71d77211cf4904b31a00a84ad07468d8258c32206e77578ac4a70","Cargo.lock":"0274b57bbc00a77a02f953882cab0c0115c545d7ef0f10228125f40bf241d696","Cargo.toml":"bb187fe9b788b66dc91c717ace2a41b5da5518672a302696ab79bf4619d510a9","Cargo.toml.orig":"a89bc798ce45a942b765694d92169e86b551bf03ea69c8c445739ae147c0cd70","LICENSE-APACHE":"a9040321c3712d8fd0b09cf52b17445de04a23a10165049ae187cd39e5c86be5","LICENSE-MIT":"831e0f43ad0bf014c1c4fec5767aae470434c1d66d6e671be2d823e729063e25","README.md":"21a915a08eb1bf6d439ce9bdf38eec06916fd62ce2710c54757626be20990022","benches/mod.rs":"c32d9f91a541821ea988c14eee710963e623ef1edf69b02b41a29bc44e04ba95","src/block_api.rs":"7d858102344cc8be2d22b207296c44087b91b2e7309c5598497c48c1182c89cc","src/consts.rs":"f7093130534180198be37aa7f4b3274e73f85ec09cad648df19d782841c1ee46","src/lib.rs":"5529df80c102cb5d7240e47a5239565c8347a4dd79ee5a085e5f2d749657d49c","src/sha256.rs":"cb03fddc1325b60935409f097f2fab3ae51cfab15bed1254f4e93d13b28b7eff","src/sha256/aarch64_sha2.rs":"d9245fa28ba2a5cac5c2ba3015f422b7eca8e604aa21c9bece93766127c22451","src/sha256/loongarch64_asm.rs":"eda57073e3dbcadca88e908dd261a2782b2bb89c06f1b97468a6ffd1cad407ff","src/sha256/riscv_zknh.rs":"5e34b50064e37c01c972c1d31324b2d92c0db9bad1f5d1f9c9097f752d15207a","src/sha256/riscv_zknh/compact.rs":"037d254980e6f30997e589536105c4672a66394b6c15b63bb3a3dd0320c9cec1","src/sha256/riscv_zknh/unroll.rs":"cdea73a1cfff029b1922370d1b8e3b3894ea6856e8f2f1b84d3c7d18e52fa036","src/sha256/riscv_zknh/utils.rs":"9970c93e9e9c180f836a0fc18d14b51c411e181b77b1002b5ec486da4492d630","src/sha256/soft.rs":"60449b6df17909cd34d533ec4d82851841cb8b26e01c0907827b33343917a31c","src/sha256/soft/compact.rs":"0f729259d0318e4c1c8055c0103da16bd360c98c11ca614b5bbe860fcda3ccff","src/sha256/soft/unroll.rs":"69af1d7a57c05b3a69fda30fb0039bc7c8b53e5d9a679bf9973eb6eda245b81a","src/sha256/wasm32_simd128.rs":"006a8667660283d340d44c91b7e2cc1bee866ef0fe13b58f5b65535542942acd","src/sha256/x86_sha.rs":"19d8c7c15ffa081332e5b438e98809ce52f14706553ec8965d6c54c5fbf7cccf","src/sha512.rs":"942a7d6180507d68a3feb9d953410a884314b8022d989a854838c5426a8ea1a0","src/sha512/aarch64_sha3.rs":"7ccb33877955e8bb200adfe1b32a036d63e23b0bfcec377bd62841f8f2ce494e","src/sha512/loongarch64_asm.rs":"e66005dc654c69248834e05377eb3580d6a689960edcbd276ca08af7d3721354","src/sha512/riscv_zknh.rs":"d48940806d6c6b4e5ec6bce09a0e0185e7a681aacf507d89fd44f9b2afdb8cf9","src/sha512/riscv_zknh/compact.rs":"3217c41aee0233d1841d6acd80b1130b6f6de9681c737cce6031607f9233f916","src/sha512/riscv_zknh/unroll.rs":"5c7246f8f6301ea6aa596d4003782b6a6a91fe118302b32fdfb1f16e1ab708ab","src/sha512/riscv_zknh/utils.rs":"7e34eb03b68a38e2de461086b3c6220a2a0bcece7b9b037aa697dd23dc91ead3","src/sha512/soft.rs":"749b892d9cf9d5757adf1f6ec77840a899f47d1a62687d385230288858d86d8b","src/sha512/soft/compact.rs":"6cd2209231ae99f5f166d3410ef1c7dae60aa2459385e8ad5f1bc4f607499c2c","src/sha512/soft/unroll.rs":"4a68218d52b722bb4e11c6d70db9e5256c930bd41b23905a0d69cd893f1e3c6d","src/sha512/wasm32_simd128.rs":"f90ec0fa19ce76c2de5461d748e2ed73565b563ce00b3b17e8eabbd6df012f92","src/sha512/x86_avx2.rs":"b67aa758f16086b47bdac0b79614773dbaa66946842c9883f794e6a13e51af67","tests/data/sha224_kat.blb":"0404ce7dbf64488c74237f06c0925468f75539aee9d647461edf3324c7411833","tests/data/sha224_serialization.bin":"6b7164859b4c83edda87ca1bab9b07afb6ba5eae77cc242b04be6ca7f122abba","tests/data/sha256_kat.blb":"f947b01761a4cd1324d5021852bcec19d07f193ea034b43bf34b08f1fb1bbf2f","tests/data/sha256_serialization.bin":"6a44d0e8c9d089de8df7f7e848a016127bb2b85608c73e19a0f898c77a283f19","tests/data/sha384_kat.blb":"2fafeb11ed81abf78a33b3e098269b0add361e8855e394293a5d7e5e57fddab9","tests/data/sha384_serialization.bin":"299df90bb78d579d7d303985b1bd9e1a9dd205d48b888d87350dc887511886b4","tests/data/sha512_224_kat.blb":"3c3c0c408eda6d2d264d50bd1fa5c012f1c5dc2f5bf625ca471fcccebd476870","tests/data/sha512_224_serialization.bin":"0be3e7d9cf7821a4e031e2c5b1609006b15995a4553691f9be311f4a87bd5f70","tests/data/sha512_256_kat.blb":"10dcef0cb0a381d2e4e336e88cb23b38859d3a5c894a3658aaef37a83c058cfb","tests/data/sha512_256_serialization.bin":"fc6735a14cbd66dcd9111021a26fa70373c5dd245272fd4874268a6622f7d97f","tests/data/sha512_kat.blb":"4963aa09edeb0c39efa94e397551a2b0018f4a3bd5f343e21010a264db8cc2e9","tests/data/sha512_serialization.bin":"d4ad28878dce9ac24ee8cc27622fc758b9db070406cad37eb89518f33f012604","tests/mod.rs":"d417a8068dde4a83a1ab8ede2ebacfb38329f67118dd3327b33e73960c4ace79"},"package":"446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4"}
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "ffe093984c004769747e998f77da8ff7c0e7a765"
},
"path_in_vcs": "sha2"
}
+216
View File
@@ -0,0 +1,216 @@
# Changelog
All notable changes to this project will be documented in this file.
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).
## 0.11.0 (2026-03-25)
### Changed
- Edition changed to 2024 and MSRV bumped to 1.85 ([#652])
- Relax MSRV policy and allow MSRV bumps in patch releases
- Update to `digest` v0.11
- Replace type aliases with newtypes ([#678])
- `compress256` and `compress512` are moved to the `block_api` module ([#678])
- Implementation of the `SerializableState` trait ([#716])
- Configuration flags for backend selection ([#614], [#615], [#686], [#802])
### Added
- `alloc` crate feature ([#678])
- RISC-V scalar crypto extension support ([#614])
### Removed
- `asm`, `asm-aarch64`, `loongarch64_asm`, and `compress` crate features ([#542])
- `soft` crate feature ([#615])
- `force-soft-compact` crate feature ([#686])
- `std` crate feature ([#678])
[#542]: https://github.com/RustCrypto/hashes/pull/542
[#614]: https://github.com/RustCrypto/hashes/pull/614
[#615]: https://github.com/RustCrypto/hashes/pull/615
[#652]: https://github.com/RustCrypto/hashes/pull/652
[#678]: https://github.com/RustCrypto/hashes/pull/678
[#686]: https://github.com/RustCrypto/hashes/pull/686
[#716]: https://github.com/RustCrypto/hashes/pull/716
[#802]: https://github.com/RustCrypto/hashes/pull/802
## 0.10.9 (2025-04-30)
### Added
- `force-soft-compact` crate feature to enable compact software backend (backport of [#686]) [#687]
[#687]: https://github.com/RustCrypto/hashes/pull/687
## 0.10.8 (2023-09-26)
### Added
- `asm!`-based backend for LoongArch64 targets gated behind `loongarch64_asm` feature [#507]
[#507]: https://github.com/RustCrypto/hashes/pull/507
## 0.10.7 (2023-06-15)
### Added
- AArch64 Neon-based backend ([#490])
[#490]: https://github.com/RustCrypto/hashes/pull/490
## 0.10.6 (2022-09-16)
### Added
- Feature-gated OID support ([#405])
[#405]: https://github.com/RustCrypto/hashes/pull/405
## 0.10.5 (2022-09-02)
### Fixed
- MSRV issue which was not resolved by v0.10.4 ([#401])
[#401]: https://github.com/RustCrypto/hashes/pull/401
## 0.10.4 (2022-09-02)
### Fixed
- MSRV issue caused by publishing v0.10.3 using a buggy Nightly toolchain ([#399])
[#399]: https://github.com/RustCrypto/hashes/pull/399
## 0.10.3 (2022-08-30)
### Changed
- Ignore `asm` feature on unsupported targets ([#388])
[#388]: https://github.com/RustCrypto/hashes/pull/388
## 0.10.2 (2022-02-17)
### Fixed
- Minimal versions build ([#363])
[#363]: https://github.com/RustCrypto/hashes/pull/363
## 0.10.1 (2022-01-06)
### Fixed
- Bug in the AVX2 backend ([#345])
## 0.10.0 (2021-12-07) [YANKED]
### Changed
- Update to `digest` v0.10 ([#217])
- Rename `Sha512Trunc224` and `Sha512Trunc256` to `Sha512_224` and `Sha512_256` respectively. ([#217])
[#217]: https://github.com/RustCrypto/hashes/pull/217
## 0.9.9 (2022-01-06)
### Fixed
- Backport [#345] bug fix for the AVX2 backend ([#346])
[#345]: https://github.com/RustCrypto/hashes/pull/345
[#346]: https://github.com/RustCrypto/hashes/pull/346
## 0.9.8 (2021-09-09) [YANKED]
### Fixed
- Bug in the AVX2 backend ([#314])
[#314]: https://github.com/RustCrypto/hashes/pull/314
## 0.9.7 (2021-09-08) [YANKED]
### Added
- x86 intrinsics support for SHA-512 ([#312])
[#312]: https://github.com/RustCrypto/hashes/pull/312
## 0.9.6 (2021-08-27)
### Changed
- Bump `cpufeatures` dependency to 0.2 ([#306])
[#306]: https://github.com/RustCrypto/hashes/pull/306
## 0.9.5 (2021-05-11)
### Changed
- Use `cpufeatures` to detect intrinsics support on `aarch64` targets ([#267])
[#267]: https://github.com/RustCrypto/hashes/pull/267
## 0.9.4 (2021-05-05)
### Added
- Hardware accelerated SHA-256 for Apple M1 CPUs with `asm` feature ([#262])
### Changed
- Bump `sha2-asm` to v0.6.1 release ([#262])
- Switch from `cpuid-bool` to `cpufeatures` ([#263])
[#262]: https://github.com/RustCrypto/hashes/pull/262
[#263]: https://github.com/RustCrypto/hashes/pull/263
## 0.9.3 (2021-01-30)
### Changed
- Use the SHA-NI extension backend with enabled `asm` feature. ([#224])
[#224]: https://github.com/RustCrypto/hashes/pull/224
## 0.9.2 (2020-11-04)
### Added
- `force-soft` feature to enforce use of software implementation. ([#203])
### Changed
- `cfg-if` dependency updated to v1.0. ([#197])
[#197]: https://github.com/RustCrypto/hashes/pull/197
[#203]: https://github.com/RustCrypto/hashes/pull/203
## 0.9.1 (2020-06-24)
### Added
- x86 hardware acceleration of SHA-256 via SHA extension intrinsics. ([#167])
[#167]: https://github.com/RustCrypto/hashes/pull/167
## 0.9.0 (2020-06-09)
### Changed
- Update to `digest` v0.9 release; MSRV 1.41+ ([#155])
- Use new `*Dirty` traits from the `digest` crate ([#153])
- Bump `block-buffer` to v0.8 release ([#151])
- Rename `*result*` to `finalize` ([#148])
- Upgrade to Rust 2018 edition ([#133])
[#155]: https://github.com/RustCrypto/hashes/pull/155
[#153]: https://github.com/RustCrypto/hashes/pull/153
[#151]: https://github.com/RustCrypto/hashes/pull/151
[#148]: https://github.com/RustCrypto/hashes/pull/148
[#133]: https://github.com/RustCrypto/hashes/pull/133
## 0.8.2 (2020-05-23)
### Added
- Expose compression function under the `compress` feature flag ([#108])
### Changed
- Use `libc` crate for `aarch64` consts ([#109])
- Minor code cleanups ([#94])
[#109]: https://github.com/RustCrypto/hashes/pull/109
[#108]: https://github.com/RustCrypto/hashes/pull/108
[#94]: https://github.com/RustCrypto/hashes/pull/94
## 0.8.1 (2020-01-05)
## 0.8.0 (2018-10-02)
## 0.7.1 (2018-04-27)
## 0.6.0 (2017-06-12)
## 0.5.3 (2017-06-03)
## 0.5.2 (2017-05-08)
## 0.5.1 (2017-05-01)
## 0.5.0 (2017-04-06)
## 0.4.2 (2017-01-23)
## 0.4.1 (2017-01-20)
## 0.4.0 (2016-12-24)
## 0.3.0 (2016-11-17)
## 0.2.0 (2016-10-26)
## 0.1.2 (2016-05-06)
## 0.1.1 (2016-05-06)
## 0.1.0 (2016-05-06)
+105
View File
@@ -0,0 +1,105 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "blobby"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89af0b093cc13baa4e51e64e65ec2422f7e73aea0e612e5ad3872986671622f1"
[[package]]
name = "block-buffer"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdd35008169921d80bc60d3d0ab416eecb028c4cd653352907921d95084790be"
dependencies = [
"hybrid-array",
"zeroize",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "const-oid"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c"
[[package]]
name = "cpufeatures"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77727bb15fa921304124b128af125e7e3b968275d1b108b379190264f4423710"
dependencies = [
"hybrid-array",
]
[[package]]
name = "digest"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4850db49bf08e663084f7fb5c87d202ef91a3907271aff24a94eb97ff039153c"
dependencies = [
"blobby",
"block-buffer",
"const-oid",
"crypto-common",
"zeroize",
]
[[package]]
name = "hex-literal"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e712f64ec3850b98572bffac52e2c6f282b29fe6c5fa6d42334b30be438d95c1"
[[package]]
name = "hybrid-array"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8655f91cd07f2b9d0c24137bd650fe69617773435ee5ec83022377777ce65ef1"
dependencies = [
"typenum",
]
[[package]]
name = "libc"
version = "0.2.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
[[package]]
name = "sha2"
version = "0.11.0"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
"hex-literal",
]
[[package]]
name = "typenum"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
[[package]]
name = "zeroize"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
+91
View File
@@ -0,0 +1,91 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2024"
rust-version = "1.85"
name = "sha2"
version = "0.11.0"
authors = ["RustCrypto Developers"]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = """
Pure Rust implementation of the SHA-2 hash function family
including SHA-224, SHA-256, SHA-384, and SHA-512.
"""
documentation = "https://docs.rs/sha2"
readme = "README.md"
keywords = [
"sha2",
"hash",
"digest",
]
categories = [
"cryptography",
"no-std",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/RustCrypto/hashes"
[package.metadata.docs.rs]
all-features = true
[features]
alloc = ["digest/alloc"]
default = [
"alloc",
"oid",
]
oid = ["digest/oid"]
zeroize = ["digest/zeroize"]
[lib]
name = "sha2"
path = "src/lib.rs"
[[test]]
name = "mod"
path = "tests/mod.rs"
[[bench]]
name = "mod"
path = "benches/mod.rs"
[dependencies.cfg-if]
version = "1"
[dependencies.digest]
version = "0.11"
[dev-dependencies.digest]
version = "0.11"
features = ["dev"]
[dev-dependencies.hex-literal]
version = "1"
[target.'cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))'.dependencies.cpufeatures]
version = "0.3"
[lints.rust.unexpected_cfgs]
level = "warn"
priority = 0
check-cfg = [
'cfg(sha2_backend, values("soft", "riscv-zknh"))',
'cfg(sha2_256_backend, values("aarch64-sha2", "soft", "riscv-zknh", "x86-sha"))',
'cfg(sha2_512_backend, values("aarch64-sha3", "soft", "riscv-zknh", "x86-avx2"))',
'cfg(sha2_backend_soft, values("compact"))',
'cfg(sha2_backend_riscv_zknh, values("compact"))',
]
+46
View File
@@ -0,0 +1,46 @@
[package]
name = "sha2"
version = "0.11.0"
authors = ["RustCrypto Developers"]
edition = "2024"
rust-version = "1.85"
documentation = "https://docs.rs/sha2"
readme = "README.md"
repository = "https://github.com/RustCrypto/hashes"
license = "MIT OR Apache-2.0"
keywords = ["sha2", "hash", "digest"]
categories = ["cryptography", "no-std"]
description = """
Pure Rust implementation of the SHA-2 hash function family
including SHA-224, SHA-256, SHA-384, and SHA-512.
"""
[dependencies]
digest = "0.11"
cfg-if = "1"
[target.'cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))'.dependencies]
cpufeatures = "0.3"
[dev-dependencies]
digest = { version = "0.11", features = ["dev"] }
hex-literal = "1"
[features]
default = ["alloc", "oid"]
alloc = ["digest/alloc"]
zeroize = ["digest/zeroize"]
oid = ["digest/oid"]
[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(sha2_backend, values("soft", "riscv-zknh"))',
'cfg(sha2_256_backend, values("aarch64-sha2", "soft", "riscv-zknh", "x86-sha"))',
'cfg(sha2_512_backend, values("aarch64-sha3", "soft", "riscv-zknh", "x86-avx2"))',
'cfg(sha2_backend_soft, values("compact"))',
'cfg(sha2_backend_riscv_zknh, values("compact"))',
]
[package.metadata.docs.rs]
all-features = true
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+28
View File
@@ -0,0 +1,28 @@
Copyright (c) 2016-2026 The RustCrypto Project Developers
Copyright (c) 2016 Artyom Pavlov
Copyright (c) 2009-2013 Mozilla Foundation
Copyright (c) 2006-2009 Graydon Hoare
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
+137
View File
@@ -0,0 +1,137 @@
# RustCrypto: SHA-2
[![crate][crate-image]][crate-link]
[![Docs][docs-image]][docs-link]
![Apache2/MIT licensed][license-image]
![Rust Version][rustc-image]
[![Project Chat][chat-image]][chat-link]
[![Build Status][build-image]][build-link]
Pure Rust implementation of the [SHA-2] cryptographic hash algorithms.
There are 6 standard algorithms specified in the SHA-2 standard:
`Sha224`, `Sha256`, `Sha512_224`, `Sha512_256`, `Sha384`, and `Sha512`.
Algorithmically, there are only 2 core algorithms: SHA-256 and SHA-512.
All other algorithms are just applications of these with different initial
hash values, and truncated to different digest bit lengths. The first two
algorithms in the list are based on SHA-256, while the last four are based
on SHA-512.
## Examples
### One-shot API
```rust
use sha2::{Sha256, Digest};
use hex_literal::hex;
let hash = Sha256::digest(b"hello world");
assert_eq!(hash, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"));
```
### Incremental API
```rust
use sha2::{Sha256, Sha512, Digest};
use hex_literal::hex;
let mut hasher = Sha256::new();
hasher.update(b"hello ");
hasher.update(b"world");
let hash256 = hasher.finalize();
assert_eq!(hash256, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"));
let mut hasher = Sha512::new();
hasher.update(b"hello world");
let hash512 = hasher.finalize();
assert_eq!(hash512, hex!(
"309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f"
"989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"
));
```
See the [`digest`] crate docs for additional examples.
## Backends
This crate supports a number of different backends.
SHA-256 and SHA-512 backends:
- `soft`: portable software implementation
- `loongarch64-asm`: `asm!`-based implementation for LoongArch64 targets
- `riscv-zknh`: uses the RISC-V `Zknh` scalar crypto extension. Experimental,
requires Nightly compiler and to enable `Zknh` and `Zbkb` (or `Zbb`)
target features at compile time.
- `wasm32-simd128`: uses the WASM `simd128` extension.
SHA-256 only backends:
- `aarch64-sha2`: uses the AArch64 `sha2` extension.
- `x86-sha`: uses the x86 SHA-NI extension.
SHA-512 only backends:
- `aarch64-sha3`: uses the AArch64 `sha3` extension.
- `x86-avx2`: uses the x86 AVX2 extension.
By default the following backends are used:
- `target_arch = "aarch64"`: use `aarch64-sha2` and `aarch64-sha3` when the required
target features are detected at runtime; otherwise fall back to `soft`.
- `any(target_arch = "x86", target_arch = "x86_64")`: use `x86-sha` and `x86-avx` when
the required target features are detected at runtime; otherwise fall back to `soft`.
- `target_arch = "loongarch64"`: use `loongarch64-asm`.
- `all(target_arch = "wasm32", target_feature = "simd128")`: use `wasm32-simd128`.
- All other targets: use `soft`.
You can force backend selection using the following configuration flags:
- `sha2_backend`: select SHA-256 and SHA-512 backends. Supported values: `soft`, `riscv-zknh`.
- `sha2_256_backend`: select SHA-256 backend. Supported values: `aarch64-sha2`, `soft`,
`riscv-zknh`, `x86-sha`.
- `sha2_512_backend`: select SHA-512 backend. Supported values: `aarch64-sha3`, `soft`,
`riscv-zknh`, `x86-avx2`.
They can be enabled using either the `RUSTFLAGS` environment variable
(e.g. `RUSTFLAGS='--cfg sha2_backend="soft"'`), or by modifying your `.cargo/config.toml` file.
Note that `sha2_backend` has higher priority than `sha2_256_backend` and `sha2_512_backend`.
In other words, using `--cfg sha2_backend="soft" --cfg sha2_256_backend="x86_sha"` will result
in selection of the software backend for SHA-256.
By default `soft` and `riscv-zknh` backends unroll round loops, which results in a better
performance at the cost of a bigger resulting binary. You can disable unrolling in the backends
by using `sha2_backend_soft = "compact"` and `sha2_backend_riscv_zknh = "compact"` configuration
flags respectively.
## License
The crate is licensed under either of:
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
* [MIT license](http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
[//]: # (badges)
[crate-image]: https://img.shields.io/crates/v/sha2.svg
[crate-link]: https://crates.io/crates/sha2
[docs-image]: https://docs.rs/sha2/badge.svg
[docs-link]: https://docs.rs/sha2/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260041-hashes
[build-image]: https://github.com/RustCrypto/hashes/actions/workflows/sha2.yml/badge.svg?branch=master
[build-link]: https://github.com/RustCrypto/hashes/actions/workflows/sha2.yml?query=branch:master
[//]: # (general links)
[SHA-2]: https://en.wikipedia.org/wiki/SHA-2
[`digest`]: https://docs.rs/digest
+22
View File
@@ -0,0 +1,22 @@
#![feature(test)]
extern crate test;
use digest::bench_update;
use sha2::{Sha256, Sha512};
use test::Bencher;
bench_update!(
Sha256::default();
sha256_10 10;
sha256_100 100;
sha256_1000 1000;
sha256_10000 10000;
);
bench_update!(
Sha512::default();
sha512_10 10;
sha512_100 100;
sha512_1000 1000;
sha512_10000 10000;
);
+250
View File
@@ -0,0 +1,250 @@
use crate::consts;
use core::fmt;
use digest::{
HashMarker, InvalidOutputSize, Output,
array::Array,
block_api::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, OutputSizeUser,
TruncSide, UpdateCore, VariableOutputCore,
},
common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
typenum::{U32, U40, U64, U80, U128, Unsigned},
};
pub use crate::{sha256::compress256, sha512::compress512};
/// Core block-level SHA-256 hasher with variable output size.
///
/// Supports initialization only for 28 and 32 byte output sizes,
/// i.e. 224 and 256 bits respectively.
#[derive(Clone)]
pub struct Sha256VarCore {
state: consts::State256,
block_len: u64,
}
impl HashMarker for Sha256VarCore {}
impl BlockSizeUser for Sha256VarCore {
type BlockSize = U64;
}
impl BufferKindUser for Sha256VarCore {
type BufferKind = Eager;
}
impl UpdateCore for Sha256VarCore {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len += blocks.len() as u64;
let blocks = Array::cast_slice_to_core(blocks);
compress256(&mut self.state, blocks);
}
}
impl OutputSizeUser for Sha256VarCore {
type OutputSize = U32;
}
impl VariableOutputCore for Sha256VarCore {
const TRUNC_SIDE: TruncSide = TruncSide::Left;
#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
let state = match output_size {
28 => consts::H256_224,
32 => consts::H256_256,
_ => return Err(InvalidOutputSize),
};
let block_len = 0;
Ok(Self { state, block_len })
}
#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bs = Self::BlockSize::U64;
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
buffer.len64_padding_be(bit_len, |b| compress256(&mut self.state, &[b.0]));
for (chunk, v) in out.chunks_exact_mut(4).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
}
}
impl AlgorithmName for Sha256VarCore {
#[inline]
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Sha256")
}
}
impl fmt::Debug for Sha256VarCore {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Sha256VarCore { ... }")
}
}
impl Drop for Sha256VarCore {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
use digest::zeroize::Zeroize;
self.state.zeroize();
self.block_len.zeroize();
}
}
}
#[cfg(feature = "zeroize")]
impl digest::zeroize::ZeroizeOnDrop for Sha256VarCore {}
impl SerializableState for Sha256VarCore {
type SerializedStateSize = U40;
fn serialize(&self) -> SerializedState<Self> {
let mut serialized_state = SerializedState::<Self>::default();
for (val, chunk) in self.state.iter().zip(serialized_state.chunks_exact_mut(4)) {
chunk.copy_from_slice(&val.to_le_bytes());
}
serialized_state[32..].copy_from_slice(&self.block_len.to_le_bytes());
serialized_state
}
fn deserialize(
serialized_state: &SerializedState<Self>,
) -> Result<Self, DeserializeStateError> {
let (serialized_state, serialized_block_len) = serialized_state.split::<U32>();
let mut state = consts::State256::default();
for (val, chunk) in state.iter_mut().zip(serialized_state.chunks_exact(4)) {
*val = u32::from_le_bytes(chunk.try_into().unwrap());
}
let block_len = u64::from_le_bytes(*serialized_block_len.as_ref());
Ok(Self { state, block_len })
}
}
/// Core block-level SHA-512 hasher with variable output size.
///
/// Supports initialization only for 28, 32, 48, and 64 byte output sizes,
/// i.e. 224, 256, 384, and 512 bits respectively.
#[derive(Clone)]
pub struct Sha512VarCore {
state: consts::State512,
block_len: u128,
}
impl HashMarker for Sha512VarCore {}
impl BlockSizeUser for Sha512VarCore {
type BlockSize = U128;
}
impl BufferKindUser for Sha512VarCore {
type BufferKind = Eager;
}
impl UpdateCore for Sha512VarCore {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len += blocks.len() as u128;
let blocks = Array::cast_slice_to_core(blocks);
compress512(&mut self.state, blocks);
}
}
impl OutputSizeUser for Sha512VarCore {
type OutputSize = U64;
}
impl VariableOutputCore for Sha512VarCore {
const TRUNC_SIDE: TruncSide = TruncSide::Left;
#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
let state = match output_size {
28 => consts::H512_224,
32 => consts::H512_256,
48 => consts::H512_384,
64 => consts::H512_512,
_ => return Err(InvalidOutputSize),
};
let block_len = 0;
Ok(Self { state, block_len })
}
#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bs = Self::BlockSize::U64 as u128;
let bit_len = 8 * (buffer.get_pos() as u128 + bs * self.block_len);
buffer.len128_padding_be(bit_len, |b| compress512(&mut self.state, &[b.0]));
for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
}
}
impl AlgorithmName for Sha512VarCore {
#[inline]
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Sha512")
}
}
impl fmt::Debug for Sha512VarCore {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Sha512VarCore { ... }")
}
}
impl Drop for Sha512VarCore {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
use digest::zeroize::Zeroize;
self.state.zeroize();
self.block_len.zeroize();
}
}
}
#[cfg(feature = "zeroize")]
impl digest::zeroize::ZeroizeOnDrop for Sha512VarCore {}
impl SerializableState for Sha512VarCore {
type SerializedStateSize = U80;
fn serialize(&self) -> SerializedState<Self> {
let mut serialized_state = SerializedState::<Self>::default();
for (val, chunk) in self.state.iter().zip(serialized_state.chunks_exact_mut(8)) {
chunk.copy_from_slice(&val.to_le_bytes());
}
serialized_state[64..].copy_from_slice(&self.block_len.to_le_bytes());
serialized_state
}
fn deserialize(
serialized_state: &SerializedState<Self>,
) -> Result<Self, DeserializeStateError> {
let (serialized_state, serialized_block_len) = serialized_state.split::<U64>();
let mut state = consts::State512::default();
for (val, chunk) in state.iter_mut().zip(serialized_state.chunks_exact(8)) {
*val = u64::from_le_bytes(chunk.try_into().unwrap());
}
let block_len = u128::from_le_bytes(*serialized_block_len.as_ref());
Ok(Self { state, block_len })
}
}
+92
View File
@@ -0,0 +1,92 @@
#![allow(dead_code)]
pub(crate) type State256 = [u32; 8];
pub(crate) type State512 = [u64; 8];
pub(crate) const H256_224: State256 = [
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
];
pub(crate) const H256_256: State256 = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
];
pub(crate) const H512_224: State512 = [
0x8c3d37c819544da2, 0x73e1996689dcd4d6, 0x1dfab7ae32ff9c82, 0x679dd514582f9fcf,
0x0f6d2b697bd44da8, 0x77e36f7304c48942, 0x3f9d85a86a1d36c8, 0x1112e6ad91d692a1,
];
pub(crate) const H512_256: State512 = [
0x22312194fc2bf72c, 0x9f555fa3c84c64c2, 0x2393b86b6f53b151, 0x963877195940eabd,
0x96283ee2a88effe3, 0xbe5e1e2553863992, 0x2b0199fc2c85b8aa, 0x0eb72ddc81c52ca2,
];
pub(crate) const H512_384: State512 = [
0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4,
];
pub(crate) const H512_512: State512 = [
0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
];
/// Round constants for SHA-256 family of digests
pub(crate) static K32: [u32; 64] = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];
/// Round constants for SHA-512 family of digests
pub(crate) const K64: [u64; 80] = [
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
];
/// Swapped round constants for SHA-256 family of digests
pub(crate) static K32X4: [[u32; 4]; 16] = {
let mut res = [[0u32; 4]; 16];
let mut i = 0;
while i < 16 {
res[i] = [K32[4 * i + 3], K32[4 * i + 2], K32[4 * i + 1], K32[4 * i]];
i += 1;
}
res
};
/// Swapped round constants for SHA-512 family of digests
pub(crate) const K64X2: [[u64; 2]; 40] = {
let mut res = [[0u64; 2]; 40];
let mut i = 0;
while i < 16 {
res[i] = [K64[4 * i + 1], K64[4 * i]];
i += 1;
}
res
};
+69
View File
@@ -0,0 +1,69 @@
#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, unreachable_pub)]
#![cfg_attr(
any(
sha2_backend = "riscv-zknh",
sha2_256_backend = "riscv-zknh",
sha2_256_backend = "riscv-zknh",
),
feature(riscv_ext_intrinsics)
)]
#![allow(clippy::needless_range_loop)]
pub use digest::{self, Digest};
use digest::{
block_api::CtOutWrapper,
consts::{U28, U32, U48, U64},
};
/// Block-level types
pub mod block_api;
#[rustfmt::skip]
mod consts;
mod sha256;
mod sha512;
digest::buffer_fixed!(
/// SHA-256 hasher.
pub struct Sha256(CtOutWrapper<block_api::Sha256VarCore, U32>);
oid: "2.16.840.1.101.3.4.2.1";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// SHA-384 hasher.
pub struct Sha384(CtOutWrapper<block_api::Sha512VarCore, U48>);
oid: "2.16.840.1.101.3.4.2.2";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// SHA-512 hasher.
pub struct Sha512(CtOutWrapper<block_api::Sha512VarCore, U64>);
oid: "2.16.840.1.101.3.4.2.3";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// SHA-224 hasher.
pub struct Sha224(CtOutWrapper<block_api::Sha256VarCore, U28>);
oid: "2.16.840.1.101.3.4.2.4";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// SHA-512/224 hasher.
pub struct Sha512_224(CtOutWrapper<block_api::Sha512VarCore, U28>);
oid: "2.16.840.1.101.3.4.2.5";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// SHA-512/256 hasher.
pub struct Sha512_256(CtOutWrapper<block_api::Sha512VarCore, U32>);
oid: "2.16.840.1.101.3.4.2.6";
impl: FixedHashTraits;
);
+88
View File
@@ -0,0 +1,88 @@
cfg_if::cfg_if! {
if #[cfg(any(sha2_backend = "soft", sha2_256_backend = "soft"))] {
mod soft;
use soft::compress;
} else if #[cfg(any(sha2_backend = "riscv-zknh", sha2_256_backend = "riscv-zknh"))] {
mod riscv_zknh;
#[cfg(not(all(
target_feature = "zknh",
any(target_feature = "zbb", target_feature = "zbkb")
)))]
compile_error!("riscv-zknh backend requires zknh and zbkb (or zbb) target features");
fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
// SAFETY: we checked above that the required target features are enabled
unsafe { riscv_zknh::compress(state, blocks) }
}
} else if #[cfg(sha2_256_backend = "x86-sha")] {
mod x86_sha;
#[cfg(not(all(
target_feature = "sha",
target_feature = "sse2",
target_feature = "ssse3",
target_feature = "sse4.1",
)))]
compile_error!("x86-sha backend requires sha, sse2, ssse3, sse4.1 target features");
fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
// SAFETY: we checked above that the required target features are enabled
unsafe { x86_sha::compress(state, blocks) }
}
} else if #[cfg(sha2_256_backend = "aarch64-sha2")] {
mod aarch64_sha2;
#[cfg(not(target_feature = "sha2"))]
compile_error!("aarch64-sha2 backend requires sha2 target feature");
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
// SAFETY: we checked above that the required target features are enabled
unsafe { aarch64_sha2::compress(state, blocks) }
}
} else if #[cfg(target_arch = "loongarch64")] {
mod loongarch64_asm;
use loongarch64_asm::compress;
} else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
mod wasm32_simd128;
use wasm32_simd128::compress;
} else {
mod soft;
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
mod x86_sha;
cpufeatures::new!(shani_cpuid, "sha", "sse2", "ssse3", "sse4.1");
} else if #[cfg(target_arch = "aarch64")] {
mod aarch64_sha2;
cpufeatures::new!(sha2_hwcap, "sha2");
}
}
fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if shani_cpuid::get() {
// SAFETY: we checked that required target features are available
return unsafe { x86_sha::compress(state, blocks) };
}
} else if #[cfg(target_arch = "aarch64")] {
if sha2_hwcap::get() {
// SAFETY: we checked that `sha2` target feature is available
return unsafe { aarch64_sha2::compress(state, blocks) };
}
}
}
soft::compress(state, blocks);
}
}
}
/// Raw SHA-256 compression function.
///
/// This is a low-level "hazmat" API which provides direct access to the core
/// functionality of SHA-256.
pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
compress(state, blocks)
}
+94
View File
@@ -0,0 +1,94 @@
//! SHA-256 `aarch64` backend.
//!
//! Implementation adapted from mbedtls.
#![allow(unsafe_op_in_unsafe_fn)]
#[cfg(not(target_arch = "aarch64"))]
compile_error!("aarch64-sha2 backend can be used only aarch64 target arches");
use crate::consts::K32;
use core::arch::aarch64::*;
#[target_feature(enable = "sha2")]
pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
// SAFETY: Requires the sha2 feature.
// Load state into vectors.
let mut abcd = vld1q_u32(state[0..4].as_ptr());
let mut efgh = vld1q_u32(state[4..8].as_ptr());
// Iterate through the message blocks.
for block in blocks {
// Keep original state values.
let abcd_orig = abcd;
let efgh_orig = efgh;
// Load the message block into vectors, assuming little endianness.
let mut s0 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[0..16].as_ptr())));
let mut s1 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[16..32].as_ptr())));
let mut s2 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[32..48].as_ptr())));
let mut s3 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[48..64].as_ptr())));
// Rounds 0 to 3
let mut tmp = vaddq_u32(s0, vld1q_u32(&K32[0]));
let mut abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
// Rounds 4 to 7
tmp = vaddq_u32(s1, vld1q_u32(&K32[4]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
// Rounds 8 to 11
tmp = vaddq_u32(s2, vld1q_u32(&K32[8]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
// Rounds 12 to 15
tmp = vaddq_u32(s3, vld1q_u32(&K32[12]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
for t in (16..64).step_by(16) {
// Rounds t to t + 3
s0 = vsha256su1q_u32(vsha256su0q_u32(s0, s1), s2, s3);
tmp = vaddq_u32(s0, vld1q_u32(&K32[t]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
// Rounds t + 4 to t + 7
s1 = vsha256su1q_u32(vsha256su0q_u32(s1, s2), s3, s0);
tmp = vaddq_u32(s1, vld1q_u32(&K32[t + 4]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
// Rounds t + 8 to t + 11
s2 = vsha256su1q_u32(vsha256su0q_u32(s2, s3), s0, s1);
tmp = vaddq_u32(s2, vld1q_u32(&K32[t + 8]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
// Rounds t + 12 to t + 15
s3 = vsha256su1q_u32(vsha256su0q_u32(s3, s0), s1, s2);
tmp = vaddq_u32(s3, vld1q_u32(&K32[t + 12]));
abcd_prev = abcd;
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
}
// Add the block-specific state to the original state.
abcd = vaddq_u32(abcd, abcd_orig);
efgh = vaddq_u32(efgh, efgh_orig);
}
// Store vectors into state.
vst1q_u32(state[0..4].as_mut_ptr(), abcd);
vst1q_u32(state[4..8].as_mut_ptr(), efgh);
}
+230
View File
@@ -0,0 +1,230 @@
//! LoongArch64 assembly backend
#[cfg(not(target_arch = "loongarch64"))]
compile_error!("loongarch-asm backend can be used only on loongarch64 target arches");
macro_rules! c {
($($l:expr)*) => {
concat!($($l ,)*)
};
}
macro_rules! rounda {
($i:literal, $a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $f:literal, $g:literal, $h:literal) => {
c!(
"ld.w $a5, $a1, (" $i " * 4);"
"revb.2h $a5, $a5;"
"rotri.w $a5, $a5, 16;"
roundtail!($i, $a, $b, $c, $d, $e, $f, $g, $h)
)
};
}
macro_rules! roundb {
($i:literal, $a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $f:literal, $g:literal, $h:literal) => {
c!(
"ld.w $a4, $sp, (((" $i " - 15) & 0xF) * 4);"
"ld.w $a5, $sp, (((" $i " - 16) & 0xF) * 4);"
"ld.w $a6, $sp, (((" $i " - 7) & 0xF) * 4);"
"add.w $a5, $a5, $a6;"
"rotri.w $a6, $a4, 18;"
"srli.w $a7, $a4, 3;"
"rotri.w $a4, $a4, 7;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"add.w $a5, $a5, $a4;"
"ld.w $a4, $sp, (((" $i " - 2) & 0xF) * 4);"
"rotri.w $a6, $a4, 19;"
"srli.w $a7, $a4, 10;"
"rotri.w $a4, $a4, 17;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"add.w $a5, $a5, $a4;"
roundtail!($i, $a, $b, $c, $d, $e, $f, $g, $h)
)
};
}
macro_rules! roundtail {
($i:literal, $a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $f:literal, $g:literal, $h:literal) => {
c!(
// Part 0
"rotri.w $a6, " $e ", 11;"
"rotri.w $a7, " $e ", 25;"
"rotri.w $a4, " $e ", 6;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"xor $a6, " $g ", " $f ";"
"ld.w $a7, $a3, " $i " * 4;"
"and $a6, $a6, " $e ";"
"xor $a6, $a6, " $g ";"
"add.w $a4, $a4, $a6;"
"add.w $a4, $a4, $a7;"
"add.w " $h ", " $h ", $a5;"
"add.w " $h ", " $h ", $a4;"
// Part 1
"add.w " $d ", " $d ", " $h ";"
// Part 2
"rotri.w $a6, " $a ", 13;"
"rotri.w $a7, " $a ", 22;"
"rotri.w $a4, " $a ", 2;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"add.w " $h ", " $h ", $a4;"
"or $a4, " $c ", " $b ";"
"and $a6, " $c ", " $b ";"
"and $a4, $a4, " $a ";"
"or $a4, $a4, $a6;"
"add.w " $h ", " $h ", $a4;"
"st.w $a5, $sp, ((" $i " & 0xF) * 4);"
)
};
}
pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
if blocks.is_empty() {
return;
}
unsafe {
core::arch::asm!(
// Allocate scratch stack space
"addi.d $sp, $sp, -64;",
// Load state
"ld.w $t0, $a0, 0",
"ld.w $t1, $a0, 4",
"ld.w $t2, $a0, 8",
"ld.w $t3, $a0, 12",
"ld.w $t4, $a0, 16",
"ld.w $t5, $a0, 20",
"ld.w $t6, $a0, 24",
"ld.w $t7, $a0, 28",
"42:",
// Do 64 rounds of hashing
rounda!( 0, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
rounda!( 1, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
rounda!( 2, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
rounda!( 3, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
rounda!( 4, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
rounda!( 5, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
rounda!( 6, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
rounda!( 7, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
rounda!( 8, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
rounda!( 9, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
rounda!(10, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
rounda!(11, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
rounda!(12, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
rounda!(13, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
rounda!(14, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
rounda!(15, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(16, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(17, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(18, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(19, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(20, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(21, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(22, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(23, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(24, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(25, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(26, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(27, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(28, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(29, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(30, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(31, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(32, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(33, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(34, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(35, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(36, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(37, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(38, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(39, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(40, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(41, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(42, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(43, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(44, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(45, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(46, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(47, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(48, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(49, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(50, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(51, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(52, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(53, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(54, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(55, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(56, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(57, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(58, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(59, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(60, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(61, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(62, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(63, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
// Update state registers
"ld.w $a4, $a0, 0", // a
"ld.w $a5, $a0, 4", // b
"ld.w $a6, $a0, 8", // c
"ld.w $a7, $a0, 12", // d
"add.w $t0, $t0, $a4",
"add.w $t1, $t1, $a5",
"add.w $t2, $t2, $a6",
"add.w $t3, $t3, $a7",
"ld.w $a4, $a0, 16", // e
"ld.w $a5, $a0, 20", // f
"ld.w $a6, $a0, 24", // g
"ld.w $a7, $a0, 28", // h
"add.w $t4, $t4, $a4",
"add.w $t5, $t5, $a5",
"add.w $t6, $t6, $a6",
"add.w $t7, $t7, $a7",
// Save updated state
"st.w $t0, $a0, 0",
"st.w $t1, $a0, 4",
"st.w $t2, $a0, 8",
"st.w $t3, $a0, 12",
"st.w $t4, $a0, 16",
"st.w $t5, $a0, 20",
"st.w $t6, $a0, 24",
"st.w $t7, $a0, 28",
// Looping over blocks
"addi.d $a1, $a1, 64",
"addi.d $a2, $a2, -1",
"bnez $a2, 42b",
// Restore stack register
"addi.d $sp, $sp, 64",
in("$a0") state,
inout("$a1") blocks.as_ptr() => _,
inout("$a2") blocks.len() => _,
in("$a3") crate::consts::K32.as_ptr(),
// Clobbers
out("$a4") _,
out("$a5") _,
out("$a6") _,
out("$a7") _,
out("$t0") _,
out("$t1") _,
out("$t2") _,
out("$t3") _,
out("$t4") _,
out("$t5") _,
out("$t6") _,
out("$t7") _,
options(preserves_flags),
);
}
}
+19
View File
@@ -0,0 +1,19 @@
#[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64")))]
compile_error!("riscv-zknh backend can be used only on riscv32 and riscv64 target arches");
mod utils;
#[cfg(target_arch = "riscv32")]
use core::arch::riscv32::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
#[cfg(target_arch = "riscv64")]
use core::arch::riscv64::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
cfg_if::cfg_if! {
if #[cfg(sha2_backend_riscv_zknh = "compact")] {
mod compact;
pub(super) use compact::compress;
} else {
mod unroll;
pub(super) use unroll::compress;
}
}
@@ -0,0 +1,67 @@
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
use crate::consts::K32;
#[target_feature(enable = "zknh")]
pub(in super::super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks.iter().map(super::utils::load_block) {
compress_block(state, block);
}
}
#[target_feature(enable = "zknh")]
fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
let mut s = *state;
for r in 0..64 {
round(&mut s, &block, r);
if r < 48 {
schedule(&mut block, r)
}
}
for i in 0..8 {
state[i] = state[i].wrapping_add(s[i]);
}
}
#[target_feature(enable = "zknh")]
fn schedule(block: &mut [u32; 16], r: usize) {
block[r % 16] = block[r % 16]
.wrapping_add(sha256sig1(block[(r + 14) % 16]))
.wrapping_add(block[(r + 9) % 16])
.wrapping_add(sha256sig0(block[(r + 1) % 16]));
}
#[target_feature(enable = "zknh")]
fn round(state: &mut [u32; 8], block: &[u32; 16], r: usize) {
let n = K32.len() - r;
#[allow(clippy::identity_op)]
let a = (n + 0) % 8;
let b = (n + 1) % 8;
let c = (n + 2) % 8;
let d = (n + 3) % 8;
let e = (n + 4) % 8;
let f = (n + 5) % 8;
let g = (n + 6) % 8;
let h = (n + 7) % 8;
state[h] = state[h]
.wrapping_add(sha256sum1(state[e]))
.wrapping_add(ch(state[e], state[f], state[g]))
.wrapping_add(K32[r])
.wrapping_add(block[r % 16]);
state[d] = state[d].wrapping_add(state[h]);
state[h] = state[h]
.wrapping_add(sha256sum0(state[a]))
.wrapping_add(maj(state[a], state[b], state[c]))
}
#[inline(always)]
fn ch(x: u32, y: u32, z: u32) -> u32 {
(x & y) ^ (!x & z)
}
#[inline(always)]
fn maj(x: u32, y: u32, z: u32) -> u32 {
(x & y) ^ (x & z) ^ (y & z)
}
@@ -0,0 +1,101 @@
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
use crate::consts::K32;
#[target_feature(enable = "zknh")]
pub(in super::super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks.iter().map(super::utils::load_block) {
compress_block(state, block);
}
}
#[target_feature(enable = "zknh")]
fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
let s = &mut state.clone();
let b = &mut block;
for i in 0..3 {
let k = &K32[16 * i..];
round_schedule::<0>(s, b, k);
round_schedule::<1>(s, b, k);
round_schedule::<2>(s, b, k);
round_schedule::<3>(s, b, k);
round_schedule::<4>(s, b, k);
round_schedule::<5>(s, b, k);
round_schedule::<6>(s, b, k);
round_schedule::<7>(s, b, k);
round_schedule::<8>(s, b, k);
round_schedule::<9>(s, b, k);
round_schedule::<10>(s, b, k);
round_schedule::<11>(s, b, k);
round_schedule::<12>(s, b, k);
round_schedule::<13>(s, b, k);
round_schedule::<14>(s, b, k);
round_schedule::<15>(s, b, k);
}
let k = &K32[48..];
round::<0>(s, b, k);
round::<1>(s, b, k);
round::<2>(s, b, k);
round::<3>(s, b, k);
round::<4>(s, b, k);
round::<5>(s, b, k);
round::<6>(s, b, k);
round::<7>(s, b, k);
round::<8>(s, b, k);
round::<9>(s, b, k);
round::<10>(s, b, k);
round::<11>(s, b, k);
round::<12>(s, b, k);
round::<13>(s, b, k);
round::<14>(s, b, k);
round::<15>(s, b, k);
for i in 0..8 {
state[i] = state[i].wrapping_add(s[i]);
}
}
#[target_feature(enable = "zknh")]
fn round_schedule<const R: usize>(state: &mut [u32; 8], block: &mut [u32; 16], k: &[u32]) {
round::<R>(state, block, k);
block[R] = block[R]
.wrapping_add(sha256sig1(block[(R + 14) % 16]))
.wrapping_add(block[(R + 9) % 16])
.wrapping_add(sha256sig0(block[(R + 1) % 16]));
}
#[target_feature(enable = "zknh")]
fn round<const R: usize>(state: &mut [u32; 8], block: &[u32; 16], k: &[u32]) {
let n = K32.len() - R;
#[allow(clippy::identity_op)]
let a = (n + 0) % 8;
let b = (n + 1) % 8;
let c = (n + 2) % 8;
let d = (n + 3) % 8;
let e = (n + 4) % 8;
let f = (n + 5) % 8;
let g = (n + 6) % 8;
let h = (n + 7) % 8;
state[h] = state[h]
.wrapping_add(sha256sum1(state[e]))
.wrapping_add(ch(state[e], state[f], state[g]))
.wrapping_add(super::utils::opaque_load::<R>(k))
.wrapping_add(block[R]);
state[d] = state[d].wrapping_add(state[h]);
state[h] = state[h]
.wrapping_add(sha256sum0(state[a]))
.wrapping_add(maj(state[a], state[b], state[c]))
}
#[inline(always)]
fn ch(x: u32, y: u32, z: u32) -> u32 {
(x & y) ^ (!x & z)
}
#[inline(always)]
fn maj(x: u32, y: u32, z: u32) -> u32 {
(x & y) ^ (x & z) ^ (y & z)
}
@@ -0,0 +1,97 @@
use core::{arch::asm, ptr};
#[inline(always)]
pub(super) fn load_block(block: &[u8; 64]) -> [u32; 16] {
if block.as_ptr().cast::<u32>().is_aligned() {
load_aligned_block(block)
} else {
load_unaligned_block(block)
}
}
#[inline(always)]
fn load_aligned_block(block: &[u8; 64]) -> [u32; 16] {
let p: *const u32 = block.as_ptr().cast();
debug_assert!(p.is_aligned());
let mut res = [0u32; 16];
for i in 0..16 {
let val = unsafe { ptr::read(p.add(i)) };
res[i] = val.to_be();
}
res
}
/// Use LW instruction on RV32 and LWU on RV64
#[cfg(target_arch = "riscv32")]
macro_rules! lw {
($r:literal) => {
concat!("lw ", $r)
};
}
#[cfg(target_arch = "riscv64")]
macro_rules! lw {
($r:literal) => {
concat!("lwu ", $r)
};
}
#[inline(always)]
fn load_unaligned_block(block: &[u8; 64]) -> [u32; 16] {
let offset = (block.as_ptr() as usize) % align_of::<u32>();
debug_assert_ne!(offset, 0);
let off1 = (8 * offset) % 32;
let off2 = (32 - off1) % 32;
let bp: *const u32 = block.as_ptr().wrapping_sub(offset).cast();
let mut left: u32;
let mut res = [0u32; 16];
unsafe {
asm!(
lw!("{left}, 0({bp})"), // left = unsafe { ptr::read(bp) };
"srl {left}, {left}, {off1}", // left >>= off1;
bp = in(reg) bp,
off1 = in(reg) off1,
left = out(reg) left,
options(pure, nostack, readonly, preserves_flags),
);
}
for i in 0..15 {
let right = unsafe { ptr::read(bp.add(1 + i)) };
res[i] = (left | (right << off2)).to_be();
left = right >> off1;
}
let right: u32;
unsafe {
asm!(
lw!("{right}, 16 * 4({bp})"), // right = ptr::read(bp.add(16));
"sll {right}, {right}, {off2}", // right <<= off2;
bp = in(reg) bp,
off2 = in(reg) off2,
right = out(reg) right,
options(pure, nostack, readonly, preserves_flags),
);
}
res[15] = (left | right).to_be();
res
}
/// This function returns `k[R]`, but prevents compiler from inlining the indexed value
#[cfg(not(sha2_backend_riscv_zknh = "compact"))]
pub(super) fn opaque_load<const R: usize>(k: &[u32]) -> u32 {
assert!(R < k.len());
let dst;
unsafe {
core::arch::asm!(
lw!("{dst}, 4*{R}({k})"),
R = const R,
k = in(reg) k.as_ptr(),
dst = out(reg) dst,
options(pure, readonly, nostack, preserves_flags),
);
}
dst
}
+16
View File
@@ -0,0 +1,16 @@
cfg_if::cfg_if! {
if #[cfg(sha2_backend_soft = "compact")] {
mod compact;
pub(super) use compact::compress;
} else {
mod unroll;
pub(super) use unroll::compress;
}
}
fn to_u32s(block: &[u8; 64]) -> [u32; 16] {
core::array::from_fn(|i| {
let chunk = block[4 * i..][..4].try_into().unwrap();
u32::from_be_bytes(chunk)
})
}
+57
View File
@@ -0,0 +1,57 @@
use crate::consts::K32;
fn compress_u32(state: &mut [u32; 8], mut block: [u32; 16]) {
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
for i in 0..64 {
let w = if i < 16 {
block[i]
} else {
let w15 = block[(i - 15) % 16];
let s0 = (w15.rotate_right(7)) ^ (w15.rotate_right(18)) ^ (w15 >> 3);
let w2 = block[(i - 2) % 16];
let s1 = (w2.rotate_right(17)) ^ (w2.rotate_right(19)) ^ (w2 >> 10);
let new_w = block[(i - 16) % 16]
.wrapping_add(s0)
.wrapping_add(block[(i - 7) % 16])
.wrapping_add(s1);
block[i % 16] = new_w;
new_w
};
let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
let ch = (e & f) ^ ((!e) & g);
let t1 = s1
.wrapping_add(ch)
.wrapping_add(K32[i])
.wrapping_add(w)
.wrapping_add(h);
let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
let maj = (a & b) ^ (a & c) ^ (b & c);
let t2 = s0.wrapping_add(maj);
h = g;
g = f;
f = e;
e = d.wrapping_add(t1);
d = c;
c = b;
b = a;
a = t1.wrapping_add(t2);
}
state[0] = state[0].wrapping_add(a);
state[1] = state[1].wrapping_add(b);
state[2] = state[2].wrapping_add(c);
state[3] = state[3].wrapping_add(d);
state[4] = state[4].wrapping_add(e);
state[5] = state[5].wrapping_add(f);
state[6] = state[6].wrapping_add(g);
state[7] = state[7].wrapping_add(h);
}
pub(in super::super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks.iter() {
compress_u32(state, super::to_u32s(block));
}
}
+100
View File
@@ -0,0 +1,100 @@
use crate::consts::K32;
#[rustfmt::skip]
macro_rules! repeat64 {
($i:ident, $b:block) => {
let $i = 0; $b; let $i = 1; $b; let $i = 2; $b; let $i = 3; $b;
let $i = 4; $b; let $i = 5; $b; let $i = 6; $b; let $i = 7; $b;
let $i = 8; $b; let $i = 9; $b; let $i = 10; $b; let $i = 11; $b;
let $i = 12; $b; let $i = 13; $b; let $i = 14; $b; let $i = 15; $b;
let $i = 16; $b; let $i = 17; $b; let $i = 18; $b; let $i = 19; $b;
let $i = 20; $b; let $i = 21; $b; let $i = 22; $b; let $i = 23; $b;
let $i = 24; $b; let $i = 25; $b; let $i = 26; $b; let $i = 27; $b;
let $i = 28; $b; let $i = 29; $b; let $i = 30; $b; let $i = 31; $b;
let $i = 32; $b; let $i = 33; $b; let $i = 34; $b; let $i = 35; $b;
let $i = 36; $b; let $i = 37; $b; let $i = 38; $b; let $i = 39; $b;
let $i = 40; $b; let $i = 41; $b; let $i = 42; $b; let $i = 43; $b;
let $i = 44; $b; let $i = 45; $b; let $i = 46; $b; let $i = 47; $b;
let $i = 48; $b; let $i = 49; $b; let $i = 50; $b; let $i = 51; $b;
let $i = 52; $b; let $i = 53; $b; let $i = 54; $b; let $i = 55; $b;
let $i = 56; $b; let $i = 57; $b; let $i = 58; $b; let $i = 59; $b;
let $i = 60; $b; let $i = 61; $b; let $i = 62; $b; let $i = 63; $b;
};
}
/// Read round constant
fn rk(i: usize) -> u32 {
// `read_volatile` forces compiler to read round constants from the static
// instead of inlining them, which improves codegen and performance on some platforms.
// On x86 targets 32-bit constants can be encoded using immediate argument on the `add`
// instruction, so it's more efficient to inline them.
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
use core::ptr::read as r;
} else {
use core::ptr::read_volatile as r;
}
}
unsafe {
let p = K32.as_ptr().add(i);
r(p)
}
}
/// Process a block with the SHA-256 algorithm.
fn compress_block(state: &mut [u32; 8], block: &[u8; 64]) {
let mut block = super::to_u32s(block);
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
repeat64!(i, {
let w = if i < 16 {
block[i]
} else {
let w15 = block[(i - 15) % 16];
let s0 = (w15.rotate_right(7)) ^ (w15.rotate_right(18)) ^ (w15 >> 3);
let w2 = block[(i - 2) % 16];
let s1 = (w2.rotate_right(17)) ^ (w2.rotate_right(19)) ^ (w2 >> 10);
block[i % 16] = block[i % 16]
.wrapping_add(s0)
.wrapping_add(block[(i - 7) % 16])
.wrapping_add(s1);
block[i % 16]
};
let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
let ch = (e & f) ^ ((!e) & g);
let t1 = s1
.wrapping_add(ch)
.wrapping_add(rk(i))
.wrapping_add(w)
.wrapping_add(h);
let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
let maj = (a & b) ^ (a & c) ^ (b & c);
let t2 = s0.wrapping_add(maj);
h = g;
g = f;
f = e;
e = d.wrapping_add(t1);
d = c;
c = b;
b = a;
a = t1.wrapping_add(t2);
});
state[0] = state[0].wrapping_add(a);
state[1] = state[1].wrapping_add(b);
state[2] = state[2].wrapping_add(c);
state[3] = state[3].wrapping_add(d);
state[4] = state[4].wrapping_add(e);
state[5] = state[5].wrapping_add(f);
state[6] = state[6].wrapping_add(g);
state[7] = state[7].wrapping_add(h);
}
pub(in super::super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks {
compress_block(state, block);
}
}
+196
View File
@@ -0,0 +1,196 @@
#![allow(clippy::many_single_char_names, unsafe_op_in_unsafe_fn)]
#[cfg(not(target_arch = "wasm32"))]
compile_error!("wasm32-simd128 backend can be used only on wasm32 target arches");
#[cfg(not(target_feature = "simd128"))]
compile_error!("wasm32-simd128 backend requires simd128 target feature");
use core::arch::wasm32::*;
use core::mem::size_of;
use crate::consts::K32;
pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
assert_eq!(SHA256_HASH_WORDS_NUM, 8);
assert_eq!(SHA256_BLOCK_WORDS_NUM, 16);
let mut ms = [u64x2(0, 0); 4];
let mut x = [u64x2(0, 0); 4];
for block in blocks {
unsafe {
let mut current_state = *state;
load_data(&mut x, &mut ms, block.as_ptr().cast());
rounds_0_47(&mut current_state, &mut x, &mut ms);
rounds_48_63(&mut current_state, &ms);
accumulate_state(state, &current_state);
}
}
}
#[inline(always)]
unsafe fn load_data(x: &mut [v128; 4], ms: &mut MsgSchedule, data: *const v128) {
macro_rules! unrolled_iterations {
($($i:literal),*) => {$(
x[$i] = v128_load(data.add($i).cast());
x[$i] = i8x16_shuffle::<3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12>(x[$i], x[$i]);
let y = i32x4_add(
x[$i],
v128_load(K32.as_ptr().add(4 * $i).cast()),
);
ms[$i] = y;
)*};
}
unrolled_iterations!(0, 1, 2, 3);
}
#[inline(always)]
unsafe fn rounds_0_47(current_state: &mut State, x: &mut [v128; 4], ms: &mut MsgSchedule) {
let mut k32_idx: usize = SHA256_BLOCK_WORDS_NUM;
for _ in 0..3 {
for j in 0..4 {
let k32 = v128_load(K32.as_ptr().add(k32_idx).cast());
let y = sha256_update_x(x, k32);
{
let ms = ms[j];
sha_round(current_state, u32x4_extract_lane::<0>(ms));
sha_round(current_state, u32x4_extract_lane::<1>(ms));
sha_round(current_state, u32x4_extract_lane::<2>(ms));
sha_round(current_state, u32x4_extract_lane::<3>(ms));
}
ms[j] = y;
k32_idx += 4;
}
}
}
#[inline(always)]
fn rounds_48_63(current_state: &mut State, ms: &MsgSchedule) {
for j in 0..4 {
let ms = ms[j];
sha_round(current_state, u32x4_extract_lane::<0>(ms));
sha_round(current_state, u32x4_extract_lane::<1>(ms));
sha_round(current_state, u32x4_extract_lane::<2>(ms));
sha_round(current_state, u32x4_extract_lane::<3>(ms));
}
}
#[inline(always)]
fn sha_round(s: &mut State, x: u32) {
macro_rules! big_sigma0 {
($a:expr) => {
$a.rotate_right(2) ^ $a.rotate_right(13) ^ $a.rotate_right(22)
};
}
macro_rules! big_sigma1 {
($a:expr) => {
$a.rotate_right(6) ^ $a.rotate_right(11) ^ $a.rotate_right(25)
};
}
macro_rules! bool3ary_202 {
($a:expr, $b:expr, $c:expr) => {
$c ^ ($a & ($b ^ $c))
};
} // Choose, MD5F, SHA1C
macro_rules! bool3ary_232 {
($a:expr, $b:expr, $c:expr) => {
($a & $b) ^ ($a & $c) ^ ($b & $c)
};
} // Majority, SHA1M
macro_rules! rotate_state {
($s:ident) => {{
let tmp = $s[7];
$s[7] = $s[6];
$s[6] = $s[5];
$s[5] = $s[4];
$s[4] = $s[3];
$s[3] = $s[2];
$s[2] = $s[1];
$s[1] = $s[0];
$s[0] = tmp;
}};
}
let t = x
.wrapping_add(s[7])
.wrapping_add(big_sigma1!(s[4]))
.wrapping_add(bool3ary_202!(s[4], s[5], s[6]));
s[7] = t
.wrapping_add(big_sigma0!(s[0]))
.wrapping_add(bool3ary_232!(s[0], s[1], s[2]));
s[3] = s[3].wrapping_add(t);
rotate_state!(s);
}
#[inline(always)]
fn accumulate_state(dst: &mut State, src: &State) {
for i in 0..SHA256_HASH_WORDS_NUM {
dst[i] = dst[i].wrapping_add(src[i]);
}
}
#[inline(always)]
unsafe fn sha256_update_x(x: &mut [v128; 4], k32: v128) -> v128 {
const SIGMA0_0: u32 = 7;
const SIGMA0_1: u32 = 18;
const SIGMA0_2: u32 = 3;
const SIGMA1_0: u32 = 17;
const SIGMA1_1: u32 = 19;
const SIGMA1_2: u32 = 10;
const SHA256_WORD_BIT_LEN: u32 = 8 * size_of::<u32>() as u32;
const ZERO: v128 = u64x2(0, 0);
let mut t0 = u32x4_shuffle::<1, 2, 3, 4>(x[0], x[1]);
let mut t3 = u32x4_shuffle::<1, 2, 3, 4>(x[2], x[3]);
let mut t2 = u32x4_shr(t0, SIGMA0_0);
x[0] = u32x4_add(x[0], t3);
t3 = u32x4_shr(t0, SIGMA0_2);
let mut t1 = u32x4_shl(t0, SHA256_WORD_BIT_LEN - SIGMA0_1);
t0 = v128_xor(t3, t2);
t3 = u32x4_shuffle::<2, 2, 3, 3>(x[3], x[3]);
t2 = u32x4_shr(t2, SIGMA0_1 - SIGMA0_0);
t0 = v128_xor(t0, t1);
t0 = v128_xor(t0, t2);
t1 = u32x4_shl(t1, SIGMA0_1 - SIGMA0_0);
t2 = u32x4_shr(t3, SIGMA1_2);
t3 = u64x2_shr(t3, SIGMA1_0);
t1 = v128_xor(t0, t1);
x[0] = u32x4_add(x[0], t1);
t2 = v128_xor(t2, t3);
t3 = u64x2_shr(t3, SIGMA1_1 - SIGMA1_0);
t2 = v128_xor(t2, t3);
t2 = u32x4_shuffle::<0, 2, 7, 7>(t2, ZERO);
x[0] = u32x4_add(x[0], t2);
t3 = u32x4_shuffle::<0, 0, 1, 1>(x[0], x[0]);
t2 = u32x4_shr(t3, SIGMA1_2);
t3 = u64x2_shr(t3, SIGMA1_0);
t2 = v128_xor(t2, t3);
t3 = u64x2_shr(t3, SIGMA1_1 - SIGMA1_0);
t2 = v128_xor(t2, t3);
t2 = u32x4_shuffle::<7, 7, 0, 2>(t2, ZERO);
x[0] = u32x4_add(x[0], t2);
let tmp = x[0];
x[0] = x[1];
x[1] = x[2];
x[2] = x[3];
x[3] = tmp;
u32x4_add(x[3], k32)
}
type State = [u32; SHA256_HASH_WORDS_NUM];
type MsgSchedule = [v128; SHA256_BLOCK_WORDS_NUM / 4];
const SHA256_BLOCK_BYTE_LEN: usize = 64;
const SHA256_HASH_BYTE_LEN: usize = 32;
const SHA256_HASH_WORDS_NUM: usize = SHA256_HASH_BYTE_LEN / size_of::<u32>();
const SHA256_BLOCK_WORDS_NUM: usize = SHA256_BLOCK_BYTE_LEN / size_of::<u32>();
+102
View File
@@ -0,0 +1,102 @@
//! SHA-256 `x86`/`x86_64` backend
#![allow(clippy::many_single_char_names, unsafe_op_in_unsafe_fn)]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
compile_error!("x86-sha backend can be used only on x86 and x86_64 target arches");
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
#[target_feature(enable = "sha,sse2,ssse3,sse4.1")]
unsafe fn schedule(v0: __m128i, v1: __m128i, v2: __m128i, v3: __m128i) -> __m128i {
let t1 = _mm_sha256msg1_epu32(v0, v1);
let t2 = _mm_alignr_epi8(v3, v2, 4);
let t3 = _mm_add_epi32(t1, t2);
_mm_sha256msg2_epu32(t3, v3)
}
macro_rules! rounds4 {
($abef:ident, $cdgh:ident, $rest:expr, $i:expr) => {{
let k = crate::consts::K32X4[$i];
let kv = _mm_set_epi32(k[0] as i32, k[1] as i32, k[2] as i32, k[3] as i32);
let t1 = _mm_add_epi32($rest, kv);
$cdgh = _mm_sha256rnds2_epu32($cdgh, $abef, t1);
let t2 = _mm_shuffle_epi32(t1, 0x0E);
$abef = _mm_sha256rnds2_epu32($abef, $cdgh, t2);
}};
}
macro_rules! schedule_rounds4 {
(
$abef:ident, $cdgh:ident,
$w0:expr, $w1:expr, $w2:expr, $w3:expr, $w4:expr,
$i: expr
) => {{
$w4 = schedule($w0, $w1, $w2, $w3);
rounds4!($abef, $cdgh, $w4, $i);
}};
}
// we use unaligned loads with `__m128i` pointers
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "sha,sse2,ssse3,sse4.1")]
pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
#[allow(non_snake_case)]
let MASK: __m128i = _mm_set_epi64x(
0x0C0D_0E0F_0809_0A0Bu64 as i64,
0x0405_0607_0001_0203u64 as i64,
);
let state_ptr: *const __m128i = state.as_ptr().cast();
let dcba = _mm_loadu_si128(state_ptr.add(0));
let hgfe = _mm_loadu_si128(state_ptr.add(1));
let cdab = _mm_shuffle_epi32(dcba, 0xB1);
let efgh = _mm_shuffle_epi32(hgfe, 0x1B);
let mut abef = _mm_alignr_epi8(cdab, efgh, 8);
let mut cdgh = _mm_blend_epi16(efgh, cdab, 0xF0);
for block in blocks {
let abef_save = abef;
let cdgh_save = cdgh;
let block_ptr: *const __m128i = block.as_ptr().cast();
let mut w0 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.add(0)), MASK);
let mut w1 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.add(1)), MASK);
let mut w2 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.add(2)), MASK);
let mut w3 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.add(3)), MASK);
let mut w4;
rounds4!(abef, cdgh, w0, 0);
rounds4!(abef, cdgh, w1, 1);
rounds4!(abef, cdgh, w2, 2);
rounds4!(abef, cdgh, w3, 3);
schedule_rounds4!(abef, cdgh, w0, w1, w2, w3, w4, 4);
schedule_rounds4!(abef, cdgh, w1, w2, w3, w4, w0, 5);
schedule_rounds4!(abef, cdgh, w2, w3, w4, w0, w1, 6);
schedule_rounds4!(abef, cdgh, w3, w4, w0, w1, w2, 7);
schedule_rounds4!(abef, cdgh, w4, w0, w1, w2, w3, 8);
schedule_rounds4!(abef, cdgh, w0, w1, w2, w3, w4, 9);
schedule_rounds4!(abef, cdgh, w1, w2, w3, w4, w0, 10);
schedule_rounds4!(abef, cdgh, w2, w3, w4, w0, w1, 11);
schedule_rounds4!(abef, cdgh, w3, w4, w0, w1, w2, 12);
schedule_rounds4!(abef, cdgh, w4, w0, w1, w2, w3, 13);
schedule_rounds4!(abef, cdgh, w0, w1, w2, w3, w4, 14);
schedule_rounds4!(abef, cdgh, w1, w2, w3, w4, w0, 15);
abef = _mm_add_epi32(abef, abef_save);
cdgh = _mm_add_epi32(cdgh, cdgh_save);
}
let feba = _mm_shuffle_epi32(abef, 0x1B);
let dchg = _mm_shuffle_epi32(cdgh, 0xB1);
let dcba = _mm_blend_epi16(feba, dchg, 0xF0);
let hgef = _mm_alignr_epi8(dchg, feba, 8);
let state_ptr_mut: *mut __m128i = state.as_mut_ptr().cast();
_mm_storeu_si128(state_ptr_mut.add(0), dcba);
_mm_storeu_si128(state_ptr_mut.add(1), hgef);
}
+83
View File
@@ -0,0 +1,83 @@
cfg_if::cfg_if! {
if #[cfg(any(sha2_backend = "soft", sha2_256_backend = "soft"))] {
mod soft;
use soft::compress;
} else if #[cfg(any(sha2_backend = "riscv-zknh", sha2_256_backend = "riscv-zknh"))] {
mod riscv_zknh;
#[cfg(not(all(
target_feature = "zknh",
any(target_feature = "zbb", target_feature = "zbkb")
)))]
compile_error!("riscv-zknh backend requires zknh and zbkb (or zbb) target features");
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
// SAFETY: we checked above that the required target features are enabled
unsafe { riscv_zknh::compress(state, blocks) }
}
} else if #[cfg(sha2_512_backend = "x86-avx2")] {
mod x86_avx2;
#[cfg(not(target_feature = "avx2"))]
compile_error!("x86-avx2 backend requires avx2 target feature");
fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
// SAFETY: we checked above that the required target features are enabled
unsafe { x86_avx2::compress(state, blocks) }
}
} else if #[cfg(sha2_512_backend = "aarch64-sha3")] {
mod aarch64_sha3;
#[cfg(not(target_feature = "sha3"))]
compile_error!("aarch64-sha3 backend requires sha3 target feature");
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
// SAFETY: we checked above that the required target features are enabled
unsafe { aarch64_sha3::compress(state, blocks) }
}
} else if #[cfg(target_arch = "loongarch64")] {
mod loongarch64_asm;
use loongarch64_asm::compress;
} else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
mod wasm32_simd128;
use wasm32_simd128::compress;
} else {
mod soft;
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
mod x86_avx2;
cpufeatures::new!(avx2_cpuid, "avx2");
} else if #[cfg(target_arch = "aarch64")] {
mod aarch64_sha3;
cpufeatures::new!(sha3_hwcap, "sha3");
}
}
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
if avx2_cpuid::get() {
// SAFETY: we checked that required target features are available
return unsafe { x86_avx2::compress(state, blocks) };
}
} else if #[cfg(target_arch = "aarch64")] {
if sha3_hwcap::get() {
// SAFETY: we checked that `sha3` target feature is available
return unsafe { aarch64_sha3::compress(state, blocks) };
}
}
}
soft::compress(state, blocks);
}
}
}
/// Raw SHA-512 compression function.
///
/// This is a low-level "hazmat" API which provides direct access to the core
/// functionality of SHA-512.
pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
compress(state, blocks)
}
+172
View File
@@ -0,0 +1,172 @@
// Implementation adapted from mbedtls.
#![allow(unsafe_op_in_unsafe_fn)]
#[cfg(not(target_arch = "aarch64"))]
compile_error!("aarch64-sha3 backend can be used only aarch64 target arches");
use crate::consts::K64;
use core::arch::aarch64::*;
#[target_feature(enable = "sha3")]
pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
// SAFETY: Requires the sha3 feature.
// Load state into vectors.
let mut ab = vld1q_u64(state[0..2].as_ptr());
let mut cd = vld1q_u64(state[2..4].as_ptr());
let mut ef = vld1q_u64(state[4..6].as_ptr());
let mut gh = vld1q_u64(state[6..8].as_ptr());
// Iterate through the message blocks.
for block in blocks {
// Keep original state values.
let ab_orig = ab;
let cd_orig = cd;
let ef_orig = ef;
let gh_orig = gh;
// Load the message block into vectors, assuming little endianness.
let mut s0 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[0..16].as_ptr())));
let mut s1 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[16..32].as_ptr())));
let mut s2 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[32..48].as_ptr())));
let mut s3 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[48..64].as_ptr())));
let mut s4 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[64..80].as_ptr())));
let mut s5 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[80..96].as_ptr())));
let mut s6 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[96..112].as_ptr())));
let mut s7 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[112..128].as_ptr())));
// Rounds 0 and 1
let mut initial_sum = vaddq_u64(s0, vld1q_u64(&K64[0]));
let mut sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
let mut intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
gh = vsha512h2q_u64(intermed, cd, ab);
cd = vaddq_u64(cd, intermed);
// Rounds 2 and 3
initial_sum = vaddq_u64(s1, vld1q_u64(&K64[2]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
ef = vsha512h2q_u64(intermed, ab, gh);
ab = vaddq_u64(ab, intermed);
// Rounds 4 and 5
initial_sum = vaddq_u64(s2, vld1q_u64(&K64[4]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
cd = vsha512h2q_u64(intermed, gh, ef);
gh = vaddq_u64(gh, intermed);
// Rounds 6 and 7
initial_sum = vaddq_u64(s3, vld1q_u64(&K64[6]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
ab = vsha512h2q_u64(intermed, ef, cd);
ef = vaddq_u64(ef, intermed);
// Rounds 8 and 9
initial_sum = vaddq_u64(s4, vld1q_u64(&K64[8]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
gh = vsha512h2q_u64(intermed, cd, ab);
cd = vaddq_u64(cd, intermed);
// Rounds 10 and 11
initial_sum = vaddq_u64(s5, vld1q_u64(&K64[10]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
ef = vsha512h2q_u64(intermed, ab, gh);
ab = vaddq_u64(ab, intermed);
// Rounds 12 and 13
initial_sum = vaddq_u64(s6, vld1q_u64(&K64[12]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
cd = vsha512h2q_u64(intermed, gh, ef);
gh = vaddq_u64(gh, intermed);
// Rounds 14 and 15
initial_sum = vaddq_u64(s7, vld1q_u64(&K64[14]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
ab = vsha512h2q_u64(intermed, ef, cd);
ef = vaddq_u64(ef, intermed);
for t in (16..80).step_by(16) {
// Rounds t and t + 1
s0 = vsha512su1q_u64(vsha512su0q_u64(s0, s1), s7, vextq_u64(s4, s5, 1));
initial_sum = vaddq_u64(s0, vld1q_u64(&K64[t]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
gh = vsha512h2q_u64(intermed, cd, ab);
cd = vaddq_u64(cd, intermed);
// Rounds t + 2 and t + 3
s1 = vsha512su1q_u64(vsha512su0q_u64(s1, s2), s0, vextq_u64(s5, s6, 1));
initial_sum = vaddq_u64(s1, vld1q_u64(&K64[t + 2]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
ef = vsha512h2q_u64(intermed, ab, gh);
ab = vaddq_u64(ab, intermed);
// Rounds t + 4 and t + 5
s2 = vsha512su1q_u64(vsha512su0q_u64(s2, s3), s1, vextq_u64(s6, s7, 1));
initial_sum = vaddq_u64(s2, vld1q_u64(&K64[t + 4]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
cd = vsha512h2q_u64(intermed, gh, ef);
gh = vaddq_u64(gh, intermed);
// Rounds t + 6 and t + 7
s3 = vsha512su1q_u64(vsha512su0q_u64(s3, s4), s2, vextq_u64(s7, s0, 1));
initial_sum = vaddq_u64(s3, vld1q_u64(&K64[t + 6]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
ab = vsha512h2q_u64(intermed, ef, cd);
ef = vaddq_u64(ef, intermed);
// Rounds t + 8 and t + 9
s4 = vsha512su1q_u64(vsha512su0q_u64(s4, s5), s3, vextq_u64(s0, s1, 1));
initial_sum = vaddq_u64(s4, vld1q_u64(&K64[t + 8]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
gh = vsha512h2q_u64(intermed, cd, ab);
cd = vaddq_u64(cd, intermed);
// Rounds t + 10 and t + 11
s5 = vsha512su1q_u64(vsha512su0q_u64(s5, s6), s4, vextq_u64(s1, s2, 1));
initial_sum = vaddq_u64(s5, vld1q_u64(&K64[t + 10]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
ef = vsha512h2q_u64(intermed, ab, gh);
ab = vaddq_u64(ab, intermed);
// Rounds t + 12 and t + 13
s6 = vsha512su1q_u64(vsha512su0q_u64(s6, s7), s5, vextq_u64(s2, s3, 1));
initial_sum = vaddq_u64(s6, vld1q_u64(&K64[t + 12]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
cd = vsha512h2q_u64(intermed, gh, ef);
gh = vaddq_u64(gh, intermed);
// Rounds t + 14 and t + 15
s7 = vsha512su1q_u64(vsha512su0q_u64(s7, s0), s6, vextq_u64(s3, s4, 1));
initial_sum = vaddq_u64(s7, vld1q_u64(&K64[t + 14]));
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
ab = vsha512h2q_u64(intermed, ef, cd);
ef = vaddq_u64(ef, intermed);
}
// Add the block-specific state to the original state.
ab = vaddq_u64(ab, ab_orig);
cd = vaddq_u64(cd, cd_orig);
ef = vaddq_u64(ef, ef_orig);
gh = vaddq_u64(gh, gh_orig);
}
// Store vectors into state.
vst1q_u64(state[0..2].as_mut_ptr(), ab);
vst1q_u64(state[2..4].as_mut_ptr(), cd);
vst1q_u64(state[4..6].as_mut_ptr(), ef);
vst1q_u64(state[6..8].as_mut_ptr(), gh);
}
+245
View File
@@ -0,0 +1,245 @@
//! LoongArch64 assembly backend
#[cfg(not(target_arch = "loongarch64"))]
compile_error!("loongarch-asm backend can be used only on loongarch64 target arches");
macro_rules! c {
($($l:expr)*) => {
concat!($($l ,)*)
};
}
macro_rules! rounda {
($i:literal, $a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $f:literal, $g:literal, $h:literal) => {
c!(
"ld.d $a5, $a1, (" $i " * 8);"
"revb.d $a5, $a5;"
roundtail!($i, $a, $b, $c, $d, $e, $f, $g, $h)
)
};
}
macro_rules! roundb {
($i:literal, $a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $f:literal, $g:literal, $h:literal) => {
c!(
"ld.d $a4, $sp, (((" $i " - 15) & 0xF) * 8);"
"ld.d $a5, $sp, (((" $i " - 16) & 0xF) * 8);"
"ld.d $a6, $sp, (((" $i " - 7) & 0xF) * 8);"
"add.d $a5, $a5, $a6;"
"rotri.d $a6, $a4, 8;"
"srli.d $a7, $a4, 7;"
"rotri.d $a4, $a4, 1;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"add.d $a5, $a5, $a4;"
"ld.d $a4, $sp, (((" $i " - 2) & 0xF) * 8);"
"rotri.d $a6, $a4, 61;"
"srli.d $a7, $a4, 6;"
"rotri.d $a4, $a4, 19;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"add.d $a5, $a5, $a4;"
roundtail!($i, $a, $b, $c, $d, $e, $f, $g, $h)
)
};
}
macro_rules! roundtail {
($i:literal, $a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $f:literal, $g:literal, $h:literal) => {
c!(
// Part 0
"rotri.d $a6, " $e ", 18;"
"rotri.d $a7, " $e ", 41;"
"rotri.d $a4, " $e ", 14;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"xor $a6, " $g ", " $f ";"
"ld.d $a7, $a3, " $i " * 8;"
"and $a6, $a6, " $e ";"
"xor $a6, $a6, " $g ";"
"add.d $a4, $a4, $a6;"
"add.d $a4, $a4, $a7;"
"add.d " $h ", " $h ", $a5;"
"add.d " $h ", " $h ", $a4;"
// Part 1
"add.d " $d ", " $d ", " $h ";"
// Part 2
"rotri.d $a6, " $a ", 39;"
"rotri.d $a7, " $a ", 34;"
"rotri.d $a4, " $a ", 28;"
"xor $a6, $a6, $a7;"
"xor $a4, $a4, $a6;"
"add.d " $h ", " $h ", $a4;"
"or $a4, " $c ", " $b ";"
"and $a6, " $c ", " $b ";"
"and $a4, $a4, " $a ";"
"or $a4, $a4, $a6;"
"add.d " $h ", " $h ", $a4;"
"st.d $a5, $sp, ((" $i " & 0xF) * 8);"
)
};
}
pub(super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
if blocks.is_empty() {
return;
}
unsafe {
core::arch::asm!(
// Allocate scratch stack space
"addi.d $sp, $sp, -128;",
// Load state
"ld.d $t0, $a0, 0",
"ld.d $t1, $a0, 8",
"ld.d $t2, $a0, 16",
"ld.d $t3, $a0, 24",
"ld.d $t4, $a0, 32",
"ld.d $t5, $a0, 40",
"ld.d $t6, $a0, 48",
"ld.d $t7, $a0, 56",
"42:",
// Do 64 rounds of hashing
rounda!( 0, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
rounda!( 1, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
rounda!( 2, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
rounda!( 3, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
rounda!( 4, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
rounda!( 5, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
rounda!( 6, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
rounda!( 7, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
rounda!( 8, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
rounda!( 9, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
rounda!(10, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
rounda!(11, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
rounda!(12, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
rounda!(13, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
rounda!(14, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
rounda!(15, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(16, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(17, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(18, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(19, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(20, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(21, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(22, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(23, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(24, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(25, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(26, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(27, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(28, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(29, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(30, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(31, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(32, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(33, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(34, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(35, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(36, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(37, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(38, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(39, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(40, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(41, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(42, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(43, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(44, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(45, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(46, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(47, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(48, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(49, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(50, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(51, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(52, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(53, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(54, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(55, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(56, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(57, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(58, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(59, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(60, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(61, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(62, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(63, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(64, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(65, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(66, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(67, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(68, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(69, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(70, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(71, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
roundb!(72, "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7"),
roundb!(73, "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6"),
roundb!(74, "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4", "$t5"),
roundb!(75, "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3", "$t4"),
roundb!(76, "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2", "$t3"),
roundb!(77, "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1" , "$t2"),
roundb!(78, "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0" , "$t1"),
roundb!(79, "$t1" , "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t0"),
// Update state registers
"ld.d $a4, $a0, 0", // a
"ld.d $a5, $a0, 8", // b
"ld.d $a6, $a0, 16", // c
"ld.d $a7, $a0, 24", // d
"add.d $t0, $t0, $a4",
"add.d $t1, $t1, $a5",
"add.d $t2, $t2, $a6",
"add.d $t3, $t3, $a7",
"ld.d $a4, $a0, 32", // e
"ld.d $a5, $a0, 40", // f
"ld.d $a6, $a0, 48", // g
"ld.d $a7, $a0, 56", // h
"add.d $t4, $t4, $a4",
"add.d $t5, $t5, $a5",
"add.d $t6, $t6, $a6",
"add.d $t7, $t7, $a7",
// Save updated state
"st.d $t0, $a0, 0",
"st.d $t1, $a0, 8",
"st.d $t2, $a0, 16",
"st.d $t3, $a0, 24",
"st.d $t4, $a0, 32",
"st.d $t5, $a0, 40",
"st.d $t6, $a0, 48",
"st.d $t7, $a0, 56",
// Looping over blocks
"addi.d $a1, $a1, 128",
"addi.d $a2, $a2, -1",
"bnez $a2, 42b",
// Restore stack register
"addi.d $sp, $sp, 128",
in("$a0") state,
inout("$a1") blocks.as_ptr() => _,
inout("$a2") blocks.len() => _,
in("$a3") crate::consts::K64.as_ptr(),
// Clobbers
out("$a4") _,
out("$a5") _,
out("$a6") _,
out("$a7") _,
out("$t0") _,
out("$t1") _,
out("$t2") _,
out("$t3") _,
out("$t4") _,
out("$t5") _,
out("$t6") _,
out("$t7") _,
options(preserves_flags),
);
}
}
+52
View File
@@ -0,0 +1,52 @@
#[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64")))]
compile_error!("riscv-zknh backend can be used only on riscv32 and riscv64 target arches");
mod utils;
cfg_if::cfg_if! {
if #[cfg(sha2_backend_riscv_zknh = "compact")] {
mod compact;
pub(super) use compact::compress;
} else {
mod unroll;
pub(super) use unroll::compress;
}
}
#[cfg(target_arch = "riscv64")]
use core::arch::riscv64::{sha512sig0, sha512sig1, sha512sum0, sha512sum1};
#[cfg(target_arch = "riscv32")]
use core::arch::riscv32::*;
#[cfg(target_arch = "riscv32")]
#[target_feature(enable = "zknh")]
fn sha512sum0(x: u64) -> u64 {
let a = sha512sum0r((x >> 32) as u32, x as u32);
let b = sha512sum0r(x as u32, (x >> 32) as u32);
((a as u64) << 32) | (b as u64)
}
#[cfg(target_arch = "riscv32")]
#[target_feature(enable = "zknh")]
fn sha512sum1(x: u64) -> u64 {
let a = sha512sum1r((x >> 32) as u32, x as u32);
let b = sha512sum1r(x as u32, (x >> 32) as u32);
((a as u64) << 32) | (b as u64)
}
#[cfg(target_arch = "riscv32")]
#[target_feature(enable = "zknh")]
fn sha512sig0(x: u64) -> u64 {
let a = sha512sig0h((x >> 32) as u32, x as u32);
let b = sha512sig0l(x as u32, (x >> 32) as u32);
((a as u64) << 32) | (b as u64)
}
#[cfg(target_arch = "riscv32")]
#[target_feature(enable = "zknh")]
fn sha512sig1(x: u64) -> u64 {
let a = sha512sig1h((x >> 32) as u32, x as u32);
let b = sha512sig1l(x as u32, (x >> 32) as u32);
((a as u64) << 32) | (b as u64)
}
@@ -0,0 +1,67 @@
use super::{sha512sig0, sha512sig1, sha512sum0, sha512sum1};
use crate::consts::K64;
#[target_feature(enable = "zknh")]
pub(in super::super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks.iter().map(super::utils::load_block) {
compress_block(state, block);
}
}
#[target_feature(enable = "zknh")]
fn compress_block(state: &mut [u64; 8], mut block: [u64; 16]) {
let mut s = *state;
for r in 0..80 {
round(&mut s, &block, r);
if r < 64 {
schedule(&mut block, r)
}
}
for i in 0..8 {
state[i] = state[i].wrapping_add(s[i]);
}
}
#[target_feature(enable = "zknh")]
fn schedule(block: &mut [u64; 16], r: usize) {
block[r % 16] = block[r % 16]
.wrapping_add(sha512sig1(block[(r + 14) % 16]))
.wrapping_add(block[(r + 9) % 16])
.wrapping_add(sha512sig0(block[(r + 1) % 16]));
}
#[target_feature(enable = "zknh")]
fn round(state: &mut [u64; 8], block: &[u64; 16], r: usize) {
let n = K64.len() - r;
#[allow(clippy::identity_op)]
let a = (n + 0) % 8;
let b = (n + 1) % 8;
let c = (n + 2) % 8;
let d = (n + 3) % 8;
let e = (n + 4) % 8;
let f = (n + 5) % 8;
let g = (n + 6) % 8;
let h = (n + 7) % 8;
state[h] = state[h]
.wrapping_add(sha512sum1(state[e]))
.wrapping_add(ch(state[e], state[f], state[g]))
.wrapping_add(K64[r])
.wrapping_add(block[r % 16]);
state[d] = state[d].wrapping_add(state[h]);
state[h] = state[h]
.wrapping_add(sha512sum0(state[a]))
.wrapping_add(maj(state[a], state[b], state[c]))
}
#[inline(always)]
fn ch(x: u64, y: u64, z: u64) -> u64 {
(x & y) ^ (!x & z)
}
#[inline(always)]
fn maj(x: u64, y: u64, z: u64) -> u64 {
(x & y) ^ (x & z) ^ (y & z)
}
@@ -0,0 +1,101 @@
use super::{sha512sig0, sha512sig1, sha512sum0, sha512sum1};
use crate::consts::K64;
#[target_feature(enable = "zknh")]
pub(in super::super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks.iter().map(super::utils::load_block) {
compress_block(state, block);
}
}
#[target_feature(enable = "zknh")]
fn compress_block(state: &mut [u64; 8], mut block: [u64; 16]) {
let s = &mut state.clone();
let b = &mut block;
for i in 0..4 {
let k = &K64[16 * i..];
round_schedule::<0>(s, b, k);
round_schedule::<1>(s, b, k);
round_schedule::<2>(s, b, k);
round_schedule::<3>(s, b, k);
round_schedule::<4>(s, b, k);
round_schedule::<5>(s, b, k);
round_schedule::<6>(s, b, k);
round_schedule::<7>(s, b, k);
round_schedule::<8>(s, b, k);
round_schedule::<9>(s, b, k);
round_schedule::<10>(s, b, k);
round_schedule::<11>(s, b, k);
round_schedule::<12>(s, b, k);
round_schedule::<13>(s, b, k);
round_schedule::<14>(s, b, k);
round_schedule::<15>(s, b, k);
}
let k = &K64[64..];
round::<0>(s, b, k);
round::<1>(s, b, k);
round::<2>(s, b, k);
round::<3>(s, b, k);
round::<4>(s, b, k);
round::<5>(s, b, k);
round::<6>(s, b, k);
round::<7>(s, b, k);
round::<8>(s, b, k);
round::<9>(s, b, k);
round::<10>(s, b, k);
round::<11>(s, b, k);
round::<12>(s, b, k);
round::<13>(s, b, k);
round::<14>(s, b, k);
round::<15>(s, b, k);
for i in 0..8 {
state[i] = state[i].wrapping_add(s[i]);
}
}
#[target_feature(enable = "zknh")]
fn round_schedule<const R: usize>(state: &mut [u64; 8], block: &mut [u64; 16], k: &[u64]) {
round::<R>(state, block, k);
block[R] = block[R]
.wrapping_add(sha512sig1(block[(R + 14) % 16]))
.wrapping_add(block[(R + 9) % 16])
.wrapping_add(sha512sig0(block[(R + 1) % 16]));
}
#[target_feature(enable = "zknh")]
fn round<const R: usize>(state: &mut [u64; 8], block: &[u64; 16], k: &[u64]) {
let n = K64.len() - R;
#[allow(clippy::identity_op)]
let a = (n + 0) % 8;
let b = (n + 1) % 8;
let c = (n + 2) % 8;
let d = (n + 3) % 8;
let e = (n + 4) % 8;
let f = (n + 5) % 8;
let g = (n + 6) % 8;
let h = (n + 7) % 8;
state[h] = state[h]
.wrapping_add(sha512sum1(state[e]))
.wrapping_add(ch(state[e], state[f], state[g]))
.wrapping_add(super::utils::opaque_load::<R>(k))
.wrapping_add(block[R]);
state[d] = state[d].wrapping_add(state[h]);
state[h] = state[h]
.wrapping_add(sha512sum0(state[a]))
.wrapping_add(maj(state[a], state[b], state[c]))
}
#[inline(always)]
fn ch(x: u64, y: u64, z: u64) -> u64 {
(x & y) ^ (!x & z)
}
#[inline(always)]
fn maj(x: u64, y: u64, z: u64) -> u64 {
(x & y) ^ (x & z) ^ (y & z)
}
+163
View File
@@ -0,0 +1,163 @@
use core::{arch::asm, ptr};
#[inline(always)]
pub(super) fn load_block(block: &[u8; 128]) -> [u64; 16] {
if block.as_ptr().cast::<usize>().is_aligned() {
load_aligned_block(block)
} else {
load_unaligned_block(block)
}
}
#[cfg(target_arch = "riscv32")]
fn load_aligned_block(block: &[u8; 128]) -> [u64; 16] {
let p: *const [u32; 32] = block.as_ptr().cast();
debug_assert!(p.is_aligned());
let block = unsafe { &*p };
let mut res = [0u64; 16];
for i in 0..16 {
let a = block[2 * i].to_be() as u64;
let b = block[2 * i + 1].to_be() as u64;
res[i] = (a << 32) | b;
}
res
}
#[cfg(target_arch = "riscv64")]
fn load_aligned_block(block: &[u8; 128]) -> [u64; 16] {
let block_ptr: *const u64 = block.as_ptr().cast();
debug_assert!(block_ptr.is_aligned());
let mut res = [0u64; 16];
for i in 0..16 {
let val = unsafe { ptr::read(block_ptr.add(i)) };
res[i] = val.to_be();
}
res
}
#[cfg(target_arch = "riscv32")]
fn load_unaligned_block(block: &[u8; 128]) -> [u64; 16] {
let offset = (block.as_ptr() as usize) % align_of::<u32>();
debug_assert_ne!(offset, 0);
let off1 = (8 * offset) % 32;
let off2 = (32 - off1) % 32;
let bp: *const u32 = block.as_ptr().wrapping_sub(offset).cast();
let mut left: u32;
let mut block32 = [0u32; 32];
unsafe {
asm!(
"lw {left}, 0({bp})", // left = unsafe { ptr::read(bp) };
"srl {left}, {left}, {off1}", // left >>= off1;
bp = in(reg) bp,
off1 = in(reg) off1,
left = out(reg) left,
options(pure, nostack, readonly, preserves_flags),
);
}
for i in 0..31 {
let right = unsafe { ptr::read(bp.add(1 + i)) };
block32[i] = left | (right << off2);
left = right >> off1;
}
let right: u32;
unsafe {
asm!(
"lw {right}, 32 * 4({bp})", // right = ptr::read(bp.add(32));
"sll {right}, {right}, {off2}", // right <<= off2;
bp = in(reg) bp,
off2 = in(reg) off2,
right = out(reg) right,
options(pure, nostack, readonly, preserves_flags),
);
}
block32[31] = left | right;
let mut block64 = [0u64; 16];
for i in 0..16 {
let a = block32[2 * i].to_be() as u64;
let b = block32[2 * i + 1].to_be() as u64;
block64[i] = (a << 32) | b;
}
block64
}
#[cfg(target_arch = "riscv64")]
fn load_unaligned_block(block: &[u8; 128]) -> [u64; 16] {
let offset = (block.as_ptr() as usize) % align_of::<u64>();
debug_assert_ne!(offset, 0);
let off1 = (8 * offset) % 64;
let off2 = (64 - off1) % 64;
let bp: *const u64 = block.as_ptr().wrapping_sub(offset).cast();
let mut left: u64;
let mut res = [0u64; 16];
unsafe {
asm!(
"ld {left}, 0({bp})", // left = unsafe { ptr::read(bp) };
"srl {left}, {left}, {off1}", // left >>= off1;
bp = in(reg) bp,
off1 = in(reg) off1,
left = out(reg) left,
options(pure, nostack, readonly, preserves_flags),
);
}
for i in 0..15 {
let right = unsafe { ptr::read(bp.add(1 + i)) };
res[i] = (left | (right << off2)).to_be();
left = right >> off1;
}
let right: u64;
unsafe {
asm!(
"ld {right}, 16 * 8({bp})", // right = ptr::read(bp.add(16));
"sll {right}, {right}, {off2}", // right <<= off2;
bp = in(reg) bp,
off2 = in(reg) off2,
right = out(reg) right,
options(pure, nostack, readonly, preserves_flags),
);
}
res[15] = (left | right).to_be();
res
}
/// This function returns `k[R]`, but prevents compiler from inlining the indexed value
#[cfg(not(sha2_backend_riscv_zknh = "compact"))]
pub(super) fn opaque_load<const R: usize>(k: &[u64]) -> u64 {
use core::arch::asm;
assert!(R < k.len());
#[cfg(target_arch = "riscv64")]
unsafe {
let dst;
asm!(
"ld {dst}, 8 * {R}({k})",
R = const R,
k = in(reg) k.as_ptr(),
dst = out(reg) dst,
options(pure, readonly, nostack, preserves_flags),
);
dst
}
#[cfg(target_arch = "riscv32")]
unsafe {
let [hi, lo]: [u32; 2];
asm!(
"lw {lo}, 8 * {R}({k})",
"lw {hi}, 8 * {R} + 4({k})",
R = const R,
k = in(reg) k.as_ptr(),
lo = out(reg) lo,
hi = out(reg) hi,
options(pure, readonly, nostack, preserves_flags),
);
((hi as u64) << 32) | (lo as u64)
}
}
+17
View File
@@ -0,0 +1,17 @@
cfg_if::cfg_if! {
if #[cfg(sha2_backend_soft = "compact")] {
mod compact;
pub(super) use compact::compress;
} else {
mod unroll;
pub(super) use unroll::compress;
}
}
#[inline(always)]
fn to_u64s(block: &[u8; 128]) -> [u64; 16] {
core::array::from_fn(|i| {
let chunk = block[8 * i..][..8].try_into().unwrap();
u64::from_be_bytes(chunk)
})
}
+56
View File
@@ -0,0 +1,56 @@
use crate::consts::K64;
fn compress_u64(state: &mut [u64; 8], block: [u64; 16]) {
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
let mut w = [0; 80];
w[..16].copy_from_slice(&block);
for i in 16..80 {
let w15 = w[i - 15];
let s0 = (w15.rotate_right(1)) ^ (w15.rotate_right(8)) ^ (w15 >> 7);
let w2 = w[i - 2];
let s1 = (w2.rotate_right(19)) ^ (w2.rotate_right(61)) ^ (w2 >> 6);
w[i] = w[i - 16]
.wrapping_add(s0)
.wrapping_add(w[i - 7])
.wrapping_add(s1);
}
for i in 0..80 {
let s1 = e.rotate_right(14) ^ e.rotate_right(18) ^ e.rotate_right(41);
let ch = (e & f) ^ ((!e) & g);
let t1 = s1
.wrapping_add(ch)
.wrapping_add(K64[i])
.wrapping_add(w[i])
.wrapping_add(h);
let s0 = a.rotate_right(28) ^ a.rotate_right(34) ^ a.rotate_right(39);
let maj = (a & b) ^ (a & c) ^ (b & c);
let t2 = s0.wrapping_add(maj);
h = g;
g = f;
f = e;
e = d.wrapping_add(t1);
d = c;
c = b;
b = a;
a = t1.wrapping_add(t2);
}
state[0] = state[0].wrapping_add(a);
state[1] = state[1].wrapping_add(b);
state[2] = state[2].wrapping_add(c);
state[3] = state[3].wrapping_add(d);
state[4] = state[4].wrapping_add(e);
state[5] = state[5].wrapping_add(f);
state[6] = state[6].wrapping_add(g);
state[7] = state[7].wrapping_add(h);
}
pub(in super::super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks.iter() {
compress_u64(state, super::to_u64s(block));
}
}
+94
View File
@@ -0,0 +1,94 @@
use crate::consts::K64;
#[rustfmt::skip]
macro_rules! repeat80 {
($i:ident, $b:block) => {
let $i = 0; $b; let $i = 1; $b; let $i = 2; $b; let $i = 3; $b;
let $i = 4; $b; let $i = 5; $b; let $i = 6; $b; let $i = 7; $b;
let $i = 8; $b; let $i = 9; $b; let $i = 10; $b; let $i = 11; $b;
let $i = 12; $b; let $i = 13; $b; let $i = 14; $b; let $i = 15; $b;
let $i = 16; $b; let $i = 17; $b; let $i = 18; $b; let $i = 19; $b;
let $i = 20; $b; let $i = 21; $b; let $i = 22; $b; let $i = 23; $b;
let $i = 24; $b; let $i = 25; $b; let $i = 26; $b; let $i = 27; $b;
let $i = 28; $b; let $i = 29; $b; let $i = 30; $b; let $i = 31; $b;
let $i = 32; $b; let $i = 33; $b; let $i = 34; $b; let $i = 35; $b;
let $i = 36; $b; let $i = 37; $b; let $i = 38; $b; let $i = 39; $b;
let $i = 40; $b; let $i = 41; $b; let $i = 42; $b; let $i = 43; $b;
let $i = 44; $b; let $i = 45; $b; let $i = 46; $b; let $i = 47; $b;
let $i = 48; $b; let $i = 49; $b; let $i = 50; $b; let $i = 51; $b;
let $i = 52; $b; let $i = 53; $b; let $i = 54; $b; let $i = 55; $b;
let $i = 56; $b; let $i = 57; $b; let $i = 58; $b; let $i = 59; $b;
let $i = 60; $b; let $i = 61; $b; let $i = 62; $b; let $i = 63; $b;
let $i = 64; $b; let $i = 65; $b; let $i = 66; $b; let $i = 67; $b;
let $i = 68; $b; let $i = 69; $b; let $i = 70; $b; let $i = 71; $b;
let $i = 72; $b; let $i = 73; $b; let $i = 74; $b; let $i = 75; $b;
let $i = 76; $b; let $i = 77; $b; let $i = 78; $b; let $i = 79; $b;
};
}
/// Read round constant
fn rk(i: usize) -> u64 {
// `read_volatile` forces the compiler to read round constants from the static
// instead of inlining them, which improves codegen and performance
unsafe {
let p = K64.as_ptr().add(i);
core::ptr::read_volatile(p)
}
}
/// Process a block with the SHA-512 algorithm.
fn compress_block(state: &mut [u64; 8], block: &[u8; 128]) {
let mut block = super::to_u64s(block);
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
repeat80!(i, {
let w = if i < 16 {
block[i]
} else {
let w15 = block[(i - 15) % 16];
let s0 = (w15.rotate_right(1)) ^ (w15.rotate_right(8)) ^ (w15 >> 7);
let w2 = block[(i - 2) % 16];
let s1 = (w2.rotate_right(19)) ^ (w2.rotate_right(61)) ^ (w2 >> 6);
block[i % 16] = block[i % 16]
.wrapping_add(s0)
.wrapping_add(block[(i - 7) % 16])
.wrapping_add(s1);
block[i % 16]
};
let s1 = e.rotate_right(14) ^ e.rotate_right(18) ^ e.rotate_right(41);
let ch = (e & f) ^ ((!e) & g);
let t1 = s1
.wrapping_add(ch)
.wrapping_add(rk(i))
.wrapping_add(w)
.wrapping_add(h);
let s0 = a.rotate_right(28) ^ a.rotate_right(34) ^ a.rotate_right(39);
let maj = (a & b) ^ (a & c) ^ (b & c);
let t2 = s0.wrapping_add(maj);
h = g;
g = f;
f = e;
e = d.wrapping_add(t1);
d = c;
c = b;
b = a;
a = t1.wrapping_add(t2);
});
state[0] = state[0].wrapping_add(a);
state[1] = state[1].wrapping_add(b);
state[2] = state[2].wrapping_add(c);
state[3] = state[3].wrapping_add(d);
state[4] = state[4].wrapping_add(e);
state[5] = state[5].wrapping_add(f);
state[6] = state[6].wrapping_add(g);
state[7] = state[7].wrapping_add(h);
}
pub(in super::super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks {
compress_block(state, block);
}
}
+177
View File
@@ -0,0 +1,177 @@
#![allow(clippy::many_single_char_names, unsafe_op_in_unsafe_fn)]
#[cfg(not(target_arch = "wasm32"))]
compile_error!("wasm32-simd128 backend can be used only on wasm32 target arches");
#[cfg(not(target_feature = "simd128"))]
compile_error!("wasm32-simd128 backend requires simd128 target feature");
use core::arch::wasm32::*;
use core::mem::size_of;
use crate::consts::K64;
pub(super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
let mut ms = [u64x2(0, 0); 8];
let mut x = [u64x2(0, 0); 8];
for block in blocks {
unsafe {
let mut current_state = *state;
load_data(&mut x, &mut ms, block.as_ptr().cast());
rounds_0_63(&mut current_state, &mut x, &mut ms);
rounds_64_79(&mut current_state, &ms);
accumulate_state(state, &current_state);
}
}
}
#[inline(always)]
unsafe fn load_data(x: &mut [v128; 8], ms: &mut MsgSchedule, data: *const v128) {
macro_rules! unrolled_iterations {
($($i:literal),*) => {$(
x[$i] = v128_load(data.add($i).cast());
x[$i] = i8x16_shuffle::<7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8>(x[$i], x[$i]);
let y = i64x2_add(
x[$i],
v128_load(K64.as_ptr().add(2 * $i).cast()),
);
ms[$i] = y;
)*};
}
unrolled_iterations!(0, 1, 2, 3, 4, 5, 6, 7);
}
#[inline(always)]
unsafe fn rounds_0_63(current_state: &mut State, x: &mut [v128; 8], ms: &mut MsgSchedule) {
let mut k64_idx: usize = SHA512_BLOCK_WORDS_NUM;
for _ in 0..4 {
for j in 0..8 {
let k64 = v128_load(K64.as_ptr().add(k64_idx).cast());
let y = sha512_update_x(x, k64);
{
let ms = ms[j];
sha_round(current_state, u64x2_extract_lane::<0>(ms));
sha_round(current_state, u64x2_extract_lane::<1>(ms));
}
ms[j] = y;
k64_idx += 2;
}
}
}
#[inline(always)]
fn rounds_64_79(current_state: &mut State, ms: &MsgSchedule) {
for j in 0..8 {
let ms = ms[j];
sha_round(current_state, u64x2_extract_lane::<0>(ms));
sha_round(current_state, u64x2_extract_lane::<1>(ms));
}
}
#[inline(always)]
fn sha_round(s: &mut State, x: u64) {
macro_rules! big_sigma0 {
($a:expr) => {
$a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39)
};
}
macro_rules! big_sigma1 {
($a:expr) => {
$a.rotate_right(14) ^ $a.rotate_right(18) ^ $a.rotate_right(41)
};
}
macro_rules! bool3ary_202 {
($a:expr, $b:expr, $c:expr) => {
$c ^ ($a & ($b ^ $c))
};
} // Choose, MD5F, SHA1C
macro_rules! bool3ary_232 {
($a:expr, $b:expr, $c:expr) => {
($a & $b) ^ ($a & $c) ^ ($b & $c)
};
} // Majority, SHA1M
macro_rules! rotate_state {
($s:ident) => {{
let tmp = $s[7];
$s[7] = $s[6];
$s[6] = $s[5];
$s[5] = $s[4];
$s[4] = $s[3];
$s[3] = $s[2];
$s[2] = $s[1];
$s[1] = $s[0];
$s[0] = tmp;
}};
}
let t = x
.wrapping_add(s[7])
.wrapping_add(big_sigma1!(s[4]))
.wrapping_add(bool3ary_202!(s[4], s[5], s[6]));
s[7] = t
.wrapping_add(big_sigma0!(s[0]))
.wrapping_add(bool3ary_232!(s[0], s[1], s[2]));
s[3] = s[3].wrapping_add(t);
rotate_state!(s);
}
#[inline(always)]
fn accumulate_state(dst: &mut State, src: &State) {
for i in 0..SHA512_HASH_WORDS_NUM {
dst[i] = dst[i].wrapping_add(src[i]);
}
}
#[inline(always)]
unsafe fn sha512_update_x(x: &mut [v128; 8], k64: v128) -> v128 {
let mut t0 = u64x2_shuffle::<1, 2>(x[0], x[1]);
let mut t3 = u64x2_shuffle::<1, 2>(x[4], x[5]);
let mut t2 = u64x2_shr(t0, 1);
x[0] = i64x2_add(x[0], t3);
t3 = u64x2_shr(t0, 7);
let mut t1 = u64x2_shl(t0, 64 - 8);
t0 = v128_xor(t3, t2);
t2 = u64x2_shr(t2, 8 - 1);
t0 = v128_xor(t0, t1);
t1 = u64x2_shl(t1, 8 - 1);
t0 = v128_xor(t0, t2);
t0 = v128_xor(t0, t1);
t3 = u64x2_shr(x[7], 6);
t2 = u64x2_shl(x[7], 64 - 61);
x[0] = i64x2_add(x[0], t0);
t1 = u64x2_shr(x[7], 19);
t3 = v128_xor(t3, t2);
t2 = u64x2_shl(t2, 61 - 19);
t3 = v128_xor(t3, t1);
t1 = u64x2_shr(t1, 61 - 19);
t3 = v128_xor(t3, t2);
t3 = v128_xor(t3, t1);
x[0] = i64x2_add(x[0], t3);
let temp = x[0];
x[0] = x[1];
x[1] = x[2];
x[2] = x[3];
x[3] = x[4];
x[4] = x[5];
x[5] = x[6];
x[6] = x[7];
x[7] = temp;
i64x2_add(x[7], k64)
}
type State = [u64; SHA512_HASH_WORDS_NUM];
type MsgSchedule = [v128; SHA512_BLOCK_WORDS_NUM / 2];
const SHA512_BLOCK_BYTE_LEN: usize = 128;
const SHA512_HASH_BYTE_LEN: usize = 64;
const SHA512_HASH_WORDS_NUM: usize = SHA512_HASH_BYTE_LEN / size_of::<u64>();
const SHA512_BLOCK_WORDS_NUM: usize = SHA512_BLOCK_BYTE_LEN / size_of::<u64>();
+346
View File
@@ -0,0 +1,346 @@
//! SHA-512 `x86`/`x86_64` backend
#![allow(clippy::many_single_char_names, unsafe_op_in_unsafe_fn)]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
compile_error!("x86-avx2 backend can be used only on x86 and x86_64 target arches");
use core::mem::size_of;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
use crate::consts::K64;
#[target_feature(enable = "avx2")]
pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
let mut start_block = 0;
if blocks.len() & 0b1 != 0 {
sha512_compress_x86_64_avx(state, &blocks[0]);
start_block += 1;
}
let mut ms: MsgSchedule = [_mm_setzero_si128(); 8];
let mut t2: RoundStates = [_mm_setzero_si128(); 40];
let mut x = [_mm256_setzero_si256(); 8];
for i in (start_block..blocks.len()).step_by(2) {
load_data_avx2(&mut x, &mut ms, &mut t2, blocks.as_ptr().add(i).cast());
// First block
let mut current_state = *state;
rounds_0_63_avx2(&mut current_state, &mut x, &mut ms, &mut t2);
rounds_64_79(&mut current_state, &ms);
accumulate_state(state, &current_state);
// Second block
current_state = *state;
process_second_block(&mut current_state, &t2);
accumulate_state(state, &current_state);
}
}
#[inline(always)]
unsafe fn sha512_compress_x86_64_avx(state: &mut [u64; 8], block: &[u8; 128]) {
let mut ms = [_mm_setzero_si128(); 8];
let mut x = [_mm_setzero_si128(); 8];
// Reduced to single iteration
let mut current_state = *state;
load_data_avx(&mut x, &mut ms, block.as_ptr().cast());
rounds_0_63_avx(&mut current_state, &mut x, &mut ms);
rounds_64_79(&mut current_state, &ms);
accumulate_state(state, &current_state);
}
#[inline(always)]
unsafe fn load_data_avx(x: &mut [__m128i; 8], ms: &mut MsgSchedule, data: *const __m128i) {
#[allow(non_snake_case)]
let MASK = _mm_setr_epi32(0x04050607, 0x00010203, 0x0c0d0e0f, 0x08090a0b);
macro_rules! unrolled_iterations {
($($i:literal),*) => {$(
x[$i] = _mm_loadu_si128(data.add($i).cast());
x[$i] = _mm_shuffle_epi8(x[$i], MASK);
let y = _mm_add_epi64(
x[$i],
_mm_loadu_si128(K64.as_ptr().add(2 * $i).cast()),
);
ms[$i] = y;
)*};
}
unrolled_iterations!(0, 1, 2, 3, 4, 5, 6, 7);
}
#[inline(always)]
unsafe fn load_data_avx2(
x: &mut [__m256i; 8],
ms: &mut MsgSchedule,
t2: &mut RoundStates,
data: *const __m128i,
) {
#[allow(non_snake_case)]
let MASK = _mm256_set_epi64x(
0x0809_0A0B_0C0D_0E0F_i64,
0x0001_0203_0405_0607_i64,
0x0809_0A0B_0C0D_0E0F_i64,
0x0001_0203_0405_0607_i64,
);
macro_rules! unrolled_iterations {
($($i:literal),*) => {$(
x[$i] = _mm256_insertf128_si256(x[$i], _mm_loadu_si128(data.add(8 + $i).cast()), 1);
x[$i] = _mm256_insertf128_si256(x[$i], _mm_loadu_si128(data.add($i).cast()), 0);
x[$i] = _mm256_shuffle_epi8(x[$i], MASK);
let t = _mm_loadu_si128(K64.as_ptr().add($i * 2).cast());
let y = _mm256_add_epi64(x[$i], _mm256_set_m128i(t, t));
ms[$i] = _mm256_extracti128_si256(y, 0);
t2[$i] = _mm256_extracti128_si256(y, 1);
)*};
}
unrolled_iterations!(0, 1, 2, 3, 4, 5, 6, 7);
}
#[inline(always)]
unsafe fn rounds_0_63_avx(current_state: &mut State, x: &mut [__m128i; 8], ms: &mut MsgSchedule) {
let mut k64_idx: usize = SHA512_BLOCK_WORDS_NUM;
for _ in 0..4 {
for j in 0..8 {
let k64 = _mm_loadu_si128(K64.as_ptr().add(k64_idx).cast());
let y = sha512_update_x_avx(x, k64);
{
let ms = cast_ms(ms);
sha_round(current_state, ms[2 * j]);
sha_round(current_state, ms[2 * j + 1]);
}
ms[j] = y;
k64_idx += 2;
}
}
}
#[inline(always)]
unsafe fn rounds_0_63_avx2(
current_state: &mut State,
x: &mut [__m256i; 8],
ms: &mut MsgSchedule,
t2: &mut RoundStates,
) {
let mut k64x4_idx: usize = SHA512_BLOCK_WORDS_NUM;
for i in 1..5 {
for j in 0..8 {
let t = _mm_loadu_si128(K64.as_ptr().add(k64x4_idx).cast());
let y = sha512_update_x_avx2(x, _mm256_set_m128i(t, t));
{
let ms = cast_ms(ms);
sha_round(current_state, ms[2 * j]);
sha_round(current_state, ms[2 * j + 1]);
}
ms[j] = _mm256_extracti128_si256(y, 0);
t2[8 * i + j] = _mm256_extracti128_si256(y, 1);
k64x4_idx += 2;
}
}
}
#[inline(always)]
fn rounds_64_79(current_state: &mut State, ms: &MsgSchedule) {
let ms = cast_ms(ms);
for i in 64..80 {
sha_round(current_state, ms[i & 0xf]);
}
}
#[inline(always)]
fn process_second_block(current_state: &mut State, t2: &RoundStates) {
for t2 in cast_rs(t2).iter() {
sha_round(current_state, *t2);
}
}
#[inline(always)]
fn sha_round(s: &mut State, x: u64) {
macro_rules! big_sigma0 {
($a:expr) => {
$a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39)
};
}
macro_rules! big_sigma1 {
($a:expr) => {
$a.rotate_right(14) ^ $a.rotate_right(18) ^ $a.rotate_right(41)
};
}
macro_rules! bool3ary_202 {
($a:expr, $b:expr, $c:expr) => {
$c ^ ($a & ($b ^ $c))
};
} // Choose, MD5F, SHA1C
macro_rules! bool3ary_232 {
($a:expr, $b:expr, $c:expr) => {
($a & $b) ^ ($a & $c) ^ ($b & $c)
};
} // Majority, SHA1M
macro_rules! rotate_state {
($s:ident) => {{
let tmp = $s[7];
$s[7] = $s[6];
$s[6] = $s[5];
$s[5] = $s[4];
$s[4] = $s[3];
$s[3] = $s[2];
$s[2] = $s[1];
$s[1] = $s[0];
$s[0] = tmp;
}};
}
let t = x
.wrapping_add(s[7])
.wrapping_add(big_sigma1!(s[4]))
.wrapping_add(bool3ary_202!(s[4], s[5], s[6]));
s[7] = t
.wrapping_add(big_sigma0!(s[0]))
.wrapping_add(bool3ary_232!(s[0], s[1], s[2]));
s[3] = s[3].wrapping_add(t);
rotate_state!(s);
}
#[inline(always)]
fn accumulate_state(dst: &mut State, src: &State) {
for i in 0..SHA512_HASH_WORDS_NUM {
dst[i] = dst[i].wrapping_add(src[i]);
}
}
macro_rules! fn_sha512_update_x {
($name:ident, $ty:ident, {
ADD64 = $ADD64:ident,
ALIGNR8 = $ALIGNR8:ident,
SRL64 = $SRL64:ident,
SLL64 = $SLL64:ident,
XOR = $XOR:ident,
}) => {
unsafe fn $name(x: &mut [$ty; 8], k64: $ty) -> $ty {
// q[2:1]
let mut t0 = $ALIGNR8(x[1], x[0], 8);
// q[10:9]
let mut t3 = $ALIGNR8(x[5], x[4], 8);
// q[2:1] >> s0[0]
let mut t2 = $SRL64(t0, 1);
// q[1:0] + q[10:9]
x[0] = $ADD64(x[0], t3);
// q[2:1] >> s0[2]
t3 = $SRL64(t0, 7);
// q[2:1] << (64 - s0[1])
let mut t1 = $SLL64(t0, 64 - 8);
// (q[2:1] >> s0[2]) ^
// (q[2:1] >> s0[0])
t0 = $XOR(t3, t2);
// q[2:1] >> s0[1]
t2 = $SRL64(t2, 8 - 1);
// (q[2:1] >> s0[2]) ^
// (q[2:1] >> s0[0]) ^
// q[2:1] << (64 - s0[1])
t0 = $XOR(t0, t1);
// q[2:1] << (64 - s0[0])
t1 = $SLL64(t1, 8 - 1);
// sigma1(q[2:1])
t0 = $XOR(t0, t2);
t0 = $XOR(t0, t1);
// q[15:14] >> s1[2]
t3 = $SRL64(x[7], 6);
// q[15:14] >> (64 - s1[1])
t2 = $SLL64(x[7], 64 - 61);
// q[1:0] + sigma0(q[2:1])
x[0] = $ADD64(x[0], t0);
// q[15:14] >> s1[0]
t1 = $SRL64(x[7], 19);
// q[15:14] >> s1[2] ^
// q[15:14] >> (64 - s1[1])
t3 = $XOR(t3, t2);
// q[15:14] >> (64 - s1[0])
t2 = $SLL64(t2, 61 - 19);
// q[15:14] >> s1[2] ^
// q[15:14] >> (64 - s1[1] ^
// q[15:14] >> s1[0]
t3 = $XOR(t3, t1);
// q[15:14] >> s1[1]
t1 = $SRL64(t1, 61 - 19);
// sigma1(q[15:14])
t3 = $XOR(t3, t2);
t3 = $XOR(t3, t1);
// q[1:0] + q[10:9] + sigma1(q[15:14]) + sigma0(q[2:1])
x[0] = $ADD64(x[0], t3);
// rotate
let temp = x[0];
x[0] = x[1];
x[1] = x[2];
x[2] = x[3];
x[3] = x[4];
x[4] = x[5];
x[5] = x[6];
x[6] = x[7];
x[7] = temp;
$ADD64(x[7], k64)
}
};
}
fn_sha512_update_x!(sha512_update_x_avx, __m128i, {
ADD64 = _mm_add_epi64,
ALIGNR8 = _mm_alignr_epi8,
SRL64 = _mm_srli_epi64,
SLL64 = _mm_slli_epi64,
XOR = _mm_xor_si128,
});
fn_sha512_update_x!(sha512_update_x_avx2, __m256i, {
ADD64 = _mm256_add_epi64,
ALIGNR8 = _mm256_alignr_epi8,
SRL64 = _mm256_srli_epi64,
SLL64 = _mm256_slli_epi64,
XOR = _mm256_xor_si256,
});
#[inline(always)]
fn cast_ms(ms: &MsgSchedule) -> &[u64; SHA512_BLOCK_WORDS_NUM] {
unsafe { &*(ms.as_ptr().cast()) }
}
#[inline(always)]
fn cast_rs(rs: &RoundStates) -> &[u64; SHA512_ROUNDS_NUM] {
unsafe { &*(rs.as_ptr().cast()) }
}
type State = [u64; SHA512_HASH_WORDS_NUM];
type MsgSchedule = [__m128i; SHA512_BLOCK_WORDS_NUM / 2];
type RoundStates = [__m128i; SHA512_ROUNDS_NUM / 2];
const SHA512_BLOCK_BYTE_LEN: usize = 128;
const SHA512_ROUNDS_NUM: usize = 80;
const SHA512_HASH_BYTE_LEN: usize = 64;
const SHA512_HASH_WORDS_NUM: usize = SHA512_HASH_BYTE_LEN / size_of::<u64>();
const SHA512_BLOCK_WORDS_NUM: usize = SHA512_BLOCK_BYTE_LEN / size_of::<u64>();
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+43
View File
@@ -0,0 +1,43 @@
use digest::{
dev::{feed_rand_16mib, fixed_reset_test},
hash_serialization_test, new_test,
};
use hex_literal::hex;
use sha2::{Digest, Sha224, Sha256, Sha384, Sha512, Sha512_224, Sha512_256};
new_test!(sha224_kat, Sha224, fixed_reset_test);
new_test!(sha256_kat, Sha256, fixed_reset_test);
new_test!(sha384_kat, Sha384, fixed_reset_test);
new_test!(sha512_kat, Sha512, fixed_reset_test);
new_test!(sha512_224_kat, Sha512_224, fixed_reset_test);
new_test!(sha512_256_kat, Sha512_256, fixed_reset_test);
hash_serialization_test!(sha224_serialization, Sha224);
hash_serialization_test!(sha256_serialization, Sha256);
hash_serialization_test!(sha384_serialization, Sha384);
hash_serialization_test!(sha512_serialization, Sha512);
hash_serialization_test!(sha512_224_serialization, Sha512_224);
hash_serialization_test!(sha512_256_serialization, Sha512_256);
#[test]
fn sha256_rand() {
let mut h = Sha256::new();
feed_rand_16mib(&mut h);
assert_eq!(
h.finalize(),
hex!("45f51fead87328fe837a86f4f1ac0eb15116ab1473adc0423ef86c62eb2320c7"),
);
}
#[test]
fn sha512_rand() {
let mut h = Sha512::new();
feed_rand_16mib(&mut h);
assert_eq!(
h.finalize(),
hex!(
"9084d75a7c0721541d737b6171eb465dc9ba08a119a182a8508484aa27a176cd"
"e7c2103b108393eb024493ced4aac56be6f57222cac41b801f11494886264997"
),
);
}