From 3aec1a84603b54e1f8d9fd048c99e72cea8a236e Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 04:09:07 -0500 Subject: [PATCH] wolfssl: add XChaCha, HKDF and HMAC self-test vectors The cascade and HPKE key schedules use HKDF and HMAC, which had no known-answer test, so a broken KDF passed the self-test gate. Refs: R3-6 --- crates/ccc-crypto-wolfssl/src/self_test.rs | 90 +++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/crates/ccc-crypto-wolfssl/src/self_test.rs b/crates/ccc-crypto-wolfssl/src/self_test.rs index eb0b8fe..5dcc18b 100644 --- a/crates/ccc-crypto-wolfssl/src/self_test.rs +++ b/crates/ccc-crypto-wolfssl/src/self_test.rs @@ -4,11 +4,11 @@ //! gate the provider from being marked `available` in the capability catalog. use ccc_crypto_core::{ - algorithms::AeadAlgorithm, + algorithms::{AeadAlgorithm, KdfAlgorithm, MacAlgorithm}, types::{AlgoTestResult, SelfTestReport}, }; -use crate::aead; +use crate::{aead, kdf, mac}; // ────────────────────────────────────────────────────────────────────────────── // AEAD vectors @@ -55,8 +55,81 @@ static AEAD_VECTORS: &[AeadVector] = &[ ct_tag: "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d6\ 3dbea45e8ca96712f180d4e9016c65a7dde15e3106075ebd", }, + // ── XChaCha20-Poly1305: pinned cross-provider conformance vector ──── + // (vectors/conformance_v1.json; wolfSSL and RustCrypto agree on it). + AeadVector { + algo: AeadAlgorithm::XChaCha20Poly1305, + key: "6e15bc630ab158ffa64df49b42e99037de852cd37a21c86f16bd640bb25900a7", + nonce: "7a21c86f16bd640bb25900a74ef59c43ea9138df862dd47b", + aad: "00a74ef59c43ea9138df862dd47b22c97017be65", + pt: "862dd47b22c97017be650cb35a01a84ff69d44eb9239e0872ed57c23ca7118bf660db45b02", + ct_tag: "cdbbd52a0f0f78264d4fea4f8b614b3fc36c2d269e70b502ff8491992e36fad2\ + 9c2a7df2af333c37b4584f08340e7623753b54b6c4", + }, ]; +// ────────────────────────────────────────────────────────────────────────────── +// KDF and MAC vectors (the cascade key schedule and the HPKE schedule depend +// on these; the policy refuses a suite whose KDF has no passing vector) +// ────────────────────────────────────────────────────────────────────────────── + +/// RFC 5869 test case 1 inputs; SHA-512 output computed with Python's hmac. +static KDF_VECTORS: &[(KdfAlgorithm, &str)] = &[ + ( + KdfAlgorithm::Sha256, + "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865", + ), + ( + KdfAlgorithm::Sha512, + "832390086cda71fb47625bb5ceb168e4c8e26a1a16ed34d9fc7fe92c1481579338da362cb8d9f925d7cb", + ), +]; + +/// RFC 4231 test case 2 (key "Jefe"). +static MAC_VECTORS: &[(MacAlgorithm, &str)] = &[ + (MacAlgorithm::HmacSha256, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"), + ( + MacAlgorithm::HmacSha512, + "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737", + ), +]; + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +fn result(algo_id: u32, name: String, outcome: Result, expected: &str) -> AlgoTestResult { + match outcome { + Ok(got) if got == expected => AlgoTestResult { algo_id, algo_name: name, passed: true, error_message: None }, + Ok(_) => AlgoTestResult { + algo_id, + algo_name: name, + passed: false, + error_message: Some("output mismatch".into()), + }, + Err(e) => AlgoTestResult { algo_id, algo_name: name, passed: false, error_message: Some(e) }, + } +} + +fn run_kdf_vectors(results: &mut Vec) { + let ikm = [0x0bu8; 22]; + let salt: Vec = (0u8..13).collect(); + let info: Vec = (0xf0u8..0xfa).collect(); + for (algo, expected) in KDF_VECTORS { + let out = kdf::derive_key(*algo, &ikm, &salt, &info, 42).map(|k| hex(&k)).map_err(|e| e.to_string()); + results.push(result(*algo as u32, format!("HKDF {algo:?} RFC 5869 case 1"), out, expected)); + } +} + +fn run_mac_vectors(results: &mut Vec) { + for (algo, expected) in MAC_VECTORS { + let out = mac::compute_mac(*algo, b"Jefe", b"what do ya want for nothing?") + .map(|t| hex(&t)) + .map_err(|e| e.to_string()); + results.push(result(*algo as u32, format!("{} RFC 4231 case 2", algo.name()), out, expected)); + } +} + // ────────────────────────────────────────────────────────────────────────────── // Runner // ────────────────────────────────────────────────────────────────────────────── @@ -67,6 +140,8 @@ pub(crate) fn run() -> SelfTestReport { for v in AEAD_VECTORS { results.push(run_aead_vector(v)); } + run_kdf_vectors(&mut results); + run_mac_vectors(&mut results); SelfTestReport::finalise("wolfssl", results) } @@ -111,3 +186,14 @@ fn run_aead_vector(v: &AeadVector) -> AlgoTestResult { }, } } + +#[cfg(test)] +mod tests { + #[test] + fn every_embedded_vector_passes() { + let r = super::run(); + let failed: Vec<_> = r.results.iter().filter(|t| !t.passed).map(|t| t.algo_name.clone()).collect(); + assert!(failed.is_empty(), "failed: {failed:?}"); + assert_eq!(r.results.len(), 7); + } +}