| 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/platform/api/quic_text_utils.h" |
| 11 #include "net/quic/test_tools/quic_test_utils.h" | 11 #include "net/quic/test_tools/quic_test_utils.h" |
| 12 | 12 |
| 13 using base::StringPiece; | |
| 14 using std::string; | 13 using std::string; |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 // The test vectors come from RFC 7539 Section 2.8.2. | 17 // The test vectors come from RFC 7539 Section 2.8.2. |
| 19 | 18 |
| 20 // Each test vector consists of six strings of lowercase hexadecimal digits. | 19 // Each test vector consists of six strings of lowercase hexadecimal digits. |
| 21 // The strings may be empty (zero length). A test vector with a nullptr |key| | 20 // The strings may be empty (zero length). A test vector with a nullptr |key| |
| 22 // marks the end of an array of test vectors. | 21 // marks the end of an array of test vectors. |
| 23 struct TestVector { | 22 struct TestVector { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 {nullptr}}; | 106 {nullptr}}; |
| 108 | 107 |
| 109 } // namespace | 108 } // namespace |
| 110 | 109 |
| 111 namespace net { | 110 namespace net { |
| 112 namespace test { | 111 namespace test { |
| 113 | 112 |
| 114 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing | 113 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
| 115 // in an nonce and also to allocate the buffer needed for the plaintext. | 114 // in an nonce and also to allocate the buffer needed for the plaintext. |
| 116 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, | 115 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
| 117 StringPiece nonce, | 116 QuicStringPiece nonce, |
| 118 StringPiece associated_data, | 117 QuicStringPiece associated_data, |
| 119 StringPiece ciphertext) { | 118 QuicStringPiece ciphertext) { |
| 120 QuicPacketNumber packet_number; | 119 QuicPacketNumber packet_number; |
| 121 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number)); | 120 QuicStringPiece nonce_prefix(nonce.data(), |
| 121 nonce.size() - sizeof(packet_number)); |
| 122 decrypter->SetNoncePrefix(nonce_prefix); | 122 decrypter->SetNoncePrefix(nonce_prefix); |
| 123 memcpy(&packet_number, nonce.data() + nonce_prefix.size(), | 123 memcpy(&packet_number, nonce.data() + nonce_prefix.size(), |
| 124 sizeof(packet_number)); | 124 sizeof(packet_number)); |
| 125 std::unique_ptr<char[]> output(new char[ciphertext.length()]); | 125 std::unique_ptr<char[]> output(new char[ciphertext.length()]); |
| 126 size_t output_length = 0; | 126 size_t output_length = 0; |
| 127 const bool success = decrypter->DecryptPacket( | 127 const bool success = decrypter->DecryptPacket( |
| 128 QuicVersionMax(), packet_number, associated_data, ciphertext, | 128 QuicVersionMax(), packet_number, associated_data, ciphertext, |
| 129 output.get(), &output_length, ciphertext.length()); | 129 output.get(), &output_length, ciphertext.length()); |
| 130 if (!success) { | 130 if (!success) { |
| 131 return nullptr; | 131 return nullptr; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 148 if (has_pt) { | 148 if (has_pt) { |
| 149 pt = QuicTextUtils::HexDecode(test_vectors[i].pt); | 149 pt = QuicTextUtils::HexDecode(test_vectors[i].pt); |
| 150 } | 150 } |
| 151 | 151 |
| 152 ChaCha20Poly1305Decrypter decrypter; | 152 ChaCha20Poly1305Decrypter decrypter; |
| 153 ASSERT_TRUE(decrypter.SetKey(key)); | 153 ASSERT_TRUE(decrypter.SetKey(key)); |
| 154 std::unique_ptr<QuicData> decrypted(DecryptWithNonce( | 154 std::unique_ptr<QuicData> decrypted(DecryptWithNonce( |
| 155 &decrypter, fixed + iv, | 155 &decrypter, fixed + iv, |
| 156 // This deliberately tests that the decrypter can handle an AAD that | 156 // This deliberately tests that the decrypter can handle an AAD that |
| 157 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | 157 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
| 158 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); | 158 QuicStringPiece(aad.length() ? aad.data() : nullptr, aad.length()), |
| 159 ct)); |
| 159 if (!decrypted.get()) { | 160 if (!decrypted.get()) { |
| 160 EXPECT_FALSE(has_pt); | 161 EXPECT_FALSE(has_pt); |
| 161 continue; | 162 continue; |
| 162 } | 163 } |
| 163 EXPECT_TRUE(has_pt); | 164 EXPECT_TRUE(has_pt); |
| 164 | 165 |
| 165 EXPECT_EQ(12u, ct.size() - decrypted->length()); | 166 EXPECT_EQ(12u, ct.size() - decrypted->length()); |
| 166 ASSERT_EQ(pt.length(), decrypted->length()); | 167 ASSERT_EQ(pt.length(), decrypted->length()); |
| 167 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 168 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 168 pt.length(), pt.data(), pt.length()); | 169 pt.length(), pt.data(), pt.length()); |
| 169 } | 170 } |
| 170 } | 171 } |
| 171 | 172 |
| 172 } // namespace test | 173 } // namespace test |
| 173 } // namespace net | 174 } // namespace net |
| OLD | NEW |