Skip to content

Commit 014d8f0

Browse files
authored
block-padding: merge Padding and RawPadding traits (#1217)
1 parent 7d8fcb9 commit 014d8f0

File tree

3 files changed

+23
-38
lines changed

3 files changed

+23
-38
lines changed

block-padding/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Changed
99
- Migrated from `generic-array` to `hybrid-array` ([#944])
1010
- Edition changed to 2024 and MSRV bumped to 1.85 ([#1149])
11+
- Merged `RawPadding` and `Padding` traits ([#1217])
12+
13+
### Removed
14+
- `Block` type alias ([#1217])
1115

1216
[#944]: https://github.com/RustCrypto/utils/pull/944
1317
[#1149]: https://github.com/RustCrypto/utils/pull/1149
18+
[#1217]: https://github.com/RustCrypto/utils/pull/1217
1419

1520
## 0.3.3 (2023-04-02)
1621
### Added

block-padding/src/lib.rs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub enum PadType {
2626
NoPadding,
2727
}
2828

29-
/// Trait for padding messages divided into blocks of arbitrary size
30-
pub trait RawPadding {
29+
/// Trait for messages padding algorithms.
30+
pub trait Padding {
3131
/// Padding type
3232
const TYPE: PadType;
3333

@@ -43,33 +43,30 @@ pub trait RawPadding {
4343
///
4444
/// Returns `Err(UnpadError)` if the block contains malformed padding.
4545
fn raw_unpad(block: &[u8]) -> Result<&[u8], UnpadError>;
46-
}
47-
48-
/// Block size.
49-
pub type Block<B> = Array<u8, B>;
50-
51-
/// Trait for padding messages divided into blocks
52-
pub trait Padding<BlockSize: ArraySize> {
53-
/// Padding type
54-
const TYPE: PadType;
5546

5647
/// Pads `block` filled with data up to `pos` (i.e length of a message
5748
/// stored in the block is equal to `pos`).
5849
///
5950
/// # Panics
6051
/// If `pos` is bigger than `BlockSize`. Most padding algorithms also
6152
/// panic if they are equal.
62-
fn pad(block: &mut Block<BlockSize>, pos: usize);
53+
fn pad<BlockSize: ArraySize>(block: &mut Array<u8, BlockSize>, pos: usize) {
54+
Self::raw_pad(block.as_mut_slice(), pos);
55+
}
6356

6457
/// Unpad data in the `block`.
6558
///
6659
/// Returns `Err(UnpadError)` if the block contains malformed padding.
67-
fn unpad(block: &Block<BlockSize>) -> Result<&[u8], UnpadError>;
60+
fn unpad<BlockSize: ArraySize>(block: &Array<u8, BlockSize>) -> Result<&[u8], UnpadError> {
61+
Self::raw_unpad(block.as_slice())
62+
}
6863

6964
/// Unpad data in the `blocks`.
7065
///
7166
/// Returns `Err(UnpadError)` if the block contains malformed padding.
72-
fn unpad_blocks(blocks: &[Block<BlockSize>]) -> Result<&[u8], UnpadError> {
67+
fn unpad_blocks<BlockSize: ArraySize>(
68+
blocks: &[Array<u8, BlockSize>],
69+
) -> Result<&[u8], UnpadError> {
7370
let bs = BlockSize::USIZE;
7471
let res_len = match (blocks.last(), Self::TYPE) {
7572
(_, PadType::NoPadding) => bs * blocks.len(),
@@ -89,23 +86,6 @@ pub trait Padding<BlockSize: ArraySize> {
8986
}
9087
}
9188

92-
impl<T, B: ArraySize> Padding<B> for T
93-
where
94-
T: RawPadding,
95-
{
96-
const TYPE: PadType = T::TYPE;
97-
98-
#[inline]
99-
fn pad(block: &mut Block<B>, pos: usize) {
100-
T::raw_pad(block.as_mut_slice(), pos);
101-
}
102-
103-
#[inline]
104-
fn unpad(block: &Block<B>) -> Result<&[u8], UnpadError> {
105-
T::raw_unpad(block.as_slice())
106-
}
107-
}
108-
10989
/// Pad block with zeros.
11090
///
11191
/// ```
@@ -127,7 +107,7 @@ where
127107
#[derive(Clone, Copy, Debug)]
128108
pub struct ZeroPadding;
129109

130-
impl RawPadding for ZeroPadding {
110+
impl Padding for ZeroPadding {
131111
const TYPE: PadType = PadType::Ambiguous;
132112

133113
#[inline]
@@ -189,7 +169,7 @@ impl Pkcs7 {
189169
}
190170
}
191171

192-
impl RawPadding for Pkcs7 {
172+
impl Padding for Pkcs7 {
193173
const TYPE: PadType = PadType::Reversible;
194174

195175
#[inline]
@@ -231,7 +211,7 @@ impl RawPadding for Pkcs7 {
231211
#[derive(Clone, Copy, Debug)]
232212
pub struct Iso10126;
233213

234-
impl RawPadding for Iso10126 {
214+
impl Padding for Iso10126 {
235215
const TYPE: PadType = PadType::Reversible;
236216

237217
#[inline]
@@ -266,7 +246,7 @@ impl RawPadding for Iso10126 {
266246
#[derive(Clone, Copy, Debug)]
267247
pub struct AnsiX923;
268248

269-
impl RawPadding for AnsiX923 {
249+
impl Padding for AnsiX923 {
270250
const TYPE: PadType = PadType::Reversible;
271251

272252
#[inline]
@@ -320,7 +300,7 @@ impl RawPadding for AnsiX923 {
320300
#[derive(Clone, Copy, Debug)]
321301
pub struct Iso7816;
322302

323-
impl RawPadding for Iso7816 {
303+
impl Padding for Iso7816 {
324304
const TYPE: PadType = PadType::Reversible;
325305

326306
#[inline]
@@ -369,7 +349,7 @@ impl RawPadding for Iso7816 {
369349
#[derive(Clone, Copy, Debug)]
370350
pub struct NoPadding;
371351

372-
impl RawPadding for NoPadding {
352+
impl Padding for NoPadding {
373353
const TYPE: PadType = PadType::NoPadding;
374354

375355
#[inline]

inout/src/reserved.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
155155
#[inline(always)]
156156
pub fn into_padded_blocks<P, BS>(self) -> Result<PaddedInOutBuf<'inp, 'out, BS>, PadError>
157157
where
158-
P: Padding<BS>,
158+
P: Padding,
159159
BS: ArraySize,
160160
{
161161
let bs = BS::USIZE;

0 commit comments

Comments
 (0)