OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <script src="../resources/js-test.js"></script> | |
5 <script src="resources/common.js"></script> | |
6 </head> | |
7 <body> | |
8 <p id="description"></p> | |
9 <div id="console"></div> | |
10 | |
11 <script> | |
12 description("Tests generating AES keys"); | |
13 | |
14 jsTestIsAsync = true; | |
15 | |
16 // Tests the 24 permutations of keys generated by: | |
17 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible
KeyLengths | |
18 // | |
19 // For practical reasons these tests are not exhaustive. | |
20 | |
21 var kPossibleAlgorithms = ['AES-CBC', 'AES-GCM']; | |
22 var kPossibleExtractable = [true, false]; | |
23 var kPossibleKeyUsages = [['encrypt'], ['decrypt', 'wrapKey'], ['encrypt', 'wrap
Key', 'unwrapKey']]; | |
24 var kPossibleKeyLengths = [128, 256]; | |
25 | |
26 // Set of all key data generated so far. | |
27 var allKeyDataGenerated = {}; | |
28 | |
29 function runTest(algorithmName, extractable, keyUsages, keyLengthBits) | |
30 { | |
31 var genAlgorithm = { name: algorithmName, length: keyLengthBits }; | |
32 | |
33 var results = {}; | |
34 | |
35 var promise = crypto.subtle.generateKey(genAlgorithm, extractable, keyUsages
).then(function(result) { | |
36 generatedKey = result; | |
37 | |
38 shouldEvaluateAs("generatedKey.extractable", extractable); | |
39 shouldEvaluateAs("generatedKey.algorithm.name", algorithmName); | |
40 shouldEvaluateAs("generatedKey.algorithm.length", keyLengthBits); | |
41 shouldEvaluateAs("generatedKey.usages.join(',')", keyUsages.join(",")); | |
42 | |
43 if (extractable) | |
44 return crypto.subtle.exportKey('raw', generatedKey); | |
45 }); | |
46 | |
47 if (extractable) { | |
48 promise = promise.then(function(result) { | |
49 keyData = result; | |
50 shouldEvaluateAs("keyData.byteLength", keyLengthBits / 8); | |
51 | |
52 var keyDataHex = bytesToHexString(keyData); | |
53 | |
54 // It is very unlikely to generate two identical keys, so | |
55 // assume if that happens something is broken. | |
56 // (8 extractable keys are generated for each bit length). | |
57 if (allKeyDataGenerated[keyDataHex]) { | |
58 testFailed("Generated identical key data: " + keyDataHex); | |
59 } else { | |
60 allKeyDataGenerated[keyDataHex] = true; | |
61 testPassed("Generated unique key data of length: " + (keyData.by
teLength * 8) + " bits"); | |
62 } | |
63 }); | |
64 } | |
65 | |
66 return promise; | |
67 } | |
68 | |
69 var lastPromise = Promise.resolve(null); | |
70 | |
71 kPossibleAlgorithms.forEach(function(algorithmName) { | |
72 kPossibleExtractable.forEach(function(extractable) { | |
73 kPossibleKeyUsages.forEach(function(keyUsages) { | |
74 kPossibleKeyLengths.forEach(function(keyLengthBits) { | |
75 lastPromise = lastPromise.then(runTest.bind(null, algorithmName,
extractable, keyUsages, keyLengthBits)); | |
76 }); | |
77 }); | |
78 }); | |
79 }); | |
80 | |
81 lastPromise.then(finishJSTest, failAndFinishJSTest); | |
82 | |
83 </script> | |
84 | |
85 </body> | |
86 </html> | |
OLD | NEW |