Skip to content

Commit

Permalink
ecies: use align_num value (#12139)
Browse files Browse the repository at this point in the history
Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
nkysg and DaniPopes authored Nov 9, 2024
1 parent 7a65cce commit b5fce61
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/net/ecies/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl ECIES {

pub fn body_len(&self) -> usize {
let len = self.body_size.unwrap();
(if len % 16 == 0 { len } else { (len / 16 + 1) * 16 }) + 16
Self::align_16(len) + 16
}

#[cfg(test)]
Expand All @@ -699,7 +699,7 @@ impl ECIES {
}

pub fn write_body(&mut self, out: &mut BytesMut, data: &[u8]) {
let len = if data.len() % 16 == 0 { data.len() } else { (data.len() / 16 + 1) * 16 };
let len = Self::align_16(data.len());
let old_len = out.len();
out.resize(old_len + len, 0);

Expand Down Expand Up @@ -732,6 +732,14 @@ impl ECIES {
self.ingress_aes.as_mut().unwrap().apply_keystream(ret);
Ok(split_at_mut(ret, size)?.0)
}

/// Returns `num` aligned to 16.
///
/// `<https://stackoverflow.com/questions/14561402/how-is-this-size-alignment-working>`
#[inline]
const fn align_16(num: usize) -> usize {
(num + (16 - 1)) & !(16 - 1)
}
}

#[cfg(test)]
Expand Down

0 comments on commit b5fce61

Please sign in to comment.