//! FILES SEALED AT ARRIVAL (e2e rulings R6 with R2, 15 Nov//! 2026): a file the object fetches for a person itself (a book's cover, a//! film's poster, an album's or a show's artwork) is sealed the moment it//! arrives, to the person's arrival public key ([`super::arrival`]). The//! object draws a fresh file key, seals the bytes as any device seals a file//! (f1, [`super::file`]), and keeps that key only wrapped to the arrival key,//! in the file's own head; it never holds a key that opens what it stored.//!//! ```text//! "CTA1" || be16(n) || wrap || f1 file//! wrap n ASCII bytes: the w1 wrap of (FK || "f1" "\n" object_key) to the arrival public key, its "w1:" dropped//! f1 file "CTF1" || prefix(7) || parts, under FK, aad "f1|" object_key: the device's own format, unchanged//! ```//!//! A device opens it with the arrival secret its DK derives: the wrap names//! this object key, so a head moved onto another file opens nothing. The inner//! file starts at `6 + n`, so range reads and the loopback door read it as//! they read any f1 file, past the head. The Worker's twin is//! apps/business/src/arrival_file.ts; both answer//! `crates/cotton-conformance/vectors/seal/arrival_file.json`.usesuper::arrival::arrival_secret;usesuper::file::{self, FileKey};usesuper::keys::DataKey;usesuper::row::Dek;usesuper::share::{open_shared_dek, wrap_dek_for_with};usesuper::{SealError, NONCE_LEN};usecrate::vault::wrap::WRAP_PREFIX;/// The first four bytes of a file sealed at arrival.pubconst MAGIC: &[u8; 4] = b"CTA1";/// The "table" the wrap is bound to, beside the object key.pubconst WRAP_TABLE: &str = "f1";/// Bytes before the wrap: the magic and its length.const LEAD: usize = 6;/// `plain` sealed at arrival for `object_key` to `arrival_pub`, with the/// randomness supplied (vectors and tests; the Worker draws its own).pubfn seal_with(fk: &FileKey, prefix: [u8; 7], ephemeral: &[u8; 32], wrap_nonce: &[u8; NONCE_LEN], arrival_pub: &[u8; 32], object_key: &str, plain: &[u8]) -> Result<Vec<u8>, SealError> {let wrap = wrap_dek_for_with(ephemeral, wrap_nonce, arrival_pub, &Dek::from_bytes(*fk.expose()), WRAP_TABLE, object_key)?;let wrap = wrap.strip_prefix(WRAP_PREFIX).ok_or(SealError::BadInput("a wrap without its prefix".into()))?;let n = u16::try_from(wrap.len()).map_err(|_| SealError::BadInput("an object key too long for an arrival head".into()))?;letmut out = Vec::with_capacity(LEAD + wrap.len() + file::sealed_len(plain.len() as u64) as usize); out.extend_from_slice(MAGIC); out.extend_from_slice(&n.to_be_bytes()); out.extend_from_slice(wrap.as_bytes()); file::seal_file_with(prefix, fk, object_key, &mut &plain[..], &mut out)?;Ok(out)}/// What a file's first bytes say: `None` when it is no arrival file; else/// where its inner f1 file starts and its wrap. `head` holds at least the/// first [`head_len`] bytes, or the whole file when shorter.pubfn read_head(head: &[u8]) -> Option<Result<(u64, String), SealError>> {if head.len() < 4 || &head[..4] != MAGIC {returnNone; }Some((|| {let n = head.get(4..LEAD).map(|b| u16::from_be_bytes([b[0], b[1]]) as usize).ok_or(SealError::DoesNotOpen)?;let wrap = head.get(LEAD..LEAD + n).ok_or(SealError::DoesNotOpen)?;let wrap = std::str::from_utf8(wrap).map_err(|_| SealError::DoesNotOpen)?;Ok(((LEAD + n) as u64, format!("{WRAP_PREFIX}{wrap}"))) })())}/// The most bytes [`read_head`] can need: the lead and the longest wrap.pubconstfn head_len() -> usize { LEAD + u16::MAX as usize}/// Whether these first bytes are an arrival file's.pubfn is_arrival_file(head: &[u8]) -> bool { head.len() >= 4 && &head[..4] == MAGIC}/// The file key in `wrap` (from [`read_head`]), opened with the arrival secret/// `dk` derives; refused unless the wrap names `object_key`.pubfn file_key(dk: &DataKey, database_id: &str, object_key: &str, wrap: &str) -> Result<FileKey, SealError> {let dek = open_shared_dek(&arrival_secret(dk, database_id)?, wrap, WRAP_TABLE, object_key)?;Ok(FileKey::from_bytes(*dek.expose()))}/// A whole arrival file opened: its plaintext.pubfn open(dk: &DataKey, database_id: &str, object_key: &str, sealed: &[u8]) -> Result<Vec<u8>, SealError> {let (at, wrap) = read_head(sealed).ok_or(SealError::DoesNotOpen)??;let fk = file_key(dk, database_id, object_key, &wrap)?;letmut out = Vec::new(); file::open_file(&fk, object_key, &mut &sealed[at as usize..], &mut out)?;Ok(out)}#[cfg(test)]#[path = "arrival_file_tests.rs"]mod tests;