| 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" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 namespace net { | 111 namespace net { |
| 112 namespace test { | 112 namespace test { |
| 113 | 113 |
| 114 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing | 114 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
| 115 // in an nonce and also to allocate the buffer needed for the plaintext. | 115 // in an nonce and also to allocate the buffer needed for the plaintext. |
| 116 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, | 116 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
| 117 StringPiece nonce, | 117 StringPiece nonce, |
| 118 StringPiece associated_data, | 118 StringPiece associated_data, |
| 119 StringPiece ciphertext) { | 119 StringPiece ciphertext) { |
| 120 QuicPathId path_id = kDefaultPathId; | |
| 121 QuicPacketNumber packet_number; | 120 QuicPacketNumber packet_number; |
| 122 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number)); | 121 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number)); |
| 123 decrypter->SetNoncePrefix(nonce_prefix); | 122 decrypter->SetNoncePrefix(nonce_prefix); |
| 124 memcpy(&packet_number, nonce.data() + nonce_prefix.size(), | 123 memcpy(&packet_number, nonce.data() + nonce_prefix.size(), |
| 125 sizeof(packet_number)); | 124 sizeof(packet_number)); |
| 126 path_id = static_cast<QuicPathId>( | |
| 127 packet_number >> 8 * (sizeof(packet_number) - sizeof(path_id))); | |
| 128 packet_number &= UINT64_C(0x00FFFFFFFFFFFFFF); | |
| 129 std::unique_ptr<char[]> output(new char[ciphertext.length()]); | 125 std::unique_ptr<char[]> output(new char[ciphertext.length()]); |
| 130 size_t output_length = 0; | 126 size_t output_length = 0; |
| 131 const bool success = decrypter->DecryptPacket( | 127 const bool success = decrypter->DecryptPacket( |
| 132 QuicVersionMax(), path_id, packet_number, associated_data, ciphertext, | 128 QuicVersionMax(), packet_number, associated_data, ciphertext, |
| 133 output.get(), &output_length, ciphertext.length()); | 129 output.get(), &output_length, ciphertext.length()); |
| 134 if (!success) { | 130 if (!success) { |
| 135 return nullptr; | 131 return nullptr; |
| 136 } | 132 } |
| 137 return new QuicData(output.release(), output_length, true); | 133 return new QuicData(output.release(), output_length, true); |
| 138 } | 134 } |
| 139 | 135 |
| 140 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { | 136 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { |
| 141 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 137 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 142 // If not present then decryption is expected to fail. | 138 // If not present then decryption is expected to fail. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 168 | 164 |
| 169 EXPECT_EQ(12u, ct.size() - decrypted->length()); | 165 EXPECT_EQ(12u, ct.size() - decrypted->length()); |
| 170 ASSERT_EQ(pt.length(), decrypted->length()); | 166 ASSERT_EQ(pt.length(), decrypted->length()); |
| 171 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 167 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 172 pt.length(), pt.data(), pt.length()); | 168 pt.length(), pt.data(), pt.length()); |
| 173 } | 169 } |
| 174 } | 170 } |
| 175 | 171 |
| 176 } // namespace test | 172 } // namespace test |
| 177 } // namespace net | 173 } // namespace net |
| OLD | NEW |