Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 if (self.importScripts) { |
| 2 <html> | 2 importScripts('common.js'); |
| 3 <head> | 3 } |
| 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 | 4 |
| 11 <script> | 5 function runCloneSymmetricTests(algorithmName, extractable, keyUsages, keyData, hashName) |
| 12 description("Tests structured cloning of AES keys"); | |
| 13 | |
| 14 jsTestIsAsync = true; | |
| 15 | |
| 16 // Tests the 32 permutations of keys generated by: | |
| 17 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible KeyData | |
| 18 // | |
| 19 // For practical reasons these tests are not exhaustive. | |
| 20 | |
| 21 var k128BitData = "30112233445566778899aabbccddeeff" | |
| 22 var k256BitData = "00112233445546778899aabbccddeeff000102030405060708090a0b0c0d0 e0f"; | |
| 23 | |
| 24 var kPossibleAlgorithms = ['AES-CBC', 'AES-GCM']; | |
| 25 var kPossibleExtractable = [true, false]; | |
| 26 var kPossibleKeyUsages = [['encrypt'], ['decrypt', 'wrapKey'], ['encrypt', 'wrap Key', 'unwrapKey']]; | |
| 27 var kPossibleKeyData = [k128BitData, k256BitData]; | |
| 28 | |
| 29 function runTest(algorithmName, extractable, keyUsages, keyData) | |
| 30 { | 6 { |
| 31 var importData = hexStringToUint8Array(keyData); | 7 var importData = hexStringToUint8Array(keyData); |
| 32 var importAlgorithm = { name: algorithmName }; | 8 var importAlgorithm = { name: algorithmName }; |
| 9 if (hashName) | |
|
eroman
2015/03/05 19:58:50
I agree, this is a good tradeoff (having hash as a
| |
| 10 importAlgorithm.hash = { name: hashName }; | |
| 33 | 11 |
| 34 var results = {}; | 12 var results = {}; |
| 35 | 13 |
| 36 return crypto.subtle.importKey('raw', importData, importAlgorithm, extractab le, keyUsages).then(function(importedKey) { | 14 return crypto.subtle.importKey('raw', importData, importAlgorithm, extractab le, keyUsages).then(function(importedKey) { |
| 37 results.importedKey = importedKey; | 15 results.importedKey = importedKey; |
| 38 importedKey.extraProperty = 'hi'; | 16 importedKey.extraProperty = 'hi'; |
| 39 return cloneKey(importedKey); | 17 return cloneKey(importedKey); |
| 40 }).then(function(clonedKey) { | 18 }).then(function(clonedKey) { |
| 41 results.clonedKey = clonedKey; | 19 results.clonedKey = clonedKey; |
| 42 if (extractable) | 20 if (extractable) |
| 43 return crypto.subtle.exportKey('raw', clonedKey); | 21 return crypto.subtle.exportKey('raw', clonedKey); |
| 44 return null; | 22 return null; |
| 45 }).then(function(clonedKeyData) { | 23 }).then(function(clonedKeyData) { |
| 46 importedKey = results.importedKey; | 24 importedKey = results.importedKey; |
| 47 clonedKey = results.clonedKey; | 25 clonedKey = results.clonedKey; |
| 48 | 26 |
| 49 shouldEvaluateAs("importedKey.extraProperty", "hi"); | 27 shouldEvaluateAs("importedKey.extraProperty", "hi"); |
| 50 shouldEvaluateAs("importedKey.type", "secret"); | 28 shouldEvaluateAs("importedKey.type", "secret"); |
| 51 shouldEvaluateAs("importedKey.extractable", extractable); | 29 shouldEvaluateAs("importedKey.extractable", extractable); |
| 52 shouldEvaluateAs("importedKey.algorithm.name", algorithmName); | 30 shouldEvaluateAs("importedKey.algorithm.name", algorithmName); |
| 53 shouldEvaluateAs("importedKey.algorithm.length", importData.length * 8); | 31 testAlgorithmSpecificParameter(algorithmName, importedKey, importData, h ashName); |
| 54 shouldEvaluateAs("importedKey.usages.join(',')", keyUsages.join(",")); | 32 shouldEvaluateAs("importedKey.usages.join(',')", keyUsages.join(",")); |
| 55 | 33 |
| 56 shouldNotBe("importedKey", "clonedKey"); | 34 shouldNotBe("importedKey", "clonedKey"); |
| 57 | 35 |
| 58 shouldBeUndefined("clonedKey.extraProperty"); | 36 shouldBeUndefined("clonedKey.extraProperty"); |
| 59 shouldEvaluateAs("clonedKey.type", "secret"); | 37 shouldEvaluateAs("clonedKey.type", "secret"); |
| 60 shouldEvaluateAs("clonedKey.extractable", extractable); | 38 shouldEvaluateAs("clonedKey.extractable", extractable); |
| 61 shouldEvaluateAs("clonedKey.algorithm.name", algorithmName); | 39 shouldEvaluateAs("clonedKey.algorithm.name", algorithmName); |
| 62 shouldEvaluateAs("clonedKey.algorithm.length", importData.length * 8); | 40 testAlgorithmSpecificParameter(algorithmName, clonedKey, importData, has hName); |
| 63 shouldEvaluateAs("clonedKey.usages.join(',')", keyUsages.join(",")); | 41 shouldEvaluateAs("clonedKey.usages.join(',')", keyUsages.join(",")); |
| 64 | 42 |
| 65 logSerializedKey(importedKey); | 43 logSerializedKey(importedKey); |
| 66 | 44 |
| 67 if (extractable) | 45 if (extractable) |
| 68 bytesShouldMatchHexString("Cloned key exported data", keyData, clone dKeyData); | 46 bytesShouldMatchHexString("Cloned key exported data", keyData, clone dKeyData); |
| 69 | 47 |
| 70 debug(""); | 48 debug(""); |
| 71 }); | 49 }); |
| 72 } | 50 } |
| 73 | 51 |
| 74 var lastPromise = Promise.resolve(null); | 52 function testCloneSymmetricKeys(algorithmName, possibleHashAlgorithms, possibleE xtractable, possibleKeyUsages, possibleKeyData) |
|
eroman
2015/03/05 20:22:49
I believe we can remove the parameter "possibleExt
| |
| 53 { | |
| 54 var lastPromise = Promise.resolve(null); | |
| 75 | 55 |
| 76 kPossibleAlgorithms.forEach(function(algorithmName) { | 56 possibleHashAlgorithms.forEach(function(hashName) { |
| 77 kPossibleExtractable.forEach(function(extractable) { | 57 possibleExtractable.forEach(function(extractable) { |
| 78 kPossibleKeyUsages.forEach(function(keyUsages) { | 58 possibleKeyUsages.forEach(function(keyUsages) { |
| 79 kPossibleKeyData.forEach(function(keyData) { | 59 possibleKeyData.forEach(function(keyData) { |
| 80 lastPromise = lastPromise.then(runTest.bind(null, algorithmName, extractable, keyUsages, keyData)); | 60 lastPromise = lastPromise.then(runCloneSymmetricTests.bind(n ull, algorithmName, extractable, keyUsages, keyData, hashName)); |
| 61 }); | |
| 81 }); | 62 }); |
| 82 }); | 63 }); |
| 83 }); | 64 }); |
| 84 }); | |
| 85 | 65 |
| 86 lastPromise.then(finishJSTest, failAndFinishJSTest); | 66 return lastPromise; |
| 67 } | |
| 87 | 68 |
| 88 </script> | 69 function testAlgorithmSpecificParameter(algorithmName, keyParams, importData, ha shName) |
| 89 | 70 { |
| 90 </body> | 71 key = keyParams; |
| 91 </html> | 72 switch (algorithmName) |
|
eroman
2015/03/05 19:58:51
I do not like this, since I would like to avoid ha
| |
| 73 { | |
| 74 case 'AES-CBC': | |
| 75 case 'AES-GCM': | |
| 76 case 'AES-CTR': | |
| 77 case 'AES-KW': | |
| 78 shouldEvaluateAs("key.algorithm.length", importData.length * 8); | |
| 79 break; | |
| 80 case 'HMAC': | |
| 81 shouldEvaluateAs("key.algorithm.length", importData.length * 8); | |
| 82 shouldEvaluateAs("key.algorithm.hash.name", hashName); | |
| 83 break; | |
| 84 } | |
| 85 } | |
| OLD | NEW |