Add SlowHash configuration (#234)
* Add `SlowHash` configuration * Use a reference * Changing generic argument to be CipherSuite instead of SlowHash Co-authored-by: Kevin Lewi <[email protected]>
This commit is contained in:
+15
-13
@@ -13,33 +13,35 @@ use generic_array::typenum::Unsigned;
|
||||
use generic_array::GenericArray;
|
||||
|
||||
/// Used for the slow hashing function in OPAQUE
|
||||
pub trait SlowHash<D: Hash> {
|
||||
pub trait SlowHash<D: Hash>: Default {
|
||||
/// Computes the slow hashing function
|
||||
fn hash(input: GenericArray<u8, <D as Digest>::OutputSize>) -> Result<Vec<u8>, InternalError>;
|
||||
fn hash(
|
||||
&self,
|
||||
input: GenericArray<u8, <D as Digest>::OutputSize>,
|
||||
) -> Result<Vec<u8>, InternalError>;
|
||||
}
|
||||
|
||||
/// A no-op hash which simply returns its input
|
||||
#[derive(Default)]
|
||||
pub struct NoOpHash;
|
||||
|
||||
impl<D: Hash> SlowHash<D> for NoOpHash {
|
||||
fn hash(input: GenericArray<u8, <D as Digest>::OutputSize>) -> Result<Vec<u8>, InternalError> {
|
||||
fn hash(
|
||||
&self,
|
||||
input: GenericArray<u8, <D as Digest>::OutputSize>,
|
||||
) -> Result<Vec<u8>, InternalError> {
|
||||
Ok(input.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "slow-hash")]
|
||||
impl<D: Hash> SlowHash<D> for argon2::Argon2<'_> {
|
||||
fn hash(input: GenericArray<u8, <D as Digest>::OutputSize>) -> Result<Vec<u8>, InternalError> {
|
||||
let params = argon2::Argon2::default();
|
||||
fn hash(
|
||||
&self,
|
||||
input: GenericArray<u8, <D as Digest>::OutputSize>,
|
||||
) -> Result<Vec<u8>, InternalError> {
|
||||
let mut output = alloc::vec![0u8; <D as Digest>::OutputSize::USIZE];
|
||||
params
|
||||
.hash_password_into(
|
||||
argon2::Algorithm::Argon2id,
|
||||
&input,
|
||||
&[0; argon2::MIN_SALT_LENGTH],
|
||||
&[],
|
||||
&mut output,
|
||||
)
|
||||
self.hash_password_into(&input, &[0; argon2::MIN_SALT_LEN], &mut output)
|
||||
.map_err(|_| InternalError::SlowHashError)?;
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user