blob: 4934f6286a32de7b0a76ecc5dfb9773aed2da267 [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
5#include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h"
6
7#include <cstdint>
8
9#include "net/third_party/quiche/src/quic/core/quic_data_reader.h"
10#include "net/third_party/quiche/src/quic/core/quic_utils.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
13
14namespace quic {
15
16NullDecrypter::NullDecrypter(Perspective perspective)
17 : perspective_(perspective) {}
18
19bool NullDecrypter::SetKey(QuicStringPiece key) {
20 return key.empty();
21}
22
23bool NullDecrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) {
24 return nonce_prefix.empty();
25}
26
27bool NullDecrypter::SetIV(QuicStringPiece iv) {
28 return iv.empty();
29}
30
QUICHE team2d187972019-03-19 16:23:47 -070031bool NullDecrypter::SetHeaderProtectionKey(QuicStringPiece key) {
32 return key.empty();
33}
34
QUICHE teama6ef0a62019-03-07 20:34:33 -050035bool NullDecrypter::SetPreliminaryKey(QuicStringPiece key) {
36 QUIC_BUG << "Should not be called";
37 return false;
38}
39
40bool NullDecrypter::SetDiversificationNonce(const DiversificationNonce& nonce) {
41 QUIC_BUG << "Should not be called";
42 return true;
43}
44
45bool NullDecrypter::DecryptPacket(uint64_t /*packet_number*/,
46 QuicStringPiece associated_data,
47 QuicStringPiece ciphertext,
48 char* output,
49 size_t* output_length,
50 size_t max_output_length) {
51 QuicDataReader reader(ciphertext.data(), ciphertext.length(),
52 HOST_BYTE_ORDER);
53 QuicUint128 hash;
54
55 if (!ReadHash(&reader, &hash)) {
56 return false;
57 }
58
59 QuicStringPiece plaintext = reader.ReadRemainingPayload();
60 if (plaintext.length() > max_output_length) {
61 QUIC_BUG << "Output buffer must be larger than the plaintext.";
62 return false;
63 }
64 if (hash != ComputeHash(associated_data, plaintext)) {
65 return false;
66 }
67 // Copy the plaintext to output.
68 memcpy(output, plaintext.data(), plaintext.length());
69 *output_length = plaintext.length();
70 return true;
71}
72
QUICHE team2d187972019-03-19 16:23:47 -070073std::string NullDecrypter::GenerateHeaderProtectionMask(
74 QuicDataReader* sample_reader) {
75 return std::string(5, 0);
76}
77
QUICHE teama6ef0a62019-03-07 20:34:33 -050078size_t NullDecrypter::GetKeySize() const {
79 return 0;
80}
81
82size_t NullDecrypter::GetIVSize() const {
83 return 0;
84}
85
86QuicStringPiece NullDecrypter::GetKey() const {
87 return QuicStringPiece();
88}
89
90QuicStringPiece NullDecrypter::GetNoncePrefix() const {
91 return QuicStringPiece();
92}
93
94uint32_t NullDecrypter::cipher_id() const {
95 return 0;
96}
97
98bool NullDecrypter::ReadHash(QuicDataReader* reader, QuicUint128* hash) {
99 uint64_t lo;
100 uint32_t hi;
101 if (!reader->ReadUInt64(&lo) || !reader->ReadUInt32(&hi)) {
102 return false;
103 }
104 *hash = MakeQuicUint128(hi, lo);
105 return true;
106}
107
108QuicUint128 NullDecrypter::ComputeHash(const QuicStringPiece data1,
109 const QuicStringPiece data2) const {
110 QuicUint128 correct_hash;
111 if (perspective_ == Perspective::IS_CLIENT) {
112 // Peer is a server.
113 correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Server");
114 } else {
115 // Peer is a client.
116 correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Client");
117 }
118 QuicUint128 mask = MakeQuicUint128(UINT64_C(0x0), UINT64_C(0xffffffff));
119 mask <<= 96;
120 correct_hash &= ~mask;
121 return correct_hash;
122}
123
124} // namespace quic