Merge rust-bitcoin/rust-bitcoin#7061: 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 on 64-bit ARM processors. On rare big-endian ARM systems, the code was accidentally reversing the byte order of data before hashing, producing completely wrong SHA-256 results. The fix disables the fast ARM-specific hashing path on big-endian ARM and falls back to the normal, correct software implementation. Wrong SHA-256 hashes could break digital signatures, transaction IDs, or any security check that relies on correct hashing.
Apply the patch. For users running rust-bitcoin on big-endian AArch64 systems, upgrade to a version containing this fix and re-verify any SHA-256-dependent outputs (addresses, transaction IDs, signatures) generated before the fix. Consider adding a CI or contrib test for aarch64_be with QEMU to prevent regression, as the project currently lacks prebuilt std for that target.
Security signals we found
Incorrect cryptographic output on big-endian AArch64
SHA-256/SHA-256d/SHA-256t digests silently wrong when SHA2 CPU feature detected
Endianness mismatch in SIMD message-word loading
Fix gates ARM SIMD backend to little-endian targets only
Fallback to verified software implementation on big-endian targets
Evidence from the diff
The AArch64 SHA2 intrinsic backend in hashes/src/sha256/crypto/arm_sha2.rs loads message words with vld1q_u32 and then unconditionally applies vrev32q_u8 to swap bytes for little-endian memory layout. On big-endian aarch64_be, vld1q_u32 already produces the correct big-endian word order, so the extra reversal corrupts every message word. The patch gates the arm_sha2 module, its cpuid helper, and both the one-block SHA256 and two-way SHA256d dispatch sites on target_endian = “little”. Big-endian targets now use the endian-correct software fallback that decodes words with u32::from_be_bytes. The PR author reproduced the issue in QEMU and confirmed the corrupted and fixed digests against the known SHA256(“abc”) answer.
Changed components
hashes/src/sha256/crypto/arm_sha2.rshashes/src/sha256/crypto/mod.rsSHA-256 hashing on big-endian AArch64 with SHA2 CPU featureSHA-256d and SHA-256t variants 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.