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