Skip to content
Open
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
15 changes: 7 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ unused_qualifications = "warn"
crypto-common = { path = "crypto-common" }
digest = { path = "digest" }
signature = { path = "signature" }
block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "block-buffer/block-sizes" }
26 changes: 13 additions & 13 deletions cipher/src/block/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common::{Block, BlockSizeUser, BlockSizes, typenum::Unsigned};
use common::{Block, BlockSizeUser, array::ArraySize, typenum::Unsigned};
use inout::{InOut, InOutBuf};

use super::{
Expand All @@ -7,51 +7,51 @@ use super::{
};

/// Closure used in methods which operate over separate blocks.
pub(super) struct BlockCtx<'inp, 'out, BS: BlockSizes> {
pub(super) struct BlockCtx<'inp, 'out, BS: ArraySize> {
pub block: InOut<'inp, 'out, Block<Self>>,
}

impl<BS: BlockSizes> BlockSizeUser for BlockCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockSizeUser for BlockCtx<'_, '_, BS> {
type BlockSize = BS;
}

impl<BS: BlockSizes> BlockCipherEncClosure for BlockCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockCipherEncClosure for BlockCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockCipherEncBackend<BlockSize = BS>>(self, backend: &B) {
backend.encrypt_block(self.block);
}
}

impl<BS: BlockSizes> BlockCipherDecClosure for BlockCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockCipherDecClosure for BlockCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockCipherDecBackend<BlockSize = BS>>(self, backend: &B) {
backend.decrypt_block(self.block);
}
}

impl<BS: BlockSizes> BlockModeEncClosure for BlockCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockModeEncClosure for BlockCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockModeEncBackend<BlockSize = BS>>(self, backend: &mut B) {
backend.encrypt_block(self.block);
}
}

impl<BS: BlockSizes> BlockModeDecClosure for BlockCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockModeDecClosure for BlockCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockModeDecBackend<BlockSize = BS>>(self, backend: &mut B) {
backend.decrypt_block(self.block);
}
}
/// Closure used in methods which operate over slice of blocks.
pub(super) struct BlocksCtx<'inp, 'out, BS: BlockSizes> {
pub(super) struct BlocksCtx<'inp, 'out, BS: ArraySize> {
pub blocks: InOutBuf<'inp, 'out, Block<Self>>,
}

impl<BS: BlockSizes> BlockSizeUser for BlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockSizeUser for BlocksCtx<'_, '_, BS> {
type BlockSize = BS;
}

impl<BS: BlockSizes> BlockCipherEncClosure for BlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockCipherEncClosure for BlocksCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockCipherEncBackend<BlockSize = BS>>(self, backend: &B) {
if B::ParBlocksSize::USIZE > 1 {
Expand All @@ -68,7 +68,7 @@ impl<BS: BlockSizes> BlockCipherEncClosure for BlocksCtx<'_, '_, BS> {
}
}

impl<BS: BlockSizes> BlockCipherDecClosure for BlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockCipherDecClosure for BlocksCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockCipherDecBackend<BlockSize = BS>>(self, backend: &B) {
if B::ParBlocksSize::USIZE > 1 {
Expand All @@ -85,7 +85,7 @@ impl<BS: BlockSizes> BlockCipherDecClosure for BlocksCtx<'_, '_, BS> {
}
}

impl<BS: BlockSizes> BlockModeEncClosure for BlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockModeEncClosure for BlocksCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockModeEncBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
Expand All @@ -102,7 +102,7 @@ impl<BS: BlockSizes> BlockModeEncClosure for BlocksCtx<'_, '_, BS> {
}
}

impl<BS: BlockSizes> BlockModeDecClosure for BlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockModeDecClosure for BlocksCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: BlockModeDecBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
Expand Down
33 changes: 19 additions & 14 deletions cipher/src/stream/core_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::StreamCipherError;
use crate::{array::Array, typenum::Unsigned};
use common::{Block, BlockSizeUser, BlockSizes, ParBlocks, ParBlocksSizeUser};
use crate::{
array::{Array, ArraySize},
typenum::Unsigned,
};
use common::{Block, BlockSizeUser, ParBlocks, ParBlocksSizeUser};
use inout::{InOut, InOutBuf};

/// Trait implemented by stream cipher backends.
Expand Down Expand Up @@ -197,26 +200,28 @@ macro_rules! impl_counter {

impl_counter! { u32 u64 u128 }

struct WriteBlockCtx<'a, BS: BlockSizes> {
struct WriteBlockCtx<'a, BS: ArraySize> {
block: &'a mut Block<Self>,
}
impl<BS: BlockSizes> BlockSizeUser for WriteBlockCtx<'_, BS> {
impl<BS: ArraySize> BlockSizeUser for WriteBlockCtx<'_, BS> {
type BlockSize = BS;
}
impl<BS: BlockSizes> StreamCipherClosure for WriteBlockCtx<'_, BS> {
impl<BS: ArraySize> StreamCipherClosure for WriteBlockCtx<'_, BS> {
#[inline(always)]
fn call<B: StreamCipherBackend<BlockSize = BS>>(self, backend: &mut B) {
backend.gen_ks_block(self.block);
}
}

struct WriteBlocksCtx<'a, BS: BlockSizes> {
struct WriteBlocksCtx<'a, BS: ArraySize> {
blocks: &'a mut [Block<Self>],
}
impl<BS: BlockSizes> BlockSizeUser for WriteBlocksCtx<'_, BS> {

impl<BS: ArraySize> BlockSizeUser for WriteBlocksCtx<'_, BS> {
type BlockSize = BS;
}
impl<BS: BlockSizes> StreamCipherClosure for WriteBlocksCtx<'_, BS> {

impl<BS: ArraySize> StreamCipherClosure for WriteBlocksCtx<'_, BS> {
#[inline(always)]
fn call<B: StreamCipherBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
Expand All @@ -233,15 +238,15 @@ impl<BS: BlockSizes> StreamCipherClosure for WriteBlocksCtx<'_, BS> {
}
}

struct ApplyBlockCtx<'inp, 'out, BS: BlockSizes> {
struct ApplyBlockCtx<'inp, 'out, BS: ArraySize> {
block: InOut<'inp, 'out, Block<Self>>,
}

impl<BS: BlockSizes> BlockSizeUser for ApplyBlockCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockSizeUser for ApplyBlockCtx<'_, '_, BS> {
type BlockSize = BS;
}

impl<BS: BlockSizes> StreamCipherClosure for ApplyBlockCtx<'_, '_, BS> {
impl<BS: ArraySize> StreamCipherClosure for ApplyBlockCtx<'_, '_, BS> {
#[inline(always)]
fn call<B: StreamCipherBackend<BlockSize = BS>>(mut self, backend: &mut B) {
let mut t = Default::default();
Expand All @@ -250,15 +255,15 @@ impl<BS: BlockSizes> StreamCipherClosure for ApplyBlockCtx<'_, '_, BS> {
}
}

struct ApplyBlocksCtx<'inp, 'out, BS: BlockSizes> {
struct ApplyBlocksCtx<'inp, 'out, BS: ArraySize> {
blocks: InOutBuf<'inp, 'out, Block<Self>>,
}

impl<BS: BlockSizes> BlockSizeUser for ApplyBlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> BlockSizeUser for ApplyBlocksCtx<'_, '_, BS> {
type BlockSize = BS;
}

impl<BS: BlockSizes> StreamCipherClosure for ApplyBlocksCtx<'_, '_, BS> {
impl<BS: ArraySize> StreamCipherClosure for ApplyBlocksCtx<'_, '_, BS> {
#[inline(always)]
#[allow(clippy::needless_range_loop)]
fn call<B: StreamCipherBackend<BlockSize = BS>>(self, backend: &mut B) {
Expand Down
71 changes: 58 additions & 13 deletions cipher/src/stream/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
OverflowError, SeekNum, StreamCipher, StreamCipherCore, StreamCipherSeek, StreamCipherSeekCore,
errors::StreamCipherError,
};
use block_buffer::ReadBuffer;
use block_buffer::{BlockSizes, ReadBuffer};
use common::{
Iv, IvSizeUser, Key, KeyInit, KeyIvInit, KeySizeUser, array::Array, typenum::Unsigned,
};
Expand All @@ -16,12 +16,20 @@ use zeroize::ZeroizeOnDrop;
/// Buffering wrapper around a [`StreamCipherCore`] implementation.
///
/// It handles data buffering and implements the slice-based traits.
pub struct StreamCipherCoreWrapper<T: StreamCipherCore> {
pub struct StreamCipherCoreWrapper<T>
where
T: StreamCipherCore,
T::BlockSize: BlockSizes,
{
core: T,
buffer: ReadBuffer<T::BlockSize>,
}

impl<T: StreamCipherCore + Clone> Clone for StreamCipherCoreWrapper<T> {
impl<T> Clone for StreamCipherCoreWrapper<T>
where
T: StreamCipherCore + Clone,
T::BlockSize: BlockSizes,
{
#[inline]
fn clone(&self) -> Self {
Self {
Expand All @@ -31,14 +39,22 @@ impl<T: StreamCipherCore + Clone> Clone for StreamCipherCoreWrapper<T> {
}
}

impl<T: StreamCipherCore + fmt::Debug> fmt::Debug for StreamCipherCoreWrapper<T> {
impl<T> fmt::Debug for StreamCipherCoreWrapper<T>
where
T: StreamCipherCore + fmt::Debug,
T::BlockSize: BlockSizes,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StreamCipherCoreWrapper")
.finish_non_exhaustive()
}
}

impl<T: StreamCipherCore> StreamCipherCoreWrapper<T> {
impl<T> StreamCipherCoreWrapper<T>
where
T: StreamCipherCore,
T::BlockSize: BlockSizes,
{
/// Initialize from a [`StreamCipherCore`] instance.
pub fn from_core(core: T) -> Self {
Self {
Expand All @@ -53,7 +69,11 @@ impl<T: StreamCipherCore> StreamCipherCoreWrapper<T> {
}
}

impl<T: StreamCipherCore> StreamCipher for StreamCipherCoreWrapper<T> {
impl<T> StreamCipher for StreamCipherCoreWrapper<T>
where
T: StreamCipherCore,
T::BlockSize: BlockSizes,
{
#[inline]
fn check_remaining(&self, data_len: usize) -> Result<(), StreamCipherError> {
let Some(rem_blocks) = self.core.remaining_blocks() else {
Expand Down Expand Up @@ -107,7 +127,11 @@ impl<T: StreamCipherCore> StreamCipher for StreamCipherCoreWrapper<T> {
}
}

impl<T: StreamCipherSeekCore> StreamCipherSeek for StreamCipherCoreWrapper<T> {
impl<T> StreamCipherSeek for StreamCipherCoreWrapper<T>
where
T: StreamCipherSeekCore,
T::BlockSize: BlockSizes,
{
#[allow(clippy::unwrap_in_result)]
fn try_current_pos<SN: SeekNum>(&self) -> Result<SN, OverflowError> {
let pos = u8::try_from(self.buffer.get_pos())
Expand Down Expand Up @@ -142,15 +166,27 @@ impl<T: StreamCipherSeekCore> StreamCipherSeek for StreamCipherCoreWrapper<T> {
// not work properly without mutually exclusive traits, see:
// https://github.com/rust-lang/rfcs/issues/1053

impl<T: KeySizeUser + StreamCipherCore> KeySizeUser for StreamCipherCoreWrapper<T> {
impl<T> KeySizeUser for StreamCipherCoreWrapper<T>
where
T: KeySizeUser + StreamCipherCore,
T::BlockSize: BlockSizes,
{
type KeySize = T::KeySize;
}

impl<T: IvSizeUser + StreamCipherCore> IvSizeUser for StreamCipherCoreWrapper<T> {
impl<T> IvSizeUser for StreamCipherCoreWrapper<T>
where
T: IvSizeUser + StreamCipherCore,
T::BlockSize: BlockSizes,
{
type IvSize = T::IvSize;
}

impl<T: KeyIvInit + StreamCipherCore> KeyIvInit for StreamCipherCoreWrapper<T> {
impl<T> KeyIvInit for StreamCipherCoreWrapper<T>
where
T: KeyIvInit + StreamCipherCore,
T::BlockSize: BlockSizes,
{
#[inline]
fn new(key: &Key<Self>, iv: &Iv<Self>) -> Self {
Self {
Expand All @@ -160,7 +196,11 @@ impl<T: KeyIvInit + StreamCipherCore> KeyIvInit for StreamCipherCoreWrapper<T> {
}
}

impl<T: KeyInit + StreamCipherCore> KeyInit for StreamCipherCoreWrapper<T> {
impl<T> KeyInit for StreamCipherCoreWrapper<T>
where
T: KeyInit + StreamCipherCore,
T::BlockSize: BlockSizes,
{
#[inline]
fn new(key: &Key<Self>) -> Self {
Self {
Expand All @@ -171,13 +211,18 @@ impl<T: KeyInit + StreamCipherCore> KeyInit for StreamCipherCoreWrapper<T> {
}

#[cfg(feature = "zeroize")]
impl<T: StreamCipherCore + ZeroizeOnDrop> ZeroizeOnDrop for StreamCipherCoreWrapper<T> {}
impl<T> ZeroizeOnDrop for StreamCipherCoreWrapper<T>
where
T: StreamCipherCore + ZeroizeOnDrop,
T::BlockSize: BlockSizes,
{
}

// Assert that `ReadBuffer` implements `ZeroizeOnDrop`
#[cfg(feature = "zeroize")]
const _: () = {
#[allow(dead_code, trivial_casts)]
fn check_buffer<BS: crate::array::ArraySize>(v: &ReadBuffer<BS>) {
fn check_buffer<BS: BlockSizes>(v: &ReadBuffer<BS>) {
let _ = v as &dyn ZeroizeOnDrop;
}
};
Loading