/** * A sealed table's kept files, opened in a window (owner's decision, 22 Nov 2026): a chat picture * kept in the person's own media (`/messages//.`) is sealed on the * device, as a photo is, but under the message table's own files root, TFR(message) * (crates/cotton-core/src/seal/keys.rs `table_file_root`). A window over messages hands that root * with the table's keys (grant.rs `files`), so an assistant reads a kept chat picture inside the * window and nowhere else; a photo's root (FR) is never handed, so no window opens a photo. * * FK = HMAC-SHA256(TFR(table), "pf|" || object key) (file.rs person_file_key) * f1 file = "CTF1" || prefix(7) || parts of 64 KiB, AES-256-GCM under FK, * nonce prefix || be32(index) || last, aad "f1|" || object key (file.rs) * * The core's twin answers crates/cotton-conformance/vectors/seal/table_file.json byte for byte. */ import { SEALED_KINDS as ARRIVAL_SEALED_KINDS } from "./arrival_file.ts"; const utf8 = new TextEncoder(); const F1_MAGIC = "CTF1"; const PREFIX = 7; const PART = 64 * 1024; const TAG = 16; /** * The table whose window opens the person's files of `kind` (keys.rs `table_of_file_kind`): a message's kept pictures, * and a person's picture (AVATAR SEAL, 27 Nov 2026). */ export function tableOfFileKind(kind: string): string | null { return kind === "messages" ? "message" : kind === "people" ? "person" : null; } /** The kind a media key names (`//…`), or null for another database's. */ export function kindOfKey(databaseId: string, key: string): string | null { const [db, kind] = key.split("/"); return db === databaseId && kind ? kind : null; } /** TFR(table) from the DK: the phone's derivation, here for the vectors and tests (the object is handed TFR in a window, never the DK). */ export async function tableFileRoot(dk: Uint8Array, databaseId: string, table: string): Promise { const key = await crypto.subtle.importKey("raw", dk, "HKDF", false, ["deriveBits"]); return new Uint8Array(await crypto.subtle.deriveBits({ name: "HKDF", hash: "SHA-256", salt: utf8.encode(databaseId), info: utf8.encode(`cotton-table-file-v1:${table}`) }, key, 256)); } /** FK for `objectKey` under a files root (file.rs `person_file_key`). */ export async function personFileKey(root: Uint8Array, objectKey: string): Promise { const mac = await crypto.subtle.importKey("raw", root, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]); return new Uint8Array(await crypto.subtle.sign("HMAC", mac, utf8.encode(`pf|${objectKey}`))); } function nonce(prefix: Uint8Array, index: number, last: boolean): Uint8Array { const n = new Uint8Array(12); n.set(prefix, 0); new DataView(n.buffer).setUint32(7, index, false); n[11] = last ? 1 : 0; return n; } /** The plaintext of an f1 file for `objectKey` under `fileKey`, or null: one answer for every way it does not open. */ export async function openF1(fileKey: Uint8Array, objectKey: string, sealed: Uint8Array): Promise { const head = 4 + PREFIX; if (sealed.length < head + TAG || new TextDecoder().decode(sealed.subarray(0, 4)) !== F1_MAGIC) return null; const prefix = sealed.subarray(4, head); const body = sealed.subarray(head); const whole = PART + TAG; const count = Math.max(1, Math.ceil(body.length / whole)); // Every part but the last is whole; the last carries 0 to PART bytes. if (body.length - (count - 1) * whole < TAG) return null; const key = await crypto.subtle.importKey("raw", fileKey, "AES-GCM", false, ["decrypt"]); const aad = utf8.encode(`f1|${objectKey}`); const out = new Uint8Array(body.length - count * TAG); let at = 0; for (let i = 0; i < count; i++) { const part = body.subarray(i * whole, Math.min(body.length, (i + 1) * whole)); try { const plain = new Uint8Array(await crypto.subtle.decrypt({ name: "AES-GCM", iv: nonce(prefix, i, i === count - 1), additionalData: aad }, key, part)); out.set(plain, at); at += plain.length; } catch { return null; } } return out; } /** * Every kind of a person's files a device seals (crates/cotton-client media::sealed SEALED_KINDS): those the object * seals at arrival too, and kept chat pictures, which only a device seals. A token may not ask after, delete or * overwrite a file of these kinds on the plain media route (AVATAR SEAL 3 (3), 27 Nov 2026). */ export const DEVICE_SEALED_KINDS: readonly string[] = [...new Set([...ARRIVAL_SEALED_KINDS, "messages"])]; /** Whether `key` (or a folder of it) is one of `databaseId`'s files of a kind a device seals. */ export function isDeviceSealedKind(databaseId: string, key: string): boolean { const kind = kindOfKey(databaseId, key); return kind !== null && DEVICE_SEALED_KINDS.includes(kind); }