Skip to content

Commit a91db4e

Browse files
authored
yescrypt: use buffer slices for smix (#666)
Removes some more pointers, replacing them with slices
1 parent 6f70786 commit a91db4e

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

yescrypt/src/pwxform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const SWIDTH: usize = 8;
1717
const PWXBYTES: usize = PWXGATHER * PWXSIMPLE * 8;
1818
const PWXWORDS: usize = PWXBYTES / size_of::<u32>();
1919
const SMASK: usize = ((1 << SWIDTH) - 1) * PWXSIMPLE * 8;
20-
const SBYTES: usize = 3 * (1 << SWIDTH) * PWXSIMPLE * 8;
20+
pub(crate) const SBYTES: usize = 3 * (1 << SWIDTH) * PWXSIMPLE * 8;
2121
pub(crate) const SWORDS: usize = SBYTES / size_of::<u32>();
2222
pub(crate) const RMIN: usize = PWXBYTES.div_ceil(128);
2323

yescrypt/src/smix.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
sha256::HMAC_SHA256_Buf,
99
};
1010

11+
const SBYTES: u64 = crate::pwxform::SBYTES as u64;
12+
1113
/// Compute `B = SMix_r(B, N)`.
1214
///
1315
/// The input B must be 128rp bytes in length; the temporary storage V must be 128rN bytes in
@@ -82,21 +84,14 @@ pub(crate) unsafe fn smix(
8284
} else {
8385
n - vchunk
8486
};
85-
let bp = b.as_mut_ptr().add(i * s);
87+
88+
let bs = &mut b[(i * s)..];
8689
let vp = v.add(vchunk as usize * s);
8790

8891
// 17: if YESCRYPT_RW flag is set
8992
let mut ctx_i = if flags.contains(Flags::RW) {
9093
// 18: SMix1_1(B_i, Sbytes / 128, S_i, no flags)
91-
smix1(
92-
bp,
93-
1,
94-
3 * (1 << 8) * 2 * 8 / 128,
95-
Flags::empty(),
96-
ctx[i].s,
97-
xy,
98-
&mut None,
99-
);
94+
smix1(bs, 1, SBYTES / 128, Flags::empty(), ctx[i].s, xy, &mut None);
10095

10196
// 19: S2_i <-- S_{i,0...2^Swidth-1}
10297
ctx[i].s2 = ctx[i].s as *mut [u32; 2];
@@ -114,7 +109,7 @@ pub(crate) unsafe fn smix(
114109
if i == 0 {
115110
// 24: passwd <-- HMAC-SHA256(B_{0,2r-1}, passwd)
116111
HMAC_SHA256_Buf(
117-
bp.add(s - 16) as *const u8,
112+
bs[(s - 16)..].as_ptr() as *const u8,
118113
64,
119114
passwd as *const u8,
120115
32,
@@ -128,11 +123,11 @@ pub(crate) unsafe fn smix(
128123
};
129124

130125
// 27: SMix1_r(B_i, n, V_{u..v}, flags)
131-
smix1(bp, r, np, flags, vp, xy, &mut ctx_i);
126+
smix1(bs, r, np, flags, vp, xy, &mut ctx_i);
132127

133128
// 28: SMix2_r(B_i, p2floor(n), Nloop_rw, V_{u..v}, flags)
134129
smix2(
135-
bp,
130+
bs,
136131
r,
137132
prev_power_of_two(np),
138133
nloop_rw,
@@ -156,7 +151,7 @@ pub(crate) unsafe fn smix(
156151

157152
// 31: SMix2_r(B_i, N, Nloop_all - Nloop_rw, V, flags excluding YESCRYPT_RW)
158153
smix2(
159-
b.as_mut_ptr().add(i * s),
154+
&mut b[(i * s)..],
160155
r,
161156
n,
162157
nloop_all - nloop_rw,
@@ -173,7 +168,7 @@ pub(crate) unsafe fn smix(
173168
/// The input B must be 128r bytes in length; the temporary storage `V` must be 128rN bytes in
174169
/// length; the temporary storage `XY` must be 256r bytes in length.
175170
unsafe fn smix1(
176-
b: *mut u32,
171+
b: &mut [u32],
177172
r: usize,
178173
n: u64,
179174
flags: Flags,
@@ -188,7 +183,7 @@ unsafe fn smix1(
188183
// 1: X <-- B
189184
for k in 0..(2 * r) {
190185
for i in 0..16 {
191-
*x.add(k * 16 + i) = u32::from_le(*b.add((k * 16) + (i * 5 % 16)));
186+
*x.add(k * 16 + i) = u32::from_le(*b.as_mut_ptr().add((k * 16) + (i * 5 % 16)));
192187
}
193188
}
194189

@@ -204,15 +199,15 @@ unsafe fn smix1(
204199

205200
// 4: X <-- H(X)
206201
match ctx {
207-
Some(ctx) => PwxformCtx::blockmix_pwxform(ctx, x, r),
202+
Some(ctx) => ctx.blockmix_pwxform(x, r),
208203
None => salsa20::blockmix_salsa8(x, y, r),
209204
}
210205
}
211206

212207
/* B' <-- X */
213208
for k in 0..(2 * r) {
214209
for i in 0..16 {
215-
*b.add((k * 16) + ((i * 5) % 16)) = (*x.add(k * 16 + i)).to_le();
210+
*b.as_mut_ptr().add((k * 16) + ((i * 5) % 16)) = (*x.add(k * 16 + i)).to_le();
216211
}
217212
}
218213
}
@@ -223,7 +218,7 @@ unsafe fn smix1(
223218
/// the temporary storage XY must be 256r bytes in length. The value N must be a power of 2
224219
/// greater than 1.
225220
unsafe fn smix2(
226-
b: *mut u32,
221+
b: &mut [u32],
227222
r: usize,
228223
n: u64,
229224
nloop: u64,
@@ -239,7 +234,7 @@ unsafe fn smix2(
239234
/* X <-- B */
240235
for k in 0..(2 * r) {
241236
for i in 0..16usize {
242-
*x.add(k * 16 + i) = u32::from_le(*b.add((k * 16) + (i * 5 % 16usize)));
237+
*x.add(k * 16 + i) = u32::from_le(*b.as_mut_ptr().add((k * 16) + (i * 5 % 16)));
243238
}
244239
}
245240

@@ -258,15 +253,15 @@ unsafe fn smix2(
258253

259254
// 8.2: X <-- H(X)
260255
match ctx {
261-
Some(ctx) => PwxformCtx::blockmix_pwxform(ctx, x, r),
256+
Some(ctx) => ctx.blockmix_pwxform(x, r),
262257
None => salsa20::blockmix_salsa8(x, y, r),
263258
}
264259
}
265260

266261
// 10: B' <-- X
267262
for k in 0..(2 * r) {
268263
for i in 0..16 {
269-
*b.add((k * 16) + ((i * 5) % 16)) = (*x.add(k * 16 + i)).to_le();
264+
*b.as_mut_ptr().add((k * 16) + ((i * 5) % 16)) = (*x.add(k * 16 + i)).to_le();
270265
}
271266
}
272267
}

0 commit comments

Comments
 (0)