-
Notifications
You must be signed in to change notification settings - Fork 1
Add padding on encrypted messages #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||
| //! Deterministic ISO/IEC 7816-4 padding. | ||||||
|
|
||||||
| // Minimum required by specification. | ||||||
| const MIN_LENGTH: usize = 1000; // ~1kB. | ||||||
|
|
||||||
| /// Padding structure. | ||||||
| #[derive(Debug, Clone)] | ||||||
| pub(crate) struct Padding; | ||||||
|
|
||||||
| impl Padding { | ||||||
| fn bucket_size(len: usize) -> usize { | ||||||
| match len { | ||||||
| 0..=MIN_LENGTH => MIN_LENGTH, | ||||||
| 1001..=8192 => len.div_ceil(1024) * 1024, | ||||||
| _ => len, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Fill an entry with bunch of paddings using ISO/IEC 7816-4. | ||||||
| pub fn pad(data: impl AsRef<[u8]>) -> Vec<u8> { | ||||||
| let data = data.as_ref(); | ||||||
|
|
||||||
| let target_len = Self::bucket_size(data.len()); | ||||||
| let mut out = Vec::with_capacity(target_len); | ||||||
|
|
||||||
| out.extend_from_slice(data); | ||||||
| out.push(0x80); | ||||||
|
|
||||||
| if out.len() < target_len { | ||||||
| out.resize(MIN_LENGTH, 0x00); | ||||||
| } | ||||||
|
|
||||||
| out | ||||||
| } | ||||||
|
|
||||||
| /// Remove zero padding at the end of data using ISO/IEC 7816-4. | ||||||
|
||||||
| /// Remove zero padding at the end of data using ISO/IEC 7816-4. | |
| /// Remove padding from the end of data using ISO/IEC 7816-4. |
Copilot
AI
Dec 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The padding functionality is critical for message privacy but lacks test coverage. Consider adding tests to verify: 1) correct padding for messages under 1000 bytes, 2) correct padding for messages between 1001-8192 bytes (bucket rounding to 1024), 3) correct padding for messages over 8192 bytes, 4) round-trip testing (pad then unpad returns original data), and 5) edge cases like empty messages or messages exactly at bucket boundaries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation says "Fill an entry with bunch of paddings" which should be "Fill an entry with padding" or "Add padding to an entry" - the phrase "bunch of paddings" is grammatically incorrect.