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