diff --git a/src/crypto/util.rs b/src/crypto/util.rs index cff4a8e..87c2ba6 100644 --- a/src/crypto/util.rs +++ b/src/crypto/util.rs @@ -1,7 +1,7 @@ use crate::error::{Result, Error}; use simple_error::simple_error; -use block_modes::block_padding::{ZeroPadding, Padding}; +/// Extracts the the last `n` bytes of a slice. n being the blocksize. pub fn extract_last_block(data: &[u8], blocksize: usize) -> Result<&[u8]> { if data.len() % blocksize != 0 { return Err(simple_error!("Data is not compatible with blocksize: data(length): {}, blocksize: {}.", data.len(), blocksize).into()) @@ -10,11 +10,15 @@ pub fn extract_last_block(data: &[u8], blocksize: usize) -> Result<&[u8]> { Ok(&data[(data.len() - blocksize)..]) } -pub fn expand_to_blocksize(data: &mut [u8], blocksize: usize) -> Result<&mut [u8]> { - // let diff = data.len() % blocksize; +/// Takes a given input and zero pads it to a multiple of blocksize +pub fn expand_to_blocksize(data: &mut [u8], blocksize: usize) -> Result> { + let diff = data.len() % blocksize; - match ZeroPadding::pad(data, data.len(), blocksize) { - Ok(x) => {Ok(x)} - Err(e) => {Err(Error::PadError(e))} + if diff == 0 { + return Ok(data.to_vec()); + } else { + let mut buf = vec![0 as u8; data.len() + blocksize - diff]; + buf[..data.len()].copy_from_slice(data); + Ok(buf) } } \ No newline at end of file