#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![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"
)]
#[cfg(feature = "simple")]
extern crate alloc;
#[cfg(feature = "simple")]
pub use password_hash;
#[cfg(feature = "simple")]
mod simple;
#[cfg(feature = "hmac")]
pub use hmac;
#[cfg(feature = "simple")]
pub use crate::simple::{Algorithm, Params, Pbkdf2};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use digest::{FixedOutput, InvalidLength, KeyInit, Update, typenum::Unsigned};
#[cfg(feature = "hmac")]
use {
digest::{
HashMarker,
block_api::BlockSizeUser,
typenum::{IsLess, NonZero, True, U256},
},
hmac::EagerHash,
};
#[inline(always)]
fn xor(res: &mut [u8], salt: &[u8]) {
debug_assert!(salt.len() >= res.len(), "length mismatch in xor");
res.iter_mut().zip(salt.iter()).for_each(|(a, b)| *a ^= b);
}
#[inline(always)]
fn pbkdf2_body<PRF>(i: u32, chunk: &mut [u8], prf: &PRF, salt: &[u8], rounds: u32)
where
PRF: Update + FixedOutput + Clone,
{
for v in chunk.iter_mut() {
*v = 0;
}
let mut salt = {
let mut prfc = prf.clone();
prfc.update(salt);
prfc.update(&(i + 1).to_be_bytes());
let salt = prfc.finalize_fixed();
xor(chunk, &salt);
salt
};
for _ in 1..rounds {
let mut prfc = prf.clone();
prfc.update(&salt);
salt = prfc.finalize_fixed();
xor(chunk, &salt);
}
}
#[inline]
pub fn pbkdf2<PRF>(
password: &[u8],
salt: &[u8],
rounds: u32,
res: &mut [u8],
) -> Result<(), InvalidLength>
where
PRF: KeyInit + Update + FixedOutput + Clone + Sync,
{
let n = PRF::OutputSize::to_usize();
let prf = PRF::new_from_slice(password)?;
#[cfg(not(feature = "parallel"))]
{
for (i, chunk) in res.chunks_mut(n).enumerate() {
pbkdf2_body(i as u32, chunk, &prf, salt, rounds);
}
}
#[cfg(feature = "parallel")]
{
res.par_chunks_mut(n).enumerate().for_each(|(i, chunk)| {
pbkdf2_body(i as u32, chunk, &prf, salt, rounds);
});
}
Ok(())
}
#[inline]
pub fn pbkdf2_array<PRF, const N: usize>(
password: &[u8],
salt: &[u8],
rounds: u32,
) -> Result<[u8; N], InvalidLength>
where
PRF: KeyInit + Update + FixedOutput + Clone + Sync,
{
let mut buf = [0u8; N];
pbkdf2::<PRF>(password, salt, rounds, &mut buf).map(|()| buf)
}
#[cfg(feature = "hmac")]
pub fn pbkdf2_hmac<D>(password: &[u8], salt: &[u8], rounds: u32, res: &mut [u8])
where
D: EagerHash + HashMarker + Update + FixedOutput + Default + Clone,
<D as EagerHash>::Core: Sync,
<D as BlockSizeUser>::BlockSize: IsLess<U256, Output = True> + NonZero,
{
crate::pbkdf2::<hmac::Hmac<D>>(password, salt, rounds, res)
.expect("HMAC can be initialized with any key length");
}
#[cfg(feature = "hmac")]
pub fn pbkdf2_hmac_array<D, const N: usize>(password: &[u8], salt: &[u8], rounds: u32) -> [u8; N]
where
D: EagerHash + HashMarker + Update + FixedOutput + Default + Clone,
<D as EagerHash>::Core: Sync,
<D as BlockSizeUser>::BlockSize: IsLess<U256, Output = True> + NonZero,
{
let mut buf = [0u8; N];
pbkdf2_hmac::<D>(password, salt, rounds, &mut buf);
buf
}