wolfssl: fix HKDF-SHA-512 extract

wc_Tls13_HKDF_Extract rejects SHA-512 here, so every HKDF-SHA-512 call
failed; extract is now HMAC(salt, ikm), pinned by reference vectors.
Refs: R3-2
This commit is contained in:
JohnE 2026-11-16 03:19:45 -05:00
parent 7a10b28f2d
commit f8c64390e3
1 changed files with 39 additions and 30 deletions

View File

@ -8,7 +8,8 @@
//! available HKDF primitive is `wc_Tls13_HKDF_Extract` (RFC 5869 Extract step) //! available HKDF primitive is `wc_Tls13_HKDF_Extract` (RFC 5869 Extract step)
//! together with the `wc_Hmac*` family. We implement RFC 5869 HKDF in Rust: //! together with the `wc_Hmac*` family. We implement RFC 5869 HKDF in Rust:
//! //!
//! - **Extract**: `PRK = HMAC-Hash(salt, IKM)` via `wc_Tls13_HKDF_Extract`. //! - **Extract**: `PRK = HMAC-Hash(salt, IKM)` computed with wolfSSL HMAC
//! (`wc_Tls13_HKDF_Extract` rejects SHA-512, so it is not used).
//! - **Expand**: `OKM = T(1)||T(2)||…` where //! - **Expand**: `OKM = T(1)||T(2)||…` where
//! `T(i) = HMAC(PRK, T(i-1) || info || i)` computed with wolfSSL HMAC. //! `T(i) = HMAC(PRK, T(i-1) || info || i)` computed with wolfSSL HMAC.
//! //!
@ -64,8 +65,7 @@ pub fn derive_key(
/// HKDF: Extract then Expand (RFC 5869). /// HKDF: Extract then Expand (RFC 5869).
/// ///
/// Extract uses `wc_Tls13_HKDF_Extract` (wolfSSL's RFC 5869 Extract). /// Both steps are implemented in Rust using wolfSSL's HMAC primitives.
/// Expand is implemented in Rust using wolfSSL's HMAC primitives.
fn hkdf( fn hkdf(
hash_type: i32, hash_type: i32,
ikm: &[u8], ikm: &[u8],
@ -85,40 +85,19 @@ fn hkdf(
hkdf_expand(hash_type, &prk, info, length) hkdf_expand(hash_type, &prk, info, length)
} }
/// HKDF-Extract: `PRK = HMAC-Hash(salt, IKM)`. /// HKDF-Extract: `PRK = HMAC-Hash(salt, IKM)` (RFC 5869 §2.2).
/// ///
/// Wraps `wc_Tls13_HKDF_Extract` which takes a mutable IKM pointer. /// Computed with wolfSSL's HMAC directly. `wc_Tls13_HKDF_Extract` was used
/// before, but it rejects SHA-512 in this build (BAD_FUNC_ARG), so
/// HKDF-SHA-512, the CCC v2 key schedule, failed on every call.
fn hkdf_extract( fn hkdf_extract(
hash_type: i32, hash_type: i32,
salt: &[u8], salt: &[u8],
ikm: &[u8], ikm: &[u8],
) -> Result<Zeroizing<Vec<u8>>, CryptoError> { ) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
let hash_len = hash_output_len(hash_type); let hash_len = hash_output_len(hash_type);
let mut prk = Zeroizing::new(vec![0u8; hash_len]); let prk = crate::mac::hmac(hash_type, salt, ikm, hash_len)?;
// wolfSSL's wc_Tls13_HKDF_Extract takes `byte* ikm` (mutable), so we Ok(Zeroizing::new(prk))
// make a writable copy.
let mut ikm_buf = ikm.to_vec();
unsafe {
let ret = crate::sys::wc_Tls13_HKDF_Extract(
prk.as_mut_ptr(),
if salt.is_empty() {
std::ptr::null()
} else {
salt.as_ptr()
},
salt.len() as u32,
ikm_buf.as_mut_ptr(),
ikm_buf.len() as u32,
hash_type,
);
if ret != 0 {
return Err(CryptoError::InternalError(format!(
"wc_Tls13_HKDF_Extract returned {ret}"
)));
}
}
Ok(prk)
} }
/// HKDF-Expand: `OKM = T(1) || T(2) || …` truncated to `length` bytes. /// HKDF-Expand: `OKM = T(1) || T(2) || …` truncated to `length` bytes.
@ -251,3 +230,33 @@ fn argon2id(
Ok(out) Ok(out)
} }
#[cfg(test)]
mod hkdf_tests {
use super::*;
fn rfc5869_case1(algo: KdfAlgorithm) -> String {
let ikm = [0x0bu8; 22];
let salt: Vec<u8> = (0u8..13).collect();
let info: Vec<u8> = (0xf0u8..0xfa).collect();
let okm = derive_key(algo, &ikm, &salt, &info, 42).unwrap();
okm.iter().map(|b| format!("{b:02x}")).collect()
}
#[test]
fn hkdf_sha256_matches_rfc5869() {
assert_eq!(
rfc5869_case1(KdfAlgorithm::Sha256),
"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865"
);
}
#[test]
fn hkdf_sha512_matches_reference() {
// RFC 5869 case 1 inputs with SHA-512 (computed with Python's hmac).
assert_eq!(
rfc5869_case1(KdfAlgorithm::Sha512),
"832390086cda71fb47625bb5ceb168e4c8e26a1a16ed34d9fc7fe92c1481579338da362cb8d9f925d7cb"
);
}
}