ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / components / webcrypto / test / aes_gcm_unittest.cc
blob2d6514924e9cc12fe348f0e7bfb84e04a89667e2
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
3 // found in the LICENSE file.
5 #include "base/stl_util.h"
6 #include "components/webcrypto/algorithm_dispatch.h"
7 #include "components/webcrypto/crypto_data.h"
8 #include "components/webcrypto/status.h"
9 #include "components/webcrypto/test/test_helpers.h"
10 #include "components/webcrypto/webcrypto_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
15 namespace webcrypto {
17 namespace {
19 // Creates an AES-GCM algorithm.
20 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
21 const std::vector<uint8_t>& iv,
22 const std::vector<uint8_t>& additional_data,
23 unsigned int tag_length_bits) {
24 EXPECT_TRUE(SupportsAesGcm());
25 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
26 blink::WebCryptoAlgorithmIdAesGcm,
27 new blink::WebCryptoAesGcmParams(
28 vector_as_array(&iv), static_cast<unsigned int>(iv.size()), true,
29 vector_as_array(&additional_data),
30 static_cast<unsigned int>(additional_data.size()), true,
31 tag_length_bits));
34 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(
35 unsigned short key_length_bits) {
36 EXPECT_TRUE(SupportsAesGcm());
37 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm,
38 key_length_bits);
41 Status AesGcmEncrypt(const blink::WebCryptoKey& key,
42 const std::vector<uint8_t>& iv,
43 const std::vector<uint8_t>& additional_data,
44 unsigned int tag_length_bits,
45 const std::vector<uint8_t>& plain_text,
46 std::vector<uint8_t>* cipher_text,
47 std::vector<uint8_t>* authentication_tag) {
48 EXPECT_TRUE(SupportsAesGcm());
49 blink::WebCryptoAlgorithm algorithm =
50 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits);
52 std::vector<uint8_t> output;
53 Status status = Encrypt(algorithm, key, CryptoData(plain_text), &output);
54 if (status.IsError())
55 return status;
57 if ((tag_length_bits % 8) != 0) {
58 ADD_FAILURE() << "Encrypt should have failed.";
59 return Status::OperationError();
62 size_t tag_length_bytes = tag_length_bits / 8;
64 if (tag_length_bytes > output.size()) {
65 ADD_FAILURE() << "tag length is larger than output";
66 return Status::OperationError();
69 // The encryption result is cipher text with authentication tag appended.
70 cipher_text->assign(output.begin(),
71 output.begin() + (output.size() - tag_length_bytes));
72 authentication_tag->assign(output.begin() + cipher_text->size(),
73 output.end());
75 return Status::Success();
78 Status AesGcmDecrypt(const blink::WebCryptoKey& key,
79 const std::vector<uint8_t>& iv,
80 const std::vector<uint8_t>& additional_data,
81 unsigned int tag_length_bits,
82 const std::vector<uint8_t>& cipher_text,
83 const std::vector<uint8_t>& authentication_tag,
84 std::vector<uint8_t>* plain_text) {
85 EXPECT_TRUE(SupportsAesGcm());
86 blink::WebCryptoAlgorithm algorithm =
87 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits);
89 // Join cipher text and authentication tag.
90 std::vector<uint8_t> cipher_text_with_tag;
91 cipher_text_with_tag.reserve(cipher_text.size() + authentication_tag.size());
92 cipher_text_with_tag.insert(cipher_text_with_tag.end(), cipher_text.begin(),
93 cipher_text.end());
94 cipher_text_with_tag.insert(cipher_text_with_tag.end(),
95 authentication_tag.begin(),
96 authentication_tag.end());
98 return Decrypt(algorithm, key, CryptoData(cipher_text_with_tag), plain_text);
101 TEST(WebCryptoAesGcmTest, GenerateKeyBadLength) {
102 if (!SupportsAesGcm()) {
103 LOG(WARNING) << "AES GCM not supported, skipping tests";
104 return;
107 const unsigned short kKeyLen[] = {0, 127, 257};
108 blink::WebCryptoKey key;
109 for (size_t i = 0; i < arraysize(kKeyLen); ++i) {
110 SCOPED_TRACE(i);
111 EXPECT_EQ(Status::ErrorGenerateAesKeyLength(),
112 GenerateSecretKey(CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), true,
113 blink::WebCryptoKeyUsageDecrypt, &key));
117 TEST(WebCryptoAesGcmTest, GenerateKeyEmptyUsage) {
118 if (!SupportsAesGcm()) {
119 LOG(WARNING) << "AES GCM not supported, skipping tests";
120 return;
123 blink::WebCryptoKey key;
124 EXPECT_EQ(Status::ErrorCreateKeyEmptyUsages(),
125 GenerateSecretKey(CreateAesGcmKeyGenAlgorithm(256), true, 0, &key));
128 TEST(WebCryptoAesGcmTest, ImportExportJwk) {
129 // Some Linux test runners may not have a new enough version of NSS.
130 if (!SupportsAesGcm()) {
131 LOG(WARNING) << "AES GCM not supported, skipping tests";
132 return;
135 const blink::WebCryptoAlgorithm algorithm =
136 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm);
138 // AES-GCM 128
139 ImportExportJwkSymmetricKey(
140 128, algorithm,
141 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
142 "A128GCM");
144 // AES-GCM 256
145 ImportExportJwkSymmetricKey(256, algorithm, blink::WebCryptoKeyUsageDecrypt,
146 "A256GCM");
149 // TODO(eroman):
150 // * Test decryption when the tag length exceeds input size
151 // * Test decryption with empty input
152 // * Test decryption with tag length of 0.
153 TEST(WebCryptoAesGcmTest, SampleSets) {
154 // Some Linux test runners may not have a new enough version of NSS.
155 if (!SupportsAesGcm()) {
156 LOG(WARNING) << "AES GCM not supported, skipping tests";
157 return;
160 scoped_ptr<base::ListValue> tests;
161 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests));
163 // Note that WebCrypto appends the authentication tag to the ciphertext.
164 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
165 SCOPED_TRACE(test_index);
166 base::DictionaryValue* test;
167 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
169 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
170 const std::vector<uint8_t> test_iv = GetBytesFromHexString(test, "iv");
171 const std::vector<uint8_t> test_additional_data =
172 GetBytesFromHexString(test, "additional_data");
173 const std::vector<uint8_t> test_plain_text =
174 GetBytesFromHexString(test, "plain_text");
175 const std::vector<uint8_t> test_authentication_tag =
176 GetBytesFromHexString(test, "authentication_tag");
177 const unsigned int test_tag_size_bits =
178 static_cast<unsigned int>(test_authentication_tag.size()) * 8;
179 const std::vector<uint8_t> test_cipher_text =
180 GetBytesFromHexString(test, "cipher_text");
182 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
183 test_key, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
184 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
186 // Verify exported raw key is identical to the imported data
187 std::vector<uint8_t> raw_key;
188 EXPECT_EQ(Status::Success(),
189 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
191 EXPECT_BYTES_EQ(test_key, raw_key);
193 // Test encryption.
194 std::vector<uint8_t> cipher_text;
195 std::vector<uint8_t> authentication_tag;
196 EXPECT_EQ(
197 Status::Success(),
198 AesGcmEncrypt(key, test_iv, test_additional_data, test_tag_size_bits,
199 test_plain_text, &cipher_text, &authentication_tag));
201 EXPECT_BYTES_EQ(test_cipher_text, cipher_text);
202 EXPECT_BYTES_EQ(test_authentication_tag, authentication_tag);
204 // Test decryption.
205 std::vector<uint8_t> plain_text;
206 EXPECT_EQ(
207 Status::Success(),
208 AesGcmDecrypt(key, test_iv, test_additional_data, test_tag_size_bits,
209 test_cipher_text, test_authentication_tag, &plain_text));
210 EXPECT_BYTES_EQ(test_plain_text, plain_text);
212 // Decryption should fail if any of the inputs are tampered with.
213 EXPECT_EQ(Status::OperationError(),
214 AesGcmDecrypt(key, Corrupted(test_iv), test_additional_data,
215 test_tag_size_bits, test_cipher_text,
216 test_authentication_tag, &plain_text));
217 EXPECT_EQ(Status::OperationError(),
218 AesGcmDecrypt(key, test_iv, Corrupted(test_additional_data),
219 test_tag_size_bits, test_cipher_text,
220 test_authentication_tag, &plain_text));
221 EXPECT_EQ(Status::OperationError(),
222 AesGcmDecrypt(key, test_iv, test_additional_data,
223 test_tag_size_bits, Corrupted(test_cipher_text),
224 test_authentication_tag, &plain_text));
225 EXPECT_EQ(Status::OperationError(),
226 AesGcmDecrypt(key, test_iv, test_additional_data,
227 test_tag_size_bits, test_cipher_text,
228 Corrupted(test_authentication_tag), &plain_text));
230 // Try different incorrect tag lengths
231 uint8_t kAlternateTagLengths[] = {0, 8, 96, 120, 128, 160, 255};
232 for (size_t tag_i = 0; tag_i < arraysize(kAlternateTagLengths); ++tag_i) {
233 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i];
234 if (test_tag_size_bits == wrong_tag_size_bits)
235 continue;
236 EXPECT_NE(Status::Success(),
237 AesGcmDecrypt(key, test_iv, test_additional_data,
238 wrong_tag_size_bits, test_cipher_text,
239 test_authentication_tag, &plain_text));
244 } // namespace
246 } // namespace webcrypto