Skip to content

Commit 6e6b4ce

Browse files
authored
Merge pull request #1181 from kazcw/master
chacha: safer outputting
2 parents ceb25f8 + aa5b0e0 commit 6e6b4ce

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

rand_chacha/src/chacha.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,7 @@ macro_rules! chacha_impl {
8787
type Results = Array64<u32>;
8888
#[inline]
8989
fn generate(&mut self, r: &mut Self::Results) {
90-
// Fill slice of words by writing to equivalent slice of bytes, then fixing endianness.
91-
self.state.refill4($rounds, unsafe {
92-
&mut *(&mut *r as *mut Array64<u32> as *mut [u8; 256])
93-
});
94-
for x in r.as_mut() {
95-
*x = x.to_le();
96-
}
90+
self.state.refill4($rounds, &mut r.0);
9791
}
9892
}
9993

rand_chacha/src/guts.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ppv_lite86::{dispatch, dispatch_light128};
1414
pub use ppv_lite86::Machine;
1515
use ppv_lite86::{vec128_storage, ArithOps, BitOps32, LaneWords4, MultiLane, StoreBytes, Vec4};
1616

17-
pub(crate) const BLOCK: usize = 64;
17+
pub(crate) const BLOCK: usize = 16;
1818
pub(crate) const BLOCK64: u64 = BLOCK as u64;
1919
const LOG2_BUFBLOCKS: u64 = 2;
2020
const BUFBLOCKS: u64 = 1 << LOG2_BUFBLOCKS;
@@ -81,7 +81,7 @@ impl ChaCha {
8181

8282
/// Produce 4 blocks of output, advancing the state
8383
#[inline(always)]
84-
pub fn refill4(&mut self, drounds: u32, out: &mut [u8; BUFSZ]) {
84+
pub fn refill4(&mut self, drounds: u32, out: &mut [u32; BUFSZ]) {
8585
refill_wide(self, drounds, out)
8686
}
8787

@@ -114,7 +114,7 @@ impl ChaCha {
114114
#[allow(clippy::many_single_char_names)]
115115
#[inline(always)]
116116
fn refill_wide_impl<Mach: Machine>(
117-
m: Mach, state: &mut ChaCha, drounds: u32, out: &mut [u8; BUFSZ],
117+
m: Mach, state: &mut ChaCha, drounds: u32, out: &mut [u32; BUFSZ],
118118
) {
119119
let k = m.vec([0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574]);
120120
let mut pos = state.pos64(m);
@@ -159,17 +159,26 @@ fn refill_wide_impl<Mach: Machine>(
159159
let sc = m.unpack(state.c);
160160
let sd = [m.unpack(state.d), d1, d2, d3];
161161
state.d = d4.into();
162-
let mut words = out.chunks_exact_mut(16);
163-
for ((((&a, &b), &c), &d), &sd) in a.iter().zip(&b).zip(&c).zip(&d).zip(&sd) {
164-
(a + k).write_le(words.next().unwrap());
165-
(b + sb).write_le(words.next().unwrap());
166-
(c + sc).write_le(words.next().unwrap());
167-
(d + sd).write_le(words.next().unwrap());
168-
}
162+
out[0..4].copy_from_slice(&(a[0] + k).to_lanes());
163+
out[4..8].copy_from_slice(&(b[0] + sb).to_lanes());
164+
out[8..12].copy_from_slice(&(c[0] + sc).to_lanes());
165+
out[12..16].copy_from_slice(&(d[0] + sd[0]).to_lanes());
166+
out[16..20].copy_from_slice(&(a[1] + k).to_lanes());
167+
out[20..24].copy_from_slice(&(b[1] + sb).to_lanes());
168+
out[24..28].copy_from_slice(&(c[1] + sc).to_lanes());
169+
out[28..32].copy_from_slice(&(d[1] + sd[1]).to_lanes());
170+
out[32..36].copy_from_slice(&(a[2] + k).to_lanes());
171+
out[36..40].copy_from_slice(&(b[2] + sb).to_lanes());
172+
out[40..44].copy_from_slice(&(c[2] + sc).to_lanes());
173+
out[44..48].copy_from_slice(&(d[2] + sd[2]).to_lanes());
174+
out[48..52].copy_from_slice(&(a[3] + k).to_lanes());
175+
out[52..56].copy_from_slice(&(b[3] + sb).to_lanes());
176+
out[56..60].copy_from_slice(&(c[3] + sc).to_lanes());
177+
out[60..64].copy_from_slice(&(d[3] + sd[3]).to_lanes());
169178
}
170179

171180
dispatch!(m, Mach, {
172-
fn refill_wide(state: &mut ChaCha, drounds: u32, out: &mut [u8; BUFSZ]) {
181+
fn refill_wide(state: &mut ChaCha, drounds: u32, out: &mut [u32; BUFSZ]) {
173182
refill_wide_impl(m, state, drounds, out);
174183
}
175184
});

0 commit comments

Comments
 (0)