Skip to content

Commit fe4a42b

Browse files
test: update WPT for WebCryptoAPI to 55ce71bb9d
PR-URL: #65813 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 1d0e5f1 commit fe4a42b

19 files changed

Lines changed: 329 additions & 714 deletions

test/fixtures/wpt/WebCryptoAPI/encap_decap/encap_decap_bits.tentative.https.any.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
// META: title=WebCryptoAPI: ML-KEM encapsulateBits() and decapsulateBits() tests
2-
// META: script=ml_kem_vectors.js
1+
// META: title=WebCryptoAPI: KEM encapsulateBits() and decapsulateBits() tests
32
// META: script=../util/helpers.js
3+
// META: script=ml_kem_vectors.js
4+
// META: script=hybrid_kem_vectors.js
45
// META: timeout=long
56

67
function define_bits_tests() {
78
var subtle = self.crypto.subtle;
8-
var variants = ['ML-KEM-512', 'ML-KEM-768', 'ML-KEM-1024'];
9+
var variants = [
10+
{ name: 'ML-KEM-512', ciphertextLength: 768 },
11+
{ name: 'ML-KEM-768', ciphertextLength: 1088 },
12+
{ name: 'ML-KEM-1024', ciphertextLength: 1568 },
13+
{ name: 'MLKEM768-P256', ciphertextLength: 1153 },
14+
{ name: 'MLKEM768-X25519', ciphertextLength: 1120 },
15+
{ name: 'MLKEM1024-P384', ciphertextLength: 1665 },
16+
];
17+
18+
variants.forEach(function (variant) {
19+
var algorithmName = variant.name;
920

10-
variants.forEach(function (algorithmName) {
1121
// Test encapsulateBits operation
1222
promise_test(async function (test) {
1323
// Generate a key pair for testing
@@ -50,24 +60,11 @@ function define_bits_tests() {
5060
'Shared key should be 32 bytes'
5161
);
5262

53-
// Verify ciphertext length based on algorithm variant
54-
var expectedCiphertextLength;
55-
switch (algorithmName) {
56-
case 'ML-KEM-512':
57-
expectedCiphertextLength = 768;
58-
break;
59-
case 'ML-KEM-768':
60-
expectedCiphertextLength = 1088;
61-
break;
62-
case 'ML-KEM-1024':
63-
expectedCiphertextLength = 1568;
64-
break;
65-
}
6663
assert_equals(
6764
encapsulatedBits.ciphertext.byteLength,
68-
expectedCiphertextLength,
65+
variant.ciphertextLength,
6966
'Ciphertext should be ' +
70-
expectedCiphertextLength +
67+
variant.ciphertextLength +
7168
' bytes for ' +
7269
algorithmName
7370
);

test/fixtures/wpt/WebCryptoAPI/encap_decap/encap_decap_keys.tentative.https.any.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
// META: title=WebCryptoAPI: ML-KEM encapsulateKey() and decapsulateKey() tests
2-
// META: script=ml_kem_vectors.js
1+
// META: title=WebCryptoAPI: KEM encapsulateKey() and decapsulateKey() tests
32
// META: script=../util/helpers.js
3+
// META: script=ml_kem_vectors.js
4+
// META: script=hybrid_kem_vectors.js
45
// META: timeout=long
56

67
function define_key_tests() {
78
var subtle = self.crypto.subtle;
8-
var variants = ['ML-KEM-512', 'ML-KEM-768', 'ML-KEM-1024'];
9+
var variants = [
10+
{ name: 'ML-KEM-512', ciphertextLength: 768 },
11+
{ name: 'ML-KEM-768', ciphertextLength: 1088 },
12+
{ name: 'ML-KEM-1024', ciphertextLength: 1568 },
13+
{ name: 'MLKEM768-P256', ciphertextLength: 1153 },
14+
{ name: 'MLKEM768-X25519', ciphertextLength: 1120 },
15+
{ name: 'MLKEM1024-P384', ciphertextLength: 1665 },
16+
];
917

1018
// Test various 256-bit shared key algorithms
1119
var sharedKeyConfigs = [
@@ -36,7 +44,9 @@ function define_key_tests() {
3644
},
3745
];
3846

39-
variants.forEach(function (algorithmName) {
47+
variants.forEach(function (variant) {
48+
var algorithmName = variant.name;
49+
4050
sharedKeyConfigs.forEach(function (config) {
4151
[true, false].forEach(function (extractable) {
4252
// Test encapsulateKey operation
@@ -115,24 +125,11 @@ function define_key_tests() {
115125
);
116126
}
117127

118-
// Verify ciphertext length based on algorithm variant
119-
var expectedCiphertextLength;
120-
switch (algorithmName) {
121-
case 'ML-KEM-512':
122-
expectedCiphertextLength = 768;
123-
break;
124-
case 'ML-KEM-768':
125-
expectedCiphertextLength = 1088;
126-
break;
127-
case 'ML-KEM-1024':
128-
expectedCiphertextLength = 1568;
129-
break;
130-
}
131128
assert_equals(
132129
encapsulatedKey.ciphertext.byteLength,
133-
expectedCiphertextLength,
130+
variant.ciphertextLength,
134131
'Ciphertext should be ' +
135-
expectedCiphertextLength +
132+
variant.ciphertextLength +
136133
' bytes for ' +
137134
algorithmName
138135
);

test/fixtures/wpt/WebCryptoAPI/encap_decap/hybrid_kem_vectors.js

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/wpt/WebCryptoAPI/encap_decap/ml_kem_vectors.js

Lines changed: 36 additions & 646 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/wpt/WebCryptoAPI/generateKey/algorithm_registry.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// META: title=WebCryptoAPI: generateKey() for Failures
2+
// META: timeout=long
3+
// META: script=../util/helpers.js
4+
// META: script=algorithm_registry.js
5+
// META: script=failures.js
6+
run_test(["MLKEM768-P256", "MLKEM768-X25519", "MLKEM1024-P384"]);

test/fixtures/wpt/WebCryptoAPI/generateKey/successes.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,22 @@ function run_test(algorithmNames, slowTest) {
4747
assert_unreached("generateKey threw an unexpected error: " + err.toString());
4848
})
4949
.then(async function (result) {
50-
// TODO: remove this block to enable ML-KEM JWK when its definition is done in IETF JOSE WG
51-
if (result.publicKey?.algorithm.name.startsWith('ML-KEM')) {
52-
const promises = [
53-
subtle.exportKey('spki', result.publicKey),
54-
extractable ? subtle.exportKey('pkcs8', result.privateKey) : undefined,
55-
subtle.exportKey('raw-public', result.publicKey),
56-
];
57-
if (extractable)
58-
promises.push(subtle.exportKey('raw-seed', result.privateKey));
59-
await Promise.all(promises);
60-
} else if (resultType === "CryptoKeyPair") {
61-
const promises = [
62-
subtle.exportKey('jwk', result.publicKey),
63-
extractable ? subtle.exportKey('jwk', result.privateKey) : undefined,
64-
subtle.exportKey('spki', result.publicKey),
65-
extractable ? subtle.exportKey('pkcs8', result.privateKey) : undefined,
66-
];
50+
if (resultType === "CryptoKeyPair") {
51+
// TODO: enable ML-KEM JWK when its definition is done in IETF JOSE WG.
52+
const isMlKem = result.publicKey.algorithm.name.startsWith('ML-KEM');
53+
const isHybridKem = result.publicKey.algorithm.name.startsWith('MLKEM');
54+
const promises = [];
55+
56+
if (!isMlKem) {
57+
promises.push(subtle.exportKey('jwk', result.publicKey));
58+
promises.push(extractable ? subtle.exportKey('jwk', result.privateKey) : undefined);
59+
}
60+
61+
if (!isHybridKem) {
62+
promises.push(subtle.exportKey('spki', result.publicKey));
63+
if (extractable)
64+
promises.push(subtle.exportKey('pkcs8', result.privateKey));
65+
}
6766

6867
switch (result.publicKey.algorithm.name.substring(0, 2)) {
6968
case 'ML':
@@ -90,7 +89,7 @@ function run_test(algorithmNames, slowTest) {
9089

9190
const [jwkPub, jwkPriv] = await Promise.all(promises);
9291

93-
if (extractable) {
92+
if (extractable && !isMlKem) {
9493
// Test that the JWK public key is a superset of the JWK private key.
9594
for (const [prop, value] of Object.entries(jwkPub)) {
9695
if (prop !== 'key_ops') {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// META: title=WebCryptoAPI: generateKey() Successful Calls
2+
// META: timeout=long
3+
// META: script=../util/helpers.js
4+
// META: script=/common/subset-tests.js
5+
// META: script=algorithm_registry.js
6+
// META: script=successes.js
7+
run_test(["MLKEM768-P256", "MLKEM768-X25519", "MLKEM1024-P384"]);

test/fixtures/wpt/WebCryptoAPI/getPublicKey.tentative.https.any.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ const algorithms = [
109109
generateKeyParams: { name: "ML-KEM-1024" },
110110
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
111111
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
112+
},
113+
{
114+
name: "MLKEM768-P256",
115+
generateKeyParams: { name: "MLKEM768-P256" },
116+
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
117+
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
118+
},
119+
{
120+
name: "MLKEM768-X25519",
121+
generateKeyParams: { name: "MLKEM768-X25519" },
122+
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
123+
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
124+
},
125+
{
126+
name: "MLKEM1024-P384",
127+
generateKeyParams: { name: "MLKEM1024-P384" },
128+
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
129+
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
112130
}
113131
];
114132

@@ -145,8 +163,9 @@ algorithms.forEach(function(algorithm) {
145163

146164
// Verify that the derived public key matches the original public key
147165
// by comparing their exported forms
148-
const originalExported = await crypto.subtle.exportKey("spki", keyPair.publicKey);
149-
const derivedExported = await crypto.subtle.exportKey("spki", publicKey);
166+
const exportFormat = algorithm.name.startsWith("MLKEM") ? "raw-public" : "spki";
167+
const originalExported = await crypto.subtle.exportKey(exportFormat, keyPair.publicKey);
168+
const derivedExported = await crypto.subtle.exportKey(exportFormat, publicKey);
150169

151170
assert_array_equals(
152171
new Uint8Array(originalExported),
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// META: title=WebCryptoAPI: importKey() for Hybrid KEM keys
2+
// META: timeout=long
3+
// META: script=../util/helpers.js
4+
// META: script=Hybrid-KEM_importKey_fixtures.js
5+
// META: script=ml_importKey.js
6+
7+
var keyData = hybridKemKeyData;
8+
9+
runTests("MLKEM768-P256");
10+
runTests("MLKEM768-X25519");
11+
runTests("MLKEM1024-P384");

0 commit comments

Comments
 (0)