cascade: add Argon2id with explicit parameters

Keyslots record their own Argon2id costs per profile; the providers'
Argon2id has one fixed set. Cross-checked against both providers.
Refs: R5-3
This commit is contained in:
JohnE 2026-11-16 03:54:15 -05:00
parent 9a39f9e1b8
commit 92c75616ef
5 changed files with 82 additions and 0 deletions

1
Cargo.lock generated
View File

@ -256,6 +256,7 @@ dependencies = [
name = "ccc-cascade"
version = "0.1.0"
dependencies = [
"argon2 0.5.3",
"ccc-crypto-core",
"ccc-crypto-rustcrypto",
"ccc-crypto-wolfssl",

View File

@ -10,6 +10,7 @@ description = "CCC v2 cascade: per-layer HKDF keys, rounds, salt, AAD binding, p
ccc-crypto-core.workspace = true
zeroize.workspace = true
thiserror.workspace = true
argon2 = { version = "0.5", default-features = false, features = ["alloc", "zeroize"] }
[dev-dependencies]
ccc-crypto-wolfssl = { path = "../ccc-crypto-wolfssl" }

View File

@ -14,6 +14,7 @@
pub mod cascade;
pub mod hpke;
pub mod padding;
pub mod password;
pub mod secret;
pub mod stream;
pub mod suite;

View File

@ -0,0 +1,59 @@
//! Password hashing with explicit parameters (Argon2id, RFC 9106).
//!
//! The providers' `KdfAlgorithm::Argon2id` uses one fixed parameter set; the
//! keyslots need the parameters each slot records (per profile, D-28: never
//! reduced per device), so this module takes them explicitly.
use zeroize::Zeroizing;
use crate::CascadeError;
/// Argon2id cost parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Argon2Params {
/// Memory in KiB.
pub memory_kib: u32,
/// Passes over memory.
pub iterations: u32,
/// Lanes.
pub parallelism: u32,
}
/// Fewest bytes of salt accepted.
pub const MIN_SALT_LEN: usize = 16;
/// Argon2id(`secret`, `salt`) with `params`, producing `out_len` bytes.
pub fn argon2id(
secret: &[u8],
salt: &[u8],
params: Argon2Params,
out_len: usize,
) -> Result<Zeroizing<Vec<u8>>, CascadeError> {
if salt.len() < MIN_SALT_LEN {
return Err(CascadeError::InvalidInput("Argon2id salt shorter than 16 bytes"));
}
let p = argon2::Params::new(params.memory_kib, params.iterations, params.parallelism, Some(out_len))
.map_err(|_| CascadeError::InvalidInput("bad Argon2id parameters"))?;
let a = argon2::Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, p);
let mut out = Zeroizing::new(vec![0u8; out_len]);
a.hash_password_into(secret, salt, &mut out).map_err(|_| CascadeError::InvalidInput("Argon2id failed"))?;
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deterministic_and_parameter_sensitive() {
// Known-answer agreement with both providers is in
// tests/password_cross_check.rs.
let p = Argon2Params { memory_kib: 64, iterations: 3, parallelism: 1 };
let a = argon2id(b"password", &[1; 16], p, 32).unwrap();
assert_eq!(a, argon2id(b"password", &[1; 16], p, 32).unwrap());
assert_ne!(a, argon2id(b"password", &[2; 16], p, 32).unwrap());
assert_ne!(a, argon2id(b"passwore", &[1; 16], p, 32).unwrap());
assert_ne!(a, argon2id(b"password", &[1; 16], Argon2Params { iterations: 4, ..p }, 32).unwrap());
assert!(argon2id(b"password", &[1; 8], p, 32).is_err());
}
}

View File

@ -0,0 +1,20 @@
//! Argon2id with explicit parameters matches both providers' fixed-parameter
//! Argon2id (64 MiB, t=3, 4 lanes), which use two different argon2 crate
//! versions, and the providers agree with the pinned conformance vectors.
use ccc_cascade::password::{argon2id, Argon2Params};
use ccc_crypto_core::{KdfAlgorithm, KdfProvider};
#[test]
fn matches_both_providers() {
let p = Argon2Params { memory_kib: 64 * 1024, iterations: 3, parallelism: 4 };
let salt = [0x24u8; 16];
let ours = argon2id(b"correct horse battery staple", &salt, p, 32).unwrap();
let wolf = ccc_crypto_wolfssl::WolfSslProvider::new()
.derive_key(KdfAlgorithm::Argon2id, b"correct horse battery staple", &salt, b"", 32)
.unwrap();
let rc = ccc_crypto_rustcrypto::RustCryptoProvider::new()
.derive_key(KdfAlgorithm::Argon2id, b"correct horse battery staple", &salt, b"", 32)
.unwrap();
assert_eq!(ours.as_slice(), wolf.as_slice());
assert_eq!(ours.as_slice(), rc.as_slice());
}