Skip to content

Commit

Permalink
chacha: hide numbered-param interface in guts
Browse files Browse the repository at this point in the history
  • Loading branch information
kazcw committed May 12, 2021
1 parent eb88236 commit 2f4bdfc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
11 changes: 4 additions & 7 deletions rand_chacha/src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ use rand_core::{CryptoRng, Error, RngCore, SeedableRng};

#[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer};

const STREAM_PARAM_NONCE: u32 = 1;
const STREAM_PARAM_BLOCK: u32 = 0;

// NB. this must remain consistent with some currently hard-coded numbers in this module
const BUF_BLOCKS: u8 = 4;
// number of 32-bit words per ChaCha block (fixed by algorithm definition)
Expand Down Expand Up @@ -196,7 +193,7 @@ macro_rules! chacha_impl {
#[inline]
pub fn get_word_pos(&self) -> u128 {
let buf_start_block = {
let buf_end_block = self.rng.core.state.get_stream_param(STREAM_PARAM_BLOCK);
let buf_end_block = self.rng.core.state.get_block_pos();
u64::wrapping_sub(buf_end_block, BUF_BLOCKS.into())
};
let (buf_offset_blocks, block_offset_words) = {
Expand All @@ -221,7 +218,7 @@ macro_rules! chacha_impl {
self.rng
.core
.state
.set_stream_param(STREAM_PARAM_BLOCK, block);
.set_block_pos(block);
self.rng.generate_and_set((word_offset % u128::from(BLOCK_WORDS)) as usize);
}

Expand All @@ -241,7 +238,7 @@ macro_rules! chacha_impl {
self.rng
.core
.state
.set_stream_param(STREAM_PARAM_NONCE, stream);
.set_nonce(stream);
if self.rng.index() != 64 {
let wp = self.get_word_pos();
self.set_word_pos(wp);
Expand All @@ -254,7 +251,7 @@ macro_rules! chacha_impl {
self.rng
.core
.state
.get_stream_param(STREAM_PARAM_NONCE)
.get_nonce()
}

/// Get the seed.
Expand Down
21 changes: 17 additions & 4 deletions rand_chacha/src/guts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const BUFBLOCKS: u64 = 1 << LOG2_BUFBLOCKS;
pub(crate) const BUFSZ64: u64 = BLOCK64 * BUFBLOCKS;
pub(crate) const BUFSZ: usize = BUFSZ64 as usize;

const STREAM_PARAM_NONCE: u32 = 1;
const STREAM_PARAM_BLOCK: u32 = 0;

#[derive(Clone, PartialEq, Eq)]
pub struct ChaCha {
pub(crate) b: vec128_storage,
Expand Down Expand Up @@ -83,13 +86,23 @@ impl ChaCha {
}

#[inline(always)]
pub fn set_stream_param(&mut self, param: u32, value: u64) {
set_stream_param(self, param, value)
pub fn set_block_pos(&mut self, value: u64) {
set_stream_param(self, STREAM_PARAM_BLOCK, value)
}

#[inline(always)]
pub fn get_block_pos(&self) -> u64 {
get_stream_param(self, STREAM_PARAM_BLOCK)
}

#[inline(always)]
pub fn set_nonce(&mut self, value: u64) {
set_stream_param(self, STREAM_PARAM_NONCE, value)
}

#[inline(always)]
pub fn get_stream_param(&self, param: u32) -> u64 {
get_stream_param(self, param)
pub fn get_nonce(&self) -> u64 {
get_stream_param(self, STREAM_PARAM_NONCE)
}

#[inline(always)]
Expand Down

0 comments on commit 2f4bdfc

Please sign in to comment.