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

Side by Side Diff: content/renderer/webcrypto/shared_crypto_unittest.cc

Issue 155623005: Refactor to share more code between OpenSSL and NSS implementations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make CryptoData ctors explicit, and other comments Created 6 years, 10 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
OLDNEW
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 "content/renderer/webcrypto/webcrypto_impl.h" 5 #include "content/renderer/webcrypto/shared_crypto.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "content/public/common/content_paths.h" 20 #include "content/public/common/content_paths.h"
21 #include "content/public/renderer/content_renderer_client.h" 21 #include "content/public/renderer/content_renderer_client.h"
22 #include "content/renderer/renderer_webkitplatformsupport_impl.h" 22 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
23 #include "content/renderer/webcrypto/crypto_data.h"
23 #include "content/renderer/webcrypto/webcrypto_util.h" 24 #include "content/renderer/webcrypto/webcrypto_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" 26 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 28 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
28 #include "third_party/WebKit/public/platform/WebCryptoKey.h" 29 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
29 #include "third_party/re2/re2/re2.h" 30 #include "third_party/re2/re2/re2.h"
30 31
31 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of 32 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of
32 // the tests: http://crbug.com/267888 33 // the tests: http://crbug.com/267888
33 #if defined(USE_OPENSSL) 34 #if defined(USE_OPENSSL)
34 #define MAYBE(test_name) DISABLED_##test_name 35 #define MAYBE(test_name) DISABLED_##test_name
35 #else 36 #else
36 #define MAYBE(test_name) test_name 37 #define MAYBE(test_name) test_name
37 #endif 38 #endif
38 39
39 // Helper macros to verify the value of a Status. 40 // Helper macros to verify the value of a Status.
40 #define EXPECT_STATUS_ERROR(code) EXPECT_FALSE((code).IsSuccess()) 41 #define EXPECT_STATUS_ERROR(code) EXPECT_FALSE((code).IsSuccess())
41 #define EXPECT_STATUS(expected, code) \ 42 #define EXPECT_STATUS(expected, code) \
42 EXPECT_EQ(expected.ToString(), (code).ToString()) 43 EXPECT_EQ(expected.ToString(), (code).ToString())
43 #define ASSERT_STATUS(expected, code) \ 44 #define ASSERT_STATUS(expected, code) \
44 ASSERT_EQ(expected.ToString(), (code).ToString()) 45 ASSERT_EQ(expected.ToString(), (code).ToString())
45 #define EXPECT_STATUS_SUCCESS(code) EXPECT_STATUS(Status::Success(), code) 46 #define EXPECT_STATUS_SUCCESS(code) EXPECT_STATUS(Status::Success(), code)
46 #define ASSERT_STATUS_SUCCESS(code) ASSERT_STATUS(Status::Success(), code) 47 #define ASSERT_STATUS_SUCCESS(code) ASSERT_STATUS(Status::Success(), code)
47 48
48 namespace content { 49 namespace content {
49 50
50 using webcrypto::Status; 51 namespace webcrypto {
51 52
52 namespace { 53 namespace {
53 54
54 // Returns a slightly modified version of the input vector. 55 // Returns a slightly modified version of the input vector.
55 // 56 //
56 // - For non-empty inputs a single bit is inverted. 57 // - For non-empty inputs a single bit is inverted.
57 // - For empty inputs, a byte is added. 58 // - For empty inputs, a byte is added.
58 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { 59 std::vector<uint8> Corrupted(const std::vector<uint8>& input) {
59 std::vector<uint8> corrupted_data(input); 60 std::vector<uint8> corrupted_data(input);
60 if (corrupted_data.empty()) 61 if (corrupted_data.empty())
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } kDigestNameToId[] = { 192 } kDigestNameToId[] = {
192 {"sha-1", blink::WebCryptoAlgorithmIdSha1}, 193 {"sha-1", blink::WebCryptoAlgorithmIdSha1},
193 {"sha-224", blink::WebCryptoAlgorithmIdSha224}, 194 {"sha-224", blink::WebCryptoAlgorithmIdSha224},
194 {"sha-256", blink::WebCryptoAlgorithmIdSha256}, 195 {"sha-256", blink::WebCryptoAlgorithmIdSha256},
195 {"sha-384", blink::WebCryptoAlgorithmIdSha384}, 196 {"sha-384", blink::WebCryptoAlgorithmIdSha384},
196 {"sha-512", blink::WebCryptoAlgorithmIdSha512}, 197 {"sha-512", blink::WebCryptoAlgorithmIdSha512},
197 }; 198 };
198 199
199 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) { 200 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) {
200 if (kDigestNameToId[i].name == algorithm_name) 201 if (kDigestNameToId[i].name == algorithm_name)
201 return webcrypto::CreateAlgorithm(kDigestNameToId[i].id); 202 return CreateAlgorithm(kDigestNameToId[i].id);
202 } 203 }
203 204
204 return blink::WebCryptoAlgorithm::createNull(); 205 return blink::WebCryptoAlgorithm::createNull();
205 } 206 }
206 207
207 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON 208 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON
208 // dictionary to a good state 209 // dictionary to a good state
209 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { 210 void RestoreJwkOctDictionary(base::DictionaryValue* dict) {
210 dict->Clear(); 211 dict->Clear();
211 dict->SetString("kty", "oct"); 212 dict->SetString("kty", "oct");
212 dict->SetString("alg", "A128CBC"); 213 dict->SetString("alg", "A128CBC");
213 dict->SetString("use", "enc"); 214 dict->SetString("use", "enc");
214 dict->SetBoolean("extractable", false); 215 dict->SetBoolean("extractable", false);
215 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); 216 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg==");
216 } 217 }
217 218
218 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( 219 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
219 const std::vector<uint8>& iv, 220 const std::vector<uint8>& iv,
220 const std::vector<uint8>& additional_data, 221 const std::vector<uint8>& additional_data,
221 unsigned int tag_length_bits) { 222 unsigned int tag_length_bits) {
222 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 223 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
223 blink::WebCryptoAlgorithmIdAesGcm, 224 blink::WebCryptoAlgorithmIdAesGcm,
224 new blink::WebCryptoAesGcmParams( 225 new blink::WebCryptoAesGcmParams(
225 webcrypto::Uint8VectorStart(iv), iv.size(), 226 Uint8VectorStart(iv), iv.size(),
226 true, 227 true,
227 webcrypto::Uint8VectorStart(additional_data), 228 Uint8VectorStart(additional_data),
228 additional_data.size(), 229 additional_data.size(),
229 true, tag_length_bits)); 230 true, tag_length_bits));
230 } 231 }
231 232
232 // Helper for ImportJwkRsaFailures. Restores the JWK JSON 233 // Helper for ImportJwkRsaFailures. Restores the JWK JSON
233 // dictionary to a good state 234 // dictionary to a good state
234 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { 235 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) {
235 dict->Clear(); 236 dict->Clear();
236 dict->SetString("kty", "RSA"); 237 dict->SetString("kty", "RSA");
237 dict->SetString("alg", "RSA1_5"); 238 dict->SetString("alg", "RSA1_5");
238 dict->SetString("use", "enc"); 239 dict->SetString("use", "enc");
239 dict->SetBoolean("extractable", false); 240 dict->SetBoolean("extractable", false);
240 dict->SetString("n", 241 dict->SetString("n",
241 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" 242 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk"
242 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" 243 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm"
243 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); 244 "e7PUJHYW1PW6ENTP0ibeiNOfFvs");
244 dict->SetString("e", "AQAB"); 245 dict->SetString("e", "AQAB");
245 } 246 }
246 247
247 blink::WebCryptoAlgorithm CreateRsaAlgorithmWithInnerHash( 248 blink::WebCryptoAlgorithm CreateRsaAlgorithmWithInnerHash(
248 blink::WebCryptoAlgorithmId algorithm_id, 249 blink::WebCryptoAlgorithmId algorithm_id,
249 blink::WebCryptoAlgorithmId hash_id) { 250 blink::WebCryptoAlgorithmId hash_id) {
250 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || 251 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
251 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); 252 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
252 DCHECK(webcrypto::IsHashAlgorithm(hash_id)); 253 DCHECK(IsHashAlgorithm(hash_id));
253 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 254 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
254 algorithm_id, 255 algorithm_id,
255 new blink::WebCryptoRsaSsaParams(webcrypto::CreateAlgorithm(hash_id))); 256 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id)));
256 } 257 }
257 258
258 // Determines if two ArrayBuffers have identical content. 259 // Determines if two ArrayBuffers have identical content.
259 bool ArrayBuffersEqual( 260 bool ArrayBuffersEqual(
260 const blink::WebArrayBuffer& a, 261 const blink::WebArrayBuffer& a,
261 const blink::WebArrayBuffer& b) { 262 const blink::WebArrayBuffer& b) {
262 return a.byteLength() == b.byteLength() && 263 return a.byteLength() == b.byteLength() &&
263 memcmp(a.data(), b.data(), a.byteLength()) == 0; 264 memcmp(a.data(), b.data(), a.byteLength()) == 0;
264 } 265 }
265 266
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72" 329 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72"
329 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca" 330 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca"
330 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d" 331 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d"
331 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71" 332 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71"
332 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd" 333 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd"
333 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027" 334 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027"
334 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319" 335 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319"
335 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" 336 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24"
336 "a79f4d"; 337 "a79f4d";
337 338
338 } // namespace 339 class SharedCryptoTest : public testing::Test {
340 protected:
341 virtual void SetUp() OVERRIDE {
342 Init();
343 }
344 };
339 345
340 class WebCryptoImplTest : public testing::Test { 346 // Wrappers to pass vector<> in place of CryptoData.
Ryan Sleevi 2014/02/13 04:24:24 This is not clear as to why this exists.
eroman 2014/02/13 23:05:38 Because otherwise this refactor has a very large i
341 protected: 347
348 Status ImportKeyInternal(blink::WebCryptoKeyFormat format,
349 const std::vector<uint8>& key_data,
350 const blink::WebCryptoAlgorithm& algorithm,
351 bool extractable,
352 blink::WebCryptoKeyUsageMask usage_mask,
353 blink::WebCryptoKey* key) {
354 return ImportKey(format,
Ryan Sleevi 2014/02/13 04:24:24 This is... a very odd wrap. That Internal() calls
eroman 2014/02/13 23:05:38 I changed the wrapping. The naming containing "In
355 CryptoData(key_data),
356 algorithm,
357 extractable,
358 usage_mask,
359 key);
360 }
361
362 Status EncryptInternal(const blink::WebCryptoAlgorithm& algorithm,
363 const blink::WebCryptoKey& key,
364 const std::vector<uint8>& data,
365 blink::WebArrayBuffer* buffer) {
366 return Encrypt(algorithm, key, CryptoData(data), buffer);
367 }
368
369 Status DecryptInternal(const blink::WebCryptoAlgorithm& algorithm,
370 const blink::WebCryptoKey& key,
371 const std::vector<uint8>& data,
372 blink::WebArrayBuffer* buffer) {
373 return Decrypt(algorithm, key, CryptoData(data), buffer);
374 }
375
376
377 // TODO: de-indent.
342 blink::WebCryptoKey ImportSecretKeyFromRaw( 378 blink::WebCryptoKey ImportSecretKeyFromRaw(
343 const std::vector<uint8>& key_raw, 379 const std::vector<uint8>& key_raw,
344 const blink::WebCryptoAlgorithm& algorithm, 380 const blink::WebCryptoAlgorithm& algorithm,
345 blink::WebCryptoKeyUsageMask usage) { 381 blink::WebCryptoKeyUsageMask usage) {
346 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 382 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
347 bool extractable = true; 383 bool extractable = true;
348 EXPECT_STATUS_SUCCESS( 384 EXPECT_STATUS_SUCCESS(
349 crypto_.ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 385 ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
350 webcrypto::Uint8VectorStart(key_raw), 386 key_raw,
351 key_raw.size(), 387 algorithm,
352 algorithm, 388 extractable,
353 extractable, 389 usage,
354 usage, 390 &key));
355 &key));
356 391
357 EXPECT_FALSE(key.isNull()); 392 EXPECT_FALSE(key.isNull());
358 EXPECT_TRUE(key.handle()); 393 EXPECT_TRUE(key.handle());
359 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 394 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
360 EXPECT_EQ(algorithm.id(), key.algorithm().id()); 395 EXPECT_EQ(algorithm.id(), key.algorithm().id());
361 EXPECT_EQ(extractable, key.extractable()); 396 EXPECT_EQ(extractable, key.extractable());
362 EXPECT_EQ(usage, key.usages()); 397 EXPECT_EQ(usage, key.usages());
363 return key; 398 return key;
364 } 399 }
365 400
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 EXPECT_EQ(extractable, extractable); 434 EXPECT_EQ(extractable, extractable);
400 EXPECT_EQ(usage_mask, private_key->usages()); 435 EXPECT_EQ(usage_mask, private_key->usages());
401 } 436 }
402 437
403 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a 438 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a
404 // runtime dependency. Test it by trying to import a key. 439 // runtime dependency. Test it by trying to import a key.
405 bool SupportsAesGcm() { 440 bool SupportsAesGcm() {
406 std::vector<uint8> key_raw(16, 0); 441 std::vector<uint8> key_raw(16, 0);
407 442
408 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 443 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
409 Status status = crypto_.ImportKeyInternal( 444 Status status = ImportKeyInternal(
410 blink::WebCryptoKeyFormatRaw, 445 blink::WebCryptoKeyFormatRaw,
411 webcrypto::Uint8VectorStart(key_raw), 446 key_raw,
412 key_raw.size(), 447 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
413 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
414 true, 448 true,
415 blink::WebCryptoKeyUsageEncrypt, 449 blink::WebCryptoKeyUsageEncrypt,
416 &key); 450 &key);
417 451
418 if (status.IsError()) 452 if (status.IsError())
419 EXPECT_EQ(Status::ErrorUnsupported().ToString(), status.ToString()); 453 EXPECT_EQ(Status::ErrorUnsupported().ToString(), status.ToString());
420 return status.IsSuccess(); 454 return status.IsSuccess();
421
422 } 455 }
423 456
424 Status AesGcmEncrypt(const blink::WebCryptoKey& key, 457 Status AesGcmEncrypt(const blink::WebCryptoKey& key,
425 const std::vector<uint8>& iv, 458 const std::vector<uint8>& iv,
426 const std::vector<uint8>& additional_data, 459 const std::vector<uint8>& additional_data,
427 unsigned int tag_length_bits, 460 unsigned int tag_length_bits,
428 const std::vector<uint8>& plain_text, 461 const std::vector<uint8>& plain_text,
429 std::vector<uint8>* cipher_text, 462 std::vector<uint8>* cipher_text,
430 std::vector<uint8>* authentication_tag) { 463 std::vector<uint8>* authentication_tag) {
431 blink::WebCryptoAlgorithm algorithm = CreateAesGcmAlgorithm( 464 blink::WebCryptoAlgorithm algorithm = CreateAesGcmAlgorithm(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 cipher_text.size() + authentication_tag.size()); 502 cipher_text.size() + authentication_tag.size());
470 cipher_text_with_tag.insert( 503 cipher_text_with_tag.insert(
471 cipher_text_with_tag.end(), cipher_text.begin(), cipher_text.end()); 504 cipher_text_with_tag.end(), cipher_text.begin(), cipher_text.end());
472 cipher_text_with_tag.insert( 505 cipher_text_with_tag.insert(
473 cipher_text_with_tag.end(), authentication_tag.begin(), 506 cipher_text_with_tag.end(), authentication_tag.begin(),
474 authentication_tag.end()); 507 authentication_tag.end());
475 508
476 return DecryptInternal(algorithm, key, cipher_text_with_tag, plain_text); 509 return DecryptInternal(algorithm, key, cipher_text_with_tag, plain_text);
477 } 510 }
478 511
479 // Forwarding methods to gain access to protected methods of 512 // Helpers to pass vector<> in place of CryptoData.
480 // WebCryptoImpl.
481
482 Status DigestInternal( 513 Status DigestInternal(
483 const blink::WebCryptoAlgorithm& algorithm, 514 const blink::WebCryptoAlgorithm& algorithm,
484 const std::vector<uint8>& data, 515 const std::vector<uint8>& data,
485 blink::WebArrayBuffer* buffer) { 516 blink::WebArrayBuffer* buffer) {
486 return crypto_.DigestInternal( 517 return Digest(algorithm, CryptoData(data), buffer);
487 algorithm, webcrypto::Uint8VectorStart(data), data.size(), buffer);
488 }
489
490 Status GenerateKeyInternal(
491 const blink::WebCryptoAlgorithm& algorithm,
492 blink::WebCryptoKey* key) {
493 bool extractable = true;
494 blink::WebCryptoKeyUsageMask usage_mask = 0;
495 return crypto_.GenerateSecretKeyInternal(
496 algorithm, extractable, usage_mask, key);
497 }
498
499 Status GenerateKeyPairInternal(
500 const blink::WebCryptoAlgorithm& algorithm,
501 bool extractable,
502 blink::WebCryptoKeyUsageMask usage_mask,
503 blink::WebCryptoKey* public_key,
504 blink::WebCryptoKey* private_key) {
505 return crypto_.GenerateKeyPairInternal(
506 algorithm, extractable, usage_mask, public_key, private_key);
507 }
508
509 Status ImportKeyInternal(
510 blink::WebCryptoKeyFormat format,
511 const std::vector<uint8>& key_data,
512 const blink::WebCryptoAlgorithm& algorithm,
513 bool extractable,
514 blink::WebCryptoKeyUsageMask usage_mask,
515 blink::WebCryptoKey* key) {
516 return crypto_.ImportKeyInternal(format,
517 webcrypto::Uint8VectorStart(key_data),
518 key_data.size(),
519 algorithm,
520 extractable,
521 usage_mask,
522 key);
523 }
524
525 Status ExportKeyInternal(
526 blink::WebCryptoKeyFormat format,
527 const blink::WebCryptoKey& key,
528 blink::WebArrayBuffer* buffer) {
529 return crypto_.ExportKeyInternal(format, key, buffer);
530 } 518 }
531 519
532 Status SignInternal( 520 Status SignInternal(
533 const blink::WebCryptoAlgorithm& algorithm, 521 const blink::WebCryptoAlgorithm& algorithm,
534 const blink::WebCryptoKey& key, 522 const blink::WebCryptoKey& key,
535 const std::vector<uint8>& data, 523 const std::vector<uint8>& data,
536 blink::WebArrayBuffer* buffer) { 524 blink::WebArrayBuffer* buffer) {
537 return crypto_.SignInternal( 525 return Sign(algorithm, key, CryptoData(data), buffer);
538 algorithm, key, webcrypto::Uint8VectorStart(data), data.size(), buffer);
539 } 526 }
540 527
541 Status VerifySignatureInternal( 528 Status VerifySignatureInternal(
542 const blink::WebCryptoAlgorithm& algorithm, 529 const blink::WebCryptoAlgorithm& algorithm,
543 const blink::WebCryptoKey& key, 530 const blink::WebCryptoKey& key,
544 const unsigned char* signature, 531 const CryptoData& signature,
545 unsigned int signature_size,
546 const std::vector<uint8>& data, 532 const std::vector<uint8>& data,
547 bool* signature_match) { 533 bool* signature_match) {
548 return crypto_.VerifySignatureInternal(algorithm, 534 return VerifySignature(algorithm,
549 key, 535 key,
550 signature, 536 signature,
551 signature_size, 537 CryptoData(data),
552 webcrypto::Uint8VectorStart(data), 538 signature_match);
553 data.size(),
554 signature_match);
555 } 539 }
556 540
541
557 Status VerifySignatureInternal( 542 Status VerifySignatureInternal(
558 const blink::WebCryptoAlgorithm& algorithm, 543 const blink::WebCryptoAlgorithm& algorithm,
559 const blink::WebCryptoKey& key, 544 const blink::WebCryptoKey& key,
560 const std::vector<uint8>& signature, 545 const std::vector<uint8>& signature,
561 const std::vector<uint8>& data, 546 const std::vector<uint8>& data,
562 bool* signature_match) { 547 bool* signature_match) {
563 return crypto_.VerifySignatureInternal( 548 return VerifySignature(
564 algorithm, 549 algorithm,
565 key, 550 key,
566 webcrypto::Uint8VectorStart(signature), 551 CryptoData(signature),
567 signature.size(), 552 CryptoData(data),
568 webcrypto::Uint8VectorStart(data),
569 data.size(),
570 signature_match); 553 signature_match);
571 } 554 }
572 555
573 Status EncryptInternal(
574 const blink::WebCryptoAlgorithm& algorithm,
575 const blink::WebCryptoKey& key,
576 const unsigned char* data,
577 unsigned int data_size,
578 blink::WebArrayBuffer* buffer) {
579 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer);
580 }
581
582 Status EncryptInternal(
583 const blink::WebCryptoAlgorithm& algorithm,
584 const blink::WebCryptoKey& key,
585 const std::vector<uint8>& data,
586 blink::WebArrayBuffer* buffer) {
587 return crypto_.EncryptInternal(
588 algorithm, key, webcrypto::Uint8VectorStart(data), data.size(), buffer);
589 }
590
591 Status DecryptInternal(
592 const blink::WebCryptoAlgorithm& algorithm,
593 const blink::WebCryptoKey& key,
594 const unsigned char* data,
595 unsigned int data_size,
596 blink::WebArrayBuffer* buffer) {
597 return crypto_.DecryptInternal(algorithm, key, data, data_size, buffer);
598 }
599
600 Status DecryptInternal(
601 const blink::WebCryptoAlgorithm& algorithm,
602 const blink::WebCryptoKey& key,
603 const std::vector<uint8>& data,
604 blink::WebArrayBuffer* buffer) {
605 return crypto_.DecryptInternal(
606 algorithm, key, webcrypto::Uint8VectorStart(data), data.size(), buffer);
607 }
608
609 Status ImportKeyJwk( 556 Status ImportKeyJwk(
610 const std::vector<uint8>& key_data, 557 const std::vector<uint8>& key_data,
611 const blink::WebCryptoAlgorithm& algorithm, 558 const blink::WebCryptoAlgorithm& algorithm,
612 bool extractable, 559 bool extractable,
613 blink::WebCryptoKeyUsageMask usage_mask, 560 blink::WebCryptoKeyUsageMask usage_mask,
614 blink::WebCryptoKey* key) { 561 blink::WebCryptoKey* key) {
615 return crypto_.ImportKeyJwk(webcrypto::Uint8VectorStart(key_data), 562 return ImportKeyJwk(CryptoData(key_data),
616 key_data.size(), 563 algorithm,
617 algorithm, 564 extractable,
618 extractable, 565 usage_mask,
619 usage_mask, 566 key);
620 key);
621 } 567 }
622 568
623 private: 569 } // namespace
624 WebCryptoImpl crypto_;
625 };
626 570
627 TEST_F(WebCryptoImplTest, StatusToString) { 571 TEST_F(SharedCryptoTest, StatusToString) {
628 EXPECT_EQ("Success", Status::Success().ToString()); 572 EXPECT_EQ("Success", Status::Success().ToString());
629 EXPECT_EQ("", Status::Error().ToString()); 573 EXPECT_EQ("", Status::Error().ToString());
630 EXPECT_EQ("The requested operation is unsupported", 574 EXPECT_EQ("The requested operation is unsupported",
631 Status::ErrorUnsupported().ToString()); 575 Status::ErrorUnsupported().ToString());
632 EXPECT_EQ("The required JWK property \"kty\" was missing", 576 EXPECT_EQ("The required JWK property \"kty\" was missing",
633 Status::ErrorJwkPropertyMissing("kty").ToString()); 577 Status::ErrorJwkPropertyMissing("kty").ToString());
634 EXPECT_EQ("The JWK property \"kty\" must be a string", 578 EXPECT_EQ("The JWK property \"kty\" must be a string",
635 Status::ErrorJwkPropertyWrongType("kty", "string").ToString()); 579 Status::ErrorJwkPropertyWrongType("kty", "string").ToString());
636 EXPECT_EQ("The JWK property \"n\" could not be base64 decoded", 580 EXPECT_EQ("The JWK property \"n\" could not be base64 decoded",
637 Status::ErrorJwkBase64Decode("n").ToString()); 581 Status::ErrorJwkBase64Decode("n").ToString());
638 } 582 }
639 583
640 TEST_F(WebCryptoImplTest, DigestSampleSets) { 584 TEST_F(SharedCryptoTest, DigestSampleSets) {
641 scoped_ptr<base::ListValue> tests; 585 scoped_ptr<base::ListValue> tests;
642 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); 586 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests));
643 587
644 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 588 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
645 SCOPED_TRACE(test_index); 589 SCOPED_TRACE(test_index);
646 base::DictionaryValue* test; 590 base::DictionaryValue* test;
647 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 591 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
648 592
649 blink::WebCryptoAlgorithm test_algorithm = 593 blink::WebCryptoAlgorithm test_algorithm =
650 GetDigestAlgorithm(test, "algorithm"); 594 GetDigestAlgorithm(test, "algorithm");
651 std::vector<uint8> test_input = GetBytesFromHexString(test, "input"); 595 std::vector<uint8> test_input = GetBytesFromHexString(test, "input");
652 std::vector<uint8> test_output = GetBytesFromHexString(test, "output"); 596 std::vector<uint8> test_output = GetBytesFromHexString(test, "output");
653 597
654 blink::WebArrayBuffer output; 598 blink::WebArrayBuffer output;
655 ASSERT_STATUS_SUCCESS(DigestInternal(test_algorithm, test_input, &output)); 599 ASSERT_STATUS_SUCCESS(DigestInternal(test_algorithm, test_input, &output));
656 ExpectArrayBufferMatches(test_output, output); 600 ExpectArrayBufferMatches(test_output, output);
657 } 601 }
658 } 602 }
659 603
660 TEST_F(WebCryptoImplTest, HMACSampleSets) { 604 TEST_F(SharedCryptoTest, HMACSampleSets) {
661 scoped_ptr<base::ListValue> tests; 605 scoped_ptr<base::ListValue> tests;
662 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); 606 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests));
663 607
664 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 608 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
665 SCOPED_TRACE(test_index); 609 SCOPED_TRACE(test_index);
666 base::DictionaryValue* test; 610 base::DictionaryValue* test;
667 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 611 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
668 612
669 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); 613 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash");
670 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 614 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
671 const std::vector<uint8> test_message = 615 const std::vector<uint8> test_message =
672 GetBytesFromHexString(test, "message"); 616 GetBytesFromHexString(test, "message");
673 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac"); 617 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac");
674 618
675 blink::WebCryptoAlgorithm algorithm = 619 blink::WebCryptoAlgorithm algorithm =
676 webcrypto::CreateHmacAlgorithmByHashId(test_hash.id()); 620 CreateHmacAlgorithmByHashId(test_hash.id());
677 621
678 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 622 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
679 test_key, algorithm, blink::WebCryptoKeyUsageSign); 623 test_key,
624 algorithm,
625 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify);
680 626
681 // Verify exported raw key is identical to the imported data 627 // Verify exported raw key is identical to the imported data
682 blink::WebArrayBuffer raw_key; 628 blink::WebArrayBuffer raw_key;
683 EXPECT_STATUS_SUCCESS( 629 EXPECT_STATUS_SUCCESS(
684 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 630 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
685 ExpectArrayBufferMatches(test_key, raw_key); 631 ExpectArrayBufferMatches(test_key, raw_key);
686 632
687 blink::WebArrayBuffer output; 633 blink::WebArrayBuffer output;
688 634
689 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, test_message, &output)); 635 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, test_message, &output));
690 636
691 ExpectArrayBufferMatches(test_mac, output); 637 ExpectArrayBufferMatches(test_mac, output);
692 638
693 bool signature_match = false; 639 bool signature_match = false;
694 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 640 EXPECT_STATUS_SUCCESS(VerifySignature(
695 algorithm, 641 algorithm,
696 key, 642 key,
697 static_cast<const unsigned char*>(output.data()), 643 CryptoData(output),
698 output.byteLength(), 644 CryptoData(test_message),
699 test_message,
700 &signature_match)); 645 &signature_match));
701 EXPECT_TRUE(signature_match); 646 EXPECT_TRUE(signature_match);
702 647
703 // Ensure truncated signature does not verify by passing one less byte. 648 // Ensure truncated signature does not verify by passing one less byte.
704 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 649 EXPECT_STATUS_SUCCESS(VerifySignature(
705 algorithm, 650 algorithm,
706 key, 651 key,
707 static_cast<const unsigned char*>(output.data()), 652 CryptoData(static_cast<const unsigned char*>(output.data()),
708 output.byteLength() - 1, 653 output.byteLength() - 1),
709 test_message, 654 CryptoData(test_message),
710 &signature_match)); 655 &signature_match));
711 EXPECT_FALSE(signature_match); 656 EXPECT_FALSE(signature_match);
712 657
713 // Ensure truncated signature does not verify by passing no bytes. 658 // Ensure truncated signature does not verify by passing no bytes.
714 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 659 EXPECT_STATUS_SUCCESS(VerifySignature(
715 algorithm, 660 algorithm,
716 key, 661 key,
717 NULL, 662 CryptoData(),
718 0, 663 CryptoData(test_message),
719 test_message,
720 &signature_match)); 664 &signature_match));
721 EXPECT_FALSE(signature_match); 665 EXPECT_FALSE(signature_match);
722 666
723 // Ensure extra long signature does not cause issues and fails. 667 // Ensure extra long signature does not cause issues and fails.
724 const unsigned char kLongSignature[1024] = { 0 }; 668 const unsigned char kLongSignature[1024] = { 0 };
725 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 669 EXPECT_STATUS_SUCCESS(VerifySignature(
726 algorithm, 670 algorithm,
727 key, 671 key,
728 kLongSignature, 672 CryptoData(kLongSignature, sizeof(kLongSignature)),
729 sizeof(kLongSignature), 673 CryptoData(test_message),
730 test_message,
731 &signature_match)); 674 &signature_match));
732 EXPECT_FALSE(signature_match); 675 EXPECT_FALSE(signature_match);
733 } 676 }
734 } 677 }
735 678
736 TEST_F(WebCryptoImplTest, AesCbcFailures) { 679 TEST_F(SharedCryptoTest, AesCbcFailures) {
737 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; 680 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c";
738 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 681 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
739 HexStringToBytes(key_hex), 682 HexStringToBytes(key_hex),
740 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 683 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
741 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 684 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
742 685
743 // Verify exported raw key is identical to the imported data 686 // Verify exported raw key is identical to the imported data
744 blink::WebArrayBuffer raw_key; 687 blink::WebArrayBuffer raw_key;
745 EXPECT_STATUS_SUCCESS( 688 EXPECT_STATUS_SUCCESS(
746 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 689 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
747 ExpectArrayBufferMatchesHex(key_hex, raw_key); 690 ExpectArrayBufferMatchesHex(key_hex, raw_key);
748 691
749 blink::WebArrayBuffer output; 692 blink::WebArrayBuffer output;
750 693
751 // Use an invalid |iv| (fewer than 16 bytes) 694 // Use an invalid |iv| (fewer than 16 bytes)
752 { 695 {
753 std::vector<uint8> input(32); 696 std::vector<uint8> input(32);
754 std::vector<uint8> iv; 697 std::vector<uint8> iv;
755 EXPECT_STATUS(Status::ErrorIncorrectSizeAesCbcIv(), EncryptInternal( 698 EXPECT_STATUS(Status::ErrorIncorrectSizeAesCbcIv(), EncryptInternal(
756 webcrypto::CreateAesCbcAlgorithm(iv), key, input, &output)); 699 webcrypto::CreateAesCbcAlgorithm(iv), key, input, &output));
(...skipping 12 matching lines...) Expand all
769 } 712 }
770 713
771 // Give an input that is too large (would cause integer overflow when 714 // Give an input that is too large (would cause integer overflow when
772 // narrowing to an int). 715 // narrowing to an int).
773 { 716 {
774 std::vector<uint8> iv(16); 717 std::vector<uint8> iv(16);
775 718
776 // Pretend the input is large. Don't pass data pointer as NULL in case that 719 // Pretend the input is large. Don't pass data pointer as NULL in case that
777 // is special cased; the implementation shouldn't actually dereference the 720 // is special cased; the implementation shouldn't actually dereference the
778 // data. 721 // data.
779 const unsigned char* input = &iv[0]; 722 CryptoData input(&iv[0], INT_MAX - 3);
780 unsigned int input_len = INT_MAX - 3;
781 723
782 EXPECT_STATUS(Status::ErrorDataTooLarge(), EncryptInternal( 724 EXPECT_STATUS(Status::ErrorDataTooLarge(), Encrypt(
783 webcrypto::CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); 725 CreateAesCbcAlgorithm(iv), key, input, &output));
784 EXPECT_STATUS(Status::ErrorDataTooLarge(), DecryptInternal( 726 EXPECT_STATUS(Status::ErrorDataTooLarge(), Decrypt(
785 webcrypto::CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); 727 CreateAesCbcAlgorithm(iv), key, input, &output));
786 } 728 }
787 729
788 // Fail importing the key (too few bytes specified) 730 // Fail importing the key (too few bytes specified)
789 { 731 {
790 std::vector<uint8> key_raw(1); 732 std::vector<uint8> key_raw(1);
791 std::vector<uint8> iv(16); 733 std::vector<uint8> iv(16);
792 734
793 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 735 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
794 EXPECT_STATUS( 736 EXPECT_STATUS(
795 Status::Error(), 737 Status::Error(),
796 ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 738 ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
797 key_raw, 739 key_raw,
798 webcrypto::CreateAesCbcAlgorithm(iv), 740 CreateAesCbcAlgorithm(iv),
799 true, 741 true,
800 blink::WebCryptoKeyUsageEncrypt, 742 blink::WebCryptoKeyUsageEncrypt,
801 &key)); 743 &key));
802 } 744 }
803 745
804 // TODO(eroman): Enable for OpenSSL once implemented.
805 #if !defined(USE_OPENSSL)
806 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret 746 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret
807 // keys). 747 // keys).
808 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), 748 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(),
809 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 749 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
810 EXPECT_STATUS(Status::ErrorUnsupported(), 750 EXPECT_STATUS(Status::ErrorUnsupported(),
811 ExportKeyInternal(blink::WebCryptoKeyFormatPkcs8, key, &output)); 751 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &output));
812 #endif
813 } 752 }
814 753
815 TEST_F(WebCryptoImplTest, MAYBE(AesCbcSampleSets)) { 754 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) {
816 scoped_ptr<base::ListValue> tests; 755 scoped_ptr<base::ListValue> tests;
817 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests)); 756 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests));
818 757
819 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 758 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
820 SCOPED_TRACE(test_index); 759 SCOPED_TRACE(test_index);
821 base::DictionaryValue* test; 760 base::DictionaryValue* test;
822 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 761 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
823 762
824 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 763 std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
825 std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); 764 std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv");
826 std::vector<uint8> test_plain_text = 765 std::vector<uint8> test_plain_text =
827 GetBytesFromHexString(test, "plain_text"); 766 GetBytesFromHexString(test, "plain_text");
828 std::vector<uint8> test_cipher_text = 767 std::vector<uint8> test_cipher_text =
829 GetBytesFromHexString(test, "cipher_text"); 768 GetBytesFromHexString(test, "cipher_text");
830 769
831 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 770 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
832 test_key, 771 test_key,
833 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 772 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
834 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 773 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
835 774
836 // Verify exported raw key is identical to the imported data 775 // Verify exported raw key is identical to the imported data
837 blink::WebArrayBuffer raw_key; 776 blink::WebArrayBuffer raw_key;
838 EXPECT_STATUS_SUCCESS(ExportKeyInternal( 777 EXPECT_STATUS_SUCCESS(ExportKey(
839 blink::WebCryptoKeyFormatRaw, key, &raw_key)); 778 blink::WebCryptoKeyFormatRaw, key, &raw_key));
840 ExpectArrayBufferMatches(test_key, raw_key); 779 ExpectArrayBufferMatches(test_key, raw_key);
841 780
842 blink::WebArrayBuffer output; 781 blink::WebArrayBuffer output;
843 782
844 // Test encryption. 783 // Test encryption.
845 EXPECT_STATUS( 784 EXPECT_STATUS(
846 Status::Success(), 785 Status::Success(),
847 EncryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), 786 EncryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv),
848 key, 787 key,
(...skipping 10 matching lines...) Expand all
859 &output)); 798 &output));
860 ExpectArrayBufferMatches(test_plain_text, output); 799 ExpectArrayBufferMatches(test_plain_text, output);
861 800
862 const unsigned int kAesCbcBlockSize = 16; 801 const unsigned int kAesCbcBlockSize = 16;
863 802
864 // Decrypt with a padding error by stripping the last block. This also ends 803 // Decrypt with a padding error by stripping the last block. This also ends
865 // up testing decryption over empty cipher text. 804 // up testing decryption over empty cipher text.
866 if (test_cipher_text.size() >= kAesCbcBlockSize) { 805 if (test_cipher_text.size() >= kAesCbcBlockSize) {
867 EXPECT_STATUS( 806 EXPECT_STATUS(
868 Status::Error(), 807 Status::Error(),
869 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), 808 Decrypt(CreateAesCbcAlgorithm(test_iv),
870 key, 809 key,
871 &test_cipher_text[0], 810 CryptoData(&test_cipher_text[0],
872 test_cipher_text.size() - kAesCbcBlockSize, 811 test_cipher_text.size() - kAesCbcBlockSize),
873 &output)); 812 &output));
874 } 813 }
875 814
876 // Decrypt cipher text which is not a multiple of block size by stripping 815 // Decrypt cipher text which is not a multiple of block size by stripping
877 // a few bytes off the cipher text. 816 // a few bytes off the cipher text.
878 if (test_cipher_text.size() > 3) { 817 if (test_cipher_text.size() > 3) {
879 EXPECT_STATUS( 818 EXPECT_STATUS(
880 Status::Error(), 819 Status::Error(),
881 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), 820 Decrypt(CreateAesCbcAlgorithm(test_iv),
882 key, 821 key,
883 &test_cipher_text[0], 822 CryptoData(&test_cipher_text[0],
884 test_cipher_text.size() - 3, 823 test_cipher_text.size() - 3),
885 &output)); 824 &output));
886 } 825 }
887 } 826 }
888 } 827 }
889 828
890 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { 829 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyAes)) {
891 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each 830 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each
892 // allowed key length. 831 // allowed key length.
893 std::vector<blink::WebCryptoAlgorithm> algorithm; 832 std::vector<blink::WebCryptoAlgorithm> algorithm;
894 const unsigned short kKeyLength[] = {128, 192, 256}; 833 const unsigned short kKeyLength[] = {128, 192, 256};
895 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) { 834 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) {
896 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i])); 835 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i]));
897 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i])); 836 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i]));
898 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i])); 837 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i]));
899 } 838 }
900 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 839 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
901 std::vector<blink::WebArrayBuffer> keys; 840 std::vector<blink::WebArrayBuffer> keys;
902 blink::WebArrayBuffer key_bytes; 841 blink::WebArrayBuffer key_bytes;
903 for (size_t i = 0; i < algorithm.size(); ++i) { 842 for (size_t i = 0; i < algorithm.size(); ++i) {
904 SCOPED_TRACE(i); 843 SCOPED_TRACE(i);
905 // Generate a small sample of keys. 844 // Generate a small sample of keys.
906 keys.clear(); 845 keys.clear();
907 for (int j = 0; j < 16; ++j) { 846 for (int j = 0; j < 16; ++j) {
908 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm[i], &key)); 847 ASSERT_STATUS_SUCCESS(GenerateSecretKey(
848 algorithm[i], true, 0, &key));
909 EXPECT_TRUE(key.handle()); 849 EXPECT_TRUE(key.handle());
910 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 850 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
911 ASSERT_STATUS_SUCCESS( 851 ASSERT_STATUS_SUCCESS(
912 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); 852 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes));
913 keys.push_back(key_bytes); 853 keys.push_back(key_bytes);
914 } 854 }
915 // Ensure all entries in the key sample set are unique. This is a simplistic 855 // Ensure all entries in the key sample set are unique. This is a simplistic
916 // estimate of whether the generated keys appear random. 856 // estimate of whether the generated keys appear random.
917 EXPECT_FALSE(CopiesExist(keys)); 857 EXPECT_FALSE(CopiesExist(keys));
918 } 858 }
919 } 859 }
920 860
921 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAesBadLength)) { 861 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyAesBadLength)) {
922 const unsigned short kKeyLen[] = {0, 127, 257}; 862 const unsigned short kKeyLen[] = {0, 127, 257};
923 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 863 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
924 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) { 864 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) {
925 SCOPED_TRACE(i); 865 SCOPED_TRACE(i);
926 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateKeyInternal( 866 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateSecretKey(
927 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), &key)); 867 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
928 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateKeyInternal( 868 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateSecretKey(
929 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), &key)); 869 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
930 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateKeyInternal( 870 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateSecretKey(
931 CreateAesKwKeyGenAlgorithm(kKeyLen[i]), &key)); 871 CreateAesKwKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
932 } 872 }
933 } 873 }
934 874
935 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { 875 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmac)) {
936 // Generate a small sample of HMAC keys. 876 // Generate a small sample of HMAC keys.
937 std::vector<blink::WebArrayBuffer> keys; 877 std::vector<blink::WebArrayBuffer> keys;
938 for (int i = 0; i < 16; ++i) { 878 for (int i = 0; i < 16; ++i) {
939 blink::WebArrayBuffer key_bytes; 879 blink::WebArrayBuffer key_bytes;
940 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 880 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
941 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( 881 blink::WebCryptoAlgorithm algorithm = CreateHmacKeyGenAlgorithm(
942 blink::WebCryptoAlgorithmIdSha1, 64); 882 blink::WebCryptoAlgorithmIdSha1, 64);
943 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm, &key)); 883 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key));
944 EXPECT_FALSE(key.isNull()); 884 EXPECT_FALSE(key.isNull());
945 EXPECT_TRUE(key.handle()); 885 EXPECT_TRUE(key.handle());
946 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 886 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
947 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 887 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
948 888
949 blink::WebArrayBuffer raw_key; 889 blink::WebArrayBuffer raw_key;
950 ASSERT_STATUS_SUCCESS( 890 ASSERT_STATUS_SUCCESS(
951 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 891 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
952 EXPECT_EQ(64U, raw_key.byteLength()); 892 EXPECT_EQ(64U, raw_key.byteLength());
953 keys.push_back(raw_key); 893 keys.push_back(raw_key);
954 } 894 }
955 // Ensure all entries in the key sample set are unique. This is a simplistic 895 // Ensure all entries in the key sample set are unique. This is a simplistic
956 // estimate of whether the generated keys appear random. 896 // estimate of whether the generated keys appear random.
957 EXPECT_FALSE(CopiesExist(keys)); 897 EXPECT_FALSE(CopiesExist(keys));
958 } 898 }
959 899
960 // If the key length is not provided, then the block size is used. 900 // If the key length is not provided, then the block size is used.
961 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmacNoLength)) { 901 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) {
962 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 902 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
963 blink::WebCryptoAlgorithm algorithm = 903 blink::WebCryptoAlgorithm algorithm =
964 webcrypto::CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); 904 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0);
965 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm, &key)); 905 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key));
966 EXPECT_TRUE(key.handle()); 906 EXPECT_TRUE(key.handle());
967 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 907 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
968 blink::WebArrayBuffer raw_key; 908 blink::WebArrayBuffer raw_key;
969 ASSERT_STATUS_SUCCESS( 909 ASSERT_STATUS_SUCCESS(
970 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 910 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
971 EXPECT_EQ(64U, raw_key.byteLength()); 911 EXPECT_EQ(64U, raw_key.byteLength());
972 912
973 // The block size for HMAC SHA-512 is larger. 913 // The block size for HMAC SHA-512 is larger.
974 algorithm = webcrypto::CreateHmacKeyGenAlgorithm( 914 algorithm = CreateHmacKeyGenAlgorithm(
975 blink::WebCryptoAlgorithmIdSha512, 0); 915 blink::WebCryptoAlgorithmIdSha512, 0);
976 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm, &key)); 916 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key));
977 ASSERT_STATUS_SUCCESS( 917 ASSERT_STATUS_SUCCESS(
978 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 918 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
979 EXPECT_EQ(128U, raw_key.byteLength()); 919 EXPECT_EQ(128U, raw_key.byteLength());
980 } 920 }
981 921
982 TEST_F(WebCryptoImplTest, MAYBE(ImportSecretKeyNoAlgorithm)) { 922 TEST_F(SharedCryptoTest, MAYBE(ImportSecretKeyNoAlgorithm)) {
983 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 923 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
984 924
985 // This fails because the algorithm is null. 925 // This fails because the algorithm is null.
986 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), ImportKeyInternal( 926 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), ImportKeyInternal(
987 blink::WebCryptoKeyFormatRaw, 927 blink::WebCryptoKeyFormatRaw,
988 HexStringToBytes("00000000000000000000"), 928 HexStringToBytes("00000000000000000000"),
989 blink::WebCryptoAlgorithm::createNull(), 929 blink::WebCryptoAlgorithm::createNull(),
990 true, 930 true,
991 blink::WebCryptoKeyUsageEncrypt, 931 blink::WebCryptoKeyUsageEncrypt,
992 &key)); 932 &key));
993 } 933 }
994 934
995 935 TEST_F(SharedCryptoTest, ImportJwkFailures) {
996 TEST_F(WebCryptoImplTest, ImportJwkFailures) {
997 936
998 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 937 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
999 blink::WebCryptoAlgorithm algorithm = 938 blink::WebCryptoAlgorithm algorithm =
1000 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 939 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
1001 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 940 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1002 941
1003 // Baseline pass: each test below breaks a single item, so we start with a 942 // Baseline pass: each test below breaks a single item, so we start with a
1004 // passing case to make sure each failure is caused by the isolated break. 943 // passing case to make sure each failure is caused by the isolated break.
1005 // Each breaking subtest below resets the dictionary to this passing case when 944 // Each breaking subtest below resets the dictionary to this passing case when
1006 // complete. 945 // complete.
1007 base::DictionaryValue dict; 946 base::DictionaryValue dict;
1008 RestoreJwkOctDictionary(&dict); 947 RestoreJwkOctDictionary(&dict);
1009 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 948 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1010 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 949 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 RestoreJwkOctDictionary(&dict); 1013 RestoreJwkOctDictionary(&dict);
1075 1014
1076 // Fail on invalid extractable (wrong type). 1015 // Fail on invalid extractable (wrong type).
1077 dict.SetInteger("extractable", 0); 1016 dict.SetInteger("extractable", 0);
1078 EXPECT_STATUS( 1017 EXPECT_STATUS(
1079 Status::ErrorJwkPropertyWrongType("extractable", "boolean"), 1018 Status::ErrorJwkPropertyWrongType("extractable", "boolean"),
1080 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1019 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1081 RestoreJwkOctDictionary(&dict); 1020 RestoreJwkOctDictionary(&dict);
1082 } 1021 }
1083 1022
1084 TEST_F(WebCryptoImplTest, ImportJwkOctFailures) { 1023 TEST_F(SharedCryptoTest, ImportJwkOctFailures) {
1085 1024
1086 base::DictionaryValue dict; 1025 base::DictionaryValue dict;
1087 RestoreJwkOctDictionary(&dict); 1026 RestoreJwkOctDictionary(&dict);
1088 blink::WebCryptoAlgorithm algorithm = 1027 blink::WebCryptoAlgorithm algorithm =
1089 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 1028 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
1090 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 1029 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1091 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1030 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1092 1031
1093 // Baseline pass. 1032 // Baseline pass.
1094 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 1033 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1095 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1034 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1096 EXPECT_EQ(algorithm.id(), key.algorithm().id()); 1035 EXPECT_EQ(algorithm.id(), key.algorithm().id());
1097 EXPECT_FALSE(key.extractable()); 1036 EXPECT_FALSE(key.extractable());
1098 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages()); 1037 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages());
1099 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1038 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
(...skipping 28 matching lines...) Expand all
1128 RestoreJwkOctDictionary(&dict); 1067 RestoreJwkOctDictionary(&dict);
1129 1068
1130 // Fail on k actual length (192 bits) inconsistent with the embedded JWK alg 1069 // Fail on k actual length (192 bits) inconsistent with the embedded JWK alg
1131 // value (128) for an AES key. 1070 // value (128) for an AES key.
1132 dict.SetString("k", "dGhpcyAgaXMgIDI0ICBieXRlcyBsb25n"); 1071 dict.SetString("k", "dGhpcyAgaXMgIDI0ICBieXRlcyBsb25n");
1133 EXPECT_STATUS(Status::ErrorJwkIncorrectKeyLength(), ImportKeyJwk( 1072 EXPECT_STATUS(Status::ErrorJwkIncorrectKeyLength(), ImportKeyJwk(
1134 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1073 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1135 RestoreJwkOctDictionary(&dict); 1074 RestoreJwkOctDictionary(&dict);
1136 } 1075 }
1137 1076
1138 TEST_F(WebCryptoImplTest, MAYBE(ImportJwkRsaFailures)) { 1077 TEST_F(SharedCryptoTest, MAYBE(ImportJwkRsaFailures)) {
1139 1078
1140 base::DictionaryValue dict; 1079 base::DictionaryValue dict;
1141 RestoreJwkRsaDictionary(&dict); 1080 RestoreJwkRsaDictionary(&dict);
1142 blink::WebCryptoAlgorithm algorithm = 1081 blink::WebCryptoAlgorithm algorithm =
1143 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1082 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1144 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 1083 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1145 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1084 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1146 1085
1147 // An RSA public key JWK _must_ have an "n" (modulus) and an "e" (exponent) 1086 // An RSA public key JWK _must_ have an "n" (modulus) and an "e" (exponent)
1148 // entry, while an RSA private key must have those plus at least a "d" 1087 // entry, while an RSA private key must have those plus at least a "d"
1149 // (private exponent) entry. 1088 // (private exponent) entry.
1150 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-18, 1089 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-18,
1151 // section 6.3. 1090 // section 6.3.
1152 1091
1153 // Baseline pass. 1092 // Baseline pass.
(...skipping 30 matching lines...) Expand all
1184 } 1123 }
1185 1124
1186 // Fail if "d" parameter is present, implying the JWK is a private key, which 1125 // Fail if "d" parameter is present, implying the JWK is a private key, which
1187 // is not supported. 1126 // is not supported.
1188 dict.SetString("d", "Qk3f0Dsyt"); 1127 dict.SetString("d", "Qk3f0Dsyt");
1189 EXPECT_STATUS(Status::ErrorJwkRsaPrivateKeyUnsupported(), ImportKeyJwk( 1128 EXPECT_STATUS(Status::ErrorJwkRsaPrivateKeyUnsupported(), ImportKeyJwk(
1190 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1129 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1191 RestoreJwkRsaDictionary(&dict); 1130 RestoreJwkRsaDictionary(&dict);
1192 } 1131 }
1193 1132
1194 TEST_F(WebCryptoImplTest, MAYBE(ImportJwkInputConsistency)) { 1133 TEST_F(SharedCryptoTest, MAYBE(ImportJwkInputConsistency)) {
1195 // The Web Crypto spec says that if a JWK value is present, but is 1134 // The Web Crypto spec says that if a JWK value is present, but is
1196 // inconsistent with the input value, the operation must fail. 1135 // inconsistent with the input value, the operation must fail.
1197 1136
1198 // Consistency rules when JWK value is not present: Inputs should be used. 1137 // Consistency rules when JWK value is not present: Inputs should be used.
1199 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1138 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1200 bool extractable = false; 1139 bool extractable = false;
1201 blink::WebCryptoAlgorithm algorithm = 1140 blink::WebCryptoAlgorithm algorithm =
1202 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256); 1141 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256);
1203 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify; 1142 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify;
1204 base::DictionaryValue dict; 1143 base::DictionaryValue dict;
1205 dict.SetString("kty", "oct"); 1144 dict.SetString("kty", "oct");
1206 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); 1145 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg");
1207 std::vector<uint8> json_vec = MakeJsonVector(dict); 1146 std::vector<uint8> json_vec = MakeJsonVector(dict);
1208 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 1147 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1209 json_vec, algorithm, extractable, usage_mask, &key)); 1148 json_vec, algorithm, extractable, usage_mask, &key));
1210 EXPECT_TRUE(key.handle()); 1149 EXPECT_TRUE(key.handle());
1211 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1150 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1212 EXPECT_EQ(extractable, key.extractable()); 1151 EXPECT_EQ(extractable, key.extractable());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 EXPECT_TRUE(key.extractable()); 1184 EXPECT_TRUE(key.extractable());
1246 EXPECT_STATUS_SUCCESS( 1185 EXPECT_STATUS_SUCCESS(
1247 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1186 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1248 EXPECT_FALSE(key.extractable()); 1187 EXPECT_FALSE(key.extractable());
1249 dict.SetBoolean("extractable", true); // restore previous value 1188 dict.SetBoolean("extractable", true); // restore previous value
1250 1189
1251 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value 1190 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value
1252 // (HMAC SHA256). 1191 // (HMAC SHA256).
1253 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk( 1192 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk(
1254 json_vec, 1193 json_vec,
1255 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1194 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1256 extractable, 1195 extractable,
1257 usage_mask, 1196 usage_mask,
1258 &key)); 1197 &key));
1259 1198
1260 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value 1199 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value
1261 // (HMAC SHA256). 1200 // (HMAC SHA256).
1262 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk( 1201 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk(
1263 json_vec, 1202 json_vec,
1264 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha1), 1203 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha1),
1265 extractable, 1204 extractable,
1266 usage_mask, 1205 usage_mask,
1267 &key)); 1206 &key));
1268 1207
1269 // Pass: JWK alg valid but input algorithm isNull: use JWK algorithm value. 1208 // Pass: JWK alg valid but input algorithm isNull: use JWK algorithm value.
1270 EXPECT_STATUS_SUCCESS(ImportKeyJwk(json_vec, 1209 EXPECT_STATUS_SUCCESS(ImportKeyJwk(json_vec,
1271 blink::WebCryptoAlgorithm::createNull(), 1210 blink::WebCryptoAlgorithm::createNull(),
1272 extractable, 1211 extractable,
1273 usage_mask, 1212 usage_mask,
1274 &key)); 1213 &key));
1275 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); 1214 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id());
1276 1215
1277 // Pass: JWK alg missing but input algorithm specified: use input value 1216 // Pass: JWK alg missing but input algorithm specified: use input value
1278 dict.Remove("alg", NULL); 1217 dict.Remove("alg", NULL);
1279 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 1218 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1280 MakeJsonVector(dict), 1219 MakeJsonVector(dict),
1281 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256), 1220 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256),
1282 extractable, 1221 extractable,
1283 usage_mask, 1222 usage_mask,
1284 &key)); 1223 &key));
1285 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); 1224 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id());
1286 dict.SetString("alg", "HS256"); 1225 dict.SetString("alg", "HS256");
1287 1226
1288 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value 1227 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value
1289 // (sign|verify) 1228 // (sign|verify)
1290 EXPECT_STATUS(Status::ErrorJwkUsageInconsistent(), ImportKeyJwk( 1229 EXPECT_STATUS(Status::ErrorJwkUsageInconsistent(), ImportKeyJwk(
1291 json_vec, algorithm, extractable, blink::WebCryptoKeyUsageEncrypt, &key)); 1230 json_vec, algorithm, extractable, blink::WebCryptoKeyUsageEncrypt, &key));
1292 1231
1293 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK 1232 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK
1294 // value (sign|verify) 1233 // value (sign|verify)
1295 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign | 1234 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign |
1296 blink::WebCryptoKeyUsageVerify; 1235 blink::WebCryptoKeyUsageVerify;
1297 EXPECT_STATUS( 1236 EXPECT_STATUS(
1298 Status::ErrorJwkUsageInconsistent(), 1237 Status::ErrorJwkUsageInconsistent(),
1299 ImportKeyJwk(json_vec, algorithm, extractable, usage_mask, &key)); 1238 ImportKeyJwk(json_vec, algorithm, extractable, usage_mask, &key));
1300 1239
1301 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value, 1240 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value,
1302 // only certain alg values are permitted. For example, when kty = "RSA" alg 1241 // only certain alg values are permitted. For example, when kty = "RSA" alg
1303 // must be of the RSA family, or when kty = "oct" alg must be symmetric 1242 // must be of the RSA family, or when kty = "oct" alg must be symmetric
1304 // algorithm. 1243 // algorithm.
1305 } 1244 }
1306 1245
1307 TEST_F(WebCryptoImplTest, MAYBE(ImportJwkHappy)) { 1246 TEST_F(SharedCryptoTest, MAYBE(ImportJwkHappy)) {
1308 1247
1309 // This test verifies the happy path of JWK import, including the application 1248 // This test verifies the happy path of JWK import, including the application
1310 // of the imported key material. 1249 // of the imported key material.
1311 1250
1312 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1251 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1313 bool extractable = false; 1252 bool extractable = false;
1314 blink::WebCryptoAlgorithm algorithm = 1253 blink::WebCryptoAlgorithm algorithm =
1315 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256); 1254 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256);
1316 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign; 1255 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign;
1317 1256
1318 // Import a symmetric key JWK and HMAC-SHA256 sign() 1257 // Import a symmetric key JWK and HMAC-SHA256 sign()
1319 // Uses the first SHA256 test vector from the HMAC sample set above. 1258 // Uses the first SHA256 test vector from the HMAC sample set above.
1320 1259
1321 base::DictionaryValue dict; 1260 base::DictionaryValue dict;
1322 dict.SetString("kty", "oct"); 1261 dict.SetString("kty", "oct");
1323 dict.SetString("alg", "HS256"); 1262 dict.SetString("alg", "HS256");
1324 dict.SetString("use", "sig"); 1263 dict.SetString("use", "sig");
1325 dict.SetBoolean("extractable", false); 1264 dict.SetBoolean("extractable", false);
(...skipping 14 matching lines...) Expand all
1340 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, message_raw, &output)); 1279 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, message_raw, &output));
1341 1280
1342 const std::string mac_raw = 1281 const std::string mac_raw =
1343 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; 1282 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b";
1344 1283
1345 ExpectArrayBufferMatchesHex(mac_raw, output); 1284 ExpectArrayBufferMatchesHex(mac_raw, output);
1346 1285
1347 // TODO(padolph): Import an RSA public key JWK and use it 1286 // TODO(padolph): Import an RSA public key JWK and use it
1348 } 1287 }
1349 1288
1350 TEST_F(WebCryptoImplTest, MAYBE(ImportExportSpki)) { 1289 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) {
1351 // Passing case: Import a valid RSA key in SPKI format. 1290 // Passing case: Import a valid RSA key in SPKI format.
1352 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1291 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1353 ASSERT_STATUS_SUCCESS(ImportKeyInternal( 1292 ASSERT_STATUS_SUCCESS(ImportKeyInternal(
1354 blink::WebCryptoKeyFormatSpki, 1293 blink::WebCryptoKeyFormatSpki,
1355 HexStringToBytes(kPublicKeySpkiDerHex), 1294 HexStringToBytes(kPublicKeySpkiDerHex),
1356 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1295 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1357 true, 1296 true,
1358 blink::WebCryptoKeyUsageEncrypt, 1297 blink::WebCryptoKeyUsageEncrypt,
1359 &key)); 1298 &key));
1360 EXPECT_TRUE(key.handle()); 1299 EXPECT_TRUE(key.handle());
1361 EXPECT_EQ(blink::WebCryptoKeyTypePublic, key.type()); 1300 EXPECT_EQ(blink::WebCryptoKeyTypePublic, key.type());
1362 EXPECT_TRUE(key.extractable()); 1301 EXPECT_TRUE(key.extractable());
1363 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages()); 1302 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages());
1364 1303
1365 // Failing case: Empty SPKI data 1304 // Failing case: Empty SPKI data
1366 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal( 1305 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal(
(...skipping 12 matching lines...) Expand all
1379 HexStringToBytes(kPublicKeySpkiDerHex), 1318 HexStringToBytes(kPublicKeySpkiDerHex),
1380 blink::WebCryptoAlgorithm::createNull(), 1319 blink::WebCryptoAlgorithm::createNull(),
1381 true, 1320 true,
1382 blink::WebCryptoKeyUsageEncrypt, 1321 blink::WebCryptoKeyUsageEncrypt,
1383 &key)); 1322 &key));
1384 1323
1385 // Failing case: Bad DER encoding. 1324 // Failing case: Bad DER encoding.
1386 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1325 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1387 blink::WebCryptoKeyFormatSpki, 1326 blink::WebCryptoKeyFormatSpki,
1388 HexStringToBytes("618333c4cb"), 1327 HexStringToBytes("618333c4cb"),
1389 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1328 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1390 true, 1329 true,
1391 blink::WebCryptoKeyUsageEncrypt, 1330 blink::WebCryptoKeyUsageEncrypt,
1392 &key)); 1331 &key));
1393 1332
1394 // Failing case: Import RSA key but provide an inconsistent input algorithm. 1333 // Failing case: Import RSA key but provide an inconsistent input algorithm.
1395 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1334 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1396 blink::WebCryptoKeyFormatSpki, 1335 blink::WebCryptoKeyFormatSpki,
1397 HexStringToBytes(kPublicKeySpkiDerHex), 1336 HexStringToBytes(kPublicKeySpkiDerHex),
1398 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1337 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1399 true, 1338 true,
1400 blink::WebCryptoKeyUsageEncrypt, 1339 blink::WebCryptoKeyUsageEncrypt,
1401 &key)); 1340 &key));
1402 1341
1403 // Passing case: Export a previously imported RSA public key in SPKI format 1342 // Passing case: Export a previously imported RSA public key in SPKI format
1404 // and compare to original data. 1343 // and compare to original data.
1405 blink::WebArrayBuffer output; 1344 blink::WebArrayBuffer output;
1406 ASSERT_STATUS_SUCCESS( 1345 ASSERT_STATUS_SUCCESS(
1407 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 1346 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
1408 ExpectArrayBufferMatchesHex(kPublicKeySpkiDerHex, output); 1347 ExpectArrayBufferMatchesHex(kPublicKeySpkiDerHex, output);
1409 1348
1410 // Failing case: Try to export a previously imported RSA public key in raw 1349 // Failing case: Try to export a previously imported RSA public key in raw
1411 // format (not allowed for a public key). 1350 // format (not allowed for a public key).
1412 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), 1351 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(),
1413 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &output)); 1352 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output));
1414 1353
1415 // Failing case: Try to export a non-extractable key 1354 // Failing case: Try to export a non-extractable key
1416 ASSERT_STATUS_SUCCESS(ImportKeyInternal( 1355 ASSERT_STATUS_SUCCESS(ImportKeyInternal(
1417 blink::WebCryptoKeyFormatSpki, 1356 blink::WebCryptoKeyFormatSpki,
1418 HexStringToBytes(kPublicKeySpkiDerHex), 1357 HexStringToBytes(kPublicKeySpkiDerHex),
1419 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1358 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1420 false, 1359 false,
1421 blink::WebCryptoKeyUsageEncrypt, 1360 blink::WebCryptoKeyUsageEncrypt,
1422 &key)); 1361 &key));
1423 EXPECT_TRUE(key.handle()); 1362 EXPECT_TRUE(key.handle());
1424 EXPECT_FALSE(key.extractable()); 1363 EXPECT_FALSE(key.extractable());
1425 EXPECT_STATUS(Status::ErrorKeyNotExtractable(), 1364 EXPECT_STATUS(Status::ErrorKeyNotExtractable(),
1426 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 1365 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
1427 } 1366 }
1428 1367
1429 TEST_F(WebCryptoImplTest, MAYBE(ImportPkcs8)) { 1368 TEST_F(SharedCryptoTest, MAYBE(ImportPkcs8)) {
1430 // Passing case: Import a valid RSA key in PKCS#8 format. 1369 // Passing case: Import a valid RSA key in PKCS#8 format.
1431 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1370 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1432 ASSERT_STATUS_SUCCESS(ImportKeyInternal( 1371 ASSERT_STATUS_SUCCESS(ImportKeyInternal(
1433 blink::WebCryptoKeyFormatPkcs8, 1372 blink::WebCryptoKeyFormatPkcs8,
1434 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1373 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1435 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 1374 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
1436 true, 1375 true,
1437 blink::WebCryptoKeyUsageSign, 1376 blink::WebCryptoKeyUsageSign,
1438 &key)); 1377 &key));
1439 EXPECT_TRUE(key.handle()); 1378 EXPECT_TRUE(key.handle());
1440 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, key.type()); 1379 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, key.type());
1441 EXPECT_TRUE(key.extractable()); 1380 EXPECT_TRUE(key.extractable());
1442 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); 1381 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages());
1443 1382
1444 // Failing case: Empty PKCS#8 data 1383 // Failing case: Empty PKCS#8 data
1445 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal( 1384 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal(
(...skipping 12 matching lines...) Expand all
1458 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1397 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1459 blink::WebCryptoAlgorithm::createNull(), 1398 blink::WebCryptoAlgorithm::createNull(),
1460 true, 1399 true,
1461 blink::WebCryptoKeyUsageSign, 1400 blink::WebCryptoKeyUsageSign,
1462 &key)); 1401 &key));
1463 1402
1464 // Failing case: Bad DER encoding. 1403 // Failing case: Bad DER encoding.
1465 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1404 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1466 blink::WebCryptoKeyFormatPkcs8, 1405 blink::WebCryptoKeyFormatPkcs8,
1467 HexStringToBytes("618333c4cb"), 1406 HexStringToBytes("618333c4cb"),
1468 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 1407 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
1469 true, 1408 true,
1470 blink::WebCryptoKeyUsageSign, 1409 blink::WebCryptoKeyUsageSign,
1471 &key)); 1410 &key));
1472 1411
1473 // Failing case: Import RSA key but provide an inconsistent input algorithm. 1412 // Failing case: Import RSA key but provide an inconsistent input algorithm.
1474 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1413 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1475 blink::WebCryptoKeyFormatPkcs8, 1414 blink::WebCryptoKeyFormatPkcs8,
1476 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1415 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1477 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1416 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1478 true, 1417 true,
1479 blink::WebCryptoKeyUsageSign, 1418 blink::WebCryptoKeyUsageSign,
1480 &key)); 1419 &key));
1481 } 1420 }
1482 1421
1483 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyPairRsa)) { 1422 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) {
1484 // Note: using unrealistic short key lengths here to avoid bogging down tests. 1423 // Note: using unrealistic short key lengths here to avoid bogging down tests.
1485 1424
1486 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. 1425 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation.
1487 const unsigned int modulus_length = 256; 1426 const unsigned int modulus_length = 256;
1488 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 1427 const std::vector<uint8> public_exponent = HexStringToBytes("010001");
1489 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1428 blink::WebCryptoAlgorithm algorithm = CreateRsaKeyGenAlgorithm(
1490 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1429 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1491 modulus_length, 1430 modulus_length,
1492 public_exponent); 1431 public_exponent);
1493 bool extractable = false; 1432 bool extractable = false;
1494 const blink::WebCryptoKeyUsageMask usage_mask = 0; 1433 const blink::WebCryptoKeyUsageMask usage_mask = 0;
1495 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1434 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1496 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1435 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1497 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1436 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1498 algorithm, extractable, usage_mask, &public_key, &private_key)); 1437 algorithm, extractable, usage_mask, &public_key, &private_key));
1499 EXPECT_FALSE(public_key.isNull()); 1438 EXPECT_FALSE(public_key.isNull());
1500 EXPECT_FALSE(private_key.isNull()); 1439 EXPECT_FALSE(private_key.isNull());
1501 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1440 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1502 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1441 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1503 EXPECT_TRUE(public_key.extractable()); 1442 EXPECT_TRUE(public_key.extractable());
1504 EXPECT_EQ(extractable, private_key.extractable()); 1443 EXPECT_EQ(extractable, private_key.extractable());
1505 EXPECT_EQ(usage_mask, public_key.usages()); 1444 EXPECT_EQ(usage_mask, public_key.usages());
1506 EXPECT_EQ(usage_mask, private_key.usages()); 1445 EXPECT_EQ(usage_mask, private_key.usages());
1507 1446
1508 // Fail with bad modulus. 1447 // Fail with bad modulus.
1509 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1448 algorithm = CreateRsaKeyGenAlgorithm(
1510 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); 1449 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent);
1511 EXPECT_STATUS(Status::ErrorGenerateRsaZeroModulus(), GenerateKeyPairInternal( 1450 EXPECT_STATUS(Status::ErrorGenerateRsaZeroModulus(), GenerateKeyPair(
1512 algorithm, extractable, usage_mask, &public_key, &private_key)); 1451 algorithm, extractable, usage_mask, &public_key, &private_key));
1513 1452
1514 // Fail with bad exponent: larger than unsigned long. 1453 // Fail with bad exponent: larger than unsigned long.
1515 unsigned int exponent_length = sizeof(unsigned long) + 1; // NOLINT 1454 unsigned int exponent_length = sizeof(unsigned long) + 1; // NOLINT
1516 const std::vector<uint8> long_exponent(exponent_length, 0x01); 1455 const std::vector<uint8> long_exponent(exponent_length, 0x01);
1517 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1456 algorithm = CreateRsaKeyGenAlgorithm(
1518 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1457 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1519 modulus_length, 1458 modulus_length,
1520 long_exponent); 1459 long_exponent);
1521 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(), 1460 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(),
1522 GenerateKeyPairInternal(algorithm, extractable, usage_mask, &public_key, 1461 GenerateKeyPair(algorithm, extractable, usage_mask, &public_key,
1523 &private_key)); 1462 &private_key));
1524 1463
1525 // Fail with bad exponent: empty. 1464 // Fail with bad exponent: empty.
1526 const std::vector<uint8> empty_exponent; 1465 const std::vector<uint8> empty_exponent;
1527 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1466 algorithm = CreateRsaKeyGenAlgorithm(
1528 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1467 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1529 modulus_length, 1468 modulus_length,
1530 empty_exponent); 1469 empty_exponent);
1531 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(), 1470 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(),
1532 GenerateKeyPairInternal(algorithm, extractable, usage_mask, &public_key, 1471 GenerateKeyPair(algorithm, extractable, usage_mask, &public_key,
1533 &private_key)); 1472 &private_key));
1534 1473
1535 // Fail with bad exponent: all zeros. 1474 // Fail with bad exponent: all zeros.
1536 std::vector<uint8> exponent_with_leading_zeros(15, 0x00); 1475 std::vector<uint8> exponent_with_leading_zeros(15, 0x00);
1537 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1476 algorithm = CreateRsaKeyGenAlgorithm(
1538 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1477 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1539 modulus_length, 1478 modulus_length,
1540 exponent_with_leading_zeros); 1479 exponent_with_leading_zeros);
1541 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(), 1480 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(),
1542 GenerateKeyPairInternal(algorithm, extractable, usage_mask, &public_key, 1481 GenerateKeyPair(algorithm, extractable, usage_mask, &public_key,
1543 &private_key)); 1482 &private_key));
1544 1483
1545 // Key generation success using exponent with leading zeros. 1484 // Key generation success using exponent with leading zeros.
1546 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(), 1485 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(),
1547 public_exponent.begin(), 1486 public_exponent.begin(),
1548 public_exponent.end()); 1487 public_exponent.end());
1549 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1488 algorithm = CreateRsaKeyGenAlgorithm(
1550 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1489 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1551 modulus_length, 1490 modulus_length,
1552 exponent_with_leading_zeros); 1491 exponent_with_leading_zeros);
1553 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1492 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1554 algorithm, extractable, usage_mask, &public_key, &private_key)); 1493 algorithm, extractable, usage_mask, &public_key, &private_key));
1555 EXPECT_FALSE(public_key.isNull()); 1494 EXPECT_FALSE(public_key.isNull());
1556 EXPECT_FALSE(private_key.isNull()); 1495 EXPECT_FALSE(private_key.isNull());
1557 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1496 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1558 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1497 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1559 EXPECT_TRUE(public_key.extractable()); 1498 EXPECT_TRUE(public_key.extractable());
1560 EXPECT_EQ(extractable, private_key.extractable()); 1499 EXPECT_EQ(extractable, private_key.extractable());
1561 EXPECT_EQ(usage_mask, public_key.usages()); 1500 EXPECT_EQ(usage_mask, public_key.usages());
1562 EXPECT_EQ(usage_mask, private_key.usages()); 1501 EXPECT_EQ(usage_mask, private_key.usages());
1563 1502
1564 // Successful WebCryptoAlgorithmIdRsaOaep key generation. 1503 // Successful WebCryptoAlgorithmIdRsaOaep key generation.
1565 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1504 algorithm = CreateRsaKeyGenAlgorithm(
1566 blink::WebCryptoAlgorithmIdRsaOaep, modulus_length, public_exponent); 1505 blink::WebCryptoAlgorithmIdRsaOaep, modulus_length, public_exponent);
1567 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1506 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1568 algorithm, extractable, usage_mask, &public_key, &private_key)); 1507 algorithm, extractable, usage_mask, &public_key, &private_key));
1569 EXPECT_FALSE(public_key.isNull()); 1508 EXPECT_FALSE(public_key.isNull());
1570 EXPECT_FALSE(private_key.isNull()); 1509 EXPECT_FALSE(private_key.isNull());
1571 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1510 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1572 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1511 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1573 EXPECT_TRUE(public_key.extractable()); 1512 EXPECT_TRUE(public_key.extractable());
1574 EXPECT_EQ(extractable, private_key.extractable()); 1513 EXPECT_EQ(extractable, private_key.extractable());
1575 EXPECT_EQ(usage_mask, public_key.usages()); 1514 EXPECT_EQ(usage_mask, public_key.usages());
1576 EXPECT_EQ(usage_mask, private_key.usages()); 1515 EXPECT_EQ(usage_mask, private_key.usages());
1577 1516
1578 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation. 1517 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation.
1579 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1518 algorithm = CreateRsaKeyGenAlgorithm(
1580 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1519 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1581 modulus_length, 1520 modulus_length,
1582 public_exponent); 1521 public_exponent);
1583 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1522 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1584 algorithm, false, usage_mask, &public_key, &private_key)); 1523 algorithm, false, usage_mask, &public_key, &private_key));
1585 EXPECT_FALSE(public_key.isNull()); 1524 EXPECT_FALSE(public_key.isNull());
1586 EXPECT_FALSE(private_key.isNull()); 1525 EXPECT_FALSE(private_key.isNull());
1587 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1526 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1588 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1527 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1589 // Even though "extractable" was set to false, the public key remains 1528 // Even though "extractable" was set to false, the public key remains
1590 // extractable. 1529 // extractable.
1591 EXPECT_TRUE(public_key.extractable()); 1530 EXPECT_TRUE(public_key.extractable());
1592 EXPECT_FALSE(private_key.extractable()); 1531 EXPECT_FALSE(private_key.extractable());
1593 EXPECT_EQ(usage_mask, public_key.usages()); 1532 EXPECT_EQ(usage_mask, public_key.usages());
1594 EXPECT_EQ(usage_mask, private_key.usages()); 1533 EXPECT_EQ(usage_mask, private_key.usages());
1595 1534
1596 // Exporting a private key as SPKI format doesn't make sense. However this 1535 // Exporting a private key as SPKI format doesn't make sense. However this
1597 // will first fail because the key is not extractable. 1536 // will first fail because the key is not extractable.
1598 blink::WebArrayBuffer output; 1537 blink::WebArrayBuffer output;
1599 EXPECT_STATUS(Status::ErrorKeyNotExtractable(), ExportKeyInternal( 1538 EXPECT_STATUS(Status::ErrorKeyNotExtractable(), ExportKey(
1600 blink::WebCryptoKeyFormatSpki, private_key, &output)); 1539 blink::WebCryptoKeyFormatSpki, private_key, &output));
1601 1540
1602 // Re-generate an extractable private_key and try to export it as SPKI format. 1541 // Re-generate an extractable private_key and try to export it as SPKI format.
1603 // This should fail since spki is for public keys. 1542 // This should fail since spki is for public keys.
1604 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1543 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1605 algorithm, true, usage_mask, &public_key, &private_key)); 1544 algorithm, true, usage_mask, &public_key, &private_key));
1606 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), ExportKeyInternal( 1545 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), ExportKey(
1607 blink::WebCryptoKeyFormatSpki, private_key, &output)); 1546 blink::WebCryptoKeyFormatSpki, private_key, &output));
1608 } 1547 }
1609 1548
1610 TEST_F(WebCryptoImplTest, MAYBE(RsaEsRoundTrip)) { 1549 TEST_F(SharedCryptoTest, MAYBE(RsaEsRoundTrip)) {
1611 // Import a key pair. 1550 // Import a key pair.
1612 blink::WebCryptoAlgorithm algorithm = 1551 blink::WebCryptoAlgorithm algorithm =
1613 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1552 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1614 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1553 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1615 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1554 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1616 ImportRsaKeyPair( 1555 ImportRsaKeyPair(
1617 HexStringToBytes(kPublicKeySpkiDerHex), 1556 HexStringToBytes(kPublicKeySpkiDerHex),
1618 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1557 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1619 algorithm, 1558 algorithm,
1620 false, 1559 false,
1621 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 1560 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
1622 &public_key, 1561 &public_key,
1623 &private_key); 1562 &private_key);
1624 1563
1625 // Make a maximum-length data message. RSAES can operate on messages up to 1564 // Make a maximum-length data message. RSAES can operate on messages up to
1626 // length of k - 11 bytes, where k is the octet length of the RSA modulus. 1565 // length of k - 11 bytes, where k is the octet length of the RSA modulus.
1627 const unsigned int kMaxMsgSizeBytes = kModulusLength / 8 - 11; 1566 const unsigned int kMaxMsgSizeBytes = kModulusLength / 8 - 11;
1628 // There are two hex chars for each byte. 1567 // There are two hex chars for each byte.
1629 const unsigned int kMsgHexSize = kMaxMsgSizeBytes * 2; 1568 const unsigned int kMsgHexSize = kMaxMsgSizeBytes * 2;
1630 char max_data_hex[kMsgHexSize+1]; 1569 char max_data_hex[kMsgHexSize+1];
1631 std::fill(&max_data_hex[0], &max_data_hex[0] + kMsgHexSize, 'a'); 1570 std::fill(&max_data_hex[0], &max_data_hex[0] + kMsgHexSize, 'a');
1632 max_data_hex[kMsgHexSize] = '\0'; 1571 max_data_hex[kMsgHexSize] = '\0';
1633 1572
1634 // Verify encrypt / decrypt round trip on a few messages. Note that RSA 1573 // Verify encrypt / decrypt round trip on a few messages. Note that RSA
1635 // encryption does not support empty input. 1574 // encryption does not support empty input.
1636 algorithm = 1575 algorithm =
1637 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1576 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1638 const char* const kTestDataHex[] = { 1577 const char* const kTestDataHex[] = {
1639 "ff", 1578 "ff",
1640 "0102030405060708090a0b0c0d0e0f", 1579 "0102030405060708090a0b0c0d0e0f",
1641 max_data_hex 1580 max_data_hex
1642 }; 1581 };
1643 blink::WebArrayBuffer encrypted_data; 1582 blink::WebArrayBuffer encrypted_data;
1644 blink::WebArrayBuffer decrypted_data; 1583 blink::WebArrayBuffer decrypted_data;
1645 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestDataHex); ++i) { 1584 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestDataHex); ++i) {
1646 SCOPED_TRACE(i); 1585 SCOPED_TRACE(i);
1647 EXPECT_STATUS_SUCCESS(EncryptInternal( 1586 EXPECT_STATUS_SUCCESS(EncryptInternal(
1648 algorithm, 1587 algorithm,
1649 public_key, 1588 public_key,
1650 HexStringToBytes(kTestDataHex[i]), 1589 HexStringToBytes(kTestDataHex[i]),
1651 &encrypted_data)); 1590 &encrypted_data));
1652 EXPECT_EQ(kModulusLength / 8, encrypted_data.byteLength()); 1591 EXPECT_EQ(kModulusLength / 8, encrypted_data.byteLength());
1653 ASSERT_STATUS_SUCCESS(DecryptInternal( 1592 ASSERT_STATUS_SUCCESS(Decrypt(
1654 algorithm, 1593 algorithm,
1655 private_key, 1594 private_key,
1656 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1595 CryptoData(encrypted_data),
1657 encrypted_data.byteLength(),
1658 &decrypted_data)); 1596 &decrypted_data));
1659 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data); 1597 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data);
1660 } 1598 }
1661 } 1599 }
1662 1600
1663 TEST_F(WebCryptoImplTest, MAYBE(RsaEsKnownAnswer)) { 1601 TEST_F(SharedCryptoTest, MAYBE(RsaEsKnownAnswer)) {
1664 scoped_ptr<base::Value> json; 1602 scoped_ptr<base::Value> json;
1665 ASSERT_TRUE(ReadJsonTestFile("rsa_es.json", &json)); 1603 ASSERT_TRUE(ReadJsonTestFile("rsa_es.json", &json));
1666 base::DictionaryValue* test = NULL; 1604 base::DictionaryValue* test = NULL;
1667 ASSERT_TRUE(json->GetAsDictionary(&test)); 1605 ASSERT_TRUE(json->GetAsDictionary(&test));
1668 1606
1669 // Because the random data in PKCS1.5 padding makes the encryption output non- 1607 // Because the random data in PKCS1.5 padding makes the encryption output non-
1670 // deterministic, we cannot easily do a typical known-answer test for RSA 1608 // deterministic, we cannot easily do a typical known-answer test for RSA
1671 // encryption / decryption. Instead we will take a known-good encrypted 1609 // encryption / decryption. Instead we will take a known-good encrypted
1672 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the 1610 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the
1673 // original known cleartext is the result. 1611 // original known cleartext is the result.
1674 1612
1675 const std::vector<uint8> rsa_spki_der = 1613 const std::vector<uint8> rsa_spki_der =
1676 GetBytesFromHexString(test, "rsa_spki_der"); 1614 GetBytesFromHexString(test, "rsa_spki_der");
1677 1615
1678 const std::vector<uint8> rsa_pkcs8_der = 1616 const std::vector<uint8> rsa_pkcs8_der =
1679 GetBytesFromHexString(test, "rsa_pkcs8_der"); 1617 GetBytesFromHexString(test, "rsa_pkcs8_der");
1680 const std::vector<uint8> ciphertext = 1618 const std::vector<uint8> ciphertext =
1681 GetBytesFromHexString(test, "ciphertext"); 1619 GetBytesFromHexString(test, "ciphertext");
1682 const std::vector<uint8> cleartext = 1620 const std::vector<uint8> cleartext =
1683 GetBytesFromHexString(test, "cleartext"); 1621 GetBytesFromHexString(test, "cleartext");
1684 1622
1685 // Import the key pair. 1623 // Import the key pair.
1686 blink::WebCryptoAlgorithm algorithm = 1624 blink::WebCryptoAlgorithm algorithm =
1687 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1625 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1688 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1626 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1689 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1627 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1690 ImportRsaKeyPair( 1628 ImportRsaKeyPair(
1691 rsa_spki_der, 1629 rsa_spki_der,
1692 rsa_pkcs8_der, 1630 rsa_pkcs8_der,
1693 algorithm, 1631 algorithm,
1694 false, 1632 false,
1695 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 1633 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
1696 &public_key, 1634 &public_key,
1697 &private_key); 1635 &private_key);
1698 1636
1699 // Decrypt the known-good ciphertext with the private key. As a check we must 1637 // Decrypt the known-good ciphertext with the private key. As a check we must
1700 // get the known original cleartext. 1638 // get the known original cleartext.
1701 blink::WebArrayBuffer decrypted_data; 1639 blink::WebArrayBuffer decrypted_data;
1702 ASSERT_STATUS_SUCCESS(DecryptInternal( 1640 ASSERT_STATUS_SUCCESS(DecryptInternal(
1703 algorithm, 1641 algorithm,
1704 private_key, 1642 private_key,
1705 ciphertext, 1643 ciphertext,
1706 &decrypted_data)); 1644 &decrypted_data));
1707 EXPECT_FALSE(decrypted_data.isNull()); 1645 EXPECT_FALSE(decrypted_data.isNull());
1708 ExpectArrayBufferMatches(cleartext, decrypted_data); 1646 ExpectArrayBufferMatches(cleartext, decrypted_data);
1709 1647
1710 // Encrypt this decrypted data with the public key. 1648 // Encrypt this decrypted data with the public key.
1711 blink::WebArrayBuffer encrypted_data; 1649 blink::WebArrayBuffer encrypted_data;
1712 ASSERT_STATUS_SUCCESS(EncryptInternal( 1650 ASSERT_STATUS_SUCCESS(Encrypt(
1713 algorithm, 1651 algorithm,
1714 public_key, 1652 public_key,
1715 reinterpret_cast<const unsigned char*>(decrypted_data.data()), 1653 CryptoData(decrypted_data),
1716 decrypted_data.byteLength(),
1717 &encrypted_data)); 1654 &encrypted_data));
1718 EXPECT_EQ(128u, encrypted_data.byteLength()); 1655 EXPECT_EQ(128u, encrypted_data.byteLength());
1719 1656
1720 // Finally, decrypt the newly encrypted result with the private key, and 1657 // Finally, decrypt the newly encrypted result with the private key, and
1721 // compare to the known original cleartext. 1658 // compare to the known original cleartext.
1722 decrypted_data.reset(); 1659 decrypted_data.reset();
1723 ASSERT_STATUS_SUCCESS(DecryptInternal( 1660 ASSERT_STATUS_SUCCESS(Decrypt(
1724 algorithm, 1661 algorithm,
1725 private_key, 1662 private_key,
1726 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1663 CryptoData(encrypted_data),
1727 encrypted_data.byteLength(),
1728 &decrypted_data)); 1664 &decrypted_data));
1729 EXPECT_FALSE(decrypted_data.isNull()); 1665 EXPECT_FALSE(decrypted_data.isNull());
1730 ExpectArrayBufferMatches(cleartext, decrypted_data); 1666 ExpectArrayBufferMatches(cleartext, decrypted_data);
1731 } 1667 }
1732 1668
1733 TEST_F(WebCryptoImplTest, MAYBE(RsaEsFailures)) { 1669 TEST_F(SharedCryptoTest, MAYBE(RsaEsFailures)) {
1734 // Import a key pair. 1670 // Import a key pair.
1735 blink::WebCryptoAlgorithm algorithm = 1671 blink::WebCryptoAlgorithm algorithm =
1736 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1672 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1737 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1673 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1738 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1674 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1739 ImportRsaKeyPair( 1675 ImportRsaKeyPair(
1740 HexStringToBytes(kPublicKeySpkiDerHex), 1676 HexStringToBytes(kPublicKeySpkiDerHex),
1741 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1677 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1742 algorithm, 1678 algorithm,
1743 false, 1679 false,
1744 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 1680 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
1745 &public_key, 1681 &public_key,
1746 &private_key); 1682 &private_key);
(...skipping 18 matching lines...) Expand all
1765 public_key, 1701 public_key,
1766 std::vector<uint8>(kMaxMsgSizeBytes + 1, '0'), 1702 std::vector<uint8>(kMaxMsgSizeBytes + 1, '0'),
1767 &encrypted_data)); 1703 &encrypted_data));
1768 1704
1769 // Generate encrypted data. 1705 // Generate encrypted data.
1770 EXPECT_STATUS(Status::Success(), 1706 EXPECT_STATUS(Status::Success(),
1771 EncryptInternal(algorithm, public_key, message_hex, &encrypted_data)); 1707 EncryptInternal(algorithm, public_key, message_hex, &encrypted_data));
1772 1708
1773 // Fail decrypt with a public key. 1709 // Fail decrypt with a public key.
1774 blink::WebArrayBuffer decrypted_data; 1710 blink::WebArrayBuffer decrypted_data;
1775 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), DecryptInternal( 1711 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), Decrypt(
1776 algorithm, 1712 algorithm,
1777 public_key, 1713 public_key,
1778 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1714 CryptoData(encrypted_data),
1779 encrypted_data.byteLength(),
1780 &decrypted_data)); 1715 &decrypted_data));
1781 1716
1782 // Corrupt encrypted data; ensure decrypt fails because padding was disrupted. 1717 // Corrupt encrypted data; ensure decrypt fails because padding was disrupted.
1783 std::vector<uint8> corrupted_data( 1718 std::vector<uint8> corrupted_data(
1784 static_cast<uint8*>(encrypted_data.data()), 1719 static_cast<uint8*>(encrypted_data.data()),
1785 static_cast<uint8*>(encrypted_data.data()) + encrypted_data.byteLength()); 1720 static_cast<uint8*>(encrypted_data.data()) + encrypted_data.byteLength());
1786 corrupted_data[corrupted_data.size() / 2] ^= 0x01; 1721 corrupted_data[corrupted_data.size() / 2] ^= 0x01;
1787 EXPECT_STATUS(Status::Error(), 1722 EXPECT_STATUS(Status::Error(),
1788 DecryptInternal(algorithm, private_key, corrupted_data, &decrypted_data)); 1723 DecryptInternal(algorithm, private_key, corrupted_data, &decrypted_data));
1789 1724
1790 // TODO(padolph): Are there other specific data corruption scenarios to 1725 // TODO(padolph): Are there other specific data corruption scenarios to
1791 // consider? 1726 // consider?
1792 1727
1793 // Do a successful decrypt with good data just for confirmation. 1728 // Do a successful decrypt with good data just for confirmation.
1794 EXPECT_STATUS_SUCCESS(DecryptInternal( 1729 EXPECT_STATUS_SUCCESS(Decrypt(
1795 algorithm, 1730 algorithm,
1796 private_key, 1731 private_key,
1797 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1732 CryptoData(encrypted_data),
1798 encrypted_data.byteLength(),
1799 &decrypted_data)); 1733 &decrypted_data));
1800 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data); 1734 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data);
1801 } 1735 }
1802 1736
1803 TEST_F(WebCryptoImplTest, MAYBE(RsaSsaSignVerifyFailures)) { 1737 TEST_F(SharedCryptoTest, MAYBE(RsaSsaSignVerifyFailures)) {
1804 // Import a key pair. 1738 // Import a key pair.
1805 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( 1739 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash(
1806 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1740 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1807 blink::WebCryptoAlgorithmIdSha1); 1741 blink::WebCryptoAlgorithmIdSha1);
1808 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1742 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1809 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1743 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1810 ImportRsaKeyPair( 1744 ImportRsaKeyPair(
1811 HexStringToBytes(kPublicKeySpkiDerHex), 1745 HexStringToBytes(kPublicKeySpkiDerHex),
1812 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1746 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1813 algorithm, 1747 algorithm,
1814 false, 1748 false,
1815 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, 1749 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify,
1816 &public_key, 1750 &public_key,
1817 &private_key); 1751 &private_key);
1818 1752
1819 blink::WebArrayBuffer signature; 1753 blink::WebArrayBuffer signature;
1820 bool signature_match; 1754 bool signature_match;
1821 1755
1822 // Compute a signature. 1756 // Compute a signature.
1823 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); 1757 const std::vector<uint8> data = HexStringToBytes("010203040506070809");
1824 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, private_key, data, &signature)); 1758 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, private_key, data, &signature));
1825 1759
1826 // Ensure truncated signature does not verify by passing one less byte. 1760 // Ensure truncated signature does not verify by passing one less byte.
1827 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1761 EXPECT_STATUS_SUCCESS(VerifySignature(
1828 algorithm, 1762 algorithm,
1829 public_key, 1763 public_key,
1830 static_cast<const unsigned char*>(signature.data()), 1764 CryptoData(reinterpret_cast<const unsigned char*>(signature.data()),
1831 signature.byteLength() - 1, 1765 signature.byteLength() - 1),
1832 data, 1766 CryptoData(data),
1833 &signature_match)); 1767 &signature_match));
1834 EXPECT_FALSE(signature_match); 1768 EXPECT_FALSE(signature_match);
1835 1769
1836 // Ensure truncated signature does not verify by passing no bytes. 1770 // Ensure truncated signature does not verify by passing no bytes.
1837 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1771 EXPECT_STATUS_SUCCESS(VerifySignature(
1838 algorithm, 1772 algorithm,
1839 public_key, 1773 public_key,
1840 NULL, 1774 CryptoData(),
1841 0, 1775 CryptoData(data),
1842 data,
1843 &signature_match)); 1776 &signature_match));
1844 EXPECT_FALSE(signature_match); 1777 EXPECT_FALSE(signature_match);
1845 1778
1846 // Ensure corrupted signature does not verify. 1779 // Ensure corrupted signature does not verify.
1847 std::vector<uint8> corrupt_sig( 1780 std::vector<uint8> corrupt_sig(
1848 static_cast<uint8*>(signature.data()), 1781 static_cast<uint8*>(signature.data()),
1849 static_cast<uint8*>(signature.data()) + signature.byteLength()); 1782 static_cast<uint8*>(signature.data()) + signature.byteLength());
1850 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1; 1783 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1;
1851 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1784 EXPECT_STATUS_SUCCESS(VerifySignatureInternal(
1852 algorithm, 1785 algorithm,
1853 public_key, 1786 public_key,
1854 webcrypto::Uint8VectorStart(corrupt_sig), 1787 CryptoData(corrupt_sig),
1855 corrupt_sig.size(),
1856 data, 1788 data,
1857 &signature_match)); 1789 &signature_match));
1858 EXPECT_FALSE(signature_match); 1790 EXPECT_FALSE(signature_match);
1859 1791
1860 // Ensure signatures that are greater than the modulus size fail. 1792 // Ensure signatures that are greater than the modulus size fail.
1861 const unsigned int long_message_size_bytes = 1024; 1793 const unsigned int long_message_size_bytes = 1024;
1862 DCHECK_GT(long_message_size_bytes, kModulusLength/8); 1794 DCHECK_GT(long_message_size_bytes, kModulusLength/8);
1863 const unsigned char kLongSignature[long_message_size_bytes] = { 0 }; 1795 const unsigned char kLongSignature[long_message_size_bytes] = { 0 };
1864 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1796 EXPECT_STATUS_SUCCESS(VerifySignature(
1865 algorithm, 1797 algorithm,
1866 public_key, 1798 public_key,
1867 kLongSignature, 1799 CryptoData(kLongSignature, sizeof(kLongSignature)),
1868 sizeof(kLongSignature), 1800 CryptoData(data),
1869 data,
1870 &signature_match)); 1801 &signature_match));
1871 EXPECT_FALSE(signature_match); 1802 EXPECT_FALSE(signature_match);
1872 1803
1873 // Ensure that verifying using a private key, rather than a public key, fails. 1804 // Ensure that verifying using a private key, rather than a public key, fails.
1874 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), VerifySignatureInternal( 1805 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), VerifySignature(
1875 algorithm, 1806 algorithm,
1876 private_key, 1807 private_key,
1877 static_cast<const unsigned char*>(signature.data()), 1808 CryptoData(signature),
1878 signature.byteLength(), 1809 CryptoData(data),
1879 data,
1880 &signature_match)); 1810 &signature_match));
1881 1811
1882 // Ensure that signing using a public key, rather than a private key, fails. 1812 // Ensure that signing using a public key, rather than a private key, fails.
1883 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), 1813 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(),
1884 SignInternal(algorithm, public_key, data, &signature)); 1814 SignInternal(algorithm, public_key, data, &signature));
1885 1815
1886 // Ensure that signing and verifying with an incompatible algorithm fails. 1816 // Ensure that signing and verifying with an incompatible algorithm fails.
1887 algorithm = 1817 algorithm =
1888 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1818 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1889 EXPECT_STATUS(Status::ErrorUnsupported(), 1819 EXPECT_STATUS(Status::ErrorUnexpected(),
1890 SignInternal(algorithm, private_key, data, &signature)); 1820 SignInternal(algorithm, private_key, data, &signature));
1891 EXPECT_STATUS(Status::ErrorUnsupported(), VerifySignatureInternal( 1821 EXPECT_STATUS(Status::ErrorUnexpected(), VerifySignature(
1892 algorithm, 1822 algorithm, public_key, CryptoData(signature), CryptoData(data),
1893 public_key,
1894 static_cast<const unsigned char*>(signature.data()),
1895 signature.byteLength(),
1896 data,
1897 &signature_match)); 1823 &signature_match));
1898 1824
1899 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash 1825 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash
1900 // based solely on the contents of the input signature data. In the Web Crypto 1826 // based solely on the contents of the input signature data. In the Web Crypto
1901 // implementation, the inner hash should be specified uniquely by the input 1827 // implementation, the inner hash should be specified uniquely by the input
1902 // algorithm parameter. To validate this behavior, call Verify with a computed 1828 // algorithm parameter. To validate this behavior, call Verify with a computed
1903 // signature that used one hash type (SHA-1), but pass in an algorithm with a 1829 // signature that used one hash type (SHA-1), but pass in an algorithm with a
1904 // different inner hash type (SHA-256). If the hash type is determined by the 1830 // different inner hash type (SHA-256). If the hash type is determined by the
1905 // signature itself (undesired), the verify will pass, while if the hash type 1831 // signature itself (undesired), the verify will pass, while if the hash type
1906 // is specified by the input algorithm (desired), the verify will fail. 1832 // is specified by the input algorithm (desired), the verify will fail.
1907 1833
1908 // Compute a signature using SHA-1 as the inner hash. 1834 // Compute a signature using SHA-1 as the inner hash.
1909 EXPECT_STATUS_SUCCESS(SignInternal(CreateRsaAlgorithmWithInnerHash( 1835 EXPECT_STATUS_SUCCESS(SignInternal(CreateRsaAlgorithmWithInnerHash(
1910 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1836 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1911 blink::WebCryptoAlgorithmIdSha1), 1837 blink::WebCryptoAlgorithmIdSha1),
1912 private_key, 1838 private_key,
1913 data, 1839 data,
1914 &signature)); 1840 &signature));
1915 1841
1916 // Now verify using an algorithm whose inner hash is SHA-256, not SHA-1. The 1842 // Now verify using an algorithm whose inner hash is SHA-256, not SHA-1. The
1917 // signature should not verify. 1843 // signature should not verify.
1918 // NOTE: public_key was produced by generateKey, and so its associated 1844 // NOTE: public_key was produced by generateKey, and so its associated
1919 // algorithm has WebCryptoRsaKeyGenParams and not WebCryptoRsaSsaParams. Thus 1845 // algorithm has WebCryptoRsaKeyGenParams and not WebCryptoRsaSsaParams. Thus
1920 // it has no inner hash to conflict with the input algorithm. 1846 // it has no inner hash to conflict with the input algorithm.
1921 bool is_match; 1847 bool is_match;
1922 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1848 EXPECT_STATUS_SUCCESS(VerifySignature(
1923 CreateRsaAlgorithmWithInnerHash( 1849 CreateRsaAlgorithmWithInnerHash(
1924 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1850 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1925 blink::WebCryptoAlgorithmIdSha256), 1851 blink::WebCryptoAlgorithmIdSha256),
1926 public_key, 1852 public_key,
1927 static_cast<const unsigned char*>(signature.data()), 1853 CryptoData(signature),
1928 signature.byteLength(), 1854 CryptoData(data),
1929 data,
1930 &is_match)); 1855 &is_match));
1931 EXPECT_FALSE(is_match); 1856 EXPECT_FALSE(is_match);
1932 } 1857 }
1933 1858
1934 TEST_F(WebCryptoImplTest, MAYBE(RsaSignVerifyKnownAnswer)) { 1859 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) {
1935 scoped_ptr<base::ListValue> tests; 1860 scoped_ptr<base::ListValue> tests;
1936 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); 1861 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests));
1937 1862
1938 // Import the key pair. 1863 // Import the key pair.
1939 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( 1864 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash(
1940 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1865 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1941 blink::WebCryptoAlgorithmIdSha1); 1866 blink::WebCryptoAlgorithmIdSha1);
1942 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1867 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1943 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1868 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1944 ImportRsaKeyPair( 1869 ImportRsaKeyPair(
(...skipping 27 matching lines...) Expand all
1972 ASSERT_STATUS_SUCCESS(VerifySignatureInternal( 1897 ASSERT_STATUS_SUCCESS(VerifySignatureInternal(
1973 algorithm, 1898 algorithm,
1974 public_key, 1899 public_key,
1975 test_signature, 1900 test_signature,
1976 test_message, 1901 test_message,
1977 &is_match)); 1902 &is_match));
1978 EXPECT_TRUE(is_match); 1903 EXPECT_TRUE(is_match);
1979 } 1904 }
1980 } 1905 }
1981 1906
1982 TEST_F(WebCryptoImplTest, MAYBE(AesKwKeyImport)) { 1907 TEST_F(SharedCryptoTest, MAYBE(AesKwKeyImport)) {
1983 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1908 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1984 blink::WebCryptoAlgorithm algorithm = 1909 blink::WebCryptoAlgorithm algorithm =
1985 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 1910 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
1986 1911
1987 // Import a 128-bit Key Encryption Key (KEK) 1912 // Import a 128-bit Key Encryption Key (KEK)
1988 std::string key_raw_hex_in = "025a8cf3f08b4f6c5f33bbc76a471939"; 1913 std::string key_raw_hex_in = "025a8cf3f08b4f6c5f33bbc76a471939";
1989 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1914 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
1990 HexStringToBytes(key_raw_hex_in), 1915 HexStringToBytes(key_raw_hex_in),
1991 algorithm, 1916 algorithm,
1992 true, 1917 true,
1993 blink::WebCryptoKeyUsageWrapKey, 1918 blink::WebCryptoKeyUsageWrapKey,
1994 &key)); 1919 &key));
1995 blink::WebArrayBuffer key_raw_out; 1920 blink::WebArrayBuffer key_raw_out;
1996 EXPECT_STATUS_SUCCESS(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, 1921 EXPECT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw,
1997 key, 1922 key,
1998 &key_raw_out)); 1923 &key_raw_out));
1999 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out); 1924 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out);
2000 1925
2001 // Import a 192-bit KEK 1926 // Import a 192-bit KEK
2002 key_raw_hex_in = "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103"; 1927 key_raw_hex_in = "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103";
2003 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1928 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
2004 HexStringToBytes(key_raw_hex_in), 1929 HexStringToBytes(key_raw_hex_in),
2005 algorithm, 1930 algorithm,
2006 true, 1931 true,
2007 blink::WebCryptoKeyUsageWrapKey, 1932 blink::WebCryptoKeyUsageWrapKey,
2008 &key)); 1933 &key));
2009 EXPECT_STATUS_SUCCESS(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, 1934 EXPECT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw,
2010 key, 1935 key,
2011 &key_raw_out)); 1936 &key_raw_out));
2012 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out); 1937 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out);
2013 1938
2014 // Import a 256-bit Key Encryption Key (KEK) 1939 // Import a 256-bit Key Encryption Key (KEK)
2015 key_raw_hex_in = 1940 key_raw_hex_in =
2016 "e11fe66380d90fa9ebefb74e0478e78f95664d0c67ca20ce4a0b5842863ac46f"; 1941 "e11fe66380d90fa9ebefb74e0478e78f95664d0c67ca20ce4a0b5842863ac46f";
2017 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1942 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
2018 HexStringToBytes(key_raw_hex_in), 1943 HexStringToBytes(key_raw_hex_in),
2019 algorithm, 1944 algorithm,
2020 true, 1945 true,
2021 blink::WebCryptoKeyUsageWrapKey, 1946 blink::WebCryptoKeyUsageWrapKey,
2022 &key)); 1947 &key));
2023 EXPECT_STATUS_SUCCESS(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, 1948 EXPECT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw,
2024 key, 1949 key,
2025 &key_raw_out)); 1950 &key_raw_out));
2026 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out); 1951 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out);
2027 1952
2028 // Fail import of 0 length key 1953 // Fail import of 0 length key
2029 EXPECT_STATUS(Status::Error(), 1954 EXPECT_STATUS(Status::Error(),
2030 ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1955 ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
2031 HexStringToBytes(""), 1956 HexStringToBytes(""),
2032 algorithm, 1957 algorithm,
2033 true, 1958 true,
2034 blink::WebCryptoKeyUsageWrapKey, 1959 blink::WebCryptoKeyUsageWrapKey,
2035 &key)); 1960 &key));
(...skipping 27 matching lines...) Expand all
2063 algorithm, 1988 algorithm,
2064 true, 1989 true,
2065 blink::WebCryptoKeyUsageWrapKey, 1990 blink::WebCryptoKeyUsageWrapKey,
2066 &key)); 1991 &key));
2067 } 1992 }
2068 1993
2069 // TODO(eroman): 1994 // TODO(eroman):
2070 // * Test decryption when the tag length exceeds input size 1995 // * Test decryption when the tag length exceeds input size
2071 // * Test decryption with empty input 1996 // * Test decryption with empty input
2072 // * Test decryption with tag length of 0. 1997 // * Test decryption with tag length of 0.
2073 TEST_F(WebCryptoImplTest, MAYBE(AesGcmSampleSets)) { 1998 TEST_F(SharedCryptoTest, MAYBE(AesGcmSampleSets)) {
2074 // Some Linux test runners may not have a new enough version of NSS. 1999 // Some Linux test runners may not have a new enough version of NSS.
2075 if (!SupportsAesGcm()) { 2000 if (!SupportsAesGcm()) {
2076 LOG(WARNING) << "AES GCM not supported, skipping tests"; 2001 LOG(WARNING) << "AES GCM not supported, skipping tests";
2077 return; 2002 return;
2078 } 2003 }
2079 2004
2080 scoped_ptr<base::ListValue> tests; 2005 scoped_ptr<base::ListValue> tests;
2081 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests)); 2006 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests));
2082 2007
2083 // Note that WebCrypto appends the authentication tag to the ciphertext. 2008 // Note that WebCrypto appends the authentication tag to the ciphertext.
2084 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 2009 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
2085 SCOPED_TRACE(test_index); 2010 SCOPED_TRACE(test_index);
2086 base::DictionaryValue* test; 2011 base::DictionaryValue* test;
2087 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 2012 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
2088 2013
2089 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 2014 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
2090 const std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); 2015 const std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv");
2091 const std::vector<uint8> test_additional_data = 2016 const std::vector<uint8> test_additional_data =
2092 GetBytesFromHexString(test, "additional_data"); 2017 GetBytesFromHexString(test, "additional_data");
2093 const std::vector<uint8> test_plain_text = 2018 const std::vector<uint8> test_plain_text =
2094 GetBytesFromHexString(test, "plain_text"); 2019 GetBytesFromHexString(test, "plain_text");
2095 const std::vector<uint8> test_authentication_tag = 2020 const std::vector<uint8> test_authentication_tag =
2096 GetBytesFromHexString(test, "authentication_tag"); 2021 GetBytesFromHexString(test, "authentication_tag");
2097 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8; 2022 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8;
2098 const std::vector<uint8> test_cipher_text = 2023 const std::vector<uint8> test_cipher_text =
2099 GetBytesFromHexString(test, "cipher_text"); 2024 GetBytesFromHexString(test, "cipher_text");
2100 2025
2101 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 2026 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
2102 test_key, 2027 test_key,
2103 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), 2028 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
2104 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 2029 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
2105 2030
2106 // Verify exported raw key is identical to the imported data 2031 // Verify exported raw key is identical to the imported data
2107 blink::WebArrayBuffer raw_key; 2032 blink::WebArrayBuffer raw_key;
2108 EXPECT_STATUS_SUCCESS(ExportKeyInternal( 2033 EXPECT_STATUS_SUCCESS(ExportKey(
2109 blink::WebCryptoKeyFormatRaw, key, &raw_key)); 2034 blink::WebCryptoKeyFormatRaw, key, &raw_key));
2110 2035
2111 ExpectArrayBufferMatches(test_key, raw_key); 2036 ExpectArrayBufferMatches(test_key, raw_key);
2112 2037
2113 // Test encryption. 2038 // Test encryption.
2114 std::vector<uint8> cipher_text; 2039 std::vector<uint8> cipher_text;
2115 std::vector<uint8> authentication_tag; 2040 std::vector<uint8> authentication_tag;
2116 EXPECT_STATUS_SUCCESS(AesGcmEncrypt(key, test_iv, test_additional_data, 2041 EXPECT_STATUS_SUCCESS(AesGcmEncrypt(key, test_iv, test_additional_data,
2117 test_tag_size_bits, test_plain_text, 2042 test_tag_size_bits, test_plain_text,
2118 &cipher_text, &authentication_tag)); 2043 &cipher_text, &authentication_tag));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2152 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i]; 2077 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i];
2153 if (test_tag_size_bits == wrong_tag_size_bits) 2078 if (test_tag_size_bits == wrong_tag_size_bits)
2154 continue; 2079 continue;
2155 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data, 2080 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data,
2156 wrong_tag_size_bits, test_cipher_text, 2081 wrong_tag_size_bits, test_cipher_text,
2157 test_authentication_tag, &plain_text)); 2082 test_authentication_tag, &plain_text));
2158 } 2083 }
2159 } 2084 }
2160 } 2085 }
2161 2086
2087 } // namespace webcrypto
2162 } // namespace content 2088 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698