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