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/crypto/chacha20_poly1305_rfc7539_encrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_encrypter.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "net/quic/crypto/chacha20_poly1305_rfc7539_decrypter.h" | 10 #include "net/quic/crypto/chacha20_poly1305_decrypter.h" |
11 #include "net/quic/test_tools/quic_test_utils.h" | 11 #include "net/quic/test_tools/quic_test_utils.h" |
12 | 12 |
13 using base::StringPiece; | 13 using base::StringPiece; |
14 using std::string; | 14 using std::string; |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // The test vectors come from RFC 7539 Section 2.8.2. | 18 // The test vectors come from RFC 7539 Section 2.8.2. |
19 | 19 |
20 // Each test vector consists of five strings of lowercase hexadecimal digits. | 20 // Each test vector consists of five strings of lowercase hexadecimal digits. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 }, | 61 }, |
62 {nullptr}}; | 62 {nullptr}}; |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 namespace net { | 66 namespace net { |
67 namespace test { | 67 namespace test { |
68 | 68 |
69 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing | 69 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing |
70 // 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. |
71 QuicData* EncryptWithNonce(ChaCha20Poly1305Rfc7539Encrypter* encrypter, | 71 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, |
72 StringPiece nonce, | 72 StringPiece nonce, |
73 StringPiece associated_data, | 73 StringPiece associated_data, |
74 StringPiece plaintext) { | 74 StringPiece plaintext) { |
75 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); | 75 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); |
76 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]); | 76 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]); |
77 | 77 |
78 if (!encrypter->Encrypt(nonce, associated_data, plaintext, | 78 if (!encrypter->Encrypt(nonce, associated_data, plaintext, |
79 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 79 reinterpret_cast<unsigned char*>(ciphertext.get()))) { |
80 return nullptr; | 80 return nullptr; |
81 } | 81 } |
82 | 82 |
83 return new QuicData(ciphertext.release(), ciphertext_size, true); | 83 return new QuicData(ciphertext.release(), ciphertext_size, true); |
84 } | 84 } |
85 | 85 |
86 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, EncryptThenDecrypt) { | 86 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { |
87 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | 87 ChaCha20Poly1305Encrypter encrypter; |
88 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | 88 ChaCha20Poly1305Decrypter decrypter; |
89 return; | |
90 } | |
91 | |
92 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
93 ChaCha20Poly1305Rfc7539Decrypter decrypter; | |
94 | 89 |
95 string key; | 90 string key; |
96 DecodeHexString(test_vectors[0].key, &key); | 91 DecodeHexString(test_vectors[0].key, &key); |
97 ASSERT_TRUE(encrypter.SetKey(key)); | 92 ASSERT_TRUE(encrypter.SetKey(key)); |
98 ASSERT_TRUE(decrypter.SetKey(key)); | 93 ASSERT_TRUE(decrypter.SetKey(key)); |
99 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); | 94 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); |
100 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); | 95 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); |
101 | 96 |
102 QuicPathId path_id = 0x42; | 97 QuicPathId path_id = 0x42; |
103 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); | 98 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); |
104 string associated_data = "associated_data"; | 99 string associated_data = "associated_data"; |
105 string plaintext = "plaintext"; | 100 string plaintext = "plaintext"; |
106 char encrypted[1024]; | 101 char encrypted[1024]; |
107 size_t len; | 102 size_t len; |
108 ASSERT_TRUE(encrypter.EncryptPacket(path_id, packet_number, associated_data, | 103 ASSERT_TRUE(encrypter.EncryptPacket(path_id, packet_number, associated_data, |
109 plaintext, encrypted, &len, | 104 plaintext, encrypted, &len, |
110 arraysize(encrypted))); | 105 arraysize(encrypted))); |
111 StringPiece ciphertext(encrypted, len); | 106 StringPiece ciphertext(encrypted, len); |
112 char decrypted[1024]; | 107 char decrypted[1024]; |
113 ASSERT_TRUE(decrypter.DecryptPacket(path_id, packet_number, associated_data, | 108 ASSERT_TRUE(decrypter.DecryptPacket(path_id, packet_number, associated_data, |
114 ciphertext, decrypted, &len, | 109 ciphertext, decrypted, &len, |
115 arraysize(decrypted))); | 110 arraysize(decrypted))); |
116 } | 111 } |
117 | 112 |
118 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, Encrypt) { | 113 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { |
119 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | |
120 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
121 return; | |
122 } | |
123 | |
124 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 114 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
125 // Decode the test vector. | 115 // Decode the test vector. |
126 string key; | 116 string key; |
127 string pt; | 117 string pt; |
128 string iv; | 118 string iv; |
129 string fixed; | 119 string fixed; |
130 string aad; | 120 string aad; |
131 string ct; | 121 string ct; |
132 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | 122 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); |
133 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 123 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
134 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 124 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
135 ASSERT_TRUE(DecodeHexString(test_vectors[i].fixed, &fixed)); | 125 ASSERT_TRUE(DecodeHexString(test_vectors[i].fixed, &fixed)); |
136 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 126 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
137 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 127 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
138 | 128 |
139 ChaCha20Poly1305Rfc7539Encrypter encrypter; | 129 ChaCha20Poly1305Encrypter encrypter; |
140 ASSERT_TRUE(encrypter.SetKey(key)); | 130 ASSERT_TRUE(encrypter.SetKey(key)); |
141 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( | 131 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( |
142 &encrypter, fixed + iv, | 132 &encrypter, fixed + iv, |
143 // This deliberately tests that the encrypter can handle an AAD that | 133 // This deliberately tests that the encrypter can handle an AAD that |
144 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | 134 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
145 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); | 135 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); |
146 ASSERT_TRUE(encrypted.get()); | 136 ASSERT_TRUE(encrypted.get()); |
147 EXPECT_EQ(12u, ct.size() - pt.size()); | 137 EXPECT_EQ(12u, ct.size() - pt.size()); |
148 EXPECT_EQ(12u, encrypted->length() - pt.size()); | 138 EXPECT_EQ(12u, encrypted->length() - pt.size()); |
149 | 139 |
150 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), | 140 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), |
151 encrypted->length(), ct.data(), | 141 encrypted->length(), ct.data(), |
152 ct.length()); | 142 ct.length()); |
153 } | 143 } |
154 } | 144 } |
155 | 145 |
156 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, GetMaxPlaintextSize) { | 146 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { |
157 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | 147 ChaCha20Poly1305Encrypter encrypter; |
158 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
159 return; | |
160 } | |
161 | |
162 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
163 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); | 148 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); |
164 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); | 149 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); |
165 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); | 150 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); |
166 } | 151 } |
167 | 152 |
168 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, GetCiphertextSize) { | 153 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { |
169 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | 154 ChaCha20Poly1305Encrypter encrypter; |
170 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
171 return; | |
172 } | |
173 | |
174 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
175 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 155 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
176 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 156 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
177 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 157 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
178 } | 158 } |
179 | 159 |
180 } // namespace test | 160 } // namespace test |
181 } // namespace net | 161 } // namespace net |
OLD | NEW |