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

Side by Side Diff: net/quic/crypto/aes_128_gcm_12_encrypter_test.cc

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 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
« no previous file with comments | « net/quic/crypto/aes_128_gcm_12_encrypter.cc ('k') | net/quic/crypto/cert_compressor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "net/quic/crypto/aes_128_gcm_12_encrypter.h"
6
7 #include <memory>
8
9 #include "net/quic/quic_utils.h"
10 #include "net/quic/test_tools/quic_test_utils.h"
11
12 using base::StringPiece;
13 using std::string;
14
15 namespace {
16
17 // The AES GCM test vectors come from the file gcmEncryptExtIV128.rsp
18 // downloaded from http://csrc.nist.gov/groups/STM/cavp/index.html on
19 // 2013-02-01. The test vectors in that file look like this:
20 //
21 // [Keylen = 128]
22 // [IVlen = 96]
23 // [PTlen = 0]
24 // [AADlen = 0]
25 // [Taglen = 128]
26 //
27 // Count = 0
28 // Key = 11754cd72aec309bf52f7687212e8957
29 // IV = 3c819d9a9bed087615030b65
30 // PT =
31 // AAD =
32 // CT =
33 // Tag = 250327c674aaf477aef2675748cf6971
34 //
35 // Count = 1
36 // Key = ca47248ac0b6f8372a97ac43508308ed
37 // IV = ffd2b598feabc9019262d2be
38 // PT =
39 // AAD =
40 // CT =
41 // Tag = 60d20404af527d248d893ae495707d1a
42 //
43 // ...
44 //
45 // The gcmEncryptExtIV128.rsp file is huge (2.8 MB), so I selected just a
46 // few test vectors for this unit test.
47
48 // Describes a group of test vectors that all have a given key length, IV
49 // length, plaintext length, AAD length, and tag length.
50 struct TestGroupInfo {
51 size_t key_len;
52 size_t iv_len;
53 size_t pt_len;
54 size_t aad_len;
55 size_t tag_len;
56 };
57
58 // Each test vector consists of six strings of lowercase hexadecimal digits.
59 // The strings may be empty (zero length). A test vector with a nullptr |key|
60 // marks the end of an array of test vectors.
61 struct TestVector {
62 const char* key;
63 const char* iv;
64 const char* pt;
65 const char* aad;
66 const char* ct;
67 const char* tag;
68 };
69
70 const TestGroupInfo test_group_info[] = {
71 {128, 96, 0, 0, 128}, {128, 96, 0, 128, 128}, {128, 96, 128, 0, 128},
72 {128, 96, 408, 160, 128}, {128, 96, 408, 720, 128}, {128, 96, 104, 0, 128},
73 };
74
75 const TestVector test_group_0[] = {
76 {"11754cd72aec309bf52f7687212e8957", "3c819d9a9bed087615030b65", "", "", "",
77 "250327c674aaf477aef2675748cf6971"},
78 {"ca47248ac0b6f8372a97ac43508308ed", "ffd2b598feabc9019262d2be", "", "", "",
79 "60d20404af527d248d893ae495707d1a"},
80 {nullptr}};
81
82 const TestVector test_group_1[] = {
83 {"77be63708971c4e240d1cb79e8d77feb", "e0e00f19fed7ba0136a797f3", "",
84 "7a43ec1d9c0a5a78a0b16533a6213cab", "",
85 "209fcc8d3675ed938e9c7166709dd946"},
86 {"7680c5d3ca6154758e510f4d25b98820", "f8f105f9c3df4965780321f8", "",
87 "c94c410194c765e3dcc7964379758ed3", "",
88 "94dca8edfcf90bb74b153c8d48a17930"},
89 {nullptr}};
90
91 const TestVector test_group_2[] = {
92 {"7fddb57453c241d03efbed3ac44e371c", "ee283a3fc75575e33efd4887",
93 "d5de42b461646c255c87bd2962d3b9a2", "", "2ccda4a5415cb91e135c2a0f78c9b2fd",
94 "b36d1df9b9d5e596f83e8b7f52971cb3"},
95 {"ab72c77b97cb5fe9a382d9fe81ffdbed", "54cc7dc2c37ec006bcc6d1da",
96 "007c5e5b3e59df24a7c355584fc1518d", "", "0e1bde206a07a9c2c1b65300f8c64997",
97 "2b4401346697138c7a4891ee59867d0c"},
98 {nullptr}};
99
100 const TestVector test_group_3[] = {
101 {"fe47fcce5fc32665d2ae399e4eec72ba", "5adb9609dbaeb58cbd6e7275",
102 "7c0e88c88899a779228465074797cd4c2e1498d259b54390b85e3eef1c02df60e743f1"
103 "b840382c4bccaf3bafb4ca8429bea063",
104 "88319d6e1d3ffa5f987199166c8a9b56c2aeba5a",
105 "98f4826f05a265e6dd2be82db241c0fbbbf9ffb1c173aa83964b7cf539304373636525"
106 "3ddbc5db8778371495da76d269e5db3e",
107 "291ef1982e4defedaa2249f898556b47"},
108 {"ec0c2ba17aa95cd6afffe949da9cc3a8", "296bce5b50b7d66096d627ef",
109 "b85b3753535b825cbe5f632c0b843c741351f18aa484281aebec2f45bb9eea2d79d987"
110 "b764b9611f6c0f8641843d5d58f3a242",
111 "f8d00f05d22bf68599bcdeb131292ad6e2df5d14",
112 "a7443d31c26bdf2a1c945e29ee4bd344a99cfaf3aa71f8b3f191f83c2adfc7a0716299"
113 "5506fde6309ffc19e716eddf1a828c5a",
114 "890147971946b627c40016da1ecf3e77"},
115 {nullptr}};
116
117 const TestVector test_group_4[] = {
118 {"2c1f21cf0f6fb3661943155c3e3d8492", "23cb5ff362e22426984d1907",
119 "42f758836986954db44bf37c6ef5e4ac0adaf38f27252a1b82d02ea949c8a1a2dbc0d6"
120 "8b5615ba7c1220ff6510e259f06655d8",
121 "5d3624879d35e46849953e45a32a624d6a6c536ed9857c613b572b0333e701557a713e"
122 "3f010ecdf9a6bd6c9e3e44b065208645aff4aabee611b391528514170084ccf587177f"
123 "4488f33cfb5e979e42b6e1cfc0a60238982a7aec",
124 "81824f0e0d523db30d3da369fdc0d60894c7a0a20646dd015073ad2732bd989b14a222"
125 "b6ad57af43e1895df9dca2a5344a62cc",
126 "57a3ee28136e94c74838997ae9823f3a"},
127 {"d9f7d2411091f947b4d6f1e2d1f0fb2e", "e1934f5db57cc983e6b180e7",
128 "73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490"
129 "c2c6f6166f4a59431e182663fcaea05a",
130 "0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d"
131 "0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a201"
132 "15d2e51398344b16bee1ed7c499b353d6c597af8",
133 "aaadbd5c92e9151ce3db7210b8714126b73e43436d242677afa50384f2149b831f1d57"
134 "3c7891c2a91fbc48db29967ec9542b23",
135 "21b51ca862cb637cdd03b99a0f93b134"},
136 {nullptr}};
137
138 const TestVector test_group_5[] = {
139 {"fe9bb47deb3a61e423c2231841cfd1fb", "4d328eb776f500a2f7fb47aa",
140 "f1cc3818e421876bb6b8bbd6c9", "", "b88c5c1977b35b517b0aeae967",
141 "43fd4727fe5cdb4b5b42818dea7ef8c9"},
142 {"6703df3701a7f54911ca72e24dca046a", "12823ab601c350ea4bc2488c",
143 "793cd125b0b84a043e3ac67717", "", "b2051c80014f42f08735a7b0cd",
144 "38e6bcd29962e5f2c13626b85a877101"},
145 {nullptr}};
146
147 const TestVector* const test_group_array[] = {
148 test_group_0, test_group_1, test_group_2,
149 test_group_3, test_group_4, test_group_5,
150 };
151
152 } // namespace
153
154 namespace net {
155 namespace test {
156
157 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing
158 // in an nonce and also to allocate the buffer needed for the ciphertext.
159 QuicData* EncryptWithNonce(Aes128Gcm12Encrypter* encrypter,
160 StringPiece nonce,
161 StringPiece associated_data,
162 StringPiece plaintext) {
163 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
164 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]);
165
166 if (!encrypter->Encrypt(nonce, associated_data, plaintext,
167 reinterpret_cast<unsigned char*>(ciphertext.get()))) {
168 return nullptr;
169 }
170
171 return new QuicData(ciphertext.release(), ciphertext_size, true);
172 }
173
174 TEST(Aes128Gcm12EncrypterTest, Encrypt) {
175 for (size_t i = 0; i < arraysize(test_group_array); i++) {
176 SCOPED_TRACE(i);
177 const TestVector* test_vectors = test_group_array[i];
178 const TestGroupInfo& test_info = test_group_info[i];
179 for (size_t j = 0; test_vectors[j].key != nullptr; j++) {
180 // Decode the test vector.
181 string key = QuicUtils::HexDecode(test_vectors[j].key);
182 string iv = QuicUtils::HexDecode(test_vectors[j].iv);
183 string pt = QuicUtils::HexDecode(test_vectors[j].pt);
184 string aad = QuicUtils::HexDecode(test_vectors[j].aad);
185 string ct = QuicUtils::HexDecode(test_vectors[j].ct);
186 string tag = QuicUtils::HexDecode(test_vectors[j].tag);
187
188 // The test vector's lengths should look sane. Note that the lengths
189 // in |test_info| are in bits.
190 EXPECT_EQ(test_info.key_len, key.length() * 8);
191 EXPECT_EQ(test_info.iv_len, iv.length() * 8);
192 EXPECT_EQ(test_info.pt_len, pt.length() * 8);
193 EXPECT_EQ(test_info.aad_len, aad.length() * 8);
194 EXPECT_EQ(test_info.pt_len, ct.length() * 8);
195 EXPECT_EQ(test_info.tag_len, tag.length() * 8);
196
197 Aes128Gcm12Encrypter encrypter;
198 ASSERT_TRUE(encrypter.SetKey(key));
199 std::unique_ptr<QuicData> encrypted(EncryptWithNonce(
200 &encrypter, iv,
201 // This deliberately tests that the encrypter can handle an AAD that
202 // is set to nullptr, as opposed to a zero-length, non-nullptr
203 // pointer.
204 aad.length() ? aad : StringPiece(), pt));
205 ASSERT_TRUE(encrypted.get());
206
207 // The test vectors have 16 byte authenticators but this code only uses
208 // the first 12.
209 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Encrypter::kAuthTagSize),
210 tag.length());
211 tag.resize(Aes128Gcm12Encrypter::kAuthTagSize);
212
213 ASSERT_EQ(ct.length() + tag.length(), encrypted->length());
214 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(),
215 ct.length(), ct.data(), ct.length());
216 test::CompareCharArraysWithHexError(
217 "authentication tag", encrypted->data() + ct.length(), tag.length(),
218 tag.data(), tag.length());
219 }
220 }
221 }
222
223 TEST(Aes128Gcm12EncrypterTest, GetMaxPlaintextSize) {
224 Aes128Gcm12Encrypter encrypter;
225 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012));
226 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112));
227 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22));
228 }
229
230 TEST(Aes128Gcm12EncrypterTest, GetCiphertextSize) {
231 Aes128Gcm12Encrypter encrypter;
232 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000));
233 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100));
234 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10));
235 }
236
237 } // namespace test
238 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/crypto/aes_128_gcm_12_encrypter.cc ('k') | net/quic/crypto/cert_compressor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698