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

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

Issue 23569007: WebCrypto: Implement importKey() and sign() for HMAC in NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Re-submit with fixed external dependencies. Created 7 years, 3 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
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 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" 86 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc"
86 "22780d7e1b95bfeaa86a678e4552", 87 "22780d7e1b95bfeaa86a678e4552",
87 // echo -n -e "\000\001\002\003\004\005" | sha512sum 88 // echo -n -e "\000\001\002\003\004\005" | sha512sum
88 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" 89 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9"
89 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", 90 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c",
90 }, 91 },
91 }, 92 },
92 }; 93 };
93 94
94 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { 95 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) {
96 SCOPED_TRACE(id_index);
97
95 WebKit::WebCryptoAlgorithm algorithm( 98 WebKit::WebCryptoAlgorithm algorithm(
96 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( 99 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
97 kAlgorithmIds[id_index], NULL)); 100 kAlgorithmIds[id_index], NULL));
98 101
99 for (size_t set_index = 0; 102 for (size_t set_index = 0;
100 set_index < ARRAYSIZE_UNSAFE(input_set); 103 set_index < ARRAYSIZE_UNSAFE(input_set);
101 set_index++) { 104 set_index++) {
105 SCOPED_TRACE(set_index);
106
102 WebKit::WebArrayBuffer array_buffer; 107 WebKit::WebArrayBuffer array_buffer;
103 108
104 WebCryptoImpl crypto; 109 WebCryptoImpl crypto;
105 crypto.DigestInternal( 110 EXPECT_TRUE(
106 algorithm, 111 crypto.DigestInternal(
107 reinterpret_cast<const unsigned char*>(input_set[set_index].input), 112 algorithm,
108 input_set[set_index].input_length, 113 reinterpret_cast<const unsigned char*>(
109 &array_buffer); 114 input_set[set_index].input),
115 input_set[set_index].input_length,
116 &array_buffer));
110 117
111 // Ignore case, it's checking the hex value. 118 // Ignore case, it's checking the hex value.
112 EXPECT_STRCASEEQ( 119 EXPECT_STRCASEEQ(
113 input_set[set_index].hex_result[id_index], 120 input_set[set_index].hex_result[id_index],
114 base::HexEncode( 121 base::HexEncode(
115 array_buffer.data(), array_buffer.byteLength()).c_str()); 122 array_buffer.data(), array_buffer.byteLength()).c_str());
116 } 123 }
117 } 124 }
118 } 125 }
119 126
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
120 } // namespace content 277 } // namespace content
OLDNEW
« chrome/installer/linux/debian/expected_deps ('K') | « content/renderer/webcrypto_impl_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698