Skip to content

Commit

Permalink
move quarter_round function to the root
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Mar 25, 2024
1 parent a61e846 commit 15258ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
27 changes: 1 addition & 26 deletions chacha20/src/backends/soft.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Portable implementation which does not rely on architecture-specific
//! intrinsics.

use crate::{Block, ChaChaCore, Unsigned, STATE_WORDS};
use crate::{quarter_round, Block, ChaChaCore, Unsigned, STATE_WORDS};
use cipher::{
consts::{U1, U64},
BlockSizeUser, ParBlocksSizeUser, StreamBackend,
Expand Down Expand Up @@ -52,28 +52,3 @@ fn run_rounds<R: Unsigned>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
}
res
}

/// The ChaCha20 quarter round function
pub(crate) fn quarter_round(
a: usize,
b: usize,
c: usize,
d: usize,
state: &mut [u32; STATE_WORDS],
) {
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(16);

state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(12);

state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(8);

state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(7);
}
28 changes: 28 additions & 0 deletions chacha20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,31 @@ impl<R: Unsigned> Drop for ChaChaCore<R> {
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<R: Unsigned> ZeroizeOnDrop for ChaChaCore<R> {}

/// The ChaCha20 quarter round function
///
/// We located this function in the root of the crate as we want it to be available
/// for the soft backend and for xchacha.
pub(crate) fn quarter_round(
a: usize,
b: usize,
c: usize,
d: usize,
state: &mut [u32; STATE_WORDS],
) {
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(16);

state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(12);

state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(8);

state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(7);
}
4 changes: 1 addition & 3 deletions chacha20/src/xchacha.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! XChaCha is an extended nonce variant of ChaCha

use super::{ChaChaCore, Key, Nonce, CONSTANTS, STATE_WORDS};
use super::{quarter_round, ChaChaCore, Key, Nonce, CONSTANTS, STATE_WORDS};
use cipher::{
consts::{U10, U16, U24, U32, U4, U6, U64},
generic_array::{typenum::Unsigned, GenericArray},
BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCore, StreamCipherCoreWrapper,
StreamCipherSeekCore, StreamClosure,
};

use crate::backends::soft::quarter_round;

#[cfg(feature = "zeroize")]
use cipher::zeroize::ZeroizeOnDrop;

Expand Down

0 comments on commit 15258ae

Please sign in to comment.