blob: 5e0c9fb9fc03b4493857d7c3ebb784f1a79b0685 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 team5be974e2020-12-29 18:35:24 -05005#include "quic/core/crypto/null_decrypter.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05006
7#include <cstdint>
8
vasilvv778cad32020-10-08 12:43:32 -07009#include "absl/strings/string_view.h"
QUICHE team5be974e2020-12-29 18:35:24 -050010#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 teama6ef0a62019-03-07 20:34:33 -050015
16namespace quic {
17
18NullDecrypter::NullDecrypter(Perspective perspective)
19 : perspective_(perspective) {}
20
vasilvv778cad32020-10-08 12:43:32 -070021bool NullDecrypter::SetKey(absl::string_view key) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050022 return key.empty();
23}
24
vasilvv778cad32020-10-08 12:43:32 -070025bool NullDecrypter::SetNoncePrefix(absl::string_view nonce_prefix) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050026 return nonce_prefix.empty();
27}
28
vasilvv778cad32020-10-08 12:43:32 -070029bool NullDecrypter::SetIV(absl::string_view iv) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050030 return iv.empty();
31}
32
vasilvv778cad32020-10-08 12:43:32 -070033bool NullDecrypter::SetHeaderProtectionKey(absl::string_view key) {
QUICHE team2d187972019-03-19 16:23:47 -070034 return key.empty();
35}
36
vasilvv778cad32020-10-08 12:43:32 -070037bool NullDecrypter::SetPreliminaryKey(absl::string_view /*key*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 QUIC_BUG << "Should not be called";
39 return false;
40}
41
dschinazi17d42422019-06-18 16:35:07 -070042bool NullDecrypter::SetDiversificationNonce(
43 const DiversificationNonce& /*nonce*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050044 QUIC_BUG << "Should not be called";
45 return true;
46}
47
48bool NullDecrypter::DecryptPacket(uint64_t /*packet_number*/,
vasilvv778cad32020-10-08 12:43:32 -070049 absl::string_view associated_data,
50 absl::string_view ciphertext,
QUICHE teama6ef0a62019-03-07 20:34:33 -050051 char* output,
52 size_t* output_length,
53 size_t max_output_length) {
54 QuicDataReader reader(ciphertext.data(), ciphertext.length(),
QUICHE team173c48f2019-11-19 16:34:44 -080055 quiche::HOST_BYTE_ORDER);
QUICHE teama6ef0a62019-03-07 20:34:33 -050056 QuicUint128 hash;
57
58 if (!ReadHash(&reader, &hash)) {
59 return false;
60 }
61
vasilvv778cad32020-10-08 12:43:32 -070062 absl::string_view plaintext = reader.ReadRemainingPayload();
QUICHE teama6ef0a62019-03-07 20:34:33 -050063 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 team2d187972019-03-19 16:23:47 -070076std::string NullDecrypter::GenerateHeaderProtectionMask(
dschinazi17d42422019-06-18 16:35:07 -070077 QuicDataReader* /*sample_reader*/) {
QUICHE team2d187972019-03-19 16:23:47 -070078 return std::string(5, 0);
79}
80
QUICHE teama6ef0a62019-03-07 20:34:33 -050081size_t NullDecrypter::GetKeySize() const {
82 return 0;
83}
84
nharper965e5922019-09-23 22:33:54 -070085size_t NullDecrypter::GetNoncePrefixSize() const {
86 return 0;
87}
88
QUICHE teama6ef0a62019-03-07 20:34:33 -050089size_t NullDecrypter::GetIVSize() const {
90 return 0;
91}
92
vasilvv778cad32020-10-08 12:43:32 -070093absl::string_view NullDecrypter::GetKey() const {
94 return absl::string_view();
QUICHE teama6ef0a62019-03-07 20:34:33 -050095}
96
vasilvv778cad32020-10-08 12:43:32 -070097absl::string_view NullDecrypter::GetNoncePrefix() const {
98 return absl::string_view();
QUICHE teama6ef0a62019-03-07 20:34:33 -050099}
100
101uint32_t NullDecrypter::cipher_id() const {
102 return 0;
103}
104
mattm8238ffb2020-10-19 12:42:27 -0700105QuicPacketCount NullDecrypter::GetIntegrityLimit() const {
106 return std::numeric_limits<QuicPacketCount>::max();
107}
108
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109bool 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
vasilvv778cad32020-10-08 12:43:32 -0700119QuicUint128 NullDecrypter::ComputeHash(const absl::string_view data1,
120 const absl::string_view data2) const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121 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