Files
web-terminal/relay-e2e/test/hkdf.test.ts
Yaojia Wang 2af57e6686 feat(relay): rendezvous-relay service — 7 packages + plans (contracts/transport/agent/control-plane/e2e/auth/web)
Multi-tenant reverse-tunnel service ("ngrok for Claude Code" with E2E): a
host-agent dials OUT to an operator-run relay; external devices reach the host
THROUGH the relay, routed by per-tenant subdomain, forwarding ciphertext only
(the relay never sees plaintext). Lets a customer reach their own self-hosted
web-terminal from anywhere with zero networking setup.

Packages — all tsc-strict + vitest green (656 tests), cross-package integration verified:
- relay-contracts: frozen shared contracts (mux frame codec, data model,
  capability token, E2E envelope, pairing) — the src/types.ts analog
- term-relay:   native WS mux + stateless data plane (subdomain routing, ciphertext forward)
- agent:        host-agent (pairing, per-host Ed25519 + mTLS dial-out, forwards to 127.0.0.1:3000)
- control-plane: accounts/hosts registry, pairing-code flow, routing table, provisioning
- relay-e2e:    browser<->agent E2E (X25519 ECDH through relay, AEAD, anti-replay, recoverable replay key)
- relay-auth:   Passkey/WebAuthn, capability tokens, per-host certs, deny-by-default tenant isolation
- relay-web:    browser login + Web Crypto E2E + client-side preview rendering

Security invariants INV1-15 enforced; cross-tenant isolation CI tripwire live
(.github/workflows/relay-tripwire.yml). Design + implementation-level plans in
docs/PLAN_RELAY_*.md and docs/EXPLORE_RELAY_SERVICE.md.

NOTE: generated autonomously per the reviewed plans. The security-critical
packages (relay-e2e, relay-auth) REQUIRE expert security audit before any real
deployment — passing tests prove self-consistency, not resistance to attackers.
Base app (src/, public/) unchanged; concurrent desktop work left uncommitted.
2026-07-02 06:10:16 +02:00

52 lines
2.6 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { unwrapAeadKey } from '../src/aead-key.js'
import { deriveMaster, deriveSessionKeys } from '../src/hkdf.js'
import { bytesToHex, hexToBytes } from './helpers.js'
import hkdfVector from './vectors/hkdf.json' with { type: 'json' }
const shared = hexToBytes(hkdfVector.sharedSecret)
const cn = hexToBytes(hkdfVector.clientNonce)
const hn = hexToBytes(hkdfVector.hostNonce)
describe('T6 hkdf direction-split', () => {
it('KAT: reproduces the frozen master + both direction subkeys', async () => {
expect(bytesToHex(deriveMaster(shared, cn, hn))).toBe(hkdfVector.master)
const dk = await deriveSessionKeys(shared, cn, hn, 'aes-256-gcm')
expect(bytesToHex(unwrapAeadKey(dk.c2h).raw)).toBe(hkdfVector.c2h)
expect(bytesToHex(unwrapAeadKey(dk.h2c).raw)).toBe(hkdfVector.h2c)
})
it('deterministic: same inputs → same keys', async () => {
const a = await deriveSessionKeys(shared, cn, hn, 'aes-256-gcm')
const b = await deriveSessionKeys(shared, cn, hn, 'aes-256-gcm')
expect(bytesToHex(unwrapAeadKey(a.c2h).raw)).toBe(bytesToHex(unwrapAeadKey(b.c2h).raw))
})
it('changing EITHER nonce changes the keys (salt = both nonces)', async () => {
const base = await deriveSessionKeys(shared, cn, hn, 'aes-256-gcm')
const altC = await deriveSessionKeys(shared, hexToBytes(hkdfVector.hostNonce), hn, 'aes-256-gcm')
const altH = await deriveSessionKeys(shared, cn, hexToBytes(hkdfVector.clientNonce), 'aes-256-gcm')
expect(bytesToHex(unwrapAeadKey(base.c2h).raw)).not.toBe(bytesToHex(unwrapAeadKey(altC.c2h).raw))
expect(bytesToHex(unwrapAeadKey(base.c2h).raw)).not.toBe(bytesToHex(unwrapAeadKey(altH.c2h).raw))
})
it('SECURITY direction disjointness: c2h !== h2c, each 32 bytes, neither equals the master', async () => {
const dk = await deriveSessionKeys(shared, cn, hn, 'aes-256-gcm')
const c2h = unwrapAeadKey(dk.c2h).raw
const h2c = unwrapAeadKey(dk.h2c).raw
const master = deriveMaster(shared, cn, hn)
expect(c2h.length).toBe(32)
expect(h2c.length).toBe(32)
expect(bytesToHex(c2h)).not.toBe(bytesToHex(h2c))
expect(bytesToHex(c2h)).not.toBe(bytesToHex(master))
expect(bytesToHex(h2c)).not.toBe(bytesToHex(master))
})
it('subkeys carry their direction label so seal binds direction into aad', async () => {
const dk = await deriveSessionKeys(shared, cn, hn, 'xchacha20-poly1305')
expect(unwrapAeadKey(dk.c2h).aadLabel).toBe('c2h')
expect(unwrapAeadKey(dk.h2c).aadLabel).toBe('h2c')
expect(unwrapAeadKey(dk.c2h).alg).toBe('xchacha20-poly1305')
})
})