//! CCC v2 known-answer vectors (vectors/cascade_v2.json) and the //! cross-provider check at cascade level: the same suite with a layer moved to //! the other provider gives byte-identical ciphertext. //! //! Regenerate (only when the format deliberately changes, with a version bump): //! cargo test -p ccc-cascade --test cascade_vectors -- --ignored regenerate use std::sync::Arc; use ccc_cascade::{open, seal, suites, Context, Layer, Policy, ProviderSet, StreamOpener, StreamSealer, Suite}; use ccc_crypto_core::AeadAlgorithm::{self, *}; use serde_json::{json, Value}; const PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../vectors/cascade_v2.json"); fn providers() -> ProviderSet { let mut p = ProviderSet::new(); let wolf = Arc::new(ccc_crypto_wolfssl::WolfSslProvider::new()); p.add(wolf.clone()); p.bind(ccc_cascade::suites::VETTED, wolf); p.add(Arc::new(ccc_crypto_rustcrypto::RustCryptoProvider::new())); p } fn hex(b: &[u8]) -> String { b.iter().map(|x| format!("{x:02x}")).collect() } fn unhex(s: &str) -> Vec { (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect() } fn pattern(n: usize, seed: u8) -> Vec { (0..n).map(|i| (i as u8).wrapping_mul(31).wrapping_add(seed)).collect() } fn cases() -> Vec<(Suite, u8, usize)> { vec![ (suites::floor_aes(), 1, 0), (suites::floor_aes(), 1, 37), (suites::standard_2(), 1, 300), (suites::high_3(), 3, 1024), (suites::ultra_3(), 16, 4096), (suites::storage_2(), 1, 255), ] } fn generate() -> Value { let p = providers(); let mut out = vec![]; for (suite, rounds, len) in cases() { let r = p.resolve(&suite, &Policy::development()).unwrap(); let (key, salt, pt, aad) = (pattern(32, 1), pattern(32, 2), pattern(len, 3), pattern(19, 4)); let ct = seal(&r, rounds, &key, &salt, Context { domain: b"ccc/v2/kat", aad: &aad }, &pt).unwrap(); out.push(json!({"suite": suite.id, "rounds": rounds, "key": hex(&key), "salt": hex(&salt), "aad": hex(&aad), "pt": hex(&pt), "ct": hex(&ct)})); } let r = p.resolve(&suites::standard_2(), &Policy::development()).unwrap(); let blob = pattern(64 * 1024 + 100, 5); let mut s = StreamSealer::new(&r, &pattern(32, 6), &pattern(32, 7), Context { domain: b"ccc/v2/kat", aad: b"b" }).unwrap(); let segs: Vec = blob.chunks(64 * 1024).enumerate().map(|(i, c)| hex(&s.seal_segment(c, i == 1).unwrap())).collect(); json!({"version": 2, "domain": "ccc/v2/kat", "cascade": out, "stream": {"suite": 2, "key": hex(&pattern(32, 6)), "salt": hex(&pattern(32, 7)), "aad": "62", "blob_len": blob.len(), "segments": segs}}) } #[test] #[ignore] fn regenerate() { std::fs::write(PATH, serde_json::to_string_pretty(&generate()).unwrap() + "\n").unwrap(); } #[test] fn pinned_vectors_still_hold() { let pinned: Value = serde_json::from_str(&std::fs::read_to_string(PATH).unwrap()).unwrap(); assert_eq!(pinned, generate(), "CCC v2 output changed; a format change needs a version bump"); // And the pinned ciphertexts open. let p = providers(); for v in pinned["cascade"].as_array().unwrap() { let suite = suites::by_id(v["suite"].as_u64().unwrap() as u16).unwrap(); let r = p.resolve(&suite, &Policy::development()).unwrap(); let aad = unhex(v["aad"].as_str().unwrap()); let pt = open( &r, v["rounds"].as_u64().unwrap() as u8, &unhex(v["key"].as_str().unwrap()), &unhex(v["salt"].as_str().unwrap()), Context { domain: b"ccc/v2/kat", aad: &aad }, &unhex(v["ct"].as_str().unwrap()), ) .unwrap(); assert_eq!(hex(&pt), v["pt"].as_str().unwrap()); } let st = &pinned["stream"]; let r = p.resolve(&suites::standard_2(), &Policy::development()).unwrap(); let mut o = StreamOpener::new( &r, &unhex(st["key"].as_str().unwrap()), &unhex(st["salt"].as_str().unwrap()), Context { domain: b"ccc/v2/kat", aad: b"b" }, ) .unwrap(); let segs = st["segments"].as_array().unwrap(); let mut n = 0; for (i, seg) in segs.iter().enumerate() { n += o.open_segment(&unhex(seg.as_str().unwrap()), i == segs.len() - 1).unwrap().len(); } o.finish().unwrap(); assert_eq!(n as u64, st["blob_len"].as_u64().unwrap()); } #[test] fn moving_a_layer_to_the_other_provider_gives_identical_bytes() { let p = providers(); let both: [AeadAlgorithm; 3] = [AesGcm256, ChaCha20Poly1305, XChaCha20Poly1305]; for alg in both { let mk = |prov: &'static str| Suite { id: 77, name: "swap", // The anchor (and so the KDF) stays wolfSSL AES-GCM; only the // tested layer moves. layers: vec![Layer { aead: AesGcm256, provider: "wolfssl" }, Layer { aead: alg, provider: prov }], }; let a = p.resolve(&mk("wolfssl"), &Policy::development()).unwrap(); let b = p.resolve(&mk("rustcrypto"), &Policy::development()).unwrap(); let ctx = Context { domain: b"swap", aad: b"aad" }; let ca = seal(&a, 3, &[9; 32], &[8; 32], ctx, b"same bytes either way").unwrap(); let cb = seal(&b, 3, &[9; 32], &[8; 32], ctx, b"same bytes either way").unwrap(); assert_eq!(ca, cb, "{alg:?}"); assert_eq!(open(&b, 3, &[9; 32], &[8; 32], ctx, &ca).unwrap().as_slice(), b"same bytes either way"); } }