| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/quic/crypto/chacha20_poly1305_encrypter.h" | |
| 6 | |
| 7 #include "net/quic/test_tools/quic_test_utils.h" | |
| 8 | |
| 9 using base::StringPiece; | |
| 10 using std::string; | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // The test vectors come from draft-agl-tls-chacha20poly1305-04 Section 7. | |
| 15 | |
| 16 // Each test vector consists of five strings of lowercase hexadecimal digits. | |
| 17 // The strings may be empty (zero length). A test vector with a nullptr |key| | |
| 18 // marks the end of an array of test vectors. | |
| 19 struct TestVector { | |
| 20 const char* key; | |
| 21 const char* pt; | |
| 22 const char* iv; | |
| 23 const char* aad; | |
| 24 const char* ct; | |
| 25 }; | |
| 26 | |
| 27 const TestVector test_vectors[] = { | |
| 28 { | |
| 29 "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" | |
| 30 "0a1007", | |
| 31 "86d09974840bded2a5ca", "cd7cf67be39c794a", "87e229d4500845a079c0", | |
| 32 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475" // "3896e1d6" truncated. | |
| 33 }, | |
| 34 {nullptr}}; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 namespace net { | |
| 39 namespace test { | |
| 40 | |
| 41 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing | |
| 42 // in an nonce and also to allocate the buffer needed for the ciphertext. | |
| 43 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, | |
| 44 StringPiece nonce, | |
| 45 StringPiece associated_data, | |
| 46 StringPiece plaintext) { | |
| 47 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); | |
| 48 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); | |
| 49 | |
| 50 if (!encrypter->Encrypt(nonce, associated_data, plaintext, | |
| 51 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | |
| 52 return nullptr; | |
| 53 } | |
| 54 | |
| 55 return new QuicData(ciphertext.release(), ciphertext_size, true); | |
| 56 } | |
| 57 | |
| 58 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { | |
| 59 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | |
| 60 // Decode the test vector. | |
| 61 string key; | |
| 62 string pt; | |
| 63 string iv; | |
| 64 string aad; | |
| 65 string ct; | |
| 66 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | |
| 67 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | |
| 68 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | |
| 69 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | |
| 70 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | |
| 71 | |
| 72 ChaCha20Poly1305Encrypter encrypter; | |
| 73 ASSERT_TRUE(encrypter.SetKey(key)); | |
| 74 scoped_ptr<QuicData> encrypted(EncryptWithNonce( | |
| 75 &encrypter, iv, | |
| 76 // This deliberately tests that the encrypter can handle an AAD that | |
| 77 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | |
| 78 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); | |
| 79 ASSERT_TRUE(encrypted.get()); | |
| 80 | |
| 81 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), | |
| 82 encrypted->length(), ct.data(), | |
| 83 ct.length()); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { | |
| 88 ChaCha20Poly1305Encrypter encrypter; | |
| 89 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); | |
| 90 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); | |
| 91 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); | |
| 92 } | |
| 93 | |
| 94 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { | |
| 95 ChaCha20Poly1305Encrypter encrypter; | |
| 96 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | |
| 97 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | |
| 98 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | |
| 99 } | |
| 100 | |
| 101 } // namespace test | |
| 102 } // namespace net | |
| OLD | NEW |