diff --git a/crates/ccc-crypto-awslc/kat/mlkem1024_seed00_ct.bin b/crates/ccc-crypto-awslc/kat/mlkem1024_seed00_ct.bin new file mode 100644 index 0000000..da8374d Binary files /dev/null and b/crates/ccc-crypto-awslc/kat/mlkem1024_seed00_ct.bin differ diff --git a/crates/ccc-crypto-awslc/kat/mlkem1024_seed00_dk.bin b/crates/ccc-crypto-awslc/kat/mlkem1024_seed00_dk.bin new file mode 100644 index 0000000..f77d4d7 Binary files /dev/null and b/crates/ccc-crypto-awslc/kat/mlkem1024_seed00_dk.bin differ diff --git a/crates/ccc-crypto-awslc/kat/mlkem768_seed00_ct.bin b/crates/ccc-crypto-awslc/kat/mlkem768_seed00_ct.bin new file mode 100644 index 0000000..1586815 Binary files /dev/null and b/crates/ccc-crypto-awslc/kat/mlkem768_seed00_ct.bin differ diff --git a/crates/ccc-crypto-awslc/kat/mlkem768_seed00_dk.bin b/crates/ccc-crypto-awslc/kat/mlkem768_seed00_dk.bin new file mode 100644 index 0000000..4a7517a Binary files /dev/null and b/crates/ccc-crypto-awslc/kat/mlkem768_seed00_dk.bin differ diff --git a/crates/ccc-crypto-awslc/src/self_test.rs b/crates/ccc-crypto-awslc/src/self_test.rs index 5b9f0b5..1392436 100644 --- a/crates/ccc-crypto-awslc/src/self_test.rs +++ b/crates/ccc-crypto-awslc/src/self_test.rs @@ -10,13 +10,22 @@ //! - HMAC: RFC 4231 test case 2. //! - HKDF: RFC 5869 test cases 1 and 3 (SHA-256), and the case 1 inputs for //! SHA-384/512, checked against OpenSSL 3.6 and the other providers. +//! - X25519: RFC 7748 §6.1. +//! - ML-KEM-768/1024: the seed-00..3f key of the IETF LAMPS examples. The +//! expanded decapsulation keys and the ciphertexts (encapsulation +//! randomness `40..6b`, OpenSSL `hexikme`) were produced by OpenSSL 3.6.1 +//! and are stored under `kat/`; their public-key and ciphertext SHA3-256 +//! values and the shared secrets are the ones the RustCrypto provider pins. +//! aws-lc-rs has no deterministic encapsulation, so the KAT checks +//! decapsulation (and implicit rejection), then a fresh encapsulation to +//! the known public key. use ccc_crypto_core::{ - algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, MacAlgorithm}, + algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm}, types::{AlgoTestResult, SelfTestReport}, }; -use crate::{aead, hash, kdf, mac, PROVIDER_NAME}; +use crate::{aead, hash, kdf, kem, mac, PROVIDER_NAME}; type Check = fn() -> Result<(), String>; @@ -87,6 +96,17 @@ const KATS: &[(u32, &str, Check)] = &[ "HKDF-SHA-512 TC1 inputs", || hkdf(KdfAlgorithm::Sha512, HKDF512_TC1), ), + (KemAlgorithm::X25519 as u32, "X25519 RFC 7748 6.1", x25519), + ( + KemAlgorithm::MlKem768 as u32, + "ML-KEM-768 seed KAT", + mlkem768, + ), + ( + KemAlgorithm::MlKem1024 as u32, + "ML-KEM-1024 seed KAT", + mlkem1024, + ), ]; /// Run every KAT. Never panics; failures are reported per algorithm. @@ -122,6 +142,10 @@ fn expect(what: &str, got: &[u8], want_hex: &str) -> Result<(), String> { } } +fn sha3(data: &[u8]) -> Vec { + hash::hash(HashAlgorithm::Sha3_256, data).unwrap_or_default() +} + fn e(err: E) -> String { err.to_string() } @@ -280,6 +304,90 @@ fn hkdf_empty_salt() -> Result<(), String> { ) } +// ── KEM ────────────────────────────────────────────────────────────────────── + +fn x25519() -> Result<(), String> { + let alice = hx("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"); + let alice_pub = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a"; + let bob = hx("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb"); + let bob_pub = hx("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f"); + let shared = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"; + + // Encapsulating with Alice's key as the ephemeral gives her public key as + // the ciphertext; Bob decapsulates it. + let enc = kem::x25519_encapsulate_deterministic(&alice, &bob_pub).map_err(e)?; + expect("ciphertext", &enc.ciphertext, alice_pub)?; + expect("encap secret", &enc.shared_secret, shared)?; + let ss = kem::decapsulate(KemAlgorithm::X25519, &bob, &enc.ciphertext).map_err(e)?; + expect("decap secret", &ss, shared) +} + +struct MlKemKat { + algo: KemAlgorithm, + params: kem::MlKemParams, + name: &'static str, + dk: &'static [u8], + ct: &'static [u8], + dk_sha3: &'static str, + pk_sha3: &'static str, + ct_sha3: &'static str, + ss: &'static str, +} + +fn mlkem_kat(k: &MlKemKat) -> Result<(), String> { + let p = k.params; + // The stored inputs are the pinned ones. + expect("decapsulation key", &sha3(k.dk), k.dk_sha3)?; + expect("ciphertext", &sha3(k.ct), k.ct_sha3)?; + // FIPS 203 dk = dk_pke || ek || H(ek) || z, with dk_pke as long as ek - 32. + let ek_start = p.ek_len - 32; + let pk = &k.dk[ek_start..ek_start + p.ek_len]; + expect("public key", &sha3(pk), k.pk_sha3)?; + + let ss = kem::decapsulate(k.algo, k.dk, k.ct).map_err(e)?; + expect("decap secret", &ss, k.ss)?; + let mut bad = k.ct.to_vec(); + bad[0] ^= 1; + let rejected = kem::decapsulate(k.algo, k.dk, &bad).map_err(e)?; + if rejected.as_slice() == hx(k.ss).as_slice() { + return Err("implicit rejection returned the real secret".into()); + } + let enc = kem::mlkem_encapsulate(p, k.name, pk).map_err(e)?; + let ss = kem::decapsulate(k.algo, k.dk, &enc.ciphertext).map_err(e)?; + if *ss != enc.shared_secret { + return Err("encapsulation to the known key does not decapsulate".into()); + } + Ok(()) +} + +fn mlkem768() -> Result<(), String> { + mlkem_kat(&MlKemKat { + algo: KemAlgorithm::MlKem768, + params: kem::MLKEM768, + name: "ML-KEM-768", + dk: include_bytes!("../kat/mlkem768_seed00_dk.bin"), + ct: include_bytes!("../kat/mlkem768_seed00_ct.bin"), + dk_sha3: "1149f17c3c4ac6ab1e3e2d9d8bd0171355ac0fa31bb8855c48ceade874c0864b", + pk_sha3: "a24e16d8f8f9383a95b77050f4d9fd2f5733eec1d63ef3c23ebf9918173669a7", + ct_sha3: "c5190623f119891d14904e62cd28a79677df85afe5ad9151cb962caa00002de4", + ss: "71e6c52c0066736b5d2e9cfe2d9694804a4c6dd57c9ed1d615df96c018782de3", + }) +} + +fn mlkem1024() -> Result<(), String> { + mlkem_kat(&MlKemKat { + algo: KemAlgorithm::MlKem1024, + params: kem::MLKEM1024, + name: "ML-KEM-1024", + dk: include_bytes!("../kat/mlkem1024_seed00_dk.bin"), + ct: include_bytes!("../kat/mlkem1024_seed00_ct.bin"), + dk_sha3: "f0db5d938027fcd9bad87847d52c14cf0c4abcf0703b749793f212111ffb303b", + pk_sha3: "61349e5c131a7e116a0463861d7d18663c5627c38c7147ddaadfd48acd7a4535", + ct_sha3: "6e264ea88c427017fceeebf87f12c106bb2e511843d5ed525fdd4d45a0205daa", + ss: "ccff2f30e61151de4c3257ca1d3a8a9de661b81c5ba34efe9b1a4af770caf9df", + }) +} + #[cfg(test)] mod tests { use super::*; @@ -303,7 +411,9 @@ mod tests { #[test] fn every_offered_algorithm_has_a_kat() { let ids: Vec = KATS.iter().map(|k| k.0).collect(); - for id in [12u32, 13, 16, 1, 2, 3, 30, 31, 32, 40, 41, 42, 44, 45] { + for id in [ + 12u32, 13, 16, 1, 2, 3, 30, 31, 32, 40, 41, 42, 44, 45, 50, 52, 53, + ] { assert!(ids.contains(&id), "no KAT for {id}"); } }