OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/stl_util.h" | 5 #include "base/stl_util.h" |
6 #include "content/child/webcrypto/algorithm_dispatch.h" | 6 #include "content/child/webcrypto/algorithm_dispatch.h" |
7 #include "content/child/webcrypto/crypto_data.h" | 7 #include "content/child/webcrypto/crypto_data.h" |
8 #include "content/child/webcrypto/jwk.h" | 8 #include "content/child/webcrypto/jwk.h" |
9 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
10 #include "content/child/webcrypto/test/test_helpers.h" | 10 #include "content/child/webcrypto/test/test_helpers.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 std::vector<uint8_t> expected_bytes = | 119 std::vector<uint8_t> expected_bytes = |
120 GetBytesFromHexString(test, "derived_bytes"); | 120 GetBytesFromHexString(test, "derived_bytes"); |
121 | 121 |
122 EXPECT_EQ(CryptoData(expected_bytes), CryptoData(derived_bytes)); | 122 EXPECT_EQ(CryptoData(expected_bytes), CryptoData(derived_bytes)); |
123 } | 123 } |
124 } | 124 } |
125 | 125 |
126 // Loads up a test ECDH public and private key for P-521. The keys | 126 // Loads up a test ECDH public and private key for P-521. The keys |
127 // come from different key pairs, and can be used for key derivation of up to | 127 // come from different key pairs, and can be used for key derivation of up to |
128 // 528 bits. | 128 // 528 bits. |
129 void LoadTestKeys(blink::WebCryptoKey* public_key, | 129 ::testing::AssertionResult LoadTestKeys(blink::WebCryptoKey* public_key, |
130 blink::WebCryptoKey* private_key) { | 130 blink::WebCryptoKey* private_key) { |
eroman
2014/12/29 18:01:04
run "git cl format" to fix these sorts of formatti
Habib Virji
2014/12/30 17:22:48
It do not remember, but will keep an eye on runnin
| |
131 // Assume that the 7th key in the test data is for P-521. | |
132 scoped_ptr<base::ListValue> tests; | 131 scoped_ptr<base::ListValue> tests; |
133 ASSERT_TRUE(ReadJsonTestFileToList("ecdh.json", &tests)); | 132 EXPECT_TRUE(ReadJsonTestFileToList("ecdh.json", &tests)); |
eroman
2014/12/29 18:01:04
Please change to:
if (!ReadJsonTestFileToList(...
Habib Virji
2014/12/30 17:22:48
Done.
| |
134 | 133 |
135 const base::DictionaryValue* test; | 134 const base::DictionaryValue* test; |
136 ASSERT_TRUE(tests->GetDictionary(6, &test)); | 135 bool valid_p521_keys = false; |
136 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | |
137 SCOPED_TRACE(test_index); | |
138 EXPECT_TRUE(tests->GetDictionary(test_index, &test)); | |
139 test->GetBoolean("valid_p521_keys", &valid_p521_keys); | |
140 if (valid_p521_keys) | |
141 break; | |
142 else if (!valid_p521_keys && tests->GetSize() == test_index + 1) | |
eroman
2014/12/29 18:01:04
use curly braces around this:
else if (...) {
.
Habib Virji
2014/12/30 17:22:48
Done.
| |
143 return ::testing::AssertionFailure() << | |
144 "The P-521 test are missing in ecdh.json"; | |
145 } | |
137 | 146 |
138 ImportKeysFromTest(test, public_key, private_key); | 147 ImportKeysFromTest(test, public_key, private_key); |
139 | 148 |
140 ASSERT_EQ(blink::WebCryptoNamedCurveP521, | 149 EXPECT_EQ(blink::WebCryptoNamedCurveP521, |
141 public_key->algorithm().ecParams()->namedCurve()); | 150 public_key->algorithm().ecParams()->namedCurve()); |
151 | |
152 return ::testing::AssertionSuccess(); | |
142 } | 153 } |
143 | 154 |
144 // Try deriving an AES key of length 129 bits. | 155 // Try deriving an AES key of length 129 bits. |
145 TEST(WebCryptoEcdhTest, DeriveKeyBadAesLength) { | 156 TEST(WebCryptoEcdhTest, DeriveKeyBadAesLength) { |
146 if (!SupportsEcdh()) | 157 if (!SupportsEcdh()) |
147 return; | 158 return; |
148 | 159 |
149 blink::WebCryptoKey public_key; | 160 blink::WebCryptoKey public_key; |
150 blink::WebCryptoKey base_key; | 161 blink::WebCryptoKey base_key; |
151 LoadTestKeys(&public_key, &base_key); | 162 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
152 | 163 |
153 blink::WebCryptoKey derived_key; | 164 blink::WebCryptoKey derived_key; |
154 | 165 |
155 ASSERT_EQ(Status::ErrorGetAesKeyLength(), | 166 ASSERT_EQ(Status::ErrorGetAesKeyLength(), |
156 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 167 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
157 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | 168 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), |
158 CreateAesGcmDerivedKeyParams(129), true, | 169 CreateAesGcmDerivedKeyParams(129), true, |
159 blink::WebCryptoKeyUsageEncrypt, &derived_key)); | 170 blink::WebCryptoKeyUsageEncrypt, &derived_key)); |
160 } | 171 } |
161 | 172 |
162 // Try deriving an AES key of length 192 bits. | 173 // Try deriving an AES key of length 192 bits. |
163 TEST(WebCryptoEcdhTest, DeriveKeyUnsupportedAesLength) { | 174 TEST(WebCryptoEcdhTest, DeriveKeyUnsupportedAesLength) { |
164 if (!SupportsEcdh()) | 175 if (!SupportsEcdh()) |
165 return; | 176 return; |
166 | 177 |
167 blink::WebCryptoKey public_key; | 178 blink::WebCryptoKey public_key; |
168 blink::WebCryptoKey base_key; | 179 blink::WebCryptoKey base_key; |
169 LoadTestKeys(&public_key, &base_key); | 180 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
170 | 181 |
171 blink::WebCryptoKey derived_key; | 182 blink::WebCryptoKey derived_key; |
172 | 183 |
173 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), | 184 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), |
174 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 185 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
175 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | 186 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), |
176 CreateAesGcmDerivedKeyParams(192), true, | 187 CreateAesGcmDerivedKeyParams(192), true, |
177 blink::WebCryptoKeyUsageEncrypt, &derived_key)); | 188 blink::WebCryptoKeyUsageEncrypt, &derived_key)); |
178 } | 189 } |
179 | 190 |
180 // Try deriving an HMAC key of length 0 bits. | 191 // Try deriving an HMAC key of length 0 bits. |
181 TEST(WebCryptoEcdhTest, DeriveKeyZeroLengthHmac) { | 192 TEST(WebCryptoEcdhTest, DeriveKeyZeroLengthHmac) { |
182 if (!SupportsEcdh()) | 193 if (!SupportsEcdh()) |
183 return; | 194 return; |
184 | 195 |
185 blink::WebCryptoKey public_key; | 196 blink::WebCryptoKey public_key; |
186 blink::WebCryptoKey base_key; | 197 blink::WebCryptoKey base_key; |
187 LoadTestKeys(&public_key, &base_key); | 198 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
188 | 199 |
189 blink::WebCryptoKey derived_key; | 200 blink::WebCryptoKey derived_key; |
190 | 201 |
191 const blink::WebCryptoAlgorithm import_algorithm = | 202 const blink::WebCryptoAlgorithm import_algorithm = |
192 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 203 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
193 | 204 |
194 ASSERT_EQ(Status::ErrorGetHmacKeyLengthZero(), | 205 ASSERT_EQ(Status::ErrorGetHmacKeyLengthZero(), |
195 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 206 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
196 import_algorithm, import_algorithm, true, | 207 import_algorithm, import_algorithm, true, |
197 blink::WebCryptoKeyUsageSign, &derived_key)); | 208 blink::WebCryptoKeyUsageSign, &derived_key)); |
198 } | 209 } |
199 | 210 |
200 // Derive an HMAC key of length 19 bits. | 211 // Derive an HMAC key of length 19 bits. |
201 TEST(WebCryptoEcdhTest, DeriveKeyHmac19Bits) { | 212 TEST(WebCryptoEcdhTest, DeriveKeyHmac19Bits) { |
202 if (!SupportsEcdh()) | 213 if (!SupportsEcdh()) |
203 return; | 214 return; |
204 | 215 |
205 blink::WebCryptoKey public_key; | 216 blink::WebCryptoKey public_key; |
206 blink::WebCryptoKey base_key; | 217 blink::WebCryptoKey base_key; |
207 LoadTestKeys(&public_key, &base_key); | 218 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
208 | 219 |
209 blink::WebCryptoKey derived_key; | 220 blink::WebCryptoKey derived_key; |
210 | 221 |
211 const blink::WebCryptoAlgorithm import_algorithm = | 222 const blink::WebCryptoAlgorithm import_algorithm = |
212 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1, 19); | 223 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1, 19); |
213 | 224 |
214 ASSERT_EQ(Status::Success(), | 225 ASSERT_EQ(Status::Success(), |
215 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 226 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
216 import_algorithm, import_algorithm, true, | 227 import_algorithm, import_algorithm, true, |
217 blink::WebCryptoKeyUsageSign, &derived_key)); | 228 blink::WebCryptoKeyUsageSign, &derived_key)); |
(...skipping 12 matching lines...) Expand all Loading... | |
230 EXPECT_EQ(0, raw_key[raw_key.size() - 1] & 0x1f); | 241 EXPECT_EQ(0, raw_key[raw_key.size() - 1] & 0x1f); |
231 } | 242 } |
232 | 243 |
233 // Derive an HMAC key with no specified length (just the hash of SHA-256). | 244 // Derive an HMAC key with no specified length (just the hash of SHA-256). |
234 TEST(WebCryptoEcdhTest, DeriveKeyHmacSha256NoLength) { | 245 TEST(WebCryptoEcdhTest, DeriveKeyHmacSha256NoLength) { |
235 if (!SupportsEcdh()) | 246 if (!SupportsEcdh()) |
236 return; | 247 return; |
237 | 248 |
238 blink::WebCryptoKey public_key; | 249 blink::WebCryptoKey public_key; |
239 blink::WebCryptoKey base_key; | 250 blink::WebCryptoKey base_key; |
240 LoadTestKeys(&public_key, &base_key); | 251 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
241 | 252 |
242 blink::WebCryptoKey derived_key; | 253 blink::WebCryptoKey derived_key; |
243 | 254 |
244 const blink::WebCryptoAlgorithm import_algorithm = | 255 const blink::WebCryptoAlgorithm import_algorithm = |
245 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha256); | 256 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha256); |
246 | 257 |
247 ASSERT_EQ(Status::Success(), | 258 ASSERT_EQ(Status::Success(), |
248 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 259 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
249 import_algorithm, import_algorithm, true, | 260 import_algorithm, import_algorithm, true, |
250 blink::WebCryptoKeyUsageSign, &derived_key)); | 261 blink::WebCryptoKeyUsageSign, &derived_key)); |
(...skipping 18 matching lines...) Expand all Loading... | |
269 // In practice, authors won't be directly generating keys from key agreement | 280 // In practice, authors won't be directly generating keys from key agreement |
270 // schemes, as that is frequently insecure, and instead be using KDFs to expand | 281 // schemes, as that is frequently insecure, and instead be using KDFs to expand |
271 // and generate keys. For simplicity of testing, however, test using an HMAC | 282 // and generate keys. For simplicity of testing, however, test using an HMAC |
272 // key. | 283 // key. |
273 TEST(WebCryptoEcdhTest, DeriveKeyHmacSha512NoLength) { | 284 TEST(WebCryptoEcdhTest, DeriveKeyHmacSha512NoLength) { |
274 if (!SupportsEcdh()) | 285 if (!SupportsEcdh()) |
275 return; | 286 return; |
276 | 287 |
277 blink::WebCryptoKey public_key; | 288 blink::WebCryptoKey public_key; |
278 blink::WebCryptoKey base_key; | 289 blink::WebCryptoKey base_key; |
279 LoadTestKeys(&public_key, &base_key); | 290 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
280 | 291 |
281 blink::WebCryptoKey derived_key; | 292 blink::WebCryptoKey derived_key; |
282 | 293 |
283 const blink::WebCryptoAlgorithm import_algorithm = | 294 const blink::WebCryptoAlgorithm import_algorithm = |
284 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha512); | 295 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha512); |
285 | 296 |
286 ASSERT_EQ(Status::ErrorEcdhLengthTooBig(528), | 297 ASSERT_EQ(Status::ErrorEcdhLengthTooBig(528), |
287 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 298 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
288 import_algorithm, import_algorithm, true, | 299 import_algorithm, import_algorithm, true, |
289 blink::WebCryptoKeyUsageSign, &derived_key)); | 300 blink::WebCryptoKeyUsageSign, &derived_key)); |
290 } | 301 } |
291 | 302 |
292 // Try deriving an AES key of length 128 bits. | 303 // Try deriving an AES key of length 128 bits. |
293 TEST(WebCryptoEcdhTest, DeriveKeyAes128) { | 304 TEST(WebCryptoEcdhTest, DeriveKeyAes128) { |
294 if (!SupportsEcdh()) | 305 if (!SupportsEcdh()) |
295 return; | 306 return; |
296 | 307 |
297 blink::WebCryptoKey public_key; | 308 blink::WebCryptoKey public_key; |
298 blink::WebCryptoKey base_key; | 309 blink::WebCryptoKey base_key; |
299 LoadTestKeys(&public_key, &base_key); | 310 ASSERT_TRUE(LoadTestKeys(&public_key, &base_key)); |
300 | 311 |
301 blink::WebCryptoKey derived_key; | 312 blink::WebCryptoKey derived_key; |
302 | 313 |
303 ASSERT_EQ(Status::Success(), | 314 ASSERT_EQ(Status::Success(), |
304 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, | 315 DeriveKey(CreateEcdhDeriveParams(public_key), base_key, |
305 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | 316 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), |
306 CreateAesGcmDerivedKeyParams(128), true, | 317 CreateAesGcmDerivedKeyParams(128), true, |
307 blink::WebCryptoKeyUsageEncrypt, &derived_key)); | 318 blink::WebCryptoKeyUsageEncrypt, &derived_key)); |
308 | 319 |
309 ASSERT_EQ(blink::WebCryptoAlgorithmIdAesGcm, derived_key.algorithm().id()); | 320 ASSERT_EQ(blink::WebCryptoAlgorithmIdAesGcm, derived_key.algorithm().id()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 CryptoData(MakeJsonVector(*private_key_json)), | 359 CryptoData(MakeJsonVector(*private_key_json)), |
349 CreateEcdhImportAlgorithm(curve), true, | 360 CreateEcdhImportAlgorithm(curve), true, |
350 0, &key)); | 361 0, &key)); |
351 } | 362 } |
352 | 363 |
353 } // namespace | 364 } // namespace |
354 | 365 |
355 } // namespace webcrypto | 366 } // namespace webcrypto |
356 | 367 |
357 } // namespace content | 368 } // namespace content |
OLD | NEW |