Cotton code
61 lines · 3.2 KB · TypeScriptRaw
/** * How the directory knows a phone number without keeping it (20 Nov 2026). * * Two steps, so the same number reaches the same row from a sign-in and from * a wait (`POST /v1/chat/wait`; contact discovery, which sent digests from * the phone, is gone: DISCOVERY OFF 1, 27 Nov 2026): * * 1. **The digest**: SHA-256 of `cotton-number-v1:` and the E.164 number, as *    64 lowercase hex characters, made by the Worker from the number it *    hears (test/chat-people.test.ts holds its vectors). It is *    NOT secret: there are only about ten billion phone numbers, and anyone *    can try them all against a digest in hours on one computer. A digest *    keeps a number out of sight; it does not keep it from someone who sets *    out to find it. * 2. **The key**: HMAC-SHA256 of the digest under the directory's number key, *    which is derived from the Worker's VAULT_KEY secret under its own label *    (never the secret itself). The directory keeps only this key. Someone who *    copies the directory's storage, without the Worker's secret, holds keys *    they cannot try numbers against. The Worker itself, which holds the *    secret, can: this protects a copy of the data at rest, never against *    the running server. * * VAULT_KEY must never change while the directory holds keys made from it: * the directory writes a check of its key on first use and refuses every * number (loudly, never by making a second database for a number it can no * longer find) when the key it is given no longer matches that check. */import type { Env } from "./env.ts";export const DIGEST_LABEL = "cotton-number-v1:";const KEY_INFO = new TextEncoder().encode("cotton directory number key v1");export const DIGEST = /^[0-9a-f]{64}$/;function hex(bytes: ArrayBuffer): string {  return [...new Uint8Array(bytes)].map((b) => b.toString(16).padStart(2, "0")).join("");}/** The digest a phone sends for an E.164 number. */export async function numberDigest(e164: string): Promise<string> {  return hex(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${DIGEST_LABEL}${e164}`)));}/** The directory's number key, derived from VAULT_KEY; null when the Worker has no VAULT_KEY. */export async function numberKeyOf(env: Pick<Env, "VAULT_KEY">): Promise<CryptoKey | null> {  const secret = env.VAULT_KEY;  if (!secret) return null;  const padded = secret.replace(/-/g, "+").replace(/_/g, "/");  const raw = Uint8Array.from(atob(padded + "=".repeat((4 - (padded.length % 4)) % 4)), (c) => c.charCodeAt(0));  const base = await crypto.subtle.importKey("raw", raw, "HKDF", false, ["deriveKey"]);  return crypto.subtle.deriveKey({ name: "HKDF", hash: "SHA-256", salt: new Uint8Array(32), info: KEY_INFO }, base, { name: "HMAC", hash: "SHA-256", length: 256 }, false, ["sign"]);}/** The key the directory keeps for a digest. */export async function keyOfDigest(key: CryptoKey, digest: string): Promise<string> {  return hex(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(digest)));}/** What the directory writes once to know its key again: the key of a fixed text. */export async function keyCheck(key: CryptoKey): Promise<string> {  return keyOfDigest(key, "cotton directory key check");}