Skip to content

Commit

Permalink
Add new docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Mar 22, 2024
1 parent 4127c33 commit 6bdf4e8
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,50 +167,42 @@ impl PacketHandler {
Ok(packet)
}

/// Decrypt the one or more messages from bytes received by a V2 peer.
/// Decode the length, in bytes, of the of the rest imbound message. Intended for use with `TcpStream` and `read_exact`.
/// Note that this does not decode to the length of contents described in BIP324, and is meant to represent the entire imbound message.
///
/// # Arguments
///
/// `ciphertext` - The entire message received from the peer.
///
/// `aad` - Optional authentication from the peer, currently only used for the first round of messages.
/// `len_slice` - The first three bytes of the message.
///
/// # Returns
///
/// A vector of messages from the peer.
///
/// # Errors
///
/// Fails if the packet was not decrypted or authenticated properly.
pub fn receive_v2_packets(
&mut self,
ciphertext: Vec<u8>,
aad: Option<Vec<u8>>,
) -> Result<Vec<ReceivedMessage>, FSChaChaError> {
let auth = aad.unwrap_or_default();
let mut messages: Vec<ReceivedMessage> = Vec::new();
let mut start_index: Option<usize> = Some(0);
while start_index.is_some() {
let (message, index) =
self.decode_packet_from_len(&ciphertext, &auth, start_index.unwrap())?;
start_index = index;
messages.push(ReceivedMessage { message })
}
Ok(messages)
}

///
pub fn decypt_len(&mut self, len_slice: [u8; 3]) -> Result<usize, FSChaChaError> {
/// The length to be read into the buffer next to receive the full message from the peer.
pub fn decypt_len(&mut self, len_slice: [u8; 3]) -> usize {
let mut enc_content_len = self.length_decoding_cipher.crypt(len_slice.to_vec());
enc_content_len.push(0u8);
let content_slice: [u8; 4] = enc_content_len
.try_into()
.expect("Length of slice should be 4.");
let content_len = u32::from_le_bytes(content_slice);
Ok(content_len as usize + 17)
content_len as usize + 17
}

/// Decrypt the rest of the message from the peer, excluding the 3 length bytes. This method should only be called after
/// calling `decrypt_len` on the first three bytes of the buffer.
///
/// # Arguments
///
/// `contents` - The message from the peer.
///
/// `aad` - Optional authentication for the peer, currently only used for the first round of messages.
///
/// # Returns
///
/// The message from the peer.
///
/// # Errors
///
/// Fails if the packet was not decrypted or authenticated properly.
pub fn decrypt_contents(
&mut self,
contents: Vec<u8>,
Expand All @@ -230,6 +222,38 @@ impl PacketHandler {
})
}

/// Decrypt the one or more messages from bytes received by a V2 peer.
///
/// # Arguments
///
/// `ciphertext` - The entire message received from the peer.
///
/// `aad` - Optional authentication from the peer, currently only used for the first round of messages.
///
/// # Returns
///
/// A vector of messages from the peer.
///
/// # Errors
///
/// Fails if the packet was not decrypted or authenticated properly.
pub fn receive_v2_packets(
&mut self,
ciphertext: Vec<u8>,
aad: Option<Vec<u8>>,
) -> Result<Vec<ReceivedMessage>, FSChaChaError> {
let auth = aad.unwrap_or_default();
let mut messages: Vec<ReceivedMessage> = Vec::new();
let mut start_index: Option<usize> = Some(0);
while start_index.is_some() {
let (message, index) =
self.decode_packet_from_len(&ciphertext, &auth, start_index.unwrap())?;
start_index = index;
messages.push(ReceivedMessage { message })
}
Ok(messages)
}

fn decode_packet_from_len(
&mut self,
ciphertext: &[u8],
Expand Down Expand Up @@ -1020,9 +1044,7 @@ mod tests {
.prepare_v2_packet(message.clone(), None, false)
.unwrap();
message_to_bob.extend(enc_packet);
let alice_message_len = bob
.decypt_len(message_to_bob[..3].try_into().unwrap())
.unwrap();
let alice_message_len = bob.decypt_len(message_to_bob[..3].try_into().unwrap());
let contents = bob
.decrypt_contents(message_to_bob[3..3 + alice_message_len].to_vec(), None)
.unwrap();
Expand Down

0 comments on commit 6bdf4e8

Please sign in to comment.