From cdb0ef77bb7c21788ac81aa167933e6feccd7fa5 Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Tue, 26 Dec 2023 16:52:10 +0100 Subject: [PATCH] Fix packetbuffer access into tag space PacketBuffer buffer and buffer_mut methods incorrectly calculated the offset of the end of the useable buffer, resulting in the returned slice pointing into the space reserved for the AES_GCM_TAG. Signed-off-by: Lee Smet --- src/crypto.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 2b6c404..57286a1 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -242,13 +242,13 @@ impl PacketBuffer { /// Get a reference to the entire useable inner buffer. pub fn buffer(&self) -> &[u8] { - let buf_end = self.buf.len() - AES_NONCE_SIZE - DATA_HEADER_SIZE; + let buf_end = self.buf.len() - AES_NONCE_SIZE - AES_TAG_SIZE; &self.buf[DATA_HEADER_SIZE..buf_end] } /// Get a mutable reference to the entire useable internal buffer. pub fn buffer_mut(&mut self) -> &mut [u8] { - let buf_end = self.buf.len() - AES_NONCE_SIZE - DATA_HEADER_SIZE; + let buf_end = self.buf.len() - AES_NONCE_SIZE - AES_TAG_SIZE; &mut self.buf[DATA_HEADER_SIZE..buf_end] } @@ -451,4 +451,14 @@ mod tests { assert_eq!(pb.buf[..DATA_HEADER_SIZE], [1, 2, 3, 4]); } + + #[test] + /// Verify [`PacketBuffer::buffer`] and [`PacketBuffer::buffer_mut`] actually have the + /// appropriate size. + fn buffer_mapping() { + let mut pb = PacketBuffer::new(); + + assert_eq!(pb.buffer().len(), super::PACKET_SIZE); + assert_eq!(pb.buffer_mut().len(), super::PACKET_SIZE); + } }