OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/webcrypto_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | 420 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" |
421 "3371289b", | 421 "3371289b", |
422 // message | 422 // message |
423 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | 423 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" |
424 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | 424 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" |
425 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | 425 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" |
426 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | 426 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", |
427 // mac | 427 // mac |
428 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | 428 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", |
429 }, | 429 }, |
| 430 // L=28, Count=71 |
| 431 { |
| 432 blink::WebCryptoAlgorithmIdSha224, |
| 433 // key |
| 434 "6c2539f4d0453efbbacc137794930413aeb392e029e0724715f9d943d6dcf7cdcc7fc19" |
| 435 "7333df4fc476d5737ac3940d40eae", |
| 436 // message |
| 437 "1f207b3fa6c905529c9f9f7894b8941b616974df2c0cc482c400f50734f293139b5bbf9" |
| 438 "7384adfafc56494ca0629ed0ca179daf03056e33295eb19ec8dcd4dff898281b4b9409c" |
| 439 "a369f662d49091a225a678b1ebb75818dcb6278a2d136319f78f9ba9df5031a4f6305ee" |
| 440 "fde5b761d2f196ee318e89bcc4acebc2e11ed3b5dc4", |
| 441 // mac |
| 442 "4a7d9d13705b0faba0db75356c8ee0635afff1544911c69c2fbb1ab2" |
| 443 }, |
| 444 // L=48, Count=50 |
| 445 { |
| 446 blink::WebCryptoAlgorithmIdSha384, |
| 447 // key |
| 448 "d137f3e6cc4af28554beb03ba7a97e60c9d3959cd3bb08068edbf68d402d0498c6ee0ae" |
| 449 "9e3a20dc7d8586e5c352f605cee19", |
| 450 // message |
| 451 "64a884670d1c1dff555483dcd3da305dfba54bdc4d817c33ccb8fe7eb2ebf6236241031" |
| 452 "09ec41644fa078491900c59a0f666f0356d9bc0b45bcc79e5fc9850f4543d96bc680090" |
| 453 "44add0838ac1260e80592fbc557b2ddaf5ed1b86d3ed8f09e622e567f1d39a340857f6a" |
| 454 "850cceef6060c48dac3dd0071fe68eb4ed2ed9aca01", |
| 455 // mac |
| 456 "c550fa53514da34f15e7f98ea87226ab6896cdfae25d3ec2335839f755cdc9a4992092e" |
| 457 "70b7e5bd422784380b6396cf5" |
| 458 }, |
| 459 // L=64, Count=65 |
| 460 { |
| 461 blink::WebCryptoAlgorithmIdSha512, |
| 462 // key |
| 463 "c367aeb5c02b727883ffe2a4ceebf911b01454beb328fb5d57fc7f11bf744576aba421e2" |
| 464 "a63426ea8109bd28ff21f53cd2bf1a11c6c989623d6ec27cdb0bbf458250857d819ff844" |
| 465 "08b4f3dce08b98b1587ee59683af8852a0a5f55bda3ab5e132b4010e", |
| 466 // message |
| 467 "1a7331c8ff1b748e3cee96952190fdbbe4ee2f79e5753bbb368255ee5b19c05a4ed9f1b2" |
| 468 "c72ff1e9b9cb0348205087befa501e7793770faf0606e9c901836a9bc8afa00d7db94ee2" |
| 469 "9eb191d5cf3fc3e8da95a0f9f4a2a7964289c3129b512bd890de8700a9205420f28a8965" |
| 470 "b6c67be28ba7fe278e5fcd16f0f22cf2b2eacbb9", |
| 471 // mac |
| 472 "4459066109cb11e6870fa9c6bfd251adfa304c0a2928ca915049704972edc560cc7c0bc3" |
| 473 "8249e9101aae2f7d4da62eaff83fb07134efc277de72b9e4ab360425" |
| 474 }, |
430 }; | 475 }; |
431 | 476 |
432 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); | 477 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); |
433 ++test_index) { | 478 ++test_index) { |
434 SCOPED_TRACE(test_index); | 479 SCOPED_TRACE(test_index); |
435 const TestCase& test = kTests[test_index]; | 480 const TestCase& test = kTests[test_index]; |
436 | 481 |
437 blink::WebCryptoAlgorithm algorithm = | 482 blink::WebCryptoAlgorithm algorithm = |
438 webcrypto::CreateHmacAlgorithmByHashId(test.algorithm); | 483 webcrypto::CreateHmacAlgorithmByHashId(test.algorithm); |
439 | 484 |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(129), &key)); | 767 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(129), &key)); |
723 } | 768 } |
724 | 769 |
725 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { | 770 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { |
726 // Generate a small sample of HMAC keys. | 771 // Generate a small sample of HMAC keys. |
727 std::vector<blink::WebArrayBuffer> keys; | 772 std::vector<blink::WebArrayBuffer> keys; |
728 for (int i = 0; i < 16; ++i) { | 773 for (int i = 0; i < 16; ++i) { |
729 blink::WebArrayBuffer key_bytes; | 774 blink::WebArrayBuffer key_bytes; |
730 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 775 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
731 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( | 776 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( |
732 blink::WebCryptoAlgorithmIdSha1, 128); | 777 blink::WebCryptoAlgorithmIdSha1, 64); |
733 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); | 778 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
734 EXPECT_FALSE(key.isNull()); | 779 EXPECT_FALSE(key.isNull()); |
735 EXPECT_TRUE(key.handle()); | 780 EXPECT_TRUE(key.handle()); |
736 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 781 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 782 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 783 |
| 784 blink::WebArrayBuffer raw_key; |
| 785 ASSERT_TRUE(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 786 EXPECT_EQ(64U, raw_key.byteLength()); |
| 787 keys.push_back(raw_key); |
737 } | 788 } |
738 // Ensure all entries in the key sample set are unique. This is a simplistic | 789 // Ensure all entries in the key sample set are unique. This is a simplistic |
739 // estimate of whether the generated keys appear random. | 790 // estimate of whether the generated keys appear random. |
740 EXPECT_FALSE(CopiesExist(keys)); | 791 EXPECT_FALSE(CopiesExist(keys)); |
741 } | 792 } |
742 | 793 |
| 794 // If the key length is not provided, then the block size is used. |
743 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmacNoLength)) { | 795 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmacNoLength)) { |
744 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 796 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
745 blink::WebCryptoAlgorithm algorithm = | 797 blink::WebCryptoAlgorithm algorithm = |
746 webcrypto::CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 798 webcrypto::CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
747 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); | 799 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
748 EXPECT_TRUE(key.handle()); | 800 EXPECT_TRUE(key.handle()); |
749 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 801 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 802 blink::WebArrayBuffer raw_key; |
| 803 ASSERT_TRUE(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 804 EXPECT_EQ(64U, raw_key.byteLength()); |
| 805 |
| 806 // The block size for HMAC SHA-512 is larger. |
| 807 algorithm = webcrypto::CreateHmacKeyGenAlgorithm( |
| 808 blink::WebCryptoAlgorithmIdSha512, 0); |
| 809 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
| 810 ASSERT_TRUE(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 811 EXPECT_EQ(128U, raw_key.byteLength()); |
750 } | 812 } |
751 | 813 |
752 TEST_F(WebCryptoImplTest, MAYBE(ImportSecretKeyNoAlgorithm)) { | 814 TEST_F(WebCryptoImplTest, MAYBE(ImportSecretKeyNoAlgorithm)) { |
753 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 815 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
754 | 816 |
755 // This fails because the algorithm is null. | 817 // This fails because the algorithm is null. |
756 EXPECT_FALSE(ImportKeyInternal( | 818 EXPECT_FALSE(ImportKeyInternal( |
757 blink::WebCryptoKeyFormatRaw, | 819 blink::WebCryptoKeyFormatRaw, |
758 HexStringToBytes("00000000000000000000"), | 820 HexStringToBytes("00000000000000000000"), |
759 blink::WebCryptoAlgorithm::createNull(), | 821 blink::WebCryptoAlgorithm::createNull(), |
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1612 EXPECT_TRUE(DecryptInternal( | 1674 EXPECT_TRUE(DecryptInternal( |
1613 algorithm, | 1675 algorithm, |
1614 private_key, | 1676 private_key, |
1615 reinterpret_cast<const unsigned char*>(encrypted_data.data()), | 1677 reinterpret_cast<const unsigned char*>(encrypted_data.data()), |
1616 encrypted_data.byteLength(), | 1678 encrypted_data.byteLength(), |
1617 &decrypted_data)); | 1679 &decrypted_data)); |
1618 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data); | 1680 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data); |
1619 } | 1681 } |
1620 | 1682 |
1621 } // namespace content | 1683 } // namespace content |
OLD | NEW |