| 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 "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 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 QuicData* DecryptWithNonce(Aes128Gcm12Decrypter* decrypter, | 255 QuicData* DecryptWithNonce(Aes128Gcm12Decrypter* decrypter, |
| 256 StringPiece nonce, | 256 StringPiece nonce, |
| 257 StringPiece associated_data, | 257 StringPiece associated_data, |
| 258 StringPiece ciphertext) { | 258 StringPiece ciphertext) { |
| 259 size_t plaintext_size = ciphertext.length(); | 259 size_t plaintext_size = ciphertext.length(); |
| 260 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 260 scoped_ptr<char[]> plaintext(new char[plaintext_size]); |
| 261 | 261 |
| 262 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, | 262 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, |
| 263 reinterpret_cast<unsigned char*>(plaintext.get()), | 263 reinterpret_cast<unsigned char*>(plaintext.get()), |
| 264 &plaintext_size)) { | 264 &plaintext_size)) { |
| 265 return NULL; | 265 return nullptr; |
| 266 } | 266 } |
| 267 return new QuicData(plaintext.release(), plaintext_size, true); | 267 return new QuicData(plaintext.release(), plaintext_size, true); |
| 268 } | 268 } |
| 269 | 269 |
| 270 TEST(Aes128Gcm12DecrypterTest, Decrypt) { | 270 TEST(Aes128Gcm12DecrypterTest, Decrypt) { |
| 271 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 271 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
| 272 SCOPED_TRACE(i); | 272 SCOPED_TRACE(i); |
| 273 const TestVector* test_vectors = test_group_array[i]; | 273 const TestVector* test_vectors = test_group_array[i]; |
| 274 const TestGroupInfo& test_info = test_group_info[i]; | 274 const TestGroupInfo& test_info = test_group_info[i]; |
| 275 for (size_t j = 0; test_vectors[j].key != NULL; j++) { | 275 for (size_t j = 0; test_vectors[j].key != nullptr; j++) { |
| 276 // If not present then decryption is expected to fail. | 276 // If not present then decryption is expected to fail. |
| 277 bool has_pt = test_vectors[j].pt; | 277 bool has_pt = test_vectors[j].pt; |
| 278 | 278 |
| 279 // Decode the test vector. | 279 // Decode the test vector. |
| 280 string key; | 280 string key; |
| 281 string iv; | 281 string iv; |
| 282 string ct; | 282 string ct; |
| 283 string aad; | 283 string aad; |
| 284 string tag; | 284 string tag; |
| 285 string pt; | 285 string pt; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 309 tag.length()); | 309 tag.length()); |
| 310 tag.resize(Aes128Gcm12Decrypter::kAuthTagSize); | 310 tag.resize(Aes128Gcm12Decrypter::kAuthTagSize); |
| 311 string ciphertext = ct + tag; | 311 string ciphertext = ct + tag; |
| 312 | 312 |
| 313 Aes128Gcm12Decrypter decrypter; | 313 Aes128Gcm12Decrypter decrypter; |
| 314 ASSERT_TRUE(decrypter.SetKey(key)); | 314 ASSERT_TRUE(decrypter.SetKey(key)); |
| 315 | 315 |
| 316 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 316 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
| 317 &decrypter, iv, | 317 &decrypter, iv, |
| 318 // This deliberately tests that the decrypter can handle an AAD that | 318 // This deliberately tests that the decrypter can handle an AAD that |
| 319 // is set to NULL, as opposed to a zero-length, non-NULL pointer. | 319 // is set to nullptr, as opposed to a zero-length, non-nullptr |
| 320 // pointer. |
| 320 aad.length() ? aad : StringPiece(), ciphertext)); | 321 aad.length() ? aad : StringPiece(), ciphertext)); |
| 321 if (!decrypted.get()) { | 322 if (!decrypted.get()) { |
| 322 EXPECT_FALSE(has_pt); | 323 EXPECT_FALSE(has_pt); |
| 323 continue; | 324 continue; |
| 324 } | 325 } |
| 325 EXPECT_TRUE(has_pt); | 326 EXPECT_TRUE(has_pt); |
| 326 | 327 |
| 327 ASSERT_EQ(pt.length(), decrypted->length()); | 328 ASSERT_EQ(pt.length(), decrypted->length()); |
| 328 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 329 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 329 pt.length(), pt.data(), pt.length()); | 330 pt.length(), pt.data(), pt.length()); |
| 330 } | 331 } |
| 331 } | 332 } |
| 332 } | 333 } |
| 333 | 334 |
| 334 } // namespace test | 335 } // namespace test |
| 335 } // namespace net | 336 } // namespace net |
| OLD | NEW |