self-test.test.ts

1.2 KB · raw

1import { describe, expect, test, vi } from "vitest";2import { runSelfTest } from "./self-test";34describe("Peach crypto KAT (self-test)", () => {5  test("passes with the correct crypto stack", async () => {6    const result = await runSelfTest();7    expect(result.ok).toBe(true);8    expect(result.error).toBeUndefined();9  });1011  // PCH-01-020: a real KAT must reject a ciphertext that diverges from the12  // pinned vector, not merely a failed round-trip. Simulate a WebCrypto whose13  // AES-GCM output differs from the committed expectation.14  test("fails when the produced ciphertext diverges from the pinned vector", async () => {15    const realEncrypt = crypto.subtle.encrypt.bind(crypto.subtle);16    const spy = vi17      .spyOn(crypto.subtle, "encrypt")18      .mockImplementation(async (...args: Parameters<typeof realEncrypt>) => {19        const ct = new Uint8Array(await realEncrypt(...args));20        ct[0] ^= 0xff; // diverge from the pinned vector21        return ct.buffer;22      });2324    const result = await runSelfTest();25    expect(result.ok).toBe(false);26    expect(result.error).toContain("does not match the pinned test vector");2728    spy.mockRestore();29  });30});