Skip to main content

opaque_vx/
ksf.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) VexaHub and contributors.
3// Copyright (c) Meta Platforms, Inc. and affiliates.
4
5//! Trait specifying a key stretching function
6
7use generic_array::{ArrayLength, GenericArray};
8
9use crate::errors::InternalError;
10
11/// Used for the key stretching function in OPAQUE
12pub trait Ksf: Default {
13    /// Computes the key stretching function
14    fn hash<L: ArrayLength>(
15        &self,
16        input: GenericArray<u8, L>,
17    ) -> Result<GenericArray<u8, L>, InternalError>;
18}
19
20/// A no-op hash which simply returns its input
21#[derive(Default)]
22pub struct Identity;
23
24impl Ksf for Identity {
25    fn hash<L: ArrayLength>(
26        &self,
27        input: GenericArray<u8, L>,
28    ) -> Result<GenericArray<u8, L>, InternalError> {
29        Ok(input)
30    }
31}
32
33#[cfg(feature = "argon2")]
34impl Ksf for argon2::Argon2<'_> {
35    fn hash<L: ArrayLength>(
36        &self,
37        input: GenericArray<u8, L>,
38    ) -> Result<GenericArray<u8, L>, InternalError> {
39        let mut output = GenericArray::default();
40        self.hash_password_into(&input, &[0; argon2::RECOMMENDED_SALT_LEN], &mut output)
41            .map_err(|_| InternalError::KsfError)?;
42        Ok(output)
43    }
44}