mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
ecies: use align_num value (#12139)
Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
This commit is contained in:
@ -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)]
|
||||
@ -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);
|
||||
|
||||
@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user