Skip to content

Commit

Permalink
rename stream cipher traits
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 12, 2024
1 parent 19e6b3f commit 3612b3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
17 changes: 12 additions & 5 deletions cipher/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ mod core_api;
mod errors;
mod wrapper;

pub use core_api::{Counter, StreamBackend, StreamCipherCore, StreamCipherSeekCore, StreamClosure};
pub use core_api::{
StreamCipherBackend, StreamCipherClosure, StreamCipherCore, StreamCipherCounter,
StreamCipherSeekCore,
};
pub use errors::{OverflowError, StreamCipherError};
pub use wrapper::StreamCipherCoreWrapper;

Expand Down Expand Up @@ -199,17 +202,21 @@ impl<C: StreamCipher> StreamCipher for &mut C {
pub trait SeekNum: Sized {
/// Try to get position for block number `block`, byte position inside
/// block `byte`, and block size `bs`.
fn from_block_byte<T: Counter>(block: T, byte: u8, bs: u8) -> Result<Self, OverflowError>;
fn from_block_byte<T: StreamCipherCounter>(
block: T,
byte: u8,
bs: u8,
) -> Result<Self, OverflowError>;

/// Try to get block number and bytes position for given block size `bs`.
fn into_block_byte<T: Counter>(self, bs: u8) -> Result<(T, u8), OverflowError>;
fn into_block_byte<T: StreamCipherCounter>(self, bs: u8) -> Result<(T, u8), OverflowError>;
}

macro_rules! impl_seek_num {
{$($t:ty )*} => {
$(
impl SeekNum for $t {
fn from_block_byte<T: Counter>(block: T, byte: u8, block_size: u8) -> Result<Self, OverflowError> {
fn from_block_byte<T: StreamCipherCounter>(block: T, byte: u8, block_size: u8) -> Result<Self, OverflowError> {
debug_assert!(byte != 0);
let rem = block_size.checked_sub(byte).ok_or(OverflowError)?;
let block: Self = block.try_into().map_err(|_| OverflowError)?;
Expand All @@ -219,7 +226,7 @@ macro_rules! impl_seek_num {
.ok_or(OverflowError)
}

fn into_block_byte<T: Counter>(self, block_size: u8) -> Result<(T, u8), OverflowError> {
fn into_block_byte<T: StreamCipherCounter>(self, block_size: u8) -> Result<(T, u8), OverflowError> {
let bs: Self = block_size.into();
let byte = (self % bs) as u8;
let block = T::try_from(self / bs).map_err(|_| OverflowError)?;
Expand Down
30 changes: 15 additions & 15 deletions cipher/src/stream/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crypto_common::{Block, BlockSizeUser, BlockSizes, ParBlocks, ParBlocksSizeUs
use inout::{InOut, InOutBuf};

/// Trait implemented by stream cipher backends.
pub trait StreamBackend: ParBlocksSizeUser {
pub trait StreamCipherBackend: ParBlocksSizeUser {
/// Generate keystream block.
fn gen_ks_block(&mut self, block: &mut Block<Self>);

Expand All @@ -30,9 +30,9 @@ pub trait StreamBackend: ParBlocksSizeUser {
/// Trait for [`StreamBackend`] users.
///
/// This trait is used to define rank-2 closures.
pub trait StreamClosure: BlockSizeUser {
pub trait StreamCipherClosure: BlockSizeUser {
/// Execute closure with the provided stream cipher backend.
fn call<B: StreamBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
fn call<B: StreamCipherBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
}

/// Block-level synchronous stream ciphers.
Expand All @@ -45,7 +45,7 @@ pub trait StreamCipherCore: BlockSizeUser + Sized {
fn remaining_blocks(&self) -> Option<usize>;

/// Process data using backend provided to the rank-2 closure.
fn process_with_backend(&mut self, f: impl StreamClosure<BlockSize = Self::BlockSize>);
fn process_with_backend(&mut self, f: impl StreamCipherClosure<BlockSize = Self::BlockSize>);

/// Write keystream block.
///
Expand Down Expand Up @@ -153,7 +153,7 @@ pub trait StreamCipherCore: BlockSizeUser + Sized {
/// This trait is implemented for `i32`, `u32`, `u64`, `u128`, and `usize`.
/// It's not intended to be implemented in third-party crates, but doing so
/// is not forbidden.
pub trait Counter:
pub trait StreamCipherCounter:
TryFrom<i32>
+ TryFrom<u32>
+ TryFrom<u64>
Expand All @@ -170,7 +170,7 @@ pub trait Counter:
/// Block-level seeking trait for stream ciphers.
pub trait StreamCipherSeekCore: StreamCipherCore {
/// Counter type used inside stream cipher.
type Counter: Counter;
type Counter: StreamCipherCounter;

/// Get current block position.
fn get_block_pos(&self) -> Self::Counter;
Expand All @@ -181,7 +181,7 @@ pub trait StreamCipherSeekCore: StreamCipherCore {

macro_rules! impl_counter {
{$($t:ty )*} => {
$( impl Counter for $t { } )*
$( impl StreamCipherCounter for $t { } )*
};
}

Expand All @@ -193,9 +193,9 @@ struct WriteBlockCtx<'a, BS: BlockSizes> {
impl<'a, BS: BlockSizes> BlockSizeUser for WriteBlockCtx<'a, BS> {
type BlockSize = BS;
}
impl<'a, BS: BlockSizes> StreamClosure for WriteBlockCtx<'a, BS> {
impl<'a, BS: BlockSizes> StreamCipherClosure for WriteBlockCtx<'a, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
fn call<B: StreamCipherBackend<BlockSize = BS>>(self, backend: &mut B) {
backend.gen_ks_block(self.block);
}
}
Expand All @@ -206,9 +206,9 @@ struct WriteBlocksCtx<'a, BS: BlockSizes> {
impl<'a, BS: BlockSizes> BlockSizeUser for WriteBlocksCtx<'a, BS> {
type BlockSize = BS;
}
impl<'a, BS: BlockSizes> StreamClosure for WriteBlocksCtx<'a, BS> {
impl<'a, BS: BlockSizes> StreamCipherClosure for WriteBlocksCtx<'a, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
fn call<B: StreamCipherBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
let (chunks, tail) = Array::slice_as_chunks_mut(self.blocks);
for chunk in chunks {
Expand All @@ -231,9 +231,9 @@ impl<'inp, 'out, BS: BlockSizes> BlockSizeUser for ApplyBlockCtx<'inp, 'out, BS>
type BlockSize = BS;
}

impl<'inp, 'out, BS: BlockSizes> StreamClosure for ApplyBlockCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> StreamCipherClosure for ApplyBlockCtx<'inp, 'out, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(mut self, backend: &mut B) {
fn call<B: StreamCipherBackend<BlockSize = BS>>(mut self, backend: &mut B) {
let mut t = Default::default();
backend.gen_ks_block(&mut t);
self.block.xor_in2out(&t);
Expand All @@ -248,10 +248,10 @@ impl<'inp, 'out, BS: BlockSizes> BlockSizeUser for ApplyBlocksCtx<'inp, 'out, BS
type BlockSize = BS;
}

impl<'inp, 'out, BS: BlockSizes> StreamClosure for ApplyBlocksCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> StreamCipherClosure for ApplyBlocksCtx<'inp, 'out, BS> {
#[inline(always)]
#[allow(clippy::needless_range_loop)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
fn call<B: StreamCipherBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
let (chunks, mut tail) = self.blocks.into_chunks::<B::ParBlocksSize>();
for mut chunk in chunks {
Expand Down

0 comments on commit 3612b3c

Please sign in to comment.