forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_128_gcm_12_encrypter_test.cc
348 lines (315 loc) · 10.5 KB
/
aes_128_gcm_12_encrypter_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
#include "net/quic/test_tools/quic_test_utils.h"
using base::StringPiece;
namespace {
// The AES GCM test vectors come from the file gcmEncryptExtIV128.rsp
// downloaded from http://csrc.nist.gov/groups/STM/cavp/index.html on
// 2013-02-01. The test vectors in that file look like this:
//
// [Keylen = 128]
// [IVlen = 96]
// [PTlen = 0]
// [AADlen = 0]
// [Taglen = 128]
//
// Count = 0
// Key = 11754cd72aec309bf52f7687212e8957
// IV = 3c819d9a9bed087615030b65
// PT =
// AAD =
// CT =
// Tag = 250327c674aaf477aef2675748cf6971
//
// Count = 1
// Key = ca47248ac0b6f8372a97ac43508308ed
// IV = ffd2b598feabc9019262d2be
// PT =
// AAD =
// CT =
// Tag = 60d20404af527d248d893ae495707d1a
//
// ...
//
// The gcmEncryptExtIV128.rsp file is huge (2.8 MB), so I selected just a
// few test vectors for this unit test.
// Describes a group of test vectors that all have a given key length, IV
// length, plaintext length, AAD length, and tag length.
struct TestGroupInfo {
size_t key_len;
size_t iv_len;
size_t pt_len;
size_t aad_len;
size_t tag_len;
};
// Each test vector consists of six strings of lowercase hexadecimal digits.
// The strings may be empty (zero length). A test vector with a NULL |key|
// marks the end of an array of test vectors.
struct TestVector {
const char* key;
const char* iv;
const char* pt;
const char* aad;
const char* ct;
const char* tag;
};
const TestGroupInfo test_group_info[] = {
{ 128, 96, 0, 0, 128 },
{ 128, 96, 0, 128, 128 },
{ 128, 96, 128, 0, 128 },
{ 128, 96, 408, 160, 128 },
{ 128, 96, 408, 720, 128 },
{ 128, 96, 104, 0, 128 },
};
const TestVector test_group_0[] = {
{ "11754cd72aec309bf52f7687212e8957",
"3c819d9a9bed087615030b65",
"",
"",
"",
"250327c674aaf477aef2675748cf6971"
},
{ "ca47248ac0b6f8372a97ac43508308ed",
"ffd2b598feabc9019262d2be",
"",
"",
"",
"60d20404af527d248d893ae495707d1a"
},
{ NULL }
};
const TestVector test_group_1[] = {
{ "77be63708971c4e240d1cb79e8d77feb",
"e0e00f19fed7ba0136a797f3",
"",
"7a43ec1d9c0a5a78a0b16533a6213cab",
"",
"209fcc8d3675ed938e9c7166709dd946"
},
{ "7680c5d3ca6154758e510f4d25b98820",
"f8f105f9c3df4965780321f8",
"",
"c94c410194c765e3dcc7964379758ed3",
"",
"94dca8edfcf90bb74b153c8d48a17930"
},
{ NULL }
};
const TestVector test_group_2[] = {
{ "7fddb57453c241d03efbed3ac44e371c",
"ee283a3fc75575e33efd4887",
"d5de42b461646c255c87bd2962d3b9a2",
"",
"2ccda4a5415cb91e135c2a0f78c9b2fd",
"b36d1df9b9d5e596f83e8b7f52971cb3"
},
{ "ab72c77b97cb5fe9a382d9fe81ffdbed",
"54cc7dc2c37ec006bcc6d1da",
"007c5e5b3e59df24a7c355584fc1518d",
"",
"0e1bde206a07a9c2c1b65300f8c64997",
"2b4401346697138c7a4891ee59867d0c"
},
{ NULL }
};
const TestVector test_group_3[] = {
{ "fe47fcce5fc32665d2ae399e4eec72ba",
"5adb9609dbaeb58cbd6e7275",
"7c0e88c88899a779228465074797cd4c2e1498d259b54390b85e3eef1c02df60e743f1"
"b840382c4bccaf3bafb4ca8429bea063",
"88319d6e1d3ffa5f987199166c8a9b56c2aeba5a",
"98f4826f05a265e6dd2be82db241c0fbbbf9ffb1c173aa83964b7cf539304373636525"
"3ddbc5db8778371495da76d269e5db3e",
"291ef1982e4defedaa2249f898556b47"
},
{ "ec0c2ba17aa95cd6afffe949da9cc3a8",
"296bce5b50b7d66096d627ef",
"b85b3753535b825cbe5f632c0b843c741351f18aa484281aebec2f45bb9eea2d79d987"
"b764b9611f6c0f8641843d5d58f3a242",
"f8d00f05d22bf68599bcdeb131292ad6e2df5d14",
"a7443d31c26bdf2a1c945e29ee4bd344a99cfaf3aa71f8b3f191f83c2adfc7a0716299"
"5506fde6309ffc19e716eddf1a828c5a",
"890147971946b627c40016da1ecf3e77"
},
{ NULL }
};
const TestVector test_group_4[] = {
{ "2c1f21cf0f6fb3661943155c3e3d8492",
"23cb5ff362e22426984d1907",
"42f758836986954db44bf37c6ef5e4ac0adaf38f27252a1b82d02ea949c8a1a2dbc0d6"
"8b5615ba7c1220ff6510e259f06655d8",
"5d3624879d35e46849953e45a32a624d6a6c536ed9857c613b572b0333e701557a713e"
"3f010ecdf9a6bd6c9e3e44b065208645aff4aabee611b391528514170084ccf587177f"
"4488f33cfb5e979e42b6e1cfc0a60238982a7aec",
"81824f0e0d523db30d3da369fdc0d60894c7a0a20646dd015073ad2732bd989b14a222"
"b6ad57af43e1895df9dca2a5344a62cc",
"57a3ee28136e94c74838997ae9823f3a"
},
{ "d9f7d2411091f947b4d6f1e2d1f0fb2e",
"e1934f5db57cc983e6b180e7",
"73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490"
"c2c6f6166f4a59431e182663fcaea05a",
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d"
"0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a201"
"15d2e51398344b16bee1ed7c499b353d6c597af8",
"aaadbd5c92e9151ce3db7210b8714126b73e43436d242677afa50384f2149b831f1d57"
"3c7891c2a91fbc48db29967ec9542b23",
"21b51ca862cb637cdd03b99a0f93b134"
},
{ NULL }
};
const TestVector test_group_5[] = {
{ "fe9bb47deb3a61e423c2231841cfd1fb",
"4d328eb776f500a2f7fb47aa",
"f1cc3818e421876bb6b8bbd6c9",
"",
"b88c5c1977b35b517b0aeae967",
"43fd4727fe5cdb4b5b42818dea7ef8c9"
},
{ "6703df3701a7f54911ca72e24dca046a",
"12823ab601c350ea4bc2488c",
"793cd125b0b84a043e3ac67717",
"",
"b2051c80014f42f08735a7b0cd",
"38e6bcd29962e5f2c13626b85a877101"
},
{ NULL }
};
const TestVector* const test_group_array[] = {
test_group_0,
test_group_1,
test_group_2,
test_group_3,
test_group_4,
test_group_5,
};
// Returns true if |ch| is a lowercase hexadecimal digit.
bool IsHexDigit(char ch) {
return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f');
}
// Converts a lowercase hexadecimal digit to its integer value.
int HexDigitToInt(char ch) {
if ('0' <= ch && ch <= '9') {
return ch - '0';
}
return ch - 'a' + 10;
}
// |in| is a string consisting of lowercase hexadecimal digits, where
// every two digits represent one byte. |out| is a buffer of size |max_len|.
// Converts |in| to bytes and stores the bytes in the |out| buffer. The
// number of bytes converted is returned in |*out_len|. Returns true on
// success, false on failure.
bool DecodeHexString(const char* in,
char* out,
size_t* out_len,
size_t max_len) {
*out_len = 0;
while (*in != '\0') {
if (!IsHexDigit(*in) || !IsHexDigit(*(in + 1))) {
return false;
}
if (*out_len >= max_len) {
return false;
}
out[*out_len] = HexDigitToInt(*in) * 16 + HexDigitToInt(*(in + 1));
(*out_len)++;
in += 2;
}
return true;
}
} // namespace
namespace net {
namespace test {
// EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing
// in an nonce and also to allocate the buffer needed for the ciphertext.
QuicData* EncryptWithNonce(Aes128Gcm12Encrypter* encrypter,
StringPiece nonce,
StringPiece associated_data,
StringPiece plaintext) {
size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
scoped_ptr<char[]> ciphertext(new char[ciphertext_size]);
if (!encrypter->Encrypt(nonce, associated_data, plaintext,
reinterpret_cast<unsigned char*>(ciphertext.get()))) {
return NULL;
}
return new QuicData(ciphertext.release(), ciphertext_size, true);
}
TEST(Aes128Gcm12EncrypterTest, Encrypt) {
if (!Aes128Gcm12Encrypter::IsSupported()) {
LOG(INFO) << "AES GCM not supported. Test skipped.";
return;
}
char key[1024];
size_t key_len;
char iv[1024];
size_t iv_len;
char pt[1024];
size_t pt_len;
char aad[1024];
size_t aad_len;
char ct[1024];
size_t ct_len;
char tag[1024];
size_t tag_len;
for (size_t i = 0; i < arraysize(test_group_array); i++) {
SCOPED_TRACE(i);
const TestVector* test_vector = test_group_array[i];
const TestGroupInfo& test_info = test_group_info[i];
for (size_t j = 0; test_vector[j].key != NULL; j++) {
// Decode the test vector.
ASSERT_TRUE(
DecodeHexString(test_vector[j].key, key, &key_len, sizeof(key)));
ASSERT_TRUE(DecodeHexString(test_vector[j].iv, iv, &iv_len, sizeof(iv)));
ASSERT_TRUE(DecodeHexString(test_vector[j].pt, pt, &pt_len, sizeof(pt)));
ASSERT_TRUE(
DecodeHexString(test_vector[j].aad, aad, &aad_len, sizeof(aad)));
ASSERT_TRUE(DecodeHexString(test_vector[j].ct, ct, &ct_len, sizeof(ct)));
ASSERT_TRUE(
DecodeHexString(test_vector[j].tag, tag, &tag_len, sizeof(tag)));
// The test vector's lengths should look sane. Note that the lengths
// in |test_info| are in bits.
EXPECT_EQ(test_info.key_len, key_len * 8);
EXPECT_EQ(test_info.iv_len, iv_len * 8);
EXPECT_EQ(test_info.pt_len, pt_len * 8);
EXPECT_EQ(test_info.aad_len, aad_len * 8);
EXPECT_EQ(test_info.pt_len, ct_len * 8);
EXPECT_EQ(test_info.tag_len, tag_len * 8);
Aes128Gcm12Encrypter encrypter;
ASSERT_TRUE(encrypter.SetKey(StringPiece(key, key_len)));
scoped_ptr<QuicData> encrypted(EncryptWithNonce(
&encrypter, StringPiece(iv, iv_len),
// OpenSSL fails if NULL is set as the AAD, as opposed to a
// zero-length, non-NULL pointer. This deliberately tests that we
// handle this case.
StringPiece(aad_len ? aad : NULL, aad_len), StringPiece(pt, pt_len)));
ASSERT_TRUE(encrypted.get());
// The test vectors have 16 byte authenticators but this code only uses
// the first 12.
ASSERT_LE(static_cast<size_t>(Aes128Gcm12Encrypter::kAuthTagSize),
tag_len);
tag_len = Aes128Gcm12Encrypter::kAuthTagSize;
ASSERT_EQ(ct_len + tag_len, encrypted->length());
test::CompareCharArraysWithHexError("ciphertext", encrypted->data(),
ct_len, ct, ct_len);
test::CompareCharArraysWithHexError(
"authentication tag", encrypted->data() + ct_len, tag_len, tag,
tag_len);
}
}
}
TEST(Aes128Gcm12EncrypterTest, GetMaxPlaintextSize) {
Aes128Gcm12Encrypter encrypter;
EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012));
EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112));
EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22));
}
TEST(Aes128Gcm12EncrypterTest, GetCiphertextSize) {
Aes128Gcm12Encrypter encrypter;
EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000));
EXPECT_EQ(112u, encrypter.GetCiphertextSize(100));
EXPECT_EQ(22u, encrypter.GetCiphertextSize(10));
}
} // namespace test
} // namespace net