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