Skip to content

Commit a5cb438

Browse files
committed
fixed endian issue for the rng regarding #447
1 parent 1c2afcc commit a5cb438

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

chacha20/src/backends/soft.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use cipher::{
1111
consts::{U1, U64},
1212
};
1313

14+
#[cfg(feature = "rng")]
15+
use crate::rng::BLOCK_WORDS;
16+
1417
pub(crate) struct Backend<'a, R: Rounds, V: Variant>(pub(crate) &'a mut ChaChaCore<R, V>);
1518

1619
#[cfg(feature = "cipher")]
@@ -45,16 +48,15 @@ impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<'_, R, V> {
4548
impl<R: Rounds, V: Variant> Backend<'_, R, V> {
4649
#[inline(always)]
4750
pub(crate) fn gen_ks_blocks(&mut self, buffer: &mut [u32; 64]) {
48-
for i in 0..4 {
51+
for block in 0..4 {
4952
let res = run_rounds::<R>(&self.0.state);
5053
let mut ctr = u64::from(self.0.state[13]) << 32 | u64::from(self.0.state[12]);
5154
ctr = ctr.wrapping_add(1);
5255
self.0.state[12] = ctr as u32;
5356
self.0.state[13] = (ctr >> 32) as u32;
5457

55-
for (word, val) in buffer[i << 4..(i + 1) << 4].iter_mut().zip(res.iter()) {
56-
*word = val.to_le();
57-
}
58+
buffer[block * BLOCK_WORDS as usize..(block + 1) * BLOCK_WORDS as usize]
59+
.copy_from_slice(&res);
5860
}
5961
}
6062
}

chacha20/src/rng.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
use cfg_if::cfg_if;
2828

2929
/// Number of 32-bit words per ChaCha block (fixed by algorithm definition).
30-
const BLOCK_WORDS: u8 = 16;
30+
pub(crate) const BLOCK_WORDS: u8 = 16;
3131

3232
/// The seed for ChaCha20. Implements ZeroizeOnDrop when the
3333
/// zeroize feature is enabled.
@@ -590,10 +590,6 @@ macro_rules! impl_chacha_rng {
590590
#[inline]
591591
fn generate(&mut self, r: &mut Self::Results) {
592592
self.0.generate(&mut r.0);
593-
#[cfg(target_endian = "big")]
594-
for word in r.0.iter_mut() {
595-
*word = word.to_le();
596-
}
597593
}
598594
}
599595
};

0 commit comments

Comments
 (0)