| 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 <memory> | |
| 8 | |
| 9 #include "net/quic/crypto/chacha20_poly1305_decrypter.h" | |
| 10 #include "net/quic/quic_utils.h" | |
| 11 #include "net/quic/test_tools/quic_test_utils.h" | |
| 12 | |
| 13 using base::StringPiece; | |
| 14 using std::string; | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // The test vectors come from RFC 7539 Section 2.8.2. | |
| 19 | |
| 20 // Each test vector consists of five strings of lowercase hexadecimal digits. | |
| 21 // The strings may be empty (zero length). A test vector with a nullptr |key| | |
| 22 // marks the end of an array of test vectors. | |
| 23 struct TestVector { | |
| 24 const char* key; | |
| 25 const char* pt; | |
| 26 const char* iv; | |
| 27 const char* fixed; | |
| 28 const char* aad; | |
| 29 const char* ct; | |
| 30 }; | |
| 31 | |
| 32 const TestVector test_vectors[] = { | |
| 33 { | |
| 34 "808182838485868788898a8b8c8d8e8f" | |
| 35 "909192939495969798999a9b9c9d9e9f", | |
| 36 | |
| 37 "4c616469657320616e642047656e746c" | |
| 38 "656d656e206f662074686520636c6173" | |
| 39 "73206f66202739393a20496620492063" | |
| 40 "6f756c64206f6666657220796f75206f" | |
| 41 "6e6c79206f6e652074697020666f7220" | |
| 42 "746865206675747572652c2073756e73" | |
| 43 "637265656e20776f756c642062652069" | |
| 44 "742e", | |
| 45 | |
| 46 "4041424344454647", | |
| 47 | |
| 48 "07000000", | |
| 49 | |
| 50 "50515253c0c1c2c3c4c5c6c7", | |
| 51 | |
| 52 "d31a8d34648e60db7b86afbc53ef7ec2" | |
| 53 "a4aded51296e08fea9e2b5a736ee62d6" | |
| 54 "3dbea45e8ca9671282fafb69da92728b" | |
| 55 "1a71de0a9e060b2905d6a5b67ecd3b36" | |
| 56 "92ddbd7f2d778b8c9803aee328091b58" | |
| 57 "fab324e4fad675945585808b4831d7bc" | |
| 58 "3ff4def08e4b7a9de576d26586cec64b" | |
| 59 "6116" | |
| 60 "1ae10b594f09e26a7e902ecb", // "d0600691" truncated | |
| 61 }, | |
| 62 {nullptr}}; | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 namespace net { | |
| 67 namespace test { | |
| 68 | |
| 69 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing | |
| 70 // in an nonce and also to allocate the buffer needed for the ciphertext. | |
| 71 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, | |
| 72 StringPiece nonce, | |
| 73 StringPiece associated_data, | |
| 74 StringPiece plaintext) { | |
| 75 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); | |
| 76 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]); | |
| 77 | |
| 78 if (!encrypter->Encrypt(nonce, associated_data, plaintext, | |
| 79 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | |
| 80 return nullptr; | |
| 81 } | |
| 82 | |
| 83 return new QuicData(ciphertext.release(), ciphertext_size, true); | |
| 84 } | |
| 85 | |
| 86 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { | |
| 87 ChaCha20Poly1305Encrypter encrypter; | |
| 88 ChaCha20Poly1305Decrypter decrypter; | |
| 89 | |
| 90 string key = QuicUtils::HexDecode(test_vectors[0].key); | |
| 91 ASSERT_TRUE(encrypter.SetKey(key)); | |
| 92 ASSERT_TRUE(decrypter.SetKey(key)); | |
| 93 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); | |
| 94 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); | |
| 95 | |
| 96 QuicPathId path_id = 0x42; | |
| 97 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); | |
| 98 string associated_data = "associated_data"; | |
| 99 string plaintext = "plaintext"; | |
| 100 char encrypted[1024]; | |
| 101 size_t len; | |
| 102 ASSERT_TRUE(encrypter.EncryptPacket(path_id, packet_number, associated_data, | |
| 103 plaintext, encrypted, &len, | |
| 104 arraysize(encrypted))); | |
| 105 StringPiece ciphertext(encrypted, len); | |
| 106 char decrypted[1024]; | |
| 107 ASSERT_TRUE(decrypter.DecryptPacket(path_id, packet_number, associated_data, | |
| 108 ciphertext, decrypted, &len, | |
| 109 arraysize(decrypted))); | |
| 110 } | |
| 111 | |
| 112 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { | |
| 113 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | |
| 114 // Decode the test vector. | |
| 115 string key = QuicUtils::HexDecode(test_vectors[i].key); | |
| 116 string pt = QuicUtils::HexDecode(test_vectors[i].pt); | |
| 117 string iv = QuicUtils::HexDecode(test_vectors[i].iv); | |
| 118 string fixed = QuicUtils::HexDecode(test_vectors[i].fixed); | |
| 119 string aad = QuicUtils::HexDecode(test_vectors[i].aad); | |
| 120 string ct = QuicUtils::HexDecode(test_vectors[i].ct); | |
| 121 | |
| 122 ChaCha20Poly1305Encrypter encrypter; | |
| 123 ASSERT_TRUE(encrypter.SetKey(key)); | |
| 124 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( | |
| 125 &encrypter, fixed + iv, | |
| 126 // This deliberately tests that the encrypter can handle an AAD that | |
| 127 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | |
| 128 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); | |
| 129 ASSERT_TRUE(encrypted.get()); | |
| 130 EXPECT_EQ(12u, ct.size() - pt.size()); | |
| 131 EXPECT_EQ(12u, encrypted->length() - pt.size()); | |
| 132 | |
| 133 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), | |
| 134 encrypted->length(), ct.data(), | |
| 135 ct.length()); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { | |
| 140 ChaCha20Poly1305Encrypter encrypter; | |
| 141 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); | |
| 142 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); | |
| 143 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); | |
| 144 } | |
| 145 | |
| 146 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { | |
| 147 ChaCha20Poly1305Encrypter encrypter; | |
| 148 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | |
| 149 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | |
| 150 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | |
| 151 } | |
| 152 | |
| 153 } // namespace test | |
| 154 } // namespace net | |
| OLD | NEW |