diff --git a/chacha20/src/backends/neon.rs b/chacha20/src/backends/neon.rs index c21f021b..a40f7e45 100644 --- a/chacha20/src/backends/neon.rs +++ b/chacha20/src/backends/neon.rs @@ -7,19 +7,15 @@ use crate::{Rounds, STATE_WORDS}; use core::{arch::aarch64::*, marker::PhantomData}; #[cfg(feature = "rand_core")] -use crate::{ChaChaCore, variants::Variant}; +use crate::{variants::Variant, ChaChaCore}; #[cfg(feature = "cipher")] use crate::chacha::Block; #[cfg(feature = "cipher")] use cipher::{ - StreamClosure, consts::{U4, U64}, - BlockSizeUser, - ParBlocks, - ParBlocksSizeUser, - StreamBackend + BlockSizeUser, ParBlocks, ParBlocksSizeUser, StreamBackend, StreamClosure, }; #[inline] @@ -49,7 +45,7 @@ where pub(crate) unsafe fn rng_inner(core: &mut ChaChaCore, buffer: &mut [u32; 64]) where R: Rounds, - V: Variant + V: Variant, { let mut backend = Backend:: { state: [ @@ -621,60 +617,24 @@ impl Backend { r3_3 = add64!(r3_3, ctrs[2]); vst1q_u8(blocks.offset(0), vreinterpretq_u8_u32(r0_0)); - vst1q_u8( - blocks.offset(16), - vreinterpretq_u8_u32(r0_1), - ); - vst1q_u8( - blocks.offset(2 * 16), - vreinterpretq_u8_u32(r0_2), - ); - vst1q_u8( - blocks.offset(3 * 16), - vreinterpretq_u8_u32(r0_3), - ); + vst1q_u8(blocks.offset(16), vreinterpretq_u8_u32(r0_1)); + vst1q_u8(blocks.offset(2 * 16), vreinterpretq_u8_u32(r0_2)); + vst1q_u8(blocks.offset(3 * 16), vreinterpretq_u8_u32(r0_3)); vst1q_u8(blocks.offset(64), vreinterpretq_u8_u32(r1_0)); - vst1q_u8( - blocks.offset(16+64), - vreinterpretq_u8_u32(r1_1), - ); - vst1q_u8( - blocks.offset(2 * 16 + 64), - vreinterpretq_u8_u32(r1_2), - ); - vst1q_u8( - blocks.offset(3 * 16 + 64), - vreinterpretq_u8_u32(r1_3), - ); + vst1q_u8(blocks.offset(16 + 64), vreinterpretq_u8_u32(r1_1)); + vst1q_u8(blocks.offset(2 * 16 + 64), vreinterpretq_u8_u32(r1_2)); + vst1q_u8(blocks.offset(3 * 16 + 64), vreinterpretq_u8_u32(r1_3)); vst1q_u8(blocks.offset(128), vreinterpretq_u8_u32(r2_0)); - vst1q_u8( - blocks.offset(16 + 128), - vreinterpretq_u8_u32(r2_1), - ); - vst1q_u8( - blocks.offset(2 * 16 + 128), - vreinterpretq_u8_u32(r2_2), - ); - vst1q_u8( - blocks.offset(3 * 16 + 128), - vreinterpretq_u8_u32(r2_3), - ); + vst1q_u8(blocks.offset(16 + 128), vreinterpretq_u8_u32(r2_1)); + vst1q_u8(blocks.offset(2 * 16 + 128), vreinterpretq_u8_u32(r2_2)); + vst1q_u8(blocks.offset(3 * 16 + 128), vreinterpretq_u8_u32(r2_3)); vst1q_u8(blocks.offset(192), vreinterpretq_u8_u32(r3_0)); - vst1q_u8( - blocks.offset(16 + 192), - vreinterpretq_u8_u32(r3_1), - ); - vst1q_u8( - blocks.offset(2 * 16 + 192), - vreinterpretq_u8_u32(r3_2), - ); - vst1q_u8( - blocks.offset(3 * 16 + 192), - vreinterpretq_u8_u32(r3_3), - ); + vst1q_u8(blocks.offset(16 + 192), vreinterpretq_u8_u32(r3_1)); + vst1q_u8(blocks.offset(2 * 16 + 192), vreinterpretq_u8_u32(r3_2)); + vst1q_u8(blocks.offset(3 * 16 + 192), vreinterpretq_u8_u32(r3_3)); self.state[3] = add64!(self.state[3], ctrs[3]); } diff --git a/chacha20/src/backends/soft.rs b/chacha20/src/backends/soft.rs index 28657975..4d6cd3c7 100644 --- a/chacha20/src/backends/soft.rs +++ b/chacha20/src/backends/soft.rs @@ -1,17 +1,15 @@ //! Portable implementation which does not rely on architecture-specific //! intrinsics. -use crate::{ChaChaCore, Rounds, STATE_WORDS, Variant}; +use crate::{ChaChaCore, Rounds, Variant, STATE_WORDS}; +#[cfg(feature = "cipher")] +use crate::chacha::Block; #[cfg(feature = "cipher")] use cipher::{ - BlockSizeUser, - ParBlocksSizeUser, - consts::{U64, U1}, - StreamBackend + consts::{U1, U64}, + BlockSizeUser, ParBlocksSizeUser, StreamBackend, }; -#[cfg(feature = "cipher")] -use crate::chacha::Block; pub(crate) struct Backend<'a, R: Rounds, V: Variant>(pub(crate) &'a mut ChaChaCore); @@ -46,7 +44,7 @@ impl<'a, R: Rounds, V: Variant> Backend<'a, R, V> { let res = run_rounds::(&self.0.state); self.0.state[12] = self.0.state[12].wrapping_add(1); - for (word, val) in buffer[i << 4..(i+1) << 4].iter_mut().zip(res.iter()) { + for (word, val) in buffer[i << 4..(i + 1) << 4].iter_mut().zip(res.iter()) { *word = val.to_le(); } } diff --git a/chacha20/src/chacha.rs b/chacha20/src/chacha.rs index 77d0e6c7..92e6a03b 100644 --- a/chacha20/src/chacha.rs +++ b/chacha20/src/chacha.rs @@ -1,10 +1,10 @@ pub use cipher::{ consts::{U12, U32, U64}, - generic_array::GenericArray, IvSizeUser, - KeyIvInit, KeySizeUser, StreamCipherCoreWrapper, + generic_array::GenericArray, + IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper, }; -use crate::{ChaChaCore, Rounds, R20, R8, R12, variants::Ietf}; +use crate::{variants::Ietf, ChaChaCore, Rounds, R12, R20, R8}; /// Key type used by all ChaCha variants. pub type Key = GenericArray; @@ -35,4 +35,4 @@ impl KeyIvInit for ChaChaCore { fn new(key: &Key, iv: &Nonce) -> Self { ChaChaCore::::new(key.as_ref(), iv.as_ref()) } -} \ No newline at end of file +} diff --git a/chacha20/src/legacy.rs b/chacha20/src/legacy.rs index 57821e4f..bcc4fe88 100644 --- a/chacha20/src/legacy.rs +++ b/chacha20/src/legacy.rs @@ -1,13 +1,12 @@ //! Legacy version of ChaCha20 with a 64-bit nonce use crate::chacha::Key; +use crate::{ChaChaCore, R20}; use cipher::{ consts::{U32, U8}, generic_array::GenericArray, - IvSizeUser, KeySizeUser, - KeyIvInit, StreamCipherCoreWrapper + IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper, }; -use crate::{ChaChaCore, R20}; /// Nonce type used by [`ChaCha20Legacy`]. pub type LegacyNonce = GenericArray; @@ -37,4 +36,4 @@ impl KeyIvInit for ChaCha20LegacyCore { fn new(key: &Key, iv: &LegacyNonce) -> Self { ChaChaCore::::new(key.as_ref(), iv.as_ref()) } -} \ No newline at end of file +} diff --git a/chacha20/src/lib.rs b/chacha20/src/lib.rs index a89e154b..d254da54 100644 --- a/chacha20/src/lib.rs +++ b/chacha20/src/lib.rs @@ -209,7 +209,7 @@ cfg_if! { /// The ChaCha core function. #[cfg_attr(feature = "rand_core", derive(Clone))] -pub struct ChaChaCore { +pub struct ChaChaCore { /// Internal state of the core function state: [u32; STATE_WORDS], /// CPU target feature tokens @@ -218,12 +218,12 @@ pub struct ChaChaCore { /// Number of rounds to perform rounds: PhantomData, /// the variant of the implementation - variant: PhantomData + variant: PhantomData, } impl ChaChaCore { /// Constructs a ChaChaCore with the specified key, iv, and amount of rounds. - /// You must ensure that the iv is of the correct size when using this method + /// You must ensure that the iv is of the correct size when using this method /// directly. fn new(key: &[u8; 32], iv: &[u8]) -> Self { let mut state = [0u32; STATE_WORDS]; @@ -254,15 +254,15 @@ impl ChaChaCore { let tokens = (); } } - Self { - state, - tokens, + Self { + state, + tokens, rounds: PhantomData, - variant: PhantomData + variant: PhantomData, } } - /// Generates 4 blocks in parallel with avx2 & neon, but merely fills + /// Generates 4 blocks in parallel with avx2 & neon, but merely fills /// 4 blocks with sse2 & soft #[cfg(feature = "rand_core")] fn generate(&mut self, buffer: &mut [u32; 64]) { diff --git a/chacha20/src/rng.rs b/chacha20/src/rng.rs index 61588e88..086812c5 100644 --- a/chacha20/src/rng.rs +++ b/chacha20/src/rng.rs @@ -203,7 +203,7 @@ macro_rules! impl_chacha_rng { pub struct $ChaChaXRng { core: $ChaChaXCore, buffer: [u32; BUFFER_SIZE], - index: usize + index: usize, } /// The ChaCha core random number generator @@ -217,7 +217,7 @@ macro_rules! impl_chacha_rng { Self { core: ChaChaCore::<$rounds, Ietf>::from_seed(seed.into()), buffer: [0u32; BUFFER_SIZE], - index: BUFFER_SIZE + index: BUFFER_SIZE, } } } @@ -324,11 +324,8 @@ macro_rules! impl_chacha_rng { pub fn get_word_pos(&self) -> u64 { // block_pos is a multiple of 4, and offset by 4; therefore, it already has the // last 2 bits set to 0, allowing us to shift it left 4 and add the index - let mut result = u64::from( - self.core - .state[12] - .wrapping_sub(BUF_BLOCKS.into()), - ) << 4; + let mut result = + u64::from(self.core.state[12].wrapping_sub(BUF_BLOCKS.into())) << 4; result += self.index as u64; // eliminate the 36th bit result & 0xfffffffff @@ -352,8 +349,7 @@ macro_rules! impl_chacha_rng { // when not using `set_word_pos`, the block_pos is always a multiple of 4. // This change follows those conventions, as well as maintaining the 6-bit // index - self.core - .state[12] = (u32::from_le_bytes(word_offset.0[0..4].try_into().unwrap())); + self.core.state[12] = (u32::from_le_bytes(word_offset.0[0..4].try_into().unwrap())); // generate will increase block_pos by 4 self.generate_and_set((word_offset.0[4] & 0x0F) as usize); } @@ -387,7 +383,10 @@ macro_rules! impl_chacha_rng { #[inline] pub fn get_stream(&self) -> u128 { let mut result = [0u8; 16]; - for (i, &big) in self.core.state[Ietf::NONCE_INDEX..BLOCK_WORDS as usize].iter().enumerate() { + for (i, &big) in self.core.state[Ietf::NONCE_INDEX..BLOCK_WORDS as usize] + .iter() + .enumerate() + { let index = i * 4; result[index + 0] = big as u8; result[index + 1] = (big >> 8) as u8; @@ -453,7 +452,7 @@ macro_rules! impl_chacha_rng { $ChaChaXRng { core, buffer: [0u32; BUFFER_SIZE], - index: BUFFER_SIZE + index: BUFFER_SIZE, } } } diff --git a/chacha20/src/variants.rs b/chacha20/src/variants.rs index da2fae37..58043a75 100644 --- a/chacha20/src/variants.rs +++ b/chacha20/src/variants.rs @@ -22,4 +22,4 @@ pub struct Legacy(); #[cfg(feature = "legacy")] impl Variant for Legacy { const NONCE_INDEX: usize = 14; -} \ No newline at end of file +} diff --git a/chacha20/src/xchacha.rs b/chacha20/src/xchacha.rs index ac0ddf9a..a7b2c92d 100644 --- a/chacha20/src/xchacha.rs +++ b/chacha20/src/xchacha.rs @@ -3,13 +3,12 @@ use cipher::{ consts::{U16, U24, U32, U64}, generic_array::GenericArray, - BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, - StreamCipherCoreWrapper, StreamClosure + BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper, StreamClosure, }; use crate::{ - ChaChaCore, variants::Ietf, STATE_WORDS, CONSTANTS, - Rounds, R20, R12, R8, StreamCipherCore, StreamCipherSeekCore, + variants::Ietf, ChaChaCore, Rounds, StreamCipherCore, StreamCipherSeekCore, CONSTANTS, R12, + R20, R8, STATE_WORDS, }; #[cfg(feature = "zeroize")]