From 6a321e3efcb2504fd665a47eabd28f34fbba4dca Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 03:26:03 -0500 Subject: [PATCH] rustcrypto: add AEAD, hash and MAC self-test vectors Startup self-tests must fail closed (D-28); each symmetric algorithm gets an RFC or NIST known-answer vector, including tag rejection. Refs: R3-4 --- crates/ccc-crypto-rustcrypto/src/lib.rs | 1 + crates/ccc-crypto-rustcrypto/src/self_test.rs | 322 ++++++++++++++++++ 2 files changed, 323 insertions(+) create mode 100644 crates/ccc-crypto-rustcrypto/src/self_test.rs diff --git a/crates/ccc-crypto-rustcrypto/src/lib.rs b/crates/ccc-crypto-rustcrypto/src/lib.rs index 05ed5a4..c4f999c 100644 --- a/crates/ccc-crypto-rustcrypto/src/lib.rs +++ b/crates/ccc-crypto-rustcrypto/src/lib.rs @@ -12,3 +12,4 @@ pub mod hash; pub mod kdf; pub mod kem; pub mod mac; +pub mod self_test; diff --git a/crates/ccc-crypto-rustcrypto/src/self_test.rs b/crates/ccc-crypto-rustcrypto/src/self_test.rs new file mode 100644 index 0000000..03c28cf --- /dev/null +++ b/crates/ccc-crypto-rustcrypto/src/self_test.rs @@ -0,0 +1,322 @@ +//! Embedded known-answer self-test (KAT) for every algorithm the provider +//! offers. +//! +//! Sources: +//! +//! - AES-256-GCM: NIST SP 800-38D test case 16. +//! - AES-256-GCM-SIV: RFC 8452 appendix C.2. +//! - ChaCha20-Poly1305: RFC 8439 §2.8.2; Poly1305: RFC 8439 §2.5.2. +//! - XChaCha20-Poly1305: draft-irtf-cfrg-xchacha-03 §A.3.1. +//! - Ascon-AEAD128: NIST SP 800-232 reference KAT (`LWC_AEAD_KAT_128_128`, count 1059). +//! - SHA-2 / SHA-3: FIPS 180-4 / FIPS 202 "abc"; BLAKE2b: RFC 7693 appendix A +//! and the reference keyed KAT. +//! - HMAC: RFC 4231 test case 2. + +use ccc_crypto_core::{ + algorithms::{AeadAlgorithm, HashAlgorithm, MacAlgorithm}, + types::{AlgoTestResult, SelfTestReport}, +}; + +use crate::{aead, hash, mac}; + +type Check = fn() -> Result<(), String>; + +/// Every KAT: (algorithm ID, test name, check). +const KATS: &[(u32, &str, Check)] = &[ + ( + AeadAlgorithm::AesGcm256 as u32, + "AES-256-GCM SP 800-38D TC16", + aes_gcm, + ), + ( + AeadAlgorithm::AesGcmSiv256 as u32, + "AES-256-GCM-SIV RFC 8452 C.2", + aes_gcm_siv, + ), + ( + AeadAlgorithm::ChaCha20Poly1305 as u32, + "ChaCha20-Poly1305 RFC 8439 2.8.2", + chacha, + ), + ( + AeadAlgorithm::XChaCha20Poly1305 as u32, + "XChaCha20-Poly1305 draft A.3.1", + xchacha, + ), + ( + AeadAlgorithm::AsconAead128 as u32, + "Ascon-AEAD128 SP 800-232 KAT", + ascon, + ), + (HashAlgorithm::Sha256 as u32, "SHA-256 abc", || { + digest(HashAlgorithm::Sha256, SHA256_ABC) + }), + (HashAlgorithm::Sha384 as u32, "SHA-384 abc", || { + digest(HashAlgorithm::Sha384, SHA384_ABC) + }), + (HashAlgorithm::Sha512 as u32, "SHA-512 abc", || { + digest(HashAlgorithm::Sha512, SHA512_ABC) + }), + (HashAlgorithm::Sha3_256 as u32, "SHA3-256 abc", || { + digest(HashAlgorithm::Sha3_256, SHA3_256_ABC) + }), + (HashAlgorithm::Sha3_512 as u32, "SHA3-512 abc", || { + digest(HashAlgorithm::Sha3_512, SHA3_512_ABC) + }), + (HashAlgorithm::Blake2b512 as u32, "BLAKE2b-512 abc", || { + digest(HashAlgorithm::Blake2b512, BLAKE2B_ABC) + }), + ( + MacAlgorithm::HmacSha256 as u32, + "HMAC-SHA-256 RFC 4231 TC2", + || hmac(MacAlgorithm::HmacSha256, HMAC256_TC2), + ), + ( + MacAlgorithm::HmacSha384 as u32, + "HMAC-SHA-384 RFC 4231 TC2", + || hmac(MacAlgorithm::HmacSha384, HMAC384_TC2), + ), + ( + MacAlgorithm::HmacSha512 as u32, + "HMAC-SHA-512 RFC 4231 TC2", + || hmac(MacAlgorithm::HmacSha512, HMAC512_TC2), + ), + ( + MacAlgorithm::Blake2bMac as u32, + "BLAKE2b-MAC keyed KAT", + blake2b_mac, + ), + ( + MacAlgorithm::Poly1305 as u32, + "Poly1305 RFC 8439 2.5.2", + poly1305, + ), +]; + +/// Run every KAT. Never panics; failures are reported per algorithm. +pub fn run() -> SelfTestReport { + let results = KATS + .iter() + .map(|(id, name, check)| { + let outcome = check(); + AlgoTestResult { + algo_id: *id, + algo_name: (*name).into(), + passed: outcome.is_ok(), + error_message: outcome.err(), + } + }) + .collect(); + SelfTestReport::finalise("rustcrypto", results) +} + +// ── Helpers ────────────────────────────────────────────────────────────────── + +fn hx(s: &str) -> Vec { + (0..s.len() / 2) + .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap_or(0)) + .collect() +} + +fn expect(what: &str, got: &[u8], want_hex: &str) -> Result<(), String> { + if got == hx(want_hex).as_slice() { + Ok(()) + } else { + Err(format!("{what}: output mismatch")) + } +} + +fn e(err: E) -> String { + err.to_string() +} + +// ── AEAD ───────────────────────────────────────────────────────────────────── + +fn aead_kat( + algo: AeadAlgorithm, + key: &str, + nonce: &str, + aad: &str, + pt: &str, + ct: &str, +) -> Result<(), String> { + let (key, nonce, aad, pt) = (hx(key), hx(nonce), hx(aad), hx(pt)); + let sealed = aead::encrypt(algo, &key, &nonce, &pt, &aad).map_err(e)?; + expect("seal", &sealed, ct)?; + let opened = aead::decrypt(algo, &key, &nonce, &sealed, &aad).map_err(e)?; + if opened != pt { + return Err("open: plaintext mismatch".into()); + } + let mut bad = sealed; + let last = bad.len() - 1; + bad[last] ^= 0x80; + if aead::decrypt(algo, &key, &nonce, &bad, &aad).is_ok() { + return Err("open accepted a tampered tag".into()); + } + Ok(()) +} + +const RFC8439_KEY: &str = "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"; +const RFC8439_AAD: &str = "50515253c0c1c2c3c4c5c6c7"; +const SUNSCREEN: &str = "4c616469657320616e642047656e746c656d656e206f662074686520636c6173\ + 73206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e6520746970\ + 20666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"; + +fn aes_gcm() -> Result<(), String> { + aead_kat( + AeadAlgorithm::AesGcm256, + "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308", + "cafebabefacedbaddecaf888", + "feedfacedeadbeeffeedfacedeadbeefabaddad2", + "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72\ + 1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39", + "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa\ + 8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662\ + 76fc6ece0f4e1768cddf8853bb2d551b", + ) +} + +fn aes_gcm_siv() -> Result<(), String> { + let key = "0100000000000000000000000000000000000000000000000000000000000000"; + let nonce = "030000000000000000000000"; + aead_kat( + AeadAlgorithm::AesGcmSiv256, + key, + nonce, + "", + "", + "07f5f4169bbf55a8400cd47ea6fd400f", + )?; + aead_kat( + AeadAlgorithm::AesGcmSiv256, + key, + nonce, + "", + "0100000000000000", + "c2ef328e5c71c83b843122130f7364b761e0b97427e3df28", + ) +} + +fn chacha() -> Result<(), String> { + aead_kat( + AeadAlgorithm::ChaCha20Poly1305, + RFC8439_KEY, + "070000004041424344454647", + RFC8439_AAD, + SUNSCREEN, + "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca967128\ + 2fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fa\ + b324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116\ + 1ae10b594f09e26a7e902ecbd0600691", + ) +} + +fn xchacha() -> Result<(), String> { + aead_kat( + AeadAlgorithm::XChaCha20Poly1305, + RFC8439_KEY, + "404142434445464748494a4b4c4d4e4f5051525354555657", + RFC8439_AAD, + SUNSCREEN, + "bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb731c7f1b0b4aa6440\ + bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b4522f8c9ba40db5d945b11b69b982c1bb9e3f\ + 3fac2bc369488f76b2383565d3fff921f9664c97637da9768812f615c68b13b52e\ + c0875924c1c7987947deafd8780acf49", + ) +} + +fn ascon() -> Result<(), String> { + aead_kat( + AeadAlgorithm::AsconAead128, + "000102030405060708090a0b0c0d0e0f", + "000102030405060708090a0b0c0d0e0f", + "0001", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "49e50547ca0c754784f2a6f936ed9497c3a556d34de4cc23f3c21cdb6d447cdb\ + 0f7324dec1d9f0b3c2125ea5aad85a12", + ) +} + +// ── Hash ───────────────────────────────────────────────────────────────────── + +const SHA256_ABC: &str = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; +const SHA384_ABC: &str = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed\ + 8086072ba1e7cc2358baeca134c825a7"; +const SHA512_ABC: &str = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a\ + 2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; +const SHA3_256_ABC: &str = "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"; +const SHA3_512_ABC: &str = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e\ + 10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"; +const BLAKE2B_ABC: &str = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1\ + 7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"; + +fn digest(algo: HashAlgorithm, want: &str) -> Result<(), String> { + expect("digest", &hash::hash(algo, b"abc").map_err(e)?, want) +} + +// ── MAC ────────────────────────────────────────────────────────────────────── + +const HMAC256_TC2: &str = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"; +const HMAC384_TC2: &str = "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e\ + 8e2240ca5e69e2c78b3239ecfab21649"; +const HMAC512_TC2: &str = "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554\ + 9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"; + +fn mac_kat(algo: MacAlgorithm, key: &[u8], data: &[u8], want: &str) -> Result<(), String> { + let tag = mac::compute_mac(algo, key, data).map_err(e)?; + expect("tag", &tag, want)?; + if !mac::verify_mac(algo, key, data, &tag).map_err(e)? { + return Err("verify rejected a valid tag".into()); + } + let mut bad = tag; + bad[0] ^= 1; + if mac::verify_mac(algo, key, data, &bad).map_err(e)? { + return Err("verify accepted a tampered tag".into()); + } + Ok(()) +} + +fn hmac(algo: MacAlgorithm, want: &str) -> Result<(), String> { + mac_kat(algo, b"Jefe", b"what do ya want for nothing?", want) +} + +fn blake2b_mac() -> Result<(), String> { + let key: Vec = (0u8..64).collect(); + mac_kat( + MacAlgorithm::Blake2bMac, + &key, + b"", + "10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786\ + b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568", + ) +} + +fn poly1305() -> Result<(), String> { + mac_kat( + MacAlgorithm::Poly1305, + &hx("85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b"), + b"Cryptographic Forum Research Group", + "a8061dc1305136c6c22b8baf0c0127a9", + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn all_kats_pass() { + let report = run(); + for r in &report.results { + assert!(r.passed, "{}: {:?}", r.algo_name, r.error_message); + } + assert!(report.all_passed); + assert_eq!(report.provider_name, "rustcrypto"); + } + + #[test] + fn kat_detects_a_wrong_answer() { + assert!(expect("x", b"abc", "616264").is_err()); + assert!(expect("x", b"abc", "616263").is_ok()); + } +}