Skip to content

Commit 63bced6

Browse files
authored
yescrypt: replace reshape_block with (slice_)as_chunks_mut (#705)
Replaces an unsafe pointer cast with (our polyfill for) the `core` method `as_chunks_mut` (MSRV 1.88, but we ship our own version for now to support Rust 1.85).
1 parent c604770 commit 63bced6

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

yescrypt/src/pwxform.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,17 @@ impl PwxformCtx<'_> {
101101

102102
/// Transform the provided block using the provided S-boxes.
103103
fn pwxform(&mut self, b: &mut [u32; 16]) {
104-
let xptr = reshape_block(b);
104+
// TODO(tarcieri): use upstream `[T]::as_chunks_mut` when MSRV is 1.88
105+
let b = slice_as_chunks_mut::<_, 2>(slice_as_chunks_mut::<_, PWXSIMPLE>(b).0).0;
105106
let mut w = self.w;
106107

107108
// 1: for i = 0 to PWXrounds - 1 do
108109
for i in 0..PWXROUNDS {
109110
// 2: for j = 0 to PWXgather - 1 do
110111
#[allow(clippy::needless_range_loop)]
111112
for j in 0..PWXGATHER {
112-
let mut xl: u32 = xptr[j][0][0];
113-
let mut xh: u32 = xptr[j][0][1];
113+
let mut xl: u32 = b[j][0][0];
114+
let mut xh: u32 = b[j][0][1];
114115

115116
// 3: p0 <-- (lo(B_{j,0}) & Smask) / (PWXsimple * 8)
116117
let p0 = &self.s0[(xl as usize & SMASK) / 8..];
@@ -124,17 +125,17 @@ impl PwxformCtx<'_> {
124125
let s0 = (u64::from(p0[k][1]) << 32).wrapping_add(u64::from(p0[k][0]));
125126
let s1 = (u64::from(p1[k][1]) << 32).wrapping_add(u64::from(p1[k][0]));
126127

127-
xl = xptr[j][k][0];
128-
xh = xptr[j][k][1];
128+
xl = b[j][k][0];
129+
xh = b[j][k][1];
129130

130131
let mut x = u64::from(xh).wrapping_mul(u64::from(xl));
131132
x = x.wrapping_add(s0);
132133
x ^= s1;
133134

134135
let x_lo = (x & 0xFFFF_FFFF) as u32;
135136
let x_hi = ((x >> 32) & 0xFFFF_FFFF) as u32;
136-
xptr[j][k][0] = x_lo;
137-
xptr[j][k][1] = x_hi;
137+
b[j][k][0] = x_lo;
138+
b[j][k][1] = x_hi;
138139

139140
// 8: if (i != 0) and (i != PWXrounds - 1)
140141
if i != 0 && i != (PWXROUNDS - 1) {
@@ -155,15 +156,3 @@ impl PwxformCtx<'_> {
155156
self.w = w & ((1 << SWIDTH) * PWXSIMPLE - 1);
156157
}
157158
}
158-
159-
#[allow(unsafe_code)]
160-
pub(crate) fn reshape_block(b: &mut [u32; 16]) -> &mut [[[u32; PWXSIMPLE]; 2]; 4] {
161-
const {
162-
assert!(
163-
size_of::<[u32; 16]>() == size_of::<[[[u32; PWXSIMPLE]; 2]; 4]>(),
164-
"PWXSIMPLE is incorrectly sized"
165-
);
166-
}
167-
168-
unsafe { &mut *core::ptr::from_mut(b).cast() }
169-
}

0 commit comments

Comments
 (0)