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

Side by Side Diff: components/webcrypto/test/aes_ctr_unittest.cc

Issue 1304063015: [refactor] Rename the webcrypto/openssl and webcrypto/test directories. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@jwk_refactor
Patch Set: Created 5 years, 3 months 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
(Empty)
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.
4
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"
13
14 namespace webcrypto {
15
16 namespace {
17
18 // Creates an AES-CTR algorithm for encryption/decryption.
19 blink::WebCryptoAlgorithm CreateAesCtrAlgorithm(
20 const std::vector<uint8_t>& counter,
21 uint8_t length_bits) {
22 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
23 blink::WebCryptoAlgorithmIdAesCtr,
24 new blink::WebCryptoAesCtrParams(
25 length_bits, vector_as_array(&counter),
26 static_cast<unsigned int>(counter.size())));
27 }
28
29 TEST(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) {
30 scoped_ptr<base::ListValue> tests;
31 ASSERT_TRUE(ReadJsonTestFileToList("aes_ctr.json", &tests));
32
33 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
34 SCOPED_TRACE(test_index);
35 base::DictionaryValue* test;
36 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
37
38 std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
39 std::vector<uint8_t> test_counter = GetBytesFromHexString(test, "counter");
40 int counter_length_bits = 0;
41 ASSERT_TRUE(test->GetInteger("length", &counter_length_bits));
42
43 std::vector<uint8_t> test_plain_text =
44 GetBytesFromHexString(test, "plain_text");
45 std::vector<uint8_t> test_cipher_text =
46 GetBytesFromHexString(test, "cipher_text");
47
48 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
49 test_key, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
50 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
51
52 EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits());
53
54 std::vector<uint8_t> output;
55
56 // Test encryption.
57 EXPECT_EQ(Status::Success(),
58 Encrypt(CreateAesCtrAlgorithm(test_counter, counter_length_bits),
59 key, CryptoData(test_plain_text), &output));
60 EXPECT_BYTES_EQ(test_cipher_text, output);
61
62 // Test decryption.
63 EXPECT_EQ(Status::Success(),
64 Decrypt(CreateAesCtrAlgorithm(test_counter, counter_length_bits),
65 key, CryptoData(test_cipher_text), &output));
66 EXPECT_BYTES_EQ(test_plain_text, output);
67 }
68 }
69
70 // The counter block must be exactly 16 bytes.
71 TEST(WebCryptoAesCtrTest, InvalidCounterBlockLength) {
72 const unsigned int kBadCounterBlockLengthBytes[] = {0, 15, 17};
73
74 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
75 std::vector<uint8>(16), // 128-bit key of all zeros.
76 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
77 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
78
79 std::vector<uint8_t> input(32);
80 std::vector<uint8_t> output;
81
82 for (size_t i = 0; i < arraysize(kBadCounterBlockLengthBytes); ++i) {
83 std::vector<uint8_t> bad_counter(kBadCounterBlockLengthBytes[i]);
84
85 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(),
86 Encrypt(CreateAesCtrAlgorithm(bad_counter, 128), key,
87 CryptoData(input), &output));
88
89 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(),
90 Decrypt(CreateAesCtrAlgorithm(bad_counter, 128), key,
91 CryptoData(input), &output));
92 }
93 }
94
95 // The counter length cannot be less than 1 or greater than 128.
96 TEST(WebCryptoAesCtrTest, InvalidCounterLength) {
97 const uint8_t kBadCounterLengthBits[] = {0, 129};
98
99 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
100 std::vector<uint8>(16), // 128-bit key of all zeros.
101 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
102 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
103
104 std::vector<uint8_t> counter(16);
105 std::vector<uint8_t> input(32);
106 std::vector<uint8_t> output;
107
108 for (size_t i = 0; i < arraysize(kBadCounterLengthBits); ++i) {
109 uint8_t bad_counter_length_bits = kBadCounterLengthBits[i];
110
111 EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
112 Encrypt(CreateAesCtrAlgorithm(counter, bad_counter_length_bits),
113 key, CryptoData(input), &output));
114
115 EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
116 Decrypt(CreateAesCtrAlgorithm(counter, bad_counter_length_bits),
117 key, CryptoData(input), &output));
118 }
119 }
120
121 // Tests wrap-around using a 4-bit counter.
122 //
123 // Wrap-around is allowed, however if the counter repeats itself an error should
124 // be thrown.
125 //
126 // Using a 4-bit counter it is possible to encrypt 16 blocks. However the 17th
127 // block would end up wrapping back to the starting value.
128 TEST(WebCryptoAesCtrTest, OverflowAndRepeatCounter) {
129 const uint8_t kCounterLengthBits = 4;
130 const uint8_t kStartCounter[] = {0, 1, 15};
131
132 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
133 std::vector<uint8>(16), // 128-bit key of all zeros.
134 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
135 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
136
137 std::vector<uint8_t> buffer(272);
138
139 // 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes
140 // long).
141 CryptoData input_16(vector_as_array(&buffer), 256);
142 CryptoData input_17(vector_as_array(&buffer), 272);
143
144 std::vector<uint8_t> output;
145
146 for (size_t i = 0; i < arraysize(kStartCounter); ++i) {
147 std::vector<uint8_t> counter(16);
148 counter[15] = kStartCounter[i];
149
150 // Baseline test: Encrypting 16 blocks should work (don't bother to check
151 // output, the known answer tests already do that).
152 EXPECT_EQ(Status::Success(),
153 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key,
154 input_16, &output));
155
156 // Encrypting/Decrypting 17 however should fail.
157 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(),
158 Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key,
159 input_17, &output));
160 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(),
161 Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key,
162 input_17, &output));
163 }
164 }
165
166 } // namespace
167
168 } // namespace webcrypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698