| 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" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { | 87 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { |
| 88 ChaCha20Poly1305Encrypter encrypter; | 88 ChaCha20Poly1305Encrypter encrypter; |
| 89 ChaCha20Poly1305Decrypter decrypter; | 89 ChaCha20Poly1305Decrypter decrypter; |
| 90 | 90 |
| 91 string key = QuicTextUtils::HexDecode(test_vectors[0].key); | 91 string key = QuicTextUtils::HexDecode(test_vectors[0].key); |
| 92 ASSERT_TRUE(encrypter.SetKey(key)); | 92 ASSERT_TRUE(encrypter.SetKey(key)); |
| 93 ASSERT_TRUE(decrypter.SetKey(key)); | 93 ASSERT_TRUE(decrypter.SetKey(key)); |
| 94 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); | 94 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); |
| 95 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); | 95 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); |
| 96 | 96 |
| 97 QuicPathId path_id = 0x42; | |
| 98 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); | 97 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); |
| 99 string associated_data = "associated_data"; | 98 string associated_data = "associated_data"; |
| 100 string plaintext = "plaintext"; | 99 string plaintext = "plaintext"; |
| 101 char encrypted[1024]; | 100 char encrypted[1024]; |
| 102 size_t len; | 101 size_t len; |
| 103 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), path_id, packet_number, | 102 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), packet_number, |
| 104 associated_data, plaintext, encrypted, | 103 associated_data, plaintext, encrypted, |
| 105 &len, arraysize(encrypted))); | 104 &len, arraysize(encrypted))); |
| 106 StringPiece ciphertext(encrypted, len); | 105 StringPiece ciphertext(encrypted, len); |
| 107 char decrypted[1024]; | 106 char decrypted[1024]; |
| 108 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), path_id, packet_number, | 107 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), packet_number, |
| 109 associated_data, ciphertext, decrypted, | 108 associated_data, ciphertext, decrypted, |
| 110 &len, arraysize(decrypted))); | 109 &len, arraysize(decrypted))); |
| 111 } | 110 } |
| 112 | 111 |
| 113 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { | 112 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { |
| 114 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 113 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 115 // Decode the test vector. | 114 // Decode the test vector. |
| 116 string key = QuicTextUtils::HexDecode(test_vectors[i].key); | 115 string key = QuicTextUtils::HexDecode(test_vectors[i].key); |
| 117 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt); | 116 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt); |
| 118 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); | 117 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 146 | 145 |
| 147 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { | 146 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { |
| 148 ChaCha20Poly1305Encrypter encrypter; | 147 ChaCha20Poly1305Encrypter encrypter; |
| 149 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 148 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
| 150 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 149 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
| 151 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 150 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
| 152 } | 151 } |
| 153 | 152 |
| 154 } // namespace test | 153 } // namespace test |
| 155 } // namespace net | 154 } // namespace net |
| OLD | NEW |