diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 5c8e83365..f3f7d4ac9 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -25,6 +25,8 @@ jobs: override: true profile: minimal - run: cargo clippy --all --all-features -- -D warnings + - run: cargo clippy --all --all-features -- -D warnings + working-directory: cipher - run: cargo clippy --all --all-features -- -D warnings working-directory: crypto - run: cargo clippy --all --all-features -- -D warnings diff --git a/cipher/CHANGELOG.md b/cipher/CHANGELOG.md index 7eee95adc..1ece484db 100644 --- a/cipher/CHANGELOG.md +++ b/cipher/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## UNRELEASED +### Added +- Allocating padded encrypt/decrypt ([#936]) + +[#936]: https://github.com/RustCrypto/traits/pull/936 + ## 0.3.0 (2022-02-10) ### Changed - Major rework of traits. Core functionality of block and stream ciphers diff --git a/cipher/Cargo.toml b/cipher/Cargo.toml index 4ef3d2a6e..572275234 100644 --- a/cipher/Cargo.toml +++ b/cipher/Cargo.toml @@ -25,7 +25,9 @@ blobby = { version = "0.3", optional = true } zeroize = { version = "1.5", optional = true, default-features = false } [features] -std = ["crypto-common/std", "inout/std"] +default = ["alloc"] +alloc = [] +std = ["alloc", "crypto-common/std", "inout/std"] block-padding = ["inout/block-padding"] rand_core = ["crypto-common/rand_core"] # Enable random key and IV generation methods dev = ["blobby"] diff --git a/cipher/src/block.rs b/cipher/src/block.rs index 9c22fe3e8..697e42e6f 100644 --- a/cipher/src/block.rs +++ b/cipher/src/block.rs @@ -11,6 +11,8 @@ //! [3]: https://en.wikipedia.org/wiki/Symmetric-key_algorithm use crate::{ParBlocks, ParBlocksSizeUser}; +#[cfg(all(feature = "block-padding", feature = "alloc"))] +use alloc::{vec, vec::Vec}; #[cfg(feature = "block-padding")] use inout::{ block_padding::{Padding, UnpadError}, @@ -173,6 +175,20 @@ pub trait BlockEncrypt: BlockSizeUser + Sized { let buf = InOutBufReserved::from_slices(msg, out_buf).map_err(|_| PadError)?; self.encrypt_padded_inout::
(buf)
}
+
+ /// Pad input and encrypt into a newly allocated Vec. Returns resulting ciphertext Vec.
+ #[cfg(all(feature = "block-padding", feature = "alloc"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "block-padding", feature = "alloc"))))]
+ #[inline]
+ fn encrypt_padded_vec (msg, &mut out)
+ .expect("enough space for encrypting is allocated")
+ .len();
+ out.truncate(len);
+ out
+ }
}
/// Decrypt-only functionality for block ciphers.
@@ -281,6 +297,24 @@ pub trait BlockDecrypt: BlockSizeUser {
let buf = InOutBuf::new(in_buf, &mut out_buf[..n]).map_err(|_| UnpadError)?;
self.decrypt_padded_inout:: (buf)
}
+
+ /// Decrypt input and unpad it in a newly allocated Vec. Returns resulting
+ /// ciphertext Vec.
+ ///
+ /// Returns [`UnpadError`] if padding is malformed or if input length is
+ /// not multiple of `Self::BlockSize`.
+ #[cfg(all(feature = "block-padding", feature = "alloc"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "block-padding", feature = "alloc"))))]
+ #[inline]
+ fn decrypt_padded_vec (buf, &mut out)?.len();
+ out.truncate(len);
+ Ok(out)
+ }
}
/// Encrypt-only functionality for block ciphers and modes with mutable access to `self`.
@@ -363,11 +397,11 @@ pub trait BlockEncryptMut: BlockSizeUser + Sized {
#[cfg(feature = "block-padding")]
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
#[inline]
- fn encrypt_padded_mut<'a, P: Padding (buf)
}
@@ -386,6 +420,20 @@ pub trait BlockEncryptMut: BlockSizeUser + Sized {
let buf = InOutBufReserved::from_slices(msg, out_buf).map_err(|_| PadError)?;
self.encrypt_padded_inout_mut:: (buf)
}
+
+ /// Pad input and encrypt into a newly allocated Vec. Returns resulting ciphertext Vec.
+ #[cfg(all(feature = "block-padding", feature = "alloc"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "block-padding", feature = "alloc"))))]
+ #[inline]
+ fn encrypt_padded_vec_mut (msg, &mut out)
+ .expect("enough space for encrypting is allocated")
+ .len();
+ out.truncate(len);
+ out
+ }
}
/// Decrypt-only functionality for block ciphers and modes with mutable access to `self`.
@@ -470,10 +518,10 @@ pub trait BlockDecryptMut: BlockSizeUser + Sized {
#[cfg(feature = "block-padding")]
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
#[inline]
- fn decrypt_padded_mut<'a, P: Padding (buf.into())
}
@@ -498,6 +546,24 @@ pub trait BlockDecryptMut: BlockSizeUser + Sized {
let buf = InOutBuf::new(in_buf, &mut out_buf[..n]).map_err(|_| UnpadError)?;
self.decrypt_padded_inout_mut:: (buf)
}
+
+ /// Decrypt input and unpad it in a newly allocated Vec. Returns resulting
+ /// ciphertext Vec.
+ ///
+ /// Returns [`UnpadError`] if padding is malformed or if input length is
+ /// not multiple of `Self::BlockSize`.
+ #[cfg(all(feature = "block-padding", feature = "alloc"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "block-padding", feature = "alloc"))))]
+ #[inline]
+ fn decrypt_padded_vec (buf, &mut out)?.len();
+ out.truncate(len);
+ Ok(out)
+ }
}
impl