mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-27 22:13:12 -08:00
dev(ciphers): add EitherHash enum and thus the functionality for choosing a hash function at runtime
This commit is contained in:
41
ciphers/src/subtle/either_hash.rs
Normal file
41
ciphers/src/subtle/either_hash.rs
Normal file
@@ -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<const KEY_LEN: usize, const HASH_LEN: usize, Error,
|
||||||
|
L: KeyedHash<KEY_LEN, HASH_LEN, Error = Error>,
|
||||||
|
R: KeyedHash<KEY_LEN, HASH_LEN, Error = Error>>
|
||||||
|
{
|
||||||
|
Left(L),
|
||||||
|
Right(R),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const KEY_LEN: usize, const HASH_LEN: usize, Error, L, R> KeyedHashInstance<KEY_LEN, HASH_LEN> for EitherHash<KEY_LEN, HASH_LEN, Error, L, R>
|
||||||
|
where
|
||||||
|
L: KeyedHash<KEY_LEN, HASH_LEN, Error = Error>,
|
||||||
|
R: KeyedHash<KEY_LEN, HASH_LEN, Error = Error>,
|
||||||
|
{
|
||||||
|
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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,5 +10,6 @@ pub mod chacha20poly1305_ietf;
|
|||||||
pub mod chacha20poly1305_ietf_libcrux;
|
pub mod chacha20poly1305_ietf_libcrux;
|
||||||
pub mod xchacha20poly1305_ietf;
|
pub mod xchacha20poly1305_ietf;
|
||||||
mod hash_functions;
|
mod hash_functions;
|
||||||
|
pub mod hash_choice;
|
||||||
|
|
||||||
pub use hash_functions::{blake2b, incorrect_hmac_blake2b, keyed_shake256};
|
pub use hash_functions::{blake2b, incorrect_hmac_blake2b, keyed_shake256};
|
||||||
Reference in New Issue
Block a user