Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: content/renderer/webcrypto_impl_unittest.cc

Issue 24237013: Refactor webcrypto unittests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase onto master Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/renderer/webcrypto_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
18 18
19 namespace {
20
21 std::vector<uint8> HexStringToBytes(const std::string& hex) {
22 std::vector<uint8> bytes;
23 base::HexStringToBytes(hex, &bytes);
24 return bytes;
25 }
26
27 void ExpectArrayBufferMatchesHex(const std::string& expected_hex,
28 const WebKit::WebArrayBuffer& array_buffer) {
29 EXPECT_STRCASEEQ(
30 expected_hex.c_str(),
31 base::HexEncode(
32 array_buffer.data(), array_buffer.byteLength()).c_str());
33 }
34
35 WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) {
36 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL);
37 }
38
39 WebKit::WebCryptoAlgorithm CreateHmacAlgorithm(
40 WebKit::WebCryptoAlgorithmId hashId) {
41 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
42 WebKit::WebCryptoAlgorithmIdHmac,
43 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hashId)));
44 }
45
46 } // namespace
47
19 namespace content { 48 namespace content {
20 49
21 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { 50 class WebCryptoImplTest : public testing::Test {
22 WebKit::WebCryptoAlgorithmIdSha1, 51 protected:
23 WebKit::WebCryptoAlgorithmIdSha224, 52 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString(
24 WebKit::WebCryptoAlgorithmIdSha256, 53 const std::string& key_hex,
25 WebKit::WebCryptoAlgorithmIdSha384, 54 const WebKit::WebCryptoAlgorithm& algorithm,
26 WebKit::WebCryptoAlgorithmIdSha512 55 WebKit::WebCryptoKeyUsageMask usage) {
27 }; 56 WebKit::WebCryptoKeyType type;
57 scoped_ptr<WebKit::WebCryptoKeyHandle> handle;
28 58
29 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { 59 std::vector<uint8> key_raw = HexStringToBytes(key_hex);
60
61 EXPECT_TRUE(crypto_.ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw,
62 key_raw.data(),
63 key_raw.size(),
64 algorithm,
65 usage,
66 &handle,
67 &type));
68
69 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, type);
70 EXPECT_TRUE(handle.get());
71
72 return WebKit::WebCryptoKey::create(
73 handle.release(), type, false, algorithm, usage);
74 }
75
76 // Forwarding methods to gain access to protected methods of
77 // WebCryptoImpl.
78
79 bool DigestInternal(
80 const WebKit::WebCryptoAlgorithm& algorithm,
81 const unsigned char* data,
82 unsigned data_size,
83 WebKit::WebArrayBuffer* buffer) {
84 return crypto_.DigestInternal(algorithm, data, data_size, buffer);
85 }
86
87 bool ImportKeyInternal(
88 WebKit::WebCryptoKeyFormat format,
89 const unsigned char* key_data,
90 unsigned key_data_size,
91 const WebKit::WebCryptoAlgorithm& algorithm,
92 WebKit::WebCryptoKeyUsageMask usage_mask,
93 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle,
94 WebKit::WebCryptoKeyType* type) {
95 return crypto_.ImportKeyInternal(
96 format, key_data, key_data_size, algorithm, usage_mask, handle, type);
97 }
98
99 bool SignInternal(
100 const WebKit::WebCryptoAlgorithm& algorithm,
101 const WebKit::WebCryptoKey& key,
102 const unsigned char* data,
103 unsigned data_size,
104 WebKit::WebArrayBuffer* buffer) {
105 return crypto_.SignInternal(algorithm, key, data, data_size, buffer);
106 }
107
108 private:
109 WebCryptoImpl crypto_;
30 }; 110 };
31 111
32 TEST_F(WebCryptoImplTest, DigestSampleSets) { 112 TEST_F(WebCryptoImplTest, DigestSampleSets) {
33 // The results are stored here in hex format for readability. 113 // The results are stored here in hex format for readability.
34 // 114 //
35 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced 115 // 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 116 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03
37 struct { 117 //
38 const char* input; 118 // Results were generated using the command sha{1,224,256,384,512}sum.
39 unsigned input_length; 119 struct TestCase {
40 const char* hex_result[arraysize(kAlgorithmIds)]; 120 WebKit::WebCryptoAlgorithmId algorithm;
41 } input_set[] = { 121 const std::string hex_input;
42 { 122 const char* hex_result;
43 "", 0, 123 };
44 { 124
45 // echo -n "" | sha1sum 125 const TestCase kTests[] = {
46 "da39a3ee5e6b4b0d3255bfef95601890afd80709", 126 { WebKit::WebCryptoAlgorithmIdSha1, "",
47 // echo -n "" | sha224sum 127 "da39a3ee5e6b4b0d3255bfef95601890afd80709"
48 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
49 // echo -n "" | sha256sum
50 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
51 // echo -n "" | sha384sum
52 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e"
53 "debfe76f65fbd51ad2f14898b95b",
54 // echo -n "" | sha512sum
55 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0"
56 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
57 },
58 }, 128 },
59 { 129 { WebKit::WebCryptoAlgorithmIdSha224, "",
60 "\000", 1, 130 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
61 {
62 // echo -n -e "\000" | sha1sum
63 "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
64 // echo -n -e "\000" | sha224sum
65 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073",
66 // echo -n -e "\000" | sha256sum
67 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
68 // echo -n -e "\000" | sha384sum
69 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933"
70 "ec2b413465966817a9c208a11717",
71 // echo -n -e "\000" | sha512sum
72 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a"
73 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee",
74 },
75 }, 131 },
76 { 132 { WebKit::WebCryptoAlgorithmIdSha256, "",
77 "\000\001\002\003\004\005", 6, 133 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
78 { 134 },
79 // echo -n -e "\000\001\002\003\004\005" | sha1sum 135 { WebKit::WebCryptoAlgorithmIdSha384, "",
80 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", 136 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e"
81 // echo -n -e "\000\001\002\003\004\005" | sha224sum 137 "debfe76f65fbd51ad2f14898b95b"
82 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", 138 },
83 // echo -n -e "\000\001\002\003\004\005" | sha256sum 139 { WebKit::WebCryptoAlgorithmIdSha512, "",
84 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", 140 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0"
85 // echo -n -e "\000\001\002\003\004\005" | sha384sum 141 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
86 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" 142 },
87 "22780d7e1b95bfeaa86a678e4552", 143 { WebKit::WebCryptoAlgorithmIdSha1, "00",
88 // echo -n -e "\000\001\002\003\004\005" | sha512sum 144 "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
89 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" 145 },
90 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", 146 { WebKit::WebCryptoAlgorithmIdSha224, "00",
91 }, 147 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073",
148 },
149 { WebKit::WebCryptoAlgorithmIdSha256, "00",
150 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
151 },
152 { WebKit::WebCryptoAlgorithmIdSha384, "00",
153 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933"
154 "ec2b413465966817a9c208a11717",
155 },
156 { WebKit::WebCryptoAlgorithmIdSha512, "00",
157 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a"
158 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee",
159 },
160 { WebKit::WebCryptoAlgorithmIdSha1, "000102030405",
161 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9",
162 },
163 { WebKit::WebCryptoAlgorithmIdSha224, "000102030405",
164 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc",
165 },
166 { WebKit::WebCryptoAlgorithmIdSha256, "000102030405",
167 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43",
168 },
169 { WebKit::WebCryptoAlgorithmIdSha384, "000102030405",
170 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc"
171 "22780d7e1b95bfeaa86a678e4552",
172 },
173 { WebKit::WebCryptoAlgorithmIdSha512, "000102030405",
174 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9"
175 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c",
92 }, 176 },
93 }; 177 };
94 178
95 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { 179 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests);
96 SCOPED_TRACE(id_index); 180 ++test_index) {
181 SCOPED_TRACE(test_index);
182 const TestCase& test = kTests[test_index];
97 183
98 WebKit::WebCryptoAlgorithm algorithm( 184 WebKit::WebCryptoAlgorithm algorithm = CreateAlgorithm(test.algorithm);
99 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( 185 std::vector<uint8> input = HexStringToBytes(test.hex_input);
100 kAlgorithmIds[id_index], NULL));
101 186
102 for (size_t set_index = 0; 187 WebKit::WebArrayBuffer output;
103 set_index < ARRAYSIZE_UNSAFE(input_set); 188 ASSERT_TRUE(DigestInternal(algorithm, input.data(), input.size(), &output));
104 set_index++) { 189 ExpectArrayBufferMatchesHex(test.hex_result, output);
105 SCOPED_TRACE(set_index);
106
107 WebKit::WebArrayBuffer array_buffer;
108
109 WebCryptoImpl crypto;
110 EXPECT_TRUE(
111 crypto.DigestInternal(
112 algorithm,
113 reinterpret_cast<const unsigned char*>(
114 input_set[set_index].input),
115 input_set[set_index].input_length,
116 &array_buffer));
117
118 // Ignore case, it's checking the hex value.
119 EXPECT_STRCASEEQ(
120 input_set[set_index].hex_result[id_index],
121 base::HexEncode(
122 array_buffer.data(), array_buffer.byteLength()).c_str());
123 }
124 } 190 }
125 } 191 }
126 192
127 TEST_F(WebCryptoImplTest, HMACSampleSets) { 193 TEST_F(WebCryptoImplTest, HMACSampleSets) {
128 struct { 194 struct TestCase {
129 WebKit::WebCryptoAlgorithmId algorithm; 195 WebKit::WebCryptoAlgorithmId algorithm;
130 const char* key; 196 const char* key;
131 const char* message; 197 const char* message;
132 const char* mac; 198 const char* mac;
133 } input_set[] = { 199 };
200
201 const TestCase kTests[] = {
134 // Empty sets. Result generated via OpenSSL commandline tool. These 202 // Empty sets. Result generated via OpenSSL commandline tool. These
135 // particular results are also posted on the Wikipedia page examples: 203 // particular results are also posted on the Wikipedia page examples:
136 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code 204 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
137 { 205 {
138 WebKit::WebCryptoAlgorithmIdSha1, 206 WebKit::WebCryptoAlgorithmIdSha1,
139 "", 207 "",
140 "", 208 "",
141 // openssl dgst -sha1 -hmac "" < /dev/null 209 // openssl dgst -sha1 -hmac "" < /dev/null
142 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", 210 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d",
143 }, 211 },
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 // message 271 // message
204 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" 272 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69"
205 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" 273 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e"
206 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" 274 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c"
207 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", 275 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99",
208 // mac 276 // mac
209 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", 277 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b",
210 }, 278 },
211 }; 279 };
212 280
213 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(input_set); index++) { 281 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests);
214 SCOPED_TRACE(index); 282 ++test_index) {
283 SCOPED_TRACE(test_index);
284 const TestCase& test = kTests[test_index];
215 285
216 WebKit::WebCryptoAlgorithm hash_algorithm( 286 WebKit::WebCryptoAlgorithm algorithm = CreateHmacAlgorithm(test.algorithm);
217 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
218 input_set[index].algorithm, NULL));
219 287
220 scoped_ptr<WebKit::WebCryptoHmacParams> hmac_params( 288 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString(
221 new WebKit::WebCryptoHmacParams(hash_algorithm)); 289 test.key, algorithm, WebKit::WebCryptoKeyUsageSign);
222 290
223 WebKit::WebCryptoAlgorithm hmac_algorithm( 291 std::vector<uint8> message_raw = HexStringToBytes(test.message);
224 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
225 WebKit::WebCryptoAlgorithmIdHmac, hmac_params.release()));
226 292
227 WebKit::WebCryptoKeyType type; 293 WebKit::WebArrayBuffer output;
228 scoped_ptr<WebKit::WebCryptoKeyHandle> handle;
229 294
230 std::vector<uint8> key_raw; 295 ASSERT_TRUE(SignInternal(
231 base::HexStringToBytes(input_set[index].key, &key_raw); 296 algorithm, key, message_raw.data(), message_raw.size(), &output));
232 297
233 WebCryptoImpl crypto; 298 ExpectArrayBufferMatchesHex(test.mac, output);
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 } 299 }
275 } 300 }
276 301
277 } // namespace content 302 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/webcrypto_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698