use crypto_common::{typenum::Unsigned, Block, BlockSizeUser, ParBlocks, ParBlocksSizeUser};
use inout::{InOut, InOutBuf};
pub trait BlockCipherEncBackend: ParBlocksSizeUser {
fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>);
#[inline(always)]
fn encrypt_par_blocks(&self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
for i in 0..Self::ParBlocksSize::USIZE {
self.encrypt_block(blocks.get(i));
}
}
#[inline(always)]
fn encrypt_tail_blocks(&self, blocks: InOutBuf<'_, '_, Block<Self>>) {
assert!(blocks.len() < Self::ParBlocksSize::USIZE);
for block in blocks {
self.encrypt_block(block);
}
}
#[inline(always)]
fn encrypt_block_inplace(&self, block: &mut Block<Self>) {
self.encrypt_block(block.into());
}
#[inline(always)]
fn encrypt_par_blocks_inplace(&self, blocks: &mut ParBlocks<Self>) {
self.encrypt_par_blocks(blocks.into());
}
#[inline(always)]
fn encrypt_tail_blocks_inplace(&self, blocks: &mut [Block<Self>]) {
self.encrypt_tail_blocks(blocks.into());
}
}
pub trait BlockCipherEncClosure: BlockSizeUser {
fn call<B: BlockCipherEncBackend<BlockSize = Self::BlockSize>>(self, backend: &B);
}
pub trait BlockCipherDecBackend: ParBlocksSizeUser {
fn decrypt_block(&self, block: InOut<'_, '_, Block<Self>>);
#[inline(always)]
fn decrypt_par_blocks(&self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
for i in 0..Self::ParBlocksSize::USIZE {
self.decrypt_block(blocks.get(i));
}
}
#[inline(always)]
fn decrypt_tail_blocks(&self, blocks: InOutBuf<'_, '_, Block<Self>>) {
assert!(blocks.len() < Self::ParBlocksSize::USIZE);
for block in blocks {
self.decrypt_block(block);
}
}
#[inline(always)]
fn decrypt_block_inplace(&self, block: &mut Block<Self>) {
self.decrypt_block(block.into());
}
#[inline(always)]
fn decrypt_par_blocks_inplace(&self, blocks: &mut ParBlocks<Self>) {
self.decrypt_par_blocks(blocks.into());
}
#[inline(always)]
fn decrypt_tail_blocks_inplace(&self, blocks: &mut [Block<Self>]) {
self.decrypt_tail_blocks(blocks.into());
}
}
pub trait BlockCipherDecClosure: BlockSizeUser {
fn call<B: BlockCipherDecBackend<BlockSize = Self::BlockSize>>(self, backend: &B);
}
pub trait BlockModeEncBackend: ParBlocksSizeUser {
fn encrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>);
#[inline(always)]
fn encrypt_par_blocks(&mut self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
for i in 0..Self::ParBlocksSize::USIZE {
self.encrypt_block(blocks.get(i));
}
}
#[inline(always)]
fn encrypt_tail_blocks(&mut self, blocks: InOutBuf<'_, '_, Block<Self>>) {
assert!(blocks.len() < Self::ParBlocksSize::USIZE);
for block in blocks {
self.encrypt_block(block);
}
}
#[inline(always)]
fn encrypt_block_inplace(&mut self, block: &mut Block<Self>) {
self.encrypt_block(block.into());
}
#[inline(always)]
fn encrypt_par_blocks_inplace(&mut self, blocks: &mut ParBlocks<Self>) {
self.encrypt_par_blocks(blocks.into());
}
#[inline(always)]
fn encrypt_tail_blocks_inplace(&mut self, blocks: &mut [Block<Self>]) {
self.encrypt_tail_blocks(blocks.into());
}
}
pub trait BlockModeEncClosure: BlockSizeUser {
fn call<B: BlockModeEncBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
}
pub trait BlockModeDecBackend: ParBlocksSizeUser {
fn decrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>);
#[inline(always)]
fn decrypt_par_blocks(&mut self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
for i in 0..Self::ParBlocksSize::USIZE {
self.decrypt_block(blocks.get(i));
}
}
#[inline(always)]
fn decrypt_tail_blocks(&mut self, blocks: InOutBuf<'_, '_, Block<Self>>) {
assert!(blocks.len() < Self::ParBlocksSize::USIZE);
for block in blocks {
self.decrypt_block(block);
}
}
#[inline(always)]
fn decrypt_block_inplace(&mut self, block: &mut Block<Self>) {
self.decrypt_block(block.into());
}
#[inline(always)]
fn decrypt_par_blocks_inplace(&mut self, blocks: &mut ParBlocks<Self>) {
self.decrypt_par_blocks(blocks.into());
}
#[inline(always)]
fn decrypt_tail_blocks_inplace(&mut self, blocks: &mut [Block<Self>]) {
self.decrypt_tail_blocks(blocks.into());
}
}
pub trait BlockModeDecClosure: BlockSizeUser {
fn call<B: BlockModeDecBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B);
}