4 <script src=
"../../resources/js-test.js"></script>
5 <script src=
"resources/common.js"></script>
8 <p id=
"description"></p>
9 <div id=
"console"></div>
12 description("Tests generating AES keys");
16 // Tests the 24 permutations of keys generated by:
17 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossibleKeyLengths
19 // For practical reasons these tests are not exhaustive.
21 var kPossibleAlgorithms
= ['AES-CBC', 'AES-GCM'];
22 var kPossibleExtractable
= [true, false];
23 var kPossibleKeyUsages
= [['encrypt'], ['decrypt', 'wrapKey'], ['encrypt', 'wrapKey', 'unwrapKey']];
24 var kPossibleKeyLengths
= [128, 256];
26 // Set of all key data generated so far.
27 var allKeyDataGenerated
= {};
29 function runTest(algorithmName
, extractable
, keyUsages
, keyLengthBits
)
31 var genAlgorithm
= { name
: algorithmName
, length
: keyLengthBits
};
35 var promise
= crypto
.subtle
.generateKey(genAlgorithm
, extractable
, keyUsages
).then(function(result
) {
36 generatedKey
= result
;
38 shouldEvaluateAs("generatedKey.extractable", extractable
);
39 shouldEvaluateAs("generatedKey.algorithm.name", algorithmName
);
40 shouldEvaluateAs("generatedKey.algorithm.length", keyLengthBits
);
41 shouldEvaluateAs("generatedKey.usages.join(',')", keyUsages
.join(","));
44 return crypto
.subtle
.exportKey('raw', generatedKey
);
48 promise
= promise
.then(function(result
) {
50 shouldEvaluateAs("keyData.byteLength", keyLengthBits
/ 8);
52 var keyDataHex
= bytesToHexString(keyData
);
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
);
60 allKeyDataGenerated
[keyDataHex
] = true;
61 testPassed("Generated unique key data of length: " + (keyData
.byteLength
* 8) + " bits");
69 var lastPromise
= Promise
.resolve(null);
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
));
81 lastPromise
.then(finishJSTest
, failAndFinishJSTest
);