101 lines
4.2 KiB
TypeScript
101 lines
4.2 KiB
TypeScript
|
|
/**
|
||
|
|
* The vault-v2 cross-language vectors, checked against this implementation.
|
||
|
|
*
|
||
|
|
* The Kotlin port is written against `test-vectors/vault-v2.json`; this test
|
||
|
|
* is what stops the file and the TypeScript code from drifting apart, so a
|
||
|
|
* port that matches the vectors also matches the client that wrote them.
|
||
|
|
* Regenerate with `bun run scripts/generate-vectors.ts vault-v2.json`.
|
||
|
|
*/
|
||
|
|
import { describe, test, expect } from 'bun:test';
|
||
|
|
import { readFileSync } from 'node:fs';
|
||
|
|
import { join } from 'node:path';
|
||
|
|
import { SubtleCryptoProvider } from '@shade/crypto-web';
|
||
|
|
import { canonicalizePayload } from '@shade/server';
|
||
|
|
import { deriveVaultKeys } from '../src/client.js';
|
||
|
|
import {
|
||
|
|
decodeManifest,
|
||
|
|
encodeManifest,
|
||
|
|
openEntryBytes,
|
||
|
|
openManifest,
|
||
|
|
packEntry,
|
||
|
|
toHex,
|
||
|
|
vaultObjectAad,
|
||
|
|
} from '../src/crypto.js';
|
||
|
|
import { sealEntryWithNonces, sealObjectWithNonce } from '../src/testing.js';
|
||
|
|
import type { VaultEntry } from '../src/types.js';
|
||
|
|
|
||
|
|
const crypto = new SubtleCryptoProvider();
|
||
|
|
const file = JSON.parse(
|
||
|
|
readFileSync(join(import.meta.dir, '..', '..', '..', 'test-vectors', 'vault-v2.json'), 'utf-8'),
|
||
|
|
) as { version: number; vectors: any[] };
|
||
|
|
|
||
|
|
function fromHex(s: string): Uint8Array {
|
||
|
|
const out = new Uint8Array(s.length / 2);
|
||
|
|
for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16);
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
const byPrefix = (p: string) => file.vectors.filter((v) => v.description.startsWith(p));
|
||
|
|
|
||
|
|
describe('vault-v2 vectors', async () => {
|
||
|
|
const k = byPrefix('vault-v2 keys')[0];
|
||
|
|
const keys = await deriveVaultKeys(fromHex(k.masterKey), k.app);
|
||
|
|
|
||
|
|
test('the file is schema version 2 and has every kind', () => {
|
||
|
|
expect(file.version).toBe(2);
|
||
|
|
expect(byPrefix('vault-v2 entry').length).toBe(3);
|
||
|
|
expect(byPrefix('vault-v2 manifest').length).toBe(2);
|
||
|
|
expect(byPrefix('vault-v2 signed').length).toBe(2);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('key derivation and AAD', () => {
|
||
|
|
expect(keys.vaultId).toBe(k.vaultId);
|
||
|
|
expect(toHex(keys.contentKey)).toBe(k.contentKey);
|
||
|
|
expect(toHex(keys.signingSeed)).toBe(k.signingSeed);
|
||
|
|
expect(toHex(keys.publicKey)).toBe(k.publicKey);
|
||
|
|
expect(toHex(vaultObjectAad(keys.vaultId))).toBe(k.objectAad);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('entries: readers reassemble, writers reproduce every byte', async () => {
|
||
|
|
for (const v of byPrefix('vault-v2 entry')) {
|
||
|
|
const entry = v.entry as VaultEntry;
|
||
|
|
const wires = v.parts.map((p: any) => fromHex(p.wire));
|
||
|
|
const plain = await openEntryBytes(crypto, keys.contentKey, keys.vaultId, entry, wires);
|
||
|
|
expect(toHex(plain)).toBe(v.plaintext);
|
||
|
|
|
||
|
|
const packed = await packEntry(plain, { chunkSize: v.chunkSize });
|
||
|
|
expect(toHex(packed.stored)).toBe(v.stored);
|
||
|
|
expect(packed.enc ?? null).toBe(v.enc);
|
||
|
|
|
||
|
|
const resealed = await sealEntryWithNonces(
|
||
|
|
keys.contentKey,
|
||
|
|
keys.vaultId,
|
||
|
|
plain,
|
||
|
|
(i) => fromHex(v.parts[i].nonce),
|
||
|
|
{ chunkSize: v.chunkSize },
|
||
|
|
);
|
||
|
|
expect(resealed.objects.map((o) => toHex(o.bytes))).toEqual(v.parts.map((p: any) => p.wire));
|
||
|
|
expect({ path: entry.path, ...resealed.locator, size: plain.length, ...(entry.mode ? { mode: entry.mode } : {}) }).toEqual(entry);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
test('manifests: v2 and v1 both open; encoding reproduces the plaintext', async () => {
|
||
|
|
for (const v of byPrefix('vault-v2 manifest')) {
|
||
|
|
const opened = await openManifest(crypto, keys.contentKey, keys.vaultId, v.hash, fromHex(v.wire));
|
||
|
|
expect(opened).toEqual(v.manifest);
|
||
|
|
expect(await decodeManifest(fromHex(v.plaintext))).toEqual(v.manifest);
|
||
|
|
expect(toHex(await encodeManifest(v.manifest))).toBe(v.plaintext);
|
||
|
|
const sealed = await sealObjectWithNonce(keys.contentKey, keys.vaultId, fromHex(v.plaintext), fromHex(v.nonce));
|
||
|
|
expect(sealed.hash).toBe(v.hash);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
test('signed bodies: canonical bytes and a deterministic Ed25519 signature', async () => {
|
||
|
|
for (const v of byPrefix('vault-v2 signed')) {
|
||
|
|
const canonical = canonicalizePayload({ ...v.body, signedAt: v.signedAt, signature: '' });
|
||
|
|
expect(toHex(canonical)).toBe(v.canonicalHex);
|
||
|
|
expect(toHex(await crypto.sign(keys.signingSeed, canonical))).toBe(v.signature);
|
||
|
|
expect(await crypto.verify(keys.publicKey, canonical, fromHex(v.signature))).toBe(true);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|