OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/core/crypto/null_decrypter.h" | 5 #include "net/quic/core/crypto/null_decrypter.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 | 8 |
9 #include "net/base/int128.h" | 9 #include "net/base/int128.h" |
10 #include "net/quic/core/quic_data_reader.h" | 10 #include "net/quic/core/quic_data_reader.h" |
11 #include "net/quic/core/quic_utils.h" | 11 #include "net/quic/core/quic_utils.h" |
12 #include "net/quic/platform/api/quic_bug_tracker.h" | 12 #include "net/quic/platform/api/quic_bug_tracker.h" |
13 | 13 |
14 using base::StringPiece; | |
15 using std::string; | 14 using std::string; |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 | 17 |
19 NullDecrypter::NullDecrypter(Perspective perspective) | 18 NullDecrypter::NullDecrypter(Perspective perspective) |
20 : perspective_(perspective) {} | 19 : perspective_(perspective) {} |
21 | 20 |
22 bool NullDecrypter::SetKey(StringPiece key) { | 21 bool NullDecrypter::SetKey(QuicStringPiece key) { |
23 return key.empty(); | 22 return key.empty(); |
24 } | 23 } |
25 | 24 |
26 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | 25 bool NullDecrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) { |
27 return nonce_prefix.empty(); | 26 return nonce_prefix.empty(); |
28 } | 27 } |
29 | 28 |
30 bool NullDecrypter::SetPreliminaryKey(StringPiece key) { | 29 bool NullDecrypter::SetPreliminaryKey(QuicStringPiece key) { |
31 QUIC_BUG << "Should not be called"; | 30 QUIC_BUG << "Should not be called"; |
32 return false; | 31 return false; |
33 } | 32 } |
34 | 33 |
35 bool NullDecrypter::SetDiversificationNonce(const DiversificationNonce& nonce) { | 34 bool NullDecrypter::SetDiversificationNonce(const DiversificationNonce& nonce) { |
36 QUIC_BUG << "Should not be called"; | 35 QUIC_BUG << "Should not be called"; |
37 return true; | 36 return true; |
38 } | 37 } |
39 | 38 |
40 bool NullDecrypter::DecryptPacket(QuicVersion version, | 39 bool NullDecrypter::DecryptPacket(QuicVersion version, |
41 QuicPacketNumber /*packet_number*/, | 40 QuicPacketNumber /*packet_number*/, |
42 StringPiece associated_data, | 41 QuicStringPiece associated_data, |
43 StringPiece ciphertext, | 42 QuicStringPiece ciphertext, |
44 char* output, | 43 char* output, |
45 size_t* output_length, | 44 size_t* output_length, |
46 size_t max_output_length) { | 45 size_t max_output_length) { |
47 QuicDataReader reader(ciphertext.data(), ciphertext.length()); | 46 QuicDataReader reader(ciphertext.data(), ciphertext.length()); |
48 uint128 hash; | 47 uint128 hash; |
49 | 48 |
50 if (!ReadHash(&reader, &hash)) { | 49 if (!ReadHash(&reader, &hash)) { |
51 return false; | 50 return false; |
52 } | 51 } |
53 | 52 |
54 StringPiece plaintext = reader.ReadRemainingPayload(); | 53 QuicStringPiece plaintext = reader.ReadRemainingPayload(); |
55 if (plaintext.length() > max_output_length) { | 54 if (plaintext.length() > max_output_length) { |
56 QUIC_BUG << "Output buffer must be larger than the plaintext."; | 55 QUIC_BUG << "Output buffer must be larger than the plaintext."; |
57 return false; | 56 return false; |
58 } | 57 } |
59 if (hash != ComputeHash(version, associated_data, plaintext)) { | 58 if (hash != ComputeHash(version, associated_data, plaintext)) { |
60 return false; | 59 return false; |
61 } | 60 } |
62 // Copy the plaintext to output. | 61 // Copy the plaintext to output. |
63 memcpy(output, plaintext.data(), plaintext.length()); | 62 memcpy(output, plaintext.data(), plaintext.length()); |
64 *output_length = plaintext.length(); | 63 *output_length = plaintext.length(); |
65 return true; | 64 return true; |
66 } | 65 } |
67 | 66 |
68 StringPiece NullDecrypter::GetKey() const { | 67 QuicStringPiece NullDecrypter::GetKey() const { |
69 return StringPiece(); | 68 return QuicStringPiece(); |
70 } | 69 } |
71 | 70 |
72 StringPiece NullDecrypter::GetNoncePrefix() const { | 71 QuicStringPiece NullDecrypter::GetNoncePrefix() const { |
73 return StringPiece(); | 72 return QuicStringPiece(); |
74 } | 73 } |
75 | 74 |
76 const char* NullDecrypter::cipher_name() const { | 75 const char* NullDecrypter::cipher_name() const { |
77 return "NULL"; | 76 return "NULL"; |
78 } | 77 } |
79 | 78 |
80 uint32_t NullDecrypter::cipher_id() const { | 79 uint32_t NullDecrypter::cipher_id() const { |
81 return 0; | 80 return 0; |
82 } | 81 } |
83 | 82 |
84 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) { | 83 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) { |
85 uint64_t lo; | 84 uint64_t lo; |
86 uint32_t hi; | 85 uint32_t hi; |
87 if (!reader->ReadUInt64(&lo) || !reader->ReadUInt32(&hi)) { | 86 if (!reader->ReadUInt64(&lo) || !reader->ReadUInt32(&hi)) { |
88 return false; | 87 return false; |
89 } | 88 } |
90 *hash = MakeUint128(hi, lo); | 89 *hash = MakeUint128(hi, lo); |
91 return true; | 90 return true; |
92 } | 91 } |
93 | 92 |
94 uint128 NullDecrypter::ComputeHash(QuicVersion version, | 93 uint128 NullDecrypter::ComputeHash(QuicVersion version, |
95 const StringPiece data1, | 94 const QuicStringPiece data1, |
96 const StringPiece data2) const { | 95 const QuicStringPiece data2) const { |
97 uint128 correct_hash; | 96 uint128 correct_hash; |
98 if (version > QUIC_VERSION_36) { | 97 if (version > QUIC_VERSION_36) { |
99 if (perspective_ == Perspective::IS_CLIENT) { | 98 if (perspective_ == Perspective::IS_CLIENT) { |
100 // Peer is a server. | 99 // Peer is a server. |
101 correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Server"); | 100 correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Server"); |
102 | 101 |
103 } else { | 102 } else { |
104 // Peer is a client. | 103 // Peer is a client. |
105 correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Client"); | 104 correct_hash = QuicUtils::FNV1a_128_Hash_Three(data1, data2, "Client"); |
106 } | 105 } |
107 } else { | 106 } else { |
108 correct_hash = QuicUtils::FNV1a_128_Hash_Two(data1, data2); | 107 correct_hash = QuicUtils::FNV1a_128_Hash_Two(data1, data2); |
109 } | 108 } |
110 uint128 mask = MakeUint128(UINT64_C(0x0), UINT64_C(0xffffffff)); | 109 uint128 mask = MakeUint128(UINT64_C(0x0), UINT64_C(0xffffffff)); |
111 mask <<= 96; | 110 mask <<= 96; |
112 correct_hash &= ~mask; | 111 correct_hash &= ~mask; |
113 return correct_hash; | 112 return correct_hash; |
114 } | 113 } |
115 | 114 |
116 } // namespace net | 115 } // namespace net |
OLD | NEW |