| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/core/crypto/chacha20_poly1305_decrypter.h" | 5 #include "net/quic/core/crypto/chacha20_poly1305_decrypter.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "net/quic/core/quic_utils.h" | 9 #include "net/quic/core/quic_utils.h" |
| 10 #include "net/quic/platform/api/quic_text_utils.h" |
| 10 #include "net/quic/test_tools/quic_test_utils.h" | 11 #include "net/quic/test_tools/quic_test_utils.h" |
| 11 | 12 |
| 12 using base::StringPiece; | 13 using base::StringPiece; |
| 13 using std::string; | 14 using std::string; |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // The test vectors come from RFC 7539 Section 2.8.2. | 18 // The test vectors come from RFC 7539 Section 2.8.2. |
| 18 | 19 |
| 19 // Each test vector consists of six strings of lowercase hexadecimal digits. | 20 // Each test vector consists of six strings of lowercase hexadecimal digits. |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 136 } |
| 136 return new QuicData(output.release(), output_length, true); | 137 return new QuicData(output.release(), output_length, true); |
| 137 } | 138 } |
| 138 | 139 |
| 139 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { | 140 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { |
| 140 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 141 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 141 // If not present then decryption is expected to fail. | 142 // If not present then decryption is expected to fail. |
| 142 bool has_pt = test_vectors[i].pt; | 143 bool has_pt = test_vectors[i].pt; |
| 143 | 144 |
| 144 // Decode the test vector. | 145 // Decode the test vector. |
| 145 string key = QuicUtils::HexDecode(test_vectors[i].key); | 146 string key = QuicTextUtils::HexDecode(test_vectors[i].key); |
| 146 string iv = QuicUtils::HexDecode(test_vectors[i].iv); | 147 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); |
| 147 string fixed = QuicUtils::HexDecode(test_vectors[i].fixed); | 148 string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed); |
| 148 string aad = QuicUtils::HexDecode(test_vectors[i].aad); | 149 string aad = QuicTextUtils::HexDecode(test_vectors[i].aad); |
| 149 string ct = QuicUtils::HexDecode(test_vectors[i].ct); | 150 string ct = QuicTextUtils::HexDecode(test_vectors[i].ct); |
| 150 string pt; | 151 string pt; |
| 151 if (has_pt) { | 152 if (has_pt) { |
| 152 pt = QuicUtils::HexDecode(test_vectors[i].pt); | 153 pt = QuicTextUtils::HexDecode(test_vectors[i].pt); |
| 153 } | 154 } |
| 154 | 155 |
| 155 ChaCha20Poly1305Decrypter decrypter; | 156 ChaCha20Poly1305Decrypter decrypter; |
| 156 ASSERT_TRUE(decrypter.SetKey(key)); | 157 ASSERT_TRUE(decrypter.SetKey(key)); |
| 157 std::unique_ptr<QuicData> decrypted(DecryptWithNonce( | 158 std::unique_ptr<QuicData> decrypted(DecryptWithNonce( |
| 158 &decrypter, fixed + iv, | 159 &decrypter, fixed + iv, |
| 159 // This deliberately tests that the decrypter can handle an AAD that | 160 // This deliberately tests that the decrypter can handle an AAD that |
| 160 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | 161 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
| 161 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); | 162 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); |
| 162 if (!decrypted.get()) { | 163 if (!decrypted.get()) { |
| 163 EXPECT_FALSE(has_pt); | 164 EXPECT_FALSE(has_pt); |
| 164 continue; | 165 continue; |
| 165 } | 166 } |
| 166 EXPECT_TRUE(has_pt); | 167 EXPECT_TRUE(has_pt); |
| 167 | 168 |
| 168 EXPECT_EQ(12u, ct.size() - decrypted->length()); | 169 EXPECT_EQ(12u, ct.size() - decrypted->length()); |
| 169 ASSERT_EQ(pt.length(), decrypted->length()); | 170 ASSERT_EQ(pt.length(), decrypted->length()); |
| 170 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 171 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 171 pt.length(), pt.data(), pt.length()); | 172 pt.length(), pt.data(), pt.length()); |
| 172 } | 173 } |
| 173 } | 174 } |
| 174 | 175 |
| 175 } // namespace test | 176 } // namespace test |
| 176 } // namespace net | 177 } // namespace net |
| OLD | NEW |