QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/core/crypto/null_decrypter.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
| 7 | #include <cstdint> |
| 8 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 9 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 10 | #include "quic/core/quic_data_reader.h" |
| 11 | #include "quic/core/quic_utils.h" |
| 12 | #include "quic/platform/api/quic_bug_tracker.h" |
| 13 | #include "quic/platform/api/quic_uint128.h" |
| 14 | #include "common/quiche_endian.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | |
| 16 | namespace quic { |
| 17 | |
| 18 | NullDecrypter::NullDecrypter(Perspective perspective) |
| 19 | : perspective_(perspective) {} |
| 20 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 21 | bool NullDecrypter::SetKey(absl::string_view key) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | return key.empty(); |
| 23 | } |
| 24 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 25 | bool NullDecrypter::SetNoncePrefix(absl::string_view nonce_prefix) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | return nonce_prefix.empty(); |
| 27 | } |
| 28 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 29 | bool NullDecrypter::SetIV(absl::string_view iv) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | return iv.empty(); |
| 31 | } |
| 32 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 33 | bool NullDecrypter::SetHeaderProtectionKey(absl::string_view key) { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 34 | return key.empty(); |
| 35 | } |
| 36 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 37 | bool NullDecrypter::SetPreliminaryKey(absl::string_view /*key*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | QUIC_BUG << "Should not be called"; |
| 39 | return false; |
| 40 | } |
| 41 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 42 | bool NullDecrypter::SetDiversificationNonce( |
| 43 | const DiversificationNonce& /*nonce*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | QUIC_BUG << "Should not be called"; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool NullDecrypter::DecryptPacket(uint64_t /*packet_number*/, |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 49 | absl::string_view associated_data, |
| 50 | absl::string_view ciphertext, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 51 | char* output, |
| 52 | size_t* output_length, |
| 53 | size_t max_output_length) { |
| 54 | QuicDataReader reader(ciphertext.data(), ciphertext.length(), |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 55 | quiche::HOST_BYTE_ORDER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | QuicUint128 hash; |
| 57 | |
| 58 | if (!ReadHash(&reader, &hash)) { |
| 59 | return false; |
| 60 | } |
| 61 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 62 | absl::string_view plaintext = reader.ReadRemainingPayload(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | if (plaintext.length() > max_output_length) { |
| 64 | QUIC_BUG << "Output buffer must be larger than the plaintext."; |
| 65 | return false; |
| 66 | } |
| 67 | if (hash != ComputeHash(associated_data, plaintext)) { |
| 68 | return false; |
| 69 | } |
| 70 | // Copy the plaintext to output. |
| 71 | memcpy(output, plaintext.data(), plaintext.length()); |
| 72 | *output_length = plaintext.length(); |
| 73 | return true; |
| 74 | } |
| 75 | |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 76 | std::string NullDecrypter::GenerateHeaderProtectionMask( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 77 | QuicDataReader* /*sample_reader*/) { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 78 | return std::string(5, 0); |
| 79 | } |
| 80 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | size_t NullDecrypter::GetKeySize() const { |
| 82 | return 0; |
| 83 | } |
| 84 | |
nharper | 965e592 | 2019-09-23 22:33:54 -0700 | [diff] [blame] | 85 | size_t NullDecrypter::GetNoncePrefixSize() const { |
| 86 | return 0; |
| 87 | } |
| 88 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | size_t NullDecrypter::GetIVSize() const { |
| 90 | return 0; |
| 91 | } |
| 92 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 93 | absl::string_view NullDecrypter::GetKey() const { |
| 94 | return absl::string_view(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | } |
| 96 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 97 | absl::string_view NullDecrypter::GetNoncePrefix() const { |
| 98 | return absl::string_view(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | uint32_t NullDecrypter::cipher_id() const { |
| 102 | return 0; |
| 103 | } |
| 104 | |
mattm | 8238ffb | 2020-10-19 12:42:27 -0700 | [diff] [blame] | 105 | QuicPacketCount NullDecrypter::GetIntegrityLimit() const { |
| 106 | return std::numeric_limits<QuicPacketCount>::max(); |
| 107 | } |
| 108 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 109 | bool NullDecrypter::ReadHash(QuicDataReader* reader, QuicUint128* hash) { |
| 110 | uint64_t lo; |
| 111 | uint32_t hi; |
| 112 | if (!reader->ReadUInt64(&lo) || !reader->ReadUInt32(&hi)) { |
| 113 | return false; |
| 114 | } |
| 115 | *hash = MakeQuicUint128(hi, lo); |
| 116 | return true; |
| 117 | } |
| 118 | |
vasilvv | 778cad3 | 2020-10-08 12:43:32 -0700 | [diff] [blame] | 119 | QuicUint128 NullDecrypter::ComputeHash(const absl::string_view data1, |
| 120 | const absl::string_view data2) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | QuicUint128 correct_hash; |
| 122 | if (perspective_ == Perspective::IS_CLIENT) { |
| 123 | // Peer is a server. |
| 124 | correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Server"); |
| 125 | } else { |
| 126 | // Peer is a client. |
| 127 | correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Client"); |
| 128 | } |
| 129 | QuicUint128 mask = MakeQuicUint128(UINT64_C(0x0), UINT64_C(0xffffffff)); |
| 130 | mask <<= 96; |
| 131 | correct_hash &= ~mask; |
| 132 | return correct_hash; |
| 133 | } |
| 134 | |
| 135 | } // namespace quic |