mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 03:39:26 +08:00
The guest joins a tailnet from Node or the page: ts2021 control over WebSocket, DERP relays, WireGuard and the packet filter in JS, loaded as a separate chunk only when the option is used. SDK net option, CLI --tailscale* flags and a page panel. Relay only; exit nodes, IPv6 and real peer traffic are not covered yet.
422 lines
17 KiB
TypeScript
422 lines
17 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Unit tests for the Tailscale client's crypto primitives
|
|
// (core/tailscale/crypto.ts): published vectors, then randomized
|
|
// cross-checks against node:crypto for everything it implements.
|
|
//
|
|
// node --test test/tailscale-crypto.test.ts
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import * as nc from "node:crypto";
|
|
import {
|
|
blake2s,
|
|
chacha20poly1305Open,
|
|
chacha20poly1305Seal,
|
|
concatBytes,
|
|
equalBytes,
|
|
hkdfBlake2s,
|
|
hmacBlake2s,
|
|
naclBoxBeforeNm,
|
|
naclBoxOpen,
|
|
naclBoxOpenAfterNm,
|
|
naclBoxSeal,
|
|
naclBoxSealAfterNm,
|
|
randomBytes,
|
|
x25519,
|
|
x25519Public,
|
|
xchacha20poly1305Open,
|
|
xchacha20poly1305Seal,
|
|
} from "../js/core/tailscale/crypto.ts";
|
|
|
|
const hex = (s: string): Uint8Array => new Uint8Array(Buffer.from(s.replace(/\s+/g, ""), "hex"));
|
|
const toHex = (b: Uint8Array): string => Buffer.from(b).toString("hex");
|
|
const bytes = (b: Uint8Array): Uint8Array => new Uint8Array(b);
|
|
|
|
const LENGTHS = [0, 1, 2, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 1300, 1420, 4096, 65535];
|
|
|
|
// --- X25519 ---
|
|
|
|
test("x25519: RFC 7748 section 5.2 vectors", () => {
|
|
assert.equal(
|
|
toHex(
|
|
x25519(
|
|
hex("a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4"),
|
|
hex("e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c"),
|
|
),
|
|
),
|
|
"c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552",
|
|
);
|
|
assert.equal(
|
|
toHex(
|
|
x25519(
|
|
hex("4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d"),
|
|
hex("e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493"),
|
|
),
|
|
),
|
|
"95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957",
|
|
);
|
|
});
|
|
|
|
test("x25519: RFC 7748 iterated vector (1 and 1000 rounds)", () => {
|
|
let k: Uint8Array = new Uint8Array(32);
|
|
k[0] = 9;
|
|
let u: Uint8Array = k.slice();
|
|
for (let i = 1; i <= 1000; i++) {
|
|
const r = x25519(k, u);
|
|
u = k;
|
|
k = r;
|
|
if (i === 1) assert.equal(toHex(k), "422c8e7a6227d7bca1350b3e2bb7279f7897b87bb6854b783c60e80311ae3079");
|
|
}
|
|
assert.equal(toHex(k), "684cf59ba83309552800ef566f2f4d3c1c3887c49360e3875f2eb94d99532c51");
|
|
});
|
|
|
|
test("x25519: RFC 7748 section 6.1 Diffie-Hellman", () => {
|
|
const a = hex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
|
const b = hex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
|
const aPub = x25519Public(a);
|
|
const bPub = x25519Public(b);
|
|
assert.equal(toHex(aPub), "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
|
|
assert.equal(toHex(bPub), "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
|
const shared = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742";
|
|
assert.equal(toHex(x25519(a, bPub)), shared);
|
|
assert.equal(toHex(x25519(b, aPub)), shared);
|
|
});
|
|
|
|
const PKCS8_X25519 = hex("302e020100300506032b656e04220420");
|
|
const SPKI_X25519 = hex("302a300506032b656e032100");
|
|
const nodePriv = (k: Uint8Array) => nc.createPrivateKey({ key: Buffer.from(concatBytes(PKCS8_X25519, k)), format: "der", type: "pkcs8" });
|
|
const nodePub = (k: Uint8Array) => nc.createPublicKey({ key: Buffer.from(concatBytes(SPKI_X25519, k)), format: "der", type: "spki" });
|
|
|
|
test("x25519: random cross-check against node:crypto", () => {
|
|
for (let i = 0; i < 64; i++) {
|
|
const a = randomBytes(32);
|
|
const b = randomBytes(32);
|
|
const aIn = a.slice();
|
|
const aPub = x25519Public(a);
|
|
const aPubNode = nc.createPublicKey(nodePriv(a)).export({ format: "der", type: "spki" }).subarray(12);
|
|
assert.equal(toHex(aPub), toHex(aPubNode));
|
|
const bPub = x25519Public(b);
|
|
const s = x25519(a, bPub);
|
|
const sNode = nc.diffieHellman({ privateKey: nodePriv(a), publicKey: nodePub(bPub) });
|
|
assert.equal(toHex(s), toHex(sNode));
|
|
assert.equal(toHex(x25519(b, aPub)), toHex(s));
|
|
assert.ok(equalBytes(a, aIn), "x25519 must not modify the private key");
|
|
}
|
|
});
|
|
|
|
test("x25519: non-canonical and high-bit public keys", () => {
|
|
// The top bit of u is ignored; u >= p is reduced (RFC 7748 section 5).
|
|
const k = randomBytes(32);
|
|
const u = randomBytes(32);
|
|
const hi = u.slice();
|
|
hi[31] = hi[31]! | 0x80;
|
|
const lo = u.slice();
|
|
lo[31] = lo[31]! & 0x7f;
|
|
assert.equal(toHex(x25519(k, hi)), toHex(x25519(k, lo)));
|
|
// p + 9 is 9.
|
|
const p9 = hex("f6ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f");
|
|
assert.equal(toHex(x25519(k, p9)), toHex(x25519Public(k)));
|
|
// Low-order points give all zeros.
|
|
assert.equal(toHex(x25519(k, new Uint8Array(32))), "00".repeat(32));
|
|
});
|
|
|
|
// --- ChaCha20-Poly1305 ---
|
|
|
|
const LADIES = new TextEncoder().encode(
|
|
"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.",
|
|
);
|
|
|
|
test("chacha20poly1305: RFC 8439 section 2.8.2 vector", () => {
|
|
const key = hex("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f");
|
|
const nonce = hex("070000004041424344454647");
|
|
const aad = hex("50515253c0c1c2c3c4c5c6c7");
|
|
const sealed = chacha20poly1305Seal(key, nonce, LADIES, aad);
|
|
assert.equal(
|
|
toHex(sealed),
|
|
"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116" +
|
|
"1ae10b594f09e26a7e902ecbd0600691",
|
|
);
|
|
assert.deepEqual(chacha20poly1305Open(key, nonce, sealed, aad), LADIES);
|
|
});
|
|
|
|
function nodeSeal(key: Uint8Array, nonce: Uint8Array, pt: Uint8Array, aad?: Uint8Array): Uint8Array {
|
|
const c = nc.createCipheriv("chacha20-poly1305", key, nonce, { authTagLength: 16 });
|
|
if (aad) c.setAAD(aad, { plaintextLength: pt.length });
|
|
return concatBytes(c.update(pt), c.final(), c.getAuthTag());
|
|
}
|
|
|
|
test("chacha20poly1305: random lengths against node:crypto", () => {
|
|
for (let i = 0; i < 500; i++) {
|
|
const len = Math.floor(Math.random() * 3000);
|
|
const key = randomBytes(32);
|
|
const nonce = randomBytes(12);
|
|
const pt = randomBytes(len);
|
|
const aad = randomBytes(Math.floor(Math.random() * 80));
|
|
assert.equal(toHex(chacha20poly1305Seal(key, nonce, pt, aad)), toHex(nodeSeal(key, nonce, pt, aad)), `len ${len}`);
|
|
}
|
|
});
|
|
|
|
test("chacha20poly1305: random cross-check against node:crypto", () => {
|
|
for (const len of LENGTHS) {
|
|
for (const aadLen of [-1, 0, 1, 12, 16, 17, 40]) {
|
|
const key = randomBytes(32);
|
|
const nonce = randomBytes(12);
|
|
const pt = randomBytes(len);
|
|
const aad = aadLen < 0 ? undefined : randomBytes(aadLen);
|
|
const ptIn = pt.slice();
|
|
const sealed = chacha20poly1305Seal(key, nonce, pt, aad);
|
|
assert.equal(toHex(sealed), toHex(nodeSeal(key, nonce, pt, aad)), `len ${len} aad ${aadLen}`);
|
|
assert.ok(equalBytes(pt, ptIn));
|
|
const opened = chacha20poly1305Open(key, nonce, sealed, aad);
|
|
assert.ok(opened && equalBytes(opened, pt), `open len ${len} aad ${aadLen}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("chacha20poly1305: unaligned buffers and subarrays", () => {
|
|
const key = randomBytes(32);
|
|
const nonce = randomBytes(12);
|
|
for (const len of [0, 5, 64, 100, 1300]) {
|
|
for (const off of [1, 2, 3, 4, 16]) {
|
|
const backing = randomBytes(len + off + 7);
|
|
const pt = backing.subarray(off, off + len);
|
|
const sealed = chacha20poly1305Seal(key, nonce, pt);
|
|
assert.equal(toHex(sealed), toHex(nodeSeal(key, nonce, pt)));
|
|
const shifted = new Uint8Array(sealed.length + off);
|
|
shifted.set(sealed, off);
|
|
const opened = chacha20poly1305Open(key, nonce, shifted.subarray(off));
|
|
assert.ok(opened && equalBytes(opened, pt));
|
|
}
|
|
}
|
|
// A Buffer works like any Uint8Array.
|
|
const pt = Buffer.from("hello, world");
|
|
assert.deepEqual(chacha20poly1305Open(key, nonce, chacha20poly1305Seal(key, nonce, pt)), bytes(pt));
|
|
});
|
|
|
|
test("chacha20poly1305: tampering is rejected", () => {
|
|
const key = randomBytes(32);
|
|
const nonce = randomBytes(12);
|
|
const aad = randomBytes(13);
|
|
for (const len of [0, 1, 16, 1300]) {
|
|
const pt = randomBytes(len);
|
|
const sealed = chacha20poly1305Seal(key, nonce, pt, aad);
|
|
for (const i of [0, Math.floor(sealed.length / 2), sealed.length - 1]) {
|
|
const bad = sealed.slice();
|
|
bad[i] = bad[i]! ^ 1;
|
|
assert.equal(chacha20poly1305Open(key, nonce, bad, aad), null, `flip byte ${i} of ${len}`);
|
|
}
|
|
const badAad = aad.slice();
|
|
badAad[0] = badAad[0]! ^ 0x80;
|
|
assert.equal(chacha20poly1305Open(key, nonce, sealed, badAad), null);
|
|
assert.equal(chacha20poly1305Open(key, nonce, sealed), null);
|
|
const badNonce = nonce.slice();
|
|
badNonce[11] = badNonce[11]! ^ 1;
|
|
assert.equal(chacha20poly1305Open(key, badNonce, sealed, aad), null);
|
|
const badKey = key.slice();
|
|
badKey[0] = badKey[0]! ^ 1;
|
|
assert.equal(chacha20poly1305Open(badKey, nonce, sealed, aad), null);
|
|
assert.equal(chacha20poly1305Open(key, nonce, sealed.subarray(0, sealed.length - 1), aad), null);
|
|
}
|
|
assert.equal(chacha20poly1305Open(key, nonce, new Uint8Array(15)), null);
|
|
assert.throws(() => chacha20poly1305Seal(new Uint8Array(31), nonce, new Uint8Array(1)), RangeError);
|
|
assert.throws(() => chacha20poly1305Seal(key, new Uint8Array(24), new Uint8Array(1)), RangeError);
|
|
});
|
|
|
|
test("chacha20poly1305: Poly1305 carry edge cases against node:crypto", () => {
|
|
// All-0xff and all-zero data push the accumulator to its limb bounds.
|
|
for (const fillByte of [0x00, 0xff]) {
|
|
for (const len of [16, 32, 1024, 4096]) {
|
|
const key = new Uint8Array(32).fill(fillByte);
|
|
const nonce = new Uint8Array(12).fill(fillByte);
|
|
const pt = new Uint8Array(len).fill(fillByte);
|
|
const aad = new Uint8Array(len).fill(fillByte);
|
|
assert.equal(toHex(chacha20poly1305Seal(key, nonce, pt, aad)), toHex(nodeSeal(key, nonce, pt, aad)));
|
|
}
|
|
}
|
|
});
|
|
|
|
// --- XChaCha20-Poly1305 ---
|
|
|
|
test("xchacha20poly1305: draft-irtf-cfrg-xchacha-03 A.3.1 vector", () => {
|
|
const key = hex("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f");
|
|
const nonce = hex("404142434445464748494a4b4c4d4e4f5051525354555657");
|
|
const aad = hex("50515253c0c1c2c3c4c5c6c7");
|
|
const sealed = xchacha20poly1305Seal(key, nonce, LADIES, aad);
|
|
assert.equal(
|
|
toHex(sealed),
|
|
"bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b4522f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff921f9664c97637da9768812f615c68b13b52e" +
|
|
"c0875924c1c7987947deafd8780acf49",
|
|
);
|
|
assert.deepEqual(xchacha20poly1305Open(key, nonce, sealed, aad), LADIES);
|
|
const bad = sealed.slice();
|
|
bad[0] = bad[0]! ^ 1;
|
|
assert.equal(xchacha20poly1305Open(key, nonce, bad, aad), null);
|
|
assert.equal(xchacha20poly1305Open(key, nonce, sealed), null);
|
|
});
|
|
|
|
test("xchacha20poly1305: round trips and tampering", () => {
|
|
for (const len of LENGTHS) {
|
|
const key = randomBytes(32);
|
|
const nonce = randomBytes(24);
|
|
const pt = randomBytes(len);
|
|
const sealed = xchacha20poly1305Seal(key, nonce, pt);
|
|
assert.equal(sealed.length, len + 16);
|
|
const opened = xchacha20poly1305Open(key, nonce, sealed);
|
|
assert.ok(opened && equalBytes(opened, pt));
|
|
const badNonce = nonce.slice();
|
|
badNonce[3] = badNonce[3]! ^ 1; // the HChaCha20 half
|
|
assert.equal(xchacha20poly1305Open(key, badNonce, sealed), null);
|
|
const bad = sealed.slice();
|
|
bad[bad.length - 1] = bad[bad.length - 1]! ^ 1;
|
|
assert.equal(xchacha20poly1305Open(key, nonce, bad), null);
|
|
}
|
|
});
|
|
|
|
// --- BLAKE2s, HMAC, HKDF ---
|
|
|
|
test("blake2s: RFC 7693 appendix B ('abc')", () => {
|
|
assert.equal(
|
|
toHex(blake2s(new TextEncoder().encode("abc"))),
|
|
"508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982",
|
|
);
|
|
});
|
|
|
|
test("blake2s: RFC 7693 appendix E self-test (keyed and unkeyed, 16..32-byte outputs)", () => {
|
|
const seq = (len: number, seed: number): Uint8Array => {
|
|
const out = new Uint8Array(len);
|
|
let a = Math.imul(0xdead4bad, seed) >>> 0;
|
|
let b = 1;
|
|
for (let i = 0; i < len; i++) {
|
|
const t = (a + b) >>> 0;
|
|
a = b;
|
|
b = t;
|
|
out[i] = t >>> 24;
|
|
}
|
|
return out;
|
|
};
|
|
const parts: Uint8Array[] = [];
|
|
for (const outLen of [16, 20, 28, 32]) {
|
|
for (const inLen of [0, 3, 64, 65, 255, 1024]) {
|
|
const input = seq(inLen, inLen);
|
|
parts.push(blake2s(input, outLen));
|
|
parts.push(blake2s(input, outLen, seq(outLen, outLen)));
|
|
}
|
|
}
|
|
assert.equal(toHex(blake2s(parts)), "6a411f08ce25adcdfb02aba641451cec53c598b24f4fc787fbdc88797f4c1dfe");
|
|
});
|
|
|
|
test("blake2s: random cross-check against node:crypto, chunked input", () => {
|
|
for (const len of LENGTHS) {
|
|
const data = randomBytes(len);
|
|
const want = toHex(nc.createHash("blake2s256").update(data).digest());
|
|
assert.equal(toHex(blake2s(data)), want, `len ${len}`);
|
|
const cut = [0, 1, 63, 64, 65].filter((c) => c <= len);
|
|
for (const c of cut) assert.equal(toHex(blake2s([data.subarray(0, c), data.subarray(c)])), want);
|
|
}
|
|
assert.throws(() => blake2s(new Uint8Array(0), 33), RangeError);
|
|
assert.throws(() => blake2s(new Uint8Array(0), 32, new Uint8Array(33)), RangeError);
|
|
});
|
|
|
|
test("hmacBlake2s: random cross-check against node:crypto", () => {
|
|
for (const keyLen of [0, 1, 32, 64, 65, 100]) {
|
|
for (const len of [0, 1, 32, 64, 65, 1300]) {
|
|
const key = randomBytes(keyLen);
|
|
const data = randomBytes(len);
|
|
const want = toHex(nc.createHmac("blake2s256", key).update(data).digest());
|
|
assert.equal(toHex(hmacBlake2s(key, data)), want);
|
|
assert.equal(toHex(hmacBlake2s(key, [data.subarray(0, len >> 1), data.subarray(len >> 1)])), want);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("hkdfBlake2s: random cross-check against node:crypto hkdf", () => {
|
|
for (const ikmLen of [0, 32, 48]) {
|
|
const ck = randomBytes(32);
|
|
const ikm = randomBytes(ikmLen);
|
|
const want = new Uint8Array(nc.hkdfSync("blake2s256", ikm, ck, new Uint8Array(0), 96));
|
|
for (const n of [1, 2, 3] as const) {
|
|
const out = hkdfBlake2s(ck, ikm, n);
|
|
assert.equal(out.length, n);
|
|
assert.equal(toHex(concatBytes(...out)), toHex(want.subarray(0, 32 * n)));
|
|
}
|
|
}
|
|
});
|
|
|
|
// --- NaCl box ---
|
|
|
|
// From NaCl's tests/box.c, box2.c and box7.c (also in libsodium's test suite).
|
|
const ALICE_SK = hex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
|
const ALICE_PK = hex("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
|
|
const BOB_SK = hex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
|
const BOB_PK = hex("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
|
const BOX_NONCE = hex("69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37");
|
|
const BOX_MSG = hex(`
|
|
be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffc
|
|
e5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb31
|
|
0e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde
|
|
048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f93776384864
|
|
5e0705`);
|
|
const BOX_SEALED = hex(`
|
|
f3ffc7703f9400e52a7dfb4b3d3305d9
|
|
8e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186a
|
|
c0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738
|
|
b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da
|
|
99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74
|
|
e355a5`);
|
|
|
|
test("nacl box: NaCl test-suite vector", () => {
|
|
assert.equal(
|
|
toHex(naclBoxBeforeNm(BOB_PK, ALICE_SK)),
|
|
"1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389",
|
|
);
|
|
assert.deepEqual(naclBoxBeforeNm(ALICE_PK, BOB_SK), naclBoxBeforeNm(BOB_PK, ALICE_SK));
|
|
const sealed = naclBoxSeal(BOX_MSG, BOX_NONCE, BOB_PK, ALICE_SK);
|
|
assert.equal(toHex(sealed), toHex(BOX_SEALED));
|
|
assert.deepEqual(naclBoxOpen(BOX_SEALED, BOX_NONCE, ALICE_PK, BOB_SK), BOX_MSG);
|
|
const k = naclBoxBeforeNm(BOB_PK, ALICE_SK);
|
|
assert.deepEqual(naclBoxSealAfterNm(k, BOX_NONCE, BOX_MSG), BOX_SEALED);
|
|
assert.deepEqual(naclBoxOpenAfterNm(k, BOX_NONCE, BOX_SEALED), BOX_MSG);
|
|
});
|
|
|
|
test("nacl box: round trips and tampering", () => {
|
|
const a = randomBytes(32);
|
|
const b = randomBytes(32);
|
|
const aPub = x25519Public(a);
|
|
const bPub = x25519Public(b);
|
|
for (const len of LENGTHS) {
|
|
const nonce = randomBytes(24);
|
|
const msg = randomBytes(len);
|
|
const sealed = naclBoxSeal(msg, nonce, bPub, a);
|
|
assert.equal(sealed.length, len + 16);
|
|
const opened = naclBoxOpen(sealed, nonce, aPub, b);
|
|
assert.ok(opened && equalBytes(opened, msg), `len ${len}`);
|
|
for (const i of [0, 15, 16, sealed.length - 1]) {
|
|
if (i >= sealed.length) continue;
|
|
const bad = sealed.slice();
|
|
bad[i] = bad[i]! ^ 1;
|
|
assert.equal(naclBoxOpen(bad, nonce, aPub, b), null);
|
|
}
|
|
const badNonce = nonce.slice();
|
|
badNonce[23] = badNonce[23]! ^ 1;
|
|
assert.equal(naclBoxOpen(sealed, badNonce, aPub, b), null);
|
|
assert.equal(naclBoxOpen(sealed, nonce, bPub, b), null);
|
|
}
|
|
assert.equal(naclBoxOpenAfterNm(randomBytes(32), randomBytes(24), new Uint8Array(15)), null);
|
|
});
|
|
|
|
// --- helpers ---
|
|
|
|
test("equalBytes, concatBytes, randomBytes", () => {
|
|
assert.ok(equalBytes(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])));
|
|
assert.ok(!equalBytes(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 4])));
|
|
assert.ok(!equalBytes(new Uint8Array([1, 2]), new Uint8Array([1, 2, 3])));
|
|
assert.ok(equalBytes(new Uint8Array(0), new Uint8Array(0)));
|
|
assert.deepEqual(concatBytes(), new Uint8Array(0));
|
|
assert.deepEqual(concatBytes(new Uint8Array([1]), new Uint8Array(0), new Uint8Array([2, 3])), new Uint8Array([1, 2, 3]));
|
|
assert.equal(randomBytes(0).length, 0);
|
|
const big = randomBytes(200000);
|
|
assert.equal(big.length, 200000);
|
|
assert.ok(big.subarray(150000).some((x) => x !== 0));
|
|
assert.ok(!equalBytes(randomBytes(32), randomBytes(32)));
|
|
});
|