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_decrypter.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/quic_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 13 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // The test vectors come from RFC 7539 Section 2.8.2. |
| 18 | |
| 19 | // Each test vector consists of six strings of lowercase hexadecimal digits. |
| 20 | // The strings may be empty (zero length). A test vector with a nullptr |key| |
| 21 | // marks the end of an array of test vectors. |
| 22 | struct TestVector { |
| 23 | // Input: |
| 24 | const char* key; |
| 25 | const char* iv; |
| 26 | const char* fixed; |
| 27 | const char* aad; |
| 28 | const char* ct; |
| 29 | |
| 30 | // Expected output: |
| 31 | const char* pt; // An empty string "" means decryption succeeded and |
| 32 | // the plaintext is zero-length. nullptr means decryption |
| 33 | // failed. |
| 34 | }; |
| 35 | |
| 36 | const TestVector test_vectors[] = { |
| 37 | {"808182838485868788898a8b8c8d8e8f" |
| 38 | "909192939495969798999a9b9c9d9e9f", |
| 39 | |
| 40 | "4041424344454647", |
| 41 | |
| 42 | "07000000", |
| 43 | |
| 44 | "50515253c0c1c2c3c4c5c6c7", |
| 45 | |
| 46 | "d31a8d34648e60db7b86afbc53ef7ec2" |
| 47 | "a4aded51296e08fea9e2b5a736ee62d6" |
| 48 | "3dbea45e8ca9671282fafb69da92728b" |
| 49 | "1a71de0a9e060b2905d6a5b67ecd3b36" |
| 50 | "92ddbd7f2d778b8c9803aee328091b58" |
| 51 | "fab324e4fad675945585808b4831d7bc" |
| 52 | "3ff4def08e4b7a9de576d26586cec64b" |
| 53 | "6116" |
| 54 | "1ae10b594f09e26a7e902ecb", // "d0600691" truncated |
| 55 | |
| 56 | "4c616469657320616e642047656e746c" |
| 57 | "656d656e206f662074686520636c6173" |
| 58 | "73206f66202739393a20496620492063" |
| 59 | "6f756c64206f6666657220796f75206f" |
| 60 | "6e6c79206f6e652074697020666f7220" |
| 61 | "746865206675747572652c2073756e73" |
| 62 | "637265656e20776f756c642062652069" |
| 63 | "742e"}, |
| 64 | // Modify the ciphertext (Poly1305 authenticator). |
| 65 | {"808182838485868788898a8b8c8d8e8f" |
| 66 | "909192939495969798999a9b9c9d9e9f", |
| 67 | |
| 68 | "4041424344454647", |
| 69 | |
| 70 | "07000000", |
| 71 | |
| 72 | "50515253c0c1c2c3c4c5c6c7", |
| 73 | |
| 74 | "d31a8d34648e60db7b86afbc53ef7ec2" |
| 75 | "a4aded51296e08fea9e2b5a736ee62d6" |
| 76 | "3dbea45e8ca9671282fafb69da92728b" |
| 77 | "1a71de0a9e060b2905d6a5b67ecd3b36" |
| 78 | "92ddbd7f2d778b8c9803aee328091b58" |
| 79 | "fab324e4fad675945585808b4831d7bc" |
| 80 | "3ff4def08e4b7a9de576d26586cec64b" |
| 81 | "6116" |
| 82 | "1ae10b594f09e26a7e902ecc", // "d0600691" truncated |
| 83 | |
| 84 | nullptr}, |
| 85 | // Modify the associated data. |
| 86 | {"808182838485868788898a8b8c8d8e8f" |
| 87 | "909192939495969798999a9b9c9d9e9f", |
| 88 | |
| 89 | "4041424344454647", |
| 90 | |
| 91 | "07000000", |
| 92 | |
| 93 | "60515253c0c1c2c3c4c5c6c7", |
| 94 | |
| 95 | "d31a8d34648e60db7b86afbc53ef7ec2" |
| 96 | "a4aded51296e08fea9e2b5a736ee62d6" |
| 97 | "3dbea45e8ca9671282fafb69da92728b" |
| 98 | "1a71de0a9e060b2905d6a5b67ecd3b36" |
| 99 | "92ddbd7f2d778b8c9803aee328091b58" |
| 100 | "fab324e4fad675945585808b4831d7bc" |
| 101 | "3ff4def08e4b7a9de576d26586cec64b" |
| 102 | "6116" |
| 103 | "1ae10b594f09e26a7e902ecb", // "d0600691" truncated |
| 104 | |
| 105 | nullptr}, |
| 106 | {nullptr}}; |
| 107 | |
| 108 | } // namespace |
| 109 | |
| 110 | namespace quic { |
| 111 | namespace test { |
| 112 | |
| 113 | // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
| 114 | // in an nonce and also to allocate the buffer needed for the plaintext. |
| 115 | QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
| 116 | QuicStringPiece nonce, |
| 117 | QuicStringPiece associated_data, |
| 118 | QuicStringPiece ciphertext) { |
| 119 | uint64_t packet_number; |
| 120 | QuicStringPiece nonce_prefix(nonce.data(), |
| 121 | nonce.size() - sizeof(packet_number)); |
| 122 | decrypter->SetNoncePrefix(nonce_prefix); |
| 123 | memcpy(&packet_number, nonce.data() + nonce_prefix.size(), |
| 124 | sizeof(packet_number)); |
| 125 | std::unique_ptr<char[]> output(new char[ciphertext.length()]); |
| 126 | size_t output_length = 0; |
| 127 | const bool success = decrypter->DecryptPacket( |
| 128 | packet_number, associated_data, ciphertext, output.get(), &output_length, |
| 129 | ciphertext.length()); |
| 130 | if (!success) { |
| 131 | return nullptr; |
| 132 | } |
| 133 | return new QuicData(output.release(), output_length, true); |
| 134 | } |
| 135 | |
| 136 | class ChaCha20Poly1305DecrypterTest : public QuicTest {}; |
| 137 | |
| 138 | TEST_F(ChaCha20Poly1305DecrypterTest, Decrypt) { |
| 139 | for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
| 140 | // If not present then decryption is expected to fail. |
| 141 | bool has_pt = test_vectors[i].pt; |
| 142 | |
| 143 | // Decode the test vector. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 144 | std::string key = QuicTextUtils::HexDecode(test_vectors[i].key); |
| 145 | std::string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); |
| 146 | std::string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed); |
| 147 | std::string aad = QuicTextUtils::HexDecode(test_vectors[i].aad); |
| 148 | std::string ct = QuicTextUtils::HexDecode(test_vectors[i].ct); |
| 149 | std::string pt; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 150 | if (has_pt) { |
| 151 | pt = QuicTextUtils::HexDecode(test_vectors[i].pt); |
| 152 | } |
| 153 | |
| 154 | ChaCha20Poly1305Decrypter decrypter; |
| 155 | ASSERT_TRUE(decrypter.SetKey(key)); |
| 156 | std::unique_ptr<QuicData> decrypted(DecryptWithNonce( |
| 157 | &decrypter, fixed + iv, |
| 158 | // This deliberately tests that the decrypter can handle an AAD that |
| 159 | // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
| 160 | QuicStringPiece(aad.length() ? aad.data() : nullptr, aad.length()), |
| 161 | ct)); |
| 162 | if (!decrypted.get()) { |
| 163 | EXPECT_FALSE(has_pt); |
| 164 | continue; |
| 165 | } |
| 166 | EXPECT_TRUE(has_pt); |
| 167 | |
| 168 | EXPECT_EQ(12u, ct.size() - decrypted->length()); |
| 169 | ASSERT_EQ(pt.length(), decrypted->length()); |
| 170 | test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
| 171 | pt.length(), pt.data(), pt.length()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | } // namespace test |
| 176 | } // namespace quic |