blob: 4ad9b2ad3824f6379b4a194102d71a18df9db3e9 [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_encrypter.h"
6
7#include "net/third_party/quiche/src/quic/core/quic_data_writer.h"
8#include "net/third_party/quiche/src/quic/core/quic_utils.h"
9
10namespace quic {
11
12const size_t kHashSizeShort = 12; // size of uint128 serialized short
13
14NullEncrypter::NullEncrypter(Perspective perspective)
15 : perspective_(perspective) {}
16
17bool NullEncrypter::SetKey(QuicStringPiece key) {
18 return key.empty();
19}
20
21bool NullEncrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) {
22 return nonce_prefix.empty();
23}
24
25bool NullEncrypter::SetIV(QuicStringPiece iv) {
26 return iv.empty();
27}
28
QUICHE team2d187972019-03-19 16:23:47 -070029bool NullEncrypter::SetHeaderProtectionKey(QuicStringPiece key) {
30 return key.empty();
31}
32
QUICHE teama6ef0a62019-03-07 20:34:33 -050033bool NullEncrypter::EncryptPacket(uint64_t /*packet_number*/,
34 QuicStringPiece associated_data,
35 QuicStringPiece plaintext,
36 char* output,
37 size_t* output_length,
38 size_t max_output_length) {
39 const size_t len = plaintext.size() + GetHashLength();
40 if (max_output_length < len) {
41 return false;
42 }
43 QuicUint128 hash;
44 if (perspective_ == Perspective::IS_SERVER) {
45 hash =
46 QuicUtils::FNV1a_128_Hash_Three(associated_data, plaintext, "Server");
47 } else {
48 hash =
49 QuicUtils::FNV1a_128_Hash_Three(associated_data, plaintext, "Client");
50 }
51 // TODO(ianswett): memmove required for in place encryption. Placing the
52 // hash at the end would allow use of memcpy, doing nothing for in place.
53 memmove(output + GetHashLength(), plaintext.data(), plaintext.length());
54 QuicUtils::SerializeUint128Short(hash,
55 reinterpret_cast<unsigned char*>(output));
56 *output_length = len;
57 return true;
58}
59
QUICHE team2d187972019-03-19 16:23:47 -070060std::string NullEncrypter::GenerateHeaderProtectionMask(
dschinazi17d42422019-06-18 16:35:07 -070061 QuicStringPiece /*sample*/) {
QUICHE team2d187972019-03-19 16:23:47 -070062 return std::string(5, 0);
63}
64
QUICHE teama6ef0a62019-03-07 20:34:33 -050065size_t NullEncrypter::GetKeySize() const {
66 return 0;
67}
68
69size_t NullEncrypter::GetNoncePrefixSize() const {
70 return 0;
71}
72
73size_t NullEncrypter::GetIVSize() const {
74 return 0;
75}
76
77size_t NullEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const {
78 return ciphertext_size - GetHashLength();
79}
80
81size_t NullEncrypter::GetCiphertextSize(size_t plaintext_size) const {
82 return plaintext_size + GetHashLength();
83}
84
85QuicStringPiece NullEncrypter::GetKey() const {
86 return QuicStringPiece();
87}
88
89QuicStringPiece NullEncrypter::GetNoncePrefix() const {
90 return QuicStringPiece();
91}
92
93size_t NullEncrypter::GetHashLength() const {
94 return kHashSizeShort;
95}
96
97} // namespace quic