Repository navigation
Expand file tree
/
Copy pathWebCryptPQCKEMRoundtrip.test.js
More file actions
97 lines (82 loc) · 2.79 KB
/
Copy pathWebCryptPQCKEMRoundtrip.test.js
File metadata and controls
97 lines (82 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { WebCryptPQC } from "../src/WebCryptPQC.js";
import { WebCryptAsym } from "../src/WebCryptAsym.js";
describe("WebCryptPQC Kyber KEM & Hybrid Round-Trip", () => {
let pqc;
let asym;
beforeAll(() => {
WebCryptPQC.enableStubTesting(true);
});
afterAll(() => {
WebCryptPQC.enableStubTesting(false);
});
beforeEach(() => {
pqc = new WebCryptPQC();
asym = new WebCryptAsym();
});
afterEach(() => {
asym.stopAutoCleanup();
});
test("Kyber512 encapsulate and decapsulate recover identical shared secret", async () => {
const { publicKey, privateKey } = await pqc.generateKyberKeyPair(WebCryptPQC.KYBER_512);
const { ciphertext, sharedSecret } = await pqc.kyberEncapsulate(
publicKey,
WebCryptPQC.KYBER_512
);
const recoveredSecret = await pqc.kyberDecapsulate(
ciphertext,
privateKey,
WebCryptPQC.KYBER_512
);
expect(Buffer.from(recoveredSecret).toString("hex")).toBe(
Buffer.from(sharedSecret).toString("hex")
);
});
test("Kyber768 encapsulate and decapsulate recover identical shared secret", async () => {
const { publicKey, privateKey } = await pqc.generateKyberKeyPair(WebCryptPQC.KYBER_768);
const { ciphertext, sharedSecret } = await pqc.kyberEncapsulate(
publicKey,
WebCryptPQC.KYBER_768
);
const recoveredSecret = await pqc.kyberDecapsulate(
ciphertext,
privateKey,
WebCryptPQC.KYBER_768
);
expect(Buffer.from(recoveredSecret).toString("hex")).toBe(
Buffer.from(sharedSecret).toString("hex")
);
});
test("Kyber1024 encapsulate and decapsulate recover identical shared secret", async () => {
const { publicKey, privateKey } = await pqc.generateKyberKeyPair(WebCryptPQC.KYBER_1024);
const { ciphertext, sharedSecret } = await pqc.kyberEncapsulate(
publicKey,
WebCryptPQC.KYBER_1024
);
const recoveredSecret = await pqc.kyberDecapsulate(
ciphertext,
privateKey,
WebCryptPQC.KYBER_1024
);
expect(Buffer.from(recoveredSecret).toString("hex")).toBe(
Buffer.from(sharedSecret).toString("hex")
);
});
test("hybridEncapsulate / hybridDecapsulate round-trip matches shared secret", async () => {
const rsaKeys = await asym.generateKeyPair(2048);
const kyberKeys = await pqc.generateKyberKeyPair(WebCryptPQC.KYBER_768);
const { combinedCiphertext, hybridSecret } = await pqc.hybridEncapsulate(
rsaKeys.publicKey,
kyberKeys.publicKey,
WebCryptPQC.KYBER_768
);
const recoveredSecret = await pqc.hybridDecapsulate(
combinedCiphertext,
rsaKeys.privateKey,
kyberKeys.privateKey,
WebCryptPQC.KYBER_768
);
expect(Buffer.from(recoveredSecret).toString("hex")).toBe(
Buffer.from(hybridSecret).toString("hex")
);
});
});