Repository Public
1import test from "node:test";2import assert from "node:assert/strict";3import { readFileSync, existsSync } from "node:fs";4import * as P from "../src/index.ts";56for (const file of ["spv1.json", "spv1-swift.json"]) {7 const url = new URL("../../crypto-test-vectors/" + file, import.meta.url);8 if (!existsSync(url)) continue;9 const v = JSON.parse(readFileSync(url, "utf8"));10 const { accountId, deviceId } = v.ids;11 const u = P.unb64u;12 test(`${file}: contexts and HKDF`, async () => {13 for (const c of v.ctx) assert.equal(P.hex(P.ctx(c.label, ...c.fields)), c.hex);14 assert.equal(P.b64u(await P.hkdf(u(v.hkdf.ikm), u(v.hkdf.salt), u(v.hkdf.info))), v.hkdf.out);15 });16 test(`${file}: keyset, device certificate, ARK envelope`, async () => {17 const account = await P.openKeyset(u(v.secrets.ark), accountId, v.keyset);18 assert.equal(P.b64u(account.sig.pub), v.account.sigPub);19 assert.equal(P.b64u(account.enc.pub), v.account.encPub);20 assert.equal(await P.accountFingerprint(account.sig.pub, account.enc.pub), v.account.fingerprint);21 assert.ok(await P.verifyDeviceCert(u(v.account.sigPub), accountId, v.device));22 assert.ok(!(await P.verifyDeviceCert(u(v.account.sigPub), accountId, { ...v.device, deviceId: accountId })));23 const dev = { enc: await P.importPrivateX963(u(v.device.encPriv), "kem"), sig: await P.importPrivateX963(u(v.device.sigPriv), "sig") };24 assert.equal(P.b64u(await P.unwrapArk(dev, accountId, deviceId, v.device.arkEnvelope)), v.secrets.ark);25 await assert.rejects(P.unwrapArk(dev, accountId, accountId, v.device.arkEnvelope), /authentication failed/);26 });27 test(`${file}: recovery`, async () => {28 const rk = await P.decodeRecoveryKey(v.recovery.text);29 assert.equal(P.b64u(rk), v.secrets.rk);30 assert.equal(await P.encodeRecoveryKey(rk), v.recovery.text);31 assert.equal(await P.recoveryAuthHash(rk, accountId), v.recovery.authHash);32 assert.equal(P.b64u(await P.openRecoveryEnvelope(rk, accountId, v.recovery.epoch, v.recovery.envelope)), v.secrets.ark);33 await assert.rejects(P.openRecoveryEnvelope(rk, accountId, v.recovery.epoch + 1, v.recovery.envelope));34 });35 test(`${file}: record, grant, chunk, pairing, relay, link, totp`, async () => {36 const vk = u(v.secrets.vk);37 assert.deepEqual(await P.openRecord(() => vk, v.record.ref, v.record.envelope), v.record.plaintext);38 const account = await P.openKeyset(u(v.secrets.ark), accountId, v.keyset);39 assert.equal(P.b64u(await P.acceptVaultGrant(account, v.grant, u(v.account.sigPub))), v.secrets.vk);40 assert.equal(P.b64u(await P.openChunk(u(v.chunk.key), u(v.chunk.prefix), v.chunk.id, v.chunk.index, v.chunk.total, u(v.chunk.sealed))), v.chunk.plaintext);41 assert.equal(await P.pairingTag(u(v.pairing.secret), v.pairing.pairingId, v.device.encPub, v.device.sigPub), v.pairing.tag);42 assert.equal(await P.pairingSas(v.pairing.pairingId, v.device.encPub, v.device.sigPub), v.pairing.sas);43 assert.equal(P.decodePairingCode(v.pairing.code).pairingId, v.pairing.pairingId);44 assert.deepEqual(await P.openRelay(account, v.relay.shareId, accountId, accountId, u(v.account.sigPub), v.relay.box), v.relay.plaintext);45 assert.deepEqual(await P.openLink(u(v.link.secret), v.link.shareId, v.link.box), v.link.plaintext);46 assert.equal((await P.linkKeys(u(v.link.secret), v.link.shareId)).authHash, v.link.authHash);47 for (const t of v.totp) assert.equal((await P.totp({ secret: u(t.secret), algorithm: t.algorithm, digits: t.digits, period: t.period }, t.atMs)).code, t.code);48 });49}50