OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/aes_128_gcm_12_decrypter.h" | 5 #include "net/quic/crypto/aes_128_gcm_12_decrypter.h" |
6 | 6 |
7 #include "net/quic/test_tools/quic_test_utils.h" | 7 #include "net/quic/test_tools/quic_test_utils.h" |
8 | 8 |
9 using base::StringPiece; | 9 using base::StringPiece; |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 const char* aad; | 62 const char* aad; |
63 const char* tag; | 63 const char* tag; |
64 | 64 |
65 // Expected output: | 65 // Expected output: |
66 const char* pt; // An empty string "" means decryption succeeded and | 66 const char* pt; // An empty string "" means decryption succeeded and |
67 // the plaintext is zero-length. NULL means decryption | 67 // the plaintext is zero-length. NULL means decryption |
68 // failed. | 68 // failed. |
69 }; | 69 }; |
70 | 70 |
71 const TestGroupInfo test_group_info[] = { | 71 const TestGroupInfo test_group_info[] = { |
72 { 128, 96, 0, 0, 128 }, | 72 {128, 96, 0, 0, 128}, |
73 { 128, 96, 0, 128, 128 }, | 73 {128, 96, 0, 128, 128}, |
74 { 128, 96, 128, 0, 128 }, | 74 {128, 96, 128, 0, 128}, |
75 { 128, 96, 408, 160, 128 }, | 75 {128, 96, 408, 160, 128}, |
76 { 128, 96, 408, 720, 128 }, | 76 {128, 96, 408, 720, 128}, |
77 { 128, 96, 104, 0, 128 }, | 77 {128, 96, 104, 0, 128}, |
78 }; | 78 }; |
79 | 79 |
80 const TestVector test_group_0[] = { | 80 const TestVector test_group_0[] = { |
81 { "cf063a34d4a9a76c2c86787d3f96db71", | 81 {"cf063a34d4a9a76c2c86787d3f96db71", "113b9785971864c83b01c787", "", "", |
82 "113b9785971864c83b01c787", | 82 "72ac8493e3a5228b5d130a69d2510e42", ""}, |
83 "", | 83 { |
84 "", | 84 "a49a5e26a2f8cb63d05546c2a62f5343", "907763b19b9b4ab6bd4f0281", "", "", |
85 "72ac8493e3a5228b5d130a69d2510e42", | 85 "a2be08210d8c470a8df6e8fbd79ec5cf", |
86 "" | 86 NULL // FAIL |
87 }, | 87 }, |
88 { "a49a5e26a2f8cb63d05546c2a62f5343", | 88 {NULL}}; |
89 "907763b19b9b4ab6bd4f0281", | |
90 "", | |
91 "", | |
92 "a2be08210d8c470a8df6e8fbd79ec5cf", | |
93 NULL // FAIL | |
94 }, | |
95 { NULL } | |
96 }; | |
97 | 89 |
98 const TestVector test_group_1[] = { | 90 const TestVector test_group_1[] = { |
99 { "d1f6af919cde85661208bdce0c27cb22", | 91 { |
100 "898c6929b435017bf031c3c5", | 92 "d1f6af919cde85661208bdce0c27cb22", "898c6929b435017bf031c3c5", "", |
101 "", | 93 "7c5faa40e636bbc91107e68010c92b9f", "ae45f11777540a2caeb128be8092468a", |
102 "7c5faa40e636bbc91107e68010c92b9f", | 94 NULL // FAIL |
103 "ae45f11777540a2caeb128be8092468a", | 95 }, |
104 NULL // FAIL | 96 {"2370e320d4344208e0ff5683f243b213", "04dbb82f044d30831c441228", "", |
105 }, | 97 "d43a8e5089eea0d026c03a85178b27da", "2a049c049d25aa95969b451d93c31c6e", |
106 { "2370e320d4344208e0ff5683f243b213", | 98 ""}, |
107 "04dbb82f044d30831c441228", | 99 {NULL}}; |
108 "", | |
109 "d43a8e5089eea0d026c03a85178b27da", | |
110 "2a049c049d25aa95969b451d93c31c6e", | |
111 "" | |
112 }, | |
113 { NULL } | |
114 }; | |
115 | 100 |
116 const TestVector test_group_2[] = { | 101 const TestVector test_group_2[] = { |
117 { "e98b72a9881a84ca6b76e0f43e68647a", | 102 {"e98b72a9881a84ca6b76e0f43e68647a", "8b23299fde174053f3d652ba", |
118 "8b23299fde174053f3d652ba", | 103 "5a3c1cf1985dbb8bed818036fdd5ab42", "", "23c7ab0f952b7091cd324835043b5eb5", |
119 "5a3c1cf1985dbb8bed818036fdd5ab42", | 104 "28286a321293253c3e0aa2704a278032"}, |
120 "", | 105 {"33240636cd3236165f1a553b773e728e", "17c4d61493ecdc8f31700b12", |
121 "23c7ab0f952b7091cd324835043b5eb5", | 106 "47bb7e23f7bdfe05a8091ac90e4f8b2e", "", "b723c70e931d9785f40fd4ab1d612dc9", |
122 "28286a321293253c3e0aa2704a278032" | 107 "95695a5b12f2870b9cc5fdc8f218a97d"}, |
123 }, | 108 { |
124 { "33240636cd3236165f1a553b773e728e", | 109 "5164df856f1e9cac04a79b808dc5be39", "e76925d5355e0584ce871b2b", |
125 "17c4d61493ecdc8f31700b12", | 110 "0216c899c88d6e32c958c7e553daa5bc", "", "a145319896329c96df291f64efbe0e3a", |
126 "47bb7e23f7bdfe05a8091ac90e4f8b2e", | 111 NULL // FAIL |
127 "", | 112 }, |
128 "b723c70e931d9785f40fd4ab1d612dc9", | 113 {NULL}}; |
129 "95695a5b12f2870b9cc5fdc8f218a97d" | |
130 }, | |
131 { "5164df856f1e9cac04a79b808dc5be39", | |
132 "e76925d5355e0584ce871b2b", | |
133 "0216c899c88d6e32c958c7e553daa5bc", | |
134 "", | |
135 "a145319896329c96df291f64efbe0e3a", | |
136 NULL // FAIL | |
137 }, | |
138 { NULL } | |
139 }; | |
140 | 114 |
141 const TestVector test_group_3[] = { | 115 const TestVector test_group_3[] = { |
142 { "af57f42c60c0fc5a09adb81ab86ca1c3", | 116 {"af57f42c60c0fc5a09adb81ab86ca1c3", "a2dc01871f37025dc0fc9a79", |
143 "a2dc01871f37025dc0fc9a79", | 117 "b9a535864f48ea7b6b1367914978f9bfa087d854bb0e269bed8d279d2eea1210e48947" |
144 "b9a535864f48ea7b6b1367914978f9bfa087d854bb0e269bed8d279d2eea1210e48947" | 118 "338b22f9bad09093276a331e9c79c7f4", |
145 "338b22f9bad09093276a331e9c79c7f4", | 119 "41dc38988945fcb44faf2ef72d0061289ef8efd8", |
146 "41dc38988945fcb44faf2ef72d0061289ef8efd8", | 120 "4f71e72bde0018f555c5adcce062e005", |
147 "4f71e72bde0018f555c5adcce062e005", | 121 "3803a0727eeb0ade441e0ec107161ded2d425ec0d102f21f51bf2cf9947c7ec4aa7279" |
148 "3803a0727eeb0ade441e0ec107161ded2d425ec0d102f21f51bf2cf9947c7ec4aa7279" | 122 "5b2f69b041596e8817d0a3c16f8fadeb"}, |
149 "5b2f69b041596e8817d0a3c16f8fadeb" | 123 {"ebc753e5422b377d3cb64b58ffa41b61", "2e1821efaced9acf1f241c9b", |
150 }, | 124 "069567190554e9ab2b50a4e1fbf9c147340a5025fdbd201929834eaf6532325899ccb9" |
151 { "ebc753e5422b377d3cb64b58ffa41b61", | 125 "f401823e04b05817243d2142a3589878", |
152 "2e1821efaced9acf1f241c9b", | 126 "b9673412fd4f88ba0e920f46dd6438ff791d8eef", |
153 "069567190554e9ab2b50a4e1fbf9c147340a5025fdbd201929834eaf6532325899ccb9" | 127 "534d9234d2351cf30e565de47baece0b", |
154 "f401823e04b05817243d2142a3589878", | 128 "39077edb35e9c5a4b1e4c2a6b9bb1fce77f00f5023af40333d6d699014c2bcf4209c18" |
155 "b9673412fd4f88ba0e920f46dd6438ff791d8eef", | 129 "353a18017f5b36bfc00b1f6dcb7ed485"}, |
156 "534d9234d2351cf30e565de47baece0b", | 130 { |
157 "39077edb35e9c5a4b1e4c2a6b9bb1fce77f00f5023af40333d6d699014c2bcf4209c18" | 131 "52bdbbf9cf477f187ec010589cb39d58", "d3be36d3393134951d324b31", |
158 "353a18017f5b36bfc00b1f6dcb7ed485" | 132 "700188da144fa692cf46e4a8499510a53d90903c967f7f13e8a1bd8151a74adc4fe63e" |
159 }, | 133 "32b992760b3a5f99e9a47838867000a9", |
160 { "52bdbbf9cf477f187ec010589cb39d58", | 134 "93c4fc6a4135f54d640b0c976bf755a06a292c33", |
161 "d3be36d3393134951d324b31", | 135 "8ca4e38aa3dfa6b1d0297021ccf3ea5f", |
162 "700188da144fa692cf46e4a8499510a53d90903c967f7f13e8a1bd8151a74adc4fe63e" | 136 NULL // FAIL |
163 "32b992760b3a5f99e9a47838867000a9", | 137 }, |
164 "93c4fc6a4135f54d640b0c976bf755a06a292c33", | 138 {NULL}}; |
165 "8ca4e38aa3dfa6b1d0297021ccf3ea5f", | |
166 NULL // FAIL | |
167 }, | |
168 { NULL } | |
169 }; | |
170 | 139 |
171 const TestVector test_group_4[] = { | 140 const TestVector test_group_4[] = { |
172 { "da2bb7d581493d692380c77105590201", | 141 {"da2bb7d581493d692380c77105590201", "44aa3e7856ca279d2eb020c6", |
173 "44aa3e7856ca279d2eb020c6", | 142 "9290d430c9e89c37f0446dbd620c9a6b34b1274aeb6f911f75867efcf95b6feda69f1a" |
174 "9290d430c9e89c37f0446dbd620c9a6b34b1274aeb6f911f75867efcf95b6feda69f1a" | 143 "f4ee16c761b3c9aeac3da03aa9889c88", |
175 "f4ee16c761b3c9aeac3da03aa9889c88", | 144 "4cd171b23bddb3a53cdf959d5c1710b481eb3785a90eb20a2345ee00d0bb7868c367ab" |
176 "4cd171b23bddb3a53cdf959d5c1710b481eb3785a90eb20a2345ee00d0bb7868c367ab" | 145 "12e6f4dd1dee72af4eee1d197777d1d6499cc541f34edbf45cda6ef90b3c024f9272d7" |
177 "12e6f4dd1dee72af4eee1d197777d1d6499cc541f34edbf45cda6ef90b3c024f9272d7" | 146 "2ec1909fb8fba7db88a4d6f7d3d925980f9f9f72", |
178 "2ec1909fb8fba7db88a4d6f7d3d925980f9f9f72", | 147 "9e3ac938d3eb0cadd6f5c9e35d22ba38", |
179 "9e3ac938d3eb0cadd6f5c9e35d22ba38", | 148 "9bbf4c1a2742f6ac80cb4e8a052e4a8f4f07c43602361355b717381edf9fabd4cb7e3a" |
180 "9bbf4c1a2742f6ac80cb4e8a052e4a8f4f07c43602361355b717381edf9fabd4cb7e3a" | 149 "d65dbd1378b196ac270588dd0621f642"}, |
181 "d65dbd1378b196ac270588dd0621f642" | 150 {"d74e4958717a9d5c0e235b76a926cae8", "0b7471141e0c70b1995fd7b1", |
182 }, | 151 "e701c57d2330bf066f9ff8cf3ca4343cafe4894651cd199bdaaa681ba486b4a65c5a22" |
183 { "d74e4958717a9d5c0e235b76a926cae8", | 152 "b0f1420be29ea547d42c713bc6af66aa", |
184 "0b7471141e0c70b1995fd7b1", | 153 "4a42b7aae8c245c6f1598a395316e4b8484dbd6e64648d5e302021b1d3fa0a38f46e22" |
185 "e701c57d2330bf066f9ff8cf3ca4343cafe4894651cd199bdaaa681ba486b4a65c5a22" | 154 "bd9c8080b863dc0016482538a8562a4bd0ba84edbe2697c76fd039527ac179ec5506cf" |
186 "b0f1420be29ea547d42c713bc6af66aa", | 155 "34a6039312774cedebf4961f3978b14a26509f96", |
187 "4a42b7aae8c245c6f1598a395316e4b8484dbd6e64648d5e302021b1d3fa0a38f46e22" | 156 "e192c23cb036f0b31592989119eed55d", |
188 "bd9c8080b863dc0016482538a8562a4bd0ba84edbe2697c76fd039527ac179ec5506cf" | 157 "840d9fb95e32559fb3602e48590280a172ca36d9b49ab69510f5bd552bfab7a306f85f" |
189 "34a6039312774cedebf4961f3978b14a26509f96", | 158 "f0a34bc305b88b804c60b90add594a17"}, |
190 "e192c23cb036f0b31592989119eed55d", | 159 { |
191 "840d9fb95e32559fb3602e48590280a172ca36d9b49ab69510f5bd552bfab7a306f85f" | 160 "1986310c725ac94ecfe6422e75fc3ee7", "93ec4214fa8e6dc4e3afc775", |
192 "f0a34bc305b88b804c60b90add594a17" | 161 "b178ec72f85a311ac4168f42a4b2c23113fbea4b85f4b9dabb74e143eb1b8b0a361e02" |
193 }, | 162 "43edfd365b90d5b325950df0ada058f9", |
194 { "1986310c725ac94ecfe6422e75fc3ee7", | 163 "e80b88e62c49c958b5e0b8b54f532d9ff6aa84c8a40132e93e55b59fc24e8decf28463" |
195 "93ec4214fa8e6dc4e3afc775", | 164 "139f155d1e8ce4ee76aaeefcd245baa0fc519f83a5fb9ad9aa40c4b21126013f576c42" |
196 "b178ec72f85a311ac4168f42a4b2c23113fbea4b85f4b9dabb74e143eb1b8b0a361e02" | 165 "72c2cb136c8fd091cc4539877a5d1e72d607f960", |
197 "43edfd365b90d5b325950df0ada058f9", | 166 "8b347853f11d75e81e8a95010be81f17", |
198 "e80b88e62c49c958b5e0b8b54f532d9ff6aa84c8a40132e93e55b59fc24e8decf28463" | 167 NULL // FAIL |
199 "139f155d1e8ce4ee76aaeefcd245baa0fc519f83a5fb9ad9aa40c4b21126013f576c42" | 168 }, |
200 "72c2cb136c8fd091cc4539877a5d1e72d607f960", | 169 {NULL}}; |
201 "8b347853f11d75e81e8a95010be81f17", | |
202 NULL // FAIL | |
203 }, | |
204 { NULL } | |
205 }; | |
206 | 170 |
207 const TestVector test_group_5[] = { | 171 const TestVector test_group_5[] = { |
208 { "387218b246c1a8257748b56980e50c94", | 172 {"387218b246c1a8257748b56980e50c94", "dd7e014198672be39f95b69d", |
209 "dd7e014198672be39f95b69d", | 173 "cdba9e73eaf3d38eceb2b04a8d", "", "ecf90f4a47c9c626d6fb2c765d201556", |
210 "cdba9e73eaf3d38eceb2b04a8d", | 174 "48f5b426baca03064554cc2b30"}, |
211 "", | 175 {"294de463721e359863887c820524b3d4", "3338b35c9d57a5d28190e8c9", |
212 "ecf90f4a47c9c626d6fb2c765d201556", | 176 "2f46634e74b8e4c89812ac83b9", "", "dabd506764e68b82a7e720aa18da0abe", |
213 "48f5b426baca03064554cc2b30" | 177 "46a2e55c8e264df211bd112685"}, |
214 }, | 178 {"28ead7fd2179e0d12aa6d5d88c58c2dc", "5055347f18b4d5add0ae5c41", |
215 { "294de463721e359863887c820524b3d4", | 179 "142d8210c3fb84774cdbd0447a", "", "5fd321d9cdb01952dc85f034736c2a7d", |
216 "3338b35c9d57a5d28190e8c9", | 180 "3b95b981086ee73cc4d0cc1422"}, |
217 "2f46634e74b8e4c89812ac83b9", | 181 { |
218 "", | 182 "7d7b6c988137b8d470c57bf674a09c87", "9edf2aa970d016ac962e1fd8", |
219 "dabd506764e68b82a7e720aa18da0abe", | 183 "a85b66c3cb5eab91d5bdc8bc0e", "", "dc054efc01f3afd21d9c2484819f569a", |
220 "46a2e55c8e264df211bd112685" | 184 NULL // FAIL |
221 }, | 185 }, |
222 { "28ead7fd2179e0d12aa6d5d88c58c2dc", | 186 {NULL}}; |
223 "5055347f18b4d5add0ae5c41", | |
224 "142d8210c3fb84774cdbd0447a", | |
225 "", | |
226 "5fd321d9cdb01952dc85f034736c2a7d", | |
227 "3b95b981086ee73cc4d0cc1422" | |
228 }, | |
229 { "7d7b6c988137b8d470c57bf674a09c87", | |
230 "9edf2aa970d016ac962e1fd8", | |
231 "a85b66c3cb5eab91d5bdc8bc0e", | |
232 "", | |
233 "dc054efc01f3afd21d9c2484819f569a", | |
234 NULL // FAIL | |
235 }, | |
236 { NULL } | |
237 }; | |
238 | 187 |
239 const TestVector* const test_group_array[] = { | 188 const TestVector* const test_group_array[] = { |
240 test_group_0, | 189 test_group_0, test_group_1, test_group_2, |
241 test_group_1, | 190 test_group_3, test_group_4, test_group_5, |
242 test_group_2, | |
243 test_group_3, | |
244 test_group_4, | |
245 test_group_5, | |
246 }; | 191 }; |
247 | 192 |
248 } // namespace | 193 } // namespace |
249 | 194 |
250 namespace net { | 195 namespace net { |
251 namespace test { | 196 namespace test { |
252 | 197 |
253 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing | 198 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
254 // in an nonce and also to allocate the buffer needed for the plaintext. | 199 // in an nonce and also to allocate the buffer needed for the plaintext. |
255 QuicData* DecryptWithNonce(Aes128Gcm12Decrypter* decrypter, | 200 QuicData* DecryptWithNonce(Aes128Gcm12Decrypter* decrypter, |
256 StringPiece nonce, | 201 StringPiece nonce, |
257 StringPiece associated_data, | 202 StringPiece associated_data, |
258 StringPiece ciphertext) { | 203 StringPiece ciphertext) { |
259 size_t plaintext_size = ciphertext.length(); | 204 size_t plaintext_size = ciphertext.length(); |
260 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 205 scoped_ptr<char[]> plaintext(new char[plaintext_size]); |
261 | 206 |
262 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, | 207 if (!decrypter->Decrypt(nonce, |
| 208 associated_data, |
| 209 ciphertext, |
263 reinterpret_cast<unsigned char*>(plaintext.get()), | 210 reinterpret_cast<unsigned char*>(plaintext.get()), |
264 &plaintext_size)) { | 211 &plaintext_size)) { |
265 return NULL; | 212 return NULL; |
266 } | 213 } |
267 return new QuicData(plaintext.release(), plaintext_size, true); | 214 return new QuicData(plaintext.release(), plaintext_size, true); |
268 } | 215 } |
269 | 216 |
270 TEST(Aes128Gcm12DecrypterTest, Decrypt) { | 217 TEST(Aes128Gcm12DecrypterTest, Decrypt) { |
271 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 218 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
272 SCOPED_TRACE(i); | 219 SCOPED_TRACE(i); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 // the first 12. | 254 // the first 12. |
308 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Decrypter::kAuthTagSize), | 255 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Decrypter::kAuthTagSize), |
309 tag.length()); | 256 tag.length()); |
310 tag.resize(Aes128Gcm12Decrypter::kAuthTagSize); | 257 tag.resize(Aes128Gcm12Decrypter::kAuthTagSize); |
311 string ciphertext = ct + tag; | 258 string ciphertext = ct + tag; |
312 | 259 |
313 Aes128Gcm12Decrypter decrypter; | 260 Aes128Gcm12Decrypter decrypter; |
314 ASSERT_TRUE(decrypter.SetKey(key)); | 261 ASSERT_TRUE(decrypter.SetKey(key)); |
315 | 262 |
316 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 263 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
317 &decrypter, iv, | 264 &decrypter, |
| 265 iv, |
318 // This deliberately tests that the decrypter can handle an AAD that | 266 // This deliberately tests that the decrypter can handle an AAD that |
319 // is set to NULL, as opposed to a zero-length, non-NULL pointer. | 267 // is set to NULL, as opposed to a zero-length, non-NULL pointer. |
320 aad.length() ? aad : StringPiece(), ciphertext)); | 268 aad.length() ? aad : StringPiece(), |
| 269 ciphertext)); |
321 if (!decrypted.get()) { | 270 if (!decrypted.get()) { |
322 EXPECT_FALSE(has_pt); | 271 EXPECT_FALSE(has_pt); |
323 continue; | 272 continue; |
324 } | 273 } |
325 EXPECT_TRUE(has_pt); | 274 EXPECT_TRUE(has_pt); |
326 | 275 |
327 ASSERT_EQ(pt.length(), decrypted->length()); | 276 ASSERT_EQ(pt.length(), decrypted->length()); |
328 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 277 test::CompareCharArraysWithHexError( |
329 pt.length(), pt.data(), pt.length()); | 278 "plaintext", decrypted->data(), pt.length(), pt.data(), pt.length()); |
330 } | 279 } |
331 } | 280 } |
332 } | 281 } |
333 | 282 |
334 } // namespace test | 283 } // namespace test |
335 } // namespace net | 284 } // namespace net |
OLD | NEW |