From f8c64390e3a411f7799f3bca685106c7df394e1a Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 03:19:45 -0500 Subject: [PATCH] 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 --- crates/ccc-crypto-wolfssl/src/kdf.rs | 69 ++++++++++++++++------------ 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/crates/ccc-crypto-wolfssl/src/kdf.rs b/crates/ccc-crypto-wolfssl/src/kdf.rs index d12f29c..762168f 100644 --- a/crates/ccc-crypto-wolfssl/src/kdf.rs +++ b/crates/ccc-crypto-wolfssl/src/kdf.rs @@ -8,7 +8,8 @@ //! 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: //! -//! - **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 //! `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). /// -/// Extract uses `wc_Tls13_HKDF_Extract` (wolfSSL's RFC 5869 Extract). -/// Expand is implemented in Rust using wolfSSL's HMAC primitives. +/// Both steps are implemented in Rust using wolfSSL's HMAC primitives. fn hkdf( hash_type: i32, ikm: &[u8], @@ -85,40 +85,19 @@ fn hkdf( 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( hash_type: i32, salt: &[u8], ikm: &[u8], ) -> Result>, CryptoError> { let hash_len = hash_output_len(hash_type); - let mut prk = Zeroizing::new(vec![0u8; hash_len]); - // wolfSSL's wc_Tls13_HKDF_Extract takes `byte* ikm` (mutable), so we - // 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) + let prk = crate::mac::hmac(hash_type, salt, ikm, hash_len)?; + Ok(Zeroizing::new(prk)) } /// HKDF-Expand: `OKM = T(1) || T(2) || …` truncated to `length` bytes. @@ -251,3 +230,33 @@ fn argon2id( Ok(out) } + +#[cfg(test)] +mod hkdf_tests { + use super::*; + + fn rfc5869_case1(algo: KdfAlgorithm) -> String { + let ikm = [0x0bu8; 22]; + let salt: Vec = (0u8..13).collect(); + let info: Vec = (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" + ); + } +}