Skip to content

Commit 6075989

Browse files
authored
sha2: fix loading of round constants on AArch64 backends (#922)
The previous code was compiling into expected assembly, but was not correct under the Stacked Borrows model.
1 parent eadfd90 commit 6075989

3 files changed

Lines changed: 44 additions & 34 deletions

File tree

‎sha2/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Removed workaround for unaligned loads in `riscv-zknh` backend ([#879])
1111
- `riscv-zknh` no longer requires `zbkb` (or `zbb`) target feature ([#879])
1212

13+
### Fixed
14+
- Loading of round constants on AArch64 backends to satisfy the Stacked Borrows model ([#922])
15+
1316
[#879]: https://github.com/RustCrypto/hashes/pull/879
17+
[#922]: https://github.com/RustCrypto/hashes/pull/922
1418

1519
## 0.11.0 (2026-03-25)
1620
### Changed

‎sha2/src/sha256/aarch64_sha2.rs‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
//!
33
//! Implementation adapted from mbedtls.
44
#![allow(unsafe_op_in_unsafe_fn)]
5+
use core::arch::aarch64::*;
56

67
#[cfg(not(target_arch = "aarch64"))]
78
compile_error!("aarch64-sha2 backend can be used only aarch64 target arches");
89

9-
use crate::consts::K32;
10-
use core::arch::aarch64::*;
11-
1210
#[target_feature(enable = "sha2")]
1311
pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
14-
// SAFETY: Requires the sha2 feature.
15-
1612
// Load state into vectors.
1713
let mut abcd = vld1q_u32(state[0..4].as_ptr());
1814
let mut efgh = vld1q_u32(state[4..8].as_ptr());
@@ -30,54 +26,54 @@ pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
3026
let mut s3 = vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(block[48..64].as_ptr())));
3127

3228
// Rounds 0 to 3
33-
let mut tmp = vaddq_u32(s0, vld1q_u32(&K32[0]));
29+
let mut tmp = vaddq_u32(s0, rk(0));
3430
let mut abcd_prev = abcd;
3531
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
3632
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
3733

3834
// Rounds 4 to 7
39-
tmp = vaddq_u32(s1, vld1q_u32(&K32[4]));
35+
tmp = vaddq_u32(s1, rk(4));
4036
abcd_prev = abcd;
4137
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
4238
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
4339

4440
// Rounds 8 to 11
45-
tmp = vaddq_u32(s2, vld1q_u32(&K32[8]));
41+
tmp = vaddq_u32(s2, rk(8));
4642
abcd_prev = abcd;
4743
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
4844
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
4945

5046
// Rounds 12 to 15
51-
tmp = vaddq_u32(s3, vld1q_u32(&K32[12]));
47+
tmp = vaddq_u32(s3, rk(12));
5248
abcd_prev = abcd;
5349
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
5450
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
5551

5652
for t in (16..64).step_by(16) {
5753
// Rounds t to t + 3
5854
s0 = vsha256su1q_u32(vsha256su0q_u32(s0, s1), s2, s3);
59-
tmp = vaddq_u32(s0, vld1q_u32(&K32[t]));
55+
tmp = vaddq_u32(s0, rk(t));
6056
abcd_prev = abcd;
6157
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
6258
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
6359

6460
// Rounds t + 4 to t + 7
6561
s1 = vsha256su1q_u32(vsha256su0q_u32(s1, s2), s3, s0);
66-
tmp = vaddq_u32(s1, vld1q_u32(&K32[t + 4]));
62+
tmp = vaddq_u32(s1, rk(t + 4));
6763
abcd_prev = abcd;
6864
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
6965
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
7066

7167
// Rounds t + 8 to t + 11
7268
s2 = vsha256su1q_u32(vsha256su0q_u32(s2, s3), s0, s1);
73-
tmp = vaddq_u32(s2, vld1q_u32(&K32[t + 8]));
69+
tmp = vaddq_u32(s2, rk(t + 8));
7470
abcd_prev = abcd;
7571
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
7672
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
7773

7874
// Rounds t + 12 to t + 15
7975
s3 = vsha256su1q_u32(vsha256su0q_u32(s3, s0), s1, s2);
80-
tmp = vaddq_u32(s3, vld1q_u32(&K32[t + 12]));
76+
tmp = vaddq_u32(s3, rk(t + 12));
8177
abcd_prev = abcd;
8278
abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
8379
efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
@@ -92,3 +88,10 @@ pub(super) unsafe fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
9288
vst1q_u32(state[0..4].as_mut_ptr(), abcd);
9389
vst1q_u32(state[4..8].as_mut_ptr(), efgh);
9490
}
91+
92+
#[target_feature(enable = "neon")]
93+
unsafe fn rk(i: usize) -> uint32x4_t {
94+
use crate::consts::K32;
95+
assert!(i + 4 <= K32.len());
96+
vld1q_u32(K32.as_ptr().add(i))
97+
}

‎sha2/src/sha512/aarch64_sha3.rs‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
// Implementation adapted from mbedtls.
22
#![allow(unsafe_op_in_unsafe_fn)]
3+
use core::arch::aarch64::*;
34

45
#[cfg(not(target_arch = "aarch64"))]
56
compile_error!("aarch64-sha3 backend can be used only aarch64 target arches");
67

7-
use crate::consts::K64;
8-
use core::arch::aarch64::*;
9-
108
#[target_feature(enable = "sha3")]
119
pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
12-
// SAFETY: Requires the sha3 feature.
13-
1410
// Load state into vectors.
1511
let mut ab = vld1q_u64(state[0..2].as_ptr());
1612
let mut cd = vld1q_u64(state[2..4].as_ptr());
@@ -36,56 +32,56 @@ pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
3632
let mut s7 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(block[112..128].as_ptr())));
3733

3834
// Rounds 0 and 1
39-
let mut initial_sum = vaddq_u64(s0, vld1q_u64(&K64[0]));
35+
let mut initial_sum = vaddq_u64(s0, rk(0));
4036
let mut sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
4137
let mut intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
4238
gh = vsha512h2q_u64(intermed, cd, ab);
4339
cd = vaddq_u64(cd, intermed);
4440

4541
// Rounds 2 and 3
46-
initial_sum = vaddq_u64(s1, vld1q_u64(&K64[2]));
42+
initial_sum = vaddq_u64(s1, rk(2));
4743
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
4844
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
4945
ef = vsha512h2q_u64(intermed, ab, gh);
5046
ab = vaddq_u64(ab, intermed);
5147

5248
// Rounds 4 and 5
53-
initial_sum = vaddq_u64(s2, vld1q_u64(&K64[4]));
49+
initial_sum = vaddq_u64(s2, rk(4));
5450
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
5551
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
5652
cd = vsha512h2q_u64(intermed, gh, ef);
5753
gh = vaddq_u64(gh, intermed);
5854

5955
// Rounds 6 and 7
60-
initial_sum = vaddq_u64(s3, vld1q_u64(&K64[6]));
56+
initial_sum = vaddq_u64(s3, rk(6));
6157
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
6258
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
6359
ab = vsha512h2q_u64(intermed, ef, cd);
6460
ef = vaddq_u64(ef, intermed);
6561

6662
// Rounds 8 and 9
67-
initial_sum = vaddq_u64(s4, vld1q_u64(&K64[8]));
63+
initial_sum = vaddq_u64(s4, rk(8));
6864
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
6965
intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
7066
gh = vsha512h2q_u64(intermed, cd, ab);
7167
cd = vaddq_u64(cd, intermed);
7268

7369
// Rounds 10 and 11
74-
initial_sum = vaddq_u64(s5, vld1q_u64(&K64[10]));
70+
initial_sum = vaddq_u64(s5, rk(10));
7571
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
7672
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
7773
ef = vsha512h2q_u64(intermed, ab, gh);
7874
ab = vaddq_u64(ab, intermed);
7975

8076
// Rounds 12 and 13
81-
initial_sum = vaddq_u64(s6, vld1q_u64(&K64[12]));
77+
initial_sum = vaddq_u64(s6, rk(12));
8278
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
8379
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
8480
cd = vsha512h2q_u64(intermed, gh, ef);
8581
gh = vaddq_u64(gh, intermed);
8682

8783
// Rounds 14 and 15
88-
initial_sum = vaddq_u64(s7, vld1q_u64(&K64[14]));
84+
initial_sum = vaddq_u64(s7, rk(14));
8985
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
9086
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
9187
ab = vsha512h2q_u64(intermed, ef, cd);
@@ -94,63 +90,63 @@ pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
9490
for t in (16..80).step_by(16) {
9591
// Rounds t and t + 1
9692
s0 = vsha512su1q_u64(vsha512su0q_u64(s0, s1), s7, vextq_u64(s4, s5, 1));
97-
initial_sum = vaddq_u64(s0, vld1q_u64(&K64[t]));
93+
initial_sum = vaddq_u64(s0, rk(t));
9894
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
9995
intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
10096
gh = vsha512h2q_u64(intermed, cd, ab);
10197
cd = vaddq_u64(cd, intermed);
10298

10399
// Rounds t + 2 and t + 3
104100
s1 = vsha512su1q_u64(vsha512su0q_u64(s1, s2), s0, vextq_u64(s5, s6, 1));
105-
initial_sum = vaddq_u64(s1, vld1q_u64(&K64[t + 2]));
101+
initial_sum = vaddq_u64(s1, rk(t + 2));
106102
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
107103
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
108104
ef = vsha512h2q_u64(intermed, ab, gh);
109105
ab = vaddq_u64(ab, intermed);
110106

111107
// Rounds t + 4 and t + 5
112108
s2 = vsha512su1q_u64(vsha512su0q_u64(s2, s3), s1, vextq_u64(s6, s7, 1));
113-
initial_sum = vaddq_u64(s2, vld1q_u64(&K64[t + 4]));
109+
initial_sum = vaddq_u64(s2, rk(t + 4));
114110
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
115111
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
116112
cd = vsha512h2q_u64(intermed, gh, ef);
117113
gh = vaddq_u64(gh, intermed);
118114

119115
// Rounds t + 6 and t + 7
120116
s3 = vsha512su1q_u64(vsha512su0q_u64(s3, s4), s2, vextq_u64(s7, s0, 1));
121-
initial_sum = vaddq_u64(s3, vld1q_u64(&K64[t + 6]));
117+
initial_sum = vaddq_u64(s3, rk(t + 6));
122118
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
123119
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
124120
ab = vsha512h2q_u64(intermed, ef, cd);
125121
ef = vaddq_u64(ef, intermed);
126122

127123
// Rounds t + 8 and t + 9
128124
s4 = vsha512su1q_u64(vsha512su0q_u64(s4, s5), s3, vextq_u64(s0, s1, 1));
129-
initial_sum = vaddq_u64(s4, vld1q_u64(&K64[t + 8]));
125+
initial_sum = vaddq_u64(s4, rk(t + 8));
130126
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
131127
intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
132128
gh = vsha512h2q_u64(intermed, cd, ab);
133129
cd = vaddq_u64(cd, intermed);
134130

135131
// Rounds t + 10 and t + 11
136132
s5 = vsha512su1q_u64(vsha512su0q_u64(s5, s6), s4, vextq_u64(s1, s2, 1));
137-
initial_sum = vaddq_u64(s5, vld1q_u64(&K64[t + 10]));
133+
initial_sum = vaddq_u64(s5, rk(t + 10));
138134
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
139135
intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
140136
ef = vsha512h2q_u64(intermed, ab, gh);
141137
ab = vaddq_u64(ab, intermed);
142138

143139
// Rounds t + 12 and t + 13
144140
s6 = vsha512su1q_u64(vsha512su0q_u64(s6, s7), s5, vextq_u64(s2, s3, 1));
145-
initial_sum = vaddq_u64(s6, vld1q_u64(&K64[t + 12]));
141+
initial_sum = vaddq_u64(s6, rk(t + 12));
146142
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
147143
intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
148144
cd = vsha512h2q_u64(intermed, gh, ef);
149145
gh = vaddq_u64(gh, intermed);
150146

151147
// Rounds t + 14 and t + 15
152148
s7 = vsha512su1q_u64(vsha512su0q_u64(s7, s0), s6, vextq_u64(s3, s4, 1));
153-
initial_sum = vaddq_u64(s7, vld1q_u64(&K64[t + 14]));
149+
initial_sum = vaddq_u64(s7, rk(t + 14));
154150
sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
155151
intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
156152
ab = vsha512h2q_u64(intermed, ef, cd);
@@ -170,3 +166,10 @@ pub(super) unsafe fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
170166
vst1q_u64(state[4..6].as_mut_ptr(), ef);
171167
vst1q_u64(state[6..8].as_mut_ptr(), gh);
172168
}
169+
170+
#[target_feature(enable = "neon")]
171+
unsafe fn rk(i: usize) -> uint64x2_t {
172+
use crate::consts::K64;
173+
assert!(i + 2 <= K64.len());
174+
vld1q_u64(K64.as_ptr().add(i))
175+
}

0 commit comments

Comments
 (0)