Skip to content

Commit 6a9be52

Browse files
authored
yescrypt: make all smix args into slices (#667)
1 parent a91db4e commit 6a9be52

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

yescrypt/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::{
4949
sha256::{HMAC_SHA256_Buf, PBKDF2_SHA256, SHA256_Buf},
5050
};
5151
use alloc::{boxed::Box, vec, vec::Vec};
52-
use core::ptr;
52+
use core::{ptr, slice};
5353

5454
#[derive(Clone)]
5555
struct Local {
@@ -232,10 +232,10 @@ unsafe fn yescrypt_kdf_body(
232232
p,
233233
t,
234234
flags,
235-
v.as_mut_ptr(),
236-
xy.as_mut_ptr(),
237-
pwxform_ctx.as_mut_slice(),
238-
sha256.as_mut_ptr() as *mut u8,
235+
v,
236+
&mut xy,
237+
&mut pwxform_ctx,
238+
slice::from_raw_parts_mut(sha256.as_mut_ptr() as *mut u8, 32),
239239
);
240240
} else {
241241
// 2: for i = 0 to p - 1 do
@@ -248,10 +248,10 @@ unsafe fn yescrypt_kdf_body(
248248
1,
249249
t,
250250
flags,
251-
v.as_mut_ptr(),
252-
xy.as_mut_ptr(),
251+
v,
252+
&mut xy,
253+
&mut [],
253254
&mut [],
254-
ptr::null_mut(),
255255
);
256256
}
257257
}

yescrypt/src/pwxform.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ impl<'a> PwxformCtx<'a> {
6161
/// Compute `B = BlockMix_pwxform{salsa20/2, ctx, r}(B)`.
6262
///
6363
/// The input `B` must be 128r bytes in length.
64-
pub(crate) unsafe fn blockmix_pwxform(&mut self, b: *mut u32, r: usize) {
64+
pub(crate) unsafe fn blockmix_pwxform(&mut self, b: &mut [u32], r: usize) {
65+
let b = b.as_mut_ptr();
6566
let mut x = [0u32; 16];
6667

6768
// Convert 128-byte blocks to PWXbytes blocks

yescrypt/src/smix.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub(crate) unsafe fn smix(
2222
p: u32,
2323
t: u32,
2424
flags: Flags,
25-
v: *mut u32,
26-
xy: *mut u32,
25+
v: &mut [u32],
26+
xy: &mut [u32],
2727
ctx: &mut [PwxformCtx<'_>],
28-
passwd: *mut u8,
28+
passwd: &mut [u8],
2929
) {
3030
let s = 32 * r;
3131

@@ -86,7 +86,7 @@ pub(crate) unsafe fn smix(
8686
};
8787

8888
let bs = &mut b[(i * s)..];
89-
let vp = v.add(vchunk as usize * s);
89+
let vp = v.as_mut_ptr().add(vchunk as usize * s);
9090

9191
// 17: if YESCRYPT_RW flag is set
9292
let mut ctx_i = if flags.contains(Flags::RW) {
@@ -111,9 +111,9 @@ pub(crate) unsafe fn smix(
111111
HMAC_SHA256_Buf(
112112
bs[(s - 16)..].as_ptr() as *const u8,
113113
64,
114-
passwd as *const u8,
114+
passwd.as_ptr(),
115115
32,
116-
passwd,
116+
passwd.as_mut_ptr(),
117117
);
118118
}
119119

@@ -156,7 +156,7 @@ pub(crate) unsafe fn smix(
156156
n,
157157
nloop_all - nloop_rw,
158158
flags & !Flags::RW,
159-
v,
159+
v.as_mut_ptr(),
160160
xy,
161161
&mut ctx_i,
162162
);
@@ -173,41 +173,42 @@ unsafe fn smix1(
173173
n: u64,
174174
flags: Flags,
175175
v: *mut u32,
176-
xy: *mut u32,
176+
xy: &mut [u32],
177177
ctx: &mut Option<&mut PwxformCtx<'_>>,
178178
) {
179179
let s = 32 * r;
180-
let x = xy;
181-
let y = xy.add(s);
180+
let (x, y) = xy.split_at_mut(s);
182181

183182
// 1: X <-- B
184183
for k in 0..(2 * r) {
185184
for i in 0..16 {
186-
*x.add(k * 16 + i) = u32::from_le(*b.as_mut_ptr().add((k * 16) + (i * 5 % 16)));
185+
*x.as_mut_ptr().add(k * 16 + i) =
186+
u32::from_le(*b.as_mut_ptr().add((k * 16) + (i * 5 % 16)));
187187
}
188188
}
189189

190190
// 2: for i = 0 to N - 1 do
191191
for i in 0..n {
192192
// 3: V_i <-- X
193-
blkcpy(v.add(usize::try_from(i).unwrap() * s), x, s);
193+
blkcpy(v.add(usize::try_from(i).unwrap() * s), x.as_ptr(), s);
194194
if flags.contains(Flags::RW) && i > 1 {
195195
let n = prev_power_of_two(i);
196-
let j = usize::try_from((integerify(x, r) & (n - 1)) + (i - n)).unwrap();
197-
blkxor(x, v.add(j * s), s);
196+
let j = usize::try_from((integerify(x.as_ptr(), r) & (n - 1)) + (i - n)).unwrap();
197+
blkxor(x.as_mut_ptr(), v.add(j * s), s);
198198
}
199199

200200
// 4: X <-- H(X)
201201
match ctx {
202202
Some(ctx) => ctx.blockmix_pwxform(x, r),
203-
None => salsa20::blockmix_salsa8(x, y, r),
203+
None => salsa20::blockmix_salsa8(x.as_mut_ptr(), y.as_mut_ptr(), r),
204204
}
205205
}
206206

207207
/* B' <-- X */
208208
for k in 0..(2 * r) {
209209
for i in 0..16 {
210-
*b.as_mut_ptr().add((k * 16) + ((i * 5) % 16)) = (*x.add(k * 16 + i)).to_le();
210+
*b.as_mut_ptr().add((k * 16) + ((i * 5) % 16)) =
211+
(*x.as_mut_ptr().add(k * 16 + i)).to_le();
211212
}
212213
}
213214
}
@@ -224,44 +225,44 @@ unsafe fn smix2(
224225
nloop: u64,
225226
flags: Flags,
226227
v: *mut u32,
227-
xy: *mut u32,
228+
xy: &mut [u32],
228229
ctx: &mut Option<&mut PwxformCtx<'_>>,
229230
) {
230231
let s = 32 * r;
231-
let x = xy;
232-
let y = xy.add(s);
232+
let (x, y) = xy.split_at_mut(s);
233233

234234
/* X <-- B */
235235
for k in 0..(2 * r) {
236236
for i in 0..16usize {
237-
*x.add(k * 16 + i) = u32::from_le(*b.as_mut_ptr().add((k * 16) + (i * 5 % 16)));
237+
*x.as_mut_ptr().add(k * 16 + i) =
238+
u32::from_le(*b.as_mut_ptr().add((k * 16) + (i * 5 % 16)));
238239
}
239240
}
240241

241242
// 6: for i = 0 to N - 1 do
242243
for _ in 0..nloop {
243244
// 7: j <-- Integerify(X) mod N
244-
let j = usize::try_from(integerify(x, r) & (n - 1)).unwrap();
245+
let j = usize::try_from(integerify(x.as_ptr(), r) & (n - 1)).unwrap();
245246

246247
// 8.1: X <-- X xor V_j
247-
blkxor(x, v.add(j * s), s);
248+
blkxor(x.as_mut_ptr(), v.add(j * s), s);
248249

249250
// V_j <-- X
250251
if flags.contains(Flags::RW) {
251-
blkcpy(v.add(j * s), x, s);
252+
blkcpy(v.add(j * s), x.as_mut_ptr(), s);
252253
}
253254

254255
// 8.2: X <-- H(X)
255256
match ctx {
256257
Some(ctx) => ctx.blockmix_pwxform(x, r),
257-
None => salsa20::blockmix_salsa8(x, y, r),
258+
None => salsa20::blockmix_salsa8(x.as_mut_ptr(), y.as_mut_ptr(), r),
258259
}
259260
}
260261

261262
// 10: B' <-- X
262263
for k in 0..(2 * r) {
263264
for i in 0..16 {
264-
*b.as_mut_ptr().add((k * 16) + ((i * 5) % 16)) = (*x.add(k * 16 + i)).to_le();
265+
*b.as_mut_ptr().add((k * 16) + ((i * 5) % 16)) = (*x.as_ptr().add(k * 16 + i)).to_le();
265266
}
266267
}
267268
}

0 commit comments

Comments
 (0)