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 | |
| 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 | |
| 10 | namespace quic { |
| 11 | |
| 12 | const size_t kHashSizeShort = 12; // size of uint128 serialized short |
| 13 | |
| 14 | NullEncrypter::NullEncrypter(Perspective perspective) |
| 15 | : perspective_(perspective) {} |
| 16 | |
| 17 | bool NullEncrypter::SetKey(QuicStringPiece key) { |
| 18 | return key.empty(); |
| 19 | } |
| 20 | |
| 21 | bool NullEncrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) { |
| 22 | return nonce_prefix.empty(); |
| 23 | } |
| 24 | |
| 25 | bool NullEncrypter::SetIV(QuicStringPiece iv) { |
| 26 | return iv.empty(); |
| 27 | } |
| 28 | |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 29 | bool NullEncrypter::SetHeaderProtectionKey(QuicStringPiece key) { |
| 30 | return key.empty(); |
| 31 | } |
| 32 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | bool 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 team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 60 | std::string NullEncrypter::GenerateHeaderProtectionMask( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 61 | QuicStringPiece /*sample*/) { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 62 | return std::string(5, 0); |
| 63 | } |
| 64 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 65 | size_t NullEncrypter::GetKeySize() const { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | size_t NullEncrypter::GetNoncePrefixSize() const { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | size_t NullEncrypter::GetIVSize() const { |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | size_t NullEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { |
| 78 | return ciphertext_size - GetHashLength(); |
| 79 | } |
| 80 | |
| 81 | size_t NullEncrypter::GetCiphertextSize(size_t plaintext_size) const { |
| 82 | return plaintext_size + GetHashLength(); |
| 83 | } |
| 84 | |
| 85 | QuicStringPiece NullEncrypter::GetKey() const { |
| 86 | return QuicStringPiece(); |
| 87 | } |
| 88 | |
| 89 | QuicStringPiece NullEncrypter::GetNoncePrefix() const { |
| 90 | return QuicStringPiece(); |
| 91 | } |
| 92 | |
| 93 | size_t NullEncrypter::GetHashLength() const { |
| 94 | return kHashSizeShort; |
| 95 | } |
| 96 | |
| 97 | } // namespace quic |