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

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: Fixes to review from eroman 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 };
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 input_set_struct {
125 WebKit::WebCryptoAlgorithmId algorithm;
126 const char* key;
127 const char* msg;
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",
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(input_set); index++) {
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() != NULL);
eroman 2013/09/06 23:03:16 nit: typical to leave off "!= NULL" in chromiumlan
Bryan Eyler 2013/09/09 22:32:10 Done.
229
230 std::vector<uint8> msg_raw;
231 base::HexStringToBytes(input_set[index].msg, &msg_raw);
232
233 WebKit::WebArrayBuffer array_buffer;
234
235 EXPECT_TRUE(
236 crypto.SignInternal(
237 hmac_algorithm,
238 handle.get(),
239 msg_raw.data(),
240 msg_raw.size(),
241 &array_buffer));
242
243 // Ignore case, it's checking the hex value.
244 EXPECT_STRCASEEQ(
245 input_set[index].mac,
246 base::HexEncode(
247 array_buffer.data(), array_buffer.byteLength()).c_str());
248 }
249 }
250
120 } // namespace content 251 } // namespace content
OLDNEW
« content/renderer/webcrypto_impl_nss.cc ('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