use tink_core::{utils::wrap_err, Prf, TinkError};
const MIN_CMAC_KEY_SIZE_IN_BYTES: usize = 16;
const RECOMMENDED_CMAC_KEY_SIZE_IN_BYTES: usize = 32;
const MIN_TAG_LENGTH_IN_BYTES: usize = 10;
const MAX_TAG_LENGTH_IN_BYTES: usize = 16;
#[derive(Clone)]
pub struct AesCmac {
prf: tink_prf::subtle::AesCmacPrf,
tag_size: usize,
}
impl AesCmac {
pub fn new(key: &[u8], tag_size: usize) -> Result<AesCmac, TinkError> {
if key.len() < MIN_CMAC_KEY_SIZE_IN_BYTES {
return Err("AesCmac: Only 256 bit keys are allowed".into());
}
if tag_size < MIN_TAG_LENGTH_IN_BYTES {
return Err(format!(
"AesCmac: tag length {tag_size} is shorter than minimum tag length {MIN_TAG_LENGTH_IN_BYTES}",
)
.into());
}
if tag_size > MAX_TAG_LENGTH_IN_BYTES {
return Err(format!(
"AesCmac: tag length {tag_size} is longer than maximum tag length {MAX_TAG_LENGTH_IN_BYTES}",
)
.into());
}
let prf = tink_prf::subtle::AesCmacPrf::new(key)
.map_err(|e| wrap_err("AesCmac: could not create AES-CMAC prf", e))?;
Ok(AesCmac { prf, tag_size })
}
}
impl tink_core::Mac for AesCmac {
fn compute_mac(&self, data: &[u8]) -> Result<Vec<u8>, TinkError> {
self.prf.compute_prf(data, self.tag_size)
}
}
pub fn validate_cmac_params(key_size: usize, tag_size: usize) -> Result<(), TinkError> {
if key_size != RECOMMENDED_CMAC_KEY_SIZE_IN_BYTES {
return Err(format!(
"Only {RECOMMENDED_CMAC_KEY_SIZE_IN_BYTES} sized keys are allowed with Tink's AES-CMAC",
)
.into());
}
if tag_size < MIN_TAG_LENGTH_IN_BYTES {
return Err("Tag size too short".into());
}
if tag_size > MAX_TAG_LENGTH_IN_BYTES {
return Err("Tag size too long".into());
}
Ok(())
}