QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 1 | // 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" |
rch | 3d925ab | 2019-03-20 16:09:54 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | bool 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 | |
| 23 | std::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}; |
rch | 3d925ab | 2019-03-20 16:09:54 -0700 | [diff] [blame] | 33 | std::string out(QUIC_ARRAYSIZE(zeroes), 0); |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 34 | CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())), |
rch | 3d925ab | 2019-03-20 16:09:54 -0700 | [diff] [blame] | 35 | zeroes, QUIC_ARRAYSIZE(zeroes), pne_key_, nonce, counter); |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 36 | return out; |
| 37 | } |
| 38 | |
| 39 | } // namespace quic |