//! Embedded NIST / RFC test vectors for the power-on self-test. //! //! These are checked at runtime by [`WolfSslProvider::self_test()`]; failures //! gate the provider from being marked `available` in the capability catalog. use ccc_crypto_core::{ algorithms::{AeadAlgorithm, KdfAlgorithm, MacAlgorithm}, types::{AlgoTestResult, SelfTestReport}, }; use crate::{aead, kdf, mac}; // ────────────────────────────────────────────────────────────────────────────── // AEAD vectors // ────────────────────────────────────────────────────────────────────────────── struct AeadVector { algo: AeadAlgorithm, key: &'static str, nonce: &'static str, aad: &'static str, pt: &'static str, ct_tag: &'static str, // ciphertext || tag, hex-encoded } /// NIST SP 800-38D and RFC 8439 test vectors. static AEAD_VECTORS: &[AeadVector] = &[ // ── AES-256-GCM: NIST SP 800-38D Test Case 16 ─────────────────────── // Key: feffe9...308308 × 2 (AES-256) // Nonce: cafebabefacedbaddecaf888 // AAD: feedfacedeadbeeffeedfacedeadbeefabaddad2 (20 bytes) // PT: d9313225...637b39 (60 bytes) // ct_tag = ciphertext (60 bytes) || GHASH tag (16 bytes) = 76 bytes AeadVector { algo: AeadAlgorithm::AesGcm256, key: "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308", nonce: "cafebabefacedbaddecaf888", aad: "feedfacedeadbeeffeedfacedeadbeefabaddad2", pt: "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72\ 1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39", ct_tag: "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa\ 8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662\ 76fc6ece0f4e1768cddf8853bb2d551b", }, // ── ChaCha20-Poly1305: RFC 8439 §2.8.2 ────────────────────────────── // PT: "Ladies and Gentlemen of the class of '99" (40 bytes) // ct_tag = ciphertext (40 bytes) || Poly1305 tag (16 bytes) = 56 bytes AeadVector { algo: AeadAlgorithm::ChaCha20Poly1305, key: "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f", nonce: "070000004041424344454647", aad: "50515253c0c1c2c3c4c5c6c7", pt: "4c616469657320616e642047656e746c656d656e206f662074686520636c617373\ 206f6620273939", 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 // ────────────────────────────────────────────────────────────────────────────── /// Run all embedded self-test vectors and return a [`SelfTestReport`]. pub(crate) fn run() -> SelfTestReport { let mut results: Vec = Vec::new(); 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) } fn run_aead_vector(v: &AeadVector) -> AlgoTestResult { let test_name = format!("{} NIST/RFC vector", v.algo.name()); let decode = |hex: &str| -> Vec { (0..hex.len()) .step_by(2) .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap_or(0)) .collect() }; let key = decode(v.key); let nonce = decode(v.nonce); let aad = decode(v.aad); let pt = decode(v.pt); let expected = decode(v.ct_tag); match aead::encrypt(v.algo, &key, &nonce, &pt, &aad) { Ok(ct_tag) if ct_tag == expected => AlgoTestResult { algo_id: v.algo as u32, algo_name: test_name, passed: true, error_message: None, }, Ok(ct_tag) => AlgoTestResult { algo_id: v.algo as u32, algo_name: test_name.clone(), passed: false, error_message: Some(format!( "output mismatch: got {} bytes, expected {} bytes", ct_tag.len(), expected.len() )), }, Err(e) => AlgoTestResult { algo_id: v.algo as u32, algo_name: test_name, passed: false, error_message: Some(e.to_string()), }, } } #[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); } }