Files
opaque-vx/src/slow_hash.rs
T

49 lines
1.4 KiB
Rust
Raw Normal View History

2020-06-08 21:02:01 -07:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//! Trait specifying a slow hashing function
2021-08-22 12:28:19 -07:00
use crate::{errors::InternalError, hash::Hash};
2021-08-12 06:25:07 +02:00
use alloc::vec::Vec;
2020-07-27 15:25:04 -07:00
use digest::Digest;
2021-06-21 11:44:04 +02:00
#[cfg(feature = "slow-hash")]
2020-12-04 21:32:23 -05:00
use generic_array::typenum::Unsigned;
2020-07-27 15:25:04 -07:00
use generic_array::GenericArray;
2020-06-08 21:02:01 -07:00
/// Used for the slow hashing function in OPAQUE
2021-09-02 11:28:21 +02:00
pub trait SlowHash<D: Hash>: Default {
/// Computes the slow hashing function
2021-09-02 11:28:21 +02:00
fn hash(
&self,
input: GenericArray<u8, <D as Digest>::OutputSize>,
) -> Result<Vec<u8>, InternalError>;
2020-06-08 21:02:01 -07:00
}
/// A no-op hash which simply returns its input
2021-09-02 11:28:21 +02:00
#[derive(Default)]
2020-06-08 21:02:01 -07:00
pub struct NoOpHash;
2020-07-27 15:25:04 -07:00
impl<D: Hash> SlowHash<D> for NoOpHash {
2021-09-02 11:28:21 +02:00
fn hash(
&self,
input: GenericArray<u8, <D as Digest>::OutputSize>,
) -> Result<Vec<u8>, InternalError> {
2020-06-08 21:02:01 -07:00
Ok(input.to_vec())
}
}
2021-06-21 11:44:04 +02:00
#[cfg(feature = "slow-hash")]
2021-06-21 10:15:40 +02:00
impl<D: Hash> SlowHash<D> for argon2::Argon2<'_> {
2021-09-02 11:28:21 +02:00
fn hash(
&self,
input: GenericArray<u8, <D as Digest>::OutputSize>,
) -> Result<Vec<u8>, InternalError> {
2021-08-17 05:11:53 +02:00
let mut output = alloc::vec![0u8; <D as Digest>::OutputSize::USIZE];
2021-09-02 11:28:21 +02:00
self.hash_password_into(&input, &[0; argon2::MIN_SALT_LEN], &mut output)
2021-08-22 12:28:19 -07:00
.map_err(|_| InternalError::SlowHashError)?;
2021-06-21 10:15:40 +02:00
Ok(output)
}
}