hashes: disable SHA256 ARM SIMD on big-endian targets
What changed, and why it matters
This commit fixes a bug in the SHA-256 hashing code used by the rust-bitcoin library on big-endian ARM (AArch64) systems. The optimized ARM code always swapped bytes assuming the computer stores data in little-endian order. On a big-endian ARM system, that extra swap scrambled the input and produced completely wrong hash outputs. The fix disables the ARM-specific fast path on big-endian targets and falls back to the slower but correct generic code. Wrong SHA-256 hashes could break Bitcoin address generation, transaction validation, or any security check that depends on a correct hash.
Treat this as a correctness/security fix and include it in the next maintenance release. Users building rust-bitcoin for big-endian AArch64 (e.g., some embedded/network appliances) should upgrade. Add CI or cross-compilation tests for aarch64_be to prevent regression, and consider a runtime self-test that compares the optimized and fallback SHA-256 outputs.
Security signals we found
Incorrect cryptographic output on a supported architecture (big-endian AArch64)
Hash mismatch could violate Bitcoin protocol invariants
Conditional-compilation bug in architecture-specific optimized path
No input validation or runtime detection of endianness before the fix
Evidence from the diff
The AArch64 SHA2 backend in hashes/src/sha256/crypto/arm_sha2.rs loads 32-bit message words with vld1q_u32 and then unconditionally byte-reverses them with vrev32q_u8 to obtain little-endian memory order. On big-endian aarch64_be, vld1q_u32 already returns big-endian-ordered u32 values, so the additional reverse corrupts every message word. The patch gates compilation and runtime use of arm_sha2 on target_endian = “little” for both the single-block SHA256 path and the two-way SHA256d path, forcing big-endian targets to use the endian-correct software fallback. The bug affected SHA256, SHA256d, and SHA256t on big-endian AArch64 when the sha2 CPU feature was present and either the std or cpufeatures feature was enabled.
Changed components
hashes/src/sha256/crypto/arm_sha2.rshashes/src/sha256/crypto/mod.rsSHA256, SHA256d, SHA256t hash engines on big-endian AArch64Inspect captured patch +9 / −1
### hashes/src/sha256/crypto/arm_sha2.rs
@@ -66,7 +66,9 @@ pub(super) unsafe fn process_blocks(state: &mut [u32; 8], blocks: &[u8]) {
msg2 = vld1q_u32(blocks.as_ptr().add(block_offset + 32).cast::<u32>());
msg3 = vld1q_u32(blocks.as_ptr().add(block_offset + 48).cast::<u32>());
- // Reverse for little endian
+ // Reverse for little endian. This module is only compiled for
+ // little-endian targets; on big-endian targets `vld1q_u32` already
+ // yields the correct big-endian message words.
msg0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(msg0)));
msg1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(msg1)));
msg2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(msg2)));
### hashes/src/sha256/crypto/mod.rs
@@ -6,6 +6,7 @@
#![allow(clippy::many_single_char_names)]
#[cfg(target_arch = "aarch64")]
+#[cfg(target_endian = "little")]
#[cfg(any(feature = "cpufeatures", feature = "std"))]
mod arm_sha2;
@@ -27,6 +28,7 @@ use super::{HashEngine, Midstate, BLOCK_SIZE};
#[cfg(feature = "cpufeatures")]
#[cfg(target_arch = "aarch64")]
+#[cfg(target_endian = "little")]
// cpufeatures crate internally uses deprecated `u8::max_value()`.
// See: https://docs.rs/cpufeatures/0.2.17/src/cpufeatures/lib.rs.html#161
#[allow(deprecated)]
@@ -327,6 +329,7 @@ impl HashEngine {
#[cfg(feature = "std")]
#[cfg(target_arch = "aarch64")]
+ #[cfg(target_endian = "little")]
{
if std::arch::is_aarch64_feature_detected!("sha2") {
unsafe { arm_sha2::process_blocks(state, blocks) };
@@ -336,6 +339,7 @@ impl HashEngine {
#[cfg(feature = "cpufeatures")]
#[cfg(target_arch = "aarch64")]
+ #[cfg(target_endian = "little")]
{
if cpuid_sha256_aarch64::get() {
unsafe { arm_sha2::process_blocks(state, blocks) };
@@ -386,6 +390,7 @@ impl HashEngine {
// 2-way ARM SHA2
#[cfg(feature = "std")]
#[cfg(target_arch = "aarch64")]
+ #[cfg(target_endian = "little")]
{
if std::arch::is_aarch64_feature_detected!("sha2") {
while count - i >= 2 {
@@ -399,6 +404,7 @@ impl HashEngine {
#[cfg(feature = "cpufeatures")]
#[cfg(target_arch = "aarch64")]
+ #[cfg(target_endian = "little")]
{
if cpuid_sha256_aarch64::get() {
while count - i >= 2 {Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.