Skip to content

Commit c52a808

Browse files
panvaruyadorno
authored andcommitted
test,crypto: update WebCryptoAPI WPT
PR-URL: #55703 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent fa4c58a commit c52a808

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

test/fixtures/wpt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Last update:
3232
- user-timing: https://github.com/web-platform-tests/wpt/tree/5ae85bf826/user-timing
3333
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi
3434
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
35-
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/75db68b054/WebCryptoAPI
35+
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/5f0f4ac1af/WebCryptoAPI
3636
- webidl/ecmascript-binding/es-exceptions: https://github.com/web-platform-tests/wpt/tree/a370aad338/webidl/ecmascript-binding/es-exceptions
3737
- webmessaging/broadcastchannel: https://github.com/web-platform-tests/wpt/tree/6495c91853/webmessaging/broadcastchannel
3838
- webstorage: https://github.com/web-platform-tests/wpt/tree/9dafa89214/webstorage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// META: title=WebCryptoAPI: deriveKey() Using HKDF and PBKDF2 from an ECDH key
2+
// META: script=derive_key_and_encrypt.js
3+
// META: script=../util/helpers.js
4+
5+
// Test imported from WebKit's source, defined to check the impact of the
6+
// 'Get Key Length' behavior of HKDF and PBKDF2, which should return 'null'
7+
// in both cases, in the 'deriveKey' operation.
8+
// https://bugs.webkit.org/show_bug.cgi?id=282096
9+
promise_test(define_tests, 'setup - define tests');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
let iv = new Uint8Array(Array(12).keys());
2+
let salt = new Uint8Array(Array(10).keys());
3+
let plaintext = new Uint8Array(Array(100).keys());
4+
5+
function define_tests() {
6+
importKeys().then((keys) => {
7+
// Make sure that ecdh produces the same shared secret and the same encryption results using a key derived from that secret.
8+
keys.forEach(keyData => {
9+
promise_test(async() => {
10+
let hkdfKey = await crypto.subtle.deriveKey({name: "ECDH", public: keyData.publicKey }, keyData.privateKey, { name: "HKDF", hash: "" , salt: new Uint8Array(), info: new Uint8Array() }, false, ["deriveKey"]);
11+
let aesKey = await crypto.subtle.deriveKey({name: "HKDF", hash: "SHA-256", salt: salt, info: plaintext}, hkdfKey, {name:"AES-GCM", length: 256}, true, ["encrypt", "decrypt"]);
12+
let result = await crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, aesKey, plaintext);
13+
assert_equals(bytesToHexString(result), "a6280c522670eaf82f6564afbeb20a5b3f2d4e13c5596f6df3dcff8c34cb2118d2770fb24d83cfac5079c323118485bb01170292ee41eb82b07208f4840478fea3771d8922785c476ba06c2a0b933fc1661431419530a916ad4468545d1af5004a1149fea241c2ff1582ee58a8b7d79935de5def");
14+
}, "HKDF derivation of a ECDH key " + keyData.test);
15+
promise_test(async() => {
16+
let pkdf2Key = await crypto.subtle.deriveKey({name: "ECDH", public: keyData.publicKey }, keyData.privateKey, { name: "PBKDF2", hash: "" , salt: new Uint8Array(), iterations: 32 }, false, ["deriveKey"]);
17+
let aesKey = await crypto.subtle.deriveKey({name: "PBKDF2", hash: "SHA-256", salt: salt, iterations: 32 }, pkdf2Key, { name:"AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]);
18+
let result = await crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, aesKey, plaintext);
19+
assert_equals(bytesToHexString(result), "c6201dfbb6fa92c1c246f6ce52f8f1c037f087efde41bac7f6485a2a8207623d2d3825b9cbe8ef864a90378667ed25544ce44cd2904bd96c19f0eeb611d626185165a8afb4e52f95700d7880f83939a42712fc4e377f198c01a61b397b76c3a4b93d932c321084bbef33332169dea09458b27df3");
20+
}, "PBKDF2 derivation of a ECDH key " + keyData.test);
21+
});
22+
}, (e) => {
23+
assert_unreached("Setup failed: " + e.message);
24+
});
25+
26+
return Promise.resolve("define_tests");
27+
}
28+
29+
async function importKeys() {
30+
// "ECDSA" with a 'P-256' curve
31+
let keyData = [
32+
hexStringToUint8Array("308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b0201010420fe77a808a7109ba5ceb93ebebad2c84a714d864ad29b62d6537e1969035c0079a144034200042684c752eef1c927a80c74e8b02ce459f848b5977f37fd878b36dae632be9a6cadd56126e404a4f75c535e5769d95b49fb1106f784f3d231b776d1f4d57927ce"),
33+
hexStringToUint8Array("042684c752eef1c927a80c74e8b02ce459f848b5977f37fd878b36dae632be9a6cadd56126e404a4f75c535e5769d95b49fb1106f784f3d231b776d1f4d57927ce"),
34+
hexStringToUint8Array("308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b020101042067521ccd1f85516118182bca3394c273bab9ce5cd6265105559e325e01f2df1ca144034200043042d8698882f2b59de972390d3fc9277e2e677a6c560148017c9475218fda1b38f76f7645fbcaf3d03e6259d080204fbafb04731b6ad53cb25c3d35d95b7c73"),
35+
hexStringToUint8Array("043042d8698882f2b59de972390d3fc9277e2e677a6c560148017c9475218fda1b38f76f7645fbcaf3d03e6259d080204fbafb04731b6ad53cb25c3d35d95b7c73"),
36+
];
37+
let extractable = true;
38+
var allKeys = await Promise.all([
39+
crypto.subtle.importKey("pkcs8", keyData[0], {name: "ECDH", namedCurve: "P-256"}, extractable, ["deriveKey", 'deriveBits']),
40+
crypto.subtle.importKey("raw", keyData[1], {name: "ECDH", namedCurve: "P-256"}, extractable, []),
41+
crypto.subtle.importKey("pkcs8", keyData[2], {name: "ECDH", namedCurve: "P-256"}, extractable, ["deriveKey", 'deriveBits']),
42+
crypto.subtle.importKey("raw", keyData[3], {name: "ECDH", namedCurve: "P-256"}, extractable, []),
43+
]);
44+
// Test cases defined combining public and private keys of each key-pair.
45+
return [
46+
{ test: 1, publicKey: allKeys[3], privateKey: allKeys[0] },
47+
{ test: 2, publicKey: allKeys[1], privateKey: allKeys[2] }
48+
];
49+
}

test/fixtures/wpt/WebCryptoAPI/util/helpers.js

+38
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,41 @@ function allNameVariants(name, slowTest) {
259259
if (slowTest) return [mixedCaseName];
260260
return unique([upCaseName, lowCaseName, mixedCaseName]);
261261
}
262+
263+
// Builds a hex string representation for an array-like input.
264+
// "bytes" can be an Array of bytes, an ArrayBuffer, or any TypedArray.
265+
// The output looks like this:
266+
// ab034c99
267+
function bytesToHexString(bytes)
268+
{
269+
if (!bytes)
270+
return null;
271+
272+
bytes = new Uint8Array(bytes);
273+
var hexBytes = [];
274+
275+
for (var i = 0; i < bytes.length; ++i) {
276+
var byteString = bytes[i].toString(16);
277+
if (byteString.length < 2)
278+
byteString = "0" + byteString;
279+
hexBytes.push(byteString);
280+
}
281+
282+
return hexBytes.join("");
283+
}
284+
285+
function hexStringToUint8Array(hexString)
286+
{
287+
if (hexString.length % 2 != 0)
288+
throw "Invalid hexString";
289+
var arrayBuffer = new Uint8Array(hexString.length / 2);
290+
291+
for (var i = 0; i < hexString.length; i += 2) {
292+
var byteValue = parseInt(hexString.substr(i, 2), 16);
293+
if (byteValue == NaN)
294+
throw "Invalid hexString";
295+
arrayBuffer[i/2] = byteValue;
296+
}
297+
298+
return arrayBuffer;
299+
}

test/fixtures/wpt/versions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"path": "wasm/webapi"
8989
},
9090
"WebCryptoAPI": {
91-
"commit": "75db68b05454e7f3a81e063373defd094c176a16",
91+
"commit": "5f0f4ac1af4848480406621fac99163c8ba0e242",
9292
"path": "WebCryptoAPI"
9393
},
9494
"webidl/ecmascript-binding/es-exceptions": {

0 commit comments

Comments
 (0)