rustcrypto: add HKDF, BLAKE2b KDF and Argon2id
Same semantics as the wolfSSL provider (zero salt, 255*HashLen cap, 64 MiB/3/4 Argon2id) so either provider derives identical keys. Refs: R3-4
This commit is contained in:
parent
bb5473eeca
commit
2a8dbdcd6b
|
|
@ -128,7 +128,20 @@ dependencies = [
|
||||||
"base64ct",
|
"base64ct",
|
||||||
"blake2 0.10.6",
|
"blake2 0.10.6",
|
||||||
"cpufeatures 0.2.17",
|
"cpufeatures 0.2.17",
|
||||||
"password-hash",
|
"password-hash 0.5.0",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "argon2"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "134c52ddac6d63c576bef8168db10c83c49c26444ecbc68060fef078925a901c"
|
||||||
|
dependencies = [
|
||||||
|
"base64ct",
|
||||||
|
"blake2 0.11.0",
|
||||||
|
"cpufeatures 0.3.1",
|
||||||
|
"password-hash 0.6.1",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -272,11 +285,13 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes-gcm",
|
"aes-gcm",
|
||||||
"aes-gcm-siv",
|
"aes-gcm-siv",
|
||||||
|
"argon2 0.6.0",
|
||||||
"ascon-aead",
|
"ascon-aead",
|
||||||
"blake2 0.11.0",
|
"blake2 0.11.0",
|
||||||
"ccc-crypto-core",
|
"ccc-crypto-core",
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"hex",
|
"hex",
|
||||||
|
"hkdf",
|
||||||
"hmac",
|
"hmac",
|
||||||
"log",
|
"log",
|
||||||
"poly1305",
|
"poly1305",
|
||||||
|
|
@ -290,7 +305,7 @@ dependencies = [
|
||||||
name = "ccc-crypto-wolfssl"
|
name = "ccc-crypto-wolfssl"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argon2",
|
"argon2 0.5.3",
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"ccc-crypto-core",
|
"ccc-crypto-core",
|
||||||
"cmake",
|
"cmake",
|
||||||
|
|
@ -541,6 +556,15 @@ version = "0.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hkdf"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018"
|
||||||
|
dependencies = [
|
||||||
|
"hmac",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hmac"
|
name = "hmac"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
|
|
@ -690,6 +714,25 @@ dependencies = [
|
||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "password-hash"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "aab41826031698d6ffcd9cff78ef56ef998e39dc7e5067cdfebe373842d4723b"
|
||||||
|
dependencies = [
|
||||||
|
"phc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "phc"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "44dc769b75f93afdddd8c7fa12d685292ddeff1e66f7f0f3a234cf1818afe892"
|
||||||
|
dependencies = [
|
||||||
|
"base64ct",
|
||||||
|
"ctutils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "poly1305"
|
name = "poly1305"
|
||||||
version = "0.9.1"
|
version = "0.9.1"
|
||||||
|
|
|
||||||
|
|
@ -28,5 +28,9 @@ hmac = "0.13"
|
||||||
poly1305 = "0.9"
|
poly1305 = "0.9"
|
||||||
subtle = "2.6"
|
subtle = "2.6"
|
||||||
|
|
||||||
|
# KDF
|
||||||
|
hkdf = "0.13"
|
||||||
|
argon2 = { version = "0.6", default-features = false, features = ["alloc", "zeroize"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,220 @@
|
||||||
|
//! KDFs: HKDF-SHA-2 (`hkdf`), the BLAKE2b-512 KDF and Argon2id (`argon2`).
|
||||||
|
//!
|
||||||
|
//! Semantics match the wolfSSL provider exactly, so either provider can
|
||||||
|
//! derive the same key:
|
||||||
|
//!
|
||||||
|
//! - HKDF (RFC 5869); an empty salt means the all-zero salt of hash length,
|
||||||
|
//! and the output is capped at 255 × hash length.
|
||||||
|
//! - BLAKE2b-512 KDF: `BLAKE2b-512(ikm || salt || info)` truncated to
|
||||||
|
//! `length` (at most 64 bytes).
|
||||||
|
//! - Argon2id v1.3 with the app defaults: 64 MiB, 3 passes, 4 lanes; the salt
|
||||||
|
//! must be at least 8 bytes and `info` is not used.
|
||||||
|
|
||||||
|
use argon2::{Algorithm, Argon2, Params, Version};
|
||||||
|
use hkdf::Hkdf;
|
||||||
|
use sha2::{Sha256, Sha384, Sha512};
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
|
use ccc_crypto_core::{
|
||||||
|
algorithms::{HashAlgorithm, KdfAlgorithm},
|
||||||
|
error::CryptoError,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Argon2id memory cost in KiB (64 MiB), as in the wolfSSL provider.
|
||||||
|
pub const ARGON2_M_COST_KIB: u32 = 65_536;
|
||||||
|
/// Argon2id passes.
|
||||||
|
pub const ARGON2_T_COST: u32 = 3;
|
||||||
|
/// Argon2id lanes.
|
||||||
|
pub const ARGON2_P_COST: u32 = 4;
|
||||||
|
|
||||||
|
/// Derive `length` bytes from `ikm`.
|
||||||
|
pub fn derive_key(
|
||||||
|
algo: KdfAlgorithm,
|
||||||
|
ikm: &[u8],
|
||||||
|
salt: &[u8],
|
||||||
|
info: &[u8],
|
||||||
|
length: usize,
|
||||||
|
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||||
|
match algo {
|
||||||
|
KdfAlgorithm::Sha256 | KdfAlgorithm::Sha384 | KdfAlgorithm::Sha512 => {
|
||||||
|
hkdf(algo, ikm, salt, info, length)
|
||||||
|
}
|
||||||
|
KdfAlgorithm::Blake2b512 => blake2b_kdf(ikm, salt, info, length),
|
||||||
|
KdfAlgorithm::Argon2id => argon2id(
|
||||||
|
ikm,
|
||||||
|
salt,
|
||||||
|
length,
|
||||||
|
ARGON2_M_COST_KIB,
|
||||||
|
ARGON2_T_COST,
|
||||||
|
ARGON2_P_COST,
|
||||||
|
),
|
||||||
|
KdfAlgorithm::Kmac256 => Err(CryptoError::FeatureNotCompiled(algo.name().into())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hkdf(
|
||||||
|
algo: KdfAlgorithm,
|
||||||
|
ikm: &[u8],
|
||||||
|
salt: &[u8],
|
||||||
|
info: &[u8],
|
||||||
|
length: usize,
|
||||||
|
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||||
|
let hash_len = match algo {
|
||||||
|
KdfAlgorithm::Sha256 => 32,
|
||||||
|
KdfAlgorithm::Sha384 => 48,
|
||||||
|
_ => 64,
|
||||||
|
};
|
||||||
|
let max_len = 255 * hash_len;
|
||||||
|
if length > max_len {
|
||||||
|
return Err(CryptoError::InvalidInput(format!(
|
||||||
|
"HKDF: requested length {length} exceeds maximum {max_len}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
// `None` is the RFC 5869 zero salt, the same as wolfSSL's empty salt.
|
||||||
|
let salt = if salt.is_empty() { None } else { Some(salt) };
|
||||||
|
let mut okm = Zeroizing::new(vec![0u8; length]);
|
||||||
|
let res = match algo {
|
||||||
|
KdfAlgorithm::Sha256 => Hkdf::<Sha256>::new(salt, ikm).expand(info, &mut okm),
|
||||||
|
KdfAlgorithm::Sha384 => Hkdf::<Sha384>::new(salt, ikm).expand(info, &mut okm),
|
||||||
|
_ => Hkdf::<Sha512>::new(salt, ikm).expand(info, &mut okm),
|
||||||
|
};
|
||||||
|
res.map_err(|_| CryptoError::InvalidInput("HKDF: output too long".into()))?;
|
||||||
|
Ok(okm)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn blake2b_kdf(
|
||||||
|
ikm: &[u8],
|
||||||
|
salt: &[u8],
|
||||||
|
info: &[u8],
|
||||||
|
length: usize,
|
||||||
|
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||||
|
if length > 64 {
|
||||||
|
return Err(CryptoError::InvalidInput(
|
||||||
|
"BLAKE2b-512 KDF: requested length exceeds 64 bytes; use HKDF-SHA512".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let mut input = Zeroizing::new(Vec::with_capacity(ikm.len() + salt.len() + info.len()));
|
||||||
|
input.extend_from_slice(ikm);
|
||||||
|
input.extend_from_slice(salt);
|
||||||
|
input.extend_from_slice(info);
|
||||||
|
let digest = Zeroizing::new(crate::hash::hash(HashAlgorithm::Blake2b512, &input)?);
|
||||||
|
Ok(Zeroizing::new(digest[..length].to_vec()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Argon2id v1.3 with explicit cost parameters.
|
||||||
|
///
|
||||||
|
/// [`derive_key`] uses the app defaults; this entry point exists for tests
|
||||||
|
/// and for callers that store their own parameters.
|
||||||
|
pub fn argon2id(
|
||||||
|
password: &[u8],
|
||||||
|
salt: &[u8],
|
||||||
|
length: usize,
|
||||||
|
m_cost_kib: u32,
|
||||||
|
t_cost: u32,
|
||||||
|
p_cost: u32,
|
||||||
|
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||||
|
if salt.len() < 8 {
|
||||||
|
return Err(CryptoError::InvalidInput(
|
||||||
|
"Argon2id requires at least 8-byte salt".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let params = Params::new(m_cost_kib, t_cost, p_cost, Some(length))
|
||||||
|
.map_err(|e| CryptoError::InvalidInput(format!("Argon2 params error: {e}")))?;
|
||||||
|
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
|
||||||
|
let mut out = Zeroizing::new(vec![0u8; length]);
|
||||||
|
argon2
|
||||||
|
.hash_password_into(password, salt, &mut out)
|
||||||
|
.map_err(|e| CryptoError::InvalidInput(format!("Argon2id hash error: {e}")))?;
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn h(s: &str) -> Vec<u8> {
|
||||||
|
hex::decode(s).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hkdf_sha256_rfc5869_tc1() {
|
||||||
|
let okm = derive_key(
|
||||||
|
KdfAlgorithm::Sha256,
|
||||||
|
&h("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
|
||||||
|
&h("000102030405060708090a0b0c"),
|
||||||
|
&h("f0f1f2f3f4f5f6f7f8f9"),
|
||||||
|
42,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
hex::encode(&*okm),
|
||||||
|
"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hkdf_sha512_rfc5869_tc1_inputs() {
|
||||||
|
// Reference value pinned for the wolfSSL HKDF-SHA-512 fix.
|
||||||
|
let okm = derive_key(
|
||||||
|
KdfAlgorithm::Sha512,
|
||||||
|
&h("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
|
||||||
|
&h("000102030405060708090a0b0c"),
|
||||||
|
&h("f0f1f2f3f4f5f6f7f8f9"),
|
||||||
|
42,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
hex::encode(&*okm),
|
||||||
|
"832390086cda71fb47625bb5ceb168e4c8e26a1a16ed34d9fc7fe92c1481579338da362cb8d9f925d7cb"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hkdf_empty_salt_is_zero_salt() {
|
||||||
|
for (algo, n) in [
|
||||||
|
(KdfAlgorithm::Sha256, 32),
|
||||||
|
(KdfAlgorithm::Sha384, 48),
|
||||||
|
(KdfAlgorithm::Sha512, 64),
|
||||||
|
] {
|
||||||
|
let a = derive_key(algo, b"ikm", b"", b"info", 32).unwrap();
|
||||||
|
let b = derive_key(algo, b"ikm", &vec![0u8; n], b"info", 32).unwrap();
|
||||||
|
assert_eq!(*a, *b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hkdf_length_limit() {
|
||||||
|
assert!(derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 255 * 32).is_ok());
|
||||||
|
assert!(matches!(
|
||||||
|
derive_key(KdfAlgorithm::Sha256, b"k", b"", b"", 255 * 32 + 1),
|
||||||
|
Err(CryptoError::InvalidInput(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn blake2b_kdf_is_hash_of_concatenation() {
|
||||||
|
let okm = derive_key(KdfAlgorithm::Blake2b512, b"a", b"b", b"c", 64).unwrap();
|
||||||
|
let digest = crate::hash::hash(HashAlgorithm::Blake2b512, b"abc").unwrap();
|
||||||
|
assert_eq!(*okm, digest);
|
||||||
|
let short = derive_key(KdfAlgorithm::Blake2b512, b"a", b"b", b"c", 16).unwrap();
|
||||||
|
assert_eq!(*short, digest[..16]);
|
||||||
|
assert!(derive_key(KdfAlgorithm::Blake2b512, b"a", b"", b"", 65).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn argon2id_rules() {
|
||||||
|
assert!(argon2id(b"pw", b"short", 32, 64, 1, 1).is_err());
|
||||||
|
let a = argon2id(b"pw", b"saltsalt", 32, 64, 1, 1).unwrap();
|
||||||
|
let b = argon2id(b"pw", b"saltsalt", 32, 64, 1, 1).unwrap();
|
||||||
|
assert_eq!(*a, *b);
|
||||||
|
assert_ne!(*a, *argon2id(b"pw", b"saltsalT", 32, 64, 1, 1).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn kmac_is_not_compiled() {
|
||||||
|
assert!(matches!(
|
||||||
|
derive_key(KdfAlgorithm::Kmac256, b"k", b"", b"", 32),
|
||||||
|
Err(CryptoError::FeatureNotCompiled(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,4 +9,5 @@
|
||||||
|
|
||||||
pub mod aead;
|
pub mod aead;
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
|
pub mod kdf;
|
||||||
pub mod mac;
|
pub mod mac;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue