Skip to content

Commit 650d6f7

Browse files
committed
core: add deriveKeyPair test for browsers.
1 parent c1c1b20 commit 650d6f7

File tree

3 files changed

+136
-7
lines changed

3 files changed

+136
-7
lines changed
Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import { expect, test } from "@playwright/test";
22

3-
test("basic test", async ({ page }) => {
3+
test("basic test with generateKeyPair", async ({ page }) => {
44
await page.goto("./index.html");
55
await page.click("text=run");
66
await page.waitForTimeout(5000);
77
await expect(page.locator("id=pass")).toHaveText("18");
88
await expect(page.locator("id=fail")).toHaveText("0");
99
});
1010

11-
test("secure curves test", async ({ browserName, page }) => {
12-
// test.skip(
13-
// browserName === "webkit",
14-
// "Secure curves are not supported in Safari",
15-
// );
16-
await page.goto("./secure_curves.html");
11+
test("basic test with deriveKeyPair", async ({ browserName, page }) => {
12+
await page.goto("./index.html");
13+
await page.click("text=run");
14+
await page.waitForTimeout(5000);
15+
if (browserName === "firefox") {
16+
await expect(page.locator("id=pass")).toHaveText("0");
17+
await expect(page.locator("id=fail")).toHaveText("18");
18+
} else {
19+
await expect(page.locator("id=pass")).toHaveText("18");
20+
await expect(page.locator("id=fail")).toHaveText("0");
21+
}
22+
});
23+
24+
test("secure curves test with generateKeyPair", async ({ browserName, page }) => {
25+
await page.goto("./secureCurves.html");
1726
await page.click("text=run");
1827
await page.waitForTimeout(5000);
1928
if (browserName === "webkit") {
@@ -24,3 +33,16 @@ test("secure curves test", async ({ browserName, page }) => {
2433
await expect(page.locator("id=fail")).toHaveText("0");
2534
}
2635
});
36+
37+
test("secure curves test with deriveKeyPair", async ({ browserName, page }) => {
38+
await page.goto("./secureCurvesWithDeriveKeyPair.html");
39+
await page.click("text=run");
40+
await page.waitForTimeout(5000);
41+
if (browserName === "chromium") {
42+
await expect(page.locator("id=pass")).toHaveText("6");
43+
await expect(page.locator("id=fail")).toHaveText("0");
44+
} else {
45+
await expect(page.locator("id=pass")).toHaveText("0");
46+
await expect(page.locator("id=fail")).toHaveText("6");
47+
}
48+
});
File renamed without changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<html>
2+
<head><title>@hpke/core test</title></head>
3+
<body>
4+
<script type="module">
5+
import {
6+
Aes128Gcm,
7+
Aes256Gcm,
8+
CipherSuite,
9+
DhkemX25519HkdfSha256,
10+
HkdfSha256,
11+
HkdfSha384,
12+
HkdfSha512,
13+
} from "./src/hpke-core.js";
14+
15+
const kems = [
16+
new DhkemX25519HkdfSha256(),
17+
];
18+
19+
const kdfs = [
20+
new HkdfSha256(),
21+
new HkdfSha384(),
22+
new HkdfSha512(),
23+
];
24+
25+
const aeads = [
26+
new Aes128Gcm(),
27+
new Aes256Gcm(),
28+
];
29+
30+
globalThis.run = async () => {
31+
let pass = 0;
32+
let fail = 0;
33+
for (const kem of kems) {
34+
for (const kdf of kdfs) {
35+
for (const aead of aeads) {
36+
try {
37+
const suite = new CipherSuite({
38+
kem: kem,
39+
kdf: kdf,
40+
aead: aead,
41+
});
42+
43+
const ikm = new Uint8Array(suite.kem.privateKeySize);
44+
globalThis.crypto.getRandomValues(ikm);
45+
const rkp = await suite.kem.deriveKeyPair(ikm.buffer);
46+
47+
const sender = await suite.createSenderContext({
48+
recipientPublicKey: rkp.publicKey,
49+
});
50+
51+
const recipient = await suite.createRecipientContext({
52+
recipientKey: rkp,
53+
enc: sender.enc,
54+
});
55+
56+
// encrypt
57+
const ct = await sender.seal(
58+
new TextEncoder().encode("hello world!"),
59+
);
60+
61+
// decrypt
62+
const pt = await recipient.open(ct);
63+
64+
// hello world!
65+
"hello world!" === new TextDecoder().decode(pt)
66+
? pass++
67+
: fail++;
68+
} catch (e) {
69+
console.error(e);
70+
fail++;
71+
}
72+
}
73+
}
74+
}
75+
document.getElementById("pass").innerHTML = pass;
76+
document.getElementById("fail").innerHTML = fail;
77+
};
78+
79+
globalThis.reset = () => {
80+
document.getElementById("pass").innerHTML = "-";
81+
document.getElementById("fail").innerHTML = "-";
82+
};
83+
</script>
84+
85+
<h1><a href="https://github.com/dajiaji/hpke-js">@hpke/core</a> test</h1>
86+
87+
<div id="operation">
88+
<button type="button" onclick="run()">run</button>
89+
<button type="button" onclick="reset()">reset</button>
90+
</div>
91+
92+
<br />
93+
94+
<div id="test">
95+
<table>
96+
<tr>
97+
<th><font color="green">pass:</font></th>
98+
<td id="pass">-</td>
99+
</tr>
100+
<tr>
101+
<th><font color="red">fail:</font></th>
102+
<td id="fail">-</td>
103+
</tr>
104+
</table>
105+
</div>
106+
</body>
107+
</html>

0 commit comments

Comments
 (0)