Skip to content

Commit

Permalink
More decoy documenation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Sep 25, 2024
1 parent c11b459 commit 8958145
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ impl PacketReader {
content_slice[0..3].copy_from_slice(&enc_content_len);
let content_len = u32::from_le_bytes(content_slice);

// Include 1-byte decoy and 16-byte tag.
content_len as usize + NUM_HEADER_BYTES + NUM_TAG_BYTES
}

Expand All @@ -258,7 +257,8 @@ impl PacketReader {
///
/// * `ciphertext` - The message from the peer excluding the first 3 length bytes. It should contain
/// the header, contents, and authentication tag.
/// * `contents` - Mutable buffer to write plaintext.
/// * `contents` - Mutable buffer to write plaintext. Note that the first byte is the header byte
/// containing protocol flags.
/// * `aad` - Optional associated authenticated data.
///
/// # Returns
Expand Down Expand Up @@ -327,7 +327,7 @@ impl PacketReader {
match self.decrypt_contents(ciphertext, &mut contents, aad)? {
PacketType::Decoy => Ok(None),
PacketType::Genuine => {
// Drop the decoy byte flag.
// Drop the header byte.
contents.remove(0);
Ok(Some(contents))
}
Expand Down Expand Up @@ -375,19 +375,19 @@ impl PacketWriter {
}

let plaintext_length = plaintext.len();
let decoy_index = NUM_LENGTH_BYTES + NUM_HEADER_BYTES - 1;
let plaintext_start_index = decoy_index + 1;
let header_index = NUM_LENGTH_BYTES + NUM_HEADER_BYTES - 1;
let plaintext_start_index = header_index + 1;
let plaintext_end_index = plaintext_start_index + plaintext_length;

// Set decoy byte.
packet[decoy_index] = packet_type.to_byte();
// Set header byte.
packet[header_index] = packet_type.to_byte();
packet[plaintext_start_index..plaintext_end_index].copy_from_slice(plaintext);

// Encrypt decoy byte and plaintext in place and produce tag.
// Encrypt header byte and plaintext in place and produce authentication tag.
let auth = aad.unwrap_or_default();
let tag = self
.packet_encoding_aead
.encrypt(auth, &mut packet[decoy_index..plaintext_end_index]);
.encrypt(auth, &mut packet[header_index..plaintext_end_index]);

// Encrypt plaintext length.
let mut content_len = [0u8; 3];
Expand Down

0 comments on commit 8958145

Please sign in to comment.