blob: 1e0d80fc95b78f151c5b90f2f1ba256d80dd40f4 [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"
QUICHE team173c48f2019-11-19 16:34:44 -080011#include "net/third_party/quiche/src/common/platform/api/quiche_endian.h"
dmcardle904ef182019-12-13 08:34:33 -080012#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE team2d187972019-03-19 16:23:47 -070013
14namespace quic {
15
dmcardle904ef182019-12-13 08:34:33 -080016bool ChaChaBaseEncrypter::SetHeaderProtectionKey(
17 quiche::QuicheStringPiece key) {
QUICHE team2d187972019-03-19 16:23:47 -070018 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 ChaChaBaseEncrypter::GenerateHeaderProtectionMask(
dmcardle904ef182019-12-13 08:34:33 -080027 quiche::QuicheStringPiece sample) {
QUICHE team2d187972019-03-19 16:23:47 -070028 if (sample.size() != 16) {
29 return std::string();
30 }
31 const uint8_t* nonce = reinterpret_cast<const uint8_t*>(sample.data()) + 4;
32 uint32_t counter;
QUICHE team173c48f2019-11-19 16:34:44 -080033 QuicDataReader(sample.data(), 4, quiche::HOST_BYTE_ORDER)
QUICHE team2d187972019-03-19 16:23:47 -070034 .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