Files
unkernel/js/core/tailscale/crypto.ts
Pooya Parsa d382f7fa9b js: add a pure-JS Tailscale network device (work in progress)
The guest joins a tailnet from Node or the page: ts2021 control over
WebSocket, DERP relays, WireGuard and the packet filter in JS, loaded as
a separate chunk only when the option is used. SDK net option, CLI
--tailscale* flags and a page panel. Relay only; exit nodes, IPv6 and
real peer traffic are not covered yet.
2026-11-28 22:06:18 +00:00

853 lines
31 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// Synchronous crypto primitives for the Tailscale client: X25519,
// ChaCha20-Poly1305 and XChaCha20-Poly1305 (WireGuard transport and
// cookies, the ts2021 Noise channel), BLAKE2s with its HMAC and Noise HKDF
// (WireGuard handshake), and NaCl box (XSalsa20-Poly1305, disco messages).
//
// Pure JS without BigInt, so it runs the same in Node and browsers and
// needs no async WebCrypto round trip per packet. ChaCha20 keeps its state
// in locals and XORs whole words where buffers are 4-byte aligned; Poly1305
// uses ten 13-bit limbs (poly1305-donna-16), whose partial products stay
// below 2^32; X25519 uses sixteen 16-bit limbs in a Float64Array with a
// constant-time Montgomery ladder (RFC 7748). Inputs are never modified.
const LE = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
function checkLen(name: string, b: Uint8Array, n: number): void {
if (b.length !== n) throw new RangeError(`${name} must be ${n} bytes, got ${b.length}`);
}
function u32(b: Uint8Array, i: number): number {
return (b[i]! | (b[i + 1]! << 8) | (b[i + 2]! << 16) | (b[i + 3]! << 24)) >>> 0;
}
function put32(b: Uint8Array, i: number, v: number): void {
b[i] = v;
b[i + 1] = v >>> 8;
b[i + 2] = v >>> 16;
b[i + 3] = v >>> 24;
}
/** n random bytes from crypto.getRandomValues (chunked at its 64 KiB limit). */
export function randomBytes(n: number): Uint8Array {
const out = new Uint8Array(n);
for (let i = 0; i < n; i += 65536) crypto.getRandomValues(out.subarray(i, Math.min(n, i + 65536)));
return out;
}
/** Constant-time comparison; false for different lengths. */
export function equalBytes(a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) return false;
let d = 0;
for (let i = 0; i < a.length; i++) d |= a[i]! ^ b[i]!;
return d === 0;
}
export function concatBytes(...parts: Uint8Array[]): Uint8Array {
let n = 0;
for (const p of parts) n += p.length;
const out = new Uint8Array(n);
let o = 0;
for (const p of parts) {
out.set(p, o);
o += p.length;
}
return out;
}
// --- ChaCha20 (RFC 8439) ---
const ks = new Uint32Array(16);
/**
* XORs src with the ChaCha20 keystream for key k (8 words) and nonce
* n0..n2 from block `counter` on, into dst at dpos.
*/
function chachaXor(k: Uint32Array, n0: number, n1: number, n2: number, counter: number, src: Uint8Array, dst: Uint8Array, dpos: number): void {
const k0 = k[0]!, k1 = k[1]!, k2 = k[2]!, k3 = k[3]!, k4 = k[4]!, k5 = k[5]!, k6 = k[6]!, k7 = k[7]!;
const len = src.length;
const aligned = LE && ((src.byteOffset & 3) | ((dst.byteOffset + dpos) & 3)) === 0;
const full = len >>> 6;
const s32 = aligned ? new Uint32Array(src.buffer, src.byteOffset, full * 16) : null;
const d32 = aligned ? new Uint32Array(dst.buffer, dst.byteOffset + dpos, full * 16) : null;
let ctr = counter;
for (let pos = 0, blk = 0; pos < len; pos += 64, blk++) {
let x0 = 0x61707865, x1 = 0x3320646e, x2 = 0x79622d32, x3 = 0x6b206574;
let x4 = k0, x5 = k1, x6 = k2, x7 = k3, x8 = k4, x9 = k5, x10 = k6, x11 = k7;
let x12 = ctr, x13 = n0, x14 = n1, x15 = n2;
for (let r = 0; r < 10; r++) {
x0 = (x0 + x4) | 0; x12 ^= x0; x12 = (x12 << 16) | (x12 >>> 16);
x8 = (x8 + x12) | 0; x4 ^= x8; x4 = (x4 << 12) | (x4 >>> 20);
x0 = (x0 + x4) | 0; x12 ^= x0; x12 = (x12 << 8) | (x12 >>> 24);
x8 = (x8 + x12) | 0; x4 ^= x8; x4 = (x4 << 7) | (x4 >>> 25);
x1 = (x1 + x5) | 0; x13 ^= x1; x13 = (x13 << 16) | (x13 >>> 16);
x9 = (x9 + x13) | 0; x5 ^= x9; x5 = (x5 << 12) | (x5 >>> 20);
x1 = (x1 + x5) | 0; x13 ^= x1; x13 = (x13 << 8) | (x13 >>> 24);
x9 = (x9 + x13) | 0; x5 ^= x9; x5 = (x5 << 7) | (x5 >>> 25);
x2 = (x2 + x6) | 0; x14 ^= x2; x14 = (x14 << 16) | (x14 >>> 16);
x10 = (x10 + x14) | 0; x6 ^= x10; x6 = (x6 << 12) | (x6 >>> 20);
x2 = (x2 + x6) | 0; x14 ^= x2; x14 = (x14 << 8) | (x14 >>> 24);
x10 = (x10 + x14) | 0; x6 ^= x10; x6 = (x6 << 7) | (x6 >>> 25);
x3 = (x3 + x7) | 0; x15 ^= x3; x15 = (x15 << 16) | (x15 >>> 16);
x11 = (x11 + x15) | 0; x7 ^= x11; x7 = (x7 << 12) | (x7 >>> 20);
x3 = (x3 + x7) | 0; x15 ^= x3; x15 = (x15 << 8) | (x15 >>> 24);
x11 = (x11 + x15) | 0; x7 ^= x11; x7 = (x7 << 7) | (x7 >>> 25);
x0 = (x0 + x5) | 0; x15 ^= x0; x15 = (x15 << 16) | (x15 >>> 16);
x10 = (x10 + x15) | 0; x5 ^= x10; x5 = (x5 << 12) | (x5 >>> 20);
x0 = (x0 + x5) | 0; x15 ^= x0; x15 = (x15 << 8) | (x15 >>> 24);
x10 = (x10 + x15) | 0; x5 ^= x10; x5 = (x5 << 7) | (x5 >>> 25);
x1 = (x1 + x6) | 0; x12 ^= x1; x12 = (x12 << 16) | (x12 >>> 16);
x11 = (x11 + x12) | 0; x6 ^= x11; x6 = (x6 << 12) | (x6 >>> 20);
x1 = (x1 + x6) | 0; x12 ^= x1; x12 = (x12 << 8) | (x12 >>> 24);
x11 = (x11 + x12) | 0; x6 ^= x11; x6 = (x6 << 7) | (x6 >>> 25);
x2 = (x2 + x7) | 0; x13 ^= x2; x13 = (x13 << 16) | (x13 >>> 16);
x8 = (x8 + x13) | 0; x7 ^= x8; x7 = (x7 << 12) | (x7 >>> 20);
x2 = (x2 + x7) | 0; x13 ^= x2; x13 = (x13 << 8) | (x13 >>> 24);
x8 = (x8 + x13) | 0; x7 ^= x8; x7 = (x7 << 7) | (x7 >>> 25);
x3 = (x3 + x4) | 0; x14 ^= x3; x14 = (x14 << 16) | (x14 >>> 16);
x9 = (x9 + x14) | 0; x4 ^= x9; x4 = (x4 << 12) | (x4 >>> 20);
x3 = (x3 + x4) | 0; x14 ^= x3; x14 = (x14 << 8) | (x14 >>> 24);
x9 = (x9 + x14) | 0; x4 ^= x9; x4 = (x4 << 7) | (x4 >>> 25);
}
x0 = (x0 + 0x61707865) | 0; x1 = (x1 + 0x3320646e) | 0;
x2 = (x2 + 0x79622d32) | 0; x3 = (x3 + 0x6b206574) | 0;
x4 = (x4 + k0) | 0; x5 = (x5 + k1) | 0; x6 = (x6 + k2) | 0; x7 = (x7 + k3) | 0;
x8 = (x8 + k4) | 0; x9 = (x9 + k5) | 0; x10 = (x10 + k6) | 0; x11 = (x11 + k7) | 0;
x12 = (x12 + ctr) | 0; x13 = (x13 + n0) | 0; x14 = (x14 + n1) | 0; x15 = (x15 + n2) | 0;
ctr = (ctr + 1) >>> 0;
if (s32 !== null && d32 !== null && blk < full) {
const w = blk * 16;
d32[w] = s32[w]! ^ x0; d32[w + 1] = s32[w + 1]! ^ x1;
d32[w + 2] = s32[w + 2]! ^ x2; d32[w + 3] = s32[w + 3]! ^ x3;
d32[w + 4] = s32[w + 4]! ^ x4; d32[w + 5] = s32[w + 5]! ^ x5;
d32[w + 6] = s32[w + 6]! ^ x6; d32[w + 7] = s32[w + 7]! ^ x7;
d32[w + 8] = s32[w + 8]! ^ x8; d32[w + 9] = s32[w + 9]! ^ x9;
d32[w + 10] = s32[w + 10]! ^ x10; d32[w + 11] = s32[w + 11]! ^ x11;
d32[w + 12] = s32[w + 12]! ^ x12; d32[w + 13] = s32[w + 13]! ^ x13;
d32[w + 14] = s32[w + 14]! ^ x14; d32[w + 15] = s32[w + 15]! ^ x15;
continue;
}
ks[0] = x0; ks[1] = x1; ks[2] = x2; ks[3] = x3; ks[4] = x4; ks[5] = x5; ks[6] = x6; ks[7] = x7;
ks[8] = x8; ks[9] = x9; ks[10] = x10; ks[11] = x11; ks[12] = x12; ks[13] = x13; ks[14] = x14; ks[15] = x15;
const n = Math.min(64, len - pos);
for (let i = 0; i < n; i++) dst[dpos + pos + i] = src[pos + i]! ^ (ks[i >>> 2]! >>> ((i & 3) << 3));
}
}
/** The 32-byte Poly1305 key: the first half of keystream block 0. */
function chachaPolyKey(k: Uint32Array, n0: number, n1: number, n2: number): Uint8Array {
const otk = new Uint8Array(32);
chachaXor(k, n0, n1, n2, 0, otk, otk, 0);
return otk;
}
function keyWords(key: Uint8Array): Uint32Array {
const k = new Uint32Array(8);
for (let i = 0; i < 8; i++) k[i] = u32(key, i * 4);
return k;
}
/** HChaCha20 (draft-irtf-cfrg-xchacha): key and 16-byte input to a 32-byte subkey. */
function hchacha20(key: Uint8Array, inp: Uint8Array): Uint32Array {
const x = new Uint32Array(16);
x[0] = 0x61707865; x[1] = 0x3320646e; x[2] = 0x79622d32; x[3] = 0x6b206574;
for (let i = 0; i < 8; i++) x[4 + i] = u32(key, i * 4);
for (let i = 0; i < 4; i++) x[12 + i] = u32(inp, i * 4);
const qr = (a: number, b: number, c: number, d: number): void => {
let xa = x[a]!, xb = x[b]!, xc = x[c]!, xd = x[d]!;
xa = (xa + xb) | 0; xd ^= xa; xd = (xd << 16) | (xd >>> 16);
xc = (xc + xd) | 0; xb ^= xc; xb = (xb << 12) | (xb >>> 20);
xa = (xa + xb) | 0; xd ^= xa; xd = (xd << 8) | (xd >>> 24);
xc = (xc + xd) | 0; xb ^= xc; xb = (xb << 7) | (xb >>> 25);
x[a] = xa; x[b] = xb; x[c] = xc; x[d] = xd;
};
for (let r = 0; r < 10; r++) {
qr(0, 4, 8, 12); qr(1, 5, 9, 13); qr(2, 6, 10, 14); qr(3, 7, 11, 15);
qr(0, 5, 10, 15); qr(1, 6, 11, 12); qr(2, 7, 8, 13); qr(3, 4, 9, 14);
}
const out = new Uint32Array(8);
for (let i = 0; i < 4; i++) {
out[i] = x[i]!;
out[4 + i] = x[12 + i]!;
}
return out;
}
// --- Poly1305 (poly1305-donna-16: ten 13-bit limbs) ---
const ZERO16 = new Uint8Array(16);
class Poly1305 {
private r = new Int32Array(10);
private h = new Int32Array(10);
private pad = new Int32Array(8);
private buf = new Uint8Array(16);
private left = 0;
private fin = 0;
constructor(key: Uint8Array) {
const t0 = key[0]! | (key[1]! << 8), t1 = key[2]! | (key[3]! << 8);
const t2 = key[4]! | (key[5]! << 8), t3 = key[6]! | (key[7]! << 8);
const t4 = key[8]! | (key[9]! << 8), t5 = key[10]! | (key[11]! << 8);
const t6 = key[12]! | (key[13]! << 8), t7 = key[14]! | (key[15]! << 8);
const r = this.r;
r[0] = t0 & 0x1fff;
r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
r[5] = (t4 >>> 1) & 0x1ffe;
r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
r[9] = (t7 >>> 5) & 0x007f;
for (let i = 0; i < 8; i++) this.pad[i] = key[16 + 2 * i]! | (key[17 + 2 * i]! << 8);
}
private blocks(m: Uint8Array, pos: number, bytes: number): void {
const hibit = this.fin ? 0 : 1 << 11;
const R = this.r, H = this.h;
const r0 = R[0]!, r1 = R[1]!, r2 = R[2]!, r3 = R[3]!, r4 = R[4]!;
const r5 = R[5]!, r6 = R[6]!, r7 = R[7]!, r8 = R[8]!, r9 = R[9]!;
const s1 = 5 * r1, s2 = 5 * r2, s3 = 5 * r3, s4 = 5 * r4, s5 = 5 * r5;
const s6 = 5 * r6, s7 = 5 * r7, s8 = 5 * r8, s9 = 5 * r9;
let h0 = H[0]!, h1 = H[1]!, h2 = H[2]!, h3 = H[3]!, h4 = H[4]!;
let h5 = H[5]!, h6 = H[6]!, h7 = H[7]!, h8 = H[8]!, h9 = H[9]!;
while (bytes >= 16) {
const t0 = m[pos]! | (m[pos + 1]! << 8), t1 = m[pos + 2]! | (m[pos + 3]! << 8);
const t2 = m[pos + 4]! | (m[pos + 5]! << 8), t3 = m[pos + 6]! | (m[pos + 7]! << 8);
const t4 = m[pos + 8]! | (m[pos + 9]! << 8), t5 = m[pos + 10]! | (m[pos + 11]! << 8);
const t6 = m[pos + 12]! | (m[pos + 13]! << 8), t7 = m[pos + 14]! | (m[pos + 15]! << 8);
h0 += t0 & 0x1fff;
h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
h5 += (t4 >>> 1) & 0x1fff;
h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
h9 += (t7 >>> 5) | hibit;
// h *= r mod 2^130 - 5, each half-row below 2^32.
let d0 = h0 * r0 + h1 * s9 + h2 * s8 + h3 * s7 + h4 * s6;
let c = d0 >>> 13;
d0 = (d0 & 0x1fff) + h5 * s5 + h6 * s4 + h7 * s3 + h8 * s2 + h9 * s1;
c += d0 >>> 13;
d0 &= 0x1fff;
let d1 = c + h0 * r1 + h1 * r0 + h2 * s9 + h3 * s8 + h4 * s7;
c = d1 >>> 13;
d1 = (d1 & 0x1fff) + h5 * s6 + h6 * s5 + h7 * s4 + h8 * s3 + h9 * s2;
c += d1 >>> 13;
d1 &= 0x1fff;
let d2 = c + h0 * r2 + h1 * r1 + h2 * r0 + h3 * s9 + h4 * s8;
c = d2 >>> 13;
d2 = (d2 & 0x1fff) + h5 * s7 + h6 * s6 + h7 * s5 + h8 * s4 + h9 * s3;
c += d2 >>> 13;
d2 &= 0x1fff;
let d3 = c + h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * s9;
c = d3 >>> 13;
d3 = (d3 & 0x1fff) + h5 * s8 + h6 * s7 + h7 * s6 + h8 * s5 + h9 * s4;
c += d3 >>> 13;
d3 &= 0x1fff;
let d4 = c + h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0;
c = d4 >>> 13;
d4 = (d4 & 0x1fff) + h5 * s9 + h6 * s8 + h7 * s7 + h8 * s6 + h9 * s5;
c += d4 >>> 13;
d4 &= 0x1fff;
let d5 = c + h0 * r5 + h1 * r4 + h2 * r3 + h3 * r2 + h4 * r1;
c = d5 >>> 13;
d5 = (d5 & 0x1fff) + h5 * r0 + h6 * s9 + h7 * s8 + h8 * s7 + h9 * s6;
c += d5 >>> 13;
d5 &= 0x1fff;
let d6 = c + h0 * r6 + h1 * r5 + h2 * r4 + h3 * r3 + h4 * r2;
c = d6 >>> 13;
d6 = (d6 & 0x1fff) + h5 * r1 + h6 * r0 + h7 * s9 + h8 * s8 + h9 * s7;
c += d6 >>> 13;
d6 &= 0x1fff;
let d7 = c + h0 * r7 + h1 * r6 + h2 * r5 + h3 * r4 + h4 * r3;
c = d7 >>> 13;
d7 = (d7 & 0x1fff) + h5 * r2 + h6 * r1 + h7 * r0 + h8 * s9 + h9 * s8;
c += d7 >>> 13;
d7 &= 0x1fff;
let d8 = c + h0 * r8 + h1 * r7 + h2 * r6 + h3 * r5 + h4 * r4;
c = d8 >>> 13;
d8 = (d8 & 0x1fff) + h5 * r3 + h6 * r2 + h7 * r1 + h8 * r0 + h9 * s9;
c += d8 >>> 13;
d8 &= 0x1fff;
let d9 = c + h0 * r9 + h1 * r8 + h2 * r7 + h3 * r6 + h4 * r5;
c = d9 >>> 13;
d9 = (d9 & 0x1fff) + h5 * r4 + h6 * r3 + h7 * r2 + h8 * r1 + h9 * r0;
c += d9 >>> 13;
d9 &= 0x1fff;
c = (((c << 2) + c) | 0) + d0;
h0 = c & 0x1fff;
h1 = d1 + (c >>> 13);
h2 = d2; h3 = d3; h4 = d4; h5 = d5; h6 = d6; h7 = d7; h8 = d8; h9 = d9;
pos += 16;
bytes -= 16;
}
H[0] = h0; H[1] = h1; H[2] = h2; H[3] = h3; H[4] = h4;
H[5] = h5; H[6] = h6; H[7] = h7; H[8] = h8; H[9] = h9;
}
update(m: Uint8Array, pos = 0, bytes = m.length - pos): void {
if (this.left) {
const want = Math.min(16 - this.left, bytes);
for (let i = 0; i < want; i++) this.buf[this.left + i] = m[pos + i]!;
this.left += want;
pos += want;
bytes -= want;
if (this.left < 16) return;
this.blocks(this.buf, 0, 16);
this.left = 0;
}
if (bytes >= 16) {
const full = bytes & ~15;
this.blocks(m, pos, full);
pos += full;
bytes -= full;
}
for (let i = 0; i < bytes; i++) this.buf[i] = m[pos + i]!;
this.left = bytes;
}
finish(): Uint8Array {
if (this.left) {
this.buf[this.left] = 1;
this.buf.fill(0, this.left + 1);
this.fin = 1;
this.blocks(this.buf, 0, 16);
}
const h = this.h;
// Two full carry passes leave every limb below 2^13 and h < 2^130.
for (let pass = 0; pass < 2; pass++) {
let c = 0;
for (let i = 0; i < 10; i++) {
const v = h[i]! + c;
c = v >>> 13;
h[i] = v & 0x1fff;
}
h[0] = h[0]! + c * 5;
}
// g = h + 5 - 2^130; take it when it does not borrow (h >= p).
const g = new Int32Array(10);
let c = 5;
for (let i = 0; i < 10; i++) {
const v = h[i]! + c;
c = v >>> 13;
g[i] = v & 0x1fff;
}
const mask = -c;
for (let i = 0; i < 10; i++) h[i] = (h[i]! & ~mask) | (g[i]! & mask);
const w = [
(h[0]! | (h[1]! << 13)) & 0xffff,
((h[1]! >>> 3) | (h[2]! << 10)) & 0xffff,
((h[2]! >>> 6) | (h[3]! << 7)) & 0xffff,
((h[3]! >>> 9) | (h[4]! << 4)) & 0xffff,
((h[4]! >>> 12) | (h[5]! << 1) | (h[6]! << 14)) & 0xffff,
((h[6]! >>> 2) | (h[7]! << 11)) & 0xffff,
((h[7]! >>> 5) | (h[8]! << 8)) & 0xffff,
((h[8]! >>> 8) | (h[9]! << 5)) & 0xffff,
];
const mac = new Uint8Array(16);
let f = 0;
for (let i = 0; i < 8; i++) {
f = w[i]! + this.pad[i]! + (f >>> 16);
mac[2 * i] = f;
mac[2 * i + 1] = f >>> 8;
}
return mac;
}
}
/** The RFC 8439 AEAD tag over aad and ciphertext ct. */
function aeadTag(otk: Uint8Array, aad: Uint8Array, ct: Uint8Array, ctPos: number, ctLen: number): Uint8Array {
const p = new Poly1305(otk);
p.update(aad);
if (aad.length & 15) p.update(ZERO16, 0, 16 - (aad.length & 15));
p.update(ct, ctPos, ctLen);
if (ctLen & 15) p.update(ZERO16, 0, 16 - (ctLen & 15));
const lens = new Uint8Array(16);
put32(lens, 0, aad.length);
put32(lens, 4, Math.floor(aad.length / 2 ** 32));
put32(lens, 8, ctLen);
put32(lens, 12, Math.floor(ctLen / 2 ** 32));
p.update(lens);
return p.finish();
}
const EMPTY = new Uint8Array(0);
function aeadSeal(k: Uint32Array, n0: number, n1: number, n2: number, plaintext: Uint8Array, aad: Uint8Array): Uint8Array {
const len = plaintext.length;
const out = new Uint8Array(len + 16);
chachaXor(k, n0, n1, n2, 1, plaintext, out, 0);
out.set(aeadTag(chachaPolyKey(k, n0, n1, n2), aad, out, 0, len), len);
return out;
}
function aeadOpen(k: Uint32Array, n0: number, n1: number, n2: number, sealed: Uint8Array, aad: Uint8Array): Uint8Array | null {
if (sealed.length < 16) return null;
const len = sealed.length - 16;
const tag = aeadTag(chachaPolyKey(k, n0, n1, n2), aad, sealed, 0, len);
if (!equalBytes(tag, sealed.subarray(len))) return null;
const out = new Uint8Array(len);
chachaXor(k, n0, n1, n2, 1, sealed.subarray(0, len), out, 0);
return out;
}
/** ChaCha20-Poly1305 (RFC 8439): ciphertext || 16-byte tag. */
export function chacha20poly1305Seal(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad: Uint8Array = EMPTY): Uint8Array {
checkLen("key", key, 32);
checkLen("nonce", nonce, 12);
return aeadSeal(keyWords(key), u32(nonce, 0), u32(nonce, 4), u32(nonce, 8), plaintext, aad);
}
/** Opens chacha20poly1305Seal output; null if it does not authenticate. */
export function chacha20poly1305Open(key: Uint8Array, nonce: Uint8Array, sealed: Uint8Array, aad: Uint8Array = EMPTY): Uint8Array | null {
checkLen("key", key, 32);
checkLen("nonce", nonce, 12);
return aeadOpen(keyWords(key), u32(nonce, 0), u32(nonce, 4), u32(nonce, 8), sealed, aad);
}
/** XChaCha20-Poly1305 (24-byte nonce): ciphertext || 16-byte tag. */
export function xchacha20poly1305Seal(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad: Uint8Array = EMPTY): Uint8Array {
checkLen("key", key, 32);
checkLen("nonce", nonce, 24);
return aeadSeal(hchacha20(key, nonce), 0, u32(nonce, 16), u32(nonce, 20), plaintext, aad);
}
export function xchacha20poly1305Open(key: Uint8Array, nonce: Uint8Array, sealed: Uint8Array, aad: Uint8Array = EMPTY): Uint8Array | null {
checkLen("key", key, 32);
checkLen("nonce", nonce, 24);
return aeadOpen(hchacha20(key, nonce), 0, u32(nonce, 16), u32(nonce, 20), sealed, aad);
}
// --- BLAKE2s (RFC 7693) ---
const B2S_IV = new Uint32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
// prettier-ignore
const SIGMA = new Uint8Array([
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 b2v = new Uint32Array(16);
const b2m = new Uint32Array(16);
function b2g(a: number, b: number, c: number, d: number, x: number, y: number): void {
const v = b2v;
let va = v[a]!, vb = v[b]!, vc = v[c]!, vd = v[d]!;
va = (va + vb + x) | 0; vd ^= va; vd = (vd >>> 16) | (vd << 16);
vc = (vc + vd) | 0; vb ^= vc; vb = (vb >>> 12) | (vb << 20);
va = (va + vb + y) | 0; vd ^= va; vd = (vd >>> 8) | (vd << 24);
vc = (vc + vd) | 0; vb ^= vc; vb = (vb >>> 7) | (vb << 25);
v[a] = va; v[b] = vb; v[c] = vc; v[d] = vd;
}
class Blake2s {
private h = new Uint32Array(8);
private buf = new Uint8Array(64);
private fill = 0;
private t = 0;
private readonly outLen: number;
constructor(outLen: number, key?: Uint8Array) {
if (!(outLen >= 1 && outLen <= 32)) throw new RangeError("blake2s output length must be 1..32");
const keyLen = key ? key.length : 0;
if (keyLen > 32) throw new RangeError("blake2s key must be at most 32 bytes");
this.outLen = outLen;
this.h.set(B2S_IV);
this.h[0] = this.h[0]! ^ 0x01010000 ^ (keyLen << 8) ^ outLen;
if (key && keyLen) {
this.buf.set(key);
this.fill = 64;
}
}
private compress(last: boolean): void {
const v = b2v, m = b2m, h = this.h, buf = this.buf;
for (let i = 0; i < 16; i++) m[i] = u32(buf, i * 4);
for (let i = 0; i < 8; i++) {
v[i] = h[i]!;
v[i + 8] = B2S_IV[i]!;
}
v[12] = v[12]! ^ (this.t >>> 0);
v[13] = v[13]! ^ Math.floor(this.t / 2 ** 32);
if (last) v[14] = ~v[14]!;
for (let r = 0; r < 10; r++) {
const s = r * 16;
b2g(0, 4, 8, 12, m[SIGMA[s]!]!, m[SIGMA[s + 1]!]!);
b2g(1, 5, 9, 13, m[SIGMA[s + 2]!]!, m[SIGMA[s + 3]!]!);
b2g(2, 6, 10, 14, m[SIGMA[s + 4]!]!, m[SIGMA[s + 5]!]!);
b2g(3, 7, 11, 15, m[SIGMA[s + 6]!]!, m[SIGMA[s + 7]!]!);
b2g(0, 5, 10, 15, m[SIGMA[s + 8]!]!, m[SIGMA[s + 9]!]!);
b2g(1, 6, 11, 12, m[SIGMA[s + 10]!]!, m[SIGMA[s + 11]!]!);
b2g(2, 7, 8, 13, m[SIGMA[s + 12]!]!, m[SIGMA[s + 13]!]!);
b2g(3, 4, 9, 14, m[SIGMA[s + 14]!]!, m[SIGMA[s + 15]!]!);
}
for (let i = 0; i < 8; i++) h[i] = h[i]! ^ v[i]! ^ v[i + 8]!;
}
update(data: Uint8Array): void {
let pos = 0;
const len = data.length;
while (pos < len) {
// A full buffer is compressed only once more input follows, so
// the last block gets the final flag.
if (this.fill === 64) {
this.t += 64;
this.compress(false);
this.fill = 0;
}
const n = Math.min(64 - this.fill, len - pos);
this.buf.set(data.subarray(pos, pos + n), this.fill);
this.fill += n;
pos += n;
}
}
digest(): Uint8Array {
this.t += this.fill;
this.buf.fill(0, this.fill);
this.compress(true);
const full = new Uint8Array(32);
for (let i = 0; i < 8; i++) put32(full, i * 4, this.h[i]!);
return full.slice(0, this.outLen);
}
}
/** BLAKE2s of data (one buffer or the concatenation of several); keyed when key is given. */
export function blake2s(data: Uint8Array | Uint8Array[], outLen = 32, key?: Uint8Array): Uint8Array {
const b = new Blake2s(outLen, key);
if (Array.isArray(data)) for (const d of data) b.update(d);
else b.update(data);
return b.digest();
}
/** HMAC (RFC 2104) with BLAKE2s-256, block size 64. */
export function hmacBlake2s(key: Uint8Array, data: Uint8Array | Uint8Array[]): Uint8Array {
const k = new Uint8Array(64);
k.set(key.length > 64 ? blake2s(key) : key);
const ipad = new Uint8Array(64), opad = new Uint8Array(64);
for (let i = 0; i < 64; i++) {
ipad[i] = k[i]! ^ 0x36;
opad[i] = k[i]! ^ 0x5c;
}
const inner = new Blake2s(32);
inner.update(ipad);
if (Array.isArray(data)) for (const d of data) inner.update(d);
else inner.update(data);
const outer = new Blake2s(32);
outer.update(opad);
outer.update(inner.digest());
return outer.digest();
}
/** Noise's HKDF over HMAC-BLAKE2s: 1 to 3 outputs of 32 bytes. */
export function hkdfBlake2s(chainingKey: Uint8Array, ikm: Uint8Array, outputs: 1 | 2 | 3): Uint8Array[] {
const prk = hmacBlake2s(chainingKey, ikm);
const out: Uint8Array[] = [];
let prev: Uint8Array = EMPTY;
for (let i = 1; i <= outputs; i++) {
prev = hmacBlake2s(prk, [prev, new Uint8Array([i])]);
out.push(prev);
}
return out;
}
// --- X25519 (RFC 7748): GF(2^255 - 19) in sixteen 16-bit limbs ---
type Gf = Float64Array;
const gf = (init?: number[]): Gf => {
const r = new Float64Array(16);
if (init) r.set(init);
return r;
};
const prod = new Float64Array(31);
const A24 = gf([0xdb41, 1]);
function carry(o: Gf): void {
let c = 0;
for (let i = 0; i < 16; i++) {
const v = o[i]! + c;
c = Math.floor(v / 65536);
o[i] = v - c * 65536;
}
o[0] = o[0]! + 38 * c;
}
/** Swaps p and q when b is 1, without branching on b. */
function cswap(p: Gf, q: Gf, b: number): void {
const m = -b;
for (let i = 0; i < 16; i++) {
const t = m & (p[i]! ^ q[i]!);
p[i] = p[i]! ^ t;
q[i] = q[i]! ^ t;
}
}
function fadd(o: Gf, a: Gf, b: Gf): void {
for (let i = 0; i < 16; i++) o[i] = a[i]! + b[i]!;
}
function fsub(o: Gf, a: Gf, b: Gf): void {
for (let i = 0; i < 16; i++) o[i] = a[i]! - b[i]!;
}
function fmul(o: Gf, a: Gf, b: Gf): void {
const t = prod;
t.fill(0);
for (let i = 0; i < 16; i++) {
const ai = a[i]!;
for (let j = 0; j < 16; j++) t[i + j] = t[i + j]! + ai * b[j]!;
}
for (let i = 0; i < 15; i++) o[i] = t[i]! + 38 * t[i + 16]!;
o[15] = t[15]!;
carry(o);
carry(o);
}
function finv(o: Gf, z: Gf): void {
// z^(p-2), p - 2 = 2^255 - 21: every exponent bit is set but bits 2 and 4.
const c = gf();
c.set(z);
for (let a = 253; a >= 0; a--) {
fmul(c, c, c);
if (a !== 2 && a !== 4) fmul(c, c, z);
}
o.set(c);
}
function pack(out: Uint8Array, n: Gf): void {
const t = gf(), m = gf();
t.set(n);
carry(t);
carry(t);
carry(t);
for (let j = 0; j < 2; j++) {
m[0] = t[0]! - 0xffed;
for (let i = 1; i < 15; i++) {
m[i] = t[i]! - 0xffff - ((m[i - 1]! >> 16) & 1);
m[i - 1] = m[i - 1]! & 0xffff;
}
m[15] = t[15]! - 0x7fff - ((m[14]! >> 16) & 1);
const b = (m[15]! >> 16) & 1;
m[14] = m[14]! & 0xffff;
cswap(t, m, 1 - b);
}
for (let i = 0; i < 16; i++) {
out[2 * i] = t[i]!;
out[2 * i + 1] = t[i]! >> 8;
}
}
/** X25519(priv, pub) per RFC 7748: priv is clamped, pub's top bit ignored. */
export function x25519(priv: Uint8Array, pub: Uint8Array): Uint8Array {
checkLen("private key", priv, 32);
checkLen("public key", pub, 32);
const k = priv.slice();
k[0] = k[0]! & 248;
k[31] = (k[31]! & 127) | 64;
const x1 = gf();
for (let i = 0; i < 16; i++) x1[i] = pub[2 * i]! | (pub[2 * i + 1]! << 8);
x1[15] = x1[15]! & 0x7fff;
const x2 = gf([1]), z2 = gf(), x3 = gf(), z3 = gf([1]);
x3.set(x1);
const a = gf(), aa = gf(), b = gf(), bb = gf(), e = gf(), c = gf(), d = gf(), da = gf(), cb = gf();
let swap = 0;
for (let t = 254; t >= 0; t--) {
const kt = (k[t >>> 3]! >>> (t & 7)) & 1;
swap ^= kt;
cswap(x2, x3, swap);
cswap(z2, z3, swap);
swap = kt;
fadd(a, x2, z2);
fmul(aa, a, a);
fsub(b, x2, z2);
fmul(bb, b, b);
fsub(e, aa, bb);
fadd(c, x3, z3);
fsub(d, x3, z3);
fmul(da, d, a);
fmul(cb, c, b);
fadd(x3, da, cb);
fmul(x3, x3, x3);
fsub(z3, da, cb);
fmul(z3, z3, z3);
fmul(z3, z3, x1);
fmul(x2, aa, bb);
fmul(z2, A24, e);
fadd(z2, z2, aa);
fmul(z2, z2, e);
}
cswap(x2, x3, swap);
cswap(z2, z3, swap);
finv(z2, z2);
fmul(x2, x2, z2);
const out = new Uint8Array(32);
pack(out, x2);
return out;
}
const BASEPOINT = new Uint8Array(32);
BASEPOINT[0] = 9;
/** The X25519 public key of priv. */
export function x25519Public(priv: Uint8Array): Uint8Array {
return x25519(priv, BASEPOINT);
}
// --- NaCl box: X25519 + HSalsa20, XSalsa20-Poly1305 ---
const SIGMA_W = [0x61707865, 0x3320646e, 0x79622d32, 0x6b206574];
/**
* The Salsa20 core over key words k (8), input words i0..i3 (nonce and
* counter). hsalsa: HSalsa20's output (8 words, no feed-forward) instead
* of the 64-byte block.
*/
function salsaCore(out: Uint32Array, k: Uint32Array, i0: number, i1: number, i2: number, i3: number, hsalsa: boolean): void {
const j0 = SIGMA_W[0]!, j1 = k[0]!, j2 = k[1]!, j3 = k[2]!, j4 = k[3]!, j5 = SIGMA_W[1]!, j6 = i0, j7 = i1;
const j8 = i2, j9 = i3, j10 = SIGMA_W[2]!, j11 = k[4]!, j12 = k[5]!, j13 = k[6]!, j14 = k[7]!, j15 = SIGMA_W[3]!;
let x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7;
let x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15;
let u: number;
for (let r = 0; r < 10; r++) {
u = (x0 + x12) | 0; x4 ^= (u << 7) | (u >>> 25);
u = (x4 + x0) | 0; x8 ^= (u << 9) | (u >>> 23);
u = (x8 + x4) | 0; x12 ^= (u << 13) | (u >>> 19);
u = (x12 + x8) | 0; x0 ^= (u << 18) | (u >>> 14);
u = (x5 + x1) | 0; x9 ^= (u << 7) | (u >>> 25);
u = (x9 + x5) | 0; x13 ^= (u << 9) | (u >>> 23);
u = (x13 + x9) | 0; x1 ^= (u << 13) | (u >>> 19);
u = (x1 + x13) | 0; x5 ^= (u << 18) | (u >>> 14);
u = (x10 + x6) | 0; x14 ^= (u << 7) | (u >>> 25);
u = (x14 + x10) | 0; x2 ^= (u << 9) | (u >>> 23);
u = (x2 + x14) | 0; x6 ^= (u << 13) | (u >>> 19);
u = (x6 + x2) | 0; x10 ^= (u << 18) | (u >>> 14);
u = (x15 + x11) | 0; x3 ^= (u << 7) | (u >>> 25);
u = (x3 + x15) | 0; x7 ^= (u << 9) | (u >>> 23);
u = (x7 + x3) | 0; x11 ^= (u << 13) | (u >>> 19);
u = (x11 + x7) | 0; x15 ^= (u << 18) | (u >>> 14);
u = (x0 + x3) | 0; x1 ^= (u << 7) | (u >>> 25);
u = (x1 + x0) | 0; x2 ^= (u << 9) | (u >>> 23);
u = (x2 + x1) | 0; x3 ^= (u << 13) | (u >>> 19);
u = (x3 + x2) | 0; x0 ^= (u << 18) | (u >>> 14);
u = (x5 + x4) | 0; x6 ^= (u << 7) | (u >>> 25);
u = (x6 + x5) | 0; x7 ^= (u << 9) | (u >>> 23);
u = (x7 + x6) | 0; x4 ^= (u << 13) | (u >>> 19);
u = (x4 + x7) | 0; x5 ^= (u << 18) | (u >>> 14);
u = (x10 + x9) | 0; x11 ^= (u << 7) | (u >>> 25);
u = (x11 + x10) | 0; x8 ^= (u << 9) | (u >>> 23);
u = (x8 + x11) | 0; x9 ^= (u << 13) | (u >>> 19);
u = (x9 + x8) | 0; x10 ^= (u << 18) | (u >>> 14);
u = (x15 + x14) | 0; x12 ^= (u << 7) | (u >>> 25);
u = (x12 + x15) | 0; x13 ^= (u << 9) | (u >>> 23);
u = (x13 + x12) | 0; x14 ^= (u << 13) | (u >>> 19);
u = (x14 + x13) | 0; x15 ^= (u << 18) | (u >>> 14);
}
if (hsalsa) {
out[0] = x0; out[1] = x5; out[2] = x10; out[3] = x15;
out[4] = x6; out[5] = x7; out[6] = x8; out[7] = x9;
return;
}
out[0] = x0 + j0; out[1] = x1 + j1; out[2] = x2 + j2; out[3] = x3 + j3;
out[4] = x4 + j4; out[5] = x5 + j5; out[6] = x6 + j6; out[7] = x7 + j7;
out[8] = x8 + j8; out[9] = x9 + j9; out[10] = x10 + j10; out[11] = x11 + j11;
out[12] = x12 + j12; out[13] = x13 + j13; out[14] = x14 + j14; out[15] = x15 + j15;
}
function hsalsa20(k: Uint32Array, inp: Uint8Array): Uint32Array {
const out = new Uint32Array(8);
salsaCore(out, k, u32(inp, 0), u32(inp, 4), u32(inp, 8), u32(inp, 12), true);
return out;
}
/**
* XSalsa20 secretbox core: the Poly1305 key and src XORed with the
* keystream from byte 32 on, into dst at dpos.
*/
function xsalsa20(shared: Uint8Array, nonce: Uint8Array, src: Uint8Array, dst: Uint8Array, dpos: number): Uint8Array {
const sub = hsalsa20(keyWords(shared), nonce);
const n0 = u32(nonce, 16), n1 = u32(nonce, 20);
const block = new Uint32Array(16);
const otk = new Uint8Array(32);
const len = src.length;
// Keystream byte s (0-based) XORs message byte s - 32.
for (let s = 0, ctr = 0; s < len + 32; s += 64, ctr++) {
salsaCore(block, sub, n0, n1, ctr >>> 0, Math.floor(ctr / 2 ** 32), false);
for (let i = 0; i < 64; i++) {
const p = s + i;
const kb = (block[i >>> 2]! >>> ((i & 3) << 3)) & 0xff;
if (p < 32) otk[p] = kb;
else if (p - 32 < len) dst[dpos + p - 32] = src[p - 32]! ^ kb;
else break;
}
}
return otk;
}
const ZERO_NONCE16 = new Uint8Array(16);
/** NaCl crypto_box_beforenm: HSalsa20 of the X25519 shared secret. */
export function naclBoxBeforeNm(peerPub: Uint8Array, priv: Uint8Array): Uint8Array {
const s = x25519(priv, peerPub);
const k = hsalsa20(keyWords(s), ZERO_NONCE16);
const out = new Uint8Array(32);
for (let i = 0; i < 8; i++) put32(out, i * 4, k[i]!);
return out;
}
/** XSalsa20-Poly1305 with a precomputed key: 16-byte tag || ciphertext. */
export function naclBoxSealAfterNm(shared: Uint8Array, nonce: Uint8Array, msg: Uint8Array): Uint8Array {
checkLen("key", shared, 32);
checkLen("nonce", nonce, 24);
const out = new Uint8Array(msg.length + 16);
const otk = xsalsa20(shared, nonce, msg, out, 16);
const p = new Poly1305(otk);
p.update(out, 16);
out.set(p.finish(), 0);
return out;
}
export function naclBoxOpenAfterNm(shared: Uint8Array, nonce: Uint8Array, boxed: Uint8Array): Uint8Array | null {
checkLen("key", shared, 32);
checkLen("nonce", nonce, 24);
if (boxed.length < 16) return null;
const ct = boxed.subarray(16);
const out = new Uint8Array(ct.length);
// The Poly1305 key comes first; decrypting into out before the check
// is harmless since out is discarded on failure.
const otk = xsalsa20(shared, nonce, ct, out, 0);
const p = new Poly1305(otk);
p.update(ct);
if (!equalBytes(p.finish(), boxed.subarray(0, 16))) return null;
return out;
}
/** NaCl box (Go's nacl/box.Seal): 16-byte tag || ciphertext. */
export function naclBoxSeal(msg: Uint8Array, nonce: Uint8Array, peerPub: Uint8Array, priv: Uint8Array): Uint8Array {
return naclBoxSealAfterNm(naclBoxBeforeNm(peerPub, priv), nonce, msg);
}
export function naclBoxOpen(boxed: Uint8Array, nonce: Uint8Array, peerPub: Uint8Array, priv: Uint8Array): Uint8Array | null {
return naclBoxOpenAfterNm(naclBoxBeforeNm(peerPub, priv), nonce, boxed);
}