Skip to content

Commit

Permalink
cipher: rename BlockCipher*/BlockMode*
Browse files Browse the repository at this point in the history
Renames the following traits:

- `BlockEncrypt` => `BlockCipherEncrypt`
- `BlockDecrypt` => `BlockCipherDecrypt`
- `BlockEncryptMut` => `BlockModeEncrypt`
- `BlockDecryptMut` => `BlockModeDecrypt`

As originally suggested in this comment:

RustCrypto/block-modes#14 (comment)

This better reflects how these traits are actually used, i.e.
`BlockCipher*` is used by ciphers, and `BlockMode*` is used by their
modes of operation.
  • Loading branch information
tarcieri committed Jan 21, 2024
1 parent e5f0970 commit 5998e58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
20 changes: 10 additions & 10 deletions cipher/src/block.rs
Expand Up @@ -78,7 +78,7 @@ pub trait BlockClosure: BlockSizeUser {
}

/// Encrypt-only functionality for block ciphers.
pub trait BlockEncrypt: BlockSizeUser + Sized {
pub trait BlockCipherEncrypt: BlockSizeUser + Sized {
/// Encrypt data using backend provided to the rank-2 closure.
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = Self::BlockSize>);

Expand Down Expand Up @@ -189,7 +189,7 @@ pub trait BlockEncrypt: BlockSizeUser + Sized {
}

/// Decrypt-only functionality for block ciphers.
pub trait BlockDecrypt: BlockSizeUser {
pub trait BlockCipherDecrypt: BlockSizeUser {
/// Decrypt data using backend provided to the rank-2 closure.
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = Self::BlockSize>);

Expand Down Expand Up @@ -315,7 +315,7 @@ pub trait BlockDecrypt: BlockSizeUser {
/// The main use case for this trait is blocks modes, but it also can be used
/// for hardware cryptographic engines which require `&mut self` access to an
/// underlying hardware peripheral.
pub trait BlockEncryptMut: BlockSizeUser + Sized {
pub trait BlockModeEncrypt: BlockSizeUser + Sized {
/// Encrypt data using backend provided to the rank-2 closure.
fn encrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>);

Expand Down Expand Up @@ -430,7 +430,7 @@ pub trait BlockEncryptMut: BlockSizeUser + Sized {
/// The main use case for this trait is blocks modes, but it also can be used
/// for hardware cryptographic engines which require `&mut self` access to an
/// underlying hardware peripheral.
pub trait BlockDecryptMut: BlockSizeUser + Sized {
pub trait BlockModeDecrypt: BlockSizeUser + Sized {
/// Decrypt data using backend provided to the rank-2 closure.
fn decrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>);

Expand Down Expand Up @@ -551,27 +551,27 @@ pub trait BlockDecryptMut: BlockSizeUser + Sized {
}
}

impl<Alg: BlockEncrypt> BlockEncryptMut for Alg {
impl<Alg: BlockCipherEncrypt> BlockModeEncrypt for Alg {
fn encrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
self.encrypt_with_backend(f);
}
}

impl<Alg: BlockDecrypt> BlockDecryptMut for Alg {
impl<Alg: BlockCipherDecrypt> BlockModeDecrypt for Alg {
fn decrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
self.decrypt_with_backend(f);
}
}

impl<Alg: BlockCipher> BlockCipher for &Alg {}

impl<Alg: BlockEncrypt> BlockEncrypt for &Alg {
impl<Alg: BlockCipherEncrypt> BlockCipherEncrypt for &Alg {
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
Alg::encrypt_with_backend(self, f);
}
}

impl<Alg: BlockDecrypt> BlockDecrypt for &Alg {
impl<Alg: BlockCipherDecrypt> BlockCipherDecrypt for &Alg {
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
Alg::decrypt_with_backend(self, f);
}
Expand Down Expand Up @@ -638,7 +638,7 @@ macro_rules! impl_simple_block_encdec {
type BlockSize = $block_size;
}

impl<$($N$(:$b0$(+$b)*)?),*> $crate::BlockEncrypt for $cipher<$($N),*> {
impl<$($N$(:$b0$(+$b)*)?),*> $crate:BlockEncryptt for $cipher<$($N),*> {
fn encrypt_with_backend(&self, f: impl $crate::BlockClosure<BlockSize = $block_size>) {
struct EncBack<'a, $($N$(:$b0$(+$b)*)?),* >(&'a $cipher<$($N),*>);

Expand All @@ -665,7 +665,7 @@ macro_rules! impl_simple_block_encdec {
}
}

impl<$($N$(:$b0$(+$b)*)?),*> $crate::BlockDecrypt for $cipher<$($N),*> {
impl<$($N$(:$b0$(+$b)*)?),*> $crate:BlockDecryptt for $cipher<$($N),*> {
fn decrypt_with_backend(&self, f: impl $crate::BlockClosure<BlockSize = $block_size>) {
struct DecBack<'a, $($N$(:$b0$(+$b)*)?),* >(&'a $cipher<$($N),*>);

Expand Down
14 changes: 7 additions & 7 deletions cipher/src/stream.rs
Expand Up @@ -5,15 +5,15 @@

use crate::errors::{OverflowError, StreamCipherError};
use crate::stream_core::Counter;
use crate::{Block, BlockDecryptMut, BlockEncryptMut};
use crate::{Block, BlockModeDecrypt, BlockModeEncrypt};
use inout::{InOutBuf, NotEqualError};

/// Marker trait for block-level asynchronous stream ciphers
pub trait AsyncStreamCipher: Sized {
/// Encrypt data using `InOutBuf`.
fn encrypt_inout(mut self, data: InOutBuf<'_, '_, u8>)
where
Self: BlockEncryptMut,
Self: BlockModeEncrypt,
{
let (blocks, mut tail) = data.into_chunks();
self.encrypt_blocks_inout_mut(blocks);
Expand All @@ -29,7 +29,7 @@ pub trait AsyncStreamCipher: Sized {
/// Decrypt data using `InOutBuf`.
fn decrypt_inout(mut self, data: InOutBuf<'_, '_, u8>)
where
Self: BlockDecryptMut,
Self: BlockModeDecrypt,
{
let (blocks, mut tail) = data.into_chunks();
self.decrypt_blocks_inout_mut(blocks);
Expand All @@ -44,31 +44,31 @@ pub trait AsyncStreamCipher: Sized {
/// Encrypt data in place.
fn encrypt(self, buf: &mut [u8])
where
Self: BlockEncryptMut,
Self: BlockModeEncrypt,
{
self.encrypt_inout(buf.into());
}

/// Decrypt data in place.
fn decrypt(self, buf: &mut [u8])
where
Self: BlockDecryptMut,
Self: BlockModeDecrypt,
{
self.decrypt_inout(buf.into());
}

/// Encrypt data from buffer to buffer.
fn encrypt_b2b(self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError>
where
Self: BlockEncryptMut,
Self: BlockModeEncrypt,
{
InOutBuf::new(in_buf, out_buf).map(|b| self.encrypt_inout(b))
}

/// Decrypt data from buffer to buffer.
fn decrypt_b2b(self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError>
where
Self: BlockDecryptMut,
Self: BlockModeDecrypt,
{
InOutBuf::new(in_buf, out_buf).map(|b| self.decrypt_inout(b))
}
Expand Down

0 comments on commit 5998e58

Please sign in to comment.