blob: eb1e95fb98caf2461fd37d8279e9c4cf1f43601c [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"
13
14namespace quic {
15
16bool ChaChaBaseDecrypter::SetHeaderProtectionKey(QuicStringPiece key) {
17 if (key.size() != GetKeySize()) {
18 QUIC_BUG << "Invalid key size for header protection";
19 return false;
20 }
21 memcpy(pne_key_, key.data(), key.size());
22 return true;
23}
24
25std::string ChaChaBaseDecrypter::GenerateHeaderProtectionMask(
26 QuicDataReader* sample_reader) {
27 QuicStringPiece sample;
28 if (!sample_reader->ReadStringPiece(&sample, 16)) {
29 return std::string();
30 }
31 const uint8_t* nonce = reinterpret_cast<const uint8_t*>(sample.data()) + 4;
32 uint32_t counter;
33 QuicDataReader(sample.data(), 4, Endianness::HOST_BYTE_ORDER)
34 .ReadUInt32(&counter);
35 const uint8_t zeroes[] = {0, 0, 0, 0, 0};
rch3d925ab2019-03-20 16:09:54 -070036 std::string out(QUIC_ARRAYSIZE(zeroes), 0);
QUICHE team2d187972019-03-19 16:23:47 -070037 CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())),
rch3d925ab2019-03-20 16:09:54 -070038 zeroes, QUIC_ARRAYSIZE(zeroes), pne_key_, nonce, counter);
QUICHE team2d187972019-03-19 16:23:47 -070039 return out;
40}
41
42} // namespace quic