diff --git a/ciphers/src/subtle/either_hash.rs b/ciphers/src/subtle/either_hash.rs new file mode 100644 index 0000000..6539d53 --- /dev/null +++ b/ciphers/src/subtle/either_hash.rs @@ -0,0 +1,41 @@ +use rosenpass_cipher_traits::{KeyedHash, KeyedHashInstance}; +use anyhow::Result; +use crate::subtle::hash_functions::keyed_shake256::SHAKE256Core; +use crate::subtle::incorrect_hmac_blake2b::Blake2bCore; + +#[derive(Debug, Eq, PartialEq)] +pub enum EitherHash, + R: KeyedHash> +{ + Left(L), + Right(R), +} + +impl KeyedHashInstance for EitherHash +where + L: KeyedHash, + R: KeyedHash, +{ + type KeyType = [u8; KEY_LEN]; + type OutputType = [u8; HASH_LEN]; + type Error = Error; + + fn keyed_hash(&self, key: &[u8; KEY_LEN], data: &[u8], out: &mut [u8; HASH_LEN]) -> Result<(), Self::Error> { + match self { + Self::Left(_) => L::keyed_hash(key, data, out), + Self::Right(_) => R::keyed_hash(key, data, out), + } + } +} + +pub type EitherShakeOrBlake = EitherHash<32, 32, anyhow::Error, SHAKE256Core<32, 32>, Blake2bCore>; + +impl Clone for EitherShakeOrBlake { + fn clone(&self) -> Self { + match self { + Self::Left(l) => Self::Left(l.clone()), + Self::Right(r) => Self::Right(r.clone()), + } + } +} diff --git a/ciphers/src/subtle/mod.rs b/ciphers/src/subtle/mod.rs index e5f6947..64c3158 100644 --- a/ciphers/src/subtle/mod.rs +++ b/ciphers/src/subtle/mod.rs @@ -10,5 +10,6 @@ pub mod chacha20poly1305_ietf; pub mod chacha20poly1305_ietf_libcrux; pub mod xchacha20poly1305_ietf; mod hash_functions; +pub mod hash_choice; pub use hash_functions::{blake2b, incorrect_hmac_blake2b, keyed_shake256}; \ No newline at end of file