1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Common padding error type.
use core::fmt;
/// A failure while adding or removing block padding.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PaddingError {
/// The requested padding position lies past the end of the block.
PositionOutOfRange,
/// The block is longer than the scheme's length encoding can describe.
///
/// PKCS#7, ANSI X9.23, and ISO 10126-2 record the padding count in a single
/// byte and therefore cannot serve blocks of 256 bytes or more.
UnsupportedBlockSize,
/// The block is already full, leaving no room for padding.
///
/// Schemes that must write at least one byte, such as PKCS#7 and
/// ISO 7816-4, report this when the padding position equals the block
/// length. Schemes that can add nothing at all, such as zero-byte padding,
/// return a count of zero instead.
BlockFull,
/// The trailing bytes of the block are not a valid encoding for the scheme.
///
/// Self-describing schemes such as PKCS#7 report this; schemes that encode
/// no length, such as zero-byte padding, cannot detect corruption at all.
CorruptPadding,
}
impl fmt::Display for PaddingError {
/// Writes a fixed description of the error. Constant time: errors carry no
/// secret data.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PositionOutOfRange => {
f.write_str("padding position is past the end of the block")
}
Self::UnsupportedBlockSize => {
f.write_str("block is too long for a single-byte padding count")
}
Self::BlockFull => f.write_str("block leaves no room for padding"),
Self::CorruptPadding => f.write_str("pad block corrupted"),
}
}
}
impl core::error::Error for PaddingError {}
#[cfg(test)]
mod tests {
extern crate std;
use std::format;
use super::PaddingError;
#[test]
fn displays_each_variant() {
assert_eq!(
format!("{}", PaddingError::PositionOutOfRange),
"padding position is past the end of the block"
);
assert_eq!(
format!("{}", PaddingError::UnsupportedBlockSize),
"block is too long for a single-byte padding count"
);
assert_eq!(
format!("{}", PaddingError::BlockFull),
"block leaves no room for padding"
);
assert_eq!(
format!("{}", PaddingError::CorruptPadding),
"pad block corrupted"
);
}
}