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" | |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { | 21 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { |
21 WebKit::WebCryptoAlgorithmIdSha1, | 22 WebKit::WebCryptoAlgorithmIdSha1, |
22 WebKit::WebCryptoAlgorithmIdSha224, | 23 WebKit::WebCryptoAlgorithmIdSha224, |
23 WebKit::WebCryptoAlgorithmIdSha256, | 24 WebKit::WebCryptoAlgorithmIdSha256, |
24 WebKit::WebCryptoAlgorithmIdSha384, | 25 WebKit::WebCryptoAlgorithmIdSha384, |
25 WebKit::WebCryptoAlgorithmIdSha512 | 26 WebKit::WebCryptoAlgorithmIdSha512 |
26 }; | 27 }; |
27 | 28 |
28 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { | 29 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { |
29 }; | 30 }; |
30 | 31 |
31 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 32 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
32 // The results are stored here in hex format for readability. | 33 // The results are stored here in hex format for readability. |
33 // | 34 // |
34 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | 35 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced |
35 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | 36 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 |
36 struct { | 37 struct { |
37 const char* input; | 38 const char* input; |
38 size_t input_length; | 39 unsigned input_length; |
39 const char* hex_result[arraysize(kAlgorithmIds)]; | 40 const char* hex_result[arraysize(kAlgorithmIds)]; |
40 } input_set[] = { | 41 } input_set[] = { |
41 { | 42 { |
42 "", 0, | 43 "", 0, |
43 { | 44 { |
44 // echo -n "" | sha1sum | 45 // echo -n "" | sha1sum |
45 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | 46 "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
46 // echo -n "" | sha224sum | 47 // echo -n "" | sha224sum |
47 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | 48 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", |
48 // echo -n "" | sha256sum | 49 // echo -n "" | sha256sum |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 WebKit::WebCryptoAlgorithm algorithm( | 96 WebKit::WebCryptoAlgorithm algorithm( |
96 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 97 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
97 kAlgorithmIds[id_index], NULL)); | 98 kAlgorithmIds[id_index], NULL)); |
98 | 99 |
99 for (size_t set_index = 0; | 100 for (size_t set_index = 0; |
100 set_index < ARRAYSIZE_UNSAFE(input_set); | 101 set_index < ARRAYSIZE_UNSAFE(input_set); |
101 set_index++) { | 102 set_index++) { |
102 WebKit::WebArrayBuffer array_buffer; | 103 WebKit::WebArrayBuffer array_buffer; |
103 | 104 |
104 WebCryptoImpl crypto; | 105 WebCryptoImpl crypto; |
105 crypto.DigestInternal( | 106 EXPECT_TRUE( |
106 algorithm, | 107 crypto.DigestInternal( |
107 reinterpret_cast<const unsigned char*>(input_set[set_index].input), | 108 algorithm, |
108 input_set[set_index].input_length, | 109 reinterpret_cast<const unsigned char*>( |
109 &array_buffer); | 110 input_set[set_index].input), |
111 input_set[set_index].input_length, | |
112 &array_buffer)); | |
110 | 113 |
111 // Ignore case, it's checking the hex value. | 114 // Ignore case, it's checking the hex value. |
112 EXPECT_STRCASEEQ( | 115 EXPECT_STRCASEEQ( |
113 input_set[set_index].hex_result[id_index], | 116 input_set[set_index].hex_result[id_index], |
114 base::HexEncode( | 117 base::HexEncode( |
115 array_buffer.data(), array_buffer.byteLength()).c_str()); | 118 array_buffer.data(), array_buffer.byteLength()).c_str()); |
116 } | 119 } |
117 } | 120 } |
118 } | 121 } |
119 | 122 |
123 TEST_F(WebCryptoImplTest, HMACSampleSets) { | |
124 struct { | |
125 WebKit::WebCryptoAlgorithmId algorithm; | |
126 const char* key; | |
127 const char* msg; | |
Ryan Sleevi
2013/09/12 22:01:56
naming nit: msg -> message
http://google-stylegui
Bryan Eyler
2013/09/12 23:09:56
Done.
| |
128 const char* mac; | |
129 } input_set[] = { | |
130 // Empty sets. Result generated via OpenSSL commandline tool. These | |
131 // particular results are also posted on the Wikipedia page examples: | |
132 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code | |
133 { | |
134 WebKit::WebCryptoAlgorithmIdSha1, | |
135 "", | |
136 "", | |
137 // openssl dgst -sha1 -hmac "" < /dev/null | |
138 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", | |
139 }, | |
140 { | |
141 WebKit::WebCryptoAlgorithmIdSha256, | |
142 "", | |
143 "", | |
144 // openssl dgst -sha256 -hmac "" < /dev/null | |
145 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", | |
146 }, | |
147 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 | |
148 // Download: | |
149 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip | |
150 // L=20 set 45 | |
151 { | |
152 WebKit::WebCryptoAlgorithmIdSha1, | |
153 "59785928d72516e31272", | |
154 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" | |
155 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" | |
156 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" | |
157 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", | |
158 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", | |
Ryan Sleevi
2013/09/12 22:01:56
From a readability, it's very difficult to note th
Bryan Eyler
2013/09/12 23:09:56
Done.
| |
159 }, | |
160 // L=20 set 299 | |
161 { | |
162 WebKit::WebCryptoAlgorithmIdSha1, | |
163 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" | |
164 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" | |
165 "f5866aa62930d75b58f6", | |
166 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" | |
167 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" | |
168 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" | |
169 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", | |
170 "4ac41ab89f625c60125ed65ffa958c6b490ea670", | |
171 }, | |
172 // L=32, set 30 | |
173 { | |
174 WebKit::WebCryptoAlgorithmIdSha256, | |
175 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" | |
176 "58ffefa176", | |
177 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | |
178 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | |
179 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | |
180 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", | |
181 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", | |
182 }, | |
183 // L=32, set 224 | |
184 { | |
185 WebKit::WebCryptoAlgorithmIdSha256, | |
186 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" | |
187 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | |
188 "3371289b", | |
189 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | |
190 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | |
191 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | |
192 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | |
193 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | |
194 }, | |
195 }; | |
196 | |
197 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(input_set); index++) { | |
Ryan Sleevi
2013/09/12 22:01:56
In general, I have a preference for seeing paramet
Bryan Eyler
2013/09/12 23:09:56
Done, and added to digest tests as well.
| |
198 WebKit::WebCryptoAlgorithm hash_algorithm( | |
199 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
200 input_set[index].algorithm, NULL)); | |
201 | |
202 scoped_ptr<WebKit::WebCryptoHmacParams> hmac_params( | |
203 new WebKit::WebCryptoHmacParams(hash_algorithm)); | |
204 | |
205 WebKit::WebCryptoAlgorithm hmac_algorithm( | |
206 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
207 WebKit::WebCryptoAlgorithmIdHmac, hmac_params.release())); | |
208 | |
209 WebKit::WebCryptoKeyType type; | |
210 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
211 | |
212 std::vector<uint8> key_raw; | |
213 base::HexStringToBytes(input_set[index].key, &key_raw); | |
214 | |
215 WebCryptoImpl crypto; | |
216 | |
217 EXPECT_TRUE( | |
218 crypto.ImportKeyInternal( | |
219 WebKit::WebCryptoKeyFormatRaw, | |
220 key_raw.data(), | |
221 key_raw.size(), | |
222 hmac_algorithm, | |
223 WebKit::WebCryptoKeyUsageSign, | |
224 &handle, | |
225 &type)); | |
226 | |
227 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, type); | |
228 ASSERT_TRUE(handle.get()); | |
229 | |
230 WebKit::WebCryptoKey crypto_key = | |
231 WebKit::WebCryptoKey::create( | |
232 handle.release(), | |
233 type, | |
234 false, | |
235 hmac_algorithm, | |
236 WebKit::WebCryptoKeyUsageSign); | |
237 | |
238 std::vector<uint8> msg_raw; | |
Ryan Sleevi
2013/09/12 22:01:56
naming nit: msg -> message
Bryan Eyler
2013/09/12 23:09:56
Done.
| |
239 base::HexStringToBytes(input_set[index].msg, &msg_raw); | |
240 | |
241 WebKit::WebArrayBuffer array_buffer; | |
242 | |
243 EXPECT_TRUE( | |
244 crypto.SignInternal( | |
245 hmac_algorithm, | |
246 crypto_key, | |
247 msg_raw.data(), | |
248 msg_raw.size(), | |
249 &array_buffer)); | |
250 | |
251 // Ignore case, it's checking the hex value. | |
252 EXPECT_STRCASEEQ( | |
253 input_set[index].mac, | |
254 base::HexEncode( | |
255 array_buffer.data(), array_buffer.byteLength()).c_str()); | |
256 } | |
257 } | |
258 | |
120 } // namespace content | 259 } // namespace content |
OLD | NEW |