Adding a hash type to CipherSuite (#24)

This commit is contained in:
Kevin Lewi
2020-07-27 15:25:04 -07:00
committed by GitHub
parent 72a3928cbb
commit c2edb2d95e
15 changed files with 570 additions and 492 deletions
+16 -9
View File
@@ -6,29 +6,36 @@
//! Trait specifying a slow hashing function
use crate::errors::InternalPakeError;
use generic_array::{typenum::U32, GenericArray};
use crate::hash::Hash;
use digest::Digest;
use generic_array::GenericArray;
/// Used for the slow hashing function in OPAQUE
pub trait SlowHash {
pub trait SlowHash<D: Hash> {
/// Computes the slow hashing function
fn hash(input: GenericArray<u8, U32>) -> Result<Vec<u8>, InternalPakeError>;
fn hash(
input: GenericArray<u8, <D as Digest>::OutputSize>,
) -> Result<Vec<u8>, InternalPakeError>;
}
/// A no-op hash which simply returns its input
pub struct NoOpHash;
impl SlowHash for NoOpHash {
fn hash(input: GenericArray<u8, U32>) -> Result<Vec<u8>, InternalPakeError> {
impl<D: Hash> SlowHash<D> for NoOpHash {
fn hash(
input: GenericArray<u8, <D as Digest>::OutputSize>,
) -> Result<Vec<u8>, InternalPakeError> {
Ok(input.to_vec())
}
}
#[cfg(feature = "slow-hash")]
impl SlowHash for scrypt::ScryptParams {
fn hash(input: GenericArray<u8, U32>) -> Result<Vec<u8>, InternalPakeError> {
impl<D: Hash> SlowHash<D> for scrypt::ScryptParams {
fn hash(
input: GenericArray<u8, <D as Digest>::OutputSize>,
) -> Result<Vec<u8>, InternalPakeError> {
let params = scrypt::ScryptParams::new(15, 8, 1).unwrap();
let mut output = [0u8; 32];
let mut output = [0u8; <D as Digest>::OutputSize::to_usize()];
scrypt::scrypt(&input, &[], &params, &mut output)
.map_err(|_| InternalPakeError::SlowHashError)?;
Ok(output.to_vec())