Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: net/quic/core/crypto/chacha20_poly1305_encrypter_test.cc

Issue 2740453006: Add QuicStringPiece which is actually StringPiece. (Closed)
Patch Set: fix compile error and rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/platform/api/quic_text_utils.h"
12 #include "net/quic/test_tools/quic_test_utils.h" 12 #include "net/quic/test_tools/quic_test_utils.h"
13 13
14 using base::StringPiece;
15 using std::string; 14 using std::string;
16 15
17 namespace { 16 namespace {
18 17
19 // The test vectors come from RFC 7539 Section 2.8.2. 18 // The test vectors come from RFC 7539 Section 2.8.2.
20 19
21 // Each test vector consists of five strings of lowercase hexadecimal digits. 20 // Each test vector consists of five strings of lowercase hexadecimal digits.
22 // The strings may be empty (zero length). A test vector with a nullptr |key| 21 // The strings may be empty (zero length). A test vector with a nullptr |key|
23 // marks the end of an array of test vectors. 22 // marks the end of an array of test vectors.
24 struct TestVector { 23 struct TestVector {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 {nullptr}}; 62 {nullptr}};
64 63
65 } // namespace 64 } // namespace
66 65
67 namespace net { 66 namespace net {
68 namespace test { 67 namespace test {
69 68
70 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing 69 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing
71 // in an nonce and also to allocate the buffer needed for the ciphertext. 70 // in an nonce and also to allocate the buffer needed for the ciphertext.
72 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, 71 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter,
73 StringPiece nonce, 72 QuicStringPiece nonce,
74 StringPiece associated_data, 73 QuicStringPiece associated_data,
75 StringPiece plaintext) { 74 QuicStringPiece plaintext) {
76 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); 75 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
77 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]); 76 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]);
78 77
79 if (!encrypter->Encrypt(nonce, associated_data, plaintext, 78 if (!encrypter->Encrypt(nonce, associated_data, plaintext,
80 reinterpret_cast<unsigned char*>(ciphertext.get()))) { 79 reinterpret_cast<unsigned char*>(ciphertext.get()))) {
81 return nullptr; 80 return nullptr;
82 } 81 }
83 82
84 return new QuicData(ciphertext.release(), ciphertext_size, true); 83 return new QuicData(ciphertext.release(), ciphertext_size, true);
85 } 84 }
86 85
87 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { 86 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) {
88 ChaCha20Poly1305Encrypter encrypter; 87 ChaCha20Poly1305Encrypter encrypter;
89 ChaCha20Poly1305Decrypter decrypter; 88 ChaCha20Poly1305Decrypter decrypter;
90 89
91 string key = QuicTextUtils::HexDecode(test_vectors[0].key); 90 string key = QuicTextUtils::HexDecode(test_vectors[0].key);
92 ASSERT_TRUE(encrypter.SetKey(key)); 91 ASSERT_TRUE(encrypter.SetKey(key));
93 ASSERT_TRUE(decrypter.SetKey(key)); 92 ASSERT_TRUE(decrypter.SetKey(key));
94 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); 93 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd"));
95 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); 94 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd"));
96 95
97 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); 96 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC);
98 string associated_data = "associated_data"; 97 string associated_data = "associated_data";
99 string plaintext = "plaintext"; 98 string plaintext = "plaintext";
100 char encrypted[1024]; 99 char encrypted[1024];
101 size_t len; 100 size_t len;
102 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), packet_number, 101 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), packet_number,
103 associated_data, plaintext, encrypted, 102 associated_data, plaintext, encrypted,
104 &len, arraysize(encrypted))); 103 &len, arraysize(encrypted)));
105 StringPiece ciphertext(encrypted, len); 104 QuicStringPiece ciphertext(encrypted, len);
106 char decrypted[1024]; 105 char decrypted[1024];
107 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), packet_number, 106 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), packet_number,
108 associated_data, ciphertext, decrypted, 107 associated_data, ciphertext, decrypted,
109 &len, arraysize(decrypted))); 108 &len, arraysize(decrypted)));
110 } 109 }
111 110
112 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { 111 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) {
113 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { 112 for (size_t i = 0; test_vectors[i].key != nullptr; i++) {
114 // Decode the test vector. 113 // Decode the test vector.
115 string key = QuicTextUtils::HexDecode(test_vectors[i].key); 114 string key = QuicTextUtils::HexDecode(test_vectors[i].key);
116 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt); 115 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt);
117 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); 116 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv);
118 string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed); 117 string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed);
119 string aad = QuicTextUtils::HexDecode(test_vectors[i].aad); 118 string aad = QuicTextUtils::HexDecode(test_vectors[i].aad);
120 string ct = QuicTextUtils::HexDecode(test_vectors[i].ct); 119 string ct = QuicTextUtils::HexDecode(test_vectors[i].ct);
121 120
122 ChaCha20Poly1305Encrypter encrypter; 121 ChaCha20Poly1305Encrypter encrypter;
123 ASSERT_TRUE(encrypter.SetKey(key)); 122 ASSERT_TRUE(encrypter.SetKey(key));
124 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( 123 std::unique_ptr<QuicData> encrypted(EncryptWithNonce(
125 &encrypter, fixed + iv, 124 &encrypter, fixed + iv,
126 // This deliberately tests that the encrypter can handle an AAD that 125 // 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. 126 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer.
128 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); 127 QuicStringPiece(aad.length() ? aad.data() : nullptr, aad.length()),
128 pt));
129 ASSERT_TRUE(encrypted.get()); 129 ASSERT_TRUE(encrypted.get());
130 EXPECT_EQ(12u, ct.size() - pt.size()); 130 EXPECT_EQ(12u, ct.size() - pt.size());
131 EXPECT_EQ(12u, encrypted->length() - pt.size()); 131 EXPECT_EQ(12u, encrypted->length() - pt.size());
132 132
133 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), 133 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(),
134 encrypted->length(), ct.data(), 134 encrypted->length(), ct.data(),
135 ct.length()); 135 ct.length());
136 } 136 }
137 } 137 }
138 138
139 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { 139 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) {
140 ChaCha20Poly1305Encrypter encrypter; 140 ChaCha20Poly1305Encrypter encrypter;
141 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); 141 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012));
142 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); 142 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112));
143 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); 143 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22));
144 } 144 }
145 145
146 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { 146 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) {
147 ChaCha20Poly1305Encrypter encrypter; 147 ChaCha20Poly1305Encrypter encrypter;
148 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); 148 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000));
149 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); 149 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100));
150 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); 150 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10));
151 } 151 }
152 152
153 } // namespace test 153 } // namespace test
154 } // namespace net 154 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/core/crypto/chacha20_poly1305_decrypter_test.cc ('k') | net/quic/core/crypto/channel_id.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698