blob: c67fd89d4ef1d4c6c692182545144f4f40712b0c [file] [log] [blame]
QUICHE team2d187972019-03-19 16:23:47 -07001// 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 "net/third_party/quiche/src/quic/core/crypto/chacha_base_decrypter.h"
6
7#include <cstdint>
8
9#include "third_party/boringssl/src/include/openssl/chacha.h"
10#include "net/third_party/quiche/src/quic/core/quic_data_reader.h"
rch3d925ab2019-03-20 16:09:54 -070011#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
QUICHE team2d187972019-03-19 16:23:47 -070012#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
QUICHE team173c48f2019-11-19 16:34:44 -080013#include "net/third_party/quiche/src/common/platform/api/quiche_endian.h"
QUICHE team2d187972019-03-19 16:23:47 -070014
15namespace quic {
16
17bool ChaChaBaseDecrypter::SetHeaderProtectionKey(QuicStringPiece key) {
18 if (key.size() != GetKeySize()) {
19 QUIC_BUG << "Invalid key size for header protection";
20 return false;
21 }
22 memcpy(pne_key_, key.data(), key.size());
23 return true;
24}
25
26std::string ChaChaBaseDecrypter::GenerateHeaderProtectionMask(
27 QuicDataReader* sample_reader) {
28 QuicStringPiece sample;
29 if (!sample_reader->ReadStringPiece(&sample, 16)) {
30 return std::string();
31 }
32 const uint8_t* nonce = reinterpret_cast<const uint8_t*>(sample.data()) + 4;
33 uint32_t counter;
QUICHE team173c48f2019-11-19 16:34:44 -080034 QuicDataReader(sample.data(), 4, quiche::HOST_BYTE_ORDER)
QUICHE team2d187972019-03-19 16:23:47 -070035 .ReadUInt32(&counter);
36 const uint8_t zeroes[] = {0, 0, 0, 0, 0};
rch3d925ab2019-03-20 16:09:54 -070037 std::string out(QUIC_ARRAYSIZE(zeroes), 0);
QUICHE team2d187972019-03-19 16:23:47 -070038 CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())),
rch3d925ab2019-03-20 16:09:54 -070039 zeroes, QUIC_ARRAYSIZE(zeroes), pne_key_, nonce, counter);
QUICHE team2d187972019-03-19 16:23:47 -070040 return out;
41}
42
43} // namespace quic