1 // Copyright (c) 2013 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 "quiche/quic/core/crypto/chacha_base_decrypter.h"
6
7 #include <cstdint>
8
9 #include "absl/base/macros.h"
10 #include "absl/strings/string_view.h"
11 #include "openssl/chacha.h"
12 #include "quiche/quic/core/quic_data_reader.h"
13 #include "quiche/quic/platform/api/quic_bug_tracker.h"
14 #include "quiche/common/quiche_endian.h"
15
16 namespace quic {
17
SetHeaderProtectionKey(absl::string_view key)18 bool ChaChaBaseDecrypter::SetHeaderProtectionKey(absl::string_view key) {
19 if (key.size() != GetKeySize()) {
20 QUIC_BUG(quic_bug_10620_1) << "Invalid key size for header protection";
21 return false;
22 }
23 memcpy(pne_key_, key.data(), key.size());
24 return true;
25 }
26
GenerateHeaderProtectionMask(QuicDataReader * sample_reader)27 std::string ChaChaBaseDecrypter::GenerateHeaderProtectionMask(
28 QuicDataReader* sample_reader) {
29 absl::string_view sample;
30 if (!sample_reader->ReadStringPiece(&sample, 16)) {
31 return std::string();
32 }
33 const uint8_t* nonce = reinterpret_cast<const uint8_t*>(sample.data()) + 4;
34 uint32_t counter;
35 QuicDataReader(sample.data(), 4, quiche::HOST_BYTE_ORDER)
36 .ReadUInt32(&counter);
37 const uint8_t zeroes[] = {0, 0, 0, 0, 0};
38 std::string out(ABSL_ARRAYSIZE(zeroes), 0);
39 CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())),
40 zeroes, ABSL_ARRAYSIZE(zeroes), pne_key_, nonce, counter);
41 return out;
42 }
43
44 } // namespace quic
45