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 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 261 |
262 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, | 262 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, |
263 reinterpret_cast<unsigned char*>(plaintext.get()), | 263 reinterpret_cast<unsigned char*>(plaintext.get()), |
264 &plaintext_size)) { | 264 &plaintext_size)) { |
265 return NULL; | 265 return NULL; |
266 } | 266 } |
267 return new QuicData(plaintext.release(), plaintext_size, true); | 267 return new QuicData(plaintext.release(), plaintext_size, true); |
268 } | 268 } |
269 | 269 |
270 TEST(Aes128Gcm12DecrypterTest, Decrypt) { | 270 TEST(Aes128Gcm12DecrypterTest, Decrypt) { |
271 if (!Aes128Gcm12Decrypter::IsSupported()) { | |
272 LOG(INFO) << "AES GCM not supported. Test skipped."; | |
273 return; | |
274 } | |
275 | |
276 string key; | |
277 string iv; | |
278 string ct; | |
279 string aad; | |
280 string tag; | |
281 string pt; | |
282 | |
283 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 271 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
284 SCOPED_TRACE(i); | 272 SCOPED_TRACE(i); |
285 const TestVector* test_vector = test_group_array[i]; | 273 const TestVector* test_vectors = test_group_array[i]; |
286 const TestGroupInfo& test_info = test_group_info[i]; | 274 const TestGroupInfo& test_info = test_group_info[i]; |
287 for (size_t j = 0; test_vector[j].key != NULL; j++) { | 275 for (size_t j = 0; test_vectors[j].key != NULL; j++) { |
288 // If not present then decryption is expected to fail. | 276 // If not present then decryption is expected to fail. |
289 bool has_pt = test_vector[j].pt; | 277 bool has_pt = test_vectors[j].pt; |
290 | 278 |
291 // Decode the test vector. | 279 // Decode the test vector. |
292 ASSERT_TRUE(DecodeHexString(test_vector[j].key, &key)); | 280 string key; |
293 ASSERT_TRUE(DecodeHexString(test_vector[j].iv, &iv)); | 281 string iv; |
294 ASSERT_TRUE(DecodeHexString(test_vector[j].ct, &ct)); | 282 string ct; |
295 ASSERT_TRUE(DecodeHexString(test_vector[j].aad, &aad)); | 283 string aad; |
296 ASSERT_TRUE(DecodeHexString(test_vector[j].tag, &tag)); | 284 string tag; |
297 if (has_pt) | 285 string pt; |
298 ASSERT_TRUE(DecodeHexString(test_vector[j].pt, &pt)); | 286 ASSERT_TRUE(DecodeHexString(test_vectors[j].key, &key)); |
| 287 ASSERT_TRUE(DecodeHexString(test_vectors[j].iv, &iv)); |
| 288 ASSERT_TRUE(DecodeHexString(test_vectors[j].ct, &ct)); |
| 289 ASSERT_TRUE(DecodeHexString(test_vectors[j].aad, &aad)); |
| 290 ASSERT_TRUE(DecodeHexString(test_vectors[j].tag, &tag)); |
| 291 if (has_pt) { |
| 292 ASSERT_TRUE(DecodeHexString(test_vectors[j].pt, &pt)); |
| 293 } |
299 | 294 |
300 // The test vector's lengths should look sane. Note that the lengths | 295 // The test vector's lengths should look sane. Note that the lengths |
301 // in |test_info| are in bits. | 296 // in |test_info| are in bits. |
302 EXPECT_EQ(test_info.key_len, key.size() * 8); | 297 EXPECT_EQ(test_info.key_len, key.size() * 8); |
303 EXPECT_EQ(test_info.iv_len, iv.size() * 8); | 298 EXPECT_EQ(test_info.iv_len, iv.size() * 8); |
304 EXPECT_EQ(test_info.pt_len, pt.size() * 8); | 299 EXPECT_EQ(test_info.pt_len, ct.size() * 8); |
305 EXPECT_EQ(test_info.aad_len, aad.size() * 8); | 300 EXPECT_EQ(test_info.aad_len, aad.size() * 8); |
306 EXPECT_EQ(test_info.tag_len, tag.size() * 8); | 301 EXPECT_EQ(test_info.tag_len, tag.size() * 8); |
307 if (has_pt) | 302 if (has_pt) { |
308 EXPECT_EQ(test_info.pt_len, pt.size() * 8); | 303 EXPECT_EQ(test_info.pt_len, pt.size() * 8); |
| 304 } |
309 | 305 |
310 // The test vectors have 16 byte authenticators but this code only uses | 306 // The test vectors have 16 byte authenticators but this code only uses |
311 // the first 12. | 307 // the first 12. |
312 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Decrypter::kAuthTagSize), | 308 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Decrypter::kAuthTagSize), |
313 tag.size()); | 309 tag.size()); |
314 string ciphertext = | 310 string ciphertext = |
315 ct + tag.substr(0, Aes128Gcm12Decrypter::kAuthTagSize); | 311 ct + tag.substr(0, Aes128Gcm12Decrypter::kAuthTagSize); |
316 | 312 |
317 Aes128Gcm12Decrypter decrypter; | 313 Aes128Gcm12Decrypter decrypter; |
318 ASSERT_TRUE(decrypter.SetKey(key)); | 314 ASSERT_TRUE(decrypter.SetKey(key)); |
319 | 315 |
320 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 316 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
321 &decrypter, iv, | 317 &decrypter, iv, |
322 // OpenSSL fails if NULL is set as the AAD, as opposed to a | 318 // This deliberately tests that the decrypter can handle an AAD that |
323 // zero-length, non-NULL pointer. | 319 // is set to NULL, as opposed to a zero-length, non-NULL pointer. |
324 aad.size() ? aad : StringPiece(), ciphertext)); | 320 aad.size() ? aad : StringPiece(), ciphertext)); |
325 if (!decrypted.get()) { | 321 if (!decrypted.get()) { |
326 EXPECT_FALSE(has_pt); | 322 EXPECT_FALSE(has_pt); |
327 continue; | 323 continue; |
328 } | 324 } |
329 EXPECT_TRUE(has_pt); | 325 EXPECT_TRUE(has_pt); |
330 | 326 |
331 ASSERT_EQ(pt.size(), decrypted->length()); | 327 ASSERT_EQ(pt.size(), decrypted->length()); |
332 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 328 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
333 pt.size(), pt.data(), pt.size()); | 329 pt.size(), pt.data(), pt.size()); |
334 } | 330 } |
335 } | 331 } |
336 } | 332 } |
337 | 333 |
338 } // namespace test | 334 } // namespace test |
339 } // namespace net | 335 } // namespace net |
OLD | NEW |