//! Sealing, opening, and the classifier that says which scheme a row is under. //! //! Every key is a counted byte pattern and every payload is invented. Nothing //! here is a value anybody has ever used. use super::*; fn vmk(seed: u8) -> Vmk { let mut bytes = [seed; 32]; bytes[0] = seed.wrapping_mul(3).wrapping_add(1); Vmk::from_bytes(bytes) } const ROW: &str = "01J00000000000000000000001"; const OTHER_ROW: &str = "01J00000000000000000000002"; /// RULE: A stored password opens only for the row it was saved on and the key version it was saved under. #[test] fn a_sealed_payload_comes_back_for_the_same_row_and_the_same_version() { let key = vmk(1); let sealed = seal(&key, ROW, 1, br#"{"kind":"note","body":"an invented note"}"#).expect("seals"); assert!(sealed.starts_with(SEAL_PREFIX), "got {sealed}"); assert_eq!( open(&key, ROW, 1, &sealed).as_deref(), Some(&br#"{"kind":"note","body":"an invented note"}"#[..]) ); } #[test] fn the_same_payload_seals_differently_every_time() { let key = vmk(2); let first = seal(&key, ROW, 1, b"the same bytes").expect("seals"); let second = seal(&key, ROW, 1, b"the same bytes").expect("seals"); assert_ne!( first, second, "a fresh nonce per seal — AES-GCM does not survive one reused" ); } #[test] fn the_nonce_makes_it_a_vector() { let key = vmk(3); let sealed = seal_with(&[7u8; 12], &key, ROW, 2, b"a payload").expect("seals"); assert_eq!( sealed, seal_with(&[7u8; 12], &key, ROW, 2, b"a payload").expect("seals") ); assert_eq!(open(&key, ROW, 2, &sealed).as_deref(), Some(&b"a payload"[..])); } #[test] fn the_wrong_key_opens_nothing() { let sealed = seal(&vmk(4), ROW, 1, b"not for that key").expect("seals"); assert_eq!(open(&vmk(5), ROW, 1, &sealed), None); } /// RULE: A stored password copied into another row does not open there, so one entry can never answer with another's secret. #[test] fn a_value_moved_to_another_row_opens_nothing() { // The aad is the row's id, so copying `value_enc` from one row into another // does not make the second row answer with the first one's password. let key = vmk(6); let sealed = seal(&key, ROW, 1, b"the first row's payload").expect("seals"); assert_eq!(open(&key, OTHER_ROW, 1, &sealed), None); } /// RULE: Once the workspace key is rotated, a password still stored under the old key fails loudly instead of quietly opening. #[test] fn a_value_left_behind_by_a_rotation_opens_nothing() { // The aad carries the key's version too: a row still holding a value sealed // under v1 fails LOUDLY when the workspace has rotated to v2, rather than // quietly decrypting under a key that was meant to be retired. let key = vmk(7); let sealed = seal(&key, ROW, 1, b"sealed under version one").expect("seals"); assert_eq!(open(&key, ROW, 2, &sealed), None); assert_eq!(open(&key, ROW, 1, &sealed).as_deref(), Some(&b"sealed under version one"[..])); } #[test] fn a_tampered_ciphertext_opens_nothing() { let key = vmk(8); let sealed = seal(&key, ROW, 1, b"a payload worth changing").expect("seals"); let body = base::decode(&sealed); for index in 0..body.len() { let mut broken = body.clone(); broken[index] ^= 0x01; assert_eq!( open(&key, ROW, 1, &base::encode(&broken)), None, "a flipped bit at byte {index} still opened" ); } } #[test] fn a_value_that_is_not_sealed_is_refused_rather_than_guessed_at() { let key = vmk(9); for value in [ "", "v2:", "c2FsdA==:bm9uY2U=:Y2lwaGVydGV4dA==", // the legacy triple "w1:c29tZXRoaW5n", // a WRAP, not a seal "v2:!!!not base64url!!!", "v2:c2hvcnQ", ] { assert_eq!(open(&key, ROW, 1, value), None, "opened {value:?}"); } } #[test] fn the_legacy_classifier_knows_the_old_shape_from_the_new_one() { // The real legacy shape: `base64:base64:base64`, which is what the server // wrote under COTTON_KEYS_SECRET. Invented parts. assert!(is_legacy("c2FsdA==:bm9uY2U=:Y2lwaGVydGV4dA==")); assert!(is_legacy("anything at all without the prefix")); assert!( !is_legacy(""), "an absent value is not legacy — there is nothing to re-seal" ); assert!(!is_legacy(&seal(&vmk(10), ROW, 1, b"sealed").expect("seals"))); assert!(!is_legacy("v2:"), "the prefix alone is not the old scheme"); } /// RULE: A workspace password key is different every time it is created, and never prints itself in a log line. #[test] fn a_minted_key_is_not_the_last_one_and_never_prints_itself() { let first = Vmk::mint(); let second = Vmk::mint(); assert_ne!(first.expose(), second.expose()); assert_eq!(format!("{first:?}"), "Vmk(…)"); assert!( !format!("{first:?}").contains(&format!("{:?}", first.expose()[0])), "a debug line must never carry a byte of the key" ); } /// The sealed body, in and out of its base64url — so the tamper vector can /// reach every byte. mod base { use base64::engine::general_purpose::URL_SAFE_NO_PAD; use base64::Engine as _; pub fn decode(sealed: &str) -> Vec { URL_SAFE_NO_PAD .decode(sealed.strip_prefix(super::SEAL_PREFIX).expect("the prefix")) .expect("base64url") } pub fn encode(body: &[u8]) -> String { format!("{}{}", super::SEAL_PREFIX, URL_SAFE_NO_PAD.encode(body)) } }