Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

salsa20: add expose-core feature #180

Merged
merged 1 commit into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion salsa20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ cipher = { version = "0.2", features = ["dev"] }

[features]
default = ["xsalsa20"]
expose-core = []
hsalsa20 = ["xsalsa20"]
xsalsa20 = []

[package.metadata.docs.rs]
all-features = true
features = ["hsalsa20", "xsalsa20"]
rustdoc-args = ["--cfg", "docsrs"]
20 changes: 14 additions & 6 deletions salsa20/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{convert::TryInto, marker::PhantomData, mem};
/// While Salsa20 is a stream cipher, not a block cipher, its core
/// primitive is a function which acts on a 512-bit block
// TODO(tarcieri): zeroize? need to make sure we're actually copying first
pub(crate) struct Block<R: Rounds> {
pub struct Block<R: Rounds> {
/// Internal state of the block function
state: [u32; STATE_WORDS],

Expand All @@ -18,7 +18,7 @@ pub(crate) struct Block<R: Rounds> {

impl<R: Rounds> Block<R> {
/// Initialize block function with the given key and IV
pub(crate) fn new(key: &Key, iv: &Nonce) -> Self {
pub fn new(key: &Key, iv: &Nonce) -> Self {
#[allow(unsafe_code)]
let mut state: [u32; STATE_WORDS] = unsafe { mem::zeroed() };
state[0] = CONSTANTS[0];
Expand Down Expand Up @@ -50,9 +50,8 @@ impl<R: Rounds> Block<R> {
}

/// Generate output, overwriting data already in the buffer
pub(crate) fn generate(&mut self, counter: u64, output: &mut [u8]) {
pub fn generate(&mut self, output: &mut [u8]) {
debug_assert_eq!(output.len(), BLOCK_SIZE);
self.counter_setup(counter);

let mut state = self.state;
self.rounds(&mut state);
Expand All @@ -63,7 +62,7 @@ impl<R: Rounds> Block<R> {
}

/// Apply generated keystream to the output buffer
pub(crate) fn apply_keystream(&mut self, counter: u64, output: &mut [u8]) {
pub fn apply_keystream(&mut self, counter: u64, output: &mut [u8]) {
debug_assert_eq!(output.len(), BLOCK_SIZE);
self.counter_setup(counter);

Expand All @@ -78,7 +77,7 @@ impl<R: Rounds> Block<R> {
}

#[inline]
fn counter_setup(&mut self, counter: u64) {
pub(crate) fn counter_setup(&mut self, counter: u64) {
self.state[8] = (counter & 0xffff_ffff) as u32;
self.state[9] = ((counter >> 32) & 0xffff_ffff) as u32;
}
Expand Down Expand Up @@ -106,6 +105,15 @@ impl<R: Rounds> Block<R> {
}
}

impl<R: Rounds> From<[u32; STATE_WORDS]> for Block<R> {
fn from(state: [u32; STATE_WORDS]) -> Block<R> {
Self {
state,
rounds: PhantomData,
}
}
}

#[inline]
#[allow(clippy::many_single_char_names)]
pub(crate) fn quarter_round(
Expand Down
10 changes: 8 additions & 2 deletions salsa20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ mod xsalsa;

pub use crate::salsa::{Key, Nonce, Salsa, Salsa12, Salsa20, Salsa8};

#[cfg(feature = "xsalsa20")]
pub use crate::xsalsa::{XNonce, XSalsa20};
#[cfg(feature = "expose-core")]
pub use crate::{
block::Block,
rounds::{R12, R20, R8},
};

#[cfg(feature = "hsalsa20")]
pub use crate::xsalsa::hsalsa20;

#[cfg(feature = "xsalsa20")]
pub use crate::xsalsa::{XNonce, XSalsa20};

/// Size of a Salsa20 block in bytes
pub const BLOCK_SIZE: usize = 64;

Expand Down
6 changes: 4 additions & 2 deletions salsa20/src/salsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ impl<R: Rounds> SyncStreamCipher for Salsa<R> {
self.buffer_pos = rem.len() as u8;
self.counter = counter;
if !rem.is_empty() {
self.block.generate(counter, &mut self.buffer);
self.block.counter_setup(counter);
self.block.generate(&mut self.buffer);
xor(rem, &self.buffer[..rem.len()]);
}

Expand All @@ -134,7 +135,8 @@ impl<R: Rounds> SyncStreamCipherSeek for Salsa<R> {
self.counter = res.0;
self.buffer_pos = res.1;
if self.buffer_pos != 0 {
self.block.generate(self.counter, &mut self.buffer);
self.block.counter_setup(self.counter);
self.block.generate(&mut self.buffer);
}
Ok(())
}
Expand Down