Skip to content
Merged
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
5 changes: 5 additions & 0 deletions cipher/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Seeking implementation in the stream cipher wrapper ([#2052])

### Removed
- `BlockCipherEncrypt::encrypt_padded*` and `BlockCipherDecrypt::decrypt_padded*` methods.
Users of the ECB mode should use the `ecb-mode` crate instead. ([#2245])

[#1759]: https://github.com/RustCrypto/traits/pull/1759
[#2052]: https://github.com/RustCrypto/traits/pull/2052
[#2237]: https://github.com/RustCrypto/traits/pull/2237
[#2245]: https://github.com/RustCrypto/traits/pull/2245

## 0.4.4 (2022-03-09)
### Changed
Expand Down
150 changes: 0 additions & 150 deletions cipher/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,90 +81,6 @@ pub trait BlockCipherEncrypt: BlockSizeUser + Sized {
InOutBuf::new(in_blocks, out_blocks)
.map(|blocks| self.encrypt_with_backend(BlocksCtx { blocks }))
}

/// Pad input and encrypt. Returns resulting ciphertext slice.
///
/// Returns [`PadError`] if length of output buffer is not sufficient.
#[cfg(feature = "block-padding")]
#[inline]
fn encrypt_padded_inout<'out, P: Padding>(
&self,
data: InOutBufReserved<'_, 'out, u8>,
) -> Result<&'out [u8], PadError> {
let mut buf = data.into_padded_blocks::<P, Self::BlockSize>()?;
self.encrypt_blocks_inout(buf.get_blocks());
if let Some(block) = buf.get_tail_block() {
self.encrypt_block_inout(block);
}
Ok(buf.into_out())
}

/// Pad input and encrypt in-place. Returns resulting ciphertext slice.
///
/// Returns [`PadError`] if length of output buffer is not sufficient.
#[cfg(feature = "block-padding")]
#[inline]
fn encrypt_padded<'a, P: Padding>(
&self,
buf: &'a mut [u8],
msg_len: usize,
) -> Result<&'a [u8], PadError> {
let buf = InOutBufReserved::from_mut_slice(buf, msg_len).map_err(|_| PadError)?;
self.encrypt_padded_inout::<P>(buf)
}

/// Pad input and encrypt buffer-to-buffer. Returns resulting ciphertext slice.
///
/// Returns [`PadError`] if length of output buffer is not sufficient.
#[cfg(feature = "block-padding")]
#[inline]
fn encrypt_padded_b2b<'a, P: Padding>(
&self,
msg: &[u8],
out_buf: &'a mut [u8],
) -> Result<&'a [u8], PadError> {
let buf = InOutBufReserved::from_slices(msg, out_buf).map_err(|_| PadError)?;
self.encrypt_padded_inout::<P>(buf)
}

/// Pad `msg` with padding algorithm `P`, encrypt it into a newly allocated `Vec`,
/// and return the resulting ciphertext vector.
///
/// # Panics
/// If `NoPadding` is used with a message size that is not a multiple of the cipher block size.
#[cfg(all(feature = "block-padding", feature = "alloc"))]
#[inline]
fn encrypt_padded_vec<P: Padding>(&self, msg: &[u8]) -> Vec<u8> {
use block_padding::{NoPadding, ZeroPadding};
use common::typenum::Unsigned;
use core::any::TypeId;

let bs = Self::BlockSize::USIZE;
let msg_len = msg.len();

let pad_type_id = TypeId::of::<P>();
let buf_blocks_len = if pad_type_id == TypeId::of::<NoPadding>() {
if msg_len % bs != 0 {
panic!(
"NoPadding is used with a {msg_len}‑byte message,
which is not a multiple of the {bs}‑byte cipher block size"
);
}
msg_len / bs
} else if pad_type_id == TypeId::of::<ZeroPadding>() {
msg_len.div_ceil(bs)
} else {
1 + msg_len / bs
};

let mut buf = vec![0; bs * buf_blocks_len];
let res_len = self
.encrypt_padded_b2b::<P>(msg, &mut buf)
.expect("`buf` has enough space for encryption")
.len();
buf.truncate(res_len);
buf
}
}

/// Decrypt-only functionality for block ciphers.
Expand Down Expand Up @@ -218,72 +134,6 @@ pub trait BlockCipherDecrypt: BlockSizeUser {
InOutBuf::new(in_blocks, out_blocks)
.map(|blocks| self.decrypt_with_backend(BlocksCtx { blocks }))
}

/// Decrypt input and unpad it. Returns resulting plaintext slice.
///
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
/// not multiple of `Self::BlockSize`.
#[cfg(feature = "block-padding")]
#[inline]
fn decrypt_padded_inout<'out, P: Padding>(
&self,
data: InOutBuf<'_, 'out, u8>,
) -> Result<&'out [u8], block_padding::Error> {
let (mut blocks, tail) = data.into_chunks();
if !tail.is_empty() {
return Err(block_padding::Error);
}
self.decrypt_blocks_inout(blocks.reborrow());
P::unpad_blocks::<Self::BlockSize>(blocks.into_out())
}

/// Decrypt input and unpad it in-place. Returns resulting plaintext slice.
///
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
/// not multiple of `Self::BlockSize`.
#[cfg(feature = "block-padding")]
#[inline]
fn decrypt_padded<'a, P: Padding>(
&self,
buf: &'a mut [u8],
) -> Result<&'a [u8], block_padding::Error> {
self.decrypt_padded_inout::<P>(buf.into())
}

/// Decrypt input and unpad it buffer-to-buffer. Returns resulting
/// plaintext slice.
///
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
/// not multiple of `Self::BlockSize`.
#[cfg(feature = "block-padding")]
#[inline]
fn decrypt_padded_b2b<'a, P: Padding>(
&self,
in_buf: &[u8],
out_buf: &'a mut [u8],
) -> Result<&'a [u8], block_padding::Error> {
if out_buf.len() < in_buf.len() {
return Err(block_padding::Error);
}
let n = in_buf.len();
// note: `new` always returns `Ok` here
let buf = InOutBuf::new(in_buf, &mut out_buf[..n]).map_err(|_| block_padding::Error)?;
self.decrypt_padded_inout::<P>(buf)
}

/// Decrypt input and unpad it in a newly allocated Vec. Returns resulting
/// plaintext `Vec`.
///
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
/// not multiple of `Self::BlockSize`.
#[cfg(all(feature = "block-padding", feature = "alloc"))]
#[inline]
fn decrypt_padded_vec<P: Padding>(&self, buf: &[u8]) -> Result<Vec<u8>, block_padding::Error> {
let mut out = vec![0; buf.len()];
let len = self.decrypt_padded_b2b::<P>(buf, &mut out)?.len();
out.truncate(len);
Ok(out)
}
}

impl<Alg: BlockCipherEncrypt> BlockCipherEncrypt for &Alg {
Expand Down