| 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_decrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_decrypter.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "net/quic/test_tools/quic_test_utils.h" | 9 #include "net/quic/test_tools/quic_test_utils.h" |
| 8 | 10 |
| 9 using base::StringPiece; | 11 using base::StringPiece; |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 // The test vectors come from draft-agl-tls-chacha20poly1305-04 Section 7. | 15 // The test vectors come from draft-agl-tls-chacha20poly1305-04 Section 7. |
| 14 | 16 |
| 15 // Each test vector consists of six strings of lowercase hexadecimal digits. | 17 // Each test vector consists of six strings of lowercase hexadecimal digits. |
| 16 // The strings may be empty (zero length). A test vector with a NULL |key| | 18 // The strings may be empty (zero length). A test vector with a NULL |key| |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 if (!ChaCha20Poly1305Decrypter::IsSupported()) { | 91 if (!ChaCha20Poly1305Decrypter::IsSupported()) { |
| 90 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; | 92 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; |
| 91 return; | 93 return; |
| 92 } | 94 } |
| 93 | 95 |
| 94 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 96 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 95 // If not present then decryption is expected to fail. | 97 // If not present then decryption is expected to fail. |
| 96 bool has_pt = test_vectors[i].pt; | 98 bool has_pt = test_vectors[i].pt; |
| 97 | 99 |
| 98 // Decode the test vector. | 100 // Decode the test vector. |
| 99 string key; | 101 std::string key; |
| 100 string iv; | 102 std::string iv; |
| 101 string aad; | 103 std::string aad; |
| 102 string ct; | 104 std::string ct; |
| 103 string pt; | 105 std::string pt; |
| 104 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | 106 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); |
| 105 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 107 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
| 106 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 108 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
| 107 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 109 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
| 108 if (has_pt) { | 110 if (has_pt) { |
| 109 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 111 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
| 110 } | 112 } |
| 111 | 113 |
| 112 ChaCha20Poly1305Decrypter decrypter; | 114 ChaCha20Poly1305Decrypter decrypter; |
| 113 ASSERT_TRUE(decrypter.SetKey(key)); | 115 ASSERT_TRUE(decrypter.SetKey(key)); |
| 114 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 116 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
| 115 &decrypter, iv, | 117 &decrypter, iv, |
| 116 // This deliberately tests that the decrypter can handle an AAD that | 118 // This deliberately tests that the decrypter can handle an AAD that |
| 117 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | 119 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
| 118 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); | 120 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); |
| 119 if (!decrypted.get()) { | 121 if (!decrypted.get()) { |
| 120 EXPECT_FALSE(has_pt); | 122 EXPECT_FALSE(has_pt); |
| 121 continue; | 123 continue; |
| 122 } | 124 } |
| 123 EXPECT_TRUE(has_pt); | 125 EXPECT_TRUE(has_pt); |
| 124 | 126 |
| 125 ASSERT_EQ(pt.length(), decrypted->length()); | 127 ASSERT_EQ(pt.length(), decrypted->length()); |
| 126 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 128 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 127 pt.length(), pt.data(), pt.length()); | 129 pt.length(), pt.data(), pt.length()); |
| 128 } | 130 } |
| 129 } | 131 } |
| 130 | 132 |
| 131 } // namespace test | 133 } // namespace test |
| 132 } // namespace net | 134 } // namespace net |
| OLD | NEW |