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
5 #include "quiche/quic/core/crypto/null_encrypter.h"
6
7 #include "absl/numeric/int128.h"
8 #include "absl/strings/string_view.h"
9 #include "quiche/quic/core/quic_data_writer.h"
10 #include "quiche/quic/core/quic_utils.h"
11
12 namespace quic {
13
14 const size_t kHashSizeShort = 12; // size of uint128 serialized short
15
NullEncrypter(Perspective perspective)16 NullEncrypter::NullEncrypter(Perspective perspective)
17 : perspective_(perspective) {}
18
SetKey(absl::string_view key)19 bool NullEncrypter::SetKey(absl::string_view key) { return key.empty(); }
20
SetNoncePrefix(absl::string_view nonce_prefix)21 bool NullEncrypter::SetNoncePrefix(absl::string_view nonce_prefix) {
22 return nonce_prefix.empty();
23 }
24
SetIV(absl::string_view iv)25 bool NullEncrypter::SetIV(absl::string_view iv) { return iv.empty(); }
26
SetHeaderProtectionKey(absl::string_view key)27 bool NullEncrypter::SetHeaderProtectionKey(absl::string_view key) {
28 return key.empty();
29 }
30
EncryptPacket(uint64_t,absl::string_view associated_data,absl::string_view plaintext,char * output,size_t * output_length,size_t max_output_length)31 bool NullEncrypter::EncryptPacket(uint64_t /*packet_number*/,
32 absl::string_view associated_data,
33 absl::string_view plaintext, char* output,
34 size_t* output_length,
35 size_t max_output_length) {
36 const size_t len = plaintext.size() + GetHashLength();
37 if (max_output_length < len) {
38 return false;
39 }
40 absl::uint128 hash;
41 if (perspective_ == Perspective::IS_SERVER) {
42 hash =
43 QuicUtils::FNV1a_128_Hash_Three(associated_data, plaintext, "Server");
44 } else {
45 hash =
46 QuicUtils::FNV1a_128_Hash_Three(associated_data, plaintext, "Client");
47 }
48 // TODO(ianswett): memmove required for in place encryption. Placing the
49 // hash at the end would allow use of memcpy, doing nothing for in place.
50 memmove(output + GetHashLength(), plaintext.data(), plaintext.length());
51 QuicUtils::SerializeUint128Short(hash,
52 reinterpret_cast<unsigned char*>(output));
53 *output_length = len;
54 return true;
55 }
56
GenerateHeaderProtectionMask(absl::string_view)57 std::string NullEncrypter::GenerateHeaderProtectionMask(
58 absl::string_view /*sample*/) {
59 return std::string(5, 0);
60 }
61
GetKeySize() const62 size_t NullEncrypter::GetKeySize() const { return 0; }
63
GetNoncePrefixSize() const64 size_t NullEncrypter::GetNoncePrefixSize() const { return 0; }
65
GetIVSize() const66 size_t NullEncrypter::GetIVSize() const { return 0; }
67
GetMaxPlaintextSize(size_t ciphertext_size) const68 size_t NullEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const {
69 return ciphertext_size - std::min(ciphertext_size, GetHashLength());
70 }
71
GetCiphertextSize(size_t plaintext_size) const72 size_t NullEncrypter::GetCiphertextSize(size_t plaintext_size) const {
73 return plaintext_size + GetHashLength();
74 }
75
GetConfidentialityLimit() const76 QuicPacketCount NullEncrypter::GetConfidentialityLimit() const {
77 return std::numeric_limits<QuicPacketCount>::max();
78 }
79
GetKey() const80 absl::string_view NullEncrypter::GetKey() const { return absl::string_view(); }
81
GetNoncePrefix() const82 absl::string_view NullEncrypter::GetNoncePrefix() const {
83 return absl::string_view();
84 }
85
GetHashLength() const86 size_t NullEncrypter::GetHashLength() const { return kHashSizeShort; }
87
88 } // namespace quic
89