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_decrypter.h" | 5 #include "net/quic/crypto/aes_128_gcm_decrypter.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 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 size_t tag_len; | 326 size_t tag_len; |
327 char pt[1024]; | 327 char pt[1024]; |
328 size_t pt_len; | 328 size_t pt_len; |
329 | 329 |
330 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 330 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
331 SCOPED_TRACE(i); | 331 SCOPED_TRACE(i); |
332 const TestVector* test_vector = test_group_array[i]; | 332 const TestVector* test_vector = test_group_array[i]; |
333 const TestGroupInfo& test_info = test_group_info[i]; | 333 const TestGroupInfo& test_info = test_group_info[i]; |
334 for (size_t j = 0; test_vector[j].key != NULL; j++) { | 334 for (size_t j = 0; test_vector[j].key != NULL; j++) { |
335 // Decode the test vector. | 335 // Decode the test vector. |
336 ASSERT_TRUE(DecodeHexString(test_vector[j].key, key, &key_len, | 336 ASSERT_TRUE( |
337 sizeof(key))); | 337 DecodeHexString(test_vector[j].key, key, &key_len, sizeof(key))); |
338 ASSERT_TRUE(DecodeHexString(test_vector[j].iv, iv, &iv_len, | 338 ASSERT_TRUE(DecodeHexString(test_vector[j].iv, iv, &iv_len, sizeof(iv))); |
339 sizeof(iv))); | 339 ASSERT_TRUE(DecodeHexString(test_vector[j].ct, ct, &ct_len, sizeof(ct))); |
340 ASSERT_TRUE(DecodeHexString(test_vector[j].ct, ct, &ct_len, | 340 ASSERT_TRUE( |
341 sizeof(ct))); | 341 DecodeHexString(test_vector[j].aad, aad, &aad_len, sizeof(aad))); |
342 ASSERT_TRUE(DecodeHexString(test_vector[j].aad, aad, &aad_len, | 342 ASSERT_TRUE( |
343 sizeof(aad))); | 343 DecodeHexString(test_vector[j].tag, tag, &tag_len, sizeof(tag))); |
344 ASSERT_TRUE(DecodeHexString(test_vector[j].tag, tag, &tag_len, | 344 ASSERT_TRUE(DecodeHexString(test_vector[j].pt, pt, &pt_len, sizeof(pt))); |
345 sizeof(tag))); | |
346 ASSERT_TRUE(DecodeHexString(test_vector[j].pt, pt, &pt_len, | |
347 sizeof(pt))); | |
348 | 345 |
349 // The test vector's lengths should look sane. Note that the lengths | 346 // The test vector's lengths should look sane. Note that the lengths |
350 // in |test_info| are in bits. | 347 // in |test_info| are in bits. |
351 EXPECT_EQ(test_info.key_len, key_len * 8); | 348 EXPECT_EQ(test_info.key_len, key_len * 8); |
352 EXPECT_EQ(test_info.iv_len, iv_len * 8); | 349 EXPECT_EQ(test_info.iv_len, iv_len * 8); |
353 EXPECT_EQ(test_info.pt_len, ct_len * 8); | 350 EXPECT_EQ(test_info.pt_len, ct_len * 8); |
354 EXPECT_EQ(test_info.aad_len, aad_len * 8); | 351 EXPECT_EQ(test_info.aad_len, aad_len * 8); |
355 EXPECT_EQ(test_info.tag_len, tag_len * 8); | 352 EXPECT_EQ(test_info.tag_len, tag_len * 8); |
356 if (pt_len != static_cast<size_t>(-1)) { | 353 if (pt_len != static_cast<size_t>(-1)) { |
357 EXPECT_EQ(test_info.pt_len, pt_len * 8); | 354 EXPECT_EQ(test_info.pt_len, pt_len * 8); |
358 } | 355 } |
359 | 356 |
360 Aes128GcmDecrypter decrypter; | 357 Aes128GcmDecrypter decrypter; |
361 ASSERT_TRUE(decrypter.SetKey(StringPiece(key, key_len))); | 358 ASSERT_TRUE(decrypter.SetKey(StringPiece(key, key_len))); |
362 string ciphertext(ct, ct_len); | 359 string ciphertext(ct, ct_len); |
363 ciphertext.append(tag, tag_len); | 360 ciphertext.append(tag, tag_len); |
364 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 361 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
365 &decrypter, StringPiece(iv, iv_len), | 362 &decrypter, StringPiece(iv, iv_len), |
366 // OpenSSL fails if NULL is set as the AAD, as opposed to a | 363 // OpenSSL fails if NULL is set as the AAD, as opposed to a |
367 // zero-length, non-NULL pointer. | 364 // zero-length, non-NULL pointer. |
368 StringPiece(aad_len ? aad : NULL, aad_len), | 365 StringPiece(aad_len ? aad : NULL, aad_len), ciphertext)); |
369 ciphertext)); | |
370 if (!decrypted.get()) { | 366 if (!decrypted.get()) { |
371 EXPECT_EQ((size_t)-1, pt_len); | 367 EXPECT_EQ((size_t)-1, pt_len); |
372 continue; | 368 continue; |
373 } | 369 } |
374 ASSERT_NE((size_t)-1, pt_len); | 370 ASSERT_NE((size_t)-1, pt_len); |
375 | 371 |
376 ASSERT_EQ(pt_len, decrypted->length()); | 372 ASSERT_EQ(pt_len, decrypted->length()); |
377 test::CompareCharArraysWithHexError( | 373 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
378 "plaintext", decrypted->data(), pt_len, | 374 pt_len, pt, pt_len); |
379 pt, pt_len); | |
380 } | 375 } |
381 } | 376 } |
382 } | 377 } |
383 | 378 |
384 } // namespace test | 379 } // namespace test |
385 } // namespace net | 380 } // namespace net |
OLD | NEW |