//! The box's vectors and refusals. No key here is real: each is a counted //! byte pattern. The vectors are also `vectors.json`, which the Worker's //! tests open with WebCrypto (apps/business/test/chat-vectors.test.ts): two //! implementations agreeing on the same bytes. use super::*; use serde_json::{json, Value}; fn scalar(seed: u8) -> [u8; 32] { let mut bytes = [seed; 32]; bytes[0] = seed.wrapping_mul(7).wrapping_add(1); bytes[31] = seed.wrapping_mul(13).wrapping_add(3); bytes } const VECTORS: &str = include_str!("vectors.json"); fn vectors() -> Vec<(u8, u8, u8, u8, String, String)> { // (sender seed, recipient seed, ephemeral seed, nonce byte, payload, box) let all: Value = serde_json::from_str(VECTORS).expect("vectors.json"); all["vectors"] .as_array() .expect("a list") .iter() .map(|v| { let seed = |k: &str| v[k].as_u64().expect(k) as u8; (seed("sender_seed"), seed("recipient_seed"), seed("ephemeral_seed"), seed("nonce_byte"), v["payload"].as_str().unwrap().to_string(), v["box"].as_str().unwrap().to_string()) }) .collect() } /// RULE: The same inputs seal to the same box, byte for byte, the box the vectors file records. #[test] fn the_vectors_seal_to_the_recorded_boxes() { if std::env::var_os("COTTON_WRITE_CHAT_VECTORS").is_some() { let cases = [(1u8, 2u8, 3u8, 9u8, r#"{"v":1,"id":"m-0001","kind":"message","from":"+15550100001","to":"+15550100002","at":1700000000000,"body":"hello"}"#), (4, 5, 6, 7, r#"{"v":1,"id":"r-0001","kind":"receipt","from":"+15550100002","to":"+15550100001","at":1700000000500,"state":"read","ids":["m-0001"]}"#)]; let list: Vec = cases .iter() .map(|(s, r, e, n, p)| { let sealed = seal_with(&scalar(*e), &[*n; 12], &scalar(*s), &public_of(&scalar(*r)), p.as_bytes()).unwrap(); json!({"sender_seed": s, "recipient_seed": r, "ephemeral_seed": e, "nonce_byte": n, "sender_secret": key_text(&scalar(*s)), "recipient_secret": key_text(&scalar(*r)), "sender_public": key_text(&public_of(&scalar(*s))), "recipient_public": key_text(&public_of(&scalar(*r))), "payload": p, "box": sealed}) }) .collect(); let doc = json!({"scheme": "c1: X25519 (ephemeral and sender static) + HKDF-SHA256 + AES-256-GCM, info cotton-chat-v1", "vectors": list}); let dir = std::env::var("CARGO_MANIFEST_DIR").expect("cargo runs tests with CARGO_MANIFEST_DIR set"); std::fs::write(std::path::Path::new(&dir).join("src/chat/vectors.json"), serde_json::to_string_pretty(&doc).unwrap() + "\n").unwrap(); return; } for (s, r, e, n, payload, sealed) in vectors() { assert_eq!(seal_with(&scalar(e), &[n; 12], &scalar(s), &public_of(&scalar(r)), payload.as_bytes()).as_deref(), Some(sealed.as_str())); let opened = open(&scalar(r), &sealed).expect("opens"); assert_eq!(opened.payload, payload.as_bytes()); assert_eq!(opened.sender, public_of(&scalar(s)), "the box names the device that sealed it"); } } /// RULE: A box opens only for the device it was sealed for. #[test] fn a_box_opens_for_its_device_alone() { let sealed = seal(&scalar(1), &public_of(&scalar(2)), b"for two").unwrap(); assert!(sealed.starts_with(BOX_PREFIX)); assert_eq!(open(&scalar(2), &sealed).unwrap().payload, b"for two"); assert!(open(&scalar(3), &sealed).is_none()); assert!(open(&scalar(1), &sealed).is_none(), "not even the sender opens it"); } /// RULE: The same words seal differently every time. #[test] fn two_seals_of_the_same_words_differ() { let to = public_of(&scalar(2)); assert_ne!(seal(&scalar(1), &to, b"same"), seal(&scalar(1), &to, b"same")); } /// RULE: A box that was changed, or claims another sender, does not open. #[test] fn a_changed_box_does_not_open() { let sealed = seal(&scalar(1), &public_of(&scalar(2)), b"untouched").unwrap(); let mut body = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(&sealed[3..]).unwrap(); // Flip one byte of the ciphertext. let last = body.len() - 1; body[last] ^= 1; assert!(open(&scalar(2), &format!("c1:{}", base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&body))).is_none()); // Swap the named sender for another device's key: the key no longer derives. let mut forged = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(&sealed[3..]).unwrap(); forged[32..64].copy_from_slice(&public_of(&scalar(9))); assert!(open(&scalar(2), &format!("c1:{}", base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&forged))).is_none()); // A vault wrap is not a box. let wrapped = crate::vault::wrap::wrap(&public_of(&scalar(2)), b"thirty-two bytes of vault master").unwrap(); assert!(open(&scalar(2), &wrapped).is_none()); assert!(open(&scalar(2), "c1:").is_none()); } /// RULE: A small-order key is refused rather than sealed to. #[test] fn a_small_order_key_is_refused() { assert!(seal(&scalar(1), &[0u8; 32], b"to nobody").is_none()); }