official-vectors.test.ts

1.9 KB · raw

1import { describe, expect, it } from "vitest";2import { blake3 } from "hash-wasm";34import { _testSeam as aesTestSeam } from "../background/aes";56function unhex(value: string): Uint8Array {7  const output = new Uint8Array(value.length / 2);8  for (let index = 0; index < value.length; index += 2) {9    output[index / 2] = Number.parseInt(value.slice(index, index + 2), 16);10  }11  return output;12}1314function hex(value: Uint8Array): string {15  return Array.from(value, (byte) => byte.toString(16).padStart(2, "0")).join("");16}1718describe("official cryptographic vectors", () => {19  // NIST CAVP gcmDecrypt256.rsp, PTlen=128, AADlen=0, Taglen=128, Count=0.20  // Source archive SHA-256 is pinned in docs/CRYPTOGRAPHIC-TEST-VECTORS.md.21  it("decrypts the NIST AES-256-GCM CAVP vector and rejects a bad tag", async () => {22    const key = unhex(23      "4c8ebfe1444ec1b2d503c6986659af2c94fafe945f72c1e8486a5acfedb8a0f8",24    );25    const nonce = "473360e0ad24889959858995";26    const ciphertext = "d2c78110ac7e8f107c0df0570bd7c90c";27    const tag = "c26a379b6d98ef2852ead8ce83a833a7";2829    const plaintext = await aesTestSeam.decryptWithAadBytes(30      key,31      nonce,32      ciphertext,33      tag,34      new Uint8Array(),35    );36    expect(hex(plaintext)).toBe("7789b41cb3ee548814ca0b388c10b343");3738    await expect(39      aesTestSeam.decryptWithAadBytes(40        key,41        nonce,42        ciphertext,43        `${tag.slice(0, -2)}a6`,44        new Uint8Array(),45      ),46    ).rejects.toThrow();47  });4849  // BLAKE3 official test_vectors.json, input_len=1024. The reference input is50  // byte i % 251 and the first 32 output bytes are the production digest size.51  it("matches the BLAKE3 reference vector across a chunk boundary", async () => {52    const input = Uint8Array.from({ length: 1024 }, (_, index) => index % 251);53    expect(await blake3(input)).toBe(54      "42214739f095a406f3fc83deb889744ac00df831c10daa55189b5d121c855af7",55    );56  });57});