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" |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/common/platform/api/quiche_endian.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 12 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 13 | |
| 14 | namespace quic { |
| 15 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 16 | bool ChaChaBaseEncrypter::SetHeaderProtectionKey( |
| 17 | quiche::QuicheStringPiece key) { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 18 | 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 | |
| 26 | std::string ChaChaBaseEncrypter::GenerateHeaderProtectionMask( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 27 | quiche::QuicheStringPiece sample) { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 28 | 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 team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 33 | QuicDataReader(sample.data(), 4, quiche::HOST_BYTE_ORDER) |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 34 | .ReadUInt32(&counter); |
| 35 | const uint8_t zeroes[] = {0, 0, 0, 0, 0}; |
rch | 3d925ab | 2019-03-20 16:09:54 -0700 | [diff] [blame] | 36 | std::string out(QUIC_ARRAYSIZE(zeroes), 0); |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 37 | CRYPTO_chacha_20(reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())), |
rch | 3d925ab | 2019-03-20 16:09:54 -0700 | [diff] [blame] | 38 | zeroes, QUIC_ARRAYSIZE(zeroes), pne_key_, nonce, counter); |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 39 | return out; |
| 40 | } |
| 41 | |
| 42 | } // namespace quic |