| 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_decrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_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 using std::string; | 10 using std::string; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 namespace net { | 69 namespace net { |
| 70 namespace test { | 70 namespace test { |
| 71 | 71 |
| 72 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing | 72 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
| 73 // in an nonce and also to allocate the buffer needed for the plaintext. | 73 // in an nonce and also to allocate the buffer needed for the plaintext. |
| 74 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, | 74 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
| 75 StringPiece nonce, | 75 StringPiece nonce, |
| 76 StringPiece associated_data, | 76 StringPiece associated_data, |
| 77 StringPiece ciphertext) { | 77 StringPiece ciphertext) { |
| 78 size_t plaintext_size = ciphertext.length(); | 78 QuicPacketSequenceNumber sequence_number; |
| 79 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 79 StringPiece nonce_prefix(nonce.data(), |
| 80 | 80 nonce.size() - sizeof(sequence_number)); |
| 81 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, | 81 decrypter->SetNoncePrefix(nonce_prefix); |
| 82 reinterpret_cast<unsigned char*>(plaintext.get()), | 82 memcpy(&sequence_number, nonce.data() + nonce_prefix.size(), |
| 83 &plaintext_size)) { | 83 sizeof(sequence_number)); |
| 84 return nullptr; | 84 return decrypter->DecryptPacket(sequence_number, associated_data, ciphertext); |
| 85 } | |
| 86 return new QuicData(plaintext.release(), plaintext_size, true); | |
| 87 } | 85 } |
| 88 | 86 |
| 89 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { | 87 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { |
| 90 if (!ChaCha20Poly1305Decrypter::IsSupported()) { | 88 if (!ChaCha20Poly1305Decrypter::IsSupported()) { |
| 91 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; | 89 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; |
| 92 return; | 90 return; |
| 93 } | 91 } |
| 94 | 92 |
| 95 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 93 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 96 // If not present then decryption is expected to fail. | 94 // If not present then decryption is expected to fail. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 124 EXPECT_TRUE(has_pt); | 122 EXPECT_TRUE(has_pt); |
| 125 | 123 |
| 126 ASSERT_EQ(pt.length(), decrypted->length()); | 124 ASSERT_EQ(pt.length(), decrypted->length()); |
| 127 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 125 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 128 pt.length(), pt.data(), pt.length()); | 126 pt.length(), pt.data(), pt.length()); |
| 129 } | 127 } |
| 130 } | 128 } |
| 131 | 129 |
| 132 } // namespace test | 130 } // namespace test |
| 133 } // namespace net | 131 } // namespace net |
| OLD | NEW |