UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

95 lines • 4.52 kB
import { createHash, generateKeyPair } from "node:crypto"; function b64url(bytes) { return Buffer.from(bytes).toString("base64url"); } function sha256b64url(text) { return createHash("sha256").update(text, "utf8").digest("base64url"); } function is32ByteB64url(value) { return /^[A-Za-z0-9_-]{43}$/.test(value) && Buffer.from(value, "base64url").length === 32; } /** `b64url(SHA-256(UTF-8(JSON.stringify([title, placeholder ?? null]))))`. */ function secretDisplayHash(title, placeholder) { return sha256b64url(JSON.stringify([title, placeholder ?? null])); } /** Builds `B`; refuses a deadline that is not a safe integer and any key or hash that is not 32 base64url bytes. */ export function secretBinding(input) { const displayHash = secretDisplayHash(input.title, input.placeholder); if (!Number.isSafeInteger(input.deadlineAtMs) || ![input.hostJkt, input.hpkePk].every(is32ByteB64url)) throw new Error("secret_binding_malformed"); return [ "omo-secret-binding-v1", input.hostInstance, input.hostJkt, input.durableSessionId, input.uiRequestId, input.deadlineAtMs, input.hpkePk, displayHash, ]; } /** `req = b64url(SHA-256(UTF-8(JSON.stringify(B))))`: names one request everywhere an answer is bound to it. */ export function secretRequestHash(binding) { return sha256b64url(JSON.stringify(binding)); } /** HPKE `info`: `UTF-8("omo-secret-answer-v1") || 0x00 || req` (its 32 bytes). */ export function secretAnswerInfo(req) { return Buffer.concat([Buffer.from("omo-secret-answer-v1"), Buffer.from([0]), Buffer.from(req, "base64url")]); } /** HPKE AAD: `UTF-8(JSON.stringify(["omo-secret-answer-aad-v1", req, principalId, grantId, answerId, stepupJti]))`. */ export function secretAnswerAad(req, principalId, grantId, answerId, jti) { return Buffer.from(JSON.stringify(["omo-secret-answer-aad-v1", req, principalId, grantId, answerId, jti])); } /** What the installation key signs for a value answer; the ciphertext enters as `b64url(SHA-256(enc || ct))`. */ export function secretAnswerTranscript(ids, jti, enc, ct) { const sealed = createHash("sha256").update(enc).update(ct).digest("base64url"); return `omo-secret-answer-v1\n${JSON.stringify([ids.req, ids.principalId, ids.grantId, ids.answerId, jti, sealed])}`; } /** What the installation key signs for a cancel. */ export function secretCancelTranscript(ids) { return `omo-secret-answer-v1\n${JSON.stringify([ids.req, ids.principalId, ids.grantId, ids.answerId, "cancel"])}`; } /** * The canonical plaintext only: `u32be(byteLength) || UTF-8(value) || zeros`, exactly the next power of two of at * least 256 and at most 64 KiB, all-zero padding and valid UTF-8 (a leading BOM is kept). Anything else is * `undefined`. */ export function unpadSecretValue(plaintext) { if (plaintext.length < 256 || plaintext.length > 65536) return undefined; // A view, not a copy, so the caller's zeroing of `plaintext` reaches every byte (design 8.2). const bytes = Buffer.from(plaintext.buffer, plaintext.byteOffset, plaintext.byteLength); const length = bytes.readUInt32BE(0); let size = 256; while (size < 4 + length) size *= 2; if (size !== bytes.length || bytes.subarray(4 + length).some((byte) => byte !== 0)) return undefined; try { return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes.subarray(4, 4 + length)); } catch { return undefined; } } /** The host key signs exactly this: `"omo-secret-request-v1\n" + JSON.stringify(B)`. */ export function secretRequestTranscript(binding) { return `omo-secret-request-v1\n${JSON.stringify(binding)}`; } /** * A fresh request key, held only by the pending request that owns the latch (design 4.1). Generated off the host loop: * it is on the session path, which holds no new synchronous crypto (no-sync-in-session-path, senpi#3993). */ export function generateSecretRequestKey() { return new Promise((resolve, reject) => { generateKeyPair("x25519", (error, publicKey, privateKey) => { if (error) return reject(error); const { x } = publicKey.export({ format: "jwk" }); if (typeof x !== "string") return reject(new Error("secret_request_key_unavailable")); resolve({ hpkePk: b64url(Buffer.from(x, "base64url")), privateKey }); }); }); } //# sourceMappingURL=secret-sealed.js.map