Skip to content

Commit 2cb332d

Browse files
authored
[WebCryptoAPI] Add tentative tests for raw-* aliases (#58774)
1 parent ba4ddc1 commit 2cb332d

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// META: title=WebCryptoAPI: raw-secret and raw-public importKey() format aliases
2+
// META: timeout=long
3+
// META: script=../util/helpers.js
4+
5+
// For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an
6+
// alias of "raw". For all existing asymmetric algorithms in WebCrypto,
7+
// "raw-public" acts as an alias of "raw".
8+
9+
"use strict";
10+
11+
const rawKeyData16 = crypto.getRandomValues(new Uint8Array(16));
12+
const rawKeyData32 = crypto.getRandomValues(new Uint8Array(32));
13+
const wrapAlgorithm = { name: "AES-GCM", iv: new Uint8Array(12) };
14+
15+
const symmetricAlgorithms = [
16+
{ algorithm: { name: "AES-CTR", length: 128 }, keyData: rawKeyData16, usages: ["encrypt", "decrypt"] },
17+
{ algorithm: { name: "AES-CBC", length: 128 }, keyData: rawKeyData16, usages: ["encrypt", "decrypt"] },
18+
{ algorithm: { name: "AES-GCM", length: 128 }, keyData: rawKeyData16, usages: ["encrypt", "decrypt"] },
19+
{ algorithm: { name: "AES-KW", length: 128 }, keyData: rawKeyData16, usages: ["wrapKey", "unwrapKey"] },
20+
{ algorithm: { name: "HMAC", hash: "SHA-256", length: 256 }, keyData: rawKeyData32, usages: ["sign", "verify"] },
21+
{ algorithm: { name: "HKDF" }, keyData: rawKeyData32, usages: ["deriveBits", "deriveKey"], extractable: false },
22+
{ algorithm: { name: "PBKDF2" }, keyData: rawKeyData32, usages: ["deriveBits", "deriveKey"], extractable: false },
23+
];
24+
25+
for (const { algorithm, keyData, usages, extractable = true } of symmetricAlgorithms) {
26+
promise_test(async () => {
27+
const key = await crypto.subtle.importKey("raw-secret", keyData, algorithm, extractable, usages);
28+
assert_goodCryptoKey(key, algorithm, extractable, usages, "secret");
29+
if (extractable) {
30+
await crypto.subtle.exportKey("raw-secret", key);
31+
}
32+
}, `importKey/exportKey with raw-secret: ${algorithm.name}`);
33+
34+
if (extractable) {
35+
promise_test(async () => {
36+
const wrappingKey = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, false, ["wrapKey", "unwrapKey"]);
37+
const key = await crypto.subtle.importKey("raw-secret", keyData, algorithm, true, usages);
38+
const wrapped = await crypto.subtle.wrapKey("raw-secret", key, wrappingKey, wrapAlgorithm);
39+
const unwrapped = await crypto.subtle.unwrapKey("raw-secret", wrapped, wrappingKey, wrapAlgorithm, algorithm, true, usages);
40+
assert_goodCryptoKey(unwrapped, algorithm, true, usages, "secret");
41+
}, `wrapKey/unwrapKey with raw-secret: ${algorithm.name}`);
42+
}
43+
}
44+
45+
const asymmetricAlgorithms = [
46+
{ algorithm: { name: "ECDSA", namedCurve: "P-256" }, usages: ["verify"] },
47+
{ algorithm: { name: "ECDH", namedCurve: "P-256" }, usages: [] },
48+
{ algorithm: { name: "Ed25519" }, usages: ["verify"] },
49+
{ algorithm: { name: "X25519" }, usages: [] },
50+
];
51+
52+
for (const { algorithm, usages } of asymmetricAlgorithms) {
53+
const generateKeyUsages = usages.length ? usages.concat("sign") : ["deriveBits"];
54+
55+
promise_test(async () => {
56+
const keyPair = await crypto.subtle.generateKey(algorithm, true, generateKeyUsages);
57+
const keyData = await crypto.subtle.exportKey("raw-public", keyPair.publicKey);
58+
59+
const key = await crypto.subtle.importKey("raw-public", keyData, algorithm, true, usages);
60+
assert_goodCryptoKey(key, algorithm, true, usages, "public");
61+
await crypto.subtle.exportKey("raw-public", key);
62+
}, `importKey/exportKey with raw-public: ${algorithm.name}`);
63+
64+
promise_test(async () => {
65+
const keyPair = await crypto.subtle.generateKey(algorithm, true, generateKeyUsages);
66+
67+
const wrappingKey = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, false, ["wrapKey", "unwrapKey"]);
68+
const wrapped = await crypto.subtle.wrapKey("raw-public", keyPair.publicKey, wrappingKey, wrapAlgorithm);
69+
const unwrapped = await crypto.subtle.unwrapKey("raw-public", wrapped, wrappingKey, wrapAlgorithm, algorithm, true, usages);
70+
assert_goodCryptoKey(unwrapped, algorithm, true, usages, "public");
71+
}, `wrapKey/unwrapKey with raw-public: ${algorithm.name}`);
72+
}

0 commit comments

Comments
 (0)