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/crypto/null_decrypter.h" | 5 #include "net/quic/crypto/null_decrypter.h" |
6 #include "net/quic/quic_utils.h" | 6 #include "net/quic/quic_utils.h" |
7 #include "net/quic/quic_data_reader.h" | 7 #include "net/quic/quic_data_reader.h" |
8 | 8 |
9 using base::StringPiece; | 9 using base::StringPiece; |
10 using std::string; | 10 using std::string; |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 NullDecrypter::NullDecrypter() {} | 14 NullDecrypter::NullDecrypter() {} |
15 | 15 |
16 bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); } | 16 bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); } |
17 | 17 |
18 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | 18 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { |
19 return nonce_prefix.empty(); | 19 return nonce_prefix.empty(); |
20 } | 20 } |
21 | 21 |
22 QuicData* NullDecrypter::DecryptPacket(QuicPacketSequenceNumber /*seq_number*/, | 22 bool NullDecrypter::DecryptPacket(QuicPacketSequenceNumber /*seq_number*/, |
23 StringPiece associated_data, | 23 const StringPiece& associated_data, |
24 StringPiece ciphertext) { | 24 const StringPiece& ciphertext, |
25 // It's worth duplicating |Decrypt|, above, in order to save a copy by using | 25 char* output, |
26 // the shared-data QuicData constructor directly. | 26 size_t* output_length, |
| 27 size_t max_output_length) { |
27 QuicDataReader reader(ciphertext.data(), ciphertext.length()); | 28 QuicDataReader reader(ciphertext.data(), ciphertext.length()); |
| 29 uint128 hash; |
28 | 30 |
29 uint128 hash; | |
30 if (!ReadHash(&reader, &hash)) { | 31 if (!ReadHash(&reader, &hash)) { |
31 return nullptr; | 32 return false; |
32 } | 33 } |
33 | 34 |
34 StringPiece plaintext = reader.ReadRemainingPayload(); | 35 StringPiece plaintext = reader.ReadRemainingPayload(); |
35 | 36 if (plaintext.length() > max_output_length) { |
36 // TODO(rch): avoid buffer copy here | 37 LOG(DFATAL) << "Output buffer must be larger than the plaintext."; |
37 string buffer = associated_data.as_string(); | 38 return false; |
38 plaintext.AppendToString(&buffer); | |
39 | |
40 if (hash != ComputeHash(buffer)) { | |
41 return nullptr; | |
42 } | 39 } |
43 return new QuicData(plaintext.data(), plaintext.length()); | 40 if (hash != ComputeHash(associated_data, plaintext)) { |
| 41 return false; |
| 42 } |
| 43 // Copy the plaintext to output. |
| 44 memcpy(output, plaintext.data(), plaintext.length()); |
| 45 *output_length = plaintext.length(); |
| 46 return true; |
44 } | 47 } |
45 | 48 |
46 StringPiece NullDecrypter::GetKey() const { return StringPiece(); } | 49 StringPiece NullDecrypter::GetKey() const { return StringPiece(); } |
47 | 50 |
48 StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); } | 51 StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); } |
49 | 52 |
50 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) { | 53 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) { |
51 uint64 lo; | 54 uint64 lo; |
52 uint32 hi; | 55 uint32 hi; |
53 if (!reader->ReadUInt64(&lo) || | 56 if (!reader->ReadUInt64(&lo) || |
54 !reader->ReadUInt32(&hi)) { | 57 !reader->ReadUInt32(&hi)) { |
55 return false; | 58 return false; |
56 } | 59 } |
57 *hash = hi; | 60 *hash = hi; |
58 *hash <<= 64; | 61 *hash <<= 64; |
59 *hash += lo; | 62 *hash += lo; |
60 return true; | 63 return true; |
61 } | 64 } |
62 | 65 |
63 uint128 NullDecrypter::ComputeHash(const string& data) const { | 66 uint128 NullDecrypter::ComputeHash(const StringPiece& data1, |
64 uint128 correct_hash = QuicUtils::FNV1a_128_Hash(data.data(), data.length()); | 67 const StringPiece& data2) const { |
| 68 uint128 correct_hash = QuicUtils::FNV1a_128_Hash_Two( |
| 69 data1.data(), data1.length(), data2.data(), data2.length()); |
65 uint128 mask(GG_UINT64_C(0x0), GG_UINT64_C(0xffffffff)); | 70 uint128 mask(GG_UINT64_C(0x0), GG_UINT64_C(0xffffffff)); |
66 mask <<= 96; | 71 mask <<= 96; |
67 correct_hash &= ~mask; | 72 correct_hash &= ~mask; |
68 return correct_hash; | 73 return correct_hash; |
69 } | 74 } |
70 | 75 |
71 } // namespace net | 76 } // namespace net |
OLD | NEW |