blob: bc09b667d5e8bb3d926c17eadfb1879ff0a12288 [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"
11#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
bnc4e9283d2019-12-17 07:08:57 -080012#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
QUICHE team173c48f2019-11-19 16:34:44 -080013#include "net/third_party/quiche/src/common/platform/api/quiche_endian.h"
dmcardle904ef182019-12-13 08:34:33 -080014#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE team2d187972019-03-19 16:23:47 -070015
16namespace quic {
17
dmcardle904ef182019-12-13 08:34:33 -080018bool ChaChaBaseDecrypter::SetHeaderProtectionKey(
19 quiche::QuicheStringPiece key) {
QUICHE team2d187972019-03-19 16:23:47 -070020 if (key.size() != GetKeySize()) {
21 QUIC_BUG << "Invalid key size for header protection";
22 return false;
23 }
24 memcpy(pne_key_, key.data(), key.size());
25 return true;
26}
27
28std::string ChaChaBaseDecrypter::GenerateHeaderProtectionMask(
29 QuicDataReader* sample_reader) {
dmcardle904ef182019-12-13 08:34:33 -080030 quiche::QuicheStringPiece sample;
QUICHE team2d187972019-03-19 16:23:47 -070031 if (!sample_reader->ReadStringPiece(&sample, 16)) {
32 return std::string();
33 }
34 const uint8_t* nonce = reinterpret_cast<const uint8_t*>(sample.data()) + 4;
35 uint32_t counter;
QUICHE team173c48f2019-11-19 16:34:44 -080036 QuicDataReader(sample.data(), 4, quiche::HOST_BYTE_ORDER)
QUICHE team2d187972019-03-19 16:23:47 -070037 .ReadUInt32(&counter);
38 const uint8_t zeroes[] = {0, 0, 0, 0, 0};
bnc4e9283d2019-12-17 07:08:57 -080039 std::string out(QUICHE_ARRAYSIZE(zeroes), 0);
QUICHE team2d187972019-03-19 16:23:47 -070040 CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())),
bnc4e9283d2019-12-17 07:08:57 -080041 zeroes, QUICHE_ARRAYSIZE(zeroes), pne_key_, nonce, counter);
QUICHE team2d187972019-03-19 16:23:47 -070042 return out;
43}
44
45} // namespace quic