ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / components / webcrypto / test / aes_ctr_unittest.cc
blob96453a1ba247023df9560e5142346b1bf75e976b
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 "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
14 namespace webcrypto {
16 namespace {
18 bool SupportsAesCtr() {
19 #if defined(USE_OPENSSL)
20 return true;
21 #else
22 return false;
23 #endif
26 // Creates an AES-CTR algorithm for encryption/decryption.
27 blink::WebCryptoAlgorithm CreateAesCtrAlgorithm(
28 const std::vector<uint8_t>& counter,
29 uint8_t length_bits) {
30 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
31 blink::WebCryptoAlgorithmIdAesCtr,
32 new blink::WebCryptoAesCtrParams(
33 length_bits, vector_as_array(&counter),
34 static_cast<unsigned int>(counter.size())));
37 TEST(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) {
38 if (!SupportsAesCtr()) {
39 LOG(WARNING) << "Skipping test because AES-CTR is not supported";
40 return;
43 scoped_ptr<base::ListValue> tests;
44 ASSERT_TRUE(ReadJsonTestFileToList("aes_ctr.json", &tests));
46 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
47 SCOPED_TRACE(test_index);
48 base::DictionaryValue* test;
49 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
51 std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
52 std::vector<uint8_t> test_counter = GetBytesFromHexString(test, "counter");
53 int counter_length_bits = 0;
54 ASSERT_TRUE(test->GetInteger("length", &counter_length_bits));
56 std::vector<uint8_t> test_plain_text =
57 GetBytesFromHexString(test, "plain_text");
58 std::vector<uint8_t> test_cipher_text =
59 GetBytesFromHexString(test, "cipher_text");
61 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
62 test_key, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
63 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
65 EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits());
67 std::vector<uint8_t> output;
69 // Test encryption.
70 EXPECT_EQ(Status::Success(),
71 Encrypt(CreateAesCtrAlgorithm(test_counter, counter_length_bits),
72 key, CryptoData(test_plain_text), &output));
73 EXPECT_BYTES_EQ(test_cipher_text, output);
75 // Test decryption.
76 EXPECT_EQ(Status::Success(),
77 Decrypt(CreateAesCtrAlgorithm(test_counter, counter_length_bits),
78 key, CryptoData(test_cipher_text), &output));
79 EXPECT_BYTES_EQ(test_plain_text, output);
83 // The counter block must be exactly 16 bytes.
84 TEST(WebCryptoAesCtrTest, InvalidCounterBlockLength) {
85 if (!SupportsAesCtr()) {
86 LOG(WARNING) << "Skipping test because AES-CTR is not supported";
87 return;
90 const unsigned int kBadCounterBlockLengthBytes[] = {0, 15, 17};
92 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
93 std::vector<uint8>(16), // 128-bit key of all zeros.
94 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
95 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
97 std::vector<uint8_t> input(32);
98 std::vector<uint8_t> output;
100 for (size_t i = 0; i < arraysize(kBadCounterBlockLengthBytes); ++i) {
101 std::vector<uint8_t> bad_counter(kBadCounterBlockLengthBytes[i]);
103 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(),
104 Encrypt(CreateAesCtrAlgorithm(bad_counter, 128), key,
105 CryptoData(input), &output));
107 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(),
108 Decrypt(CreateAesCtrAlgorithm(bad_counter, 128), key,
109 CryptoData(input), &output));
113 // The counter length cannot be less than 1 or greater than 128.
114 TEST(WebCryptoAesCtrTest, InvalidCounterLength) {
115 if (!SupportsAesCtr()) {
116 LOG(WARNING) << "Skipping test because AES-CTR is not supported";
117 return;
120 const uint8_t kBadCounterLengthBits[] = {0, 129};
122 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
123 std::vector<uint8>(16), // 128-bit key of all zeros.
124 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
125 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
127 std::vector<uint8_t> counter(16);
128 std::vector<uint8_t> input(32);
129 std::vector<uint8_t> output;
131 for (size_t i = 0; i < arraysize(kBadCounterLengthBits); ++i) {
132 uint8_t bad_counter_length_bits = kBadCounterLengthBits[i];
134 EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
135 Encrypt(CreateAesCtrAlgorithm(counter, bad_counter_length_bits),
136 key, CryptoData(input), &output));
138 EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
139 Decrypt(CreateAesCtrAlgorithm(counter, bad_counter_length_bits),
140 key, CryptoData(input), &output));
144 // Tests wrap-around using a 4-bit counter.
146 // Wrap-around is allowed, however if the counter repeats itself an error should
147 // be thrown.
149 // Using a 4-bit counter it is possible to encrypt 16 blocks. However the 17th
150 // block would end up wrapping back to the starting value.
151 TEST(WebCryptoAesCtrTest, OverflowAndRepeatCounter) {
152 if (!SupportsAesCtr()) {
153 LOG(WARNING) << "Skipping test because AES-CTR is not supported";
154 return;
157 const uint8_t kCounterLengthBits = 4;
158 const uint8_t kStartCounter[] = {0, 1, 15};
160 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
161 std::vector<uint8>(16), // 128-bit key of all zeros.
162 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
163 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
165 std::vector<uint8_t> buffer(272);
167 // 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes
168 // long).
169 CryptoData input_16(vector_as_array(&buffer), 256);
170 CryptoData input_17(vector_as_array(&buffer), 272);
172 std::vector<uint8_t> output;
174 for (size_t i = 0; i < arraysize(kStartCounter); ++i) {
175 std::vector<uint8_t> counter(16);
176 counter[15] = kStartCounter[i];
178 // Baseline test: Encrypting 16 blocks should work (don't bother to check
179 // output, the known answer tests already do that).
180 EXPECT_EQ(Status::Success(),
181 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key,
182 input_16, &output));
184 // Encrypting/Decrypting 17 however should fail.
185 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(),
186 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key,
187 input_17, &output));
188 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(),
189 Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key,
190 input_17, &output));
194 } // namespace
196 } // namespace webcrypto