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_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 | |
| 14 | namespace quic { |
| 15 | |
| 16 | NullDecrypter::NullDecrypter(Perspective perspective) |
| 17 | : perspective_(perspective) {} |
| 18 | |
| 19 | bool NullDecrypter::SetKey(QuicStringPiece key) { |
| 20 | return key.empty(); |
| 21 | } |
| 22 | |
| 23 | bool NullDecrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) { |
| 24 | return nonce_prefix.empty(); |
| 25 | } |
| 26 | |
| 27 | bool NullDecrypter::SetIV(QuicStringPiece iv) { |
| 28 | return iv.empty(); |
| 29 | } |
| 30 | |
| 31 | bool NullDecrypter::SetPreliminaryKey(QuicStringPiece key) { |
| 32 | QUIC_BUG << "Should not be called"; |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | bool NullDecrypter::SetDiversificationNonce(const DiversificationNonce& nonce) { |
| 37 | QUIC_BUG << "Should not be called"; |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | bool NullDecrypter::DecryptPacket(uint64_t /*packet_number*/, |
| 42 | QuicStringPiece associated_data, |
| 43 | QuicStringPiece ciphertext, |
| 44 | char* output, |
| 45 | size_t* output_length, |
| 46 | size_t max_output_length) { |
| 47 | QuicDataReader reader(ciphertext.data(), ciphertext.length(), |
| 48 | HOST_BYTE_ORDER); |
| 49 | QuicUint128 hash; |
| 50 | |
| 51 | if (!ReadHash(&reader, &hash)) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | QuicStringPiece plaintext = reader.ReadRemainingPayload(); |
| 56 | if (plaintext.length() > max_output_length) { |
| 57 | QUIC_BUG << "Output buffer must be larger than the plaintext."; |
| 58 | return false; |
| 59 | } |
| 60 | if (hash != ComputeHash(associated_data, plaintext)) { |
| 61 | return false; |
| 62 | } |
| 63 | // Copy the plaintext to output. |
| 64 | memcpy(output, plaintext.data(), plaintext.length()); |
| 65 | *output_length = plaintext.length(); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | size_t NullDecrypter::GetKeySize() const { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | size_t NullDecrypter::GetIVSize() const { |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | QuicStringPiece NullDecrypter::GetKey() const { |
| 78 | return QuicStringPiece(); |
| 79 | } |
| 80 | |
| 81 | QuicStringPiece NullDecrypter::GetNoncePrefix() const { |
| 82 | return QuicStringPiece(); |
| 83 | } |
| 84 | |
| 85 | uint32_t NullDecrypter::cipher_id() const { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | bool NullDecrypter::ReadHash(QuicDataReader* reader, QuicUint128* hash) { |
| 90 | uint64_t lo; |
| 91 | uint32_t hi; |
| 92 | if (!reader->ReadUInt64(&lo) || !reader->ReadUInt32(&hi)) { |
| 93 | return false; |
| 94 | } |
| 95 | *hash = MakeQuicUint128(hi, lo); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | QuicUint128 NullDecrypter::ComputeHash(const QuicStringPiece data1, |
| 100 | const QuicStringPiece data2) const { |
| 101 | QuicUint128 correct_hash; |
| 102 | if (perspective_ == Perspective::IS_CLIENT) { |
| 103 | // Peer is a server. |
| 104 | correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Server"); |
| 105 | } else { |
| 106 | // Peer is a client. |
| 107 | correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Client"); |
| 108 | } |
| 109 | QuicUint128 mask = MakeQuicUint128(UINT64_C(0x0), UINT64_C(0xffffffff)); |
| 110 | mask <<= 96; |
| 111 | correct_hash &= ~mask; |
| 112 | return correct_hash; |
| 113 | } |
| 114 | |
| 115 | } // namespace quic |