OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <script src="../resources/js-test.js"></script> | 4 <script src="../resources/js-test.js"></script> |
5 <script src="resources/common.js"></script> | 5 <script src="resources/common.js"></script> |
6 </head> | 6 </head> |
7 <body> | 7 <body> |
8 <p id="description"></p> | 8 <p id="description"></p> |
9 <div id="console"></div> | 9 <div id="console"></div> |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 { | 69 { |
70 algorithm: "SHA-256", | 70 algorithm: "SHA-256", |
71 key: "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63
8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b083371289b", | 71 key: "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63
8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b083371289b", |
72 message: "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b79
8c69025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050eb4f061
8eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c8f6d4eef3418c17b
1ef0b46c2a7188305d9811dccb3d99", | 72 message: "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b79
8c69025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050eb4f061
8eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c8f6d4eef3418c17b
1ef0b46c2a7188305d9811dccb3d99", |
73 mac: "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | 73 mac: "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", |
74 } | 74 } |
75 ]; | 75 ]; |
76 | 76 |
77 function runSuccessTestCase(testCase) | 77 function runSuccessTestCase(testCase) |
78 { | 78 { |
79 var algorithm = {name: 'HMAC', hash: {name: testCase.algorithm}}; | 79 var importAlgorithm = {name: 'HMAC', hash: {name: testCase.algorithm}}; |
| 80 var algorithm = {name: 'HMAC'}; |
80 | 81 |
81 var key = null; | 82 var key = null; |
82 var keyData = hexStringToUint8Array(testCase.key); | 83 var keyData = hexStringToUint8Array(testCase.key); |
83 var usages = ['sign', 'verify']; | 84 var usages = ['sign', 'verify']; |
84 var extractable = false; | 85 var extractable = false; |
85 | 86 |
86 // (1) Import the key | 87 // (1) Import the key |
87 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { | 88 return crypto.subtle.importKey('raw', keyData, importAlgorithm, extractable,
usages).then(function(result) { |
88 key = result; | 89 key = result; |
89 | 90 |
90 // shouldBe() can only resolve variables in global context. | 91 // shouldBe() can only resolve variables in global context. |
91 tmpKey = key; | 92 tmpKey = key; |
92 shouldBe("tmpKey.type", "'secret'") | 93 shouldBe("tmpKey.type", "'secret'") |
93 shouldBe("tmpKey.extractable", "false") | 94 shouldBe("tmpKey.extractable", "false") |
94 shouldBe("tmpKey.algorithm.name", "'HMAC'") | 95 shouldBe("tmpKey.algorithm.name", "'HMAC'") |
| 96 shouldBe("tmpKey.algorithm.hash.name", "'" + testCase.algorithm + "'") |
95 shouldBe("tmpKey.usages.join(',')", "'sign,verify'") | 97 shouldBe("tmpKey.usages.join(',')", "'sign,verify'") |
96 | 98 |
97 // (2) Sign. | 99 // (2) Sign. |
98 return crypto.subtle.sign(algorithm, key, hexStringToUint8Array(testCase
.message)); | 100 return crypto.subtle.sign(algorithm, key, hexStringToUint8Array(testCase
.message)); |
99 }).then(function(result) { | 101 }).then(function(result) { |
100 bytesShouldMatchHexString("Mac", testCase.mac, result); | 102 bytesShouldMatchHexString("Mac", testCase.mac, result); |
101 | 103 |
102 // (3) Verify | 104 // (3) Verify |
103 return crypto.subtle.verify(algorithm, key, hexStringToUint8Array(testCa
se.mac), hexStringToUint8Array(testCase.message)); | 105 return crypto.subtle.verify(algorithm, key, hexStringToUint8Array(testCa
se.mac), hexStringToUint8Array(testCase.message)); |
104 }).then(function(result) { | 106 }).then(function(result) { |
105 verifyResult = result; | 107 verifyResult = result; |
106 shouldBe("verifyResult", "true") | 108 shouldBe("verifyResult", "true") |
107 | 109 |
108 // (4) Verify truncated mac (by stripping 1 byte off of it). | 110 // (4) Verify truncated mac (by stripping 1 byte off of it). |
109 var expectedMac = hexStringToUint8Array(testCase.mac); | 111 var expectedMac = hexStringToUint8Array(testCase.mac); |
110 return crypto.subtle.verify(algorithm, key, expectedMac.subarray(0, expe
ctedMac.byteLength - 1), hexStringToUint8Array(testCase.message)); | 112 return crypto.subtle.verify(algorithm, key, expectedMac.subarray(0, expe
ctedMac.byteLength - 1), hexStringToUint8Array(testCase.message)); |
111 }).then(function(result) { | 113 }).then(function(result) { |
112 verifyResult = result; | 114 verifyResult = result; |
113 shouldBe("verifyResult", "false") | 115 shouldBe("verifyResult", "false") |
114 }); | 116 }); |
115 } | 117 } |
116 | 118 |
117 // Add all of the tests defined above. | 119 // Add all of the tests defined above. |
118 for (var i = 0; i < kHmacTestVectors.length; ++i) { | 120 for (var i = 0; i < kHmacTestVectors.length; ++i) { |
119 allTests.push(runSuccessTestCase(kHmacTestVectors[i])); | 121 allTests.push(runSuccessTestCase(kHmacTestVectors[i])); |
120 } | 122 } |
121 | 123 |
122 hmacSha1 = {name: 'hmac', hash: {name: 'sha-1'}}; | 124 hmac = {name: 'hmac'}; |
123 data = asciiToUint8Array("hello"); | 125 data = asciiToUint8Array("hello"); |
124 | 126 |
125 allTests.push(importTestKeys().then(function(importedKeys) { | 127 allTests.push(importTestKeys().then(function(importedKeys) { |
126 keys = importedKeys; | 128 keys = importedKeys; |
127 | 129 |
128 // Pass invalid signature parameters to verify() | 130 // Pass invalid signature parameters to verify() |
129 shouldThrow("crypto.subtle.verify(hmacSha1, keys.hmacSha1, null, data)"); | 131 shouldThrow("crypto.subtle.verify(hmac, keys.hmacSha1, null, data)"); |
130 shouldThrow("crypto.subtle.verify(hmacSha1, keys.hmacSha1, 'a', data)"); | 132 shouldThrow("crypto.subtle.verify(hmac, keys.hmacSha1, 'a', data)"); |
131 shouldThrow("crypto.subtle.verify(hmacSha1, keys.hmacSha1, [], data)"); | 133 shouldThrow("crypto.subtle.verify(hmac, keys.hmacSha1, [], data)"); |
132 | 134 |
133 // Operation does not support signing. | 135 // Operation does not support signing. |
134 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'sha-1'}, keys.hmacSh
a1, data)"); | 136 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'sha-1'}, keys.hmacSh
a1, data)"); |
135 | 137 |
136 // Operation doesn't support signing (also given an invalid key, but the | 138 // Operation doesn't support signing (also given an invalid key, but the |
137 // first failure takes priority) | 139 // first failure takes priority) |
138 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'RSAES-PKCS1-v1_5'},
keys.hmacSha1, data)"); | 140 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'RSAES-PKCS1-v1_5'},
keys.hmacSha1, data)"); |
139 | |
140 // Key's algorithm must match. | |
141 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac', hash: {name:
'sha-256'}}, keys.hmacSha1, data)"); | |
142 | |
143 // --------------------------------------------------- | |
144 // HMAC normalization failures (HmacParams) | |
145 // --------------------------------------------------- | |
146 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac'}, keys.hmacSha
1, data)"); | |
147 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac', hash: 3}, key
s.hmacSha1, data)"); | |
148 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac', hash: null},
keys.hmacSha1, data)"); | |
149 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac', hash: {}}, ke
ys.hmacSha1, data)"); | |
150 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac', hash: {name:
'foo'}}, keys.hmacSha1, data)"); | |
151 shouldRejectPromiseWithNull("crypto.subtle.sign({name: 'hmac', hash: {name:
'AES-CBC'}}, keys.hmacSha1, data)"); | |
152 })); | 141 })); |
153 | 142 |
154 // ------------------------------------------------- | 143 // ------------------------------------------------- |
155 // Wait until all the tests have been run. | 144 // Wait until all the tests have been run. |
156 // ------------------------------------------------- | 145 // ------------------------------------------------- |
157 | 146 |
158 Promise.all(allTests).then(finishJSTest, failAndFinishJSTest); | 147 Promise.all(allTests).then(finishJSTest, failAndFinishJSTest); |
159 | 148 |
160 </script> | 149 </script> |
161 | 150 |
162 </body> | 151 </body> |
OLD | NEW |