OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webcrypto_impl.h" | 5 #include "webcrypto_impl.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "content/public/renderer/content_renderer_client.h" | 11 #include "content/public/renderer/content_renderer_client.h" |
12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
13 #include "content/renderer/webcrypto_impl.h" | 13 #include "content/renderer/webcrypto_impl.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
18 | 17 |
19 namespace content { | 18 namespace content { |
20 | 19 |
21 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { | 20 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { |
22 WebKit::WebCryptoAlgorithmIdSha1, | 21 WebKit::WebCryptoAlgorithmIdSha1, |
23 WebKit::WebCryptoAlgorithmIdSha224, | 22 WebKit::WebCryptoAlgorithmIdSha224, |
24 WebKit::WebCryptoAlgorithmIdSha256, | 23 WebKit::WebCryptoAlgorithmIdSha256, |
25 WebKit::WebCryptoAlgorithmIdSha384, | 24 WebKit::WebCryptoAlgorithmIdSha384, |
26 WebKit::WebCryptoAlgorithmIdSha512 | 25 WebKit::WebCryptoAlgorithmIdSha512 |
27 }; | 26 }; |
28 | 27 |
29 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { | 28 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { |
30 }; | 29 }; |
31 | 30 |
32 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 31 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
33 // The results are stored here in hex format for readability. | 32 // The results are stored here in hex format for readability. |
34 // | 33 // |
35 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | 34 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced |
36 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | 35 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 |
37 struct { | 36 struct { |
38 const char* input; | 37 const char* input; |
39 unsigned input_length; | 38 size_t input_length; |
40 const char* hex_result[arraysize(kAlgorithmIds)]; | 39 const char* hex_result[arraysize(kAlgorithmIds)]; |
41 } input_set[] = { | 40 } input_set[] = { |
42 { | 41 { |
43 "", 0, | 42 "", 0, |
44 { | 43 { |
45 // echo -n "" | sha1sum | 44 // echo -n "" | sha1sum |
46 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | 45 "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
47 // echo -n "" | sha224sum | 46 // echo -n "" | sha224sum |
48 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | 47 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", |
49 // echo -n "" | sha256sum | 48 // echo -n "" | sha256sum |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | 85 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" |
87 "22780d7e1b95bfeaa86a678e4552", | 86 "22780d7e1b95bfeaa86a678e4552", |
88 // echo -n -e "\000\001\002\003\004\005" | sha512sum | 87 // echo -n -e "\000\001\002\003\004\005" | sha512sum |
89 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | 88 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" |
90 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | 89 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", |
91 }, | 90 }, |
92 }, | 91 }, |
93 }; | 92 }; |
94 | 93 |
95 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { | 94 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { |
96 SCOPED_TRACE(id_index); | |
97 | |
98 WebKit::WebCryptoAlgorithm algorithm( | 95 WebKit::WebCryptoAlgorithm algorithm( |
99 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 96 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
100 kAlgorithmIds[id_index], NULL)); | 97 kAlgorithmIds[id_index], NULL)); |
101 | 98 |
102 for (size_t set_index = 0; | 99 for (size_t set_index = 0; |
103 set_index < ARRAYSIZE_UNSAFE(input_set); | 100 set_index < ARRAYSIZE_UNSAFE(input_set); |
104 set_index++) { | 101 set_index++) { |
105 SCOPED_TRACE(set_index); | |
106 | |
107 WebKit::WebArrayBuffer array_buffer; | 102 WebKit::WebArrayBuffer array_buffer; |
108 | 103 |
109 WebCryptoImpl crypto; | 104 WebCryptoImpl crypto; |
110 EXPECT_TRUE( | 105 crypto.DigestInternal( |
111 crypto.DigestInternal( | 106 algorithm, |
112 algorithm, | 107 reinterpret_cast<const unsigned char*>(input_set[set_index].input), |
113 reinterpret_cast<const unsigned char*>( | 108 input_set[set_index].input_length, |
114 input_set[set_index].input), | 109 &array_buffer); |
115 input_set[set_index].input_length, | |
116 &array_buffer)); | |
117 | 110 |
118 // Ignore case, it's checking the hex value. | 111 // Ignore case, it's checking the hex value. |
119 EXPECT_STRCASEEQ( | 112 EXPECT_STRCASEEQ( |
120 input_set[set_index].hex_result[id_index], | 113 input_set[set_index].hex_result[id_index], |
121 base::HexEncode( | 114 base::HexEncode( |
122 array_buffer.data(), array_buffer.byteLength()).c_str()); | 115 array_buffer.data(), array_buffer.byteLength()).c_str()); |
123 } | 116 } |
124 } | 117 } |
125 } | 118 } |
126 | 119 |
127 TEST_F(WebCryptoImplTest, HMACSampleSets) { | |
128 struct { | |
129 WebKit::WebCryptoAlgorithmId algorithm; | |
130 const char* key; | |
131 const char* message; | |
132 const char* mac; | |
133 } input_set[] = { | |
134 // Empty sets. Result generated via OpenSSL commandline tool. These | |
135 // particular results are also posted on the Wikipedia page examples: | |
136 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code | |
137 { | |
138 WebKit::WebCryptoAlgorithmIdSha1, | |
139 "", | |
140 "", | |
141 // openssl dgst -sha1 -hmac "" < /dev/null | |
142 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", | |
143 }, | |
144 { | |
145 WebKit::WebCryptoAlgorithmIdSha256, | |
146 "", | |
147 "", | |
148 // openssl dgst -sha256 -hmac "" < /dev/null | |
149 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", | |
150 }, | |
151 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 | |
152 // Download: | |
153 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip | |
154 // L=20 set 45 | |
155 { | |
156 WebKit::WebCryptoAlgorithmIdSha1, | |
157 // key | |
158 "59785928d72516e31272", | |
159 // message | |
160 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" | |
161 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" | |
162 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" | |
163 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", | |
164 // mac | |
165 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", | |
166 }, | |
167 // L=20 set 299 | |
168 { | |
169 WebKit::WebCryptoAlgorithmIdSha1, | |
170 // key | |
171 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" | |
172 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" | |
173 "f5866aa62930d75b58f6", | |
174 // message | |
175 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" | |
176 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" | |
177 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" | |
178 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", | |
179 // mac | |
180 "4ac41ab89f625c60125ed65ffa958c6b490ea670", | |
181 }, | |
182 // L=32, set 30 | |
183 { | |
184 WebKit::WebCryptoAlgorithmIdSha256, | |
185 // key | |
186 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" | |
187 "58ffefa176", | |
188 // message | |
189 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | |
190 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | |
191 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | |
192 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", | |
193 // mac | |
194 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", | |
195 }, | |
196 // L=32, set 224 | |
197 { | |
198 WebKit::WebCryptoAlgorithmIdSha256, | |
199 // key | |
200 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" | |
201 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | |
202 "3371289b", | |
203 // message | |
204 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | |
205 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | |
206 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | |
207 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | |
208 // mac | |
209 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | |
210 }, | |
211 }; | |
212 | |
213 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(input_set); index++) { | |
214 SCOPED_TRACE(index); | |
215 | |
216 WebKit::WebCryptoAlgorithm hash_algorithm( | |
217 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
218 input_set[index].algorithm, NULL)); | |
219 | |
220 scoped_ptr<WebKit::WebCryptoHmacParams> hmac_params( | |
221 new WebKit::WebCryptoHmacParams(hash_algorithm)); | |
222 | |
223 WebKit::WebCryptoAlgorithm hmac_algorithm( | |
224 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
225 WebKit::WebCryptoAlgorithmIdHmac, hmac_params.release())); | |
226 | |
227 WebKit::WebCryptoKeyType type; | |
228 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
229 | |
230 std::vector<uint8> key_raw; | |
231 base::HexStringToBytes(input_set[index].key, &key_raw); | |
232 | |
233 WebCryptoImpl crypto; | |
234 | |
235 EXPECT_TRUE( | |
236 crypto.ImportKeyInternal( | |
237 WebKit::WebCryptoKeyFormatRaw, | |
238 key_raw.data(), | |
239 key_raw.size(), | |
240 hmac_algorithm, | |
241 WebKit::WebCryptoKeyUsageSign, | |
242 &handle, | |
243 &type)); | |
244 | |
245 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, type); | |
246 ASSERT_TRUE(handle.get()); | |
247 | |
248 WebKit::WebCryptoKey crypto_key = | |
249 WebKit::WebCryptoKey::create( | |
250 handle.release(), | |
251 type, | |
252 false, | |
253 hmac_algorithm, | |
254 WebKit::WebCryptoKeyUsageSign); | |
255 | |
256 std::vector<uint8> message_raw; | |
257 base::HexStringToBytes(input_set[index].message, &message_raw); | |
258 | |
259 WebKit::WebArrayBuffer array_buffer; | |
260 | |
261 EXPECT_TRUE( | |
262 crypto.SignInternal( | |
263 hmac_algorithm, | |
264 crypto_key, | |
265 message_raw.data(), | |
266 message_raw.size(), | |
267 &array_buffer)); | |
268 | |
269 // Ignore case, it's checking the hex value. | |
270 EXPECT_STRCASEEQ( | |
271 input_set[index].mac, | |
272 base::HexEncode( | |
273 array_buffer.data(), array_buffer.byteLength()).c_str()); | |
274 } | |
275 } | |
276 | |
277 } // namespace content | 120 } // namespace content |
OLD | NEW |