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