blob: 04d902f4b4cc50e1af34b7e94c8a6976e70ddb10 [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_encrypter.h"
6
7#include "third_party/boringssl/src/include/openssl/chacha.h"
8#include "net/third_party/quiche/src/quic/core/quic_data_reader.h"
rch3d925ab2019-03-20 16:09:54 -07009#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
QUICHE team2d187972019-03-19 16:23:47 -070010#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
11
12namespace quic {
13
14bool ChaChaBaseEncrypter::SetHeaderProtectionKey(QuicStringPiece key) {
15 if (key.size() != GetKeySize()) {
16 QUIC_BUG << "Invalid key size for header protection";
17 return false;
18 }
19 memcpy(pne_key_, key.data(), key.size());
20 return true;
21}
22
23std::string ChaChaBaseEncrypter::GenerateHeaderProtectionMask(
24 QuicStringPiece sample) {
25 if (sample.size() != 16) {
26 return std::string();
27 }
28 const uint8_t* nonce = reinterpret_cast<const uint8_t*>(sample.data()) + 4;
29 uint32_t counter;
30 QuicDataReader(sample.data(), 4, Endianness::HOST_BYTE_ORDER)
31 .ReadUInt32(&counter);
32 const uint8_t zeroes[] = {0, 0, 0, 0, 0};
rch3d925ab2019-03-20 16:09:54 -070033 std::string out(QUIC_ARRAYSIZE(zeroes), 0);
QUICHE team2d187972019-03-19 16:23:47 -070034 CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())),
rch3d925ab2019-03-20 16:09:54 -070035 zeroes, QUIC_ARRAYSIZE(zeroes), pne_key_, nonce, counter);
QUICHE team2d187972019-03-19 16:23:47 -070036 return out;
37}
38
39} // namespace quic