From d379be9591f82babbd3fee93f8eb3cf45d5761b9 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: Fri, 26 Sep 2025 15:20:12 +0300 Subject: [PATCH 1/6] block-padding: tweak `pad_detached`, add `PaddedData` enum --- block-padding/src/lib.rs | 72 +++++++++++++++++++++++++++++++++++----- inout/src/reserved.rs | 8 ++++- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/block-padding/src/lib.rs b/block-padding/src/lib.rs index 7fe9da7a..9b195bc9 100644 --- a/block-padding/src/lib.rs +++ b/block-padding/src/lib.rs @@ -48,21 +48,18 @@ pub trait Padding { /// Pad message and return padded tail block. /// - /// `Err` is returned only by [`NoPadding`] if `data` length is not multiple of the block size. - /// [`NoPadding`] and [`ZeroPadding`] return `Ok((blocks, None))` if `data` length - /// is multiple of block size. All other padding implementations should always return - /// `Ok((blocks, Some(tail_block)))`. - #[allow(clippy::type_complexity)] + /// [`PaddedData::Error`] is returned only by [`NoPadding`] if `data` length is not multiple + /// of the block size. [`NoPadding`] and [`ZeroPadding`] return [`PaddedData::NoPad`] + /// if `data` length is multiple of block size. All other padding implementations + /// should always return [`PaddedData::Pad`]. #[inline] - fn pad_detached( - data: &[u8], - ) -> Result<(&[Array], Option>), Error> { + fn pad_detached(data: &[u8]) -> PaddedData<'_, BlockSize> { let (blocks, tail) = Array::slice_as_chunks(data); let mut tail_block = Array::default(); let pos = tail.len(); tail_block[..pos].copy_from_slice(tail); Self::pad(&mut tail_block, pos); - Ok((blocks, Some(tail_block))) + PaddedData::Pad { blocks, tail_block } } /// Unpad data in `blocks` and return unpadded byte slice. @@ -120,6 +117,19 @@ impl Padding for ZeroPadding { Ok(&block[..0]) } + #[inline] + fn pad_detached(data: &[u8]) -> PaddedData<'_, BlockSize> { + let (blocks, tail) = Array::slice_as_chunks(data); + if tail.is_empty() { + return PaddedData::NoPad { blocks }; + } + let mut tail_block = Array::default(); + let pos = tail.len(); + tail_block[..pos].copy_from_slice(tail); + Self::pad(&mut tail_block, pos); + PaddedData::Pad { blocks, tail_block } + } + #[inline] fn unpad_blocks(blocks: &[Array]) -> Result<&[u8], Error> { let buf = Array::slice_as_flattened(blocks); @@ -353,6 +363,16 @@ impl Padding for NoPadding { Ok(block) } + #[inline] + fn pad_detached(data: &[u8]) -> PaddedData<'_, BlockSize> { + let (blocks, tail) = Array::slice_as_chunks(data); + if tail.is_empty() { + PaddedData::NoPad { blocks } + } else { + PaddedData::Error + } + } + #[inline] fn unpad_blocks(blocks: &[Array]) -> Result<&[u8], Error> { Ok(Array::slice_as_flattened(blocks)) @@ -370,3 +390,37 @@ impl fmt::Display for Error { } impl core::error::Error for Error {} + +/// Padded data split into blocks with detached last block returned by [`Padding::pad_detached`]. +#[derive(Debug)] +pub enum PaddedData<'a, BlockSize: ArraySize> { + /// Message split into blocks with detached and padded `tail_block`. + Pad { + /// Message blocks. + blocks: &'a [Array], + /// Last message block with padding. + tail_block: Array, + }, + /// [`NoPadding`] or [`ZeroPadding`] were used on a message which does not require any padding. + NoPad { + /// Message blocks. + blocks: &'a [Array], + }, + /// [`NoPadding`] was used on a message with size not multiple of the block size. + Error, +} + +impl<'a, BlockSize: ArraySize> PaddedData<'a, BlockSize> { + /// Unwrap the `Pad` variant. + pub fn unwrap(self) -> (&'a [Array], Array) { + match self { + PaddedData::Pad { blocks, tail_block } => (blocks, tail_block), + PaddedData::NoPad { .. } => { + panic!("Expected `PaddedData::Pad`, but got `PaddedData::NoPad`"); + } + PaddedData::Error => { + panic!("Expected `PaddedData::Pad`, but got `PaddedData::Error`"); + } + } + } +} diff --git a/inout/src/reserved.rs b/inout/src/reserved.rs index ab143dfe..95152c17 100644 --- a/inout/src/reserved.rs +++ b/inout/src/reserved.rs @@ -160,7 +160,13 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> { { let bs = BS::USIZE; let blocks_len = self.in_len / bs; - let (blocks, tail_block) = P::pad_detached(self.get_in()).map_err(|_| PadError)?; + + use block_padding::PaddedData; + let (blocks, tail_block) = match P::pad_detached(self.get_in()) { + PaddedData::Pad { blocks, tail_block } => (blocks, Some(tail_block)), + PaddedData::NoPad { blocks } => (blocks, None), + PaddedData::Error => return Err(PadError), + }; assert_eq!(blocks.len(), blocks_len); From bab636b441cff67b3e42f6c66e368b6e1831436e 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: Fri, 26 Sep 2025 15:23:37 +0300 Subject: [PATCH 2/6] update changelog --- block-padding/CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block-padding/CHANGELOG.md b/block-padding/CHANGELOG.md index 4a21889e..e1171828 100644 --- a/block-padding/CHANGELOG.md +++ b/block-padding/CHANGELOG.md @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 0.4.0 (unreleased) ### Added -- `Padding::pad_detached` method ([#1225]) +- `Padding::pad_detached` method ([#1225], [#1227]) +- `PaddedData` enum ([#1227]) ### Changed - Migrated from `generic-array` to `hybrid-array` ([#944]) @@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#1149]: https://github.com/RustCrypto/utils/pull/1149 [#1217]: https://github.com/RustCrypto/utils/pull/1217 [#1225]: https://github.com/RustCrypto/utils/pull/1225 +[#1227]: https://github.com/RustCrypto/utils/pull/1227 ## 0.3.3 (2023-04-02) ### Added From 73eacbfd1d6b633345eef2baeecaf5cbf881e6be 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: Mon, 6 Oct 2025 16:07:40 +0300 Subject: [PATCH 3/6] Update changelog --- block-padding/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/block-padding/CHANGELOG.md b/block-padding/CHANGELOG.md index b07efa8e..5d703ab1 100644 --- a/block-padding/CHANGELOG.md +++ b/block-padding/CHANGELOG.md @@ -4,9 +4,13 @@ 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). +## 0.4.1 (unreleased) +### Fixed +- Implementation of the `Padding::pad_detached` method for `NoPadding` and `ZeroPadding` ([#1227]) + ## 0.4.0 (2025-10-06) ### Added -- `Padding::pad_detached` method ([#1225], [#1227]) +- `Padding::pad_detached` method ([#1225]) - `PaddedData` enum ([#1227]) ### Changed From 58c8920158371268c656b4d5dc919626c0d005da 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: Mon, 6 Oct 2025 16:11:35 +0300 Subject: [PATCH 4/6] Update changelog --- block-padding/CHANGELOG.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/block-padding/CHANGELOG.md b/block-padding/CHANGELOG.md index 5d703ab1..72e6e08b 100644 --- a/block-padding/CHANGELOG.md +++ b/block-padding/CHANGELOG.md @@ -4,14 +4,18 @@ 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). -## 0.4.1 (unreleased) +## 0.4.1 (2025-10-06) +### Changed +- `Padding::pad_detached` method returns `PaddedData` enum ([#1227]) + ### Fixed -- Implementation of the `Padding::pad_detached` method for `NoPadding` and `ZeroPadding` ([#1227]) +- `Padding::pad_detached` method for `NoPadding` and `ZeroPadding` ([#1227]) -## 0.4.0 (2025-10-06) +[#1227]: https://github.com/RustCrypto/utils/pull/1227 + +## 0.4.0 (2025-10-06) [YANKED] ### Added - `Padding::pad_detached` method ([#1225]) -- `PaddedData` enum ([#1227]) ### Changed - Migrated from `generic-array` to `hybrid-array` ([#944]) @@ -27,7 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#1149]: https://github.com/RustCrypto/utils/pull/1149 [#1217]: https://github.com/RustCrypto/utils/pull/1217 [#1225]: https://github.com/RustCrypto/utils/pull/1225 -[#1227]: https://github.com/RustCrypto/utils/pull/1227 ## 0.3.3 (2023-04-02) ### Added From df4ef3a77330b51dd28448359a2256845e21f906 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: Mon, 6 Oct 2025 16:14:30 +0300 Subject: [PATCH 5/6] Bump block-padding and inout versions --- Cargo.lock | 4 ++-- block-padding/Cargo.toml | 2 +- inout/CHANGELOG.md | 8 +++++++- inout/Cargo.toml | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4f547e8..052a5f1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,7 +26,7 @@ dependencies = [ [[package]] name = "block-padding" -version = "0.4.0" +version = "0.4.1" dependencies = [ "hybrid-array", ] @@ -130,7 +130,7 @@ dependencies = [ [[package]] name = "inout" -version = "0.2.0" +version = "0.2.1" dependencies = [ "block-padding", "hybrid-array", diff --git a/block-padding/Cargo.toml b/block-padding/Cargo.toml index 7376d6d4..6c6e7489 100644 --- a/block-padding/Cargo.toml +++ b/block-padding/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "block-padding" -version = "0.4.0" +version = "0.4.1" authors = ["RustCrypto Developers"] edition = "2024" rust-version = "1.85" diff --git a/inout/CHANGELOG.md b/inout/CHANGELOG.md index 879d5853..52b3c4c3 100644 --- a/inout/CHANGELOG.md +++ b/inout/CHANGELOG.md @@ -4,7 +4,13 @@ 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). -## 0.2.0 (2025-10-06) +## 0.2.1 (2025-10-06) +### Changed +- Migrate to fixed `Padding::pad_detached` from `block-padding` v0.4.1 ([#1227]) + +[#1227]: https://github.com/RustCrypto/utils/pull/1227 + +## 0.2.0 (2025-10-06) [YANKED] ### Changed - Migrated from `generic-array` to `hybrid-array` ([#944]) - Edition changed to 2024 and MSRV bumped to 1.85 ([#1149]) diff --git a/inout/Cargo.toml b/inout/Cargo.toml index 00330fc4..aa667fdd 100644 --- a/inout/Cargo.toml +++ b/inout/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "inout" -version = "0.2.0" +version = "0.2.1" authors = ["RustCrypto Developers"] edition = "2024" rust-version = "1.85" From a8641c4ce6710f3a7b55351c8f375f640f37727b 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: Mon, 6 Oct 2025 16:16:20 +0300 Subject: [PATCH 6/6] update changelog --- block-padding/CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/block-padding/CHANGELOG.md b/block-padding/CHANGELOG.md index 72e6e08b..6b9cd109 100644 --- a/block-padding/CHANGELOG.md +++ b/block-padding/CHANGELOG.md @@ -5,8 +5,11 @@ 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). ## 0.4.1 (2025-10-06) +### Added +- `PaddedData` enum ([#1227]) + ### Changed -- `Padding::pad_detached` method returns `PaddedData` enum ([#1227]) +- `Padding::pad_detached` method returns `PaddedData` ([#1227]) ### Fixed - `Padding::pad_detached` method for `NoPadding` and `ZeroPadding` ([#1227])