mesh/merge-gate head 1bc5719b8b85 merged into trunk a2d2af00dc5d beside mesh-host#116@bff1ac14 — pass, with a note: builds mesh-sdk (+40 dependent(s)) → ac…
mesh/repo-check head 1bc5719b8b85 merged into trunk a2d2af00dc5d beside mesh-host#116@bff1ac14 — pass: its merge-check.sh passed, each part in its toolchai…
mesh/delivery delivered
mesh/delivery-group group release/57-config-and-provisioning delivered: every member is delivered
72 lines
3.0 KiB
TypeScript
72 lines
3.0 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import { createPrivateKey } from "node:crypto";
|
|
|
|
import { seal, open, generateKeyPair, type SealedBox } from "../dist/seal/index.js";
|
|
import { sealWith } from "../dist/seal/box.js";
|
|
|
|
// The fixtures the Go package proves itself against: boxes the licence manager's own seal.go made, and
|
|
// the one the agent module's TypeScript made. One format, three implementations.
|
|
const fixtures = new URL("../go/seal/testdata/", import.meta.url);
|
|
const lm = JSON.parse(readFileSync(new URL("sealed-by-licence-manager.json", fixtures), "utf8")) as {
|
|
recipient: { publicKey: string; privateKey: string };
|
|
sealed: { plaintext: string; box: SealedBox }[];
|
|
vectors: { plaintext: string; ephPrivate: string; iv: string; box: SealedBox }[];
|
|
};
|
|
|
|
// PKCS #8 for an X25519 private key is this prefix and the 32 raw bytes.
|
|
const pkcs8 = (raw: Buffer) =>
|
|
createPrivateKey({ key: Buffer.concat([Buffer.from("302e020100300506032b656e04220420", "hex"), raw]), format: "der", type: "pkcs8" });
|
|
|
|
test("seal: every box the licence manager sealed opens to its plaintext", () => {
|
|
assert.ok(lm.sealed.length > 0);
|
|
for (const s of lm.sealed) assert.equal(open(s.box, lm.recipient.privateKey), s.plaintext);
|
|
});
|
|
|
|
test("seal: given the same one-time key and iv, the box is byte for byte the licence manager's", () => {
|
|
assert.ok(lm.vectors.length > 0);
|
|
for (const v of lm.vectors) {
|
|
const box = sealWith(v.plaintext, lm.recipient.publicKey, pkcs8(Buffer.from(v.ephPrivate, "base64")), Buffer.from(v.iv, "base64"));
|
|
assert.deepEqual(box, v.box);
|
|
assert.equal(open(box, lm.recipient.privateKey), v.plaintext);
|
|
}
|
|
});
|
|
|
|
test("seal: the agent module's TypeScript box opens", () => {
|
|
const f = JSON.parse(readFileSync(new URL("sealed-by-typescript.json", fixtures), "utf8"));
|
|
assert.equal(open(f.box, f.privateKey), f.plaintext);
|
|
});
|
|
|
|
test("seal: a box opens for its recipient only, and two boxes share no one-time key or iv", () => {
|
|
const a = generateKeyPair();
|
|
const b = generateKeyPair();
|
|
const box = seal("a token", a.publicKey);
|
|
assert.equal(open(box, a.privateKey), "a token");
|
|
assert.throws(() => open(box, b.privateKey));
|
|
const again = seal("a token", a.publicKey);
|
|
assert.notEqual(again.eph, box.eph);
|
|
assert.notEqual(again.iv, box.iv);
|
|
});
|
|
|
|
test("seal: a box changed in any field, or of another version, does not open", () => {
|
|
const good = lm.sealed[1].box;
|
|
const flip = (s: string) => {
|
|
const b = Buffer.from(s, "base64");
|
|
b[b.length - 1] ^= 1;
|
|
return b.toString("base64");
|
|
};
|
|
for (const box of [
|
|
{ ...good, ct: flip(good.ct) },
|
|
{ ...good, tag: flip(good.tag) },
|
|
{ ...good, iv: flip(good.iv) },
|
|
{ ...good, eph: flip(good.eph) },
|
|
{ ...good, v: 2 },
|
|
]) assert.throws(() => open(box as SealedBox, lm.recipient.privateKey));
|
|
});
|
|
|
|
test("seal: a key that is not X25519 PEM is refused", () => {
|
|
assert.throws(() => seal("x", "not a key"));
|
|
assert.throws(() => open(lm.sealed[0].box, lm.recipient.publicKey));
|
|
});
|