OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/stl_util.h" | |
6 #include "base/values.h" | 5 #include "base/values.h" |
7 #include "components/webcrypto/algorithm_dispatch.h" | 6 #include "components/webcrypto/algorithm_dispatch.h" |
8 #include "components/webcrypto/algorithms/test_helpers.h" | 7 #include "components/webcrypto/algorithms/test_helpers.h" |
9 #include "components/webcrypto/crypto_data.h" | 8 #include "components/webcrypto/crypto_data.h" |
10 #include "components/webcrypto/status.h" | 9 #include "components/webcrypto/status.h" |
11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 11 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
13 | 12 |
14 namespace webcrypto { | 13 namespace webcrypto { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 // Creates an AES-CTR algorithm for encryption/decryption. | 17 // Creates an AES-CTR algorithm for encryption/decryption. |
19 blink::WebCryptoAlgorithm CreateAesCtrAlgorithm( | 18 blink::WebCryptoAlgorithm CreateAesCtrAlgorithm( |
20 const std::vector<uint8_t>& counter, | 19 const std::vector<uint8_t>& counter, |
21 uint8_t length_bits) { | 20 uint8_t length_bits) { |
22 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 21 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
23 blink::WebCryptoAlgorithmIdAesCtr, | 22 blink::WebCryptoAlgorithmIdAesCtr, |
24 new blink::WebCryptoAesCtrParams( | 23 new blink::WebCryptoAesCtrParams( |
25 length_bits, vector_as_array(&counter), | 24 length_bits, counter.data(), |
26 static_cast<unsigned int>(counter.size()))); | 25 static_cast<unsigned int>(counter.size()))); |
27 } | 26 } |
28 | 27 |
29 class WebCryptoAesCtrTest : public WebCryptoTestBase {}; | 28 class WebCryptoAesCtrTest : public WebCryptoTestBase {}; |
30 | 29 |
31 TEST_F(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) { | 30 TEST_F(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) { |
32 scoped_ptr<base::ListValue> tests; | 31 scoped_ptr<base::ListValue> tests; |
33 ASSERT_TRUE(ReadJsonTestFileToList("aes_ctr.json", &tests)); | 32 ASSERT_TRUE(ReadJsonTestFileToList("aes_ctr.json", &tests)); |
34 | 33 |
35 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | 34 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 132 |
134 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 133 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
135 std::vector<uint8>(16), // 128-bit key of all zeros. | 134 std::vector<uint8>(16), // 128-bit key of all zeros. |
136 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), | 135 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), |
137 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 136 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
138 | 137 |
139 std::vector<uint8_t> buffer(272); | 138 std::vector<uint8_t> buffer(272); |
140 | 139 |
141 // 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes | 140 // 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes |
142 // long). | 141 // long). |
143 CryptoData input_16(vector_as_array(&buffer), 256); | 142 CryptoData input_16(buffer.data(), 256); |
144 CryptoData input_17(vector_as_array(&buffer), 272); | 143 CryptoData input_17(buffer.data(), 272); |
145 | 144 |
146 std::vector<uint8_t> output; | 145 std::vector<uint8_t> output; |
147 | 146 |
148 for (size_t i = 0; i < arraysize(kStartCounter); ++i) { | 147 for (size_t i = 0; i < arraysize(kStartCounter); ++i) { |
149 std::vector<uint8_t> counter(16); | 148 std::vector<uint8_t> counter(16); |
150 counter[15] = kStartCounter[i]; | 149 counter[15] = kStartCounter[i]; |
151 | 150 |
152 // Baseline test: Encrypting 16 blocks should work (don't bother to check | 151 // Baseline test: Encrypting 16 blocks should work (don't bother to check |
153 // output, the known answer tests already do that). | 152 // output, the known answer tests already do that). |
154 EXPECT_EQ(Status::Success(), | 153 EXPECT_EQ(Status::Success(), |
155 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, | 154 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, |
156 input_16, &output)); | 155 input_16, &output)); |
157 | 156 |
158 // Encrypting/Decrypting 17 however should fail. | 157 // Encrypting/Decrypting 17 however should fail. |
159 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(), | 158 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(), |
160 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, | 159 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, |
161 input_17, &output)); | 160 input_17, &output)); |
162 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(), | 161 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(), |
163 Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, | 162 Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, |
164 input_17, &output)); | 163 input_17, &output)); |
165 } | 164 } |
166 } | 165 } |
167 | 166 |
168 } // namespace | 167 } // namespace |
169 | 168 |
170 } // namespace webcrypto | 169 } // namespace webcrypto |
OLD | NEW |