| 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_rfc7539_encrypter.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "net/quic/crypto/chacha20_poly1305_rfc7539_decrypter.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(ChaCha20Poly1305Rfc7539Encrypter* 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(ChaCha20Poly1305Rfc7539EncrypterTest, EncryptThenDecrypt) { | |
| 87 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | |
| 88 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
| 93 ChaCha20Poly1305Rfc7539Decrypter decrypter; | |
| 94 | |
| 95 string key; | |
| 96 DecodeHexString(test_vectors[0].key, &key); | |
| 97 ASSERT_TRUE(encrypter.SetKey(key)); | |
| 98 ASSERT_TRUE(decrypter.SetKey(key)); | |
| 99 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); | |
| 100 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); | |
| 101 | |
| 102 QuicPathId path_id = 0x42; | |
| 103 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); | |
| 104 string associated_data = "associated_data"; | |
| 105 string plaintext = "plaintext"; | |
| 106 char encrypted[1024]; | |
| 107 size_t len; | |
| 108 ASSERT_TRUE(encrypter.EncryptPacket(path_id, packet_number, associated_data, | |
| 109 plaintext, encrypted, &len, | |
| 110 arraysize(encrypted))); | |
| 111 StringPiece ciphertext(encrypted, len); | |
| 112 char decrypted[1024]; | |
| 113 ASSERT_TRUE(decrypter.DecryptPacket(path_id, packet_number, associated_data, | |
| 114 ciphertext, decrypted, &len, | |
| 115 arraysize(decrypted))); | |
| 116 } | |
| 117 | |
| 118 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, Encrypt) { | |
| 119 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | |
| 120 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { | |
| 125 // Decode the test vector. | |
| 126 string key; | |
| 127 string pt; | |
| 128 string iv; | |
| 129 string fixed; | |
| 130 string aad; | |
| 131 string ct; | |
| 132 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | |
| 133 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | |
| 134 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | |
| 135 ASSERT_TRUE(DecodeHexString(test_vectors[i].fixed, &fixed)); | |
| 136 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | |
| 137 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | |
| 138 | |
| 139 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
| 140 ASSERT_TRUE(encrypter.SetKey(key)); | |
| 141 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( | |
| 142 &encrypter, fixed + iv, | |
| 143 // This deliberately tests that the encrypter can handle an AAD that | |
| 144 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. | |
| 145 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); | |
| 146 ASSERT_TRUE(encrypted.get()); | |
| 147 EXPECT_EQ(12u, ct.size() - pt.size()); | |
| 148 EXPECT_EQ(12u, encrypted->length() - pt.size()); | |
| 149 | |
| 150 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), | |
| 151 encrypted->length(), ct.data(), | |
| 152 ct.length()); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, GetMaxPlaintextSize) { | |
| 157 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | |
| 158 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
| 163 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); | |
| 164 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); | |
| 165 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); | |
| 166 } | |
| 167 | |
| 168 TEST(ChaCha20Poly1305Rfc7539EncrypterTest, GetCiphertextSize) { | |
| 169 if (!ChaCha20Poly1305Rfc7539Encrypter::IsSupported()) { | |
| 170 VLOG(1) << "ChaCha20+Poly1305 not supported. Test skipped."; | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 ChaCha20Poly1305Rfc7539Encrypter encrypter; | |
| 175 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | |
| 176 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | |
| 177 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | |
| 178 } | |
| 179 | |
| 180 } // namespace test | |
| 181 } // namespace net | |
| OLD | NEW |