use crate::fapi_sys::{TPM2_ALG_ID, constants};
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum HashAlgorithm {
Sha1,
Sha2_256,
Sha2_384,
Sha2_512,
Sha3_256,
Sha3_384,
Sha3_512,
SM3_256,
UnknownAlgorithm,
}
impl HashAlgorithm {
pub(crate) fn from_id(algo_id: TPM2_ALG_ID) -> Self {
match algo_id {
constants::TPM2_ALG_SHA1 => Self::Sha1,
constants::TPM2_ALG_SHA256 => Self::Sha2_256,
constants::TPM2_ALG_SHA384 => Self::Sha2_384,
constants::TPM2_ALG_SHA512 => Self::Sha2_512,
constants::TPM2_ALG_SHA3_256 => Self::Sha3_256,
constants::TPM2_ALG_SHA3_384 => Self::Sha3_384,
constants::TPM2_ALG_SHA3_512 => Self::Sha3_512,
constants::TPM2_ALG_SM3_256 => Self::SM3_256,
_ => Self::UnknownAlgorithm,
}
}
}
#[cfg(test)]
mod tests {
use super::HashAlgorithm;
use std::collections::HashSet;
#[test]
fn test_hash_ids() {
let mut id_set: HashSet<HashAlgorithm> = HashSet::new();
for algorithm_id in 0u16..=0xFFFF_u16 {
match HashAlgorithm::from_id(algorithm_id) {
HashAlgorithm::UnknownAlgorithm => (),
identifier => assert!(id_set.insert(identifier)),
}
}
assert_eq!(id_set.len(), 8usize);
}
}