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.
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
//! Trait specifying a slow hashing function
|
|
|
|
|
|
2020-06-08 21:02:01 -07:00
|
|
|
use crate::errors::InternalPakeError;
|
|
|
|
|
|
2020-07-03 18:28:33 -04:00
|
|
|
use generic_array::{typenum::U32, GenericArray};
|
2020-06-08 21:02:01 -07:00
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
/// Used for the slow hashing function in OPAQUE
|
2020-06-08 21:02:01 -07:00
|
|
|
pub trait SlowHash {
|
2020-06-14 23:25:31 -07:00
|
|
|
/// Computes the slow hashing function
|
2020-07-03 18:28:33 -04:00
|
|
|
fn hash(input: GenericArray<u8, U32>) -> Result<Vec<u8>, InternalPakeError>;
|
2020-06-08 21:02:01 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
/// A no-op hash which simply returns its input
|
2020-06-08 21:02:01 -07:00
|
|
|
pub struct NoOpHash;
|
|
|
|
|
|
|
|
|
|
impl SlowHash for NoOpHash {
|
2020-07-03 18:28:33 -04:00
|
|
|
fn hash(input: GenericArray<u8, U32>) -> Result<Vec<u8>, InternalPakeError> {
|
2020-06-08 21:02:01 -07:00
|
|
|
Ok(input.to_vec())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 14:56:34 -07:00
|
|
|
#[cfg(feature = "slow-hash")]
|
|
|
|
|
impl SlowHash for scrypt::ScryptParams {
|
2020-07-03 18:28:33 -04:00
|
|
|
fn hash(input: GenericArray<u8, U32>) -> Result<Vec<u8>, InternalPakeError> {
|
2020-06-11 14:56:34 -07:00
|
|
|
let params = scrypt::ScryptParams::new(15, 8, 1).unwrap();
|
2020-06-08 21:02:01 -07:00
|
|
|
let mut output = [0u8; 32];
|
2020-06-11 14:56:34 -07:00
|
|
|
scrypt::scrypt(&input, &[], ¶ms, &mut output)
|
|
|
|
|
.map_err(|_| InternalPakeError::SlowHashError)?;
|
2020-06-08 21:02:01 -07:00
|
|
|
Ok(output.to_vec())
|
|
|
|
|
}
|
|
|
|
|
}
|