Repository navigation
Expand file tree
/
Copy pathWebCryptPQC.test.js
More file actions
303 lines (232 loc) · 11.2 KB
/
Copy pathWebCryptPQC.test.js
File metadata and controls
303 lines (232 loc) · 11.2 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { WebCryptPQC } from "../src/WebCryptPQC.js";
describe("WebCryptPQC - Post-Quantum Cryptography", () => {
let pqc;
beforeEach(() => {
pqc = new WebCryptPQC();
});
describe("Kyber Key Encapsulation Mechanism (KEM)", () => {
describe("Kyber Key Generation", () => {
test("generates a key pair", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber768");
expect(keys).toHaveProperty("publicKey");
expect(keys).toHaveProperty("privateKey");
expect(keys.publicKey).toBeInstanceOf(Uint8Array);
expect(keys.privateKey).toBeInstanceOf(Uint8Array);
});
test("generates valid key pair for Kyber512", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber512");
expect(keys.publicKey.byteLength).toBe(800);
expect(keys.privateKey.byteLength).toBe(1632);
});
test("generates valid key pair for Kyber768", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber768");
expect(keys.publicKey.byteLength).toBe(1184);
expect(keys.privateKey.byteLength).toBe(2400);
});
test("generates valid key pair for Kyber1024", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber1024");
expect(keys.publicKey.byteLength).toBe(1568);
expect(keys.privateKey.byteLength).toBe(3168);
});
test("generates different key pairs on each call", async () => {
const keys1 = await pqc.generateKyberKeyPair("Kyber768");
const keys2 = await pqc.generateKyberKeyPair("Kyber768");
expect(keys1.publicKey).not.toEqual(keys2.publicKey);
expect(keys1.privateKey).not.toEqual(keys2.privateKey);
});
});
describe("Kyber Encapsulation", () => {
test("encapsulates using public key", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber768");
const result = await pqc.kyberEncapsulate(keys.publicKey, "Kyber768");
expect(result).toHaveProperty("ciphertext");
expect(result).toHaveProperty("sharedSecret");
expect(result.ciphertext).toBeInstanceOf(Uint8Array);
expect(result.sharedSecret).toBeInstanceOf(Uint8Array);
});
test("produces correct ciphertext sizes", async () => {
const keys512 = await pqc.generateKyberKeyPair("Kyber512");
const result512 = await pqc.kyberEncapsulate(keys512.publicKey, "Kyber512");
expect(result512.ciphertext.byteLength).toBe(768);
const keys768 = await pqc.generateKyberKeyPair("Kyber768");
const result768 = await pqc.kyberEncapsulate(keys768.publicKey, "Kyber768");
expect(result768.ciphertext.byteLength).toBe(1088);
const keys1024 = await pqc.generateKyberKeyPair("Kyber1024");
const result1024 = await pqc.kyberEncapsulate(keys1024.publicKey, "Kyber1024");
expect(result1024.ciphertext.byteLength).toBe(1568);
});
test("produces different ciphertexts each call", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber768");
const result1 = await pqc.kyberEncapsulate(keys.publicKey, "Kyber768");
const result2 = await pqc.kyberEncapsulate(keys.publicKey, "Kyber768");
expect(result1.ciphertext).not.toEqual(result2.ciphertext);
});
});
describe("Kyber Decapsulation", () => {
test("decapsulates using private key", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber768");
const { ciphertext } = await pqc.kyberEncapsulate(keys.publicKey, "Kyber768");
const sharedSecret = await pqc.kyberDecapsulate(ciphertext, keys.privateKey, "Kyber768");
expect(sharedSecret).toBeInstanceOf(Uint8Array);
expect(sharedSecret.byteLength).toBe(32);
});
});
});
describe("Dilithium Digital Signatures", () => {
describe("generateDilithiumKeyPair", () => {
test("generates valid key pair", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
expect(keys).toHaveProperty("publicKey");
expect(keys).toHaveProperty("privateKey");
expect(keys.publicKey).toBeInstanceOf(Uint8Array);
expect(keys.privateKey).toBeInstanceOf(Uint8Array);
});
test("Dilithium2 generates valid key pair with correct sizes", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium2");
expect(keys.publicKey.byteLength).toBe(1312);
expect(keys.privateKey.byteLength).toBe(2544);
});
test("Dilithium3 generates valid key pair with correct sizes", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
expect(keys.publicKey.byteLength).toBe(1952);
expect(keys.privateKey.byteLength).toBe(4000);
});
test("Dilithium5 generates valid key pair with correct sizes", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium5");
expect(keys.publicKey.byteLength).toBe(2592);
expect(keys.privateKey.byteLength).toBe(4864);
});
test("generates different key pairs on each call", async () => {
const keys1 = await pqc.generateDilithiumKeyPair("Dilithium3");
const keys2 = await pqc.generateDilithiumKeyPair("Dilithium3");
expect(keys1.publicKey).not.toEqual(keys2.publicKey);
expect(keys1.privateKey).not.toEqual(keys2.privateKey);
});
});
describe("dilithiumSign", () => {
test("signs a message", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const document = new Uint8Array([1, 2, 3, 4, 5]);
const signature = await pqc.dilithiumSign(document, keys.privateKey, "Dilithium3");
expect(signature).toBeInstanceOf(Uint8Array);
});
test("produces correct signature sizes", async () => {
const keys2 = await pqc.generateDilithiumKeyPair("Dilithium2");
const doc = new Uint8Array([1, 2, 3]);
const sig2 = await pqc.dilithiumSign(doc, keys2.privateKey, "Dilithium2");
expect(sig2.byteLength).toBe(2420);
const keys3 = await pqc.generateDilithiumKeyPair("Dilithium3");
const sig3 = await pqc.dilithiumSign(doc, keys3.privateKey, "Dilithium3");
expect(sig3.byteLength).toBe(3293);
const keys5 = await pqc.generateDilithiumKeyPair("Dilithium5");
const sig5 = await pqc.dilithiumSign(doc, keys5.privateKey, "Dilithium5");
expect(sig5.byteLength).toBe(4595);
});
test("signatures are valid (stub implementation)", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const document = new Uint8Array([1, 2, 3, 4, 5]);
const sig1 = await pqc.dilithiumSign(document, keys.privateKey, "Dilithium3");
expect(sig1).toBeInstanceOf(Uint8Array);
expect(sig1.byteLength).toBe(3293);
});
});
describe("dilithiumVerify", () => {
test("verifies valid signature", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const document = new Uint8Array([1, 2, 3, 4, 5]);
const signature = await pqc.dilithiumSign(document, keys.privateKey, "Dilithium3");
const verified = await pqc.dilithiumVerify(
document,
signature,
keys.publicKey,
"Dilithium3"
);
expect(verified).toBe(true);
});
test("verifies signatures with correct size (stub implementation)", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const document = new Uint8Array([1, 2, 3, 4, 5]);
const signature = await pqc.dilithiumSign(document, keys.privateKey, "Dilithium3");
const verified = await pqc.dilithiumVerify(
document,
signature,
keys.publicKey,
"Dilithium3"
);
expect(verified).toBe(true);
});
test("rejects tampered signatures and modified documents", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const document = new Uint8Array([1, 2, 3, 4, 5]);
const signature = await pqc.dilithiumSign(document, keys.privateKey, "Dilithium3");
// Tampered signature byte
const tamperedSig = new Uint8Array(signature);
tamperedSig[65] ^= 0xff;
const verifiedTampered = await pqc.dilithiumVerify(
document,
tamperedSig,
keys.publicKey,
"Dilithium3"
);
expect(verifiedTampered).toBe(false);
// Modified document
const modifiedDoc = new Uint8Array([1, 2, 3, 4, 99]);
const verifiedModified = await pqc.dilithiumVerify(
modifiedDoc,
signature,
keys.publicKey,
"Dilithium3"
);
expect(verifiedModified).toBe(false);
});
test("verifies with correct public key", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const document = new Uint8Array([1, 2, 3, 4, 5]);
const signature = await pqc.dilithiumSign(document, keys.privateKey, "Dilithium3");
const verified = await pqc.dilithiumVerify(
document,
signature,
keys.publicKey,
"Dilithium3"
);
expect(verified).toBe(true);
});
});
describe("Key Serialization", () => {
test("Kyber keys can be converted to/from Base64", async () => {
const keys = await pqc.generateKyberKeyPair("Kyber768");
const pubB64 = pqc.kyberPublicKeyToBase64(keys.publicKey);
const privB64 = pqc.kyberPrivateKeyToBase64(keys.privateKey);
expect(typeof pubB64).toBe("string");
expect(typeof privB64).toBe("string");
const pubRecover = pqc.kyberPublicKeyFromBase64(pubB64);
const privRecover = pqc.kyberPrivateKeyFromBase64(privB64);
expect(pubRecover).toEqual(keys.publicKey);
expect(privRecover).toEqual(keys.privateKey);
});
test("Dilithium keys can be converted to/from Base64", async () => {
const keys = await pqc.generateDilithiumKeyPair("Dilithium3");
const pubB64 = pqc.dilithiumPublicKeyToBase64(keys.publicKey);
const privB64 = pqc.dilithiumPrivateKeyToBase64(keys.privateKey);
expect(typeof pubB64).toBe("string");
expect(typeof privB64).toBe("string");
const pubRecover = pqc.dilithiumPublicKeyFromBase64(pubB64);
const privRecover = pqc.dilithiumPrivateKeyFromBase64(privB64);
expect(pubRecover).toEqual(keys.publicKey);
expect(privRecover).toEqual(keys.privateKey);
});
});
describe("Algorithm Constants", () => {
test("Kyber constants are defined correctly", () => {
expect(WebCryptPQC.KYBER_PARAMS["Kyber512"].publicKeySize).toBe(800);
expect(WebCryptPQC.KYBER_PARAMS["Kyber768"].ciphertextSize).toBe(1088);
expect(WebCryptPQC.KYBER_PARAMS["Kyber1024"].sharedSecretSize).toBe(32);
});
test("Dilithium constants are defined correctly", () => {
expect(WebCryptPQC.DILITHIUM_PARAMS["Dilithium2"].publicKeySize).toBe(1312);
expect(WebCryptPQC.DILITHIUM_PARAMS["Dilithium3"].signatureSize).toBe(3293);
expect(WebCryptPQC.DILITHIUM_PARAMS["Dilithium5"].privateKeySize).toBe(4864);
});
});
});
});