use aes::{
cipher::{generic_array::GenericArray, KeyIvInit, StreamCipher},
Aes128,
};
use std::fmt;
#[derive(Debug)]
pub enum SymmError {
InvalidKey,
InvalidNonce,
SourceDestinationMismatch,
}
impl fmt::Display for SymmError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SymmError::InvalidKey => f.write_str("Key must be 16 bytes long"),
SymmError::InvalidNonce => f.write_str("Nonce must be 16 bytes long"),
SymmError::SourceDestinationMismatch => f.write_str("Source and destination must have equal length"),
}
}
}
pub fn encrypt_128_ctr(k: &[u8], iv: &[u8], plain: &[u8], dest: &mut [u8]) -> Result<(), SymmError> {
if k.len() != 16 {
return Err(SymmError::InvalidKey);
}
if iv.len() != 16 {
return Err(SymmError::InvalidNonce);
}
if plain.len() != dest.len() {
return Err(SymmError::SourceDestinationMismatch);
}
let key = GenericArray::from_slice(k);
let nonce = GenericArray::from_slice(iv);
dest.copy_from_slice(plain);
let mut cipher_ctr = ctr::Ctr128BE::<Aes128>::new(&key, &nonce);
cipher_ctr.apply_keystream(dest);
Ok(())
}
pub fn decrypt_128_ctr(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) -> Result<(), SymmError> {
encrypt_128_ctr(k, iv, encrypted, dest)
}