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 <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2023-12-26 16:52:10 +01:00
parent 305ab2beea
commit cdb0ef77bb
+12 -2
View File
@@ -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);
}
}