use core::ffi::c_int;
use crate::error::{check, len_as_c_int, WolfCryptError};
macro_rules! impl_pbkdf2 {
(
$fn_name:ident,
$hash_type:expr,
$cfg_gate:meta,
$doc:expr
) => {
#[doc = $doc]
#[$cfg_gate]
pub fn $fn_name(
password: &[u8],
salt: &[u8],
rounds: u32,
output: &mut [u8],
) -> Result<(), WolfCryptError> {
if rounds == 0 || rounds > c_int::MAX as u32 {
return Err(WolfCryptError::INVALID_INPUT);
}
let rc = unsafe {
wolfcrypt_rs::wc_PBKDF2(
output.as_mut_ptr(),
password.as_ptr(),
len_as_c_int(password.len()),
salt.as_ptr(),
len_as_c_int(salt.len()),
rounds as c_int,
len_as_c_int(output.len()),
$hash_type,
)
};
check(rc, "wc_PBKDF2")
}
};
}
impl_pbkdf2!(
pbkdf2_hmac_sha256,
wolfcrypt_rs::WC_HASH_TYPE_SHA256,
cfg(wolfssl_pbkdf2),
"Derive a key from `password` using PBKDF2-HMAC-SHA256.\n\n\
Writes `output.len()` bytes of derived key material into `output`.\n\n\
Returns an error if wolfCrypt rejects the parameters (e.g. zero-length\n\
output or unsupported hash type)."
);
impl_pbkdf2!(
pbkdf2_hmac_sha384,
wolfcrypt_rs::WC_HASH_TYPE_SHA384,
cfg(all(wolfssl_pbkdf2, wolfssl_sha384)),
"Derive a key from `password` using PBKDF2-HMAC-SHA384.\n\n\
Writes `output.len()` bytes of derived key material into `output`.\n\n\
Returns an error if wolfCrypt rejects the parameters (e.g. zero-length\n\
output or unsupported hash type)."
);
impl_pbkdf2!(
pbkdf2_hmac_sha512,
wolfcrypt_rs::WC_HASH_TYPE_SHA512,
cfg(all(wolfssl_pbkdf2, wolfssl_sha512)),
"Derive a key from `password` using PBKDF2-HMAC-SHA512.\n\n\
Writes `output.len()` bytes of derived key material into `output`.\n\n\
Returns an error if wolfCrypt rejects the parameters (e.g. zero-length\n\
output or unsupported hash type)."
);