OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "net/quic/crypto/aes_128_gcm_encrypter.h" | 5 #include "net/quic/crypto/aes_128_gcm_encrypter.h" |
6 | 6 |
7 #include "net/quic/test_tools/quic_test_utils.h" | 7 #include "net/quic/test_tools/quic_test_utils.h" |
8 | 8 |
9 using base::StringPiece; | 9 using base::StringPiece; |
10 | 10 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 size_t ct_len; | 278 size_t ct_len; |
279 char tag[1024]; | 279 char tag[1024]; |
280 size_t tag_len; | 280 size_t tag_len; |
281 | 281 |
282 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 282 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
283 SCOPED_TRACE(i); | 283 SCOPED_TRACE(i); |
284 const TestVector* test_vector = test_group_array[i]; | 284 const TestVector* test_vector = test_group_array[i]; |
285 const TestGroupInfo& test_info = test_group_info[i]; | 285 const TestGroupInfo& test_info = test_group_info[i]; |
286 for (size_t j = 0; test_vector[j].key != NULL; j++) { | 286 for (size_t j = 0; test_vector[j].key != NULL; j++) { |
287 // Decode the test vector. | 287 // Decode the test vector. |
288 ASSERT_TRUE(DecodeHexString(test_vector[j].key, key, &key_len, | 288 ASSERT_TRUE( |
289 sizeof(key))); | 289 DecodeHexString(test_vector[j].key, key, &key_len, sizeof(key))); |
290 ASSERT_TRUE(DecodeHexString(test_vector[j].iv, iv, &iv_len, | 290 ASSERT_TRUE(DecodeHexString(test_vector[j].iv, iv, &iv_len, sizeof(iv))); |
291 sizeof(iv))); | 291 ASSERT_TRUE(DecodeHexString(test_vector[j].pt, pt, &pt_len, sizeof(pt))); |
292 ASSERT_TRUE(DecodeHexString(test_vector[j].pt, pt, &pt_len, | 292 ASSERT_TRUE( |
293 sizeof(pt))); | 293 DecodeHexString(test_vector[j].aad, aad, &aad_len, sizeof(aad))); |
294 ASSERT_TRUE(DecodeHexString(test_vector[j].aad, aad, &aad_len, | 294 ASSERT_TRUE(DecodeHexString(test_vector[j].ct, ct, &ct_len, sizeof(ct))); |
295 sizeof(aad))); | 295 ASSERT_TRUE( |
296 ASSERT_TRUE(DecodeHexString(test_vector[j].ct, ct, &ct_len, | 296 DecodeHexString(test_vector[j].tag, tag, &tag_len, sizeof(tag))); |
297 sizeof(ct))); | |
298 ASSERT_TRUE(DecodeHexString(test_vector[j].tag, tag, &tag_len, | |
299 sizeof(tag))); | |
300 | 297 |
301 // The test vector's lengths should look sane. Note that the lengths | 298 // The test vector's lengths should look sane. Note that the lengths |
302 // in |test_info| are in bits. | 299 // in |test_info| are in bits. |
303 EXPECT_EQ(test_info.key_len, key_len * 8); | 300 EXPECT_EQ(test_info.key_len, key_len * 8); |
304 EXPECT_EQ(test_info.iv_len, iv_len * 8); | 301 EXPECT_EQ(test_info.iv_len, iv_len * 8); |
305 EXPECT_EQ(test_info.pt_len, pt_len * 8); | 302 EXPECT_EQ(test_info.pt_len, pt_len * 8); |
306 EXPECT_EQ(test_info.aad_len, aad_len * 8); | 303 EXPECT_EQ(test_info.aad_len, aad_len * 8); |
307 EXPECT_EQ(test_info.pt_len, ct_len * 8); | 304 EXPECT_EQ(test_info.pt_len, ct_len * 8); |
308 EXPECT_EQ(test_info.tag_len, tag_len * 8); | 305 EXPECT_EQ(test_info.tag_len, tag_len * 8); |
309 | 306 |
310 Aes128GcmEncrypter encrypter; | 307 Aes128GcmEncrypter encrypter; |
311 ASSERT_TRUE(encrypter.SetKey(StringPiece(key, key_len))); | 308 ASSERT_TRUE(encrypter.SetKey(StringPiece(key, key_len))); |
312 scoped_ptr<QuicData> encrypted(EncryptWithNonce( | 309 scoped_ptr<QuicData> encrypted(EncryptWithNonce( |
313 &encrypter, StringPiece(iv, iv_len), | 310 &encrypter, StringPiece(iv, iv_len), |
314 // OpenSSL fails if NULL is set as the AAD, as opposed to a | 311 // OpenSSL fails if NULL is set as the AAD, as opposed to a |
315 // zero-length, non-NULL pointer. This deliberately tests that we | 312 // zero-length, non-NULL pointer. This deliberately tests that we |
316 // handle this case. | 313 // handle this case. |
317 StringPiece(aad_len ? aad : NULL, aad_len), | 314 StringPiece(aad_len ? aad : NULL, aad_len), StringPiece(pt, pt_len))); |
318 StringPiece(pt, pt_len))); | |
319 ASSERT_TRUE(encrypted.get()); | 315 ASSERT_TRUE(encrypted.get()); |
320 ASSERT_EQ(ct_len + tag_len, encrypted->length()); | 316 ASSERT_EQ(ct_len + tag_len, encrypted->length()); |
| 317 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), |
| 318 ct_len, ct, ct_len); |
321 test::CompareCharArraysWithHexError( | 319 test::CompareCharArraysWithHexError( |
322 "ciphertext", encrypted->data(), ct_len, | 320 "authentication tag", encrypted->data() + ct_len, tag_len, tag, |
323 ct, ct_len); | 321 tag_len); |
324 test::CompareCharArraysWithHexError( | |
325 "authentication tag", encrypted->data() + ct_len, tag_len, | |
326 tag, tag_len); | |
327 } | 322 } |
328 } | 323 } |
329 } | 324 } |
330 | 325 |
331 TEST(Aes128GcmEncrypterTest, GetMaxPlaintextSize) { | 326 TEST(Aes128GcmEncrypterTest, GetMaxPlaintextSize) { |
332 Aes128GcmEncrypter encrypter; | 327 Aes128GcmEncrypter encrypter; |
333 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1016)); | 328 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1016)); |
334 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(116)); | 329 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(116)); |
335 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(26)); | 330 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(26)); |
336 } | 331 } |
337 | 332 |
338 TEST(Aes128GcmEncrypterTest, GetCiphertextSize) { | 333 TEST(Aes128GcmEncrypterTest, GetCiphertextSize) { |
339 Aes128GcmEncrypter encrypter; | 334 Aes128GcmEncrypter encrypter; |
340 EXPECT_EQ(1016u, encrypter.GetCiphertextSize(1000)); | 335 EXPECT_EQ(1016u, encrypter.GetCiphertextSize(1000)); |
341 EXPECT_EQ(116u, encrypter.GetCiphertextSize(100)); | 336 EXPECT_EQ(116u, encrypter.GetCiphertextSize(100)); |
342 EXPECT_EQ(26u, encrypter.GetCiphertextSize(10)); | 337 EXPECT_EQ(26u, encrypter.GetCiphertextSize(10)); |
343 } | 338 } |
344 | 339 |
345 } // namespace test | 340 } // namespace test |
346 } // namespace net | 341 } // namespace net |
OLD | NEW |