Skip to content

Commit

Permalink
NoiseEncoder w/o binary_sv2 doc cmts + decoder consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
rrybarczyk committed Sep 10, 2024
1 parent 08a98bf commit 1956a6d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions protocols/v2/codec-sv2/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}
}

/// Processes frames during the handshake phase.
/// Processes and decodes frames during the handshake phase of the Noise protocol.
///
/// Used while the codec is in the handshake phase of the Noise protocol. It processes and
/// returns a handshake frame that has been received and encapsulates it in an `Frame`,
/// indicating the frame has been processed and is ready to be handled by the codec.
/// Used while the codec is in the handshake phase of the Noise protocol. It decodes a received
/// handshake frame from the `noise_buffer`, converting it into a `HandShakeFrame`, and then
/// encapsulates it a `Frame`, marking it as ready for further processing by the codec.
fn while_handshaking(&mut self) -> Frame<T, B::Slice> {
let src = self.noise_buffer.get_data_owned().as_mut().to_vec();

Expand All @@ -236,7 +236,7 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
self.noise_buffer.get_writable(self.missing_noise_b)
}

/// Checks if the buffers are droppable.
/// Determines whether the decoder's internal buffers can be safely dropped.
pub fn droppable(&self) -> bool {
self.noise_buffer.is_droppable() && self.sv2_buffer.is_droppable()
}
Expand Down
13 changes: 13 additions & 0 deletions protocols/v2/codec-sv2/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ type Item<T> = Frame<T, Slice>;

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + GetSize> NoiseEncoder<T> {
/// Encodes a Noise-encrypted frame.
///
/// Encodes the given `item` into a Sv2 frame and then encrypts the frame using the Noise
/// protocol. The `state` of the Noise codec determines whether the encoder is in the handshake
/// or transport phase. On success, an encrypted frame as a `Slice` is returned.
#[inline]
pub fn encode(&mut self, item: Item<T>, state: &mut State) -> Result<Slice> {
match state {
Expand Down Expand Up @@ -114,6 +119,12 @@ impl<T: Serialize + GetSize> NoiseEncoder<T> {
Ok(self.noise_buffer.get_data_owned())
}

/// Processes and encodes Sv2 frames during the handshake phase of the Noise protocol.
///
/// Used when the codec is in the handshake phase. It encodes the provided `item` into a
/// handshake frame and stores the payload in the `noise_buffer`. This is necessary to
/// establish the initial secure communication channel before transitioning to the transport
/// phase of the Noise protocol.
#[inline(never)]
fn while_handshaking(&mut self, item: Item<T>) -> Result<()> {
// ENCODE THE SV2 FRAME
Expand All @@ -129,13 +140,15 @@ impl<T: Serialize + GetSize> NoiseEncoder<T> {
Ok(())
}

/// Determines whether the encoder's internal buffers can be safely dropped.
pub fn droppable(&self) -> bool {
self.noise_buffer.is_droppable() && self.sv2_buffer.is_droppable()
}
}

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + binary_sv2::GetSize> NoiseEncoder<T> {
/// Creates a new `NoiseEncoder` with default buffer sizes.
pub fn new() -> Self {
#[cfg(not(feature = "with_buffer_pool"))]
let size = 512;
Expand Down

0 comments on commit 1956a6d

Please sign in to comment.