use crate::{Error, KdfAlg, Result};
use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "encryption")]
use {crate::Cipher, bcrypt_pbkdf::bcrypt_pbkdf, rand_core::TryCryptoRng, zeroize::Zeroizing};
#[cfg(feature = "encryption")]
const DEFAULT_BCRYPT_ROUNDS: u32 = 32;
#[cfg(feature = "alloc")]
const MAX_BCRYPT_ROUNDS: u32 = 1 << 20;
#[cfg(feature = "encryption")]
const DEFAULT_SALT_SIZE: usize = 16;
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
#[derive(Default)]
pub enum Kdf {
#[default]
None,
#[cfg(feature = "alloc")]
Bcrypt {
salt: Vec<u8>,
rounds: u32,
},
}
impl Kdf {
#[cfg(feature = "encryption")]
pub fn new<R: TryCryptoRng + ?Sized>(algorithm: KdfAlg, rng: &mut R) -> Result<Self> {
let mut salt = vec![0u8; DEFAULT_SALT_SIZE];
rng.try_fill_bytes(&mut salt)
.map_err(|_| Error::RngFailure)?;
match algorithm {
KdfAlg::None => {
Err(Error::AlgorithmUnknown)
}
KdfAlg::Bcrypt => Ok(Kdf::Bcrypt {
salt,
rounds: DEFAULT_BCRYPT_ROUNDS,
}),
}
}
#[must_use]
pub fn algorithm(&self) -> KdfAlg {
match self {
Self::None => KdfAlg::None,
#[cfg(feature = "alloc")]
Self::Bcrypt { .. } => KdfAlg::Bcrypt,
}
}
#[cfg(feature = "encryption")]
pub fn derive(&self, password: impl AsRef<[u8]>, output: &mut [u8]) -> Result<()> {
match self {
Kdf::None => Err(Error::Decrypted),
Kdf::Bcrypt { salt, rounds } => {
validate_bcrypt_rounds(*rounds)?;
bcrypt_pbkdf(password, salt, *rounds, output).map_err(|_| Error::Crypto)?;
Ok(())
}
}
}
#[cfg(feature = "encryption")]
pub fn derive_key_and_iv(
&self,
cipher: Cipher,
password: impl AsRef<[u8]>,
) -> Result<(Zeroizing<Vec<u8>>, Vec<u8>)> {
let (key_size, iv_size) = cipher.key_and_iv_size().ok_or(Error::Decrypted)?;
let okm_size = key_size
.checked_add(iv_size)
.ok_or(encoding::Error::Length)?;
let mut okm = Zeroizing::new(vec![0u8; okm_size]);
self.derive(password, &mut okm)?;
let mut iv = okm.split_off(key_size);
if cipher == Cipher::ChaCha20Poly1305 {
iv.copy_from_slice(&cipher::ChaChaNonce::default());
}
Ok((okm, iv))
}
#[must_use]
pub fn is_none(&self) -> bool {
self == &Self::None
}
#[must_use]
pub fn is_some(&self) -> bool {
!self.is_none()
}
#[cfg(feature = "alloc")]
#[must_use]
pub fn is_bcrypt(&self) -> bool {
matches!(self, Self::Bcrypt { .. })
}
}
impl Decode for Kdf {
type Error = Error;
fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
match KdfAlg::decode(reader)? {
KdfAlg::None => {
if usize::decode(reader)? == 0 {
Ok(Self::None)
} else {
Err(Error::AlgorithmUnknown)
}
}
KdfAlg::Bcrypt => {
#[cfg(not(feature = "alloc"))]
return Err(Error::AlgorithmUnknown);
#[cfg(feature = "alloc")]
reader.read_prefixed(|reader| {
let salt = Vec::decode(reader)?;
let rounds = u32::decode(reader)?;
validate_bcrypt_rounds(rounds)?;
Ok(Self::Bcrypt { salt, rounds })
})
}
}
}
}
impl Encode for Kdf {
fn encoded_len(&self) -> encoding::Result<usize> {
let kdfopts_prefixed_len = match self {
Self::None => 4,
#[cfg(feature = "alloc")]
Self::Bcrypt { salt, rounds } => {
validate_bcrypt_rounds(*rounds).map_err(|_| encoding::Error::Overflow)?;
[12, salt.len()].checked_sum()?
}
};
[self.algorithm().encoded_len()?, kdfopts_prefixed_len].checked_sum()
}
fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.encoded_len()?;
self.algorithm().encode(writer)?;
match self {
Self::None => 0usize.encode(writer)?,
#[cfg(feature = "alloc")]
Self::Bcrypt { salt, rounds } => {
[8, salt.len()].checked_sum()?.encode(writer)?;
salt.encode(writer)?;
rounds.encode(writer)?;
}
}
Ok(())
}
}
#[cfg(feature = "alloc")]
fn validate_bcrypt_rounds(rounds: u32) -> Result<()> {
if !(1..=MAX_BCRYPT_ROUNDS).contains(&rounds) {
return Err(Error::Crypto);
}
Ok(())
}
#[cfg(all(test, feature = "alloc"))]
mod tests {
use super::{Kdf, MAX_BCRYPT_ROUNDS};
use crate::Error;
use alloc::vec::Vec;
use encoding::{Decode, Encode, Reader};
#[test]
fn bcrypt_rounds_boundaries() {
let mut encoded = Kdf::Bcrypt {
salt: b"salt".to_vec(),
rounds: 1,
}
.encode_vec()
.unwrap();
for (rounds, valid) in [
(0, false),
(1, true),
(MAX_BCRYPT_ROUNDS, true),
(MAX_BCRYPT_ROUNDS + 1, false),
(u32::MAX, false),
] {
let kdf = Kdf::Bcrypt {
salt: b"salt".to_vec(),
rounds,
};
let offset = encoded.len() - 4;
encoded[offset..].copy_from_slice(&rounds.to_be_bytes());
let mut output = Vec::new();
let expected = if valid {
assert_eq!(kdf.encoded_len(), Ok(encoded.len()));
kdf.encode(&mut output).unwrap();
assert_eq!(output, encoded);
Ok(kdf)
} else {
assert_eq!(kdf.encoded_len(), Err(encoding::Error::Overflow));
assert_eq!(kdf.encode(&mut output), Err(encoding::Error::Overflow));
assert!(output.is_empty());
Err(Error::Crypto)
};
let mut decoder = encoded.as_slice().into_decoder().unwrap();
assert_eq!(Kdf::decode(&mut decoder), expected, "rounds: {rounds}");
if valid {
decoder.finish(()).unwrap();
}
}
}
#[cfg(feature = "encryption")]
#[test]
fn derive_bcrypt_invalid_rounds() {
struct UnreadPassword;
impl AsRef<[u8]> for UnreadPassword {
fn as_ref(&self) -> &[u8] {
panic!("invalid rounds must be rejected before accessing the password");
}
}
for rounds in [0, MAX_BCRYPT_ROUNDS + 1, u32::MAX] {
let kdf = Kdf::Bcrypt {
salt: b"salt".to_vec(),
rounds,
};
let mut output = [0xa5; 32];
assert_eq!(kdf.derive(UnreadPassword, &mut output), Err(Error::Crypto));
assert_eq!(output, [0xa5; 32]);
}
}
}