Skip to content

Commit ad22c7b

Browse files
authored
Replace AsynStreamCipher impl with inherent methods in CFB crates (#99)
It's a quick-and-dirty solution for the removal of the trait from `cipher`. I plan to do a more involved rework later.
1 parent 49199e9 commit ad22c7b

8 files changed

Lines changed: 145 additions & 33 deletions

File tree

cfb-mode/src/decrypt.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use cipher::{
2-
AlgorithmName, Array, AsyncStreamCipher, Block, BlockCipherDecrypt, BlockCipherEncBackend,
3-
BlockCipherEncClosure, BlockCipherEncrypt, BlockModeDecBackend, BlockModeDecClosure,
4-
BlockModeDecrypt, BlockSizeUser, InOut, InnerIvInit, Iv, IvSizeUser, IvState, ParBlocks,
5-
ParBlocksSizeUser,
2+
AlgorithmName, Array, Block, BlockCipherDecrypt, BlockCipherEncBackend, BlockCipherEncClosure,
3+
BlockCipherEncrypt, BlockModeDecBackend, BlockModeDecClosure, BlockModeDecrypt, BlockSizeUser,
4+
InnerIvInit, Iv, IvSizeUser, IvState, ParBlocks, ParBlocksSizeUser,
65
common::{BlockSizes, InnerUser},
6+
inout::{InOut, InOutBuf, NotEqualError},
77
typenum::Unsigned,
88
};
99
use core::fmt;
@@ -186,7 +186,36 @@ where
186186
}
187187
}
188188

189-
impl<C> AsyncStreamCipher for Decryptor<C> where C: BlockCipherEncrypt {}
189+
impl<C> Decryptor<C>
190+
where
191+
C: BlockCipherEncrypt,
192+
{
193+
/// Decrypt data using `InOutBuf`.
194+
pub fn decrypt_inout(mut self, data: InOutBuf<'_, '_, u8>) {
195+
let (blocks, mut tail) = data.into_chunks();
196+
self.decrypt_blocks_inout(blocks);
197+
let n = tail.len();
198+
if n != 0 {
199+
let mut block = Block::<Self>::default();
200+
block[..n].copy_from_slice(tail.get_in());
201+
self.decrypt_block(&mut block);
202+
tail.get_out().copy_from_slice(&block[..n]);
203+
}
204+
}
205+
206+
/// Decrypt data in place.
207+
pub fn decrypt(self, buf: &mut [u8]) {
208+
self.decrypt_inout(buf.into());
209+
}
210+
211+
/// Decrypt data from buffer to buffer.
212+
///
213+
/// # Errors
214+
/// If `in_buf` and `out_buf` have different lengths.
215+
pub fn decrypt_b2b(self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError> {
216+
InOutBuf::new(in_buf, out_buf).map(|b| self.decrypt_inout(b))
217+
}
218+
}
190219

191220
impl<C> InnerUser for Decryptor<C>
192221
where

cfb-mode/src/encrypt.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use cipher::{
2-
AlgorithmName, AsyncStreamCipher, Block, BlockCipherDecrypt, BlockCipherEncBackend,
3-
BlockCipherEncClosure, BlockCipherEncrypt, BlockModeEncBackend, BlockModeEncClosure,
4-
BlockModeEncrypt, BlockSizeUser, InOut, InnerIvInit, Iv, IvSizeUser, IvState,
5-
ParBlocksSizeUser,
2+
AlgorithmName, Block, BlockCipherDecrypt, BlockCipherEncBackend, BlockCipherEncClosure,
3+
BlockCipherEncrypt, BlockModeEncBackend, BlockModeEncClosure, BlockModeEncrypt, BlockSizeUser,
4+
InnerIvInit, Iv, IvSizeUser, IvState, ParBlocksSizeUser,
65
array::Array,
76
common::{BlockSizes, InnerUser},
87
consts::U1,
8+
inout::{InOut, InOutBuf, NotEqualError},
99
};
1010
use core::fmt;
1111

@@ -75,7 +75,36 @@ where
7575
}
7676
}
7777

78-
impl<C> AsyncStreamCipher for Encryptor<C> where C: BlockCipherEncrypt {}
78+
impl<C> Encryptor<C>
79+
where
80+
C: BlockCipherEncrypt,
81+
{
82+
/// Encrypt data using `InOutBuf`.
83+
pub fn encrypt_inout(mut self, data: InOutBuf<'_, '_, u8>) {
84+
let (blocks, mut tail) = data.into_chunks();
85+
self.encrypt_blocks_inout(blocks);
86+
let n = tail.len();
87+
if n != 0 {
88+
let mut block = Block::<Self>::default();
89+
block[..n].copy_from_slice(tail.get_in());
90+
self.encrypt_block(&mut block);
91+
tail.get_out().copy_from_slice(&block[..n]);
92+
}
93+
}
94+
95+
/// Encrypt data in place.
96+
pub fn encrypt(self, buf: &mut [u8]) {
97+
self.encrypt_inout(buf.into());
98+
}
99+
100+
/// Encrypt data from buffer to buffer.
101+
///
102+
/// # Errors
103+
/// If `in_buf` and `out_buf` have different lengths.
104+
pub fn encrypt_b2b(self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError> {
105+
InOutBuf::new(in_buf, out_buf).map(|b| self.encrypt_inout(b))
106+
}
107+
}
79108

80109
impl<C> InnerUser for Encryptor<C>
81110
where

cfb-mode/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//!
1515
//! # Example
1616
//! ```
17-
//! use aes::cipher::{AsyncStreamCipher, KeyIvInit};
17+
//! use aes::cipher::KeyIvInit;
1818
//! use hex_literal::hex;
1919
//!
2020
//! type Aes128CfbEnc = cfb_mode::Encryptor<aes::Aes128>;

cfb-mode/tests/async_stream.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
//! Tests for the `AsyncStreamCipher` trait methods
1+
//! Tests for inherent methods
22
use aes::*;
33
use cfb_mode::{BufDecryptor, BufEncryptor, Decryptor, Encryptor};
4-
use cipher::KeyInit;
4+
use cipher::{KeyInit, KeyIvInit};
55

66
#[test]
77
fn aes128_cfb_async_test() {
8-
use cipher::{AsyncStreamCipher, KeyIvInit};
9-
108
type Enc = Encryptor<Aes128>;
119
type Dec = Decryptor<Aes128>;
1210

@@ -34,8 +32,6 @@ fn aes128_cfb_async_test() {
3432

3533
#[test]
3634
fn aes128_cfb_buffered_test() {
37-
use cipher::{AsyncStreamCipher, KeyIvInit};
38-
3935
type Enc = Encryptor<Aes128>;
4036

4137
type BufEnc = BufEncryptor<Aes128>;

cfb8/src/decrypt.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use cipher::{
2-
AlgorithmName, AsyncStreamCipher, Block, BlockCipherEncBackend, BlockCipherEncClosure,
3-
BlockCipherEncrypt, BlockModeDecBackend, BlockModeDecClosure, BlockModeDecrypt, BlockSizeUser,
4-
InnerIvInit, Iv, IvState, ParBlocksSizeUser,
2+
AlgorithmName, Block, BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt,
3+
BlockModeDecBackend, BlockModeDecClosure, BlockModeDecrypt, BlockSizeUser, InnerIvInit, Iv,
4+
IvState, ParBlocksSizeUser,
55
array::Array,
66
common::{BlockSizes, InnerUser, IvSizeUser},
77
consts::U1,
8-
inout::InOut,
8+
inout::{InOut, InOutBuf, NotEqualError},
99
};
1010
use core::fmt;
1111

@@ -67,7 +67,36 @@ where
6767
}
6868
}
6969

70-
impl<C: BlockCipherEncrypt> AsyncStreamCipher for Decryptor<C> {}
70+
impl<C> Decryptor<C>
71+
where
72+
C: BlockCipherEncrypt,
73+
{
74+
/// Decrypt data using `InOutBuf`.
75+
pub fn decrypt_inout(&mut self, data: InOutBuf<'_, '_, u8>) {
76+
let (blocks, mut tail) = data.into_chunks();
77+
self.decrypt_blocks_inout(blocks);
78+
let n = tail.len();
79+
if n != 0 {
80+
let mut block = Block::<Self>::default();
81+
block[..n].copy_from_slice(tail.get_in());
82+
self.decrypt_block(&mut block);
83+
tail.get_out().copy_from_slice(&block[..n]);
84+
}
85+
}
86+
87+
/// Decrypt data in place.
88+
pub fn decrypt(&mut self, buf: &mut [u8]) {
89+
self.decrypt_inout(buf.into());
90+
}
91+
92+
/// Decrypt data from buffer to buffer.
93+
///
94+
/// # Errors
95+
/// If `in_buf` and `out_buf` have different lengths.
96+
pub fn decrypt_b2b(&mut self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError> {
97+
InOutBuf::new(in_buf, out_buf).map(|b| self.decrypt_inout(b))
98+
}
99+
}
71100

72101
impl<C> InnerUser for Decryptor<C>
73102
where

cfb8/src/encrypt.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use cipher::{
2-
AlgorithmName, AsyncStreamCipher, Block, BlockCipherEncBackend, BlockCipherEncClosure,
3-
BlockCipherEncrypt, BlockModeEncBackend, BlockModeEncClosure, BlockModeEncrypt, BlockSizeUser,
4-
InnerIvInit, Iv, IvState, ParBlocksSizeUser,
2+
AlgorithmName, Block, BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt,
3+
BlockModeEncBackend, BlockModeEncClosure, BlockModeEncrypt, BlockSizeUser, InnerIvInit, Iv,
4+
IvState, ParBlocksSizeUser,
55
array::Array,
66
common::{BlockSizes, InnerUser, IvSizeUser},
77
consts::U1,
8-
inout::InOut,
8+
inout::{InOut, InOutBuf, NotEqualError},
99
};
1010
use core::fmt;
1111

@@ -67,7 +67,36 @@ where
6767
}
6868
}
6969

70-
impl<C: BlockCipherEncrypt> AsyncStreamCipher for Encryptor<C> {}
70+
impl<C> Encryptor<C>
71+
where
72+
C: BlockCipherEncrypt,
73+
{
74+
/// Encrypt data using `InOutBuf`.
75+
pub fn encrypt_inout(&mut self, data: InOutBuf<'_, '_, u8>) {
76+
let (blocks, mut tail) = data.into_chunks();
77+
self.encrypt_blocks_inout(blocks);
78+
let n = tail.len();
79+
if n != 0 {
80+
let mut block = Block::<Self>::default();
81+
block[..n].copy_from_slice(tail.get_in());
82+
self.encrypt_block(&mut block);
83+
tail.get_out().copy_from_slice(&block[..n]);
84+
}
85+
}
86+
87+
/// Encrypt data in place.
88+
pub fn encrypt(&mut self, buf: &mut [u8]) {
89+
self.encrypt_inout(buf.into());
90+
}
91+
92+
/// Encrypt data from buffer to buffer.
93+
///
94+
/// # Errors
95+
/// If `in_buf` and `out_buf` have different lengths.
96+
pub fn encrypt_b2b(&mut self, in_buf: &[u8], out_buf: &mut [u8]) -> Result<(), NotEqualError> {
97+
InOutBuf::new(in_buf, out_buf).map(|b| self.encrypt_inout(b))
98+
}
99+
}
71100

72101
impl<C> InnerUser for Encryptor<C>
73102
where

cfb8/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//!
1515
//! # Example
1616
//! ```
17-
//! use aes::cipher::{AsyncStreamCipher, KeyIvInit};
17+
//! use aes::cipher::KeyIvInit;
1818
//! use hex_literal::hex;
1919
//!
2020
//! type Aes128Cfb8Enc = cfb8::Encryptor<aes::Aes128>;

cfb8/tests/async_stream.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Tests for the [`AsyncStreamCipher`] methods.
1+
//! Tests for inherent methods.
22
use aes::*;
33
use cfb8::{Decryptor, Encryptor};
4-
use cipher::{AsyncStreamCipher, KeyIvInit};
4+
use cipher::KeyIvInit;
55

66
#[test]
77
fn aes128_cfb8_async_test() {
@@ -14,17 +14,17 @@ fn aes128_cfb8_async_test() {
1414
for (i, b) in pt.iter_mut().enumerate() {
1515
*b = (i % 11) as u8;
1616
}
17-
let enc = Enc::new_from_slices(&key, &iv).unwrap();
17+
let mut enc = Enc::new_from_slices(&key, &iv).unwrap();
1818
let mut ct = pt;
1919
enc.encrypt(&mut ct);
2020
for i in 1..100 {
21-
let enc = Enc::new_from_slices(&key, &iv).unwrap();
21+
let mut enc = Enc::new_from_slices(&key, &iv).unwrap();
2222
let mut t = pt;
2323
let t = &mut t[..i];
2424
enc.encrypt(t);
2525
assert_eq!(t, &ct[..i]);
2626

27-
let dec = Dec::new_from_slices(&key, &iv).unwrap();
27+
let mut dec = Dec::new_from_slices(&key, &iv).unwrap();
2828
dec.decrypt(t);
2929
assert_eq!(t, &pt[..i]);
3030
}

0 commit comments

Comments
 (0)