Skip to content

Commit

Permalink
removed some currently unncecessary things from variants.rs; got test…
Browse files Browse the repository at this point in the history
…s to compile and pass
  • Loading branch information
nstilt1 committed Dec 14, 2023
1 parent c41cafa commit 1a7bc32
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 85 deletions.
5 changes: 4 additions & 1 deletion chacha20/src/backends/sse2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{ChaChaCore, Rounds, variants::Variant, STATE_WORDS};
use crate::{Rounds, variants::Variant, STATE_WORDS};
use core::marker::PhantomData;

#[cfg(feature = "rand_core")]
use crate::ChaChaCore;

#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
Expand Down
13 changes: 4 additions & 9 deletions chacha20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,30 +308,25 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {

#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherSeekCore for ChaChaCore<R, V> {
type Counter = V::Counter;
type Counter = u32;

#[inline(always)]
fn get_block_pos(&self) -> Self::Counter {
// seems like we have to cast a spell to do anything with this type
V::into_block_counter(&self.state[12..14])
self.state[12]
}

#[inline(always)]
fn set_block_pos(&mut self, pos: Self::Counter) {
// seems like we have to cast a spell to do anything with this type
let result = V::from_block_counter(pos);
self.state[12] = result.as_ref()[0];
if !V::IS_U32 {
self.state[13] = result.as_ref()[1];
}
self.state[12] = pos
}
}

#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherCore for ChaChaCore<R, V> {
#[inline(always)]
fn remaining_blocks(&self) -> Option<usize> {
let rem = V::remaining_blocks(self.get_block_pos());
let rem = u32::MAX - self.get_block_pos();
rem.try_into().ok()
}

Expand Down
68 changes: 0 additions & 68 deletions chacha20/src/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,26 @@
/// in order for Variant to compile. This way, we don't need to expose
/// VariantCounter when it is only for internal use.

#[cfg(feature = "cipher")]
/// A trait to restrict the counter for the cipher crate
pub trait VariantCounter: cipher::Counter {}
#[cfg(not(feature = "cipher"))]
pub trait VariantCounter {}

/// A trait that distinguishes some ChaCha variants
pub trait Variant: Clone {
/// the type used for the variant's nonce
type Nonce: AsRef<[u8]>;
/// the size of the Nonce in u32s
const NONCE_INDEX: usize;
/// the counter's type
type Counter: VariantCounter;
/// a bool to help with figuring out the size of the Counter
const IS_U32: bool;
/// The result type of `from_block_counter` to handle either 1 or 2 u32 vals
type CounterVals: AsRef<[u32]>;
/// A helper method for getting the block pos using these types
fn into_block_counter(vals: &[u32]) -> Self::Counter;
/// A helper method for setting the block pos using these types
fn from_block_counter(val: Self::Counter) -> Self::CounterVals;
/// A helper method for calculating the remaining blocks using these types
fn remaining_blocks(block_pos: Self::Counter) -> Self::Counter;
}

impl VariantCounter for u32 {}

#[derive(Clone)]
/// The details pertaining to the IETF variant
pub struct Ietf();
impl Variant for Ietf {
type Counter = u32;
const IS_U32: bool = true;
type Nonce = [u8; 12];
const NONCE_INDEX: usize = 13;
type CounterVals = [u32; 1];

fn into_block_counter(vals: &[u32]) -> Self::Counter {
vals[0]
}
fn from_block_counter(val: Self::Counter) -> Self::CounterVals {
[val]
}
fn remaining_blocks(block_pos: Self::Counter) -> Self::Counter {
u32::MAX - block_pos
}
}

#[derive(Clone)]
#[cfg(feature = "legacy")]
pub struct Legacy();

#[cfg(feature = "legacy")]
impl VariantCounter for u64 {}

#[cfg(feature = "legacy")]
impl Variant for Legacy {
type Counter = u32;
const IS_U32: bool = true;
type Nonce = crate::legacy::LegacyNonce;
const NONCE_INDEX: usize = 14;
type CounterVals = [u32; 1];
fn into_block_counter(vals: &[u32]) -> Self::Counter {
vals[0]
//(vals[0] as u64) << 32 | (vals[1] as u64)
}
fn from_block_counter(val: Self::Counter) -> Self::CounterVals {
[val]
//[(val >> 32) as u32, val as u32];
}
fn remaining_blocks(block_counter: Self::Counter) -> Self::Counter {
u32::MAX - block_counter
//u64::MAX - block_counter
}
}

#[derive(Clone)]
Expand All @@ -85,18 +30,5 @@ pub struct XChaChaVariant {}

#[cfg(feature = "xchacha")]
impl Variant for XChaChaVariant {
type Counter = u32;
type Nonce = [u8; 12];
const IS_U32: bool = true;
const NONCE_INDEX: usize = 13;
type CounterVals = [u32; 1];
fn into_block_counter(vals: &[u32]) -> Self::Counter {
vals[0]
}
fn from_block_counter(val: Self::Counter) -> Self::CounterVals {
[val]
}
fn remaining_blocks(block_pos: Self::Counter) -> Self::Counter {
u32::MAX - block_pos
}
}
14 changes: 7 additions & 7 deletions chacha20/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Tests for ChaCha20 (IETF and "djb" versions) as well as XChaCha20
#[cfg(feature = "chacha20")]
#[cfg(feature = "cipher")]
use chacha20::ChaCha20;

#[cfg(feature = "legacy")]
Expand All @@ -9,18 +9,18 @@ use chacha20::ChaCha20Legacy;
use chacha20::XChaCha20;

// IETF version of ChaCha20 (96-bit nonce)
#[cfg(feature = "chacha")]
#[cfg(feature = "cipher")]
cipher::stream_cipher_test!(chacha20_core, "chacha20", ChaCha20);
#[cfg(feature = "chacha")]
#[cfg(feature = "cipher")]
cipher::stream_cipher_seek_test!(chacha20_seek, ChaCha20);
#[cfg(feature = "xchacha")]
cipher::stream_cipher_seek_test!(xchacha20_seek, XChaCha20);
#[cfg(feature = "legacy")]
cipher::stream_cipher_seek_test!(chacha20legacy_seek, ChaCha20Legacy);

#[cfg(feature = "chacha")]
#[cfg(feature = "cipher")]
mod chacha20test {
use chacha20::{ChaCha20, Key, Nonce};
use chacha20::{ChaCha20, Key};
use cipher::{KeyIvInit, StreamCipher};
use hex_literal::hex;

Expand Down Expand Up @@ -71,7 +71,7 @@ mod chacha20test {

#[test]
fn chacha20_keystream() {
let mut cipher = ChaCha20::new(&Key::from(KEY), &Nonce::from(IV));
let mut cipher = ChaCha20::new(&Key::from(KEY), &IV.into());

// The test vectors omit the first 64-bytes of the keystream
let mut prefix = [0u8; 64];
Expand All @@ -84,7 +84,7 @@ mod chacha20test {

#[test]
fn chacha20_encryption() {
let mut cipher = ChaCha20::new(&Key::from(KEY), &Nonce::from(IV));
let mut cipher = ChaCha20::new(&Key::from(KEY), &IV.into());
let mut buf = PLAINTEXT;

// The test vectors omit the first 64-bytes of the keystream
Expand Down

0 comments on commit 1a7bc32

Please sign in to comment.