| 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_12_decrypter.h" | 5 #include "net/quic/crypto/aes_128_gcm_12_decrypter.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "net/quic/test_tools/quic_test_utils.h" | 9 #include "net/quic/test_tools/quic_test_utils.h" |
| 8 | 10 |
| 9 using base::StringPiece; | 11 using base::StringPiece; |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 // The AES GCM test vectors come from the file gcmDecrypt128.rsp | 15 // The AES GCM test vectors come from the file gcmDecrypt128.rsp |
| 14 // downloaded from http://csrc.nist.gov/groups/STM/cavp/index.html on | 16 // downloaded from http://csrc.nist.gov/groups/STM/cavp/index.html on |
| 15 // 2013-02-01. The test vectors in that file look like this: | 17 // 2013-02-01. The test vectors in that file look like this: |
| 16 // | 18 // |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 TEST(Aes128Gcm12DecrypterTest, Decrypt) { | 272 TEST(Aes128Gcm12DecrypterTest, Decrypt) { |
| 271 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 273 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
| 272 SCOPED_TRACE(i); | 274 SCOPED_TRACE(i); |
| 273 const TestVector* test_vectors = test_group_array[i]; | 275 const TestVector* test_vectors = test_group_array[i]; |
| 274 const TestGroupInfo& test_info = test_group_info[i]; | 276 const TestGroupInfo& test_info = test_group_info[i]; |
| 275 for (size_t j = 0; test_vectors[j].key != nullptr; j++) { | 277 for (size_t j = 0; test_vectors[j].key != nullptr; j++) { |
| 276 // If not present then decryption is expected to fail. | 278 // If not present then decryption is expected to fail. |
| 277 bool has_pt = test_vectors[j].pt; | 279 bool has_pt = test_vectors[j].pt; |
| 278 | 280 |
| 279 // Decode the test vector. | 281 // Decode the test vector. |
| 280 string key; | 282 std::string key; |
| 281 string iv; | 283 std::string iv; |
| 282 string ct; | 284 std::string ct; |
| 283 string aad; | 285 std::string aad; |
| 284 string tag; | 286 std::string tag; |
| 285 string pt; | 287 std::string pt; |
| 286 ASSERT_TRUE(DecodeHexString(test_vectors[j].key, &key)); | 288 ASSERT_TRUE(DecodeHexString(test_vectors[j].key, &key)); |
| 287 ASSERT_TRUE(DecodeHexString(test_vectors[j].iv, &iv)); | 289 ASSERT_TRUE(DecodeHexString(test_vectors[j].iv, &iv)); |
| 288 ASSERT_TRUE(DecodeHexString(test_vectors[j].ct, &ct)); | 290 ASSERT_TRUE(DecodeHexString(test_vectors[j].ct, &ct)); |
| 289 ASSERT_TRUE(DecodeHexString(test_vectors[j].aad, &aad)); | 291 ASSERT_TRUE(DecodeHexString(test_vectors[j].aad, &aad)); |
| 290 ASSERT_TRUE(DecodeHexString(test_vectors[j].tag, &tag)); | 292 ASSERT_TRUE(DecodeHexString(test_vectors[j].tag, &tag)); |
| 291 if (has_pt) { | 293 if (has_pt) { |
| 292 ASSERT_TRUE(DecodeHexString(test_vectors[j].pt, &pt)); | 294 ASSERT_TRUE(DecodeHexString(test_vectors[j].pt, &pt)); |
| 293 } | 295 } |
| 294 | 296 |
| 295 // The test vector's lengths should look sane. Note that the lengths | 297 // The test vector's lengths should look sane. Note that the lengths |
| 296 // in |test_info| are in bits. | 298 // in |test_info| are in bits. |
| 297 EXPECT_EQ(test_info.key_len, key.length() * 8); | 299 EXPECT_EQ(test_info.key_len, key.length() * 8); |
| 298 EXPECT_EQ(test_info.iv_len, iv.length() * 8); | 300 EXPECT_EQ(test_info.iv_len, iv.length() * 8); |
| 299 EXPECT_EQ(test_info.pt_len, ct.length() * 8); | 301 EXPECT_EQ(test_info.pt_len, ct.length() * 8); |
| 300 EXPECT_EQ(test_info.aad_len, aad.length() * 8); | 302 EXPECT_EQ(test_info.aad_len, aad.length() * 8); |
| 301 EXPECT_EQ(test_info.tag_len, tag.length() * 8); | 303 EXPECT_EQ(test_info.tag_len, tag.length() * 8); |
| 302 if (has_pt) { | 304 if (has_pt) { |
| 303 EXPECT_EQ(test_info.pt_len, pt.length() * 8); | 305 EXPECT_EQ(test_info.pt_len, pt.length() * 8); |
| 304 } | 306 } |
| 305 | 307 |
| 306 // The test vectors have 16 byte authenticators but this code only uses | 308 // The test vectors have 16 byte authenticators but this code only uses |
| 307 // the first 12. | 309 // the first 12. |
| 308 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Decrypter::kAuthTagSize), | 310 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Decrypter::kAuthTagSize), |
| 309 tag.length()); | 311 tag.length()); |
| 310 tag.resize(Aes128Gcm12Decrypter::kAuthTagSize); | 312 tag.resize(Aes128Gcm12Decrypter::kAuthTagSize); |
| 311 string ciphertext = ct + tag; | 313 std::string ciphertext = ct + tag; |
| 312 | 314 |
| 313 Aes128Gcm12Decrypter decrypter; | 315 Aes128Gcm12Decrypter decrypter; |
| 314 ASSERT_TRUE(decrypter.SetKey(key)); | 316 ASSERT_TRUE(decrypter.SetKey(key)); |
| 315 | 317 |
| 316 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 318 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
| 317 &decrypter, iv, | 319 &decrypter, iv, |
| 318 // This deliberately tests that the decrypter can handle an AAD that | 320 // This deliberately tests that the decrypter can handle an AAD that |
| 319 // is set to nullptr, as opposed to a zero-length, non-nullptr | 321 // is set to nullptr, as opposed to a zero-length, non-nullptr |
| 320 // pointer. | 322 // pointer. |
| 321 aad.length() ? aad : StringPiece(), ciphertext)); | 323 aad.length() ? aad : StringPiece(), ciphertext)); |
| 322 if (!decrypted.get()) { | 324 if (!decrypted.get()) { |
| 323 EXPECT_FALSE(has_pt); | 325 EXPECT_FALSE(has_pt); |
| 324 continue; | 326 continue; |
| 325 } | 327 } |
| 326 EXPECT_TRUE(has_pt); | 328 EXPECT_TRUE(has_pt); |
| 327 | 329 |
| 328 ASSERT_EQ(pt.length(), decrypted->length()); | 330 ASSERT_EQ(pt.length(), decrypted->length()); |
| 329 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 331 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 330 pt.length(), pt.data(), pt.length()); | 332 pt.length(), pt.data(), pt.length()); |
| 331 } | 333 } |
| 332 } | 334 } |
| 333 } | 335 } |
| 334 | 336 |
| 335 } // namespace test | 337 } // namespace test |
| 336 } // namespace net | 338 } // namespace net |
| OLD | NEW |