From 3612b3c1b5b2c8aa08ec7dde1121144e56db40db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 12 Aug 2024 17:35:46 +0300 Subject: [PATCH] rename stream cipher traits --- cipher/src/stream.rs | 17 ++++++++++++----- cipher/src/stream/core_api.rs | 30 +++++++++++++++--------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/cipher/src/stream.rs b/cipher/src/stream.rs index aeb316df..520837db 100644 --- a/cipher/src/stream.rs +++ b/cipher/src/stream.rs @@ -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; @@ -199,17 +202,21 @@ impl 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(block: T, byte: u8, bs: u8) -> Result; + fn from_block_byte( + block: T, + byte: u8, + bs: u8, + ) -> Result; /// Try to get block number and bytes position for given block size `bs`. - fn into_block_byte(self, bs: u8) -> Result<(T, u8), OverflowError>; + fn into_block_byte(self, bs: u8) -> Result<(T, u8), OverflowError>; } macro_rules! impl_seek_num { {$($t:ty )*} => { $( impl SeekNum for $t { - fn from_block_byte(block: T, byte: u8, block_size: u8) -> Result { + fn from_block_byte(block: T, byte: u8, block_size: u8) -> Result { debug_assert!(byte != 0); let rem = block_size.checked_sub(byte).ok_or(OverflowError)?; let block: Self = block.try_into().map_err(|_| OverflowError)?; @@ -219,7 +226,7 @@ macro_rules! impl_seek_num { .ok_or(OverflowError) } - fn into_block_byte(self, block_size: u8) -> Result<(T, u8), OverflowError> { + fn into_block_byte(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)?; diff --git a/cipher/src/stream/core_api.rs b/cipher/src/stream/core_api.rs index cc16788d..08889879 100644 --- a/cipher/src/stream/core_api.rs +++ b/cipher/src/stream/core_api.rs @@ -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); @@ -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>(self, backend: &mut B); + fn call>(self, backend: &mut B); } /// Block-level synchronous stream ciphers. @@ -45,7 +45,7 @@ pub trait StreamCipherCore: BlockSizeUser + Sized { fn remaining_blocks(&self) -> Option; /// Process data using backend provided to the rank-2 closure. - fn process_with_backend(&mut self, f: impl StreamClosure); + fn process_with_backend(&mut self, f: impl StreamCipherClosure); /// Write keystream block. /// @@ -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 + TryFrom + TryFrom @@ -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; @@ -181,7 +181,7 @@ pub trait StreamCipherSeekCore: StreamCipherCore { macro_rules! impl_counter { {$($t:ty )*} => { - $( impl Counter for $t { } )* + $( impl StreamCipherCounter for $t { } )* }; } @@ -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>(self, backend: &mut B) { + fn call>(self, backend: &mut B) { backend.gen_ks_block(self.block); } } @@ -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>(self, backend: &mut B) { + fn call>(self, backend: &mut B) { if B::ParBlocksSize::USIZE > 1 { let (chunks, tail) = Array::slice_as_chunks_mut(self.blocks); for chunk in chunks { @@ -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>(mut self, backend: &mut B) { + fn call>(mut self, backend: &mut B) { let mut t = Default::default(); backend.gen_ks_block(&mut t); self.block.xor_in2out(&t); @@ -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>(self, backend: &mut B) { + fn call>(self, backend: &mut B) { if B::ParBlocksSize::USIZE > 1 { let (chunks, mut tail) = self.blocks.into_chunks::(); for mut chunk in chunks {