Skip to content

Commit

Permalink
Get rid of message in garbage split
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Sep 25, 2024
1 parent 8c9832f commit d4e4fd5
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ impl<'a> Handshake<'a> {
packet_buffer: &mut [u8],
) -> Result<(), Error> {
// Find the end of the garbage.
let (garbage, message) = split_garbage_and_message(
let (garbage, message) = split_garbage(
buffer,
self.remote_garbage_terminator
.ok_or(Error::HandshakeOutOfOrder)?,
Expand Down Expand Up @@ -871,24 +871,23 @@ impl<'a> Handshake<'a> {
}
}

/// Split a message on the garbage terminator returning the garbage itself
/// and the remaing message. The message is expected to be the version packet,
/// but could be decoy packets.
/// Split off garbage in the buffer on the garbage terminator.
///
/// # Returns
///
/// A `Result` containing the garbage and the remaining ciphertext not including the terminator.
///
/// # Error
///
/// * `CiphertextTooSmall` - Buffer did not contain a garbage terminator.
/// * `MaxGarbageLength` - Buffer did not contain the garbage terminator and contains too much garbage, should not be retried.
fn split_garbage_and_message(
message: &[u8],
garbage_term: [u8; 16],
) -> Result<(&[u8], &[u8]), Error> {
if let Some(index) = message
fn split_garbage(buffer: &[u8], garbage_term: [u8; 16]) -> Result<(&[u8], &[u8]), Error> {
if let Some(index) = buffer
.windows(garbage_term.len())
.position(|window| window == garbage_term)
{
Ok((&message[..index], &message[(index + garbage_term.len())..]))
} else if message.len() >= (MAX_NUM_GARBAGE_BYTES + NUM_GARBAGE_TERMINTOR_BYTES) {
Ok((&buffer[..index], &buffer[(index + garbage_term.len())..]))
} else if buffer.len() >= (MAX_NUM_GARBAGE_BYTES + NUM_GARBAGE_TERMINTOR_BYTES) {
Err(Error::MaxGarbageLength)
} else {
// Terminator not found, the message needs more information.
Expand Down Expand Up @@ -1013,7 +1012,7 @@ mod tests {
fn test_max_garbage() {
let too_much_garbage = vec![0; 4111];
let garbage_terminator = [1; 16];
let result = split_garbage_and_message(&too_much_garbage, garbage_terminator);
let result = split_garbage(&too_much_garbage, garbage_terminator);
assert!(matches!(result, Err(Error::MaxGarbageLength)));
}

Expand Down

0 comments on commit d4e4fd5

Please sign in to comment.