| 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_encrypter.h" | 5 #include "net/quic/core/crypto/chacha20_poly1305_encrypter.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "net/quic/core/crypto/chacha20_poly1305_decrypter.h" | 9 #include "net/quic/core/crypto/chacha20_poly1305_decrypter.h" |
| 10 #include "net/quic/core/quic_utils.h" | 10 #include "net/quic/core/quic_utils.h" |
| 11 #include "net/quic/platform/api/quic_text_utils.h" |
| 11 #include "net/quic/test_tools/quic_test_utils.h" | 12 #include "net/quic/test_tools/quic_test_utils.h" |
| 12 | 13 |
| 13 using base::StringPiece; | 14 using base::StringPiece; |
| 14 using std::string; | 15 using std::string; |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // The test vectors come from RFC 7539 Section 2.8.2. | 19 // The test vectors come from RFC 7539 Section 2.8.2. |
| 19 | 20 |
| 20 // Each test vector consists of five strings of lowercase hexadecimal digits. | 21 // Each test vector consists of five strings of lowercase hexadecimal digits. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 return nullptr; | 81 return nullptr; |
| 81 } | 82 } |
| 82 | 83 |
| 83 return new QuicData(ciphertext.release(), ciphertext_size, true); | 84 return new QuicData(ciphertext.release(), ciphertext_size, true); |
| 84 } | 85 } |
| 85 | 86 |
| 86 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { | 87 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { |
| 87 ChaCha20Poly1305Encrypter encrypter; | 88 ChaCha20Poly1305Encrypter encrypter; |
| 88 ChaCha20Poly1305Decrypter decrypter; | 89 ChaCha20Poly1305Decrypter decrypter; |
| 89 | 90 |
| 90 string key = QuicUtils::HexDecode(test_vectors[0].key); | 91 string key = QuicTextUtils::HexDecode(test_vectors[0].key); |
| 91 ASSERT_TRUE(encrypter.SetKey(key)); | 92 ASSERT_TRUE(encrypter.SetKey(key)); |
| 92 ASSERT_TRUE(decrypter.SetKey(key)); | 93 ASSERT_TRUE(decrypter.SetKey(key)); |
| 93 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); | 94 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); |
| 94 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); | 95 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); |
| 95 | 96 |
| 96 QuicPathId path_id = 0x42; | 97 QuicPathId path_id = 0x42; |
| 97 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); | 98 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); |
| 98 string associated_data = "associated_data"; | 99 string associated_data = "associated_data"; |
| 99 string plaintext = "plaintext"; | 100 string plaintext = "plaintext"; |
| 100 char encrypted[1024]; | 101 char encrypted[1024]; |
| 101 size_t len; | 102 size_t len; |
| 102 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), path_id, packet_number, | 103 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), path_id, packet_number, |
| 103 associated_data, plaintext, encrypted, | 104 associated_data, plaintext, encrypted, |
| 104 &len, arraysize(encrypted))); | 105 &len, arraysize(encrypted))); |
| 105 StringPiece ciphertext(encrypted, len); | 106 StringPiece ciphertext(encrypted, len); |
| 106 char decrypted[1024]; | 107 char decrypted[1024]; |
| 107 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), path_id, packet_number, | 108 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), path_id, packet_number, |
| 108 associated_data, ciphertext, decrypted, | 109 associated_data, ciphertext, decrypted, |
| 109 &len, arraysize(decrypted))); | 110 &len, arraysize(decrypted))); |
| 110 } | 111 } |
| 111 | 112 |
| 112 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { | 113 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { |
| 113 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 114 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 114 // Decode the test vector. | 115 // Decode the test vector. |
| 115 string key = QuicUtils::HexDecode(test_vectors[i].key); | 116 string key = QuicTextUtils::HexDecode(test_vectors[i].key); |
| 116 string pt = QuicUtils::HexDecode(test_vectors[i].pt); | 117 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt); |
| 117 string iv = QuicUtils::HexDecode(test_vectors[i].iv); | 118 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); |
| 118 string fixed = QuicUtils::HexDecode(test_vectors[i].fixed); | 119 string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed); |
| 119 string aad = QuicUtils::HexDecode(test_vectors[i].aad); | 120 string aad = QuicTextUtils::HexDecode(test_vectors[i].aad); |
| 120 string ct = QuicUtils::HexDecode(test_vectors[i].ct); | 121 string ct = QuicTextUtils::HexDecode(test_vectors[i].ct); |
| 121 | 122 |
| 122 ChaCha20Poly1305Encrypter encrypter; | 123 ChaCha20Poly1305Encrypter encrypter; |
| 123 ASSERT_TRUE(encrypter.SetKey(key)); | 124 ASSERT_TRUE(encrypter.SetKey(key)); |
| 124 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( | 125 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( |
| 125 &encrypter, fixed + iv, | 126 &encrypter, fixed + iv, |
| 126 // This deliberately tests that the encrypter can handle an AAD that | 127 // This deliberately tests that the encrypter can handle an AAD that |
| 127 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | 128 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
| 128 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); | 129 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); |
| 129 ASSERT_TRUE(encrypted.get()); | 130 ASSERT_TRUE(encrypted.get()); |
| 130 EXPECT_EQ(12u, ct.size() - pt.size()); | 131 EXPECT_EQ(12u, ct.size() - pt.size()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 145 | 146 |
| 146 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { | 147 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { |
| 147 ChaCha20Poly1305Encrypter encrypter; | 148 ChaCha20Poly1305Encrypter encrypter; |
| 148 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 149 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
| 149 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 150 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
| 150 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 151 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
| 151 } | 152 } |
| 152 | 153 |
| 153 } // namespace test | 154 } // namespace test |
| 154 } // namespace net | 155 } // namespace net |
| OLD | NEW |