#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
#![warn(rust_2018_idioms, missing_docs)]
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
#[cfg(feature = "alloc")]
extern crate alloc;
mod algorithm;
mod balloon;
mod error;
mod params;
pub use crate::{
algorithm::Algorithm,
error::{Error, Result},
params::Params,
};
#[cfg(feature = "password-hash")]
pub use password_hash::{
self, CustomizedPasswordHasher, PasswordHasher, PasswordVerifier, Version,
phc::{Output, PasswordHash},
};
use core::marker::PhantomData;
use crypto_bigint::ArrayDecoding;
use digest::array::Array;
use digest::typenum::Unsigned;
use digest::{Digest, FixedOutputReset};
#[cfg(feature = "kdf")]
pub use kdf::{self, Kdf, Pbkdf};
#[cfg(all(feature = "alloc", feature = "password-hash"))]
pub use password_hash::phc::Salt;
#[cfg(all(feature = "alloc", feature = "password-hash"))]
use password_hash::phc::ParamsString;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
#[derive(Clone, Default)]
pub struct Balloon<'key, D: Digest + FixedOutputReset>
where
Array<u8, D::OutputSize>: ArrayDecoding,
{
pub digest: PhantomData<D>,
pub algorithm: Algorithm,
pub params: Params,
pub secret: Option<&'key [u8]>,
}
impl<'key, D> Balloon<'key, D>
where
D: Digest + FixedOutputReset,
Array<u8, D::OutputSize>: ArrayDecoding,
{
pub fn new(algorithm: Algorithm, params: Params, secret: Option<&'key [u8]>) -> Self {
Self {
digest: PhantomData,
algorithm,
params,
secret,
}
}
#[cfg(feature = "alloc")]
pub fn hash_password_to_array(
&self,
pwd: &[u8],
salt: &[u8],
) -> Result<Array<u8, D::OutputSize>> {
let mut output = Array::default();
self.hash_password_into(pwd, salt, &mut output)?;
Ok(output)
}
#[cfg(feature = "alloc")]
pub fn hash_password_into(&self, pwd: &[u8], salt: &[u8], output: &mut [u8]) -> Result<()> {
#[cfg(not(feature = "parallel"))]
let mut memory = alloc::vec![Array::default(); self.params.s_cost.get() as usize];
#[cfg(feature = "parallel")]
let mut memory = alloc::vec![Array::default(); (self.params.s_cost.get() * self.params.p_cost.get()) as usize];
self.hash_password_into_with_memory(pwd, salt, &mut memory, output)?;
#[cfg(feature = "zeroize")]
memory.iter_mut().for_each(|block| block.zeroize());
Ok(())
}
pub fn hash_password_with_memory(
&self,
pwd: &[u8],
salt: &[u8],
memory_blocks: &mut [Array<u8, D::OutputSize>],
) -> Result<Array<u8, D::OutputSize>> {
let mut output = Array::default();
self.hash_password_into_with_memory(pwd, salt, memory_blocks, &mut output)?;
Ok(output)
}
pub fn hash_password_into_with_memory(
&self,
pwd: &[u8],
salt: &[u8],
memory_blocks: &mut [Array<u8, D::OutputSize>],
output: &mut [u8],
) -> Result<()> {
let output = if output.len() == D::OutputSize::USIZE {
<&mut Array<_, _>>::try_from(output).expect("size mismatch")
} else {
return Err(Error::OutputSize {
actual: output.len(),
expected: D::OutputSize::USIZE,
});
};
match self.algorithm {
Algorithm::Balloon => {
balloon::balloon::<D>(pwd, salt, self.secret, self.params, memory_blocks).map(
|hash| {
output.copy_from_slice(&hash);
},
)
}
Algorithm::BalloonM => {
balloon::balloon_m::<D>(pwd, salt, self.secret, self.params, memory_blocks, output)
}
}
}
}
#[cfg(feature = "kdf")]
impl<D> Kdf for Balloon<'_, D>
where
D: Digest + FixedOutputReset,
Array<u8, D::OutputSize>: ArrayDecoding,
{
fn derive_key(&self, password: &[u8], salt: &[u8], out: &mut [u8]) -> kdf::Result<()> {
self.hash_password_into(password, salt, out)?;
Ok(())
}
}
#[cfg(feature = "kdf")]
impl<D> Pbkdf for Balloon<'_, D>
where
D: Digest + FixedOutputReset,
Array<u8, D::OutputSize>: ArrayDecoding,
{
}
#[cfg(all(feature = "alloc", feature = "password-hash"))]
impl<D> CustomizedPasswordHasher<PasswordHash> for Balloon<'_, D>
where
D: Digest + FixedOutputReset,
Array<u8, D::OutputSize>: ArrayDecoding,
{
type Params = Params;
fn hash_password_customized(
&self,
password: &[u8],
salt: &[u8],
alg_id: Option<&str>,
version: Option<Version>,
params: Params,
) -> password_hash::Result<PasswordHash> {
let algorithm = alg_id
.map(Algorithm::try_from)
.transpose()?
.unwrap_or_default();
if let Some(version) = version {
if version != 1 {
return Err(password_hash::Error::Version);
}
}
Self::new(algorithm, params, self.secret).hash_password_with_salt(password, salt)
}
}
#[cfg(all(feature = "alloc", feature = "password-hash"))]
impl<D> PasswordHasher<PasswordHash> for Balloon<'_, D>
where
D: Digest + FixedOutputReset,
Array<u8, D::OutputSize>: ArrayDecoding,
{
fn hash_password_with_salt(
&self,
password: &[u8],
salt: &[u8],
) -> password_hash::Result<PasswordHash> {
let salt = Salt::new(salt)?;
let hash = self.hash_password_to_array(password, &salt)?;
let output = Output::new(&hash)?;
Ok(PasswordHash {
algorithm: self.algorithm.ident(),
version: Some(1),
params: ParamsString::try_from(&self.params)?,
salt: Some(salt),
hash: Some(output),
})
}
}
#[cfg(all(feature = "alloc", feature = "password-hash"))]
impl<D> PasswordVerifier<str> for Balloon<'_, D>
where
D: Digest + FixedOutputReset,
Array<u8, D::OutputSize>: ArrayDecoding,
{
fn verify_password(&self, password: &[u8], hash: &str) -> password_hash::Result<()> {
self.verify_password(password, &PasswordHash::new(hash)?)
}
}
impl<D: Digest + FixedOutputReset> From<Params> for Balloon<'_, D>
where
Array<u8, D::OutputSize>: ArrayDecoding,
{
fn from(params: Params) -> Self {
Self::new(Algorithm::default(), params, None)
}
}