Cotton code
91 lines · 4.5 KB · RustRaw
//! Files sealed at arrival: round trips, refusals by name, and the vector the//! Worker's twin answers byte for byte. Synthetic keys and bytes only.use serde_json::{json, Value};use super::*;use crate::seal::arrival::arrival_public;const DATABASE: &str = "00000000-0000-4000-8000-0000000000a1";const KEY: &str = "00000000-0000-4000-8000-0000000000a1/photos/0a0b.jpg";fn dk() -> DataKey {    DataKey::from_bytes([21; 32])}fn plain(len: usize) -> Vec<u8> {    (0..len).map(|i| (i * 7 % 251) as u8).collect()}fn sealed(plain: &[u8]) -> Vec<u8> {    let public = arrival_public(&dk(), DATABASE).unwrap();    seal_with(&FileKey::from_bytes([7; 32]), [8; 7], &[5; 32], &[6; 12], &public, KEY, plain).unwrap()}/// RULE (R6, R2): a file the object fetched is stored sealed to the arrival/// key, carries its file key only wrapped, and opens on a device holding the/// DK into the bytes the object was handed, across part edges.#[test]fn an_arrival_file_opens_with_the_key_the_data_key_derives() {    for len in [0, 1, file::PART - 1, file::PART, file::PART + 1, 3 * file::PART + 5] {        let bytes = plain(len);        let stored = sealed(&bytes);        assert!(is_arrival_file(&stored));        assert!(!stored.windows(32).any(|w| w == [7u8; 32]), "the file key is never in the clear");        assert_eq!(open(&dk(), DATABASE, KEY, &stored).unwrap(), bytes, "{len} bytes");        let (at, _) = read_head(&stored).unwrap().unwrap();        assert_eq!(&stored[at as usize..at as usize + 4], file::MAGIC, "the inner file is a device's f1");    }}/// RULE: another DK, another database or another object key opens nothing,/// and a head moved onto another file or an altered part is refused by name.#[test]fn an_arrival_file_opens_for_no_other_key_database_or_file() {    let stored = sealed(&plain(1000));    assert_eq!(open(&DataKey::from_bytes([22; 32]), DATABASE, KEY, &stored).unwrap_err().code(), "seal_does_not_open");    assert_eq!(open(&dk(), "00000000-0000-4000-8000-0000000000a2", KEY, &stored).unwrap_err().code(), "seal_does_not_open");    assert_eq!(open(&dk(), DATABASE, "00000000-0000-4000-8000-0000000000a1/photos/0c0d.jpg", &stored).unwrap_err().code(), "seal_does_not_open");    let mut altered = stored.clone();    let last = altered.len() - 1;    altered[last] ^= 1;    assert_eq!(open(&dk(), DATABASE, KEY, &altered).unwrap_err().code(), "seal_does_not_open");    assert_eq!(open(&dk(), DATABASE, KEY, &stored[..10]).unwrap_err().code(), "seal_does_not_open");    assert!(read_head(b"CTF1abcdefg").is_none(), "a device's own file is no arrival file");}fn vector() -> Value {    let path = cotton_store::manifest_dir().join("../cotton-conformance/vectors/seal/arrival_file.json");    serde_json::from_str(&std::fs::read_to_string(&path).unwrap_or_else(|error| panic!("{}: {error}", path.display()))).expect("the vector reads")}/// RULE: the vector the Worker's twin seals byte for byte.#[test]fn the_arrival_file_vector_holds() {    let v = vector();    let bytes = hex::decode(v["plain_hex"].as_str().unwrap()).unwrap();    assert_eq!(v["object_key"], KEY);    assert_eq!(hex::encode(sealed(&bytes)), v["stored_hex"].as_str().unwrap(), "the stored file");    assert_eq!(open(&dk(), DATABASE, KEY, &hex::decode(v["stored_hex"].as_str().unwrap()).unwrap()).unwrap(), bytes);}/// Writes the vector (run by hand with `-- --ignored` when the format changes).#[test]#[ignore]fn write_the_arrival_file_vector() {    let bytes = b"a synthetic cover, not a picture of anything".to_vec();    let v = json!({        "about": "Sealed at arrival, a file (e2e rulings R6 with R2): the object seals a file it fetched to the person's arrival public key (crates/cotton-core/src/seal/arrival_file.rs; the Worker's twin apps/business/src/arrival_file.ts). With the randomness given both make `stored_hex` byte for byte; a device holding the DK (32 bytes of 21) opens it into `plain_hex`. Synthetic keys and bytes only.",        "database": DATABASE,        "object_key": KEY,        "file_key_hex": hex::encode([7u8; 32]),        "prefix_hex": hex::encode([8u8; 7]),        "ephemeral_hex": hex::encode([5u8; 32]),        "wrap_nonce_hex": hex::encode([6u8; 12]),        "arrival_pub": crate::seal::share::key_text(&arrival_public(&dk(), DATABASE).unwrap()),        "plain_hex": hex::encode(&bytes),        "stored_hex": hex::encode(sealed(&bytes)),    });    let path = cotton_store::manifest_dir().join("../cotton-conformance/vectors/seal/arrival_file.json");    std::fs::write(path, serde_json::to_string_pretty(&v).unwrap() + "\n").unwrap();}