// SEALED BOXES — libsodium's `crypto_box_seal` opened, for a vendor whose API takes a secret encrypted to a public key it // published (GitHub's Actions secrets: "Secrets must be encrypted using the libsodium sealed box", with the repository's // public key; `gh secret set` does it). The twin holds the key pair, derived from a seed, so it can decrypt what its // runner must receive, as the vendor does. Everything here is deterministic: X25519 from Node, and HSalsa20, XSalsa20, // Poly1305 and BLAKE2b written from their specifications (the kernel takes no dependency for them: B1). // // A sealed box is `ephemeral_pk (32) || mac (16) || ciphertext`; its nonce is BLAKE2b-192(ephemeral_pk || recipient_pk), // its key crypto_box_beforenm(ephemeral_pk, recipient_sk) = HSalsa20(X25519(sk, epk), 0), and its body XSalsa20-Poly1305 // (https://doc.libsodium.org/public-key_cryptography/sealed_boxes, "Algorithm details"). // a namespace, never named imports: signing.ts reaches this from the kernel a browser client bundles, and the browser's // `node:crypto` polyfill has no X25519 (signing.ts, the same rule) import * as nodeCrypto from 'node:crypto'; const PKCS8_X25519 = Buffer.from('302e020100300506032b656e04220420', 'hex'); const SPKI_X25519 = Buffer.from('302a300506032b656e032100', 'hex'); /** A key pair from a seed: the secret key is SHA-256 of the seed (X25519 clamps it), the public key its X25519 point. */ export function sealedBoxKeyPair(seed: string): { publicKey: Uint8Array; secretKey: Uint8Array } { const secretKey = new Uint8Array(nodeCrypto.createHash('sha256').update(`sealed-box:${seed}`).digest()); const priv = nodeCrypto.createPrivateKey({ key: Buffer.concat([PKCS8_X25519, secretKey]), format: 'der', type: 'pkcs8' }); const spki = nodeCrypto.createPublicKey(priv).export({ type: 'spki', format: 'der' }) as Buffer; return { publicKey: new Uint8Array(spki.subarray(spki.length - 32)), secretKey }; } function x25519(secretKey: Uint8Array, publicKey: Uint8Array): Uint8Array { const privateKey = nodeCrypto.createPrivateKey({ key: Buffer.concat([PKCS8_X25519, secretKey]), format: 'der', type: 'pkcs8' }); const pub = nodeCrypto.createPublicKey({ key: Buffer.concat([SPKI_X25519, publicKey]), format: 'der', type: 'spki' }); return new Uint8Array(nodeCrypto.diffieHellman({ privateKey, publicKey: pub })); } // ── Salsa20 (https://cr.yp.to/snuffle/spec.pdf) ───────────────────────────────────────────── const rotl = (v: number, c: number): number => ((v << c) | (v >>> (32 - c))) >>> 0; const u32 = (b: Uint8Array, i: number): number => (b[i]! | (b[i + 1]! << 8) | (b[i + 2]! << 16) | (b[i + 3]! << 24)) >>> 0; const SIGMA = new TextEncoder().encode('expand 32-byte k'); /** The Salsa20 state for a key, a 16-byte input (nonce and counter, or HSalsa20's nonce) and the constants. */ function state(key: Uint8Array, input: Uint8Array): Uint32Array { const s = new Uint32Array(16); s[0] = u32(SIGMA, 0); s[5] = u32(SIGMA, 4); s[10] = u32(SIGMA, 8); s[15] = u32(SIGMA, 12); for (let i = 0; i < 4; i++) { s[1 + i] = u32(key, i * 4); s[11 + i] = u32(key, 16 + i * 4); } for (let i = 0; i < 4; i++) s[6 + i] = u32(input, i * 4); return s; } /** Twenty rounds (ten double rounds) over a state, in place. */ function rounds(x: Uint32Array): void { for (let i = 0; i < 10; i++) { x[4] ^= rotl((x[0]! + x[12]!) >>> 0, 7); x[8] ^= rotl((x[4]! + x[0]!) >>> 0, 9); x[12] ^= rotl((x[8]! + x[4]!) >>> 0, 13); x[0] ^= rotl((x[12]! + x[8]!) >>> 0, 18); x[9] ^= rotl((x[5]! + x[1]!) >>> 0, 7); x[13] ^= rotl((x[9]! + x[5]!) >>> 0, 9); x[1] ^= rotl((x[13]! + x[9]!) >>> 0, 13); x[5] ^= rotl((x[1]! + x[13]!) >>> 0, 18); x[14] ^= rotl((x[10]! + x[6]!) >>> 0, 7); x[2] ^= rotl((x[14]! + x[10]!) >>> 0, 9); x[6] ^= rotl((x[2]! + x[14]!) >>> 0, 13); x[10] ^= rotl((x[6]! + x[2]!) >>> 0, 18); x[3] ^= rotl((x[15]! + x[11]!) >>> 0, 7); x[7] ^= rotl((x[3]! + x[15]!) >>> 0, 9); x[11] ^= rotl((x[7]! + x[3]!) >>> 0, 13); x[15] ^= rotl((x[11]! + x[7]!) >>> 0, 18); x[1] ^= rotl((x[0]! + x[3]!) >>> 0, 7); x[2] ^= rotl((x[1]! + x[0]!) >>> 0, 9); x[3] ^= rotl((x[2]! + x[1]!) >>> 0, 13); x[0] ^= rotl((x[3]! + x[2]!) >>> 0, 18); x[6] ^= rotl((x[5]! + x[4]!) >>> 0, 7); x[7] ^= rotl((x[6]! + x[5]!) >>> 0, 9); x[4] ^= rotl((x[7]! + x[6]!) >>> 0, 13); x[5] ^= rotl((x[4]! + x[7]!) >>> 0, 18); x[11] ^= rotl((x[10]! + x[9]!) >>> 0, 7); x[8] ^= rotl((x[11]! + x[10]!) >>> 0, 9); x[9] ^= rotl((x[8]! + x[11]!) >>> 0, 13); x[10] ^= rotl((x[9]! + x[8]!) >>> 0, 18); x[12] ^= rotl((x[15]! + x[14]!) >>> 0, 7); x[13] ^= rotl((x[12]! + x[15]!) >>> 0, 9); x[14] ^= rotl((x[13]! + x[12]!) >>> 0, 13); x[15] ^= rotl((x[14]! + x[13]!) >>> 0, 18); } } const put32 = (out: Uint8Array, at: number, v: number): void => { out[at] = v & 255; out[at + 1] = (v >>> 8) & 255; out[at + 2] = (v >>> 16) & 255; out[at + 3] = (v >>> 24) & 255; }; /** HSalsa20: a 32-byte subkey from a key and a 16-byte nonce (the rounds' words 0, 5, 10, 15, 6, 7, 8, 9, not added). */ function hsalsa20(key: Uint8Array, nonce16: Uint8Array): Uint8Array { const x = state(key, nonce16); rounds(x); const out = new Uint8Array(32); [0, 5, 10, 15, 6, 7, 8, 9].forEach((w, i) => put32(out, i * 4, x[w]!)); return out; } /** The XSalsa20 keystream of `length` bytes for a key and a 24-byte nonce. */ function xsalsa20Stream(key: Uint8Array, nonce24: Uint8Array, length: number): Uint8Array { const sub = hsalsa20(key, nonce24.subarray(0, 16)); const out = new Uint8Array(length); const input = new Uint8Array(16); input.set(nonce24.subarray(16, 24), 0); for (let block = 0, at = 0; at < length; block++, at += 64) { put32(input, 8, block >>> 0); put32(input, 12, Math.floor(block / 0x100000000) >>> 0); const s = state(sub, input); const x = s.slice(); rounds(x); const bytes = new Uint8Array(64); for (let i = 0; i < 16; i++) put32(bytes, i * 4, (x[i]! + s[i]!) >>> 0); out.set(bytes.subarray(0, Math.min(64, length - at)), at); } return out; } // ── Poly1305 (RFC 8439 section 2.5) ───────────────────────────────────────────────────────── function poly1305(key: Uint8Array, message: Uint8Array): Uint8Array { const le = (b: Uint8Array): bigint => b.reduceRight((acc, v) => (acc << 8n) | BigInt(v), 0n); const r = le(key.subarray(0, 16)) & 0x0ffffffc0ffffffc0ffffffc0fffffffn; const s = le(key.subarray(16, 32)); const p = (1n << 130n) - 5n; let acc = 0n; for (let i = 0; i < message.length; i += 16) { const chunk = message.subarray(i, i + 16); const n = le(chunk) + (1n << BigInt(8 * chunk.length)); acc = ((acc + n) * r) % p; } acc = (acc + s) & ((1n << 128n) - 1n); const out = new Uint8Array(16); for (let i = 0; i < 16; i++) { out[i] = Number(acc & 255n); acc >>= 8n; } return out; } // ── BLAKE2b (RFC 7693), for the sealed box's 24-byte nonce ──────────────────────────────────── const IV = [0x6a09e667f3bcc908n, 0xbb67ae8584caa73bn, 0x3c6ef372fe94f82bn, 0xa54ff53a5f1d36f1n, 0x510e527fade682d1n, 0x9b05688c2b3e6c1fn, 0x1f83d9abfb41bd6bn, 0x5be0cd19137e2179n]; const SIGMA_B = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], [11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], [7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8], [9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], [2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9], [12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], [13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10], [6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], [10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0], ]; const M64 = (1n << 64n) - 1n; const rotr64 = (v: bigint, c: bigint): bigint => ((v >> c) | (v << (64n - c))) & M64; function blake2b(input: Uint8Array, outlen: number): Uint8Array { const h = IV.slice(); h[0] ^= 0x01010000n ^ BigInt(outlen); const blocks = Math.max(1, Math.ceil(input.length / 128)); for (let b = 0; b < blocks; b++) { const chunk = new Uint8Array(128); chunk.set(input.subarray(b * 128, b * 128 + 128)); const m: bigint[] = []; for (let i = 0; i < 16; i++) { let w = 0n; for (let j = 7; j >= 0; j--) w = (w << 8n) | BigInt(chunk[i * 8 + j]!); m.push(w); } const last = b === blocks - 1; const t = BigInt(last ? input.length : (b + 1) * 128); const v = [...h, ...IV]; v[12] ^= t & M64; v[13] ^= t >> 64n; if (last) v[14] ^= M64; const g = (a: number, bb: number, c: number, d: number, x: bigint, y: bigint): void => { v[a] = (v[a]! + v[bb]! + x) & M64; v[d] = rotr64(v[d]! ^ v[a]!, 32n); v[c] = (v[c]! + v[d]!) & M64; v[bb] = rotr64(v[bb]! ^ v[c]!, 24n); v[a] = (v[a]! + v[bb]! + y) & M64; v[d] = rotr64(v[d]! ^ v[a]!, 16n); v[c] = (v[c]! + v[d]!) & M64; v[bb] = rotr64(v[bb]! ^ v[c]!, 63n); }; for (let r = 0; r < 12; r++) { const s = SIGMA_B[r % 10]!; g(0, 4, 8, 12, m[s[0]!]!, m[s[1]!]!); g(1, 5, 9, 13, m[s[2]!]!, m[s[3]!]!); g(2, 6, 10, 14, m[s[4]!]!, m[s[5]!]!); g(3, 7, 11, 15, m[s[6]!]!, m[s[7]!]!); g(0, 5, 10, 15, m[s[8]!]!, m[s[9]!]!); g(1, 6, 11, 12, m[s[10]!]!, m[s[11]!]!); g(2, 7, 8, 13, m[s[12]!]!, m[s[13]!]!); g(3, 4, 9, 14, m[s[14]!]!, m[s[15]!]!); } for (let i = 0; i < 8; i++) h[i] = h[i]! ^ v[i]! ^ v[i + 8]!; } const out = new Uint8Array(outlen); for (let i = 0; i < outlen; i++) out[i] = Number((h[i >> 3]! >> BigInt(8 * (i & 7))) & 255n); return out; } /** A sealed box opened with the recipient's key pair: the plaintext, or undefined when it is not one for this key (its * MAC does not hold). */ export function sealedBoxOpen(sealed: Uint8Array, keyPair: { publicKey: Uint8Array; secretKey: Uint8Array }): Uint8Array | undefined { if (sealed.length < 48) return undefined; const epk = sealed.subarray(0, 32); const mac = sealed.subarray(32, 48); const body = sealed.subarray(48); const nonce = blake2b(new Uint8Array([...epk, ...keyPair.publicKey]), 24); const key = hsalsa20(x25519(keyPair.secretKey, epk), new Uint8Array(16)); const stream = xsalsa20Stream(key, nonce, 32 + body.length); const expected = poly1305(stream.subarray(0, 32), body); let diff = 0; for (let i = 0; i < 16; i++) diff |= expected[i]! ^ mac[i]!; if (diff !== 0) return undefined; const out = new Uint8Array(body.length); for (let i = 0; i < body.length; i++) out[i] = body[i]! ^ stream[32 + i]!; return out; } /** A sealed box for a recipient's public key, as a client makes one (`gh secret set`, libsodium's `crypto_box_seal`): a * fresh ephemeral key pair from `seed` (random when none is given), then `ephemeral_pk || mac || ciphertext`. */ export function sealedBoxSeal(message: Uint8Array, recipientPublicKey: Uint8Array, seed?: string): Uint8Array { const ephemeral = sealedBoxKeyPair(seed ?? nodeCrypto.randomBytes(32).toString('hex')); const nonce = blake2b(new Uint8Array([...ephemeral.publicKey, ...recipientPublicKey]), 24); const key = hsalsa20(x25519(ephemeral.secretKey, recipientPublicKey), new Uint8Array(16)); const stream = xsalsa20Stream(key, nonce, 32 + message.length); const body = new Uint8Array(message.length); for (let i = 0; i < message.length; i++) body[i] = message[i]! ^ stream[32 + i]!; return new Uint8Array([...ephemeral.publicKey, ...poly1305(stream.subarray(0, 32), body), ...body]); }