Skip to content

Commit

Permalink
Fix AeadNull
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert committed Jan 30, 2025
1 parent 2745a34 commit 006eb03
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
57 changes: 41 additions & 16 deletions neqo-crypto/src/aead_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl AeadNull {
) -> Res<&'a [u8]> {
let l = input.len();
output[..l].copy_from_slice(input);
output[l..l + 16].copy_from_slice(AEAD_NULL_TAG);
Ok(&output[..l + 16])
output[l..l + self.expansion()].copy_from_slice(AEAD_NULL_TAG);
Ok(&output[..l + self.expansion()])
}

#[allow(clippy::missing_errors_doc)]
Expand All @@ -52,37 +52,62 @@ impl AeadNull {
_count: u64,
_aad: Range<usize>,
input: Range<usize>,
_data: &mut [u8],
data: &mut [u8],
) -> Res<usize> {
Ok(input.len() + 16)
data[input.end..input.end + self.expansion()].copy_from_slice(AEAD_NULL_TAG);
Ok(input.len() + self.expansion())
}

#[allow(clippy::missing_errors_doc)]
pub fn decrypt<'a>(
&self,
_count: u64,
_aad: &[u8],
input: &[u8],
output: &'a mut [u8],
) -> Res<&'a [u8]> {
if input.len() < AEAD_NULL_TAG.len() {
fn decrypt_check(&self, _count: u64, _aad: &[u8], input: &[u8]) -> Res<usize> {
if input.len() < self.expansion() {
return Err(Error::from(SEC_ERROR_BAD_DATA));
}

let len_encrypted = input.len() - AEAD_NULL_TAG.len();
let len_encrypted = input.len() - self.expansion();
// Check that:
// 1) expansion is all zeros and
// 2) if the encrypted data is also supplied that at least some values are no zero
// (otherwise padding will be interpreted as a valid packet)
if &input[len_encrypted..] == AEAD_NULL_TAG
&& (len_encrypted == 0 || input[..len_encrypted].iter().any(|x| *x != 0x0))
{
output[..len_encrypted].copy_from_slice(&input[..len_encrypted]);
Ok(&output[..len_encrypted])
Ok(len_encrypted)
} else {
Err(Error::from(SEC_ERROR_BAD_DATA))
}
}

#[allow(clippy::missing_errors_doc)]
pub fn decrypt<'a>(
&self,
count: u64,
aad: &[u8],
input: &[u8],
output: &'a mut [u8],
) -> Res<&'a [u8]> {
self.decrypt_check(count, aad, input).map(|len| {
output[..len].copy_from_slice(&input[..len]);
&output[..len]
})
}

#[allow(clippy::missing_errors_doc)]
pub fn decrypt_in_place<'a>(
&self,
count: u64,
aad: Range<usize>,
input: Range<usize>,
data: &'a mut [u8],
) -> Res<&'a mut [u8]> {
let aad = data
.get(aad)
.ok_or_else(|| Error::from(SEC_ERROR_BAD_DATA))?;
let inp = data
.get(input.clone())
.ok_or_else(|| Error::from(SEC_ERROR_BAD_DATA))?;
self.decrypt_check(count, aad, inp)
.map(move |len| &mut data[input.start..input.start + len])
}
}

impl fmt::Debug for AeadNull {
Expand Down
1 change: 1 addition & 0 deletions neqo-transport/src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ impl<'a> PublicPacket<'a> {
self.data.len()
}

#[must_use]
pub fn data(&self) -> &[u8] {
self.data
}
Expand Down

0 comments on commit 006eb03

Please sign in to comment.