/** * Finding: S-01 * * Application-layer encryption for the PII that remains after minimisation (name, email, * phone). Disk-level encryption at the database provider protects against stolen disks; this * protects against the more common leaks: a database dump, an over-broad admin export, a * leaked read replica credential, or a log line that prints a whole row. * * Format: v{keyVersion}.{iv}.{authTag}.{ciphertext}, all base64url. The key version makes * rotation possible: new writes use the newest key, old rows still decrypt. * * Keys: 32 random bytes each, base64, in env vars PII_KEY_V1, PII_KEY_V2, ... and a separate * PII_HMAC_KEY for the blind index. Generate with: openssl rand -base64 32 */ import { createCipheriv, createDecipheriv, createHmac, randomBytes } from "node:crypto"; const ALGO = "aes-256-gcm"; const IV_BYTES = 12; // 96-bit IV is the GCM standard function key(version: number): Buffer { const raw = process.env[`PII_KEY_V${version}`]; if (!raw) throw new Error(`PII_KEY_V${version} is not configured`); const buf = Buffer.from(raw, "base64"); if (buf.length !== 32) throw new Error(`PII_KEY_V${version} must be 32 bytes`); return buf; } const currentVersion = () => Number(process.env.PII_KEY_CURRENT ?? "1"); /** * `context` is bound as additional authenticated data, so a ciphertext copied from one column * or row into another fails to decrypt instead of silently swapping data. */ export function encrypt(plaintext: string, context: string): string { const v = currentVersion(); const iv = randomBytes(IV_BYTES); const cipher = createCipheriv(ALGO, key(v), iv); cipher.setAAD(Buffer.from(context)); const ct = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]); const tag = cipher.getAuthTag(); return [`v${v}`, iv.toString("base64url"), tag.toString("base64url"), ct.toString("base64url")].join("."); } export function decrypt(token: string, context: string): string { const [ver, iv, tag, ct] = token.split("."); if (!ver?.startsWith("v") || !iv || !tag || !ct) throw new Error("Malformed ciphertext"); const decipher = createDecipheriv(ALGO, key(Number(ver.slice(1))), Buffer.from(iv, "base64url")); decipher.setAAD(Buffer.from(context)); decipher.setAuthTag(Buffer.from(tag, "base64url")); return Buffer.concat([decipher.update(Buffer.from(ct, "base64url")), decipher.final()]).toString("utf8"); } /** * Blind index: deterministic keyed hash for equality lookups and unique constraints * (e.g. "has this email already applied to this role?") without storing plaintext. * Keyed with HMAC, so it can't be reversed with a rainbow table of common emails. */ export function blindIndex(value: string): string { const k = process.env.PII_HMAC_KEY; if (!k) throw new Error("PII_HMAC_KEY is not configured"); return createHmac("sha256", Buffer.from(k, "base64")).update(value.trim().toLowerCase()).digest("hex"); } /** Context strings: one per column, so ciphertexts aren't interchangeable. */ export const ctx = { applicantName: "application.fullName", applicantEmail: "application.email", applicantPhone: "application.phone", subscriberEmail: "subscriber.email", } as const;