| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <script src="../fast/js/resources/js-test-pre.js"></script> | 4 <script src="../fast/js/resources/js-test-pre.js"></script> |
| 5 </head> | 5 </head> |
| 6 <body> | 6 <body> |
| 7 <p id="description"></p> | 7 <p id="description"></p> |
| 8 <div id="console"></div> | 8 <div id="console"></div> |
| 9 | 9 |
| 10 <script> | 10 <script> |
| 11 description("Tests algorithm normalization."); | 11 description("Tests algorithm normalization."); |
| 12 | 12 |
| 13 jsTestIsAsync = true; | 13 jsTestIsAsync = true; |
| 14 | 14 |
| 15 // FIXME: Rename this to crypto-operation.html, since it tests the basic |
| 16 // construction of CryptoOperations. |
| 17 |
| 15 // ------------------------------- | 18 // ------------------------------- |
| 16 // Helpers to return a normalized algorithm identifier. | 19 // Helpers to return a normalized algorithm identifier. |
| 17 // ------------------------------- | 20 // ------------------------------- |
| 18 | 21 |
| 19 key = null; | 22 aesCbcKey = null; |
| 23 hmacSha1Key = null; |
| 20 | 24 |
| 21 function normalizeDigest(algorithmIdentifier) | 25 function normalizeDigest(algorithmIdentifier) |
| 22 { | 26 { |
| 23 return crypto.subtle.digest(algorithmIdentifier).algorithm; | 27 return crypto.subtle.digest(algorithmIdentifier).algorithm; |
| 24 } | 28 } |
| 25 | 29 |
| 26 function normalizeEncrypt(algorithmIdentifier) | 30 function normalizeEncrypt(algorithmIdentifier) |
| 27 { | 31 { |
| 28 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm; | 32 return crypto.subtle.encrypt(algorithmIdentifier, aesCbcKey).algorithm; |
| 29 } | 33 } |
| 30 | 34 |
| 31 function normalizeSign(algorithmIdentifier) | 35 function normalizeSign(algorithmIdentifier) |
| 32 { | 36 { |
| 33 return crypto.subtle.sign(algorithmIdentifier, key).algorithm; | 37 return crypto.subtle.sign(algorithmIdentifier, hmacSha1Key).algorithm; |
| 34 } | 38 } |
| 35 | 39 |
| 36 function runTests() | 40 function runTests() |
| 37 { | 41 { |
| 38 // ------------------------------- | 42 // ------------------------------- |
| 39 // Case insensitivity of "name" | 43 // Case insensitivity of "name" |
| 40 // ------------------------------- | 44 // ------------------------------- |
| 41 algorithm = normalizeDigest({name: "SHA-1"}); | 45 algorithm = normalizeDigest({name: "SHA-1"}); |
| 42 shouldBe("algorithm.name", "'SHA-1'"); | 46 shouldBe("algorithm.name", "'SHA-1'"); |
| 43 algorithm = normalizeDigest({name: "sHa-256"}); | 47 algorithm = normalizeDigest({name: "sHa-256"}); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 shouldThrow("normalizeSign({name: 'hmac'})"); // Missing "hash" | 130 shouldThrow("normalizeSign({name: 'hmac'})"); // Missing "hash" |
| 127 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'})"); // Not a valid "h
ash" | 131 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'})"); // Not a valid "h
ash" |
| 128 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: origi
nalIv }})"); // Not a valid "hash" | 132 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: origi
nalIv }})"); // Not a valid "hash" |
| 129 | 133 |
| 130 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}}; | 134 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}}; |
| 131 algorithm = normalizeSign(validHmacSha1); | 135 algorithm = normalizeSign(validHmacSha1); |
| 132 shouldBe("algorithm.name", "'HMAC'"); | 136 shouldBe("algorithm.name", "'HMAC'"); |
| 133 shouldBe("algorithm.hash.name", "'SHA-1'"); | 137 shouldBe("algorithm.hash.name", "'SHA-1'"); |
| 134 | 138 |
| 135 shouldThrow("normalizeEncrypt(validHmacSha1)"); // Not defined for encrypt() | 139 shouldThrow("normalizeEncrypt(validHmacSha1)"); // Not defined for encrypt() |
| 140 |
| 141 // ------------------------------- |
| 142 // Try using a key for an unsupported operation. |
| 143 // ------------------------------- |
| 144 algorithmIdentifier = { name: "aes-cbc", iv: originalIv }; |
| 145 shouldThrow("crypto.subtle.encrypt(algorithmIdentifier, aesCbcKeyNoEncrypt)"
); |
| 146 |
| 147 // ------------------------------- |
| 148 // Try using an HMAC-SHA1 key for encrypting AES-CBC |
| 149 // ------------------------------- |
| 150 algorithmIdentifier = { name: "aes-cbc", iv: originalIv }; |
| 151 shouldThrow("crypto.subtle.encrypt(algorithmIdentifier, hmacSha1Key)"); |
| 152 |
| 153 // ------------------------------- |
| 154 // Try using an HMAC-SHA1 key for signing HMAC-SHA256 |
| 155 // ------------------------------- |
| 156 algorithmIdentifier = {name: 'hmac', hash: {name: 'sha-256'}}; |
| 157 shouldThrow("crypto.subtle.sign(algorithmIdentifier, hmacSha1Key)"); |
| 136 } | 158 } |
| 137 | 159 |
| 138 function keyImported(newKey) | 160 function importAesCbcKey(keyUsages) |
| 139 { | 161 { |
| 140 key = newKey; | 162 var keyFormat = "spki"; |
| 141 runTests(); | 163 var data = new Uint8Array([]); |
| 142 finishJSTest(); | 164 var algorithm = {name: "aes-cbc"}; |
| 165 var extractable = false; |
| 166 |
| 167 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 168 } |
| 169 |
| 170 function importHmacSha1Key() |
| 171 { |
| 172 var keyFormat = "spki"; |
| 173 var data = new Uint8Array([]); |
| 174 var algorithm = {name: 'hmac', hash: {name: 'sha-1'}}; |
| 175 var extractable = false; |
| 176 var keyUsages = ['encrypt', 'decrypt', 'sign', 'verify']; |
| 177 |
| 178 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 143 } | 179 } |
| 144 | 180 |
| 145 function failedKeyImport(value) | 181 function failedKeyImport(value) |
| 146 { | 182 { |
| 147 debug("Failed importing key: " + value); | 183 debug("Failed importing key: " + value); |
| 148 finishJSTest(); | 184 finishJSTest(); |
| 149 } | 185 } |
| 150 | 186 |
| 151 // This is a bogus key import, however the mock constructs something usable. | 187 // Import two keys before starting the tests: one for AES-CBC, and one for |
| 152 keyFormat = "spki"; | 188 // HMAC SHA1. |
| 153 data = new Uint8Array([]); | 189 Promise.every(importAesCbcKey(['encrypt', 'decrypt', 'sign', 'verify']), importA
esCbcKey(['decrypt', 'sign', 'verify']), importHmacSha1Key()).then(function(keys
) |
| 154 algorithm = {name: "aes-cbc"}; | 190 { |
| 155 extractable = false; | 191 aesCbcKey = keys[0]; |
| 156 keyUsages = []; | 192 aesCbcKeyNoEncrypt = keys[1]; |
| 193 hmacSha1Key = keys[2]; |
| 157 | 194 |
| 158 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then
(keyImported, failedKeyImport); | 195 runTests(); |
| 196 finishJSTest(); |
| 197 |
| 198 }, failedKeyImport); |
| 159 | 199 |
| 160 </script> | 200 </script> |
| 161 | 201 |
| 162 <script src="../fast/js/resources/js-test-post.js"></script> | 202 <script src="../fast/js/resources/js-test-post.js"></script> |
| 163 </body> | 203 </body> |
| 164 </html> | 204 </html> |
| OLD | NEW |