Skip to content

Commit c604770

Browse files
authored
yescrypt: enable clippy::cast_possible_truncation (#704)
Fixes offenses, either by masking to show that truncation is expected, using `TryFrom` and `?` to convert to `Error::Internal` if the conversion fails, or starting with a narrower type and widening instead of trying to go the other direction.
1 parent 3534279 commit c604770

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

yescrypt/src/encoding.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ static ITOA64: &[u8] = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq
1212
/// Reverse lookup table for (s)crypt-flavored Base64 alphabet.
1313
pub const ATOI64: [u8; 128] = {
1414
let mut tbl = [0xFFu8; 128]; // use 0xFF as a placeholder for invalid chars
15-
let mut i = 0;
15+
let mut i = 0u8;
1616
while i < 64 {
17-
tbl[ITOA64[i] as usize] = i as u8;
17+
tbl[ITOA64[i as usize] as usize] = i;
1818
i += 1;
1919
}
2020
tbl
@@ -47,7 +47,7 @@ pub(crate) fn decode64<'o>(src: &str, dst: &'o mut [u8]) -> Result<&'o [u8]> {
4747
}
4848

4949
while pos < dst.len() {
50-
dst[pos] = value as u8;
50+
dst[pos] = (value & 0xFF) as u8;
5151
pos += 1;
5252
value >>= 8;
5353
bits -= 8;

yescrypt/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![deny(unsafe_code)]
99
#![warn(
1010
clippy::cast_lossless,
11-
// TODO: clippy::cast_possible_truncation,
11+
clippy::cast_possible_truncation,
1212
clippy::cast_possible_wrap,
1313
clippy::cast_precision_loss,
1414
clippy::cast_sign_loss,
@@ -172,9 +172,10 @@ pub fn yescrypt_verify(passwd: &[u8], hash: &str) -> Result<()> {
172172

173173
/// yescrypt Key Derivation Function (KDF)
174174
pub fn yescrypt_kdf(passwd: &[u8], salt: &[u8], params: &Params, out: &mut [u8]) -> Result<()> {
175-
// Perform conditional pre-hashing
176175
let mut passwd = passwd;
177176
let mut dk = [0u8; 32];
177+
178+
// Conditionally perform pre-hashing
178179
if params.mode.is_rw()
179180
&& params.p >= 1
180181
&& params.n / u64::from(params.p) >= 0x100
@@ -185,7 +186,7 @@ pub fn yescrypt_kdf(passwd: &[u8], salt: &[u8], params: &Params, out: &mut [u8])
185186
prehash_params.t = 0;
186187
yescrypt_kdf_body(passwd, salt, &prehash_params, true, &mut dk)?;
187188

188-
// Use derived key as the "password" for the subsequent step
189+
// Use derived key as the "password" for the subsequent step when pre-hashing
189190
passwd = &dk;
190191
}
191192

@@ -215,7 +216,7 @@ fn yescrypt_kdf_body(
215216
return Err(Error::Params);
216217
}
217218

218-
let mut v = vec![0; 32 * (r as usize) * (n as usize)];
219+
let mut v = vec![0; 32 * (r as usize) * usize::try_from(n)?];
219220
let mut b = vec![0; 32 * (r as usize) * (p as usize)];
220221
let mut xy = vec![0; 64 * (r as usize)];
221222

yescrypt/src/params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Params {
8282

8383
if mode.is_rw()
8484
&& (n / u64::from(p) <= 1
85-
|| r < RMIN as u32
85+
|| r < RMIN
8686
|| u64::from(p) > u64::MAX / (3 * (1 << 8) * 2 * 8)
8787
|| u64::from(p) > u64::MAX / (size_of::<PwxformCtx<'_>>() as u64))
8888
{

yescrypt/src/pwxform.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
salsa20,
99
util::{slice_as_chunks_mut, xor},
1010
};
11+
use core::mem;
1112

1213
/// Number of 64-bit lanes per "simple SIMD" lane (requiring only arithmetic and bitwise operations
1314
/// on its 64-bit elements). Must be a power of 2.
@@ -31,7 +32,9 @@ const PWXWORDS: usize = PWXBYTES / size_of::<u32>();
3132
const SMASK: usize = ((1 << SWIDTH) - 1) * PWXSIMPLE * 8;
3233
pub(crate) const SBYTES: usize = 3 * (1 << SWIDTH) * PWXSIMPLE * 8;
3334
pub(crate) const SWORDS: usize = SBYTES / size_of::<u32>();
34-
pub(crate) const RMIN: usize = PWXBYTES.div_ceil(128);
35+
36+
#[allow(clippy::cast_possible_truncation)]
37+
pub(crate) const RMIN: u32 = PWXBYTES.div_ceil(128) as u32;
3538

3639
/// Parallel wide transformation (pwxform) context.
3740
pub(crate) struct PwxformCtx<'a> {
@@ -128,23 +131,25 @@ impl PwxformCtx<'_> {
128131
x = x.wrapping_add(s0);
129132
x ^= s1;
130133

131-
xptr[j][k][0] = x as u32;
132-
xptr[j][k][1] = (x >> 32) as u32;
134+
let x_lo = (x & 0xFFFF_FFFF) as u32;
135+
let x_hi = ((x >> 32) & 0xFFFF_FFFF) as u32;
136+
xptr[j][k][0] = x_lo;
137+
xptr[j][k][1] = x_hi;
133138

134139
// 8: if (i != 0) and (i != PWXrounds - 1)
135140
if i != 0 && i != (PWXROUNDS - 1) {
136141
// 9: S2_w <-- B_j
137-
self.s2[w][0] = x as u32;
138-
self.s2[w][1] = (x >> 32) as u32;
142+
self.s2[w][0] = x_lo;
143+
self.s2[w][1] = x_hi;
139144
w += 1;
140145
}
141146
}
142147
}
143148
}
144149

145150
// 14: (S0, S1, S2) <-- (S2, S0, S1)
146-
core::mem::swap(&mut self.s0, &mut self.s2);
147-
core::mem::swap(&mut self.s1, &mut self.s2);
151+
mem::swap(&mut self.s0, &mut self.s2);
152+
mem::swap(&mut self.s1, &mut self.s2);
148153

149154
// 15: w <-- w mod 2^Swidth
150155
self.w = w & ((1 << SWIDTH) * PWXSIMPLE - 1);

yescrypt/src/smix.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub(crate) fn smix(
6969
// 10: Nloop_rw <-- Nloop_rw + (Nloop_rw mod 2)
7070
nloop_rw += 1;
7171
nloop_rw &= !1; // round up to even
72-
let mut vchunk = 0;
7372

7473
// S_n = [S_i for i in 0..p]
7574
let mut sn = if mode.is_rw() {
@@ -80,6 +79,8 @@ pub(crate) fn smix(
8079
let mut ctxs = Vec::with_capacity(sn.len());
8180
let mut sn = sn.iter_mut();
8281

82+
let mut vchunk = 0;
83+
8384
// 11: for i = 0 to p - 1 do
8485
// 12: u <-- in
8586
#[allow(clippy::needless_range_loop)]
@@ -95,7 +96,7 @@ pub(crate) fn smix(
9596
};
9697

9798
let bs = &mut b[(i * s)..];
98-
let vp = &mut v[vchunk as usize * s..];
99+
let vp = &mut v[(usize::try_from(vchunk)? * s)..];
99100

100101
// 17: if YESCRYPT_RW flag is set
101102
let mut ctx_i = if mode.is_rw() {
@@ -216,7 +217,7 @@ fn smix1(
216217
// 2: for i = 0 to N - 1 do
217218
for i in 0..n {
218219
// 3: V_i <-- X
219-
v[i as usize * s..][..s].copy_from_slice(x);
220+
v[(usize::try_from(i)? * s)..][..s].copy_from_slice(x);
220221
if mode.is_rw() && i > 1 {
221222
let n = prev_power_of_two(i);
222223
let j = usize::try_from((integerify(x, r) & (n - 1)) + (i - n))?;

0 commit comments

Comments
 (0)