S15 TypeScript seals: seal, open and sealAnonymous beside go/seal
The forge module's minting processes are TypeScript and the vault's two steps need ADR 0206's sealed box and the NaCl anonymous box its intake opens; both now live once in mesh-sdk, proven byte for byte against the Go package's bytes (tweetnacl 1.0.3 and blakejs 1.2.1 pinned). Builds: design 47 H2 §808, §822, H-A-407 and its addendum
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { seal, open, sealWith, sealAnonymous, sealAnonymousWith, generateKeyPair, type Box } from "../dist/seal/index.js";
|
||||
|
||||
// The seals are proven against the Go package's bytes (design 47 H2, H-A-407): the fixtures go/seal's tests make and
|
||||
// check on every run.
|
||||
const testdata = join(import.meta.dirname, "..", "go", "seal", "testdata");
|
||||
const fixture = (name: string) => JSON.parse(readFileSync(join(testdata, name), "utf8"));
|
||||
|
||||
// ADR 0206's box: every box Go's seal opens (the licence manager's, the agent module's TypeScript) opens here; given
|
||||
// the same one-time key and iv, seal makes Go's box byte for byte, so go/seal's Open opens it; another key and a
|
||||
// changed field are refused.
|
||||
test("seal and open make and open go/seal's box byte for byte", () => {
|
||||
const f = fixture("sealed-by-licence-manager.json");
|
||||
for (const s of f.sealed as { plaintext: string; box: Box }[]) {
|
||||
assert.equal(open(s.box, f.recipient.privateKey), s.plaintext);
|
||||
}
|
||||
for (const v of f.vectors as { plaintext: string; ephPrivate: string; iv: string; box: Box }[]) {
|
||||
const got = sealWith(v.plaintext, f.recipient.publicKey, Buffer.from(v.ephPrivate, "base64"), Buffer.from(v.iv, "base64"));
|
||||
assert.deepEqual(got, v.box);
|
||||
}
|
||||
const ts = fixture("sealed-by-typescript.json");
|
||||
assert.equal(open(ts.box, ts.privateKey), ts.plaintext);
|
||||
|
||||
const a = generateKeyPair();
|
||||
const b = generateKeyPair();
|
||||
const box = seal("a token", a.publicKey);
|
||||
assert.equal(open(box, a.privateKey), "a token");
|
||||
assert.notDeepEqual(seal("a token", a.publicKey), box);
|
||||
assert.throws(() => open(box, b.privateKey), /does not open with this key/);
|
||||
assert.throws(() => open({ ...box, ct: Buffer.from("changed").toString("base64") }, a.privateKey), /does not open/);
|
||||
assert.throws(() => open({ ...box, v: 2 }, a.privateKey), /version 2/);
|
||||
});
|
||||
|
||||
// The NaCl anonymous box the vault's intake opens: given the same one-time key, sealAnonymous makes what
|
||||
// golang.org/x/crypto/nacl/box's SealAnonymous made, byte for byte; a fresh one differs and is as long.
|
||||
test("sealAnonymous makes Go's anonymous box byte for byte", () => {
|
||||
const f = fixture("sealed-anonymous-by-go.json");
|
||||
assert.ok(f.vectors.length > 0);
|
||||
for (const v of f.vectors as { plaintext: string; ephSecret: string; sealed: string }[]) {
|
||||
assert.equal(sealAnonymousWith(v.plaintext, f.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
||||
assert.equal(sealAnonymousWith(Buffer.from(v.plaintext, "utf8"), f.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
||||
}
|
||||
const fresh = sealAnonymous("a token", f.recipient.public);
|
||||
assert.notEqual(fresh, sealAnonymous("a token", f.recipient.public));
|
||||
assert.equal(Buffer.from(fresh, "base64").length, 32 + 16 + "a token".length);
|
||||
assert.throws(() => sealAnonymous("x", "c2hvcnQ="), /not a raw X25519 public key/);
|
||||
});
|
||||
Reference in New Issue
Block a user