use crate::*;
pub const ARGON2_ID_BYTES_MIN: usize =
libsodium_sys::crypto_pwhash_argon2id_BYTES_MIN as usize;
pub const ARGON2_ID_SALTBYTES: usize =
libsodium_sys::crypto_pwhash_argon2id_SALTBYTES as usize;
pub const ARGON2_ID_PASSWD_MIN: usize =
libsodium_sys::crypto_pwhash_argon2id_PASSWD_MIN as usize;
pub const ARGON2_ID_PASSWD_MAX: usize =
libsodium_sys::crypto_pwhash_argon2id_PASSWD_MAX as usize;
pub const ARGON2_ID_OPSLIMIT_MIN: u32 =
libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_MIN;
pub const ARGON2_ID_OPSLIMIT_INTERACTIVE: u32 =
libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE;
pub const ARGON2_ID_OPSLIMIT_MODERATE: u32 =
libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_MODERATE;
pub const ARGON2_ID_OPSLIMIT_SENSITIVE: u32 =
libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE;
pub const ARGON2_ID_OPSLIMIT_MAX: u32 =
libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_MAX;
pub const ARGON2_ID_MEMLIMIT_MIN: u32 =
libsodium_sys::crypto_pwhash_argon2id_MEMLIMIT_MIN;
pub const ARGON2_ID_MEMLIMIT_INTERACTIVE: u32 =
libsodium_sys::crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE;
pub const ARGON2_ID_MEMLIMIT_MODERATE: u32 =
libsodium_sys::crypto_pwhash_argon2id_MEMLIMIT_MODERATE;
pub const ARGON2_ID_MEMLIMIT_SENSITIVE: u32 =
libsodium_sys::crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE;
pub fn blocking_argon2id(
hash: &mut [u8],
passphrase: &[u8],
salt: &[u8; ARGON2_ID_SALTBYTES],
ops_limit: u32,
mem_limit: u32,
) -> Result<()> {
if hash.len() < libsodium_sys::crypto_pwhash_argon2id_BYTES_MIN as usize {
return Err(Error::other("bad hash size"));
}
if passphrase.len()
< libsodium_sys::crypto_pwhash_argon2id_PASSWD_MIN as usize
|| passphrase.len()
> libsodium_sys::crypto_pwhash_argon2id_PASSWD_MAX as usize
{
return Err(Error::other("bad passphrase size"));
}
if !(libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_MIN
..=libsodium_sys::crypto_pwhash_argon2id_OPSLIMIT_MAX)
.contains(&ops_limit)
{
return Err(Error::other("bad ops limit"));
}
if mem_limit < libsodium_sys::crypto_pwhash_argon2id_MEMLIMIT_MIN {
return Err(Error::other("bad mem limit"));
}
crate::sodium_init();
unsafe {
if libsodium_sys::crypto_pwhash(
raw_ptr_char!(hash),
hash.len() as libc::c_ulonglong,
raw_ptr_ichar_immut!(passphrase),
passphrase.len() as libc::c_ulonglong,
raw_ptr_char_immut!(salt),
ops_limit as u64,
mem_limit as usize,
libsodium_sys::crypto_pwhash_argon2id_ALG_ARGON2ID13 as libc::c_int,
) == 0_i32
{
Ok(())
} else {
Err(Error::other("internal"))
}
}
}