From a55befdd3d6fcd48808237142b90467a0b81223a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 20 Sep 2025 15:13:37 +0300 Subject: [PATCH 1/4] block-padding: merge `Padding` and `RawPadding` traits --- block-padding/src/lib.rs | 51 ++++++++++++---------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/block-padding/src/lib.rs b/block-padding/src/lib.rs index 912cf7ff..ae3f211b 100644 --- a/block-padding/src/lib.rs +++ b/block-padding/src/lib.rs @@ -26,8 +26,8 @@ pub enum PadType { NoPadding, } -/// Trait for padding messages divided into blocks of arbitrary size -pub trait RawPadding { +/// Trait for messages padding algorithms. +pub trait Padding { /// Padding type const TYPE: PadType; @@ -43,15 +43,7 @@ pub trait RawPadding { /// /// Returns `Err(UnpadError)` if the block contains malformed padding. fn raw_unpad(block: &[u8]) -> Result<&[u8], UnpadError>; -} - -/// Block size. -pub type Block = Array; -/// Trait for padding messages divided into blocks -pub trait Padding { - /// Padding type - const TYPE: PadType; /// Pads `block` filled with data up to `pos` (i.e length of a message /// stored in the block is equal to `pos`). @@ -59,17 +51,21 @@ pub trait Padding { /// # Panics /// If `pos` is bigger than `BlockSize`. Most padding algorithms also /// panic if they are equal. - fn pad(block: &mut Block, pos: usize); + fn pad(block: &mut Array, pos: usize) { + Self::raw_pad(block.as_mut_slice(), pos); + } /// Unpad data in the `block`. /// /// Returns `Err(UnpadError)` if the block contains malformed padding. - fn unpad(block: &Block) -> Result<&[u8], UnpadError>; + fn unpad(block: &Array) -> Result<&[u8], UnpadError>{ + Self::raw_unpad(block.as_slice()) + } /// Unpad data in the `blocks`. /// /// Returns `Err(UnpadError)` if the block contains malformed padding. - fn unpad_blocks(blocks: &[Block]) -> Result<&[u8], UnpadError> { + fn unpad_blocks(blocks: &[Array]) -> Result<&[u8], UnpadError> { let bs = BlockSize::USIZE; let res_len = match (blocks.last(), Self::TYPE) { (_, PadType::NoPadding) => bs * blocks.len(), @@ -89,23 +85,6 @@ pub trait Padding { } } -impl Padding for T -where - T: RawPadding, -{ - const TYPE: PadType = T::TYPE; - - #[inline] - fn pad(block: &mut Block, pos: usize) { - T::raw_pad(block.as_mut_slice(), pos); - } - - #[inline] - fn unpad(block: &Block) -> Result<&[u8], UnpadError> { - T::raw_unpad(block.as_slice()) - } -} - /// Pad block with zeros. /// /// ``` @@ -127,7 +106,7 @@ where #[derive(Clone, Copy, Debug)] pub struct ZeroPadding; -impl RawPadding for ZeroPadding { +impl Padding for ZeroPadding { const TYPE: PadType = PadType::Ambiguous; #[inline] @@ -189,7 +168,7 @@ impl Pkcs7 { } } -impl RawPadding for Pkcs7 { +impl Padding for Pkcs7 { const TYPE: PadType = PadType::Reversible; #[inline] @@ -231,7 +210,7 @@ impl RawPadding for Pkcs7 { #[derive(Clone, Copy, Debug)] pub struct Iso10126; -impl RawPadding for Iso10126 { +impl Padding for Iso10126 { const TYPE: PadType = PadType::Reversible; #[inline] @@ -266,7 +245,7 @@ impl RawPadding for Iso10126 { #[derive(Clone, Copy, Debug)] pub struct AnsiX923; -impl RawPadding for AnsiX923 { +impl Padding for AnsiX923 { const TYPE: PadType = PadType::Reversible; #[inline] @@ -320,7 +299,7 @@ impl RawPadding for AnsiX923 { #[derive(Clone, Copy, Debug)] pub struct Iso7816; -impl RawPadding for Iso7816 { +impl Padding for Iso7816 { const TYPE: PadType = PadType::Reversible; #[inline] @@ -369,7 +348,7 @@ impl RawPadding for Iso7816 { #[derive(Clone, Copy, Debug)] pub struct NoPadding; -impl RawPadding for NoPadding { +impl Padding for NoPadding { const TYPE: PadType = PadType::NoPadding; #[inline] From d188b5554f1aaad26dfa28d5b8cdac55f123d6c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 20 Sep 2025 15:17:08 +0300 Subject: [PATCH 2/4] fmt --- block-padding/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/block-padding/src/lib.rs b/block-padding/src/lib.rs index ae3f211b..da556015 100644 --- a/block-padding/src/lib.rs +++ b/block-padding/src/lib.rs @@ -44,7 +44,6 @@ pub trait Padding { /// Returns `Err(UnpadError)` if the block contains malformed padding. fn raw_unpad(block: &[u8]) -> Result<&[u8], UnpadError>; - /// Pads `block` filled with data up to `pos` (i.e length of a message /// stored in the block is equal to `pos`). /// @@ -58,14 +57,16 @@ pub trait Padding { /// Unpad data in the `block`. /// /// Returns `Err(UnpadError)` if the block contains malformed padding. - fn unpad(block: &Array) -> Result<&[u8], UnpadError>{ + fn unpad(block: &Array) -> Result<&[u8], UnpadError> { Self::raw_unpad(block.as_slice()) } /// Unpad data in the `blocks`. /// /// Returns `Err(UnpadError)` if the block contains malformed padding. - fn unpad_blocks(blocks: &[Array]) -> Result<&[u8], UnpadError> { + fn unpad_blocks( + blocks: &[Array], + ) -> Result<&[u8], UnpadError> { let bs = BlockSize::USIZE; let res_len = match (blocks.last(), Self::TYPE) { (_, PadType::NoPadding) => bs * blocks.len(), From 099fa61437e6f4257c48a5f397026ec6a63c9c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 20 Sep 2025 15:18:44 +0300 Subject: [PATCH 3/4] fix inout --- inout/src/reserved.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inout/src/reserved.rs b/inout/src/reserved.rs index 635d323e..701f8b50 100644 --- a/inout/src/reserved.rs +++ b/inout/src/reserved.rs @@ -155,7 +155,7 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> { #[inline(always)] pub fn into_padded_blocks(self) -> Result, PadError> where - P: Padding, + P: Padding, BS: ArraySize, { let bs = BS::USIZE; From 7024945d97cd5b9f81888ef0b7ac711b668a091e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 20 Sep 2025 15:20:08 +0300 Subject: [PATCH 4/4] Update changelog --- block-padding/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/block-padding/CHANGELOG.md b/block-padding/CHANGELOG.md index 91bbd955..d624b500 100644 --- a/block-padding/CHANGELOG.md +++ b/block-padding/CHANGELOG.md @@ -8,9 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Migrated from `generic-array` to `hybrid-array` ([#944]) - Edition changed to 2024 and MSRV bumped to 1.85 ([#1149]) +- Merged `RawPadding` and `Padding` traits ([#1217]) + +### Removed +- `Block` type alias ([#1217]) [#944]: https://github.com/RustCrypto/utils/pull/944 [#1149]: https://github.com/RustCrypto/utils/pull/1149 +[#1217]: https://github.com/RustCrypto/utils/pull/1217 ## 0.3.3 (2023-04-02) ### Added