Skip to content

Conversation

@RealHinome
Copy link
Member

Follow specification.

@RealHinome RealHinome self-assigned this Dec 21, 2025
@RealHinome RealHinome added the enhancement New feature or request label Dec 21, 2025
@RealHinome
Copy link
Member Author

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements padding for encrypted messages to comply with a specification requirement. The changes introduce ISO/IEC 7816-4 padding with a minimum message size of 1KB and bucket-based sizing for messages between 1-8KB.

Key Changes:

  • Added new padding module with pad/unpad functions using ISO/IEC 7816-4 standard
  • Modified WebRTC send API to accept byte slices instead of strings
  • Integrated padding into message send/receive flow

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
libturms/src/padding.rs New padding module implementing bucket-based message padding with ISO/IEC 7816-4
libturms/src/channel.rs Integrated padding: unpads received messages and pads outgoing DHKey events
p2p/src/webrtc.rs Changed send method signature from String to generic byte slice parameter
p2p/src/lib.rs Moved lint attributes to proper location at file start
p2p/src/models.rs Added documentation comments for X3DH and URGENT flag
error/src/lib.rs Added RandOs error variant for OS random number generation errors
error/Cargo.toml Added rand and rand_chacha dependencies
Cargo.lock Updated with rand dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}
}

/// Fill an entry with bunch of paddings using ISO/IEC 7816-4.
Copy link

Copilot AI Dec 21, 2025

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.

Suggested change
/// Fill an entry with bunch of paddings using ISO/IEC 7816-4.
/// Add padding to an entry using ISO/IEC 7816-4.

Copilot uses AI. Check for mistakes.
out
}

/// Remove zero padding at the end of data using ISO/IEC 7816-4.
Copy link

Copilot AI Dec 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation says "Remove zero padding at the end of data" which should be "Remove padding from the end of data" since ISO/IEC 7816-4 padding includes both the 0x80 marker byte and zero bytes.

Suggested change
/// 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 uses AI. Check for mistakes.
Comment on lines +1 to +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.
pub fn unpad(mut data: Vec<u8>) -> Vec<u8> {
// Scan from the end.
while let Some(&last) = data.last() {
match last {
0x00 => {
data.pop();
},
0x80 => {
data.pop();
return data;
},
_ => break,
}
}

data
}
}
Copy link

Copilot AI Dec 21, 2025

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.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@RealHinome RealHinome marked this pull request as ready for review December 21, 2025 19:37
@RealHinome RealHinome merged commit 338b664 into master Dec 21, 2025
1 check passed
@RealHinome RealHinome deleted the padding branch December 21, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants