OLD | NEW |
| (Empty) |
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/quic/crypto/null_decrypter.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "net/quic/quic_bug_tracker.h" | |
10 #include "net/quic/quic_data_reader.h" | |
11 #include "net/quic/quic_utils.h" | |
12 | |
13 using base::StringPiece; | |
14 using std::string; | |
15 | |
16 namespace net { | |
17 | |
18 NullDecrypter::NullDecrypter() {} | |
19 | |
20 bool NullDecrypter::SetKey(StringPiece key) { | |
21 return key.empty(); | |
22 } | |
23 | |
24 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | |
25 return nonce_prefix.empty(); | |
26 } | |
27 | |
28 bool NullDecrypter::SetPreliminaryKey(StringPiece key) { | |
29 QUIC_BUG << "Should not be called"; | |
30 return false; | |
31 } | |
32 | |
33 bool NullDecrypter::SetDiversificationNonce(DiversificationNonce nonce) { | |
34 QUIC_BUG << "Should not be called"; | |
35 return true; | |
36 } | |
37 | |
38 bool NullDecrypter::DecryptPacket(QuicPathId /*path_id*/, | |
39 QuicPacketNumber /*packet_number*/, | |
40 StringPiece associated_data, | |
41 StringPiece ciphertext, | |
42 char* output, | |
43 size_t* output_length, | |
44 size_t max_output_length) { | |
45 QuicDataReader reader(ciphertext.data(), ciphertext.length()); | |
46 uint128 hash; | |
47 | |
48 if (!ReadHash(&reader, &hash)) { | |
49 return false; | |
50 } | |
51 | |
52 StringPiece plaintext = reader.ReadRemainingPayload(); | |
53 if (plaintext.length() > max_output_length) { | |
54 QUIC_BUG << "Output buffer must be larger than the plaintext."; | |
55 return false; | |
56 } | |
57 if (hash != ComputeHash(associated_data, plaintext)) { | |
58 return false; | |
59 } | |
60 // Copy the plaintext to output. | |
61 memcpy(output, plaintext.data(), plaintext.length()); | |
62 *output_length = plaintext.length(); | |
63 return true; | |
64 } | |
65 | |
66 StringPiece NullDecrypter::GetKey() const { | |
67 return StringPiece(); | |
68 } | |
69 | |
70 StringPiece NullDecrypter::GetNoncePrefix() const { | |
71 return StringPiece(); | |
72 } | |
73 | |
74 const char* NullDecrypter::cipher_name() const { | |
75 return "NULL"; | |
76 } | |
77 | |
78 uint32_t NullDecrypter::cipher_id() const { | |
79 return 0; | |
80 } | |
81 | |
82 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) { | |
83 uint64_t lo; | |
84 uint32_t hi; | |
85 if (!reader->ReadUInt64(&lo) || !reader->ReadUInt32(&hi)) { | |
86 return false; | |
87 } | |
88 *hash = hi; | |
89 *hash <<= 64; | |
90 *hash += lo; | |
91 return true; | |
92 } | |
93 | |
94 uint128 NullDecrypter::ComputeHash(const StringPiece data1, | |
95 const StringPiece data2) const { | |
96 uint128 correct_hash = QuicUtils::FNV1a_128_Hash_Two( | |
97 data1.data(), data1.length(), data2.data(), data2.length()); | |
98 uint128 mask(UINT64_C(0x0), UINT64_C(0xffffffff)); | |
99 mask <<= 96; | |
100 correct_hash &= ~mask; | |
101 return correct_hash; | |
102 } | |
103 | |
104 } // namespace net | |
OLD | NEW |