/** * 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 (`<database>/messages/<thread>/<sha>.<ext>`) 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 = newTextEncoder();constF1_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). */exportfunction tableOfFileKind(kind: string): string | null {return kind === "messages" ? "message" : kind === "people" ? "person" : null;}/** The kind a media key names (`<database>/<kind>/…`), or null for another database's. */exportfunction 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). */exportasyncfunction tableFileRoot(dk: Uint8Array, databaseId: string, table: string): Promise<Uint8Array> {const key = await crypto.subtle.importKey("raw", dk, "HKDF", false, ["deriveBits"]);returnnewUint8Array(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`). */exportasyncfunction personFileKey(root: Uint8Array, objectKey: string): Promise<Uint8Array> {const mac = await crypto.subtle.importKey("raw", root, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);returnnewUint8Array(await crypto.subtle.sign("HMAC", mac, utf8.encode(`pf|${objectKey}`)));}function nonce(prefix: Uint8Array, index: number, last: boolean): Uint8Array {const n = newUint8Array(12); n.set(prefix, 0);newDataView(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. */exportasyncfunction openF1(fileKey: Uint8Array, objectKey: string, sealed: Uint8Array): Promise<Uint8Array | null> {const head = 4 + PREFIX;if (sealed.length < head + TAG || newTextDecoder().decode(sealed.subarray(0, 4)) !== F1_MAGIC) returnnull;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) returnnull;const key = await crypto.subtle.importKey("raw", fileKey, "AES-GCM", false, ["decrypt"]);const aad = utf8.encode(`f1|${objectKey}`);const out = newUint8Array(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 = newUint8Array(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 {returnnull; } }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). */exportconst DEVICE_SEALED_KINDS: readonly string[] = [...newSet([...ARRIVAL_SEALED_KINDS, "messages"])];/** Whether `key` (or a folder of it) is one of `databaseId`'s files of a kind a device seals. */exportfunction isDeviceSealedKind(databaseId: string, key: string): boolean {const kind = kindOfKey(databaseId, key);return kind !== null && DEVICE_SEALED_KINDS.includes(kind);}