| 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/crypto/chacha20_poly1305_rfc7539_decrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_decrypter.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "net/quic/quic_flags.h" | 9 #include "net/quic/quic_flags.h" |
| 10 #include "net/quic/test_tools/quic_test_utils.h" | 10 #include "net/quic/test_tools/quic_test_utils.h" |
| 11 | 11 |
| 12 using base::StringPiece; | 12 using base::StringPiece; |
| 13 using std::string; | 13 using std::string; |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 nullptr}, | 105 nullptr}, |
| 106 {nullptr}}; | 106 {nullptr}}; |
| 107 | 107 |
| 108 } // namespace | 108 } // namespace |
| 109 | 109 |
| 110 namespace net { | 110 namespace net { |
| 111 namespace test { | 111 namespace test { |
| 112 | 112 |
| 113 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing | 113 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
| 114 // 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. |
| 115 QuicData* DecryptWithNonce(ChaCha20Poly1305Rfc7539Decrypter* decrypter, | 115 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
| 116 StringPiece nonce, | 116 StringPiece nonce, |
| 117 StringPiece associated_data, | 117 StringPiece associated_data, |
| 118 StringPiece ciphertext) { | 118 StringPiece ciphertext) { |
| 119 QuicPathId path_id = kDefaultPathId; | 119 QuicPathId path_id = kDefaultPathId; |
| 120 QuicPacketNumber packet_number; | 120 QuicPacketNumber packet_number; |
| 121 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number)); | 121 StringPiece nonce_prefix(nonce.data(), 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 path_id = static_cast<QuicPathId>( | 125 path_id = static_cast<QuicPathId>( |
| 126 packet_number >> 8 * (sizeof(packet_number) - sizeof(path_id))); | 126 packet_number >> 8 * (sizeof(packet_number) - sizeof(path_id))); |
| 127 packet_number &= UINT64_C(0x00FFFFFFFFFFFFFF); | 127 packet_number &= UINT64_C(0x00FFFFFFFFFFFFFF); |
| 128 std::unique_ptr<char[]> output(new char[ciphertext.length()]); | 128 std::unique_ptr<char[]> output(new char[ciphertext.length()]); |
| 129 size_t output_length = 0; | 129 size_t output_length = 0; |
| 130 const bool success = decrypter->DecryptPacket( | 130 const bool success = decrypter->DecryptPacket( |
| 131 path_id, packet_number, associated_data, ciphertext, output.get(), | 131 path_id, packet_number, associated_data, ciphertext, output.get(), |
| 132 &output_length, ciphertext.length()); | 132 &output_length, ciphertext.length()); |
| 133 if (!success) { | 133 if (!success) { |
| 134 return nullptr; | 134 return nullptr; |
| 135 } | 135 } |
| 136 return new QuicData(output.release(), output_length, true); | 136 return new QuicData(output.release(), output_length, true); |
| 137 } | 137 } |
| 138 | 138 |
| 139 TEST(ChaCha20Poly1305Rfc7539DecrypterTest, Decrypt) { | 139 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { |
| 140 if (!ChaCha20Poly1305Rfc7539Decrypter::IsSupported()) { | |
| 141 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
| 142 return; | |
| 143 } | |
| 144 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 140 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 145 // If not present then decryption is expected to fail. | 141 // If not present then decryption is expected to fail. |
| 146 bool has_pt = test_vectors[i].pt; | 142 bool has_pt = test_vectors[i].pt; |
| 147 | 143 |
| 148 // Decode the test vector. | 144 // Decode the test vector. |
| 149 string key; | 145 string key; |
| 150 string iv; | 146 string iv; |
| 151 string fixed; | 147 string fixed; |
| 152 string aad; | 148 string aad; |
| 153 string ct; | 149 string ct; |
| 154 string pt; | 150 string pt; |
| 155 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | 151 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); |
| 156 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 152 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
| 157 ASSERT_TRUE(DecodeHexString(test_vectors[i].fixed, &fixed)); | 153 ASSERT_TRUE(DecodeHexString(test_vectors[i].fixed, &fixed)); |
| 158 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 154 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
| 159 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 155 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
| 160 if (has_pt) { | 156 if (has_pt) { |
| 161 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 157 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
| 162 } | 158 } |
| 163 | 159 |
| 164 ChaCha20Poly1305Rfc7539Decrypter decrypter; | 160 ChaCha20Poly1305Decrypter decrypter; |
| 165 ASSERT_TRUE(decrypter.SetKey(key)); | 161 ASSERT_TRUE(decrypter.SetKey(key)); |
| 166 std::unique_ptr<QuicData> decrypted(DecryptWithNonce( | 162 std::unique_ptr<QuicData> decrypted(DecryptWithNonce( |
| 167 &decrypter, fixed + iv, | 163 &decrypter, fixed + iv, |
| 168 // This deliberately tests that the decrypter can handle an AAD that | 164 // This deliberately tests that the decrypter can handle an AAD that |
| 169 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | 165 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
| 170 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); | 166 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); |
| 171 if (!decrypted.get()) { | 167 if (!decrypted.get()) { |
| 172 EXPECT_FALSE(has_pt); | 168 EXPECT_FALSE(has_pt); |
| 173 continue; | 169 continue; |
| 174 } | 170 } |
| 175 EXPECT_TRUE(has_pt); | 171 EXPECT_TRUE(has_pt); |
| 176 | 172 |
| 177 EXPECT_EQ(12u, ct.size() - decrypted->length()); | 173 EXPECT_EQ(12u, ct.size() - decrypted->length()); |
| 178 ASSERT_EQ(pt.length(), decrypted->length()); | 174 ASSERT_EQ(pt.length(), decrypted->length()); |
| 179 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 175 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 180 pt.length(), pt.data(), pt.length()); | 176 pt.length(), pt.data(), pt.length()); |
| 181 } | 177 } |
| 182 } | 178 } |
| 183 | 179 |
| 184 } // namespace test | 180 } // namespace test |
| 185 } // namespace net | 181 } // namespace net |
| OLD | NEW |