| 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 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 EXPECT_FALSE(DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), | 690 EXPECT_FALSE(DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), |
| 691 key, | 691 key, |
| 692 &cipher_text[0], | 692 &cipher_text[0], |
| 693 cipher_text.size() - 3, | 693 cipher_text.size() - 3, |
| 694 &output)); | 694 &output)); |
| 695 } | 695 } |
| 696 } | 696 } |
| 697 } | 697 } |
| 698 | 698 |
| 699 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { | 699 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { |
| 700 // Generate a small sample of AES keys. | 700 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each |
| 701 // allowed key length. |
| 702 std::vector<blink::WebCryptoAlgorithm> algorithm; |
| 703 const unsigned short kKeyLength[] = {128, 192, 256}; |
| 704 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) { |
| 705 algorithm.push_back(webcrypto::CreateAesCbcKeyGenAlgorithm(kKeyLength[i])); |
| 706 algorithm.push_back(webcrypto::CreateAesGcmKeyGenAlgorithm(kKeyLength[i])); |
| 707 algorithm.push_back(webcrypto::CreateAesKwKeyGenAlgorithm(kKeyLength[i])); |
| 708 } |
| 709 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 701 std::vector<blink::WebArrayBuffer> keys; | 710 std::vector<blink::WebArrayBuffer> keys; |
| 702 blink::WebArrayBuffer key_bytes; | 711 blink::WebArrayBuffer key_bytes; |
| 703 for (int i = 0; i < 16; ++i) { | 712 for (size_t i = 0; i < algorithm.size(); ++i) { |
| 704 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 713 SCOPED_TRACE(i); |
| 705 ASSERT_TRUE( | 714 // Generate a small sample of keys. |
| 706 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(128), &key)); | 715 keys.clear(); |
| 707 EXPECT_TRUE(key.handle()); | 716 for (int j = 0; j < 16; ++j) { |
| 708 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 717 ASSERT_TRUE(GenerateKeyInternal(algorithm[i], &key)); |
| 709 ASSERT_TRUE( | 718 EXPECT_TRUE(key.handle()); |
| 710 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); | 719 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 711 keys.push_back(key_bytes); | 720 ASSERT_TRUE( |
| 721 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); |
| 722 keys.push_back(key_bytes); |
| 723 } |
| 724 // Ensure all entries in the key sample set are unique. This is a simplistic |
| 725 // estimate of whether the generated keys appear random. |
| 726 EXPECT_FALSE(CopiesExist(keys)); |
| 712 } | 727 } |
| 713 // Ensure all entries in the key sample set are unique. This is a simplistic | |
| 714 // estimate of whether the generated keys appear random. | |
| 715 EXPECT_FALSE(CopiesExist(keys)); | |
| 716 } | 728 } |
| 717 | 729 |
| 718 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAesBadLength)) { | 730 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAesBadLength)) { |
| 731 const unsigned short kKeyLen[] = {0, 127, 257}; |
| 719 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 732 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 720 EXPECT_FALSE( | 733 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) { |
| 721 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key)); | 734 SCOPED_TRACE(i); |
| 722 EXPECT_FALSE( | 735 EXPECT_FALSE(GenerateKeyInternal( |
| 723 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key)); | 736 webcrypto::CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), &key)); |
| 724 EXPECT_FALSE( | 737 EXPECT_FALSE(GenerateKeyInternal( |
| 725 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(129), &key)); | 738 webcrypto::CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), &key)); |
| 739 EXPECT_FALSE(GenerateKeyInternal( |
| 740 webcrypto::CreateAesKwKeyGenAlgorithm(kKeyLen[i]), &key)); |
| 741 } |
| 726 } | 742 } |
| 727 | 743 |
| 728 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { | 744 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { |
| 729 // Generate a small sample of HMAC keys. | 745 // Generate a small sample of HMAC keys. |
| 730 std::vector<blink::WebArrayBuffer> keys; | 746 std::vector<blink::WebArrayBuffer> keys; |
| 731 for (int i = 0; i < 16; ++i) { | 747 for (int i = 0; i < 16; ++i) { |
| 732 blink::WebArrayBuffer key_bytes; | 748 blink::WebArrayBuffer key_bytes; |
| 733 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 749 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 734 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( | 750 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( |
| 735 blink::WebCryptoAlgorithmIdSha1, 128); | 751 blink::WebCryptoAlgorithmIdSha1, 128); |
| (...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1698 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; | 1714 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; |
| 1699 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, | 1715 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
| 1700 HexStringToBytes(key_raw_hex_in), | 1716 HexStringToBytes(key_raw_hex_in), |
| 1701 algorithm, | 1717 algorithm, |
| 1702 true, | 1718 true, |
| 1703 blink::WebCryptoKeyUsageWrapKey, | 1719 blink::WebCryptoKeyUsageWrapKey, |
| 1704 &key)); | 1720 &key)); |
| 1705 } | 1721 } |
| 1706 | 1722 |
| 1707 } // namespace content | 1723 } // namespace content |
| OLD | NEW |