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

Side by Side Diff: net/quic/crypto/chacha20_poly1305_decrypter_test.cc

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/quic/crypto/chacha20_poly1305_decrypter.h"
6
7 #include <memory>
8
9 #include "net/quic/quic_flags.h"
10 #include "net/quic/quic_utils.h"
11 #include "net/quic/test_tools/quic_test_utils.h"
12
13 using base::StringPiece;
14 using std::string;
15
16 namespace {
17
18 // The test vectors come from RFC 7539 Section 2.8.2.
19
20 // Each test vector consists of six strings of lowercase hexadecimal digits.
21 // The strings may be empty (zero length). A test vector with a nullptr |key|
22 // marks the end of an array of test vectors.
23 struct TestVector {
24 // Input:
25 const char* key;
26 const char* iv;
27 const char* fixed;
28 const char* aad;
29 const char* ct;
30
31 // Expected output:
32 const char* pt; // An empty string "" means decryption succeeded and
33 // the plaintext is zero-length. nullptr means decryption
34 // failed.
35 };
36
37 const TestVector test_vectors[] = {
38 {"808182838485868788898a8b8c8d8e8f"
39 "909192939495969798999a9b9c9d9e9f",
40
41 "4041424344454647",
42
43 "07000000",
44
45 "50515253c0c1c2c3c4c5c6c7",
46
47 "d31a8d34648e60db7b86afbc53ef7ec2"
48 "a4aded51296e08fea9e2b5a736ee62d6"
49 "3dbea45e8ca9671282fafb69da92728b"
50 "1a71de0a9e060b2905d6a5b67ecd3b36"
51 "92ddbd7f2d778b8c9803aee328091b58"
52 "fab324e4fad675945585808b4831d7bc"
53 "3ff4def08e4b7a9de576d26586cec64b"
54 "6116"
55 "1ae10b594f09e26a7e902ecb", // "d0600691" truncated
56
57 "4c616469657320616e642047656e746c"
58 "656d656e206f662074686520636c6173"
59 "73206f66202739393a20496620492063"
60 "6f756c64206f6666657220796f75206f"
61 "6e6c79206f6e652074697020666f7220"
62 "746865206675747572652c2073756e73"
63 "637265656e20776f756c642062652069"
64 "742e"},
65 // Modify the ciphertext (Poly1305 authenticator).
66 {"808182838485868788898a8b8c8d8e8f"
67 "909192939495969798999a9b9c9d9e9f",
68
69 "4041424344454647",
70
71 "07000000",
72
73 "50515253c0c1c2c3c4c5c6c7",
74
75 "d31a8d34648e60db7b86afbc53ef7ec2"
76 "a4aded51296e08fea9e2b5a736ee62d6"
77 "3dbea45e8ca9671282fafb69da92728b"
78 "1a71de0a9e060b2905d6a5b67ecd3b36"
79 "92ddbd7f2d778b8c9803aee328091b58"
80 "fab324e4fad675945585808b4831d7bc"
81 "3ff4def08e4b7a9de576d26586cec64b"
82 "6116"
83 "1ae10b594f09e26a7e902ecc", // "d0600691" truncated
84
85 nullptr},
86 // Modify the associated data.
87 {"808182838485868788898a8b8c8d8e8f"
88 "909192939495969798999a9b9c9d9e9f",
89
90 "4041424344454647",
91
92 "07000000",
93
94 "60515253c0c1c2c3c4c5c6c7",
95
96 "d31a8d34648e60db7b86afbc53ef7ec2"
97 "a4aded51296e08fea9e2b5a736ee62d6"
98 "3dbea45e8ca9671282fafb69da92728b"
99 "1a71de0a9e060b2905d6a5b67ecd3b36"
100 "92ddbd7f2d778b8c9803aee328091b58"
101 "fab324e4fad675945585808b4831d7bc"
102 "3ff4def08e4b7a9de576d26586cec64b"
103 "6116"
104 "1ae10b594f09e26a7e902ecb", // "d0600691" truncated
105
106 nullptr},
107 {nullptr}};
108
109 } // namespace
110
111 namespace net {
112 namespace test {
113
114 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing
115 // in an nonce and also to allocate the buffer needed for the plaintext.
116 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter,
117 StringPiece nonce,
118 StringPiece associated_data,
119 StringPiece ciphertext) {
120 QuicPathId path_id = kDefaultPathId;
121 QuicPacketNumber packet_number;
122 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number));
123 decrypter->SetNoncePrefix(nonce_prefix);
124 memcpy(&packet_number, nonce.data() + nonce_prefix.size(),
125 sizeof(packet_number));
126 path_id = static_cast<QuicPathId>(
127 packet_number >> 8 * (sizeof(packet_number) - sizeof(path_id)));
128 packet_number &= UINT64_C(0x00FFFFFFFFFFFFFF);
129 std::unique_ptr<char[]> output(new char[ciphertext.length()]);
130 size_t output_length = 0;
131 const bool success = decrypter->DecryptPacket(
132 path_id, packet_number, associated_data, ciphertext, output.get(),
133 &output_length, ciphertext.length());
134 if (!success) {
135 return nullptr;
136 }
137 return new QuicData(output.release(), output_length, true);
138 }
139
140 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) {
141 for (size_t i = 0; test_vectors[i].key != nullptr; i++) {
142 // If not present then decryption is expected to fail.
143 bool has_pt = test_vectors[i].pt;
144
145 // Decode the test vector.
146 string key = QuicUtils::HexDecode(test_vectors[i].key);
147 string iv = QuicUtils::HexDecode(test_vectors[i].iv);
148 string fixed = QuicUtils::HexDecode(test_vectors[i].fixed);
149 string aad = QuicUtils::HexDecode(test_vectors[i].aad);
150 string ct = QuicUtils::HexDecode(test_vectors[i].ct);
151 string pt;
152 if (has_pt) {
153 pt = QuicUtils::HexDecode(test_vectors[i].pt);
154 }
155
156 ChaCha20Poly1305Decrypter decrypter;
157 ASSERT_TRUE(decrypter.SetKey(key));
158 std::unique_ptr<QuicData> decrypted(DecryptWithNonce(
159 &decrypter, fixed + iv,
160 // This deliberately tests that the decrypter can handle an AAD that
161 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer.
162 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct));
163 if (!decrypted.get()) {
164 EXPECT_FALSE(has_pt);
165 continue;
166 }
167 EXPECT_TRUE(has_pt);
168
169 EXPECT_EQ(12u, ct.size() - decrypted->length());
170 ASSERT_EQ(pt.length(), decrypted->length());
171 test::CompareCharArraysWithHexError("plaintext", decrypted->data(),
172 pt.length(), pt.data(), pt.length());
173 }
174 }
175
176 } // namespace test
177 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/crypto/chacha20_poly1305_decrypter.cc ('k') | net/quic/crypto/chacha20_poly1305_encrypter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698