| 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_encrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_encrypter.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 five strings of lowercase hexadecimal digits. | 17 // Each test vector consists of five 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 59 } |
| 58 | 60 |
| 59 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { | 61 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { |
| 60 if (!ChaCha20Poly1305Encrypter::IsSupported()) { | 62 if (!ChaCha20Poly1305Encrypter::IsSupported()) { |
| 61 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; | 63 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; |
| 62 return; | 64 return; |
| 63 } | 65 } |
| 64 | 66 |
| 65 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | 67 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 66 // Decode the test vector. | 68 // Decode the test vector. |
| 67 string key; | 69 std::string key; |
| 68 string pt; | 70 std::string pt; |
| 69 string iv; | 71 std::string iv; |
| 70 string aad; | 72 std::string aad; |
| 71 string ct; | 73 std::string ct; |
| 72 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | 74 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); |
| 73 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 75 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
| 74 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 76 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
| 75 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 77 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
| 76 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 78 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
| 77 | 79 |
| 78 ChaCha20Poly1305Encrypter encrypter; | 80 ChaCha20Poly1305Encrypter encrypter; |
| 79 ASSERT_TRUE(encrypter.SetKey(key)); | 81 ASSERT_TRUE(encrypter.SetKey(key)); |
| 80 scoped_ptr<QuicData> encrypted(EncryptWithNonce( | 82 scoped_ptr<QuicData> encrypted(EncryptWithNonce( |
| 81 &encrypter, iv, | 83 &encrypter, iv, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 99 | 101 |
| 100 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { | 102 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { |
| 101 ChaCha20Poly1305Encrypter encrypter; | 103 ChaCha20Poly1305Encrypter encrypter; |
| 102 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 104 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
| 103 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 105 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
| 104 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 106 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
| 105 } | 107 } |
| 106 | 108 |
| 107 } // namespace test | 109 } // namespace test |
| 108 } // namespace net | 110 } // namespace net |
| OLD | NEW |