Skip to content
Draft
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
57 changes: 57 additions & 0 deletions cipher/src/async_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Traits which define functionality of asynchronous (a.k.a. self-synchronizing) stream ciphers.

use crate::block::{BlockModeDecrypt, BlockModeEncrypt};
use crypto_common::Block;
use inout::{InOutBuf, NotEqualError};

/// Asynchronous stream cipher encryptor.
pub trait AsyncStreamCipherCoreEncrypt: BlockModeEncrypt {
/// Encrypt data using `InOutBuf`.
fn encrypt_inout(mut self, data: InOutBuf<'_, '_, u8>) {
let (blocks, mut tail) = data.into_chunks();
self.encrypt_blocks_inout(blocks);
let n = tail.len();
if n != 0 {
let mut block = Block::<Self>::default();
block[..n].copy_from_slice(tail.get_in());
self.encrypt_block(&mut block);
tail.get_out().copy_from_slice(&block[..n]);
}
}

/// Encrypt data in place.
fn encrypt(self, buf: &mut [u8]) {
self.encrypt_inout(buf.into());
}

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

/// Asynchronous stream cipher decryptor.
pub trait AsyncStreamCipherCoreDecrypt: BlockModeDecrypt {
/// Decrypt data using `InOutBuf`.
fn decrypt_inout(mut self, data: InOutBuf<'_, '_, u8>) {
let (blocks, mut tail) = data.into_chunks();
self.decrypt_blocks_inout(blocks);
let n = tail.len();
if n != 0 {
let mut block = Block::<Self>::default();
block[..n].copy_from_slice(tail.get_in());
self.decrypt_block(&mut block);
tail.get_out().copy_from_slice(&block[..n]);
}
}

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

/// Decrypt data from buffer to buffer.
fn decrypt_b2b(self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError> {
InOutBuf::new(in_buf, out_buf).map(|b| self.decrypt_inout(b))
}
}
1 change: 1 addition & 0 deletions cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub use inout::block_padding;
#[cfg(feature = "zeroize")]
pub use zeroize;

pub mod async_stream;
pub mod block;
#[cfg(feature = "dev")]
pub mod dev;
Expand Down
68 changes: 0 additions & 68 deletions cipher/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! See the [RustCrypto/stream-ciphers](https://github.com/RustCrypto/stream-ciphers) repository
//! for ciphers implementation.

use crate::block::{BlockModeDecrypt, BlockModeEncrypt};
use crypto_common::Block;
use inout::{InOutBuf, NotEqualError};

mod core_api;
Expand All @@ -20,72 +18,6 @@ pub use errors::{OverflowError, StreamCipherError};
#[cfg(feature = "stream-wrapper")]
pub use wrapper::StreamCipherCoreWrapper;

/// Asynchronous stream cipher trait.
pub trait AsyncStreamCipher: Sized {
/// Encrypt data using `InOutBuf`.
fn encrypt_inout(mut self, data: InOutBuf<'_, '_, u8>)
where
Self: BlockModeEncrypt,
{
let (blocks, mut tail) = data.into_chunks();
self.encrypt_blocks_inout(blocks);
let n = tail.len();
if n != 0 {
let mut block = Block::<Self>::default();
block[..n].copy_from_slice(tail.get_in());
self.encrypt_block(&mut block);
tail.get_out().copy_from_slice(&block[..n]);
}
}

/// Decrypt data using `InOutBuf`.
fn decrypt_inout(mut self, data: InOutBuf<'_, '_, u8>)
where
Self: BlockModeDecrypt,
{
let (blocks, mut tail) = data.into_chunks();
self.decrypt_blocks_inout(blocks);
let n = tail.len();
if n != 0 {
let mut block = Block::<Self>::default();
block[..n].copy_from_slice(tail.get_in());
self.decrypt_block(&mut block);
tail.get_out().copy_from_slice(&block[..n]);
}
}
/// Encrypt data in place.
fn encrypt(self, buf: &mut [u8])
where
Self: BlockModeEncrypt,
{
self.encrypt_inout(buf.into());
}

/// Decrypt data in place.
fn decrypt(self, buf: &mut [u8])
where
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: 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: BlockModeDecrypt,
{
InOutBuf::new(in_buf, out_buf).map(|b| self.decrypt_inout(b))
}
}

/// Stream cipher trait.
///
/// This trait applies only to synchronous stream ciphers, which generate a keystream and
Expand Down