use crate::hkdf;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Algorithm {
Sha1,
Sha256,
Sha384,
Sha512,
}
impl From<hkdf::Algorithm> for Algorithm {
fn from(value: hkdf::Algorithm) -> Self {
match value {
hkdf::Algorithm::Sha256 => Self::Sha256,
hkdf::Algorithm::Sha384 => Self::Sha384,
hkdf::Algorithm::Sha512 => Self::Sha512,
}
}
}
pub const fn tag_size(alg: Algorithm) -> usize {
match alg {
Algorithm::Sha1 => 20,
Algorithm::Sha256 => 32,
Algorithm::Sha384 => 48,
Algorithm::Sha512 => 64,
}
}
pub fn hmac(alg: Algorithm, key: &[u8], data: &[u8], tag_length: Option<usize>) -> Vec<u8> {
let native_tag_length = tag_size(alg);
let tag_length = match tag_length {
Some(v) => v,
None => native_tag_length,
};
let mut dst: Vec<_> = match alg {
Algorithm::Sha1 => crate::hacl::hmac::sha1(key, data).into(),
Algorithm::Sha256 => crate::hacl::hmac::sha2_256(key, data).into(),
Algorithm::Sha384 => crate::hacl::hmac::sha2_384(key, data).into(),
Algorithm::Sha512 => crate::hacl::hmac::sha2_512(key, data).into(),
};
dst.truncate(tag_length);
dst
}