#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvalidLength;
macro_rules! kalyna_ecb_variant {
($name:ident, $expanded:ident, $key_bytes:literal, $block_bytes:literal) => {
#[doc = concat!(
"ECB mode over [`super::kalyna::", stringify!($expanded), "`] - see the module doc ",
"comment for the citation and the misuse warning."
)]
pub struct $name {
key: super::kalyna::$expanded,
}
impl $name {
#[must_use]
pub fn new(key: &[u8; $key_bytes]) -> Self {
Self {
key: super::kalyna::$expanded::new(key),
}
}
pub fn encrypt_in_place(&self, buf: &mut [u8]) -> Result<(), InvalidLength> {
if !buf.len().is_multiple_of($block_bytes) {
return Err(InvalidLength);
}
for block in buf.chunks_exact_mut($block_bytes) {
let mut input = [0u8; $block_bytes];
input.copy_from_slice(block);
block.copy_from_slice(&self.key.encrypt_block(&input));
}
Ok(())
}
pub fn decrypt_in_place(&self, buf: &mut [u8]) -> Result<(), InvalidLength> {
if !buf.len().is_multiple_of($block_bytes) {
return Err(InvalidLength);
}
for block in buf.chunks_exact_mut($block_bytes) {
let mut input = [0u8; $block_bytes];
input.copy_from_slice(block);
block.copy_from_slice(&self.key.decrypt_block(&input));
}
Ok(())
}
}
};
}
kalyna_ecb_variant!(Kalyna128_128Ecb, Kalyna128_128ExpandedKey, 16, 16);
kalyna_ecb_variant!(Kalyna128_256Ecb, Kalyna128_256ExpandedKey, 32, 16);
kalyna_ecb_variant!(Kalyna256_256Ecb, Kalyna256_256ExpandedKey, 32, 32);
kalyna_ecb_variant!(Kalyna256_512Ecb, Kalyna256_512ExpandedKey, 64, 32);
kalyna_ecb_variant!(Kalyna512_512Ecb, Kalyna512_512ExpandedKey, 64, 64);