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

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

Issue 119413002: [webcrypto] Add key generation for AES-GCM and AES-KW for NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 bool CopiesExist(std::vector<blink::WebArrayBuffer> bufs) { 95 bool CopiesExist(std::vector<blink::WebArrayBuffer> bufs) {
96 for (size_t i = 0; i < bufs.size(); ++i) { 96 for (size_t i = 0; i < bufs.size(); ++i) {
97 for (size_t j = i + 1; j < bufs.size(); ++j) { 97 for (size_t j = i + 1; j < bufs.size(); ++j) {
98 if (ArrayBuffersEqual(bufs[i], bufs[j])) 98 if (ArrayBuffersEqual(bufs[i], bufs[j]))
99 return true; 99 return true;
100 } 100 }
101 } 101 }
102 return false; 102 return false;
103 } 103 }
104 104
105 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
106 blink::WebCryptoAlgorithmId aes_alg_id,
107 unsigned short length) {
108 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
109 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length));
110 }
111
112 blink::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(
113 unsigned short key_length_bits) {
114 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesCbc,
115 key_length_bits);
116 }
117
118 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(
119 unsigned short key_length_bits) {
120 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm,
121 key_length_bits);
122 }
123
124 blink::WebCryptoAlgorithm CreateAesKwKeyGenAlgorithm(
125 unsigned short key_length_bits) {
126 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesKw,
127 key_length_bits);
128 }
129
105 } // namespace 130 } // namespace
106 131
107 class WebCryptoImplTest : public testing::Test { 132 class WebCryptoImplTest : public testing::Test {
108 protected: 133 protected:
109 blink::WebCryptoKey ImportSecretKeyFromRawHexString( 134 blink::WebCryptoKey ImportSecretKeyFromRawHexString(
110 const std::string& key_hex, 135 const std::string& key_hex,
111 const blink::WebCryptoAlgorithm& algorithm, 136 const blink::WebCryptoAlgorithm& algorithm,
112 blink::WebCryptoKeyUsageMask usage) { 137 blink::WebCryptoKeyUsageMask usage) {
113 std::vector<uint8> key_raw = HexStringToBytes(key_hex); 138 std::vector<uint8> key_raw = HexStringToBytes(key_hex);
114 139
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 EXPECT_FALSE(DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), 760 EXPECT_FALSE(DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv),
736 key, 761 key,
737 &cipher_text[0], 762 &cipher_text[0],
738 cipher_text.size() - 3, 763 cipher_text.size() - 3,
739 &output)); 764 &output));
740 } 765 }
741 } 766 }
742 } 767 }
743 768
744 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { 769 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) {
745 // Generate a small sample of AES keys. 770 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each
771 // allowed key length.
772 std::vector<blink::WebCryptoAlgorithm> algorithm;
773 const unsigned short kKeyLength[] = {128, 192, 256};
774 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) {
775 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i]));
776 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i]));
777 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i]));
778 }
779 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
746 std::vector<blink::WebArrayBuffer> keys; 780 std::vector<blink::WebArrayBuffer> keys;
747 blink::WebArrayBuffer key_bytes; 781 blink::WebArrayBuffer key_bytes;
748 for (int i = 0; i < 16; ++i) { 782 for (size_t i = 0; i < algorithm.size(); ++i) {
749 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 783 SCOPED_TRACE(i);
750 ASSERT_TRUE( 784 // Generate a small sample of keys.
751 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(128), &key)); 785 keys.clear();
752 EXPECT_TRUE(key.handle()); 786 for (int j = 0; j < 16; ++j) {
753 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 787 ASSERT_TRUE(GenerateKeyInternal(algorithm[i], &key));
754 ASSERT_TRUE( 788 EXPECT_TRUE(key.handle());
755 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); 789 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
756 keys.push_back(key_bytes); 790 ASSERT_TRUE(
791 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes));
792 keys.push_back(key_bytes);
793 }
794 // Ensure all entries in the key sample set are unique. This is a simplistic
795 // estimate of whether the generated keys appear random.
796 EXPECT_FALSE(CopiesExist(keys));
757 } 797 }
758 // Ensure all entries in the key sample set are unique. This is a simplistic
759 // estimate of whether the generated keys appear random.
760 EXPECT_FALSE(CopiesExist(keys));
761 } 798 }
762 799
763 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAesBadLength)) { 800 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAesBadLength)) {
801 const unsigned short kKeyLen[] = {0, 127, 257};
764 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 802 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
765 EXPECT_FALSE( 803 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) {
766 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key)); 804 SCOPED_TRACE(i);
767 EXPECT_FALSE( 805 EXPECT_FALSE(GenerateKeyInternal(
768 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key)); 806 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), &key));
769 EXPECT_FALSE( 807 EXPECT_FALSE(GenerateKeyInternal(
770 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(129), &key)); 808 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), &key));
809 EXPECT_FALSE(GenerateKeyInternal(
810 CreateAesKwKeyGenAlgorithm(kKeyLen[i]), &key));
811 }
771 } 812 }
772 813
773 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { 814 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) {
774 // Generate a small sample of HMAC keys. 815 // Generate a small sample of HMAC keys.
775 std::vector<blink::WebArrayBuffer> keys; 816 std::vector<blink::WebArrayBuffer> keys;
776 for (int i = 0; i < 16; ++i) { 817 for (int i = 0; i < 16; ++i) {
777 blink::WebArrayBuffer key_bytes; 818 blink::WebArrayBuffer key_bytes;
778 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 819 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
779 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( 820 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm(
780 blink::WebCryptoAlgorithmIdSha1, 64); 821 blink::WebCryptoAlgorithmIdSha1, 64);
(...skipping 979 matching lines...) Expand 10 before | Expand all | Expand 10 after
1760 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; 1801 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a";
1761 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1802 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
1762 HexStringToBytes(key_raw_hex_in), 1803 HexStringToBytes(key_raw_hex_in),
1763 algorithm, 1804 algorithm,
1764 true, 1805 true,
1765 blink::WebCryptoKeyUsageWrapKey, 1806 blink::WebCryptoKeyUsageWrapKey,
1766 &key)); 1807 &key));
1767 } 1808 }
1768 1809
1769 } // namespace content 1810 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/webcrypto/webcrypto_impl_nss.cc ('k') | content/renderer/webcrypto/webcrypto_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698